inftlmount.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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 <asm/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. instr->mtd = inftl->mbd.mtd;
  190. /*
  191. * Most likely this is using the
  192. * undocumented qiuck mount feature.
  193. * We don't support that, we will need
  194. * to erase the hidden block for full
  195. * compatibility.
  196. */
  197. instr->addr = ip->Reserved0 * inftl->EraseSize;
  198. instr->len = inftl->EraseSize;
  199. mtd_erase(mtd, instr);
  200. }
  201. if ((ip->lastUnit - ip->firstUnit + 1) < ip->virtualUnits) {
  202. printk(KERN_WARNING "INFTL: Media Header "
  203. "Partition %d sanity check failed\n"
  204. " firstUnit %d : lastUnit %d > "
  205. "virtualUnits %d\n", i, ip->lastUnit,
  206. ip->firstUnit, ip->Reserved0);
  207. return -1;
  208. }
  209. if (ip->Reserved1 != 0) {
  210. printk(KERN_WARNING "INFTL: Media Header "
  211. "Partition %d sanity check failed: "
  212. "Reserved1 %d != 0\n",
  213. i, ip->Reserved1);
  214. return -1;
  215. }
  216. if (ip->flags & INFTL_BDTL)
  217. break;
  218. }
  219. if (i >= 4) {
  220. printk(KERN_WARNING "INFTL: Media Header Partition "
  221. "sanity check failed:\n No partition "
  222. "marked as Disk Partition\n");
  223. return -1;
  224. }
  225. inftl->nb_boot_blocks = ip->firstUnit;
  226. inftl->numvunits = ip->virtualUnits;
  227. if (inftl->numvunits > (inftl->nb_blocks -
  228. inftl->nb_boot_blocks - 2)) {
  229. printk(KERN_WARNING "INFTL: Media Header sanity check "
  230. "failed:\n numvunits (%d) > nb_blocks "
  231. "(%d) - nb_boot_blocks(%d) - 2\n",
  232. inftl->numvunits, inftl->nb_blocks,
  233. inftl->nb_boot_blocks);
  234. return -1;
  235. }
  236. inftl->mbd.size = inftl->numvunits *
  237. (inftl->EraseSize / SECTORSIZE);
  238. /*
  239. * Block count is set to last used EUN (we won't need to keep
  240. * any meta-data past that point).
  241. */
  242. inftl->firstEUN = ip->firstUnit;
  243. inftl->lastEUN = ip->lastUnit;
  244. inftl->nb_blocks = ip->lastUnit + 1;
  245. /* Memory alloc */
  246. inftl->PUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
  247. if (!inftl->PUtable) {
  248. printk(KERN_WARNING "INFTL: allocation of PUtable "
  249. "failed (%zd bytes)\n",
  250. inftl->nb_blocks * sizeof(u16));
  251. return -ENOMEM;
  252. }
  253. inftl->VUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
  254. if (!inftl->VUtable) {
  255. kfree(inftl->PUtable);
  256. printk(KERN_WARNING "INFTL: allocation of VUtable "
  257. "failed (%zd bytes)\n",
  258. inftl->nb_blocks * sizeof(u16));
  259. return -ENOMEM;
  260. }
  261. /* Mark the blocks before INFTL MediaHeader as reserved */
  262. for (i = 0; i < inftl->nb_boot_blocks; i++)
  263. inftl->PUtable[i] = BLOCK_RESERVED;
  264. /* Mark all remaining blocks as potentially containing data */
  265. for (; i < inftl->nb_blocks; i++)
  266. inftl->PUtable[i] = BLOCK_NOTEXPLORED;
  267. /* Mark this boot record (NFTL MediaHeader) block as reserved */
  268. inftl->PUtable[block] = BLOCK_RESERVED;
  269. /* Read Bad Erase Unit Table and modify PUtable[] accordingly */
  270. for (i = 0; i < inftl->nb_blocks; i++) {
  271. int physblock;
  272. /* If any of the physical eraseblocks are bad, don't
  273. use the unit. */
  274. for (physblock = 0; physblock < inftl->EraseSize; physblock += inftl->mbd.mtd->erasesize) {
  275. if (mtd_block_isbad(inftl->mbd.mtd,
  276. i * inftl->EraseSize + physblock))
  277. inftl->PUtable[i] = BLOCK_RESERVED;
  278. }
  279. }
  280. inftl->MediaUnit = block;
  281. return 0;
  282. }
  283. /* Not found. */
  284. return -1;
  285. }
  286. static int memcmpb(void *a, int c, int n)
  287. {
  288. int i;
  289. for (i = 0; i < n; i++) {
  290. if (c != ((unsigned char *)a)[i])
  291. return 1;
  292. }
  293. return 0;
  294. }
  295. /*
  296. * check_free_sector: check if a free sector is actually FREE,
  297. * i.e. All 0xff in data and oob area.
  298. */
  299. static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
  300. int len, int check_oob)
  301. {
  302. u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
  303. struct mtd_info *mtd = inftl->mbd.mtd;
  304. size_t retlen;
  305. int i;
  306. for (i = 0; i < len; i += SECTORSIZE) {
  307. if (mtd_read(mtd, address, SECTORSIZE, &retlen, buf))
  308. return -1;
  309. if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
  310. return -1;
  311. if (check_oob) {
  312. if(inftl_read_oob(mtd, address, mtd->oobsize,
  313. &retlen, &buf[SECTORSIZE]) < 0)
  314. return -1;
  315. if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
  316. return -1;
  317. }
  318. address += SECTORSIZE;
  319. }
  320. return 0;
  321. }
  322. /*
  323. * INFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase
  324. * Unit and Update INFTL metadata. Each erase operation is
  325. * checked with check_free_sectors.
  326. *
  327. * Return: 0 when succeed, -1 on error.
  328. *
  329. * ToDo: 1. Is it necessary to check_free_sector after erasing ??
  330. */
  331. int INFTL_formatblock(struct INFTLrecord *inftl, int block)
  332. {
  333. size_t retlen;
  334. struct inftl_unittail uci;
  335. struct erase_info *instr = &inftl->instr;
  336. struct mtd_info *mtd = inftl->mbd.mtd;
  337. int physblock;
  338. pr_debug("INFTL: INFTL_formatblock(inftl=%p,block=%d)\n", inftl, block);
  339. memset(instr, 0, sizeof(struct erase_info));
  340. /* FIXME: Shouldn't we be setting the 'discarded' flag to zero
  341. _first_? */
  342. /* Use async erase interface, test return code */
  343. instr->mtd = inftl->mbd.mtd;
  344. instr->addr = block * inftl->EraseSize;
  345. instr->len = inftl->mbd.mtd->erasesize;
  346. /* Erase one physical eraseblock at a time, even though the NAND api
  347. allows us to group them. This way we if we have a failure, we can
  348. mark only the failed block in the bbt. */
  349. for (physblock = 0; physblock < inftl->EraseSize;
  350. physblock += instr->len, instr->addr += instr->len) {
  351. mtd_erase(inftl->mbd.mtd, instr);
  352. if (instr->state == MTD_ERASE_FAILED) {
  353. printk(KERN_WARNING "INFTL: error while formatting block %d\n",
  354. block);
  355. goto fail;
  356. }
  357. /*
  358. * Check the "freeness" of Erase Unit before updating metadata.
  359. * FixMe: is this check really necessary? Since we have check
  360. * the return code after the erase operation.
  361. */
  362. if (check_free_sectors(inftl, instr->addr, instr->len, 1) != 0)
  363. goto fail;
  364. }
  365. uci.EraseMark = cpu_to_le16(ERASE_MARK);
  366. uci.EraseMark1 = cpu_to_le16(ERASE_MARK);
  367. uci.Reserved[0] = 0;
  368. uci.Reserved[1] = 0;
  369. uci.Reserved[2] = 0;
  370. uci.Reserved[3] = 0;
  371. instr->addr = block * inftl->EraseSize + SECTORSIZE * 2;
  372. if (inftl_write_oob(mtd, instr->addr + 8, 8, &retlen, (char *)&uci) < 0)
  373. goto fail;
  374. return 0;
  375. fail:
  376. /* could not format, update the bad block table (caller is responsible
  377. for setting the PUtable to BLOCK_RESERVED on failure) */
  378. mtd_block_markbad(inftl->mbd.mtd, instr->addr);
  379. return -1;
  380. }
  381. /*
  382. * format_chain: Format an invalid Virtual Unit chain. It frees all the Erase
  383. * Units in a Virtual Unit Chain, i.e. all the units are disconnected.
  384. *
  385. * Since the chain is invalid then we will have to erase it from its
  386. * head (normally for INFTL we go from the oldest). But if it has a
  387. * loop then there is no oldest...
  388. */
  389. static void format_chain(struct INFTLrecord *inftl, unsigned int first_block)
  390. {
  391. unsigned int block = first_block, block1;
  392. printk(KERN_WARNING "INFTL: formatting chain at block %d\n",
  393. first_block);
  394. for (;;) {
  395. block1 = inftl->PUtable[block];
  396. printk(KERN_WARNING "INFTL: formatting block %d\n", block);
  397. if (INFTL_formatblock(inftl, block) < 0) {
  398. /*
  399. * Cannot format !!!! Mark it as Bad Unit,
  400. */
  401. inftl->PUtable[block] = BLOCK_RESERVED;
  402. } else {
  403. inftl->PUtable[block] = BLOCK_FREE;
  404. }
  405. /* Goto next block on the chain */
  406. block = block1;
  407. if (block == BLOCK_NIL || block >= inftl->lastEUN)
  408. break;
  409. }
  410. }
  411. void INFTL_dumptables(struct INFTLrecord *s)
  412. {
  413. int i;
  414. pr_debug("-------------------------------------------"
  415. "----------------------------------\n");
  416. pr_debug("VUtable[%d] ->", s->nb_blocks);
  417. for (i = 0; i < s->nb_blocks; i++) {
  418. if ((i % 8) == 0)
  419. pr_debug("\n%04x: ", i);
  420. pr_debug("%04x ", s->VUtable[i]);
  421. }
  422. pr_debug("\n-------------------------------------------"
  423. "----------------------------------\n");
  424. pr_debug("PUtable[%d-%d=%d] ->", s->firstEUN, s->lastEUN, s->nb_blocks);
  425. for (i = 0; i <= s->lastEUN; i++) {
  426. if ((i % 8) == 0)
  427. pr_debug("\n%04x: ", i);
  428. pr_debug("%04x ", s->PUtable[i]);
  429. }
  430. pr_debug("\n-------------------------------------------"
  431. "----------------------------------\n");
  432. pr_debug("INFTL ->\n"
  433. " EraseSize = %d\n"
  434. " h/s/c = %d/%d/%d\n"
  435. " numvunits = %d\n"
  436. " firstEUN = %d\n"
  437. " lastEUN = %d\n"
  438. " numfreeEUNs = %d\n"
  439. " LastFreeEUN = %d\n"
  440. " nb_blocks = %d\n"
  441. " nb_boot_blocks = %d",
  442. s->EraseSize, s->heads, s->sectors, s->cylinders,
  443. s->numvunits, s->firstEUN, s->lastEUN, s->numfreeEUNs,
  444. s->LastFreeEUN, s->nb_blocks, s->nb_boot_blocks);
  445. pr_debug("\n-------------------------------------------"
  446. "----------------------------------\n");
  447. }
  448. void INFTL_dumpVUchains(struct INFTLrecord *s)
  449. {
  450. int logical, block, i;
  451. pr_debug("-------------------------------------------"
  452. "----------------------------------\n");
  453. pr_debug("INFTL Virtual Unit Chains:\n");
  454. for (logical = 0; logical < s->nb_blocks; logical++) {
  455. block = s->VUtable[logical];
  456. if (block >= s->nb_blocks)
  457. continue;
  458. pr_debug(" LOGICAL %d --> %d ", logical, block);
  459. for (i = 0; i < s->nb_blocks; i++) {
  460. if (s->PUtable[block] == BLOCK_NIL)
  461. break;
  462. block = s->PUtable[block];
  463. pr_debug("%d ", block);
  464. }
  465. pr_debug("\n");
  466. }
  467. pr_debug("-------------------------------------------"
  468. "----------------------------------\n");
  469. }
  470. int INFTL_mount(struct INFTLrecord *s)
  471. {
  472. struct mtd_info *mtd = s->mbd.mtd;
  473. unsigned int block, first_block, prev_block, last_block;
  474. unsigned int first_logical_block, logical_block, erase_mark;
  475. int chain_length, do_format_chain;
  476. struct inftl_unithead1 h0;
  477. struct inftl_unittail h1;
  478. size_t retlen;
  479. int i;
  480. u8 *ANACtable, ANAC;
  481. pr_debug("INFTL: INFTL_mount(inftl=%p)\n", s);
  482. /* Search for INFTL MediaHeader and Spare INFTL Media Header */
  483. if (find_boot_record(s) < 0) {
  484. printk(KERN_WARNING "INFTL: could not find valid boot record?\n");
  485. return -ENXIO;
  486. }
  487. /* Init the logical to physical table */
  488. for (i = 0; i < s->nb_blocks; i++)
  489. s->VUtable[i] = BLOCK_NIL;
  490. logical_block = block = BLOCK_NIL;
  491. /* Temporary buffer to store ANAC numbers. */
  492. ANACtable = kcalloc(s->nb_blocks, sizeof(u8), GFP_KERNEL);
  493. if (!ANACtable) {
  494. printk(KERN_WARNING "INFTL: allocation of ANACtable "
  495. "failed (%zd bytes)\n",
  496. s->nb_blocks * sizeof(u8));
  497. return -ENOMEM;
  498. }
  499. /*
  500. * First pass is to explore each physical unit, and construct the
  501. * virtual chains that exist (newest physical unit goes into VUtable).
  502. * Any block that is in any way invalid will be left in the
  503. * NOTEXPLORED state. Then at the end we will try to format it and
  504. * mark it as free.
  505. */
  506. pr_debug("INFTL: pass 1, explore each unit\n");
  507. for (first_block = s->firstEUN; first_block <= s->lastEUN; first_block++) {
  508. if (s->PUtable[first_block] != BLOCK_NOTEXPLORED)
  509. continue;
  510. do_format_chain = 0;
  511. first_logical_block = BLOCK_NIL;
  512. last_block = BLOCK_NIL;
  513. block = first_block;
  514. for (chain_length = 0; ; chain_length++) {
  515. if ((chain_length == 0) &&
  516. (s->PUtable[block] != BLOCK_NOTEXPLORED)) {
  517. /* Nothing to do here, onto next block */
  518. break;
  519. }
  520. if (inftl_read_oob(mtd, block * s->EraseSize + 8,
  521. 8, &retlen, (char *)&h0) < 0 ||
  522. inftl_read_oob(mtd, block * s->EraseSize +
  523. 2 * SECTORSIZE + 8, 8, &retlen,
  524. (char *)&h1) < 0) {
  525. /* Should never happen? */
  526. do_format_chain++;
  527. break;
  528. }
  529. logical_block = le16_to_cpu(h0.virtualUnitNo);
  530. prev_block = le16_to_cpu(h0.prevUnitNo);
  531. erase_mark = le16_to_cpu((h1.EraseMark | h1.EraseMark1));
  532. ANACtable[block] = h0.ANAC;
  533. /* Previous block is relative to start of Partition */
  534. if (prev_block < s->nb_blocks)
  535. prev_block += s->firstEUN;
  536. /* Already explored partial chain? */
  537. if (s->PUtable[block] != BLOCK_NOTEXPLORED) {
  538. /* Check if chain for this logical */
  539. if (logical_block == first_logical_block) {
  540. if (last_block != BLOCK_NIL)
  541. s->PUtable[last_block] = block;
  542. }
  543. break;
  544. }
  545. /* Check for invalid block */
  546. if (erase_mark != ERASE_MARK) {
  547. printk(KERN_WARNING "INFTL: corrupt block %d "
  548. "in chain %d, chain length %d, erase "
  549. "mark 0x%x?\n", block, first_block,
  550. chain_length, erase_mark);
  551. /*
  552. * Assume end of chain, probably incomplete
  553. * fold/erase...
  554. */
  555. if (chain_length == 0)
  556. do_format_chain++;
  557. break;
  558. }
  559. /* Check for it being free already then... */
  560. if ((logical_block == BLOCK_FREE) ||
  561. (logical_block == BLOCK_NIL)) {
  562. s->PUtable[block] = BLOCK_FREE;
  563. break;
  564. }
  565. /* Sanity checks on block numbers */
  566. if ((logical_block >= s->nb_blocks) ||
  567. ((prev_block >= s->nb_blocks) &&
  568. (prev_block != BLOCK_NIL))) {
  569. if (chain_length > 0) {
  570. printk(KERN_WARNING "INFTL: corrupt "
  571. "block %d in chain %d?\n",
  572. block, first_block);
  573. do_format_chain++;
  574. }
  575. break;
  576. }
  577. if (first_logical_block == BLOCK_NIL) {
  578. first_logical_block = logical_block;
  579. } else {
  580. if (first_logical_block != logical_block) {
  581. /* Normal for folded chain... */
  582. break;
  583. }
  584. }
  585. /*
  586. * Current block is valid, so if we followed a virtual
  587. * chain to get here then we can set the previous
  588. * block pointer in our PUtable now. Then move onto
  589. * the previous block in the chain.
  590. */
  591. s->PUtable[block] = BLOCK_NIL;
  592. if (last_block != BLOCK_NIL)
  593. s->PUtable[last_block] = block;
  594. last_block = block;
  595. block = prev_block;
  596. /* Check for end of chain */
  597. if (block == BLOCK_NIL)
  598. break;
  599. /* Validate next block before following it... */
  600. if (block > s->lastEUN) {
  601. printk(KERN_WARNING "INFTL: invalid previous "
  602. "block %d in chain %d?\n", block,
  603. first_block);
  604. do_format_chain++;
  605. break;
  606. }
  607. }
  608. if (do_format_chain) {
  609. format_chain(s, first_block);
  610. continue;
  611. }
  612. /*
  613. * Looks like a valid chain then. It may not really be the
  614. * newest block in the chain, but it is the newest we have
  615. * found so far. We might update it in later iterations of
  616. * this loop if we find something newer.
  617. */
  618. s->VUtable[first_logical_block] = first_block;
  619. logical_block = BLOCK_NIL;
  620. }
  621. INFTL_dumptables(s);
  622. /*
  623. * Second pass, check for infinite loops in chains. These are
  624. * possible because we don't update the previous pointers when
  625. * we fold chains. No big deal, just fix them up in PUtable.
  626. */
  627. pr_debug("INFTL: pass 2, validate virtual chains\n");
  628. for (logical_block = 0; logical_block < s->numvunits; logical_block++) {
  629. block = s->VUtable[logical_block];
  630. last_block = BLOCK_NIL;
  631. /* Check for free/reserved/nil */
  632. if (block >= BLOCK_RESERVED)
  633. continue;
  634. ANAC = ANACtable[block];
  635. for (i = 0; i < s->numvunits; i++) {
  636. if (s->PUtable[block] == BLOCK_NIL)
  637. break;
  638. if (s->PUtable[block] > s->lastEUN) {
  639. printk(KERN_WARNING "INFTL: invalid prev %d, "
  640. "in virtual chain %d\n",
  641. s->PUtable[block], logical_block);
  642. s->PUtable[block] = BLOCK_NIL;
  643. }
  644. if (ANACtable[block] != ANAC) {
  645. /*
  646. * Chain must point back to itself. This is ok,
  647. * but we will need adjust the tables with this
  648. * newest block and oldest block.
  649. */
  650. s->VUtable[logical_block] = block;
  651. s->PUtable[last_block] = BLOCK_NIL;
  652. break;
  653. }
  654. ANAC--;
  655. last_block = block;
  656. block = s->PUtable[block];
  657. }
  658. if (i >= s->nb_blocks) {
  659. /*
  660. * Uhoo, infinite chain with valid ANACS!
  661. * Format whole chain...
  662. */
  663. format_chain(s, first_block);
  664. }
  665. }
  666. INFTL_dumptables(s);
  667. INFTL_dumpVUchains(s);
  668. /*
  669. * Third pass, format unreferenced blocks and init free block count.
  670. */
  671. s->numfreeEUNs = 0;
  672. s->LastFreeEUN = BLOCK_NIL;
  673. pr_debug("INFTL: pass 3, format unused blocks\n");
  674. for (block = s->firstEUN; block <= s->lastEUN; block++) {
  675. if (s->PUtable[block] == BLOCK_NOTEXPLORED) {
  676. printk("INFTL: unreferenced block %d, formatting it\n",
  677. block);
  678. if (INFTL_formatblock(s, block) < 0)
  679. s->PUtable[block] = BLOCK_RESERVED;
  680. else
  681. s->PUtable[block] = BLOCK_FREE;
  682. }
  683. if (s->PUtable[block] == BLOCK_FREE) {
  684. s->numfreeEUNs++;
  685. if (s->LastFreeEUN == BLOCK_NIL)
  686. s->LastFreeEUN = block;
  687. }
  688. }
  689. kfree(ANACtable);
  690. return 0;
  691. }