blob: 7fd1fa33daf2fbccf21880e6d53f0904af8c30cd [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Wolfgang Denk47f57792005-08-08 01:03:24 +02004 *
wdenkae65f502004-01-03 21:24:46 +00005 * (C) Copyright 2002
6 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
Wolfgang Denk47f57792005-08-08 01:03:24 +02007 *
wdenkae65f502004-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 Denk47f57792005-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 *
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 *
Wolfgang Denkeb95c852005-08-10 15:14:32 +020048 *
Wolfgang Denk47f57792005-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
89 */
90#include <common.h>
91#include <command.h>
92#include <malloc.h>
93#include <jffs2/jffs2.h>
Wolfgang Denk47f57792005-08-08 01:03:24 +020094#include <linux/list.h>
95#include <linux/ctype.h>
96
97#if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
98
99#include <cramfs/cramfs_fs.h>
100
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100101#if (CONFIG_COMMANDS & CFG_CMD_NAND)
102#ifdef CFG_NAND_LEGACY
103#include <linux/mtd/nand_legacy.h>
104#else /* !CFG_NAND_LEGACY */
105#include <linux/mtd/nand.h>
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200106#include <nand.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100107#endif /* !CFG_NAND_LEGACY */
108#endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */
Wolfgang Denk47f57792005-08-08 01:03:24 +0200109/* enable/disable debugging messages */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100110#define DEBUG_JFFS
111#undef DEBUG_JFFS
Wolfgang Denk47f57792005-08-08 01:03:24 +0200112
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100113#ifdef DEBUG_JFFS
Wolfgang Denk47f57792005-08-08 01:03:24 +0200114# define DEBUGF(fmt, args...) printf(fmt ,##args)
115#else
116# define DEBUGF(fmt, args...)
117#endif
118
119/* special size referring to all the remaining space in a partition */
120#define SIZE_REMAINING 0xFFFFFFFF
121
122/* special offset value, it is used when not provided by user
123 *
124 * this value is used temporarily during parsing, later such offests
125 * are recalculated */
126#define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
127
128/* minimum partition size */
129#define MIN_PART_SIZE 4096
130
131/* this flag needs to be set in part_info struct mask_flags
132 * field for read-only partitions */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100133#define MTD_WRITEABLE_CMD 1
Wolfgang Denk47f57792005-08-08 01:03:24 +0200134
135#ifdef CONFIG_JFFS2_CMDLINE
136/* default values for mtdids and mtdparts variables */
137#if defined(MTDIDS_DEFAULT)
138static const char *const mtdids_default = MTDIDS_DEFAULT;
139#else
140#warning "MTDIDS_DEFAULT not defined!"
141static const char *const mtdids_default = NULL;
142#endif
143
144#if defined(MTDPARTS_DEFAULT)
145static const char *const mtdparts_default = MTDPARTS_DEFAULT;
146#else
147#warning "MTDPARTS_DEFAULT not defined!"
148static const char *const mtdparts_default = NULL;
149#endif
150
151/* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
152#define MTDIDS_MAXLEN 128
153#define MTDPARTS_MAXLEN 512
154#define PARTITION_MAXLEN 16
155static char last_ids[MTDIDS_MAXLEN];
156static char last_parts[MTDPARTS_MAXLEN];
157static char last_partition[PARTITION_MAXLEN];
158
159/* low level jffs2 cache cleaning routine */
160extern void jffs2_free_cache(struct part_info *part);
161
162/* mtdids mapping list, filled by parse_ids() */
163struct list_head mtdids;
164
165/* device/partition list, parse_cmdline() parses into here */
166struct list_head devices;
167#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
168
169/* current active device and partition number */
170static struct mtd_device *current_dev = NULL;
171static u8 current_partnum = 0;
172
173extern int cramfs_check (struct part_info *info);
174extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
175extern int cramfs_ls (struct part_info *info, char *filename);
176extern int cramfs_info (struct part_info *info);
177
178static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num);
179
180/* command line only routines */
181#ifdef CONFIG_JFFS2_CMDLINE
182
183static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
184static int device_del(struct mtd_device *dev);
185
186/**
187 * Parses a string into a number. The number stored at ptr is
188 * potentially suffixed with K (for kilobytes, or 1024 bytes),
189 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
190 * 1073741824). If the number is suffixed with K, M, or G, then
191 * the return value is the number multiplied by one kilobyte, one
192 * megabyte, or one gigabyte, respectively.
193 *
194 * @param ptr where parse begins
195 * @param retptr output pointer to next char after parse completes (output)
196 * @return resulting unsigned int
197 */
198static unsigned long memsize_parse (const char *const ptr, const char **retptr)
199{
200 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
201
202 switch (**retptr) {
203 case 'G':
204 case 'g':
205 ret <<= 10;
206 case 'M':
207 case 'm':
208 ret <<= 10;
209 case 'K':
210 case 'k':
211 ret <<= 10;
212 (*retptr)++;
213 default:
214 break;
215 }
216
217 return ret;
218}
219
220/**
221 * Format string describing supplied size. This routine does the opposite job
222 * to memsize_parse(). Size in bytes is converted to string and if possible
223 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
224 *
225 * Note, that this routine does not check for buffer overflow, it's the caller
226 * who must assure enough space.
227 *
228 * @param buf output buffer
229 * @param size size to be converted to string
230 */
231static void memsize_format(char *buf, u32 size)
232{
233#define SIZE_GB ((u32)1024*1024*1024)
234#define SIZE_MB ((u32)1024*1024)
235#define SIZE_KB ((u32)1024)
236
237 if ((size % SIZE_GB) == 0)
238 sprintf(buf, "%lug", size/SIZE_GB);
239 else if ((size % SIZE_MB) == 0)
240 sprintf(buf, "%lum", size/SIZE_MB);
241 else if (size % SIZE_KB == 0)
242 sprintf(buf, "%luk", size/SIZE_KB);
243 else
244 sprintf(buf, "%lu", size);
245}
246
247/**
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200248 * This routine does global indexing of all partitions. Resulting index for
249 * current partition is saved in 'mtddevnum'. Current partition name in
250 * 'mtddevname'.
251 */
252static void index_partitions(void)
253{
254 char buf[16];
255 u16 mtddevnum;
256 struct part_info *part;
257 struct list_head *dentry;
258 struct mtd_device *dev;
259
260 DEBUGF("--- index partitions ---\n");
261
262 if (current_dev) {
263 mtddevnum = 0;
264 list_for_each(dentry, &devices) {
265 dev = list_entry(dentry, struct mtd_device, link);
266 if (dev == current_dev) {
267 mtddevnum += current_partnum;
268 sprintf(buf, "%d", mtddevnum);
269 setenv("mtddevnum", buf);
270 break;
271 }
272 mtddevnum += dev->num_parts;
273 }
274
275 part = jffs2_part_info(current_dev, current_partnum);
276 setenv("mtddevname", part->name);
277
278 DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
279 } else {
280 setenv("mtddevnum", NULL);
281 setenv("mtddevname", NULL);
282
283 DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
284 }
285}
286
287/**
Wolfgang Denk47f57792005-08-08 01:03:24 +0200288 * Save current device and partition in environment variable 'partition'.
289 */
290static void current_save(void)
291{
292 char buf[16];
293
294 DEBUGF("--- current_save ---\n");
295
296 if (current_dev) {
297 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_dev->id->type),
298 current_dev->id->num, current_partnum);
299
300 setenv("partition", buf);
301 strncpy(last_partition, buf, 16);
302
303 DEBUGF("=> partition %s\n", buf);
304 } else {
305 setenv("partition", NULL);
306 last_partition[0] = '\0';
307
308 DEBUGF("=> partition NULL\n");
309 }
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200310 index_partitions();
Wolfgang Denk47f57792005-08-08 01:03:24 +0200311}
312
313/**
314 * Performs sanity check for supplied NOR flash partition. Table of existing
315 * NOR flash devices is searched and partition device is located. Alignment
316 * with the granularity of NOR flash sectors is verified.
317 *
318 * @param id of the parent device
319 * @param part partition to validate
320 * @return 0 if partition is valid, 1 otherwise
321 */
322static int part_validate_nor(struct mtdids *id, struct part_info *part)
323{
324#if (CONFIG_COMMANDS & CFG_CMD_FLASH)
325 /* info for FLASH chips */
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200326 extern flash_info_t flash_info[];
Wolfgang Denk47f57792005-08-08 01:03:24 +0200327 flash_info_t *flash;
328 int offset_aligned;
329 u32 end_offset;
330 int i;
331
332 flash = &flash_info[id->num];
333
334 offset_aligned = 0;
335 for (i = 0; i < flash->sector_count; i++) {
336 if ((flash->start[i] - flash->start[0]) == part->offset) {
337 offset_aligned = 1;
338 break;
339 }
340 }
341 if (offset_aligned == 0) {
342 printf("%s%d: partition (%s) start offset alignment incorrect\n",
343 MTD_DEV_TYPE(id->type), id->num, part->name);
344 return 1;
345 }
346
347 end_offset = part->offset + part->size;
348 for (i = 0; i < flash->sector_count; i++) {
349 if ((flash->start[i] - flash->start[0]) == end_offset)
350 return 0;
351 }
352
353 if (flash->size == end_offset)
354 return 0;
355
356 printf("%s%d: partition (%s) size alignment incorrect\n",
357 MTD_DEV_TYPE(id->type), id->num, part->name);
358#endif
359 return 1;
360}
361
362/**
363 * Performs sanity check for supplied NAND flash partition. Table of existing
364 * NAND flash devices is searched and partition device is located. Alignment
365 * with the granularity of nand erasesize is verified.
366 *
367 * @param id of the parent device
368 * @param part partition to validate
369 * @return 0 if partition is valid, 1 otherwise
370 */
371static int part_validate_nand(struct mtdids *id, struct part_info *part)
372{
373#if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
374 /* info for NAND chips */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200375 nand_info_t *nand;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200376
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200377 nand = &nand_info[id->num];
Wolfgang Denk47f57792005-08-08 01:03:24 +0200378
379 if ((unsigned long)(part->offset) % nand->erasesize) {
380 printf("%s%d: partition (%s) start offset alignment incorrect\n",
381 MTD_DEV_TYPE(id->type), id->num, part->name);
382 return 1;
383 }
384
385 if (part->size % nand->erasesize) {
386 printf("%s%d: partition (%s) size alignment incorrect\n",
387 MTD_DEV_TYPE(id->type), id->num, part->name);
388 return 1;
389 }
390
391 return 0;
392#else
393 return 1;
394#endif
395}
396
397/**
398 * Performs sanity check for supplied partition. Offset and size are verified
399 * to be within valid range. Partition type is checked and either
400 * parts_validate_nor() or parts_validate_nand() is called with the argument
401 * of part.
402 *
403 * @param id of the parent device
404 * @param part partition to validate
405 * @return 0 if partition is valid, 1 otherwise
406 */
407static int part_validate(struct mtdids *id, struct part_info *part)
408{
409 if (part->size == SIZE_REMAINING)
410 part->size = id->size - part->offset;
411
412 if (part->offset > id->size) {
413 printf("%s: offset %08lx beyond flash size %08lx\n",
414 id->mtd_id, part->offset, id->size);
415 return 1;
416 }
417
418 if ((part->offset + part->size) <= part->offset) {
419 printf("%s%d: partition (%s) size too big\n",
420 MTD_DEV_TYPE(id->type), id->num, part->name);
421 return 1;
422 }
423
424 if (part->offset + part->size > id->size) {
425 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
426 return 1;
427 }
428
429 if (id->type == MTD_DEV_TYPE_NAND)
430 return part_validate_nand(id, part);
431 else if (id->type == MTD_DEV_TYPE_NOR)
432 return part_validate_nor(id, part);
433 else
434 DEBUGF("part_validate: invalid dev type\n");
435
436 return 1;
437}
438
439/**
440 * Delete selected partition from the partion list of the specified device.
441 *
442 * @param dev device to delete partition from
443 * @param part partition to delete
444 * @return 0 on success, 1 otherwise
445 */
446static int part_del(struct mtd_device *dev, struct part_info *part)
447{
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200448 u8 current_save_needed = 0;
449
Wolfgang Denk47f57792005-08-08 01:03:24 +0200450 /* if there is only one partition, remove whole device */
451 if (dev->num_parts == 1)
452 return device_del(dev);
453
454 /* otherwise just delete this partition */
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200455
Wolfgang Denk47f57792005-08-08 01:03:24 +0200456 if (dev == current_dev) {
457 /* we are modyfing partitions for the current device,
458 * update current */
459 struct part_info *curr_pi;
460 curr_pi = jffs2_part_info(current_dev, current_partnum);
461
462 if (curr_pi) {
463 if (curr_pi == part) {
464 printf("current partition deleted, resetting current to 0\n");
465 current_partnum = 0;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200466 } else if (part->offset <= curr_pi->offset) {
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200467 current_partnum--;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200468 }
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200469 current_save_needed = 1;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200470 }
471 }
472
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100473#ifdef CFG_NAND_LEGACY
Wolfgang Denk47f57792005-08-08 01:03:24 +0200474 jffs2_free_cache(part);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200475#endif
Wolfgang Denk47f57792005-08-08 01:03:24 +0200476 list_del(&part->link);
477 free(part);
478 dev->num_parts--;
479
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200480 if (current_save_needed > 0)
481 current_save();
482 else
483 index_partitions();
484
Wolfgang Denk47f57792005-08-08 01:03:24 +0200485 return 0;
486}
487
488/**
489 * Delete all partitions from parts head list, free memory.
490 *
491 * @param head list of partitions to delete
492 */
493static void part_delall(struct list_head *head)
494{
495 struct list_head *entry, *n;
496 struct part_info *part_tmp;
497
498 /* clean tmp_list and free allocated memory */
499 list_for_each_safe(entry, n, head) {
500 part_tmp = list_entry(entry, struct part_info, link);
501
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100502#ifdef CFG_NAND_LEGACY
Wolfgang Denk47f57792005-08-08 01:03:24 +0200503 jffs2_free_cache(part_tmp);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200504#endif
Wolfgang Denk47f57792005-08-08 01:03:24 +0200505 list_del(entry);
506 free(part_tmp);
507 }
508}
509
510/**
511 * Add new partition to the supplied partition list. Make sure partitions are
512 * sorted by offset in ascending order.
513 *
514 * @param head list this partition is to be added to
515 * @param new partition to be added
516 */
517static int part_sort_add(struct mtd_device *dev, struct part_info *part)
518{
519 struct list_head *entry;
520 struct part_info *new_pi, *curr_pi;
521
522 /* link partition to parrent dev */
523 part->dev = dev;
524
525 if (list_empty(&dev->parts)) {
526 DEBUGF("part_sort_add: list empty\n");
527 list_add(&part->link, &dev->parts);
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200528 dev->num_parts++;
529 index_partitions();
Wolfgang Denk47f57792005-08-08 01:03:24 +0200530 return 0;
531 }
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200532
Wolfgang Denk47f57792005-08-08 01:03:24 +0200533 new_pi = list_entry(&part->link, struct part_info, link);
534
535 /* get current partition info if we are updating current device */
536 curr_pi = NULL;
537 if (dev == current_dev)
538 curr_pi = jffs2_part_info(current_dev, current_partnum);
539
540 list_for_each(entry, &dev->parts) {
541 struct part_info *pi;
542
543 pi = list_entry(entry, struct part_info, link);
544
545 /* be compliant with kernel cmdline, allow only one partition at offset zero */
546 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
547 printf("cannot add second partition at offset 0\n");
548 return 1;
549 }
550
551 if (new_pi->offset <= pi->offset) {
552 list_add_tail(&part->link, entry);
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200553 dev->num_parts++;
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200554
Wolfgang Denk47f57792005-08-08 01:03:24 +0200555 if (curr_pi && (pi->offset <= curr_pi->offset)) {
556 /* we are modyfing partitions for the current
557 * device, update current */
558 current_partnum++;
559 current_save();
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200560 } else {
561 index_partitions();
Wolfgang Denk47f57792005-08-08 01:03:24 +0200562 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200563 return 0;
564 }
565 }
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200566
Wolfgang Denk47f57792005-08-08 01:03:24 +0200567 list_add_tail(&part->link, &dev->parts);
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200568 dev->num_parts++;
569 index_partitions();
Wolfgang Denk47f57792005-08-08 01:03:24 +0200570 return 0;
571}
572
573/**
574 * Add provided partition to the partition list of a given device.
575 *
576 * @param dev device to which partition is added
577 * @param part partition to be added
578 * @return 0 on success, 1 otherwise
579 */
580static int part_add(struct mtd_device *dev, struct part_info *part)
581{
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200582 /* verify alignment and size */
Wolfgang Denk47f57792005-08-08 01:03:24 +0200583 if (part_validate(dev->id, part) != 0)
584 return 1;
585
586 /* partition is ok, add it to the list */
587 if (part_sort_add(dev, part) != 0)
588 return 1;
589
Wolfgang Denk47f57792005-08-08 01:03:24 +0200590 return 0;
591}
592
593/**
594 * Parse one partition definition, allocate memory and return pointer to this
595 * location in retpart.
596 *
597 * @param partdef pointer to the partition definition string i.e. <part-def>
598 * @param ret output pointer to next char after parse completes (output)
599 * @param retpart pointer to the allocated partition (output)
600 * @return 0 on success, 1 otherwise
601 */
602static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
603{
604 struct part_info *part;
605 unsigned long size;
606 unsigned long offset;
607 const char *name;
608 int name_len;
609 unsigned int mask_flags;
610 const char *p;
611
612 p = partdef;
613 *retpart = NULL;
614 *ret = NULL;
615
616 /* fetch the partition size */
617 if (*p == '-') {
618 /* assign all remaining space to this partition */
619 DEBUGF("'-': remaining size assigned\n");
620 size = SIZE_REMAINING;
621 p++;
622 } else {
623 size = memsize_parse(p, &p);
624 if (size < MIN_PART_SIZE) {
625 printf("partition size too small (%lx)\n", size);
626 return 1;
627 }
628 }
629
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200630 /* check for offset */
Wolfgang Denk47f57792005-08-08 01:03:24 +0200631 offset = OFFSET_NOT_SPECIFIED;
632 if (*p == '@') {
633 p++;
634 offset = memsize_parse(p, &p);
635 }
636
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200637 /* now look for the name */
Wolfgang Denk47f57792005-08-08 01:03:24 +0200638 if (*p == '(') {
639 name = ++p;
640 if ((p = strchr(name, ')')) == NULL) {
641 printf("no closing ) found in partition name\n");
642 return 1;
643 }
644 name_len = p - name + 1;
645 if ((name_len - 1) == 0) {
646 printf("empty partition name\n");
647 return 1;
648 }
649 p++;
650 } else {
651 /* 0x00000000@0x00000000 */
652 name_len = 22;
653 name = NULL;
654 }
655
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200656 /* test for options */
Wolfgang Denk47f57792005-08-08 01:03:24 +0200657 mask_flags = 0;
658 if (strncmp(p, "ro", 2) == 0) {
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100659 mask_flags |= MTD_WRITEABLE_CMD;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200660 p += 2;
661 }
662
663 /* check for next partition definition */
664 if (*p == ',') {
665 if (size == SIZE_REMAINING) {
666 *ret = NULL;
667 printf("no partitions allowed after a fill-up partition\n");
668 return 1;
669 }
670 *ret = ++p;
671 } else if ((*p == ';') || (*p == '\0')) {
672 *ret = p;
673 } else {
674 printf("unexpected character '%c' at the end of partition\n", *p);
675 *ret = NULL;
676 return 1;
677 }
678
679 /* allocate memory */
680 part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
681 if (!part) {
682 printf("out of memory\n");
683 return 1;
684 }
685 memset(part, 0, sizeof(struct part_info) + name_len);
686 part->size = size;
687 part->offset = offset;
688 part->mask_flags = mask_flags;
689 part->name = (char *)(part + 1);
690
691 if (name) {
692 /* copy user provided name */
693 strncpy(part->name, name, name_len - 1);
694 part->auto_name = 0;
695 } else {
696 /* auto generated name in form of size@offset */
697 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
698 part->auto_name = 1;
699 }
700
701 part->name[name_len - 1] = '\0';
702 INIT_LIST_HEAD(&part->link);
703
704 DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
705 part->name, part->size,
706 part->offset, part->mask_flags);
707
708 *retpart = part;
709 return 0;
710}
711#endif/* #ifdef CONFIG_JFFS2_CMDLINE */
712
713/**
714 * Check device number to be within valid range for given device type.
715 *
716 * @param dev device to validate
717 * @return 0 if device is valid, 1 otherwise
718 */
719static int device_validate(u8 type, u8 num, u32 *size)
720{
721 if (type == MTD_DEV_TYPE_NOR) {
722#if (CONFIG_COMMANDS & CFG_CMD_FLASH)
723 if (num < CFG_MAX_FLASH_BANKS) {
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200724 extern flash_info_t flash_info[];
Wolfgang Denk47f57792005-08-08 01:03:24 +0200725 *size = flash_info[num].size;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200726
Wolfgang Denk47f57792005-08-08 01:03:24 +0200727 return 0;
728 }
729
730 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
731 MTD_DEV_TYPE(type), num, CFG_MAX_FLASH_BANKS - 1);
732#else
733 printf("support for FLASH devices not present\n");
734#endif
735 } else if (type == MTD_DEV_TYPE_NAND) {
736#if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
737 if (num < CFG_MAX_NAND_DEVICE) {
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100738#ifndef CFG_NAND_LEGACY
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200739 *size = nand_info[num].size;
740#else
Wolfgang Denk47f57792005-08-08 01:03:24 +0200741 extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
742 *size = nand_dev_desc[num].totlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200743#endif
Wolfgang Denk47f57792005-08-08 01:03:24 +0200744 return 0;
745 }
746
747 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
748 MTD_DEV_TYPE(type), num, CFG_MAX_NAND_DEVICE - 1);
749#else
750 printf("support for NAND devices not present\n");
751#endif
752 }
753
754 return 1;
755}
756
757#ifdef CONFIG_JFFS2_CMDLINE
758/**
759 * Delete all mtd devices from a supplied devices list, free memory allocated for
760 * each device and delete all device partitions.
761 *
762 * @return 0 on success, 1 otherwise
763 */
764static int device_delall(struct list_head *head)
765{
766 struct list_head *entry, *n;
767 struct mtd_device *dev_tmp;
768
769 /* clean devices list */
770 list_for_each_safe(entry, n, head) {
771 dev_tmp = list_entry(entry, struct mtd_device, link);
772 list_del(entry);
773 part_delall(&dev_tmp->parts);
774 free(dev_tmp);
775 }
776 INIT_LIST_HEAD(&devices);
777
778 return 0;
779}
780
781/**
782 * If provided device exists it's partitions are deleted, device is removed
783 * from device list and device memory is freed.
784 *
785 * @param dev device to be deleted
786 * @return 0 on success, 1 otherwise
787 */
788static int device_del(struct mtd_device *dev)
789{
790 part_delall(&dev->parts);
791 list_del(&dev->link);
792 free(dev);
793
794 if (dev == current_dev) {
795 /* we just deleted current device */
796 if (list_empty(&devices)) {
797 current_dev = NULL;
798 } else {
799 /* reset first partition from first dev from the
800 * devices list as current */
801 current_dev = list_entry(devices.next, struct mtd_device, link);
802 current_partnum = 0;
803 }
804 current_save();
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200805 return 0;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200806 }
807
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200808 index_partitions();
Wolfgang Denk47f57792005-08-08 01:03:24 +0200809 return 0;
810}
811
812/**
813 * Search global device list and return pointer to the device of type and num
814 * specified.
815 *
816 * @param type device type
817 * @param num device number
818 * @return NULL if requested device does not exist
819 */
820static struct mtd_device* device_find(u8 type, u8 num)
821{
822 struct list_head *entry;
823 struct mtd_device *dev_tmp;
824
825 list_for_each(entry, &devices) {
826 dev_tmp = list_entry(entry, struct mtd_device, link);
827
828 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
829 return dev_tmp;
830 }
831
832 return NULL;
833}
834
835/**
836 * Add specified device to the global device list.
837 *
838 * @param dev device to be added
839 */
840static void device_add(struct mtd_device *dev)
841{
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200842 u8 current_save_needed = 0;
843
Wolfgang Denk47f57792005-08-08 01:03:24 +0200844 if (list_empty(&devices)) {
845 current_dev = dev;
846 current_partnum = 0;
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200847 current_save_needed = 1;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200848 }
849
850 list_add_tail(&dev->link, &devices);
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200851
852 if (current_save_needed > 0)
853 current_save();
854 else
855 index_partitions();
Wolfgang Denk47f57792005-08-08 01:03:24 +0200856}
857
858/**
859 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
860 * return pointer to the device structure.
861 *
862 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
863 * @param ret output pointer to next char after parse completes (output)
864 * @param retdev pointer to the allocated device (output)
865 * @return 0 on success, 1 otherwise
866 */
867static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
868{
869 struct mtd_device *dev;
870 struct part_info *part;
871 struct mtdids *id;
872 const char *mtd_id;
873 unsigned int mtd_id_len;
874 const char *p, *pend;
875 LIST_HEAD(tmp_list);
876 struct list_head *entry, *n;
877 u16 num_parts;
878 u32 offset;
879 int err = 1;
880
881 p = mtd_dev;
882 *retdev = NULL;
883 *ret = NULL;
884
885 DEBUGF("===device_parse===\n");
886
887 /* fetch <mtd-id> */
888 mtd_id = p;
889 if (!(p = strchr(mtd_id, ':'))) {
890 printf("no <mtd-id> identifier\n");
891 return 1;
892 }
893 mtd_id_len = p - mtd_id + 1;
894 p++;
895
896 /* verify if we have a valid device specified */
897 if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
898 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
899 return 1;
900 }
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200901
902 DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
Wolfgang Denk47f57792005-08-08 01:03:24 +0200903 id->type, MTD_DEV_TYPE(id->type),
904 id->num, id->mtd_id);
905 pend = strchr(p, ';');
906 DEBUGF("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
907
908
909 /* parse partitions */
910 num_parts = 0;
911
912 offset = 0;
913 if ((dev = device_find(id->type, id->num)) != NULL) {
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200914 /* if device already exists start at the end of the last partition */
Wolfgang Denk47f57792005-08-08 01:03:24 +0200915 part = list_entry(dev->parts.prev, struct part_info, link);
916 offset = part->offset + part->size;
917 }
918
919 while (p && (*p != '\0') && (*p != ';')) {
920 err = 1;
921 if ((part_parse(p, &p, &part) != 0) || (!part))
922 break;
923
924 /* calculate offset when not specified */
925 if (part->offset == OFFSET_NOT_SPECIFIED)
926 part->offset = offset;
927 else
928 offset = part->offset;
929
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200930 /* verify alignment and size */
Wolfgang Denk47f57792005-08-08 01:03:24 +0200931 if (part_validate(id, part) != 0)
932 break;
933
934 offset += part->size;
935
936 /* partition is ok, add it to the list */
937 list_add_tail(&part->link, &tmp_list);
938 num_parts++;
939 err = 0;
940 }
941 if (err == 1) {
942 part_delall(&tmp_list);
943 return 1;
944 }
945
946 if (num_parts == 0) {
947 printf("no partitions for device %s%d (%s)\n",
948 MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
949 return 1;
950 }
951
952 DEBUGF("\ntotal partitions: %d\n", num_parts);
953
954 /* check for next device presence */
955 if (p) {
956 if (*p == ';') {
957 *ret = ++p;
958 } else if (*p == '\0') {
959 *ret = p;
960 } else {
961 printf("unexpected character '%c' at the end of device\n", *p);
962 *ret = NULL;
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200963 return 1;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200964 }
965 }
966
967 /* allocate memory for mtd_device structure */
968 if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
969 printf("out of memory\n");
970 return 1;
971 }
972 memset(dev, 0, sizeof(struct mtd_device));
973 dev->id = id;
Marian Balakowiczfe1444a2005-09-11 15:27:12 +0200974 dev->num_parts = 0; /* part_sort_add increments num_parts */
Wolfgang Denk47f57792005-08-08 01:03:24 +0200975 INIT_LIST_HEAD(&dev->parts);
976 INIT_LIST_HEAD(&dev->link);
977
978 /* move partitions from tmp_list to dev->parts */
979 list_for_each_safe(entry, n, &tmp_list) {
980 part = list_entry(entry, struct part_info, link);
981 list_del(entry);
982 if (part_sort_add(dev, part) != 0) {
983 device_del(dev);
984 return 1;
985 }
986 }
987
988 *retdev = dev;
989
990 DEBUGF("===\n\n");
991 return 0;
992}
993
994/**
995 * Initialize global device list.
996 *
997 * @return 0 on success, 1 otherwise
998 */
999static int devices_init(void)
1000{
1001 last_parts[0] = '\0';
1002 current_dev = NULL;
1003 current_save();
1004
1005 return device_delall(&devices);
1006}
1007
1008/*
1009 * Search global mtdids list and find id of requested type and number.
1010 *
1011 * @return pointer to the id if it exists, NULL otherwise
1012 */
1013static struct mtdids* id_find(u8 type, u8 num)
1014{
1015 struct list_head *entry;
1016 struct mtdids *id;
Wolfgang Denkeb95c852005-08-10 15:14:32 +02001017
Wolfgang Denk47f57792005-08-08 01:03:24 +02001018 list_for_each(entry, &mtdids) {
1019 id = list_entry(entry, struct mtdids, link);
1020
1021 if ((id->type == type) && (id->num == num))
1022 return id;
1023 }
1024
1025 return NULL;
1026}
1027
1028/**
Wolfgang Denkeb95c852005-08-10 15:14:32 +02001029 * Search global mtdids list and find id of a requested mtd_id.
Wolfgang Denk47f57792005-08-08 01:03:24 +02001030 *
1031 * Note: first argument is not null terminated.
1032 *
1033 * @param mtd_id string containing requested mtd_id
1034 * @param mtd_id_len length of supplied mtd_id
1035 * @return pointer to the id if it exists, NULL otherwise
1036 */
1037static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
1038{
1039 struct list_head *entry;
1040 struct mtdids *id;
Wolfgang Denkeb95c852005-08-10 15:14:32 +02001041
Wolfgang Denk47f57792005-08-08 01:03:24 +02001042 DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1043 mtd_id_len, mtd_id, mtd_id_len);
1044
1045 list_for_each(entry, &mtdids) {
1046 id = list_entry(entry, struct mtdids, link);
1047
1048 DEBUGF("entry: '%s' (len = %d)\n",
1049 id->mtd_id, strlen(id->mtd_id));
1050
1051 if (mtd_id_len != strlen(id->mtd_id))
1052 continue;
1053 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1054 return id;
1055 }
1056
1057 return NULL;
1058}
1059#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1060
1061/**
1062 * Parse device id string <dev-id> := 'nand'|'nor'<dev-num>, return device
1063 * type and number.
1064 *
1065 * @param id string describing device id
1066 * @param ret_id output pointer to next char after parse completes (output)
1067 * @param dev_type parsed device type (output)
1068 * @param dev_num parsed device number (output)
1069 * @return 0 on success, 1 otherwise
1070 */
1071int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
1072{
1073 const char *p = id;
1074
1075 *dev_type = 0;
1076 if (strncmp(p, "nand", 4) == 0) {
1077 *dev_type = MTD_DEV_TYPE_NAND;
1078 p += 4;
1079 } else if (strncmp(p, "nor", 3) == 0) {
1080 *dev_type = MTD_DEV_TYPE_NOR;
1081 p += 3;
1082 } else {
1083 printf("incorrect device type in %s\n", id);
1084 return 1;
1085 }
1086
1087 if (!isdigit(*p)) {
1088 printf("incorrect device number in %s\n", id);
1089 return 1;
1090 }
1091
1092 *dev_num = simple_strtoul(p, (char **)&p, 0);
1093 if (ret_id)
1094 *ret_id = p;
1095 return 0;
1096}
1097
1098#ifdef CONFIG_JFFS2_CMDLINE
1099/**
1100 * Process all devices and generate corresponding mtdparts string describing
1101 * all partitions on all devices.
1102 *
1103 * @param buf output buffer holding generated mtdparts string (output)
1104 * @param buflen buffer size
1105 * @return 0 on success, 1 otherwise
1106 */
1107static int generate_mtdparts(char *buf, u32 buflen)
1108{
1109 struct list_head *pentry, *dentry;
1110 struct mtd_device *dev;
1111 struct part_info *part, *prev_part;
1112 char *p = buf;
1113 char tmpbuf[32];
1114 u32 size, offset, len, part_cnt;
1115 u32 maxlen = buflen - 1;
1116
1117 DEBUGF("--- generate_mtdparts ---\n");
1118
1119 if (list_empty(&devices)) {
1120 buf[0] = '\0';
1121 return 0;
1122 }
Wolfgang Denkeb95c852005-08-10 15:14:32 +02001123
Wolfgang Denk47f57792005-08-08 01:03:24 +02001124 sprintf(p, "mtdparts=");
1125 p += 9;
1126
1127 list_for_each(dentry, &devices) {
1128 dev = list_entry(dentry, struct mtd_device, link);
Wolfgang Denkeb95c852005-08-10 15:14:32 +02001129
Wolfgang Denk47f57792005-08-08 01:03:24 +02001130 /* copy mtd_id */
1131 len = strlen(dev->id->mtd_id) + 1;
1132 if (len > maxlen)
1133 goto cleanup;
1134 memcpy(p, dev->id->mtd_id, len - 1);
1135 p += len - 1;
1136 *(p++) = ':';
1137 maxlen -= len;
1138
1139 /* format partitions */
1140 prev_part = NULL;
1141 part_cnt = 0;
1142 list_for_each(pentry, &dev->parts) {
1143 part = list_entry(pentry, struct part_info, link);
1144 size = part->size;
1145 offset = part->offset;
1146 part_cnt++;
1147
1148 /* partition size */
1149 memsize_format(tmpbuf, size);
1150 len = strlen(tmpbuf);
1151 if (len > maxlen)
1152 goto cleanup;
1153 memcpy(p, tmpbuf, len);
1154 p += len;
1155 maxlen -= len;
Wolfgang Denkeb95c852005-08-10 15:14:32 +02001156
1157
Wolfgang Denk47f57792005-08-08 01:03:24 +02001158 /* add offset only when there is a gap between
1159 * partitions */
1160 if ((!prev_part && (offset != 0)) ||
1161 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1162
1163 memsize_format(tmpbuf, offset);
1164 len = strlen(tmpbuf) + 1;
1165 if (len > maxlen)
1166 goto cleanup;
1167 *(p++) = '@';
1168 memcpy(p, tmpbuf, len - 1);
1169 p += len - 1;
1170 maxlen -= len;
1171 }
1172
1173 /* copy name only if user supplied */
1174 if(!part->auto_name) {
1175 len = strlen(part->name) + 2;
1176 if (len > maxlen)
1177 goto cleanup;
1178
1179 *(p++) = '(';
1180 memcpy(p, part->name, len - 2);
1181 p += len - 2;
1182 *(p++) = ')';
1183 maxlen -= len;
1184 }
Wolfgang Denkeb95c852005-08-10 15:14:32 +02001185
Wolfgang Denk47f57792005-08-08 01:03:24 +02001186 /* ro mask flag */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +01001187 if (part->mask_flags && MTD_WRITEABLE_CMD) {
Wolfgang Denk47f57792005-08-08 01:03:24 +02001188 len = 2;
1189 if (len > maxlen)
1190 goto cleanup;
1191 *(p++) = 'r';
1192 *(p++) = 'o';
1193 maxlen -= 2;
1194 }
1195
1196 /* print ',' separator if there are other partitions
1197 * following */
1198 if (dev->num_parts > part_cnt) {
1199 if (1 > maxlen)
1200 goto cleanup;
1201 *(p++) = ',';
1202 maxlen--;
1203 }
1204 prev_part = part;
1205 }
1206 /* print ';' separator if there are other devices following */
1207 if (dentry->next != &devices) {
1208 if (1 > maxlen)
1209 goto cleanup;
1210 *(p++) = ';';
1211 maxlen--;
1212 }
1213 }
1214
1215 /* we still have at least one char left, as we decremented maxlen at
1216 * the begining */
1217 *p = '\0';
1218
1219 return 0;
1220
1221cleanup:
1222 last_parts[0] = '\0';
1223 return 1;
1224}
1225
1226/**
1227 * Call generate_mtdparts to process all devices and generate corresponding
1228 * mtdparts string, save it in mtdparts environment variable.
1229 *
1230 * @param buf output buffer holding generated mtdparts string (output)
1231 * @param buflen buffer size
1232 * @return 0 on success, 1 otherwise
1233 */
1234static int generate_mtdparts_save(char *buf, u32 buflen)
1235{
1236 int ret;
1237
1238 ret = generate_mtdparts(buf, buflen);
1239
1240 if ((buf[0] != '\0') && (ret == 0))
1241 setenv("mtdparts", buf);
1242 else
1243 setenv("mtdparts", NULL);
1244
1245 return ret;
1246}
1247
1248/**
1249 * Format and print out a partition list for each device from global device
1250 * list.
1251 */
1252static void list_partitions(void)
1253{
1254 struct list_head *dentry, *pentry;
1255 struct part_info *part;
1256 struct mtd_device *dev;
1257 int part_num;
1258
1259 DEBUGF("\n---list_partitions---\n");
1260 list_for_each(dentry, &devices) {
1261 dev = list_entry(dentry, struct mtd_device, link);
1262 printf("\ndevice %s%d <%s>, # parts = %d\n",
1263 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1264 dev->id->mtd_id, dev->num_parts);
1265 printf(" #: name\t\t\tsize\t\toffset\t\tmask_flags\n");
Wolfgang Denkeb95c852005-08-10 15:14:32 +02001266
Wolfgang Denk47f57792005-08-08 01:03:24 +02001267 /* list partitions for given device */
1268 part_num = 0;
1269 list_for_each(pentry, &dev->parts) {
1270 part = list_entry(pentry, struct part_info, link);
Wolfgang Denk872622d2006-09-13 10:23:06 +02001271 printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
Wolfgang Denk47f57792005-08-08 01:03:24 +02001272 part_num, part->name, part->size,
1273 part->offset, part->mask_flags);
1274
1275 part_num++;
1276 }
1277 }
1278 if (list_empty(&devices))
1279 printf("no partitions defined\n");
1280
1281 /* current_dev is not NULL only when we have non empty device list */
1282 if (current_dev) {
1283 part = jffs2_part_info(current_dev, current_partnum);
1284 if (part) {
1285 printf("\nactive partition: %s%d,%d - (%s) 0x%08lx @ 0x%08lx\n",
1286 MTD_DEV_TYPE(current_dev->id->type),
1287 current_dev->id->num, current_partnum,
1288 part->name, part->size, part->offset);
1289 } else {
1290 printf("could not get current partition info\n\n");
1291 }
1292 }
1293
1294 printf("\ndefaults:\n");
1295 printf("mtdids : %s\n", mtdids_default);
1296 printf("mtdparts: %s\n", mtdparts_default);
1297}
1298
1299/**
1300 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1301 * corresponding device and verify partition number.
wdenkc6097192002-11-03 00:24:07 +00001302 *
Stefan Roese198b23e2006-10-28 15:55:52 +02001303 * @param id string describing device and partition or partition name
Wolfgang Denk47f57792005-08-08 01:03:24 +02001304 * @param dev pointer to the requested device (output)
1305 * @param part_num verified partition number (output)
1306 * @param part pointer to requested partition (output)
1307 * @return 0 on success, 1 otherwise
1308 */
1309int find_dev_and_part(const char *id, struct mtd_device **dev,
1310 u8 *part_num, struct part_info **part)
1311{
Stefan Roese198b23e2006-10-28 15:55:52 +02001312 struct list_head *dentry, *pentry;
Wolfgang Denk47f57792005-08-08 01:03:24 +02001313 u8 type, dnum, pnum;
1314 const char *p;
1315
1316 DEBUGF("--- find_dev_and_part ---\nid = %s\n", id);
1317
Stefan Roese198b23e2006-10-28 15:55:52 +02001318 list_for_each(dentry, &devices) {
1319 *part_num = 0;
1320 *dev = list_entry(dentry, struct mtd_device, link);
1321 list_for_each(pentry, &(*dev)->parts) {
1322 *part = list_entry(pentry, struct part_info, link);
1323 if (strcmp((*part)->name, id) == 0)
1324 return 0;
1325 (*part_num)++;
1326 }
1327 }
1328
Wolfgang Denk47f57792005-08-08 01:03:24 +02001329 p = id;
1330 *dev = NULL;
1331 *part = NULL;
1332 *part_num = 0;
1333
1334 if (id_parse(p, &p, &type, &dnum) != 0)
1335 return 1;
1336
1337 if ((*p++ != ',') || (*p == '\0')) {
1338 printf("no partition number specified\n");
1339 return 1;
1340 }
1341 pnum = simple_strtoul(p, (char **)&p, 0);
1342 if (*p != '\0') {
1343 printf("unexpected trailing character '%c'\n", *p);
1344 return 1;
1345 }
Wolfgang Denkeb95c852005-08-10 15:14:32 +02001346
Wolfgang Denk47f57792005-08-08 01:03:24 +02001347 if ((*dev = device_find(type, dnum)) == NULL) {
1348 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1349 return 1;
1350 }
1351
1352 if ((*part = jffs2_part_info(*dev, pnum)) == NULL) {
1353 printf("no such partition\n");
1354 *dev = NULL;
1355 return 1;
1356 }
1357
1358 *part_num = pnum;
1359
1360 return 0;
1361}
1362
1363/**
1364 * Find and delete partition. For partition id format see find_dev_and_part().
wdenkc6097192002-11-03 00:24:07 +00001365 *
Wolfgang Denk47f57792005-08-08 01:03:24 +02001366 * @param id string describing device and partition
1367 * @return 0 on success, 1 otherwise
1368 */
1369static int delete_partition(const char *id)
1370{
1371 u8 pnum;
1372 struct mtd_device *dev;
1373 struct part_info *part;
1374
1375 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1376
1377 DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n",
1378 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1379 part->name, part->size, part->offset);
1380
1381 if (part_del(dev, part) != 0)
1382 return 1;
1383
1384 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1385 printf("generated mtdparts too long, reseting to null\n");
1386 return 1;
1387 }
1388 return 0;
1389 }
1390
1391 printf("partition %s not found\n", id);
1392 return 1;
1393}
1394
1395/**
1396 * Accept character string describing mtd partitions and call device_parse()
1397 * for each entry. Add created devices to the global devices list.
wdenkc6097192002-11-03 00:24:07 +00001398 *
Wolfgang Denk47f57792005-08-08 01:03:24 +02001399 * @param mtdparts string specifing mtd partitions
1400 * @return 0 on success, 1 otherwise
wdenkc6097192002-11-03 00:24:07 +00001401 */
Wolfgang Denk47f57792005-08-08 01:03:24 +02001402static int parse_mtdparts(const char *const mtdparts)
1403{
1404 const char *p = mtdparts;
1405 struct mtd_device *dev;
1406 int err = 1;
wdenkc6097192002-11-03 00:24:07 +00001407
Wolfgang Denk47f57792005-08-08 01:03:24 +02001408 DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1409
1410 /* delete all devices and partitions */
1411 if (devices_init() != 0) {
1412 printf("could not initialise device list\n");
1413 return err;
1414 }
1415
1416 /* re-read 'mtdparts' variable, devices_init may be updating env */
1417 p = getenv("mtdparts");
Wolfgang Denkeb95c852005-08-10 15:14:32 +02001418
Wolfgang Denk47f57792005-08-08 01:03:24 +02001419 if (strncmp(p, "mtdparts=", 9) != 0) {
1420 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1421 return err;
1422 }
1423 p += 9;
1424
1425 while (p && (*p != '\0')) {
1426 err = 1;
1427 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1428 break;
1429
1430 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1431 dev->id->num, dev->id->mtd_id);
1432
1433 /* check if parsed device is already on the list */
1434 if (device_find(dev->id->type, dev->id->num) != NULL) {
1435 printf("device %s%d redefined, please correct mtdparts variable\n",
1436 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1437 break;
1438 }
1439
1440 list_add_tail(&dev->link, &devices);
1441 err = 0;
1442 }
1443 if (err == 1) {
1444 device_delall(&devices);
1445 return 1;
1446 }
1447
1448 return 0;
1449}
1450
1451/**
1452 * Parse provided string describing mtdids mapping (see file header for mtdids
1453 * variable format). Allocate memory for each entry and add all found entries
1454 * to the global mtdids list.
1455 *
1456 * @param ids mapping string
1457 * @return 0 on success, 1 otherwise
wdenkc6097192002-11-03 00:24:07 +00001458 */
Wolfgang Denk47f57792005-08-08 01:03:24 +02001459static int parse_mtdids(const char *const ids)
1460{
1461 const char *p = ids;
1462 const char *mtd_id;
1463 int mtd_id_len;
1464 struct mtdids *id;
1465 struct list_head *entry, *n;
1466 struct mtdids *id_tmp;
1467 u8 type, num;
1468 u32 size;
1469 int ret = 1;
wdenkc6097192002-11-03 00:24:07 +00001470
Wolfgang Denk47f57792005-08-08 01:03:24 +02001471 DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
wdenkae65f502004-01-03 21:24:46 +00001472
Wolfgang Denk47f57792005-08-08 01:03:24 +02001473 /* clean global mtdids list */
1474 list_for_each_safe(entry, n, &mtdids) {
1475 id_tmp = list_entry(entry, struct mtdids, link);
1476 DEBUGF("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
1477 list_del(entry);
1478 free(id_tmp);
1479 }
1480 last_ids[0] = '\0';
1481 INIT_LIST_HEAD(&mtdids);
wdenkae65f502004-01-03 21:24:46 +00001482
Wolfgang Denk47f57792005-08-08 01:03:24 +02001483 while(p && (*p != '\0')) {
1484
1485 ret = 1;
1486 /* parse 'nor'|'nand'<dev-num> */
1487 if (id_parse(p, &p, &type, &num) != 0)
1488 break;
1489
1490 if (*p != '=') {
1491 printf("mtdids: incorrect <dev-num>\n");
1492 break;
1493 }
1494 p++;
1495
1496 /* check if requested device exists */
1497 if (device_validate(type, num, &size) != 0)
1498 return 1;
1499
1500 /* locate <mtd-id> */
1501 mtd_id = p;
1502 if ((p = strchr(mtd_id, ',')) != NULL) {
1503 mtd_id_len = p - mtd_id + 1;
1504 p++;
1505 } else {
1506 mtd_id_len = strlen(mtd_id) + 1;
1507 }
1508 if (mtd_id_len == 0) {
1509 printf("mtdids: no <mtd-id> identifier\n");
1510 break;
1511 }
wdenkad276f22004-01-04 16:28:35 +00001512
Wolfgang Denk47f57792005-08-08 01:03:24 +02001513 /* check if this id is already on the list */
1514 int double_entry = 0;
1515 list_for_each(entry, &mtdids) {
1516 id_tmp = list_entry(entry, struct mtdids, link);
1517 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1518 double_entry = 1;
1519 break;
1520 }
1521 }
1522 if (double_entry) {
1523 printf("device id %s%d redefined, please correct mtdids variable\n",
1524 MTD_DEV_TYPE(type), num);
1525 break;
1526 }
wdenkc6097192002-11-03 00:24:07 +00001527
Wolfgang Denk47f57792005-08-08 01:03:24 +02001528 /* allocate mtdids structure */
1529 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1530 printf("out of memory\n");
1531 break;
1532 }
1533 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1534 id->num = num;
1535 id->type = type;
1536 id->size = size;
1537 id->mtd_id = (char *)(id + 1);
1538 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1539 id->mtd_id[mtd_id_len - 1] = '\0';
1540 INIT_LIST_HEAD(&id->link);
wdenkc6097192002-11-03 00:24:07 +00001541
Wolfgang Denk47f57792005-08-08 01:03:24 +02001542 DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
1543 MTD_DEV_TYPE(id->type), id->num,
1544 id->size, id->mtd_id);
wdenkda04a8b2004-08-02 23:22:59 +00001545
Wolfgang Denk47f57792005-08-08 01:03:24 +02001546 list_add_tail(&id->link, &mtdids);
1547 ret = 0;
1548 }
1549 if (ret == 1) {
1550 /* clean mtdids list and free allocated memory */
1551 list_for_each_safe(entry, n, &mtdids) {
1552 id_tmp = list_entry(entry, struct mtdids, link);
1553 list_del(entry);
1554 free(id_tmp);
1555 }
1556 return 1;
1557 }
wdenkc6097192002-11-03 00:24:07 +00001558
Wolfgang Denk47f57792005-08-08 01:03:24 +02001559 return 0;
1560}
wdenk8886a662004-04-18 19:43:36 +00001561
Wolfgang Denk47f57792005-08-08 01:03:24 +02001562/**
1563 * Parse and initialize global mtdids mapping and create global
1564 * device/partition list.
1565 *
1566 * @return 0 on success, 1 otherwise
1567 */
1568int mtdparts_init(void)
wdenkc6097192002-11-03 00:24:07 +00001569{
Wolfgang Denk47f57792005-08-08 01:03:24 +02001570 static int initialized = 0;
1571 const char *ids, *parts;
1572 const char *current_partition;
1573 int ids_changed;
1574 char tmp_ep[PARTITION_MAXLEN];
1575
1576 DEBUGF("\n---mtdparts_init---\n");
1577 if (!initialized) {
1578 INIT_LIST_HEAD(&mtdids);
1579 INIT_LIST_HEAD(&devices);
1580 memset(last_ids, 0, MTDIDS_MAXLEN);
1581 memset(last_parts, 0, MTDPARTS_MAXLEN);
1582 memset(last_partition, 0, PARTITION_MAXLEN);
1583 initialized = 1;
1584 }
1585
1586 /* get variables */
1587 ids = getenv("mtdids");
1588 parts = getenv("mtdparts");
1589 current_partition = getenv("partition");
1590
1591 /* save it for later parsing, cannot rely on current partition pointer
1592 * as 'partition' variable may be updated during init */
1593 tmp_ep[0] = '\0';
1594 if (current_partition)
1595 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1596
1597 DEBUGF("last_ids : %s\n", last_ids);
1598 DEBUGF("env_ids : %s\n", ids);
1599 DEBUGF("last_parts: %s\n", last_parts);
1600 DEBUGF("env_parts : %s\n\n", parts);
1601
1602 DEBUGF("last_partition : %s\n", last_partition);
1603 DEBUGF("env_partition : %s\n", current_partition);
1604
1605 /* if mtdids varible is empty try to use defaults */
1606 if (!ids) {
1607 if (mtdids_default) {
1608 DEBUGF("mtdids variable not defined, using default\n");
1609 ids = mtdids_default;
1610 setenv("mtdids", (char *)ids);
1611 } else {
1612 printf("mtdids not defined, no default present\n");
1613 return 1;
1614 }
1615 }
1616 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1617 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1618 return 1;
1619 }
wdenkc6097192002-11-03 00:24:07 +00001620
Wolfgang Denk47f57792005-08-08 01:03:24 +02001621 /* do no try to use defaults when mtdparts variable is not defined,
1622 * just check the length */
1623 if (!parts)
1624 printf("mtdparts variable not set, see 'help mtdparts'\n");
wdenkc6097192002-11-03 00:24:07 +00001625
Wolfgang Denk47f57792005-08-08 01:03:24 +02001626 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1627 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1628 return 1;
1629 }
1630
1631 /* check if we have already parsed those mtdids */
1632 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1633 ids_changed = 0;
1634 } else {
1635 ids_changed = 1;
1636
1637 if (parse_mtdids(ids) != 0) {
Marian Balakowiczfe1444a2005-09-11 15:27:12 +02001638 devices_init();
Wolfgang Denk47f57792005-08-08 01:03:24 +02001639 return 1;
1640 }
1641
1642 /* ok it's good, save new ids */
1643 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1644 }
1645
1646 /* parse partitions if either mtdparts or mtdids were updated */
1647 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1648 if (parse_mtdparts(parts) != 0)
1649 return 1;
1650
1651 if (list_empty(&devices)) {
1652 printf("mtdparts_init: no valid partitions\n");
1653 return 1;
1654 }
1655
1656 /* ok it's good, save new parts */
1657 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1658
1659 /* reset first partition from first dev from the list as current */
1660 current_dev = list_entry(devices.next, struct mtd_device, link);
1661 current_partnum = 0;
1662 current_save();
1663
1664 DEBUGF("mtdparts_init: current_dev = %s%d, current_partnum = %d\n",
1665 MTD_DEV_TYPE(current_dev->id->type),
1666 current_dev->id->num, current_partnum);
1667 }
1668
1669 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1670 if (!parts && (last_parts[0] != '\0'))
1671 return devices_init();
1672
1673 /* do not process current partition if mtdparts variable is null */
1674 if (!parts)
1675 return 0;
1676
1677 /* is current partition set in environment? if so, use it */
1678 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1679 struct part_info *p;
1680 struct mtd_device *cdev;
1681 u8 pnum;
1682
1683 DEBUGF("--- getting current partition: %s\n", tmp_ep);
1684
1685 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
1686 current_dev = cdev;
1687 current_partnum = pnum;
1688 current_save();
1689 }
1690 } else if (getenv("partition") == NULL) {
1691 DEBUGF("no partition variable set, setting...\n");
1692 current_save();
1693 }
1694
1695 return 0;
1696}
1697#else /* #ifdef CONFIG_JFFS2_CMDLINE */
1698/*
1699 * 'Static' version of command line mtdparts_init() routine. Single partition on
1700 * a single device configuration.
1701 */
1702
1703/**
1704 * Parse and initialize global mtdids mapping and create global
Wolfgang Denkeb95c852005-08-10 15:14:32 +02001705 * device/partition list.
Wolfgang Denk47f57792005-08-08 01:03:24 +02001706 *
1707 * @return 0 on success, 1 otherwise
1708 */
1709int mtdparts_init(void)
1710{
1711 static int initialized = 0;
1712 u32 size;
1713 char *dev_name;
1714
1715 DEBUGF("\n---mtdparts_init---\n");
1716 if (!initialized) {
Wolfgang Denkc0d487b2005-08-09 21:41:20 +02001717 struct mtdids *id;
1718 struct part_info *part;
1719
Wolfgang Denk47f57792005-08-08 01:03:24 +02001720 initialized = 1;
1721 current_dev = (struct mtd_device *)
1722 malloc(sizeof(struct mtd_device) +
1723 sizeof(struct part_info) +
1724 sizeof(struct mtdids));
1725 if (!current_dev) {
1726 printf("out of memory\n");
1727 return 1;
1728 }
1729 memset(current_dev, 0, sizeof(struct mtd_device) +
1730 sizeof(struct part_info) + sizeof(struct mtdids));
1731
Wolfgang Denkc0d487b2005-08-09 21:41:20 +02001732 id = (struct mtdids *)(current_dev + 1);
1733 part = (struct part_info *)(id + 1);
wdenkc6097192002-11-03 00:24:07 +00001734
Wolfgang Denk47f57792005-08-08 01:03:24 +02001735 /* id */
1736 id->mtd_id = "single part";
wdenkc6097192002-11-03 00:24:07 +00001737
Wolfgang Denk47f57792005-08-08 01:03:24 +02001738#if defined(CONFIG_JFFS2_DEV)
1739 dev_name = CONFIG_JFFS2_DEV;
wdenkc6097192002-11-03 00:24:07 +00001740#else
Wolfgang Denk47f57792005-08-08 01:03:24 +02001741 dev_name = "nor0";
wdenkc6097192002-11-03 00:24:07 +00001742#endif
1743
Wolfgang Denk47f57792005-08-08 01:03:24 +02001744 if ((id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
1745 (device_validate(id->type, id->num, &size) != 0)) {
1746 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
1747 free(current_dev);
1748 return 1;
1749 }
1750 id->size = size;
1751 INIT_LIST_HEAD(&id->link);
1752
1753 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
1754 id->type, id->num, id->size, id->mtd_id);
1755
1756 /* partition */
1757 part->name = "static";
1758 part->auto_name = 0;
1759
1760#if defined(CONFIG_JFFS2_PART_SIZE)
1761 part->size = CONFIG_JFFS2_PART_SIZE;
1762#else
1763 part->size = SIZE_REMAINING;
1764#endif
wdenkc6097192002-11-03 00:24:07 +00001765
Wolfgang Denk47f57792005-08-08 01:03:24 +02001766#if defined(CONFIG_JFFS2_PART_OFFSET)
1767 part->offset = CONFIG_JFFS2_PART_OFFSET;
1768#else
1769 part->offset = 0x00000000;
wdenkc6097192002-11-03 00:24:07 +00001770#endif
1771
Wolfgang Denk47f57792005-08-08 01:03:24 +02001772 part->dev = current_dev;
1773 INIT_LIST_HEAD(&part->link);
1774
1775 /* recalculate size if needed */
1776 if (part->size == SIZE_REMAINING)
1777 part->size = id->size - part->offset;
wdenkc6097192002-11-03 00:24:07 +00001778
Wolfgang Denk47f57792005-08-08 01:03:24 +02001779 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
1780 part->name, part->size, part->offset);
1781
1782 /* device */
1783 current_dev->id = id;
1784 INIT_LIST_HEAD(&current_dev->link);
1785 current_dev->num_parts = 1;
1786 INIT_LIST_HEAD(&current_dev->parts);
1787 list_add(&part->link, &current_dev->parts);
wdenk8886a662004-04-18 19:43:36 +00001788 }
Wolfgang Denk47f57792005-08-08 01:03:24 +02001789
wdenk8886a662004-04-18 19:43:36 +00001790 return 0;
1791}
Wolfgang Denk47f57792005-08-08 01:03:24 +02001792#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
wdenk8886a662004-04-18 19:43:36 +00001793
Wolfgang Denk47f57792005-08-08 01:03:24 +02001794/**
1795 * Return pointer to the partition of a requested number from a requested
1796 * device.
1797 *
1798 * @param dev device that is to be searched for a partition
1799 * @param part_num requested partition number
1800 * @return pointer to the part_info, NULL otherwise
1801 */
1802static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
wdenk8886a662004-04-18 19:43:36 +00001803{
Wolfgang Denk47f57792005-08-08 01:03:24 +02001804 struct list_head *entry;
1805 struct part_info *part;
1806 int num;
wdenk8886a662004-04-18 19:43:36 +00001807
Wolfgang Denk47f57792005-08-08 01:03:24 +02001808 if (!dev)
1809 return NULL;
wdenk8886a662004-04-18 19:43:36 +00001810
Wolfgang Denk47f57792005-08-08 01:03:24 +02001811 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
1812 part_num, MTD_DEV_TYPE(dev->id->type),
1813 dev->id->num, dev->id->mtd_id);
wdenk8886a662004-04-18 19:43:36 +00001814
Wolfgang Denk47f57792005-08-08 01:03:24 +02001815 if (part_num >= dev->num_parts) {
1816 printf("invalid partition number %d for device %s%d (%s)\n",
1817 part_num, MTD_DEV_TYPE(dev->id->type),
1818 dev->id->num, dev->id->mtd_id);
1819 return NULL;
1820 }
wdenk8886a662004-04-18 19:43:36 +00001821
Wolfgang Denk47f57792005-08-08 01:03:24 +02001822 /* locate partition number, return it */
1823 num = 0;
1824 list_for_each(entry, &dev->parts) {
1825 part = list_entry(entry, struct part_info, link);
1826
1827 if (part_num == num++) {
1828 return part;
1829 }
wdenkc6097192002-11-03 00:24:07 +00001830 }
Wolfgang Denk47f57792005-08-08 01:03:24 +02001831
1832 return NULL;
wdenkc6097192002-11-03 00:24:07 +00001833}
wdenk8886a662004-04-18 19:43:36 +00001834
Wolfgang Denk47f57792005-08-08 01:03:24 +02001835/***************************************************/
1836/* U-boot commands */
1837/***************************************************/
wdenkae65f502004-01-03 21:24:46 +00001838
Wolfgang Denk47f57792005-08-08 01:03:24 +02001839/**
1840 * Routine implementing fsload u-boot command. This routine tries to load
1841 * a requested file from jffs2/cramfs filesystem on a current partition.
1842 *
1843 * @param cmdtp command internal data
1844 * @param flag command flag
1845 * @param argc number of arguments supplied to the command
1846 * @param argv arguments list
1847 * @return 0 on success, 1 otherwise
1848 */
1849int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenkc6097192002-11-03 00:24:07 +00001850{
wdenkf2140d52004-07-01 16:30:44 +00001851 char *fsname;
wdenk3c711762004-06-09 13:37:52 +00001852 char *filename;
wdenkf2140d52004-07-01 16:30:44 +00001853 int size;
1854 struct part_info *part;
1855 ulong offset = load_addr;
wdenk3c711762004-06-09 13:37:52 +00001856
1857 /* pre-set Boot file name */
1858 if ((filename = getenv("bootfile")) == NULL) {
1859 filename = "uImage";
1860 }
1861
wdenkc6097192002-11-03 00:24:07 +00001862 if (argc == 2) {
1863 filename = argv[1];
1864 }
1865 if (argc == 3) {
1866 offset = simple_strtoul(argv[1], NULL, 16);
wdenk42c05472004-03-23 22:14:11 +00001867 load_addr = offset;
wdenkc6097192002-11-03 00:24:07 +00001868 filename = argv[2];
1869 }
1870
Wolfgang Denk47f57792005-08-08 01:03:24 +02001871 /* make sure we are in sync with env variables */
1872 if (mtdparts_init() !=0)
1873 return 1;
1874
1875 if ((part = jffs2_part_info(current_dev, current_partnum))){
wdenkc6097192002-11-03 00:24:07 +00001876
wdenkae65f502004-01-03 21:24:46 +00001877 /* check partition type for cramfs */
1878 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
1879 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
1880
1881 if (cramfs_check(part)) {
1882 size = cramfs_load ((char *) offset, part, filename);
1883 } else {
1884 /* if this is not cramfs assume jffs2 */
1885 size = jffs2_1pass_load((char *)offset, part, filename);
1886 }
wdenkc6097192002-11-03 00:24:07 +00001887
1888 if (size > 0) {
1889 char buf[10];
wdenkae65f502004-01-03 21:24:46 +00001890 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
1891 fsname, size, offset);
wdenkc6097192002-11-03 00:24:07 +00001892 sprintf(buf, "%x", size);
1893 setenv("filesize", buf);
1894 } else {
wdenkae65f502004-01-03 21:24:46 +00001895 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
wdenkc6097192002-11-03 00:24:07 +00001896 }
1897
1898 return !(size > 0);
1899 }
Wolfgang Denk79117cd2005-08-16 09:32:45 +02001900 return 1;
wdenkc6097192002-11-03 00:24:07 +00001901}
1902
Wolfgang Denk47f57792005-08-08 01:03:24 +02001903/**
1904 * Routine implementing u-boot ls command which lists content of a given
1905 * directory on a current partition.
1906 *
1907 * @param cmdtp command internal data
1908 * @param flag command flag
1909 * @param argc number of arguments supplied to the command
1910 * @param argv arguments list
1911 * @return 0 on success, 1 otherwise
1912 */
1913int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenkc6097192002-11-03 00:24:07 +00001914{
wdenk8d414a72005-06-10 10:00:19 +00001915 char *filename = "/";
wdenkc6097192002-11-03 00:24:07 +00001916 int ret;
1917 struct part_info *part;
1918
1919 if (argc == 2)
1920 filename = argv[1];
1921
Wolfgang Denk47f57792005-08-08 01:03:24 +02001922 /* make sure we are in sync with env variables */
1923 if (mtdparts_init() !=0)
1924 return 1;
1925
1926 if ((part = jffs2_part_info(current_dev, current_partnum))){
wdenkc6097192002-11-03 00:24:07 +00001927
wdenkae65f502004-01-03 21:24:46 +00001928 /* check partition type for cramfs */
1929 if (cramfs_check(part)) {
1930 ret = cramfs_ls (part, filename);
1931 } else {
1932 /* if this is not cramfs assume jffs2 */
1933 ret = jffs2_1pass_ls(part, filename);
1934 }
wdenkc6097192002-11-03 00:24:07 +00001935
Wolfgang Denk79117cd2005-08-16 09:32:45 +02001936 return ret ? 0 : 1;
wdenkc6097192002-11-03 00:24:07 +00001937 }
Wolfgang Denk79117cd2005-08-16 09:32:45 +02001938 return 1;
wdenkc6097192002-11-03 00:24:07 +00001939}
1940
Wolfgang Denk47f57792005-08-08 01:03:24 +02001941/**
1942 * Routine implementing u-boot fsinfo command. This routine prints out
1943 * miscellaneous filesystem informations/statistics.
1944 *
1945 * @param cmdtp command internal data
1946 * @param flag command flag
1947 * @param argc number of arguments supplied to the command
1948 * @param argv arguments list
1949 * @return 0 on success, 1 otherwise
1950 */
1951int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenkc6097192002-11-03 00:24:07 +00001952{
wdenkc6097192002-11-03 00:24:07 +00001953 struct part_info *part;
wdenkae65f502004-01-03 21:24:46 +00001954 char *fsname;
1955 int ret;
wdenkc6097192002-11-03 00:24:07 +00001956
Wolfgang Denk47f57792005-08-08 01:03:24 +02001957 /* make sure we are in sync with env variables */
1958 if (mtdparts_init() !=0)
1959 return 1;
Wolfgang Denkeb95c852005-08-10 15:14:32 +02001960
Wolfgang Denk47f57792005-08-08 01:03:24 +02001961 if ((part = jffs2_part_info(current_dev, current_partnum))){
wdenkae65f502004-01-03 21:24:46 +00001962
1963 /* check partition type for cramfs */
1964 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
1965 printf("### filesystem type is %s\n", fsname);
wdenkc6097192002-11-03 00:24:07 +00001966
wdenkae65f502004-01-03 21:24:46 +00001967 if (cramfs_check(part)) {
1968 ret = cramfs_info (part);
1969 } else {
1970 /* if this is not cramfs assume jffs2 */
1971 ret = jffs2_1pass_info(part);
1972 }
wdenkc6097192002-11-03 00:24:07 +00001973
Wolfgang Denk79117cd2005-08-16 09:32:45 +02001974 return ret ? 0 : 1;
wdenkc6097192002-11-03 00:24:07 +00001975 }
Wolfgang Denk79117cd2005-08-16 09:32:45 +02001976 return 1;
wdenkc6097192002-11-03 00:24:07 +00001977}
1978
Wolfgang Denk47f57792005-08-08 01:03:24 +02001979/* command line only */
1980#ifdef CONFIG_JFFS2_CMDLINE
1981/**
1982 * Routine implementing u-boot chpart command. Sets new current partition based
1983 * on the user supplied partition id. For partition id format see find_dev_and_part().
1984 *
1985 * @param cmdtp command internal data
1986 * @param flag command flag
1987 * @param argc number of arguments supplied to the command
1988 * @param argv arguments list
1989 * @return 0 on success, 1 otherwise
1990 */
1991int do_jffs2_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenkc6097192002-11-03 00:24:07 +00001992{
Wolfgang Denk47f57792005-08-08 01:03:24 +02001993/* command line only */
1994 struct mtd_device *dev;
1995 struct part_info *part;
1996 u8 pnum;
wdenkc6097192002-11-03 00:24:07 +00001997
Wolfgang Denk47f57792005-08-08 01:03:24 +02001998 if (mtdparts_init() !=0)
1999 return 1;
2000
2001 if (argc < 2) {
2002 printf("no partition id specified\n");
2003 return 1;
2004 }
2005
2006 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
2007 return 1;
2008
2009 current_dev = dev;
2010 current_partnum = pnum;
2011 current_save();
2012
2013 printf("partition changed to %s%d,%d\n",
2014 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
2015
2016 return 0;
2017}
2018
2019/**
2020 * Routine implementing u-boot mtdparts command. Initialize/update default global
2021 * partition list and process user partition request (list, add, del).
2022 *
2023 * @param cmdtp command internal data
2024 * @param flag command flag
2025 * @param argc number of arguments supplied to the command
2026 * @param argv arguments list
2027 * @return 0 on success, 1 otherwise
2028 */
2029int do_jffs2_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2030{
2031 if (argc == 2) {
2032 if (strcmp(argv[1], "default") == 0) {
2033 setenv("mtdids", (char *)mtdids_default);
2034 setenv("mtdparts", (char *)mtdparts_default);
2035 setenv("partition", NULL);
2036
2037 mtdparts_init();
2038 return 0;
2039 } else if (strcmp(argv[1], "delall") == 0) {
2040 /* this may be the first run, initialize lists if needed */
2041 mtdparts_init();
2042
2043 setenv("mtdparts", NULL);
wdenkc6097192002-11-03 00:24:07 +00002044
Wolfgang Denk47f57792005-08-08 01:03:24 +02002045 /* devices_init() calls current_save() */
2046 return devices_init();
2047 }
wdenkc6097192002-11-03 00:24:07 +00002048 }
2049
Wolfgang Denk47f57792005-08-08 01:03:24 +02002050 /* make sure we are in sync with env variables */
2051 if (mtdparts_init() != 0)
2052 return 1;
2053
2054 if (argc == 1) {
2055 list_partitions();
wdenkc6097192002-11-03 00:24:07 +00002056 return 0;
2057 }
Wolfgang Denkeb95c852005-08-10 15:14:32 +02002058
Wolfgang Denk47f57792005-08-08 01:03:24 +02002059 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
2060 if (((argc == 5) || (argc == 6)) && (strcmp(argv[1], "add") == 0)) {
2061#define PART_ADD_DESC_MAXLEN 64
2062 char tmpbuf[PART_ADD_DESC_MAXLEN];
2063 u8 type, num, len;
2064 struct mtd_device *dev;
2065 struct mtd_device *dev_tmp;
2066 struct mtdids *id;
2067 struct part_info *p;
wdenkc6097192002-11-03 00:24:07 +00002068
Wolfgang Denk47f57792005-08-08 01:03:24 +02002069 if (id_parse(argv[2], NULL, &type, &num) != 0)
2070 return 1;
wdenkc6097192002-11-03 00:24:07 +00002071
Wolfgang Denk47f57792005-08-08 01:03:24 +02002072 if ((id = id_find(type, num)) == NULL) {
2073 printf("no such device %s defined in mtdids variable\n", argv[2]);
2074 return 1;
2075 }
wdenka0ebde52004-09-08 22:03:11 +00002076
Wolfgang Denk47f57792005-08-08 01:03:24 +02002077 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
2078 len += strlen(argv[3]); /* size@offset */
2079 len += strlen(argv[4]) + 2; /* '(' name ')' */
2080 if (argv[5] && (strlen(argv[5]) == 2))
2081 len += 2; /* 'ro' */
wdenk57b2d802003-06-27 21:31:46 +00002082
Wolfgang Denk47f57792005-08-08 01:03:24 +02002083 if (len >= PART_ADD_DESC_MAXLEN) {
2084 printf("too long partition description\n");
2085 return 1;
2086 }
2087 sprintf(tmpbuf, "%s:%s(%s)%s",
Wolfgang Denkeb95c852005-08-10 15:14:32 +02002088 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
Wolfgang Denk47f57792005-08-08 01:03:24 +02002089 DEBUGF("add tmpbuf: %s\n", tmpbuf);
2090
2091 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
2092 return 1;
wdenk57b2d802003-06-27 21:31:46 +00002093
Wolfgang Denk47f57792005-08-08 01:03:24 +02002094 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
2095 dev->id->num, dev->id->mtd_id);
2096
2097 if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) {
2098 device_add(dev);
2099 } else {
2100 /* merge new partition with existing ones*/
2101 p = list_entry(dev->parts.next, struct part_info, link);
2102 if (part_add(dev_tmp, p) != 0) {
2103 device_del(dev);
2104 return 1;
2105 }
2106 }
2107
2108 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
2109 printf("generated mtdparts too long, reseting to null\n");
2110 return 1;
2111 }
2112
2113 return 0;
2114 }
2115
2116 /* mtdparts del part-id */
2117 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
2118 DEBUGF("del: part-id = %s\n", argv[2]);
2119
2120 return delete_partition(argv[2]);
2121 }
2122
2123 printf ("Usage:\n%s\n", cmdtp->usage);
2124 return 1;
2125}
2126#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2127
2128/***************************************************/
wdenkf287a242003-07-01 21:06:45 +00002129U_BOOT_CMD(
2130 fsload, 3, 0, do_jffs2_fsload,
wdenk6568eaa2004-06-09 12:42:26 +00002131 "fsload\t- load binary file from a filesystem image\n",
wdenk57b2d802003-06-27 21:31:46 +00002132 "[ off ] [ filename ]\n"
2133 " - load binary file from flash bank\n"
2134 " with offset 'off'\n"
2135);
Wolfgang Denk47f57792005-08-08 01:03:24 +02002136U_BOOT_CMD(
2137 ls, 2, 1, do_jffs2_ls,
2138 "ls\t- list files in a directory (default /)\n",
2139 "[ directory ]\n"
2140 " - list files in a directory.\n"
2141);
wdenk57b2d802003-06-27 21:31:46 +00002142
wdenkf287a242003-07-01 21:06:45 +00002143U_BOOT_CMD(
2144 fsinfo, 1, 1, do_jffs2_fsinfo,
wdenk6568eaa2004-06-09 12:42:26 +00002145 "fsinfo\t- print information about filesystems\n",
wdenk57b2d802003-06-27 21:31:46 +00002146 " - print information about filesystems\n"
2147);
2148
Wolfgang Denk47f57792005-08-08 01:03:24 +02002149#ifdef CONFIG_JFFS2_CMDLINE
wdenkf287a242003-07-01 21:06:45 +00002150U_BOOT_CMD(
Wolfgang Denk47f57792005-08-08 01:03:24 +02002151 chpart, 2, 0, do_jffs2_chpart,
2152 "chpart\t- change active partition\n",
2153 "part-id\n"
2154 " - change active partition (e.g. part-id = nand0,1)\n"
2155);
2156
2157U_BOOT_CMD(
2158 mtdparts, 6, 0, do_jffs2_mtdparts,
2159 "mtdparts- define flash/nand partitions\n",
2160 "\n"
2161 " - list partition table\n"
2162 "mtdparts delall\n"
2163 " - delete all partitions\n"
2164 "mtdparts del part-id\n"
2165 " - delete partition (e.g. part-id = nand0,1)\n"
2166 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2167 " - add partition\n"
2168 "mtdparts default\n"
2169 " - reset partition table to defaults\n\n"
2170 "-----\n\n"
2171 "this command uses three environment variables:\n\n"
2172 "'partition' - keeps current partition identifier\n\n"
2173 "partition := <part-id>\n"
2174 "<part-id> := <dev-id>,part_num\n\n"
2175 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2176 "mtdids=<idmap>[,<idmap>,...]\n\n"
2177 "<idmap> := <dev-id>=<mtd-id>\n"
2178 "<dev-id> := 'nand'|'nor'<dev-num>\n"
2179 "<dev-num> := mtd device number, 0...\n"
2180 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2181 "'mtdparts' - partition list\n\n"
2182 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2183 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2184 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2185 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2186 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2187 "<offset> := partition start offset within the device\n"
2188 "<name> := '(' NAME ')'\n"
2189 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)\n"
wdenk57b2d802003-06-27 21:31:46 +00002190);
Wolfgang Denk47f57792005-08-08 01:03:24 +02002191#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2192
2193/***************************************************/
wdenk57b2d802003-06-27 21:31:46 +00002194
wdenkc6097192002-11-03 00:24:07 +00002195#endif /* CFG_CMD_JFFS2 */