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