inftlmount.c 23 KB

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