blob: b375feaad190023e9c11c4084af495e8dbbddffb [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)
Stefan Roese68d7d652009-03-19 13:30:36 +010097#include <linux/mtd/nand.h>
98#include <nand.h>
Stefan Roese68d7d652009-03-19 13:30:36 +010099#endif
100
101#if defined(CONFIG_CMD_ONENAND)
Stefan Roese68d7d652009-03-19 13:30:36 +0100102#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
Stefan Roese68d7d652009-03-19 13:30:36 +0100136static const char *const mtdids_default = NULL;
137#endif
138
139#if defined(MTDPARTS_DEFAULT)
140static const char *const mtdparts_default = MTDPARTS_DEFAULT;
141#else
Stefan Roese68d7d652009-03-19 13:30:36 +0100142static 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 */
Stefan Roese76b58832009-05-16 12:04:22 +0200163struct mtd_device *current_mtd_dev = NULL;
164u8 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100165
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
Stefan Roese76b58832009-05-16 12:04:22 +0200248 if (current_mtd_dev) {
Stefan Roese68d7d652009-03-19 13:30:36 +0100249 mtddevnum = 0;
250 list_for_each(dentry, &devices) {
251 dev = list_entry(dentry, struct mtd_device, link);
Stefan Roese76b58832009-05-16 12:04:22 +0200252 if (dev == current_mtd_dev) {
253 mtddevnum += current_mtd_partnum;
Stefan Roese68d7d652009-03-19 13:30:36 +0100254 sprintf(buf, "%d", mtddevnum);
255 setenv("mtddevnum", buf);
256 break;
257 }
258 mtddevnum += dev->num_parts;
259 }
260
Stefan Roese76b58832009-05-16 12:04:22 +0200261 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +0100262 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
Stefan Roese76b58832009-05-16 12:04:22 +0200282 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);
Stefan Roese68d7d652009-03-19 13:30:36 +0100285
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/**
Stefan Roese864aa032009-05-12 14:31:56 +0200300 * Performs sanity check for supplied flash partition.
301 * Table of existing MTD flash devices is searched and partition device
Stefan Roese68d7d652009-03-19 13:30:36 +0100302 * is located. Alignment with the granularity of nand erasesize is verified.
303 *
304 * @param id of the parent device
305 * @param part partition to validate
306 * @return 0 if partition is valid, 1 otherwise
307 */
Stefan Roese864aa032009-05-12 14:31:56 +0200308static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
Stefan Roese68d7d652009-03-19 13:30:36 +0100309{
Stefan Roese68d7d652009-03-19 13:30:36 +0100310 struct mtd_info *mtd;
Stefan Roese864aa032009-05-12 14:31:56 +0200311 char mtd_dev[16];
312 int i, j;
313 ulong start;
Stefan Roese68d7d652009-03-19 13:30:36 +0100314
Stefan Roese864aa032009-05-12 14:31:56 +0200315 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);
319 return 1;
320 }
Stefan Roese68d7d652009-03-19 13:30:36 +0100321
322 part->sector_size = mtd->erasesize;
323
Stefan Roese864aa032009-05-12 14:31:56 +0200324 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 }
Stefan Roese68d7d652009-03-19 13:30:36 +0100335
Stefan Roese864aa032009-05-12 14:31:56 +0200336 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 }
356
357 printf("%s%d: partition (%s) start offset alignment incorrect\n",
358 MTD_DEV_TYPE(id->type), id->num, part->name);
Stefan Roese68d7d652009-03-19 13:30:36 +0100359 return 1;
Stefan Roese864aa032009-05-12 14:31:56 +0200360
361 start_ok:
362
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;
375
376 printf("%s%d: partition (%s) size alignment incorrect\n",
377 MTD_DEV_TYPE(id->type), id->num, part->name);
378 return 1;
379
380 end_ok:
381 return 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100382 }
383
384 return 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100385}
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
Stefan Roese864aa032009-05-12 14:31:56 +0200420 /*
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);
Stefan Roese68d7d652009-03-19 13:30:36 +0100425}
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
Stefan Roese76b58832009-05-16 12:04:22 +0200444 if (dev == current_mtd_dev) {
Stefan Roese68d7d652009-03-19 13:30:36 +0100445 /* we are modyfing partitions for the current device,
446 * update current */
447 struct part_info *curr_pi;
Stefan Roese76b58832009-05-16 12:04:22 +0200448 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +0100449
450 if (curr_pi) {
451 if (curr_pi == part) {
452 printf("current partition deleted, resetting current to 0\n");
Stefan Roese76b58832009-05-16 12:04:22 +0200453 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100454 } else if (part->offset <= curr_pi->offset) {
Stefan Roese76b58832009-05-16 12:04:22 +0200455 current_mtd_partnum--;
Stefan Roese68d7d652009-03-19 13:30:36 +0100456 }
457 current_save_needed = 1;
458 }
459 }
460
Stefan Roese68d7d652009-03-19 13:30:36 +0100461 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
Stefan Roese68d7d652009-03-19 13:30:36 +0100487 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;
Stefan Roese76b58832009-05-16 12:04:22 +0200519 if (dev == current_mtd_dev)
520 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +0100521
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 */
Stefan Roese76b58832009-05-16 12:04:22 +0200540 current_mtd_partnum++;
Stefan Roese68d7d652009-03-19 13:30:36 +0100541 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{
Stefan Roese864aa032009-05-12 14:31:56 +0200702 struct mtd_info *mtd;
703 char mtd_dev[16];
Stefan Roese68d7d652009-03-19 13:30:36 +0100704
Stefan Roese864aa032009-05-12 14:31:56 +0200705 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 }
Stefan Roese68d7d652009-03-19 13:30:36 +0100711
Stefan Roese864aa032009-05-12 14:31:56 +0200712 *size = mtd->size;
Stefan Roese68d7d652009-03-19 13:30:36 +0100713
Stefan Roese864aa032009-05-12 14:31:56 +0200714 return 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100715}
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
Stefan Roese76b58832009-05-16 12:04:22 +0200753 if (dev == current_mtd_dev) {
Stefan Roese68d7d652009-03-19 13:30:36 +0100754 /* we just deleted current device */
755 if (list_empty(&devices)) {
Stefan Roese76b58832009-05-16 12:04:22 +0200756 current_mtd_dev = NULL;
Stefan Roese68d7d652009-03-19 13:30:36 +0100757 } else {
758 /* reset first partition from first dev from the
759 * devices list as current */
Stefan Roese76b58832009-05-16 12:04:22 +0200760 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
761 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100762 }
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 */
779static struct mtd_device* device_find(u8 type, u8 num)
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)) {
Stefan Roese76b58832009-05-16 12:04:22 +0200804 current_mtd_dev = dev;
805 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100806 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';
Stefan Roese76b58832009-05-16 12:04:22 +0200961 current_mtd_dev = NULL;
Stefan Roese68d7d652009-03-19 13:30:36 +0100962 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);
David Brownell08f077d2009-04-16 19:55:48 -07001225 printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001226
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
Stefan Roese76b58832009-05-16 12:04:22 +02001241 /* 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);
Stefan Roese68d7d652009-03-19 13:30:36 +01001244 if (part) {
1245 printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
Stefan Roese76b58832009-05-16 12:04:22 +02001246 MTD_DEV_TYPE(current_mtd_dev->id->type),
1247 current_mtd_dev->id->num, current_mtd_partnum,
Stefan Roese68d7d652009-03-19 13:30:36 +01001248 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");
Wolfgang Denk36c91692009-05-17 16:01:54 +02001255 printf("mtdids : %s\n",
1256 mtdids_default ? mtdids_default : "none");
1257 printf("mtdparts: %s\n",
1258 mtdparts_default ? mtdparts_default : "none");
Stefan Roese68d7d652009-03-19 13:30:36 +01001259}
1260
1261/**
1262 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1263 * corresponding device and verify partition number.
1264 *
1265 * @param id string describing device and partition or partition name
1266 * @param dev pointer to the requested device (output)
1267 * @param part_num verified partition number (output)
1268 * @param part pointer to requested partition (output)
1269 * @return 0 on success, 1 otherwise
1270 */
1271int find_dev_and_part(const char *id, struct mtd_device **dev,
1272 u8 *part_num, struct part_info **part)
1273{
1274 struct list_head *dentry, *pentry;
1275 u8 type, dnum, pnum;
1276 const char *p;
1277
1278 DEBUGF("--- find_dev_and_part ---\nid = %s\n", id);
1279
1280 list_for_each(dentry, &devices) {
1281 *part_num = 0;
1282 *dev = list_entry(dentry, struct mtd_device, link);
1283 list_for_each(pentry, &(*dev)->parts) {
1284 *part = list_entry(pentry, struct part_info, link);
1285 if (strcmp((*part)->name, id) == 0)
1286 return 0;
1287 (*part_num)++;
1288 }
1289 }
1290
1291 p = id;
1292 *dev = NULL;
1293 *part = NULL;
1294 *part_num = 0;
1295
1296 if (mtd_id_parse(p, &p, &type, &dnum) != 0)
1297 return 1;
1298
1299 if ((*p++ != ',') || (*p == '\0')) {
1300 printf("no partition number specified\n");
1301 return 1;
1302 }
1303 pnum = simple_strtoul(p, (char **)&p, 0);
1304 if (*p != '\0') {
1305 printf("unexpected trailing character '%c'\n", *p);
1306 return 1;
1307 }
1308
1309 if ((*dev = device_find(type, dnum)) == NULL) {
1310 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1311 return 1;
1312 }
1313
1314 if ((*part = mtd_part_info(*dev, pnum)) == NULL) {
1315 printf("no such partition\n");
1316 *dev = NULL;
1317 return 1;
1318 }
1319
1320 *part_num = pnum;
1321
1322 return 0;
1323}
1324
1325/**
1326 * Find and delete partition. For partition id format see find_dev_and_part().
1327 *
1328 * @param id string describing device and partition
1329 * @return 0 on success, 1 otherwise
1330 */
1331static int delete_partition(const char *id)
1332{
1333 u8 pnum;
1334 struct mtd_device *dev;
1335 struct part_info *part;
1336
1337 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1338
Wolfgang Denk37daa772009-09-02 10:21:20 +02001339 DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08x@0x%08x\n",
Stefan Roese68d7d652009-03-19 13:30:36 +01001340 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1341 part->name, part->size, part->offset);
1342
1343 if (part_del(dev, part) != 0)
1344 return 1;
1345
1346 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1347 printf("generated mtdparts too long, reseting to null\n");
1348 return 1;
1349 }
1350 return 0;
1351 }
1352
1353 printf("partition %s not found\n", id);
1354 return 1;
1355}
1356
1357/**
1358 * Accept character string describing mtd partitions and call device_parse()
1359 * for each entry. Add created devices to the global devices list.
1360 *
1361 * @param mtdparts string specifing mtd partitions
1362 * @return 0 on success, 1 otherwise
1363 */
1364static int parse_mtdparts(const char *const mtdparts)
1365{
1366 const char *p = mtdparts;
1367 struct mtd_device *dev;
1368 int err = 1;
1369
1370 DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1371
1372 /* delete all devices and partitions */
1373 if (mtd_devices_init() != 0) {
1374 printf("could not initialise device list\n");
1375 return err;
1376 }
1377
1378 /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
1379 p = getenv("mtdparts");
1380
1381 if (strncmp(p, "mtdparts=", 9) != 0) {
1382 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1383 return err;
1384 }
1385 p += 9;
1386
1387 while (p && (*p != '\0')) {
1388 err = 1;
1389 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1390 break;
1391
1392 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1393 dev->id->num, dev->id->mtd_id);
1394
1395 /* check if parsed device is already on the list */
1396 if (device_find(dev->id->type, dev->id->num) != NULL) {
1397 printf("device %s%d redefined, please correct mtdparts variable\n",
1398 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1399 break;
1400 }
1401
1402 list_add_tail(&dev->link, &devices);
1403 err = 0;
1404 }
1405 if (err == 1) {
1406 device_delall(&devices);
1407 return 1;
1408 }
1409
1410 return 0;
1411}
1412
1413/**
1414 * Parse provided string describing mtdids mapping (see file header for mtdids
1415 * variable format). Allocate memory for each entry and add all found entries
1416 * to the global mtdids list.
1417 *
1418 * @param ids mapping string
1419 * @return 0 on success, 1 otherwise
1420 */
1421static int parse_mtdids(const char *const ids)
1422{
1423 const char *p = ids;
1424 const char *mtd_id;
1425 int mtd_id_len;
1426 struct mtdids *id;
1427 struct list_head *entry, *n;
1428 struct mtdids *id_tmp;
1429 u8 type, num;
1430 u32 size;
1431 int ret = 1;
1432
1433 DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
1434
1435 /* clean global mtdids list */
1436 list_for_each_safe(entry, n, &mtdids) {
1437 id_tmp = list_entry(entry, struct mtdids, link);
1438 DEBUGF("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
1439 list_del(entry);
1440 free(id_tmp);
1441 }
1442 last_ids[0] = '\0';
1443 INIT_LIST_HEAD(&mtdids);
1444
1445 while(p && (*p != '\0')) {
1446
1447 ret = 1;
1448 /* parse 'nor'|'nand'|'onenand'<dev-num> */
1449 if (mtd_id_parse(p, &p, &type, &num) != 0)
1450 break;
1451
1452 if (*p != '=') {
1453 printf("mtdids: incorrect <dev-num>\n");
1454 break;
1455 }
1456 p++;
1457
1458 /* check if requested device exists */
1459 if (mtd_device_validate(type, num, &size) != 0)
1460 return 1;
1461
1462 /* locate <mtd-id> */
1463 mtd_id = p;
1464 if ((p = strchr(mtd_id, ',')) != NULL) {
1465 mtd_id_len = p - mtd_id + 1;
1466 p++;
1467 } else {
1468 mtd_id_len = strlen(mtd_id) + 1;
1469 }
1470 if (mtd_id_len == 0) {
1471 printf("mtdids: no <mtd-id> identifier\n");
1472 break;
1473 }
1474
1475 /* check if this id is already on the list */
1476 int double_entry = 0;
1477 list_for_each(entry, &mtdids) {
1478 id_tmp = list_entry(entry, struct mtdids, link);
1479 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1480 double_entry = 1;
1481 break;
1482 }
1483 }
1484 if (double_entry) {
1485 printf("device id %s%d redefined, please correct mtdids variable\n",
1486 MTD_DEV_TYPE(type), num);
1487 break;
1488 }
1489
1490 /* allocate mtdids structure */
1491 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1492 printf("out of memory\n");
1493 break;
1494 }
1495 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1496 id->num = num;
1497 id->type = type;
1498 id->size = size;
1499 id->mtd_id = (char *)(id + 1);
1500 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1501 id->mtd_id[mtd_id_len - 1] = '\0';
1502 INIT_LIST_HEAD(&id->link);
1503
1504 DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
1505 MTD_DEV_TYPE(id->type), id->num,
1506 id->size, id->mtd_id);
1507
1508 list_add_tail(&id->link, &mtdids);
1509 ret = 0;
1510 }
1511 if (ret == 1) {
1512 /* clean mtdids list and free allocated memory */
1513 list_for_each_safe(entry, n, &mtdids) {
1514 id_tmp = list_entry(entry, struct mtdids, link);
1515 list_del(entry);
1516 free(id_tmp);
1517 }
1518 return 1;
1519 }
1520
1521 return 0;
1522}
1523
1524/**
1525 * Parse and initialize global mtdids mapping and create global
1526 * device/partition list.
1527 *
1528 * @return 0 on success, 1 otherwise
1529 */
1530int mtdparts_init(void)
1531{
1532 static int initialized = 0;
1533 const char *ids, *parts;
1534 const char *current_partition;
1535 int ids_changed;
1536 char tmp_ep[PARTITION_MAXLEN];
1537
1538 DEBUGF("\n---mtdparts_init---\n");
1539 if (!initialized) {
1540 INIT_LIST_HEAD(&mtdids);
1541 INIT_LIST_HEAD(&devices);
1542 memset(last_ids, 0, MTDIDS_MAXLEN);
1543 memset(last_parts, 0, MTDPARTS_MAXLEN);
1544 memset(last_partition, 0, PARTITION_MAXLEN);
1545 initialized = 1;
1546 }
1547
1548 /* get variables */
1549 ids = getenv("mtdids");
1550 parts = getenv("mtdparts");
1551 current_partition = getenv("partition");
1552
1553 /* save it for later parsing, cannot rely on current partition pointer
1554 * as 'partition' variable may be updated during init */
1555 tmp_ep[0] = '\0';
1556 if (current_partition)
1557 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1558
1559 DEBUGF("last_ids : %s\n", last_ids);
1560 DEBUGF("env_ids : %s\n", ids);
1561 DEBUGF("last_parts: %s\n", last_parts);
1562 DEBUGF("env_parts : %s\n\n", parts);
1563
1564 DEBUGF("last_partition : %s\n", last_partition);
1565 DEBUGF("env_partition : %s\n", current_partition);
1566
1567 /* if mtdids varible is empty try to use defaults */
1568 if (!ids) {
1569 if (mtdids_default) {
1570 DEBUGF("mtdids variable not defined, using default\n");
1571 ids = mtdids_default;
1572 setenv("mtdids", (char *)ids);
1573 } else {
1574 printf("mtdids not defined, no default present\n");
1575 return 1;
1576 }
1577 }
1578 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1579 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1580 return 1;
1581 }
1582
1583 /* do no try to use defaults when mtdparts variable is not defined,
1584 * just check the length */
1585 if (!parts)
1586 printf("mtdparts variable not set, see 'help mtdparts'\n");
1587
1588 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1589 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1590 return 1;
1591 }
1592
1593 /* check if we have already parsed those mtdids */
1594 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1595 ids_changed = 0;
1596 } else {
1597 ids_changed = 1;
1598
1599 if (parse_mtdids(ids) != 0) {
1600 mtd_devices_init();
1601 return 1;
1602 }
1603
1604 /* ok it's good, save new ids */
1605 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1606 }
1607
1608 /* parse partitions if either mtdparts or mtdids were updated */
1609 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1610 if (parse_mtdparts(parts) != 0)
1611 return 1;
1612
1613 if (list_empty(&devices)) {
1614 printf("mtdparts_init: no valid partitions\n");
1615 return 1;
1616 }
1617
1618 /* ok it's good, save new parts */
1619 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1620
1621 /* reset first partition from first dev from the list as current */
Stefan Roese76b58832009-05-16 12:04:22 +02001622 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
1623 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +01001624 current_save();
1625
Stefan Roese76b58832009-05-16 12:04:22 +02001626 DEBUGF("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n",
1627 MTD_DEV_TYPE(current_mtd_dev->id->type),
1628 current_mtd_dev->id->num, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +01001629 }
1630
1631 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1632 if (!parts && (last_parts[0] != '\0'))
1633 return mtd_devices_init();
1634
1635 /* do not process current partition if mtdparts variable is null */
1636 if (!parts)
1637 return 0;
1638
1639 /* is current partition set in environment? if so, use it */
1640 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1641 struct part_info *p;
1642 struct mtd_device *cdev;
1643 u8 pnum;
1644
1645 DEBUGF("--- getting current partition: %s\n", tmp_ep);
1646
1647 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
Stefan Roese76b58832009-05-16 12:04:22 +02001648 current_mtd_dev = cdev;
1649 current_mtd_partnum = pnum;
Stefan Roese68d7d652009-03-19 13:30:36 +01001650 current_save();
1651 }
1652 } else if (getenv("partition") == NULL) {
1653 DEBUGF("no partition variable set, setting...\n");
1654 current_save();
1655 }
1656
1657 return 0;
1658}
1659
1660/**
1661 * Return pointer to the partition of a requested number from a requested
1662 * device.
1663 *
1664 * @param dev device that is to be searched for a partition
1665 * @param part_num requested partition number
1666 * @return pointer to the part_info, NULL otherwise
1667 */
1668static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
1669{
1670 struct list_head *entry;
1671 struct part_info *part;
1672 int num;
1673
1674 if (!dev)
1675 return NULL;
1676
1677 DEBUGF("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
1678 part_num, MTD_DEV_TYPE(dev->id->type),
1679 dev->id->num, dev->id->mtd_id);
1680
1681 if (part_num >= dev->num_parts) {
1682 printf("invalid partition number %d for device %s%d (%s)\n",
1683 part_num, MTD_DEV_TYPE(dev->id->type),
1684 dev->id->num, dev->id->mtd_id);
1685 return NULL;
1686 }
1687
1688 /* locate partition number, return it */
1689 num = 0;
1690 list_for_each(entry, &dev->parts) {
1691 part = list_entry(entry, struct part_info, link);
1692
1693 if (part_num == num++) {
1694 return part;
1695 }
1696 }
1697
1698 return NULL;
1699}
1700
1701/***************************************************/
1702/* U-boot commands */
1703/***************************************************/
1704/* command line only */
1705/**
1706 * Routine implementing u-boot chpart command. Sets new current partition based
1707 * on the user supplied partition id. For partition id format see find_dev_and_part().
1708 *
1709 * @param cmdtp command internal data
1710 * @param flag command flag
1711 * @param argc number of arguments supplied to the command
1712 * @param argv arguments list
1713 * @return 0 on success, 1 otherwise
1714 */
1715int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1716{
1717/* command line only */
1718 struct mtd_device *dev;
1719 struct part_info *part;
1720 u8 pnum;
1721
1722 if (mtdparts_init() !=0)
1723 return 1;
1724
1725 if (argc < 2) {
1726 printf("no partition id specified\n");
1727 return 1;
1728 }
1729
1730 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
1731 return 1;
1732
Stefan Roese76b58832009-05-16 12:04:22 +02001733 current_mtd_dev = dev;
1734 current_mtd_partnum = pnum;
Stefan Roese68d7d652009-03-19 13:30:36 +01001735 current_save();
1736
1737 printf("partition changed to %s%d,%d\n",
1738 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
1739
1740 return 0;
1741}
1742
1743/**
1744 * Routine implementing u-boot mtdparts command. Initialize/update default global
1745 * partition list and process user partition request (list, add, del).
1746 *
1747 * @param cmdtp command internal data
1748 * @param flag command flag
1749 * @param argc number of arguments supplied to the command
1750 * @param argv arguments list
1751 * @return 0 on success, 1 otherwise
1752 */
1753int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1754{
1755 if (argc == 2) {
1756 if (strcmp(argv[1], "default") == 0) {
1757 setenv("mtdids", (char *)mtdids_default);
1758 setenv("mtdparts", (char *)mtdparts_default);
1759 setenv("partition", NULL);
1760
1761 mtdparts_init();
1762 return 0;
1763 } else if (strcmp(argv[1], "delall") == 0) {
1764 /* this may be the first run, initialize lists if needed */
1765 mtdparts_init();
1766
1767 setenv("mtdparts", NULL);
1768
1769 /* mtd_devices_init() calls current_save() */
1770 return mtd_devices_init();
1771 }
1772 }
1773
1774 /* make sure we are in sync with env variables */
1775 if (mtdparts_init() != 0)
1776 return 1;
1777
1778 if (argc == 1) {
1779 list_partitions();
1780 return 0;
1781 }
1782
1783 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
1784 if (((argc == 5) || (argc == 6)) && (strcmp(argv[1], "add") == 0)) {
1785#define PART_ADD_DESC_MAXLEN 64
1786 char tmpbuf[PART_ADD_DESC_MAXLEN];
1787 u8 type, num, len;
1788 struct mtd_device *dev;
1789 struct mtd_device *dev_tmp;
1790 struct mtdids *id;
1791 struct part_info *p;
1792
1793 if (mtd_id_parse(argv[2], NULL, &type, &num) != 0)
1794 return 1;
1795
1796 if ((id = id_find(type, num)) == NULL) {
1797 printf("no such device %s defined in mtdids variable\n", argv[2]);
1798 return 1;
1799 }
1800
1801 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
1802 len += strlen(argv[3]); /* size@offset */
1803 len += strlen(argv[4]) + 2; /* '(' name ')' */
1804 if (argv[5] && (strlen(argv[5]) == 2))
1805 len += 2; /* 'ro' */
1806
1807 if (len >= PART_ADD_DESC_MAXLEN) {
1808 printf("too long partition description\n");
1809 return 1;
1810 }
1811 sprintf(tmpbuf, "%s:%s(%s)%s",
1812 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
1813 DEBUGF("add tmpbuf: %s\n", tmpbuf);
1814
1815 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
1816 return 1;
1817
1818 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1819 dev->id->num, dev->id->mtd_id);
1820
1821 if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) {
1822 device_add(dev);
1823 } else {
1824 /* merge new partition with existing ones*/
1825 p = list_entry(dev->parts.next, struct part_info, link);
1826 if (part_add(dev_tmp, p) != 0) {
1827 device_del(dev);
1828 return 1;
1829 }
1830 }
1831
1832 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1833 printf("generated mtdparts too long, reseting to null\n");
1834 return 1;
1835 }
1836
1837 return 0;
1838 }
1839
1840 /* mtdparts del part-id */
1841 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
1842 DEBUGF("del: part-id = %s\n", argv[2]);
1843
1844 return delete_partition(argv[2]);
1845 }
1846
1847 cmd_usage(cmdtp);
1848 return 1;
1849}
1850
1851/***************************************************/
1852U_BOOT_CMD(
1853 chpart, 2, 0, do_chpart,
1854 "change active partition",
1855 "part-id\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001856 " - change active partition (e.g. part-id = nand0,1)"
Stefan Roese68d7d652009-03-19 13:30:36 +01001857);
1858
1859U_BOOT_CMD(
1860 mtdparts, 6, 0, do_mtdparts,
1861 "define flash/nand partitions",
1862 "\n"
1863 " - list partition table\n"
1864 "mtdparts delall\n"
1865 " - delete all partitions\n"
1866 "mtdparts del part-id\n"
1867 " - delete partition (e.g. part-id = nand0,1)\n"
1868 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
1869 " - add partition\n"
1870 "mtdparts default\n"
1871 " - reset partition table to defaults\n\n"
1872 "-----\n\n"
1873 "this command uses three environment variables:\n\n"
1874 "'partition' - keeps current partition identifier\n\n"
1875 "partition := <part-id>\n"
1876 "<part-id> := <dev-id>,part_num\n\n"
1877 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
1878 "mtdids=<idmap>[,<idmap>,...]\n\n"
1879 "<idmap> := <dev-id>=<mtd-id>\n"
1880 "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n"
1881 "<dev-num> := mtd device number, 0...\n"
1882 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
1883 "'mtdparts' - partition list\n\n"
1884 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
1885 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
1886 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
1887 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
1888 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
1889 "<offset> := partition start offset within the device\n"
1890 "<name> := '(' NAME ')'\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001891 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)"
Stefan Roese68d7d652009-03-19 13:30:36 +01001892);
1893/***************************************************/