inftlmount.c 23 KB

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