inftlmount.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. * inftlmount.c -- INFTL mount code with extensive checks.
  3. *
  4. * Author: Greg Ungerer (gerg@snapgear.com)
  5. * Copyright © 2002-2003, Greg Ungerer (gerg@snapgear.com)
  6. *
  7. * Based heavily on the nftlmount.c code which is:
  8. * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
  9. * Copyright © 2000 Netgem S.A.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <asm/errno.h>
  28. #include <asm/io.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/delay.h>
  31. #include <linux/slab.h>
  32. #include <linux/mtd/mtd.h>
  33. #include <linux/mtd/nftl.h>
  34. #include <linux/mtd/inftl.h>
  35. /*
  36. * find_boot_record: Find the INFTL Media Header and its Spare copy which
  37. * contains the various device information of the INFTL partition and
  38. * Bad Unit Table. Update the PUtable[] table according to the Bad
  39. * Unit Table. PUtable[] is used for management of Erase Unit in
  40. * other routines in inftlcore.c and inftlmount.c.
  41. */
  42. static int find_boot_record(struct INFTLrecord *inftl)
  43. {
  44. struct inftl_unittail h1;
  45. //struct inftl_oob oob;
  46. unsigned int i, block;
  47. u8 buf[SECTORSIZE];
  48. struct INFTLMediaHeader *mh = &inftl->MediaHdr;
  49. struct mtd_info *mtd = inftl->mbd.mtd;
  50. struct INFTLPartition *ip;
  51. size_t retlen;
  52. pr_debug("INFTL: find_boot_record(inftl=%p)\n", inftl);
  53. /*
  54. * Assume logical EraseSize == physical erasesize for starting the
  55. * scan. We'll sort it out later if we find a MediaHeader which says
  56. * otherwise.
  57. */
  58. inftl->EraseSize = inftl->mbd.mtd->erasesize;
  59. inftl->nb_blocks = (u32)inftl->mbd.mtd->size / inftl->EraseSize;
  60. inftl->MediaUnit = BLOCK_NIL;
  61. /* Search for a valid boot record */
  62. for (block = 0; block < inftl->nb_blocks; block++) {
  63. int ret;
  64. /*
  65. * Check for BNAND header first. Then whinge if it's found
  66. * but later checks fail.
  67. */
  68. ret = mtd_read(mtd, block * inftl->EraseSize, SECTORSIZE,
  69. &retlen, buf);
  70. /* We ignore ret in case the ECC of the MediaHeader is invalid
  71. (which is apparently acceptable) */
  72. if (retlen != SECTORSIZE) {
  73. static int warncount = 5;
  74. if (warncount) {
  75. printk(KERN_WARNING "INFTL: block read at 0x%x "
  76. "of mtd%d failed: %d\n",
  77. block * inftl->EraseSize,
  78. inftl->mbd.mtd->index, ret);
  79. if (!--warncount)
  80. printk(KERN_WARNING "INFTL: further "
  81. "failures for this block will "
  82. "not be printed\n");
  83. }
  84. continue;
  85. }
  86. if (retlen < 6 || memcmp(buf, "BNAND", 6)) {
  87. /* BNAND\0 not found. Continue */
  88. continue;
  89. }
  90. /* To be safer with BIOS, also use erase mark as discriminant */
  91. ret = inftl_read_oob(mtd,
  92. block * inftl->EraseSize + SECTORSIZE + 8,
  93. 8, &retlen,(char *)&h1);
  94. if (ret < 0) {
  95. printk(KERN_WARNING "INFTL: ANAND header found at "
  96. "0x%x in mtd%d, but OOB data read failed "
  97. "(err %d)\n", block * inftl->EraseSize,
  98. inftl->mbd.mtd->index, ret);
  99. continue;
  100. }
  101. /*
  102. * This is the first we've seen.
  103. * Copy the media header structure into place.
  104. */
  105. memcpy(mh, buf, sizeof(struct INFTLMediaHeader));
  106. /* Read the spare media header at offset 4096 */
  107. mtd_read(mtd, block * inftl->EraseSize + 4096, SECTORSIZE,
  108. &retlen, buf);
  109. if (retlen != SECTORSIZE) {
  110. printk(KERN_WARNING "INFTL: Unable to read spare "
  111. "Media Header\n");
  112. return -1;
  113. }
  114. /* Check if this one is the same as the first one we found. */
  115. if (memcmp(mh, buf, sizeof(struct INFTLMediaHeader))) {
  116. printk(KERN_WARNING "INFTL: Primary and spare Media "
  117. "Headers disagree.\n");
  118. return -1;
  119. }
  120. mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
  121. mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
  122. mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
  123. mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
  124. mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
  125. mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
  126. pr_debug("INFTL: Media Header ->\n"
  127. " bootRecordID = %s\n"
  128. " NoOfBootImageBlocks = %d\n"
  129. " NoOfBinaryPartitions = %d\n"
  130. " NoOfBDTLPartitions = %d\n"
  131. " BlockMultiplerBits = %d\n"
  132. " FormatFlgs = %d\n"
  133. " OsakVersion = 0x%x\n"
  134. " PercentUsed = %d\n",
  135. mh->bootRecordID, mh->NoOfBootImageBlocks,
  136. mh->NoOfBinaryPartitions,
  137. mh->NoOfBDTLPartitions,
  138. mh->BlockMultiplierBits, mh->FormatFlags,
  139. mh->OsakVersion, mh->PercentUsed);
  140. if (mh->NoOfBDTLPartitions == 0) {
  141. printk(KERN_WARNING "INFTL: Media Header sanity check "
  142. "failed: NoOfBDTLPartitions (%d) == 0, "
  143. "must be at least 1\n", mh->NoOfBDTLPartitions);
  144. return -1;
  145. }
  146. if ((mh->NoOfBDTLPartitions + mh->NoOfBinaryPartitions) > 4) {
  147. printk(KERN_WARNING "INFTL: Media Header sanity check "
  148. "failed: Total Partitions (%d) > 4, "
  149. "BDTL=%d Binary=%d\n", mh->NoOfBDTLPartitions +
  150. mh->NoOfBinaryPartitions,
  151. mh->NoOfBDTLPartitions,
  152. mh->NoOfBinaryPartitions);
  153. return -1;
  154. }
  155. if (mh->BlockMultiplierBits > 1) {
  156. printk(KERN_WARNING "INFTL: sorry, we don't support "
  157. "UnitSizeFactor 0x%02x\n",
  158. mh->BlockMultiplierBits);
  159. return -1;
  160. } else if (mh->BlockMultiplierBits == 1) {
  161. printk(KERN_WARNING "INFTL: support for INFTL with "
  162. "UnitSizeFactor 0x%02x is experimental\n",
  163. mh->BlockMultiplierBits);
  164. inftl->EraseSize = inftl->mbd.mtd->erasesize <<
  165. mh->BlockMultiplierBits;
  166. inftl->nb_blocks = (u32)inftl->mbd.mtd->size / inftl->EraseSize;
  167. block >>= mh->BlockMultiplierBits;
  168. }
  169. /* Scan the partitions */
  170. for (i = 0; (i < 4); i++) {
  171. ip = &mh->Partitions[i];
  172. ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
  173. ip->firstUnit = le32_to_cpu(ip->firstUnit);
  174. ip->lastUnit = le32_to_cpu(ip->lastUnit);
  175. ip->flags = le32_to_cpu(ip->flags);
  176. ip->spareUnits = le32_to_cpu(ip->spareUnits);
  177. ip->Reserved0 = le32_to_cpu(ip->Reserved0);
  178. pr_debug(" PARTITION[%d] ->\n"
  179. " virtualUnits = %d\n"
  180. " firstUnit = %d\n"
  181. " lastUnit = %d\n"
  182. " flags = 0x%x\n"
  183. " spareUnits = %d\n",
  184. i, ip->virtualUnits, ip->firstUnit,
  185. ip->lastUnit, ip->flags,
  186. ip->spareUnits);
  187. if (ip->Reserved0 != ip->firstUnit) {
  188. struct erase_info *instr = &inftl->instr;
  189. /*
  190. * Most likely this is using the
  191. * undocumented qiuck mount feature.
  192. * We don't support that, we will need
  193. * to erase the hidden block for full
  194. * compatibility.
  195. */
  196. instr->addr = ip->Reserved0 * inftl->EraseSize;
  197. instr->len = inftl->EraseSize;
  198. mtd_erase(mtd, instr);
  199. }
  200. if ((ip->lastUnit - ip->firstUnit + 1) < ip->virtualUnits) {
  201. printk(KERN_WARNING "INFTL: Media Header "
  202. "Partition %d sanity check failed\n"
  203. " firstUnit %d : lastUnit %d > "
  204. "virtualUnits %d\n", i, ip->lastUnit,
  205. ip->firstUnit, ip->Reserved0);
  206. return -1;
  207. }
  208. if (ip->Reserved1 != 0) {
  209. printk(KERN_WARNING "INFTL: Media Header "
  210. "Partition %d sanity check failed: "
  211. "Reserved1 %d != 0\n",
  212. i, ip->Reserved1);
  213. return -1;
  214. }
  215. if (ip->flags & INFTL_BDTL)
  216. break;
  217. }
  218. if (i >= 4) {
  219. printk(KERN_WARNING "INFTL: Media Header Partition "
  220. "sanity check failed:\n No partition "
  221. "marked as Disk Partition\n");
  222. return -1;
  223. }
  224. inftl->nb_boot_blocks = ip->firstUnit;
  225. inftl->numvunits = ip->virtualUnits;
  226. if (inftl->numvunits > (inftl->nb_blocks -
  227. inftl->nb_boot_blocks - 2)) {
  228. printk(KERN_WARNING "INFTL: Media Header sanity check "
  229. "failed:\n numvunits (%d) > nb_blocks "
  230. "(%d) - nb_boot_blocks(%d) - 2\n",
  231. inftl->numvunits, inftl->nb_blocks,
  232. inftl->nb_boot_blocks);
  233. return -1;
  234. }
  235. inftl->mbd.size = inftl->numvunits *
  236. (inftl->EraseSize / SECTORSIZE);
  237. /*
  238. * Block count is set to last used EUN (we won't need to keep
  239. * any meta-data past that point).
  240. */
  241. inftl->firstEUN = ip->firstUnit;
  242. inftl->lastEUN = ip->lastUnit;
  243. inftl->nb_blocks = ip->lastUnit + 1;
  244. /* Memory alloc */
  245. inftl->PUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
  246. if (!inftl->PUtable) {
  247. printk(KERN_WARNING "INFTL: allocation of PUtable "
  248. "failed (%zd bytes)\n",
  249. inftl->nb_blocks * sizeof(u16));
  250. return -ENOMEM;
  251. }
  252. inftl->VUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
  253. if (!inftl->VUtable) {
  254. kfree(inftl->PUtable);
  255. printk(KERN_WARNING "INFTL: allocation of VUtable "
  256. "failed (%zd bytes)\n",
  257. inftl->nb_blocks * sizeof(u16));
  258. return -ENOMEM;
  259. }
  260. /* Mark the blocks before INFTL MediaHeader as reserved */
  261. for (i = 0; i < inftl->nb_boot_blocks; i++)
  262. inftl->PUtable[i] = BLOCK_RESERVED;
  263. /* Mark all remaining blocks as potentially containing data */
  264. for (; i < inftl->nb_blocks; i++)
  265. inftl->PUtable[i] = BLOCK_NOTEXPLORED;
  266. /* Mark this boot record (NFTL MediaHeader) block as reserved */
  267. inftl->PUtable[block] = BLOCK_RESERVED;
  268. /* Read Bad Erase Unit Table and modify PUtable[] accordingly */
  269. for (i = 0; i < inftl->nb_blocks; i++) {
  270. int physblock;
  271. /* If any of the physical eraseblocks are bad, don't
  272. use the unit. */
  273. for (physblock = 0; physblock < inftl->EraseSize; physblock += inftl->mbd.mtd->erasesize) {
  274. if (mtd_block_isbad(inftl->mbd.mtd,
  275. i * inftl->EraseSize + physblock))
  276. inftl->PUtable[i] = BLOCK_RESERVED;
  277. }
  278. }
  279. inftl->MediaUnit = block;
  280. return 0;
  281. }
  282. /* Not found. */
  283. return -1;
  284. }
  285. static int memcmpb(void *a, int c, int n)
  286. {
  287. int i;
  288. for (i = 0; i < n; i++) {
  289. if (c != ((unsigned char *)a)[i])
  290. return 1;
  291. }
  292. return 0;
  293. }
  294. /*
  295. * check_free_sector: check if a free sector is actually FREE,
  296. * i.e. All 0xff in data and oob area.
  297. */
  298. static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
  299. int len, int check_oob)
  300. {
  301. u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
  302. struct mtd_info *mtd = inftl->mbd.mtd;
  303. size_t retlen;
  304. int i;
  305. for (i = 0; i < len; i += SECTORSIZE) {
  306. if (mtd_read(mtd, address, SECTORSIZE, &retlen, buf))
  307. return -1;
  308. if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
  309. return -1;
  310. if (check_oob) {
  311. if(inftl_read_oob(mtd, address, mtd->oobsize,
  312. &retlen, &buf[SECTORSIZE]) < 0)
  313. return -1;
  314. if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
  315. return -1;
  316. }
  317. address += SECTORSIZE;
  318. }
  319. return 0;
  320. }
  321. /*
  322. * INFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase
  323. * Unit and Update INFTL metadata. Each erase operation is
  324. * checked with check_free_sectors.
  325. *
  326. * Return: 0 when succeed, -1 on error.
  327. *
  328. * ToDo: 1. Is it necessary to check_free_sector after erasing ??
  329. */
  330. int INFTL_formatblock(struct INFTLrecord *inftl, int block)
  331. {
  332. size_t retlen;
  333. struct inftl_unittail uci;
  334. struct erase_info *instr = &inftl->instr;
  335. struct mtd_info *mtd = inftl->mbd.mtd;
  336. int physblock;
  337. pr_debug("INFTL: INFTL_formatblock(inftl=%p,block=%d)\n", inftl, block);
  338. memset(instr, 0, sizeof(struct erase_info));
  339. /* FIXME: Shouldn't we be setting the 'discarded' flag to zero
  340. _first_? */
  341. /* Use async erase interface, test return code */
  342. instr->addr = block * inftl->EraseSize;
  343. instr->len = inftl->mbd.mtd->erasesize;
  344. /* Erase one physical eraseblock at a time, even though the NAND api
  345. allows us to group them. This way we if we have a failure, we can
  346. mark only the failed block in the bbt. */
  347. for (physblock = 0; physblock < inftl->EraseSize;
  348. physblock += instr->len, instr->addr += instr->len) {
  349. int ret;
  350. ret = mtd_erase(inftl->mbd.mtd, instr);
  351. if (ret) {
  352. printk(KERN_WARNING "INFTL: error while formatting block %d\n",
  353. block);
  354. goto fail;
  355. }
  356. /*
  357. * Check the "freeness" of Erase Unit before updating metadata.
  358. * FixMe: is this check really necessary? Since we have check
  359. * the return code after the erase operation.
  360. */
  361. if (check_free_sectors(inftl, instr->addr, instr->len, 1) != 0)
  362. goto fail;
  363. }
  364. uci.EraseMark = cpu_to_le16(ERASE_MARK);
  365. uci.EraseMark1 = cpu_to_le16(ERASE_MARK);
  366. uci.Reserved[0] = 0;
  367. uci.Reserved[1] = 0;
  368. uci.Reserved[2] = 0;
  369. uci.Reserved[3] = 0;
  370. instr->addr = block * inftl->EraseSize + SECTORSIZE * 2;
  371. if (inftl_write_oob(mtd, instr->addr + 8, 8, &retlen, (char *)&uci) < 0)
  372. goto fail;
  373. return 0;
  374. fail:
  375. /* could not format, update the bad block table (caller is responsible
  376. for setting the PUtable to BLOCK_RESERVED on failure) */
  377. mtd_block_markbad(inftl->mbd.mtd, instr->addr);
  378. return -1;
  379. }
  380. /*
  381. * format_chain: Format an invalid Virtual Unit chain. It frees all the Erase
  382. * Units in a Virtual Unit Chain, i.e. all the units are disconnected.
  383. *
  384. * Since the chain is invalid then we will have to erase it from its
  385. * head (normally for INFTL we go from the oldest). But if it has a
  386. * loop then there is no oldest...
  387. */
  388. static void format_chain(struct INFTLrecord *inftl, unsigned int first_block)
  389. {
  390. unsigned int block = first_block, block1;
  391. printk(KERN_WARNING "INFTL: formatting chain at block %d\n",
  392. first_block);
  393. for (;;) {
  394. block1 = inftl->PUtable[block];
  395. printk(KERN_WARNING "INFTL: formatting block %d\n", block);
  396. if (INFTL_formatblock(inftl, block) < 0) {
  397. /*
  398. * Cannot format !!!! Mark it as Bad Unit,
  399. */
  400. inftl->PUtable[block] = BLOCK_RESERVED;
  401. } else {
  402. inftl->PUtable[block] = BLOCK_FREE;
  403. }
  404. /* Goto next block on the chain */
  405. block = block1;
  406. if (block == BLOCK_NIL || block >= inftl->lastEUN)
  407. break;
  408. }
  409. }
  410. void INFTL_dumptables(struct INFTLrecord *s)
  411. {
  412. int i;
  413. pr_debug("-------------------------------------------"
  414. "----------------------------------\n");
  415. pr_debug("VUtable[%d] ->", s->nb_blocks);
  416. for (i = 0; i < s->nb_blocks; i++) {
  417. if ((i % 8) == 0)
  418. pr_debug("\n%04x: ", i);
  419. pr_debug("%04x ", s->VUtable[i]);
  420. }
  421. pr_debug("\n-------------------------------------------"
  422. "----------------------------------\n");
  423. pr_debug("PUtable[%d-%d=%d] ->", s->firstEUN, s->lastEUN, s->nb_blocks);
  424. for (i = 0; i <= s->lastEUN; i++) {
  425. if ((i % 8) == 0)
  426. pr_debug("\n%04x: ", i);
  427. pr_debug("%04x ", s->PUtable[i]);
  428. }
  429. pr_debug("\n-------------------------------------------"
  430. "----------------------------------\n");
  431. pr_debug("INFTL ->\n"
  432. " EraseSize = %d\n"
  433. " h/s/c = %d/%d/%d\n"
  434. " numvunits = %d\n"
  435. " firstEUN = %d\n"
  436. " lastEUN = %d\n"
  437. " numfreeEUNs = %d\n"
  438. " LastFreeEUN = %d\n"
  439. " nb_blocks = %d\n"
  440. " nb_boot_blocks = %d",
  441. s->EraseSize, s->heads, s->sectors, s->cylinders,
  442. s->numvunits, s->firstEUN, s->lastEUN, s->numfreeEUNs,
  443. s->LastFreeEUN, s->nb_blocks, s->nb_boot_blocks);
  444. pr_debug("\n-------------------------------------------"
  445. "----------------------------------\n");
  446. }
  447. void INFTL_dumpVUchains(struct INFTLrecord *s)
  448. {
  449. int logical, block, i;
  450. pr_debug("-------------------------------------------"
  451. "----------------------------------\n");
  452. pr_debug("INFTL Virtual Unit Chains:\n");
  453. for (logical = 0; logical < s->nb_blocks; logical++) {
  454. block = s->VUtable[logical];
  455. if (block >= s->nb_blocks)
  456. continue;
  457. pr_debug(" LOGICAL %d --> %d ", logical, block);
  458. for (i = 0; i < s->nb_blocks; i++) {
  459. if (s->PUtable[block] == BLOCK_NIL)
  460. break;
  461. block = s->PUtable[block];
  462. pr_debug("%d ", block);
  463. }
  464. pr_debug("\n");
  465. }
  466. pr_debug("-------------------------------------------"
  467. "----------------------------------\n");
  468. }
  469. int INFTL_mount(struct INFTLrecord *s)
  470. {
  471. struct mtd_info *mtd = s->mbd.mtd;
  472. unsigned int block, first_block, prev_block, last_block;
  473. unsigned int first_logical_block, logical_block, erase_mark;
  474. int chain_length, do_format_chain;
  475. struct inftl_unithead1 h0;
  476. struct inftl_unittail h1;
  477. size_t retlen;
  478. int i;
  479. u8 *ANACtable, ANAC;
  480. pr_debug("INFTL: INFTL_mount(inftl=%p)\n", s);
  481. /* Search for INFTL MediaHeader and Spare INFTL Media Header */
  482. if (find_boot_record(s) < 0) {
  483. printk(KERN_WARNING "INFTL: could not find valid boot record?\n");
  484. return -ENXIO;
  485. }
  486. /* Init the logical to physical table */
  487. for (i = 0; i < s->nb_blocks; i++)
  488. s->VUtable[i] = BLOCK_NIL;
  489. logical_block = block = BLOCK_NIL;
  490. /* Temporary buffer to store ANAC numbers. */
  491. ANACtable = kcalloc(s->nb_blocks, sizeof(u8), GFP_KERNEL);
  492. if (!ANACtable) {
  493. printk(KERN_WARNING "INFTL: allocation of ANACtable "
  494. "failed (%zd bytes)\n",
  495. s->nb_blocks * sizeof(u8));
  496. return -ENOMEM;
  497. }
  498. /*
  499. * First pass is to explore each physical unit, and construct the
  500. * virtual chains that exist (newest physical unit goes into VUtable).
  501. * Any block that is in any way invalid will be left in the
  502. * NOTEXPLORED state. Then at the end we will try to format it and
  503. * mark it as free.
  504. */
  505. pr_debug("INFTL: pass 1, explore each unit\n");
  506. for (first_block = s->firstEUN; first_block <= s->lastEUN; first_block++) {
  507. if (s->PUtable[first_block] != BLOCK_NOTEXPLORED)
  508. continue;
  509. do_format_chain = 0;
  510. first_logical_block = BLOCK_NIL;
  511. last_block = BLOCK_NIL;
  512. block = first_block;
  513. for (chain_length = 0; ; chain_length++) {
  514. if ((chain_length == 0) &&
  515. (s->PUtable[block] != BLOCK_NOTEXPLORED)) {
  516. /* Nothing to do here, onto next block */
  517. break;
  518. }
  519. if (inftl_read_oob(mtd, block * s->EraseSize + 8,
  520. 8, &retlen, (char *)&h0) < 0 ||
  521. inftl_read_oob(mtd, block * s->EraseSize +
  522. 2 * SECTORSIZE + 8, 8, &retlen,
  523. (char *)&h1) < 0) {
  524. /* Should never happen? */
  525. do_format_chain++;
  526. break;
  527. }
  528. logical_block = le16_to_cpu(h0.virtualUnitNo);
  529. prev_block = le16_to_cpu(h0.prevUnitNo);
  530. erase_mark = le16_to_cpu((h1.EraseMark | h1.EraseMark1));
  531. ANACtable[block] = h0.ANAC;
  532. /* Previous block is relative to start of Partition */
  533. if (prev_block < s->nb_blocks)
  534. prev_block += s->firstEUN;
  535. /* Already explored partial chain? */
  536. if (s->PUtable[block] != BLOCK_NOTEXPLORED) {
  537. /* Check if chain for this logical */
  538. if (logical_block == first_logical_block) {
  539. if (last_block != BLOCK_NIL)
  540. s->PUtable[last_block] = block;
  541. }
  542. break;
  543. }
  544. /* Check for invalid block */
  545. if (erase_mark != ERASE_MARK) {
  546. printk(KERN_WARNING "INFTL: corrupt block %d "
  547. "in chain %d, chain length %d, erase "
  548. "mark 0x%x?\n", block, first_block,
  549. chain_length, erase_mark);
  550. /*
  551. * Assume end of chain, probably incomplete
  552. * fold/erase...
  553. */
  554. if (chain_length == 0)
  555. do_format_chain++;
  556. break;
  557. }
  558. /* Check for it being free already then... */
  559. if ((logical_block == BLOCK_FREE) ||
  560. (logical_block == BLOCK_NIL)) {
  561. s->PUtable[block] = BLOCK_FREE;
  562. break;
  563. }
  564. /* Sanity checks on block numbers */
  565. if ((logical_block >= s->nb_blocks) ||
  566. ((prev_block >= s->nb_blocks) &&
  567. (prev_block != BLOCK_NIL))) {
  568. if (chain_length > 0) {
  569. printk(KERN_WARNING "INFTL: corrupt "
  570. "block %d in chain %d?\n",
  571. block, first_block);
  572. do_format_chain++;
  573. }
  574. break;
  575. }
  576. if (first_logical_block == BLOCK_NIL) {
  577. first_logical_block = logical_block;
  578. } else {
  579. if (first_logical_block != logical_block) {
  580. /* Normal for folded chain... */
  581. break;
  582. }
  583. }
  584. /*
  585. * Current block is valid, so if we followed a virtual
  586. * chain to get here then we can set the previous
  587. * block pointer in our PUtable now. Then move onto
  588. * the previous block in the chain.
  589. */
  590. s->PUtable[block] = BLOCK_NIL;
  591. if (last_block != BLOCK_NIL)
  592. s->PUtable[last_block] = block;
  593. last_block = block;
  594. block = prev_block;
  595. /* Check for end of chain */
  596. if (block == BLOCK_NIL)
  597. break;
  598. /* Validate next block before following it... */
  599. if (block > s->lastEUN) {
  600. printk(KERN_WARNING "INFTL: invalid previous "
  601. "block %d in chain %d?\n", block,
  602. first_block);
  603. do_format_chain++;
  604. break;
  605. }
  606. }
  607. if (do_format_chain) {
  608. format_chain(s, first_block);
  609. continue;
  610. }
  611. /*
  612. * Looks like a valid chain then. It may not really be the
  613. * newest block in the chain, but it is the newest we have
  614. * found so far. We might update it in later iterations of
  615. * this loop if we find something newer.
  616. */
  617. s->VUtable[first_logical_block] = first_block;
  618. logical_block = BLOCK_NIL;
  619. }
  620. INFTL_dumptables(s);
  621. /*
  622. * Second pass, check for infinite loops in chains. These are
  623. * possible because we don't update the previous pointers when
  624. * we fold chains. No big deal, just fix them up in PUtable.
  625. */
  626. pr_debug("INFTL: pass 2, validate virtual chains\n");
  627. for (logical_block = 0; logical_block < s->numvunits; logical_block++) {
  628. block = s->VUtable[logical_block];
  629. last_block = BLOCK_NIL;
  630. /* Check for free/reserved/nil */
  631. if (block >= BLOCK_RESERVED)
  632. continue;
  633. ANAC = ANACtable[block];
  634. for (i = 0; i < s->numvunits; i++) {
  635. if (s->PUtable[block] == BLOCK_NIL)
  636. break;
  637. if (s->PUtable[block] > s->lastEUN) {
  638. printk(KERN_WARNING "INFTL: invalid prev %d, "
  639. "in virtual chain %d\n",
  640. s->PUtable[block], logical_block);
  641. s->PUtable[block] = BLOCK_NIL;
  642. }
  643. if (ANACtable[block] != ANAC) {
  644. /*
  645. * Chain must point back to itself. This is ok,
  646. * but we will need adjust the tables with this
  647. * newest block and oldest block.
  648. */
  649. s->VUtable[logical_block] = block;
  650. s->PUtable[last_block] = BLOCK_NIL;
  651. break;
  652. }
  653. ANAC--;
  654. last_block = block;
  655. block = s->PUtable[block];
  656. }
  657. if (i >= s->nb_blocks) {
  658. /*
  659. * Uhoo, infinite chain with valid ANACS!
  660. * Format whole chain...
  661. */
  662. format_chain(s, first_block);
  663. }
  664. }
  665. INFTL_dumptables(s);
  666. INFTL_dumpVUchains(s);
  667. /*
  668. * Third pass, format unreferenced blocks and init free block count.
  669. */
  670. s->numfreeEUNs = 0;
  671. s->LastFreeEUN = BLOCK_NIL;
  672. pr_debug("INFTL: pass 3, format unused blocks\n");
  673. for (block = s->firstEUN; block <= s->lastEUN; block++) {
  674. if (s->PUtable[block] == BLOCK_NOTEXPLORED) {
  675. printk("INFTL: unreferenced block %d, formatting it\n",
  676. block);
  677. if (INFTL_formatblock(s, block) < 0)
  678. s->PUtable[block] = BLOCK_RESERVED;
  679. else
  680. s->PUtable[block] = BLOCK_FREE;
  681. }
  682. if (s->PUtable[block] == BLOCK_FREE) {
  683. s->numfreeEUNs++;
  684. if (s->LastFreeEUN == BLOCK_NIL)
  685. s->LastFreeEUN = block;
  686. }
  687. }
  688. kfree(ANACtable);
  689. return 0;
  690. }