blob: fda0c955f486564dc2b276e265dd25dfba482eb7 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Wolfgang Denk700a0c62005-08-08 01:03:24 +02004 *
wdenkdd875c72004-01-03 21:24:46 +00005 * (C) Copyright 2002
6 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
Wolfgang Denk700a0c62005-08-08 01:03:24 +02007 *
wdenkdd875c72004-01-03 21:24:46 +00008 * (C) Copyright 2003
9 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
wdenkc6097192002-11-03 00:24:07 +000010 *
Wolfgang Denk700a0c62005-08-08 01:03:24 +020011 * (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 *
wdenkc6097192002-11-03 00:24:07 +000021 * 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/*
Wolfgang Denk700a0c62005-08-08 01:03:24 +020041 * 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 *
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +020048 *
Wolfgang Denk700a0c62005-08-08 01:03:24 +020049 * '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'<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
87/*
88 * JFFS2/CRAMFS support
wdenkc6097192002-11-03 00:24:07 +000089 */
90#include <common.h>
91#include <command.h>
Wolfgang Denk700a0c62005-08-08 01:03:24 +020092#include <malloc.h>
93#include <jffs2/jffs2.h>
94#include <linux/mtd/nand.h>
95#include <linux/list.h>
96#include <linux/ctype.h>
wdenkc6097192002-11-03 00:24:07 +000097
98#if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
wdenkdd875c72004-01-03 21:24:46 +000099
100#include <cramfs/cramfs_fs.h>
101
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200102/* enable/disable debugging messages */
103#define DEBUG
104#undef DEBUG
105
106#ifdef DEBUG
107# define DEBUGF(fmt, args...) printf(fmt ,##args)
108#else
109# define DEBUGF(fmt, args...)
110#endif
111
112/* special size referring to all the remaining space in a partition */
113#define SIZE_REMAINING 0xFFFFFFFF
114
115/* special offset value, it is used when not provided by user
116 *
117 * this value is used temporarily during parsing, later such offests
118 * are recalculated */
119#define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
120
121/* minimum partition size */
122#define MIN_PART_SIZE 4096
123
124/* this flag needs to be set in part_info struct mask_flags
125 * field for read-only partitions */
126#define MTD_WRITEABLE 1
127
128#ifdef CONFIG_JFFS2_CMDLINE
129/* default values for mtdids and mtdparts variables */
130#if defined(MTDIDS_DEFAULT)
131static const char *const mtdids_default = MTDIDS_DEFAULT;
132#else
133#warning "MTDIDS_DEFAULT not defined!"
134static const char *const mtdids_default = NULL;
135#endif
136
137#if defined(MTDPARTS_DEFAULT)
138static const char *const mtdparts_default = MTDPARTS_DEFAULT;
139#else
140#warning "MTDPARTS_DEFAULT not defined!"
141static const char *const mtdparts_default = NULL;
142#endif
143
144/* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
145#define MTDIDS_MAXLEN 128
146#define MTDPARTS_MAXLEN 512
147#define PARTITION_MAXLEN 16
148static char last_ids[MTDIDS_MAXLEN];
149static char last_parts[MTDPARTS_MAXLEN];
150static char last_partition[PARTITION_MAXLEN];
151
152/* low level jffs2 cache cleaning routine */
153extern void jffs2_free_cache(struct part_info *part);
154
155/* mtdids mapping list, filled by parse_ids() */
156struct list_head mtdids;
157
158/* device/partition list, parse_cmdline() parses into here */
159struct list_head devices;
160#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
161
162/* current active device and partition number */
163static struct mtd_device *current_dev = NULL;
164static u8 current_partnum = 0;
165
wdenk180d3f72004-01-04 16:28:35 +0000166extern int cramfs_check (struct part_info *info);
167extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
168extern int cramfs_ls (struct part_info *info, char *filename);
169extern int cramfs_info (struct part_info *info);
170
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200171static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num);
wdenkc6097192002-11-03 00:24:07 +0000172
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200173/* command line only routines */
174#ifdef CONFIG_JFFS2_CMDLINE
wdenkc6097192002-11-03 00:24:07 +0000175
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200176static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
177static int device_del(struct mtd_device *dev);
wdenk6705d812004-08-02 23:22:59 +0000178
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200179/**
180 * Parses a string into a number. The number stored at ptr is
181 * potentially suffixed with K (for kilobytes, or 1024 bytes),
182 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
183 * 1073741824). If the number is suffixed with K, M, or G, then
184 * the return value is the number multiplied by one kilobyte, one
185 * megabyte, or one gigabyte, respectively.
186 *
187 * @param ptr where parse begins
188 * @param retptr output pointer to next char after parse completes (output)
189 * @return resulting unsigned int
190 */
191static unsigned long memsize_parse (const char *const ptr, const char **retptr)
wdenkc6097192002-11-03 00:24:07 +0000192{
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200193 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
194
195 switch (**retptr) {
196 case 'G':
197 case 'g':
198 ret <<= 10;
199 case 'M':
200 case 'm':
201 ret <<= 10;
202 case 'K':
203 case 'k':
204 ret <<= 10;
205 (*retptr)++;
206 default:
207 break;
208 }
209
210 return ret;
211}
212
213/**
214 * Format string describing supplied size. This routine does the opposite job
215 * to memsize_parse(). Size in bytes is converted to string and if possible
216 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
217 *
218 * Note, that this routine does not check for buffer overflow, it's the caller
219 * who must assure enough space.
220 *
221 * @param buf output buffer
222 * @param size size to be converted to string
223 */
224static void memsize_format(char *buf, u32 size)
225{
226#define SIZE_GB ((u32)1024*1024*1024)
227#define SIZE_MB ((u32)1024*1024)
228#define SIZE_KB ((u32)1024)
229
230 if ((size % SIZE_GB) == 0)
231 sprintf(buf, "%lug", size/SIZE_GB);
232 else if ((size % SIZE_MB) == 0)
233 sprintf(buf, "%lum", size/SIZE_MB);
234 else if (size % SIZE_KB == 0)
235 sprintf(buf, "%luk", size/SIZE_KB);
236 else
237 sprintf(buf, "%lu", size);
238}
239
240/**
Marian Balakowicz7d454772005-09-11 15:27:12 +0200241 * This routine does global indexing of all partitions. Resulting index for
242 * current partition is saved in 'mtddevnum'. Current partition name in
243 * 'mtddevname'.
244 */
245static void index_partitions(void)
246{
247 char buf[16];
248 u16 mtddevnum;
249 struct part_info *part;
250 struct list_head *dentry;
251 struct mtd_device *dev;
252
253 DEBUGF("--- index partitions ---\n");
254
255 if (current_dev) {
256 mtddevnum = 0;
257 list_for_each(dentry, &devices) {
258 dev = list_entry(dentry, struct mtd_device, link);
259 if (dev == current_dev) {
260 mtddevnum += current_partnum;
261 sprintf(buf, "%d", mtddevnum);
262 setenv("mtddevnum", buf);
263 break;
264 }
265 mtddevnum += dev->num_parts;
266 }
267
268 part = jffs2_part_info(current_dev, current_partnum);
269 setenv("mtddevname", part->name);
270
271 DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
272 } else {
273 setenv("mtddevnum", NULL);
274 setenv("mtddevname", NULL);
275
276 DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
277 }
278}
279
280/**
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200281 * Save current device and partition in environment variable 'partition'.
282 */
283static void current_save(void)
284{
285 char buf[16];
286
287 DEBUGF("--- current_save ---\n");
288
289 if (current_dev) {
290 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_dev->id->type),
291 current_dev->id->num, current_partnum);
292
293 setenv("partition", buf);
294 strncpy(last_partition, buf, 16);
295
296 DEBUGF("=> partition %s\n", buf);
297 } else {
298 setenv("partition", NULL);
299 last_partition[0] = '\0';
300
301 DEBUGF("=> partition NULL\n");
302 }
Marian Balakowicz7d454772005-09-11 15:27:12 +0200303 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200304}
305
306/**
307 * Performs sanity check for supplied NOR flash partition. Table of existing
308 * NOR flash devices is searched and partition device is located. Alignment
309 * with the granularity of NOR flash sectors is verified.
310 *
311 * @param id of the parent device
312 * @param part partition to validate
313 * @return 0 if partition is valid, 1 otherwise
314 */
315static int part_validate_nor(struct mtdids *id, struct part_info *part)
316{
317#if (CONFIG_COMMANDS & CFG_CMD_FLASH)
318 /* info for FLASH chips */
319 extern flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
320 flash_info_t *flash;
321 int offset_aligned;
322 u32 end_offset;
wdenkc6097192002-11-03 00:24:07 +0000323 int i;
324
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200325 flash = &flash_info[id->num];
wdenkc6097192002-11-03 00:24:07 +0000326
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200327 offset_aligned = 0;
328 for (i = 0; i < flash->sector_count; i++) {
329 if ((flash->start[i] - flash->start[0]) == part->offset) {
330 offset_aligned = 1;
331 break;
332 }
333 }
334 if (offset_aligned == 0) {
335 printf("%s%d: partition (%s) start offset alignment incorrect\n",
336 MTD_DEV_TYPE(id->type), id->num, part->name);
337 return 1;
338 }
wdenkc6097192002-11-03 00:24:07 +0000339
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200340 end_offset = part->offset + part->size;
341 for (i = 0; i < flash->sector_count; i++) {
342 if ((flash->start[i] - flash->start[0]) == end_offset)
343 return 0;
344 }
wdenkc6097192002-11-03 00:24:07 +0000345
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200346 if (flash->size == end_offset)
347 return 0;
348
349 printf("%s%d: partition (%s) size alignment incorrect\n",
350 MTD_DEV_TYPE(id->type), id->num, part->name);
351#endif
352 return 1;
353}
354
355/**
356 * Performs sanity check for supplied NAND flash partition. Table of existing
357 * NAND flash devices is searched and partition device is located. Alignment
358 * with the granularity of nand erasesize is verified.
359 *
360 * @param id of the parent device
361 * @param part partition to validate
362 * @return 0 if partition is valid, 1 otherwise
363 */
364static int part_validate_nand(struct mtdids *id, struct part_info *part)
365{
366#if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
367 /* info for NAND chips */
368 extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
369 struct nand_chip *nand;
370
371 nand = &nand_dev_desc[id->num];
372
373 if ((unsigned long)(part->offset) % nand->erasesize) {
374 printf("%s%d: partition (%s) start offset alignment incorrect\n",
375 MTD_DEV_TYPE(id->type), id->num, part->name);
376 return 1;
377 }
378
379 if (part->size % nand->erasesize) {
380 printf("%s%d: partition (%s) size alignment incorrect\n",
381 MTD_DEV_TYPE(id->type), id->num, part->name);
382 return 1;
383 }
384
385 return 0;
wdenkc6097192002-11-03 00:24:07 +0000386#else
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200387 return 1;
wdenkc6097192002-11-03 00:24:07 +0000388#endif
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200389}
wdenkc6097192002-11-03 00:24:07 +0000390
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200391/**
392 * Performs sanity check for supplied partition. Offset and size are verified
393 * to be within valid range. Partition type is checked and either
394 * parts_validate_nor() or parts_validate_nand() is called with the argument
395 * of part.
396 *
397 * @param id of the parent device
398 * @param part partition to validate
399 * @return 0 if partition is valid, 1 otherwise
400 */
401static int part_validate(struct mtdids *id, struct part_info *part)
402{
403 if (part->size == SIZE_REMAINING)
404 part->size = id->size - part->offset;
wdenkc6097192002-11-03 00:24:07 +0000405
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200406 if (part->offset > id->size) {
407 printf("%s: offset %08lx beyond flash size %08lx\n",
408 id->mtd_id, part->offset, id->size);
409 return 1;
wdenkc6097192002-11-03 00:24:07 +0000410 }
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200411
412 if ((part->offset + part->size) <= part->offset) {
413 printf("%s%d: partition (%s) size too big\n",
414 MTD_DEV_TYPE(id->type), id->num, part->name);
415 return 1;
416 }
417
418 if (part->offset + part->size > id->size) {
419 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
420 return 1;
421 }
422
423 if (id->type == MTD_DEV_TYPE_NAND)
424 return part_validate_nand(id, part);
425 else if (id->type == MTD_DEV_TYPE_NOR)
426 return part_validate_nor(id, part);
427 else
428 DEBUGF("part_validate: invalid dev type\n");
429
430 return 1;
431}
432
433/**
434 * Delete selected partition from the partion list of the specified device.
435 *
436 * @param dev device to delete partition from
437 * @param part partition to delete
438 * @return 0 on success, 1 otherwise
439 */
440static int part_del(struct mtd_device *dev, struct part_info *part)
441{
Marian Balakowicz7d454772005-09-11 15:27:12 +0200442 u8 current_save_needed = 0;
443
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200444 /* if there is only one partition, remove whole device */
445 if (dev->num_parts == 1)
446 return device_del(dev);
447
448 /* otherwise just delete this partition */
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200449
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200450 if (dev == current_dev) {
451 /* we are modyfing partitions for the current device,
452 * update current */
453 struct part_info *curr_pi;
454 curr_pi = jffs2_part_info(current_dev, current_partnum);
455
456 if (curr_pi) {
457 if (curr_pi == part) {
458 printf("current partition deleted, resetting current to 0\n");
459 current_partnum = 0;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200460 } else if (part->offset <= curr_pi->offset) {
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200461 current_partnum--;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200462 }
Marian Balakowicz7d454772005-09-11 15:27:12 +0200463 current_save_needed = 1;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200464 }
465 }
466
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200467 jffs2_free_cache(part);
468 list_del(&part->link);
469 free(part);
470 dev->num_parts--;
471
Marian Balakowicz7d454772005-09-11 15:27:12 +0200472 if (current_save_needed > 0)
473 current_save();
474 else
475 index_partitions();
476
wdenkc6097192002-11-03 00:24:07 +0000477 return 0;
478}
wdenk998eaae2004-04-18 19:43:36 +0000479
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200480/**
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)
wdenk998eaae2004-04-18 19:43:36 +0000486{
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200487 struct list_head *entry, *n;
488 struct part_info *part_tmp;
wdenk998eaae2004-04-18 19:43:36 +0000489
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200490 /* 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);
wdenk998eaae2004-04-18 19:43:36 +0000493
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200494 jffs2_free_cache(part_tmp);
495 list_del(entry);
496 free(part_tmp);
wdenk998eaae2004-04-18 19:43:36 +0000497 }
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200498}
499
500/**
501 * Add new partition to the supplied partition list. Make sure partitions are
502 * sorted by offset in ascending order.
503 *
504 * @param head list this partition is to be added to
505 * @param new partition to be added
506 */
507static int part_sort_add(struct mtd_device *dev, struct part_info *part)
508{
509 struct list_head *entry;
510 struct part_info *new_pi, *curr_pi;
511
512 /* link partition to parrent dev */
513 part->dev = dev;
514
515 if (list_empty(&dev->parts)) {
516 DEBUGF("part_sort_add: list empty\n");
517 list_add(&part->link, &dev->parts);
Marian Balakowicz7d454772005-09-11 15:27:12 +0200518 dev->num_parts++;
519 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200520 return 0;
521 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200522
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200523 new_pi = list_entry(&part->link, struct part_info, link);
524
525 /* get current partition info if we are updating current device */
526 curr_pi = NULL;
527 if (dev == current_dev)
528 curr_pi = jffs2_part_info(current_dev, current_partnum);
529
530 list_for_each(entry, &dev->parts) {
531 struct part_info *pi;
532
533 pi = list_entry(entry, struct part_info, link);
534
535 /* be compliant with kernel cmdline, allow only one partition at offset zero */
536 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
537 printf("cannot add second partition at offset 0\n");
538 return 1;
539 }
540
541 if (new_pi->offset <= pi->offset) {
542 list_add_tail(&part->link, entry);
Marian Balakowicz7d454772005-09-11 15:27:12 +0200543 dev->num_parts++;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200544
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200545 if (curr_pi && (pi->offset <= curr_pi->offset)) {
546 /* we are modyfing partitions for the current
547 * device, update current */
548 current_partnum++;
549 current_save();
Marian Balakowicz7d454772005-09-11 15:27:12 +0200550 } else {
551 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200552 }
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200553 return 0;
554 }
555 }
Marian Balakowicz7d454772005-09-11 15:27:12 +0200556
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200557 list_add_tail(&part->link, &dev->parts);
Marian Balakowicz7d454772005-09-11 15:27:12 +0200558 dev->num_parts++;
559 index_partitions();
wdenk998eaae2004-04-18 19:43:36 +0000560 return 0;
561}
562
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200563/**
564 * Add provided partition to the partition list of a given device.
565 *
566 * @param dev device to which partition is added
567 * @param part partition to be added
568 * @return 0 on success, 1 otherwise
569 */
570static int part_add(struct mtd_device *dev, struct part_info *part)
wdenkc6097192002-11-03 00:24:07 +0000571{
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200572 /* verify alignment and size */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200573 if (part_validate(dev->id, part) != 0)
574 return 1;
575
576 /* partition is ok, add it to the list */
577 if (part_sort_add(dev, part) != 0)
578 return 1;
579
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200580 return 0;
581}
582
583/**
584 * Parse one partition definition, allocate memory and return pointer to this
585 * location in retpart.
586 *
587 * @param partdef pointer to the partition definition string i.e. <part-def>
588 * @param ret output pointer to next char after parse completes (output)
589 * @param retpart pointer to the allocated partition (output)
590 * @return 0 on success, 1 otherwise
591 */
592static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
593{
594 struct part_info *part;
595 unsigned long size;
596 unsigned long offset;
597 const char *name;
598 int name_len;
599 unsigned int mask_flags;
600 const char *p;
601
602 p = partdef;
603 *retpart = NULL;
604 *ret = NULL;
605
606 /* fetch the partition size */
607 if (*p == '-') {
608 /* assign all remaining space to this partition */
609 DEBUGF("'-': remaining size assigned\n");
610 size = SIZE_REMAINING;
611 p++;
612 } else {
613 size = memsize_parse(p, &p);
614 if (size < MIN_PART_SIZE) {
615 printf("partition size too small (%lx)\n", size);
616 return 1;
617 }
618 }
619
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200620 /* check for offset */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200621 offset = OFFSET_NOT_SPECIFIED;
622 if (*p == '@') {
623 p++;
624 offset = memsize_parse(p, &p);
625 }
626
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200627 /* now look for the name */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200628 if (*p == '(') {
629 name = ++p;
630 if ((p = strchr(name, ')')) == NULL) {
631 printf("no closing ) found in partition name\n");
632 return 1;
633 }
634 name_len = p - name + 1;
635 if ((name_len - 1) == 0) {
636 printf("empty partition name\n");
637 return 1;
638 }
639 p++;
640 } else {
641 /* 0x00000000@0x00000000 */
642 name_len = 22;
643 name = NULL;
644 }
645
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200646 /* test for options */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200647 mask_flags = 0;
648 if (strncmp(p, "ro", 2) == 0) {
649 mask_flags |= MTD_WRITEABLE;
650 p += 2;
651 }
652
653 /* check for next partition definition */
654 if (*p == ',') {
655 if (size == SIZE_REMAINING) {
656 *ret = NULL;
657 printf("no partitions allowed after a fill-up partition\n");
658 return 1;
659 }
660 *ret = ++p;
661 } else if ((*p == ';') || (*p == '\0')) {
662 *ret = p;
663 } else {
664 printf("unexpected character '%c' at the end of partition\n", *p);
665 *ret = NULL;
666 return 1;
667 }
668
669 /* allocate memory */
670 part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
671 if (!part) {
672 printf("out of memory\n");
673 return 1;
674 }
675 memset(part, 0, sizeof(struct part_info) + name_len);
676 part->size = size;
677 part->offset = offset;
678 part->mask_flags = mask_flags;
679 part->name = (char *)(part + 1);
680
681 if (name) {
682 /* copy user provided name */
683 strncpy(part->name, name, name_len - 1);
684 part->auto_name = 0;
685 } else {
686 /* auto generated name in form of size@offset */
687 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
688 part->auto_name = 1;
689 }
690
691 part->name[name_len - 1] = '\0';
692 INIT_LIST_HEAD(&part->link);
693
694 DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
695 part->name, part->size,
696 part->offset, part->mask_flags);
697
698 *retpart = part;
699 return 0;
700}
701#endif/* #ifdef CONFIG_JFFS2_CMDLINE */
702
703/**
704 * Check device number to be within valid range for given device type.
705 *
706 * @param dev device to validate
707 * @return 0 if device is valid, 1 otherwise
708 */
709static int device_validate(u8 type, u8 num, u32 *size)
710{
711 if (type == MTD_DEV_TYPE_NOR) {
712#if (CONFIG_COMMANDS & CFG_CMD_FLASH)
713 if (num < CFG_MAX_FLASH_BANKS) {
714 extern flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
715 *size = flash_info[num].size;
716 return 0;
717 }
718
719 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
720 MTD_DEV_TYPE(type), num, CFG_MAX_FLASH_BANKS - 1);
721#else
722 printf("support for FLASH devices not present\n");
723#endif
724 } else if (type == MTD_DEV_TYPE_NAND) {
725#if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
726 if (num < CFG_MAX_NAND_DEVICE) {
727 extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
728 *size = nand_dev_desc[num].totlen;
729 return 0;
730 }
731
732 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
733 MTD_DEV_TYPE(type), num, CFG_MAX_NAND_DEVICE - 1);
734#else
735 printf("support for NAND devices not present\n");
736#endif
737 }
738
739 return 1;
740}
741
742#ifdef CONFIG_JFFS2_CMDLINE
743/**
744 * Delete all mtd devices from a supplied devices list, free memory allocated for
745 * each device and delete all device partitions.
746 *
747 * @return 0 on success, 1 otherwise
748 */
749static int device_delall(struct list_head *head)
750{
751 struct list_head *entry, *n;
752 struct mtd_device *dev_tmp;
753
754 /* clean devices list */
755 list_for_each_safe(entry, n, head) {
756 dev_tmp = list_entry(entry, struct mtd_device, link);
757 list_del(entry);
758 part_delall(&dev_tmp->parts);
759 free(dev_tmp);
760 }
761 INIT_LIST_HEAD(&devices);
762
763 return 0;
764}
765
766/**
767 * If provided device exists it's partitions are deleted, device is removed
768 * from device list and device memory is freed.
769 *
770 * @param dev device to be deleted
771 * @return 0 on success, 1 otherwise
772 */
773static int device_del(struct mtd_device *dev)
774{
775 part_delall(&dev->parts);
776 list_del(&dev->link);
777 free(dev);
778
779 if (dev == current_dev) {
780 /* we just deleted current device */
781 if (list_empty(&devices)) {
782 current_dev = NULL;
783 } else {
784 /* reset first partition from first dev from the
785 * devices list as current */
786 current_dev = list_entry(devices.next, struct mtd_device, link);
787 current_partnum = 0;
788 }
789 current_save();
Marian Balakowicz7d454772005-09-11 15:27:12 +0200790 return 0;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200791 }
792
Marian Balakowicz7d454772005-09-11 15:27:12 +0200793 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200794 return 0;
795}
796
797/**
798 * Search global device list and return pointer to the device of type and num
799 * specified.
800 *
801 * @param type device type
802 * @param num device number
803 * @return NULL if requested device does not exist
804 */
805static struct mtd_device* device_find(u8 type, u8 num)
806{
807 struct list_head *entry;
808 struct mtd_device *dev_tmp;
809
810 list_for_each(entry, &devices) {
811 dev_tmp = list_entry(entry, struct mtd_device, link);
812
813 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
814 return dev_tmp;
815 }
816
817 return NULL;
818}
819
820/**
821 * Add specified device to the global device list.
822 *
823 * @param dev device to be added
824 */
825static void device_add(struct mtd_device *dev)
826{
Marian Balakowicz7d454772005-09-11 15:27:12 +0200827 u8 current_save_needed = 0;
828
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200829 if (list_empty(&devices)) {
830 current_dev = dev;
831 current_partnum = 0;
Marian Balakowicz7d454772005-09-11 15:27:12 +0200832 current_save_needed = 1;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200833 }
834
835 list_add_tail(&dev->link, &devices);
Marian Balakowicz7d454772005-09-11 15:27:12 +0200836
837 if (current_save_needed > 0)
838 current_save();
839 else
840 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200841}
842
843/**
844 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
845 * return pointer to the device structure.
846 *
847 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
848 * @param ret output pointer to next char after parse completes (output)
849 * @param retdev pointer to the allocated device (output)
850 * @return 0 on success, 1 otherwise
851 */
852static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
853{
854 struct mtd_device *dev;
855 struct part_info *part;
856 struct mtdids *id;
857 const char *mtd_id;
858 unsigned int mtd_id_len;
859 const char *p, *pend;
860 LIST_HEAD(tmp_list);
861 struct list_head *entry, *n;
862 u16 num_parts;
863 u32 offset;
864 int err = 1;
865
866 p = mtd_dev;
867 *retdev = NULL;
868 *ret = NULL;
869
870 DEBUGF("===device_parse===\n");
871
872 /* fetch <mtd-id> */
873 mtd_id = p;
874 if (!(p = strchr(mtd_id, ':'))) {
875 printf("no <mtd-id> identifier\n");
876 return 1;
877 }
878 mtd_id_len = p - mtd_id + 1;
879 p++;
880
881 /* verify if we have a valid device specified */
882 if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
883 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
884 return 1;
885 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200886
887 DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200888 id->type, MTD_DEV_TYPE(id->type),
889 id->num, id->mtd_id);
890 pend = strchr(p, ';');
891 DEBUGF("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
892
893
894 /* parse partitions */
895 num_parts = 0;
896
897 offset = 0;
898 if ((dev = device_find(id->type, id->num)) != NULL) {
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200899 /* if device already exists start at the end of the last partition */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200900 part = list_entry(dev->parts.prev, struct part_info, link);
901 offset = part->offset + part->size;
902 }
903
904 while (p && (*p != '\0') && (*p != ';')) {
905 err = 1;
906 if ((part_parse(p, &p, &part) != 0) || (!part))
907 break;
908
909 /* calculate offset when not specified */
910 if (part->offset == OFFSET_NOT_SPECIFIED)
911 part->offset = offset;
912 else
913 offset = part->offset;
914
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200915 /* verify alignment and size */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200916 if (part_validate(id, part) != 0)
917 break;
918
919 offset += part->size;
920
921 /* partition is ok, add it to the list */
922 list_add_tail(&part->link, &tmp_list);
923 num_parts++;
924 err = 0;
925 }
926 if (err == 1) {
927 part_delall(&tmp_list);
928 return 1;
929 }
930
931 if (num_parts == 0) {
932 printf("no partitions for device %s%d (%s)\n",
933 MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
934 return 1;
935 }
936
937 DEBUGF("\ntotal partitions: %d\n", num_parts);
938
939 /* check for next device presence */
940 if (p) {
941 if (*p == ';') {
942 *ret = ++p;
943 } else if (*p == '\0') {
944 *ret = p;
945 } else {
946 printf("unexpected character '%c' at the end of device\n", *p);
947 *ret = NULL;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200948 return 1;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200949 }
950 }
951
952 /* allocate memory for mtd_device structure */
953 if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
954 printf("out of memory\n");
955 return 1;
956 }
957 memset(dev, 0, sizeof(struct mtd_device));
958 dev->id = id;
Marian Balakowicz7d454772005-09-11 15:27:12 +0200959 dev->num_parts = 0; /* part_sort_add increments num_parts */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200960 INIT_LIST_HEAD(&dev->parts);
961 INIT_LIST_HEAD(&dev->link);
962
963 /* move partitions from tmp_list to dev->parts */
964 list_for_each_safe(entry, n, &tmp_list) {
965 part = list_entry(entry, struct part_info, link);
966 list_del(entry);
967 if (part_sort_add(dev, part) != 0) {
968 device_del(dev);
969 return 1;
970 }
971 }
972
973 *retdev = dev;
974
975 DEBUGF("===\n\n");
976 return 0;
977}
978
979/**
980 * Initialize global device list.
981 *
982 * @return 0 on success, 1 otherwise
983 */
984static int devices_init(void)
985{
986 last_parts[0] = '\0';
987 current_dev = NULL;
988 current_save();
989
990 return device_delall(&devices);
991}
992
993/*
994 * Search global mtdids list and find id of requested type and number.
995 *
996 * @return pointer to the id if it exists, NULL otherwise
997 */
998static struct mtdids* id_find(u8 type, u8 num)
999{
1000 struct list_head *entry;
1001 struct mtdids *id;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001002
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001003 list_for_each(entry, &mtdids) {
1004 id = list_entry(entry, struct mtdids, link);
1005
1006 if ((id->type == type) && (id->num == num))
1007 return id;
1008 }
1009
1010 return NULL;
1011}
1012
1013/**
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001014 * Search global mtdids list and find id of a requested mtd_id.
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001015 *
1016 * Note: first argument is not null terminated.
1017 *
1018 * @param mtd_id string containing requested mtd_id
1019 * @param mtd_id_len length of supplied mtd_id
1020 * @return pointer to the id if it exists, NULL otherwise
1021 */
1022static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
1023{
1024 struct list_head *entry;
1025 struct mtdids *id;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001026
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001027 DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1028 mtd_id_len, mtd_id, mtd_id_len);
1029
1030 list_for_each(entry, &mtdids) {
1031 id = list_entry(entry, struct mtdids, link);
1032
1033 DEBUGF("entry: '%s' (len = %d)\n",
1034 id->mtd_id, strlen(id->mtd_id));
1035
1036 if (mtd_id_len != strlen(id->mtd_id))
1037 continue;
1038 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1039 return id;
1040 }
1041
1042 return NULL;
1043}
1044#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1045
1046/**
1047 * Parse device id string <dev-id> := 'nand'|'nor'<dev-num>, return device
1048 * type and number.
1049 *
1050 * @param id string describing device id
1051 * @param ret_id output pointer to next char after parse completes (output)
1052 * @param dev_type parsed device type (output)
1053 * @param dev_num parsed device number (output)
1054 * @return 0 on success, 1 otherwise
1055 */
1056int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
1057{
1058 const char *p = id;
1059
1060 *dev_type = 0;
1061 if (strncmp(p, "nand", 4) == 0) {
1062 *dev_type = MTD_DEV_TYPE_NAND;
1063 p += 4;
1064 } else if (strncmp(p, "nor", 3) == 0) {
1065 *dev_type = MTD_DEV_TYPE_NOR;
1066 p += 3;
1067 } else {
1068 printf("incorrect device type in %s\n", id);
1069 return 1;
1070 }
1071
1072 if (!isdigit(*p)) {
1073 printf("incorrect device number in %s\n", id);
1074 return 1;
1075 }
1076
1077 *dev_num = simple_strtoul(p, (char **)&p, 0);
1078 if (ret_id)
1079 *ret_id = p;
1080 return 0;
1081}
1082
1083#ifdef CONFIG_JFFS2_CMDLINE
1084/**
1085 * Process all devices and generate corresponding mtdparts string describing
1086 * all partitions on all devices.
1087 *
1088 * @param buf output buffer holding generated mtdparts string (output)
1089 * @param buflen buffer size
1090 * @return 0 on success, 1 otherwise
1091 */
1092static int generate_mtdparts(char *buf, u32 buflen)
1093{
1094 struct list_head *pentry, *dentry;
1095 struct mtd_device *dev;
1096 struct part_info *part, *prev_part;
1097 char *p = buf;
1098 char tmpbuf[32];
1099 u32 size, offset, len, part_cnt;
1100 u32 maxlen = buflen - 1;
1101
1102 DEBUGF("--- generate_mtdparts ---\n");
1103
1104 if (list_empty(&devices)) {
1105 buf[0] = '\0';
1106 return 0;
1107 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001108
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001109 sprintf(p, "mtdparts=");
1110 p += 9;
1111
1112 list_for_each(dentry, &devices) {
1113 dev = list_entry(dentry, struct mtd_device, link);
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001114
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001115 /* copy mtd_id */
1116 len = strlen(dev->id->mtd_id) + 1;
1117 if (len > maxlen)
1118 goto cleanup;
1119 memcpy(p, dev->id->mtd_id, len - 1);
1120 p += len - 1;
1121 *(p++) = ':';
1122 maxlen -= len;
1123
1124 /* format partitions */
1125 prev_part = NULL;
1126 part_cnt = 0;
1127 list_for_each(pentry, &dev->parts) {
1128 part = list_entry(pentry, struct part_info, link);
1129 size = part->size;
1130 offset = part->offset;
1131 part_cnt++;
1132
1133 /* partition size */
1134 memsize_format(tmpbuf, size);
1135 len = strlen(tmpbuf);
1136 if (len > maxlen)
1137 goto cleanup;
1138 memcpy(p, tmpbuf, len);
1139 p += len;
1140 maxlen -= len;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001141
1142
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001143 /* add offset only when there is a gap between
1144 * partitions */
1145 if ((!prev_part && (offset != 0)) ||
1146 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1147
1148 memsize_format(tmpbuf, offset);
1149 len = strlen(tmpbuf) + 1;
1150 if (len > maxlen)
1151 goto cleanup;
1152 *(p++) = '@';
1153 memcpy(p, tmpbuf, len - 1);
1154 p += len - 1;
1155 maxlen -= len;
1156 }
1157
1158 /* copy name only if user supplied */
1159 if(!part->auto_name) {
1160 len = strlen(part->name) + 2;
1161 if (len > maxlen)
1162 goto cleanup;
1163
1164 *(p++) = '(';
1165 memcpy(p, part->name, len - 2);
1166 p += len - 2;
1167 *(p++) = ')';
1168 maxlen -= len;
1169 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001170
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001171 /* ro mask flag */
1172 if (part->mask_flags && MTD_WRITEABLE) {
1173 len = 2;
1174 if (len > maxlen)
1175 goto cleanup;
1176 *(p++) = 'r';
1177 *(p++) = 'o';
1178 maxlen -= 2;
1179 }
1180
1181 /* print ',' separator if there are other partitions
1182 * following */
1183 if (dev->num_parts > part_cnt) {
1184 if (1 > maxlen)
1185 goto cleanup;
1186 *(p++) = ',';
1187 maxlen--;
1188 }
1189 prev_part = part;
1190 }
1191 /* print ';' separator if there are other devices following */
1192 if (dentry->next != &devices) {
1193 if (1 > maxlen)
1194 goto cleanup;
1195 *(p++) = ';';
1196 maxlen--;
1197 }
1198 }
1199
1200 /* we still have at least one char left, as we decremented maxlen at
1201 * the begining */
1202 *p = '\0';
1203
1204 return 0;
1205
1206cleanup:
1207 last_parts[0] = '\0';
1208 return 1;
1209}
1210
1211/**
1212 * Call generate_mtdparts to process all devices and generate corresponding
1213 * mtdparts string, save it in mtdparts environment variable.
1214 *
1215 * @param buf output buffer holding generated mtdparts string (output)
1216 * @param buflen buffer size
1217 * @return 0 on success, 1 otherwise
1218 */
1219static int generate_mtdparts_save(char *buf, u32 buflen)
1220{
1221 int ret;
1222
1223 ret = generate_mtdparts(buf, buflen);
1224
1225 if ((buf[0] != '\0') && (ret == 0))
1226 setenv("mtdparts", buf);
1227 else
1228 setenv("mtdparts", NULL);
1229
1230 return ret;
1231}
1232
1233/**
1234 * Format and print out a partition list for each device from global device
1235 * list.
1236 */
1237static void list_partitions(void)
1238{
1239 struct list_head *dentry, *pentry;
1240 struct part_info *part;
1241 struct mtd_device *dev;
1242 int part_num;
1243
1244 DEBUGF("\n---list_partitions---\n");
1245 list_for_each(dentry, &devices) {
1246 dev = list_entry(dentry, struct mtd_device, link);
1247 printf("\ndevice %s%d <%s>, # parts = %d\n",
1248 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1249 dev->id->mtd_id, dev->num_parts);
1250 printf(" #: name\t\t\tsize\t\toffset\t\tmask_flags\n");
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001251
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001252 /* list partitions for given device */
1253 part_num = 0;
1254 list_for_each(pentry, &dev->parts) {
1255 part = list_entry(pentry, struct part_info, link);
1256 printf(" %d: %-22s\t0x%08x\t0x%08x\t%d\n",
1257 part_num, part->name, part->size,
1258 part->offset, part->mask_flags);
1259
1260 part_num++;
1261 }
1262 }
1263 if (list_empty(&devices))
1264 printf("no partitions defined\n");
1265
1266 /* current_dev is not NULL only when we have non empty device list */
1267 if (current_dev) {
1268 part = jffs2_part_info(current_dev, current_partnum);
1269 if (part) {
1270 printf("\nactive partition: %s%d,%d - (%s) 0x%08lx @ 0x%08lx\n",
1271 MTD_DEV_TYPE(current_dev->id->type),
1272 current_dev->id->num, current_partnum,
1273 part->name, part->size, part->offset);
1274 } else {
1275 printf("could not get current partition info\n\n");
1276 }
1277 }
1278
1279 printf("\ndefaults:\n");
1280 printf("mtdids : %s\n", mtdids_default);
1281 printf("mtdparts: %s\n", mtdparts_default);
1282}
1283
1284/**
1285 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1286 * corresponding device and verify partition number.
1287 *
1288 * @param id string describing device and partition
1289 * @param dev pointer to the requested device (output)
1290 * @param part_num verified partition number (output)
1291 * @param part pointer to requested partition (output)
1292 * @return 0 on success, 1 otherwise
1293 */
1294int find_dev_and_part(const char *id, struct mtd_device **dev,
1295 u8 *part_num, struct part_info **part)
1296{
1297 u8 type, dnum, pnum;
1298 const char *p;
1299
1300 DEBUGF("--- find_dev_and_part ---\nid = %s\n", id);
1301
1302 p = id;
1303 *dev = NULL;
1304 *part = NULL;
1305 *part_num = 0;
1306
1307 if (id_parse(p, &p, &type, &dnum) != 0)
1308 return 1;
1309
1310 if ((*p++ != ',') || (*p == '\0')) {
1311 printf("no partition number specified\n");
1312 return 1;
1313 }
1314 pnum = simple_strtoul(p, (char **)&p, 0);
1315 if (*p != '\0') {
1316 printf("unexpected trailing character '%c'\n", *p);
1317 return 1;
1318 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001319
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001320 if ((*dev = device_find(type, dnum)) == NULL) {
1321 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1322 return 1;
1323 }
1324
1325 if ((*part = jffs2_part_info(*dev, pnum)) == NULL) {
1326 printf("no such partition\n");
1327 *dev = NULL;
1328 return 1;
1329 }
1330
1331 *part_num = pnum;
1332
1333 return 0;
1334}
1335
1336/**
1337 * Find and delete partition. For partition id format see find_dev_and_part().
1338 *
1339 * @param id string describing device and partition
1340 * @return 0 on success, 1 otherwise
1341 */
1342static int delete_partition(const char *id)
1343{
1344 u8 pnum;
1345 struct mtd_device *dev;
1346 struct part_info *part;
1347
1348 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1349
1350 DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n",
1351 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1352 part->name, part->size, part->offset);
1353
1354 if (part_del(dev, part) != 0)
1355 return 1;
1356
1357 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1358 printf("generated mtdparts too long, reseting to null\n");
1359 return 1;
1360 }
1361 return 0;
1362 }
1363
1364 printf("partition %s not found\n", id);
1365 return 1;
1366}
1367
1368/**
1369 * Accept character string describing mtd partitions and call device_parse()
1370 * for each entry. Add created devices to the global devices list.
1371 *
1372 * @param mtdparts string specifing mtd partitions
1373 * @return 0 on success, 1 otherwise
1374 */
1375static int parse_mtdparts(const char *const mtdparts)
1376{
1377 const char *p = mtdparts;
1378 struct mtd_device *dev;
1379 int err = 1;
1380
1381 DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1382
1383 /* delete all devices and partitions */
1384 if (devices_init() != 0) {
1385 printf("could not initialise device list\n");
1386 return err;
1387 }
1388
1389 /* re-read 'mtdparts' variable, devices_init may be updating env */
1390 p = getenv("mtdparts");
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001391
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001392 if (strncmp(p, "mtdparts=", 9) != 0) {
1393 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1394 return err;
1395 }
1396 p += 9;
1397
1398 while (p && (*p != '\0')) {
1399 err = 1;
1400 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1401 break;
1402
1403 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1404 dev->id->num, dev->id->mtd_id);
1405
1406 /* check if parsed device is already on the list */
1407 if (device_find(dev->id->type, dev->id->num) != NULL) {
1408 printf("device %s%d redefined, please correct mtdparts variable\n",
1409 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1410 break;
1411 }
1412
1413 list_add_tail(&dev->link, &devices);
1414 err = 0;
1415 }
1416 if (err == 1) {
1417 device_delall(&devices);
1418 return 1;
1419 }
1420
1421 return 0;
1422}
1423
1424/**
1425 * Parse provided string describing mtdids mapping (see file header for mtdids
1426 * variable format). Allocate memory for each entry and add all found entries
1427 * to the global mtdids list.
1428 *
1429 * @param ids mapping string
1430 * @return 0 on success, 1 otherwise
1431 */
1432static int parse_mtdids(const char *const ids)
1433{
1434 const char *p = ids;
1435 const char *mtd_id;
1436 int mtd_id_len;
1437 struct mtdids *id;
1438 struct list_head *entry, *n;
1439 struct mtdids *id_tmp;
1440 u8 type, num;
1441 u32 size;
1442 int ret = 1;
1443
1444 DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
1445
1446 /* clean global mtdids list */
1447 list_for_each_safe(entry, n, &mtdids) {
1448 id_tmp = list_entry(entry, struct mtdids, link);
1449 DEBUGF("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
1450 list_del(entry);
1451 free(id_tmp);
1452 }
1453 last_ids[0] = '\0';
1454 INIT_LIST_HEAD(&mtdids);
1455
1456 while(p && (*p != '\0')) {
1457
1458 ret = 1;
1459 /* parse 'nor'|'nand'<dev-num> */
1460 if (id_parse(p, &p, &type, &num) != 0)
1461 break;
1462
1463 if (*p != '=') {
1464 printf("mtdids: incorrect <dev-num>\n");
1465 break;
1466 }
1467 p++;
1468
1469 /* check if requested device exists */
1470 if (device_validate(type, num, &size) != 0)
1471 return 1;
1472
1473 /* locate <mtd-id> */
1474 mtd_id = p;
1475 if ((p = strchr(mtd_id, ',')) != NULL) {
1476 mtd_id_len = p - mtd_id + 1;
1477 p++;
1478 } else {
1479 mtd_id_len = strlen(mtd_id) + 1;
1480 }
1481 if (mtd_id_len == 0) {
1482 printf("mtdids: no <mtd-id> identifier\n");
1483 break;
1484 }
1485
1486 /* check if this id is already on the list */
1487 int double_entry = 0;
1488 list_for_each(entry, &mtdids) {
1489 id_tmp = list_entry(entry, struct mtdids, link);
1490 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1491 double_entry = 1;
1492 break;
1493 }
1494 }
1495 if (double_entry) {
1496 printf("device id %s%d redefined, please correct mtdids variable\n",
1497 MTD_DEV_TYPE(type), num);
1498 break;
1499 }
1500
1501 /* allocate mtdids structure */
1502 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1503 printf("out of memory\n");
1504 break;
1505 }
1506 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1507 id->num = num;
1508 id->type = type;
1509 id->size = size;
1510 id->mtd_id = (char *)(id + 1);
1511 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1512 id->mtd_id[mtd_id_len - 1] = '\0';
1513 INIT_LIST_HEAD(&id->link);
1514
1515 DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
1516 MTD_DEV_TYPE(id->type), id->num,
1517 id->size, id->mtd_id);
1518
1519 list_add_tail(&id->link, &mtdids);
1520 ret = 0;
1521 }
1522 if (ret == 1) {
1523 /* clean mtdids list and free allocated memory */
1524 list_for_each_safe(entry, n, &mtdids) {
1525 id_tmp = list_entry(entry, struct mtdids, link);
1526 list_del(entry);
1527 free(id_tmp);
1528 }
1529 return 1;
1530 }
1531
1532 return 0;
1533}
1534
1535/**
1536 * Parse and initialize global mtdids mapping and create global
1537 * device/partition list.
1538 *
1539 * @return 0 on success, 1 otherwise
1540 */
1541int mtdparts_init(void)
1542{
1543 static int initialized = 0;
1544 const char *ids, *parts;
1545 const char *current_partition;
1546 int ids_changed;
1547 char tmp_ep[PARTITION_MAXLEN];
1548
1549 DEBUGF("\n---mtdparts_init---\n");
1550 if (!initialized) {
1551 INIT_LIST_HEAD(&mtdids);
1552 INIT_LIST_HEAD(&devices);
1553 memset(last_ids, 0, MTDIDS_MAXLEN);
1554 memset(last_parts, 0, MTDPARTS_MAXLEN);
1555 memset(last_partition, 0, PARTITION_MAXLEN);
1556 initialized = 1;
1557 }
1558
1559 /* get variables */
1560 ids = getenv("mtdids");
1561 parts = getenv("mtdparts");
1562 current_partition = getenv("partition");
1563
1564 /* save it for later parsing, cannot rely on current partition pointer
1565 * as 'partition' variable may be updated during init */
1566 tmp_ep[0] = '\0';
1567 if (current_partition)
1568 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1569
1570 DEBUGF("last_ids : %s\n", last_ids);
1571 DEBUGF("env_ids : %s\n", ids);
1572 DEBUGF("last_parts: %s\n", last_parts);
1573 DEBUGF("env_parts : %s\n\n", parts);
1574
1575 DEBUGF("last_partition : %s\n", last_partition);
1576 DEBUGF("env_partition : %s\n", current_partition);
1577
1578 /* if mtdids varible is empty try to use defaults */
1579 if (!ids) {
1580 if (mtdids_default) {
1581 DEBUGF("mtdids variable not defined, using default\n");
1582 ids = mtdids_default;
1583 setenv("mtdids", (char *)ids);
1584 } else {
1585 printf("mtdids not defined, no default present\n");
1586 return 1;
1587 }
1588 }
1589 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1590 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1591 return 1;
1592 }
1593
1594 /* do no try to use defaults when mtdparts variable is not defined,
1595 * just check the length */
1596 if (!parts)
1597 printf("mtdparts variable not set, see 'help mtdparts'\n");
1598
1599 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1600 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1601 return 1;
1602 }
1603
1604 /* check if we have already parsed those mtdids */
1605 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1606 ids_changed = 0;
1607 } else {
1608 ids_changed = 1;
1609
1610 if (parse_mtdids(ids) != 0) {
Marian Balakowicz7d454772005-09-11 15:27:12 +02001611 devices_init();
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001612 return 1;
1613 }
1614
1615 /* ok it's good, save new ids */
1616 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1617 }
1618
1619 /* parse partitions if either mtdparts or mtdids were updated */
1620 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1621 if (parse_mtdparts(parts) != 0)
1622 return 1;
1623
1624 if (list_empty(&devices)) {
1625 printf("mtdparts_init: no valid partitions\n");
1626 return 1;
1627 }
1628
1629 /* ok it's good, save new parts */
1630 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1631
1632 /* reset first partition from first dev from the list as current */
1633 current_dev = list_entry(devices.next, struct mtd_device, link);
1634 current_partnum = 0;
1635 current_save();
1636
1637 DEBUGF("mtdparts_init: current_dev = %s%d, current_partnum = %d\n",
1638 MTD_DEV_TYPE(current_dev->id->type),
1639 current_dev->id->num, current_partnum);
1640 }
1641
1642 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1643 if (!parts && (last_parts[0] != '\0'))
1644 return devices_init();
1645
1646 /* do not process current partition if mtdparts variable is null */
1647 if (!parts)
1648 return 0;
1649
1650 /* is current partition set in environment? if so, use it */
1651 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1652 struct part_info *p;
1653 struct mtd_device *cdev;
1654 u8 pnum;
1655
1656 DEBUGF("--- getting current partition: %s\n", tmp_ep);
1657
1658 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
1659 current_dev = cdev;
1660 current_partnum = pnum;
1661 current_save();
1662 }
1663 } else if (getenv("partition") == NULL) {
1664 DEBUGF("no partition variable set, setting...\n");
1665 current_save();
1666 }
1667
1668 return 0;
1669}
1670#else /* #ifdef CONFIG_JFFS2_CMDLINE */
1671/*
1672 * 'Static' version of command line mtdparts_init() routine. Single partition on
1673 * a single device configuration.
1674 */
1675
1676/**
1677 * Parse and initialize global mtdids mapping and create global
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001678 * device/partition list.
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001679 *
1680 * @return 0 on success, 1 otherwise
1681 */
1682int mtdparts_init(void)
1683{
1684 static int initialized = 0;
1685 u32 size;
1686 char *dev_name;
1687
1688 DEBUGF("\n---mtdparts_init---\n");
1689 if (!initialized) {
Wolfgang Denkc4e0e682005-08-09 21:41:20 +02001690 struct mtdids *id;
1691 struct part_info *part;
1692
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001693 initialized = 1;
1694 current_dev = (struct mtd_device *)
1695 malloc(sizeof(struct mtd_device) +
1696 sizeof(struct part_info) +
1697 sizeof(struct mtdids));
1698 if (!current_dev) {
1699 printf("out of memory\n");
1700 return 1;
1701 }
1702 memset(current_dev, 0, sizeof(struct mtd_device) +
1703 sizeof(struct part_info) + sizeof(struct mtdids));
1704
Wolfgang Denkc4e0e682005-08-09 21:41:20 +02001705 id = (struct mtdids *)(current_dev + 1);
1706 part = (struct part_info *)(id + 1);
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001707
1708 /* id */
1709 id->mtd_id = "single part";
1710
1711#if defined(CONFIG_JFFS2_DEV)
1712 dev_name = CONFIG_JFFS2_DEV;
1713#else
1714 dev_name = "nor0";
1715#endif
1716
1717 if ((id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
1718 (device_validate(id->type, id->num, &size) != 0)) {
1719 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
1720 free(current_dev);
1721 return 1;
1722 }
1723 id->size = size;
1724 INIT_LIST_HEAD(&id->link);
1725
1726 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
1727 id->type, id->num, id->size, id->mtd_id);
1728
1729 /* partition */
1730 part->name = "static";
1731 part->auto_name = 0;
1732
1733#if defined(CONFIG_JFFS2_PART_SIZE)
1734 part->size = CONFIG_JFFS2_PART_SIZE;
1735#else
1736 part->size = SIZE_REMAINING;
1737#endif
1738
1739#if defined(CONFIG_JFFS2_PART_OFFSET)
1740 part->offset = CONFIG_JFFS2_PART_OFFSET;
1741#else
1742 part->offset = 0x00000000;
1743#endif
1744
1745 part->dev = current_dev;
1746 INIT_LIST_HEAD(&part->link);
1747
1748 /* recalculate size if needed */
1749 if (part->size == SIZE_REMAINING)
1750 part->size = id->size - part->offset;
1751
1752 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
1753 part->name, part->size, part->offset);
1754
1755 /* device */
1756 current_dev->id = id;
1757 INIT_LIST_HEAD(&current_dev->link);
1758 current_dev->num_parts = 1;
1759 INIT_LIST_HEAD(&current_dev->parts);
1760 list_add(&part->link, &current_dev->parts);
1761 }
1762
1763 return 0;
1764}
1765#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1766
1767/**
1768 * Return pointer to the partition of a requested number from a requested
1769 * device.
1770 *
1771 * @param dev device that is to be searched for a partition
1772 * @param part_num requested partition number
1773 * @return pointer to the part_info, NULL otherwise
1774 */
1775static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
1776{
1777 struct list_head *entry;
1778 struct part_info *part;
1779 int num;
1780
1781 if (!dev)
1782 return NULL;
1783
1784 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
1785 part_num, MTD_DEV_TYPE(dev->id->type),
1786 dev->id->num, dev->id->mtd_id);
1787
1788 if (part_num >= dev->num_parts) {
1789 printf("invalid partition number %d for device %s%d (%s)\n",
1790 part_num, MTD_DEV_TYPE(dev->id->type),
1791 dev->id->num, dev->id->mtd_id);
1792 return NULL;
1793 }
1794
1795 /* locate partition number, return it */
1796 num = 0;
1797 list_for_each(entry, &dev->parts) {
1798 part = list_entry(entry, struct part_info, link);
1799
1800 if (part_num == num++) {
1801 return part;
1802 }
1803 }
1804
1805 return NULL;
1806}
1807
1808/***************************************************/
1809/* U-boot commands */
1810/***************************************************/
1811
1812/**
1813 * Routine implementing fsload u-boot command. This routine tries to load
1814 * a requested file from jffs2/cramfs filesystem on a current partition.
1815 *
1816 * @param cmdtp command internal data
1817 * @param flag command flag
1818 * @param argc number of arguments supplied to the command
1819 * @param argv arguments list
1820 * @return 0 on success, 1 otherwise
1821 */
1822int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1823{
wdenk39539882004-07-01 16:30:44 +00001824 char *fsname;
wdenkf39748a2004-06-09 13:37:52 +00001825 char *filename;
wdenk39539882004-07-01 16:30:44 +00001826 int size;
1827 struct part_info *part;
1828 ulong offset = load_addr;
wdenkf39748a2004-06-09 13:37:52 +00001829
1830 /* pre-set Boot file name */
1831 if ((filename = getenv("bootfile")) == NULL) {
1832 filename = "uImage";
1833 }
1834
wdenkc6097192002-11-03 00:24:07 +00001835 if (argc == 2) {
1836 filename = argv[1];
1837 }
1838 if (argc == 3) {
1839 offset = simple_strtoul(argv[1], NULL, 16);
wdenk4b9206e2004-03-23 22:14:11 +00001840 load_addr = offset;
wdenkc6097192002-11-03 00:24:07 +00001841 filename = argv[2];
1842 }
1843
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001844 /* make sure we are in sync with env variables */
1845 if (mtdparts_init() !=0)
1846 return 1;
1847
1848 if ((part = jffs2_part_info(current_dev, current_partnum))){
wdenkc6097192002-11-03 00:24:07 +00001849
wdenkdd875c72004-01-03 21:24:46 +00001850 /* check partition type for cramfs */
1851 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
1852 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
1853
1854 if (cramfs_check(part)) {
1855 size = cramfs_load ((char *) offset, part, filename);
1856 } else {
1857 /* if this is not cramfs assume jffs2 */
1858 size = jffs2_1pass_load((char *)offset, part, filename);
1859 }
wdenkc6097192002-11-03 00:24:07 +00001860
1861 if (size > 0) {
1862 char buf[10];
wdenkdd875c72004-01-03 21:24:46 +00001863 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
1864 fsname, size, offset);
wdenkc6097192002-11-03 00:24:07 +00001865 sprintf(buf, "%x", size);
1866 setenv("filesize", buf);
1867 } else {
wdenkdd875c72004-01-03 21:24:46 +00001868 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
wdenkc6097192002-11-03 00:24:07 +00001869 }
1870
1871 return !(size > 0);
1872 }
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02001873 return 1;
wdenkc6097192002-11-03 00:24:07 +00001874}
1875
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001876/**
1877 * Routine implementing u-boot ls command which lists content of a given
1878 * directory on a current partition.
1879 *
1880 * @param cmdtp command internal data
1881 * @param flag command flag
1882 * @param argc number of arguments supplied to the command
1883 * @param argv arguments list
1884 * @return 0 on success, 1 otherwise
1885 */
1886int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenkc6097192002-11-03 00:24:07 +00001887{
wdenka87589d2005-06-10 10:00:19 +00001888 char *filename = "/";
wdenkc6097192002-11-03 00:24:07 +00001889 int ret;
1890 struct part_info *part;
1891
1892 if (argc == 2)
1893 filename = argv[1];
1894
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001895 /* make sure we are in sync with env variables */
1896 if (mtdparts_init() !=0)
1897 return 1;
1898
1899 if ((part = jffs2_part_info(current_dev, current_partnum))){
wdenkc6097192002-11-03 00:24:07 +00001900
wdenkdd875c72004-01-03 21:24:46 +00001901 /* check partition type for cramfs */
1902 if (cramfs_check(part)) {
1903 ret = cramfs_ls (part, filename);
1904 } else {
1905 /* if this is not cramfs assume jffs2 */
1906 ret = jffs2_1pass_ls(part, filename);
1907 }
wdenkc6097192002-11-03 00:24:07 +00001908
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02001909 return ret ? 0 : 1;
wdenkc6097192002-11-03 00:24:07 +00001910 }
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02001911 return 1;
wdenkc6097192002-11-03 00:24:07 +00001912}
1913
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001914/**
1915 * Routine implementing u-boot fsinfo command. This routine prints out
1916 * miscellaneous filesystem informations/statistics.
1917 *
1918 * @param cmdtp command internal data
1919 * @param flag command flag
1920 * @param argc number of arguments supplied to the command
1921 * @param argv arguments list
1922 * @return 0 on success, 1 otherwise
1923 */
1924int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenkc6097192002-11-03 00:24:07 +00001925{
wdenkc6097192002-11-03 00:24:07 +00001926 struct part_info *part;
wdenkdd875c72004-01-03 21:24:46 +00001927 char *fsname;
1928 int ret;
wdenkc6097192002-11-03 00:24:07 +00001929
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001930 /* make sure we are in sync with env variables */
1931 if (mtdparts_init() !=0)
1932 return 1;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001933
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001934 if ((part = jffs2_part_info(current_dev, current_partnum))){
wdenkc6097192002-11-03 00:24:07 +00001935
wdenkdd875c72004-01-03 21:24:46 +00001936 /* check partition type for cramfs */
1937 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
1938 printf("### filesystem type is %s\n", fsname);
1939
1940 if (cramfs_check(part)) {
1941 ret = cramfs_info (part);
1942 } else {
1943 /* if this is not cramfs assume jffs2 */
1944 ret = jffs2_1pass_info(part);
1945 }
wdenkc6097192002-11-03 00:24:07 +00001946
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02001947 return ret ? 0 : 1;
wdenkc6097192002-11-03 00:24:07 +00001948 }
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02001949 return 1;
wdenkc6097192002-11-03 00:24:07 +00001950}
1951
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001952/* command line only */
1953#ifdef CONFIG_JFFS2_CMDLINE
1954/**
1955 * Routine implementing u-boot chpart command. Sets new current partition based
1956 * on the user supplied partition id. For partition id format see find_dev_and_part().
1957 *
1958 * @param cmdtp command internal data
1959 * @param flag command flag
1960 * @param argc number of arguments supplied to the command
1961 * @param argv arguments list
1962 * @return 0 on success, 1 otherwise
1963 */
1964int do_jffs2_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenkc6097192002-11-03 00:24:07 +00001965{
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001966/* command line only */
1967 struct mtd_device *dev;
1968 struct part_info *part;
1969 u8 pnum;
wdenkc6097192002-11-03 00:24:07 +00001970
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001971 if (mtdparts_init() !=0)
1972 return 1;
wdenkc6097192002-11-03 00:24:07 +00001973
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001974 if (argc < 2) {
1975 printf("no partition id specified\n");
1976 return 1;
wdenkc6097192002-11-03 00:24:07 +00001977 }
1978
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001979 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
1980 return 1;
wdenkc6097192002-11-03 00:24:07 +00001981
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001982 current_dev = dev;
1983 current_partnum = pnum;
1984 current_save();
1985
1986 printf("partition changed to %s%d,%d\n",
1987 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
1988
wdenkc6097192002-11-03 00:24:07 +00001989 return 0;
wdenkc6097192002-11-03 00:24:07 +00001990}
wdenkeedcd072004-09-08 22:03:11 +00001991
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001992/**
1993 * Routine implementing u-boot mtdparts command. Initialize/update default global
1994 * partition list and process user partition request (list, add, del).
1995 *
1996 * @param cmdtp command internal data
1997 * @param flag command flag
1998 * @param argc number of arguments supplied to the command
1999 * @param argv arguments list
2000 * @return 0 on success, 1 otherwise
2001 */
2002int do_jffs2_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2003{
2004 if (argc == 2) {
2005 if (strcmp(argv[1], "default") == 0) {
2006 setenv("mtdids", (char *)mtdids_default);
2007 setenv("mtdparts", (char *)mtdparts_default);
2008 setenv("partition", NULL);
2009
2010 mtdparts_init();
2011 return 0;
2012 } else if (strcmp(argv[1], "delall") == 0) {
2013 /* this may be the first run, initialize lists if needed */
2014 mtdparts_init();
2015
2016 setenv("mtdparts", NULL);
2017
2018 /* devices_init() calls current_save() */
2019 return devices_init();
2020 }
2021 }
2022
2023 /* make sure we are in sync with env variables */
2024 if (mtdparts_init() != 0)
2025 return 1;
2026
2027 if (argc == 1) {
2028 list_partitions();
2029 return 0;
2030 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02002031
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002032 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
2033 if (((argc == 5) || (argc == 6)) && (strcmp(argv[1], "add") == 0)) {
2034#define PART_ADD_DESC_MAXLEN 64
2035 char tmpbuf[PART_ADD_DESC_MAXLEN];
2036 u8 type, num, len;
2037 struct mtd_device *dev;
2038 struct mtd_device *dev_tmp;
2039 struct mtdids *id;
2040 struct part_info *p;
2041
2042 if (id_parse(argv[2], NULL, &type, &num) != 0)
2043 return 1;
2044
2045 if ((id = id_find(type, num)) == NULL) {
2046 printf("no such device %s defined in mtdids variable\n", argv[2]);
2047 return 1;
2048 }
2049
2050 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
2051 len += strlen(argv[3]); /* size@offset */
2052 len += strlen(argv[4]) + 2; /* '(' name ')' */
2053 if (argv[5] && (strlen(argv[5]) == 2))
2054 len += 2; /* 'ro' */
2055
2056 if (len >= PART_ADD_DESC_MAXLEN) {
2057 printf("too long partition description\n");
2058 return 1;
2059 }
2060 sprintf(tmpbuf, "%s:%s(%s)%s",
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02002061 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002062 DEBUGF("add tmpbuf: %s\n", tmpbuf);
2063
2064 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
2065 return 1;
2066
2067 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
2068 dev->id->num, dev->id->mtd_id);
2069
2070 if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) {
2071 device_add(dev);
2072 } else {
2073 /* merge new partition with existing ones*/
2074 p = list_entry(dev->parts.next, struct part_info, link);
2075 if (part_add(dev_tmp, p) != 0) {
2076 device_del(dev);
2077 return 1;
2078 }
2079 }
2080
2081 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
2082 printf("generated mtdparts too long, reseting to null\n");
2083 return 1;
2084 }
2085
2086 return 0;
2087 }
2088
2089 /* mtdparts del part-id */
2090 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
2091 DEBUGF("del: part-id = %s\n", argv[2]);
2092
2093 return delete_partition(argv[2]);
2094 }
2095
2096 printf ("Usage:\n%s\n", cmdtp->usage);
2097 return 1;
2098}
2099#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
wdenk8bde7f72003-06-27 21:31:46 +00002100
2101/***************************************************/
wdenk0d498392003-07-01 21:06:45 +00002102U_BOOT_CMD(
2103 fsload, 3, 0, do_jffs2_fsload,
wdenkaa5590b2004-06-09 12:42:26 +00002104 "fsload\t- load binary file from a filesystem image\n",
wdenk8bde7f72003-06-27 21:31:46 +00002105 "[ off ] [ filename ]\n"
2106 " - load binary file from flash bank\n"
2107 " with offset 'off'\n"
2108);
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002109U_BOOT_CMD(
2110 ls, 2, 1, do_jffs2_ls,
2111 "ls\t- list files in a directory (default /)\n",
2112 "[ directory ]\n"
2113 " - list files in a directory.\n"
2114);
wdenk8bde7f72003-06-27 21:31:46 +00002115
wdenk0d498392003-07-01 21:06:45 +00002116U_BOOT_CMD(
2117 fsinfo, 1, 1, do_jffs2_fsinfo,
wdenkaa5590b2004-06-09 12:42:26 +00002118 "fsinfo\t- print information about filesystems\n",
wdenk8bde7f72003-06-27 21:31:46 +00002119 " - print information about filesystems\n"
2120);
2121
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002122#ifdef CONFIG_JFFS2_CMDLINE
wdenk0d498392003-07-01 21:06:45 +00002123U_BOOT_CMD(
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002124 chpart, 2, 0, do_jffs2_chpart,
2125 "chpart\t- change active partition\n",
2126 "part-id\n"
2127 " - change active partition (e.g. part-id = nand0,1)\n"
wdenk8bde7f72003-06-27 21:31:46 +00002128);
2129
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002130U_BOOT_CMD(
2131 mtdparts, 6, 0, do_jffs2_mtdparts,
2132 "mtdparts- define flash/nand partitions\n",
2133 "\n"
2134 " - list partition table\n"
2135 "mtdparts delall\n"
2136 " - delete all partitions\n"
2137 "mtdparts del part-id\n"
2138 " - delete partition (e.g. part-id = nand0,1)\n"
2139 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2140 " - add partition\n"
2141 "mtdparts default\n"
2142 " - reset partition table to defaults\n\n"
2143 "-----\n\n"
2144 "this command uses three environment variables:\n\n"
2145 "'partition' - keeps current partition identifier\n\n"
2146 "partition := <part-id>\n"
2147 "<part-id> := <dev-id>,part_num\n\n"
2148 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2149 "mtdids=<idmap>[,<idmap>,...]\n\n"
2150 "<idmap> := <dev-id>=<mtd-id>\n"
2151 "<dev-id> := 'nand'|'nor'<dev-num>\n"
2152 "<dev-num> := mtd device number, 0...\n"
2153 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2154 "'mtdparts' - partition list\n\n"
2155 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2156 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2157 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2158 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2159 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2160 "<offset> := partition start offset within the device\n"
2161 "<name> := '(' NAME ')'\n"
2162 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)\n"
2163);
2164#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2165
2166/***************************************************/
2167
wdenkc6097192002-11-03 00:24:07 +00002168#endif /* CFG_CMD_JFFS2 */