blob: 5192dee8ae23cf398a204e2dcb16c23fbb970113 [file] [log] [blame]
Stefan Roese68d7d652009-03-19 13:30:36 +01001/*
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 *
Ben Gardinerca75b202010-08-31 17:48:03 -040018 * (C) Copyright 2008
19 * Harald Welte, OpenMoko, Inc., Harald Welte <laforge@openmoko.org>
20 *
Stefan Roese68d7d652009-03-19 13:30:36 +010021 * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
22 * Copyright 2002 SYSGO Real-Time Solutions GmbH
23 *
24 * See file CREDITS for list of people who contributed to this
25 * project.
26 *
27 * This program is free software; you can redistribute it and/or
28 * modify it under the terms of the GNU General Public License as
29 * published by the Free Software Foundation; either version 2 of
30 * the License, or (at your option) any later version.
31 *
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
40 * MA 02111-1307 USA
41 */
42
43/*
44 * Three environment variables are used by the parsing routines:
45 *
46 * 'partition' - keeps current partition identifier
47 *
48 * partition := <part-id>
49 * <part-id> := <dev-id>,part_num
50 *
51 *
52 * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
53 *
54 * mtdids=<idmap>[,<idmap>,...]
55 *
56 * <idmap> := <dev-id>=<mtd-id>
57 * <dev-id> := 'nand'|'nor'|'onenand'<dev-num>
58 * <dev-num> := mtd device number, 0...
59 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
60 *
61 *
62 * 'mtdparts' - partition list
63 *
64 * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
65 *
66 * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...]
67 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
68 * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
69 * <size> := standard linux memsize OR '-' to denote all remaining space
70 * <offset> := partition start offset within the device
71 * <name> := '(' NAME ')'
72 * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)
73 *
74 * Notes:
75 * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
76 * - if the above variables are not set defaults for a given target are used
77 *
78 * Examples:
79 *
80 * 1 NOR Flash, with 1 single writable partition:
81 * mtdids=nor0=edb7312-nor
82 * mtdparts=mtdparts=edb7312-nor:-
83 *
84 * 1 NOR Flash with 2 partitions, 1 NAND with one
85 * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
86 * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
87 *
88 */
89
Stefan Roese68d7d652009-03-19 13:30:36 +010090#include <common.h>
91#include <command.h>
92#include <malloc.h>
Ladislav Michlb93b24b2009-03-23 12:06:07 +010093#include <jffs2/load_kernel.h>
Stefan Roese68d7d652009-03-19 13:30:36 +010094#include <linux/list.h>
95#include <linux/ctype.h>
Stefan Roese864aa032009-05-12 14:31:56 +020096#include <linux/err.h>
97#include <linux/mtd/mtd.h>
Stefan Roese68d7d652009-03-19 13:30:36 +010098
99#if defined(CONFIG_CMD_NAND)
Stefan Roese68d7d652009-03-19 13:30:36 +0100100#include <linux/mtd/nand.h>
101#include <nand.h>
Stefan Roese68d7d652009-03-19 13:30:36 +0100102#endif
103
104#if defined(CONFIG_CMD_ONENAND)
Stefan Roese68d7d652009-03-19 13:30:36 +0100105#include <linux/mtd/onenand.h>
106#include <onenand_uboot.h>
107#endif
108
Stefan Roese68d7d652009-03-19 13:30:36 +0100109/* special size referring to all the remaining space in a partition */
110#define SIZE_REMAINING 0xFFFFFFFF
111
112/* special offset value, it is used when not provided by user
113 *
114 * this value is used temporarily during parsing, later such offests
115 * are recalculated */
116#define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
117
118/* minimum partition size */
119#define MIN_PART_SIZE 4096
120
121/* this flag needs to be set in part_info struct mask_flags
122 * field for read-only partitions */
123#define MTD_WRITEABLE_CMD 1
124
125/* default values for mtdids and mtdparts variables */
126#if defined(MTDIDS_DEFAULT)
127static const char *const mtdids_default = MTDIDS_DEFAULT;
128#else
Stefan Roese68d7d652009-03-19 13:30:36 +0100129static const char *const mtdids_default = NULL;
130#endif
131
132#if defined(MTDPARTS_DEFAULT)
133static const char *const mtdparts_default = MTDPARTS_DEFAULT;
134#else
Stefan Roese68d7d652009-03-19 13:30:36 +0100135static const char *const mtdparts_default = NULL;
136#endif
137
138/* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
139#define MTDIDS_MAXLEN 128
140#define MTDPARTS_MAXLEN 512
141#define PARTITION_MAXLEN 16
142static char last_ids[MTDIDS_MAXLEN];
143static char last_parts[MTDPARTS_MAXLEN];
144static char last_partition[PARTITION_MAXLEN];
145
146/* low level jffs2 cache cleaning routine */
147extern void jffs2_free_cache(struct part_info *part);
148
149/* mtdids mapping list, filled by parse_ids() */
Kim Phillips088f1b12012-10-29 13:34:31 +0000150static struct list_head mtdids;
Stefan Roese68d7d652009-03-19 13:30:36 +0100151
152/* device/partition list, parse_cmdline() parses into here */
Kim Phillips088f1b12012-10-29 13:34:31 +0000153static struct list_head devices;
Stefan Roese68d7d652009-03-19 13:30:36 +0100154
155/* current active device and partition number */
Stefan Roese76b58832009-05-16 12:04:22 +0200156struct mtd_device *current_mtd_dev = NULL;
157u8 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100158
159static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
160
161/* command line only routines */
162static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
163static int device_del(struct mtd_device *dev);
164
165/**
166 * Parses a string into a number. The number stored at ptr is
167 * potentially suffixed with K (for kilobytes, or 1024 bytes),
168 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
169 * 1073741824). If the number is suffixed with K, M, or G, then
170 * the return value is the number multiplied by one kilobyte, one
171 * megabyte, or one gigabyte, respectively.
172 *
173 * @param ptr where parse begins
174 * @param retptr output pointer to next char after parse completes (output)
175 * @return resulting unsigned int
176 */
177static unsigned long memsize_parse (const char *const ptr, const char **retptr)
178{
179 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
180
181 switch (**retptr) {
182 case 'G':
183 case 'g':
184 ret <<= 10;
185 case 'M':
186 case 'm':
187 ret <<= 10;
188 case 'K':
189 case 'k':
190 ret <<= 10;
191 (*retptr)++;
192 default:
193 break;
194 }
195
196 return ret;
197}
198
199/**
200 * Format string describing supplied size. This routine does the opposite job
201 * to memsize_parse(). Size in bytes is converted to string and if possible
202 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
203 *
204 * Note, that this routine does not check for buffer overflow, it's the caller
205 * who must assure enough space.
206 *
207 * @param buf output buffer
208 * @param size size to be converted to string
209 */
210static void memsize_format(char *buf, u32 size)
211{
212#define SIZE_GB ((u32)1024*1024*1024)
213#define SIZE_MB ((u32)1024*1024)
214#define SIZE_KB ((u32)1024)
215
216 if ((size % SIZE_GB) == 0)
217 sprintf(buf, "%ug", size/SIZE_GB);
218 else if ((size % SIZE_MB) == 0)
219 sprintf(buf, "%um", size/SIZE_MB);
220 else if (size % SIZE_KB == 0)
221 sprintf(buf, "%uk", size/SIZE_KB);
222 else
223 sprintf(buf, "%u", size);
224}
225
226/**
227 * This routine does global indexing of all partitions. Resulting index for
228 * current partition is saved in 'mtddevnum'. Current partition name in
229 * 'mtddevname'.
230 */
231static void index_partitions(void)
232{
Stefan Roese68d7d652009-03-19 13:30:36 +0100233 u16 mtddevnum;
234 struct part_info *part;
235 struct list_head *dentry;
236 struct mtd_device *dev;
237
Wolfgang Denke1d29502010-04-28 10:58:10 +0200238 debug("--- index partitions ---\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100239
Stefan Roese76b58832009-05-16 12:04:22 +0200240 if (current_mtd_dev) {
Stefan Roese68d7d652009-03-19 13:30:36 +0100241 mtddevnum = 0;
242 list_for_each(dentry, &devices) {
243 dev = list_entry(dentry, struct mtd_device, link);
Stefan Roese76b58832009-05-16 12:04:22 +0200244 if (dev == current_mtd_dev) {
245 mtddevnum += current_mtd_partnum;
Simon Glass41ef3722013-02-24 17:33:22 +0000246 setenv_ulong("mtddevnum", mtddevnum);
Stefan Roese68d7d652009-03-19 13:30:36 +0100247 break;
248 }
249 mtddevnum += dev->num_parts;
250 }
251
Stefan Roese76b58832009-05-16 12:04:22 +0200252 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +0100253 setenv("mtddevname", part->name);
254
Wolfgang Denke1d29502010-04-28 10:58:10 +0200255 debug("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
Stefan Roese68d7d652009-03-19 13:30:36 +0100256 } else {
257 setenv("mtddevnum", NULL);
258 setenv("mtddevname", NULL);
259
Wolfgang Denke1d29502010-04-28 10:58:10 +0200260 debug("=> mtddevnum NULL\n=> mtddevname NULL\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100261 }
262}
263
264/**
265 * Save current device and partition in environment variable 'partition'.
266 */
267static void current_save(void)
268{
269 char buf[16];
270
Wolfgang Denke1d29502010-04-28 10:58:10 +0200271 debug("--- current_save ---\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100272
Stefan Roese76b58832009-05-16 12:04:22 +0200273 if (current_mtd_dev) {
274 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type),
275 current_mtd_dev->id->num, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +0100276
277 setenv("partition", buf);
278 strncpy(last_partition, buf, 16);
279
Wolfgang Denke1d29502010-04-28 10:58:10 +0200280 debug("=> partition %s\n", buf);
Stefan Roese68d7d652009-03-19 13:30:36 +0100281 } else {
282 setenv("partition", NULL);
283 last_partition[0] = '\0';
284
Wolfgang Denke1d29502010-04-28 10:58:10 +0200285 debug("=> partition NULL\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100286 }
287 index_partitions();
288}
289
Ben Gardiner0a026d32010-08-31 17:48:00 -0400290
291/**
292 * Produce a mtd_info given a type and num.
293 *
294 * @param type mtd type
295 * @param num mtd number
296 * @param mtd a pointer to an mtd_info instance (output)
297 * @return 0 if device is valid, 1 otherwise
298 */
299static int get_mtd_info(u8 type, u8 num, struct mtd_info **mtd)
300{
301 char mtd_dev[16];
302
303 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(type), num);
304 *mtd = get_mtd_device_nm(mtd_dev);
305 if (IS_ERR(*mtd)) {
306 printf("Device %s not found!\n", mtd_dev);
307 return 1;
308 }
309
310 return 0;
311}
312
Stefan Roese68d7d652009-03-19 13:30:36 +0100313/**
Stefan Roese864aa032009-05-12 14:31:56 +0200314 * Performs sanity check for supplied flash partition.
315 * Table of existing MTD flash devices is searched and partition device
Stefan Roese68d7d652009-03-19 13:30:36 +0100316 * is located. Alignment with the granularity of nand erasesize 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 */
Stefan Roese864aa032009-05-12 14:31:56 +0200322static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
Stefan Roese68d7d652009-03-19 13:30:36 +0100323{
Ben Gardiner0a026d32010-08-31 17:48:00 -0400324 struct mtd_info *mtd = NULL;
Stefan Roese864aa032009-05-12 14:31:56 +0200325 int i, j;
326 ulong start;
Stefan Roese68d7d652009-03-19 13:30:36 +0100327
Ben Gardiner0a026d32010-08-31 17:48:00 -0400328 if (get_mtd_info(id->type, id->num, &mtd))
Stefan Roese864aa032009-05-12 14:31:56 +0200329 return 1;
Stefan Roese68d7d652009-03-19 13:30:36 +0100330
331 part->sector_size = mtd->erasesize;
332
Stefan Roese864aa032009-05-12 14:31:56 +0200333 if (!mtd->numeraseregions) {
334 /*
335 * Only one eraseregion (NAND, OneNAND or uniform NOR),
336 * checking for alignment is easy here
337 */
338 if ((unsigned long)part->offset % mtd->erasesize) {
339 printf("%s%d: partition (%s) start offset"
340 "alignment incorrect\n",
341 MTD_DEV_TYPE(id->type), id->num, part->name);
342 return 1;
343 }
Stefan Roese68d7d652009-03-19 13:30:36 +0100344
Stefan Roese864aa032009-05-12 14:31:56 +0200345 if (part->size % mtd->erasesize) {
346 printf("%s%d: partition (%s) size alignment incorrect\n",
347 MTD_DEV_TYPE(id->type), id->num, part->name);
348 return 1;
349 }
350 } else {
351 /*
352 * Multiple eraseregions (non-uniform NOR),
353 * checking for alignment is more complex here
354 */
355
356 /* Check start alignment */
357 for (i = 0; i < mtd->numeraseregions; i++) {
358 start = mtd->eraseregions[i].offset;
359 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
360 if (part->offset == start)
361 goto start_ok;
362 start += mtd->eraseregions[i].erasesize;
363 }
364 }
365
366 printf("%s%d: partition (%s) start offset alignment incorrect\n",
367 MTD_DEV_TYPE(id->type), id->num, part->name);
Stefan Roese68d7d652009-03-19 13:30:36 +0100368 return 1;
Stefan Roese864aa032009-05-12 14:31:56 +0200369
370 start_ok:
371
372 /* Check end/size alignment */
373 for (i = 0; i < mtd->numeraseregions; i++) {
374 start = mtd->eraseregions[i].offset;
375 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
376 if ((part->offset + part->size) == start)
377 goto end_ok;
378 start += mtd->eraseregions[i].erasesize;
379 }
380 }
381 /* Check last sector alignment */
382 if ((part->offset + part->size) == start)
383 goto end_ok;
384
385 printf("%s%d: partition (%s) size alignment incorrect\n",
386 MTD_DEV_TYPE(id->type), id->num, part->name);
387 return 1;
388
389 end_ok:
390 return 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100391 }
392
393 return 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100394}
395
396
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 */
407static int part_validate(struct mtdids *id, struct part_info *part)
408{
409 if (part->size == SIZE_REMAINING)
410 part->size = id->size - part->offset;
411
412 if (part->offset > id->size) {
413 printf("%s: offset %08x beyond flash size %08x\n",
414 id->mtd_id, part->offset, id->size);
415 return 1;
416 }
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
Stefan Roese864aa032009-05-12 14:31:56 +0200429 /*
430 * Now we need to check if the partition starts and ends on
431 * sector (eraseblock) regions
432 */
433 return part_validate_eraseblock(id, part);
Stefan Roese68d7d652009-03-19 13:30:36 +0100434}
435
436/**
437 * Delete selected partition from the partion list of the specified device.
438 *
439 * @param dev device to delete partition from
440 * @param part partition to delete
441 * @return 0 on success, 1 otherwise
442 */
443static int part_del(struct mtd_device *dev, struct part_info *part)
444{
445 u8 current_save_needed = 0;
446
447 /* if there is only one partition, remove whole device */
448 if (dev->num_parts == 1)
449 return device_del(dev);
450
451 /* otherwise just delete this partition */
452
Stefan Roese76b58832009-05-16 12:04:22 +0200453 if (dev == current_mtd_dev) {
Stefan Roese68d7d652009-03-19 13:30:36 +0100454 /* we are modyfing partitions for the current device,
455 * update current */
456 struct part_info *curr_pi;
Stefan Roese76b58832009-05-16 12:04:22 +0200457 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +0100458
459 if (curr_pi) {
460 if (curr_pi == part) {
461 printf("current partition deleted, resetting current to 0\n");
Stefan Roese76b58832009-05-16 12:04:22 +0200462 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100463 } else if (part->offset <= curr_pi->offset) {
Stefan Roese76b58832009-05-16 12:04:22 +0200464 current_mtd_partnum--;
Stefan Roese68d7d652009-03-19 13:30:36 +0100465 }
466 current_save_needed = 1;
467 }
468 }
469
Stefan Roese68d7d652009-03-19 13:30:36 +0100470 list_del(&part->link);
471 free(part);
472 dev->num_parts--;
473
474 if (current_save_needed > 0)
475 current_save();
476 else
477 index_partitions();
478
479 return 0;
480}
481
482/**
483 * Delete all partitions from parts head list, free memory.
484 *
485 * @param head list of partitions to delete
486 */
487static void part_delall(struct list_head *head)
488{
489 struct list_head *entry, *n;
490 struct part_info *part_tmp;
491
492 /* clean tmp_list and free allocated memory */
493 list_for_each_safe(entry, n, head) {
494 part_tmp = list_entry(entry, struct part_info, link);
495
Stefan Roese68d7d652009-03-19 13:30:36 +0100496 list_del(entry);
497 free(part_tmp);
498 }
499}
500
501/**
502 * Add new partition to the supplied partition list. Make sure partitions are
503 * sorted by offset in ascending order.
504 *
505 * @param head list this partition is to be added to
506 * @param new partition to be added
507 */
508static int part_sort_add(struct mtd_device *dev, struct part_info *part)
509{
510 struct list_head *entry;
511 struct part_info *new_pi, *curr_pi;
512
513 /* link partition to parrent dev */
514 part->dev = dev;
515
516 if (list_empty(&dev->parts)) {
Wolfgang Denke1d29502010-04-28 10:58:10 +0200517 debug("part_sort_add: list empty\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100518 list_add(&part->link, &dev->parts);
519 dev->num_parts++;
520 index_partitions();
521 return 0;
522 }
523
524 new_pi = list_entry(&part->link, struct part_info, link);
525
526 /* get current partition info if we are updating current device */
527 curr_pi = NULL;
Stefan Roese76b58832009-05-16 12:04:22 +0200528 if (dev == current_mtd_dev)
529 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +0100530
531 list_for_each(entry, &dev->parts) {
532 struct part_info *pi;
533
534 pi = list_entry(entry, struct part_info, link);
535
536 /* be compliant with kernel cmdline, allow only one partition at offset zero */
537 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
538 printf("cannot add second partition at offset 0\n");
539 return 1;
540 }
541
542 if (new_pi->offset <= pi->offset) {
543 list_add_tail(&part->link, entry);
544 dev->num_parts++;
545
546 if (curr_pi && (pi->offset <= curr_pi->offset)) {
547 /* we are modyfing partitions for the current
548 * device, update current */
Stefan Roese76b58832009-05-16 12:04:22 +0200549 current_mtd_partnum++;
Stefan Roese68d7d652009-03-19 13:30:36 +0100550 current_save();
551 } else {
552 index_partitions();
553 }
554 return 0;
555 }
556 }
557
558 list_add_tail(&part->link, &dev->parts);
559 dev->num_parts++;
560 index_partitions();
561 return 0;
562}
563
564/**
565 * Add provided partition to the partition list of a given device.
566 *
567 * @param dev device to which partition is added
568 * @param part partition to be added
569 * @return 0 on success, 1 otherwise
570 */
571static int part_add(struct mtd_device *dev, struct part_info *part)
572{
573 /* verify alignment and size */
574 if (part_validate(dev->id, part) != 0)
575 return 1;
576
577 /* partition is ok, add it to the list */
578 if (part_sort_add(dev, part) != 0)
579 return 1;
580
581 return 0;
582}
583
584/**
585 * Parse one partition definition, allocate memory and return pointer to this
586 * location in retpart.
587 *
588 * @param partdef pointer to the partition definition string i.e. <part-def>
589 * @param ret output pointer to next char after parse completes (output)
590 * @param retpart pointer to the allocated partition (output)
591 * @return 0 on success, 1 otherwise
592 */
593static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
594{
595 struct part_info *part;
596 unsigned long size;
597 unsigned long offset;
598 const char *name;
599 int name_len;
600 unsigned int mask_flags;
601 const char *p;
602
603 p = partdef;
604 *retpart = NULL;
605 *ret = NULL;
606
607 /* fetch the partition size */
608 if (*p == '-') {
609 /* assign all remaining space to this partition */
Wolfgang Denke1d29502010-04-28 10:58:10 +0200610 debug("'-': remaining size assigned\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100611 size = SIZE_REMAINING;
612 p++;
613 } else {
614 size = memsize_parse(p, &p);
615 if (size < MIN_PART_SIZE) {
616 printf("partition size too small (%lx)\n", size);
617 return 1;
618 }
619 }
620
621 /* check for offset */
622 offset = OFFSET_NOT_SPECIFIED;
623 if (*p == '@') {
624 p++;
625 offset = memsize_parse(p, &p);
626 }
627
628 /* now look for the name */
629 if (*p == '(') {
630 name = ++p;
631 if ((p = strchr(name, ')')) == NULL) {
632 printf("no closing ) found in partition name\n");
633 return 1;
634 }
635 name_len = p - name + 1;
636 if ((name_len - 1) == 0) {
637 printf("empty partition name\n");
638 return 1;
639 }
640 p++;
641 } else {
642 /* 0x00000000@0x00000000 */
643 name_len = 22;
644 name = NULL;
645 }
646
647 /* test for options */
648 mask_flags = 0;
649 if (strncmp(p, "ro", 2) == 0) {
650 mask_flags |= MTD_WRITEABLE_CMD;
651 p += 2;
652 }
653
654 /* check for next partition definition */
655 if (*p == ',') {
656 if (size == SIZE_REMAINING) {
657 *ret = NULL;
658 printf("no partitions allowed after a fill-up partition\n");
659 return 1;
660 }
661 *ret = ++p;
662 } else if ((*p == ';') || (*p == '\0')) {
663 *ret = p;
664 } else {
665 printf("unexpected character '%c' at the end of partition\n", *p);
666 *ret = NULL;
667 return 1;
668 }
669
670 /* allocate memory */
671 part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
672 if (!part) {
673 printf("out of memory\n");
674 return 1;
675 }
676 memset(part, 0, sizeof(struct part_info) + name_len);
677 part->size = size;
678 part->offset = offset;
679 part->mask_flags = mask_flags;
680 part->name = (char *)(part + 1);
681
682 if (name) {
683 /* copy user provided name */
684 strncpy(part->name, name, name_len - 1);
685 part->auto_name = 0;
686 } else {
687 /* auto generated name in form of size@offset */
688 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
689 part->auto_name = 1;
690 }
691
692 part->name[name_len - 1] = '\0';
693 INIT_LIST_HEAD(&part->link);
694
Wolfgang Denke1d29502010-04-28 10:58:10 +0200695 debug("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
Stefan Roese68d7d652009-03-19 13:30:36 +0100696 part->name, part->size,
697 part->offset, part->mask_flags);
698
699 *retpart = part;
700 return 0;
701}
702
703/**
704 * Check device number to be within valid range for given device type.
705 *
Ben Gardiner0a026d32010-08-31 17:48:00 -0400706 * @param type mtd type
707 * @param num mtd number
708 * @param size a pointer to the size of the mtd device (output)
Stefan Roese68d7d652009-03-19 13:30:36 +0100709 * @return 0 if device is valid, 1 otherwise
710 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000711static int mtd_device_validate(u8 type, u8 num, u32 *size)
Stefan Roese68d7d652009-03-19 13:30:36 +0100712{
Ben Gardiner0a026d32010-08-31 17:48:00 -0400713 struct mtd_info *mtd = NULL;
Stefan Roese68d7d652009-03-19 13:30:36 +0100714
Ben Gardiner0a026d32010-08-31 17:48:00 -0400715 if (get_mtd_info(type, num, &mtd))
Stefan Roese864aa032009-05-12 14:31:56 +0200716 return 1;
Stefan Roese68d7d652009-03-19 13:30:36 +0100717
Stefan Roese864aa032009-05-12 14:31:56 +0200718 *size = mtd->size;
Stefan Roese68d7d652009-03-19 13:30:36 +0100719
Stefan Roese864aa032009-05-12 14:31:56 +0200720 return 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100721}
722
723/**
724 * Delete all mtd devices from a supplied devices list, free memory allocated for
725 * each device and delete all device partitions.
726 *
727 * @return 0 on success, 1 otherwise
728 */
729static int device_delall(struct list_head *head)
730{
731 struct list_head *entry, *n;
732 struct mtd_device *dev_tmp;
733
734 /* clean devices list */
735 list_for_each_safe(entry, n, head) {
736 dev_tmp = list_entry(entry, struct mtd_device, link);
737 list_del(entry);
738 part_delall(&dev_tmp->parts);
739 free(dev_tmp);
740 }
741 INIT_LIST_HEAD(&devices);
742
743 return 0;
744}
745
746/**
747 * If provided device exists it's partitions are deleted, device is removed
748 * from device list and device memory is freed.
749 *
750 * @param dev device to be deleted
751 * @return 0 on success, 1 otherwise
752 */
753static int device_del(struct mtd_device *dev)
754{
755 part_delall(&dev->parts);
756 list_del(&dev->link);
757 free(dev);
758
Stefan Roese76b58832009-05-16 12:04:22 +0200759 if (dev == current_mtd_dev) {
Stefan Roese68d7d652009-03-19 13:30:36 +0100760 /* we just deleted current device */
761 if (list_empty(&devices)) {
Stefan Roese76b58832009-05-16 12:04:22 +0200762 current_mtd_dev = NULL;
Stefan Roese68d7d652009-03-19 13:30:36 +0100763 } else {
764 /* reset first partition from first dev from the
765 * devices list as current */
Stefan Roese76b58832009-05-16 12:04:22 +0200766 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
767 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100768 }
769 current_save();
770 return 0;
771 }
772
773 index_partitions();
774 return 0;
775}
776
777/**
778 * Search global device list and return pointer to the device of type and num
779 * specified.
780 *
781 * @param type device type
782 * @param num device number
783 * @return NULL if requested device does not exist
784 */
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100785struct mtd_device *device_find(u8 type, u8 num)
Stefan Roese68d7d652009-03-19 13:30:36 +0100786{
787 struct list_head *entry;
788 struct mtd_device *dev_tmp;
789
790 list_for_each(entry, &devices) {
791 dev_tmp = list_entry(entry, struct mtd_device, link);
792
793 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
794 return dev_tmp;
795 }
796
797 return NULL;
798}
799
800/**
801 * Add specified device to the global device list.
802 *
803 * @param dev device to be added
804 */
805static void device_add(struct mtd_device *dev)
806{
807 u8 current_save_needed = 0;
808
809 if (list_empty(&devices)) {
Stefan Roese76b58832009-05-16 12:04:22 +0200810 current_mtd_dev = dev;
811 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100812 current_save_needed = 1;
813 }
814
815 list_add_tail(&dev->link, &devices);
816
817 if (current_save_needed > 0)
818 current_save();
819 else
820 index_partitions();
821}
822
823/**
824 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
825 * return pointer to the device structure.
826 *
827 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
828 * @param ret output pointer to next char after parse completes (output)
829 * @param retdev pointer to the allocated device (output)
830 * @return 0 on success, 1 otherwise
831 */
832static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
833{
834 struct mtd_device *dev;
835 struct part_info *part;
836 struct mtdids *id;
837 const char *mtd_id;
838 unsigned int mtd_id_len;
Wolfgang Denk419a1fe2011-10-05 22:48:12 +0200839 const char *p;
Wolfgang Denk419a1fe2011-10-05 22:48:12 +0200840 const char *pend;
Stefan Roese68d7d652009-03-19 13:30:36 +0100841 LIST_HEAD(tmp_list);
842 struct list_head *entry, *n;
843 u16 num_parts;
844 u32 offset;
845 int err = 1;
846
Wolfgang Denke1d29502010-04-28 10:58:10 +0200847 debug("===device_parse===\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100848
Wolfgang Denk2697eff2010-04-28 10:53:47 +0200849 assert(retdev);
850 *retdev = NULL;
851
852 if (ret)
853 *ret = NULL;
854
Stefan Roese68d7d652009-03-19 13:30:36 +0100855 /* fetch <mtd-id> */
Wolfgang Denk2697eff2010-04-28 10:53:47 +0200856 mtd_id = p = mtd_dev;
Stefan Roese68d7d652009-03-19 13:30:36 +0100857 if (!(p = strchr(mtd_id, ':'))) {
858 printf("no <mtd-id> identifier\n");
859 return 1;
860 }
861 mtd_id_len = p - mtd_id + 1;
862 p++;
863
864 /* verify if we have a valid device specified */
865 if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
866 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
867 return 1;
868 }
869
Wolfgang Denk419a1fe2011-10-05 22:48:12 +0200870#ifdef DEBUG
871 pend = strchr(p, ';');
872#endif
Wolfgang Denke1d29502010-04-28 10:58:10 +0200873 debug("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
Stefan Roese68d7d652009-03-19 13:30:36 +0100874 id->type, MTD_DEV_TYPE(id->type),
875 id->num, id->mtd_id);
Wolfgang Denke1d29502010-04-28 10:58:10 +0200876 debug("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
Stefan Roese68d7d652009-03-19 13:30:36 +0100877
878
879 /* parse partitions */
880 num_parts = 0;
881
882 offset = 0;
883 if ((dev = device_find(id->type, id->num)) != NULL) {
884 /* if device already exists start at the end of the last partition */
885 part = list_entry(dev->parts.prev, struct part_info, link);
886 offset = part->offset + part->size;
887 }
888
889 while (p && (*p != '\0') && (*p != ';')) {
890 err = 1;
891 if ((part_parse(p, &p, &part) != 0) || (!part))
892 break;
893
894 /* calculate offset when not specified */
895 if (part->offset == OFFSET_NOT_SPECIFIED)
896 part->offset = offset;
897 else
898 offset = part->offset;
899
900 /* verify alignment and size */
901 if (part_validate(id, part) != 0)
902 break;
903
904 offset += part->size;
905
906 /* partition is ok, add it to the list */
907 list_add_tail(&part->link, &tmp_list);
908 num_parts++;
909 err = 0;
910 }
911 if (err == 1) {
912 part_delall(&tmp_list);
913 return 1;
914 }
915
916 if (num_parts == 0) {
917 printf("no partitions for device %s%d (%s)\n",
918 MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
919 return 1;
920 }
921
Wolfgang Denke1d29502010-04-28 10:58:10 +0200922 debug("\ntotal partitions: %d\n", num_parts);
Stefan Roese68d7d652009-03-19 13:30:36 +0100923
924 /* check for next device presence */
925 if (p) {
926 if (*p == ';') {
Wolfgang Denk2697eff2010-04-28 10:53:47 +0200927 if (ret)
928 *ret = ++p;
Stefan Roese68d7d652009-03-19 13:30:36 +0100929 } else if (*p == '\0') {
Wolfgang Denk2697eff2010-04-28 10:53:47 +0200930 if (ret)
931 *ret = p;
Stefan Roese68d7d652009-03-19 13:30:36 +0100932 } else {
933 printf("unexpected character '%c' at the end of device\n", *p);
Wolfgang Denk2697eff2010-04-28 10:53:47 +0200934 if (ret)
935 *ret = NULL;
Stefan Roese68d7d652009-03-19 13:30:36 +0100936 return 1;
937 }
938 }
939
940 /* allocate memory for mtd_device structure */
941 if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
942 printf("out of memory\n");
943 return 1;
944 }
945 memset(dev, 0, sizeof(struct mtd_device));
946 dev->id = id;
947 dev->num_parts = 0; /* part_sort_add increments num_parts */
948 INIT_LIST_HEAD(&dev->parts);
949 INIT_LIST_HEAD(&dev->link);
950
951 /* move partitions from tmp_list to dev->parts */
952 list_for_each_safe(entry, n, &tmp_list) {
953 part = list_entry(entry, struct part_info, link);
954 list_del(entry);
955 if (part_sort_add(dev, part) != 0) {
956 device_del(dev);
957 return 1;
958 }
959 }
960
961 *retdev = dev;
962
Wolfgang Denke1d29502010-04-28 10:58:10 +0200963 debug("===\n\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100964 return 0;
965}
966
967/**
968 * Initialize global device list.
969 *
970 * @return 0 on success, 1 otherwise
971 */
972static int mtd_devices_init(void)
973{
974 last_parts[0] = '\0';
Stefan Roese76b58832009-05-16 12:04:22 +0200975 current_mtd_dev = NULL;
Stefan Roese68d7d652009-03-19 13:30:36 +0100976 current_save();
977
978 return device_delall(&devices);
979}
980
981/*
982 * Search global mtdids list and find id of requested type and number.
983 *
984 * @return pointer to the id if it exists, NULL otherwise
985 */
986static struct mtdids* id_find(u8 type, u8 num)
987{
988 struct list_head *entry;
989 struct mtdids *id;
990
991 list_for_each(entry, &mtdids) {
992 id = list_entry(entry, struct mtdids, link);
993
994 if ((id->type == type) && (id->num == num))
995 return id;
996 }
997
998 return NULL;
999}
1000
1001/**
1002 * Search global mtdids list and find id of a requested mtd_id.
1003 *
1004 * Note: first argument is not null terminated.
1005 *
1006 * @param mtd_id string containing requested mtd_id
1007 * @param mtd_id_len length of supplied mtd_id
1008 * @return pointer to the id if it exists, NULL otherwise
1009 */
1010static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
1011{
1012 struct list_head *entry;
1013 struct mtdids *id;
1014
Wolfgang Denke1d29502010-04-28 10:58:10 +02001015 debug("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
Stefan Roese68d7d652009-03-19 13:30:36 +01001016 mtd_id_len, mtd_id, mtd_id_len);
1017
1018 list_for_each(entry, &mtdids) {
1019 id = list_entry(entry, struct mtdids, link);
1020
Wolfgang Denke1d29502010-04-28 10:58:10 +02001021 debug("entry: '%s' (len = %d)\n",
Stefan Roese68d7d652009-03-19 13:30:36 +01001022 id->mtd_id, strlen(id->mtd_id));
1023
1024 if (mtd_id_len != strlen(id->mtd_id))
1025 continue;
1026 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1027 return id;
1028 }
1029
1030 return NULL;
1031}
1032
1033/**
1034 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
1035 * return device type and number.
1036 *
1037 * @param id string describing device id
1038 * @param ret_id output pointer to next char after parse completes (output)
1039 * @param dev_type parsed device type (output)
1040 * @param dev_num parsed device number (output)
1041 * @return 0 on success, 1 otherwise
1042 */
Kim Phillips088f1b12012-10-29 13:34:31 +00001043int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type,
1044 u8 *dev_num)
Stefan Roese68d7d652009-03-19 13:30:36 +01001045{
1046 const char *p = id;
1047
1048 *dev_type = 0;
1049 if (strncmp(p, "nand", 4) == 0) {
1050 *dev_type = MTD_DEV_TYPE_NAND;
1051 p += 4;
1052 } else if (strncmp(p, "nor", 3) == 0) {
1053 *dev_type = MTD_DEV_TYPE_NOR;
1054 p += 3;
1055 } else if (strncmp(p, "onenand", 7) == 0) {
1056 *dev_type = MTD_DEV_TYPE_ONENAND;
1057 p += 7;
1058 } else {
1059 printf("incorrect device type in %s\n", id);
1060 return 1;
1061 }
1062
1063 if (!isdigit(*p)) {
1064 printf("incorrect device number in %s\n", id);
1065 return 1;
1066 }
1067
1068 *dev_num = simple_strtoul(p, (char **)&p, 0);
1069 if (ret_id)
1070 *ret_id = p;
1071 return 0;
1072}
1073
1074/**
1075 * Process all devices and generate corresponding mtdparts string describing
1076 * all partitions on all devices.
1077 *
1078 * @param buf output buffer holding generated mtdparts string (output)
1079 * @param buflen buffer size
1080 * @return 0 on success, 1 otherwise
1081 */
1082static int generate_mtdparts(char *buf, u32 buflen)
1083{
1084 struct list_head *pentry, *dentry;
1085 struct mtd_device *dev;
1086 struct part_info *part, *prev_part;
1087 char *p = buf;
1088 char tmpbuf[32];
1089 u32 size, offset, len, part_cnt;
1090 u32 maxlen = buflen - 1;
1091
Wolfgang Denke1d29502010-04-28 10:58:10 +02001092 debug("--- generate_mtdparts ---\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001093
1094 if (list_empty(&devices)) {
1095 buf[0] = '\0';
1096 return 0;
1097 }
1098
1099 sprintf(p, "mtdparts=");
1100 p += 9;
1101
1102 list_for_each(dentry, &devices) {
1103 dev = list_entry(dentry, struct mtd_device, link);
1104
1105 /* copy mtd_id */
1106 len = strlen(dev->id->mtd_id) + 1;
1107 if (len > maxlen)
1108 goto cleanup;
1109 memcpy(p, dev->id->mtd_id, len - 1);
1110 p += len - 1;
1111 *(p++) = ':';
1112 maxlen -= len;
1113
1114 /* format partitions */
1115 prev_part = NULL;
1116 part_cnt = 0;
1117 list_for_each(pentry, &dev->parts) {
1118 part = list_entry(pentry, struct part_info, link);
1119 size = part->size;
1120 offset = part->offset;
1121 part_cnt++;
1122
1123 /* partition size */
1124 memsize_format(tmpbuf, size);
1125 len = strlen(tmpbuf);
1126 if (len > maxlen)
1127 goto cleanup;
1128 memcpy(p, tmpbuf, len);
1129 p += len;
1130 maxlen -= len;
1131
1132
1133 /* add offset only when there is a gap between
1134 * partitions */
1135 if ((!prev_part && (offset != 0)) ||
1136 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1137
1138 memsize_format(tmpbuf, offset);
1139 len = strlen(tmpbuf) + 1;
1140 if (len > maxlen)
1141 goto cleanup;
1142 *(p++) = '@';
1143 memcpy(p, tmpbuf, len - 1);
1144 p += len - 1;
1145 maxlen -= len;
1146 }
1147
1148 /* copy name only if user supplied */
1149 if(!part->auto_name) {
1150 len = strlen(part->name) + 2;
1151 if (len > maxlen)
1152 goto cleanup;
1153
1154 *(p++) = '(';
1155 memcpy(p, part->name, len - 2);
1156 p += len - 2;
1157 *(p++) = ')';
1158 maxlen -= len;
1159 }
1160
1161 /* ro mask flag */
1162 if (part->mask_flags && MTD_WRITEABLE_CMD) {
1163 len = 2;
1164 if (len > maxlen)
1165 goto cleanup;
1166 *(p++) = 'r';
1167 *(p++) = 'o';
1168 maxlen -= 2;
1169 }
1170
1171 /* print ',' separator if there are other partitions
1172 * following */
1173 if (dev->num_parts > part_cnt) {
1174 if (1 > maxlen)
1175 goto cleanup;
1176 *(p++) = ',';
1177 maxlen--;
1178 }
1179 prev_part = part;
1180 }
1181 /* print ';' separator if there are other devices following */
1182 if (dentry->next != &devices) {
1183 if (1 > maxlen)
1184 goto cleanup;
1185 *(p++) = ';';
1186 maxlen--;
1187 }
1188 }
1189
1190 /* we still have at least one char left, as we decremented maxlen at
1191 * the begining */
1192 *p = '\0';
1193
1194 return 0;
1195
1196cleanup:
1197 last_parts[0] = '\0';
1198 return 1;
1199}
1200
1201/**
1202 * Call generate_mtdparts to process all devices and generate corresponding
1203 * mtdparts string, save it in mtdparts environment variable.
1204 *
1205 * @param buf output buffer holding generated mtdparts string (output)
1206 * @param buflen buffer size
1207 * @return 0 on success, 1 otherwise
1208 */
1209static int generate_mtdparts_save(char *buf, u32 buflen)
1210{
1211 int ret;
1212
1213 ret = generate_mtdparts(buf, buflen);
1214
1215 if ((buf[0] != '\0') && (ret == 0))
1216 setenv("mtdparts", buf);
1217 else
1218 setenv("mtdparts", NULL);
1219
1220 return ret;
1221}
1222
Ben Gardiner04ac3802010-08-31 17:48:02 -04001223#if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
Stefan Roese68d7d652009-03-19 13:30:36 +01001224/**
Ben Gardiner04ac3802010-08-31 17:48:02 -04001225 * Get the net size (w/o bad blocks) of the given partition.
1226 *
1227 * @param mtd the mtd info
1228 * @param part the partition
1229 * @return the calculated net size of this partition
Stefan Roese68d7d652009-03-19 13:30:36 +01001230 */
Ben Gardiner04ac3802010-08-31 17:48:02 -04001231static uint64_t net_part_size(struct mtd_info *mtd, struct part_info *part)
1232{
Scott Wood36650ca2010-09-09 15:40:03 -05001233 uint64_t i, net_size = 0;
1234
Ben Gardiner04ac3802010-08-31 17:48:02 -04001235 if (!mtd->block_isbad)
1236 return part->size;
1237
Ben Gardiner04ac3802010-08-31 17:48:02 -04001238 for (i = 0; i < part->size; i += mtd->erasesize) {
1239 if (!mtd->block_isbad(mtd, part->offset + i))
1240 net_size += mtd->erasesize;
1241 }
Scott Wood36650ca2010-09-09 15:40:03 -05001242
Ben Gardiner04ac3802010-08-31 17:48:02 -04001243 return net_size;
1244}
1245#endif
1246
1247static void print_partition_table(void)
Stefan Roese68d7d652009-03-19 13:30:36 +01001248{
1249 struct list_head *dentry, *pentry;
1250 struct part_info *part;
1251 struct mtd_device *dev;
1252 int part_num;
1253
Stefan Roese68d7d652009-03-19 13:30:36 +01001254 list_for_each(dentry, &devices) {
1255 dev = list_entry(dentry, struct mtd_device, link);
Ben Gardiner04ac3802010-08-31 17:48:02 -04001256 /* list partitions for given device */
1257 part_num = 0;
1258#if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
1259 struct mtd_info *mtd;
1260
1261 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
1262 return;
1263
1264 printf("\ndevice %s%d <%s>, # parts = %d\n",
1265 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1266 dev->id->mtd_id, dev->num_parts);
1267 printf(" #: name\t\tsize\t\tnet size\toffset\t\tmask_flags\n");
1268
1269 list_for_each(pentry, &dev->parts) {
1270 u32 net_size;
1271 char *size_note;
1272
1273 part = list_entry(pentry, struct part_info, link);
1274 net_size = net_part_size(mtd, part);
1275 size_note = part->size == net_size ? " " : " (!)";
1276 printf("%2d: %-20s0x%08x\t0x%08x%s\t0x%08x\t%d\n",
1277 part_num, part->name, part->size,
1278 net_size, size_note, part->offset,
1279 part->mask_flags);
1280#else /* !defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
Stefan Roese68d7d652009-03-19 13:30:36 +01001281 printf("\ndevice %s%d <%s>, # parts = %d\n",
1282 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1283 dev->id->mtd_id, dev->num_parts);
David Brownell08f077d2009-04-16 19:55:48 -07001284 printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001285
Stefan Roese68d7d652009-03-19 13:30:36 +01001286 list_for_each(pentry, &dev->parts) {
1287 part = list_entry(pentry, struct part_info, link);
1288 printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
1289 part_num, part->name, part->size,
1290 part->offset, part->mask_flags);
Ben Gardiner04ac3802010-08-31 17:48:02 -04001291#endif /* defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
Stefan Roese68d7d652009-03-19 13:30:36 +01001292 part_num++;
1293 }
1294 }
Ben Gardiner04ac3802010-08-31 17:48:02 -04001295
Stefan Roese68d7d652009-03-19 13:30:36 +01001296 if (list_empty(&devices))
1297 printf("no partitions defined\n");
Ben Gardiner04ac3802010-08-31 17:48:02 -04001298}
1299
1300/**
1301 * Format and print out a partition list for each device from global device
1302 * list.
1303 */
1304static void list_partitions(void)
1305{
1306 struct part_info *part;
1307
1308 debug("\n---list_partitions---\n");
1309 print_partition_table();
Stefan Roese68d7d652009-03-19 13:30:36 +01001310
Stefan Roese76b58832009-05-16 12:04:22 +02001311 /* current_mtd_dev is not NULL only when we have non empty device list */
1312 if (current_mtd_dev) {
1313 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +01001314 if (part) {
1315 printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
Stefan Roese76b58832009-05-16 12:04:22 +02001316 MTD_DEV_TYPE(current_mtd_dev->id->type),
1317 current_mtd_dev->id->num, current_mtd_partnum,
Stefan Roese68d7d652009-03-19 13:30:36 +01001318 part->name, part->size, part->offset);
1319 } else {
1320 printf("could not get current partition info\n\n");
1321 }
1322 }
1323
1324 printf("\ndefaults:\n");
Wolfgang Denk36c91692009-05-17 16:01:54 +02001325 printf("mtdids : %s\n",
1326 mtdids_default ? mtdids_default : "none");
Anatolij Gustschina6934472010-02-24 00:29:44 +01001327 /*
1328 * Using printf() here results in printbuffer overflow
1329 * if default mtdparts string is greater than console
1330 * printbuffer. Use puts() to prevent system crashes.
1331 */
1332 puts("mtdparts: ");
1333 puts(mtdparts_default ? mtdparts_default : "none");
1334 puts("\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001335}
1336
1337/**
1338 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1339 * corresponding device and verify partition number.
1340 *
1341 * @param id string describing device and partition or partition name
1342 * @param dev pointer to the requested device (output)
1343 * @param part_num verified partition number (output)
1344 * @param part pointer to requested partition (output)
1345 * @return 0 on success, 1 otherwise
1346 */
1347int find_dev_and_part(const char *id, struct mtd_device **dev,
1348 u8 *part_num, struct part_info **part)
1349{
1350 struct list_head *dentry, *pentry;
1351 u8 type, dnum, pnum;
1352 const char *p;
1353
Wolfgang Denke1d29502010-04-28 10:58:10 +02001354 debug("--- find_dev_and_part ---\nid = %s\n", id);
Stefan Roese68d7d652009-03-19 13:30:36 +01001355
1356 list_for_each(dentry, &devices) {
1357 *part_num = 0;
1358 *dev = list_entry(dentry, struct mtd_device, link);
1359 list_for_each(pentry, &(*dev)->parts) {
1360 *part = list_entry(pentry, struct part_info, link);
1361 if (strcmp((*part)->name, id) == 0)
1362 return 0;
1363 (*part_num)++;
1364 }
1365 }
1366
1367 p = id;
1368 *dev = NULL;
1369 *part = NULL;
1370 *part_num = 0;
1371
1372 if (mtd_id_parse(p, &p, &type, &dnum) != 0)
1373 return 1;
1374
1375 if ((*p++ != ',') || (*p == '\0')) {
1376 printf("no partition number specified\n");
1377 return 1;
1378 }
1379 pnum = simple_strtoul(p, (char **)&p, 0);
1380 if (*p != '\0') {
1381 printf("unexpected trailing character '%c'\n", *p);
1382 return 1;
1383 }
1384
1385 if ((*dev = device_find(type, dnum)) == NULL) {
1386 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1387 return 1;
1388 }
1389
1390 if ((*part = mtd_part_info(*dev, pnum)) == NULL) {
1391 printf("no such partition\n");
1392 *dev = NULL;
1393 return 1;
1394 }
1395
1396 *part_num = pnum;
1397
1398 return 0;
1399}
1400
1401/**
1402 * Find and delete partition. For partition id format see find_dev_and_part().
1403 *
1404 * @param id string describing device and partition
1405 * @return 0 on success, 1 otherwise
1406 */
1407static int delete_partition(const char *id)
1408{
1409 u8 pnum;
1410 struct mtd_device *dev;
1411 struct part_info *part;
1412
1413 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1414
Wolfgang Denke1d29502010-04-28 10:58:10 +02001415 debug("delete_partition: device = %s%d, partition %d = (%s) 0x%08x@0x%08x\n",
Stefan Roese68d7d652009-03-19 13:30:36 +01001416 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1417 part->name, part->size, part->offset);
1418
1419 if (part_del(dev, part) != 0)
1420 return 1;
1421
1422 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
Robert P. J. Daya22bf162013-02-04 13:51:10 +00001423 printf("generated mtdparts too long, resetting to null\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001424 return 1;
1425 }
1426 return 0;
1427 }
1428
1429 printf("partition %s not found\n", id);
1430 return 1;
1431}
1432
Ben Gardinerca75b202010-08-31 17:48:03 -04001433#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
1434/**
1435 * Increase the size of the given partition so that it's net size is at least
1436 * as large as the size member and such that the next partition would start on a
1437 * good block if it were adjacent to this partition.
1438 *
1439 * @param mtd the mtd device
1440 * @param part the partition
1441 * @param next_offset pointer to the offset of the next partition after this
1442 * partition's size has been modified (output)
1443 */
1444static void spread_partition(struct mtd_info *mtd, struct part_info *part,
1445 uint64_t *next_offset)
1446{
1447 uint64_t net_size, padding_size = 0;
1448 int truncated;
1449
1450 mtd_get_len_incl_bad(mtd, part->offset, part->size, &net_size,
1451 &truncated);
1452
1453 /*
1454 * Absorb bad blocks immediately following this
1455 * partition also into the partition, such that
1456 * the next partition starts with a good block.
1457 */
1458 if (!truncated) {
1459 mtd_get_len_incl_bad(mtd, part->offset + net_size,
1460 mtd->erasesize, &padding_size, &truncated);
1461 if (truncated)
1462 padding_size = 0;
1463 else
1464 padding_size -= mtd->erasesize;
1465 }
1466
1467 if (truncated) {
1468 printf("truncated partition %s to %lld bytes\n", part->name,
1469 (uint64_t) net_size + padding_size);
1470 }
1471
1472 part->size = net_size + padding_size;
1473 *next_offset = part->offset + part->size;
1474}
1475
1476/**
1477 * Adjust all of the partition sizes, such that all partitions are at least
1478 * as big as their mtdparts environment variable sizes and they each start
1479 * on a good block.
1480 *
1481 * @return 0 on success, 1 otherwise
1482 */
1483static int spread_partitions(void)
1484{
1485 struct list_head *dentry, *pentry;
1486 struct mtd_device *dev;
1487 struct part_info *part;
1488 struct mtd_info *mtd;
1489 int part_num;
1490 uint64_t cur_offs;
1491
1492 list_for_each(dentry, &devices) {
1493 dev = list_entry(dentry, struct mtd_device, link);
1494
1495 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
1496 return 1;
1497
1498 part_num = 0;
1499 cur_offs = 0;
1500 list_for_each(pentry, &dev->parts) {
1501 part = list_entry(pentry, struct part_info, link);
1502
1503 debug("spread_partitions: device = %s%d, partition %d ="
1504 " (%s) 0x%08x@0x%08x\n",
1505 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1506 part_num, part->name, part->size,
1507 part->offset);
1508
1509 if (cur_offs > part->offset)
1510 part->offset = cur_offs;
1511
1512 spread_partition(mtd, part, &cur_offs);
1513
1514 part_num++;
1515 }
1516 }
1517
1518 index_partitions();
1519
1520 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
Robert P. J. Daya22bf162013-02-04 13:51:10 +00001521 printf("generated mtdparts too long, resetting to null\n");
Ben Gardinerca75b202010-08-31 17:48:03 -04001522 return 1;
1523 }
1524 return 0;
1525}
1526#endif /* CONFIG_CMD_MTDPARTS_SPREAD */
1527
Stefan Roese68d7d652009-03-19 13:30:36 +01001528/**
1529 * Accept character string describing mtd partitions and call device_parse()
1530 * for each entry. Add created devices to the global devices list.
1531 *
1532 * @param mtdparts string specifing mtd partitions
1533 * @return 0 on success, 1 otherwise
1534 */
1535static int parse_mtdparts(const char *const mtdparts)
1536{
1537 const char *p = mtdparts;
1538 struct mtd_device *dev;
1539 int err = 1;
1540
Wolfgang Denke1d29502010-04-28 10:58:10 +02001541 debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
Stefan Roese68d7d652009-03-19 13:30:36 +01001542
1543 /* delete all devices and partitions */
1544 if (mtd_devices_init() != 0) {
1545 printf("could not initialise device list\n");
1546 return err;
1547 }
1548
1549 /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
1550 p = getenv("mtdparts");
1551
1552 if (strncmp(p, "mtdparts=", 9) != 0) {
1553 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1554 return err;
1555 }
1556 p += 9;
1557
1558 while (p && (*p != '\0')) {
1559 err = 1;
1560 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1561 break;
1562
Wolfgang Denke1d29502010-04-28 10:58:10 +02001563 debug("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
Stefan Roese68d7d652009-03-19 13:30:36 +01001564 dev->id->num, dev->id->mtd_id);
1565
1566 /* check if parsed device is already on the list */
1567 if (device_find(dev->id->type, dev->id->num) != NULL) {
1568 printf("device %s%d redefined, please correct mtdparts variable\n",
1569 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1570 break;
1571 }
1572
1573 list_add_tail(&dev->link, &devices);
1574 err = 0;
1575 }
1576 if (err == 1) {
1577 device_delall(&devices);
1578 return 1;
1579 }
1580
1581 return 0;
1582}
1583
1584/**
1585 * Parse provided string describing mtdids mapping (see file header for mtdids
1586 * variable format). Allocate memory for each entry and add all found entries
1587 * to the global mtdids list.
1588 *
1589 * @param ids mapping string
1590 * @return 0 on success, 1 otherwise
1591 */
1592static int parse_mtdids(const char *const ids)
1593{
1594 const char *p = ids;
1595 const char *mtd_id;
1596 int mtd_id_len;
1597 struct mtdids *id;
1598 struct list_head *entry, *n;
1599 struct mtdids *id_tmp;
1600 u8 type, num;
1601 u32 size;
1602 int ret = 1;
1603
Wolfgang Denke1d29502010-04-28 10:58:10 +02001604 debug("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
Stefan Roese68d7d652009-03-19 13:30:36 +01001605
1606 /* clean global mtdids list */
1607 list_for_each_safe(entry, n, &mtdids) {
1608 id_tmp = list_entry(entry, struct mtdids, link);
Wolfgang Denke1d29502010-04-28 10:58:10 +02001609 debug("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
Stefan Roese68d7d652009-03-19 13:30:36 +01001610 list_del(entry);
1611 free(id_tmp);
1612 }
1613 last_ids[0] = '\0';
1614 INIT_LIST_HEAD(&mtdids);
1615
1616 while(p && (*p != '\0')) {
1617
1618 ret = 1;
1619 /* parse 'nor'|'nand'|'onenand'<dev-num> */
1620 if (mtd_id_parse(p, &p, &type, &num) != 0)
1621 break;
1622
1623 if (*p != '=') {
1624 printf("mtdids: incorrect <dev-num>\n");
1625 break;
1626 }
1627 p++;
1628
1629 /* check if requested device exists */
1630 if (mtd_device_validate(type, num, &size) != 0)
1631 return 1;
1632
1633 /* locate <mtd-id> */
1634 mtd_id = p;
1635 if ((p = strchr(mtd_id, ',')) != NULL) {
1636 mtd_id_len = p - mtd_id + 1;
1637 p++;
1638 } else {
1639 mtd_id_len = strlen(mtd_id) + 1;
1640 }
1641 if (mtd_id_len == 0) {
1642 printf("mtdids: no <mtd-id> identifier\n");
1643 break;
1644 }
1645
1646 /* check if this id is already on the list */
1647 int double_entry = 0;
1648 list_for_each(entry, &mtdids) {
1649 id_tmp = list_entry(entry, struct mtdids, link);
1650 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1651 double_entry = 1;
1652 break;
1653 }
1654 }
1655 if (double_entry) {
1656 printf("device id %s%d redefined, please correct mtdids variable\n",
1657 MTD_DEV_TYPE(type), num);
1658 break;
1659 }
1660
1661 /* allocate mtdids structure */
1662 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1663 printf("out of memory\n");
1664 break;
1665 }
1666 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1667 id->num = num;
1668 id->type = type;
1669 id->size = size;
1670 id->mtd_id = (char *)(id + 1);
1671 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1672 id->mtd_id[mtd_id_len - 1] = '\0';
1673 INIT_LIST_HEAD(&id->link);
1674
Wolfgang Denke1d29502010-04-28 10:58:10 +02001675 debug("+ id %s%d\t%16d bytes\t%s\n",
Stefan Roese68d7d652009-03-19 13:30:36 +01001676 MTD_DEV_TYPE(id->type), id->num,
1677 id->size, id->mtd_id);
1678
1679 list_add_tail(&id->link, &mtdids);
1680 ret = 0;
1681 }
1682 if (ret == 1) {
1683 /* clean mtdids list and free allocated memory */
1684 list_for_each_safe(entry, n, &mtdids) {
1685 id_tmp = list_entry(entry, struct mtdids, link);
1686 list_del(entry);
1687 free(id_tmp);
1688 }
1689 return 1;
1690 }
1691
1692 return 0;
1693}
1694
1695/**
1696 * Parse and initialize global mtdids mapping and create global
1697 * device/partition list.
1698 *
1699 * @return 0 on success, 1 otherwise
1700 */
1701int mtdparts_init(void)
1702{
1703 static int initialized = 0;
1704 const char *ids, *parts;
1705 const char *current_partition;
1706 int ids_changed;
1707 char tmp_ep[PARTITION_MAXLEN];
1708
Wolfgang Denke1d29502010-04-28 10:58:10 +02001709 debug("\n---mtdparts_init---\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001710 if (!initialized) {
1711 INIT_LIST_HEAD(&mtdids);
1712 INIT_LIST_HEAD(&devices);
1713 memset(last_ids, 0, MTDIDS_MAXLEN);
1714 memset(last_parts, 0, MTDPARTS_MAXLEN);
1715 memset(last_partition, 0, PARTITION_MAXLEN);
1716 initialized = 1;
1717 }
1718
1719 /* get variables */
1720 ids = getenv("mtdids");
1721 parts = getenv("mtdparts");
1722 current_partition = getenv("partition");
1723
1724 /* save it for later parsing, cannot rely on current partition pointer
1725 * as 'partition' variable may be updated during init */
1726 tmp_ep[0] = '\0';
1727 if (current_partition)
1728 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1729
Wolfgang Denke1d29502010-04-28 10:58:10 +02001730 debug("last_ids : %s\n", last_ids);
1731 debug("env_ids : %s\n", ids);
1732 debug("last_parts: %s\n", last_parts);
1733 debug("env_parts : %s\n\n", parts);
Stefan Roese68d7d652009-03-19 13:30:36 +01001734
Wolfgang Denke1d29502010-04-28 10:58:10 +02001735 debug("last_partition : %s\n", last_partition);
1736 debug("env_partition : %s\n", current_partition);
Stefan Roese68d7d652009-03-19 13:30:36 +01001737
1738 /* if mtdids varible is empty try to use defaults */
1739 if (!ids) {
1740 if (mtdids_default) {
Wolfgang Denke1d29502010-04-28 10:58:10 +02001741 debug("mtdids variable not defined, using default\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001742 ids = mtdids_default;
1743 setenv("mtdids", (char *)ids);
1744 } else {
1745 printf("mtdids not defined, no default present\n");
1746 return 1;
1747 }
1748 }
1749 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1750 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1751 return 1;
1752 }
1753
1754 /* do no try to use defaults when mtdparts variable is not defined,
1755 * just check the length */
1756 if (!parts)
1757 printf("mtdparts variable not set, see 'help mtdparts'\n");
1758
1759 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1760 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1761 return 1;
1762 }
1763
1764 /* check if we have already parsed those mtdids */
1765 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1766 ids_changed = 0;
1767 } else {
1768 ids_changed = 1;
1769
1770 if (parse_mtdids(ids) != 0) {
1771 mtd_devices_init();
1772 return 1;
1773 }
1774
1775 /* ok it's good, save new ids */
1776 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1777 }
1778
1779 /* parse partitions if either mtdparts or mtdids were updated */
1780 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1781 if (parse_mtdparts(parts) != 0)
1782 return 1;
1783
1784 if (list_empty(&devices)) {
1785 printf("mtdparts_init: no valid partitions\n");
1786 return 1;
1787 }
1788
1789 /* ok it's good, save new parts */
1790 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1791
1792 /* reset first partition from first dev from the list as current */
Stefan Roese76b58832009-05-16 12:04:22 +02001793 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
1794 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +01001795 current_save();
1796
Wolfgang Denke1d29502010-04-28 10:58:10 +02001797 debug("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n",
Stefan Roese76b58832009-05-16 12:04:22 +02001798 MTD_DEV_TYPE(current_mtd_dev->id->type),
1799 current_mtd_dev->id->num, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +01001800 }
1801
1802 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1803 if (!parts && (last_parts[0] != '\0'))
1804 return mtd_devices_init();
1805
1806 /* do not process current partition if mtdparts variable is null */
1807 if (!parts)
1808 return 0;
1809
1810 /* is current partition set in environment? if so, use it */
1811 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1812 struct part_info *p;
1813 struct mtd_device *cdev;
1814 u8 pnum;
1815
Wolfgang Denke1d29502010-04-28 10:58:10 +02001816 debug("--- getting current partition: %s\n", tmp_ep);
Stefan Roese68d7d652009-03-19 13:30:36 +01001817
1818 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
Stefan Roese76b58832009-05-16 12:04:22 +02001819 current_mtd_dev = cdev;
1820 current_mtd_partnum = pnum;
Stefan Roese68d7d652009-03-19 13:30:36 +01001821 current_save();
1822 }
1823 } else if (getenv("partition") == NULL) {
Wolfgang Denke1d29502010-04-28 10:58:10 +02001824 debug("no partition variable set, setting...\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001825 current_save();
1826 }
1827
1828 return 0;
1829}
1830
1831/**
1832 * Return pointer to the partition of a requested number from a requested
1833 * device.
1834 *
1835 * @param dev device that is to be searched for a partition
1836 * @param part_num requested partition number
1837 * @return pointer to the part_info, NULL otherwise
1838 */
1839static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
1840{
1841 struct list_head *entry;
1842 struct part_info *part;
1843 int num;
1844
1845 if (!dev)
1846 return NULL;
1847
Wolfgang Denke1d29502010-04-28 10:58:10 +02001848 debug("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
Stefan Roese68d7d652009-03-19 13:30:36 +01001849 part_num, MTD_DEV_TYPE(dev->id->type),
1850 dev->id->num, dev->id->mtd_id);
1851
1852 if (part_num >= dev->num_parts) {
1853 printf("invalid partition number %d for device %s%d (%s)\n",
1854 part_num, MTD_DEV_TYPE(dev->id->type),
1855 dev->id->num, dev->id->mtd_id);
1856 return NULL;
1857 }
1858
1859 /* locate partition number, return it */
1860 num = 0;
1861 list_for_each(entry, &dev->parts) {
1862 part = list_entry(entry, struct part_info, link);
1863
1864 if (part_num == num++) {
1865 return part;
1866 }
1867 }
1868
1869 return NULL;
1870}
1871
1872/***************************************************/
1873/* U-boot commands */
1874/***************************************************/
1875/* command line only */
1876/**
1877 * Routine implementing u-boot chpart command. Sets new current partition based
1878 * on the user supplied partition id. For partition id format see find_dev_and_part().
1879 *
1880 * @param cmdtp command internal data
1881 * @param flag command flag
1882 * @param argc number of arguments supplied to the command
1883 * @param argv arguments list
1884 * @return 0 on success, 1 otherwise
1885 */
Kim Phillips088f1b12012-10-29 13:34:31 +00001886static int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stefan Roese68d7d652009-03-19 13:30:36 +01001887{
1888/* command line only */
1889 struct mtd_device *dev;
1890 struct part_info *part;
1891 u8 pnum;
1892
1893 if (mtdparts_init() !=0)
1894 return 1;
1895
1896 if (argc < 2) {
1897 printf("no partition id specified\n");
1898 return 1;
1899 }
1900
1901 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
1902 return 1;
1903
Stefan Roese76b58832009-05-16 12:04:22 +02001904 current_mtd_dev = dev;
1905 current_mtd_partnum = pnum;
Stefan Roese68d7d652009-03-19 13:30:36 +01001906 current_save();
1907
1908 printf("partition changed to %s%d,%d\n",
1909 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
1910
1911 return 0;
1912}
1913
1914/**
1915 * Routine implementing u-boot mtdparts command. Initialize/update default global
1916 * partition list and process user partition request (list, add, del).
1917 *
1918 * @param cmdtp command internal data
1919 * @param flag command flag
1920 * @param argc number of arguments supplied to the command
1921 * @param argv arguments list
1922 * @return 0 on success, 1 otherwise
1923 */
Kim Phillips088f1b12012-10-29 13:34:31 +00001924static int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc,
1925 char * const argv[])
Stefan Roese68d7d652009-03-19 13:30:36 +01001926{
1927 if (argc == 2) {
1928 if (strcmp(argv[1], "default") == 0) {
1929 setenv("mtdids", (char *)mtdids_default);
1930 setenv("mtdparts", (char *)mtdparts_default);
1931 setenv("partition", NULL);
1932
1933 mtdparts_init();
1934 return 0;
1935 } else if (strcmp(argv[1], "delall") == 0) {
1936 /* this may be the first run, initialize lists if needed */
1937 mtdparts_init();
1938
1939 setenv("mtdparts", NULL);
1940
1941 /* mtd_devices_init() calls current_save() */
1942 return mtd_devices_init();
1943 }
1944 }
1945
1946 /* make sure we are in sync with env variables */
1947 if (mtdparts_init() != 0)
1948 return 1;
1949
1950 if (argc == 1) {
1951 list_partitions();
1952 return 0;
1953 }
1954
1955 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
Ben Gardiner59a50d22010-08-31 17:48:04 -04001956 if (((argc == 5) || (argc == 6)) && (strncmp(argv[1], "add", 3) == 0)) {
Stefan Roese68d7d652009-03-19 13:30:36 +01001957#define PART_ADD_DESC_MAXLEN 64
1958 char tmpbuf[PART_ADD_DESC_MAXLEN];
Ben Gardiner59a50d22010-08-31 17:48:04 -04001959#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
1960 struct mtd_info *mtd;
1961 uint64_t next_offset;
1962#endif
Stefan Roese68d7d652009-03-19 13:30:36 +01001963 u8 type, num, len;
1964 struct mtd_device *dev;
1965 struct mtd_device *dev_tmp;
1966 struct mtdids *id;
1967 struct part_info *p;
1968
1969 if (mtd_id_parse(argv[2], NULL, &type, &num) != 0)
1970 return 1;
1971
1972 if ((id = id_find(type, num)) == NULL) {
1973 printf("no such device %s defined in mtdids variable\n", argv[2]);
1974 return 1;
1975 }
1976
1977 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
1978 len += strlen(argv[3]); /* size@offset */
1979 len += strlen(argv[4]) + 2; /* '(' name ')' */
1980 if (argv[5] && (strlen(argv[5]) == 2))
1981 len += 2; /* 'ro' */
1982
1983 if (len >= PART_ADD_DESC_MAXLEN) {
1984 printf("too long partition description\n");
1985 return 1;
1986 }
1987 sprintf(tmpbuf, "%s:%s(%s)%s",
1988 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
Wolfgang Denke1d29502010-04-28 10:58:10 +02001989 debug("add tmpbuf: %s\n", tmpbuf);
Stefan Roese68d7d652009-03-19 13:30:36 +01001990
1991 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
1992 return 1;
1993
Wolfgang Denke1d29502010-04-28 10:58:10 +02001994 debug("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
Stefan Roese68d7d652009-03-19 13:30:36 +01001995 dev->id->num, dev->id->mtd_id);
1996
Ben Gardiner59a50d22010-08-31 17:48:04 -04001997 p = list_entry(dev->parts.next, struct part_info, link);
1998
1999#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2000 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
2001 return 1;
2002
2003 if (!strcmp(&argv[1][3], ".spread")) {
2004 spread_partition(mtd, p, &next_offset);
2005 debug("increased %s to %d bytes\n", p->name, p->size);
2006 }
2007#endif
2008
2009 dev_tmp = device_find(dev->id->type, dev->id->num);
2010 if (dev_tmp == NULL) {
Stefan Roese68d7d652009-03-19 13:30:36 +01002011 device_add(dev);
Ben Gardiner59a50d22010-08-31 17:48:04 -04002012 } else if (part_add(dev_tmp, p) != 0) {
Stefan Roese68d7d652009-03-19 13:30:36 +01002013 /* merge new partition with existing ones*/
Ben Gardiner59a50d22010-08-31 17:48:04 -04002014 device_del(dev);
2015 return 1;
Stefan Roese68d7d652009-03-19 13:30:36 +01002016 }
2017
2018 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
Robert P. J. Daya22bf162013-02-04 13:51:10 +00002019 printf("generated mtdparts too long, resetting to null\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01002020 return 1;
2021 }
2022
2023 return 0;
2024 }
2025
2026 /* mtdparts del part-id */
2027 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
Wolfgang Denke1d29502010-04-28 10:58:10 +02002028 debug("del: part-id = %s\n", argv[2]);
Stefan Roese68d7d652009-03-19 13:30:36 +01002029
2030 return delete_partition(argv[2]);
2031 }
2032
Ben Gardinerca75b202010-08-31 17:48:03 -04002033#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2034 if ((argc == 2) && (strcmp(argv[1], "spread") == 0))
2035 return spread_partitions();
2036#endif /* CONFIG_CMD_MTDPARTS_SPREAD */
2037
Simon Glass4c12eeb2011-12-10 08:44:01 +00002038 return CMD_RET_USAGE;
Stefan Roese68d7d652009-03-19 13:30:36 +01002039}
2040
2041/***************************************************/
2042U_BOOT_CMD(
2043 chpart, 2, 0, do_chpart,
2044 "change active partition",
2045 "part-id\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02002046 " - change active partition (e.g. part-id = nand0,1)"
Stefan Roese68d7d652009-03-19 13:30:36 +01002047);
2048
Kim Phillips088f1b12012-10-29 13:34:31 +00002049#ifdef CONFIG_SYS_LONGHELP
2050static char mtdparts_help_text[] =
Stefan Roese68d7d652009-03-19 13:30:36 +01002051 "\n"
2052 " - list partition table\n"
2053 "mtdparts delall\n"
2054 " - delete all partitions\n"
2055 "mtdparts del part-id\n"
2056 " - delete partition (e.g. part-id = nand0,1)\n"
2057 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2058 " - add partition\n"
Ben Gardiner59a50d22010-08-31 17:48:04 -04002059#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2060 "mtdparts add.spread <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2061 " - add partition, padding size by skipping bad blocks\n"
2062#endif
Stefan Roese68d7d652009-03-19 13:30:36 +01002063 "mtdparts default\n"
Ben Gardinerca75b202010-08-31 17:48:03 -04002064 " - reset partition table to defaults\n"
2065#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2066 "mtdparts spread\n"
2067 " - adjust the sizes of the partitions so they are\n"
2068 " at least as big as the mtdparts variable specifies\n"
2069 " and they each start on a good block\n\n"
2070#else
2071 "\n"
2072#endif /* CONFIG_CMD_MTDPARTS_SPREAD */
Stefan Roese68d7d652009-03-19 13:30:36 +01002073 "-----\n\n"
2074 "this command uses three environment variables:\n\n"
2075 "'partition' - keeps current partition identifier\n\n"
2076 "partition := <part-id>\n"
2077 "<part-id> := <dev-id>,part_num\n\n"
2078 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2079 "mtdids=<idmap>[,<idmap>,...]\n\n"
2080 "<idmap> := <dev-id>=<mtd-id>\n"
2081 "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n"
2082 "<dev-num> := mtd device number, 0...\n"
2083 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2084 "'mtdparts' - partition list\n\n"
2085 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2086 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2087 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2088 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2089 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2090 "<offset> := partition start offset within the device\n"
2091 "<name> := '(' NAME ')'\n"
Kim Phillips088f1b12012-10-29 13:34:31 +00002092 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)";
2093#endif
2094
2095U_BOOT_CMD(
2096 mtdparts, 6, 0, do_mtdparts,
2097 "define flash/nand partitions", mtdparts_help_text
Stefan Roese68d7d652009-03-19 13:30:36 +01002098);
2099/***************************************************/