Blame view

cmd/mtdparts.c 53.8 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
68d7d6510   Stefan Roese   Separate mtdparts...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /*
   * (C) Copyright 2002
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   *
   * (C) Copyright 2002
   * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
   *
   * (C) Copyright 2003
   * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
   *
   * (C) Copyright 2005
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   *
   *   Added support for reading flash partition table from environment.
   *   Parsing routines are based on driver/mtd/cmdline.c from the linux 2.4
   *   kernel tree.
   *
ca75b20eb   Ben Gardiner   mtdparts: add new...
19
20
21
   * (C) Copyright 2008
   * Harald Welte, OpenMoko, Inc., Harald Welte <laforge@openmoko.org>
   *
68d7d6510   Stefan Roese   Separate mtdparts...
22
23
   *   $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
   *   Copyright 2002 SYSGO Real-Time Solutions GmbH
68d7d6510   Stefan Roese   Separate mtdparts...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
   */
  
  /*
   * Three environment variables are used by the parsing routines:
   *
   * 'partition' - keeps current partition identifier
   *
   * partition  := <part-id>
   * <part-id>  := <dev-id>,part_num
   *
   *
   * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
   *
   * mtdids=<idmap>[,<idmap>,...]
   *
   * <idmap>    := <dev-id>=<mtd-id>
00ac922db   Miquel Raynal   cmd: mtdparts: ac...
40
   * <dev-id>   := 'nand'|'nor'|'onenand'|'spi-nand'<dev-num>
68d7d6510   Stefan Roese   Separate mtdparts...
41
42
43
44
45
46
   * <dev-num>  := mtd device number, 0...
   * <mtd-id>   := unique device tag used by linux kernel to find mtd device (mtd->name)
   *
   *
   * 'mtdparts' - partition list
   *
d60aea94e   Miquel Raynal   cmd: mtdparts: re...
47
   * mtdparts=[mtdparts=]<mtd-def>[;<mtd-def>...]
68d7d6510   Stefan Roese   Separate mtdparts...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
   *
   * <mtd-def>  := <mtd-id>:<part-def>[,<part-def>...]
   * <mtd-id>   := unique device tag used by linux kernel to find mtd device (mtd->name)
   * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
   * <size>     := standard linux memsize OR '-' to denote all remaining space
   * <offset>   := partition start offset within the device
   * <name>     := '(' NAME ')'
   * <ro-flag>  := when set to 'ro' makes partition read-only (not used, passed to kernel)
   *
   * Notes:
   * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
   * - if the above variables are not set defaults for a given target are used
   *
   * Examples:
   *
   * 1 NOR Flash, with 1 single writable partition:
   * mtdids=nor0=edb7312-nor
d60aea94e   Miquel Raynal   cmd: mtdparts: re...
65
   * mtdparts=[mtdparts=]edb7312-nor:-
68d7d6510   Stefan Roese   Separate mtdparts...
66
67
68
   *
   * 1 NOR Flash with 2 partitions, 1 NAND with one
   * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
d60aea94e   Miquel Raynal   cmd: mtdparts: re...
69
   * mtdparts=[mtdparts=]edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
68d7d6510   Stefan Roese   Separate mtdparts...
70
71
   *
   */
68d7d6510   Stefan Roese   Separate mtdparts...
72
73
  #include <common.h>
  #include <command.h>
3a7d55716   Simon Glass   env: Move env_get...
74
  #include <env.h>
68d7d6510   Stefan Roese   Separate mtdparts...
75
  #include <malloc.h>
b93b24bf7   Ladislav Michl   Separate mtdparts...
76
  #include <jffs2/load_kernel.h>
68d7d6510   Stefan Roese   Separate mtdparts...
77
78
  #include <linux/list.h>
  #include <linux/ctype.h>
864aa034f   Stefan Roese   cmd_mtdparts: Mov...
79
80
  #include <linux/err.h>
  #include <linux/mtd/mtd.h>
68d7d6510   Stefan Roese   Separate mtdparts...
81
82
  
  #if defined(CONFIG_CMD_NAND)
6ae3900a8   Masahiro Yamada   mtd: nand: Rename...
83
  #include <linux/mtd/rawnand.h>
68d7d6510   Stefan Roese   Separate mtdparts...
84
  #include <nand.h>
68d7d6510   Stefan Roese   Separate mtdparts...
85
86
87
  #endif
  
  #if defined(CONFIG_CMD_ONENAND)
68d7d6510   Stefan Roese   Separate mtdparts...
88
89
90
  #include <linux/mtd/onenand.h>
  #include <onenand_uboot.h>
  #endif
a7eb1d66c   Joe Hershberger   mtd: Make mtdpart...
91
  DECLARE_GLOBAL_DATA_PTR;
68d7d6510   Stefan Roese   Separate mtdparts...
92
  /* special size referring to all the remaining space in a partition */
39ac34473   Paul Burton   cmd_mtdparts: use...
93
  #define SIZE_REMAINING		(~0llu)
68d7d6510   Stefan Roese   Separate mtdparts...
94
95
96
97
98
  
  /* special offset value, it is used when not provided by user
   *
   * this value is used temporarily during parsing, later such offests
   * are recalculated */
39ac34473   Paul Burton   cmd_mtdparts: use...
99
  #define OFFSET_NOT_SPECIFIED	(~0llu)
68d7d6510   Stefan Roese   Separate mtdparts...
100
101
102
103
104
105
106
107
108
  
  /* minimum partition size */
  #define MIN_PART_SIZE		4096
  
  /* this flag needs to be set in part_info struct mask_flags
   * field for read-only partitions */
  #define MTD_WRITEABLE_CMD		1
  
  /* default values for mtdids and mtdparts variables */
af3244365   Ladislav Michl   cmd: mtdparts: su...
109
  #if !defined(MTDIDS_DEFAULT)
0269dfae0   Maxime Ripard   cmd: Add Kconfig ...
110
111
112
  #ifdef CONFIG_MTDIDS_DEFAULT
  #define MTDIDS_DEFAULT CONFIG_MTDIDS_DEFAULT
  #else
af3244365   Ladislav Michl   cmd: mtdparts: su...
113
  #define MTDIDS_DEFAULT NULL
68d7d6510   Stefan Roese   Separate mtdparts...
114
  #endif
0269dfae0   Maxime Ripard   cmd: Add Kconfig ...
115
  #endif
af3244365   Ladislav Michl   cmd: mtdparts: su...
116
  #if !defined(MTDPARTS_DEFAULT)
0269dfae0   Maxime Ripard   cmd: Add Kconfig ...
117
118
119
  #ifdef CONFIG_MTDPARTS_DEFAULT
  #define MTDPARTS_DEFAULT CONFIG_MTDPARTS_DEFAULT
  #else
af3244365   Ladislav Michl   cmd: mtdparts: su...
120
121
  #define MTDPARTS_DEFAULT NULL
  #endif
0269dfae0   Maxime Ripard   cmd: Add Kconfig ...
122
  #endif
af3244365   Ladislav Michl   cmd: mtdparts: su...
123
124
  #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
  extern void board_mtdparts_default(const char **mtdids, const char **mtdparts);
68d7d6510   Stefan Roese   Separate mtdparts...
125
  #endif
af3244365   Ladislav Michl   cmd: mtdparts: su...
126
127
  static const char *mtdids_default = MTDIDS_DEFAULT;
  static const char *mtdparts_default = MTDPARTS_DEFAULT;
68d7d6510   Stefan Roese   Separate mtdparts...
128
129
130
131
132
  
  /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
  #define MTDIDS_MAXLEN		128
  #define MTDPARTS_MAXLEN		512
  #define PARTITION_MAXLEN	16
54f1792c8   Tom Rini   mtdparts: Fix var...
133
134
135
  static char last_ids[MTDIDS_MAXLEN + 1];
  static char last_parts[MTDPARTS_MAXLEN + 1];
  static char last_partition[PARTITION_MAXLEN + 1];
68d7d6510   Stefan Roese   Separate mtdparts...
136
137
138
139
140
  
  /* low level jffs2 cache cleaning routine */
  extern void jffs2_free_cache(struct part_info *part);
  
  /* mtdids mapping list, filled by parse_ids() */
088f1b199   Kim Phillips   common/cmd_*.c: s...
141
  static struct list_head mtdids;
68d7d6510   Stefan Roese   Separate mtdparts...
142
143
  
  /* device/partition list, parse_cmdline() parses into here */
088f1b199   Kim Phillips   common/cmd_*.c: s...
144
  static struct list_head devices;
68d7d6510   Stefan Roese   Separate mtdparts...
145
146
  
  /* current active device and partition number */
76b5883da   Stefan Roese   jffs2/mtdparts: F...
147
148
  struct mtd_device *current_mtd_dev = NULL;
  u8 current_mtd_partnum = 0;
68d7d6510   Stefan Roese   Separate mtdparts...
149

f8f744a3e   Ladislav Michl   cmd: mtdparts: us...
150
  u8 use_defaults;
68d7d6510   Stefan Roese   Separate mtdparts...
151
152
153
154
155
  static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
  
  /* command line only routines */
  static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
  static int device_del(struct mtd_device *dev);
75e8911f6   Han Xu   MLK-22259-2: cmd:...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  #ifdef CONFIG_MTDPARTS_SKIP_INVALID
  int skip_counter = 0;
  /*
   * find a seperator to locate the next entry
   * @param p pointer of the pointer of input char string
   * @param sp seperator charactor
   * @param n find the nth seperator
   * @param limit the looking scope
   * @return 1 on success, otherwise 0
   */
  static int find_seperator(const char **p, char sp, int n, int limit)
  {
  	int i, j;
  
  	/* n = 0 means do nothing */
  	if (!n)
  		return 1;
  
  	i = j = 0;
  
  	while (*p && (**p != '\0') && (i < limit)) {
  		if (**p == sp) {
  			(*p)++;
  			j++;
  			if (j == n)
  				return 1;
  		}
  		(*p)++;
  		i++;
  	}
  
  	return 0;
  }
  #endif
68d7d6510   Stefan Roese   Separate mtdparts...
190
191
192
193
194
195
196
197
198
199
200
201
  /**
   * Parses a string into a number.  The number stored at ptr is
   * potentially suffixed with K (for kilobytes, or 1024 bytes),
   * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
   * 1073741824).  If the number is suffixed with K, M, or G, then
   * the return value is the number multiplied by one kilobyte, one
   * megabyte, or one gigabyte, respectively.
   *
   * @param ptr where parse begins
   * @param retptr output pointer to next char after parse completes (output)
   * @return resulting unsigned int
   */
39ac34473   Paul Burton   cmd_mtdparts: use...
202
  static u64 memsize_parse (const char *const ptr, const char **retptr)
68d7d6510   Stefan Roese   Separate mtdparts...
203
  {
39ac34473   Paul Burton   cmd_mtdparts: use...
204
  	u64 ret = simple_strtoull(ptr, (char **)retptr, 0);
68d7d6510   Stefan Roese   Separate mtdparts...
205
206
207
208
209
  
  	switch (**retptr) {
  		case 'G':
  		case 'g':
  			ret <<= 10;
2b9ace552   Miquel Raynal   cmd: mtdparts: ad...
210
  			/* Fallthrough */
68d7d6510   Stefan Roese   Separate mtdparts...
211
212
213
  		case 'M':
  		case 'm':
  			ret <<= 10;
2b9ace552   Miquel Raynal   cmd: mtdparts: ad...
214
  			/* Fallthrough */
68d7d6510   Stefan Roese   Separate mtdparts...
215
216
217
218
  		case 'K':
  		case 'k':
  			ret <<= 10;
  			(*retptr)++;
2b9ace552   Miquel Raynal   cmd: mtdparts: ad...
219
  			/* Fallthrough */
68d7d6510   Stefan Roese   Separate mtdparts...
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  		default:
  			break;
  	}
  
  	return ret;
  }
  
  /**
   * Format string describing supplied size. This routine does the opposite job
   * to memsize_parse(). Size in bytes is converted to string and if possible
   * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
   *
   * Note, that this routine does not check for buffer overflow, it's the caller
   * who must assure enough space.
   *
   * @param buf output buffer
   * @param size size to be converted to string
   */
39ac34473   Paul Burton   cmd_mtdparts: use...
238
  static void memsize_format(char *buf, u64 size)
68d7d6510   Stefan Roese   Separate mtdparts...
239
240
241
242
243
244
  {
  #define SIZE_GB ((u32)1024*1024*1024)
  #define SIZE_MB ((u32)1024*1024)
  #define SIZE_KB ((u32)1024)
  
  	if ((size % SIZE_GB) == 0)
39ac34473   Paul Burton   cmd_mtdparts: use...
245
  		sprintf(buf, "%llug", size/SIZE_GB);
68d7d6510   Stefan Roese   Separate mtdparts...
246
  	else if ((size % SIZE_MB) == 0)
39ac34473   Paul Burton   cmd_mtdparts: use...
247
  		sprintf(buf, "%llum", size/SIZE_MB);
68d7d6510   Stefan Roese   Separate mtdparts...
248
  	else if (size % SIZE_KB == 0)
39ac34473   Paul Burton   cmd_mtdparts: use...
249
  		sprintf(buf, "%lluk", size/SIZE_KB);
68d7d6510   Stefan Roese   Separate mtdparts...
250
  	else
39ac34473   Paul Burton   cmd_mtdparts: use...
251
  		sprintf(buf, "%llu", size);
68d7d6510   Stefan Roese   Separate mtdparts...
252
253
254
255
256
257
258
259
260
  }
  
  /**
   * This routine does global indexing of all partitions. Resulting index for
   * current partition is saved in 'mtddevnum'. Current partition name in
   * 'mtddevname'.
   */
  static void index_partitions(void)
  {
68d7d6510   Stefan Roese   Separate mtdparts...
261
262
263
264
  	u16 mtddevnum;
  	struct part_info *part;
  	struct list_head *dentry;
  	struct mtd_device *dev;
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
265
266
  	debug("--- index partitions ---
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
267

76b5883da   Stefan Roese   jffs2/mtdparts: F...
268
  	if (current_mtd_dev) {
68d7d6510   Stefan Roese   Separate mtdparts...
269
270
271
  		mtddevnum = 0;
  		list_for_each(dentry, &devices) {
  			dev = list_entry(dentry, struct mtd_device, link);
76b5883da   Stefan Roese   jffs2/mtdparts: F...
272
273
  			if (dev == current_mtd_dev) {
  				mtddevnum += current_mtd_partnum;
018f53032   Simon Glass   env: Rename commo...
274
  				env_set_ulong("mtddevnum", mtddevnum);
54f1792c8   Tom Rini   mtdparts: Fix var...
275
276
  				debug("=> mtddevnum %d,
  ", mtddevnum);
68d7d6510   Stefan Roese   Separate mtdparts...
277
278
279
280
  				break;
  			}
  			mtddevnum += dev->num_parts;
  		}
76b5883da   Stefan Roese   jffs2/mtdparts: F...
281
  		part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
54f1792c8   Tom Rini   mtdparts: Fix var...
282
283
284
285
286
287
288
  		if (part) {
  			env_set("mtddevname", part->name);
  
  			debug("=> mtddevname %s
  ", part->name);
  		} else {
  			env_set("mtddevname", NULL);
68d7d6510   Stefan Roese   Separate mtdparts...
289

54f1792c8   Tom Rini   mtdparts: Fix var...
290
291
292
  			debug("=> mtddevname NULL
  ");
  		}
68d7d6510   Stefan Roese   Separate mtdparts...
293
  	} else {
382bee57f   Simon Glass   env: Rename seten...
294
295
  		env_set("mtddevnum", NULL);
  		env_set("mtddevname", NULL);
68d7d6510   Stefan Roese   Separate mtdparts...
296

e1d2950d0   Wolfgang Denk   mtdparts: get rid...
297
298
299
  		debug("=> mtddevnum NULL
  => mtddevname NULL
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
300
301
302
303
304
305
306
307
308
  	}
  }
  
  /**
   * Save current device and partition in environment variable 'partition'.
   */
  static void current_save(void)
  {
  	char buf[16];
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
309
310
  	debug("--- current_save ---
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
311

76b5883da   Stefan Roese   jffs2/mtdparts: F...
312
313
314
  	if (current_mtd_dev) {
  		sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type),
  					current_mtd_dev->id->num, current_mtd_partnum);
68d7d6510   Stefan Roese   Separate mtdparts...
315

382bee57f   Simon Glass   env: Rename seten...
316
  		env_set("partition", buf);
68d7d6510   Stefan Roese   Separate mtdparts...
317
  		strncpy(last_partition, buf, 16);
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
318
319
  		debug("=> partition %s
  ", buf);
68d7d6510   Stefan Roese   Separate mtdparts...
320
  	} else {
382bee57f   Simon Glass   env: Rename seten...
321
  		env_set("partition", NULL);
68d7d6510   Stefan Roese   Separate mtdparts...
322
  		last_partition[0] = '\0';
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
323
324
  		debug("=> partition NULL
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
325
326
327
  	}
  	index_partitions();
  }
0a026d3e8   Ben Gardiner   mtdparts: regroup...
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
  
  /**
   * Produce a mtd_info given a type and num.
   *
   * @param type mtd type
   * @param num mtd number
   * @param mtd a pointer to an mtd_info instance (output)
   * @return 0 if device is valid, 1 otherwise
   */
  static int get_mtd_info(u8 type, u8 num, struct mtd_info **mtd)
  {
  	char mtd_dev[16];
  
  	sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(type), num);
  	*mtd = get_mtd_device_nm(mtd_dev);
  	if (IS_ERR(*mtd)) {
  		printf("Device %s not found!
  ", mtd_dev);
  		return 1;
  	}
1668d6443   Heiko Schocher   mtdparts: fix use...
348
  	put_mtd_device(*mtd);
0a026d3e8   Ben Gardiner   mtdparts: regroup...
349
350
351
  
  	return 0;
  }
68d7d6510   Stefan Roese   Separate mtdparts...
352
  /**
864aa034f   Stefan Roese   cmd_mtdparts: Mov...
353
354
355
   * Performs sanity check for supplied flash partition.
   * Table of existing MTD flash devices is searched and partition device
   * is located. Alignment with the granularity of nand erasesize is verified.
68d7d6510   Stefan Roese   Separate mtdparts...
356
357
358
359
360
   *
   * @param id of the parent device
   * @param part partition to validate
   * @return 0 if partition is valid, 1 otherwise
   */
864aa034f   Stefan Roese   cmd_mtdparts: Mov...
361
  static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
68d7d6510   Stefan Roese   Separate mtdparts...
362
  {
0a026d3e8   Ben Gardiner   mtdparts: regroup...
363
  	struct mtd_info *mtd = NULL;
864aa034f   Stefan Roese   cmd_mtdparts: Mov...
364
365
  	int i, j;
  	ulong start;
39ac34473   Paul Burton   cmd_mtdparts: use...
366
  	u64 offset, size;
864aa034f   Stefan Roese   cmd_mtdparts: Mov...
367

0a026d3e8   Ben Gardiner   mtdparts: regroup...
368
  	if (get_mtd_info(id->type, id->num, &mtd))
68d7d6510   Stefan Roese   Separate mtdparts...
369
  		return 1;
68d7d6510   Stefan Roese   Separate mtdparts...
370

864aa034f   Stefan Roese   cmd_mtdparts: Mov...
371
  	part->sector_size = mtd->erasesize;
68d7d6510   Stefan Roese   Separate mtdparts...
372

864aa034f   Stefan Roese   cmd_mtdparts: Mov...
373
374
  	if (!mtd->numeraseregions) {
  		/*
00ac922db   Miquel Raynal   cmd: mtdparts: ac...
375
  		 * Only one eraseregion (NAND, SPI-NAND, OneNAND or uniform NOR),
864aa034f   Stefan Roese   cmd_mtdparts: Mov...
376
377
  		 * checking for alignment is easy here
  		 */
39ac34473   Paul Burton   cmd_mtdparts: use...
378
379
  		offset = part->offset;
  		if (do_div(offset, mtd->erasesize)) {
864aa034f   Stefan Roese   cmd_mtdparts: Mov...
380
381
382
383
384
385
  			printf("%s%d: partition (%s) start offset"
  			       "alignment incorrect
  ",
  			       MTD_DEV_TYPE(id->type), id->num, part->name);
  			return 1;
  		}
68d7d6510   Stefan Roese   Separate mtdparts...
386

39ac34473   Paul Burton   cmd_mtdparts: use...
387
388
  		size = part->size;
  		if (do_div(size, mtd->erasesize)) {
864aa034f   Stefan Roese   cmd_mtdparts: Mov...
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
  			printf("%s%d: partition (%s) size alignment incorrect
  ",
  			       MTD_DEV_TYPE(id->type), id->num, part->name);
  			return 1;
  		}
  	} else {
  		/*
  		 * Multiple eraseregions (non-uniform NOR),
  		 * checking for alignment is more complex here
  		 */
  
  		/* Check start alignment */
  		for (i = 0; i < mtd->numeraseregions; i++) {
  			start = mtd->eraseregions[i].offset;
  			for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
  				if (part->offset == start)
  					goto start_ok;
  				start += mtd->eraseregions[i].erasesize;
  			}
  		}
68d7d6510   Stefan Roese   Separate mtdparts...
409

68d7d6510   Stefan Roese   Separate mtdparts...
410
411
  		printf("%s%d: partition (%s) start offset alignment incorrect
  ",
864aa034f   Stefan Roese   cmd_mtdparts: Mov...
412
  		       MTD_DEV_TYPE(id->type), id->num, part->name);
68d7d6510   Stefan Roese   Separate mtdparts...
413
  		return 1;
68d7d6510   Stefan Roese   Separate mtdparts...
414

864aa034f   Stefan Roese   cmd_mtdparts: Mov...
415
  	start_ok:
68d7d6510   Stefan Roese   Separate mtdparts...
416

864aa034f   Stefan Roese   cmd_mtdparts: Mov...
417
418
419
420
421
422
423
424
425
426
427
428
  		/* Check end/size alignment */
  		for (i = 0; i < mtd->numeraseregions; i++) {
  			start = mtd->eraseregions[i].offset;
  			for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
  				if ((part->offset + part->size) == start)
  					goto end_ok;
  				start += mtd->eraseregions[i].erasesize;
  			}
  		}
  		/* Check last sector alignment */
  		if ((part->offset + part->size) == start)
  			goto end_ok;
68d7d6510   Stefan Roese   Separate mtdparts...
429

68d7d6510   Stefan Roese   Separate mtdparts...
430
431
  		printf("%s%d: partition (%s) size alignment incorrect
  ",
864aa034f   Stefan Roese   cmd_mtdparts: Mov...
432
  		       MTD_DEV_TYPE(id->type), id->num, part->name);
68d7d6510   Stefan Roese   Separate mtdparts...
433
  		return 1;
864aa034f   Stefan Roese   cmd_mtdparts: Mov...
434
435
436
  
  	end_ok:
  		return 0;
68d7d6510   Stefan Roese   Separate mtdparts...
437
438
439
  	}
  
  	return 0;
68d7d6510   Stefan Roese   Separate mtdparts...
440
441
442
443
  }
  
  
  /**
f501991dc   Otavio Salvador   mtd: Fix function...
444
445
446
   * Performs sanity check for supplied partition. Offset and size are
   * verified to be within valid range. Partition type is checked and
   * part_validate_eraseblock() is called with the argument of part.
68d7d6510   Stefan Roese   Separate mtdparts...
447
448
449
450
451
452
453
454
455
456
457
   *
   * @param id of the parent device
   * @param part partition to validate
   * @return 0 if partition is valid, 1 otherwise
   */
  static int part_validate(struct mtdids *id, struct part_info *part)
  {
  	if (part->size == SIZE_REMAINING)
  		part->size = id->size - part->offset;
  
  	if (part->offset > id->size) {
39ac34473   Paul Burton   cmd_mtdparts: use...
458
459
  		printf("%s: offset %08llx beyond flash size %08llx
  ",
68d7d6510   Stefan Roese   Separate mtdparts...
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
  				id->mtd_id, part->offset, id->size);
  		return 1;
  	}
  
  	if ((part->offset + part->size) <= part->offset) {
  		printf("%s%d: partition (%s) size too big
  ",
  				MTD_DEV_TYPE(id->type), id->num, part->name);
  		return 1;
  	}
  
  	if (part->offset + part->size > id->size) {
  		printf("%s: partitioning exceeds flash size
  ", id->mtd_id);
  		return 1;
  	}
864aa034f   Stefan Roese   cmd_mtdparts: Mov...
476
477
478
479
480
  	/*
  	 * Now we need to check if the partition starts and ends on
  	 * sector (eraseblock) regions
  	 */
  	return part_validate_eraseblock(id, part);
68d7d6510   Stefan Roese   Separate mtdparts...
481
482
483
  }
  
  /**
1f8b546f9   Robert P. J. Day   Fix some obvious ...
484
   * Delete selected partition from the partition list of the specified device.
68d7d6510   Stefan Roese   Separate mtdparts...
485
486
487
488
489
490
491
492
493
494
495
496
497
498
   *
   * @param dev device to delete partition from
   * @param part partition to delete
   * @return 0 on success, 1 otherwise
   */
  static int part_del(struct mtd_device *dev, struct part_info *part)
  {
  	u8 current_save_needed = 0;
  
  	/* if there is only one partition, remove whole device */
  	if (dev->num_parts == 1)
  		return device_del(dev);
  
  	/* otherwise just delete this partition */
76b5883da   Stefan Roese   jffs2/mtdparts: F...
499
  	if (dev == current_mtd_dev) {
68d7d6510   Stefan Roese   Separate mtdparts...
500
501
502
  		/* we are modyfing partitions for the current device,
  		 * update current */
  		struct part_info *curr_pi;
76b5883da   Stefan Roese   jffs2/mtdparts: F...
503
  		curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
68d7d6510   Stefan Roese   Separate mtdparts...
504
505
506
507
508
  
  		if (curr_pi) {
  			if (curr_pi == part) {
  				printf("current partition deleted, resetting current to 0
  ");
76b5883da   Stefan Roese   jffs2/mtdparts: F...
509
  				current_mtd_partnum = 0;
68d7d6510   Stefan Roese   Separate mtdparts...
510
  			} else if (part->offset <= curr_pi->offset) {
76b5883da   Stefan Roese   jffs2/mtdparts: F...
511
  				current_mtd_partnum--;
68d7d6510   Stefan Roese   Separate mtdparts...
512
513
514
515
  			}
  			current_save_needed = 1;
  		}
  	}
68d7d6510   Stefan Roese   Separate mtdparts...
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
  	list_del(&part->link);
  	free(part);
  	dev->num_parts--;
  
  	if (current_save_needed > 0)
  		current_save();
  	else
  		index_partitions();
  
  	return 0;
  }
  
  /**
   * Delete all partitions from parts head list, free memory.
   *
   * @param head list of partitions to delete
   */
  static void part_delall(struct list_head *head)
  {
  	struct list_head *entry, *n;
  	struct part_info *part_tmp;
  
  	/* clean tmp_list and free allocated memory */
  	list_for_each_safe(entry, n, head) {
  		part_tmp = list_entry(entry, struct part_info, link);
68d7d6510   Stefan Roese   Separate mtdparts...
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
  		list_del(entry);
  		free(part_tmp);
  	}
  }
  
  /**
   * Add new partition to the supplied partition list. Make sure partitions are
   * sorted by offset in ascending order.
   *
   * @param head list this partition is to be added to
   * @param new partition to be added
   */
  static int part_sort_add(struct mtd_device *dev, struct part_info *part)
  {
  	struct list_head *entry;
  	struct part_info *new_pi, *curr_pi;
  
  	/* link partition to parrent dev */
  	part->dev = dev;
  
  	if (list_empty(&dev->parts)) {
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
562
563
  		debug("part_sort_add: list empty
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
564
565
566
567
568
569
570
571
572
573
  		list_add(&part->link, &dev->parts);
  		dev->num_parts++;
  		index_partitions();
  		return 0;
  	}
  
  	new_pi = list_entry(&part->link, struct part_info, link);
  
  	/* get current partition info if we are updating current device */
  	curr_pi = NULL;
76b5883da   Stefan Roese   jffs2/mtdparts: F...
574
575
  	if (dev == current_mtd_dev)
  		curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
68d7d6510   Stefan Roese   Separate mtdparts...
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
  
  	list_for_each(entry, &dev->parts) {
  		struct part_info *pi;
  
  		pi = list_entry(entry, struct part_info, link);
  
  		/* be compliant with kernel cmdline, allow only one partition at offset zero */
  		if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
  			printf("cannot add second partition at offset 0
  ");
  			return 1;
  		}
  
  		if (new_pi->offset <= pi->offset) {
  			list_add_tail(&part->link, entry);
  			dev->num_parts++;
  
  			if (curr_pi && (pi->offset <= curr_pi->offset)) {
  				/* we are modyfing partitions for the current
  				 * device, update current */
76b5883da   Stefan Roese   jffs2/mtdparts: F...
596
  				current_mtd_partnum++;
68d7d6510   Stefan Roese   Separate mtdparts...
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
  				current_save();
  			} else {
  				index_partitions();
  			}
  			return 0;
  		}
  	}
  
  	list_add_tail(&part->link, &dev->parts);
  	dev->num_parts++;
  	index_partitions();
  	return 0;
  }
  
  /**
   * Add provided partition to the partition list of a given device.
   *
   * @param dev device to which partition is added
   * @param part partition to be added
   * @return 0 on success, 1 otherwise
   */
  static int part_add(struct mtd_device *dev, struct part_info *part)
  {
  	/* verify alignment and size */
  	if (part_validate(dev->id, part) != 0)
  		return 1;
  
  	/* partition is ok, add it to the list */
  	if (part_sort_add(dev, part) != 0)
  		return 1;
  
  	return 0;
  }
  
  /**
   * Parse one partition definition, allocate memory and return pointer to this
   * location in retpart.
   *
   * @param partdef pointer to the partition definition string i.e. <part-def>
   * @param ret output pointer to next char after parse completes (output)
   * @param retpart pointer to the allocated partition (output)
   * @return 0 on success, 1 otherwise
   */
  static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
  {
  	struct part_info *part;
39ac34473   Paul Burton   cmd_mtdparts: use...
643
644
  	u64 size;
  	u64 offset;
68d7d6510   Stefan Roese   Separate mtdparts...
645
646
647
648
649
650
651
652
653
654
655
656
  	const char *name;
  	int name_len;
  	unsigned int mask_flags;
  	const char *p;
  
  	p = partdef;
  	*retpart = NULL;
  	*ret = NULL;
  
  	/* fetch the partition size */
  	if (*p == '-') {
  		/* assign all remaining space to this partition */
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
657
658
  		debug("'-': remaining size assigned
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
659
660
661
662
663
  		size = SIZE_REMAINING;
  		p++;
  	} else {
  		size = memsize_parse(p, &p);
  		if (size < MIN_PART_SIZE) {
39ac34473   Paul Burton   cmd_mtdparts: use...
664
665
  			printf("partition size too small (%llx)
  ", size);
68d7d6510   Stefan Roese   Separate mtdparts...
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
  			return 1;
  		}
  	}
  
  	/* check for offset */
  	offset = OFFSET_NOT_SPECIFIED;
  	if (*p == '@') {
  		p++;
  		offset = memsize_parse(p, &p);
  	}
  
  	/* now look for the name */
  	if (*p == '(') {
  		name = ++p;
  		if ((p = strchr(name, ')')) == NULL) {
  			printf("no closing ) found in partition name
  ");
  			return 1;
  		}
  		name_len = p - name + 1;
  		if ((name_len - 1) == 0) {
  			printf("empty partition name
  ");
  			return 1;
  		}
  		p++;
  	} else {
  		/* 0x00000000@0x00000000 */
  		name_len = 22;
  		name = NULL;
  	}
  
  	/* test for options */
  	mask_flags = 0;
  	if (strncmp(p, "ro", 2) == 0) {
  		mask_flags |= MTD_WRITEABLE_CMD;
  		p += 2;
  	}
  
  	/* check for next partition definition */
  	if (*p == ',') {
  		if (size == SIZE_REMAINING) {
  			*ret = NULL;
  			printf("no partitions allowed after a fill-up partition
  ");
  			return 1;
  		}
  		*ret = ++p;
  	} else if ((*p == ';') || (*p == '\0')) {
  		*ret = p;
  	} else {
  		printf("unexpected character '%c' at the end of partition
  ", *p);
  		*ret = NULL;
  		return 1;
  	}
  
  	/*  allocate memory */
  	part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
  	if (!part) {
  		printf("out of memory
  ");
  		return 1;
  	}
  	memset(part, 0, sizeof(struct part_info) + name_len);
  	part->size = size;
  	part->offset = offset;
  	part->mask_flags = mask_flags;
  	part->name = (char *)(part + 1);
  
  	if (name) {
  		/* copy user provided name */
  		strncpy(part->name, name, name_len - 1);
  		part->auto_name = 0;
  	} else {
  		/* auto generated name in form of size@offset */
149c21b09   Kay Potthoff   mtdparts: fixed b...
742
  		snprintf(part->name, name_len, "0x%08llx@0x%08llx", size, offset);
68d7d6510   Stefan Roese   Separate mtdparts...
743
744
745
746
747
  		part->auto_name = 1;
  	}
  
  	part->name[name_len - 1] = '\0';
  	INIT_LIST_HEAD(&part->link);
39ac34473   Paul Burton   cmd_mtdparts: use...
748
749
  	debug("+ partition: name %-22s size 0x%08llx offset 0x%08llx mask flags %d
  ",
68d7d6510   Stefan Roese   Separate mtdparts...
750
751
752
753
754
755
756
757
758
759
  			part->name, part->size,
  			part->offset, part->mask_flags);
  
  	*retpart = part;
  	return 0;
  }
  
  /**
   * Check device number to be within valid range for given device type.
   *
0a026d3e8   Ben Gardiner   mtdparts: regroup...
760
761
762
   * @param type mtd type
   * @param num mtd number
   * @param size a pointer to the size of the mtd device (output)
68d7d6510   Stefan Roese   Separate mtdparts...
763
764
   * @return 0 if device is valid, 1 otherwise
   */
39ac34473   Paul Burton   cmd_mtdparts: use...
765
  static int mtd_device_validate(u8 type, u8 num, u64 *size)
68d7d6510   Stefan Roese   Separate mtdparts...
766
  {
0a026d3e8   Ben Gardiner   mtdparts: regroup...
767
  	struct mtd_info *mtd = NULL;
68d7d6510   Stefan Roese   Separate mtdparts...
768

0a026d3e8   Ben Gardiner   mtdparts: regroup...
769
  	if (get_mtd_info(type, num, &mtd))
864aa034f   Stefan Roese   cmd_mtdparts: Mov...
770
  		return 1;
68d7d6510   Stefan Roese   Separate mtdparts...
771

864aa034f   Stefan Roese   cmd_mtdparts: Mov...
772
  	*size = mtd->size;
68d7d6510   Stefan Roese   Separate mtdparts...
773

864aa034f   Stefan Roese   cmd_mtdparts: Mov...
774
  	return 0;
68d7d6510   Stefan Roese   Separate mtdparts...
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
  }
  
  /**
   * Delete all mtd devices from a supplied devices list, free memory allocated for
   * each device and delete all device partitions.
   *
   * @return 0 on success, 1 otherwise
   */
  static int device_delall(struct list_head *head)
  {
  	struct list_head *entry, *n;
  	struct mtd_device *dev_tmp;
  
  	/* clean devices list */
  	list_for_each_safe(entry, n, head) {
  		dev_tmp = list_entry(entry, struct mtd_device, link);
  		list_del(entry);
  		part_delall(&dev_tmp->parts);
  		free(dev_tmp);
  	}
  	INIT_LIST_HEAD(&devices);
  
  	return 0;
  }
  
  /**
   * If provided device exists it's partitions are deleted, device is removed
   * from device list and device memory is freed.
   *
   * @param dev device to be deleted
   * @return 0 on success, 1 otherwise
   */
  static int device_del(struct mtd_device *dev)
  {
  	part_delall(&dev->parts);
  	list_del(&dev->link);
  	free(dev);
76b5883da   Stefan Roese   jffs2/mtdparts: F...
812
  	if (dev == current_mtd_dev) {
68d7d6510   Stefan Roese   Separate mtdparts...
813
814
  		/* we just deleted current device */
  		if (list_empty(&devices)) {
76b5883da   Stefan Roese   jffs2/mtdparts: F...
815
  			current_mtd_dev = NULL;
68d7d6510   Stefan Roese   Separate mtdparts...
816
817
818
  		} else {
  			/* reset first partition from first dev from the
  			 * devices list as current */
76b5883da   Stefan Roese   jffs2/mtdparts: F...
819
820
  			current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
  			current_mtd_partnum = 0;
68d7d6510   Stefan Roese   Separate mtdparts...
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
  		}
  		current_save();
  		return 0;
  	}
  
  	index_partitions();
  	return 0;
  }
  
  /**
   * Search global device list and return pointer to the device of type and num
   * specified.
   *
   * @param type device type
   * @param num device number
   * @return NULL if requested device does not exist
   */
3c950e2eb   Anatolij Gustschin   fdt_support: add ...
838
  struct mtd_device *device_find(u8 type, u8 num)
68d7d6510   Stefan Roese   Separate mtdparts...
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
  {
  	struct list_head *entry;
  	struct mtd_device *dev_tmp;
  
  	list_for_each(entry, &devices) {
  		dev_tmp = list_entry(entry, struct mtd_device, link);
  
  		if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
  			return dev_tmp;
  	}
  
  	return NULL;
  }
  
  /**
   * Add specified device to the global device list.
   *
   * @param dev device to be added
   */
  static void device_add(struct mtd_device *dev)
  {
  	u8 current_save_needed = 0;
  
  	if (list_empty(&devices)) {
76b5883da   Stefan Roese   jffs2/mtdparts: F...
863
864
  		current_mtd_dev = dev;
  		current_mtd_partnum = 0;
68d7d6510   Stefan Roese   Separate mtdparts...
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
  		current_save_needed = 1;
  	}
  
  	list_add_tail(&dev->link, &devices);
  
  	if (current_save_needed > 0)
  		current_save();
  	else
  		index_partitions();
  }
  
  /**
   * Parse device type, name and mtd-id. If syntax is ok allocate memory and
   * return pointer to the device structure.
   *
   * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
   * @param ret output pointer to next char after parse completes (output)
   * @param retdev pointer to the allocated device (output)
   * @return 0 on success, 1 otherwise
   */
  static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
  {
  	struct mtd_device *dev;
  	struct part_info *part;
  	struct mtdids *id;
  	const char *mtd_id;
  	unsigned int mtd_id_len;
419a1fe95   Wolfgang Denk   CFI: fix warning:...
892
  	const char *p;
419a1fe95   Wolfgang Denk   CFI: fix warning:...
893
  	const char *pend;
68d7d6510   Stefan Roese   Separate mtdparts...
894
895
896
  	LIST_HEAD(tmp_list);
  	struct list_head *entry, *n;
  	u16 num_parts;
39ac34473   Paul Burton   cmd_mtdparts: use...
897
  	u64 offset;
68d7d6510   Stefan Roese   Separate mtdparts...
898
  	int err = 1;
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
899
900
  	debug("===device_parse===
  ");
2697eff1a   Wolfgang Denk   mtdparts: fix wri...
901
902
  
  	assert(retdev);
68d7d6510   Stefan Roese   Separate mtdparts...
903
  	*retdev = NULL;
68d7d6510   Stefan Roese   Separate mtdparts...
904

2697eff1a   Wolfgang Denk   mtdparts: fix wri...
905
906
  	if (ret)
  		*ret = NULL;
68d7d6510   Stefan Roese   Separate mtdparts...
907
908
  
  	/* fetch <mtd-id> */
2697eff1a   Wolfgang Denk   mtdparts: fix wri...
909
  	mtd_id = p = mtd_dev;
68d7d6510   Stefan Roese   Separate mtdparts...
910
911
912
913
914
915
916
917
918
919
920
921
922
923
  	if (!(p = strchr(mtd_id, ':'))) {
  		printf("no <mtd-id> identifier
  ");
  		return 1;
  	}
  	mtd_id_len = p - mtd_id + 1;
  	p++;
  
  	/* verify if we have a valid device specified */
  	if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
  		printf("invalid mtd device '%.*s'
  ", mtd_id_len - 1, mtd_id);
  		return 1;
  	}
419a1fe95   Wolfgang Denk   CFI: fix warning:...
924
  	pend = strchr(p, ';');
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
925
926
  	debug("dev type = %d (%s), dev num = %d, mtd-id = %s
  ",
68d7d6510   Stefan Roese   Separate mtdparts...
927
928
  			id->type, MTD_DEV_TYPE(id->type),
  			id->num, id->mtd_id);
5d69a5d17   Vasili Galka   Fix a few printf ...
929
930
  	debug("parsing partitions %.*s
  ", (int)(pend ? pend - p : strlen(p)), p);
68d7d6510   Stefan Roese   Separate mtdparts...
931

68d7d6510   Stefan Roese   Separate mtdparts...
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
  	/* parse partitions */
  	num_parts = 0;
  
  	offset = 0;
  	if ((dev = device_find(id->type, id->num)) != NULL) {
  		/* if device already exists start at the end of the last partition */
  		part = list_entry(dev->parts.prev, struct part_info, link);
  		offset = part->offset + part->size;
  	}
  
  	while (p && (*p != '\0') && (*p != ';')) {
  		err = 1;
  		if ((part_parse(p, &p, &part) != 0) || (!part))
  			break;
  
  		/* calculate offset when not specified */
  		if (part->offset == OFFSET_NOT_SPECIFIED)
  			part->offset = offset;
  		else
  			offset = part->offset;
  
  		/* verify alignment and size */
  		if (part_validate(id, part) != 0)
  			break;
  
  		offset += part->size;
  
  		/* partition is ok, add it to the list */
  		list_add_tail(&part->link, &tmp_list);
  		num_parts++;
  		err = 0;
  	}
  	if (err == 1) {
  		part_delall(&tmp_list);
  		return 1;
  	}
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
968
969
970
  	debug("
  total partitions: %d
  ", num_parts);
68d7d6510   Stefan Roese   Separate mtdparts...
971
972
973
974
  
  	/* check for next device presence */
  	if (p) {
  		if (*p == ';') {
2697eff1a   Wolfgang Denk   mtdparts: fix wri...
975
976
  			if (ret)
  				*ret = ++p;
68d7d6510   Stefan Roese   Separate mtdparts...
977
  		} else if (*p == '\0') {
2697eff1a   Wolfgang Denk   mtdparts: fix wri...
978
979
  			if (ret)
  				*ret = p;
68d7d6510   Stefan Roese   Separate mtdparts...
980
981
982
  		} else {
  			printf("unexpected character '%c' at the end of device
  ", *p);
2697eff1a   Wolfgang Denk   mtdparts: fix wri...
983
984
  			if (ret)
  				*ret = NULL;
68d7d6510   Stefan Roese   Separate mtdparts...
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
  			return 1;
  		}
  	}
  
  	/* allocate memory for mtd_device structure */
  	if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
  		printf("out of memory
  ");
  		return 1;
  	}
  	memset(dev, 0, sizeof(struct mtd_device));
  	dev->id = id;
  	dev->num_parts = 0; /* part_sort_add increments num_parts */
  	INIT_LIST_HEAD(&dev->parts);
  	INIT_LIST_HEAD(&dev->link);
  
  	/* move partitions from tmp_list to dev->parts */
  	list_for_each_safe(entry, n, &tmp_list) {
  		part = list_entry(entry, struct part_info, link);
  		list_del(entry);
  		if (part_sort_add(dev, part) != 0) {
  			device_del(dev);
  			return 1;
  		}
  	}
  
  	*retdev = dev;
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
1012
1013
1014
  	debug("===
  
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
  	return 0;
  }
  
  /**
   * Initialize global device list.
   *
   * @return 0 on success, 1 otherwise
   */
  static int mtd_devices_init(void)
  {
  	last_parts[0] = '\0';
76b5883da   Stefan Roese   jffs2/mtdparts: F...
1026
  	current_mtd_dev = NULL;
68d7d6510   Stefan Roese   Separate mtdparts...
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
  	current_save();
  
  	return device_delall(&devices);
  }
  
  /*
   * Search global mtdids list and find id of requested type and number.
   *
   * @return pointer to the id if it exists, NULL otherwise
   */
  static struct mtdids* id_find(u8 type, u8 num)
  {
  	struct list_head *entry;
  	struct mtdids *id;
  
  	list_for_each(entry, &mtdids) {
  		id = list_entry(entry, struct mtdids, link);
  
  		if ((id->type == type) && (id->num == num))
  			return id;
  	}
  
  	return NULL;
  }
  
  /**
   * Search global mtdids list and find id of a requested mtd_id.
   *
   * Note: first argument is not null terminated.
   *
   * @param mtd_id string containing requested mtd_id
   * @param mtd_id_len length of supplied mtd_id
   * @return pointer to the id if it exists, NULL otherwise
   */
  static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
  {
  	struct list_head *entry;
  	struct mtdids *id;
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
1065
1066
  	debug("--- id_find_by_mtd_id: '%.*s' (len = %d)
  ",
68d7d6510   Stefan Roese   Separate mtdparts...
1067
1068
1069
1070
  			mtd_id_len, mtd_id, mtd_id_len);
  
  	list_for_each(entry, &mtdids) {
  		id = list_entry(entry, struct mtdids, link);
5d69a5d17   Vasili Galka   Fix a few printf ...
1071
1072
  		debug("entry: '%s' (len = %zu)
  ",
68d7d6510   Stefan Roese   Separate mtdparts...
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
  				id->mtd_id, strlen(id->mtd_id));
  
  		if (mtd_id_len != strlen(id->mtd_id))
  			continue;
  		if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
  			return id;
  	}
  
  	return NULL;
  }
  
  /**
00ac922db   Miquel Raynal   cmd: mtdparts: ac...
1085
   * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'|'spi-nand'<dev-num>,
68d7d6510   Stefan Roese   Separate mtdparts...
1086
1087
1088
1089
1090
1091
1092
1093
   * return device type and number.
   *
   * @param id string describing device id
   * @param ret_id output pointer to next char after parse completes (output)
   * @param dev_type parsed device type (output)
   * @param dev_num parsed device number (output)
   * @return 0 on success, 1 otherwise
   */
088f1b199   Kim Phillips   common/cmd_*.c: s...
1094
1095
  int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type,
  		 u8 *dev_num)
68d7d6510   Stefan Roese   Separate mtdparts...
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
  {
  	const char *p = id;
  
  	*dev_type = 0;
  	if (strncmp(p, "nand", 4) == 0) {
  		*dev_type = MTD_DEV_TYPE_NAND;
  		p += 4;
  	} else if (strncmp(p, "nor", 3) == 0) {
  		*dev_type = MTD_DEV_TYPE_NOR;
  		p += 3;
  	} else if (strncmp(p, "onenand", 7) == 0) {
  		*dev_type = MTD_DEV_TYPE_ONENAND;
  		p += 7;
00ac922db   Miquel Raynal   cmd: mtdparts: ac...
1109
1110
1111
  	} else if (strncmp(p, "spi-nand", 8) == 0) {
  		*dev_type = MTD_DEV_TYPE_SPINAND;
  		p += 8;
68d7d6510   Stefan Roese   Separate mtdparts...
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
  	} else {
  		printf("incorrect device type in %s
  ", id);
  		return 1;
  	}
  
  	if (!isdigit(*p)) {
  		printf("incorrect device number in %s
  ", id);
  		return 1;
  	}
  
  	*dev_num = simple_strtoul(p, (char **)&p, 0);
  	if (ret_id)
  		*ret_id = p;
  	return 0;
  }
  
  /**
   * Process all devices and generate corresponding mtdparts string describing
   * all partitions on all devices.
   *
   * @param buf output buffer holding generated mtdparts string (output)
   * @param buflen buffer size
   * @return 0 on success, 1 otherwise
   */
  static int generate_mtdparts(char *buf, u32 buflen)
  {
  	struct list_head *pentry, *dentry;
  	struct mtd_device *dev;
  	struct part_info *part, *prev_part;
  	char *p = buf;
  	char tmpbuf[32];
39ac34473   Paul Burton   cmd_mtdparts: use...
1145
1146
  	u64 size, offset;
  	u32 len, part_cnt;
68d7d6510   Stefan Roese   Separate mtdparts...
1147
  	u32 maxlen = buflen - 1;
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
1148
1149
  	debug("--- generate_mtdparts ---
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
1150
1151
1152
1153
1154
  
  	if (list_empty(&devices)) {
  		buf[0] = '\0';
  		return 0;
  	}
68d7d6510   Stefan Roese   Separate mtdparts...
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
  	list_for_each(dentry, &devices) {
  		dev = list_entry(dentry, struct mtd_device, link);
  
  		/* copy mtd_id */
  		len = strlen(dev->id->mtd_id) + 1;
  		if (len > maxlen)
  			goto cleanup;
  		memcpy(p, dev->id->mtd_id, len - 1);
  		p += len - 1;
  		*(p++) = ':';
  		maxlen -= len;
  
  		/* format partitions */
  		prev_part = NULL;
  		part_cnt = 0;
  		list_for_each(pentry, &dev->parts) {
  			part = list_entry(pentry, struct part_info, link);
  			size = part->size;
  			offset = part->offset;
  			part_cnt++;
  
  			/* partition size */
  			memsize_format(tmpbuf, size);
  			len = strlen(tmpbuf);
  			if (len > maxlen)
  				goto cleanup;
  			memcpy(p, tmpbuf, len);
  			p += len;
  			maxlen -= len;
  
  
  			/* add offset only when there is a gap between
  			 * partitions */
  			if ((!prev_part && (offset != 0)) ||
  					(prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
  
  				memsize_format(tmpbuf, offset);
  				len = strlen(tmpbuf) + 1;
  				if (len > maxlen)
  					goto cleanup;
  				*(p++) = '@';
  				memcpy(p, tmpbuf, len - 1);
  				p += len - 1;
  				maxlen -= len;
  			}
  
  			/* copy name only if user supplied */
  			if(!part->auto_name) {
  				len = strlen(part->name) + 2;
  				if (len > maxlen)
  					goto cleanup;
  
  				*(p++) = '(';
  				memcpy(p, part->name, len - 2);
  				p += len - 2;
  				*(p++) = ')';
  				maxlen -= len;
  			}
  
  			/* ro mask flag */
  			if (part->mask_flags && MTD_WRITEABLE_CMD) {
  				len = 2;
  				if (len > maxlen)
  					goto cleanup;
  				*(p++) = 'r';
  				*(p++) = 'o';
  				maxlen -= 2;
  			}
  
  			/* print ',' separator if there are other partitions
  			 * following */
  			if (dev->num_parts > part_cnt) {
  				if (1 > maxlen)
  					goto cleanup;
  				*(p++) = ',';
  				maxlen--;
  			}
  			prev_part = part;
  		}
  		/* print ';' separator if there are other devices following */
  		if (dentry->next != &devices) {
  			if (1 > maxlen)
  				goto cleanup;
  			*(p++) = ';';
  			maxlen--;
  		}
  	}
  
  	/* we still have at least one char left, as we decremented maxlen at
  	 * the begining */
  	*p = '\0';
  
  	return 0;
  
  cleanup:
  	last_parts[0] = '\0';
  	return 1;
  }
  
  /**
   * Call generate_mtdparts to process all devices and generate corresponding
   * mtdparts string, save it in mtdparts environment variable.
   *
   * @param buf output buffer holding generated mtdparts string (output)
   * @param buflen buffer size
   * @return 0 on success, 1 otherwise
   */
  static int generate_mtdparts_save(char *buf, u32 buflen)
  {
  	int ret;
  
  	ret = generate_mtdparts(buf, buflen);
  
  	if ((buf[0] != '\0') && (ret == 0))
382bee57f   Simon Glass   env: Rename seten...
1269
  		env_set("mtdparts", buf);
68d7d6510   Stefan Roese   Separate mtdparts...
1270
  	else
382bee57f   Simon Glass   env: Rename seten...
1271
  		env_set("mtdparts", NULL);
68d7d6510   Stefan Roese   Separate mtdparts...
1272
1273
1274
  
  	return ret;
  }
04ac38028   Ben Gardiner   mtdparts: show ne...
1275
  #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
68d7d6510   Stefan Roese   Separate mtdparts...
1276
  /**
04ac38028   Ben Gardiner   mtdparts: show ne...
1277
1278
1279
1280
1281
   * Get the net size (w/o bad blocks) of the given partition.
   *
   * @param mtd the mtd info
   * @param part the partition
   * @return the calculated net size of this partition
68d7d6510   Stefan Roese   Separate mtdparts...
1282
   */
04ac38028   Ben Gardiner   mtdparts: show ne...
1283
1284
  static uint64_t net_part_size(struct mtd_info *mtd, struct part_info *part)
  {
36650ca9f   Scott Wood   Followup fixes on...
1285
  	uint64_t i, net_size = 0;
672c57057   Alexander Dahl   cmd: mtdparts: Fi...
1286
  	if (!mtd->_block_isbad)
04ac38028   Ben Gardiner   mtdparts: show ne...
1287
  		return part->size;
04ac38028   Ben Gardiner   mtdparts: show ne...
1288
  	for (i = 0; i < part->size; i += mtd->erasesize) {
672c57057   Alexander Dahl   cmd: mtdparts: Fi...
1289
  		if (!mtd->_block_isbad(mtd, part->offset + i))
04ac38028   Ben Gardiner   mtdparts: show ne...
1290
1291
  			net_size += mtd->erasesize;
  	}
36650ca9f   Scott Wood   Followup fixes on...
1292

04ac38028   Ben Gardiner   mtdparts: show ne...
1293
1294
1295
1296
1297
  	return net_size;
  }
  #endif
  
  static void print_partition_table(void)
68d7d6510   Stefan Roese   Separate mtdparts...
1298
1299
1300
1301
1302
  {
  	struct list_head *dentry, *pentry;
  	struct part_info *part;
  	struct mtd_device *dev;
  	int part_num;
68d7d6510   Stefan Roese   Separate mtdparts...
1303
1304
  	list_for_each(dentry, &devices) {
  		dev = list_entry(dentry, struct mtd_device, link);
04ac38028   Ben Gardiner   mtdparts: show ne...
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
  		/* list partitions for given device */
  		part_num = 0;
  #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
  		struct mtd_info *mtd;
  
  		if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
  			return;
  
  		printf("
  device %s%d <%s>, # parts = %d
  ",
  				MTD_DEV_TYPE(dev->id->type), dev->id->num,
  				dev->id->mtd_id, dev->num_parts);
  		printf(" #: name\t\tsize\t\tnet size\toffset\t\tmask_flags
  ");
  
  		list_for_each(pentry, &dev->parts) {
  			u32 net_size;
  			char *size_note;
  
  			part = list_entry(pentry, struct part_info, link);
  			net_size = net_part_size(mtd, part);
  			size_note = part->size == net_size ? " " : " (!)";
672c57057   Alexander Dahl   cmd: mtdparts: Fi...
1328
1329
  			printf("%2d: %-20s0x%08llx\t0x%08x%s\t0x%08llx\t%d
  ",
04ac38028   Ben Gardiner   mtdparts: show ne...
1330
1331
1332
1333
  					part_num, part->name, part->size,
  					net_size, size_note, part->offset,
  					part->mask_flags);
  #else /* !defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
68d7d6510   Stefan Roese   Separate mtdparts...
1334
1335
1336
1337
1338
  		printf("
  device %s%d <%s>, # parts = %d
  ",
  				MTD_DEV_TYPE(dev->id->type), dev->id->num,
  				dev->id->mtd_id, dev->num_parts);
08f077da9   David Brownell   mtdpart command: ...
1339
1340
  		printf(" #: name\t\tsize\t\toffset\t\tmask_flags
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
1341

68d7d6510   Stefan Roese   Separate mtdparts...
1342
1343
  		list_for_each(pentry, &dev->parts) {
  			part = list_entry(pentry, struct part_info, link);
39ac34473   Paul Burton   cmd_mtdparts: use...
1344
1345
  			printf("%2d: %-20s0x%08llx\t0x%08llx\t%d
  ",
68d7d6510   Stefan Roese   Separate mtdparts...
1346
1347
  					part_num, part->name, part->size,
  					part->offset, part->mask_flags);
04ac38028   Ben Gardiner   mtdparts: show ne...
1348
  #endif /* defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
68d7d6510   Stefan Roese   Separate mtdparts...
1349
1350
1351
  			part_num++;
  		}
  	}
04ac38028   Ben Gardiner   mtdparts: show ne...
1352

68d7d6510   Stefan Roese   Separate mtdparts...
1353
1354
1355
  	if (list_empty(&devices))
  		printf("no partitions defined
  ");
04ac38028   Ben Gardiner   mtdparts: show ne...
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
  }
  
  /**
   * Format and print out a partition list for each device from global device
   * list.
   */
  static void list_partitions(void)
  {
  	struct part_info *part;
  
  	debug("
  ---list_partitions---
  ");
  	print_partition_table();
68d7d6510   Stefan Roese   Separate mtdparts...
1370

76b5883da   Stefan Roese   jffs2/mtdparts: F...
1371
1372
1373
  	/* current_mtd_dev is not NULL only when we have non empty device list */
  	if (current_mtd_dev) {
  		part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
68d7d6510   Stefan Roese   Separate mtdparts...
1374
  		if (part) {
39ac34473   Paul Burton   cmd_mtdparts: use...
1375
1376
1377
  			printf("
  active partition: %s%d,%d - (%s) 0x%08llx @ 0x%08llx
  ",
76b5883da   Stefan Roese   jffs2/mtdparts: F...
1378
1379
  					MTD_DEV_TYPE(current_mtd_dev->id->type),
  					current_mtd_dev->id->num, current_mtd_partnum,
68d7d6510   Stefan Roese   Separate mtdparts...
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
  					part->name, part->size, part->offset);
  		} else {
  			printf("could not get current partition info
  
  ");
  		}
  	}
  
  	printf("
  defaults:
  ");
36c9169aa   Wolfgang Denk   cmd_mtdparts.c: a...
1391
1392
1393
  	printf("mtdids  : %s
  ",
  		mtdids_default ? mtdids_default : "none");
a693447ce   Anatolij Gustschin   cmd_mtdparts.c: p...
1394
1395
1396
1397
1398
1399
1400
1401
1402
  	/*
  	 * Using printf() here results in printbuffer overflow
  	 * if default mtdparts string is greater than console
  	 * printbuffer. Use puts() to prevent system crashes.
  	 */
  	puts("mtdparts: ");
  	puts(mtdparts_default ? mtdparts_default : "none");
  	puts("
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
  }
  
  /**
   * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
   * corresponding device and verify partition number.
   *
   * @param id string describing device and partition or partition name
   * @param dev pointer to the requested device (output)
   * @param part_num verified partition number (output)
   * @param part pointer to requested partition (output)
   * @return 0 on success, 1 otherwise
   */
  int find_dev_and_part(const char *id, struct mtd_device **dev,
  		u8 *part_num, struct part_info **part)
  {
  	struct list_head *dentry, *pentry;
  	u8 type, dnum, pnum;
  	const char *p;
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
1421
1422
1423
  	debug("--- find_dev_and_part ---
  id = %s
  ", id);
68d7d6510   Stefan Roese   Separate mtdparts...
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
  
  	list_for_each(dentry, &devices) {
  		*part_num = 0;
  		*dev = list_entry(dentry, struct mtd_device, link);
  		list_for_each(pentry, &(*dev)->parts) {
  			*part = list_entry(pentry, struct part_info, link);
  			if (strcmp((*part)->name, id) == 0)
  				return 0;
  			(*part_num)++;
  		}
  	}
  
  	p = id;
  	*dev = NULL;
  	*part = NULL;
  	*part_num = 0;
  
  	if (mtd_id_parse(p, &p, &type, &dnum) != 0)
  		return 1;
  
  	if ((*p++ != ',') || (*p == '\0')) {
  		printf("no partition number specified
  ");
  		return 1;
  	}
  	pnum = simple_strtoul(p, (char **)&p, 0);
  	if (*p != '\0') {
  		printf("unexpected trailing character '%c'
  ", *p);
  		return 1;
  	}
  
  	if ((*dev = device_find(type, dnum)) == NULL) {
  		printf("no such device %s%d
  ", MTD_DEV_TYPE(type), dnum);
  		return 1;
  	}
  
  	if ((*part = mtd_part_info(*dev, pnum)) == NULL) {
  		printf("no such partition
  ");
  		*dev = NULL;
  		return 1;
  	}
  
  	*part_num = pnum;
  
  	return 0;
  }
  
  /**
   * Find and delete partition. For partition id format see find_dev_and_part().
   *
   * @param id string describing device and partition
   * @return 0 on success, 1 otherwise
   */
  static int delete_partition(const char *id)
  {
  	u8 pnum;
  	struct mtd_device *dev;
  	struct part_info *part;
  
  	if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
39ac34473   Paul Burton   cmd_mtdparts: use...
1487
1488
  		debug("delete_partition: device = %s%d, partition %d = (%s) 0x%08llx@0x%08llx
  ",
68d7d6510   Stefan Roese   Separate mtdparts...
1489
1490
1491
1492
1493
1494
1495
  				MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
  				part->name, part->size, part->offset);
  
  		if (part_del(dev, part) != 0)
  			return 1;
  
  		if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
a22bf16be   Robert P. J. Day   cmd_mtdparts.c: C...
1496
1497
  			printf("generated mtdparts too long, resetting to null
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
1498
1499
1500
1501
1502
1503
1504
1505
1506
  			return 1;
  		}
  		return 0;
  	}
  
  	printf("partition %s not found
  ", id);
  	return 1;
  }
ca75b20eb   Ben Gardiner   mtdparts: add new...
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
  #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  /**
   * Increase the size of the given partition so that it's net size is at least
   * as large as the size member and such that the next partition would start on a
   * good block if it were adjacent to this partition.
   *
   * @param mtd the mtd device
   * @param part the partition
   * @param next_offset pointer to the offset of the next partition after this
   *                    partition's size has been modified (output)
   */
  static void spread_partition(struct mtd_info *mtd, struct part_info *part,
  			     uint64_t *next_offset)
  {
  	uint64_t net_size, padding_size = 0;
  	int truncated;
  
  	mtd_get_len_incl_bad(mtd, part->offset, part->size, &net_size,
  			     &truncated);
  
  	/*
  	 * Absorb bad blocks immediately following this
  	 * partition also into the partition, such that
  	 * the next partition starts with a good block.
  	 */
  	if (!truncated) {
  		mtd_get_len_incl_bad(mtd, part->offset + net_size,
  				     mtd->erasesize, &padding_size, &truncated);
  		if (truncated)
  			padding_size = 0;
  		else
  			padding_size -= mtd->erasesize;
  	}
  
  	if (truncated) {
  		printf("truncated partition %s to %lld bytes
  ", part->name,
  		       (uint64_t) net_size + padding_size);
  	}
  
  	part->size = net_size + padding_size;
  	*next_offset = part->offset + part->size;
  }
  
  /**
   * Adjust all of the partition sizes, such that all partitions are at least
   * as big as their mtdparts environment variable sizes and they each start
   * on a good block.
   *
   * @return 0 on success, 1 otherwise
   */
  static int spread_partitions(void)
  {
  	struct list_head *dentry, *pentry;
  	struct mtd_device *dev;
  	struct part_info *part;
  	struct mtd_info *mtd;
  	int part_num;
  	uint64_t cur_offs;
  
  	list_for_each(dentry, &devices) {
  		dev = list_entry(dentry, struct mtd_device, link);
  
  		if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
  			return 1;
  
  		part_num = 0;
  		cur_offs = 0;
  		list_for_each(pentry, &dev->parts) {
  			part = list_entry(pentry, struct part_info, link);
  
  			debug("spread_partitions: device = %s%d, partition %d ="
59441ac3c   Steve Rae   mtd: fix compiler...
1579
1580
  				" (%s) 0x%08llx@0x%08llx
  ",
ca75b20eb   Ben Gardiner   mtdparts: add new...
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
  				MTD_DEV_TYPE(dev->id->type), dev->id->num,
  				part_num, part->name, part->size,
  				part->offset);
  
  			if (cur_offs > part->offset)
  				part->offset = cur_offs;
  
  			spread_partition(mtd, part, &cur_offs);
  
  			part_num++;
  		}
  	}
  
  	index_partitions();
  
  	if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
a22bf16be   Robert P. J. Day   cmd_mtdparts.c: C...
1597
1598
  		printf("generated mtdparts too long, resetting to null
  ");
ca75b20eb   Ben Gardiner   mtdparts: add new...
1599
1600
1601
1602
1603
  		return 1;
  	}
  	return 0;
  }
  #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
68d7d6510   Stefan Roese   Separate mtdparts...
1604
  /**
1c2a262a9   Ladislav Michl   cmd: mtdparts: co...
1605
1606
1607
1608
1609
1610
1611
   * The mtdparts variable tends to be long. If we need to access it
   * before the env is relocated, then we need to use our own stack
   * buffer.  gd->env_buf will be too small.
   *
   * @param buf temporary buffer pointer MTDPARTS_MAXLEN long
   * @return mtdparts variable string, NULL if not found
   */
723806cc5   Simon Glass   env: Rename some ...
1612
  static const char *env_get_mtdparts(char *buf)
1c2a262a9   Ladislav Michl   cmd: mtdparts: co...
1613
1614
  {
  	if (gd->flags & GD_FLG_ENV_READY)
00caae6d4   Simon Glass   env: Rename geten...
1615
1616
  		return env_get("mtdparts");
  	if (env_get_f("mtdparts", buf, MTDPARTS_MAXLEN) != -1)
1c2a262a9   Ladislav Michl   cmd: mtdparts: co...
1617
1618
1619
1620
1621
  		return buf;
  	return NULL;
  }
  
  /**
68d7d6510   Stefan Roese   Separate mtdparts...
1622
1623
1624
1625
1626
1627
1628
1629
   * Accept character string describing mtd partitions and call device_parse()
   * for each entry. Add created devices to the global devices list.
   *
   * @param mtdparts string specifing mtd partitions
   * @return 0 on success, 1 otherwise
   */
  static int parse_mtdparts(const char *const mtdparts)
  {
06a040a31   Ladislav Michl   cmd: mtdparts: fi...
1630
  	const char *p;
68d7d6510   Stefan Roese   Separate mtdparts...
1631
1632
  	struct mtd_device *dev;
  	int err = 1;
a7eb1d66c   Joe Hershberger   mtd: Make mtdpart...
1633
  	char tmp_parts[MTDPARTS_MAXLEN];
68d7d6510   Stefan Roese   Separate mtdparts...
1634

1aca4d5ae   Lothar Waßmann   cmd: mtdparts: fi...
1635
1636
1637
1638
1639
  	debug("
  ---parse_mtdparts---
  mtdparts = %s
  
  ", mtdparts);
68d7d6510   Stefan Roese   Separate mtdparts...
1640
1641
1642
1643
1644
1645
1646
1647
1648
  
  	/* delete all devices and partitions */
  	if (mtd_devices_init() != 0) {
  		printf("could not initialise device list
  ");
  		return err;
  	}
  
  	/* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
723806cc5   Simon Glass   env: Rename some ...
1649
  	p = env_get_mtdparts(tmp_parts);
06a040a31   Ladislav Michl   cmd: mtdparts: fi...
1650
1651
  	if (!p)
  		p = mtdparts;
d60aea94e   Miquel Raynal   cmd: mtdparts: re...
1652
1653
1654
  	/* Skip the useless prefix, if any */
  	if (strncmp(p, "mtdparts=", 9) == 0)
  		p += 9;
68d7d6510   Stefan Roese   Separate mtdparts...
1655

06a040a31   Ladislav Michl   cmd: mtdparts: fi...
1656
  	while (*p != '\0') {
68d7d6510   Stefan Roese   Separate mtdparts...
1657
  		err = 1;
75e8911f6   Han Xu   MLK-22259-2: cmd:...
1658
1659
1660
1661
1662
1663
1664
  #ifdef CONFIG_MTDPARTS_SKIP_INVALID
  		if (!find_seperator(&p, ';', skip_counter, MTDPARTS_MAXLEN)) {
  			printf("goes wrong when skip invalid parts
  ");
  			return 1;
  		}
  #endif
68d7d6510   Stefan Roese   Separate mtdparts...
1665
1666
  		if ((device_parse(p, &p, &dev) != 0) || (!dev))
  			break;
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
1667
1668
  		debug("+ device: %s\t%d\t%s
  ", MTD_DEV_TYPE(dev->id->type),
68d7d6510   Stefan Roese   Separate mtdparts...
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
  				dev->id->num, dev->id->mtd_id);
  
  		/* check if parsed device is already on the list */
  		if (device_find(dev->id->type, dev->id->num) != NULL) {
  			printf("device %s%d redefined, please correct mtdparts variable
  ",
  					MTD_DEV_TYPE(dev->id->type), dev->id->num);
  			break;
  		}
  
  		list_add_tail(&dev->link, &devices);
  		err = 0;
  	}
54f1792c8   Tom Rini   mtdparts: Fix var...
1682
1683
  	if (err == 1) {
  		free(dev);
68d7d6510   Stefan Roese   Separate mtdparts...
1684
  		device_delall(&devices);
54f1792c8   Tom Rini   mtdparts: Fix var...
1685
  	}
68d7d6510   Stefan Roese   Separate mtdparts...
1686

06a040a31   Ladislav Michl   cmd: mtdparts: fi...
1687
  	return err;
68d7d6510   Stefan Roese   Separate mtdparts...
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
  }
  
  /**
   * Parse provided string describing mtdids mapping (see file header for mtdids
   * variable format). Allocate memory for each entry and add all found entries
   * to the global mtdids list.
   *
   * @param ids mapping string
   * @return 0 on success, 1 otherwise
   */
  static int parse_mtdids(const char *const ids)
  {
  	const char *p = ids;
  	const char *mtd_id;
  	int mtd_id_len;
  	struct mtdids *id;
  	struct list_head *entry, *n;
  	struct mtdids *id_tmp;
  	u8 type, num;
39ac34473   Paul Burton   cmd_mtdparts: use...
1707
  	u64 size;
68d7d6510   Stefan Roese   Separate mtdparts...
1708
  	int ret = 1;
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
1709
1710
1711
1712
1713
  	debug("
  ---parse_mtdids---
  mtdids = %s
  
  ", ids);
68d7d6510   Stefan Roese   Separate mtdparts...
1714
1715
1716
1717
  
  	/* clean global mtdids list */
  	list_for_each_safe(entry, n, &mtdids) {
  		id_tmp = list_entry(entry, struct mtdids, link);
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
1718
1719
  		debug("mtdids del: %d %d
  ", id_tmp->type, id_tmp->num);
68d7d6510   Stefan Roese   Separate mtdparts...
1720
1721
1722
1723
1724
1725
1726
1727
1728
  		list_del(entry);
  		free(id_tmp);
  	}
  	last_ids[0] = '\0';
  	INIT_LIST_HEAD(&mtdids);
  
  	while(p && (*p != '\0')) {
  
  		ret = 1;
00ac922db   Miquel Raynal   cmd: mtdparts: ac...
1729
  		/* parse 'nor'|'nand'|'onenand'|'spi-nand'<dev-num> */
68d7d6510   Stefan Roese   Separate mtdparts...
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
  		if (mtd_id_parse(p, &p, &type, &num) != 0)
  			break;
  
  		if (*p != '=') {
  			printf("mtdids: incorrect <dev-num>
  ");
  			break;
  		}
  		p++;
  
  		/* check if requested device exists */
75e8911f6   Han Xu   MLK-22259-2: cmd:...
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
  		if (mtd_device_validate(type, num, &size) != 0) {
  #ifdef CONFIG_MTDPARTS_SKIP_INVALID
  			if (find_seperator(&p, ',', 1, MTDIDS_MAXLEN)) {
  				printf("current device is invalid, skip it and check the next one
  ");
  				skip_counter++;
  				continue;
  			} else {
  				printf("the only deivce is invalid
  ");
  				return 1;
  			}
  #else
68d7d6510   Stefan Roese   Separate mtdparts...
1754
  			return 1;
75e8911f6   Han Xu   MLK-22259-2: cmd:...
1755
1756
  #endif
  		}
68d7d6510   Stefan Roese   Separate mtdparts...
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
  
  		/* locate <mtd-id> */
  		mtd_id = p;
  		if ((p = strchr(mtd_id, ',')) != NULL) {
  			mtd_id_len = p - mtd_id + 1;
  			p++;
  		} else {
  			mtd_id_len = strlen(mtd_id) + 1;
  		}
  		if (mtd_id_len == 0) {
  			printf("mtdids: no <mtd-id> identifier
  ");
  			break;
  		}
  
  		/* check if this id is already on the list */
  		int double_entry = 0;
  		list_for_each(entry, &mtdids) {
  			id_tmp = list_entry(entry, struct mtdids, link);
  			if ((id_tmp->type == type) && (id_tmp->num == num)) {
  				double_entry = 1;
  				break;
  			}
  		}
  		if (double_entry) {
  			printf("device id %s%d redefined, please correct mtdids variable
  ",
  					MTD_DEV_TYPE(type), num);
  			break;
  		}
  
  		/* allocate mtdids structure */
  		if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
  			printf("out of memory
  ");
  			break;
  		}
  		memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
  		id->num = num;
  		id->type = type;
  		id->size = size;
  		id->mtd_id = (char *)(id + 1);
  		strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
  		id->mtd_id[mtd_id_len - 1] = '\0';
  		INIT_LIST_HEAD(&id->link);
39ac34473   Paul Burton   cmd_mtdparts: use...
1802
1803
  		debug("+ id %s%d\t%16lld bytes\t%s
  ",
68d7d6510   Stefan Roese   Separate mtdparts...
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
  				MTD_DEV_TYPE(id->type), id->num,
  				id->size, id->mtd_id);
  
  		list_add_tail(&id->link, &mtdids);
  		ret = 0;
  	}
  	if (ret == 1) {
  		/* clean mtdids list and free allocated memory */
  		list_for_each_safe(entry, n, &mtdids) {
  			id_tmp = list_entry(entry, struct mtdids, link);
  			list_del(entry);
  			free(id_tmp);
  		}
  		return 1;
  	}
  
  	return 0;
  }
1c2a262a9   Ladislav Michl   cmd: mtdparts: co...
1822

68d7d6510   Stefan Roese   Separate mtdparts...
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
  /**
   * Parse and initialize global mtdids mapping and create global
   * device/partition list.
   *
   * @return 0 on success, 1 otherwise
   */
  int mtdparts_init(void)
  {
  	static int initialized = 0;
  	const char *ids, *parts;
  	const char *current_partition;
  	int ids_changed;
bc028345a   Tom Rini   mtdparts: Fix fin...
1835
  	char tmp_ep[PARTITION_MAXLEN + 1];
a7eb1d66c   Joe Hershberger   mtd: Make mtdpart...
1836
  	char tmp_parts[MTDPARTS_MAXLEN];
68d7d6510   Stefan Roese   Separate mtdparts...
1837

e1d2950d0   Wolfgang Denk   mtdparts: get rid...
1838
1839
1840
  	debug("
  ---mtdparts_init---
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
1841
1842
1843
  	if (!initialized) {
  		INIT_LIST_HEAD(&mtdids);
  		INIT_LIST_HEAD(&devices);
54f1792c8   Tom Rini   mtdparts: Fix var...
1844
1845
1846
  		memset(last_ids, 0, sizeof(last_ids));
  		memset(last_parts, 0, sizeof(last_parts));
  		memset(last_partition, 0, sizeof(last_partition));
af3244365   Ladislav Michl   cmd: mtdparts: su...
1847
1848
1849
  #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
  		board_mtdparts_default(&mtdids_default, &mtdparts_default);
  #endif
f8f744a3e   Ladislav Michl   cmd: mtdparts: us...
1850
  		use_defaults = 1;
68d7d6510   Stefan Roese   Separate mtdparts...
1851
1852
1853
1854
  		initialized = 1;
  	}
  
  	/* get variables */
00caae6d4   Simon Glass   env: Rename geten...
1855
  	ids = env_get("mtdids");
723806cc5   Simon Glass   env: Rename some ...
1856
  	parts = env_get_mtdparts(tmp_parts);
00caae6d4   Simon Glass   env: Rename geten...
1857
  	current_partition = env_get("partition");
68d7d6510   Stefan Roese   Separate mtdparts...
1858
1859
1860
  
  	/* save it for later parsing, cannot rely on current partition pointer
  	 * as 'partition' variable may be updated during init */
bc028345a   Tom Rini   mtdparts: Fix fin...
1861
  	memset(tmp_parts, 0, sizeof(tmp_parts));
8b3cec7da   Tom Rini   mtdparts: Fix uni...
1862
  	memset(tmp_ep, 0, sizeof(tmp_ep));
68d7d6510   Stefan Roese   Separate mtdparts...
1863
1864
  	if (current_partition)
  		strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
1865
1866
1867
1868
1869
1870
1871
1872
1873
  	debug("last_ids  : %s
  ", last_ids);
  	debug("env_ids   : %s
  ", ids);
  	debug("last_parts: %s
  ", last_parts);
  	debug("env_parts : %s
  
  ", parts);
68d7d6510   Stefan Roese   Separate mtdparts...
1874

e1d2950d0   Wolfgang Denk   mtdparts: get rid...
1875
1876
1877
1878
  	debug("last_partition : %s
  ", last_partition);
  	debug("env_partition  : %s
  ", current_partition);
68d7d6510   Stefan Roese   Separate mtdparts...
1879

1cc0a9f49   Robert P. J. Day   Fix various typos...
1880
  	/* if mtdids variable is empty try to use defaults */
68d7d6510   Stefan Roese   Separate mtdparts...
1881
1882
  	if (!ids) {
  		if (mtdids_default) {
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
1883
1884
  			debug("mtdids variable not defined, using default
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
1885
  			ids = mtdids_default;
382bee57f   Simon Glass   env: Rename seten...
1886
  			env_set("mtdids", (char *)ids);
68d7d6510   Stefan Roese   Separate mtdparts...
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
  		} else {
  			printf("mtdids not defined, no default present
  ");
  			return 1;
  		}
  	}
  	if (strlen(ids) > MTDIDS_MAXLEN - 1) {
  		printf("mtdids too long (> %d)
  ", MTDIDS_MAXLEN);
  		return 1;
  	}
f8f744a3e   Ladislav Michl   cmd: mtdparts: us...
1898
1899
1900
1901
1902
  	/* use defaults when mtdparts variable is not defined
  	 * once mtdparts is saved environment, drop use_defaults flag */
  	if (!parts) {
  		if (mtdparts_default && use_defaults) {
  			parts = mtdparts_default;
382bee57f   Simon Glass   env: Rename seten...
1903
  			if (env_set("mtdparts", (char *)parts) == 0)
f8f744a3e   Ladislav Michl   cmd: mtdparts: us...
1904
1905
1906
1907
1908
  				use_defaults = 0;
  		} else
  			printf("mtdparts variable not set, see 'help mtdparts'
  ");
  	}
68d7d6510   Stefan Roese   Separate mtdparts...
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
  
  	if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
  		printf("mtdparts too long (> %d)
  ", MTDPARTS_MAXLEN);
  		return 1;
  	}
  
  	/* check if we have already parsed those mtdids */
  	if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
  		ids_changed = 0;
  	} else {
  		ids_changed = 1;
  
  		if (parse_mtdids(ids) != 0) {
  			mtd_devices_init();
  			return 1;
  		}
  
  		/* ok it's good, save new ids */
  		strncpy(last_ids, ids, MTDIDS_MAXLEN);
  	}
  
  	/* parse partitions if either mtdparts or mtdids were updated */
  	if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
  		if (parse_mtdparts(parts) != 0)
  			return 1;
  
  		if (list_empty(&devices)) {
  			printf("mtdparts_init: no valid partitions
  ");
  			return 1;
  		}
  
  		/* ok it's good, save new parts */
  		strncpy(last_parts, parts, MTDPARTS_MAXLEN);
  
  		/* reset first partition from first dev from the list as current */
76b5883da   Stefan Roese   jffs2/mtdparts: F...
1946
1947
  		current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
  		current_mtd_partnum = 0;
68d7d6510   Stefan Roese   Separate mtdparts...
1948
  		current_save();
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
1949
1950
  		debug("mtdparts_init: current_mtd_dev  = %s%d, current_mtd_partnum = %d
  ",
76b5883da   Stefan Roese   jffs2/mtdparts: F...
1951
1952
  				MTD_DEV_TYPE(current_mtd_dev->id->type),
  				current_mtd_dev->id->num, current_mtd_partnum);
68d7d6510   Stefan Roese   Separate mtdparts...
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
  	}
  
  	/* mtdparts variable was reset to NULL, delete all devices/partitions */
  	if (!parts && (last_parts[0] != '\0'))
  		return mtd_devices_init();
  
  	/* do not process current partition if mtdparts variable is null */
  	if (!parts)
  		return 0;
  
  	/* is current partition set in environment? if so, use it */
  	if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
  		struct part_info *p;
  		struct mtd_device *cdev;
  		u8 pnum;
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
1968
1969
  		debug("--- getting current partition: %s
  ", tmp_ep);
68d7d6510   Stefan Roese   Separate mtdparts...
1970
1971
  
  		if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
76b5883da   Stefan Roese   jffs2/mtdparts: F...
1972
1973
  			current_mtd_dev = cdev;
  			current_mtd_partnum = pnum;
68d7d6510   Stefan Roese   Separate mtdparts...
1974
1975
  			current_save();
  		}
00caae6d4   Simon Glass   env: Rename geten...
1976
  	} else if (env_get("partition") == NULL) {
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
1977
1978
  		debug("no partition variable set, setting...
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
  		current_save();
  	}
  
  	return 0;
  }
  
  /**
   * Return pointer to the partition of a requested number from a requested
   * device.
   *
   * @param dev device that is to be searched for a partition
   * @param part_num requested partition number
   * @return pointer to the part_info, NULL otherwise
   */
  static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
  {
  	struct list_head *entry;
  	struct part_info *part;
  	int num;
  
  	if (!dev)
  		return NULL;
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
2001
2002
2003
  	debug("
  --- mtd_part_info: partition number %d for device %s%d (%s)
  ",
68d7d6510   Stefan Roese   Separate mtdparts...
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
  			part_num, MTD_DEV_TYPE(dev->id->type),
  			dev->id->num, dev->id->mtd_id);
  
  	if (part_num >= dev->num_parts) {
  		printf("invalid partition number %d for device %s%d (%s)
  ",
  				part_num, MTD_DEV_TYPE(dev->id->type),
  				dev->id->num, dev->id->mtd_id);
  		return NULL;
  	}
  
  	/* locate partition number, return it */
  	num = 0;
  	list_for_each(entry, &dev->parts) {
  		part = list_entry(entry, struct part_info, link);
  
  		if (part_num == num++) {
  			return part;
  		}
  	}
  
  	return NULL;
  }
  
  /***************************************************/
a187559e3   Bin Meng   Use correct spell...
2029
  /* U-Boot commands				   */
68d7d6510   Stefan Roese   Separate mtdparts...
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
  /***************************************************/
  /* command line only */
  /**
   * Routine implementing u-boot chpart command. Sets new current partition based
   * on the user supplied partition id. For partition id format see find_dev_and_part().
   *
   * @param cmdtp command internal data
   * @param flag command flag
   * @param argc number of arguments supplied to the command
   * @param argv arguments list
   * @return 0 on success, 1 otherwise
   */
088f1b199   Kim Phillips   common/cmd_*.c: s...
2042
  static int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
68d7d6510   Stefan Roese   Separate mtdparts...
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
  {
  /* command line only */
  	struct mtd_device *dev;
  	struct part_info *part;
  	u8 pnum;
  
  	if (mtdparts_init() !=0)
  		return 1;
  
  	if (argc < 2) {
  		printf("no partition id specified
  ");
  		return 1;
  	}
  
  	if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
  		return 1;
76b5883da   Stefan Roese   jffs2/mtdparts: F...
2060
2061
  	current_mtd_dev = dev;
  	current_mtd_partnum = pnum;
68d7d6510   Stefan Roese   Separate mtdparts...
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
  	current_save();
  
  	printf("partition changed to %s%d,%d
  ",
  			MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
  
  	return 0;
  }
  
  /**
   * Routine implementing u-boot mtdparts command. Initialize/update default global
   * partition list and process user partition request (list, add, del).
   *
   * @param cmdtp command internal data
   * @param flag command flag
   * @param argc number of arguments supplied to the command
   * @param argv arguments list
   * @return 0 on success, 1 otherwise
   */
088f1b199   Kim Phillips   common/cmd_*.c: s...
2081
2082
  static int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc,
  		       char * const argv[])
68d7d6510   Stefan Roese   Separate mtdparts...
2083
2084
2085
  {
  	if (argc == 2) {
  		if (strcmp(argv[1], "default") == 0) {
382bee57f   Simon Glass   env: Rename seten...
2086
2087
2088
  			env_set("mtdids", NULL);
  			env_set("mtdparts", NULL);
  			env_set("partition", NULL);
f8f744a3e   Ladislav Michl   cmd: mtdparts: us...
2089
  			use_defaults = 1;
68d7d6510   Stefan Roese   Separate mtdparts...
2090
2091
2092
2093
2094
2095
  
  			mtdparts_init();
  			return 0;
  		} else if (strcmp(argv[1], "delall") == 0) {
  			/* this may be the first run, initialize lists if needed */
  			mtdparts_init();
382bee57f   Simon Glass   env: Rename seten...
2096
  			env_set("mtdparts", NULL);
68d7d6510   Stefan Roese   Separate mtdparts...
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
  
  			/* mtd_devices_init() calls current_save() */
  			return mtd_devices_init();
  		}
  	}
  
  	/* make sure we are in sync with env variables */
  	if (mtdparts_init() != 0)
  		return 1;
  
  	if (argc == 1) {
  		list_partitions();
  		return 0;
  	}
  
  	/* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
59a50d2de   Ben Gardiner   mtdparts: new add...
2113
  	if (((argc == 5) || (argc == 6)) && (strncmp(argv[1], "add", 3) == 0)) {
68d7d6510   Stefan Roese   Separate mtdparts...
2114
2115
  #define PART_ADD_DESC_MAXLEN 64
  		char tmpbuf[PART_ADD_DESC_MAXLEN];
59a50d2de   Ben Gardiner   mtdparts: new add...
2116
2117
2118
2119
  #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  		struct mtd_info *mtd;
  		uint64_t next_offset;
  #endif
68d7d6510   Stefan Roese   Separate mtdparts...
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
  		u8 type, num, len;
  		struct mtd_device *dev;
  		struct mtd_device *dev_tmp;
  		struct mtdids *id;
  		struct part_info *p;
  
  		if (mtd_id_parse(argv[2], NULL, &type, &num) != 0)
  			return 1;
  
  		if ((id = id_find(type, num)) == NULL) {
  			printf("no such device %s defined in mtdids variable
  ", argv[2]);
  			return 1;
  		}
  
  		len = strlen(id->mtd_id) + 1;	/* 'mtd_id:' */
  		len += strlen(argv[3]);		/* size@offset */
  		len += strlen(argv[4]) + 2;	/* '(' name ')' */
  		if (argv[5] && (strlen(argv[5]) == 2))
  			len += 2;		/* 'ro' */
  
  		if (len >= PART_ADD_DESC_MAXLEN) {
  			printf("too long partition description
  ");
  			return 1;
  		}
  		sprintf(tmpbuf, "%s:%s(%s)%s",
  				id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
2148
2149
  		debug("add tmpbuf: %s
  ", tmpbuf);
68d7d6510   Stefan Roese   Separate mtdparts...
2150
2151
2152
  
  		if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
  			return 1;
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
2153
2154
  		debug("+ %s\t%d\t%s
  ", MTD_DEV_TYPE(dev->id->type),
68d7d6510   Stefan Roese   Separate mtdparts...
2155
  				dev->id->num, dev->id->mtd_id);
59a50d2de   Ben Gardiner   mtdparts: new add...
2156
2157
2158
2159
2160
2161
2162
2163
  		p = list_entry(dev->parts.next, struct part_info, link);
  
  #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  		if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
  			return 1;
  
  		if (!strcmp(&argv[1][3], ".spread")) {
  			spread_partition(mtd, p, &next_offset);
59441ac3c   Steve Rae   mtd: fix compiler...
2164
2165
  			debug("increased %s to %llu bytes
  ", p->name, p->size);
59a50d2de   Ben Gardiner   mtdparts: new add...
2166
2167
2168
2169
2170
  		}
  #endif
  
  		dev_tmp = device_find(dev->id->type, dev->id->num);
  		if (dev_tmp == NULL) {
68d7d6510   Stefan Roese   Separate mtdparts...
2171
  			device_add(dev);
59a50d2de   Ben Gardiner   mtdparts: new add...
2172
  		} else if (part_add(dev_tmp, p) != 0) {
68d7d6510   Stefan Roese   Separate mtdparts...
2173
  			/* merge new partition with existing ones*/
59a50d2de   Ben Gardiner   mtdparts: new add...
2174
2175
  			device_del(dev);
  			return 1;
68d7d6510   Stefan Roese   Separate mtdparts...
2176
2177
2178
  		}
  
  		if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
a22bf16be   Robert P. J. Day   cmd_mtdparts.c: C...
2179
2180
  			printf("generated mtdparts too long, resetting to null
  ");
68d7d6510   Stefan Roese   Separate mtdparts...
2181
2182
2183
2184
2185
2186
2187
2188
  			return 1;
  		}
  
  		return 0;
  	}
  
  	/* mtdparts del part-id */
  	if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
e1d2950d0   Wolfgang Denk   mtdparts: get rid...
2189
2190
  		debug("del: part-id = %s
  ", argv[2]);
68d7d6510   Stefan Roese   Separate mtdparts...
2191
2192
2193
  
  		return delete_partition(argv[2]);
  	}
ca75b20eb   Ben Gardiner   mtdparts: add new...
2194
2195
2196
2197
  #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  	if ((argc == 2) && (strcmp(argv[1], "spread") == 0))
  		return spread_partitions();
  #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
4c12eeb8b   Simon Glass   Convert cmd_usage...
2198
  	return CMD_RET_USAGE;
68d7d6510   Stefan Roese   Separate mtdparts...
2199
2200
2201
2202
2203
2204
2205
2206
  }
  
  /***************************************************/
  U_BOOT_CMD(
  	chpart,	2,	0,	do_chpart,
  	"change active partition",
  	"part-id
  "
a89c33db9   Wolfgang Denk   General help mess...
2207
  	"    - change active partition (e.g. part-id = nand0,1)"
68d7d6510   Stefan Roese   Separate mtdparts...
2208
  );
088f1b199   Kim Phillips   common/cmd_*.c: s...
2209
2210
  #ifdef CONFIG_SYS_LONGHELP
  static char mtdparts_help_text[] =
68d7d6510   Stefan Roese   Separate mtdparts...
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
  	"
  "
  	"    - list partition table
  "
  	"mtdparts delall
  "
  	"    - delete all partitions
  "
  	"mtdparts del part-id
  "
  	"    - delete partition (e.g. part-id = nand0,1)
  "
  	"mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]
  "
  	"    - add partition
  "
59a50d2de   Ben Gardiner   mtdparts: new add...
2227
2228
2229
2230
2231
2232
  #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  	"mtdparts add.spread <mtd-dev> <size>[@<offset>] [<name>] [ro]
  "
  	"    - add partition, padding size by skipping bad blocks
  "
  #endif
68d7d6510   Stefan Roese   Separate mtdparts...
2233
2234
  	"mtdparts default
  "
ca75b20eb   Ben Gardiner   mtdparts: add new...
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
  	"    - reset partition table to defaults
  "
  #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  	"mtdparts spread
  "
  	"    - adjust the sizes of the partitions so they are
  "
  	"      at least as big as the mtdparts variable specifies
  "
  	"      and they each start on a good block
  
  "
  #else
  	"
  "
  #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
68d7d6510   Stefan Roese   Separate mtdparts...
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
  	"-----
  
  "
  	"this command uses three environment variables:
  
  "
  	"'partition' - keeps current partition identifier
  
  "
  	"partition  := <part-id>
  "
  	"<part-id>  := <dev-id>,part_num
  
  "
  	"'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
  
  "
  	"mtdids=<idmap>[,<idmap>,...]
  
  "
  	"<idmap>    := <dev-id>=<mtd-id>
  "
00ac922db   Miquel Raynal   cmd: mtdparts: ac...
2273
2274
  	"<dev-id>   := 'nand'|'nor'|'onenand'|'spi-nand'<dev-num>
  "
68d7d6510   Stefan Roese   Separate mtdparts...
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
  	"<dev-num>  := mtd device number, 0...
  "
  	"<mtd-id>   := unique device tag used by linux kernel to find mtd device (mtd->name)
  
  "
  	"'mtdparts' - partition list
  
  "
  	"mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
  
  "
  	"<mtd-def>  := <mtd-id>:<part-def>[,<part-def>...]
  "
  	"<mtd-id>   := unique device tag used by linux kernel to find mtd device (mtd->name)
  "
  	"<part-def> := <size>[@<offset>][<name>][<ro-flag>]
  "
  	"<size>     := standard linux memsize OR '-' to denote all remaining space
  "
  	"<offset>   := partition start offset within the device
  "
  	"<name>     := '(' NAME ')'
  "
088f1b199   Kim Phillips   common/cmd_*.c: s...
2298
2299
2300
2301
2302
2303
  	"<ro-flag>  := when set to 'ro' makes partition read-only (not used, passed to kernel)";
  #endif
  
  U_BOOT_CMD(
  	mtdparts,	6,	0,	do_mtdparts,
  	"define flash/nand partitions", mtdparts_help_text
68d7d6510   Stefan Roese   Separate mtdparts...
2304
2305
  );
  /***************************************************/