/
ftl.c
1122 lines (913 loc) · 32 KB
/
ftl.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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "openiboot.h"
#include "ftl.h"
#include "nand.h"
#include "util.h"
#define FTL_ID_V1 0x43303033
#define FTL_ID_V2 0x43303034
int HasFTLInit = FALSE;
static NANDData* Data;
static UnknownNANDType* Data2;
static int findDeviceInfoBBT(int bank, void* deviceInfoBBT) {
uint8_t* buffer = malloc(Data->bytesPerPage);
int lowestBlock = Data->blocksPerBank - (Data->blocksPerBank / 10);
int block;
for(block = Data->blocksPerBank - 1; block >= lowestBlock; block--) {
int page;
int badBlockCount = 0;
for(page = 0; page < Data->pagesPerBlock; page++) {
if(badBlockCount > 2) {
DebugPrintf("ftl: findDeviceInfoBBT - too many bad pages, skipping block %d\r\n", block);
break;
}
int ret = nand_read_alternate_ecc(bank, (block * Data->pagesPerBlock) + page, buffer);
if(ret != 0) {
if(ret == 1) {
DebugPrintf("ftl: findDeviceInfoBBT - found 'badBlock' on bank %d, page %d\r\n", (block * Data->pagesPerBlock) + page);
badBlockCount++;
}
DebugPrintf("ftl: findDeviceInfoBBT - skipping bank %d, page %d\r\n", (block * Data->pagesPerBlock) + page);
continue;
}
if(memcmp(buffer, "DEVICEINFOBBT\0\0\0", 16) == 0) {
if(deviceInfoBBT) {
memcpy(deviceInfoBBT, buffer + 0x38, *((uint32_t*)(buffer + 0x34)));
}
free(buffer);
return TRUE;
} else {
DebugPrintf("ftl: did not find signature on bank %d, page %d\r\n", (block * Data->pagesPerBlock) + page);
}
}
}
free(buffer);
return FALSE;
}
static int hasDeviceInfoBBT() {
int bank;
int good = TRUE;
for(bank = 0; bank < Data->banksTotal; bank++) {
good = findDeviceInfoBBT(bank, NULL);
if(!good)
return FALSE;
}
return good;
}
static VFLData1Type VFLData1;
static VFLCxt* pstVFLCxt = NULL;
static uint8_t* pstBBTArea = NULL;
static uint32_t* ScatteredPageNumberBuffer = NULL;
static uint16_t* ScatteredBankNumberBuffer = NULL;
static int VFLData4 = 0;
static uint8_t VFLData5[0xF8];
static int VFL_Init() {
memset(&VFLData1, 0, sizeof(VFLData1));
if(pstVFLCxt == NULL) {
pstVFLCxt = malloc(Data->banksTotal * sizeof(VFLCxt));
if(pstVFLCxt == NULL)
return -1;
}
if(pstBBTArea == NULL) {
pstBBTArea = (uint8_t*) malloc((Data->blocksPerBank + 7) / 8);
if(pstBBTArea == NULL)
return -1;
}
if(ScatteredPageNumberBuffer == NULL && ScatteredBankNumberBuffer == NULL) {
ScatteredPageNumberBuffer = (uint32_t*) malloc(Data->pagesPerSubBlk * 4);
ScatteredBankNumberBuffer = (uint16_t*) malloc(Data->pagesPerSubBlk * 4);
if(ScatteredPageNumberBuffer == NULL || ScatteredBankNumberBuffer == NULL)
return -1;
}
VFLData4 = 0;
return 0;
}
static FTLData1Type FTLData1;
static FTLCxt* pstFTLCxt;
static FTLCxt* FTLCxtBuffer;
static SpareData* FTLSpareBuffer;
static uint32_t* ScatteredVirtualPageNumberBuffer;
static uint8_t* StoreCxt;
static int FTL_Init() {
int numPagesToWriteInStoreCxt = 0;
int pagesPerCounterTable = ((Data->userSubBlksTotal + 23) * sizeof(uint16_t)) / Data->bytesPerPage;
if((((Data->userSubBlksTotal + 23) * sizeof(uint16_t)) % Data->bytesPerPage) != 0) {
pagesPerCounterTable++;
}
numPagesToWriteInStoreCxt = pagesPerCounterTable * 2;
int pagesForMapTable = (Data->userSubBlksTotal * sizeof(uint16_t)) / Data->bytesPerPage;
if(((Data->userSubBlksTotal * sizeof(uint16_t)) % Data->bytesPerPage) != 0) {
pagesForMapTable++;
}
numPagesToWriteInStoreCxt += pagesForMapTable;
int pagesForLogPageOffsets = (Data->pagesPerSubBlk * 34) / Data->bytesPerPage;
if(((Data->pagesPerSubBlk * 34) % Data->bytesPerPage) != 0) {
pagesForLogPageOffsets++;
}
numPagesToWriteInStoreCxt += pagesForLogPageOffsets + 2;
if(numPagesToWriteInStoreCxt >= Data->pagesPerSubBlk) {
bufferPrintf("nand: error - FTL_NUM_PAGES_TO_WRITE_IN_STORECXT >= PAGES_PER_SUBLK\r\n");
return -1;
}
int pagesPerSimpleMergeBuffer = Data->pagesPerSubBlk / 8;
if((pagesPerSimpleMergeBuffer * 2) >= Data->pagesPerSubBlk) {
bufferPrintf("nand: error - (PAGES_PER_SIMPLE_MERGE_BUFFER * 2) >= PAGES_PER_SUBLK\r\n");
return -1;
}
memset(&FTLData1, 0, 0x58);
if(pstFTLCxt == NULL) {
pstFTLCxt = FTLCxtBuffer = (FTLCxt*) malloc(sizeof(FTLCxt));
if(pstFTLCxt == NULL)
return -1;
memset(pstFTLCxt->field_3D8, 0, sizeof(pstFTLCxt->field_3D8));
}
pstFTLCxt->field_31C = 0;
pstFTLCxt->pawMapTable = (uint16_t*) malloc(Data->userSubBlksTotal * sizeof(uint16_t));
pstFTLCxt->wPageOffsets = (uint16_t*) malloc((Data->pagesPerSubBlk * 18) * sizeof(uint16_t));
pstFTLCxt->pawEraseCounterTable = (uint16_t*) malloc((Data->userSubBlksTotal + 23) * sizeof(uint16_t));
pstFTLCxt->pawReadCounterTable = (uint16_t*) malloc((Data->userSubBlksTotal + 23) * sizeof(uint16_t));
FTLSpareBuffer = (SpareData*) malloc(Data->pagesPerSubBlk * sizeof(SpareData));
if((Data->pagesPerSubBlk / 8) >= numPagesToWriteInStoreCxt) {
numPagesToWriteInStoreCxt = Data->pagesPerSubBlk / 8;
}
StoreCxt = malloc(Data->bytesPerPage * numPagesToWriteInStoreCxt);
ScatteredVirtualPageNumberBuffer = (uint32_t*) malloc(Data->pagesPerSubBlk * sizeof(uint32_t*));
if(!pstFTLCxt->pawMapTable || !pstFTLCxt->wPageOffsets || !pstFTLCxt->pawEraseCounterTable || !FTLCxtBuffer->pawReadCounterTable || ! FTLSpareBuffer || !StoreCxt || !ScatteredVirtualPageNumberBuffer)
return -1;
int i;
for(i = 0; i < 18; i++) {
pstFTLCxt->pLog[i].wPageOffsets = pstFTLCxt->wPageOffsets + (i * Data->pagesPerSubBlk);
memset(pstFTLCxt->pLog[i].wPageOffsets, 0xFF, Data->pagesPerSubBlk * 2);
pstFTLCxt->pLog[i].field_10 = 1;
pstFTLCxt->pLog[i].field_C = 0;
pstFTLCxt->pLog[i].field_E = 0;
}
return 0;
}
// pageBuffer and spareBuffer are represented by single BUF struct within Whimory
static int vfl_read_page(int bank, int block, int page, uint8_t* pageBuffer, uint8_t* spareBuffer) {
int i;
for(i = 0; i < 8; i++) {
if(nand_read(bank, (block * Data->pagesPerBlock) + page + i, pageBuffer, spareBuffer, TRUE, TRUE) == 0) {
SpareData* spareData = (SpareData*) spareBuffer;
if(spareData->field_8 == 0 && spareData->field_9 == 0x80)
return TRUE;
}
}
return FALSE;
}
static void vfl_checksum(void* data, int size, uint32_t* a, uint32_t* b) {
int i;
uint32_t* buffer = (uint32_t*) data;
uint32_t x = 0;
uint32_t y = 0;
for(i = 0; i < (size / 4); i++) {
x += buffer[i];
y ^= buffer[i];
}
*a = x + 0xAABBCCDD;
*b = y ^ 0xAABBCCDD;
}
static int vfl_gen_checksum(int bank) {
vfl_checksum(&pstVFLCxt[bank], (uint32_t)&pstVFLCxt[bank].checksum1 - (uint32_t)&pstVFLCxt[bank], &pstVFLCxt[bank].checksum1, &pstVFLCxt[bank].checksum2);
return FALSE;
}
static int vfl_check_checksum(int bank) {
static int counter = 0;
counter++;
uint32_t checksum1;
uint32_t checksum2;
vfl_checksum(&pstVFLCxt[bank], (uint32_t)&pstVFLCxt[bank].checksum1 - (uint32_t)&pstVFLCxt[bank], &checksum1, &checksum2);
// Yeah, this looks fail, but this is actually the logic they use
if(checksum1 == pstVFLCxt[bank].checksum1)
return TRUE;
if(checksum2 != pstVFLCxt[bank].checksum2)
return TRUE;
return FALSE;
}
static void virtual_page_number_to_virtual_address(uint32_t dwVpn, uint16_t* virtualBank, uint16_t* virtualBlock, uint16_t* virtualPage) {
*virtualBank = dwVpn % Data->banksTotal;
*virtualBlock = dwVpn / Data->pagesPerSubBlk;
*virtualPage = (dwVpn / Data->banksTotal) % Data->pagesPerBlock;
}
// badBlockTable is a bit array with 8 virtual blocks in one bit entry
static int isGoodBlock(uint8_t* badBlockTable, uint16_t virtualBlock) {
int index = virtualBlock/8;
return ((badBlockTable[index / 8] >> (7 - (index % 8))) & 0x1) == 0x1;
}
static uint16_t virtual_block_to_physical_block(uint16_t virtualBank, uint16_t virtualBlock) {
if(isGoodBlock(pstVFLCxt[virtualBank].badBlockTable, virtualBlock))
return virtualBlock;
int pwDesPbn;
for(pwDesPbn = 0; pwDesPbn < pstVFLCxt[virtualBank].numReservedBlocks; pwDesPbn++) {
if(pstVFLCxt[virtualBank].reservedBlockPoolMap[pwDesPbn] == virtualBlock) {
if(pwDesPbn >= Data->blocksPerBank) {
bufferPrintf("ftl: Destination physical block for remapping is greater than number of blocks per bank!");
}
return pstVFLCxt[virtualBank].reservedBlockPoolStart + pwDesPbn;
}
}
return virtualBlock;
}
int VFL_Erase(uint16_t block) {
uint16_t physicalBlock;
int ret;
int bank;
for(bank = 0; bank < Data->banksTotal; bank++) {
physicalBlock = virtual_block_to_physical_block(bank, block + Data2->field_4);
ret = nand_erase(bank, physicalBlock);
if(ret) {
return ret;
}
}
return 0;
}
int VFL_Read(uint32_t virtualPageNumber, uint8_t* buffer, uint8_t* spare, int empty_ok, int* refresh_page) {
if(refresh_page) {
*refresh_page = FALSE;
}
VFLData1.field_8++;
VFLData1.field_20++;
uint32_t dwVpn = virtualPageNumber + (Data->pagesPerSubBlk * Data2->field_4);
if(dwVpn >= Data->pagesTotal) {
bufferPrintf("ftl: dwVpn overflow: %d\r\n", dwVpn);
return ERROR_ARG;
}
if(dwVpn < Data->pagesPerSubBlk) {
bufferPrintf("ftl: dwVpn underflow: %d\r\n", dwVpn);
}
uint16_t virtualBank;
uint16_t virtualBlock;
uint16_t virtualPage;
uint16_t physicalBlock;
virtual_page_number_to_virtual_address(dwVpn, &virtualBank, &virtualBlock, &virtualPage);
physicalBlock = virtual_block_to_physical_block(virtualBank, virtualBlock);
int page = physicalBlock * Data->pagesPerBlock + virtualPage;
int ret = nand_read(virtualBank, page, buffer, spare, TRUE, TRUE);
if(!empty_ok && ret == ERROR_EMPTYBLOCK) {
ret = ERROR_NAND;
}
if(refresh_page) {
if((Data->field_2F <= 0 && ret == 0) || ret == ERROR_NAND) {
bufferPrintf("ftl: setting refresh_page to TRUE due to the following factors: Data->field_2F = %x, ret = %d\r\n", Data->field_2F, ret);
*refresh_page = TRUE;
}
}
if(ret == ERROR_ARG || ret == ERROR_NAND) {
nand_bank_reset(virtualBank, 100);
ret = nand_read(virtualBank, page, buffer, spare, TRUE, TRUE);
if(!empty_ok && ret == ERROR_EMPTYBLOCK) {
return ERROR_NAND;
}
if(ret == ERROR_ARG || ret == ERROR_NAND)
return ret;
}
if(ret == ERROR_EMPTYBLOCK) {
if(spare) {
memset(spare, 0xFF, sizeof(SpareData));
}
}
return ret;
}
static int VFL_ReadMultiplePagesInVb(int logicalBlock, int logicalPage, int count, uint8_t* main, SpareData* spare, int* refresh_page) {
int i;
int currentPage = logicalPage;
for(i = 0; i < count; i++) {
int ret = VFL_Read((logicalBlock * Data->pagesPerSubBlk) + currentPage, main + (Data->bytesPerPage * i), (uint8_t*) &spare[i], TRUE, refresh_page);
currentPage++;
if(ret != 0)
return FALSE;
}
return TRUE;
}
static int VFL_ReadScatteredPagesInVb(uint32_t* virtualPageNumber, int count, uint8_t* main, SpareData* spare, int* refresh_page) {
VFLData1.field_8 += count;
VFLData1.field_20++;
if(refresh_page) {
*refresh_page = FALSE;
}
int i = 0;
for(i = 0; i < count; i++) {
uint32_t dwVpn = virtualPageNumber[i] + (Data->pagesPerSubBlk * Data2->field_4);
uint16_t virtualBlock;
uint16_t virtualPage;
uint16_t physicalBlock;
virtual_page_number_to_virtual_address(dwVpn, &ScatteredBankNumberBuffer[i], &virtualBlock, &virtualPage);
physicalBlock = virtual_block_to_physical_block(ScatteredBankNumberBuffer[i], virtualBlock);
ScatteredPageNumberBuffer[i] = physicalBlock * Data->pagesPerBlock + virtualPage;
}
int ret = nand_read_multiple(ScatteredBankNumberBuffer, ScatteredPageNumberBuffer, main, spare, count);
if(Data->field_2F <= 0 && refresh_page != NULL) {
bufferPrintf("ftl: VFL_ReadScatteredPagesInVb mark page for refresh\r\n");
*refresh_page = TRUE;
}
if(ret != 0)
return FALSE;
else
return TRUE;
}
// sub_18015A9C
static uint16_t* VFL_get_maxThing() {
int bank = 0;
int max = 0;
uint16_t* maxThing = NULL;
for(bank = 0; bank < Data->banksTotal; bank++) {
int cur = pstVFLCxt[bank].field_0;
if(max <= cur) {
max = cur;
maxThing = pstVFLCxt[bank].field_4;
}
}
return maxThing;
}
static int VFL_Open() {
int bank = 0;
for(bank = 0; bank < Data->banksTotal; bank++) {
if(!findDeviceInfoBBT(bank, pstBBTArea)) {
bufferPrintf("ftl: findDeviceInfoBBT failed\r\n");
return -1;
}
if(bank >= Data->banksTotal) {
return -1;
}
VFLCxt* curVFLCxt = &pstVFLCxt[bank];
uint8_t* pageBuffer = malloc(Data->bytesPerPage);
uint8_t* spareBuffer = malloc(Data->bytesPerSpare);
if(pageBuffer == NULL || spareBuffer == NULL) {
bufferPrintf("ftl: cannot allocate page and spare buffer\r\n");
return -1;
}
int i = 1;
for(i = 1; i < Data2->field_0; i++) {
// so pstBBTArea is a bit array of some sort
if(!(pstBBTArea[i / 8] & (1 << (i & 0x7))))
continue;
if(vfl_read_page(bank, i, 0, pageBuffer, spareBuffer) == TRUE) {
memcpy(curVFLCxt->VFLCxtBlock, ((VFLCxt*)pageBuffer)->VFLCxtBlock, sizeof(curVFLCxt->VFLCxtBlock));
break;
}
}
if(i == Data2->field_0) {
bufferPrintf("ftl: cannot find readable VFLCxtBlock\r\n");
free(pageBuffer);
free(spareBuffer);
return -1;
}
int minLpn = 0xFFFFFFFF;
int VFLCxtIdx = 4;
for(i = 0; i < 4; i++) {
uint16_t block = curVFLCxt->VFLCxtBlock[i];
if(block == 0xFFFF)
continue;
if(vfl_read_page(bank, block, 0, pageBuffer, spareBuffer) != TRUE)
continue;
SpareData* spareData = (SpareData*) spareBuffer;
if(spareData->logicalPageNumber > 0 && spareData->logicalPageNumber <= minLpn) {
minLpn = spareData->logicalPageNumber;
VFLCxtIdx = i;
}
}
if(VFLCxtIdx == 4) {
bufferPrintf("ftl: cannot find readable VFLCxtBlock index in spares\r\n");
free(pageBuffer);
free(spareBuffer);
return -1;
}
int page = 8;
int last = 0;
for(page = 8; page < Data->pagesPerBlock; page += 8) {
if(vfl_read_page(bank, curVFLCxt->VFLCxtBlock[VFLCxtIdx], page, pageBuffer, spareBuffer) == FALSE) {
break;
}
last = page;
}
if(vfl_read_page(bank, curVFLCxt->VFLCxtBlock[VFLCxtIdx], last, pageBuffer, spareBuffer) == FALSE) {
bufferPrintf("ftl: cannot find readable VFLCxt\n");
free(pageBuffer);
free(spareBuffer);
return -1;
}
// Aha, so the upshot is that this finds the VFLCxt and copies it into pstVFLCxt
memcpy(&pstVFLCxt[bank], pageBuffer, sizeof(VFLCxt));
if(curVFLCxt->field_0 >= VFLData4) {
VFLData4 = curVFLCxt->field_0;
}
free(pageBuffer);
free(spareBuffer);
if(vfl_check_checksum(bank) == FALSE) {
bufferPrintf("ftl: VFLCxt has bad checksum\n");
return -1;
}
}
void* maxThing = VFL_get_maxThing();
uint16_t buffer[3];
memcpy(buffer, maxThing, 6);
for(bank = 0; bank < Data->banksTotal; bank++) {
memcpy(pstVFLCxt[bank].field_4, buffer, sizeof(buffer));
vfl_gen_checksum(bank);
}
return 0;
}
void FTL_64bit_sum(uint64_t* src, uint64_t* dest, int size) {
int i;
for(i = 0; i < size / sizeof(uint64_t); i++) {
dest[i] += src[i];
}
}
static int FTL_Restore() {
return FALSE;
}
static int FTL_GetStruct(FTLStruct type, void** data, int* size) {
switch(type) {
case FTLData1SID:
*data = &FTLData1;
*size = sizeof(FTLData1);
return TRUE;
default:
return FALSE;
}
}
static int VFL_GetStruct(FTLStruct type, void** data, int* size) {
switch(type) {
case VFLData1SID:
*data = &VFLData1;
*size = sizeof(VFLData1);
return TRUE;
case VFLData5SID:
*data = VFLData5;
*size = 0xF8;
return TRUE;
default:
return FALSE;
}
}
static int sum_data(uint8_t* pageBuffer) {
void* data;
int size;
FTL_GetStruct(FTLData1SID, &data, &size);
FTL_64bit_sum((uint64_t*)pageBuffer, (uint64_t*)data, size);
VFL_GetStruct(VFLData1SID, &data, &size);
FTL_64bit_sum((uint64_t*)(pageBuffer + 0x200), (uint64_t*)data, size);
VFL_GetStruct(VFLData5SID, &data, &size);
FTL_64bit_sum((uint64_t*)(pageBuffer + 0x400), (uint64_t*)data, size);
return TRUE;
}
static int FTL_Open(int* pagesAvailable, int* bytesPerPage) {
int refreshPage;
int ret;
int i;
uint16_t* pawMapTable = pstFTLCxt->pawMapTable;
uint16_t* pawEraseCounterTable = pstFTLCxt->pawEraseCounterTable;
void* pawReadCounterTable = pstFTLCxt->pawReadCounterTable;
uint16_t* wPageOffsets = pstFTLCxt->wPageOffsets;
void* thing;
if((thing = VFL_get_maxThing()) == NULL)
goto FTL_Open_Error;
memcpy(pstFTLCxt->thing, thing, sizeof(pstFTLCxt->thing));
uint8_t* pageBuffer = malloc(Data->bytesPerPage);
uint8_t* spareBuffer = malloc(Data->bytesPerSpare);
if(!pageBuffer || !spareBuffer) {
bufferPrintf("ftl: FTL_Open ran out of memory!\r\n");
return ERROR_ARG;
}
uint32_t ftlCxtBlock = 0xffff;
uint32_t minLpn = 0xffffffff;
for(i = 0; i < sizeof(pstFTLCxt->thing)/sizeof(uint16_t); i++) {
ret = VFL_Read(Data->pagesPerSubBlk * pstFTLCxt->thing[i], pageBuffer, spareBuffer, TRUE, &refreshPage);
if(ret == ERROR_ARG) {
free(pageBuffer);
free(spareBuffer);
goto FTL_Open_Error;
}
SpareData* spareData = (SpareData*) spareBuffer;
if((spareData->field_9 - 0x43) > 0xC)
continue;
if(ret != 0)
continue;
if(ftlCxtBlock != 0xffff && spareData->logicalPageNumber >= minLpn)
continue;
minLpn = spareData->logicalPageNumber;
ftlCxtBlock = pstFTLCxt->thing[i];
}
if(ftlCxtBlock == 0xffff) {
bufferPrintf("ftl: Cannot find context!\r\n");
goto FTL_Open_Error_Release;
}
bufferPrintf("ftl: Successfully found FTL context block: %d\r\n", ftlCxtBlock);
int ftlCxtFound = FALSE;
for(i = Data->pagesPerSubBlk - 1; i > 0; i--) {
ret = VFL_Read(Data->pagesPerSubBlk * ftlCxtBlock + i, pageBuffer, spareBuffer, TRUE, &refreshPage);
if(ret == 1) {
continue;
} else if(ret == 0 && ((SpareData*)spareBuffer)->field_9 == 0x43) {
memcpy(FTLCxtBuffer, pageBuffer, sizeof(FTLCxt));
ftlCxtFound = TRUE;
break;
} else {
ftlCxtFound = FALSE;
break;
}
}
// Restore now possibly overwritten (by data from NAND) pointers from backed up copies
pstFTLCxt->pawMapTable = pawMapTable;
pstFTLCxt->pawEraseCounterTable = pawEraseCounterTable;
pstFTLCxt->pawReadCounterTable = pawReadCounterTable;
pstFTLCxt->wPageOffsets = wPageOffsets;
for(i = 0; i < 18; i++) {
pstFTLCxt->pLog[i].wPageOffsets = pstFTLCxt->wPageOffsets + (i * Data->pagesPerSubBlk);
}
if(!ftlCxtFound)
goto FTL_Open_Error_Release;
bufferPrintf("ftl: Successfully read FTL context block.\r\n");
int pagesToRead;
pagesToRead = (Data->userSubBlksTotal * sizeof(uint16_t)) / Data->bytesPerPage;
if(((Data->userSubBlksTotal * sizeof(uint16_t)) % Data->bytesPerPage) != 0)
pagesToRead++;
for(i = 0; i < pagesToRead; i++) {
if(VFL_Read(pstFTLCxt->pages_for_pawMapTable[i], pageBuffer, spareBuffer, TRUE, &refreshPage) != 0)
goto FTL_Open_Error_Release;
int toRead = Data->bytesPerPage;
if(toRead > ((Data->userSubBlksTotal * sizeof(uint16_t)) - (i * Data->bytesPerPage))) {
toRead = (Data->userSubBlksTotal * sizeof(uint16_t)) - (i * Data->bytesPerPage);
}
memcpy(((uint8_t*)pstFTLCxt->pawMapTable) + (i * Data->bytesPerPage), pageBuffer, toRead);
}
pagesToRead = (Data->pagesPerSubBlk * (17 * sizeof(uint16_t))) / Data->bytesPerPage;
if(((Data->pagesPerSubBlk * (17 * sizeof(uint16_t))) % Data->bytesPerPage) != 0)
pagesToRead++;
for(i = 0; i < pagesToRead; i++) {
if(VFL_Read(pstFTLCxt->pages_for_wPageOffsets[i], pageBuffer, spareBuffer, TRUE, &refreshPage) != 0)
goto FTL_Open_Error_Release;
int toRead = Data->bytesPerPage;
if(toRead > ((Data->pagesPerSubBlk * (17 * sizeof(uint16_t))) - (i * Data->bytesPerPage))) {
toRead = (Data->pagesPerSubBlk * (17 * sizeof(uint16_t))) - (i * Data->bytesPerPage);
}
memcpy(((uint8_t*)pstFTLCxt->wPageOffsets) + (i * Data->bytesPerPage), pageBuffer, toRead);
}
pagesToRead = ((Data->userSubBlksTotal + 23) * sizeof(uint16_t)) / Data->bytesPerPage;
if((((Data->userSubBlksTotal + 23) * sizeof(uint16_t)) % Data->bytesPerPage) != 0)
pagesToRead++;
for(i = 0; i < pagesToRead; i++) {
if(VFL_Read(pstFTLCxt->pages_for_pawEraseCounterTable[i], pageBuffer, spareBuffer, TRUE, &refreshPage) != 0)
goto FTL_Open_Error_Release;
int toRead = Data->bytesPerPage;
if(toRead > (((Data->userSubBlksTotal + 23) * sizeof(uint16_t)) - (i * Data->bytesPerPage))) {
toRead = ((Data->userSubBlksTotal + 23) * sizeof(uint16_t)) - (i * Data->bytesPerPage);
}
memcpy(((uint8_t*)pstFTLCxt->pawEraseCounterTable) + (i * Data->bytesPerPage), pageBuffer, toRead);
}
int success = FALSE;
bufferPrintf("ftl: Detected version %x %x\r\n", FTLCxtBuffer->versionLower, FTLCxtBuffer->versionUpper);
if(FTLCxtBuffer->versionLower == 0x46560001 && FTLCxtBuffer->versionUpper == 0xB9A9FFFE) {
pagesToRead = ((Data->userSubBlksTotal + 23) * sizeof(uint16_t)) / Data->bytesPerPage;
if((((Data->userSubBlksTotal + 23) * sizeof(uint16_t)) % Data->bytesPerPage) != 0)
pagesToRead++;
success = TRUE;
for(i = 0; i < pagesToRead; i++) {
if(VFL_Read(pstFTLCxt->pages_for_pawReadCounterTable[i], pageBuffer, spareBuffer, TRUE, &refreshPage) != 0) {
success = FALSE;
break;
}
int toRead = Data->bytesPerPage;
if(toRead > (((Data->userSubBlksTotal + 23) * sizeof(uint16_t)) - (i * Data->bytesPerPage))) {
toRead = ((Data->userSubBlksTotal + 23) * sizeof(uint16_t)) - (i * Data->bytesPerPage);
}
memcpy(((uint8_t*)pstFTLCxt->pawReadCounterTable) + (i * Data->bytesPerPage), pageBuffer, toRead);
}
if((pstFTLCxt->field_3D4 + 1) == 0) {
int x = pstFTLCxt->page_3D0 / Data->pagesPerSubBlk;
if(x == 0 || x <= Data->userSubBlksTotal) {
if(VFL_Read(pstFTLCxt->page_3D0, pageBuffer, spareBuffer, TRUE, &refreshPage) != 0)
goto FTL_Open_Error_Release;
sum_data(pageBuffer);
}
}
} else {
bufferPrintf("ftl: updating the FTL from seemingly compatible version\r\n");
for(i = 0; i < (Data->userSubBlksTotal + 23); i++) {
pstFTLCxt->pawReadCounterTable[i] = 0x1388;
}
for(i = 0; i < 5; i++) {
pstFTLCxt->elements2[i].field_0 = -1;
pstFTLCxt->elements2[i].field_2 = -1;
}
pstFTLCxt->field_3C8 = 0;
pstFTLCxt->field_31C = 0;
FTLCxtBuffer->versionLower = 0x46560000;
FTLCxtBuffer->versionUpper = 0xB9A9FFFF;
success = TRUE;
}
if(success) {
bufferPrintf("ftl: FTL successfully opened!\r\n");
free(pageBuffer);
free(spareBuffer);
*pagesAvailable = Data->userPagesTotal;
*bytesPerPage = Data->bytesPerPage;
return 0;
}
FTL_Open_Error_Release:
free(pageBuffer);
free(spareBuffer);
FTL_Open_Error:
bufferPrintf("ftl: FTL_Open cannot load FTLCxt!\r\n");
if(FTL_Restore() != FALSE) {
*pagesAvailable = Data->userPagesTotal;
*bytesPerPage = Data->bytesPerPage;
return 0;
} else {
return ERROR_ARG;
}
}
uint32_t FTL_map_page(FTLCxtLog* pLog, int lbn, int offset) {
if(pLog && pLog->wPageOffsets[offset] != 0xFFFF) {
if(((pLog->wVbn * Data->pagesPerSubBlk) + pLog->wPageOffsets[offset] + 1) != 0)
return (pLog->wVbn * Data->pagesPerSubBlk) + pLog->wPageOffsets[offset];
}
return (pstFTLCxt->pawMapTable[lbn] * Data->pagesPerSubBlk) + offset;
}
int FTL_Read(int logicalPageNumber, int totalPagesToRead, uint8_t* pBuf) {
int i;
int hasError = FALSE;
FTLData1.field_8 += totalPagesToRead;
FTLData1.field_18++;
pstFTLCxt->totalReadCount++;
if(!pBuf) {
return ERROR_ARG;
}
if(totalPagesToRead == 0 || (logicalPageNumber + totalPagesToRead) >= Data->userPagesTotal) {
bufferPrintf("ftl: invalid input parameters\r\n");
return ERROR_INPUT;
}
int lbn = logicalPageNumber / Data->pagesPerSubBlk;
int offset = logicalPageNumber - (lbn * Data->pagesPerSubBlk);
uint8_t* pageBuffer = malloc(Data->bytesPerPage);
uint8_t* spareBuffer = malloc(Data->bytesPerSpare);
if(!pageBuffer || !spareBuffer) {
bufferPrintf("ftl: FTL_Read ran out of memory!\r\n");
return ERROR_ARG;
}
FTLCxtLog* pLog = NULL;
for(i = 0; i < 17; i++) {
if(pstFTLCxt->pLog[i].wVbn == 0xFFFF)
continue;
if(pstFTLCxt->pLog[i].wLbn == lbn) {
pLog = &pstFTLCxt->pLog[i];
break;
}
}
int ret = 0;
int pagesRead = 0;
int pagesToRead;
int refreshPage;
int currentLogicalPageNumber = logicalPageNumber;
while(TRUE) {
pagesToRead = Data->pagesPerSubBlk - offset;
if(pagesToRead >= (totalPagesToRead - pagesRead))
pagesToRead = totalPagesToRead - pagesRead;
int readSuccessful;
if(pLog != NULL) {
for(i = 0; i < pagesToRead; i++) {
ScatteredVirtualPageNumberBuffer[i] = FTL_map_page(pLog, lbn, offset + i);
if((ScatteredVirtualPageNumberBuffer[i] / Data->pagesPerSubBlk) == pLog->wVbn) {
pstFTLCxt->pawReadCounterTable[ScatteredVirtualPageNumberBuffer[i] / Data->pagesPerSubBlk]++;
} else {
pstFTLCxt->pawReadCounterTable[pstFTLCxt->pawMapTable[ScatteredVirtualPageNumberBuffer[i] / Data->pagesPerSubBlk]]++;
}
}
readSuccessful = VFL_ReadScatteredPagesInVb(ScatteredVirtualPageNumberBuffer, pagesToRead, pBuf + (pagesRead * Data->bytesPerPage), FTLSpareBuffer, &refreshPage);
if(refreshPage) {
bufferPrintf("ftl: _AddLbnToRefreshList (0x%x, 0x%x, 0x%x)\r\n", lbn, pstFTLCxt->pawMapTable[lbn], pLog->wVbn);
}
} else {
// VFL_ReadMultiplePagesInVb has a different calling convention and implementation than the equivalent iBoot function.
// Ours is a bit less optimized, and just calls VFL_Read for each page.
pstFTLCxt->pawReadCounterTable[pstFTLCxt->pawMapTable[lbn]] += pagesToRead;
readSuccessful = VFL_ReadMultiplePagesInVb(pstFTLCxt->pawMapTable[lbn], offset, pagesToRead, pBuf + (pagesRead * Data->bytesPerPage), FTLSpareBuffer, &refreshPage);
if(refreshPage) {
bufferPrintf("ftl: _AddLbnToRefreshList (0x%x, 0x%x)\r\n", lbn, pstFTLCxt->pawMapTable[lbn]);
}
}
int loop = 0;
if(readSuccessful) {
for(i = 0; i < pagesToRead; i++) {
if(FTLSpareBuffer[i].eccMark == 0xFF)
continue;
bufferPrintf("ftl: CHECK_FTL_ECC_MARK (0x%x, 0x%x, 0x%x, 0x%x)\r\n", lbn, offset, i, FTLSpareBuffer[i].eccMark);
hasError = TRUE;
}
pagesRead += pagesToRead;
currentLogicalPageNumber += pagesToRead;
offset += pagesToRead;
if(pagesRead == totalPagesToRead) {
goto FTL_Read_Done;
}
loop = FALSE;
} else {
loop = TRUE;
}
do {
if(pagesRead != totalPagesToRead && Data->pagesPerSubBlk != offset) {
int virtualPage = FTL_map_page(pLog, lbn, offset);
ret = VFL_Read(virtualPage, pBuf + (Data->bytesPerPage * pagesRead), spareBuffer, TRUE, &refreshPage);
if(refreshPage) {
bufferPrintf("ftl: _AddLbnToRefreshList (0x%x, 0x%x)\r\n", lbn, virtualPage / Data->pagesPerSubBlk);
}
if(ret == ERROR_ARG)
goto FTL_Read_Error_Release;
if(ret == ERROR_NAND || ((SpareData*) spareBuffer)->eccMark != 0xFF) {
bufferPrintf("ftl: ECC error, ECC mark is: %x\r\n", ((SpareData*) spareBuffer)->eccMark);
hasError = TRUE;
if(pLog) {
virtualPage = FTL_map_page(pLog, lbn, offset);
bufferPrintf("ftl: lbn 0x%x pLog->wVbn 0x%x pawMapTable 0x%x offset 0x%x vpn 0x%x\r\n", lbn, pLog->wVbn, pstFTLCxt->pawMapTable[lbn], offset, virtualPage);
} else {
virtualPage = FTL_map_page(NULL, lbn, offset);
bufferPrintf("ftl: lbn 0x%x pawMapTable 0x%x offset 0x%x vpn 0x%x\r\n", lbn, pstFTLCxt->pawMapTable[lbn], offset, virtualPage);
}
}
if(ret == 0) {
if(((SpareData*) spareBuffer)->logicalPageNumber != offset) {
bufferPrintf("ftl: error, dwWrittenLpn(0x%x) != dwLpn(0x%x)\r\n", ((SpareData*) spareBuffer)->logicalPageNumber, offset);
}
}
pagesRead++;
currentLogicalPageNumber++;
offset++;
if(pagesRead == totalPagesToRead) {
goto FTL_Read_Done;
}
}
if(offset == Data->pagesPerSubBlk) {
lbn++;
if(lbn >= Data->userSubBlksTotal)
goto FTL_Read_Error_Release;
pLog = NULL;
for(i = 0; i < 17; i++) {
if(pstFTLCxt->pLog[i].wVbn != 0xFFFF && pstFTLCxt->pLog[i].wLbn == lbn) {
pLog = &pstFTLCxt->pLog[i];
break;
}
}
offset = 0;
break;
}
} while(loop);
}
FTL_Read_Done:
free(pageBuffer);
free(spareBuffer);
if(hasError) {
bufferPrintf("ftl: USER_DATA_ERROR, failed with (0x%x, 0x%x, 0x%x)\r\n", logicalPageNumber, totalPagesToRead, pBuf);
return ERROR_NAND;
}
return 0;
FTL_Read_Error_Release:
free(pageBuffer);
free(spareBuffer);
bufferPrintf("ftl: _FTLRead error!\r\n");
return ret;
}
int ftl_setup() {
if(HasFTLInit)
return 0;
nand_setup();
Data = nand_get_geometry();
Data2 = nand_get_data();
if(VFL_Init() != 0) {
bufferPrintf("ftl: VFL_Init failed\r\n");
return -1;
}
if(FTL_Init() != 0) {
bufferPrintf("ftl: FTL_Init failed\r\n");
return -1;
}
int i;
int foundSignature = FALSE;
DebugPrintf("ftl: Attempting to read %d pages from first block of first bank.\r\n", Data->pagesPerBlock);
uint8_t* buffer = malloc(Data->bytesPerPage);
for(i = 0; i < Data->pagesPerBlock; i++) {
int ret;
if((ret = nand_read_alternate_ecc(0, i, buffer)) == 0) {
uint32_t id = *((uint32_t*) buffer);
if(id == FTL_ID_V1 || id == FTL_ID_V2) {
bufferPrintf("ftl: Found production format: %x\r\n", id);
foundSignature = TRUE;
break;
} else {
DebugPrintf("ftl: Found non-matching signature: %x\r\n", ((uint32_t*) buffer));
}
} else {
DebugPrintf("ftl: page %d of first bank is unreadable: %x!\r\n", i, ret);
}
}
free(buffer);
if(!foundSignature || !hasDeviceInfoBBT()) {
bufferPrintf("ftl: no signature or production format.\r\n");
return -1;
}
if(VFL_Open() != 0) {
bufferPrintf("ftl: VFL_Open failed\r\n");
return -1;
}
int pagesAvailable;
int bytesPerPage;