inftlmount.c 22 KB

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