blob: 791a572cc51a60986cdc21681dea5924b3fb0e3b [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>
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +090054 * <dev-id> := 'nand'|'nor'|'onenand'<dev-num>
Wolfgang Denk700a0c62005-08-08 01:03:24 +020055 * <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
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900106
107#if defined(CONFIG_CMD_ONENAND)
108#include <linux/mtd/mtd.h>
109#include <linux/mtd/onenand.h>
110#include <onenand_uboot.h>
111#endif
112
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200113/* enable/disable debugging messages */
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100114#define DEBUG_JFFS
115#undef DEBUG_JFFS
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200116
Michal Simekab4b9562007-08-06 23:31:49 +0200117#ifdef DEBUG_JFFS
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200118# define DEBUGF(fmt, args...) printf(fmt ,##args)
119#else
120# define DEBUGF(fmt, args...)
121#endif
122
123/* special size referring to all the remaining space in a partition */
124#define SIZE_REMAINING 0xFFFFFFFF
125
126/* special offset value, it is used when not provided by user
127 *
128 * this value is used temporarily during parsing, later such offests
129 * are recalculated */
130#define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
131
132/* minimum partition size */
133#define MIN_PART_SIZE 4096
134
135/* this flag needs to be set in part_info struct mask_flags
136 * field for read-only partitions */
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100137#define MTD_WRITEABLE_CMD 1
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200138
139#ifdef CONFIG_JFFS2_CMDLINE
140/* default values for mtdids and mtdparts variables */
141#if defined(MTDIDS_DEFAULT)
142static const char *const mtdids_default = MTDIDS_DEFAULT;
143#else
144#warning "MTDIDS_DEFAULT not defined!"
145static const char *const mtdids_default = NULL;
146#endif
147
148#if defined(MTDPARTS_DEFAULT)
149static const char *const mtdparts_default = MTDPARTS_DEFAULT;
150#else
151#warning "MTDPARTS_DEFAULT not defined!"
152static const char *const mtdparts_default = NULL;
153#endif
154
155/* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
156#define MTDIDS_MAXLEN 128
157#define MTDPARTS_MAXLEN 512
158#define PARTITION_MAXLEN 16
159static char last_ids[MTDIDS_MAXLEN];
160static char last_parts[MTDPARTS_MAXLEN];
161static char last_partition[PARTITION_MAXLEN];
162
163/* low level jffs2 cache cleaning routine */
164extern void jffs2_free_cache(struct part_info *part);
165
166/* mtdids mapping list, filled by parse_ids() */
167struct list_head mtdids;
168
169/* device/partition list, parse_cmdline() parses into here */
170struct list_head devices;
171#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
172
173/* current active device and partition number */
174static struct mtd_device *current_dev = NULL;
175static u8 current_partnum = 0;
176
Harald Weltef540c422007-12-19 14:09:58 +0100177#if defined(CONFIG_CMD_CRAMFS)
wdenk180d3f72004-01-04 16:28:35 +0000178extern int cramfs_check (struct part_info *info);
179extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
180extern int cramfs_ls (struct part_info *info, char *filename);
181extern int cramfs_info (struct part_info *info);
Harald Weltef540c422007-12-19 14:09:58 +0100182#else
183/* defining empty macros for function names is ugly but avoids ifdef clutter
184 * all over the code */
185#define cramfs_check(x) (0)
186#define cramfs_load(x,y,z) (-1)
187#define cramfs_ls(x,y) (0)
188#define cramfs_info(x) (0)
189#endif
wdenk180d3f72004-01-04 16:28:35 +0000190
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200191static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num);
wdenkc6097192002-11-03 00:24:07 +0000192
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200193/* command line only routines */
194#ifdef CONFIG_JFFS2_CMDLINE
wdenkc6097192002-11-03 00:24:07 +0000195
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200196static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
197static int device_del(struct mtd_device *dev);
wdenk6705d812004-08-02 23:22:59 +0000198
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200199/**
Michal Simekab4b9562007-08-06 23:31:49 +0200200 * Parses a string into a number. The number stored at ptr is
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200201 * potentially suffixed with K (for kilobytes, or 1024 bytes),
202 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
Michal Simekab4b9562007-08-06 23:31:49 +0200203 * 1073741824). If the number is suffixed with K, M, or G, then
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200204 * the return value is the number multiplied by one kilobyte, one
205 * megabyte, or one gigabyte, respectively.
206 *
207 * @param ptr where parse begins
208 * @param retptr output pointer to next char after parse completes (output)
209 * @return resulting unsigned int
210 */
211static unsigned long memsize_parse (const char *const ptr, const char **retptr)
wdenkc6097192002-11-03 00:24:07 +0000212{
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200213 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
214
215 switch (**retptr) {
216 case 'G':
217 case 'g':
218 ret <<= 10;
219 case 'M':
220 case 'm':
221 ret <<= 10;
222 case 'K':
223 case 'k':
224 ret <<= 10;
225 (*retptr)++;
226 default:
227 break;
228 }
229
230 return ret;
231}
232
233/**
234 * Format string describing supplied size. This routine does the opposite job
235 * to memsize_parse(). Size in bytes is converted to string and if possible
236 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
237 *
238 * Note, that this routine does not check for buffer overflow, it's the caller
239 * who must assure enough space.
240 *
241 * @param buf output buffer
242 * @param size size to be converted to string
243 */
244static void memsize_format(char *buf, u32 size)
245{
246#define SIZE_GB ((u32)1024*1024*1024)
247#define SIZE_MB ((u32)1024*1024)
248#define SIZE_KB ((u32)1024)
249
250 if ((size % SIZE_GB) == 0)
Kim Phillips4109df62008-07-10 14:00:15 -0500251 sprintf(buf, "%ug", size/SIZE_GB);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200252 else if ((size % SIZE_MB) == 0)
Kim Phillips4109df62008-07-10 14:00:15 -0500253 sprintf(buf, "%um", size/SIZE_MB);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200254 else if (size % SIZE_KB == 0)
Kim Phillips4109df62008-07-10 14:00:15 -0500255 sprintf(buf, "%uk", size/SIZE_KB);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200256 else
Kim Phillips4109df62008-07-10 14:00:15 -0500257 sprintf(buf, "%u", size);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200258}
259
260/**
Marian Balakowicz7d454772005-09-11 15:27:12 +0200261 * This routine does global indexing of all partitions. Resulting index for
262 * current partition is saved in 'mtddevnum'. Current partition name in
263 * 'mtddevname'.
264 */
265static void index_partitions(void)
266{
267 char buf[16];
268 u16 mtddevnum;
269 struct part_info *part;
270 struct list_head *dentry;
271 struct mtd_device *dev;
272
273 DEBUGF("--- index partitions ---\n");
274
275 if (current_dev) {
276 mtddevnum = 0;
277 list_for_each(dentry, &devices) {
278 dev = list_entry(dentry, struct mtd_device, link);
279 if (dev == current_dev) {
280 mtddevnum += current_partnum;
281 sprintf(buf, "%d", mtddevnum);
282 setenv("mtddevnum", buf);
283 break;
284 }
285 mtddevnum += dev->num_parts;
286 }
287
288 part = jffs2_part_info(current_dev, current_partnum);
289 setenv("mtddevname", part->name);
290
291 DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
292 } else {
293 setenv("mtddevnum", NULL);
294 setenv("mtddevname", NULL);
295
296 DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
297 }
298}
299
300/**
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200301 * Save current device and partition in environment variable 'partition'.
302 */
303static void current_save(void)
304{
305 char buf[16];
306
307 DEBUGF("--- current_save ---\n");
308
309 if (current_dev) {
310 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_dev->id->type),
311 current_dev->id->num, current_partnum);
312
313 setenv("partition", buf);
314 strncpy(last_partition, buf, 16);
315
316 DEBUGF("=> partition %s\n", buf);
317 } else {
318 setenv("partition", NULL);
319 last_partition[0] = '\0';
320
321 DEBUGF("=> partition NULL\n");
322 }
Marian Balakowicz7d454772005-09-11 15:27:12 +0200323 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200324}
325
326/**
327 * Performs sanity check for supplied NOR flash partition. Table of existing
328 * NOR flash devices is searched and partition device is located. Alignment
329 * with the granularity of NOR flash sectors is verified.
330 *
331 * @param id of the parent device
332 * @param part partition to validate
333 * @return 0 if partition is valid, 1 otherwise
334 */
335static int part_validate_nor(struct mtdids *id, struct part_info *part)
336{
Jon Loeligerc76fe472007-07-08 18:02:23 -0500337#if defined(CONFIG_CMD_FLASH)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200338 /* info for FLASH chips */
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200339 extern flash_info_t flash_info[];
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200340 flash_info_t *flash;
341 int offset_aligned;
342 u32 end_offset;
wdenkc6097192002-11-03 00:24:07 +0000343 int i;
344
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200345 flash = &flash_info[id->num];
wdenkc6097192002-11-03 00:24:07 +0000346
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200347 offset_aligned = 0;
348 for (i = 0; i < flash->sector_count; i++) {
349 if ((flash->start[i] - flash->start[0]) == part->offset) {
350 offset_aligned = 1;
351 break;
352 }
353 }
354 if (offset_aligned == 0) {
355 printf("%s%d: partition (%s) start offset alignment incorrect\n",
356 MTD_DEV_TYPE(id->type), id->num, part->name);
357 return 1;
358 }
wdenkc6097192002-11-03 00:24:07 +0000359
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200360 end_offset = part->offset + part->size;
361 for (i = 0; i < flash->sector_count; i++) {
362 if ((flash->start[i] - flash->start[0]) == end_offset)
363 return 0;
364 }
wdenkc6097192002-11-03 00:24:07 +0000365
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200366 if (flash->size == end_offset)
367 return 0;
368
369 printf("%s%d: partition (%s) size alignment incorrect\n",
370 MTD_DEV_TYPE(id->type), id->num, part->name);
371#endif
372 return 1;
373}
374
375/**
376 * Performs sanity check for supplied NAND flash partition. Table of existing
377 * NAND flash devices is searched and partition device is located. Alignment
378 * with the granularity of nand erasesize is verified.
379 *
380 * @param id of the parent device
381 * @param part partition to validate
382 * @return 0 if partition is valid, 1 otherwise
383 */
384static int part_validate_nand(struct mtdids *id, struct part_info *part)
385{
Jon Loeligerc76fe472007-07-08 18:02:23 -0500386#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200387 /* info for NAND chips */
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200388 nand_info_t *nand;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200389
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200390 nand = &nand_info[id->num];
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200391
392 if ((unsigned long)(part->offset) % nand->erasesize) {
393 printf("%s%d: partition (%s) start offset alignment incorrect\n",
394 MTD_DEV_TYPE(id->type), id->num, part->name);
395 return 1;
396 }
397
398 if (part->size % nand->erasesize) {
399 printf("%s%d: partition (%s) size alignment incorrect\n",
400 MTD_DEV_TYPE(id->type), id->num, part->name);
401 return 1;
402 }
403
404 return 0;
wdenkc6097192002-11-03 00:24:07 +0000405#else
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200406 return 1;
wdenkc6097192002-11-03 00:24:07 +0000407#endif
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200408}
wdenkc6097192002-11-03 00:24:07 +0000409
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200410/**
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900411 * Performs sanity check for supplied OneNAND flash partition.
412 * Table of existing OneNAND flash devices is searched and partition device
413 * is located. Alignment with the granularity of nand erasesize is verified.
414 *
415 * @param id of the parent device
416 * @param part partition to validate
417 * @return 0 if partition is valid, 1 otherwise
418 */
419static int part_validate_onenand(struct mtdids *id, struct part_info *part)
420{
421#if defined(CONFIG_CMD_ONENAND)
422 /* info for OneNAND chips */
423 struct mtd_info *mtd;
424
425 mtd = &onenand_mtd;
426
427 if ((unsigned long)(part->offset) % mtd->erasesize) {
428 printf("%s%d: partition (%s) start offset"
429 "alignment incorrect\n",
430 MTD_DEV_TYPE(id->type), id->num, part->name);
431 return 1;
432 }
433
434 if (part->size % mtd->erasesize) {
435 printf("%s%d: partition (%s) size alignment incorrect\n",
436 MTD_DEV_TYPE(id->type), id->num, part->name);
437 return 1;
438 }
439
440 return 0;
441#else
442 return 1;
443#endif
444}
445
446
447/**
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200448 * Performs sanity check for supplied partition. Offset and size are verified
449 * to be within valid range. Partition type is checked and either
450 * parts_validate_nor() or parts_validate_nand() is called with the argument
451 * of part.
452 *
453 * @param id of the parent device
454 * @param part partition to validate
455 * @return 0 if partition is valid, 1 otherwise
456 */
457static int part_validate(struct mtdids *id, struct part_info *part)
458{
459 if (part->size == SIZE_REMAINING)
460 part->size = id->size - part->offset;
wdenkc6097192002-11-03 00:24:07 +0000461
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200462 if (part->offset > id->size) {
Kim Phillips4109df62008-07-10 14:00:15 -0500463 printf("%s: offset %08x beyond flash size %08x\n",
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200464 id->mtd_id, part->offset, id->size);
465 return 1;
wdenkc6097192002-11-03 00:24:07 +0000466 }
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200467
468 if ((part->offset + part->size) <= part->offset) {
469 printf("%s%d: partition (%s) size too big\n",
470 MTD_DEV_TYPE(id->type), id->num, part->name);
471 return 1;
472 }
473
474 if (part->offset + part->size > id->size) {
475 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
476 return 1;
477 }
478
479 if (id->type == MTD_DEV_TYPE_NAND)
480 return part_validate_nand(id, part);
481 else if (id->type == MTD_DEV_TYPE_NOR)
482 return part_validate_nor(id, part);
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900483 else if (id->type == MTD_DEV_TYPE_ONENAND)
484 return part_validate_onenand(id, part);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200485 else
486 DEBUGF("part_validate: invalid dev type\n");
487
488 return 1;
489}
490
491/**
492 * Delete selected partition from the partion list of the specified device.
493 *
494 * @param dev device to delete partition from
495 * @param part partition to delete
496 * @return 0 on success, 1 otherwise
497 */
498static int part_del(struct mtd_device *dev, struct part_info *part)
499{
Marian Balakowicz7d454772005-09-11 15:27:12 +0200500 u8 current_save_needed = 0;
501
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200502 /* if there is only one partition, remove whole device */
503 if (dev->num_parts == 1)
504 return device_del(dev);
505
506 /* otherwise just delete this partition */
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200507
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200508 if (dev == current_dev) {
509 /* we are modyfing partitions for the current device,
510 * update current */
511 struct part_info *curr_pi;
512 curr_pi = jffs2_part_info(current_dev, current_partnum);
513
514 if (curr_pi) {
515 if (curr_pi == part) {
516 printf("current partition deleted, resetting current to 0\n");
517 current_partnum = 0;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200518 } else if (part->offset <= curr_pi->offset) {
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200519 current_partnum--;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200520 }
Marian Balakowicz7d454772005-09-11 15:27:12 +0200521 current_save_needed = 1;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200522 }
523 }
524
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +0200525#ifdef CONFIG_NAND_LEGACY
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200526 jffs2_free_cache(part);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200527#endif
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200528 list_del(&part->link);
529 free(part);
530 dev->num_parts--;
531
Marian Balakowicz7d454772005-09-11 15:27:12 +0200532 if (current_save_needed > 0)
533 current_save();
534 else
535 index_partitions();
536
wdenkc6097192002-11-03 00:24:07 +0000537 return 0;
538}
wdenk998eaae2004-04-18 19:43:36 +0000539
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200540/**
541 * Delete all partitions from parts head list, free memory.
542 *
543 * @param head list of partitions to delete
544 */
545static void part_delall(struct list_head *head)
wdenk998eaae2004-04-18 19:43:36 +0000546{
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200547 struct list_head *entry, *n;
548 struct part_info *part_tmp;
wdenk998eaae2004-04-18 19:43:36 +0000549
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200550 /* clean tmp_list and free allocated memory */
551 list_for_each_safe(entry, n, head) {
552 part_tmp = list_entry(entry, struct part_info, link);
wdenk998eaae2004-04-18 19:43:36 +0000553
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +0200554#ifdef CONFIG_NAND_LEGACY
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200555 jffs2_free_cache(part_tmp);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200556#endif
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200557 list_del(entry);
558 free(part_tmp);
wdenk998eaae2004-04-18 19:43:36 +0000559 }
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200560}
561
562/**
563 * Add new partition to the supplied partition list. Make sure partitions are
564 * sorted by offset in ascending order.
565 *
566 * @param head list this partition is to be added to
567 * @param new partition to be added
568 */
569static int part_sort_add(struct mtd_device *dev, struct part_info *part)
570{
571 struct list_head *entry;
572 struct part_info *new_pi, *curr_pi;
573
574 /* link partition to parrent dev */
575 part->dev = dev;
576
577 if (list_empty(&dev->parts)) {
578 DEBUGF("part_sort_add: list empty\n");
579 list_add(&part->link, &dev->parts);
Marian Balakowicz7d454772005-09-11 15:27:12 +0200580 dev->num_parts++;
581 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200582 return 0;
583 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200584
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200585 new_pi = list_entry(&part->link, struct part_info, link);
586
587 /* get current partition info if we are updating current device */
588 curr_pi = NULL;
589 if (dev == current_dev)
590 curr_pi = jffs2_part_info(current_dev, current_partnum);
591
592 list_for_each(entry, &dev->parts) {
593 struct part_info *pi;
594
595 pi = list_entry(entry, struct part_info, link);
596
597 /* be compliant with kernel cmdline, allow only one partition at offset zero */
598 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
599 printf("cannot add second partition at offset 0\n");
600 return 1;
601 }
602
603 if (new_pi->offset <= pi->offset) {
604 list_add_tail(&part->link, entry);
Marian Balakowicz7d454772005-09-11 15:27:12 +0200605 dev->num_parts++;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200606
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200607 if (curr_pi && (pi->offset <= curr_pi->offset)) {
608 /* we are modyfing partitions for the current
609 * device, update current */
610 current_partnum++;
611 current_save();
Marian Balakowicz7d454772005-09-11 15:27:12 +0200612 } else {
613 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200614 }
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200615 return 0;
616 }
617 }
Marian Balakowicz7d454772005-09-11 15:27:12 +0200618
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200619 list_add_tail(&part->link, &dev->parts);
Marian Balakowicz7d454772005-09-11 15:27:12 +0200620 dev->num_parts++;
621 index_partitions();
wdenk998eaae2004-04-18 19:43:36 +0000622 return 0;
623}
624
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200625/**
626 * Add provided partition to the partition list of a given device.
627 *
628 * @param dev device to which partition is added
629 * @param part partition to be added
630 * @return 0 on success, 1 otherwise
631 */
632static int part_add(struct mtd_device *dev, struct part_info *part)
wdenkc6097192002-11-03 00:24:07 +0000633{
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200634 /* verify alignment and size */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200635 if (part_validate(dev->id, part) != 0)
636 return 1;
637
638 /* partition is ok, add it to the list */
639 if (part_sort_add(dev, part) != 0)
640 return 1;
641
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200642 return 0;
643}
644
645/**
646 * Parse one partition definition, allocate memory and return pointer to this
647 * location in retpart.
648 *
649 * @param partdef pointer to the partition definition string i.e. <part-def>
650 * @param ret output pointer to next char after parse completes (output)
651 * @param retpart pointer to the allocated partition (output)
652 * @return 0 on success, 1 otherwise
653 */
654static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
655{
656 struct part_info *part;
657 unsigned long size;
658 unsigned long offset;
659 const char *name;
660 int name_len;
661 unsigned int mask_flags;
662 const char *p;
663
664 p = partdef;
665 *retpart = NULL;
666 *ret = NULL;
667
668 /* fetch the partition size */
669 if (*p == '-') {
670 /* assign all remaining space to this partition */
671 DEBUGF("'-': remaining size assigned\n");
672 size = SIZE_REMAINING;
673 p++;
674 } else {
675 size = memsize_parse(p, &p);
676 if (size < MIN_PART_SIZE) {
677 printf("partition size too small (%lx)\n", size);
678 return 1;
679 }
680 }
681
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200682 /* check for offset */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200683 offset = OFFSET_NOT_SPECIFIED;
684 if (*p == '@') {
685 p++;
686 offset = memsize_parse(p, &p);
687 }
688
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200689 /* now look for the name */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200690 if (*p == '(') {
691 name = ++p;
692 if ((p = strchr(name, ')')) == NULL) {
693 printf("no closing ) found in partition name\n");
694 return 1;
695 }
696 name_len = p - name + 1;
697 if ((name_len - 1) == 0) {
698 printf("empty partition name\n");
699 return 1;
700 }
701 p++;
702 } else {
703 /* 0x00000000@0x00000000 */
704 name_len = 22;
705 name = NULL;
706 }
707
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200708 /* test for options */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200709 mask_flags = 0;
710 if (strncmp(p, "ro", 2) == 0) {
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100711 mask_flags |= MTD_WRITEABLE_CMD;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200712 p += 2;
713 }
714
715 /* check for next partition definition */
716 if (*p == ',') {
717 if (size == SIZE_REMAINING) {
718 *ret = NULL;
719 printf("no partitions allowed after a fill-up partition\n");
720 return 1;
721 }
722 *ret = ++p;
723 } else if ((*p == ';') || (*p == '\0')) {
724 *ret = p;
725 } else {
726 printf("unexpected character '%c' at the end of partition\n", *p);
727 *ret = NULL;
728 return 1;
729 }
730
Michal Simekab4b9562007-08-06 23:31:49 +0200731 /* allocate memory */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200732 part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
733 if (!part) {
734 printf("out of memory\n");
735 return 1;
736 }
737 memset(part, 0, sizeof(struct part_info) + name_len);
738 part->size = size;
739 part->offset = offset;
740 part->mask_flags = mask_flags;
741 part->name = (char *)(part + 1);
742
743 if (name) {
744 /* copy user provided name */
745 strncpy(part->name, name, name_len - 1);
746 part->auto_name = 0;
747 } else {
748 /* auto generated name in form of size@offset */
749 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
750 part->auto_name = 1;
751 }
752
753 part->name[name_len - 1] = '\0';
754 INIT_LIST_HEAD(&part->link);
755
756 DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
757 part->name, part->size,
758 part->offset, part->mask_flags);
759
760 *retpart = part;
761 return 0;
762}
763#endif/* #ifdef CONFIG_JFFS2_CMDLINE */
764
765/**
766 * Check device number to be within valid range for given device type.
767 *
768 * @param dev device to validate
769 * @return 0 if device is valid, 1 otherwise
770 */
771static int device_validate(u8 type, u8 num, u32 *size)
772{
773 if (type == MTD_DEV_TYPE_NOR) {
Jon Loeligerc76fe472007-07-08 18:02:23 -0500774#if defined(CONFIG_CMD_FLASH)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200775 if (num < CONFIG_SYS_MAX_FLASH_BANKS) {
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200776 extern flash_info_t flash_info[];
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200777 *size = flash_info[num].size;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200778
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200779 return 0;
780 }
781
782 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200783 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200784#else
785 printf("support for FLASH devices not present\n");
786#endif
787 } else if (type == MTD_DEV_TYPE_NAND) {
Jon Loeligerc76fe472007-07-08 18:02:23 -0500788#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200789 if (num < CONFIG_SYS_MAX_NAND_DEVICE) {
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +0200790#ifndef CONFIG_NAND_LEGACY
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200791 *size = nand_info[num].size;
792#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200793 extern struct nand_chip nand_dev_desc[CONFIG_SYS_MAX_NAND_DEVICE];
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200794 *size = nand_dev_desc[num].totlen;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200795#endif
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200796 return 0;
797 }
798
799 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200800 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200801#else
802 printf("support for NAND devices not present\n");
803#endif
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900804 } else if (type == MTD_DEV_TYPE_ONENAND) {
805#if defined(CONFIG_CMD_ONENAND)
806 *size = onenand_mtd.size;
807 return 0;
808#else
809 printf("support for OneNAND devices not present\n");
810#endif
811 } else
812 printf("Unknown defice type %d\n", type);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200813
814 return 1;
815}
816
817#ifdef CONFIG_JFFS2_CMDLINE
818/**
819 * Delete all mtd devices from a supplied devices list, free memory allocated for
820 * each device and delete all device partitions.
821 *
822 * @return 0 on success, 1 otherwise
823 */
824static int device_delall(struct list_head *head)
825{
826 struct list_head *entry, *n;
827 struct mtd_device *dev_tmp;
828
829 /* clean devices list */
830 list_for_each_safe(entry, n, head) {
831 dev_tmp = list_entry(entry, struct mtd_device, link);
832 list_del(entry);
833 part_delall(&dev_tmp->parts);
834 free(dev_tmp);
835 }
836 INIT_LIST_HEAD(&devices);
837
838 return 0;
839}
840
841/**
842 * If provided device exists it's partitions are deleted, device is removed
843 * from device list and device memory is freed.
844 *
845 * @param dev device to be deleted
846 * @return 0 on success, 1 otherwise
847 */
848static int device_del(struct mtd_device *dev)
849{
850 part_delall(&dev->parts);
851 list_del(&dev->link);
852 free(dev);
853
854 if (dev == current_dev) {
855 /* we just deleted current device */
856 if (list_empty(&devices)) {
857 current_dev = NULL;
858 } else {
859 /* reset first partition from first dev from the
860 * devices list as current */
861 current_dev = list_entry(devices.next, struct mtd_device, link);
862 current_partnum = 0;
863 }
864 current_save();
Marian Balakowicz7d454772005-09-11 15:27:12 +0200865 return 0;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200866 }
867
Marian Balakowicz7d454772005-09-11 15:27:12 +0200868 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200869 return 0;
870}
871
872/**
873 * Search global device list and return pointer to the device of type and num
874 * specified.
875 *
876 * @param type device type
877 * @param num device number
878 * @return NULL if requested device does not exist
879 */
880static struct mtd_device* device_find(u8 type, u8 num)
881{
882 struct list_head *entry;
883 struct mtd_device *dev_tmp;
884
885 list_for_each(entry, &devices) {
886 dev_tmp = list_entry(entry, struct mtd_device, link);
887
888 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
889 return dev_tmp;
890 }
891
892 return NULL;
893}
894
895/**
896 * Add specified device to the global device list.
897 *
898 * @param dev device to be added
899 */
900static void device_add(struct mtd_device *dev)
901{
Marian Balakowicz7d454772005-09-11 15:27:12 +0200902 u8 current_save_needed = 0;
903
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200904 if (list_empty(&devices)) {
905 current_dev = dev;
906 current_partnum = 0;
Marian Balakowicz7d454772005-09-11 15:27:12 +0200907 current_save_needed = 1;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200908 }
909
910 list_add_tail(&dev->link, &devices);
Marian Balakowicz7d454772005-09-11 15:27:12 +0200911
912 if (current_save_needed > 0)
913 current_save();
914 else
915 index_partitions();
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200916}
917
918/**
919 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
920 * return pointer to the device structure.
921 *
922 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
923 * @param ret output pointer to next char after parse completes (output)
924 * @param retdev pointer to the allocated device (output)
925 * @return 0 on success, 1 otherwise
926 */
927static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
928{
929 struct mtd_device *dev;
930 struct part_info *part;
931 struct mtdids *id;
932 const char *mtd_id;
933 unsigned int mtd_id_len;
934 const char *p, *pend;
935 LIST_HEAD(tmp_list);
936 struct list_head *entry, *n;
937 u16 num_parts;
938 u32 offset;
939 int err = 1;
940
941 p = mtd_dev;
942 *retdev = NULL;
943 *ret = NULL;
944
945 DEBUGF("===device_parse===\n");
946
947 /* fetch <mtd-id> */
948 mtd_id = p;
949 if (!(p = strchr(mtd_id, ':'))) {
950 printf("no <mtd-id> identifier\n");
951 return 1;
952 }
953 mtd_id_len = p - mtd_id + 1;
954 p++;
955
956 /* verify if we have a valid device specified */
957 if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
958 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
959 return 1;
960 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200961
962 DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200963 id->type, MTD_DEV_TYPE(id->type),
964 id->num, id->mtd_id);
965 pend = strchr(p, ';');
966 DEBUGF("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
967
968
969 /* parse partitions */
970 num_parts = 0;
971
972 offset = 0;
973 if ((dev = device_find(id->type, id->num)) != NULL) {
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200974 /* if device already exists start at the end of the last partition */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200975 part = list_entry(dev->parts.prev, struct part_info, link);
976 offset = part->offset + part->size;
977 }
978
979 while (p && (*p != '\0') && (*p != ';')) {
980 err = 1;
981 if ((part_parse(p, &p, &part) != 0) || (!part))
982 break;
983
984 /* calculate offset when not specified */
985 if (part->offset == OFFSET_NOT_SPECIFIED)
986 part->offset = offset;
987 else
988 offset = part->offset;
989
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200990 /* verify alignment and size */
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200991 if (part_validate(id, part) != 0)
992 break;
993
994 offset += part->size;
995
996 /* partition is ok, add it to the list */
997 list_add_tail(&part->link, &tmp_list);
998 num_parts++;
999 err = 0;
1000 }
1001 if (err == 1) {
1002 part_delall(&tmp_list);
1003 return 1;
1004 }
1005
1006 if (num_parts == 0) {
1007 printf("no partitions for device %s%d (%s)\n",
1008 MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
1009 return 1;
1010 }
1011
1012 DEBUGF("\ntotal partitions: %d\n", num_parts);
1013
1014 /* check for next device presence */
1015 if (p) {
1016 if (*p == ';') {
1017 *ret = ++p;
1018 } else if (*p == '\0') {
1019 *ret = p;
1020 } else {
1021 printf("unexpected character '%c' at the end of device\n", *p);
1022 *ret = NULL;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001023 return 1;
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001024 }
1025 }
1026
1027 /* allocate memory for mtd_device structure */
1028 if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
1029 printf("out of memory\n");
1030 return 1;
1031 }
1032 memset(dev, 0, sizeof(struct mtd_device));
1033 dev->id = id;
Marian Balakowicz7d454772005-09-11 15:27:12 +02001034 dev->num_parts = 0; /* part_sort_add increments num_parts */
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001035 INIT_LIST_HEAD(&dev->parts);
1036 INIT_LIST_HEAD(&dev->link);
1037
1038 /* move partitions from tmp_list to dev->parts */
1039 list_for_each_safe(entry, n, &tmp_list) {
1040 part = list_entry(entry, struct part_info, link);
1041 list_del(entry);
1042 if (part_sort_add(dev, part) != 0) {
1043 device_del(dev);
1044 return 1;
1045 }
1046 }
1047
1048 *retdev = dev;
1049
1050 DEBUGF("===\n\n");
1051 return 0;
1052}
1053
1054/**
1055 * Initialize global device list.
1056 *
1057 * @return 0 on success, 1 otherwise
1058 */
1059static int devices_init(void)
1060{
1061 last_parts[0] = '\0';
1062 current_dev = NULL;
1063 current_save();
1064
1065 return device_delall(&devices);
1066}
1067
1068/*
1069 * Search global mtdids list and find id of requested type and number.
1070 *
1071 * @return pointer to the id if it exists, NULL otherwise
1072 */
1073static struct mtdids* id_find(u8 type, u8 num)
1074{
1075 struct list_head *entry;
1076 struct mtdids *id;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001077
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001078 list_for_each(entry, &mtdids) {
1079 id = list_entry(entry, struct mtdids, link);
1080
1081 if ((id->type == type) && (id->num == num))
1082 return id;
1083 }
1084
1085 return NULL;
1086}
1087
1088/**
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001089 * Search global mtdids list and find id of a requested mtd_id.
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001090 *
1091 * Note: first argument is not null terminated.
1092 *
1093 * @param mtd_id string containing requested mtd_id
1094 * @param mtd_id_len length of supplied mtd_id
1095 * @return pointer to the id if it exists, NULL otherwise
1096 */
1097static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
1098{
1099 struct list_head *entry;
1100 struct mtdids *id;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001101
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001102 DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1103 mtd_id_len, mtd_id, mtd_id_len);
1104
1105 list_for_each(entry, &mtdids) {
1106 id = list_entry(entry, struct mtdids, link);
1107
1108 DEBUGF("entry: '%s' (len = %d)\n",
1109 id->mtd_id, strlen(id->mtd_id));
1110
1111 if (mtd_id_len != strlen(id->mtd_id))
1112 continue;
1113 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1114 return id;
1115 }
1116
1117 return NULL;
1118}
1119#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1120
1121/**
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +09001122 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
1123 * return device type and number.
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001124 *
1125 * @param id string describing device id
1126 * @param ret_id output pointer to next char after parse completes (output)
1127 * @param dev_type parsed device type (output)
1128 * @param dev_num parsed device number (output)
1129 * @return 0 on success, 1 otherwise
1130 */
1131int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
1132{
1133 const char *p = id;
1134
1135 *dev_type = 0;
1136 if (strncmp(p, "nand", 4) == 0) {
1137 *dev_type = MTD_DEV_TYPE_NAND;
1138 p += 4;
1139 } else if (strncmp(p, "nor", 3) == 0) {
1140 *dev_type = MTD_DEV_TYPE_NOR;
1141 p += 3;
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +09001142 } else if (strncmp(p, "onenand", 7) == 0) {
1143 *dev_type = MTD_DEV_TYPE_ONENAND;
1144 p += 7;
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001145 } else {
1146 printf("incorrect device type in %s\n", id);
1147 return 1;
1148 }
1149
1150 if (!isdigit(*p)) {
1151 printf("incorrect device number in %s\n", id);
1152 return 1;
1153 }
1154
1155 *dev_num = simple_strtoul(p, (char **)&p, 0);
1156 if (ret_id)
1157 *ret_id = p;
1158 return 0;
1159}
1160
1161#ifdef CONFIG_JFFS2_CMDLINE
1162/**
1163 * Process all devices and generate corresponding mtdparts string describing
1164 * all partitions on all devices.
1165 *
1166 * @param buf output buffer holding generated mtdparts string (output)
1167 * @param buflen buffer size
1168 * @return 0 on success, 1 otherwise
1169 */
1170static int generate_mtdparts(char *buf, u32 buflen)
1171{
1172 struct list_head *pentry, *dentry;
1173 struct mtd_device *dev;
1174 struct part_info *part, *prev_part;
1175 char *p = buf;
1176 char tmpbuf[32];
1177 u32 size, offset, len, part_cnt;
1178 u32 maxlen = buflen - 1;
1179
1180 DEBUGF("--- generate_mtdparts ---\n");
1181
1182 if (list_empty(&devices)) {
1183 buf[0] = '\0';
1184 return 0;
1185 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001186
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001187 sprintf(p, "mtdparts=");
1188 p += 9;
1189
1190 list_for_each(dentry, &devices) {
1191 dev = list_entry(dentry, struct mtd_device, link);
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001192
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001193 /* copy mtd_id */
1194 len = strlen(dev->id->mtd_id) + 1;
1195 if (len > maxlen)
1196 goto cleanup;
1197 memcpy(p, dev->id->mtd_id, len - 1);
1198 p += len - 1;
1199 *(p++) = ':';
1200 maxlen -= len;
1201
1202 /* format partitions */
1203 prev_part = NULL;
1204 part_cnt = 0;
1205 list_for_each(pentry, &dev->parts) {
1206 part = list_entry(pentry, struct part_info, link);
1207 size = part->size;
1208 offset = part->offset;
1209 part_cnt++;
1210
1211 /* partition size */
1212 memsize_format(tmpbuf, size);
1213 len = strlen(tmpbuf);
1214 if (len > maxlen)
1215 goto cleanup;
1216 memcpy(p, tmpbuf, len);
1217 p += len;
1218 maxlen -= len;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001219
1220
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001221 /* add offset only when there is a gap between
1222 * partitions */
1223 if ((!prev_part && (offset != 0)) ||
1224 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1225
1226 memsize_format(tmpbuf, offset);
1227 len = strlen(tmpbuf) + 1;
1228 if (len > maxlen)
1229 goto cleanup;
1230 *(p++) = '@';
1231 memcpy(p, tmpbuf, len - 1);
1232 p += len - 1;
1233 maxlen -= len;
1234 }
1235
1236 /* copy name only if user supplied */
1237 if(!part->auto_name) {
1238 len = strlen(part->name) + 2;
1239 if (len > maxlen)
1240 goto cleanup;
1241
1242 *(p++) = '(';
1243 memcpy(p, part->name, len - 2);
1244 p += len - 2;
1245 *(p++) = ')';
1246 maxlen -= len;
1247 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001248
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001249 /* ro mask flag */
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +01001250 if (part->mask_flags && MTD_WRITEABLE_CMD) {
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001251 len = 2;
1252 if (len > maxlen)
1253 goto cleanup;
1254 *(p++) = 'r';
1255 *(p++) = 'o';
1256 maxlen -= 2;
1257 }
1258
1259 /* print ',' separator if there are other partitions
1260 * following */
1261 if (dev->num_parts > part_cnt) {
1262 if (1 > maxlen)
1263 goto cleanup;
1264 *(p++) = ',';
1265 maxlen--;
1266 }
1267 prev_part = part;
1268 }
1269 /* print ';' separator if there are other devices following */
1270 if (dentry->next != &devices) {
1271 if (1 > maxlen)
1272 goto cleanup;
1273 *(p++) = ';';
1274 maxlen--;
1275 }
1276 }
1277
1278 /* we still have at least one char left, as we decremented maxlen at
1279 * the begining */
1280 *p = '\0';
1281
1282 return 0;
1283
1284cleanup:
1285 last_parts[0] = '\0';
1286 return 1;
1287}
1288
1289/**
1290 * Call generate_mtdparts to process all devices and generate corresponding
1291 * mtdparts string, save it in mtdparts environment variable.
1292 *
1293 * @param buf output buffer holding generated mtdparts string (output)
1294 * @param buflen buffer size
1295 * @return 0 on success, 1 otherwise
1296 */
1297static int generate_mtdparts_save(char *buf, u32 buflen)
1298{
1299 int ret;
1300
1301 ret = generate_mtdparts(buf, buflen);
1302
1303 if ((buf[0] != '\0') && (ret == 0))
1304 setenv("mtdparts", buf);
1305 else
1306 setenv("mtdparts", NULL);
1307
1308 return ret;
1309}
1310
1311/**
1312 * Format and print out a partition list for each device from global device
1313 * list.
1314 */
1315static void list_partitions(void)
1316{
1317 struct list_head *dentry, *pentry;
1318 struct part_info *part;
1319 struct mtd_device *dev;
1320 int part_num;
1321
1322 DEBUGF("\n---list_partitions---\n");
1323 list_for_each(dentry, &devices) {
1324 dev = list_entry(dentry, struct mtd_device, link);
1325 printf("\ndevice %s%d <%s>, # parts = %d\n",
1326 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1327 dev->id->mtd_id, dev->num_parts);
1328 printf(" #: name\t\t\tsize\t\toffset\t\tmask_flags\n");
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001329
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001330 /* list partitions for given device */
1331 part_num = 0;
1332 list_for_each(pentry, &dev->parts) {
1333 part = list_entry(pentry, struct part_info, link);
Wolfgang Denka87d46f2006-09-13 10:23:06 +02001334 printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001335 part_num, part->name, part->size,
1336 part->offset, part->mask_flags);
1337
1338 part_num++;
1339 }
1340 }
1341 if (list_empty(&devices))
1342 printf("no partitions defined\n");
1343
1344 /* current_dev is not NULL only when we have non empty device list */
1345 if (current_dev) {
1346 part = jffs2_part_info(current_dev, current_partnum);
1347 if (part) {
Kim Phillips4109df62008-07-10 14:00:15 -05001348 printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001349 MTD_DEV_TYPE(current_dev->id->type),
1350 current_dev->id->num, current_partnum,
1351 part->name, part->size, part->offset);
1352 } else {
1353 printf("could not get current partition info\n\n");
1354 }
1355 }
1356
1357 printf("\ndefaults:\n");
1358 printf("mtdids : %s\n", mtdids_default);
1359 printf("mtdparts: %s\n", mtdparts_default);
1360}
1361
1362/**
1363 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1364 * corresponding device and verify partition number.
1365 *
Stefan Roese856f0542006-10-28 15:55:52 +02001366 * @param id string describing device and partition or partition name
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001367 * @param dev pointer to the requested device (output)
1368 * @param part_num verified partition number (output)
1369 * @param part pointer to requested partition (output)
1370 * @return 0 on success, 1 otherwise
1371 */
1372int find_dev_and_part(const char *id, struct mtd_device **dev,
1373 u8 *part_num, struct part_info **part)
1374{
Stefan Roese856f0542006-10-28 15:55:52 +02001375 struct list_head *dentry, *pentry;
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001376 u8 type, dnum, pnum;
1377 const char *p;
1378
1379 DEBUGF("--- find_dev_and_part ---\nid = %s\n", id);
1380
Stefan Roese856f0542006-10-28 15:55:52 +02001381 list_for_each(dentry, &devices) {
1382 *part_num = 0;
1383 *dev = list_entry(dentry, struct mtd_device, link);
1384 list_for_each(pentry, &(*dev)->parts) {
1385 *part = list_entry(pentry, struct part_info, link);
1386 if (strcmp((*part)->name, id) == 0)
1387 return 0;
1388 (*part_num)++;
1389 }
1390 }
1391
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001392 p = id;
1393 *dev = NULL;
1394 *part = NULL;
1395 *part_num = 0;
1396
1397 if (id_parse(p, &p, &type, &dnum) != 0)
1398 return 1;
1399
1400 if ((*p++ != ',') || (*p == '\0')) {
1401 printf("no partition number specified\n");
1402 return 1;
1403 }
1404 pnum = simple_strtoul(p, (char **)&p, 0);
1405 if (*p != '\0') {
1406 printf("unexpected trailing character '%c'\n", *p);
1407 return 1;
1408 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001409
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001410 if ((*dev = device_find(type, dnum)) == NULL) {
1411 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1412 return 1;
1413 }
1414
1415 if ((*part = jffs2_part_info(*dev, pnum)) == NULL) {
1416 printf("no such partition\n");
1417 *dev = NULL;
1418 return 1;
1419 }
1420
1421 *part_num = pnum;
1422
1423 return 0;
1424}
1425
1426/**
1427 * Find and delete partition. For partition id format see find_dev_and_part().
1428 *
1429 * @param id string describing device and partition
1430 * @return 0 on success, 1 otherwise
1431 */
1432static int delete_partition(const char *id)
1433{
1434 u8 pnum;
1435 struct mtd_device *dev;
1436 struct part_info *part;
1437
1438 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1439
1440 DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n",
1441 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1442 part->name, part->size, part->offset);
1443
1444 if (part_del(dev, part) != 0)
1445 return 1;
1446
1447 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1448 printf("generated mtdparts too long, reseting to null\n");
1449 return 1;
1450 }
1451 return 0;
1452 }
1453
1454 printf("partition %s not found\n", id);
1455 return 1;
1456}
1457
1458/**
1459 * Accept character string describing mtd partitions and call device_parse()
1460 * for each entry. Add created devices to the global devices list.
1461 *
1462 * @param mtdparts string specifing mtd partitions
1463 * @return 0 on success, 1 otherwise
1464 */
1465static int parse_mtdparts(const char *const mtdparts)
1466{
1467 const char *p = mtdparts;
1468 struct mtd_device *dev;
1469 int err = 1;
1470
1471 DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1472
1473 /* delete all devices and partitions */
1474 if (devices_init() != 0) {
1475 printf("could not initialise device list\n");
1476 return err;
1477 }
1478
1479 /* re-read 'mtdparts' variable, devices_init may be updating env */
1480 p = getenv("mtdparts");
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001481
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001482 if (strncmp(p, "mtdparts=", 9) != 0) {
1483 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1484 return err;
1485 }
1486 p += 9;
1487
1488 while (p && (*p != '\0')) {
1489 err = 1;
1490 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1491 break;
1492
1493 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1494 dev->id->num, dev->id->mtd_id);
1495
1496 /* check if parsed device is already on the list */
1497 if (device_find(dev->id->type, dev->id->num) != NULL) {
1498 printf("device %s%d redefined, please correct mtdparts variable\n",
1499 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1500 break;
1501 }
1502
1503 list_add_tail(&dev->link, &devices);
1504 err = 0;
1505 }
1506 if (err == 1) {
1507 device_delall(&devices);
1508 return 1;
1509 }
1510
1511 return 0;
1512}
1513
1514/**
1515 * Parse provided string describing mtdids mapping (see file header for mtdids
1516 * variable format). Allocate memory for each entry and add all found entries
1517 * to the global mtdids list.
1518 *
1519 * @param ids mapping string
1520 * @return 0 on success, 1 otherwise
1521 */
1522static int parse_mtdids(const char *const ids)
1523{
1524 const char *p = ids;
1525 const char *mtd_id;
1526 int mtd_id_len;
1527 struct mtdids *id;
1528 struct list_head *entry, *n;
1529 struct mtdids *id_tmp;
1530 u8 type, num;
1531 u32 size;
1532 int ret = 1;
1533
1534 DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
1535
1536 /* clean global mtdids list */
1537 list_for_each_safe(entry, n, &mtdids) {
1538 id_tmp = list_entry(entry, struct mtdids, link);
1539 DEBUGF("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
1540 list_del(entry);
1541 free(id_tmp);
1542 }
1543 last_ids[0] = '\0';
1544 INIT_LIST_HEAD(&mtdids);
1545
1546 while(p && (*p != '\0')) {
1547
1548 ret = 1;
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +09001549 /* parse 'nor'|'nand'|'onenand'<dev-num> */
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001550 if (id_parse(p, &p, &type, &num) != 0)
1551 break;
1552
1553 if (*p != '=') {
1554 printf("mtdids: incorrect <dev-num>\n");
1555 break;
1556 }
1557 p++;
1558
1559 /* check if requested device exists */
1560 if (device_validate(type, num, &size) != 0)
1561 return 1;
1562
1563 /* locate <mtd-id> */
1564 mtd_id = p;
1565 if ((p = strchr(mtd_id, ',')) != NULL) {
1566 mtd_id_len = p - mtd_id + 1;
1567 p++;
1568 } else {
1569 mtd_id_len = strlen(mtd_id) + 1;
1570 }
1571 if (mtd_id_len == 0) {
1572 printf("mtdids: no <mtd-id> identifier\n");
1573 break;
1574 }
1575
1576 /* check if this id is already on the list */
1577 int double_entry = 0;
1578 list_for_each(entry, &mtdids) {
1579 id_tmp = list_entry(entry, struct mtdids, link);
1580 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1581 double_entry = 1;
1582 break;
1583 }
1584 }
1585 if (double_entry) {
1586 printf("device id %s%d redefined, please correct mtdids variable\n",
1587 MTD_DEV_TYPE(type), num);
1588 break;
1589 }
1590
1591 /* allocate mtdids structure */
1592 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1593 printf("out of memory\n");
1594 break;
1595 }
1596 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1597 id->num = num;
1598 id->type = type;
1599 id->size = size;
1600 id->mtd_id = (char *)(id + 1);
1601 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1602 id->mtd_id[mtd_id_len - 1] = '\0';
1603 INIT_LIST_HEAD(&id->link);
1604
1605 DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
1606 MTD_DEV_TYPE(id->type), id->num,
1607 id->size, id->mtd_id);
1608
1609 list_add_tail(&id->link, &mtdids);
1610 ret = 0;
1611 }
1612 if (ret == 1) {
1613 /* clean mtdids list and free allocated memory */
1614 list_for_each_safe(entry, n, &mtdids) {
1615 id_tmp = list_entry(entry, struct mtdids, link);
1616 list_del(entry);
1617 free(id_tmp);
1618 }
1619 return 1;
1620 }
1621
1622 return 0;
1623}
1624
1625/**
1626 * Parse and initialize global mtdids mapping and create global
1627 * device/partition list.
1628 *
1629 * @return 0 on success, 1 otherwise
1630 */
1631int mtdparts_init(void)
1632{
1633 static int initialized = 0;
1634 const char *ids, *parts;
1635 const char *current_partition;
1636 int ids_changed;
1637 char tmp_ep[PARTITION_MAXLEN];
1638
1639 DEBUGF("\n---mtdparts_init---\n");
1640 if (!initialized) {
1641 INIT_LIST_HEAD(&mtdids);
1642 INIT_LIST_HEAD(&devices);
1643 memset(last_ids, 0, MTDIDS_MAXLEN);
1644 memset(last_parts, 0, MTDPARTS_MAXLEN);
1645 memset(last_partition, 0, PARTITION_MAXLEN);
1646 initialized = 1;
1647 }
1648
1649 /* get variables */
1650 ids = getenv("mtdids");
1651 parts = getenv("mtdparts");
1652 current_partition = getenv("partition");
1653
1654 /* save it for later parsing, cannot rely on current partition pointer
1655 * as 'partition' variable may be updated during init */
1656 tmp_ep[0] = '\0';
1657 if (current_partition)
1658 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1659
1660 DEBUGF("last_ids : %s\n", last_ids);
1661 DEBUGF("env_ids : %s\n", ids);
1662 DEBUGF("last_parts: %s\n", last_parts);
1663 DEBUGF("env_parts : %s\n\n", parts);
1664
1665 DEBUGF("last_partition : %s\n", last_partition);
1666 DEBUGF("env_partition : %s\n", current_partition);
1667
1668 /* if mtdids varible is empty try to use defaults */
1669 if (!ids) {
1670 if (mtdids_default) {
1671 DEBUGF("mtdids variable not defined, using default\n");
1672 ids = mtdids_default;
1673 setenv("mtdids", (char *)ids);
1674 } else {
1675 printf("mtdids not defined, no default present\n");
1676 return 1;
1677 }
1678 }
1679 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1680 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1681 return 1;
1682 }
1683
1684 /* do no try to use defaults when mtdparts variable is not defined,
1685 * just check the length */
1686 if (!parts)
1687 printf("mtdparts variable not set, see 'help mtdparts'\n");
1688
1689 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1690 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1691 return 1;
1692 }
1693
1694 /* check if we have already parsed those mtdids */
1695 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1696 ids_changed = 0;
1697 } else {
1698 ids_changed = 1;
1699
1700 if (parse_mtdids(ids) != 0) {
Marian Balakowicz7d454772005-09-11 15:27:12 +02001701 devices_init();
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001702 return 1;
1703 }
1704
1705 /* ok it's good, save new ids */
1706 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1707 }
1708
1709 /* parse partitions if either mtdparts or mtdids were updated */
1710 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1711 if (parse_mtdparts(parts) != 0)
1712 return 1;
1713
1714 if (list_empty(&devices)) {
1715 printf("mtdparts_init: no valid partitions\n");
1716 return 1;
1717 }
1718
1719 /* ok it's good, save new parts */
1720 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1721
1722 /* reset first partition from first dev from the list as current */
1723 current_dev = list_entry(devices.next, struct mtd_device, link);
1724 current_partnum = 0;
1725 current_save();
1726
1727 DEBUGF("mtdparts_init: current_dev = %s%d, current_partnum = %d\n",
1728 MTD_DEV_TYPE(current_dev->id->type),
1729 current_dev->id->num, current_partnum);
1730 }
1731
1732 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1733 if (!parts && (last_parts[0] != '\0'))
1734 return devices_init();
1735
1736 /* do not process current partition if mtdparts variable is null */
1737 if (!parts)
1738 return 0;
1739
1740 /* is current partition set in environment? if so, use it */
1741 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1742 struct part_info *p;
1743 struct mtd_device *cdev;
1744 u8 pnum;
1745
1746 DEBUGF("--- getting current partition: %s\n", tmp_ep);
1747
1748 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
1749 current_dev = cdev;
1750 current_partnum = pnum;
1751 current_save();
1752 }
1753 } else if (getenv("partition") == NULL) {
1754 DEBUGF("no partition variable set, setting...\n");
1755 current_save();
1756 }
1757
1758 return 0;
1759}
1760#else /* #ifdef CONFIG_JFFS2_CMDLINE */
1761/*
1762 * 'Static' version of command line mtdparts_init() routine. Single partition on
1763 * a single device configuration.
1764 */
1765
1766/**
1767 * Parse and initialize global mtdids mapping and create global
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02001768 * device/partition list.
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001769 *
1770 * @return 0 on success, 1 otherwise
1771 */
1772int mtdparts_init(void)
1773{
1774 static int initialized = 0;
1775 u32 size;
1776 char *dev_name;
1777
1778 DEBUGF("\n---mtdparts_init---\n");
1779 if (!initialized) {
Wolfgang Denkc4e0e682005-08-09 21:41:20 +02001780 struct mtdids *id;
1781 struct part_info *part;
1782
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001783 initialized = 1;
1784 current_dev = (struct mtd_device *)
1785 malloc(sizeof(struct mtd_device) +
1786 sizeof(struct part_info) +
1787 sizeof(struct mtdids));
1788 if (!current_dev) {
1789 printf("out of memory\n");
1790 return 1;
1791 }
1792 memset(current_dev, 0, sizeof(struct mtd_device) +
1793 sizeof(struct part_info) + sizeof(struct mtdids));
1794
Wolfgang Denkc4e0e682005-08-09 21:41:20 +02001795 id = (struct mtdids *)(current_dev + 1);
1796 part = (struct part_info *)(id + 1);
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001797
1798 /* id */
1799 id->mtd_id = "single part";
1800
1801#if defined(CONFIG_JFFS2_DEV)
1802 dev_name = CONFIG_JFFS2_DEV;
1803#else
1804 dev_name = "nor0";
1805#endif
1806
1807 if ((id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
1808 (device_validate(id->type, id->num, &size) != 0)) {
1809 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
1810 free(current_dev);
1811 return 1;
1812 }
1813 id->size = size;
1814 INIT_LIST_HEAD(&id->link);
1815
1816 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
1817 id->type, id->num, id->size, id->mtd_id);
1818
1819 /* partition */
1820 part->name = "static";
1821 part->auto_name = 0;
1822
1823#if defined(CONFIG_JFFS2_PART_SIZE)
1824 part->size = CONFIG_JFFS2_PART_SIZE;
1825#else
1826 part->size = SIZE_REMAINING;
1827#endif
1828
1829#if defined(CONFIG_JFFS2_PART_OFFSET)
1830 part->offset = CONFIG_JFFS2_PART_OFFSET;
1831#else
1832 part->offset = 0x00000000;
1833#endif
1834
1835 part->dev = current_dev;
1836 INIT_LIST_HEAD(&part->link);
1837
1838 /* recalculate size if needed */
1839 if (part->size == SIZE_REMAINING)
1840 part->size = id->size - part->offset;
1841
1842 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
1843 part->name, part->size, part->offset);
1844
1845 /* device */
1846 current_dev->id = id;
1847 INIT_LIST_HEAD(&current_dev->link);
1848 current_dev->num_parts = 1;
1849 INIT_LIST_HEAD(&current_dev->parts);
1850 list_add(&part->link, &current_dev->parts);
1851 }
1852
1853 return 0;
1854}
1855#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1856
1857/**
1858 * Return pointer to the partition of a requested number from a requested
1859 * device.
1860 *
1861 * @param dev device that is to be searched for a partition
1862 * @param part_num requested partition number
1863 * @return pointer to the part_info, NULL otherwise
1864 */
1865static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
1866{
1867 struct list_head *entry;
1868 struct part_info *part;
1869 int num;
1870
1871 if (!dev)
1872 return NULL;
1873
1874 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
1875 part_num, MTD_DEV_TYPE(dev->id->type),
1876 dev->id->num, dev->id->mtd_id);
1877
1878 if (part_num >= dev->num_parts) {
1879 printf("invalid partition number %d for device %s%d (%s)\n",
1880 part_num, MTD_DEV_TYPE(dev->id->type),
1881 dev->id->num, dev->id->mtd_id);
1882 return NULL;
1883 }
1884
1885 /* locate partition number, return it */
1886 num = 0;
1887 list_for_each(entry, &dev->parts) {
1888 part = list_entry(entry, struct part_info, link);
1889
1890 if (part_num == num++) {
1891 return part;
1892 }
1893 }
1894
1895 return NULL;
1896}
1897
Michal Simekab4b9562007-08-06 23:31:49 +02001898/***************************************************/
1899/* U-boot commands */
1900/***************************************************/
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001901
1902/**
1903 * Routine implementing fsload u-boot command. This routine tries to load
1904 * a requested file from jffs2/cramfs filesystem on a current partition.
1905 *
1906 * @param cmdtp command internal data
1907 * @param flag command flag
1908 * @param argc number of arguments supplied to the command
1909 * @param argv arguments list
1910 * @return 0 on success, 1 otherwise
1911 */
1912int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1913{
wdenk39539882004-07-01 16:30:44 +00001914 char *fsname;
wdenkf39748a2004-06-09 13:37:52 +00001915 char *filename;
wdenk39539882004-07-01 16:30:44 +00001916 int size;
1917 struct part_info *part;
1918 ulong offset = load_addr;
wdenkf39748a2004-06-09 13:37:52 +00001919
1920 /* pre-set Boot file name */
1921 if ((filename = getenv("bootfile")) == NULL) {
1922 filename = "uImage";
1923 }
1924
wdenkc6097192002-11-03 00:24:07 +00001925 if (argc == 2) {
1926 filename = argv[1];
1927 }
1928 if (argc == 3) {
1929 offset = simple_strtoul(argv[1], NULL, 16);
wdenk4b9206e2004-03-23 22:14:11 +00001930 load_addr = offset;
wdenkc6097192002-11-03 00:24:07 +00001931 filename = argv[2];
1932 }
1933
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001934 /* make sure we are in sync with env variables */
1935 if (mtdparts_init() !=0)
1936 return 1;
1937
1938 if ((part = jffs2_part_info(current_dev, current_partnum))){
wdenkc6097192002-11-03 00:24:07 +00001939
Michal Simek991b0892007-09-15 00:03:35 +02001940 /* check partition type for cramfs */
1941 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
wdenkdd875c72004-01-03 21:24:46 +00001942 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
1943
1944 if (cramfs_check(part)) {
1945 size = cramfs_load ((char *) offset, part, filename);
1946 } else {
Michal Simek991b0892007-09-15 00:03:35 +02001947 /* if this is not cramfs assume jffs2 */
wdenkdd875c72004-01-03 21:24:46 +00001948 size = jffs2_1pass_load((char *)offset, part, filename);
1949 }
wdenkc6097192002-11-03 00:24:07 +00001950
1951 if (size > 0) {
1952 char buf[10];
wdenkdd875c72004-01-03 21:24:46 +00001953 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
1954 fsname, size, offset);
wdenkc6097192002-11-03 00:24:07 +00001955 sprintf(buf, "%x", size);
1956 setenv("filesize", buf);
1957 } else {
wdenkdd875c72004-01-03 21:24:46 +00001958 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
wdenkc6097192002-11-03 00:24:07 +00001959 }
1960
1961 return !(size > 0);
1962 }
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02001963 return 1;
wdenkc6097192002-11-03 00:24:07 +00001964}
1965
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001966/**
1967 * Routine implementing u-boot ls command which lists content of a given
1968 * directory on a current partition.
1969 *
1970 * @param cmdtp command internal data
1971 * @param flag command flag
1972 * @param argc number of arguments supplied to the command
1973 * @param argv arguments list
1974 * @return 0 on success, 1 otherwise
1975 */
1976int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenkc6097192002-11-03 00:24:07 +00001977{
wdenka87589d2005-06-10 10:00:19 +00001978 char *filename = "/";
wdenkc6097192002-11-03 00:24:07 +00001979 int ret;
1980 struct part_info *part;
1981
1982 if (argc == 2)
1983 filename = argv[1];
1984
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001985 /* make sure we are in sync with env variables */
1986 if (mtdparts_init() !=0)
1987 return 1;
1988
1989 if ((part = jffs2_part_info(current_dev, current_partnum))){
wdenkc6097192002-11-03 00:24:07 +00001990
wdenkdd875c72004-01-03 21:24:46 +00001991 /* check partition type for cramfs */
1992 if (cramfs_check(part)) {
1993 ret = cramfs_ls (part, filename);
1994 } else {
Michal Simek991b0892007-09-15 00:03:35 +02001995 /* if this is not cramfs assume jffs2 */
wdenkdd875c72004-01-03 21:24:46 +00001996 ret = jffs2_1pass_ls(part, filename);
1997 }
wdenkc6097192002-11-03 00:24:07 +00001998
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02001999 return ret ? 0 : 1;
wdenkc6097192002-11-03 00:24:07 +00002000 }
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02002001 return 1;
wdenkc6097192002-11-03 00:24:07 +00002002}
2003
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002004/**
2005 * Routine implementing u-boot fsinfo command. This routine prints out
2006 * miscellaneous filesystem informations/statistics.
2007 *
2008 * @param cmdtp command internal data
2009 * @param flag command flag
2010 * @param argc number of arguments supplied to the command
2011 * @param argv arguments list
2012 * @return 0 on success, 1 otherwise
2013 */
2014int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenkc6097192002-11-03 00:24:07 +00002015{
wdenkc6097192002-11-03 00:24:07 +00002016 struct part_info *part;
Michal Simek991b0892007-09-15 00:03:35 +02002017 char *fsname;
wdenkdd875c72004-01-03 21:24:46 +00002018 int ret;
wdenkc6097192002-11-03 00:24:07 +00002019
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002020 /* make sure we are in sync with env variables */
2021 if (mtdparts_init() !=0)
2022 return 1;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02002023
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002024 if ((part = jffs2_part_info(current_dev, current_partnum))){
wdenkc6097192002-11-03 00:24:07 +00002025
wdenkdd875c72004-01-03 21:24:46 +00002026 /* check partition type for cramfs */
Michal Simek991b0892007-09-15 00:03:35 +02002027 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
2028 printf("### filesystem type is %s\n", fsname);
wdenkdd875c72004-01-03 21:24:46 +00002029
2030 if (cramfs_check(part)) {
2031 ret = cramfs_info (part);
2032 } else {
Michal Simek991b0892007-09-15 00:03:35 +02002033 /* if this is not cramfs assume jffs2 */
wdenkdd875c72004-01-03 21:24:46 +00002034 ret = jffs2_1pass_info(part);
2035 }
wdenkc6097192002-11-03 00:24:07 +00002036
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02002037 return ret ? 0 : 1;
wdenkc6097192002-11-03 00:24:07 +00002038 }
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02002039 return 1;
wdenkc6097192002-11-03 00:24:07 +00002040}
2041
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002042/* command line only */
2043#ifdef CONFIG_JFFS2_CMDLINE
2044/**
2045 * Routine implementing u-boot chpart command. Sets new current partition based
2046 * on the user supplied partition id. For partition id format see find_dev_and_part().
2047 *
2048 * @param cmdtp command internal data
2049 * @param flag command flag
2050 * @param argc number of arguments supplied to the command
2051 * @param argv arguments list
2052 * @return 0 on success, 1 otherwise
2053 */
2054int do_jffs2_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenkc6097192002-11-03 00:24:07 +00002055{
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002056/* command line only */
2057 struct mtd_device *dev;
2058 struct part_info *part;
2059 u8 pnum;
wdenkc6097192002-11-03 00:24:07 +00002060
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002061 if (mtdparts_init() !=0)
2062 return 1;
wdenkc6097192002-11-03 00:24:07 +00002063
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002064 if (argc < 2) {
2065 printf("no partition id specified\n");
2066 return 1;
wdenkc6097192002-11-03 00:24:07 +00002067 }
2068
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002069 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
2070 return 1;
wdenkc6097192002-11-03 00:24:07 +00002071
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002072 current_dev = dev;
2073 current_partnum = pnum;
2074 current_save();
2075
2076 printf("partition changed to %s%d,%d\n",
2077 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
2078
wdenkc6097192002-11-03 00:24:07 +00002079 return 0;
wdenkc6097192002-11-03 00:24:07 +00002080}
wdenkeedcd072004-09-08 22:03:11 +00002081
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002082/**
2083 * Routine implementing u-boot mtdparts command. Initialize/update default global
2084 * partition list and process user partition request (list, add, del).
2085 *
2086 * @param cmdtp command internal data
2087 * @param flag command flag
2088 * @param argc number of arguments supplied to the command
2089 * @param argv arguments list
2090 * @return 0 on success, 1 otherwise
2091 */
2092int do_jffs2_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2093{
2094 if (argc == 2) {
2095 if (strcmp(argv[1], "default") == 0) {
2096 setenv("mtdids", (char *)mtdids_default);
2097 setenv("mtdparts", (char *)mtdparts_default);
2098 setenv("partition", NULL);
2099
2100 mtdparts_init();
2101 return 0;
2102 } else if (strcmp(argv[1], "delall") == 0) {
2103 /* this may be the first run, initialize lists if needed */
2104 mtdparts_init();
2105
2106 setenv("mtdparts", NULL);
2107
2108 /* devices_init() calls current_save() */
2109 return devices_init();
2110 }
2111 }
2112
2113 /* make sure we are in sync with env variables */
2114 if (mtdparts_init() != 0)
2115 return 1;
2116
2117 if (argc == 1) {
2118 list_partitions();
2119 return 0;
2120 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02002121
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002122 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
2123 if (((argc == 5) || (argc == 6)) && (strcmp(argv[1], "add") == 0)) {
2124#define PART_ADD_DESC_MAXLEN 64
2125 char tmpbuf[PART_ADD_DESC_MAXLEN];
2126 u8 type, num, len;
2127 struct mtd_device *dev;
2128 struct mtd_device *dev_tmp;
2129 struct mtdids *id;
2130 struct part_info *p;
2131
2132 if (id_parse(argv[2], NULL, &type, &num) != 0)
2133 return 1;
2134
2135 if ((id = id_find(type, num)) == NULL) {
2136 printf("no such device %s defined in mtdids variable\n", argv[2]);
2137 return 1;
2138 }
2139
2140 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
2141 len += strlen(argv[3]); /* size@offset */
2142 len += strlen(argv[4]) + 2; /* '(' name ')' */
2143 if (argv[5] && (strlen(argv[5]) == 2))
2144 len += 2; /* 'ro' */
2145
2146 if (len >= PART_ADD_DESC_MAXLEN) {
2147 printf("too long partition description\n");
2148 return 1;
2149 }
2150 sprintf(tmpbuf, "%s:%s(%s)%s",
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +02002151 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002152 DEBUGF("add tmpbuf: %s\n", tmpbuf);
2153
2154 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
2155 return 1;
2156
2157 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
2158 dev->id->num, dev->id->mtd_id);
2159
2160 if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) {
2161 device_add(dev);
2162 } else {
2163 /* merge new partition with existing ones*/
2164 p = list_entry(dev->parts.next, struct part_info, link);
2165 if (part_add(dev_tmp, p) != 0) {
2166 device_del(dev);
2167 return 1;
2168 }
2169 }
2170
2171 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
2172 printf("generated mtdparts too long, reseting to null\n");
2173 return 1;
2174 }
2175
2176 return 0;
2177 }
2178
2179 /* mtdparts del part-id */
2180 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
2181 DEBUGF("del: part-id = %s\n", argv[2]);
2182
2183 return delete_partition(argv[2]);
2184 }
2185
2186 printf ("Usage:\n%s\n", cmdtp->usage);
2187 return 1;
2188}
2189#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
wdenk8bde7f72003-06-27 21:31:46 +00002190
2191/***************************************************/
wdenk0d498392003-07-01 21:06:45 +00002192U_BOOT_CMD(
2193 fsload, 3, 0, do_jffs2_fsload,
wdenkaa5590b2004-06-09 12:42:26 +00002194 "fsload\t- load binary file from a filesystem image\n",
wdenk8bde7f72003-06-27 21:31:46 +00002195 "[ off ] [ filename ]\n"
2196 " - load binary file from flash bank\n"
2197 " with offset 'off'\n"
2198);
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002199U_BOOT_CMD(
2200 ls, 2, 1, do_jffs2_ls,
2201 "ls\t- list files in a directory (default /)\n",
2202 "[ directory ]\n"
2203 " - list files in a directory.\n"
2204);
wdenk8bde7f72003-06-27 21:31:46 +00002205
wdenk0d498392003-07-01 21:06:45 +00002206U_BOOT_CMD(
2207 fsinfo, 1, 1, do_jffs2_fsinfo,
wdenkaa5590b2004-06-09 12:42:26 +00002208 "fsinfo\t- print information about filesystems\n",
wdenk8bde7f72003-06-27 21:31:46 +00002209 " - print information about filesystems\n"
2210);
2211
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002212#ifdef CONFIG_JFFS2_CMDLINE
wdenk0d498392003-07-01 21:06:45 +00002213U_BOOT_CMD(
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002214 chpart, 2, 0, do_jffs2_chpart,
2215 "chpart\t- change active partition\n",
2216 "part-id\n"
2217 " - change active partition (e.g. part-id = nand0,1)\n"
wdenk8bde7f72003-06-27 21:31:46 +00002218);
2219
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002220U_BOOT_CMD(
2221 mtdparts, 6, 0, do_jffs2_mtdparts,
2222 "mtdparts- define flash/nand partitions\n",
2223 "\n"
2224 " - list partition table\n"
2225 "mtdparts delall\n"
2226 " - delete all partitions\n"
2227 "mtdparts del part-id\n"
2228 " - delete partition (e.g. part-id = nand0,1)\n"
2229 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2230 " - add partition\n"
2231 "mtdparts default\n"
2232 " - reset partition table to defaults\n\n"
2233 "-----\n\n"
2234 "this command uses three environment variables:\n\n"
2235 "'partition' - keeps current partition identifier\n\n"
2236 "partition := <part-id>\n"
2237 "<part-id> := <dev-id>,part_num\n\n"
2238 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2239 "mtdids=<idmap>[,<idmap>,...]\n\n"
2240 "<idmap> := <dev-id>=<mtd-id>\n"
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +09002241 "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n"
Wolfgang Denk700a0c62005-08-08 01:03:24 +02002242 "<dev-num> := mtd device number, 0...\n"
2243 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2244 "'mtdparts' - partition list\n\n"
2245 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2246 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2247 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2248 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2249 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2250 "<offset> := partition start offset within the device\n"
2251 "<name> := '(' NAME ')'\n"
2252 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)\n"
2253);
2254#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2255
2256/***************************************************/