inftlmount.c 23 KB

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