inftlmount.c 23 KB

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