blob: c4010091338fe30e9531917b9622fa2686214123 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese68d7d652009-03-19 13:30:36 +01002/*
3 * (C) Copyright 2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * (C) Copyright 2002
7 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
8 *
9 * (C) Copyright 2003
10 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
11 *
12 * (C) Copyright 2005
13 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
14 *
15 * Added support for reading flash partition table from environment.
16 * Parsing routines are based on driver/mtd/cmdline.c from the linux 2.4
17 * kernel tree.
18 *
Ben Gardinerca75b202010-08-31 17:48:03 -040019 * (C) Copyright 2008
20 * Harald Welte, OpenMoko, Inc., Harald Welte <laforge@openmoko.org>
21 *
Stefan Roese68d7d652009-03-19 13:30:36 +010022 * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
23 * Copyright 2002 SYSGO Real-Time Solutions GmbH
Stefan Roese68d7d652009-03-19 13:30:36 +010024 */
25
26/*
27 * Three environment variables are used by the parsing routines:
28 *
29 * 'partition' - keeps current partition identifier
30 *
31 * partition := <part-id>
32 * <part-id> := <dev-id>,part_num
33 *
34 *
35 * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
36 *
37 * mtdids=<idmap>[,<idmap>,...]
38 *
39 * <idmap> := <dev-id>=<mtd-id>
40 * <dev-id> := 'nand'|'nor'|'onenand'<dev-num>
41 * <dev-num> := mtd device number, 0...
42 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
43 *
44 *
45 * 'mtdparts' - partition list
46 *
47 * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
48 *
49 * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...]
50 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
51 * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
52 * <size> := standard linux memsize OR '-' to denote all remaining space
53 * <offset> := partition start offset within the device
54 * <name> := '(' NAME ')'
55 * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)
56 *
57 * Notes:
58 * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
59 * - if the above variables are not set defaults for a given target are used
60 *
61 * Examples:
62 *
63 * 1 NOR Flash, with 1 single writable partition:
64 * mtdids=nor0=edb7312-nor
65 * mtdparts=mtdparts=edb7312-nor:-
66 *
67 * 1 NOR Flash with 2 partitions, 1 NAND with one
68 * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
69 * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
70 *
71 */
72
Stefan Roese68d7d652009-03-19 13:30:36 +010073#include <common.h>
74#include <command.h>
75#include <malloc.h>
Ladislav Michlb93b24b2009-03-23 12:06:07 +010076#include <jffs2/load_kernel.h>
Stefan Roese68d7d652009-03-19 13:30:36 +010077#include <linux/list.h>
78#include <linux/ctype.h>
Stefan Roese864aa032009-05-12 14:31:56 +020079#include <linux/err.h>
80#include <linux/mtd/mtd.h>
Stefan Roese68d7d652009-03-19 13:30:36 +010081
82#if defined(CONFIG_CMD_NAND)
Masahiro Yamada6ae39002017-11-30 13:45:24 +090083#include <linux/mtd/rawnand.h>
Stefan Roese68d7d652009-03-19 13:30:36 +010084#include <nand.h>
Stefan Roese68d7d652009-03-19 13:30:36 +010085#endif
86
87#if defined(CONFIG_CMD_ONENAND)
Stefan Roese68d7d652009-03-19 13:30:36 +010088#include <linux/mtd/onenand.h>
89#include <onenand_uboot.h>
90#endif
91
Joe Hershbergera7eb1d62013-04-08 10:32:50 +000092DECLARE_GLOBAL_DATA_PTR;
93
Stefan Roese68d7d652009-03-19 13:30:36 +010094/* special size referring to all the remaining space in a partition */
Paul Burton39ac3442013-09-04 15:16:57 +010095#define SIZE_REMAINING (~0llu)
Stefan Roese68d7d652009-03-19 13:30:36 +010096
97/* special offset value, it is used when not provided by user
98 *
99 * this value is used temporarily during parsing, later such offests
100 * are recalculated */
Paul Burton39ac3442013-09-04 15:16:57 +0100101#define OFFSET_NOT_SPECIFIED (~0llu)
Stefan Roese68d7d652009-03-19 13:30:36 +0100102
103/* minimum partition size */
104#define MIN_PART_SIZE 4096
105
106/* this flag needs to be set in part_info struct mask_flags
107 * field for read-only partitions */
108#define MTD_WRITEABLE_CMD 1
109
110/* default values for mtdids and mtdparts variables */
Ladislav Michlaf324432016-07-12 20:28:26 +0200111#if !defined(MTDIDS_DEFAULT)
Maxime Ripard0269dfa2017-02-27 18:22:04 +0100112#ifdef CONFIG_MTDIDS_DEFAULT
113#define MTDIDS_DEFAULT CONFIG_MTDIDS_DEFAULT
114#else
Ladislav Michlaf324432016-07-12 20:28:26 +0200115#define MTDIDS_DEFAULT NULL
Stefan Roese68d7d652009-03-19 13:30:36 +0100116#endif
Maxime Ripard0269dfa2017-02-27 18:22:04 +0100117#endif
Ladislav Michlaf324432016-07-12 20:28:26 +0200118#if !defined(MTDPARTS_DEFAULT)
Maxime Ripard0269dfa2017-02-27 18:22:04 +0100119#ifdef CONFIG_MTDPARTS_DEFAULT
120#define MTDPARTS_DEFAULT CONFIG_MTDPARTS_DEFAULT
121#else
Ladislav Michlaf324432016-07-12 20:28:26 +0200122#define MTDPARTS_DEFAULT NULL
Stefan Roese68d7d652009-03-19 13:30:36 +0100123#endif
Maxime Ripard0269dfa2017-02-27 18:22:04 +0100124#endif
Ladislav Michlaf324432016-07-12 20:28:26 +0200125#if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
126extern void board_mtdparts_default(const char **mtdids, const char **mtdparts);
127#endif
128static const char *mtdids_default = MTDIDS_DEFAULT;
129static const char *mtdparts_default = MTDPARTS_DEFAULT;
Stefan Roese68d7d652009-03-19 13:30:36 +0100130
131/* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
132#define MTDIDS_MAXLEN 128
133#define MTDPARTS_MAXLEN 512
134#define PARTITION_MAXLEN 16
Tom Rini54f17922017-08-14 20:42:27 -0400135static char last_ids[MTDIDS_MAXLEN + 1];
136static char last_parts[MTDPARTS_MAXLEN + 1];
137static char last_partition[PARTITION_MAXLEN + 1];
Stefan Roese68d7d652009-03-19 13:30:36 +0100138
139/* low level jffs2 cache cleaning routine */
140extern void jffs2_free_cache(struct part_info *part);
141
142/* mtdids mapping list, filled by parse_ids() */
Kim Phillips088f1b12012-10-29 13:34:31 +0000143static struct list_head mtdids;
Stefan Roese68d7d652009-03-19 13:30:36 +0100144
145/* device/partition list, parse_cmdline() parses into here */
Kim Phillips088f1b12012-10-29 13:34:31 +0000146static struct list_head devices;
Stefan Roese68d7d652009-03-19 13:30:36 +0100147
148/* current active device and partition number */
Stefan Roese76b58832009-05-16 12:04:22 +0200149struct mtd_device *current_mtd_dev = NULL;
150u8 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100151
Ladislav Michlf8f744a2016-07-12 20:28:25 +0200152u8 use_defaults;
153
Stefan Roese68d7d652009-03-19 13:30:36 +0100154static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
155
156/* command line only routines */
157static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
158static int device_del(struct mtd_device *dev);
159
160/**
161 * Parses a string into a number. The number stored at ptr is
162 * potentially suffixed with K (for kilobytes, or 1024 bytes),
163 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
164 * 1073741824). If the number is suffixed with K, M, or G, then
165 * the return value is the number multiplied by one kilobyte, one
166 * megabyte, or one gigabyte, respectively.
167 *
168 * @param ptr where parse begins
169 * @param retptr output pointer to next char after parse completes (output)
170 * @return resulting unsigned int
171 */
Paul Burton39ac3442013-09-04 15:16:57 +0100172static u64 memsize_parse (const char *const ptr, const char **retptr)
Stefan Roese68d7d652009-03-19 13:30:36 +0100173{
Paul Burton39ac3442013-09-04 15:16:57 +0100174 u64 ret = simple_strtoull(ptr, (char **)retptr, 0);
Stefan Roese68d7d652009-03-19 13:30:36 +0100175
176 switch (**retptr) {
177 case 'G':
178 case 'g':
179 ret <<= 10;
180 case 'M':
181 case 'm':
182 ret <<= 10;
183 case 'K':
184 case 'k':
185 ret <<= 10;
186 (*retptr)++;
187 default:
188 break;
189 }
190
191 return ret;
192}
193
194/**
195 * Format string describing supplied size. This routine does the opposite job
196 * to memsize_parse(). Size in bytes is converted to string and if possible
197 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
198 *
199 * Note, that this routine does not check for buffer overflow, it's the caller
200 * who must assure enough space.
201 *
202 * @param buf output buffer
203 * @param size size to be converted to string
204 */
Paul Burton39ac3442013-09-04 15:16:57 +0100205static void memsize_format(char *buf, u64 size)
Stefan Roese68d7d652009-03-19 13:30:36 +0100206{
207#define SIZE_GB ((u32)1024*1024*1024)
208#define SIZE_MB ((u32)1024*1024)
209#define SIZE_KB ((u32)1024)
210
211 if ((size % SIZE_GB) == 0)
Paul Burton39ac3442013-09-04 15:16:57 +0100212 sprintf(buf, "%llug", size/SIZE_GB);
Stefan Roese68d7d652009-03-19 13:30:36 +0100213 else if ((size % SIZE_MB) == 0)
Paul Burton39ac3442013-09-04 15:16:57 +0100214 sprintf(buf, "%llum", size/SIZE_MB);
Stefan Roese68d7d652009-03-19 13:30:36 +0100215 else if (size % SIZE_KB == 0)
Paul Burton39ac3442013-09-04 15:16:57 +0100216 sprintf(buf, "%lluk", size/SIZE_KB);
Stefan Roese68d7d652009-03-19 13:30:36 +0100217 else
Paul Burton39ac3442013-09-04 15:16:57 +0100218 sprintf(buf, "%llu", size);
Stefan Roese68d7d652009-03-19 13:30:36 +0100219}
220
221/**
222 * This routine does global indexing of all partitions. Resulting index for
223 * current partition is saved in 'mtddevnum'. Current partition name in
224 * 'mtddevname'.
225 */
226static void index_partitions(void)
227{
Stefan Roese68d7d652009-03-19 13:30:36 +0100228 u16 mtddevnum;
229 struct part_info *part;
230 struct list_head *dentry;
231 struct mtd_device *dev;
232
Wolfgang Denke1d29502010-04-28 10:58:10 +0200233 debug("--- index partitions ---\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100234
Stefan Roese76b58832009-05-16 12:04:22 +0200235 if (current_mtd_dev) {
Stefan Roese68d7d652009-03-19 13:30:36 +0100236 mtddevnum = 0;
237 list_for_each(dentry, &devices) {
238 dev = list_entry(dentry, struct mtd_device, link);
Stefan Roese76b58832009-05-16 12:04:22 +0200239 if (dev == current_mtd_dev) {
240 mtddevnum += current_mtd_partnum;
Simon Glass018f5302017-08-03 12:22:10 -0600241 env_set_ulong("mtddevnum", mtddevnum);
Tom Rini54f17922017-08-14 20:42:27 -0400242 debug("=> mtddevnum %d,\n", mtddevnum);
Stefan Roese68d7d652009-03-19 13:30:36 +0100243 break;
244 }
245 mtddevnum += dev->num_parts;
246 }
247
Stefan Roese76b58832009-05-16 12:04:22 +0200248 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Tom Rini54f17922017-08-14 20:42:27 -0400249 if (part) {
250 env_set("mtddevname", part->name);
Stefan Roese68d7d652009-03-19 13:30:36 +0100251
Tom Rini54f17922017-08-14 20:42:27 -0400252 debug("=> mtddevname %s\n", part->name);
253 } else {
254 env_set("mtddevname", NULL);
255
256 debug("=> mtddevname NULL\n");
257 }
Stefan Roese68d7d652009-03-19 13:30:36 +0100258 } else {
Simon Glass382bee52017-08-03 12:22:09 -0600259 env_set("mtddevnum", NULL);
260 env_set("mtddevname", NULL);
Stefan Roese68d7d652009-03-19 13:30:36 +0100261
Wolfgang Denke1d29502010-04-28 10:58:10 +0200262 debug("=> mtddevnum NULL\n=> mtddevname NULL\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100263 }
264}
265
266/**
267 * Save current device and partition in environment variable 'partition'.
268 */
269static void current_save(void)
270{
271 char buf[16];
272
Wolfgang Denke1d29502010-04-28 10:58:10 +0200273 debug("--- current_save ---\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100274
Stefan Roese76b58832009-05-16 12:04:22 +0200275 if (current_mtd_dev) {
276 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type),
277 current_mtd_dev->id->num, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +0100278
Simon Glass382bee52017-08-03 12:22:09 -0600279 env_set("partition", buf);
Stefan Roese68d7d652009-03-19 13:30:36 +0100280 strncpy(last_partition, buf, 16);
281
Wolfgang Denke1d29502010-04-28 10:58:10 +0200282 debug("=> partition %s\n", buf);
Stefan Roese68d7d652009-03-19 13:30:36 +0100283 } else {
Simon Glass382bee52017-08-03 12:22:09 -0600284 env_set("partition", NULL);
Stefan Roese68d7d652009-03-19 13:30:36 +0100285 last_partition[0] = '\0';
286
Wolfgang Denke1d29502010-04-28 10:58:10 +0200287 debug("=> partition NULL\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100288 }
289 index_partitions();
290}
291
Ben Gardiner0a026d32010-08-31 17:48:00 -0400292
293/**
294 * Produce a mtd_info given a type and num.
295 *
296 * @param type mtd type
297 * @param num mtd number
298 * @param mtd a pointer to an mtd_info instance (output)
299 * @return 0 if device is valid, 1 otherwise
300 */
301static int get_mtd_info(u8 type, u8 num, struct mtd_info **mtd)
302{
303 char mtd_dev[16];
304
305 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(type), num);
306 *mtd = get_mtd_device_nm(mtd_dev);
307 if (IS_ERR(*mtd)) {
308 printf("Device %s not found!\n", mtd_dev);
309 return 1;
310 }
Heiko Schocher1668d642014-07-14 09:17:11 +0200311 put_mtd_device(*mtd);
Ben Gardiner0a026d32010-08-31 17:48:00 -0400312
313 return 0;
314}
315
Stefan Roese68d7d652009-03-19 13:30:36 +0100316/**
Stefan Roese864aa032009-05-12 14:31:56 +0200317 * Performs sanity check for supplied flash partition.
318 * Table of existing MTD flash devices is searched and partition device
Stefan Roese68d7d652009-03-19 13:30:36 +0100319 * is located. Alignment with the granularity of nand erasesize is verified.
320 *
321 * @param id of the parent device
322 * @param part partition to validate
323 * @return 0 if partition is valid, 1 otherwise
324 */
Stefan Roese864aa032009-05-12 14:31:56 +0200325static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
Stefan Roese68d7d652009-03-19 13:30:36 +0100326{
Ben Gardiner0a026d32010-08-31 17:48:00 -0400327 struct mtd_info *mtd = NULL;
Stefan Roese864aa032009-05-12 14:31:56 +0200328 int i, j;
329 ulong start;
Paul Burton39ac3442013-09-04 15:16:57 +0100330 u64 offset, size;
Stefan Roese68d7d652009-03-19 13:30:36 +0100331
Ben Gardiner0a026d32010-08-31 17:48:00 -0400332 if (get_mtd_info(id->type, id->num, &mtd))
Stefan Roese864aa032009-05-12 14:31:56 +0200333 return 1;
Stefan Roese68d7d652009-03-19 13:30:36 +0100334
335 part->sector_size = mtd->erasesize;
336
Stefan Roese864aa032009-05-12 14:31:56 +0200337 if (!mtd->numeraseregions) {
338 /*
339 * Only one eraseregion (NAND, OneNAND or uniform NOR),
340 * checking for alignment is easy here
341 */
Paul Burton39ac3442013-09-04 15:16:57 +0100342 offset = part->offset;
343 if (do_div(offset, mtd->erasesize)) {
Stefan Roese864aa032009-05-12 14:31:56 +0200344 printf("%s%d: partition (%s) start offset"
345 "alignment incorrect\n",
346 MTD_DEV_TYPE(id->type), id->num, part->name);
347 return 1;
348 }
Stefan Roese68d7d652009-03-19 13:30:36 +0100349
Paul Burton39ac3442013-09-04 15:16:57 +0100350 size = part->size;
351 if (do_div(size, mtd->erasesize)) {
Stefan Roese864aa032009-05-12 14:31:56 +0200352 printf("%s%d: partition (%s) size alignment incorrect\n",
353 MTD_DEV_TYPE(id->type), id->num, part->name);
354 return 1;
355 }
356 } else {
357 /*
358 * Multiple eraseregions (non-uniform NOR),
359 * checking for alignment is more complex here
360 */
361
362 /* Check start alignment */
363 for (i = 0; i < mtd->numeraseregions; i++) {
364 start = mtd->eraseregions[i].offset;
365 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
366 if (part->offset == start)
367 goto start_ok;
368 start += mtd->eraseregions[i].erasesize;
369 }
370 }
371
372 printf("%s%d: partition (%s) start offset alignment incorrect\n",
373 MTD_DEV_TYPE(id->type), id->num, part->name);
Stefan Roese68d7d652009-03-19 13:30:36 +0100374 return 1;
Stefan Roese864aa032009-05-12 14:31:56 +0200375
376 start_ok:
377
378 /* Check end/size alignment */
379 for (i = 0; i < mtd->numeraseregions; i++) {
380 start = mtd->eraseregions[i].offset;
381 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
382 if ((part->offset + part->size) == start)
383 goto end_ok;
384 start += mtd->eraseregions[i].erasesize;
385 }
386 }
387 /* Check last sector alignment */
388 if ((part->offset + part->size) == start)
389 goto end_ok;
390
391 printf("%s%d: partition (%s) size alignment incorrect\n",
392 MTD_DEV_TYPE(id->type), id->num, part->name);
393 return 1;
394
395 end_ok:
396 return 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100397 }
398
399 return 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100400}
401
402
403/**
Otavio Salvadorf5019912013-10-03 08:04:38 -0300404 * Performs sanity check for supplied partition. Offset and size are
405 * verified to be within valid range. Partition type is checked and
406 * part_validate_eraseblock() is called with the argument of part.
Stefan Roese68d7d652009-03-19 13:30:36 +0100407 *
408 * @param id of the parent device
409 * @param part partition to validate
410 * @return 0 if partition is valid, 1 otherwise
411 */
412static int part_validate(struct mtdids *id, struct part_info *part)
413{
414 if (part->size == SIZE_REMAINING)
415 part->size = id->size - part->offset;
416
417 if (part->offset > id->size) {
Paul Burton39ac3442013-09-04 15:16:57 +0100418 printf("%s: offset %08llx beyond flash size %08llx\n",
Stefan Roese68d7d652009-03-19 13:30:36 +0100419 id->mtd_id, part->offset, id->size);
420 return 1;
421 }
422
423 if ((part->offset + part->size) <= part->offset) {
424 printf("%s%d: partition (%s) size too big\n",
425 MTD_DEV_TYPE(id->type), id->num, part->name);
426 return 1;
427 }
428
429 if (part->offset + part->size > id->size) {
430 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
431 return 1;
432 }
433
Stefan Roese864aa032009-05-12 14:31:56 +0200434 /*
435 * Now we need to check if the partition starts and ends on
436 * sector (eraseblock) regions
437 */
438 return part_validate_eraseblock(id, part);
Stefan Roese68d7d652009-03-19 13:30:36 +0100439}
440
441/**
Robert P. J. Day1f8b5462013-08-21 11:39:19 -0400442 * Delete selected partition from the partition list of the specified device.
Stefan Roese68d7d652009-03-19 13:30:36 +0100443 *
444 * @param dev device to delete partition from
445 * @param part partition to delete
446 * @return 0 on success, 1 otherwise
447 */
448static int part_del(struct mtd_device *dev, struct part_info *part)
449{
450 u8 current_save_needed = 0;
451
452 /* if there is only one partition, remove whole device */
453 if (dev->num_parts == 1)
454 return device_del(dev);
455
456 /* otherwise just delete this partition */
457
Stefan Roese76b58832009-05-16 12:04:22 +0200458 if (dev == current_mtd_dev) {
Stefan Roese68d7d652009-03-19 13:30:36 +0100459 /* we are modyfing partitions for the current device,
460 * update current */
461 struct part_info *curr_pi;
Stefan Roese76b58832009-05-16 12:04:22 +0200462 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +0100463
464 if (curr_pi) {
465 if (curr_pi == part) {
466 printf("current partition deleted, resetting current to 0\n");
Stefan Roese76b58832009-05-16 12:04:22 +0200467 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100468 } else if (part->offset <= curr_pi->offset) {
Stefan Roese76b58832009-05-16 12:04:22 +0200469 current_mtd_partnum--;
Stefan Roese68d7d652009-03-19 13:30:36 +0100470 }
471 current_save_needed = 1;
472 }
473 }
474
Stefan Roese68d7d652009-03-19 13:30:36 +0100475 list_del(&part->link);
476 free(part);
477 dev->num_parts--;
478
479 if (current_save_needed > 0)
480 current_save();
481 else
482 index_partitions();
483
484 return 0;
485}
486
487/**
488 * Delete all partitions from parts head list, free memory.
489 *
490 * @param head list of partitions to delete
491 */
492static void part_delall(struct list_head *head)
493{
494 struct list_head *entry, *n;
495 struct part_info *part_tmp;
496
497 /* clean tmp_list and free allocated memory */
498 list_for_each_safe(entry, n, head) {
499 part_tmp = list_entry(entry, struct part_info, link);
500
Stefan Roese68d7d652009-03-19 13:30:36 +0100501 list_del(entry);
502 free(part_tmp);
503 }
504}
505
506/**
507 * Add new partition to the supplied partition list. Make sure partitions are
508 * sorted by offset in ascending order.
509 *
510 * @param head list this partition is to be added to
511 * @param new partition to be added
512 */
513static int part_sort_add(struct mtd_device *dev, struct part_info *part)
514{
515 struct list_head *entry;
516 struct part_info *new_pi, *curr_pi;
517
518 /* link partition to parrent dev */
519 part->dev = dev;
520
521 if (list_empty(&dev->parts)) {
Wolfgang Denke1d29502010-04-28 10:58:10 +0200522 debug("part_sort_add: list empty\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100523 list_add(&part->link, &dev->parts);
524 dev->num_parts++;
525 index_partitions();
526 return 0;
527 }
528
529 new_pi = list_entry(&part->link, struct part_info, link);
530
531 /* get current partition info if we are updating current device */
532 curr_pi = NULL;
Stefan Roese76b58832009-05-16 12:04:22 +0200533 if (dev == current_mtd_dev)
534 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +0100535
536 list_for_each(entry, &dev->parts) {
537 struct part_info *pi;
538
539 pi = list_entry(entry, struct part_info, link);
540
541 /* be compliant with kernel cmdline, allow only one partition at offset zero */
542 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
543 printf("cannot add second partition at offset 0\n");
544 return 1;
545 }
546
547 if (new_pi->offset <= pi->offset) {
548 list_add_tail(&part->link, entry);
549 dev->num_parts++;
550
551 if (curr_pi && (pi->offset <= curr_pi->offset)) {
552 /* we are modyfing partitions for the current
553 * device, update current */
Stefan Roese76b58832009-05-16 12:04:22 +0200554 current_mtd_partnum++;
Stefan Roese68d7d652009-03-19 13:30:36 +0100555 current_save();
556 } else {
557 index_partitions();
558 }
559 return 0;
560 }
561 }
562
563 list_add_tail(&part->link, &dev->parts);
564 dev->num_parts++;
565 index_partitions();
566 return 0;
567}
568
569/**
570 * Add provided partition to the partition list of a given device.
571 *
572 * @param dev device to which partition is added
573 * @param part partition to be added
574 * @return 0 on success, 1 otherwise
575 */
576static int part_add(struct mtd_device *dev, struct part_info *part)
577{
578 /* verify alignment and size */
579 if (part_validate(dev->id, part) != 0)
580 return 1;
581
582 /* partition is ok, add it to the list */
583 if (part_sort_add(dev, part) != 0)
584 return 1;
585
586 return 0;
587}
588
589/**
590 * Parse one partition definition, allocate memory and return pointer to this
591 * location in retpart.
592 *
593 * @param partdef pointer to the partition definition string i.e. <part-def>
594 * @param ret output pointer to next char after parse completes (output)
595 * @param retpart pointer to the allocated partition (output)
596 * @return 0 on success, 1 otherwise
597 */
598static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
599{
600 struct part_info *part;
Paul Burton39ac3442013-09-04 15:16:57 +0100601 u64 size;
602 u64 offset;
Stefan Roese68d7d652009-03-19 13:30:36 +0100603 const char *name;
604 int name_len;
605 unsigned int mask_flags;
606 const char *p;
607
608 p = partdef;
609 *retpart = NULL;
610 *ret = NULL;
611
612 /* fetch the partition size */
613 if (*p == '-') {
614 /* assign all remaining space to this partition */
Wolfgang Denke1d29502010-04-28 10:58:10 +0200615 debug("'-': remaining size assigned\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100616 size = SIZE_REMAINING;
617 p++;
618 } else {
619 size = memsize_parse(p, &p);
620 if (size < MIN_PART_SIZE) {
Paul Burton39ac3442013-09-04 15:16:57 +0100621 printf("partition size too small (%llx)\n", size);
Stefan Roese68d7d652009-03-19 13:30:36 +0100622 return 1;
623 }
624 }
625
626 /* check for offset */
627 offset = OFFSET_NOT_SPECIFIED;
628 if (*p == '@') {
629 p++;
630 offset = memsize_parse(p, &p);
631 }
632
633 /* now look for the name */
634 if (*p == '(') {
635 name = ++p;
636 if ((p = strchr(name, ')')) == NULL) {
637 printf("no closing ) found in partition name\n");
638 return 1;
639 }
640 name_len = p - name + 1;
641 if ((name_len - 1) == 0) {
642 printf("empty partition name\n");
643 return 1;
644 }
645 p++;
646 } else {
647 /* 0x00000000@0x00000000 */
648 name_len = 22;
649 name = NULL;
650 }
651
652 /* test for options */
653 mask_flags = 0;
654 if (strncmp(p, "ro", 2) == 0) {
655 mask_flags |= MTD_WRITEABLE_CMD;
656 p += 2;
657 }
658
659 /* check for next partition definition */
660 if (*p == ',') {
661 if (size == SIZE_REMAINING) {
662 *ret = NULL;
663 printf("no partitions allowed after a fill-up partition\n");
664 return 1;
665 }
666 *ret = ++p;
667 } else if ((*p == ';') || (*p == '\0')) {
668 *ret = p;
669 } else {
670 printf("unexpected character '%c' at the end of partition\n", *p);
671 *ret = NULL;
672 return 1;
673 }
674
675 /* allocate memory */
676 part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
677 if (!part) {
678 printf("out of memory\n");
679 return 1;
680 }
681 memset(part, 0, sizeof(struct part_info) + name_len);
682 part->size = size;
683 part->offset = offset;
684 part->mask_flags = mask_flags;
685 part->name = (char *)(part + 1);
686
687 if (name) {
688 /* copy user provided name */
689 strncpy(part->name, name, name_len - 1);
690 part->auto_name = 0;
691 } else {
692 /* auto generated name in form of size@offset */
Paul Burton39ac3442013-09-04 15:16:57 +0100693 sprintf(part->name, "0x%08llx@0x%08llx", size, offset);
Stefan Roese68d7d652009-03-19 13:30:36 +0100694 part->auto_name = 1;
695 }
696
697 part->name[name_len - 1] = '\0';
698 INIT_LIST_HEAD(&part->link);
699
Paul Burton39ac3442013-09-04 15:16:57 +0100700 debug("+ partition: name %-22s size 0x%08llx offset 0x%08llx mask flags %d\n",
Stefan Roese68d7d652009-03-19 13:30:36 +0100701 part->name, part->size,
702 part->offset, part->mask_flags);
703
704 *retpart = part;
705 return 0;
706}
707
708/**
709 * Check device number to be within valid range for given device type.
710 *
Ben Gardiner0a026d32010-08-31 17:48:00 -0400711 * @param type mtd type
712 * @param num mtd number
713 * @param size a pointer to the size of the mtd device (output)
Stefan Roese68d7d652009-03-19 13:30:36 +0100714 * @return 0 if device is valid, 1 otherwise
715 */
Paul Burton39ac3442013-09-04 15:16:57 +0100716static int mtd_device_validate(u8 type, u8 num, u64 *size)
Stefan Roese68d7d652009-03-19 13:30:36 +0100717{
Ben Gardiner0a026d32010-08-31 17:48:00 -0400718 struct mtd_info *mtd = NULL;
Stefan Roese68d7d652009-03-19 13:30:36 +0100719
Ben Gardiner0a026d32010-08-31 17:48:00 -0400720 if (get_mtd_info(type, num, &mtd))
Stefan Roese864aa032009-05-12 14:31:56 +0200721 return 1;
Stefan Roese68d7d652009-03-19 13:30:36 +0100722
Stefan Roese864aa032009-05-12 14:31:56 +0200723 *size = mtd->size;
Stefan Roese68d7d652009-03-19 13:30:36 +0100724
Stefan Roese864aa032009-05-12 14:31:56 +0200725 return 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100726}
727
728/**
729 * Delete all mtd devices from a supplied devices list, free memory allocated for
730 * each device and delete all device partitions.
731 *
732 * @return 0 on success, 1 otherwise
733 */
734static int device_delall(struct list_head *head)
735{
736 struct list_head *entry, *n;
737 struct mtd_device *dev_tmp;
738
739 /* clean devices list */
740 list_for_each_safe(entry, n, head) {
741 dev_tmp = list_entry(entry, struct mtd_device, link);
742 list_del(entry);
743 part_delall(&dev_tmp->parts);
744 free(dev_tmp);
745 }
746 INIT_LIST_HEAD(&devices);
747
748 return 0;
749}
750
751/**
752 * If provided device exists it's partitions are deleted, device is removed
753 * from device list and device memory is freed.
754 *
755 * @param dev device to be deleted
756 * @return 0 on success, 1 otherwise
757 */
758static int device_del(struct mtd_device *dev)
759{
760 part_delall(&dev->parts);
761 list_del(&dev->link);
762 free(dev);
763
Stefan Roese76b58832009-05-16 12:04:22 +0200764 if (dev == current_mtd_dev) {
Stefan Roese68d7d652009-03-19 13:30:36 +0100765 /* we just deleted current device */
766 if (list_empty(&devices)) {
Stefan Roese76b58832009-05-16 12:04:22 +0200767 current_mtd_dev = NULL;
Stefan Roese68d7d652009-03-19 13:30:36 +0100768 } else {
769 /* reset first partition from first dev from the
770 * devices list as current */
Stefan Roese76b58832009-05-16 12:04:22 +0200771 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
772 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100773 }
774 current_save();
775 return 0;
776 }
777
778 index_partitions();
779 return 0;
780}
781
782/**
783 * Search global device list and return pointer to the device of type and num
784 * specified.
785 *
786 * @param type device type
787 * @param num device number
788 * @return NULL if requested device does not exist
789 */
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100790struct mtd_device *device_find(u8 type, u8 num)
Stefan Roese68d7d652009-03-19 13:30:36 +0100791{
792 struct list_head *entry;
793 struct mtd_device *dev_tmp;
794
795 list_for_each(entry, &devices) {
796 dev_tmp = list_entry(entry, struct mtd_device, link);
797
798 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
799 return dev_tmp;
800 }
801
802 return NULL;
803}
804
805/**
806 * Add specified device to the global device list.
807 *
808 * @param dev device to be added
809 */
810static void device_add(struct mtd_device *dev)
811{
812 u8 current_save_needed = 0;
813
814 if (list_empty(&devices)) {
Stefan Roese76b58832009-05-16 12:04:22 +0200815 current_mtd_dev = dev;
816 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +0100817 current_save_needed = 1;
818 }
819
820 list_add_tail(&dev->link, &devices);
821
822 if (current_save_needed > 0)
823 current_save();
824 else
825 index_partitions();
826}
827
828/**
829 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
830 * return pointer to the device structure.
831 *
832 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
833 * @param ret output pointer to next char after parse completes (output)
834 * @param retdev pointer to the allocated device (output)
835 * @return 0 on success, 1 otherwise
836 */
837static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
838{
839 struct mtd_device *dev;
840 struct part_info *part;
841 struct mtdids *id;
842 const char *mtd_id;
843 unsigned int mtd_id_len;
Wolfgang Denk419a1fe2011-10-05 22:48:12 +0200844 const char *p;
Wolfgang Denk419a1fe2011-10-05 22:48:12 +0200845 const char *pend;
Stefan Roese68d7d652009-03-19 13:30:36 +0100846 LIST_HEAD(tmp_list);
847 struct list_head *entry, *n;
848 u16 num_parts;
Paul Burton39ac3442013-09-04 15:16:57 +0100849 u64 offset;
Stefan Roese68d7d652009-03-19 13:30:36 +0100850 int err = 1;
851
Wolfgang Denke1d29502010-04-28 10:58:10 +0200852 debug("===device_parse===\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100853
Wolfgang Denk2697eff2010-04-28 10:53:47 +0200854 assert(retdev);
855 *retdev = NULL;
856
857 if (ret)
858 *ret = NULL;
859
Stefan Roese68d7d652009-03-19 13:30:36 +0100860 /* fetch <mtd-id> */
Wolfgang Denk2697eff2010-04-28 10:53:47 +0200861 mtd_id = p = mtd_dev;
Stefan Roese68d7d652009-03-19 13:30:36 +0100862 if (!(p = strchr(mtd_id, ':'))) {
863 printf("no <mtd-id> identifier\n");
864 return 1;
865 }
866 mtd_id_len = p - mtd_id + 1;
867 p++;
868
869 /* verify if we have a valid device specified */
870 if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
871 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
872 return 1;
873 }
874
Wolfgang Denk419a1fe2011-10-05 22:48:12 +0200875 pend = strchr(p, ';');
Wolfgang Denke1d29502010-04-28 10:58:10 +0200876 debug("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
Stefan Roese68d7d652009-03-19 13:30:36 +0100877 id->type, MTD_DEV_TYPE(id->type),
878 id->num, id->mtd_id);
Vasili Galka5d69a5d2014-08-26 13:45:48 +0300879 debug("parsing partitions %.*s\n", (int)(pend ? pend - p : strlen(p)), p);
Stefan Roese68d7d652009-03-19 13:30:36 +0100880
Stefan Roese68d7d652009-03-19 13:30:36 +0100881 /* parse partitions */
882 num_parts = 0;
883
884 offset = 0;
885 if ((dev = device_find(id->type, id->num)) != NULL) {
886 /* if device already exists start at the end of the last partition */
887 part = list_entry(dev->parts.prev, struct part_info, link);
888 offset = part->offset + part->size;
889 }
890
891 while (p && (*p != '\0') && (*p != ';')) {
892 err = 1;
893 if ((part_parse(p, &p, &part) != 0) || (!part))
894 break;
895
896 /* calculate offset when not specified */
897 if (part->offset == OFFSET_NOT_SPECIFIED)
898 part->offset = offset;
899 else
900 offset = part->offset;
901
902 /* verify alignment and size */
903 if (part_validate(id, part) != 0)
904 break;
905
906 offset += part->size;
907
908 /* partition is ok, add it to the list */
909 list_add_tail(&part->link, &tmp_list);
910 num_parts++;
911 err = 0;
912 }
913 if (err == 1) {
914 part_delall(&tmp_list);
915 return 1;
916 }
917
Wolfgang Denke1d29502010-04-28 10:58:10 +0200918 debug("\ntotal partitions: %d\n", num_parts);
Stefan Roese68d7d652009-03-19 13:30:36 +0100919
920 /* check for next device presence */
921 if (p) {
922 if (*p == ';') {
Wolfgang Denk2697eff2010-04-28 10:53:47 +0200923 if (ret)
924 *ret = ++p;
Stefan Roese68d7d652009-03-19 13:30:36 +0100925 } else if (*p == '\0') {
Wolfgang Denk2697eff2010-04-28 10:53:47 +0200926 if (ret)
927 *ret = p;
Stefan Roese68d7d652009-03-19 13:30:36 +0100928 } else {
929 printf("unexpected character '%c' at the end of device\n", *p);
Wolfgang Denk2697eff2010-04-28 10:53:47 +0200930 if (ret)
931 *ret = NULL;
Stefan Roese68d7d652009-03-19 13:30:36 +0100932 return 1;
933 }
934 }
935
936 /* allocate memory for mtd_device structure */
937 if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
938 printf("out of memory\n");
939 return 1;
940 }
941 memset(dev, 0, sizeof(struct mtd_device));
942 dev->id = id;
943 dev->num_parts = 0; /* part_sort_add increments num_parts */
944 INIT_LIST_HEAD(&dev->parts);
945 INIT_LIST_HEAD(&dev->link);
946
947 /* move partitions from tmp_list to dev->parts */
948 list_for_each_safe(entry, n, &tmp_list) {
949 part = list_entry(entry, struct part_info, link);
950 list_del(entry);
951 if (part_sort_add(dev, part) != 0) {
952 device_del(dev);
953 return 1;
954 }
955 }
956
957 *retdev = dev;
958
Wolfgang Denke1d29502010-04-28 10:58:10 +0200959 debug("===\n\n");
Stefan Roese68d7d652009-03-19 13:30:36 +0100960 return 0;
961}
962
963/**
964 * Initialize global device list.
965 *
966 * @return 0 on success, 1 otherwise
967 */
968static int mtd_devices_init(void)
969{
970 last_parts[0] = '\0';
Stefan Roese76b58832009-05-16 12:04:22 +0200971 current_mtd_dev = NULL;
Stefan Roese68d7d652009-03-19 13:30:36 +0100972 current_save();
973
974 return device_delall(&devices);
975}
976
977/*
978 * Search global mtdids list and find id of requested type and number.
979 *
980 * @return pointer to the id if it exists, NULL otherwise
981 */
982static struct mtdids* id_find(u8 type, u8 num)
983{
984 struct list_head *entry;
985 struct mtdids *id;
986
987 list_for_each(entry, &mtdids) {
988 id = list_entry(entry, struct mtdids, link);
989
990 if ((id->type == type) && (id->num == num))
991 return id;
992 }
993
994 return NULL;
995}
996
997/**
998 * Search global mtdids list and find id of a requested mtd_id.
999 *
1000 * Note: first argument is not null terminated.
1001 *
1002 * @param mtd_id string containing requested mtd_id
1003 * @param mtd_id_len length of supplied mtd_id
1004 * @return pointer to the id if it exists, NULL otherwise
1005 */
1006static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
1007{
1008 struct list_head *entry;
1009 struct mtdids *id;
1010
Wolfgang Denke1d29502010-04-28 10:58:10 +02001011 debug("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
Stefan Roese68d7d652009-03-19 13:30:36 +01001012 mtd_id_len, mtd_id, mtd_id_len);
1013
1014 list_for_each(entry, &mtdids) {
1015 id = list_entry(entry, struct mtdids, link);
1016
Vasili Galka5d69a5d2014-08-26 13:45:48 +03001017 debug("entry: '%s' (len = %zu)\n",
Stefan Roese68d7d652009-03-19 13:30:36 +01001018 id->mtd_id, strlen(id->mtd_id));
1019
1020 if (mtd_id_len != strlen(id->mtd_id))
1021 continue;
1022 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1023 return id;
1024 }
1025
1026 return NULL;
1027}
1028
1029/**
1030 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
1031 * return device type and number.
1032 *
1033 * @param id string describing device id
1034 * @param ret_id output pointer to next char after parse completes (output)
1035 * @param dev_type parsed device type (output)
1036 * @param dev_num parsed device number (output)
1037 * @return 0 on success, 1 otherwise
1038 */
Kim Phillips088f1b12012-10-29 13:34:31 +00001039int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type,
1040 u8 *dev_num)
Stefan Roese68d7d652009-03-19 13:30:36 +01001041{
1042 const char *p = id;
1043
1044 *dev_type = 0;
1045 if (strncmp(p, "nand", 4) == 0) {
1046 *dev_type = MTD_DEV_TYPE_NAND;
1047 p += 4;
1048 } else if (strncmp(p, "nor", 3) == 0) {
1049 *dev_type = MTD_DEV_TYPE_NOR;
1050 p += 3;
1051 } else if (strncmp(p, "onenand", 7) == 0) {
1052 *dev_type = MTD_DEV_TYPE_ONENAND;
1053 p += 7;
1054 } else {
1055 printf("incorrect device type in %s\n", id);
1056 return 1;
1057 }
1058
1059 if (!isdigit(*p)) {
1060 printf("incorrect device number in %s\n", id);
1061 return 1;
1062 }
1063
1064 *dev_num = simple_strtoul(p, (char **)&p, 0);
1065 if (ret_id)
1066 *ret_id = p;
1067 return 0;
1068}
1069
1070/**
1071 * Process all devices and generate corresponding mtdparts string describing
1072 * all partitions on all devices.
1073 *
1074 * @param buf output buffer holding generated mtdparts string (output)
1075 * @param buflen buffer size
1076 * @return 0 on success, 1 otherwise
1077 */
1078static int generate_mtdparts(char *buf, u32 buflen)
1079{
1080 struct list_head *pentry, *dentry;
1081 struct mtd_device *dev;
1082 struct part_info *part, *prev_part;
1083 char *p = buf;
1084 char tmpbuf[32];
Paul Burton39ac3442013-09-04 15:16:57 +01001085 u64 size, offset;
1086 u32 len, part_cnt;
Stefan Roese68d7d652009-03-19 13:30:36 +01001087 u32 maxlen = buflen - 1;
1088
Wolfgang Denke1d29502010-04-28 10:58:10 +02001089 debug("--- generate_mtdparts ---\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001090
1091 if (list_empty(&devices)) {
1092 buf[0] = '\0';
1093 return 0;
1094 }
1095
Ben Whitten192bc692015-12-30 13:05:58 +00001096 strcpy(p, "mtdparts=");
Stefan Roese68d7d652009-03-19 13:30:36 +01001097 p += 9;
1098
1099 list_for_each(dentry, &devices) {
1100 dev = list_entry(dentry, struct mtd_device, link);
1101
1102 /* copy mtd_id */
1103 len = strlen(dev->id->mtd_id) + 1;
1104 if (len > maxlen)
1105 goto cleanup;
1106 memcpy(p, dev->id->mtd_id, len - 1);
1107 p += len - 1;
1108 *(p++) = ':';
1109 maxlen -= len;
1110
1111 /* format partitions */
1112 prev_part = NULL;
1113 part_cnt = 0;
1114 list_for_each(pentry, &dev->parts) {
1115 part = list_entry(pentry, struct part_info, link);
1116 size = part->size;
1117 offset = part->offset;
1118 part_cnt++;
1119
1120 /* partition size */
1121 memsize_format(tmpbuf, size);
1122 len = strlen(tmpbuf);
1123 if (len > maxlen)
1124 goto cleanup;
1125 memcpy(p, tmpbuf, len);
1126 p += len;
1127 maxlen -= len;
1128
1129
1130 /* add offset only when there is a gap between
1131 * partitions */
1132 if ((!prev_part && (offset != 0)) ||
1133 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1134
1135 memsize_format(tmpbuf, offset);
1136 len = strlen(tmpbuf) + 1;
1137 if (len > maxlen)
1138 goto cleanup;
1139 *(p++) = '@';
1140 memcpy(p, tmpbuf, len - 1);
1141 p += len - 1;
1142 maxlen -= len;
1143 }
1144
1145 /* copy name only if user supplied */
1146 if(!part->auto_name) {
1147 len = strlen(part->name) + 2;
1148 if (len > maxlen)
1149 goto cleanup;
1150
1151 *(p++) = '(';
1152 memcpy(p, part->name, len - 2);
1153 p += len - 2;
1154 *(p++) = ')';
1155 maxlen -= len;
1156 }
1157
1158 /* ro mask flag */
1159 if (part->mask_flags && MTD_WRITEABLE_CMD) {
1160 len = 2;
1161 if (len > maxlen)
1162 goto cleanup;
1163 *(p++) = 'r';
1164 *(p++) = 'o';
1165 maxlen -= 2;
1166 }
1167
1168 /* print ',' separator if there are other partitions
1169 * following */
1170 if (dev->num_parts > part_cnt) {
1171 if (1 > maxlen)
1172 goto cleanup;
1173 *(p++) = ',';
1174 maxlen--;
1175 }
1176 prev_part = part;
1177 }
1178 /* print ';' separator if there are other devices following */
1179 if (dentry->next != &devices) {
1180 if (1 > maxlen)
1181 goto cleanup;
1182 *(p++) = ';';
1183 maxlen--;
1184 }
1185 }
1186
1187 /* we still have at least one char left, as we decremented maxlen at
1188 * the begining */
1189 *p = '\0';
1190
1191 return 0;
1192
1193cleanup:
1194 last_parts[0] = '\0';
1195 return 1;
1196}
1197
1198/**
1199 * Call generate_mtdparts to process all devices and generate corresponding
1200 * mtdparts string, save it in mtdparts environment variable.
1201 *
1202 * @param buf output buffer holding generated mtdparts string (output)
1203 * @param buflen buffer size
1204 * @return 0 on success, 1 otherwise
1205 */
1206static int generate_mtdparts_save(char *buf, u32 buflen)
1207{
1208 int ret;
1209
1210 ret = generate_mtdparts(buf, buflen);
1211
1212 if ((buf[0] != '\0') && (ret == 0))
Simon Glass382bee52017-08-03 12:22:09 -06001213 env_set("mtdparts", buf);
Stefan Roese68d7d652009-03-19 13:30:36 +01001214 else
Simon Glass382bee52017-08-03 12:22:09 -06001215 env_set("mtdparts", NULL);
Stefan Roese68d7d652009-03-19 13:30:36 +01001216
1217 return ret;
1218}
1219
Ben Gardiner04ac3802010-08-31 17:48:02 -04001220#if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
Stefan Roese68d7d652009-03-19 13:30:36 +01001221/**
Ben Gardiner04ac3802010-08-31 17:48:02 -04001222 * Get the net size (w/o bad blocks) of the given partition.
1223 *
1224 * @param mtd the mtd info
1225 * @param part the partition
1226 * @return the calculated net size of this partition
Stefan Roese68d7d652009-03-19 13:30:36 +01001227 */
Ben Gardiner04ac3802010-08-31 17:48:02 -04001228static uint64_t net_part_size(struct mtd_info *mtd, struct part_info *part)
1229{
Scott Wood36650ca2010-09-09 15:40:03 -05001230 uint64_t i, net_size = 0;
1231
Ben Gardiner04ac3802010-08-31 17:48:02 -04001232 if (!mtd->block_isbad)
1233 return part->size;
1234
Ben Gardiner04ac3802010-08-31 17:48:02 -04001235 for (i = 0; i < part->size; i += mtd->erasesize) {
1236 if (!mtd->block_isbad(mtd, part->offset + i))
1237 net_size += mtd->erasesize;
1238 }
Scott Wood36650ca2010-09-09 15:40:03 -05001239
Ben Gardiner04ac3802010-08-31 17:48:02 -04001240 return net_size;
1241}
1242#endif
1243
1244static void print_partition_table(void)
Stefan Roese68d7d652009-03-19 13:30:36 +01001245{
1246 struct list_head *dentry, *pentry;
1247 struct part_info *part;
1248 struct mtd_device *dev;
1249 int part_num;
1250
Stefan Roese68d7d652009-03-19 13:30:36 +01001251 list_for_each(dentry, &devices) {
1252 dev = list_entry(dentry, struct mtd_device, link);
Ben Gardiner04ac3802010-08-31 17:48:02 -04001253 /* list partitions for given device */
1254 part_num = 0;
1255#if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
1256 struct mtd_info *mtd;
1257
1258 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
1259 return;
1260
1261 printf("\ndevice %s%d <%s>, # parts = %d\n",
1262 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1263 dev->id->mtd_id, dev->num_parts);
1264 printf(" #: name\t\tsize\t\tnet size\toffset\t\tmask_flags\n");
1265
1266 list_for_each(pentry, &dev->parts) {
1267 u32 net_size;
1268 char *size_note;
1269
1270 part = list_entry(pentry, struct part_info, link);
1271 net_size = net_part_size(mtd, part);
1272 size_note = part->size == net_size ? " " : " (!)";
1273 printf("%2d: %-20s0x%08x\t0x%08x%s\t0x%08x\t%d\n",
1274 part_num, part->name, part->size,
1275 net_size, size_note, part->offset,
1276 part->mask_flags);
1277#else /* !defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
Stefan Roese68d7d652009-03-19 13:30:36 +01001278 printf("\ndevice %s%d <%s>, # parts = %d\n",
1279 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1280 dev->id->mtd_id, dev->num_parts);
David Brownell08f077d2009-04-16 19:55:48 -07001281 printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001282
Stefan Roese68d7d652009-03-19 13:30:36 +01001283 list_for_each(pentry, &dev->parts) {
1284 part = list_entry(pentry, struct part_info, link);
Paul Burton39ac3442013-09-04 15:16:57 +01001285 printf("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Stefan Roese68d7d652009-03-19 13:30:36 +01001286 part_num, part->name, part->size,
1287 part->offset, part->mask_flags);
Ben Gardiner04ac3802010-08-31 17:48:02 -04001288#endif /* defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
Stefan Roese68d7d652009-03-19 13:30:36 +01001289 part_num++;
1290 }
1291 }
Ben Gardiner04ac3802010-08-31 17:48:02 -04001292
Stefan Roese68d7d652009-03-19 13:30:36 +01001293 if (list_empty(&devices))
1294 printf("no partitions defined\n");
Ben Gardiner04ac3802010-08-31 17:48:02 -04001295}
1296
1297/**
1298 * Format and print out a partition list for each device from global device
1299 * list.
1300 */
1301static void list_partitions(void)
1302{
1303 struct part_info *part;
1304
1305 debug("\n---list_partitions---\n");
1306 print_partition_table();
Stefan Roese68d7d652009-03-19 13:30:36 +01001307
Stefan Roese76b58832009-05-16 12:04:22 +02001308 /* current_mtd_dev is not NULL only when we have non empty device list */
1309 if (current_mtd_dev) {
1310 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +01001311 if (part) {
Paul Burton39ac3442013-09-04 15:16:57 +01001312 printf("\nactive partition: %s%d,%d - (%s) 0x%08llx @ 0x%08llx\n",
Stefan Roese76b58832009-05-16 12:04:22 +02001313 MTD_DEV_TYPE(current_mtd_dev->id->type),
1314 current_mtd_dev->id->num, current_mtd_partnum,
Stefan Roese68d7d652009-03-19 13:30:36 +01001315 part->name, part->size, part->offset);
1316 } else {
1317 printf("could not get current partition info\n\n");
1318 }
1319 }
1320
1321 printf("\ndefaults:\n");
Wolfgang Denk36c91692009-05-17 16:01:54 +02001322 printf("mtdids : %s\n",
1323 mtdids_default ? mtdids_default : "none");
Anatolij Gustschina6934472010-02-24 00:29:44 +01001324 /*
1325 * Using printf() here results in printbuffer overflow
1326 * if default mtdparts string is greater than console
1327 * printbuffer. Use puts() to prevent system crashes.
1328 */
1329 puts("mtdparts: ");
1330 puts(mtdparts_default ? mtdparts_default : "none");
1331 puts("\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001332}
1333
1334/**
1335 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1336 * corresponding device and verify partition number.
1337 *
1338 * @param id string describing device and partition or partition name
1339 * @param dev pointer to the requested device (output)
1340 * @param part_num verified partition number (output)
1341 * @param part pointer to requested partition (output)
1342 * @return 0 on success, 1 otherwise
1343 */
1344int find_dev_and_part(const char *id, struct mtd_device **dev,
1345 u8 *part_num, struct part_info **part)
1346{
1347 struct list_head *dentry, *pentry;
1348 u8 type, dnum, pnum;
1349 const char *p;
1350
Wolfgang Denke1d29502010-04-28 10:58:10 +02001351 debug("--- find_dev_and_part ---\nid = %s\n", id);
Stefan Roese68d7d652009-03-19 13:30:36 +01001352
1353 list_for_each(dentry, &devices) {
1354 *part_num = 0;
1355 *dev = list_entry(dentry, struct mtd_device, link);
1356 list_for_each(pentry, &(*dev)->parts) {
1357 *part = list_entry(pentry, struct part_info, link);
1358 if (strcmp((*part)->name, id) == 0)
1359 return 0;
1360 (*part_num)++;
1361 }
1362 }
1363
1364 p = id;
1365 *dev = NULL;
1366 *part = NULL;
1367 *part_num = 0;
1368
1369 if (mtd_id_parse(p, &p, &type, &dnum) != 0)
1370 return 1;
1371
1372 if ((*p++ != ',') || (*p == '\0')) {
1373 printf("no partition number specified\n");
1374 return 1;
1375 }
1376 pnum = simple_strtoul(p, (char **)&p, 0);
1377 if (*p != '\0') {
1378 printf("unexpected trailing character '%c'\n", *p);
1379 return 1;
1380 }
1381
1382 if ((*dev = device_find(type, dnum)) == NULL) {
1383 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1384 return 1;
1385 }
1386
1387 if ((*part = mtd_part_info(*dev, pnum)) == NULL) {
1388 printf("no such partition\n");
1389 *dev = NULL;
1390 return 1;
1391 }
1392
1393 *part_num = pnum;
1394
1395 return 0;
1396}
1397
1398/**
1399 * Find and delete partition. For partition id format see find_dev_and_part().
1400 *
1401 * @param id string describing device and partition
1402 * @return 0 on success, 1 otherwise
1403 */
1404static int delete_partition(const char *id)
1405{
1406 u8 pnum;
1407 struct mtd_device *dev;
1408 struct part_info *part;
1409
1410 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1411
Paul Burton39ac3442013-09-04 15:16:57 +01001412 debug("delete_partition: device = %s%d, partition %d = (%s) 0x%08llx@0x%08llx\n",
Stefan Roese68d7d652009-03-19 13:30:36 +01001413 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1414 part->name, part->size, part->offset);
1415
1416 if (part_del(dev, part) != 0)
1417 return 1;
1418
1419 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
Robert P. J. Daya22bf162013-02-04 13:51:10 +00001420 printf("generated mtdparts too long, resetting to null\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001421 return 1;
1422 }
1423 return 0;
1424 }
1425
1426 printf("partition %s not found\n", id);
1427 return 1;
1428}
1429
Ben Gardinerca75b202010-08-31 17:48:03 -04001430#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
1431/**
1432 * Increase the size of the given partition so that it's net size is at least
1433 * as large as the size member and such that the next partition would start on a
1434 * good block if it were adjacent to this partition.
1435 *
1436 * @param mtd the mtd device
1437 * @param part the partition
1438 * @param next_offset pointer to the offset of the next partition after this
1439 * partition's size has been modified (output)
1440 */
1441static void spread_partition(struct mtd_info *mtd, struct part_info *part,
1442 uint64_t *next_offset)
1443{
1444 uint64_t net_size, padding_size = 0;
1445 int truncated;
1446
1447 mtd_get_len_incl_bad(mtd, part->offset, part->size, &net_size,
1448 &truncated);
1449
1450 /*
1451 * Absorb bad blocks immediately following this
1452 * partition also into the partition, such that
1453 * the next partition starts with a good block.
1454 */
1455 if (!truncated) {
1456 mtd_get_len_incl_bad(mtd, part->offset + net_size,
1457 mtd->erasesize, &padding_size, &truncated);
1458 if (truncated)
1459 padding_size = 0;
1460 else
1461 padding_size -= mtd->erasesize;
1462 }
1463
1464 if (truncated) {
1465 printf("truncated partition %s to %lld bytes\n", part->name,
1466 (uint64_t) net_size + padding_size);
1467 }
1468
1469 part->size = net_size + padding_size;
1470 *next_offset = part->offset + part->size;
1471}
1472
1473/**
1474 * Adjust all of the partition sizes, such that all partitions are at least
1475 * as big as their mtdparts environment variable sizes and they each start
1476 * on a good block.
1477 *
1478 * @return 0 on success, 1 otherwise
1479 */
1480static int spread_partitions(void)
1481{
1482 struct list_head *dentry, *pentry;
1483 struct mtd_device *dev;
1484 struct part_info *part;
1485 struct mtd_info *mtd;
1486 int part_num;
1487 uint64_t cur_offs;
1488
1489 list_for_each(dentry, &devices) {
1490 dev = list_entry(dentry, struct mtd_device, link);
1491
1492 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
1493 return 1;
1494
1495 part_num = 0;
1496 cur_offs = 0;
1497 list_for_each(pentry, &dev->parts) {
1498 part = list_entry(pentry, struct part_info, link);
1499
1500 debug("spread_partitions: device = %s%d, partition %d ="
Steve Rae59441ac2016-06-29 13:50:59 -07001501 " (%s) 0x%08llx@0x%08llx\n",
Ben Gardinerca75b202010-08-31 17:48:03 -04001502 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1503 part_num, part->name, part->size,
1504 part->offset);
1505
1506 if (cur_offs > part->offset)
1507 part->offset = cur_offs;
1508
1509 spread_partition(mtd, part, &cur_offs);
1510
1511 part_num++;
1512 }
1513 }
1514
1515 index_partitions();
1516
1517 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
Robert P. J. Daya22bf162013-02-04 13:51:10 +00001518 printf("generated mtdparts too long, resetting to null\n");
Ben Gardinerca75b202010-08-31 17:48:03 -04001519 return 1;
1520 }
1521 return 0;
1522}
1523#endif /* CONFIG_CMD_MTDPARTS_SPREAD */
1524
Stefan Roese68d7d652009-03-19 13:30:36 +01001525/**
Ladislav Michl1c2a2622016-07-12 20:28:24 +02001526 * The mtdparts variable tends to be long. If we need to access it
1527 * before the env is relocated, then we need to use our own stack
1528 * buffer. gd->env_buf will be too small.
1529 *
1530 * @param buf temporary buffer pointer MTDPARTS_MAXLEN long
1531 * @return mtdparts variable string, NULL if not found
1532 */
Simon Glass723806c2017-08-03 12:22:15 -06001533static const char *env_get_mtdparts(char *buf)
Ladislav Michl1c2a2622016-07-12 20:28:24 +02001534{
1535 if (gd->flags & GD_FLG_ENV_READY)
Simon Glass00caae62017-08-03 12:22:12 -06001536 return env_get("mtdparts");
1537 if (env_get_f("mtdparts", buf, MTDPARTS_MAXLEN) != -1)
Ladislav Michl1c2a2622016-07-12 20:28:24 +02001538 return buf;
1539 return NULL;
1540}
1541
1542/**
Stefan Roese68d7d652009-03-19 13:30:36 +01001543 * Accept character string describing mtd partitions and call device_parse()
1544 * for each entry. Add created devices to the global devices list.
1545 *
1546 * @param mtdparts string specifing mtd partitions
1547 * @return 0 on success, 1 otherwise
1548 */
1549static int parse_mtdparts(const char *const mtdparts)
1550{
Ladislav Michl06a040a2016-07-12 20:28:23 +02001551 const char *p;
Stefan Roese68d7d652009-03-19 13:30:36 +01001552 struct mtd_device *dev;
1553 int err = 1;
Joe Hershbergera7eb1d62013-04-08 10:32:50 +00001554 char tmp_parts[MTDPARTS_MAXLEN];
Stefan Roese68d7d652009-03-19 13:30:36 +01001555
Lothar Waßmann1aca4d52017-06-08 14:04:03 +02001556 debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", mtdparts);
Stefan Roese68d7d652009-03-19 13:30:36 +01001557
1558 /* delete all devices and partitions */
1559 if (mtd_devices_init() != 0) {
1560 printf("could not initialise device list\n");
1561 return err;
1562 }
1563
1564 /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
Simon Glass723806c2017-08-03 12:22:15 -06001565 p = env_get_mtdparts(tmp_parts);
Ladislav Michl06a040a2016-07-12 20:28:23 +02001566 if (!p)
1567 p = mtdparts;
1568
Stefan Roese68d7d652009-03-19 13:30:36 +01001569 if (strncmp(p, "mtdparts=", 9) != 0) {
1570 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1571 return err;
1572 }
1573 p += 9;
1574
Ladislav Michl06a040a2016-07-12 20:28:23 +02001575 while (*p != '\0') {
Stefan Roese68d7d652009-03-19 13:30:36 +01001576 err = 1;
1577 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1578 break;
1579
Wolfgang Denke1d29502010-04-28 10:58:10 +02001580 debug("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
Stefan Roese68d7d652009-03-19 13:30:36 +01001581 dev->id->num, dev->id->mtd_id);
1582
1583 /* check if parsed device is already on the list */
1584 if (device_find(dev->id->type, dev->id->num) != NULL) {
1585 printf("device %s%d redefined, please correct mtdparts variable\n",
1586 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1587 break;
1588 }
1589
1590 list_add_tail(&dev->link, &devices);
1591 err = 0;
1592 }
Tom Rini54f17922017-08-14 20:42:27 -04001593 if (err == 1) {
1594 free(dev);
Stefan Roese68d7d652009-03-19 13:30:36 +01001595 device_delall(&devices);
Tom Rini54f17922017-08-14 20:42:27 -04001596 }
Stefan Roese68d7d652009-03-19 13:30:36 +01001597
Ladislav Michl06a040a2016-07-12 20:28:23 +02001598 return err;
Stefan Roese68d7d652009-03-19 13:30:36 +01001599}
1600
1601/**
1602 * Parse provided string describing mtdids mapping (see file header for mtdids
1603 * variable format). Allocate memory for each entry and add all found entries
1604 * to the global mtdids list.
1605 *
1606 * @param ids mapping string
1607 * @return 0 on success, 1 otherwise
1608 */
1609static int parse_mtdids(const char *const ids)
1610{
1611 const char *p = ids;
1612 const char *mtd_id;
1613 int mtd_id_len;
1614 struct mtdids *id;
1615 struct list_head *entry, *n;
1616 struct mtdids *id_tmp;
1617 u8 type, num;
Paul Burton39ac3442013-09-04 15:16:57 +01001618 u64 size;
Stefan Roese68d7d652009-03-19 13:30:36 +01001619 int ret = 1;
1620
Wolfgang Denke1d29502010-04-28 10:58:10 +02001621 debug("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
Stefan Roese68d7d652009-03-19 13:30:36 +01001622
1623 /* clean global mtdids list */
1624 list_for_each_safe(entry, n, &mtdids) {
1625 id_tmp = list_entry(entry, struct mtdids, link);
Wolfgang Denke1d29502010-04-28 10:58:10 +02001626 debug("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
Stefan Roese68d7d652009-03-19 13:30:36 +01001627 list_del(entry);
1628 free(id_tmp);
1629 }
1630 last_ids[0] = '\0';
1631 INIT_LIST_HEAD(&mtdids);
1632
1633 while(p && (*p != '\0')) {
1634
1635 ret = 1;
1636 /* parse 'nor'|'nand'|'onenand'<dev-num> */
1637 if (mtd_id_parse(p, &p, &type, &num) != 0)
1638 break;
1639
1640 if (*p != '=') {
1641 printf("mtdids: incorrect <dev-num>\n");
1642 break;
1643 }
1644 p++;
1645
1646 /* check if requested device exists */
1647 if (mtd_device_validate(type, num, &size) != 0)
1648 return 1;
1649
1650 /* locate <mtd-id> */
1651 mtd_id = p;
1652 if ((p = strchr(mtd_id, ',')) != NULL) {
1653 mtd_id_len = p - mtd_id + 1;
1654 p++;
1655 } else {
1656 mtd_id_len = strlen(mtd_id) + 1;
1657 }
1658 if (mtd_id_len == 0) {
1659 printf("mtdids: no <mtd-id> identifier\n");
1660 break;
1661 }
1662
1663 /* check if this id is already on the list */
1664 int double_entry = 0;
1665 list_for_each(entry, &mtdids) {
1666 id_tmp = list_entry(entry, struct mtdids, link);
1667 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1668 double_entry = 1;
1669 break;
1670 }
1671 }
1672 if (double_entry) {
1673 printf("device id %s%d redefined, please correct mtdids variable\n",
1674 MTD_DEV_TYPE(type), num);
1675 break;
1676 }
1677
1678 /* allocate mtdids structure */
1679 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1680 printf("out of memory\n");
1681 break;
1682 }
1683 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1684 id->num = num;
1685 id->type = type;
1686 id->size = size;
1687 id->mtd_id = (char *)(id + 1);
1688 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1689 id->mtd_id[mtd_id_len - 1] = '\0';
1690 INIT_LIST_HEAD(&id->link);
1691
Paul Burton39ac3442013-09-04 15:16:57 +01001692 debug("+ id %s%d\t%16lld bytes\t%s\n",
Stefan Roese68d7d652009-03-19 13:30:36 +01001693 MTD_DEV_TYPE(id->type), id->num,
1694 id->size, id->mtd_id);
1695
1696 list_add_tail(&id->link, &mtdids);
1697 ret = 0;
1698 }
1699 if (ret == 1) {
1700 /* clean mtdids list and free allocated memory */
1701 list_for_each_safe(entry, n, &mtdids) {
1702 id_tmp = list_entry(entry, struct mtdids, link);
1703 list_del(entry);
1704 free(id_tmp);
1705 }
1706 return 1;
1707 }
1708
1709 return 0;
1710}
1711
Ladislav Michl1c2a2622016-07-12 20:28:24 +02001712
Stefan Roese68d7d652009-03-19 13:30:36 +01001713/**
1714 * Parse and initialize global mtdids mapping and create global
1715 * device/partition list.
1716 *
1717 * @return 0 on success, 1 otherwise
1718 */
1719int mtdparts_init(void)
1720{
1721 static int initialized = 0;
1722 const char *ids, *parts;
1723 const char *current_partition;
1724 int ids_changed;
Tom Rinibc028342017-08-20 20:05:40 -04001725 char tmp_ep[PARTITION_MAXLEN + 1];
Joe Hershbergera7eb1d62013-04-08 10:32:50 +00001726 char tmp_parts[MTDPARTS_MAXLEN];
Stefan Roese68d7d652009-03-19 13:30:36 +01001727
Wolfgang Denke1d29502010-04-28 10:58:10 +02001728 debug("\n---mtdparts_init---\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001729 if (!initialized) {
1730 INIT_LIST_HEAD(&mtdids);
1731 INIT_LIST_HEAD(&devices);
Tom Rini54f17922017-08-14 20:42:27 -04001732 memset(last_ids, 0, sizeof(last_ids));
1733 memset(last_parts, 0, sizeof(last_parts));
1734 memset(last_partition, 0, sizeof(last_partition));
Ladislav Michlaf324432016-07-12 20:28:26 +02001735#if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
1736 board_mtdparts_default(&mtdids_default, &mtdparts_default);
1737#endif
Ladislav Michlf8f744a2016-07-12 20:28:25 +02001738 use_defaults = 1;
Stefan Roese68d7d652009-03-19 13:30:36 +01001739 initialized = 1;
1740 }
1741
1742 /* get variables */
Simon Glass00caae62017-08-03 12:22:12 -06001743 ids = env_get("mtdids");
Simon Glass723806c2017-08-03 12:22:15 -06001744 parts = env_get_mtdparts(tmp_parts);
Simon Glass00caae62017-08-03 12:22:12 -06001745 current_partition = env_get("partition");
Stefan Roese68d7d652009-03-19 13:30:36 +01001746
1747 /* save it for later parsing, cannot rely on current partition pointer
1748 * as 'partition' variable may be updated during init */
Tom Rinibc028342017-08-20 20:05:40 -04001749 memset(tmp_parts, 0, sizeof(tmp_parts));
Tom Rini8b3cec72017-08-26 16:59:24 -04001750 memset(tmp_ep, 0, sizeof(tmp_ep));
Stefan Roese68d7d652009-03-19 13:30:36 +01001751 if (current_partition)
1752 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1753
Wolfgang Denke1d29502010-04-28 10:58:10 +02001754 debug("last_ids : %s\n", last_ids);
1755 debug("env_ids : %s\n", ids);
1756 debug("last_parts: %s\n", last_parts);
1757 debug("env_parts : %s\n\n", parts);
Stefan Roese68d7d652009-03-19 13:30:36 +01001758
Wolfgang Denke1d29502010-04-28 10:58:10 +02001759 debug("last_partition : %s\n", last_partition);
1760 debug("env_partition : %s\n", current_partition);
Stefan Roese68d7d652009-03-19 13:30:36 +01001761
Robert P. J. Day1cc0a9f2016-05-04 04:47:31 -04001762 /* if mtdids variable is empty try to use defaults */
Stefan Roese68d7d652009-03-19 13:30:36 +01001763 if (!ids) {
1764 if (mtdids_default) {
Wolfgang Denke1d29502010-04-28 10:58:10 +02001765 debug("mtdids variable not defined, using default\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001766 ids = mtdids_default;
Simon Glass382bee52017-08-03 12:22:09 -06001767 env_set("mtdids", (char *)ids);
Stefan Roese68d7d652009-03-19 13:30:36 +01001768 } else {
1769 printf("mtdids not defined, no default present\n");
1770 return 1;
1771 }
1772 }
1773 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1774 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1775 return 1;
1776 }
1777
Ladislav Michlf8f744a2016-07-12 20:28:25 +02001778 /* use defaults when mtdparts variable is not defined
1779 * once mtdparts is saved environment, drop use_defaults flag */
1780 if (!parts) {
1781 if (mtdparts_default && use_defaults) {
1782 parts = mtdparts_default;
Simon Glass382bee52017-08-03 12:22:09 -06001783 if (env_set("mtdparts", (char *)parts) == 0)
Ladislav Michlf8f744a2016-07-12 20:28:25 +02001784 use_defaults = 0;
1785 } else
1786 printf("mtdparts variable not set, see 'help mtdparts'\n");
1787 }
Stefan Roese68d7d652009-03-19 13:30:36 +01001788
1789 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1790 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1791 return 1;
1792 }
1793
1794 /* check if we have already parsed those mtdids */
1795 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1796 ids_changed = 0;
1797 } else {
1798 ids_changed = 1;
1799
1800 if (parse_mtdids(ids) != 0) {
1801 mtd_devices_init();
1802 return 1;
1803 }
1804
1805 /* ok it's good, save new ids */
1806 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1807 }
1808
1809 /* parse partitions if either mtdparts or mtdids were updated */
1810 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1811 if (parse_mtdparts(parts) != 0)
1812 return 1;
1813
1814 if (list_empty(&devices)) {
1815 printf("mtdparts_init: no valid partitions\n");
1816 return 1;
1817 }
1818
1819 /* ok it's good, save new parts */
1820 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1821
1822 /* reset first partition from first dev from the list as current */
Stefan Roese76b58832009-05-16 12:04:22 +02001823 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
1824 current_mtd_partnum = 0;
Stefan Roese68d7d652009-03-19 13:30:36 +01001825 current_save();
1826
Wolfgang Denke1d29502010-04-28 10:58:10 +02001827 debug("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n",
Stefan Roese76b58832009-05-16 12:04:22 +02001828 MTD_DEV_TYPE(current_mtd_dev->id->type),
1829 current_mtd_dev->id->num, current_mtd_partnum);
Stefan Roese68d7d652009-03-19 13:30:36 +01001830 }
1831
1832 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1833 if (!parts && (last_parts[0] != '\0'))
1834 return mtd_devices_init();
1835
1836 /* do not process current partition if mtdparts variable is null */
1837 if (!parts)
1838 return 0;
1839
1840 /* is current partition set in environment? if so, use it */
1841 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1842 struct part_info *p;
1843 struct mtd_device *cdev;
1844 u8 pnum;
1845
Wolfgang Denke1d29502010-04-28 10:58:10 +02001846 debug("--- getting current partition: %s\n", tmp_ep);
Stefan Roese68d7d652009-03-19 13:30:36 +01001847
1848 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
Stefan Roese76b58832009-05-16 12:04:22 +02001849 current_mtd_dev = cdev;
1850 current_mtd_partnum = pnum;
Stefan Roese68d7d652009-03-19 13:30:36 +01001851 current_save();
1852 }
Simon Glass00caae62017-08-03 12:22:12 -06001853 } else if (env_get("partition") == NULL) {
Wolfgang Denke1d29502010-04-28 10:58:10 +02001854 debug("no partition variable set, setting...\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01001855 current_save();
1856 }
1857
1858 return 0;
1859}
1860
1861/**
1862 * Return pointer to the partition of a requested number from a requested
1863 * device.
1864 *
1865 * @param dev device that is to be searched for a partition
1866 * @param part_num requested partition number
1867 * @return pointer to the part_info, NULL otherwise
1868 */
1869static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
1870{
1871 struct list_head *entry;
1872 struct part_info *part;
1873 int num;
1874
1875 if (!dev)
1876 return NULL;
1877
Wolfgang Denke1d29502010-04-28 10:58:10 +02001878 debug("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
Stefan Roese68d7d652009-03-19 13:30:36 +01001879 part_num, MTD_DEV_TYPE(dev->id->type),
1880 dev->id->num, dev->id->mtd_id);
1881
1882 if (part_num >= dev->num_parts) {
1883 printf("invalid partition number %d for device %s%d (%s)\n",
1884 part_num, MTD_DEV_TYPE(dev->id->type),
1885 dev->id->num, dev->id->mtd_id);
1886 return NULL;
1887 }
1888
1889 /* locate partition number, return it */
1890 num = 0;
1891 list_for_each(entry, &dev->parts) {
1892 part = list_entry(entry, struct part_info, link);
1893
1894 if (part_num == num++) {
1895 return part;
1896 }
1897 }
1898
1899 return NULL;
1900}
1901
1902/***************************************************/
Bin Menga1875592016-02-05 19:30:11 -08001903/* U-Boot commands */
Stefan Roese68d7d652009-03-19 13:30:36 +01001904/***************************************************/
1905/* command line only */
1906/**
1907 * Routine implementing u-boot chpart command. Sets new current partition based
1908 * on the user supplied partition id. For partition id format see find_dev_and_part().
1909 *
1910 * @param cmdtp command internal data
1911 * @param flag command flag
1912 * @param argc number of arguments supplied to the command
1913 * @param argv arguments list
1914 * @return 0 on success, 1 otherwise
1915 */
Kim Phillips088f1b12012-10-29 13:34:31 +00001916static int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stefan Roese68d7d652009-03-19 13:30:36 +01001917{
1918/* command line only */
1919 struct mtd_device *dev;
1920 struct part_info *part;
1921 u8 pnum;
1922
1923 if (mtdparts_init() !=0)
1924 return 1;
1925
1926 if (argc < 2) {
1927 printf("no partition id specified\n");
1928 return 1;
1929 }
1930
1931 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
1932 return 1;
1933
Stefan Roese76b58832009-05-16 12:04:22 +02001934 current_mtd_dev = dev;
1935 current_mtd_partnum = pnum;
Stefan Roese68d7d652009-03-19 13:30:36 +01001936 current_save();
1937
1938 printf("partition changed to %s%d,%d\n",
1939 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
1940
1941 return 0;
1942}
1943
1944/**
1945 * Routine implementing u-boot mtdparts command. Initialize/update default global
1946 * partition list and process user partition request (list, add, del).
1947 *
1948 * @param cmdtp command internal data
1949 * @param flag command flag
1950 * @param argc number of arguments supplied to the command
1951 * @param argv arguments list
1952 * @return 0 on success, 1 otherwise
1953 */
Kim Phillips088f1b12012-10-29 13:34:31 +00001954static int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc,
1955 char * const argv[])
Stefan Roese68d7d652009-03-19 13:30:36 +01001956{
1957 if (argc == 2) {
1958 if (strcmp(argv[1], "default") == 0) {
Simon Glass382bee52017-08-03 12:22:09 -06001959 env_set("mtdids", NULL);
1960 env_set("mtdparts", NULL);
1961 env_set("partition", NULL);
Ladislav Michlf8f744a2016-07-12 20:28:25 +02001962 use_defaults = 1;
Stefan Roese68d7d652009-03-19 13:30:36 +01001963
1964 mtdparts_init();
1965 return 0;
1966 } else if (strcmp(argv[1], "delall") == 0) {
1967 /* this may be the first run, initialize lists if needed */
1968 mtdparts_init();
1969
Simon Glass382bee52017-08-03 12:22:09 -06001970 env_set("mtdparts", NULL);
Stefan Roese68d7d652009-03-19 13:30:36 +01001971
1972 /* mtd_devices_init() calls current_save() */
1973 return mtd_devices_init();
1974 }
1975 }
1976
1977 /* make sure we are in sync with env variables */
1978 if (mtdparts_init() != 0)
1979 return 1;
1980
1981 if (argc == 1) {
1982 list_partitions();
1983 return 0;
1984 }
1985
1986 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
Ben Gardiner59a50d22010-08-31 17:48:04 -04001987 if (((argc == 5) || (argc == 6)) && (strncmp(argv[1], "add", 3) == 0)) {
Stefan Roese68d7d652009-03-19 13:30:36 +01001988#define PART_ADD_DESC_MAXLEN 64
1989 char tmpbuf[PART_ADD_DESC_MAXLEN];
Ben Gardiner59a50d22010-08-31 17:48:04 -04001990#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
1991 struct mtd_info *mtd;
1992 uint64_t next_offset;
1993#endif
Stefan Roese68d7d652009-03-19 13:30:36 +01001994 u8 type, num, len;
1995 struct mtd_device *dev;
1996 struct mtd_device *dev_tmp;
1997 struct mtdids *id;
1998 struct part_info *p;
1999
2000 if (mtd_id_parse(argv[2], NULL, &type, &num) != 0)
2001 return 1;
2002
2003 if ((id = id_find(type, num)) == NULL) {
2004 printf("no such device %s defined in mtdids variable\n", argv[2]);
2005 return 1;
2006 }
2007
2008 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
2009 len += strlen(argv[3]); /* size@offset */
2010 len += strlen(argv[4]) + 2; /* '(' name ')' */
2011 if (argv[5] && (strlen(argv[5]) == 2))
2012 len += 2; /* 'ro' */
2013
2014 if (len >= PART_ADD_DESC_MAXLEN) {
2015 printf("too long partition description\n");
2016 return 1;
2017 }
2018 sprintf(tmpbuf, "%s:%s(%s)%s",
2019 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
Wolfgang Denke1d29502010-04-28 10:58:10 +02002020 debug("add tmpbuf: %s\n", tmpbuf);
Stefan Roese68d7d652009-03-19 13:30:36 +01002021
2022 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
2023 return 1;
2024
Wolfgang Denke1d29502010-04-28 10:58:10 +02002025 debug("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
Stefan Roese68d7d652009-03-19 13:30:36 +01002026 dev->id->num, dev->id->mtd_id);
2027
Ben Gardiner59a50d22010-08-31 17:48:04 -04002028 p = list_entry(dev->parts.next, struct part_info, link);
2029
2030#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2031 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
2032 return 1;
2033
2034 if (!strcmp(&argv[1][3], ".spread")) {
2035 spread_partition(mtd, p, &next_offset);
Steve Rae59441ac2016-06-29 13:50:59 -07002036 debug("increased %s to %llu bytes\n", p->name, p->size);
Ben Gardiner59a50d22010-08-31 17:48:04 -04002037 }
2038#endif
2039
2040 dev_tmp = device_find(dev->id->type, dev->id->num);
2041 if (dev_tmp == NULL) {
Stefan Roese68d7d652009-03-19 13:30:36 +01002042 device_add(dev);
Ben Gardiner59a50d22010-08-31 17:48:04 -04002043 } else if (part_add(dev_tmp, p) != 0) {
Stefan Roese68d7d652009-03-19 13:30:36 +01002044 /* merge new partition with existing ones*/
Ben Gardiner59a50d22010-08-31 17:48:04 -04002045 device_del(dev);
2046 return 1;
Stefan Roese68d7d652009-03-19 13:30:36 +01002047 }
2048
2049 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
Robert P. J. Daya22bf162013-02-04 13:51:10 +00002050 printf("generated mtdparts too long, resetting to null\n");
Stefan Roese68d7d652009-03-19 13:30:36 +01002051 return 1;
2052 }
2053
2054 return 0;
2055 }
2056
2057 /* mtdparts del part-id */
2058 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
Wolfgang Denke1d29502010-04-28 10:58:10 +02002059 debug("del: part-id = %s\n", argv[2]);
Stefan Roese68d7d652009-03-19 13:30:36 +01002060
2061 return delete_partition(argv[2]);
2062 }
2063
Ben Gardinerca75b202010-08-31 17:48:03 -04002064#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2065 if ((argc == 2) && (strcmp(argv[1], "spread") == 0))
2066 return spread_partitions();
2067#endif /* CONFIG_CMD_MTDPARTS_SPREAD */
2068
Simon Glass4c12eeb2011-12-10 08:44:01 +00002069 return CMD_RET_USAGE;
Stefan Roese68d7d652009-03-19 13:30:36 +01002070}
2071
2072/***************************************************/
2073U_BOOT_CMD(
2074 chpart, 2, 0, do_chpart,
2075 "change active partition",
2076 "part-id\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02002077 " - change active partition (e.g. part-id = nand0,1)"
Stefan Roese68d7d652009-03-19 13:30:36 +01002078);
2079
Kim Phillips088f1b12012-10-29 13:34:31 +00002080#ifdef CONFIG_SYS_LONGHELP
2081static char mtdparts_help_text[] =
Stefan Roese68d7d652009-03-19 13:30:36 +01002082 "\n"
2083 " - list partition table\n"
2084 "mtdparts delall\n"
2085 " - delete all partitions\n"
2086 "mtdparts del part-id\n"
2087 " - delete partition (e.g. part-id = nand0,1)\n"
2088 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2089 " - add partition\n"
Ben Gardiner59a50d22010-08-31 17:48:04 -04002090#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2091 "mtdparts add.spread <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2092 " - add partition, padding size by skipping bad blocks\n"
2093#endif
Stefan Roese68d7d652009-03-19 13:30:36 +01002094 "mtdparts default\n"
Ben Gardinerca75b202010-08-31 17:48:03 -04002095 " - reset partition table to defaults\n"
2096#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2097 "mtdparts spread\n"
2098 " - adjust the sizes of the partitions so they are\n"
2099 " at least as big as the mtdparts variable specifies\n"
2100 " and they each start on a good block\n\n"
2101#else
2102 "\n"
2103#endif /* CONFIG_CMD_MTDPARTS_SPREAD */
Stefan Roese68d7d652009-03-19 13:30:36 +01002104 "-----\n\n"
2105 "this command uses three environment variables:\n\n"
2106 "'partition' - keeps current partition identifier\n\n"
2107 "partition := <part-id>\n"
2108 "<part-id> := <dev-id>,part_num\n\n"
2109 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2110 "mtdids=<idmap>[,<idmap>,...]\n\n"
2111 "<idmap> := <dev-id>=<mtd-id>\n"
2112 "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n"
2113 "<dev-num> := mtd device number, 0...\n"
2114 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2115 "'mtdparts' - partition list\n\n"
2116 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2117 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2118 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2119 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2120 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2121 "<offset> := partition start offset within the device\n"
2122 "<name> := '(' NAME ')'\n"
Kim Phillips088f1b12012-10-29 13:34:31 +00002123 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)";
2124#endif
2125
2126U_BOOT_CMD(
2127 mtdparts, 6, 0, do_mtdparts,
2128 "define flash/nand partitions", mtdparts_help_text
Stefan Roese68d7d652009-03-19 13:30:36 +01002129);
2130/***************************************************/