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