blob: c031d8034919ee9a7e902ca258e64ddf42b113da [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
Michal Simekab4b9562007-08-06 23:31:49 +020031 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenkc6097192002-11-03 00:24:07 +000032 * 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/*
Michal Simek991b0892007-09-15 00:03:35 +020088 * 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>
Wolfgang Denk700a0c62005-08-08 01:03:24 +020094#include <linux/list.h>
95#include <linux/ctype.h>
wdenkdd875c72004-01-03 21:24:46 +000096#include <cramfs/cramfs_fs.h>
97
Jon Loeligerc76fe472007-07-08 18:02:23 -050098#if defined(CONFIG_CMD_NAND)
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +020099#ifdef CONFIG_NAND_LEGACY
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100100#include <linux/mtd/nand_legacy.h>
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +0200101#else /* !CONFIG_NAND_LEGACY */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100102#include <linux/mtd/nand.h>
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200103#include <nand.h>
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +0200104#endif /* !CONFIG_NAND_LEGACY */
Jon Loeligerc76fe472007-07-08 18:02:23 -0500105#endif
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200106/* enable/disable debugging messages */
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100107#define DEBUG_JFFS
108#undef DEBUG_JFFS
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200109
Michal Simekab4b9562007-08-06 23:31:49 +0200110#ifdef DEBUG_JFFS
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200111# define DEBUGF(fmt, args...) printf(fmt ,##args)
112#else
113# define DEBUGF(fmt, args...)
114#endif
115
116/* special size referring to all the remaining space in a partition */
117#define SIZE_REMAINING 0xFFFFFFFF
118
119/* special offset value, it is used when not provided by user
120 *
121 * this value is used temporarily during parsing, later such offests
122 * are recalculated */
123#define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
124
125/* minimum partition size */
126#define MIN_PART_SIZE 4096
127
128/* this flag needs to be set in part_info struct mask_flags
129 * field for read-only partitions */
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100130#define MTD_WRITEABLE_CMD 1
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200131
132#ifdef CONFIG_JFFS2_CMDLINE
133/* default values for mtdids and mtdparts variables */
134#if defined(MTDIDS_DEFAULT)
135static const char *const mtdids_default = MTDIDS_DEFAULT;
136#else
137#warning "MTDIDS_DEFAULT not defined!"
138static const char *const mtdids_default = NULL;
139#endif
140
141#if defined(MTDPARTS_DEFAULT)
142static const char *const mtdparts_default = MTDPARTS_DEFAULT;
143#else
144#warning "MTDPARTS_DEFAULT not defined!"
145static const char *const mtdparts_default = NULL;
146#endif
147
148/* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
149#define MTDIDS_MAXLEN 128
150#define MTDPARTS_MAXLEN 512
151#define PARTITION_MAXLEN 16
152static char last_ids[MTDIDS_MAXLEN];
153static char last_parts[MTDPARTS_MAXLEN];
154static char last_partition[PARTITION_MAXLEN];
155
156/* low level jffs2 cache cleaning routine */
157extern void jffs2_free_cache(struct part_info *part);
158
159/* mtdids mapping list, filled by parse_ids() */
160struct list_head mtdids;
161
162/* device/partition list, parse_cmdline() parses into here */
163struct list_head devices;
164#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
165
166/* current active device and partition number */
167static struct mtd_device *current_dev = NULL;
168static u8 current_partnum = 0;
169
Harald Weltef540c422007-12-19 14:09:58 +0100170#if defined(CONFIG_CMD_CRAMFS)
wdenk180d3f72004-01-04 16:28:35 +0000171extern int cramfs_check (struct part_info *info);
172extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
173extern int cramfs_ls (struct part_info *info, char *filename);
174extern int cramfs_info (struct part_info *info);
Harald Weltef540c422007-12-19 14:09:58 +0100175#else
176/* defining empty macros for function names is ugly but avoids ifdef clutter
177 * all over the code */
178#define cramfs_check(x) (0)
179#define cramfs_load(x,y,z) (-1)
180#define cramfs_ls(x,y) (0)
181#define cramfs_info(x) (0)
182#endif
wdenk180d3f72004-01-04 16:28:35 +0000183
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200184static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num);
wdenkc6097192002-11-03 00:24:07 +0000185
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200186/* command line only routines */
187#ifdef CONFIG_JFFS2_CMDLINE
wdenkc6097192002-11-03 00:24:07 +0000188
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200189static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
190static int device_del(struct mtd_device *dev);
wdenk6705d812004-08-02 23:22:59 +0000191
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200192/**
Michal Simekab4b9562007-08-06 23:31:49 +0200193 * Parses a string into a number. The number stored at ptr is
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200194 * potentially suffixed with K (for kilobytes, or 1024 bytes),
195 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
Michal Simekab4b9562007-08-06 23:31:49 +0200196 * 1073741824). If the number is suffixed with K, M, or G, then
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200197 * the return value is the number multiplied by one kilobyte, one
198 * megabyte, or one gigabyte, respectively.
199 *
200 * @param ptr where parse begins
201 * @param retptr output pointer to next char after parse completes (output)
202 * @return resulting unsigned int
203 */
204static unsigned long memsize_parse (const char *const ptr, const char **retptr)
wdenkc6097192002-11-03 00:24:07 +0000205{
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200206 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
207
208 switch (**retptr) {
209 case 'G':
210 case 'g':
211 ret <<= 10;
212 case 'M':
213 case 'm':
214 ret <<= 10;
215 case 'K':
216 case 'k':
217 ret <<= 10;
218 (*retptr)++;
219 default:
220 break;
221 }
222
223 return ret;
224}
225
226/**
227 * Format string describing supplied size. This routine does the opposite job
228 * to memsize_parse(). Size in bytes is converted to string and if possible
229 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
230 *
231 * Note, that this routine does not check for buffer overflow, it's the caller
232 * who must assure enough space.
233 *
234 * @param buf output buffer
235 * @param size size to be converted to string
236 */
237static void memsize_format(char *buf, u32 size)
238{
239#define SIZE_GB ((u32)1024*1024*1024)
240#define SIZE_MB ((u32)1024*1024)
241#define SIZE_KB ((u32)1024)
242
243 if ((size % SIZE_GB) == 0)
Kim Phillips4109df62008-07-10 14:00:15 -0500244 sprintf(buf, "%ug", size/SIZE_GB);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200245 else if ((size % SIZE_MB) == 0)
Kim Phillips4109df62008-07-10 14:00:15 -0500246 sprintf(buf, "%um", size/SIZE_MB);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200247 else if (size % SIZE_KB == 0)
Kim Phillips4109df62008-07-10 14:00:15 -0500248 sprintf(buf, "%uk", size/SIZE_KB);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200249 else
Kim Phillips4109df62008-07-10 14:00:15 -0500250 sprintf(buf, "%u", size);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200251}
252
253/**
Marian Balakowicz7d454772005-09-11 15:27:12 +0200254 * This routine does global indexing of all partitions. Resulting index for
255 * current partition is saved in 'mtddevnum'. Current partition name in
256 * 'mtddevname'.
257 */
258static void index_partitions(void)
259{
260 char buf[16];
261 u16 mtddevnum;
262 struct part_info *part;
263 struct list_head *dentry;
264 struct mtd_device *dev;
265
266 DEBUGF("--- index partitions ---\n");
267
268 if (current_dev) {
269 mtddevnum = 0;
270 list_for_each(dentry, &devices) {
271 dev = list_entry(dentry, struct mtd_device, link);
272 if (dev == current_dev) {
273 mtddevnum += current_partnum;
274 sprintf(buf, "%d", mtddevnum);
275 setenv("mtddevnum", buf);
276 break;
277 }
278 mtddevnum += dev->num_parts;
279 }
280
281 part = jffs2_part_info(current_dev, current_partnum);
282 setenv("mtddevname", part->name);
283
284 DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
285 } else {
286 setenv("mtddevnum", NULL);
287 setenv("mtddevname", NULL);
288
289 DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
290 }
291}
292
293/**
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200294 * Save current device and partition in environment variable 'partition'.
295 */
296static void current_save(void)
297{
298 char buf[16];
299
300 DEBUGF("--- current_save ---\n");
301
302 if (current_dev) {
303 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_dev->id->type),
304 current_dev->id->num, current_partnum);
305
306 setenv("partition", buf);
307 strncpy(last_partition, buf, 16);
308
309 DEBUGF("=> partition %s\n", buf);
310 } else {
311 setenv("partition", NULL);
312 last_partition[0] = '\0';
313
314 DEBUGF("=> partition NULL\n");
315 }
Marian Balakowicz7d454772005-09-11 15:27:12 +0200316 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200317}
318
319/**
320 * Performs sanity check for supplied NOR flash partition. Table of existing
321 * NOR flash devices is searched and partition device is located. Alignment
322 * with the granularity of NOR flash sectors is verified.
323 *
324 * @param id of the parent device
325 * @param part partition to validate
326 * @return 0 if partition is valid, 1 otherwise
327 */
328static int part_validate_nor(struct mtdids *id, struct part_info *part)
329{
Jon Loeligerc76fe472007-07-08 18:02:23 -0500330#if defined(CONFIG_CMD_FLASH)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200331 /* info for FLASH chips */
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200332 extern flash_info_t flash_info[];
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200333 flash_info_t *flash;
334 int offset_aligned;
335 u32 end_offset;
wdenkc6097192002-11-03 00:24:07 +0000336 int i;
337
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200338 flash = &flash_info[id->num];
wdenkc6097192002-11-03 00:24:07 +0000339
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200340 offset_aligned = 0;
341 for (i = 0; i < flash->sector_count; i++) {
342 if ((flash->start[i] - flash->start[0]) == part->offset) {
343 offset_aligned = 1;
344 break;
345 }
346 }
347 if (offset_aligned == 0) {
348 printf("%s%d: partition (%s) start offset alignment incorrect\n",
349 MTD_DEV_TYPE(id->type), id->num, part->name);
350 return 1;
351 }
wdenkc6097192002-11-03 00:24:07 +0000352
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200353 end_offset = part->offset + part->size;
354 for (i = 0; i < flash->sector_count; i++) {
355 if ((flash->start[i] - flash->start[0]) == end_offset)
356 return 0;
357 }
wdenkc6097192002-11-03 00:24:07 +0000358
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200359 if (flash->size == end_offset)
360 return 0;
361
362 printf("%s%d: partition (%s) size alignment incorrect\n",
363 MTD_DEV_TYPE(id->type), id->num, part->name);
364#endif
365 return 1;
366}
367
368/**
369 * Performs sanity check for supplied NAND flash partition. Table of existing
370 * NAND flash devices is searched and partition device is located. Alignment
371 * with the granularity of nand erasesize is verified.
372 *
373 * @param id of the parent device
374 * @param part partition to validate
375 * @return 0 if partition is valid, 1 otherwise
376 */
377static int part_validate_nand(struct mtdids *id, struct part_info *part)
378{
Jon Loeligerc76fe472007-07-08 18:02:23 -0500379#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200380 /* info for NAND chips */
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200381 nand_info_t *nand;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200382
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200383 nand = &nand_info[id->num];
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200384
385 if ((unsigned long)(part->offset) % nand->erasesize) {
386 printf("%s%d: partition (%s) start offset alignment incorrect\n",
387 MTD_DEV_TYPE(id->type), id->num, part->name);
388 return 1;
389 }
390
391 if (part->size % nand->erasesize) {
392 printf("%s%d: partition (%s) size alignment incorrect\n",
393 MTD_DEV_TYPE(id->type), id->num, part->name);
394 return 1;
395 }
396
397 return 0;
wdenkc6097192002-11-03 00:24:07 +0000398#else
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200399 return 1;
wdenkc6097192002-11-03 00:24:07 +0000400#endif
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200401}
wdenkc6097192002-11-03 00:24:07 +0000402
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200403/**
404 * Performs sanity check for supplied partition. Offset and size are verified
405 * to be within valid range. Partition type is checked and either
406 * parts_validate_nor() or parts_validate_nand() is called with the argument
407 * of part.
408 *
409 * @param id of the parent device
410 * @param part partition to validate
411 * @return 0 if partition is valid, 1 otherwise
412 */
413static int part_validate(struct mtdids *id, struct part_info *part)
414{
415 if (part->size == SIZE_REMAINING)
416 part->size = id->size - part->offset;
wdenkc6097192002-11-03 00:24:07 +0000417
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200418 if (part->offset > id->size) {
Kim Phillips4109df62008-07-10 14:00:15 -0500419 printf("%s: offset %08x beyond flash size %08x\n",
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200420 id->mtd_id, part->offset, id->size);
421 return 1;
wdenkc6097192002-11-03 00:24:07 +0000422 }
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200423
424 if ((part->offset + part->size) <= part->offset) {
425 printf("%s%d: partition (%s) size too big\n",
426 MTD_DEV_TYPE(id->type), id->num, part->name);
427 return 1;
428 }
429
430 if (part->offset + part->size > id->size) {
431 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
432 return 1;
433 }
434
435 if (id->type == MTD_DEV_TYPE_NAND)
436 return part_validate_nand(id, part);
437 else if (id->type == MTD_DEV_TYPE_NOR)
438 return part_validate_nor(id, part);
439 else
440 DEBUGF("part_validate: invalid dev type\n");
441
442 return 1;
443}
444
445/**
446 * Delete selected partition from the partion list of the specified device.
447 *
448 * @param dev device to delete partition from
449 * @param part partition to delete
450 * @return 0 on success, 1 otherwise
451 */
452static int part_del(struct mtd_device *dev, struct part_info *part)
453{
Marian Balakowicz7d454772005-09-11 15:27:12 +0200454 u8 current_save_needed = 0;
455
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200456 /* if there is only one partition, remove whole device */
457 if (dev->num_parts == 1)
458 return device_del(dev);
459
460 /* otherwise just delete this partition */
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200461
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200462 if (dev == current_dev) {
463 /* we are modyfing partitions for the current device,
464 * update current */
465 struct part_info *curr_pi;
466 curr_pi = jffs2_part_info(current_dev, current_partnum);
467
468 if (curr_pi) {
469 if (curr_pi == part) {
470 printf("current partition deleted, resetting current to 0\n");
471 current_partnum = 0;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200472 } else if (part->offset <= curr_pi->offset) {
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200473 current_partnum--;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200474 }
Marian Balakowicz7d454772005-09-11 15:27:12 +0200475 current_save_needed = 1;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200476 }
477 }
478
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +0200479#ifdef CONFIG_NAND_LEGACY
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200480 jffs2_free_cache(part);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200481#endif
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200482 list_del(&part->link);
483 free(part);
484 dev->num_parts--;
485
Marian Balakowicz7d454772005-09-11 15:27:12 +0200486 if (current_save_needed > 0)
487 current_save();
488 else
489 index_partitions();
490
wdenkc6097192002-11-03 00:24:07 +0000491 return 0;
492}
wdenk998eaae2004-04-18 19:43:36 +0000493
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200494/**
495 * Delete all partitions from parts head list, free memory.
496 *
497 * @param head list of partitions to delete
498 */
499static void part_delall(struct list_head *head)
wdenk998eaae2004-04-18 19:43:36 +0000500{
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200501 struct list_head *entry, *n;
502 struct part_info *part_tmp;
wdenk998eaae2004-04-18 19:43:36 +0000503
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200504 /* clean tmp_list and free allocated memory */
505 list_for_each_safe(entry, n, head) {
506 part_tmp = list_entry(entry, struct part_info, link);
wdenk998eaae2004-04-18 19:43:36 +0000507
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +0200508#ifdef CONFIG_NAND_LEGACY
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200509 jffs2_free_cache(part_tmp);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200510#endif
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200511 list_del(entry);
512 free(part_tmp);
wdenk998eaae2004-04-18 19:43:36 +0000513 }
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200514}
515
516/**
517 * Add new partition to the supplied partition list. Make sure partitions are
518 * sorted by offset in ascending order.
519 *
520 * @param head list this partition is to be added to
521 * @param new partition to be added
522 */
523static int part_sort_add(struct mtd_device *dev, struct part_info *part)
524{
525 struct list_head *entry;
526 struct part_info *new_pi, *curr_pi;
527
528 /* link partition to parrent dev */
529 part->dev = dev;
530
531 if (list_empty(&dev->parts)) {
532 DEBUGF("part_sort_add: list empty\n");
533 list_add(&part->link, &dev->parts);
Marian Balakowicz7d454772005-09-11 15:27:12 +0200534 dev->num_parts++;
535 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200536 return 0;
537 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200538
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200539 new_pi = list_entry(&part->link, struct part_info, link);
540
541 /* get current partition info if we are updating current device */
542 curr_pi = NULL;
543 if (dev == current_dev)
544 curr_pi = jffs2_part_info(current_dev, current_partnum);
545
546 list_for_each(entry, &dev->parts) {
547 struct part_info *pi;
548
549 pi = list_entry(entry, struct part_info, link);
550
551 /* be compliant with kernel cmdline, allow only one partition at offset zero */
552 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
553 printf("cannot add second partition at offset 0\n");
554 return 1;
555 }
556
557 if (new_pi->offset <= pi->offset) {
558 list_add_tail(&part->link, entry);
Marian Balakowicz7d454772005-09-11 15:27:12 +0200559 dev->num_parts++;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200560
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200561 if (curr_pi && (pi->offset <= curr_pi->offset)) {
562 /* we are modyfing partitions for the current
563 * device, update current */
564 current_partnum++;
565 current_save();
Marian Balakowicz7d454772005-09-11 15:27:12 +0200566 } else {
567 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200568 }
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200569 return 0;
570 }
571 }
Marian Balakowicz7d454772005-09-11 15:27:12 +0200572
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200573 list_add_tail(&part->link, &dev->parts);
Marian Balakowicz7d454772005-09-11 15:27:12 +0200574 dev->num_parts++;
575 index_partitions();
wdenk998eaae2004-04-18 19:43:36 +0000576 return 0;
577}
578
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200579/**
580 * Add provided partition to the partition list of a given device.
581 *
582 * @param dev device to which partition is added
583 * @param part partition to be added
584 * @return 0 on success, 1 otherwise
585 */
586static int part_add(struct mtd_device *dev, struct part_info *part)
wdenkc6097192002-11-03 00:24:07 +0000587{
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200588 /* verify alignment and size */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200589 if (part_validate(dev->id, part) != 0)
590 return 1;
591
592 /* partition is ok, add it to the list */
593 if (part_sort_add(dev, part) != 0)
594 return 1;
595
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200596 return 0;
597}
598
599/**
600 * Parse one partition definition, allocate memory and return pointer to this
601 * location in retpart.
602 *
603 * @param partdef pointer to the partition definition string i.e. <part-def>
604 * @param ret output pointer to next char after parse completes (output)
605 * @param retpart pointer to the allocated partition (output)
606 * @return 0 on success, 1 otherwise
607 */
608static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
609{
610 struct part_info *part;
611 unsigned long size;
612 unsigned long offset;
613 const char *name;
614 int name_len;
615 unsigned int mask_flags;
616 const char *p;
617
618 p = partdef;
619 *retpart = NULL;
620 *ret = NULL;
621
622 /* fetch the partition size */
623 if (*p == '-') {
624 /* assign all remaining space to this partition */
625 DEBUGF("'-': remaining size assigned\n");
626 size = SIZE_REMAINING;
627 p++;
628 } else {
629 size = memsize_parse(p, &p);
630 if (size < MIN_PART_SIZE) {
631 printf("partition size too small (%lx)\n", size);
632 return 1;
633 }
634 }
635
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200636 /* check for offset */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200637 offset = OFFSET_NOT_SPECIFIED;
638 if (*p == '@') {
639 p++;
640 offset = memsize_parse(p, &p);
641 }
642
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200643 /* now look for the name */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200644 if (*p == '(') {
645 name = ++p;
646 if ((p = strchr(name, ')')) == NULL) {
647 printf("no closing ) found in partition name\n");
648 return 1;
649 }
650 name_len = p - name + 1;
651 if ((name_len - 1) == 0) {
652 printf("empty partition name\n");
653 return 1;
654 }
655 p++;
656 } else {
657 /* 0x00000000@0x00000000 */
658 name_len = 22;
659 name = NULL;
660 }
661
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200662 /* test for options */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200663 mask_flags = 0;
664 if (strncmp(p, "ro", 2) == 0) {
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100665 mask_flags |= MTD_WRITEABLE_CMD;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200666 p += 2;
667 }
668
669 /* check for next partition definition */
670 if (*p == ',') {
671 if (size == SIZE_REMAINING) {
672 *ret = NULL;
673 printf("no partitions allowed after a fill-up partition\n");
674 return 1;
675 }
676 *ret = ++p;
677 } else if ((*p == ';') || (*p == '\0')) {
678 *ret = p;
679 } else {
680 printf("unexpected character '%c' at the end of partition\n", *p);
681 *ret = NULL;
682 return 1;
683 }
684
Michal Simekab4b9562007-08-06 23:31:49 +0200685 /* allocate memory */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200686 part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
687 if (!part) {
688 printf("out of memory\n");
689 return 1;
690 }
691 memset(part, 0, sizeof(struct part_info) + name_len);
692 part->size = size;
693 part->offset = offset;
694 part->mask_flags = mask_flags;
695 part->name = (char *)(part + 1);
696
697 if (name) {
698 /* copy user provided name */
699 strncpy(part->name, name, name_len - 1);
700 part->auto_name = 0;
701 } else {
702 /* auto generated name in form of size@offset */
703 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
704 part->auto_name = 1;
705 }
706
707 part->name[name_len - 1] = '\0';
708 INIT_LIST_HEAD(&part->link);
709
710 DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
711 part->name, part->size,
712 part->offset, part->mask_flags);
713
714 *retpart = part;
715 return 0;
716}
717#endif/* #ifdef CONFIG_JFFS2_CMDLINE */
718
719/**
720 * Check device number to be within valid range for given device type.
721 *
722 * @param dev device to validate
723 * @return 0 if device is valid, 1 otherwise
724 */
725static int device_validate(u8 type, u8 num, u32 *size)
726{
727 if (type == MTD_DEV_TYPE_NOR) {
Jon Loeligerc76fe472007-07-08 18:02:23 -0500728#if defined(CONFIG_CMD_FLASH)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200729 if (num < CFG_MAX_FLASH_BANKS) {
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200730 extern flash_info_t flash_info[];
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200731 *size = flash_info[num].size;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200732
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200733 return 0;
734 }
735
736 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
737 MTD_DEV_TYPE(type), num, CFG_MAX_FLASH_BANKS - 1);
738#else
739 printf("support for FLASH devices not present\n");
740#endif
741 } else if (type == MTD_DEV_TYPE_NAND) {
Jon Loeligerc76fe472007-07-08 18:02:23 -0500742#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200743 if (num < CFG_MAX_NAND_DEVICE) {
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +0200744#ifndef CONFIG_NAND_LEGACY
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200745 *size = nand_info[num].size;
746#else
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200747 extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
748 *size = nand_dev_desc[num].totlen;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200749#endif
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200750 return 0;
751 }
752
753 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
754 MTD_DEV_TYPE(type), num, CFG_MAX_NAND_DEVICE - 1);
755#else
756 printf("support for NAND devices not present\n");
757#endif
758 }
759
760 return 1;
761}
762
763#ifdef CONFIG_JFFS2_CMDLINE
764/**
765 * Delete all mtd devices from a supplied devices list, free memory allocated for
766 * each device and delete all device partitions.
767 *
768 * @return 0 on success, 1 otherwise
769 */
770static int device_delall(struct list_head *head)
771{
772 struct list_head *entry, *n;
773 struct mtd_device *dev_tmp;
774
775 /* clean devices list */
776 list_for_each_safe(entry, n, head) {
777 dev_tmp = list_entry(entry, struct mtd_device, link);
778 list_del(entry);
779 part_delall(&dev_tmp->parts);
780 free(dev_tmp);
781 }
782 INIT_LIST_HEAD(&devices);
783
784 return 0;
785}
786
787/**
788 * If provided device exists it's partitions are deleted, device is removed
789 * from device list and device memory is freed.
790 *
791 * @param dev device to be deleted
792 * @return 0 on success, 1 otherwise
793 */
794static int device_del(struct mtd_device *dev)
795{
796 part_delall(&dev->parts);
797 list_del(&dev->link);
798 free(dev);
799
800 if (dev == current_dev) {
801 /* we just deleted current device */
802 if (list_empty(&devices)) {
803 current_dev = NULL;
804 } else {
805 /* reset first partition from first dev from the
806 * devices list as current */
807 current_dev = list_entry(devices.next, struct mtd_device, link);
808 current_partnum = 0;
809 }
810 current_save();
Marian Balakowicz7d454772005-09-11 15:27:12 +0200811 return 0;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200812 }
813
Marian Balakowicz7d454772005-09-11 15:27:12 +0200814 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200815 return 0;
816}
817
818/**
819 * Search global device list and return pointer to the device of type and num
820 * specified.
821 *
822 * @param type device type
823 * @param num device number
824 * @return NULL if requested device does not exist
825 */
826static struct mtd_device* device_find(u8 type, u8 num)
827{
828 struct list_head *entry;
829 struct mtd_device *dev_tmp;
830
831 list_for_each(entry, &devices) {
832 dev_tmp = list_entry(entry, struct mtd_device, link);
833
834 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
835 return dev_tmp;
836 }
837
838 return NULL;
839}
840
841/**
842 * Add specified device to the global device list.
843 *
844 * @param dev device to be added
845 */
846static void device_add(struct mtd_device *dev)
847{
Marian Balakowicz7d454772005-09-11 15:27:12 +0200848 u8 current_save_needed = 0;
849
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200850 if (list_empty(&devices)) {
851 current_dev = dev;
852 current_partnum = 0;
Marian Balakowicz7d454772005-09-11 15:27:12 +0200853 current_save_needed = 1;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200854 }
855
856 list_add_tail(&dev->link, &devices);
Marian Balakowicz7d454772005-09-11 15:27:12 +0200857
858 if (current_save_needed > 0)
859 current_save();
860 else
861 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200862}
863
864/**
865 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
866 * return pointer to the device structure.
867 *
868 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
869 * @param ret output pointer to next char after parse completes (output)
870 * @param retdev pointer to the allocated device (output)
871 * @return 0 on success, 1 otherwise
872 */
873static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
874{
875 struct mtd_device *dev;
876 struct part_info *part;
877 struct mtdids *id;
878 const char *mtd_id;
879 unsigned int mtd_id_len;
880 const char *p, *pend;
881 LIST_HEAD(tmp_list);
882 struct list_head *entry, *n;
883 u16 num_parts;
884 u32 offset;
885 int err = 1;
886
887 p = mtd_dev;
888 *retdev = NULL;
889 *ret = NULL;
890
891 DEBUGF("===device_parse===\n");
892
893 /* fetch <mtd-id> */
894 mtd_id = p;
895 if (!(p = strchr(mtd_id, ':'))) {
896 printf("no <mtd-id> identifier\n");
897 return 1;
898 }
899 mtd_id_len = p - mtd_id + 1;
900 p++;
901
902 /* verify if we have a valid device specified */
903 if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
904 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
905 return 1;
906 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200907
908 DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200909 id->type, MTD_DEV_TYPE(id->type),
910 id->num, id->mtd_id);
911 pend = strchr(p, ';');
912 DEBUGF("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
913
914
915 /* parse partitions */
916 num_parts = 0;
917
918 offset = 0;
919 if ((dev = device_find(id->type, id->num)) != NULL) {
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200920 /* if device already exists start at the end of the last partition */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200921 part = list_entry(dev->parts.prev, struct part_info, link);
922 offset = part->offset + part->size;
923 }
924
925 while (p && (*p != '\0') && (*p != ';')) {
926 err = 1;
927 if ((part_parse(p, &p, &part) != 0) || (!part))
928 break;
929
930 /* calculate offset when not specified */
931 if (part->offset == OFFSET_NOT_SPECIFIED)
932 part->offset = offset;
933 else
934 offset = part->offset;
935
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200936 /* verify alignment and size */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200937 if (part_validate(id, part) != 0)
938 break;
939
940 offset += part->size;
941
942 /* partition is ok, add it to the list */
943 list_add_tail(&part->link, &tmp_list);
944 num_parts++;
945 err = 0;
946 }
947 if (err == 1) {
948 part_delall(&tmp_list);
949 return 1;
950 }
951
952 if (num_parts == 0) {
953 printf("no partitions for device %s%d (%s)\n",
954 MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
955 return 1;
956 }
957
958 DEBUGF("\ntotal partitions: %d\n", num_parts);
959
960 /* check for next device presence */
961 if (p) {
962 if (*p == ';') {
963 *ret = ++p;
964 } else if (*p == '\0') {
965 *ret = p;
966 } else {
967 printf("unexpected character '%c' at the end of device\n", *p);
968 *ret = NULL;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200969 return 1;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200970 }
971 }
972
973 /* allocate memory for mtd_device structure */
974 if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
975 printf("out of memory\n");
976 return 1;
977 }
978 memset(dev, 0, sizeof(struct mtd_device));
979 dev->id = id;
Marian Balakowicz7d454772005-09-11 15:27:12 +0200980 dev->num_parts = 0; /* part_sort_add increments num_parts */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200981 INIT_LIST_HEAD(&dev->parts);
982 INIT_LIST_HEAD(&dev->link);
983
984 /* move partitions from tmp_list to dev->parts */
985 list_for_each_safe(entry, n, &tmp_list) {
986 part = list_entry(entry, struct part_info, link);
987 list_del(entry);
988 if (part_sort_add(dev, part) != 0) {
989 device_del(dev);
990 return 1;
991 }
992 }
993
994 *retdev = dev;
995
996 DEBUGF("===\n\n");
997 return 0;
998}
999
1000/**
1001 * Initialize global device list.
1002 *
1003 * @return 0 on success, 1 otherwise
1004 */
1005static int devices_init(void)
1006{
1007 last_parts[0] = '\0';
1008 current_dev = NULL;
1009 current_save();
1010
1011 return device_delall(&devices);
1012}
1013
1014/*
1015 * Search global mtdids list and find id of requested type and number.
1016 *
1017 * @return pointer to the id if it exists, NULL otherwise
1018 */
1019static struct mtdids* id_find(u8 type, u8 num)
1020{
1021 struct list_head *entry;
1022 struct mtdids *id;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001023
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001024 list_for_each(entry, &mtdids) {
1025 id = list_entry(entry, struct mtdids, link);
1026
1027 if ((id->type == type) && (id->num == num))
1028 return id;
1029 }
1030
1031 return NULL;
1032}
1033
1034/**
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001035 * Search global mtdids list and find id of a requested mtd_id.
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001036 *
1037 * Note: first argument is not null terminated.
1038 *
1039 * @param mtd_id string containing requested mtd_id
1040 * @param mtd_id_len length of supplied mtd_id
1041 * @return pointer to the id if it exists, NULL otherwise
1042 */
1043static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
1044{
1045 struct list_head *entry;
1046 struct mtdids *id;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001047
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001048 DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1049 mtd_id_len, mtd_id, mtd_id_len);
1050
1051 list_for_each(entry, &mtdids) {
1052 id = list_entry(entry, struct mtdids, link);
1053
1054 DEBUGF("entry: '%s' (len = %d)\n",
1055 id->mtd_id, strlen(id->mtd_id));
1056
1057 if (mtd_id_len != strlen(id->mtd_id))
1058 continue;
1059 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1060 return id;
1061 }
1062
1063 return NULL;
1064}
1065#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1066
1067/**
1068 * Parse device id string <dev-id> := 'nand'|'nor'<dev-num>, return device
1069 * type and number.
1070 *
1071 * @param id string describing device id
1072 * @param ret_id output pointer to next char after parse completes (output)
1073 * @param dev_type parsed device type (output)
1074 * @param dev_num parsed device number (output)
1075 * @return 0 on success, 1 otherwise
1076 */
1077int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
1078{
1079 const char *p = id;
1080
1081 *dev_type = 0;
1082 if (strncmp(p, "nand", 4) == 0) {
1083 *dev_type = MTD_DEV_TYPE_NAND;
1084 p += 4;
1085 } else if (strncmp(p, "nor", 3) == 0) {
1086 *dev_type = MTD_DEV_TYPE_NOR;
1087 p += 3;
1088 } else {
1089 printf("incorrect device type in %s\n", id);
1090 return 1;
1091 }
1092
1093 if (!isdigit(*p)) {
1094 printf("incorrect device number in %s\n", id);
1095 return 1;
1096 }
1097
1098 *dev_num = simple_strtoul(p, (char **)&p, 0);
1099 if (ret_id)
1100 *ret_id = p;
1101 return 0;
1102}
1103
1104#ifdef CONFIG_JFFS2_CMDLINE
1105/**
1106 * Process all devices and generate corresponding mtdparts string describing
1107 * all partitions on all devices.
1108 *
1109 * @param buf output buffer holding generated mtdparts string (output)
1110 * @param buflen buffer size
1111 * @return 0 on success, 1 otherwise
1112 */
1113static int generate_mtdparts(char *buf, u32 buflen)
1114{
1115 struct list_head *pentry, *dentry;
1116 struct mtd_device *dev;
1117 struct part_info *part, *prev_part;
1118 char *p = buf;
1119 char tmpbuf[32];
1120 u32 size, offset, len, part_cnt;
1121 u32 maxlen = buflen - 1;
1122
1123 DEBUGF("--- generate_mtdparts ---\n");
1124
1125 if (list_empty(&devices)) {
1126 buf[0] = '\0';
1127 return 0;
1128 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001129
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001130 sprintf(p, "mtdparts=");
1131 p += 9;
1132
1133 list_for_each(dentry, &devices) {
1134 dev = list_entry(dentry, struct mtd_device, link);
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001135
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001136 /* copy mtd_id */
1137 len = strlen(dev->id->mtd_id) + 1;
1138 if (len > maxlen)
1139 goto cleanup;
1140 memcpy(p, dev->id->mtd_id, len - 1);
1141 p += len - 1;
1142 *(p++) = ':';
1143 maxlen -= len;
1144
1145 /* format partitions */
1146 prev_part = NULL;
1147 part_cnt = 0;
1148 list_for_each(pentry, &dev->parts) {
1149 part = list_entry(pentry, struct part_info, link);
1150 size = part->size;
1151 offset = part->offset;
1152 part_cnt++;
1153
1154 /* partition size */
1155 memsize_format(tmpbuf, size);
1156 len = strlen(tmpbuf);
1157 if (len > maxlen)
1158 goto cleanup;
1159 memcpy(p, tmpbuf, len);
1160 p += len;
1161 maxlen -= len;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001162
1163
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001164 /* add offset only when there is a gap between
1165 * partitions */
1166 if ((!prev_part && (offset != 0)) ||
1167 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1168
1169 memsize_format(tmpbuf, offset);
1170 len = strlen(tmpbuf) + 1;
1171 if (len > maxlen)
1172 goto cleanup;
1173 *(p++) = '@';
1174 memcpy(p, tmpbuf, len - 1);
1175 p += len - 1;
1176 maxlen -= len;
1177 }
1178
1179 /* copy name only if user supplied */
1180 if(!part->auto_name) {
1181 len = strlen(part->name) + 2;
1182 if (len > maxlen)
1183 goto cleanup;
1184
1185 *(p++) = '(';
1186 memcpy(p, part->name, len - 2);
1187 p += len - 2;
1188 *(p++) = ')';
1189 maxlen -= len;
1190 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001191
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001192 /* ro mask flag */
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +01001193 if (part->mask_flags && MTD_WRITEABLE_CMD) {
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001194 len = 2;
1195 if (len > maxlen)
1196 goto cleanup;
1197 *(p++) = 'r';
1198 *(p++) = 'o';
1199 maxlen -= 2;
1200 }
1201
1202 /* print ',' separator if there are other partitions
1203 * following */
1204 if (dev->num_parts > part_cnt) {
1205 if (1 > maxlen)
1206 goto cleanup;
1207 *(p++) = ',';
1208 maxlen--;
1209 }
1210 prev_part = part;
1211 }
1212 /* print ';' separator if there are other devices following */
1213 if (dentry->next != &devices) {
1214 if (1 > maxlen)
1215 goto cleanup;
1216 *(p++) = ';';
1217 maxlen--;
1218 }
1219 }
1220
1221 /* we still have at least one char left, as we decremented maxlen at
1222 * the begining */
1223 *p = '\0';
1224
1225 return 0;
1226
1227cleanup:
1228 last_parts[0] = '\0';
1229 return 1;
1230}
1231
1232/**
1233 * Call generate_mtdparts to process all devices and generate corresponding
1234 * mtdparts string, save it in mtdparts environment variable.
1235 *
1236 * @param buf output buffer holding generated mtdparts string (output)
1237 * @param buflen buffer size
1238 * @return 0 on success, 1 otherwise
1239 */
1240static int generate_mtdparts_save(char *buf, u32 buflen)
1241{
1242 int ret;
1243
1244 ret = generate_mtdparts(buf, buflen);
1245
1246 if ((buf[0] != '\0') && (ret == 0))
1247 setenv("mtdparts", buf);
1248 else
1249 setenv("mtdparts", NULL);
1250
1251 return ret;
1252}
1253
1254/**
1255 * Format and print out a partition list for each device from global device
1256 * list.
1257 */
1258static void list_partitions(void)
1259{
1260 struct list_head *dentry, *pentry;
1261 struct part_info *part;
1262 struct mtd_device *dev;
1263 int part_num;
1264
1265 DEBUGF("\n---list_partitions---\n");
1266 list_for_each(dentry, &devices) {
1267 dev = list_entry(dentry, struct mtd_device, link);
1268 printf("\ndevice %s%d <%s>, # parts = %d\n",
1269 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1270 dev->id->mtd_id, dev->num_parts);
1271 printf(" #: name\t\t\tsize\t\toffset\t\tmask_flags\n");
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001272
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001273 /* list partitions for given device */
1274 part_num = 0;
1275 list_for_each(pentry, &dev->parts) {
1276 part = list_entry(pentry, struct part_info, link);
Wolfgang Denka87d46f2006-09-13 10:23:06 +02001277 printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001278 part_num, part->name, part->size,
1279 part->offset, part->mask_flags);
1280
1281 part_num++;
1282 }
1283 }
1284 if (list_empty(&devices))
1285 printf("no partitions defined\n");
1286
1287 /* current_dev is not NULL only when we have non empty device list */
1288 if (current_dev) {
1289 part = jffs2_part_info(current_dev, current_partnum);
1290 if (part) {
Kim Phillips4109df62008-07-10 14:00:15 -05001291 printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001292 MTD_DEV_TYPE(current_dev->id->type),
1293 current_dev->id->num, current_partnum,
1294 part->name, part->size, part->offset);
1295 } else {
1296 printf("could not get current partition info\n\n");
1297 }
1298 }
1299
1300 printf("\ndefaults:\n");
1301 printf("mtdids : %s\n", mtdids_default);
1302 printf("mtdparts: %s\n", mtdparts_default);
1303}
1304
1305/**
1306 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1307 * corresponding device and verify partition number.
1308 *
Stefan Roese856f0542006-10-28 15:55:52 +02001309 * @param id string describing device and partition or partition name
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001310 * @param dev pointer to the requested device (output)
1311 * @param part_num verified partition number (output)
1312 * @param part pointer to requested partition (output)
1313 * @return 0 on success, 1 otherwise
1314 */
1315int find_dev_and_part(const char *id, struct mtd_device **dev,
1316 u8 *part_num, struct part_info **part)
1317{
Stefan Roese856f0542006-10-28 15:55:52 +02001318 struct list_head *dentry, *pentry;
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001319 u8 type, dnum, pnum;
1320 const char *p;
1321
1322 DEBUGF("--- find_dev_and_part ---\nid = %s\n", id);
1323
Stefan Roese856f0542006-10-28 15:55:52 +02001324 list_for_each(dentry, &devices) {
1325 *part_num = 0;
1326 *dev = list_entry(dentry, struct mtd_device, link);
1327 list_for_each(pentry, &(*dev)->parts) {
1328 *part = list_entry(pentry, struct part_info, link);
1329 if (strcmp((*part)->name, id) == 0)
1330 return 0;
1331 (*part_num)++;
1332 }
1333 }
1334
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001335 p = id;
1336 *dev = NULL;
1337 *part = NULL;
1338 *part_num = 0;
1339
1340 if (id_parse(p, &p, &type, &dnum) != 0)
1341 return 1;
1342
1343 if ((*p++ != ',') || (*p == '\0')) {
1344 printf("no partition number specified\n");
1345 return 1;
1346 }
1347 pnum = simple_strtoul(p, (char **)&p, 0);
1348 if (*p != '\0') {
1349 printf("unexpected trailing character '%c'\n", *p);
1350 return 1;
1351 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001352
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001353 if ((*dev = device_find(type, dnum)) == NULL) {
1354 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1355 return 1;
1356 }
1357
1358 if ((*part = jffs2_part_info(*dev, pnum)) == NULL) {
1359 printf("no such partition\n");
1360 *dev = NULL;
1361 return 1;
1362 }
1363
1364 *part_num = pnum;
1365
1366 return 0;
1367}
1368
1369/**
1370 * Find and delete partition. For partition id format see find_dev_and_part().
1371 *
1372 * @param id string describing device and partition
1373 * @return 0 on success, 1 otherwise
1374 */
1375static int delete_partition(const char *id)
1376{
1377 u8 pnum;
1378 struct mtd_device *dev;
1379 struct part_info *part;
1380
1381 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1382
1383 DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n",
1384 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1385 part->name, part->size, part->offset);
1386
1387 if (part_del(dev, part) != 0)
1388 return 1;
1389
1390 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1391 printf("generated mtdparts too long, reseting to null\n");
1392 return 1;
1393 }
1394 return 0;
1395 }
1396
1397 printf("partition %s not found\n", id);
1398 return 1;
1399}
1400
1401/**
1402 * Accept character string describing mtd partitions and call device_parse()
1403 * for each entry. Add created devices to the global devices list.
1404 *
1405 * @param mtdparts string specifing mtd partitions
1406 * @return 0 on success, 1 otherwise
1407 */
1408static int parse_mtdparts(const char *const mtdparts)
1409{
1410 const char *p = mtdparts;
1411 struct mtd_device *dev;
1412 int err = 1;
1413
1414 DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1415
1416 /* delete all devices and partitions */
1417 if (devices_init() != 0) {
1418 printf("could not initialise device list\n");
1419 return err;
1420 }
1421
1422 /* re-read 'mtdparts' variable, devices_init may be updating env */
1423 p = getenv("mtdparts");
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001424
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001425 if (strncmp(p, "mtdparts=", 9) != 0) {
1426 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1427 return err;
1428 }
1429 p += 9;
1430
1431 while (p && (*p != '\0')) {
1432 err = 1;
1433 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1434 break;
1435
1436 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1437 dev->id->num, dev->id->mtd_id);
1438
1439 /* check if parsed device is already on the list */
1440 if (device_find(dev->id->type, dev->id->num) != NULL) {
1441 printf("device %s%d redefined, please correct mtdparts variable\n",
1442 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1443 break;
1444 }
1445
1446 list_add_tail(&dev->link, &devices);
1447 err = 0;
1448 }
1449 if (err == 1) {
1450 device_delall(&devices);
1451 return 1;
1452 }
1453
1454 return 0;
1455}
1456
1457/**
1458 * Parse provided string describing mtdids mapping (see file header for mtdids
1459 * variable format). Allocate memory for each entry and add all found entries
1460 * to the global mtdids list.
1461 *
1462 * @param ids mapping string
1463 * @return 0 on success, 1 otherwise
1464 */
1465static int parse_mtdids(const char *const ids)
1466{
1467 const char *p = ids;
1468 const char *mtd_id;
1469 int mtd_id_len;
1470 struct mtdids *id;
1471 struct list_head *entry, *n;
1472 struct mtdids *id_tmp;
1473 u8 type, num;
1474 u32 size;
1475 int ret = 1;
1476
1477 DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
1478
1479 /* clean global mtdids list */
1480 list_for_each_safe(entry, n, &mtdids) {
1481 id_tmp = list_entry(entry, struct mtdids, link);
1482 DEBUGF("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
1483 list_del(entry);
1484 free(id_tmp);
1485 }
1486 last_ids[0] = '\0';
1487 INIT_LIST_HEAD(&mtdids);
1488
1489 while(p && (*p != '\0')) {
1490
1491 ret = 1;
1492 /* parse 'nor'|'nand'<dev-num> */
1493 if (id_parse(p, &p, &type, &num) != 0)
1494 break;
1495
1496 if (*p != '=') {
1497 printf("mtdids: incorrect <dev-num>\n");
1498 break;
1499 }
1500 p++;
1501
1502 /* check if requested device exists */
1503 if (device_validate(type, num, &size) != 0)
1504 return 1;
1505
1506 /* locate <mtd-id> */
1507 mtd_id = p;
1508 if ((p = strchr(mtd_id, ',')) != NULL) {
1509 mtd_id_len = p - mtd_id + 1;
1510 p++;
1511 } else {
1512 mtd_id_len = strlen(mtd_id) + 1;
1513 }
1514 if (mtd_id_len == 0) {
1515 printf("mtdids: no <mtd-id> identifier\n");
1516 break;
1517 }
1518
1519 /* check if this id is already on the list */
1520 int double_entry = 0;
1521 list_for_each(entry, &mtdids) {
1522 id_tmp = list_entry(entry, struct mtdids, link);
1523 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1524 double_entry = 1;
1525 break;
1526 }
1527 }
1528 if (double_entry) {
1529 printf("device id %s%d redefined, please correct mtdids variable\n",
1530 MTD_DEV_TYPE(type), num);
1531 break;
1532 }
1533
1534 /* allocate mtdids structure */
1535 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1536 printf("out of memory\n");
1537 break;
1538 }
1539 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1540 id->num = num;
1541 id->type = type;
1542 id->size = size;
1543 id->mtd_id = (char *)(id + 1);
1544 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1545 id->mtd_id[mtd_id_len - 1] = '\0';
1546 INIT_LIST_HEAD(&id->link);
1547
1548 DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
1549 MTD_DEV_TYPE(id->type), id->num,
1550 id->size, id->mtd_id);
1551
1552 list_add_tail(&id->link, &mtdids);
1553 ret = 0;
1554 }
1555 if (ret == 1) {
1556 /* clean mtdids list and free allocated memory */
1557 list_for_each_safe(entry, n, &mtdids) {
1558 id_tmp = list_entry(entry, struct mtdids, link);
1559 list_del(entry);
1560 free(id_tmp);
1561 }
1562 return 1;
1563 }
1564
1565 return 0;
1566}
1567
1568/**
1569 * Parse and initialize global mtdids mapping and create global
1570 * device/partition list.
1571 *
1572 * @return 0 on success, 1 otherwise
1573 */
1574int mtdparts_init(void)
1575{
1576 static int initialized = 0;
1577 const char *ids, *parts;
1578 const char *current_partition;
1579 int ids_changed;
1580 char tmp_ep[PARTITION_MAXLEN];
1581
1582 DEBUGF("\n---mtdparts_init---\n");
1583 if (!initialized) {
1584 INIT_LIST_HEAD(&mtdids);
1585 INIT_LIST_HEAD(&devices);
1586 memset(last_ids, 0, MTDIDS_MAXLEN);
1587 memset(last_parts, 0, MTDPARTS_MAXLEN);
1588 memset(last_partition, 0, PARTITION_MAXLEN);
1589 initialized = 1;
1590 }
1591
1592 /* get variables */
1593 ids = getenv("mtdids");
1594 parts = getenv("mtdparts");
1595 current_partition = getenv("partition");
1596
1597 /* save it for later parsing, cannot rely on current partition pointer
1598 * as 'partition' variable may be updated during init */
1599 tmp_ep[0] = '\0';
1600 if (current_partition)
1601 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1602
1603 DEBUGF("last_ids : %s\n", last_ids);
1604 DEBUGF("env_ids : %s\n", ids);
1605 DEBUGF("last_parts: %s\n", last_parts);
1606 DEBUGF("env_parts : %s\n\n", parts);
1607
1608 DEBUGF("last_partition : %s\n", last_partition);
1609 DEBUGF("env_partition : %s\n", current_partition);
1610
1611 /* if mtdids varible is empty try to use defaults */
1612 if (!ids) {
1613 if (mtdids_default) {
1614 DEBUGF("mtdids variable not defined, using default\n");
1615 ids = mtdids_default;
1616 setenv("mtdids", (char *)ids);
1617 } else {
1618 printf("mtdids not defined, no default present\n");
1619 return 1;
1620 }
1621 }
1622 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1623 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1624 return 1;
1625 }
1626
1627 /* do no try to use defaults when mtdparts variable is not defined,
1628 * just check the length */
1629 if (!parts)
1630 printf("mtdparts variable not set, see 'help mtdparts'\n");
1631
1632 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1633 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1634 return 1;
1635 }
1636
1637 /* check if we have already parsed those mtdids */
1638 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1639 ids_changed = 0;
1640 } else {
1641 ids_changed = 1;
1642
1643 if (parse_mtdids(ids) != 0) {
Marian Balakowicz7d454772005-09-11 15:27:12 +02001644 devices_init();
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001645 return 1;
1646 }
1647
1648 /* ok it's good, save new ids */
1649 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1650 }
1651
1652 /* parse partitions if either mtdparts or mtdids were updated */
1653 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1654 if (parse_mtdparts(parts) != 0)
1655 return 1;
1656
1657 if (list_empty(&devices)) {
1658 printf("mtdparts_init: no valid partitions\n");
1659 return 1;
1660 }
1661
1662 /* ok it's good, save new parts */
1663 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1664
1665 /* reset first partition from first dev from the list as current */
1666 current_dev = list_entry(devices.next, struct mtd_device, link);
1667 current_partnum = 0;
1668 current_save();
1669
1670 DEBUGF("mtdparts_init: current_dev = %s%d, current_partnum = %d\n",
1671 MTD_DEV_TYPE(current_dev->id->type),
1672 current_dev->id->num, current_partnum);
1673 }
1674
1675 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1676 if (!parts && (last_parts[0] != '\0'))
1677 return devices_init();
1678
1679 /* do not process current partition if mtdparts variable is null */
1680 if (!parts)
1681 return 0;
1682
1683 /* is current partition set in environment? if so, use it */
1684 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1685 struct part_info *p;
1686 struct mtd_device *cdev;
1687 u8 pnum;
1688
1689 DEBUGF("--- getting current partition: %s\n", tmp_ep);
1690
1691 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
1692 current_dev = cdev;
1693 current_partnum = pnum;
1694 current_save();
1695 }
1696 } else if (getenv("partition") == NULL) {
1697 DEBUGF("no partition variable set, setting...\n");
1698 current_save();
1699 }
1700
1701 return 0;
1702}
1703#else /* #ifdef CONFIG_JFFS2_CMDLINE */
1704/*
1705 * 'Static' version of command line mtdparts_init() routine. Single partition on
1706 * a single device configuration.
1707 */
1708
1709/**
1710 * Parse and initialize global mtdids mapping and create global
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001711 * device/partition list.
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001712 *
1713 * @return 0 on success, 1 otherwise
1714 */
1715int mtdparts_init(void)
1716{
1717 static int initialized = 0;
1718 u32 size;
1719 char *dev_name;
1720
1721 DEBUGF("\n---mtdparts_init---\n");
1722 if (!initialized) {
Wolfgang Denkc4e0e682005-08-09 21:41:20 +02001723 struct mtdids *id;
1724 struct part_info *part;
1725
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001726 initialized = 1;
1727 current_dev = (struct mtd_device *)
1728 malloc(sizeof(struct mtd_device) +
1729 sizeof(struct part_info) +
1730 sizeof(struct mtdids));
1731 if (!current_dev) {
1732 printf("out of memory\n");
1733 return 1;
1734 }
1735 memset(current_dev, 0, sizeof(struct mtd_device) +
1736 sizeof(struct part_info) + sizeof(struct mtdids));
1737
Wolfgang Denkc4e0e682005-08-09 21:41:20 +02001738 id = (struct mtdids *)(current_dev + 1);
1739 part = (struct part_info *)(id + 1);
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001740
1741 /* id */
1742 id->mtd_id = "single part";
1743
1744#if defined(CONFIG_JFFS2_DEV)
1745 dev_name = CONFIG_JFFS2_DEV;
1746#else
1747 dev_name = "nor0";
1748#endif
1749
1750 if ((id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
1751 (device_validate(id->type, id->num, &size) != 0)) {
1752 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
1753 free(current_dev);
1754 return 1;
1755 }
1756 id->size = size;
1757 INIT_LIST_HEAD(&id->link);
1758
1759 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
1760 id->type, id->num, id->size, id->mtd_id);
1761
1762 /* partition */
1763 part->name = "static";
1764 part->auto_name = 0;
1765
1766#if defined(CONFIG_JFFS2_PART_SIZE)
1767 part->size = CONFIG_JFFS2_PART_SIZE;
1768#else
1769 part->size = SIZE_REMAINING;
1770#endif
1771
1772#if defined(CONFIG_JFFS2_PART_OFFSET)
1773 part->offset = CONFIG_JFFS2_PART_OFFSET;
1774#else
1775 part->offset = 0x00000000;
1776#endif
1777
1778 part->dev = current_dev;
1779 INIT_LIST_HEAD(&part->link);
1780
1781 /* recalculate size if needed */
1782 if (part->size == SIZE_REMAINING)
1783 part->size = id->size - part->offset;
1784
1785 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
1786 part->name, part->size, part->offset);
1787
1788 /* device */
1789 current_dev->id = id;
1790 INIT_LIST_HEAD(&current_dev->link);
1791 current_dev->num_parts = 1;
1792 INIT_LIST_HEAD(&current_dev->parts);
1793 list_add(&part->link, &current_dev->parts);
1794 }
1795
1796 return 0;
1797}
1798#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1799
1800/**
1801 * Return pointer to the partition of a requested number from a requested
1802 * device.
1803 *
1804 * @param dev device that is to be searched for a partition
1805 * @param part_num requested partition number
1806 * @return pointer to the part_info, NULL otherwise
1807 */
1808static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
1809{
1810 struct list_head *entry;
1811 struct part_info *part;
1812 int num;
1813
1814 if (!dev)
1815 return NULL;
1816
1817 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
1818 part_num, MTD_DEV_TYPE(dev->id->type),
1819 dev->id->num, dev->id->mtd_id);
1820
1821 if (part_num >= dev->num_parts) {
1822 printf("invalid partition number %d for device %s%d (%s)\n",
1823 part_num, MTD_DEV_TYPE(dev->id->type),
1824 dev->id->num, dev->id->mtd_id);
1825 return NULL;
1826 }
1827
1828 /* locate partition number, return it */
1829 num = 0;
1830 list_for_each(entry, &dev->parts) {
1831 part = list_entry(entry, struct part_info, link);
1832
1833 if (part_num == num++) {
1834 return part;
1835 }
1836 }
1837
1838 return NULL;
1839}
1840
Michal Simekab4b9562007-08-06 23:31:49 +02001841/***************************************************/
1842/* U-boot commands */
1843/***************************************************/
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001844
1845/**
1846 * Routine implementing fsload u-boot command. This routine tries to load
1847 * a requested file from jffs2/cramfs filesystem on a current partition.
1848 *
1849 * @param cmdtp command internal data
1850 * @param flag command flag
1851 * @param argc number of arguments supplied to the command
1852 * @param argv arguments list
1853 * @return 0 on success, 1 otherwise
1854 */
1855int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1856{
wdenk39539882004-07-01 16:30:44 +00001857 char *fsname;
wdenkf39748a2004-06-09 13:37:52 +00001858 char *filename;
wdenk39539882004-07-01 16:30:44 +00001859 int size;
1860 struct part_info *part;
1861 ulong offset = load_addr;
wdenkf39748a2004-06-09 13:37:52 +00001862
1863 /* pre-set Boot file name */
1864 if ((filename = getenv("bootfile")) == NULL) {
1865 filename = "uImage";
1866 }
1867
wdenkc6097192002-11-03 00:24:07 +00001868 if (argc == 2) {
1869 filename = argv[1];
1870 }
1871 if (argc == 3) {
1872 offset = simple_strtoul(argv[1], NULL, 16);
wdenk4b9206e2004-03-23 22:14:11 +00001873 load_addr = offset;
wdenkc6097192002-11-03 00:24:07 +00001874 filename = argv[2];
1875 }
1876
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001877 /* make sure we are in sync with env variables */
1878 if (mtdparts_init() !=0)
1879 return 1;
1880
1881 if ((part = jffs2_part_info(current_dev, current_partnum))){
wdenkc6097192002-11-03 00:24:07 +00001882
Michal Simek991b0892007-09-15 00:03:35 +02001883 /* check partition type for cramfs */
1884 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
wdenkdd875c72004-01-03 21:24:46 +00001885 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
1886
1887 if (cramfs_check(part)) {
1888 size = cramfs_load ((char *) offset, part, filename);
1889 } else {
Michal Simek991b0892007-09-15 00:03:35 +02001890 /* if this is not cramfs assume jffs2 */
wdenkdd875c72004-01-03 21:24:46 +00001891 size = jffs2_1pass_load((char *)offset, part, filename);
1892 }
wdenkc6097192002-11-03 00:24:07 +00001893
1894 if (size > 0) {
1895 char buf[10];
wdenkdd875c72004-01-03 21:24:46 +00001896 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
1897 fsname, size, offset);
wdenkc6097192002-11-03 00:24:07 +00001898 sprintf(buf, "%x", size);
1899 setenv("filesize", buf);
1900 } else {
wdenkdd875c72004-01-03 21:24:46 +00001901 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
wdenkc6097192002-11-03 00:24:07 +00001902 }
1903
1904 return !(size > 0);
1905 }
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02001906 return 1;
wdenkc6097192002-11-03 00:24:07 +00001907}
1908
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001909/**
1910 * Routine implementing u-boot ls command which lists content of a given
1911 * directory on a current partition.
1912 *
1913 * @param cmdtp command internal data
1914 * @param flag command flag
1915 * @param argc number of arguments supplied to the command
1916 * @param argv arguments list
1917 * @return 0 on success, 1 otherwise
1918 */
1919int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenkc6097192002-11-03 00:24:07 +00001920{
wdenka87589d2005-06-10 10:00:19 +00001921 char *filename = "/";
wdenkc6097192002-11-03 00:24:07 +00001922 int ret;
1923 struct part_info *part;
1924
1925 if (argc == 2)
1926 filename = argv[1];
1927
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001928 /* make sure we are in sync with env variables */
1929 if (mtdparts_init() !=0)
1930 return 1;
1931
1932 if ((part = jffs2_part_info(current_dev, current_partnum))){
wdenkc6097192002-11-03 00:24:07 +00001933
wdenkdd875c72004-01-03 21:24:46 +00001934 /* check partition type for cramfs */
1935 if (cramfs_check(part)) {
1936 ret = cramfs_ls (part, filename);
1937 } else {
Michal Simek991b0892007-09-15 00:03:35 +02001938 /* if this is not cramfs assume jffs2 */
wdenkdd875c72004-01-03 21:24:46 +00001939 ret = jffs2_1pass_ls(part, filename);
1940 }
wdenkc6097192002-11-03 00:24:07 +00001941
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02001942 return ret ? 0 : 1;
wdenkc6097192002-11-03 00:24:07 +00001943 }
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02001944 return 1;
wdenkc6097192002-11-03 00:24:07 +00001945}
1946
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001947/**
1948 * Routine implementing u-boot fsinfo command. This routine prints out
1949 * miscellaneous filesystem informations/statistics.
1950 *
1951 * @param cmdtp command internal data
1952 * @param flag command flag
1953 * @param argc number of arguments supplied to the command
1954 * @param argv arguments list
1955 * @return 0 on success, 1 otherwise
1956 */
1957int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenkc6097192002-11-03 00:24:07 +00001958{
wdenkc6097192002-11-03 00:24:07 +00001959 struct part_info *part;
Michal Simek991b0892007-09-15 00:03:35 +02001960 char *fsname;
wdenkdd875c72004-01-03 21:24:46 +00001961 int ret;
wdenkc6097192002-11-03 00:24:07 +00001962
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001963 /* make sure we are in sync with env variables */
1964 if (mtdparts_init() !=0)
1965 return 1;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001966
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001967 if ((part = jffs2_part_info(current_dev, current_partnum))){
wdenkc6097192002-11-03 00:24:07 +00001968
wdenkdd875c72004-01-03 21:24:46 +00001969 /* check partition type for cramfs */
Michal Simek991b0892007-09-15 00:03:35 +02001970 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
1971 printf("### filesystem type is %s\n", fsname);
wdenkdd875c72004-01-03 21:24:46 +00001972
1973 if (cramfs_check(part)) {
1974 ret = cramfs_info (part);
1975 } else {
Michal Simek991b0892007-09-15 00:03:35 +02001976 /* if this is not cramfs assume jffs2 */
wdenkdd875c72004-01-03 21:24:46 +00001977 ret = jffs2_1pass_info(part);
1978 }
wdenkc6097192002-11-03 00:24:07 +00001979
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02001980 return ret ? 0 : 1;
wdenkc6097192002-11-03 00:24:07 +00001981 }
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02001982 return 1;
wdenkc6097192002-11-03 00:24:07 +00001983}
1984
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001985/* command line only */
1986#ifdef CONFIG_JFFS2_CMDLINE
1987/**
1988 * Routine implementing u-boot chpart command. Sets new current partition based
1989 * on the user supplied partition id. For partition id format see find_dev_and_part().
1990 *
1991 * @param cmdtp command internal data
1992 * @param flag command flag
1993 * @param argc number of arguments supplied to the command
1994 * @param argv arguments list
1995 * @return 0 on success, 1 otherwise
1996 */
1997int do_jffs2_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenkc6097192002-11-03 00:24:07 +00001998{
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001999/* command line only */
2000 struct mtd_device *dev;
2001 struct part_info *part;
2002 u8 pnum;
wdenkc6097192002-11-03 00:24:07 +00002003
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002004 if (mtdparts_init() !=0)
2005 return 1;
wdenkc6097192002-11-03 00:24:07 +00002006
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002007 if (argc < 2) {
2008 printf("no partition id specified\n");
2009 return 1;
wdenkc6097192002-11-03 00:24:07 +00002010 }
2011
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002012 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
2013 return 1;
wdenkc6097192002-11-03 00:24:07 +00002014
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002015 current_dev = dev;
2016 current_partnum = pnum;
2017 current_save();
2018
2019 printf("partition changed to %s%d,%d\n",
2020 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
2021
wdenkc6097192002-11-03 00:24:07 +00002022 return 0;
wdenkc6097192002-11-03 00:24:07 +00002023}
wdenkeedcd072004-09-08 22:03:11 +00002024
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002025/**
2026 * Routine implementing u-boot mtdparts command. Initialize/update default global
2027 * partition list and process user partition request (list, add, del).
2028 *
2029 * @param cmdtp command internal data
2030 * @param flag command flag
2031 * @param argc number of arguments supplied to the command
2032 * @param argv arguments list
2033 * @return 0 on success, 1 otherwise
2034 */
2035int do_jffs2_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2036{
2037 if (argc == 2) {
2038 if (strcmp(argv[1], "default") == 0) {
2039 setenv("mtdids", (char *)mtdids_default);
2040 setenv("mtdparts", (char *)mtdparts_default);
2041 setenv("partition", NULL);
2042
2043 mtdparts_init();
2044 return 0;
2045 } else if (strcmp(argv[1], "delall") == 0) {
2046 /* this may be the first run, initialize lists if needed */
2047 mtdparts_init();
2048
2049 setenv("mtdparts", NULL);
2050
2051 /* devices_init() calls current_save() */
2052 return devices_init();
2053 }
2054 }
2055
2056 /* make sure we are in sync with env variables */
2057 if (mtdparts_init() != 0)
2058 return 1;
2059
2060 if (argc == 1) {
2061 list_partitions();
2062 return 0;
2063 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02002064
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002065 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
2066 if (((argc == 5) || (argc == 6)) && (strcmp(argv[1], "add") == 0)) {
2067#define PART_ADD_DESC_MAXLEN 64
2068 char tmpbuf[PART_ADD_DESC_MAXLEN];
2069 u8 type, num, len;
2070 struct mtd_device *dev;
2071 struct mtd_device *dev_tmp;
2072 struct mtdids *id;
2073 struct part_info *p;
2074
2075 if (id_parse(argv[2], NULL, &type, &num) != 0)
2076 return 1;
2077
2078 if ((id = id_find(type, num)) == NULL) {
2079 printf("no such device %s defined in mtdids variable\n", argv[2]);
2080 return 1;
2081 }
2082
2083 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
2084 len += strlen(argv[3]); /* size@offset */
2085 len += strlen(argv[4]) + 2; /* '(' name ')' */
2086 if (argv[5] && (strlen(argv[5]) == 2))
2087 len += 2; /* 'ro' */
2088
2089 if (len >= PART_ADD_DESC_MAXLEN) {
2090 printf("too long partition description\n");
2091 return 1;
2092 }
2093 sprintf(tmpbuf, "%s:%s(%s)%s",
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02002094 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002095 DEBUGF("add tmpbuf: %s\n", tmpbuf);
2096
2097 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
2098 return 1;
2099
2100 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
2101 dev->id->num, dev->id->mtd_id);
2102
2103 if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) {
2104 device_add(dev);
2105 } else {
2106 /* merge new partition with existing ones*/
2107 p = list_entry(dev->parts.next, struct part_info, link);
2108 if (part_add(dev_tmp, p) != 0) {
2109 device_del(dev);
2110 return 1;
2111 }
2112 }
2113
2114 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
2115 printf("generated mtdparts too long, reseting to null\n");
2116 return 1;
2117 }
2118
2119 return 0;
2120 }
2121
2122 /* mtdparts del part-id */
2123 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
2124 DEBUGF("del: part-id = %s\n", argv[2]);
2125
2126 return delete_partition(argv[2]);
2127 }
2128
2129 printf ("Usage:\n%s\n", cmdtp->usage);
2130 return 1;
2131}
2132#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
wdenk8bde7f72003-06-27 21:31:46 +00002133
2134/***************************************************/
wdenk0d498392003-07-01 21:06:45 +00002135U_BOOT_CMD(
2136 fsload, 3, 0, do_jffs2_fsload,
wdenkaa5590b2004-06-09 12:42:26 +00002137 "fsload\t- load binary file from a filesystem image\n",
wdenk8bde7f72003-06-27 21:31:46 +00002138 "[ off ] [ filename ]\n"
2139 " - load binary file from flash bank\n"
2140 " with offset 'off'\n"
2141);
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002142U_BOOT_CMD(
2143 ls, 2, 1, do_jffs2_ls,
2144 "ls\t- list files in a directory (default /)\n",
2145 "[ directory ]\n"
2146 " - list files in a directory.\n"
2147);
wdenk8bde7f72003-06-27 21:31:46 +00002148
wdenk0d498392003-07-01 21:06:45 +00002149U_BOOT_CMD(
2150 fsinfo, 1, 1, do_jffs2_fsinfo,
wdenkaa5590b2004-06-09 12:42:26 +00002151 "fsinfo\t- print information about filesystems\n",
wdenk8bde7f72003-06-27 21:31:46 +00002152 " - print information about filesystems\n"
2153);
2154
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002155#ifdef CONFIG_JFFS2_CMDLINE
wdenk0d498392003-07-01 21:06:45 +00002156U_BOOT_CMD(
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002157 chpart, 2, 0, do_jffs2_chpart,
2158 "chpart\t- change active partition\n",
2159 "part-id\n"
2160 " - change active partition (e.g. part-id = nand0,1)\n"
wdenk8bde7f72003-06-27 21:31:46 +00002161);
2162
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002163U_BOOT_CMD(
2164 mtdparts, 6, 0, do_jffs2_mtdparts,
2165 "mtdparts- define flash/nand partitions\n",
2166 "\n"
2167 " - list partition table\n"
2168 "mtdparts delall\n"
2169 " - delete all partitions\n"
2170 "mtdparts del part-id\n"
2171 " - delete partition (e.g. part-id = nand0,1)\n"
2172 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2173 " - add partition\n"
2174 "mtdparts default\n"
2175 " - reset partition table to defaults\n\n"
2176 "-----\n\n"
2177 "this command uses three environment variables:\n\n"
2178 "'partition' - keeps current partition identifier\n\n"
2179 "partition := <part-id>\n"
2180 "<part-id> := <dev-id>,part_num\n\n"
2181 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2182 "mtdids=<idmap>[,<idmap>,...]\n\n"
2183 "<idmap> := <dev-id>=<mtd-id>\n"
2184 "<dev-id> := 'nand'|'nor'<dev-num>\n"
2185 "<dev-num> := mtd device number, 0...\n"
2186 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2187 "'mtdparts' - partition list\n\n"
2188 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2189 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2190 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2191 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2192 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2193 "<offset> := partition start offset within the device\n"
2194 "<name> := '(' NAME ')'\n"
2195 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)\n"
2196);
2197#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2198
2199/***************************************************/