Stefan Roese | 68d7d65 | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2002 |
| 6 | * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de> |
| 7 | * |
| 8 | * (C) Copyright 2003 |
| 9 | * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de> |
| 10 | * |
| 11 | * (C) Copyright 2005 |
| 12 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 13 | * |
| 14 | * Added support for reading flash partition table from environment. |
| 15 | * Parsing routines are based on driver/mtd/cmdline.c from the linux 2.4 |
| 16 | * kernel tree. |
| 17 | * |
| 18 | * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $ |
| 19 | * Copyright 2002 SYSGO Real-Time Solutions GmbH |
| 20 | * |
| 21 | * See file CREDITS for list of people who contributed to this |
| 22 | * project. |
| 23 | * |
| 24 | * This program is free software; you can redistribute it and/or |
| 25 | * modify it under the terms of the GNU General Public License as |
| 26 | * published by the Free Software Foundation; either version 2 of |
| 27 | * the License, or (at your option) any later version. |
| 28 | * |
| 29 | * This program is distributed in the hope that it will be useful, |
| 30 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 32 | * GNU General Public License for more details. |
| 33 | * |
| 34 | * You should have received a copy of the GNU General Public License |
| 35 | * along with this program; if not, write to the Free Software |
| 36 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 37 | * MA 02111-1307 USA |
| 38 | */ |
| 39 | |
| 40 | /* |
| 41 | * Three environment variables are used by the parsing routines: |
| 42 | * |
| 43 | * 'partition' - keeps current partition identifier |
| 44 | * |
| 45 | * partition := <part-id> |
| 46 | * <part-id> := <dev-id>,part_num |
| 47 | * |
| 48 | * |
| 49 | * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping |
| 50 | * |
| 51 | * mtdids=<idmap>[,<idmap>,...] |
| 52 | * |
| 53 | * <idmap> := <dev-id>=<mtd-id> |
| 54 | * <dev-id> := 'nand'|'nor'|'onenand'<dev-num> |
| 55 | * <dev-num> := mtd device number, 0... |
| 56 | * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name) |
| 57 | * |
| 58 | * |
| 59 | * 'mtdparts' - partition list |
| 60 | * |
| 61 | * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...] |
| 62 | * |
| 63 | * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...] |
| 64 | * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name) |
| 65 | * <part-def> := <size>[@<offset>][<name>][<ro-flag>] |
| 66 | * <size> := standard linux memsize OR '-' to denote all remaining space |
| 67 | * <offset> := partition start offset within the device |
| 68 | * <name> := '(' NAME ')' |
| 69 | * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel) |
| 70 | * |
| 71 | * Notes: |
| 72 | * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping |
| 73 | * - if the above variables are not set defaults for a given target are used |
| 74 | * |
| 75 | * Examples: |
| 76 | * |
| 77 | * 1 NOR Flash, with 1 single writable partition: |
| 78 | * mtdids=nor0=edb7312-nor |
| 79 | * mtdparts=mtdparts=edb7312-nor:- |
| 80 | * |
| 81 | * 1 NOR Flash with 2 partitions, 1 NAND with one |
| 82 | * mtdids=nor0=edb7312-nor,nand0=edb7312-nand |
| 83 | * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home) |
| 84 | * |
| 85 | */ |
| 86 | |
| 87 | /* |
| 88 | * JFFS2/CRAMFS support |
| 89 | */ |
| 90 | #include <common.h> |
| 91 | #include <command.h> |
| 92 | #include <malloc.h> |
| 93 | #include <jffs2/jffs2.h> |
| 94 | #include <linux/list.h> |
| 95 | #include <linux/ctype.h> |
| 96 | #include <cramfs/cramfs_fs.h> |
| 97 | |
| 98 | #if defined(CONFIG_CMD_NAND) |
| 99 | #ifdef CONFIG_NAND_LEGACY |
| 100 | #include <linux/mtd/nand_legacy.h> |
| 101 | #else /* !CONFIG_NAND_LEGACY */ |
| 102 | #include <linux/mtd/nand.h> |
| 103 | #include <nand.h> |
| 104 | #endif /* !CONFIG_NAND_LEGACY */ |
| 105 | #endif |
| 106 | |
| 107 | #if defined(CONFIG_CMD_ONENAND) |
| 108 | #include <linux/mtd/mtd.h> |
| 109 | #include <linux/mtd/onenand.h> |
| 110 | #include <onenand_uboot.h> |
| 111 | #endif |
| 112 | |
| 113 | /* enable/disable debugging messages */ |
| 114 | #define DEBUG_MTDPARTS |
| 115 | #undef DEBUG_MTDPARTS |
| 116 | |
| 117 | #ifdef DEBUG_MTDPARTS |
| 118 | # define DEBUGF(fmt, args...) printf(fmt ,##args) |
| 119 | #else |
| 120 | # define DEBUGF(fmt, args...) |
| 121 | #endif |
| 122 | |
| 123 | /* special size referring to all the remaining space in a partition */ |
| 124 | #define SIZE_REMAINING 0xFFFFFFFF |
| 125 | |
| 126 | /* special offset value, it is used when not provided by user |
| 127 | * |
| 128 | * this value is used temporarily during parsing, later such offests |
| 129 | * are recalculated */ |
| 130 | #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF |
| 131 | |
| 132 | /* minimum partition size */ |
| 133 | #define MIN_PART_SIZE 4096 |
| 134 | |
| 135 | /* this flag needs to be set in part_info struct mask_flags |
| 136 | * field for read-only partitions */ |
| 137 | #define MTD_WRITEABLE_CMD 1 |
| 138 | |
| 139 | /* default values for mtdids and mtdparts variables */ |
| 140 | #if defined(MTDIDS_DEFAULT) |
| 141 | static const char *const mtdids_default = MTDIDS_DEFAULT; |
| 142 | #else |
| 143 | #warning "MTDIDS_DEFAULT not defined!" |
| 144 | static const char *const mtdids_default = NULL; |
| 145 | #endif |
| 146 | |
| 147 | #if defined(MTDPARTS_DEFAULT) |
| 148 | static const char *const mtdparts_default = MTDPARTS_DEFAULT; |
| 149 | #else |
| 150 | #warning "MTDPARTS_DEFAULT not defined!" |
| 151 | static const char *const mtdparts_default = NULL; |
| 152 | #endif |
| 153 | |
| 154 | /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */ |
| 155 | #define MTDIDS_MAXLEN 128 |
| 156 | #define MTDPARTS_MAXLEN 512 |
| 157 | #define PARTITION_MAXLEN 16 |
| 158 | static char last_ids[MTDIDS_MAXLEN]; |
| 159 | static char last_parts[MTDPARTS_MAXLEN]; |
| 160 | static char last_partition[PARTITION_MAXLEN]; |
| 161 | |
| 162 | /* low level jffs2 cache cleaning routine */ |
| 163 | extern void jffs2_free_cache(struct part_info *part); |
| 164 | |
| 165 | /* mtdids mapping list, filled by parse_ids() */ |
| 166 | struct list_head mtdids; |
| 167 | |
| 168 | /* device/partition list, parse_cmdline() parses into here */ |
| 169 | struct list_head devices; |
| 170 | |
| 171 | /* current active device and partition number */ |
| 172 | static struct mtd_device *current_dev = NULL; |
| 173 | static u8 current_partnum = 0; |
| 174 | |
| 175 | static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num); |
| 176 | |
| 177 | /* command line only routines */ |
| 178 | static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len); |
| 179 | static int device_del(struct mtd_device *dev); |
| 180 | |
| 181 | /** |
| 182 | * Parses a string into a number. The number stored at ptr is |
| 183 | * potentially suffixed with K (for kilobytes, or 1024 bytes), |
| 184 | * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or |
| 185 | * 1073741824). If the number is suffixed with K, M, or G, then |
| 186 | * the return value is the number multiplied by one kilobyte, one |
| 187 | * megabyte, or one gigabyte, respectively. |
| 188 | * |
| 189 | * @param ptr where parse begins |
| 190 | * @param retptr output pointer to next char after parse completes (output) |
| 191 | * @return resulting unsigned int |
| 192 | */ |
| 193 | static unsigned long memsize_parse (const char *const ptr, const char **retptr) |
| 194 | { |
| 195 | unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0); |
| 196 | |
| 197 | switch (**retptr) { |
| 198 | case 'G': |
| 199 | case 'g': |
| 200 | ret <<= 10; |
| 201 | case 'M': |
| 202 | case 'm': |
| 203 | ret <<= 10; |
| 204 | case 'K': |
| 205 | case 'k': |
| 206 | ret <<= 10; |
| 207 | (*retptr)++; |
| 208 | default: |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Format string describing supplied size. This routine does the opposite job |
| 217 | * to memsize_parse(). Size in bytes is converted to string and if possible |
| 218 | * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix. |
| 219 | * |
| 220 | * Note, that this routine does not check for buffer overflow, it's the caller |
| 221 | * who must assure enough space. |
| 222 | * |
| 223 | * @param buf output buffer |
| 224 | * @param size size to be converted to string |
| 225 | */ |
| 226 | static void memsize_format(char *buf, u32 size) |
| 227 | { |
| 228 | #define SIZE_GB ((u32)1024*1024*1024) |
| 229 | #define SIZE_MB ((u32)1024*1024) |
| 230 | #define SIZE_KB ((u32)1024) |
| 231 | |
| 232 | if ((size % SIZE_GB) == 0) |
| 233 | sprintf(buf, "%ug", size/SIZE_GB); |
| 234 | else if ((size % SIZE_MB) == 0) |
| 235 | sprintf(buf, "%um", size/SIZE_MB); |
| 236 | else if (size % SIZE_KB == 0) |
| 237 | sprintf(buf, "%uk", size/SIZE_KB); |
| 238 | else |
| 239 | sprintf(buf, "%u", size); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * This routine does global indexing of all partitions. Resulting index for |
| 244 | * current partition is saved in 'mtddevnum'. Current partition name in |
| 245 | * 'mtddevname'. |
| 246 | */ |
| 247 | static void index_partitions(void) |
| 248 | { |
| 249 | char buf[16]; |
| 250 | u16 mtddevnum; |
| 251 | struct part_info *part; |
| 252 | struct list_head *dentry; |
| 253 | struct mtd_device *dev; |
| 254 | |
| 255 | DEBUGF("--- index partitions ---\n"); |
| 256 | |
| 257 | if (current_dev) { |
| 258 | mtddevnum = 0; |
| 259 | list_for_each(dentry, &devices) { |
| 260 | dev = list_entry(dentry, struct mtd_device, link); |
| 261 | if (dev == current_dev) { |
| 262 | mtddevnum += current_partnum; |
| 263 | sprintf(buf, "%d", mtddevnum); |
| 264 | setenv("mtddevnum", buf); |
| 265 | break; |
| 266 | } |
| 267 | mtddevnum += dev->num_parts; |
| 268 | } |
| 269 | |
| 270 | part = mtd_part_info(current_dev, current_partnum); |
| 271 | setenv("mtddevname", part->name); |
| 272 | |
| 273 | DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name); |
| 274 | } else { |
| 275 | setenv("mtddevnum", NULL); |
| 276 | setenv("mtddevname", NULL); |
| 277 | |
| 278 | DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n"); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Save current device and partition in environment variable 'partition'. |
| 284 | */ |
| 285 | static void current_save(void) |
| 286 | { |
| 287 | char buf[16]; |
| 288 | |
| 289 | DEBUGF("--- current_save ---\n"); |
| 290 | |
| 291 | if (current_dev) { |
| 292 | sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_dev->id->type), |
| 293 | current_dev->id->num, current_partnum); |
| 294 | |
| 295 | setenv("partition", buf); |
| 296 | strncpy(last_partition, buf, 16); |
| 297 | |
| 298 | DEBUGF("=> partition %s\n", buf); |
| 299 | } else { |
| 300 | setenv("partition", NULL); |
| 301 | last_partition[0] = '\0'; |
| 302 | |
| 303 | DEBUGF("=> partition NULL\n"); |
| 304 | } |
| 305 | index_partitions(); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Performs sanity check for supplied NOR flash partition. Table of existing |
| 310 | * NOR flash devices is searched and partition device is located. Alignment |
| 311 | * with the granularity of NOR flash sectors is verified. |
| 312 | * |
| 313 | * @param id of the parent device |
| 314 | * @param part partition to validate |
| 315 | * @return 0 if partition is valid, 1 otherwise |
| 316 | */ |
| 317 | static int part_validate_nor(struct mtdids *id, struct part_info *part) |
| 318 | { |
| 319 | #if defined(CONFIG_CMD_FLASH) |
| 320 | /* info for FLASH chips */ |
| 321 | extern flash_info_t flash_info[]; |
| 322 | flash_info_t *flash; |
| 323 | int offset_aligned; |
| 324 | u32 end_offset, sector_size = 0; |
| 325 | int i; |
| 326 | |
| 327 | flash = &flash_info[id->num]; |
| 328 | |
| 329 | /* size of last sector */ |
| 330 | part->sector_size = flash->size - |
| 331 | (flash->start[flash->sector_count-1] - flash->start[0]); |
| 332 | |
| 333 | offset_aligned = 0; |
| 334 | for (i = 0; i < flash->sector_count; i++) { |
| 335 | if ((flash->start[i] - flash->start[0]) == part->offset) { |
| 336 | offset_aligned = 1; |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | if (offset_aligned == 0) { |
| 341 | printf("%s%d: partition (%s) start offset alignment incorrect\n", |
| 342 | MTD_DEV_TYPE(id->type), id->num, part->name); |
| 343 | return 1; |
| 344 | } |
| 345 | |
| 346 | end_offset = part->offset + part->size; |
| 347 | offset_aligned = 0; |
| 348 | for (i = 0; i < flash->sector_count; i++) { |
| 349 | if (i) { |
| 350 | sector_size = flash->start[i] - flash->start[i-1]; |
| 351 | if (part->sector_size < sector_size) |
| 352 | part->sector_size = sector_size; |
| 353 | } |
| 354 | if ((flash->start[i] - flash->start[0]) == end_offset) |
| 355 | offset_aligned = 1; |
| 356 | } |
| 357 | |
| 358 | if (offset_aligned || flash->size == end_offset) |
| 359 | return 0; |
| 360 | |
| 361 | printf("%s%d: partition (%s) size alignment incorrect\n", |
| 362 | MTD_DEV_TYPE(id->type), id->num, part->name); |
| 363 | #endif |
| 364 | return 1; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Performs sanity check for supplied NAND flash partition. Table of existing |
| 369 | * NAND flash devices is searched and partition device is located. Alignment |
| 370 | * with the granularity of nand erasesize is verified. |
| 371 | * |
| 372 | * @param id of the parent device |
| 373 | * @param part partition to validate |
| 374 | * @return 0 if partition is valid, 1 otherwise |
| 375 | */ |
| 376 | static int part_validate_nand(struct mtdids *id, struct part_info *part) |
| 377 | { |
| 378 | #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND) |
| 379 | /* info for NAND chips */ |
| 380 | nand_info_t *nand; |
| 381 | |
| 382 | nand = &nand_info[id->num]; |
| 383 | |
| 384 | part->sector_size = nand->erasesize; |
| 385 | |
| 386 | if ((unsigned long)(part->offset) % nand->erasesize) { |
| 387 | printf("%s%d: partition (%s) start offset alignment incorrect\n", |
| 388 | MTD_DEV_TYPE(id->type), id->num, part->name); |
| 389 | return 1; |
| 390 | } |
| 391 | |
| 392 | if (part->size % nand->erasesize) { |
| 393 | printf("%s%d: partition (%s) size alignment incorrect\n", |
| 394 | MTD_DEV_TYPE(id->type), id->num, part->name); |
| 395 | return 1; |
| 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | #else |
| 400 | return 1; |
| 401 | #endif |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Performs sanity check for supplied OneNAND flash partition. |
| 406 | * Table of existing OneNAND flash devices is searched and partition device |
| 407 | * is located. Alignment with the granularity of nand erasesize is verified. |
| 408 | * |
| 409 | * @param id of the parent device |
| 410 | * @param part partition to validate |
| 411 | * @return 0 if partition is valid, 1 otherwise |
| 412 | */ |
| 413 | static int part_validate_onenand(struct mtdids *id, struct part_info *part) |
| 414 | { |
| 415 | #if defined(CONFIG_CMD_ONENAND) |
| 416 | /* info for OneNAND chips */ |
| 417 | struct mtd_info *mtd; |
| 418 | |
| 419 | mtd = &onenand_mtd; |
| 420 | |
| 421 | part->sector_size = mtd->erasesize; |
| 422 | |
| 423 | if ((unsigned long)(part->offset) % mtd->erasesize) { |
| 424 | printf("%s%d: partition (%s) start offset" |
| 425 | "alignment incorrect\n", |
| 426 | MTD_DEV_TYPE(id->type), id->num, part->name); |
| 427 | return 1; |
| 428 | } |
| 429 | |
| 430 | if (part->size % mtd->erasesize) { |
| 431 | printf("%s%d: partition (%s) size alignment incorrect\n", |
| 432 | MTD_DEV_TYPE(id->type), id->num, part->name); |
| 433 | return 1; |
| 434 | } |
| 435 | |
| 436 | return 0; |
| 437 | #else |
| 438 | return 1; |
| 439 | #endif |
| 440 | } |
| 441 | |
| 442 | |
| 443 | /** |
| 444 | * Performs sanity check for supplied partition. Offset and size are verified |
| 445 | * to be within valid range. Partition type is checked and either |
| 446 | * parts_validate_nor() or parts_validate_nand() is called with the argument |
| 447 | * of part. |
| 448 | * |
| 449 | * @param id of the parent device |
| 450 | * @param part partition to validate |
| 451 | * @return 0 if partition is valid, 1 otherwise |
| 452 | */ |
| 453 | static int part_validate(struct mtdids *id, struct part_info *part) |
| 454 | { |
| 455 | if (part->size == SIZE_REMAINING) |
| 456 | part->size = id->size - part->offset; |
| 457 | |
| 458 | if (part->offset > id->size) { |
| 459 | printf("%s: offset %08x beyond flash size %08x\n", |
| 460 | id->mtd_id, part->offset, id->size); |
| 461 | return 1; |
| 462 | } |
| 463 | |
| 464 | if ((part->offset + part->size) <= part->offset) { |
| 465 | printf("%s%d: partition (%s) size too big\n", |
| 466 | MTD_DEV_TYPE(id->type), id->num, part->name); |
| 467 | return 1; |
| 468 | } |
| 469 | |
| 470 | if (part->offset + part->size > id->size) { |
| 471 | printf("%s: partitioning exceeds flash size\n", id->mtd_id); |
| 472 | return 1; |
| 473 | } |
| 474 | |
| 475 | if (id->type == MTD_DEV_TYPE_NAND) |
| 476 | return part_validate_nand(id, part); |
| 477 | else if (id->type == MTD_DEV_TYPE_NOR) |
| 478 | return part_validate_nor(id, part); |
| 479 | else if (id->type == MTD_DEV_TYPE_ONENAND) |
| 480 | return part_validate_onenand(id, part); |
| 481 | else |
| 482 | DEBUGF("part_validate: invalid dev type\n"); |
| 483 | |
| 484 | return 1; |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Delete selected partition from the partion list of the specified device. |
| 489 | * |
| 490 | * @param dev device to delete partition from |
| 491 | * @param part partition to delete |
| 492 | * @return 0 on success, 1 otherwise |
| 493 | */ |
| 494 | static int part_del(struct mtd_device *dev, struct part_info *part) |
| 495 | { |
| 496 | u8 current_save_needed = 0; |
| 497 | |
| 498 | /* if there is only one partition, remove whole device */ |
| 499 | if (dev->num_parts == 1) |
| 500 | return device_del(dev); |
| 501 | |
| 502 | /* otherwise just delete this partition */ |
| 503 | |
| 504 | if (dev == current_dev) { |
| 505 | /* we are modyfing partitions for the current device, |
| 506 | * update current */ |
| 507 | struct part_info *curr_pi; |
| 508 | curr_pi = mtd_part_info(current_dev, current_partnum); |
| 509 | |
| 510 | if (curr_pi) { |
| 511 | if (curr_pi == part) { |
| 512 | printf("current partition deleted, resetting current to 0\n"); |
| 513 | current_partnum = 0; |
| 514 | } else if (part->offset <= curr_pi->offset) { |
| 515 | current_partnum--; |
| 516 | } |
| 517 | current_save_needed = 1; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | #ifdef CONFIG_NAND_LEGACY |
| 522 | jffs2_free_cache(part); |
| 523 | #endif |
| 524 | list_del(&part->link); |
| 525 | free(part); |
| 526 | dev->num_parts--; |
| 527 | |
| 528 | if (current_save_needed > 0) |
| 529 | current_save(); |
| 530 | else |
| 531 | index_partitions(); |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Delete all partitions from parts head list, free memory. |
| 538 | * |
| 539 | * @param head list of partitions to delete |
| 540 | */ |
| 541 | static void part_delall(struct list_head *head) |
| 542 | { |
| 543 | struct list_head *entry, *n; |
| 544 | struct part_info *part_tmp; |
| 545 | |
| 546 | /* clean tmp_list and free allocated memory */ |
| 547 | list_for_each_safe(entry, n, head) { |
| 548 | part_tmp = list_entry(entry, struct part_info, link); |
| 549 | |
| 550 | #ifdef CONFIG_NAND_LEGACY |
| 551 | jffs2_free_cache(part_tmp); |
| 552 | #endif |
| 553 | list_del(entry); |
| 554 | free(part_tmp); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * Add new partition to the supplied partition list. Make sure partitions are |
| 560 | * sorted by offset in ascending order. |
| 561 | * |
| 562 | * @param head list this partition is to be added to |
| 563 | * @param new partition to be added |
| 564 | */ |
| 565 | static int part_sort_add(struct mtd_device *dev, struct part_info *part) |
| 566 | { |
| 567 | struct list_head *entry; |
| 568 | struct part_info *new_pi, *curr_pi; |
| 569 | |
| 570 | /* link partition to parrent dev */ |
| 571 | part->dev = dev; |
| 572 | |
| 573 | if (list_empty(&dev->parts)) { |
| 574 | DEBUGF("part_sort_add: list empty\n"); |
| 575 | list_add(&part->link, &dev->parts); |
| 576 | dev->num_parts++; |
| 577 | index_partitions(); |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | new_pi = list_entry(&part->link, struct part_info, link); |
| 582 | |
| 583 | /* get current partition info if we are updating current device */ |
| 584 | curr_pi = NULL; |
| 585 | if (dev == current_dev) |
| 586 | curr_pi = mtd_part_info(current_dev, current_partnum); |
| 587 | |
| 588 | list_for_each(entry, &dev->parts) { |
| 589 | struct part_info *pi; |
| 590 | |
| 591 | pi = list_entry(entry, struct part_info, link); |
| 592 | |
| 593 | /* be compliant with kernel cmdline, allow only one partition at offset zero */ |
| 594 | if ((new_pi->offset == pi->offset) && (pi->offset == 0)) { |
| 595 | printf("cannot add second partition at offset 0\n"); |
| 596 | return 1; |
| 597 | } |
| 598 | |
| 599 | if (new_pi->offset <= pi->offset) { |
| 600 | list_add_tail(&part->link, entry); |
| 601 | dev->num_parts++; |
| 602 | |
| 603 | if (curr_pi && (pi->offset <= curr_pi->offset)) { |
| 604 | /* we are modyfing partitions for the current |
| 605 | * device, update current */ |
| 606 | current_partnum++; |
| 607 | current_save(); |
| 608 | } else { |
| 609 | index_partitions(); |
| 610 | } |
| 611 | return 0; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | list_add_tail(&part->link, &dev->parts); |
| 616 | dev->num_parts++; |
| 617 | index_partitions(); |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Add provided partition to the partition list of a given device. |
| 623 | * |
| 624 | * @param dev device to which partition is added |
| 625 | * @param part partition to be added |
| 626 | * @return 0 on success, 1 otherwise |
| 627 | */ |
| 628 | static int part_add(struct mtd_device *dev, struct part_info *part) |
| 629 | { |
| 630 | /* verify alignment and size */ |
| 631 | if (part_validate(dev->id, part) != 0) |
| 632 | return 1; |
| 633 | |
| 634 | /* partition is ok, add it to the list */ |
| 635 | if (part_sort_add(dev, part) != 0) |
| 636 | return 1; |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * Parse one partition definition, allocate memory and return pointer to this |
| 643 | * location in retpart. |
| 644 | * |
| 645 | * @param partdef pointer to the partition definition string i.e. <part-def> |
| 646 | * @param ret output pointer to next char after parse completes (output) |
| 647 | * @param retpart pointer to the allocated partition (output) |
| 648 | * @return 0 on success, 1 otherwise |
| 649 | */ |
| 650 | static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart) |
| 651 | { |
| 652 | struct part_info *part; |
| 653 | unsigned long size; |
| 654 | unsigned long offset; |
| 655 | const char *name; |
| 656 | int name_len; |
| 657 | unsigned int mask_flags; |
| 658 | const char *p; |
| 659 | |
| 660 | p = partdef; |
| 661 | *retpart = NULL; |
| 662 | *ret = NULL; |
| 663 | |
| 664 | /* fetch the partition size */ |
| 665 | if (*p == '-') { |
| 666 | /* assign all remaining space to this partition */ |
| 667 | DEBUGF("'-': remaining size assigned\n"); |
| 668 | size = SIZE_REMAINING; |
| 669 | p++; |
| 670 | } else { |
| 671 | size = memsize_parse(p, &p); |
| 672 | if (size < MIN_PART_SIZE) { |
| 673 | printf("partition size too small (%lx)\n", size); |
| 674 | return 1; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | /* check for offset */ |
| 679 | offset = OFFSET_NOT_SPECIFIED; |
| 680 | if (*p == '@') { |
| 681 | p++; |
| 682 | offset = memsize_parse(p, &p); |
| 683 | } |
| 684 | |
| 685 | /* now look for the name */ |
| 686 | if (*p == '(') { |
| 687 | name = ++p; |
| 688 | if ((p = strchr(name, ')')) == NULL) { |
| 689 | printf("no closing ) found in partition name\n"); |
| 690 | return 1; |
| 691 | } |
| 692 | name_len = p - name + 1; |
| 693 | if ((name_len - 1) == 0) { |
| 694 | printf("empty partition name\n"); |
| 695 | return 1; |
| 696 | } |
| 697 | p++; |
| 698 | } else { |
| 699 | /* 0x00000000@0x00000000 */ |
| 700 | name_len = 22; |
| 701 | name = NULL; |
| 702 | } |
| 703 | |
| 704 | /* test for options */ |
| 705 | mask_flags = 0; |
| 706 | if (strncmp(p, "ro", 2) == 0) { |
| 707 | mask_flags |= MTD_WRITEABLE_CMD; |
| 708 | p += 2; |
| 709 | } |
| 710 | |
| 711 | /* check for next partition definition */ |
| 712 | if (*p == ',') { |
| 713 | if (size == SIZE_REMAINING) { |
| 714 | *ret = NULL; |
| 715 | printf("no partitions allowed after a fill-up partition\n"); |
| 716 | return 1; |
| 717 | } |
| 718 | *ret = ++p; |
| 719 | } else if ((*p == ';') || (*p == '\0')) { |
| 720 | *ret = p; |
| 721 | } else { |
| 722 | printf("unexpected character '%c' at the end of partition\n", *p); |
| 723 | *ret = NULL; |
| 724 | return 1; |
| 725 | } |
| 726 | |
| 727 | /* allocate memory */ |
| 728 | part = (struct part_info *)malloc(sizeof(struct part_info) + name_len); |
| 729 | if (!part) { |
| 730 | printf("out of memory\n"); |
| 731 | return 1; |
| 732 | } |
| 733 | memset(part, 0, sizeof(struct part_info) + name_len); |
| 734 | part->size = size; |
| 735 | part->offset = offset; |
| 736 | part->mask_flags = mask_flags; |
| 737 | part->name = (char *)(part + 1); |
| 738 | |
| 739 | if (name) { |
| 740 | /* copy user provided name */ |
| 741 | strncpy(part->name, name, name_len - 1); |
| 742 | part->auto_name = 0; |
| 743 | } else { |
| 744 | /* auto generated name in form of size@offset */ |
| 745 | sprintf(part->name, "0x%08lx@0x%08lx", size, offset); |
| 746 | part->auto_name = 1; |
| 747 | } |
| 748 | |
| 749 | part->name[name_len - 1] = '\0'; |
| 750 | INIT_LIST_HEAD(&part->link); |
| 751 | |
| 752 | DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n", |
| 753 | part->name, part->size, |
| 754 | part->offset, part->mask_flags); |
| 755 | |
| 756 | *retpart = part; |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Check device number to be within valid range for given device type. |
| 762 | * |
| 763 | * @param dev device to validate |
| 764 | * @return 0 if device is valid, 1 otherwise |
| 765 | */ |
| 766 | int mtd_device_validate(u8 type, u8 num, u32 *size) |
| 767 | { |
| 768 | if (type == MTD_DEV_TYPE_NOR) { |
| 769 | #if defined(CONFIG_CMD_FLASH) |
| 770 | if (num < CONFIG_SYS_MAX_FLASH_BANKS) { |
| 771 | extern flash_info_t flash_info[]; |
| 772 | *size = flash_info[num].size; |
| 773 | |
| 774 | return 0; |
| 775 | } |
| 776 | |
| 777 | printf("no such FLASH device: %s%d (valid range 0 ... %d\n", |
| 778 | MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1); |
| 779 | #else |
| 780 | printf("support for FLASH devices not present\n"); |
| 781 | #endif |
| 782 | } else if (type == MTD_DEV_TYPE_NAND) { |
| 783 | #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND) |
| 784 | if (num < CONFIG_SYS_MAX_NAND_DEVICE) { |
| 785 | #ifndef CONFIG_NAND_LEGACY |
| 786 | *size = nand_info[num].size; |
| 787 | #else |
| 788 | extern struct nand_chip nand_dev_desc[CONFIG_SYS_MAX_NAND_DEVICE]; |
| 789 | *size = nand_dev_desc[num].totlen; |
| 790 | #endif |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | printf("no such NAND device: %s%d (valid range 0 ... %d)\n", |
| 795 | MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1); |
| 796 | #else |
| 797 | printf("support for NAND devices not present\n"); |
| 798 | #endif |
| 799 | } else if (type == MTD_DEV_TYPE_ONENAND) { |
| 800 | #if defined(CONFIG_CMD_ONENAND) |
| 801 | *size = onenand_mtd.size; |
| 802 | return 0; |
| 803 | #else |
| 804 | printf("support for OneNAND devices not present\n"); |
| 805 | #endif |
| 806 | } else |
| 807 | printf("Unknown defice type %d\n", type); |
| 808 | |
| 809 | return 1; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * Delete all mtd devices from a supplied devices list, free memory allocated for |
| 814 | * each device and delete all device partitions. |
| 815 | * |
| 816 | * @return 0 on success, 1 otherwise |
| 817 | */ |
| 818 | static int device_delall(struct list_head *head) |
| 819 | { |
| 820 | struct list_head *entry, *n; |
| 821 | struct mtd_device *dev_tmp; |
| 822 | |
| 823 | /* clean devices list */ |
| 824 | list_for_each_safe(entry, n, head) { |
| 825 | dev_tmp = list_entry(entry, struct mtd_device, link); |
| 826 | list_del(entry); |
| 827 | part_delall(&dev_tmp->parts); |
| 828 | free(dev_tmp); |
| 829 | } |
| 830 | INIT_LIST_HEAD(&devices); |
| 831 | |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | /** |
| 836 | * If provided device exists it's partitions are deleted, device is removed |
| 837 | * from device list and device memory is freed. |
| 838 | * |
| 839 | * @param dev device to be deleted |
| 840 | * @return 0 on success, 1 otherwise |
| 841 | */ |
| 842 | static int device_del(struct mtd_device *dev) |
| 843 | { |
| 844 | part_delall(&dev->parts); |
| 845 | list_del(&dev->link); |
| 846 | free(dev); |
| 847 | |
| 848 | if (dev == current_dev) { |
| 849 | /* we just deleted current device */ |
| 850 | if (list_empty(&devices)) { |
| 851 | current_dev = NULL; |
| 852 | } else { |
| 853 | /* reset first partition from first dev from the |
| 854 | * devices list as current */ |
| 855 | current_dev = list_entry(devices.next, struct mtd_device, link); |
| 856 | current_partnum = 0; |
| 857 | } |
| 858 | current_save(); |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | index_partitions(); |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * Search global device list and return pointer to the device of type and num |
| 868 | * specified. |
| 869 | * |
| 870 | * @param type device type |
| 871 | * @param num device number |
| 872 | * @return NULL if requested device does not exist |
| 873 | */ |
| 874 | static struct mtd_device* device_find(u8 type, u8 num) |
| 875 | { |
| 876 | struct list_head *entry; |
| 877 | struct mtd_device *dev_tmp; |
| 878 | |
| 879 | list_for_each(entry, &devices) { |
| 880 | dev_tmp = list_entry(entry, struct mtd_device, link); |
| 881 | |
| 882 | if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num)) |
| 883 | return dev_tmp; |
| 884 | } |
| 885 | |
| 886 | return NULL; |
| 887 | } |
| 888 | |
| 889 | /** |
| 890 | * Add specified device to the global device list. |
| 891 | * |
| 892 | * @param dev device to be added |
| 893 | */ |
| 894 | static void device_add(struct mtd_device *dev) |
| 895 | { |
| 896 | u8 current_save_needed = 0; |
| 897 | |
| 898 | if (list_empty(&devices)) { |
| 899 | current_dev = dev; |
| 900 | current_partnum = 0; |
| 901 | current_save_needed = 1; |
| 902 | } |
| 903 | |
| 904 | list_add_tail(&dev->link, &devices); |
| 905 | |
| 906 | if (current_save_needed > 0) |
| 907 | current_save(); |
| 908 | else |
| 909 | index_partitions(); |
| 910 | } |
| 911 | |
| 912 | /** |
| 913 | * Parse device type, name and mtd-id. If syntax is ok allocate memory and |
| 914 | * return pointer to the device structure. |
| 915 | * |
| 916 | * @param mtd_dev pointer to the device definition string i.e. <mtd-dev> |
| 917 | * @param ret output pointer to next char after parse completes (output) |
| 918 | * @param retdev pointer to the allocated device (output) |
| 919 | * @return 0 on success, 1 otherwise |
| 920 | */ |
| 921 | static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev) |
| 922 | { |
| 923 | struct mtd_device *dev; |
| 924 | struct part_info *part; |
| 925 | struct mtdids *id; |
| 926 | const char *mtd_id; |
| 927 | unsigned int mtd_id_len; |
| 928 | const char *p, *pend; |
| 929 | LIST_HEAD(tmp_list); |
| 930 | struct list_head *entry, *n; |
| 931 | u16 num_parts; |
| 932 | u32 offset; |
| 933 | int err = 1; |
| 934 | |
| 935 | p = mtd_dev; |
| 936 | *retdev = NULL; |
| 937 | *ret = NULL; |
| 938 | |
| 939 | DEBUGF("===device_parse===\n"); |
| 940 | |
| 941 | /* fetch <mtd-id> */ |
| 942 | mtd_id = p; |
| 943 | if (!(p = strchr(mtd_id, ':'))) { |
| 944 | printf("no <mtd-id> identifier\n"); |
| 945 | return 1; |
| 946 | } |
| 947 | mtd_id_len = p - mtd_id + 1; |
| 948 | p++; |
| 949 | |
| 950 | /* verify if we have a valid device specified */ |
| 951 | if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) { |
| 952 | printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id); |
| 953 | return 1; |
| 954 | } |
| 955 | |
| 956 | DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n", |
| 957 | id->type, MTD_DEV_TYPE(id->type), |
| 958 | id->num, id->mtd_id); |
| 959 | pend = strchr(p, ';'); |
| 960 | DEBUGF("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p); |
| 961 | |
| 962 | |
| 963 | /* parse partitions */ |
| 964 | num_parts = 0; |
| 965 | |
| 966 | offset = 0; |
| 967 | if ((dev = device_find(id->type, id->num)) != NULL) { |
| 968 | /* if device already exists start at the end of the last partition */ |
| 969 | part = list_entry(dev->parts.prev, struct part_info, link); |
| 970 | offset = part->offset + part->size; |
| 971 | } |
| 972 | |
| 973 | while (p && (*p != '\0') && (*p != ';')) { |
| 974 | err = 1; |
| 975 | if ((part_parse(p, &p, &part) != 0) || (!part)) |
| 976 | break; |
| 977 | |
| 978 | /* calculate offset when not specified */ |
| 979 | if (part->offset == OFFSET_NOT_SPECIFIED) |
| 980 | part->offset = offset; |
| 981 | else |
| 982 | offset = part->offset; |
| 983 | |
| 984 | /* verify alignment and size */ |
| 985 | if (part_validate(id, part) != 0) |
| 986 | break; |
| 987 | |
| 988 | offset += part->size; |
| 989 | |
| 990 | /* partition is ok, add it to the list */ |
| 991 | list_add_tail(&part->link, &tmp_list); |
| 992 | num_parts++; |
| 993 | err = 0; |
| 994 | } |
| 995 | if (err == 1) { |
| 996 | part_delall(&tmp_list); |
| 997 | return 1; |
| 998 | } |
| 999 | |
| 1000 | if (num_parts == 0) { |
| 1001 | printf("no partitions for device %s%d (%s)\n", |
| 1002 | MTD_DEV_TYPE(id->type), id->num, id->mtd_id); |
| 1003 | return 1; |
| 1004 | } |
| 1005 | |
| 1006 | DEBUGF("\ntotal partitions: %d\n", num_parts); |
| 1007 | |
| 1008 | /* check for next device presence */ |
| 1009 | if (p) { |
| 1010 | if (*p == ';') { |
| 1011 | *ret = ++p; |
| 1012 | } else if (*p == '\0') { |
| 1013 | *ret = p; |
| 1014 | } else { |
| 1015 | printf("unexpected character '%c' at the end of device\n", *p); |
| 1016 | *ret = NULL; |
| 1017 | return 1; |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | /* allocate memory for mtd_device structure */ |
| 1022 | if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) { |
| 1023 | printf("out of memory\n"); |
| 1024 | return 1; |
| 1025 | } |
| 1026 | memset(dev, 0, sizeof(struct mtd_device)); |
| 1027 | dev->id = id; |
| 1028 | dev->num_parts = 0; /* part_sort_add increments num_parts */ |
| 1029 | INIT_LIST_HEAD(&dev->parts); |
| 1030 | INIT_LIST_HEAD(&dev->link); |
| 1031 | |
| 1032 | /* move partitions from tmp_list to dev->parts */ |
| 1033 | list_for_each_safe(entry, n, &tmp_list) { |
| 1034 | part = list_entry(entry, struct part_info, link); |
| 1035 | list_del(entry); |
| 1036 | if (part_sort_add(dev, part) != 0) { |
| 1037 | device_del(dev); |
| 1038 | return 1; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | *retdev = dev; |
| 1043 | |
| 1044 | DEBUGF("===\n\n"); |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | /** |
| 1049 | * Initialize global device list. |
| 1050 | * |
| 1051 | * @return 0 on success, 1 otherwise |
| 1052 | */ |
| 1053 | static int mtd_devices_init(void) |
| 1054 | { |
| 1055 | last_parts[0] = '\0'; |
| 1056 | current_dev = NULL; |
| 1057 | current_save(); |
| 1058 | |
| 1059 | return device_delall(&devices); |
| 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | * Search global mtdids list and find id of requested type and number. |
| 1064 | * |
| 1065 | * @return pointer to the id if it exists, NULL otherwise |
| 1066 | */ |
| 1067 | static struct mtdids* id_find(u8 type, u8 num) |
| 1068 | { |
| 1069 | struct list_head *entry; |
| 1070 | struct mtdids *id; |
| 1071 | |
| 1072 | list_for_each(entry, &mtdids) { |
| 1073 | id = list_entry(entry, struct mtdids, link); |
| 1074 | |
| 1075 | if ((id->type == type) && (id->num == num)) |
| 1076 | return id; |
| 1077 | } |
| 1078 | |
| 1079 | return NULL; |
| 1080 | } |
| 1081 | |
| 1082 | /** |
| 1083 | * Search global mtdids list and find id of a requested mtd_id. |
| 1084 | * |
| 1085 | * Note: first argument is not null terminated. |
| 1086 | * |
| 1087 | * @param mtd_id string containing requested mtd_id |
| 1088 | * @param mtd_id_len length of supplied mtd_id |
| 1089 | * @return pointer to the id if it exists, NULL otherwise |
| 1090 | */ |
| 1091 | static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len) |
| 1092 | { |
| 1093 | struct list_head *entry; |
| 1094 | struct mtdids *id; |
| 1095 | |
| 1096 | DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n", |
| 1097 | mtd_id_len, mtd_id, mtd_id_len); |
| 1098 | |
| 1099 | list_for_each(entry, &mtdids) { |
| 1100 | id = list_entry(entry, struct mtdids, link); |
| 1101 | |
| 1102 | DEBUGF("entry: '%s' (len = %d)\n", |
| 1103 | id->mtd_id, strlen(id->mtd_id)); |
| 1104 | |
| 1105 | if (mtd_id_len != strlen(id->mtd_id)) |
| 1106 | continue; |
| 1107 | if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0) |
| 1108 | return id; |
| 1109 | } |
| 1110 | |
| 1111 | return NULL; |
| 1112 | } |
| 1113 | |
| 1114 | /** |
| 1115 | * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>, |
| 1116 | * return device type and number. |
| 1117 | * |
| 1118 | * @param id string describing device id |
| 1119 | * @param ret_id output pointer to next char after parse completes (output) |
| 1120 | * @param dev_type parsed device type (output) |
| 1121 | * @param dev_num parsed device number (output) |
| 1122 | * @return 0 on success, 1 otherwise |
| 1123 | */ |
| 1124 | int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num) |
| 1125 | { |
| 1126 | const char *p = id; |
| 1127 | |
| 1128 | *dev_type = 0; |
| 1129 | if (strncmp(p, "nand", 4) == 0) { |
| 1130 | *dev_type = MTD_DEV_TYPE_NAND; |
| 1131 | p += 4; |
| 1132 | } else if (strncmp(p, "nor", 3) == 0) { |
| 1133 | *dev_type = MTD_DEV_TYPE_NOR; |
| 1134 | p += 3; |
| 1135 | } else if (strncmp(p, "onenand", 7) == 0) { |
| 1136 | *dev_type = MTD_DEV_TYPE_ONENAND; |
| 1137 | p += 7; |
| 1138 | } else { |
| 1139 | printf("incorrect device type in %s\n", id); |
| 1140 | return 1; |
| 1141 | } |
| 1142 | |
| 1143 | if (!isdigit(*p)) { |
| 1144 | printf("incorrect device number in %s\n", id); |
| 1145 | return 1; |
| 1146 | } |
| 1147 | |
| 1148 | *dev_num = simple_strtoul(p, (char **)&p, 0); |
| 1149 | if (ret_id) |
| 1150 | *ret_id = p; |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | /** |
| 1155 | * Process all devices and generate corresponding mtdparts string describing |
| 1156 | * all partitions on all devices. |
| 1157 | * |
| 1158 | * @param buf output buffer holding generated mtdparts string (output) |
| 1159 | * @param buflen buffer size |
| 1160 | * @return 0 on success, 1 otherwise |
| 1161 | */ |
| 1162 | static int generate_mtdparts(char *buf, u32 buflen) |
| 1163 | { |
| 1164 | struct list_head *pentry, *dentry; |
| 1165 | struct mtd_device *dev; |
| 1166 | struct part_info *part, *prev_part; |
| 1167 | char *p = buf; |
| 1168 | char tmpbuf[32]; |
| 1169 | u32 size, offset, len, part_cnt; |
| 1170 | u32 maxlen = buflen - 1; |
| 1171 | |
| 1172 | DEBUGF("--- generate_mtdparts ---\n"); |
| 1173 | |
| 1174 | if (list_empty(&devices)) { |
| 1175 | buf[0] = '\0'; |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
| 1179 | sprintf(p, "mtdparts="); |
| 1180 | p += 9; |
| 1181 | |
| 1182 | list_for_each(dentry, &devices) { |
| 1183 | dev = list_entry(dentry, struct mtd_device, link); |
| 1184 | |
| 1185 | /* copy mtd_id */ |
| 1186 | len = strlen(dev->id->mtd_id) + 1; |
| 1187 | if (len > maxlen) |
| 1188 | goto cleanup; |
| 1189 | memcpy(p, dev->id->mtd_id, len - 1); |
| 1190 | p += len - 1; |
| 1191 | *(p++) = ':'; |
| 1192 | maxlen -= len; |
| 1193 | |
| 1194 | /* format partitions */ |
| 1195 | prev_part = NULL; |
| 1196 | part_cnt = 0; |
| 1197 | list_for_each(pentry, &dev->parts) { |
| 1198 | part = list_entry(pentry, struct part_info, link); |
| 1199 | size = part->size; |
| 1200 | offset = part->offset; |
| 1201 | part_cnt++; |
| 1202 | |
| 1203 | /* partition size */ |
| 1204 | memsize_format(tmpbuf, size); |
| 1205 | len = strlen(tmpbuf); |
| 1206 | if (len > maxlen) |
| 1207 | goto cleanup; |
| 1208 | memcpy(p, tmpbuf, len); |
| 1209 | p += len; |
| 1210 | maxlen -= len; |
| 1211 | |
| 1212 | |
| 1213 | /* add offset only when there is a gap between |
| 1214 | * partitions */ |
| 1215 | if ((!prev_part && (offset != 0)) || |
| 1216 | (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) { |
| 1217 | |
| 1218 | memsize_format(tmpbuf, offset); |
| 1219 | len = strlen(tmpbuf) + 1; |
| 1220 | if (len > maxlen) |
| 1221 | goto cleanup; |
| 1222 | *(p++) = '@'; |
| 1223 | memcpy(p, tmpbuf, len - 1); |
| 1224 | p += len - 1; |
| 1225 | maxlen -= len; |
| 1226 | } |
| 1227 | |
| 1228 | /* copy name only if user supplied */ |
| 1229 | if(!part->auto_name) { |
| 1230 | len = strlen(part->name) + 2; |
| 1231 | if (len > maxlen) |
| 1232 | goto cleanup; |
| 1233 | |
| 1234 | *(p++) = '('; |
| 1235 | memcpy(p, part->name, len - 2); |
| 1236 | p += len - 2; |
| 1237 | *(p++) = ')'; |
| 1238 | maxlen -= len; |
| 1239 | } |
| 1240 | |
| 1241 | /* ro mask flag */ |
| 1242 | if (part->mask_flags && MTD_WRITEABLE_CMD) { |
| 1243 | len = 2; |
| 1244 | if (len > maxlen) |
| 1245 | goto cleanup; |
| 1246 | *(p++) = 'r'; |
| 1247 | *(p++) = 'o'; |
| 1248 | maxlen -= 2; |
| 1249 | } |
| 1250 | |
| 1251 | /* print ',' separator if there are other partitions |
| 1252 | * following */ |
| 1253 | if (dev->num_parts > part_cnt) { |
| 1254 | if (1 > maxlen) |
| 1255 | goto cleanup; |
| 1256 | *(p++) = ','; |
| 1257 | maxlen--; |
| 1258 | } |
| 1259 | prev_part = part; |
| 1260 | } |
| 1261 | /* print ';' separator if there are other devices following */ |
| 1262 | if (dentry->next != &devices) { |
| 1263 | if (1 > maxlen) |
| 1264 | goto cleanup; |
| 1265 | *(p++) = ';'; |
| 1266 | maxlen--; |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | /* we still have at least one char left, as we decremented maxlen at |
| 1271 | * the begining */ |
| 1272 | *p = '\0'; |
| 1273 | |
| 1274 | return 0; |
| 1275 | |
| 1276 | cleanup: |
| 1277 | last_parts[0] = '\0'; |
| 1278 | return 1; |
| 1279 | } |
| 1280 | |
| 1281 | /** |
| 1282 | * Call generate_mtdparts to process all devices and generate corresponding |
| 1283 | * mtdparts string, save it in mtdparts environment variable. |
| 1284 | * |
| 1285 | * @param buf output buffer holding generated mtdparts string (output) |
| 1286 | * @param buflen buffer size |
| 1287 | * @return 0 on success, 1 otherwise |
| 1288 | */ |
| 1289 | static int generate_mtdparts_save(char *buf, u32 buflen) |
| 1290 | { |
| 1291 | int ret; |
| 1292 | |
| 1293 | ret = generate_mtdparts(buf, buflen); |
| 1294 | |
| 1295 | if ((buf[0] != '\0') && (ret == 0)) |
| 1296 | setenv("mtdparts", buf); |
| 1297 | else |
| 1298 | setenv("mtdparts", NULL); |
| 1299 | |
| 1300 | return ret; |
| 1301 | } |
| 1302 | |
| 1303 | /** |
| 1304 | * Format and print out a partition list for each device from global device |
| 1305 | * list. |
| 1306 | */ |
| 1307 | static void list_partitions(void) |
| 1308 | { |
| 1309 | struct list_head *dentry, *pentry; |
| 1310 | struct part_info *part; |
| 1311 | struct mtd_device *dev; |
| 1312 | int part_num; |
| 1313 | |
| 1314 | DEBUGF("\n---list_partitions---\n"); |
| 1315 | list_for_each(dentry, &devices) { |
| 1316 | dev = list_entry(dentry, struct mtd_device, link); |
| 1317 | printf("\ndevice %s%d <%s>, # parts = %d\n", |
| 1318 | MTD_DEV_TYPE(dev->id->type), dev->id->num, |
| 1319 | dev->id->mtd_id, dev->num_parts); |
| 1320 | printf(" #: name\t\t\tsize\t\toffset\t\tmask_flags\n"); |
| 1321 | |
| 1322 | /* list partitions for given device */ |
| 1323 | part_num = 0; |
| 1324 | list_for_each(pentry, &dev->parts) { |
| 1325 | part = list_entry(pentry, struct part_info, link); |
| 1326 | printf("%2d: %-20s0x%08x\t0x%08x\t%d\n", |
| 1327 | part_num, part->name, part->size, |
| 1328 | part->offset, part->mask_flags); |
| 1329 | |
| 1330 | part_num++; |
| 1331 | } |
| 1332 | } |
| 1333 | if (list_empty(&devices)) |
| 1334 | printf("no partitions defined\n"); |
| 1335 | |
| 1336 | /* current_dev is not NULL only when we have non empty device list */ |
| 1337 | if (current_dev) { |
| 1338 | part = mtd_part_info(current_dev, current_partnum); |
| 1339 | if (part) { |
| 1340 | printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n", |
| 1341 | MTD_DEV_TYPE(current_dev->id->type), |
| 1342 | current_dev->id->num, current_partnum, |
| 1343 | part->name, part->size, part->offset); |
| 1344 | } else { |
| 1345 | printf("could not get current partition info\n\n"); |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | printf("\ndefaults:\n"); |
| 1350 | printf("mtdids : %s\n", mtdids_default); |
| 1351 | printf("mtdparts: %s\n", mtdparts_default); |
| 1352 | } |
| 1353 | |
| 1354 | /** |
| 1355 | * Given partition identifier in form of <dev_type><dev_num>,<part_num> find |
| 1356 | * corresponding device and verify partition number. |
| 1357 | * |
| 1358 | * @param id string describing device and partition or partition name |
| 1359 | * @param dev pointer to the requested device (output) |
| 1360 | * @param part_num verified partition number (output) |
| 1361 | * @param part pointer to requested partition (output) |
| 1362 | * @return 0 on success, 1 otherwise |
| 1363 | */ |
| 1364 | int find_dev_and_part(const char *id, struct mtd_device **dev, |
| 1365 | u8 *part_num, struct part_info **part) |
| 1366 | { |
| 1367 | struct list_head *dentry, *pentry; |
| 1368 | u8 type, dnum, pnum; |
| 1369 | const char *p; |
| 1370 | |
| 1371 | DEBUGF("--- find_dev_and_part ---\nid = %s\n", id); |
| 1372 | |
| 1373 | list_for_each(dentry, &devices) { |
| 1374 | *part_num = 0; |
| 1375 | *dev = list_entry(dentry, struct mtd_device, link); |
| 1376 | list_for_each(pentry, &(*dev)->parts) { |
| 1377 | *part = list_entry(pentry, struct part_info, link); |
| 1378 | if (strcmp((*part)->name, id) == 0) |
| 1379 | return 0; |
| 1380 | (*part_num)++; |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | p = id; |
| 1385 | *dev = NULL; |
| 1386 | *part = NULL; |
| 1387 | *part_num = 0; |
| 1388 | |
| 1389 | if (mtd_id_parse(p, &p, &type, &dnum) != 0) |
| 1390 | return 1; |
| 1391 | |
| 1392 | if ((*p++ != ',') || (*p == '\0')) { |
| 1393 | printf("no partition number specified\n"); |
| 1394 | return 1; |
| 1395 | } |
| 1396 | pnum = simple_strtoul(p, (char **)&p, 0); |
| 1397 | if (*p != '\0') { |
| 1398 | printf("unexpected trailing character '%c'\n", *p); |
| 1399 | return 1; |
| 1400 | } |
| 1401 | |
| 1402 | if ((*dev = device_find(type, dnum)) == NULL) { |
| 1403 | printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum); |
| 1404 | return 1; |
| 1405 | } |
| 1406 | |
| 1407 | if ((*part = mtd_part_info(*dev, pnum)) == NULL) { |
| 1408 | printf("no such partition\n"); |
| 1409 | *dev = NULL; |
| 1410 | return 1; |
| 1411 | } |
| 1412 | |
| 1413 | *part_num = pnum; |
| 1414 | |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
| 1418 | /** |
| 1419 | * Find and delete partition. For partition id format see find_dev_and_part(). |
| 1420 | * |
| 1421 | * @param id string describing device and partition |
| 1422 | * @return 0 on success, 1 otherwise |
| 1423 | */ |
| 1424 | static int delete_partition(const char *id) |
| 1425 | { |
| 1426 | u8 pnum; |
| 1427 | struct mtd_device *dev; |
| 1428 | struct part_info *part; |
| 1429 | |
| 1430 | if (find_dev_and_part(id, &dev, &pnum, &part) == 0) { |
| 1431 | |
| 1432 | DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n", |
| 1433 | MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum, |
| 1434 | part->name, part->size, part->offset); |
| 1435 | |
| 1436 | if (part_del(dev, part) != 0) |
| 1437 | return 1; |
| 1438 | |
| 1439 | if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) { |
| 1440 | printf("generated mtdparts too long, reseting to null\n"); |
| 1441 | return 1; |
| 1442 | } |
| 1443 | return 0; |
| 1444 | } |
| 1445 | |
| 1446 | printf("partition %s not found\n", id); |
| 1447 | return 1; |
| 1448 | } |
| 1449 | |
| 1450 | /** |
| 1451 | * Accept character string describing mtd partitions and call device_parse() |
| 1452 | * for each entry. Add created devices to the global devices list. |
| 1453 | * |
| 1454 | * @param mtdparts string specifing mtd partitions |
| 1455 | * @return 0 on success, 1 otherwise |
| 1456 | */ |
| 1457 | static int parse_mtdparts(const char *const mtdparts) |
| 1458 | { |
| 1459 | const char *p = mtdparts; |
| 1460 | struct mtd_device *dev; |
| 1461 | int err = 1; |
| 1462 | |
| 1463 | DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p); |
| 1464 | |
| 1465 | /* delete all devices and partitions */ |
| 1466 | if (mtd_devices_init() != 0) { |
| 1467 | printf("could not initialise device list\n"); |
| 1468 | return err; |
| 1469 | } |
| 1470 | |
| 1471 | /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */ |
| 1472 | p = getenv("mtdparts"); |
| 1473 | |
| 1474 | if (strncmp(p, "mtdparts=", 9) != 0) { |
| 1475 | printf("mtdparts variable doesn't start with 'mtdparts='\n"); |
| 1476 | return err; |
| 1477 | } |
| 1478 | p += 9; |
| 1479 | |
| 1480 | while (p && (*p != '\0')) { |
| 1481 | err = 1; |
| 1482 | if ((device_parse(p, &p, &dev) != 0) || (!dev)) |
| 1483 | break; |
| 1484 | |
| 1485 | DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type), |
| 1486 | dev->id->num, dev->id->mtd_id); |
| 1487 | |
| 1488 | /* check if parsed device is already on the list */ |
| 1489 | if (device_find(dev->id->type, dev->id->num) != NULL) { |
| 1490 | printf("device %s%d redefined, please correct mtdparts variable\n", |
| 1491 | MTD_DEV_TYPE(dev->id->type), dev->id->num); |
| 1492 | break; |
| 1493 | } |
| 1494 | |
| 1495 | list_add_tail(&dev->link, &devices); |
| 1496 | err = 0; |
| 1497 | } |
| 1498 | if (err == 1) { |
| 1499 | device_delall(&devices); |
| 1500 | return 1; |
| 1501 | } |
| 1502 | |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | /** |
| 1507 | * Parse provided string describing mtdids mapping (see file header for mtdids |
| 1508 | * variable format). Allocate memory for each entry and add all found entries |
| 1509 | * to the global mtdids list. |
| 1510 | * |
| 1511 | * @param ids mapping string |
| 1512 | * @return 0 on success, 1 otherwise |
| 1513 | */ |
| 1514 | static int parse_mtdids(const char *const ids) |
| 1515 | { |
| 1516 | const char *p = ids; |
| 1517 | const char *mtd_id; |
| 1518 | int mtd_id_len; |
| 1519 | struct mtdids *id; |
| 1520 | struct list_head *entry, *n; |
| 1521 | struct mtdids *id_tmp; |
| 1522 | u8 type, num; |
| 1523 | u32 size; |
| 1524 | int ret = 1; |
| 1525 | |
| 1526 | DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids); |
| 1527 | |
| 1528 | /* clean global mtdids list */ |
| 1529 | list_for_each_safe(entry, n, &mtdids) { |
| 1530 | id_tmp = list_entry(entry, struct mtdids, link); |
| 1531 | DEBUGF("mtdids del: %d %d\n", id_tmp->type, id_tmp->num); |
| 1532 | list_del(entry); |
| 1533 | free(id_tmp); |
| 1534 | } |
| 1535 | last_ids[0] = '\0'; |
| 1536 | INIT_LIST_HEAD(&mtdids); |
| 1537 | |
| 1538 | while(p && (*p != '\0')) { |
| 1539 | |
| 1540 | ret = 1; |
| 1541 | /* parse 'nor'|'nand'|'onenand'<dev-num> */ |
| 1542 | if (mtd_id_parse(p, &p, &type, &num) != 0) |
| 1543 | break; |
| 1544 | |
| 1545 | if (*p != '=') { |
| 1546 | printf("mtdids: incorrect <dev-num>\n"); |
| 1547 | break; |
| 1548 | } |
| 1549 | p++; |
| 1550 | |
| 1551 | /* check if requested device exists */ |
| 1552 | if (mtd_device_validate(type, num, &size) != 0) |
| 1553 | return 1; |
| 1554 | |
| 1555 | /* locate <mtd-id> */ |
| 1556 | mtd_id = p; |
| 1557 | if ((p = strchr(mtd_id, ',')) != NULL) { |
| 1558 | mtd_id_len = p - mtd_id + 1; |
| 1559 | p++; |
| 1560 | } else { |
| 1561 | mtd_id_len = strlen(mtd_id) + 1; |
| 1562 | } |
| 1563 | if (mtd_id_len == 0) { |
| 1564 | printf("mtdids: no <mtd-id> identifier\n"); |
| 1565 | break; |
| 1566 | } |
| 1567 | |
| 1568 | /* check if this id is already on the list */ |
| 1569 | int double_entry = 0; |
| 1570 | list_for_each(entry, &mtdids) { |
| 1571 | id_tmp = list_entry(entry, struct mtdids, link); |
| 1572 | if ((id_tmp->type == type) && (id_tmp->num == num)) { |
| 1573 | double_entry = 1; |
| 1574 | break; |
| 1575 | } |
| 1576 | } |
| 1577 | if (double_entry) { |
| 1578 | printf("device id %s%d redefined, please correct mtdids variable\n", |
| 1579 | MTD_DEV_TYPE(type), num); |
| 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | /* allocate mtdids structure */ |
| 1584 | if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) { |
| 1585 | printf("out of memory\n"); |
| 1586 | break; |
| 1587 | } |
| 1588 | memset(id, 0, sizeof(struct mtdids) + mtd_id_len); |
| 1589 | id->num = num; |
| 1590 | id->type = type; |
| 1591 | id->size = size; |
| 1592 | id->mtd_id = (char *)(id + 1); |
| 1593 | strncpy(id->mtd_id, mtd_id, mtd_id_len - 1); |
| 1594 | id->mtd_id[mtd_id_len - 1] = '\0'; |
| 1595 | INIT_LIST_HEAD(&id->link); |
| 1596 | |
| 1597 | DEBUGF("+ id %s%d\t%16d bytes\t%s\n", |
| 1598 | MTD_DEV_TYPE(id->type), id->num, |
| 1599 | id->size, id->mtd_id); |
| 1600 | |
| 1601 | list_add_tail(&id->link, &mtdids); |
| 1602 | ret = 0; |
| 1603 | } |
| 1604 | if (ret == 1) { |
| 1605 | /* clean mtdids list and free allocated memory */ |
| 1606 | list_for_each_safe(entry, n, &mtdids) { |
| 1607 | id_tmp = list_entry(entry, struct mtdids, link); |
| 1608 | list_del(entry); |
| 1609 | free(id_tmp); |
| 1610 | } |
| 1611 | return 1; |
| 1612 | } |
| 1613 | |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | /** |
| 1618 | * Parse and initialize global mtdids mapping and create global |
| 1619 | * device/partition list. |
| 1620 | * |
| 1621 | * @return 0 on success, 1 otherwise |
| 1622 | */ |
| 1623 | int mtdparts_init(void) |
| 1624 | { |
| 1625 | static int initialized = 0; |
| 1626 | const char *ids, *parts; |
| 1627 | const char *current_partition; |
| 1628 | int ids_changed; |
| 1629 | char tmp_ep[PARTITION_MAXLEN]; |
| 1630 | |
| 1631 | DEBUGF("\n---mtdparts_init---\n"); |
| 1632 | if (!initialized) { |
| 1633 | INIT_LIST_HEAD(&mtdids); |
| 1634 | INIT_LIST_HEAD(&devices); |
| 1635 | memset(last_ids, 0, MTDIDS_MAXLEN); |
| 1636 | memset(last_parts, 0, MTDPARTS_MAXLEN); |
| 1637 | memset(last_partition, 0, PARTITION_MAXLEN); |
| 1638 | initialized = 1; |
| 1639 | } |
| 1640 | |
| 1641 | /* get variables */ |
| 1642 | ids = getenv("mtdids"); |
| 1643 | parts = getenv("mtdparts"); |
| 1644 | current_partition = getenv("partition"); |
| 1645 | |
| 1646 | /* save it for later parsing, cannot rely on current partition pointer |
| 1647 | * as 'partition' variable may be updated during init */ |
| 1648 | tmp_ep[0] = '\0'; |
| 1649 | if (current_partition) |
| 1650 | strncpy(tmp_ep, current_partition, PARTITION_MAXLEN); |
| 1651 | |
| 1652 | DEBUGF("last_ids : %s\n", last_ids); |
| 1653 | DEBUGF("env_ids : %s\n", ids); |
| 1654 | DEBUGF("last_parts: %s\n", last_parts); |
| 1655 | DEBUGF("env_parts : %s\n\n", parts); |
| 1656 | |
| 1657 | DEBUGF("last_partition : %s\n", last_partition); |
| 1658 | DEBUGF("env_partition : %s\n", current_partition); |
| 1659 | |
| 1660 | /* if mtdids varible is empty try to use defaults */ |
| 1661 | if (!ids) { |
| 1662 | if (mtdids_default) { |
| 1663 | DEBUGF("mtdids variable not defined, using default\n"); |
| 1664 | ids = mtdids_default; |
| 1665 | setenv("mtdids", (char *)ids); |
| 1666 | } else { |
| 1667 | printf("mtdids not defined, no default present\n"); |
| 1668 | return 1; |
| 1669 | } |
| 1670 | } |
| 1671 | if (strlen(ids) > MTDIDS_MAXLEN - 1) { |
| 1672 | printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN); |
| 1673 | return 1; |
| 1674 | } |
| 1675 | |
| 1676 | /* do no try to use defaults when mtdparts variable is not defined, |
| 1677 | * just check the length */ |
| 1678 | if (!parts) |
| 1679 | printf("mtdparts variable not set, see 'help mtdparts'\n"); |
| 1680 | |
| 1681 | if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) { |
| 1682 | printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN); |
| 1683 | return 1; |
| 1684 | } |
| 1685 | |
| 1686 | /* check if we have already parsed those mtdids */ |
| 1687 | if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) { |
| 1688 | ids_changed = 0; |
| 1689 | } else { |
| 1690 | ids_changed = 1; |
| 1691 | |
| 1692 | if (parse_mtdids(ids) != 0) { |
| 1693 | mtd_devices_init(); |
| 1694 | return 1; |
| 1695 | } |
| 1696 | |
| 1697 | /* ok it's good, save new ids */ |
| 1698 | strncpy(last_ids, ids, MTDIDS_MAXLEN); |
| 1699 | } |
| 1700 | |
| 1701 | /* parse partitions if either mtdparts or mtdids were updated */ |
| 1702 | if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) { |
| 1703 | if (parse_mtdparts(parts) != 0) |
| 1704 | return 1; |
| 1705 | |
| 1706 | if (list_empty(&devices)) { |
| 1707 | printf("mtdparts_init: no valid partitions\n"); |
| 1708 | return 1; |
| 1709 | } |
| 1710 | |
| 1711 | /* ok it's good, save new parts */ |
| 1712 | strncpy(last_parts, parts, MTDPARTS_MAXLEN); |
| 1713 | |
| 1714 | /* reset first partition from first dev from the list as current */ |
| 1715 | current_dev = list_entry(devices.next, struct mtd_device, link); |
| 1716 | current_partnum = 0; |
| 1717 | current_save(); |
| 1718 | |
| 1719 | DEBUGF("mtdparts_init: current_dev = %s%d, current_partnum = %d\n", |
| 1720 | MTD_DEV_TYPE(current_dev->id->type), |
| 1721 | current_dev->id->num, current_partnum); |
| 1722 | } |
| 1723 | |
| 1724 | /* mtdparts variable was reset to NULL, delete all devices/partitions */ |
| 1725 | if (!parts && (last_parts[0] != '\0')) |
| 1726 | return mtd_devices_init(); |
| 1727 | |
| 1728 | /* do not process current partition if mtdparts variable is null */ |
| 1729 | if (!parts) |
| 1730 | return 0; |
| 1731 | |
| 1732 | /* is current partition set in environment? if so, use it */ |
| 1733 | if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) { |
| 1734 | struct part_info *p; |
| 1735 | struct mtd_device *cdev; |
| 1736 | u8 pnum; |
| 1737 | |
| 1738 | DEBUGF("--- getting current partition: %s\n", tmp_ep); |
| 1739 | |
| 1740 | if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) { |
| 1741 | current_dev = cdev; |
| 1742 | current_partnum = pnum; |
| 1743 | current_save(); |
| 1744 | } |
| 1745 | } else if (getenv("partition") == NULL) { |
| 1746 | DEBUGF("no partition variable set, setting...\n"); |
| 1747 | current_save(); |
| 1748 | } |
| 1749 | |
| 1750 | return 0; |
| 1751 | } |
| 1752 | |
| 1753 | /** |
| 1754 | * Return pointer to the partition of a requested number from a requested |
| 1755 | * device. |
| 1756 | * |
| 1757 | * @param dev device that is to be searched for a partition |
| 1758 | * @param part_num requested partition number |
| 1759 | * @return pointer to the part_info, NULL otherwise |
| 1760 | */ |
| 1761 | static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num) |
| 1762 | { |
| 1763 | struct list_head *entry; |
| 1764 | struct part_info *part; |
| 1765 | int num; |
| 1766 | |
| 1767 | if (!dev) |
| 1768 | return NULL; |
| 1769 | |
| 1770 | DEBUGF("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n", |
| 1771 | part_num, MTD_DEV_TYPE(dev->id->type), |
| 1772 | dev->id->num, dev->id->mtd_id); |
| 1773 | |
| 1774 | if (part_num >= dev->num_parts) { |
| 1775 | printf("invalid partition number %d for device %s%d (%s)\n", |
| 1776 | part_num, MTD_DEV_TYPE(dev->id->type), |
| 1777 | dev->id->num, dev->id->mtd_id); |
| 1778 | return NULL; |
| 1779 | } |
| 1780 | |
| 1781 | /* locate partition number, return it */ |
| 1782 | num = 0; |
| 1783 | list_for_each(entry, &dev->parts) { |
| 1784 | part = list_entry(entry, struct part_info, link); |
| 1785 | |
| 1786 | if (part_num == num++) { |
| 1787 | return part; |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | return NULL; |
| 1792 | } |
| 1793 | |
| 1794 | /***************************************************/ |
| 1795 | /* U-boot commands */ |
| 1796 | /***************************************************/ |
| 1797 | /* command line only */ |
| 1798 | /** |
| 1799 | * Routine implementing u-boot chpart command. Sets new current partition based |
| 1800 | * on the user supplied partition id. For partition id format see find_dev_and_part(). |
| 1801 | * |
| 1802 | * @param cmdtp command internal data |
| 1803 | * @param flag command flag |
| 1804 | * @param argc number of arguments supplied to the command |
| 1805 | * @param argv arguments list |
| 1806 | * @return 0 on success, 1 otherwise |
| 1807 | */ |
| 1808 | int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 1809 | { |
| 1810 | /* command line only */ |
| 1811 | struct mtd_device *dev; |
| 1812 | struct part_info *part; |
| 1813 | u8 pnum; |
| 1814 | |
| 1815 | if (mtdparts_init() !=0) |
| 1816 | return 1; |
| 1817 | |
| 1818 | if (argc < 2) { |
| 1819 | printf("no partition id specified\n"); |
| 1820 | return 1; |
| 1821 | } |
| 1822 | |
| 1823 | if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0) |
| 1824 | return 1; |
| 1825 | |
| 1826 | current_dev = dev; |
| 1827 | current_partnum = pnum; |
| 1828 | current_save(); |
| 1829 | |
| 1830 | printf("partition changed to %s%d,%d\n", |
| 1831 | MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum); |
| 1832 | |
| 1833 | return 0; |
| 1834 | } |
| 1835 | |
| 1836 | /** |
| 1837 | * Routine implementing u-boot mtdparts command. Initialize/update default global |
| 1838 | * partition list and process user partition request (list, add, del). |
| 1839 | * |
| 1840 | * @param cmdtp command internal data |
| 1841 | * @param flag command flag |
| 1842 | * @param argc number of arguments supplied to the command |
| 1843 | * @param argv arguments list |
| 1844 | * @return 0 on success, 1 otherwise |
| 1845 | */ |
| 1846 | int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 1847 | { |
| 1848 | if (argc == 2) { |
| 1849 | if (strcmp(argv[1], "default") == 0) { |
| 1850 | setenv("mtdids", (char *)mtdids_default); |
| 1851 | setenv("mtdparts", (char *)mtdparts_default); |
| 1852 | setenv("partition", NULL); |
| 1853 | |
| 1854 | mtdparts_init(); |
| 1855 | return 0; |
| 1856 | } else if (strcmp(argv[1], "delall") == 0) { |
| 1857 | /* this may be the first run, initialize lists if needed */ |
| 1858 | mtdparts_init(); |
| 1859 | |
| 1860 | setenv("mtdparts", NULL); |
| 1861 | |
| 1862 | /* mtd_devices_init() calls current_save() */ |
| 1863 | return mtd_devices_init(); |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | /* make sure we are in sync with env variables */ |
| 1868 | if (mtdparts_init() != 0) |
| 1869 | return 1; |
| 1870 | |
| 1871 | if (argc == 1) { |
| 1872 | list_partitions(); |
| 1873 | return 0; |
| 1874 | } |
| 1875 | |
| 1876 | /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */ |
| 1877 | if (((argc == 5) || (argc == 6)) && (strcmp(argv[1], "add") == 0)) { |
| 1878 | #define PART_ADD_DESC_MAXLEN 64 |
| 1879 | char tmpbuf[PART_ADD_DESC_MAXLEN]; |
| 1880 | u8 type, num, len; |
| 1881 | struct mtd_device *dev; |
| 1882 | struct mtd_device *dev_tmp; |
| 1883 | struct mtdids *id; |
| 1884 | struct part_info *p; |
| 1885 | |
| 1886 | if (mtd_id_parse(argv[2], NULL, &type, &num) != 0) |
| 1887 | return 1; |
| 1888 | |
| 1889 | if ((id = id_find(type, num)) == NULL) { |
| 1890 | printf("no such device %s defined in mtdids variable\n", argv[2]); |
| 1891 | return 1; |
| 1892 | } |
| 1893 | |
| 1894 | len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */ |
| 1895 | len += strlen(argv[3]); /* size@offset */ |
| 1896 | len += strlen(argv[4]) + 2; /* '(' name ')' */ |
| 1897 | if (argv[5] && (strlen(argv[5]) == 2)) |
| 1898 | len += 2; /* 'ro' */ |
| 1899 | |
| 1900 | if (len >= PART_ADD_DESC_MAXLEN) { |
| 1901 | printf("too long partition description\n"); |
| 1902 | return 1; |
| 1903 | } |
| 1904 | sprintf(tmpbuf, "%s:%s(%s)%s", |
| 1905 | id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : ""); |
| 1906 | DEBUGF("add tmpbuf: %s\n", tmpbuf); |
| 1907 | |
| 1908 | if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev)) |
| 1909 | return 1; |
| 1910 | |
| 1911 | DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type), |
| 1912 | dev->id->num, dev->id->mtd_id); |
| 1913 | |
| 1914 | if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) { |
| 1915 | device_add(dev); |
| 1916 | } else { |
| 1917 | /* merge new partition with existing ones*/ |
| 1918 | p = list_entry(dev->parts.next, struct part_info, link); |
| 1919 | if (part_add(dev_tmp, p) != 0) { |
| 1920 | device_del(dev); |
| 1921 | return 1; |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) { |
| 1926 | printf("generated mtdparts too long, reseting to null\n"); |
| 1927 | return 1; |
| 1928 | } |
| 1929 | |
| 1930 | return 0; |
| 1931 | } |
| 1932 | |
| 1933 | /* mtdparts del part-id */ |
| 1934 | if ((argc == 3) && (strcmp(argv[1], "del") == 0)) { |
| 1935 | DEBUGF("del: part-id = %s\n", argv[2]); |
| 1936 | |
| 1937 | return delete_partition(argv[2]); |
| 1938 | } |
| 1939 | |
| 1940 | cmd_usage(cmdtp); |
| 1941 | return 1; |
| 1942 | } |
| 1943 | |
| 1944 | /***************************************************/ |
| 1945 | U_BOOT_CMD( |
| 1946 | chpart, 2, 0, do_chpart, |
| 1947 | "change active partition", |
| 1948 | "part-id\n" |
| 1949 | " - change active partition (e.g. part-id = nand0,1)\n" |
| 1950 | ); |
| 1951 | |
| 1952 | U_BOOT_CMD( |
| 1953 | mtdparts, 6, 0, do_mtdparts, |
| 1954 | "define flash/nand partitions", |
| 1955 | "\n" |
| 1956 | " - list partition table\n" |
| 1957 | "mtdparts delall\n" |
| 1958 | " - delete all partitions\n" |
| 1959 | "mtdparts del part-id\n" |
| 1960 | " - delete partition (e.g. part-id = nand0,1)\n" |
| 1961 | "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n" |
| 1962 | " - add partition\n" |
| 1963 | "mtdparts default\n" |
| 1964 | " - reset partition table to defaults\n\n" |
| 1965 | "-----\n\n" |
| 1966 | "this command uses three environment variables:\n\n" |
| 1967 | "'partition' - keeps current partition identifier\n\n" |
| 1968 | "partition := <part-id>\n" |
| 1969 | "<part-id> := <dev-id>,part_num\n\n" |
| 1970 | "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n" |
| 1971 | "mtdids=<idmap>[,<idmap>,...]\n\n" |
| 1972 | "<idmap> := <dev-id>=<mtd-id>\n" |
| 1973 | "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n" |
| 1974 | "<dev-num> := mtd device number, 0...\n" |
| 1975 | "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n" |
| 1976 | "'mtdparts' - partition list\n\n" |
| 1977 | "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n" |
| 1978 | "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n" |
| 1979 | "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n" |
| 1980 | "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n" |
| 1981 | "<size> := standard linux memsize OR '-' to denote all remaining space\n" |
| 1982 | "<offset> := partition start offset within the device\n" |
| 1983 | "<name> := '(' NAME ')'\n" |
| 1984 | "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)\n" |
| 1985 | ); |
| 1986 | /***************************************************/ |