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