blob: 2d1446ecb8d5ea9784da8f08e36c4a321dbe8490 [file] [log] [blame]
Stefan Roeseb1423dd2009-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 Roeseb1423dd2009-03-19 13:30:36 +010087#include <common.h>
88#include <command.h>
89#include <malloc.h>
Ladislav Michl816af312009-03-23 12:06:07 +010090#include <jffs2/load_kernel.h>
Stefan Roeseb1423dd2009-03-19 13:30:36 +010091#include <linux/list.h>
92#include <linux/ctype.h>
Stefan Roese8e66ea72009-05-12 14:31:56 +020093#include <linux/err.h>
94#include <linux/mtd/mtd.h>
Stefan Roeseb1423dd2009-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 Roeseb1423dd2009-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
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100140static const char *const mtdids_default = NULL;
141#endif
142
143#if defined(MTDPARTS_DEFAULT)
144static const char *const mtdparts_default = MTDPARTS_DEFAULT;
145#else
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100146static const char *const mtdparts_default = NULL;
147#endif
148
149/* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
150#define MTDIDS_MAXLEN 128
151#define MTDPARTS_MAXLEN 512
152#define PARTITION_MAXLEN 16
153static char last_ids[MTDIDS_MAXLEN];
154static char last_parts[MTDPARTS_MAXLEN];
155static char last_partition[PARTITION_MAXLEN];
156
157/* low level jffs2 cache cleaning routine */
158extern void jffs2_free_cache(struct part_info *part);
159
160/* mtdids mapping list, filled by parse_ids() */
161struct list_head mtdids;
162
163/* device/partition list, parse_cmdline() parses into here */
164struct list_head devices;
165
166/* current active device and partition number */
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200167struct mtd_device *current_mtd_dev = NULL;
168u8 current_mtd_partnum = 0;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100169
170static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
171
172/* command line only routines */
173static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
174static int device_del(struct mtd_device *dev);
175
176/**
177 * Parses a string into a number. The number stored at ptr is
178 * potentially suffixed with K (for kilobytes, or 1024 bytes),
179 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
180 * 1073741824). If the number is suffixed with K, M, or G, then
181 * the return value is the number multiplied by one kilobyte, one
182 * megabyte, or one gigabyte, respectively.
183 *
184 * @param ptr where parse begins
185 * @param retptr output pointer to next char after parse completes (output)
186 * @return resulting unsigned int
187 */
188static unsigned long memsize_parse (const char *const ptr, const char **retptr)
189{
190 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
191
192 switch (**retptr) {
193 case 'G':
194 case 'g':
195 ret <<= 10;
196 case 'M':
197 case 'm':
198 ret <<= 10;
199 case 'K':
200 case 'k':
201 ret <<= 10;
202 (*retptr)++;
203 default:
204 break;
205 }
206
207 return ret;
208}
209
210/**
211 * Format string describing supplied size. This routine does the opposite job
212 * to memsize_parse(). Size in bytes is converted to string and if possible
213 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
214 *
215 * Note, that this routine does not check for buffer overflow, it's the caller
216 * who must assure enough space.
217 *
218 * @param buf output buffer
219 * @param size size to be converted to string
220 */
221static void memsize_format(char *buf, u32 size)
222{
223#define SIZE_GB ((u32)1024*1024*1024)
224#define SIZE_MB ((u32)1024*1024)
225#define SIZE_KB ((u32)1024)
226
227 if ((size % SIZE_GB) == 0)
228 sprintf(buf, "%ug", size/SIZE_GB);
229 else if ((size % SIZE_MB) == 0)
230 sprintf(buf, "%um", size/SIZE_MB);
231 else if (size % SIZE_KB == 0)
232 sprintf(buf, "%uk", size/SIZE_KB);
233 else
234 sprintf(buf, "%u", size);
235}
236
237/**
238 * This routine does global indexing of all partitions. Resulting index for
239 * current partition is saved in 'mtddevnum'. Current partition name in
240 * 'mtddevname'.
241 */
242static void index_partitions(void)
243{
244 char buf[16];
245 u16 mtddevnum;
246 struct part_info *part;
247 struct list_head *dentry;
248 struct mtd_device *dev;
249
250 DEBUGF("--- index partitions ---\n");
251
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200252 if (current_mtd_dev) {
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100253 mtddevnum = 0;
254 list_for_each(dentry, &devices) {
255 dev = list_entry(dentry, struct mtd_device, link);
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200256 if (dev == current_mtd_dev) {
257 mtddevnum += current_mtd_partnum;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100258 sprintf(buf, "%d", mtddevnum);
259 setenv("mtddevnum", buf);
260 break;
261 }
262 mtddevnum += dev->num_parts;
263 }
264
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200265 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100266 setenv("mtddevname", part->name);
267
268 DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
269 } else {
270 setenv("mtddevnum", NULL);
271 setenv("mtddevname", NULL);
272
273 DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
274 }
275}
276
277/**
278 * Save current device and partition in environment variable 'partition'.
279 */
280static void current_save(void)
281{
282 char buf[16];
283
284 DEBUGF("--- current_save ---\n");
285
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200286 if (current_mtd_dev) {
287 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type),
288 current_mtd_dev->id->num, current_mtd_partnum);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100289
290 setenv("partition", buf);
291 strncpy(last_partition, buf, 16);
292
293 DEBUGF("=> partition %s\n", buf);
294 } else {
295 setenv("partition", NULL);
296 last_partition[0] = '\0';
297
298 DEBUGF("=> partition NULL\n");
299 }
300 index_partitions();
301}
302
303/**
Stefan Roese8e66ea72009-05-12 14:31:56 +0200304 * Performs sanity check for supplied flash partition.
305 * Table of existing MTD flash devices is searched and partition device
306 * is located. Alignment with the granularity of nand erasesize is verified.
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100307 *
308 * @param id of the parent device
309 * @param part partition to validate
310 * @return 0 if partition is valid, 1 otherwise
311 */
Stefan Roese8e66ea72009-05-12 14:31:56 +0200312static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100313{
Stefan Roese8e66ea72009-05-12 14:31:56 +0200314 struct mtd_info *mtd;
315 char mtd_dev[16];
316 int i, j;
317 ulong start;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100318
Stefan Roese8e66ea72009-05-12 14:31:56 +0200319 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(id->type), id->num);
320 mtd = get_mtd_device_nm(mtd_dev);
321 if (IS_ERR(mtd)) {
322 printf("Partition %s not found on device %s!\n", part->name, mtd_dev);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100323 return 1;
324 }
325
Stefan Roese8e66ea72009-05-12 14:31:56 +0200326 part->sector_size = mtd->erasesize;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100327
Stefan Roese8e66ea72009-05-12 14:31:56 +0200328 if (!mtd->numeraseregions) {
329 /*
330 * Only one eraseregion (NAND, OneNAND or uniform NOR),
331 * checking for alignment is easy here
332 */
333 if ((unsigned long)part->offset % mtd->erasesize) {
334 printf("%s%d: partition (%s) start offset"
335 "alignment incorrect\n",
336 MTD_DEV_TYPE(id->type), id->num, part->name);
337 return 1;
338 }
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100339
Stefan Roese8e66ea72009-05-12 14:31:56 +0200340 if (part->size % mtd->erasesize) {
341 printf("%s%d: partition (%s) size alignment incorrect\n",
342 MTD_DEV_TYPE(id->type), id->num, part->name);
343 return 1;
344 }
345 } else {
346 /*
347 * Multiple eraseregions (non-uniform NOR),
348 * checking for alignment is more complex here
349 */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100350
Stefan Roese8e66ea72009-05-12 14:31:56 +0200351 /* Check start alignment */
352 for (i = 0; i < mtd->numeraseregions; i++) {
353 start = mtd->eraseregions[i].offset;
354 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
355 if (part->offset == start)
356 goto start_ok;
357 start += mtd->eraseregions[i].erasesize;
358 }
359 }
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100360
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100361 printf("%s%d: partition (%s) start offset alignment incorrect\n",
Stefan Roese8e66ea72009-05-12 14:31:56 +0200362 MTD_DEV_TYPE(id->type), id->num, part->name);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100363 return 1;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100364
Stefan Roese8e66ea72009-05-12 14:31:56 +0200365 start_ok:
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100366
Stefan Roese8e66ea72009-05-12 14:31:56 +0200367 /* Check end/size alignment */
368 for (i = 0; i < mtd->numeraseregions; i++) {
369 start = mtd->eraseregions[i].offset;
370 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
371 if ((part->offset + part->size) == start)
372 goto end_ok;
373 start += mtd->eraseregions[i].erasesize;
374 }
375 }
376 /* Check last sector alignment */
377 if ((part->offset + part->size) == start)
378 goto end_ok;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100379
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100380 printf("%s%d: partition (%s) size alignment incorrect\n",
Stefan Roese8e66ea72009-05-12 14:31:56 +0200381 MTD_DEV_TYPE(id->type), id->num, part->name);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100382 return 1;
Stefan Roese8e66ea72009-05-12 14:31:56 +0200383
384 end_ok:
385 return 0;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100386 }
387
388 return 0;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100389}
390
391
392/**
393 * Performs sanity check for supplied partition. Offset and size are verified
394 * to be within valid range. Partition type is checked and either
395 * parts_validate_nor() or parts_validate_nand() is called with the argument
396 * of part.
397 *
398 * @param id of the parent device
399 * @param part partition to validate
400 * @return 0 if partition is valid, 1 otherwise
401 */
402static int part_validate(struct mtdids *id, struct part_info *part)
403{
404 if (part->size == SIZE_REMAINING)
405 part->size = id->size - part->offset;
406
407 if (part->offset > id->size) {
408 printf("%s: offset %08x beyond flash size %08x\n",
409 id->mtd_id, part->offset, id->size);
410 return 1;
411 }
412
413 if ((part->offset + part->size) <= part->offset) {
414 printf("%s%d: partition (%s) size too big\n",
415 MTD_DEV_TYPE(id->type), id->num, part->name);
416 return 1;
417 }
418
419 if (part->offset + part->size > id->size) {
420 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
421 return 1;
422 }
423
Stefan Roese8e66ea72009-05-12 14:31:56 +0200424 /*
425 * Now we need to check if the partition starts and ends on
426 * sector (eraseblock) regions
427 */
428 return part_validate_eraseblock(id, part);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100429}
430
431/**
432 * Delete selected partition from the partion list of the specified device.
433 *
434 * @param dev device to delete partition from
435 * @param part partition to delete
436 * @return 0 on success, 1 otherwise
437 */
438static int part_del(struct mtd_device *dev, struct part_info *part)
439{
440 u8 current_save_needed = 0;
441
442 /* if there is only one partition, remove whole device */
443 if (dev->num_parts == 1)
444 return device_del(dev);
445
446 /* otherwise just delete this partition */
447
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200448 if (dev == current_mtd_dev) {
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100449 /* we are modyfing partitions for the current device,
450 * update current */
451 struct part_info *curr_pi;
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200452 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100453
454 if (curr_pi) {
455 if (curr_pi == part) {
456 printf("current partition deleted, resetting current to 0\n");
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200457 current_mtd_partnum = 0;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100458 } else if (part->offset <= curr_pi->offset) {
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200459 current_mtd_partnum--;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100460 }
461 current_save_needed = 1;
462 }
463 }
464
465#ifdef CONFIG_NAND_LEGACY
466 jffs2_free_cache(part);
467#endif
468 list_del(&part->link);
469 free(part);
470 dev->num_parts--;
471
472 if (current_save_needed > 0)
473 current_save();
474 else
475 index_partitions();
476
477 return 0;
478}
479
480/**
481 * Delete all partitions from parts head list, free memory.
482 *
483 * @param head list of partitions to delete
484 */
485static void part_delall(struct list_head *head)
486{
487 struct list_head *entry, *n;
488 struct part_info *part_tmp;
489
490 /* clean tmp_list and free allocated memory */
491 list_for_each_safe(entry, n, head) {
492 part_tmp = list_entry(entry, struct part_info, link);
493
494#ifdef CONFIG_NAND_LEGACY
495 jffs2_free_cache(part_tmp);
496#endif
497 list_del(entry);
498 free(part_tmp);
499 }
500}
501
502/**
503 * Add new partition to the supplied partition list. Make sure partitions are
504 * sorted by offset in ascending order.
505 *
506 * @param head list this partition is to be added to
507 * @param new partition to be added
508 */
509static int part_sort_add(struct mtd_device *dev, struct part_info *part)
510{
511 struct list_head *entry;
512 struct part_info *new_pi, *curr_pi;
513
514 /* link partition to parrent dev */
515 part->dev = dev;
516
517 if (list_empty(&dev->parts)) {
518 DEBUGF("part_sort_add: list empty\n");
519 list_add(&part->link, &dev->parts);
520 dev->num_parts++;
521 index_partitions();
522 return 0;
523 }
524
525 new_pi = list_entry(&part->link, struct part_info, link);
526
527 /* get current partition info if we are updating current device */
528 curr_pi = NULL;
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200529 if (dev == current_mtd_dev)
530 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100531
532 list_for_each(entry, &dev->parts) {
533 struct part_info *pi;
534
535 pi = list_entry(entry, struct part_info, link);
536
537 /* be compliant with kernel cmdline, allow only one partition at offset zero */
538 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
539 printf("cannot add second partition at offset 0\n");
540 return 1;
541 }
542
543 if (new_pi->offset <= pi->offset) {
544 list_add_tail(&part->link, entry);
545 dev->num_parts++;
546
547 if (curr_pi && (pi->offset <= curr_pi->offset)) {
548 /* we are modyfing partitions for the current
549 * device, update current */
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200550 current_mtd_partnum++;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100551 current_save();
552 } else {
553 index_partitions();
554 }
555 return 0;
556 }
557 }
558
559 list_add_tail(&part->link, &dev->parts);
560 dev->num_parts++;
561 index_partitions();
562 return 0;
563}
564
565/**
566 * Add provided partition to the partition list of a given device.
567 *
568 * @param dev device to which partition is added
569 * @param part partition to be added
570 * @return 0 on success, 1 otherwise
571 */
572static int part_add(struct mtd_device *dev, struct part_info *part)
573{
574 /* verify alignment and size */
575 if (part_validate(dev->id, part) != 0)
576 return 1;
577
578 /* partition is ok, add it to the list */
579 if (part_sort_add(dev, part) != 0)
580 return 1;
581
582 return 0;
583}
584
585/**
586 * Parse one partition definition, allocate memory and return pointer to this
587 * location in retpart.
588 *
589 * @param partdef pointer to the partition definition string i.e. <part-def>
590 * @param ret output pointer to next char after parse completes (output)
591 * @param retpart pointer to the allocated partition (output)
592 * @return 0 on success, 1 otherwise
593 */
594static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
595{
596 struct part_info *part;
597 unsigned long size;
598 unsigned long offset;
599 const char *name;
600 int name_len;
601 unsigned int mask_flags;
602 const char *p;
603
604 p = partdef;
605 *retpart = NULL;
606 *ret = NULL;
607
608 /* fetch the partition size */
609 if (*p == '-') {
610 /* assign all remaining space to this partition */
611 DEBUGF("'-': remaining size assigned\n");
612 size = SIZE_REMAINING;
613 p++;
614 } else {
615 size = memsize_parse(p, &p);
616 if (size < MIN_PART_SIZE) {
617 printf("partition size too small (%lx)\n", size);
618 return 1;
619 }
620 }
621
622 /* check for offset */
623 offset = OFFSET_NOT_SPECIFIED;
624 if (*p == '@') {
625 p++;
626 offset = memsize_parse(p, &p);
627 }
628
629 /* now look for the name */
630 if (*p == '(') {
631 name = ++p;
632 if ((p = strchr(name, ')')) == NULL) {
633 printf("no closing ) found in partition name\n");
634 return 1;
635 }
636 name_len = p - name + 1;
637 if ((name_len - 1) == 0) {
638 printf("empty partition name\n");
639 return 1;
640 }
641 p++;
642 } else {
643 /* 0x00000000@0x00000000 */
644 name_len = 22;
645 name = NULL;
646 }
647
648 /* test for options */
649 mask_flags = 0;
650 if (strncmp(p, "ro", 2) == 0) {
651 mask_flags |= MTD_WRITEABLE_CMD;
652 p += 2;
653 }
654
655 /* check for next partition definition */
656 if (*p == ',') {
657 if (size == SIZE_REMAINING) {
658 *ret = NULL;
659 printf("no partitions allowed after a fill-up partition\n");
660 return 1;
661 }
662 *ret = ++p;
663 } else if ((*p == ';') || (*p == '\0')) {
664 *ret = p;
665 } else {
666 printf("unexpected character '%c' at the end of partition\n", *p);
667 *ret = NULL;
668 return 1;
669 }
670
671 /* allocate memory */
672 part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
673 if (!part) {
674 printf("out of memory\n");
675 return 1;
676 }
677 memset(part, 0, sizeof(struct part_info) + name_len);
678 part->size = size;
679 part->offset = offset;
680 part->mask_flags = mask_flags;
681 part->name = (char *)(part + 1);
682
683 if (name) {
684 /* copy user provided name */
685 strncpy(part->name, name, name_len - 1);
686 part->auto_name = 0;
687 } else {
688 /* auto generated name in form of size@offset */
689 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
690 part->auto_name = 1;
691 }
692
693 part->name[name_len - 1] = '\0';
694 INIT_LIST_HEAD(&part->link);
695
696 DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
697 part->name, part->size,
698 part->offset, part->mask_flags);
699
700 *retpart = part;
701 return 0;
702}
703
704/**
705 * Check device number to be within valid range for given device type.
706 *
707 * @param dev device to validate
708 * @return 0 if device is valid, 1 otherwise
709 */
710int mtd_device_validate(u8 type, u8 num, u32 *size)
711{
Stefan Roese8e66ea72009-05-12 14:31:56 +0200712 struct mtd_info *mtd;
713 char mtd_dev[16];
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100714
Stefan Roese8e66ea72009-05-12 14:31:56 +0200715 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(type), num);
716 mtd = get_mtd_device_nm(mtd_dev);
717 if (IS_ERR(mtd)) {
718 printf("Device %s not found!\n", mtd_dev);
719 return 1;
720 }
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100721
Stefan Roese8e66ea72009-05-12 14:31:56 +0200722 *size = mtd->size;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100723
Stefan Roese8e66ea72009-05-12 14:31:56 +0200724 return 0;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100725}
726
727/**
728 * Delete all mtd devices from a supplied devices list, free memory allocated for
729 * each device and delete all device partitions.
730 *
731 * @return 0 on success, 1 otherwise
732 */
733static int device_delall(struct list_head *head)
734{
735 struct list_head *entry, *n;
736 struct mtd_device *dev_tmp;
737
738 /* clean devices list */
739 list_for_each_safe(entry, n, head) {
740 dev_tmp = list_entry(entry, struct mtd_device, link);
741 list_del(entry);
742 part_delall(&dev_tmp->parts);
743 free(dev_tmp);
744 }
745 INIT_LIST_HEAD(&devices);
746
747 return 0;
748}
749
750/**
751 * If provided device exists it's partitions are deleted, device is removed
752 * from device list and device memory is freed.
753 *
754 * @param dev device to be deleted
755 * @return 0 on success, 1 otherwise
756 */
757static int device_del(struct mtd_device *dev)
758{
759 part_delall(&dev->parts);
760 list_del(&dev->link);
761 free(dev);
762
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200763 if (dev == current_mtd_dev) {
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100764 /* we just deleted current device */
765 if (list_empty(&devices)) {
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200766 current_mtd_dev = NULL;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100767 } else {
768 /* reset first partition from first dev from the
769 * devices list as current */
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200770 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
771 current_mtd_partnum = 0;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100772 }
773 current_save();
774 return 0;
775 }
776
777 index_partitions();
778 return 0;
779}
780
781/**
782 * Search global device list and return pointer to the device of type and num
783 * specified.
784 *
785 * @param type device type
786 * @param num device number
787 * @return NULL if requested device does not exist
788 */
789static struct mtd_device* device_find(u8 type, u8 num)
790{
791 struct list_head *entry;
792 struct mtd_device *dev_tmp;
793
794 list_for_each(entry, &devices) {
795 dev_tmp = list_entry(entry, struct mtd_device, link);
796
797 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
798 return dev_tmp;
799 }
800
801 return NULL;
802}
803
804/**
805 * Add specified device to the global device list.
806 *
807 * @param dev device to be added
808 */
809static void device_add(struct mtd_device *dev)
810{
811 u8 current_save_needed = 0;
812
813 if (list_empty(&devices)) {
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200814 current_mtd_dev = dev;
815 current_mtd_partnum = 0;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100816 current_save_needed = 1;
817 }
818
819 list_add_tail(&dev->link, &devices);
820
821 if (current_save_needed > 0)
822 current_save();
823 else
824 index_partitions();
825}
826
827/**
828 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
829 * return pointer to the device structure.
830 *
831 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
832 * @param ret output pointer to next char after parse completes (output)
833 * @param retdev pointer to the allocated device (output)
834 * @return 0 on success, 1 otherwise
835 */
836static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
837{
838 struct mtd_device *dev;
839 struct part_info *part;
840 struct mtdids *id;
841 const char *mtd_id;
842 unsigned int mtd_id_len;
843 const char *p, *pend;
844 LIST_HEAD(tmp_list);
845 struct list_head *entry, *n;
846 u16 num_parts;
847 u32 offset;
848 int err = 1;
849
850 p = mtd_dev;
851 *retdev = NULL;
852 *ret = NULL;
853
854 DEBUGF("===device_parse===\n");
855
856 /* fetch <mtd-id> */
857 mtd_id = p;
858 if (!(p = strchr(mtd_id, ':'))) {
859 printf("no <mtd-id> identifier\n");
860 return 1;
861 }
862 mtd_id_len = p - mtd_id + 1;
863 p++;
864
865 /* verify if we have a valid device specified */
866 if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
867 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
868 return 1;
869 }
870
871 DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
872 id->type, MTD_DEV_TYPE(id->type),
873 id->num, id->mtd_id);
874 pend = strchr(p, ';');
875 DEBUGF("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
876
877
878 /* parse partitions */
879 num_parts = 0;
880
881 offset = 0;
882 if ((dev = device_find(id->type, id->num)) != NULL) {
883 /* if device already exists start at the end of the last partition */
884 part = list_entry(dev->parts.prev, struct part_info, link);
885 offset = part->offset + part->size;
886 }
887
888 while (p && (*p != '\0') && (*p != ';')) {
889 err = 1;
890 if ((part_parse(p, &p, &part) != 0) || (!part))
891 break;
892
893 /* calculate offset when not specified */
894 if (part->offset == OFFSET_NOT_SPECIFIED)
895 part->offset = offset;
896 else
897 offset = part->offset;
898
899 /* verify alignment and size */
900 if (part_validate(id, part) != 0)
901 break;
902
903 offset += part->size;
904
905 /* partition is ok, add it to the list */
906 list_add_tail(&part->link, &tmp_list);
907 num_parts++;
908 err = 0;
909 }
910 if (err == 1) {
911 part_delall(&tmp_list);
912 return 1;
913 }
914
915 if (num_parts == 0) {
916 printf("no partitions for device %s%d (%s)\n",
917 MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
918 return 1;
919 }
920
921 DEBUGF("\ntotal partitions: %d\n", num_parts);
922
923 /* check for next device presence */
924 if (p) {
925 if (*p == ';') {
926 *ret = ++p;
927 } else if (*p == '\0') {
928 *ret = p;
929 } else {
930 printf("unexpected character '%c' at the end of device\n", *p);
931 *ret = NULL;
932 return 1;
933 }
934 }
935
936 /* allocate memory for mtd_device structure */
937 if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
938 printf("out of memory\n");
939 return 1;
940 }
941 memset(dev, 0, sizeof(struct mtd_device));
942 dev->id = id;
943 dev->num_parts = 0; /* part_sort_add increments num_parts */
944 INIT_LIST_HEAD(&dev->parts);
945 INIT_LIST_HEAD(&dev->link);
946
947 /* move partitions from tmp_list to dev->parts */
948 list_for_each_safe(entry, n, &tmp_list) {
949 part = list_entry(entry, struct part_info, link);
950 list_del(entry);
951 if (part_sort_add(dev, part) != 0) {
952 device_del(dev);
953 return 1;
954 }
955 }
956
957 *retdev = dev;
958
959 DEBUGF("===\n\n");
960 return 0;
961}
962
963/**
964 * Initialize global device list.
965 *
966 * @return 0 on success, 1 otherwise
967 */
968static int mtd_devices_init(void)
969{
970 last_parts[0] = '\0';
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200971 current_mtd_dev = NULL;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100972 current_save();
973
974 return device_delall(&devices);
975}
976
977/*
978 * Search global mtdids list and find id of requested type and number.
979 *
980 * @return pointer to the id if it exists, NULL otherwise
981 */
982static struct mtdids* id_find(u8 type, u8 num)
983{
984 struct list_head *entry;
985 struct mtdids *id;
986
987 list_for_each(entry, &mtdids) {
988 id = list_entry(entry, struct mtdids, link);
989
990 if ((id->type == type) && (id->num == num))
991 return id;
992 }
993
994 return NULL;
995}
996
997/**
998 * Search global mtdids list and find id of a requested mtd_id.
999 *
1000 * Note: first argument is not null terminated.
1001 *
1002 * @param mtd_id string containing requested mtd_id
1003 * @param mtd_id_len length of supplied mtd_id
1004 * @return pointer to the id if it exists, NULL otherwise
1005 */
1006static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
1007{
1008 struct list_head *entry;
1009 struct mtdids *id;
1010
1011 DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1012 mtd_id_len, mtd_id, mtd_id_len);
1013
1014 list_for_each(entry, &mtdids) {
1015 id = list_entry(entry, struct mtdids, link);
1016
1017 DEBUGF("entry: '%s' (len = %d)\n",
1018 id->mtd_id, strlen(id->mtd_id));
1019
1020 if (mtd_id_len != strlen(id->mtd_id))
1021 continue;
1022 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1023 return id;
1024 }
1025
1026 return NULL;
1027}
1028
1029/**
1030 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
1031 * return device type and number.
1032 *
1033 * @param id string describing device id
1034 * @param ret_id output pointer to next char after parse completes (output)
1035 * @param dev_type parsed device type (output)
1036 * @param dev_num parsed device number (output)
1037 * @return 0 on success, 1 otherwise
1038 */
1039int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
1040{
1041 const char *p = id;
1042
1043 *dev_type = 0;
1044 if (strncmp(p, "nand", 4) == 0) {
1045 *dev_type = MTD_DEV_TYPE_NAND;
1046 p += 4;
1047 } else if (strncmp(p, "nor", 3) == 0) {
1048 *dev_type = MTD_DEV_TYPE_NOR;
1049 p += 3;
1050 } else if (strncmp(p, "onenand", 7) == 0) {
1051 *dev_type = MTD_DEV_TYPE_ONENAND;
1052 p += 7;
1053 } else {
1054 printf("incorrect device type in %s\n", id);
1055 return 1;
1056 }
1057
1058 if (!isdigit(*p)) {
1059 printf("incorrect device number in %s\n", id);
1060 return 1;
1061 }
1062
1063 *dev_num = simple_strtoul(p, (char **)&p, 0);
1064 if (ret_id)
1065 *ret_id = p;
1066 return 0;
1067}
1068
1069/**
1070 * Process all devices and generate corresponding mtdparts string describing
1071 * all partitions on all devices.
1072 *
1073 * @param buf output buffer holding generated mtdparts string (output)
1074 * @param buflen buffer size
1075 * @return 0 on success, 1 otherwise
1076 */
1077static int generate_mtdparts(char *buf, u32 buflen)
1078{
1079 struct list_head *pentry, *dentry;
1080 struct mtd_device *dev;
1081 struct part_info *part, *prev_part;
1082 char *p = buf;
1083 char tmpbuf[32];
1084 u32 size, offset, len, part_cnt;
1085 u32 maxlen = buflen - 1;
1086
1087 DEBUGF("--- generate_mtdparts ---\n");
1088
1089 if (list_empty(&devices)) {
1090 buf[0] = '\0';
1091 return 0;
1092 }
1093
1094 sprintf(p, "mtdparts=");
1095 p += 9;
1096
1097 list_for_each(dentry, &devices) {
1098 dev = list_entry(dentry, struct mtd_device, link);
1099
1100 /* copy mtd_id */
1101 len = strlen(dev->id->mtd_id) + 1;
1102 if (len > maxlen)
1103 goto cleanup;
1104 memcpy(p, dev->id->mtd_id, len - 1);
1105 p += len - 1;
1106 *(p++) = ':';
1107 maxlen -= len;
1108
1109 /* format partitions */
1110 prev_part = NULL;
1111 part_cnt = 0;
1112 list_for_each(pentry, &dev->parts) {
1113 part = list_entry(pentry, struct part_info, link);
1114 size = part->size;
1115 offset = part->offset;
1116 part_cnt++;
1117
1118 /* partition size */
1119 memsize_format(tmpbuf, size);
1120 len = strlen(tmpbuf);
1121 if (len > maxlen)
1122 goto cleanup;
1123 memcpy(p, tmpbuf, len);
1124 p += len;
1125 maxlen -= len;
1126
1127
1128 /* add offset only when there is a gap between
1129 * partitions */
1130 if ((!prev_part && (offset != 0)) ||
1131 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1132
1133 memsize_format(tmpbuf, offset);
1134 len = strlen(tmpbuf) + 1;
1135 if (len > maxlen)
1136 goto cleanup;
1137 *(p++) = '@';
1138 memcpy(p, tmpbuf, len - 1);
1139 p += len - 1;
1140 maxlen -= len;
1141 }
1142
1143 /* copy name only if user supplied */
1144 if(!part->auto_name) {
1145 len = strlen(part->name) + 2;
1146 if (len > maxlen)
1147 goto cleanup;
1148
1149 *(p++) = '(';
1150 memcpy(p, part->name, len - 2);
1151 p += len - 2;
1152 *(p++) = ')';
1153 maxlen -= len;
1154 }
1155
1156 /* ro mask flag */
1157 if (part->mask_flags && MTD_WRITEABLE_CMD) {
1158 len = 2;
1159 if (len > maxlen)
1160 goto cleanup;
1161 *(p++) = 'r';
1162 *(p++) = 'o';
1163 maxlen -= 2;
1164 }
1165
1166 /* print ',' separator if there are other partitions
1167 * following */
1168 if (dev->num_parts > part_cnt) {
1169 if (1 > maxlen)
1170 goto cleanup;
1171 *(p++) = ',';
1172 maxlen--;
1173 }
1174 prev_part = part;
1175 }
1176 /* print ';' separator if there are other devices following */
1177 if (dentry->next != &devices) {
1178 if (1 > maxlen)
1179 goto cleanup;
1180 *(p++) = ';';
1181 maxlen--;
1182 }
1183 }
1184
1185 /* we still have at least one char left, as we decremented maxlen at
1186 * the begining */
1187 *p = '\0';
1188
1189 return 0;
1190
1191cleanup:
1192 last_parts[0] = '\0';
1193 return 1;
1194}
1195
1196/**
1197 * Call generate_mtdparts to process all devices and generate corresponding
1198 * mtdparts string, save it in mtdparts environment variable.
1199 *
1200 * @param buf output buffer holding generated mtdparts string (output)
1201 * @param buflen buffer size
1202 * @return 0 on success, 1 otherwise
1203 */
1204static int generate_mtdparts_save(char *buf, u32 buflen)
1205{
1206 int ret;
1207
1208 ret = generate_mtdparts(buf, buflen);
1209
1210 if ((buf[0] != '\0') && (ret == 0))
1211 setenv("mtdparts", buf);
1212 else
1213 setenv("mtdparts", NULL);
1214
1215 return ret;
1216}
1217
1218/**
1219 * Format and print out a partition list for each device from global device
1220 * list.
1221 */
1222static void list_partitions(void)
1223{
1224 struct list_head *dentry, *pentry;
1225 struct part_info *part;
1226 struct mtd_device *dev;
1227 int part_num;
1228
1229 DEBUGF("\n---list_partitions---\n");
1230 list_for_each(dentry, &devices) {
1231 dev = list_entry(dentry, struct mtd_device, link);
1232 printf("\ndevice %s%d <%s>, # parts = %d\n",
1233 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1234 dev->id->mtd_id, dev->num_parts);
David Brownelld8a6b812009-04-16 19:55:48 -07001235 printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001236
1237 /* list partitions for given device */
1238 part_num = 0;
1239 list_for_each(pentry, &dev->parts) {
1240 part = list_entry(pentry, struct part_info, link);
1241 printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
1242 part_num, part->name, part->size,
1243 part->offset, part->mask_flags);
1244
1245 part_num++;
1246 }
1247 }
1248 if (list_empty(&devices))
1249 printf("no partitions defined\n");
1250
Stefan Roese4fa1f2d2009-05-16 12:04:22 +02001251 /* current_mtd_dev is not NULL only when we have non empty device list */
1252 if (current_mtd_dev) {
1253 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001254 if (part) {
1255 printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
Stefan Roese4fa1f2d2009-05-16 12:04:22 +02001256 MTD_DEV_TYPE(current_mtd_dev->id->type),
1257 current_mtd_dev->id->num, current_mtd_partnum,
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001258 part->name, part->size, part->offset);
1259 } else {
1260 printf("could not get current partition info\n\n");
1261 }
1262 }
1263
1264 printf("\ndefaults:\n");
Wolfgang Denk21c0a542009-05-17 16:01:54 +02001265 printf("mtdids : %s\n",
1266 mtdids_default ? mtdids_default : "none");
1267 printf("mtdparts: %s\n",
1268 mtdparts_default ? mtdparts_default : "none");
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001269}
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 Roese4fa1f2d2009-05-16 12:04:22 +02001632 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
1633 current_mtd_partnum = 0;
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001634 current_save();
1635
Stefan Roese4fa1f2d2009-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 Roeseb1423dd2009-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 Roese4fa1f2d2009-05-16 12:04:22 +02001658 current_mtd_dev = cdev;
1659 current_mtd_partnum = pnum;
Stefan Roeseb1423dd2009-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 Roese4fa1f2d2009-05-16 12:04:22 +02001743 current_mtd_dev = dev;
1744 current_mtd_partnum = pnum;
Stefan Roeseb1423dd2009-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"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001866 " - change active partition (e.g. part-id = nand0,1)"
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001867);
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"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001901 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)"
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001902);
1903/***************************************************/