inftlmount.c 22 KB

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