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