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