-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathraw.c
746 lines (667 loc) · 20.2 KB
/
raw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#include <gccore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <malloc.h>
#include <sys/dir.h>
#include <dirent.h>
#include <ogcsys.h>
#include <ogc/libversion.h>
#if (_V_MAJOR_ <= 2) && (_V_MINOR_ <= 2)
#ifndef ogc_card_sync
#define ogc_card_sync __card_sync
#endif
#ifndef ogc_card_read
#define ogc_card_read __card_read
#endif
#ifndef ogc_card_sectorerase
#define ogc_card_sectorerase __card_sectorerase
#endif
#ifndef ogc_card_write
#define ogc_card_write __card_write
#endif
#include "card.h"
extern s32 __card_write(s32 chn,u32 address,u32 block_len,void *buffer,cardcallback callback);
extern s32 __card_sectorerase(s32 chn,u32 sector,cardcallback callback);
extern s32 __card_sync(s32 chn);
extern s32 __card_read(s32 chn,u32 address,u32 block_len,void *buffer,cardcallback callback);
s32 CARD_GetFreeBlocks(s32 chn, u16* freeblocks);
s32 CARD_GetSerialNo(s32 chn,u32 *serial1,u32 *serial2);
#else
#include <ogc/card.h>
#endif
#include "sdsupp.h"
#include "mcard.h"
#include "gci.h"
#include "freetype.h"
#include "raw.h"
#define mem_free(x) {free(x);x=NULL;}
//Enabling this definition will enable additional checks to ensure raw restore worked
#define DEBUGRAW
u8 *CardBuffer = 0;
#ifdef DEBUGRAW
u8 *CheckBuffer = 0;
u8 *EraseCheckBuffer = 0;
#endif
s8 read_data = 0;
s8 write_data = 0;
s8 erase_data = 0;
Header cardheader;
extern u8 currFolder[260];
extern int folderCount;
extern char fatpath[4];
extern syssram* __SYS_LockSram();
extern syssramex* __SYS_LockSramEx();
extern u32 __SYS_UnlockSram(u32 write);
extern u32 __SYS_UnlockSramEx(u32 write);
#if (_V_MAJOR_ >= 2) && (_V_MINOR_ >= 3)
extern s32 ogc_card_write(s32 chn,u32 address,u32 block_len,void *buffer,cardcallback callback);
extern s32 ogc_card_sectorerase(s32 chn,u32 sector,cardcallback callback);
extern s32 ogc_card_sync(s32 chn);
extern s32 ogc_card_read(s32 chn,u32 address,u32 block_len,void *buffer,cardcallback callback);
#endif
syssramex *sramex;
u8 imageserial[12];
void freecardbuf(){
if(CardBuffer)
{
if(CardBuffer != NULL)
{
mem_free(CardBuffer);
CardBuffer = NULL;
}
}
}
//code from tueidj's Devolution
void getserial(u8 *serial)
{
int i;
uint64_t rand;
// rand() taken from K&R, lol
rand = cardheader.formatTime; // seed
for (i=0; i < 12; i++)
{
rand = (rand * 1103515245 + 12345) >> 16;
cardheader.serial[i] -= rand;
rand = ((uint32_t)rand * 1103515245 + 12345) >> 16;
rand &= 0x7FFF;
serial[i] = cardheader.serial[i];
}
}
void ClearFlashID(s32 chn){
int i;
sramex = __SYS_LockSramEx();
for (i=0; i<12; i++){
sramex->flash_id[chn][i] = 0x00;
}
__SYS_UnlockSramEx(1);
}
void _read_callback(s32 chn,s32 result)
{
read_data =1;
}
void _write_callback(s32 chn,s32 result)
{
write_data =1;
}
void _erase_callback(s32 chn,s32 result)
{
erase_data =1;
}
u64 Card_SerialNo(s32 slot)
{
int err;
u32 SectorSize = 0;
CARD_Init(NULL,NULL);
EXI_ProbeReset();
err = MountCard(slot);
if (err < 0)
{
WaitCardError("SerialNo", err);
return -1; /*** Unable to mount the card ***/
}
usleep(10*1000);
err = CARD_GetSectorSize(slot,&SectorSize);
if(err < 0 )
{
WaitCardError("SerialNo Sectsize", err);
CARD_Unmount(slot);
return -1;
}
CardBuffer = (u8*)memalign(32,SectorSize);
if(CardBuffer == NULL)
{
WaitPrompt("SerialNo: Failed to malloc memory.");
CARD_Unmount(slot);
return -1;
}
s32 current_block = 0;
int read = 0;
while( 1 )
{
read_data = 0;
if( (err != 0) || current_block >= 1)
break;
DCInvalidateRange(CardBuffer+SectorSize*current_block,SectorSize);
err = ogc_card_read(slot,0,SectorSize,CardBuffer+SectorSize*current_block,_read_callback);
if(err == 0)
{
while(read_data == 0)
usleep(1*1000); //sleep untill the read is done
read = read + SectorSize;
current_block++;
}
else
{
mem_free(CardBuffer);
WaitCardError("BakRaw __read", err);
CARD_Unmount(slot);
return -1;
}
}
//get the true serial
u64 serial1[4];
int i;
for (i=0;i<4;i++){
memcpy(&serial1[i], CardBuffer+(8*i), sizeof(u64));
}
u64 serialA = serial1[0]^serial1[1]^serial1[2]^serial1[3];
mem_free(CardBuffer);
CARD_Unmount(slot);
return serialA;
}
//output is 29 char long
void time2name(char *name)
{
int month, day, year, hour, min, sec;
month = day = year = hour = min = sec = 0;
char monthstr[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec"
};
// Taken from void SecondsToDate(int seconds, int *pYear, int *pMonth, int *pDay)
// Calculates year month and day since jan 1, 1970 from (t) seconds
// Reference: Fliegel, H. F. and van Flandern, T. C. (1968).
// Communications of the ACM, Vol. 11, No. 10 (October, 1968).
// Original code in Fortran
int I, J, K, L, N;
u32 t = time(NULL);
L = t / 86400 + 2509157;
N = 4 * L / 146097;
L = L - (146097 * N + 3) / 4;
I = 4000 * (L + 1) / 1461001;
L = L - 1461 * I / 4 + 31;
J = 80 * L / 2447;
K = L - 2447 * J / 80;
L = J / 11;
J = J + 2 - 12 * L;
I = 100 * (N - 49) + I + L;
year = I;
month = J;
day = K;
sec = t % 60;
t /= 60;
min = t % 60;
t /= 60;
hour = t % 24;
sprintf(name, "%04d_%02d%s_%02d_%02d-%02d-%02d", year, month, monthstr[month-1], day, hour, min, sec);
}
s8 BackupRawImage(s32 slot, s32 *bytes_writen )
{
int err;
char filename[256];
char msg[128+256];
char msg2[128+256];
msg2[0] = '\0';
FILE* dumpFd = 0;
u32 SectorSize = 0;
s32 BlockCount = 0;
CARD_Init(NULL,NULL);
EXI_ProbeReset();
err = MountCard(slot);
if (err < 0)
{
WaitCardError("BakRaw", err);
return -1; /*** Unable to mount the card ***/
}
usleep(10*1000);
err = CARD_GetSectorSize(slot,&SectorSize);
if(err < 0 )
{
WaitCardError("BakRaw Sectsize", err);
CARD_Unmount(slot);
return -1;
}
err = CARD_GetBlockCount(slot,(u32*)&BlockCount);
if(err < 0 )
{
WaitCardError("BakRaw Blockcount", err);
CARD_Unmount(slot);
return -1;
}
CardBuffer = (u8*)memalign(32,SectorSize);
if(CardBuffer == NULL)
{
//printf("failed to malloc memory. fail\n");
WaitPrompt("BackRaw: Failed to malloc memory.");
CARD_Unmount(slot);
return -1;
}
s32 current_block = 0;
int read = 0;
s32 writen = 0;
char name[64];
int filenumber = 1;
sprintf (filename, "%s:/%s", fatpath, MCSAVES);
mkdir(filename, S_IREAD | S_IWRITE);
time2name(name);
sprintf (filename, "%s:/%s/%04db_%s.raw", fatpath, MCSAVES, BlockCount-5, name);
//not really needed because the filename has seconds in it and the same filename will "never" happen
while (file_exists(filename)){
sprintf (filename, "%s:/%s/%04db_%s_%02d.raw", fatpath, MCSAVES, BlockCount-5, name, filenumber);
filenumber++;
}
dumpFd = fopen(filename,"wb");
if (dumpFd == NULL)
{
mem_free(CardBuffer);
//printf("can not create file on SD\n");
WaitPrompt("BackRaw: cannot create file on SD.");
CARD_Unmount(slot);
return -1;
}
//printf("dumping...\n");
int card_addr = SectorSize*current_block;
while(current_block < BlockCount)
{
read_data = 0;//Reset read_callback
card_addr = SectorSize*current_block;
//printf("\rReading : %u bytes of %u (block %d)...",read,BlockCount*SectorSize,current_block);
sprintf(msg, "Reading...: Block %d of %d (%u bytes of %u)",current_block,BlockCount,read,BlockCount*SectorSize);
writeStatusBar(msg, msg2);
//memset(CardBuffer, 0xFF, SectorSize);//Reset buffer. In GC memory card bytes are set to 0xFF when erasing.
DCInvalidateRange(CardBuffer,SectorSize);
if( (err != 0) || current_block >= BlockCount)
break;
err = ogc_card_read(slot,card_addr,SectorSize,CardBuffer,_read_callback);//Read 1 block
if(err == 0)//Correct read
{
while(read_data == 0)
usleep(1*1000); //sleep untill the read is done
read = read + SectorSize;
current_block++;
}
else //card_read failed
{
mem_free(CardBuffer);
//printf("\nerror reading data : %d...\n",err);
fclose(dumpFd);
WaitCardError("BakRaw __read", err);
CARD_Unmount(slot);
return -1;
}
//Write Block to fat storage
//printf("\n");
//u8* ptr = (u8*)CardBuffer;
//fwrite((u8*)ptr,SectorSize,1,dumpFd);
fwrite(CardBuffer,SectorSize,1,dumpFd);
//writen += SectorSize;
if(bytes_writen != NULL)
*bytes_writen += SectorSize;
//printf("\rWriting : %u bytes of %u",writen+SectorSize,read);
sprintf(msg2, "Writing...: Block %d of %d (%u bytes of %u)",current_block,BlockCount,*bytes_writen,BlockCount*SectorSize);
writeStatusBar(msg, msg2);
}
fclose(dumpFd);
mem_free(CardBuffer);
CARD_Unmount(slot);
return 1;
}
s8 RestoreRawImage( s32 slot, char *sdfilename, s32 *bytes_writen )
{
FILE *dumpFd = 0;
char filename[256];
char msg[128+256];
int err;
u32 SectorSize = 0;
s32 BlockCount = 0;
s32 current_block = 0;
s32 writen = 0;
s32 write_addr = 0;
err = MountCard(slot);
if (err < 0)
{
WaitCardError("RestRaw", err);
return -1; /*** Unable to mount the card ***/
}
usleep(10*1000);
err = CARD_GetSectorSize(slot,&SectorSize);
if(err < 0 )
{
WaitCardError("RestRaw Sectsize", err);
CARD_Unmount(slot);
return -1;
}
err = CARD_GetBlockCount(slot,(u32*)&BlockCount);
if(err < 0 )
{
WaitCardError("RestRaw Blockcount", err);
CARD_Unmount(slot);
return -1;
}
CardBuffer = (u8*)memalign(32,SectorSize);
if(CardBuffer == NULL)
{
//printf("failed to malloc memory. fail\n");
WaitPrompt("RestRaw: Failed to malloc memory.");
CARD_Unmount(slot);
return -1;
}
#ifdef DEBUGRAW
CheckBuffer = (u8*)memalign(32,SectorSize);
if(CheckBuffer == NULL)
{
mem_free(CardBuffer);
WaitPrompt("RestRaw: Failed to malloc memory (CheckBuffer)");
CARD_Unmount(slot);
return -1;
}
EraseCheckBuffer = (u8*)memalign(32,SectorSize);
if(EraseCheckBuffer == NULL)
{
mem_free(CardBuffer);
mem_free(CheckBuffer);
WaitPrompt("RestRaw: Failed to malloc memory (EraseCheckBuffer)");
CARD_Unmount(slot);
return -1;
}
memset(EraseCheckBuffer, 0xFF, SectorSize);
#endif
/*** Make fullpath filename ***/
sprintf (filename, "%s:/%s/%s", fatpath, currFolder, sdfilename);
/*** Open the SD Card file ***/
dumpFd = fopen ( filename , "rb" );
if (dumpFd <= 0)
{
mem_free(CardBuffer);
#ifdef DEBUGRAW
mem_free(CheckBuffer);
mem_free(EraseCheckBuffer);
#endif
sprintf(msg, "Couldn't open %s", filename);
WaitPrompt (msg);
CARD_Unmount(slot);
return -1;
}
//first read the content of the SD dump into the buffer.
u32 lSize;
fseek (dumpFd , 0 , SEEK_END);
lSize = ftell (dumpFd);
rewind (dumpFd);
if(lSize != BlockCount*SectorSize)
{
//check for mci raw image
if((lSize-64) != BlockCount*SectorSize){
//incorrect dump size D:<
fclose(dumpFd);
mem_free(CardBuffer);
#ifdef DEBUGRAW
mem_free(CheckBuffer);
mem_free(EraseCheckBuffer);
#endif
WaitPrompt ("Card and image sizes differ");
CARD_Unmount(slot);
return -1;
}
}
//printf("are you -SURE- you want to recover? a faulty backup or even a program\nfailure will corrupt the memory card!\n");
int erase = 1;
//0 = B wass pressed -> ask again
if (!slot){
erase = WaitPromptChoice("Are you -SURE- you want to restore to memory card in slot A?", "Restore", "Cancel");
}else{
erase = WaitPromptChoice("Are you -SURE- you want to restore to memory card in slot B?", "Restore", "Cancel");
}
if (!erase)
{
if (!slot){
erase = WaitPromptChoiceAZ("All contents of memory card in slot A will be overwritten, continue?", "Restore", "Cancel");
}else{
erase = WaitPromptChoiceAZ("All contents of memory card in slot B will be erased, continue?", "Restore", "Cancel");
}
if (!erase)//Actual restore starts here
{
//ShowAction ("Reading data...");
//If it's a MCI image, skip the header
if ((lSize-64) == BlockCount*SectorSize) fseek(dumpFd, 64, SEEK_SET);
s32 upblock = 0;
s32 write_len = SectorSize;
//s32 write_len = 0x80;//pagesize of memory card
/*
//Test code to skip blocks
int blockSkip = 1023;
if ((lSize-64) == BlockCount*SectorSize) fseek(dumpFd, 64+blockSkip*SectorSize, SEEK_SET);
else fseek(dumpFd, blockSkip*SectorSize, SEEK_SET);
current_block = blockSkip;
BlockCount = BlockCount; //Change to how many blocks you'd like to write
write_addr = current_block*SectorSize;
//Testcode end
*/
while (1)
{
if( (err != 0) || current_block >= BlockCount || writen == BlockCount*SectorSize)
break;
fread(CardBuffer,SectorSize,1,dumpFd);
//fclose(dumpFd);
/*
//Test code to see if raw image is correctly read
char testfile[64];
sprintf(testfile, "%s:/fatread.bin", fatpath);
FILE *test = 0;
test = fopen ( testfile , "wb" );
fwrite (CardBuffer , 1 , SectorSize , test);
fclose (test);
//Testcode end
*/
//printf("writing data to memory card...\n");
//ShowAction ("Writing data to memory card...");
//printf("\rWriting... : %d of %d (block %d)",writen,BlockCount*SectorSize,current_block);
sprintf(msg, "Writing...: Block %d of %d (%d of %d)",current_block,BlockCount,writen,BlockCount*SectorSize);
ShowAction (msg);
//gprintf("\rWriting... : %d of %d (block %d of %d)",writen,BlockCount*SectorSize,current_block,BlockCount);
write_data = 0;//Write callback
erase_data = 0;//Erase callback
//DCStoreRange is called in card.c before actual writing to the card, let's copy it
DCStoreRange(CardBuffer,write_len);
//Data has to be erased before writing for propper restore (for some reason)
if(writen == 0 || !(writen%SectorSize))//We can only erase full sectors (blocks), but we can write in pages or other sizes
{
//s32 ogc_card_sectorerase(s32 chn,u32 sector,cardcallback callback);
err = ogc_card_sectorerase(slot,write_addr, _erase_callback);
if(err == 0)
{
while(erase_data == 0)
usleep(1*1000); //sleep until the erase is done which sadly takes alot longer then read
ogc_card_sync(slot);
#ifdef DEBUGRAW //This code section is broken right now
//At this point the sector (block) should be erased
//Check if the sector was erased (all set to 0xFF).
//Some unofficial memory cards apparently don't erase sectors when using ogc_card_sectorerase(),
//instead they can directly properly store the data calling just ogc_card_write(),
//so the following check will always fail, preventing restore for those cards, so I'm disabling
//it and relying only on the ogc_card_read() check done after writing the data.
/*
memset(CheckBuffer,0,write_len);
DCInvalidateRange(CheckBuffer,write_len);
read_data = 0;
err = ogc_card_read(slot,writen,write_len,CheckBuffer,_read_callback);
while(read_data == 0) //sleep until reading is done
usleep(1*1000);
if(err == 0) //Read OK
{
if(!memcmp(EraseCheckBuffer,CheckBuffer,write_len))
{
//printf("read and write are the same y0\n");
}
else
{
//Uncomment to dump contents that where read
char dumpfile[64];
sprintf(dumpfile, "%s:/setcheck_dump.bin", fatpath);
FILE* dump = fopen(dumpfile,"wb");
fwrite(CheckBuffer,1,write_len,dump);
fclose(dump);
fclose(dumpFd);
mem_free(CardBuffer);
mem_free(CheckBuffer);
mem_free(EraseCheckBuffer);
sprintf(msg, "Error! _sectorerase failed for unknown reasons at sector %d!", current_block);
WaitPrompt (msg);
CARD_Unmount(slot);
return -1;
}
}
else //Read failed
{
fclose(dumpFd);
mem_free(CardBuffer);
mem_free(CheckBuffer);
mem_free(EraseCheckBuffer);
sprintf(msg, "Error reading data(%d) Memory card could be corrupt now!!!",err);
WaitPrompt (msg);
CARD_Unmount(slot);
return -1;
}
*/
#endif
}else //sector_erase failed
{
//Cleanup
fclose(dumpFd);
mem_free(CardBuffer);
#ifdef DEBUGRAW
mem_free(CheckBuffer);
mem_free(EraseCheckBuffer);
#endif
//printf("\nerror writing data(%d) Memory card could be corrupt now!!!\n",err);
sprintf(msg, "error %d erasing sector. Memory card could be corrupt now!!!",err);
WaitPrompt (msg);
CARD_Unmount(slot);
return -1;
}
}
//s32 ogc_card_write(s32 chn,u32 address,u32 block_len,void *buffer,cardcallback callback)
err = ogc_card_write(slot,write_addr,write_len,CardBuffer,_write_callback);
if(err == 0)
{
while(write_data == 0)
usleep(1*1000); //sleep untill the write is done which sadly takes alot longer then read
ogc_card_sync(slot);
#ifdef DEBUGRAW
//Check the written data against the buffer
memset(CheckBuffer,0,write_len);
DCInvalidateRange(CheckBuffer,write_len);
read_data = 0;
//err = ogc_card_read(slot,SectorSize*current_block,SectorSize,CheckBuffer,_read_callback);
err = ogc_card_read(slot,write_addr,write_len,CheckBuffer,_read_callback);
while(read_data == 0)
usleep(1*1000);
ogc_card_sync(slot);
if(err == 0)//Read OK
{
if(!memcmp(CardBuffer,CheckBuffer,write_len))
{
//printf("read and write are the same y0\n");
}
else //Written and read data differ
{
//printf("different data. writing to SD (0x%08X), error %d\n",*check,CardError);
//Uncomment to dump contents that where read
char checkdump[64];
sprintf(checkdump, "%s:/check_dump.bin", fatpath);
FILE* dump = fopen(checkdump,"wb");
fwrite(CheckBuffer,1,write_len,dump);
fclose(dump);
//Cleanup
fclose(dumpFd);
mem_free(CardBuffer);
mem_free(CheckBuffer);
mem_free(EraseCheckBuffer);
sprintf(msg, "Error! Writing failed for unknown reasons at block %d!", current_block);
WaitPrompt (msg);
CARD_Unmount(slot);
return -1;
}
}
else //Reading failed
{
//Cleanup
fclose(dumpFd);
mem_free(CardBuffer);
mem_free(CheckBuffer);
mem_free(EraseCheckBuffer);
sprintf(msg, "Error %d reading data). Memory card could be corrupt now!!!",err);
WaitPrompt (msg);
CARD_Unmount(slot);
return -1;
}
#endif
/*At this point:
1. Data was read from fat device
2. Sector was erased (+- checked if properly erased)
3. Data was written to memory card
4. Written data was checked to be correctly written
So, next block it is
*/
writen += write_len;
upblock+=upblock+write_len;
write_addr += write_len;
if(upblock == SectorSize) //Advance block if we are writing in sizes different than SectorSize (i.e. a page, which is 0x80)
{
current_block++;
upblock = 0;
}
}
else //card_write failed
{
//Cleanup
fclose(dumpFd);
mem_free(CardBuffer);
#ifdef DEBUGRAW
mem_free(CheckBuffer);
mem_free(EraseCheckBuffer);
#endif
//printf("\nerror writing data(%d) Memory card could be corrupt now!!!\n",err);
sprintf(msg, "Error %d writing data. Memory card could be corrupt now!!!",err);
WaitPrompt (msg);
CARD_Unmount(slot);
return -1;
}
} // END while lopp
//Restore finished
fclose(dumpFd);
//printf("\n");
//gprintf("\n");
if(bytes_writen != NULL)
*bytes_writen = writen;
mem_free(CardBuffer);
#ifdef DEBUGRAW
mem_free(CheckBuffer);
mem_free(EraseCheckBuffer);
#endif
CARD_Unmount(slot);
return 1;
}
//User canceled restore (2nd prompt)
}
//User canceled restore (1st prompt)
//Clean
mem_free(CardBuffer);
#ifdef DEBUGRAW
mem_free(CheckBuffer);
mem_free(EraseCheckBuffer);
#endif
CARD_Unmount(slot);
return -1;
}