freemyipod r435 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r434‎ | r435 | r436 >
Date:20:02, 16 January 2011
Author:theseven
Status:new
Tags:
Comment:
emCORE: Allocate nandfsck buffers dynamically
Modified paths:
  • /emcore/trunk/target/ipodnano2g/ftl.c (modified) (history)

Diff [purge]

Index: emcore/trunk/target/ipodnano2g/ftl.c
@@ -417,19 +417,21 @@
418418 back if something fails while compacting a scattered page block. */
419419 static uint16_t ftl_offsets_backup[0x200] CACHEALIGN_ATTR;
420420
421 -/* Buffers needed for FTL recovery */
422 -static uint32_t blk_usn[0x2000] INITBSS_ATTR;
423 -static uint8_t blk_type[0x2000] INITBSS_ATTR;
424 -static uint32_t erasectr_usn[8] INITBSS_ATTR;
425 -static uint32_t pageusn[0x200] INITBSS_ATTR;
426 -static uint8_t pagedata[0x200][0x800] INITBSS_ATTR CACHEALIGN_ATTR;
 421+struct nandfsck_bss /* This will be malloc()ed if needed */
 422+{
 423+ /* Buffers needed for FTL recovery */
 424+ uint32_t blk_usn[0x2000];
 425+ int8_t blk_type[0x2000];
 426+ uint32_t erasectr_usn[8];
 427+ uint32_t pageusn[0x200];
 428+ uint8_t pagedata[0x200][0x800];
427429
428 -/* State information needed for FTL recovery */
429 -static uint32_t meta_usn INITBSS_ATTR;
430 -static uint32_t user_usn INITBSS_ATTR;
431 -static uint32_t allocmode INITBSS_ATTR;
432 -static uint32_t firstfree INITBSS_ATTR;
433 -
 430+ /* State information needed for FTL recovery */
 431+ uint32_t meta_usn;
 432+ uint32_t user_usn;
 433+ uint32_t allocmode;
 434+ uint32_t firstfree;
 435+};
434436 #endif
435437
436438
@@ -2236,31 +2238,32 @@
22372239
22382240
22392241 /* Block allocator for FTL recovery */
2240 -static uint32_t ftl_alloc_block() INITCODE_ATTR;
2241 -static uint32_t ftl_alloc_block()
 2242+static uint32_t ftl_alloc_block(struct nandfsck_bss* nb) INITCODE_ATTR;
 2243+static uint32_t ftl_alloc_block(struct nandfsck_bss* nb)
22422244 {
22432245 while (1)
22442246 {
2245 - for (; firstfree < ftl_nand_type->userblocks + 0x17; firstfree++)
2246 - if (!blk_type[firstfree]) break;
2247 - else if (allocmode && blk_type[firstfree] != 1)
 2247+ for (; nb->firstfree < ftl_nand_type->userblocks + 0x17; nb->firstfree++)
 2248+ if (!nb->blk_type[nb->firstfree]) break;
 2249+ else if (nb->allocmode && nb->blk_type[nb->firstfree] != 1)
22482250 {
2249 - if (ftl_erase_block(firstfree))
 2251+ if (ftl_erase_block(nb->firstfree))
22502252 {
2251 - cprintf(CONSOLE_BOOT, "Couldn't erase vBlock %d (pool alloc)!\n", firstfree);
 2253+ cprintf(CONSOLE_BOOT, "Couldn't erase vBlock %d (pool alloc)!\n",
 2254+ nb->firstfree);
22522255 return 1;
22532256 }
22542257 break;
22552258 }
2256 - if (firstfree < ftl_nand_type->userblocks + 0x17)
 2259+ if (nb->firstfree < ftl_nand_type->userblocks + 0x17)
22572260 {
2258 - blk_type[firstfree] = 1;
2259 - return firstfree++;
 2261+ nb->blk_type[nb->firstfree] = 1;
 2262+ return nb->firstfree++;
22602263 }
2261 - if (!allocmode)
 2264+ if (!nb->allocmode)
22622265 {
2263 - allocmode = 1;
2264 - firstfree = 0;
 2266+ nb->allocmode = 1;
 2267+ nb->firstfree = 0;
22652268 }
22662269 else
22672270 {
@@ -2279,6 +2282,8 @@
22802283 struct progressbar_state progressbar;
22812284 #endif
22822285
 2286+ struct nandfsck_bss* nb = (struct nandfsck_bss*)memalign(0x10, sizeof(struct nandfsck_bss));
 2287+
22832288 cputs(CONSOLE_BOOT, "Scanning flash...\n");
22842289 #ifdef HAVE_LCD
22852290 lcdconsole_progressbar(&progressbar, 0, ftl_nand_type->userblocks + 0x17);
@@ -2286,12 +2291,12 @@
22872292 uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
22882293 memset(&ftl_cxt, 0x00, sizeof(ftl_cxt));
22892294 memset(ftl_map, 0xff, sizeof(ftl_map));
2290 - memset(blk_usn, 0x00, sizeof(blk_usn));
2291 - memset(blk_type, 0x00, sizeof(blk_type));
 2295+ memset(nb->blk_usn, 0x00, sizeof(nb->blk_usn));
 2296+ memset(nb->blk_type, 0x00, sizeof(nb->blk_type));
22922297 memset(ftl_erasectr, 0x00, sizeof(ftl_erasectr));
2293 - memset(erasectr_usn, 0xff, sizeof(erasectr_usn));
2294 - user_usn = 0;
2295 - meta_usn = 0xffffffff;
 2298+ memset(nb->erasectr_usn, 0xff, sizeof(nb->erasectr_usn));
 2299+ nb->user_usn = 0;
 2300+ nb->meta_usn = 0xffffffff;
22962301 for (i = 0; i < ftl_nand_type->userblocks + 0x17; i++)
22972302 {
22982303 uint32_t ret = ftl_vfl_read((i + 1) * ppb - 1, 0, &ftl_sparebuffer[0], 1, 0);
@@ -2298,17 +2303,17 @@
22992304 if ((ret & 0x11F) == 0 && ftl_sparebuffer[0].meta.type == 0x41)
23002305 {
23012306 uint32_t lbn = ftl_sparebuffer[0].user.lpn / ppb;
2302 - if (ftl_sparebuffer[0].user.usn > user_usn)
2303 - user_usn = ftl_sparebuffer[0].user.usn;
2304 - if (ftl_sparebuffer[0].user.usn > blk_usn[lbn])
 2307+ if (ftl_sparebuffer[0].user.usn > nb->user_usn)
 2308+ nb->user_usn = ftl_sparebuffer[0].user.usn;
 2309+ if (ftl_sparebuffer[0].user.usn > nb->blk_usn[lbn])
23052310 {
23062311 if (ftl_map[lbn] != 0xffff)
2307 - blk_type[ftl_map[lbn]] = 5;
2308 - blk_usn[lbn] = ftl_sparebuffer[0].user.usn;
 2312+ nb->blk_type[ftl_map[lbn]] = 5;
 2313+ nb->blk_usn[lbn] = ftl_sparebuffer[0].user.usn;
23092314 ftl_map[lbn] = i;
2310 - blk_type[i] = 1;
 2315+ nb->blk_type[i] = 1;
23112316 }
2312 - else blk_type[i] = 5;
 2317+ else nb->blk_type[i] = 5;
23132318 }
23142319 else
23152320 for (j = 0; j < ppb; j++)
@@ -2317,25 +2322,25 @@
23182323 if (ret & 2) break;
23192324 if (ret & 0x11F)
23202325 {
2321 - blk_type[i] = 4;
 2326+ nb->blk_type[i] = 4;
23222327 continue;
23232328 }
23242329 if (ftl_sparebuffer[0].meta.type == 0x40)
23252330 {
2326 - blk_type[i] = 2;
 2331+ nb->blk_type[i] = 2;
23272332 break;
23282333 }
23292334 else if (ftl_sparebuffer[0].meta.type - 0x43 <= 4)
23302335 {
2331 - blk_type[i] = 3;
 2336+ nb->blk_type[i] = 3;
23322337 if (ftl_sparebuffer[0].meta.type == 0x46)
23332338 {
23342339 uint32_t idx = ftl_sparebuffer[0].meta.idx;
2335 - if (ftl_sparebuffer[0].meta.usn < meta_usn)
2336 - meta_usn = ftl_sparebuffer[0].meta.usn;
2337 - if (ftl_sparebuffer[0].meta.usn < erasectr_usn[idx])
 2340+ if (ftl_sparebuffer[0].meta.usn < nb->meta_usn)
 2341+ nb->meta_usn = ftl_sparebuffer[0].meta.usn;
 2342+ if (ftl_sparebuffer[0].meta.usn < nb->erasectr_usn[idx])
23382343 {
2339 - erasectr_usn[idx] = ftl_sparebuffer[0].meta.usn;
 2344+ nb->erasectr_usn[idx] = ftl_sparebuffer[0].meta.usn;
23402345 ret = ftl_vfl_read(i * ppb + j, &ftl_erasectr[idx << 10],
23412346 &ftl_sparebuffer[0], 1, 0);
23422347 if (ret & 0x11f) memset(&ftl_erasectr[idx << 10], 0, 0x800);
@@ -2366,6 +2371,7 @@
23672372 }
23682373 }
23692374 cprintf(CONSOLE_BOOT, "\n");
 2375+ free(nb);
23702376 return 1;
23712377 }
23722378 }
@@ -2391,7 +2397,7 @@
23922398 cputs(CONSOLE_BOOT, "Committing scattered pages...\n");
23932399 count = 0;
23942400 for (i = 0; i < ftl_nand_type->userblocks + 0x17; i++)
2395 - if (blk_type[i] == 2) count++;
 2401+ if (nb->blk_type[i] == 2) count++;
23962402 uint32_t block;
23972403 uint32_t dirty;
23982404 if (count)
@@ -2401,7 +2407,7 @@
24022408 #endif
24032409 count = 0;
24042410 for (i = 0; i < ftl_nand_type->userblocks + 0x17; i++)
2405 - if (blk_type[i] == 2)
 2411+ if (nb->blk_type[i] == 2)
24062412 {
24072413 block = 0xffff;
24082414 for (j = 0; j < ppb; j++)
@@ -2417,17 +2423,18 @@
24182424 cprintf(CONSOLE_BOOT, "Invalid block type %02X while reading "
24192425 "vPage %d (scattered page)!\n",
24202426 ftl_sparebuffer[0].meta.type, i * ppb + j);
 2427+ free(nb);
24212428 return 1;
24222429 }
24232430 if (block == 0xffff)
24242431 {
24252432 block = ftl_sparebuffer[0].user.lpn / ppb;
2426 - memset(pageusn, 0x00, 0x800);
2427 - memset(pagedata, 0x00, 0x100000);
 2433+ memset(nb->pageusn, 0x00, 0x800);
 2434+ memset(nb->pagedata, 0x00, 0x100000);
24282435 if (ftl_map[block] != 0xffff)
24292436 for (k = 0; k < ppb; k++)
24302437 {
2431 - uint32_t ret = ftl_vfl_read(ftl_map[block] * ppb + k, pagedata[k],
 2438+ uint32_t ret = ftl_vfl_read(ftl_map[block] * ppb + k, nb->pagedata[k],
24322439 &ftl_copyspare[0], 1, 0);
24332440 if (ret & 0x11F) continue;
24342441 if (ftl_copyspare[0].user.type != 0x40
@@ -2437,6 +2444,7 @@
24382445 "vPage %d (scattered page orig)!\n",
24392446 ftl_sparebuffer[0].meta.type,
24402447 ftl_map[block] * ppb + k);
 2448+ free(nb);
24412449 return 1;
24422450 }
24432451 if (block != ftl_copyspare[0].user.lpn / ppb)
@@ -2445,9 +2453,10 @@
24462454 "block (vPage %d, LPN %d)!\n",
24472455 ftl_map[block] * ppb + k,
24482456 ftl_sparebuffer[0].user.usn);
 2457+ free(nb);
24492458 return 1;
24502459 }
2451 - pageusn[k] = ftl_copyspare[0].user.usn;
 2460+ nb->pageusn[k] = ftl_copyspare[0].user.usn;
24522461 }
24532462 dirty = 0;
24542463 }
@@ -2456,15 +2465,16 @@
24572466 cprintf(CONSOLE_BOOT, "Foreign page in scattered page block "
24582467 "block (vPage %d, LPN %d)!\n",
24592468 i * ppb + j, ftl_sparebuffer[0].user.lpn);
 2469+ free(nb);
24602470 return 1;
24612471 }
24622472 uint32_t idx = ftl_sparebuffer[0].user.lpn % ppb;
2463 - if (ftl_sparebuffer[0].user.usn > user_usn)
2464 - user_usn = ftl_sparebuffer[0].user.usn;
2465 - if (ftl_sparebuffer[0].user.usn > pageusn[idx])
 2473+ if (ftl_sparebuffer[0].user.usn > nb->user_usn)
 2474+ nb->user_usn = ftl_sparebuffer[0].user.usn;
 2475+ if (ftl_sparebuffer[0].user.usn > nb->pageusn[idx])
24662476 {
2467 - pageusn[idx] = ftl_sparebuffer[0].user.usn;
2468 - memcpy(pagedata[idx], ftl_buffer, 0x800);
 2477+ nb->pageusn[idx] = ftl_sparebuffer[0].user.usn;
 2478+ memcpy(nb->pagedata[idx], ftl_buffer, 0x800);
24692479 dirty = 1;
24702480 }
24712481 }
@@ -2474,6 +2484,7 @@
24752485 {
24762486 cprintf(CONSOLE_BOOT, "Couldn't erase vBlock %d "
24772487 "(scattered page commit)!\n", i);
 2488+ free(nb);
24782489 return 1;
24792490 }
24802491 for (j = 0; j < ppb; j++)
@@ -2483,21 +2494,22 @@
24842495 #endif
24852496 memset(&ftl_sparebuffer[0], 0xFF, 0x40);
24862497 ftl_sparebuffer[0].user.lpn = block * ppb + j;
2487 - ftl_sparebuffer[0].user.usn = pageusn[j];
 2498+ ftl_sparebuffer[0].user.usn = nb->pageusn[j];
24882499 ftl_sparebuffer[0].user.type = 0x40;
24892500 if (j == ppb - 1) ftl_sparebuffer[0].user.type = 0x41;
2490 - if (ftl_vfl_write(i * ppb + j, 1, pagedata[j], &ftl_sparebuffer[0]))
 2501+ if (ftl_vfl_write(i * ppb + j, 1, nb->pagedata[j], &ftl_sparebuffer[0]))
24912502 {
24922503 cprintf(CONSOLE_BOOT, "Couldn't write vPage %d "
24932504 "(scattered page commit)!\n", i * ppb + j);
 2505+ free(nb);
24942506 return 1;
24952507 }
24962508 }
2497 - if (ftl_map[block] != 0xffff) blk_type[ftl_map[block]] = 5;
2498 - blk_type[i] = 1;
 2509+ if (ftl_map[block] != 0xffff) nb->blk_type[ftl_map[block]] = 5;
 2510+ nb->blk_type[i] = 1;
24992511 ftl_map[block] = i;
25002512 }
2501 - else blk_type[i] = 5;
 2513+ else nb->blk_type[i] = 5;
25022514 #ifdef HAVE_LCD
25032515 progressbar_setpos(&progressbar, ++count * ppb * 2, false);
25042516 #endif
@@ -2505,17 +2517,18 @@
25062518 }
25072519
25082520 cputs(CONSOLE_BOOT, "Fixing block map...\n");
2509 - allocmode = 0;
2510 - firstfree = 0;
2511 - for (i = 0; i < 3; i++) ftl_cxt.ftlctrlblocks[i] = ftl_alloc_block();
2512 - for (i = 0; i < 20; i++) ftl_cxt.blockpool[i] = ftl_alloc_block();
 2521+ nb->allocmode = 0;
 2522+ nb->firstfree = 0;
 2523+ for (i = 0; i < 3; i++) ftl_cxt.ftlctrlblocks[i] = ftl_alloc_block(nb);
 2524+ for (i = 0; i < 20; i++) ftl_cxt.blockpool[i] = ftl_alloc_block(nb);
25132525 for (i = 0; i < ftl_nand_type->userblocks; i++)
25142526 if (ftl_map[i] == 0xffff)
2515 - ftl_map[i] = ftl_alloc_block();
2516 - ftl_cxt.usn = meta_usn - 1;
2517 - ftl_cxt.nextblockusn = user_usn + 1;
 2527+ ftl_map[i] = ftl_alloc_block(nb);
 2528+ ftl_cxt.usn = nb->meta_usn - 1;
 2529+ ftl_cxt.nextblockusn = nb->user_usn + 1;
25182530 ftl_cxt.freecount = 20;
25192531 ftl_cxt.clean_flag = 1;
 2532+ free(nb);
25202533
25212534 cputs(CONSOLE_BOOT, "Committing FTL context...\n");
25222535 uint32_t blockmappages = ftl_nand_type->userblocks >> 10;