|
14 | 14 |
|
15 | 15 | extern "C" { |
16 | 16 | bool nand_ReadSectors(sec_t sector, sec_t numSectors,void* buffer); |
17 | | - bool nand_WriteSectors(sec_t sector, sec_t numSectors,void* buffer); //!!! |
| 17 | + bool nand_WriteSectors(sec_t sector, sec_t numSectors,const void* buffer); //!!! |
18 | 18 | } |
19 | 19 |
|
20 | 20 | int menuTop = 5, statusTop = 18; |
@@ -228,54 +228,92 @@ void restoreNAND() { |
228 | 228 |
|
229 | 229 | if (!isDSiMode()) { |
230 | 230 | iprintf("Not a DSi or 3ds!\n"); |
231 | | - } else { |
| 231 | + return; |
| 232 | + } |
| 233 | + |
| 234 | + FILE *f = fopen(nand_type, "rb"); |
| 235 | + |
| 236 | + if (f == NULL) { |
| 237 | + iprintf("Failure opening %s\n", nand_type); |
| 238 | + return; |
| 239 | + } |
| 240 | + |
| 241 | + //Sanity checks |
| 242 | + // Size check |
| 243 | + fseek(f, 0, SEEK_END); |
| 244 | + size_t dump_size = ftell(f); |
| 245 | + if ( dump_size != (sizMB * 1024 * 1024) ) { |
| 246 | + iprintf("%s and NAND sizes\ndo not match.\nOperation aborted.", nand_type); |
| 247 | + fclose(f); |
| 248 | + return; |
| 249 | + } |
| 250 | + rewind(f); |
| 251 | + // MBR(decrypted image) check |
| 252 | + // Taken from TWLtool (https://github.com/WinterMute/twltool) |
| 253 | + struct { |
| 254 | + u8 code[446]; |
| 255 | + struct { |
| 256 | + u8 status; |
| 257 | + u8 start_chs[3]; |
| 258 | + u8 partition_type; |
| 259 | + u8 end_chs[3]; |
| 260 | + u32 start_sector; |
| 261 | + u32 num_sectors; |
| 262 | + } __attribute__((__packed__)) partition[4]; |
| 263 | + u8 signature[2]; |
| 264 | + } mbr; |
| 265 | + fread(&mbr, 1, 0x200, f); |
| 266 | + if(mbr.signature[0] == 0x55 || mbr.signature[1] == 0xAA) { |
| 267 | + iprintf("Found MBR in %s.\nImage is not encrypted.\nOperation aborted.", nand_type); |
| 268 | + fclose(f); |
| 269 | + return; |
| 270 | + } |
| 271 | + rewind(f); |
| 272 | + //Sanity checks end |
| 273 | + |
| 274 | + iprintf("Sure? NAND restore is DANGEROUS!"); |
| 275 | + iprintf("START + SELECT confirm\n"); |
| 276 | + iprintf("B to cancel\n"); |
| 277 | + |
| 278 | + while (1) { |
| 279 | + scanKeys(); |
| 280 | + int keys = keysHeld(); |
| 281 | + if ((keys & KEY_START) && (keys & KEY_SELECT))break; |
| 282 | + if (keys & KEY_B) { |
| 283 | + clearStatus(); |
| 284 | + fclose(f); |
| 285 | + return; |
| 286 | + } |
| 287 | + swiWaitForVBlank(); |
| 288 | + } |
| 289 | + |
| 290 | + clearStatus(); |
| 291 | + |
| 292 | + iprintf("Reading %s/%s\n\n", dirname, nand_type); |
| 293 | + size_t i; |
| 294 | + size_t sectors = 128; |
| 295 | + size_t blocks = (sizMB * 1024 * 1024) / (sectors * 512); |
| 296 | + for (i=0; i < blocks; i++) { |
232 | 297 |
|
233 | | - iprintf("Sure? NAND restore is DANGEROUS!"); |
234 | | - iprintf("START + SELECT confirm\n"); |
235 | | - iprintf("B to exit\n"); |
| 298 | + size_t read = fread(firmware_buffer, 1, 512 * sectors, f); |
236 | 299 |
|
237 | | - while(1){ |
238 | | - scanKeys(); |
239 | | - int keys = keysHeld(); |
240 | | - if((keys & KEY_START) && (keys & KEY_SELECT))break; |
241 | | - if(keys & KEY_B){ |
242 | | - clearStatus(); |
243 | | - return; |
244 | | - } |
245 | | - swiWaitForVBlank(); |
| 300 | + if(read != 512 * sectors) { |
| 301 | + iprintf("\nError reading SD!\n"); |
| 302 | + break; |
246 | 303 | } |
247 | 304 |
|
248 | | - clearStatus(); |
249 | | - |
250 | | - FILE *f = fopen(nand_type, "rb"); |
251 | | - |
252 | | - if (NULL == f) { |
253 | | - iprintf("failure creating %s\n", nand_type); |
254 | | - } else { |
255 | | - iprintf("Reading %s/%s\n\n", dirname, nand_type); |
256 | | - size_t i; |
257 | | - size_t sectors = 128; |
258 | | - size_t blocks = (sizMB * 1024 * 1024) / (sectors * 512); |
259 | | - for (i=0; i < blocks; i++) { |
260 | | - |
261 | | - size_t read = fread(firmware_buffer, 1, 512 * sectors, f); |
262 | | - |
263 | | - if(read != 512 * sectors) { |
264 | | - iprintf("\nError reading SD!\n"); |
265 | | - break; |
266 | | - } |
267 | | - |
268 | | - if(!nand_WriteSectors(i * sectors,sectors,firmware_buffer)) { |
269 | | - iprintf("\nError writing NAND!\n"); |
270 | | - break; |
271 | | - } |
272 | | - |
273 | | - iprintf("%d/%d DON'T poweroff!\r", i+1, blocks); |
274 | | - } |
275 | | - fclose(f); |
| 305 | + if(!nand_WriteSectors(i * sectors,sectors,firmware_buffer)) { |
| 306 | + iprintf("\nError writing NAND!\n"); |
| 307 | + break; |
276 | 308 | } |
| 309 | + |
| 310 | + iprintf("%d/%d DON'T poweroff!\r", i+1, blocks); |
277 | 311 | } |
278 | | - |
| 312 | + fclose(f); |
| 313 | + |
| 314 | + clearStatus(); |
| 315 | + iprintf("Restore %s success.\nYou may now Exit and restart\nyour console.",nand_type); |
| 316 | + |
279 | 317 | } |
280 | 318 |
|
281 | 319 | void dumpCID(){ |
|
0 commit comments