1 | #include "vmc.h"
|
---|
2 |
|
---|
3 |
|
---|
4 | // Functions specific to the ps2
|
---|
5 |
|
---|
6 | int readPage ( int fd, unsigned char* page, unsigned int pagenum )
|
---|
7 | {
|
---|
8 |
|
---|
9 | int unit;
|
---|
10 | unsigned int position;
|
---|
11 |
|
---|
12 | unit = ( fd == g_Vmc_Image[ 0 ].fd ) ? 0 : 1;
|
---|
13 |
|
---|
14 | if ( g_Vmc_Image[ unit ].ecc_flag )
|
---|
15 | {
|
---|
16 |
|
---|
17 | position = pagenum * ( g_Vmc_Image[ unit ].header.page_size + 0x10 );
|
---|
18 |
|
---|
19 | }
|
---|
20 | else
|
---|
21 | {
|
---|
22 |
|
---|
23 | position = pagenum * g_Vmc_Image[ unit ].header.page_size;
|
---|
24 |
|
---|
25 | }
|
---|
26 |
|
---|
27 | lseek ( fd, position, SEEK_SET );
|
---|
28 | read ( fd, page, g_Vmc_Image[ unit ].header.page_size );
|
---|
29 |
|
---|
30 | return 0;
|
---|
31 |
|
---|
32 | }
|
---|
33 |
|
---|
34 | int readCluster ( int fd, unsigned char* cluster, unsigned int clusternum )
|
---|
35 | {
|
---|
36 |
|
---|
37 | int i, unit;
|
---|
38 | char* Page_Data;
|
---|
39 | unsigned int Page_Num;
|
---|
40 |
|
---|
41 | unit = ( fd == g_Vmc_Image[ 0 ].fd ) ? 0 : 1;
|
---|
42 |
|
---|
43 | Page_Num = clusternum * g_Vmc_Image[ unit ].header.pages_per_cluster;
|
---|
44 |
|
---|
45 | Page_Data = ( char* )malloc ( ( g_Vmc_Image[ unit ].header.page_size + 0xFF ) & ~( unsigned int )0xFF );
|
---|
46 |
|
---|
47 | for ( i = 0; i < g_Vmc_Image[ unit ].header.pages_per_cluster; i++ )
|
---|
48 | {
|
---|
49 |
|
---|
50 | memset ( Page_Data, g_Vmc_Image[ unit ].erase_byte, g_Vmc_Image[ unit ].header.page_size );
|
---|
51 |
|
---|
52 | readPage ( fd, Page_Data, Page_Num + i );
|
---|
53 |
|
---|
54 | memcpy ( cluster + ( i * g_Vmc_Image[ unit ].header.page_size ), Page_Data, g_Vmc_Image[ unit ].header.page_size );
|
---|
55 |
|
---|
56 | }
|
---|
57 |
|
---|
58 | free( Page_Data );
|
---|
59 |
|
---|
60 | return 0;
|
---|
61 |
|
---|
62 | }
|
---|
63 |
|
---|
64 | int writePage ( int fd, unsigned char* page, unsigned int pagenum )
|
---|
65 | {
|
---|
66 |
|
---|
67 | int unit;
|
---|
68 | unsigned int position;
|
---|
69 |
|
---|
70 | unit = ( fd == g_Vmc_Image[ 0 ].fd ) ? 0 : 1;
|
---|
71 |
|
---|
72 | if ( g_Vmc_Image[ unit ].ecc_flag )
|
---|
73 | {
|
---|
74 |
|
---|
75 | position = pagenum * ( g_Vmc_Image[ unit ].header.page_size + 0x10 );
|
---|
76 |
|
---|
77 | }
|
---|
78 | else
|
---|
79 | {
|
---|
80 |
|
---|
81 | position = pagenum * g_Vmc_Image[ unit ].header.page_size;
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | lseek ( fd, position, SEEK_SET );
|
---|
86 | write ( fd, page, g_Vmc_Image[ unit ].header.page_size );
|
---|
87 |
|
---|
88 | if ( g_Vmc_Image[ unit ].ecc_flag )
|
---|
89 | {
|
---|
90 |
|
---|
91 | char* ECC_Data;
|
---|
92 |
|
---|
93 | ECC_Data = ( char* )malloc ( ( 0x10 + 0xFF ) & ~( unsigned int )0xFF );
|
---|
94 |
|
---|
95 | buildECC ( unit, page, ECC_Data );
|
---|
96 | write ( fd, ECC_Data, 0x10 );
|
---|
97 |
|
---|
98 | free( ECC_Data );
|
---|
99 |
|
---|
100 | }
|
---|
101 |
|
---|
102 | return 0;
|
---|
103 |
|
---|
104 | }
|
---|
105 |
|
---|
106 | int writeCluster ( int fd, unsigned char* cluster, unsigned int clusternum )
|
---|
107 | {
|
---|
108 |
|
---|
109 | int i, unit;
|
---|
110 | char* Page_Data;
|
---|
111 | unsigned int Page_Num;
|
---|
112 |
|
---|
113 | unit = ( fd == g_Vmc_Image[ 0 ].fd ) ? 0 : 1;
|
---|
114 |
|
---|
115 | Page_Num = clusternum * g_Vmc_Image[ unit ].header.pages_per_cluster;
|
---|
116 |
|
---|
117 | Page_Data = ( char* )malloc ( ( g_Vmc_Image[ unit ].header.page_size + 0xFF ) & ~( unsigned int )0xFF );
|
---|
118 |
|
---|
119 | for ( i = 0; i < g_Vmc_Image[ unit ].header.pages_per_cluster; i++ )
|
---|
120 | {
|
---|
121 |
|
---|
122 | memset ( Page_Data, g_Vmc_Image[ unit ].erase_byte, g_Vmc_Image[ unit ].header.page_size );
|
---|
123 | memcpy ( Page_Data, cluster + ( i * g_Vmc_Image[ unit ].header.page_size ), g_Vmc_Image[ unit ].header.page_size );
|
---|
124 |
|
---|
125 | writePage ( fd, Page_Data, Page_Num + i );
|
---|
126 |
|
---|
127 | }
|
---|
128 |
|
---|
129 | free( Page_Data );
|
---|
130 |
|
---|
131 | return 0;
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | int writeClusterPart ( int fd, unsigned char* cluster, unsigned int clusternum, int cluster_offset, int size )
|
---|
136 | {
|
---|
137 |
|
---|
138 | int i, unit;
|
---|
139 | char* Page_Data;
|
---|
140 | unsigned int Page_Num, Page_offset;
|
---|
141 |
|
---|
142 | unit = ( fd == g_Vmc_Image[ 0 ].fd ) ? 0 : 1;
|
---|
143 |
|
---|
144 | Page_Num = clusternum * g_Vmc_Image[ unit ].header.pages_per_cluster;
|
---|
145 | Page_offset = cluster_offset;
|
---|
146 |
|
---|
147 | Page_Data = ( char* )malloc ( ( g_Vmc_Image[ unit ].header.page_size + 0xFF ) & ~( unsigned int )0xFF );
|
---|
148 |
|
---|
149 | for ( i = 0; i < g_Vmc_Image[ unit ].header.pages_per_cluster; i++ )
|
---|
150 | {
|
---|
151 |
|
---|
152 | if ( ( cluster_offset >= ( g_Vmc_Image[ unit ].header.page_size * i ) ) && ( cluster_offset < ( g_Vmc_Image[ unit ].header.page_size * ( i + 1 ) ) ) )
|
---|
153 | {
|
---|
154 |
|
---|
155 | Page_Num += i;
|
---|
156 | Page_offset = cluster_offset - g_Vmc_Image[ unit ].header.page_size * i;
|
---|
157 |
|
---|
158 | memset ( Page_Data, g_Vmc_Image[ unit ].erase_byte, g_Vmc_Image[ unit ].header.page_size );
|
---|
159 | readPage ( fd, Page_Data, Page_Num );
|
---|
160 |
|
---|
161 | if ( size > g_Vmc_Image[ unit ].header.page_size - Page_offset )
|
---|
162 | {
|
---|
163 |
|
---|
164 | memcpy ( Page_Data + Page_offset, cluster, g_Vmc_Image[ unit ].header.page_size - Page_offset );
|
---|
165 | writePage ( fd, Page_Data, Page_Num );
|
---|
166 |
|
---|
167 | Page_Num++;
|
---|
168 |
|
---|
169 | memset ( Page_Data, g_Vmc_Image[ unit ].erase_byte, g_Vmc_Image[ unit ].header.page_size );
|
---|
170 | readPage ( fd, Page_Data, Page_Num );
|
---|
171 |
|
---|
172 | memcpy ( Page_Data, cluster + ( g_Vmc_Image[ unit ].header.page_size - Page_offset ), size - ( g_Vmc_Image[ unit ].header.page_size - Page_offset ) );
|
---|
173 | writePage ( fd, Page_Data, Page_Num );
|
---|
174 |
|
---|
175 | }
|
---|
176 | else
|
---|
177 | {
|
---|
178 |
|
---|
179 | memcpy ( Page_Data + Page_offset, cluster, size );
|
---|
180 | writePage ( fd, Page_Data, Page_Num );
|
---|
181 |
|
---|
182 | }
|
---|
183 |
|
---|
184 | }
|
---|
185 |
|
---|
186 | }
|
---|
187 |
|
---|
188 | free( Page_Data );
|
---|
189 |
|
---|
190 | return 0;
|
---|
191 |
|
---|
192 | }
|
---|
193 |
|
---|
194 | int eraseBlock ( int fd, unsigned int block )
|
---|
195 | {
|
---|
196 |
|
---|
197 | int i, unit;
|
---|
198 | char* Page_Data;
|
---|
199 | unsigned int Page_Num;
|
---|
200 |
|
---|
201 | unit = ( fd == g_Vmc_Image[ 0 ].fd ) ? 0 : 1;
|
---|
202 |
|
---|
203 | Page_Data = ( char* )malloc ( ( g_Vmc_Image[ unit ].header.page_size + 0xFF ) & ~( unsigned int )0xFF );
|
---|
204 |
|
---|
205 | Page_Num = block * g_Vmc_Image[ unit ].header.pages_per_block;
|
---|
206 |
|
---|
207 | memset ( Page_Data, g_Vmc_Image[ unit ].erase_byte, g_Vmc_Image[ unit ].header.page_size );
|
---|
208 |
|
---|
209 | for ( i = 0; i < g_Vmc_Image[ unit ].header.pages_per_block; i++ )
|
---|
210 | {
|
---|
211 |
|
---|
212 | writePage ( fd, Page_Data, Page_Num + i );
|
---|
213 |
|
---|
214 | }
|
---|
215 |
|
---|
216 | free( Page_Data );
|
---|
217 |
|
---|
218 | return 0;
|
---|
219 |
|
---|
220 | }
|
---|