blob: a021b2d198d1689a153d6d6c89e305539773654c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roeseb1423dd2009-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 Gardinere6064f52010-08-31 17:48:03 -040019 * (C) Copyright 2008
20 * Harald Welte, OpenMoko, Inc., Harald Welte <laforge@openmoko.org>
21 *
Stefan Roeseb1423dd2009-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 Roeseb1423dd2009-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>
Miquel Raynalceb9cae2018-09-06 09:08:45 +020040 * <dev-id> := 'nand'|'nor'|'onenand'|'spi-nand'<dev-num>
Stefan Roeseb1423dd2009-03-19 13:30:36 +010041 * <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 *
Miquel Raynal24de4a52018-09-06 09:08:46 +020047 * mtdparts=[mtdparts=]<mtd-def>[;<mtd-def>...]
Stefan Roeseb1423dd2009-03-19 13:30:36 +010048 *
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
Miquel Raynal24de4a52018-09-06 09:08:46 +020065 * mtdparts=[mtdparts=]edb7312-nor:-
Stefan Roeseb1423dd2009-03-19 13:30:36 +010066 *
67 * 1 NOR Flash with 2 partitions, 1 NAND with one
68 * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
Miquel Raynal24de4a52018-09-06 09:08:46 +020069 * mtdparts=[mtdparts=]edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
Stefan Roeseb1423dd2009-03-19 13:30:36 +010070 *
71 */
72
Stefan Roeseb1423dd2009-03-19 13:30:36 +010073#include <command.h>
Simon Glassdb229612019-08-01 09:46:42 -060074#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060075#include <log.h>
Stefan Roeseb1423dd2009-03-19 13:30:36 +010076#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060077#include <asm/global_data.h>
Ladislav Michl816af312009-03-23 12:06:07 +010078#include <jffs2/load_kernel.h>
Stefan Roeseb1423dd2009-03-19 13:30:36 +010079#include <linux/list.h>
80#include <linux/ctype.h>
Stefan Roese8e66ea72009-05-12 14:31:56 +020081#include <linux/err.h>
82#include <linux/mtd/mtd.h>
Stefan Roeseb1423dd2009-03-19 13:30:36 +010083
84#if defined(CONFIG_CMD_NAND)
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090085#include <linux/mtd/rawnand.h>
Stefan Roeseb1423dd2009-03-19 13:30:36 +010086#include <nand.h>
Stefan Roeseb1423dd2009-03-19 13:30:36 +010087#endif
88
89#if defined(CONFIG_CMD_ONENAND)
Stefan Roeseb1423dd2009-03-19 13:30:36 +010090#include <linux/mtd/onenand.h>
91#include <onenand_uboot.h>
92#endif
93
Joe Hershberger3902c602013-04-08 10:32:50 +000094DECLARE_GLOBAL_DATA_PTR;
95
Stefan Roeseb1423dd2009-03-19 13:30:36 +010096/* special size referring to all the remaining space in a partition */
Paul Burton5f578df2013-09-04 15:16:57 +010097#define SIZE_REMAINING (~0llu)
Stefan Roeseb1423dd2009-03-19 13:30:36 +010098
99/* special offset value, it is used when not provided by user
100 *
101 * this value is used temporarily during parsing, later such offests
102 * are recalculated */
Paul Burton5f578df2013-09-04 15:16:57 +0100103#define OFFSET_NOT_SPECIFIED (~0llu)
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100104
105/* minimum partition size */
106#define MIN_PART_SIZE 4096
107
108/* this flag needs to be set in part_info struct mask_flags
109 * field for read-only partitions */
110#define MTD_WRITEABLE_CMD 1
111
112/* default values for mtdids and mtdparts variables */
Maxime Ripardbf2fbd12017-02-27 18:22:04 +0100113#ifdef CONFIG_MTDIDS_DEFAULT
114#define MTDIDS_DEFAULT CONFIG_MTDIDS_DEFAULT
115#else
Ladislav Michl207350b2016-07-12 20:28:26 +0200116#define MTDIDS_DEFAULT NULL
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100117#endif
Maxime Ripardbf2fbd12017-02-27 18:22:04 +0100118#ifdef CONFIG_MTDPARTS_DEFAULT
119#define MTDPARTS_DEFAULT CONFIG_MTDPARTS_DEFAULT
120#else
Ladislav Michl207350b2016-07-12 20:28:26 +0200121#define MTDPARTS_DEFAULT NULL
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100122#endif
Patrick Delaunayb281fd02022-12-07 09:26:44 +0100123
Ladislav Michl207350b2016-07-12 20:28:26 +0200124#if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
125extern void board_mtdparts_default(const char **mtdids, const char **mtdparts);
126#endif
127static const char *mtdids_default = MTDIDS_DEFAULT;
128static const char *mtdparts_default = MTDPARTS_DEFAULT;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100129
130/* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
131#define MTDIDS_MAXLEN 128
132#define MTDPARTS_MAXLEN 512
133#define PARTITION_MAXLEN 16
Tom Rini4a786c92017-08-14 20:42:27 -0400134static char last_ids[MTDIDS_MAXLEN + 1];
135static char last_parts[MTDPARTS_MAXLEN + 1];
136static char last_partition[PARTITION_MAXLEN + 1];
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100137
138/* low level jffs2 cache cleaning routine */
139extern void jffs2_free_cache(struct part_info *part);
140
141/* mtdids mapping list, filled by parse_ids() */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000142static struct list_head mtdids;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100143
144/* device/partition list, parse_cmdline() parses into here */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000145static struct list_head devices;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100146
147/* current active device and partition number */
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200148struct mtd_device *current_mtd_dev = NULL;
149u8 current_mtd_partnum = 0;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100150
Ladislav Michla91260e2016-07-12 20:28:25 +0200151u8 use_defaults;
152
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100153static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
154
155/* command line only routines */
156static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
157static int device_del(struct mtd_device *dev);
158
159/**
160 * Parses a string into a number. The number stored at ptr is
161 * potentially suffixed with K (for kilobytes, or 1024 bytes),
162 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
163 * 1073741824). If the number is suffixed with K, M, or G, then
164 * the return value is the number multiplied by one kilobyte, one
165 * megabyte, or one gigabyte, respectively.
166 *
167 * @param ptr where parse begins
168 * @param retptr output pointer to next char after parse completes (output)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100169 * Return: resulting unsigned int
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100170 */
Paul Burton5f578df2013-09-04 15:16:57 +0100171static u64 memsize_parse (const char *const ptr, const char **retptr)
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100172{
Paul Burton5f578df2013-09-04 15:16:57 +0100173 u64 ret = simple_strtoull(ptr, (char **)retptr, 0);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100174
175 switch (**retptr) {
176 case 'G':
177 case 'g':
178 ret <<= 10;
Miquel Raynald989b582018-08-16 17:30:21 +0200179 /* Fallthrough */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100180 case 'M':
181 case 'm':
182 ret <<= 10;
Miquel Raynald989b582018-08-16 17:30:21 +0200183 /* Fallthrough */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100184 case 'K':
185 case 'k':
186 ret <<= 10;
187 (*retptr)++;
Miquel Raynald989b582018-08-16 17:30:21 +0200188 /* Fallthrough */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100189 default:
190 break;
191 }
192
193 return ret;
194}
195
196/**
197 * Format string describing supplied size. This routine does the opposite job
198 * to memsize_parse(). Size in bytes is converted to string and if possible
199 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
200 *
201 * Note, that this routine does not check for buffer overflow, it's the caller
202 * who must assure enough space.
203 *
204 * @param buf output buffer
205 * @param size size to be converted to string
206 */
Paul Burton5f578df2013-09-04 15:16:57 +0100207static void memsize_format(char *buf, u64 size)
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100208{
209#define SIZE_GB ((u32)1024*1024*1024)
210#define SIZE_MB ((u32)1024*1024)
211#define SIZE_KB ((u32)1024)
212
213 if ((size % SIZE_GB) == 0)
Paul Burton5f578df2013-09-04 15:16:57 +0100214 sprintf(buf, "%llug", size/SIZE_GB);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100215 else if ((size % SIZE_MB) == 0)
Paul Burton5f578df2013-09-04 15:16:57 +0100216 sprintf(buf, "%llum", size/SIZE_MB);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100217 else if (size % SIZE_KB == 0)
Paul Burton5f578df2013-09-04 15:16:57 +0100218 sprintf(buf, "%lluk", size/SIZE_KB);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100219 else
Paul Burton5f578df2013-09-04 15:16:57 +0100220 sprintf(buf, "%llu", size);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100221}
222
223/**
224 * This routine does global indexing of all partitions. Resulting index for
225 * current partition is saved in 'mtddevnum'. Current partition name in
226 * 'mtddevname'.
227 */
228static void index_partitions(void)
229{
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100230 u16 mtddevnum;
231 struct part_info *part;
232 struct list_head *dentry;
233 struct mtd_device *dev;
234
Wolfgang Denk58daebd2010-04-28 10:58:10 +0200235 debug("--- index partitions ---\n");
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100236
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200237 if (current_mtd_dev) {
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100238 mtddevnum = 0;
239 list_for_each(dentry, &devices) {
240 dev = list_entry(dentry, struct mtd_device, link);
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200241 if (dev == current_mtd_dev) {
242 mtddevnum += current_mtd_partnum;
Simon Glass4d949a22017-08-03 12:22:10 -0600243 env_set_ulong("mtddevnum", mtddevnum);
Tom Rini4a786c92017-08-14 20:42:27 -0400244 debug("=> mtddevnum %d,\n", mtddevnum);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100245 break;
246 }
247 mtddevnum += dev->num_parts;
248 }
249
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200250 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Tom Rini4a786c92017-08-14 20:42:27 -0400251 if (part) {
252 env_set("mtddevname", part->name);
253
254 debug("=> mtddevname %s\n", part->name);
255 } else {
256 env_set("mtddevname", NULL);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100257
Tom Rini4a786c92017-08-14 20:42:27 -0400258 debug("=> mtddevname NULL\n");
259 }
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100260 } else {
Simon Glass6a38e412017-08-03 12:22:09 -0600261 env_set("mtddevnum", NULL);
262 env_set("mtddevname", NULL);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100263
Wolfgang Denk58daebd2010-04-28 10:58:10 +0200264 debug("=> mtddevnum NULL\n=> mtddevname NULL\n");
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100265 }
266}
267
268/**
269 * Save current device and partition in environment variable 'partition'.
270 */
271static void current_save(void)
272{
273 char buf[16];
274
Wolfgang Denk58daebd2010-04-28 10:58:10 +0200275 debug("--- current_save ---\n");
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100276
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200277 if (current_mtd_dev) {
278 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type),
279 current_mtd_dev->id->num, current_mtd_partnum);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100280
Simon Glass6a38e412017-08-03 12:22:09 -0600281 env_set("partition", buf);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100282 strncpy(last_partition, buf, 16);
283
Wolfgang Denk58daebd2010-04-28 10:58:10 +0200284 debug("=> partition %s\n", buf);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100285 } else {
Simon Glass6a38e412017-08-03 12:22:09 -0600286 env_set("partition", NULL);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100287 last_partition[0] = '\0';
288
Wolfgang Denk58daebd2010-04-28 10:58:10 +0200289 debug("=> partition NULL\n");
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100290 }
291 index_partitions();
292}
293
Ben Gardinerf37bf3f2010-08-31 17:48:00 -0400294/**
295 * Produce a mtd_info given a type and num.
296 *
297 * @param type mtd type
298 * @param num mtd number
299 * @param mtd a pointer to an mtd_info instance (output)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100300 * Return: 0 if device is valid, 1 otherwise
Ben Gardinerf37bf3f2010-08-31 17:48:00 -0400301 */
302static int get_mtd_info(u8 type, u8 num, struct mtd_info **mtd)
303{
304 char mtd_dev[16];
305
306 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(type), num);
307 *mtd = get_mtd_device_nm(mtd_dev);
308 if (IS_ERR(*mtd)) {
309 printf("Device %s not found!\n", mtd_dev);
310 return 1;
311 }
Heiko Schocher7bc79e02014-07-14 09:17:11 +0200312 put_mtd_device(*mtd);
Ben Gardinerf37bf3f2010-08-31 17:48:00 -0400313
314 return 0;
315}
316
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100317/**
Stefan Roese8e66ea72009-05-12 14:31:56 +0200318 * Performs sanity check for supplied flash partition.
319 * Table of existing MTD flash devices is searched and partition device
320 * is located. Alignment with the granularity of nand erasesize is verified.
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100321 *
322 * @param id of the parent device
323 * @param part partition to validate
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100324 * Return: 0 if partition is valid, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100325 */
Stefan Roese8e66ea72009-05-12 14:31:56 +0200326static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100327{
Ben Gardinerf37bf3f2010-08-31 17:48:00 -0400328 struct mtd_info *mtd = NULL;
Stefan Roese8e66ea72009-05-12 14:31:56 +0200329 int i, j;
330 ulong start;
Paul Burton5f578df2013-09-04 15:16:57 +0100331 u64 offset, size;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100332
Ben Gardinerf37bf3f2010-08-31 17:48:00 -0400333 if (get_mtd_info(id->type, id->num, &mtd))
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100334 return 1;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100335
Stefan Roese8e66ea72009-05-12 14:31:56 +0200336 part->sector_size = mtd->erasesize;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100337
Stefan Roese8e66ea72009-05-12 14:31:56 +0200338 if (!mtd->numeraseregions) {
339 /*
Miquel Raynalceb9cae2018-09-06 09:08:45 +0200340 * Only one eraseregion (NAND, SPI-NAND, OneNAND or uniform NOR),
Stefan Roese8e66ea72009-05-12 14:31:56 +0200341 * checking for alignment is easy here
342 */
Paul Burton5f578df2013-09-04 15:16:57 +0100343 offset = part->offset;
344 if (do_div(offset, mtd->erasesize)) {
Stefan Roese8e66ea72009-05-12 14:31:56 +0200345 printf("%s%d: partition (%s) start offset"
346 "alignment incorrect\n",
347 MTD_DEV_TYPE(id->type), id->num, part->name);
348 return 1;
349 }
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100350
Paul Burton5f578df2013-09-04 15:16:57 +0100351 size = part->size;
352 if (do_div(size, mtd->erasesize)) {
Stefan Roese8e66ea72009-05-12 14:31:56 +0200353 printf("%s%d: partition (%s) size alignment incorrect\n",
354 MTD_DEV_TYPE(id->type), id->num, part->name);
355 return 1;
356 }
357 } else {
358 /*
359 * Multiple eraseregions (non-uniform NOR),
360 * checking for alignment is more complex here
361 */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100362
Stefan Roese8e66ea72009-05-12 14:31:56 +0200363 /* Check start alignment */
364 for (i = 0; i < mtd->numeraseregions; i++) {
365 start = mtd->eraseregions[i].offset;
366 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
367 if (part->offset == start)
368 goto start_ok;
369 start += mtd->eraseregions[i].erasesize;
370 }
371 }
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100372
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100373 printf("%s%d: partition (%s) start offset alignment incorrect\n",
Stefan Roese8e66ea72009-05-12 14:31:56 +0200374 MTD_DEV_TYPE(id->type), id->num, part->name);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100375 return 1;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100376
Stefan Roese8e66ea72009-05-12 14:31:56 +0200377 start_ok:
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100378
Stefan Roese8e66ea72009-05-12 14:31:56 +0200379 /* Check end/size alignment */
380 for (i = 0; i < mtd->numeraseregions; i++) {
381 start = mtd->eraseregions[i].offset;
382 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
383 if ((part->offset + part->size) == start)
384 goto end_ok;
385 start += mtd->eraseregions[i].erasesize;
386 }
387 }
388 /* Check last sector alignment */
389 if ((part->offset + part->size) == start)
390 goto end_ok;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100391
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100392 printf("%s%d: partition (%s) size alignment incorrect\n",
Stefan Roese8e66ea72009-05-12 14:31:56 +0200393 MTD_DEV_TYPE(id->type), id->num, part->name);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100394 return 1;
Stefan Roese8e66ea72009-05-12 14:31:56 +0200395
396 end_ok:
397 return 0;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100398 }
399
400 return 0;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100401}
402
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100403/**
Otavio Salvador6bd89e42013-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 Roeseb1423dd2009-03-19 13:30:36 +0100407 *
408 * @param id of the parent device
409 * @param part partition to validate
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100410 * Return: 0 if partition is valid, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100411 */
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 Burton5f578df2013-09-04 15:16:57 +0100418 printf("%s: offset %08llx beyond flash size %08llx\n",
Stefan Roeseb1423dd2009-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 Roese8e66ea72009-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 Roeseb1423dd2009-03-19 13:30:36 +0100439}
440
441/**
Robert P. J. Daye3f2c2e2013-08-21 11:39:19 -0400442 * Delete selected partition from the partition list of the specified device.
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100443 *
444 * @param dev device to delete partition from
445 * @param part partition to delete
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100446 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100447 */
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 Roese4fa1f2d2009-05-16 12:04:22 +0200458 if (dev == current_mtd_dev) {
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100459 /* we are modyfing partitions for the current device,
460 * update current */
461 struct part_info *curr_pi;
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200462 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roeseb1423dd2009-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 Roese4fa1f2d2009-05-16 12:04:22 +0200467 current_mtd_partnum = 0;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100468 } else if (part->offset <= curr_pi->offset) {
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200469 current_mtd_partnum--;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100470 }
471 current_save_needed = 1;
472 }
473 }
474
Stefan Roeseb1423dd2009-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 Roeseb1423dd2009-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 Denk58daebd2010-04-28 10:58:10 +0200522 debug("part_sort_add: list empty\n");
Stefan Roeseb1423dd2009-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 Roese4fa1f2d2009-05-16 12:04:22 +0200533 if (dev == current_mtd_dev)
534 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roeseb1423dd2009-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 Roese4fa1f2d2009-05-16 12:04:22 +0200554 current_mtd_partnum++;
Stefan Roeseb1423dd2009-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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100574 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100575 */
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)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100596 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100597 */
598static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
599{
600 struct part_info *part;
Paul Burton5f578df2013-09-04 15:16:57 +0100601 u64 size;
602 u64 offset;
Stefan Roeseb1423dd2009-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 Denk58daebd2010-04-28 10:58:10 +0200615 debug("'-': remaining size assigned\n");
Stefan Roeseb1423dd2009-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 Burton5f578df2013-09-04 15:16:57 +0100621 printf("partition size too small (%llx)\n", size);
Stefan Roeseb1423dd2009-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 */
Kay Potthoff79694d62018-07-17 08:19:39 +0200693 snprintf(part->name, name_len, "0x%08llx@0x%08llx", size, offset);
Stefan Roeseb1423dd2009-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 Burton5f578df2013-09-04 15:16:57 +0100700 debug("+ partition: name %-22s size 0x%08llx offset 0x%08llx mask flags %d\n",
Stefan Roeseb1423dd2009-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 Gardinerf37bf3f2010-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)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100714 * Return: 0 if device is valid, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100715 */
Paul Burton5f578df2013-09-04 15:16:57 +0100716static int mtd_device_validate(u8 type, u8 num, u64 *size)
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100717{
Ben Gardinerf37bf3f2010-08-31 17:48:00 -0400718 struct mtd_info *mtd = NULL;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100719
Ben Gardinerf37bf3f2010-08-31 17:48:00 -0400720 if (get_mtd_info(type, num, &mtd))
Stefan Roese8e66ea72009-05-12 14:31:56 +0200721 return 1;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100722
Stefan Roese8e66ea72009-05-12 14:31:56 +0200723 *size = mtd->size;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100724
Stefan Roese8e66ea72009-05-12 14:31:56 +0200725 return 0;
Stefan Roeseb1423dd2009-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 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100732 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100733 */
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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100756 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100757 */
758static int device_del(struct mtd_device *dev)
759{
760 part_delall(&dev->parts);
761 list_del(&dev->link);
762 free(dev);
763
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200764 if (dev == current_mtd_dev) {
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100765 /* we just deleted current device */
766 if (list_empty(&devices)) {
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200767 current_mtd_dev = NULL;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100768 } else {
769 /* reset first partition from first dev from the
770 * devices list as current */
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200771 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
772 current_mtd_partnum = 0;
Stefan Roeseb1423dd2009-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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100788 * Return: NULL if requested device does not exist
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100789 */
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100790struct mtd_device *device_find(u8 type, u8 num)
Stefan Roeseb1423dd2009-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 Roese4fa1f2d2009-05-16 12:04:22 +0200815 current_mtd_dev = dev;
816 current_mtd_partnum = 0;
Stefan Roeseb1423dd2009-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)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100835 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100836 */
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 Denke967dc12011-10-05 22:48:12 +0200844 const char *p;
Wolfgang Denke967dc12011-10-05 22:48:12 +0200845 const char *pend;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100846 LIST_HEAD(tmp_list);
847 struct list_head *entry, *n;
848 u16 num_parts;
Paul Burton5f578df2013-09-04 15:16:57 +0100849 u64 offset;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100850 int err = 1;
851
Wolfgang Denk58daebd2010-04-28 10:58:10 +0200852 debug("===device_parse===\n");
Wolfgang Denkdc6e65c2010-04-28 10:53:47 +0200853
854 assert(retdev);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100855 *retdev = NULL;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100856
Wolfgang Denkdc6e65c2010-04-28 10:53:47 +0200857 if (ret)
858 *ret = NULL;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100859
860 /* fetch <mtd-id> */
Wolfgang Denkdc6e65c2010-04-28 10:53:47 +0200861 mtd_id = p = mtd_dev;
Stefan Roeseb1423dd2009-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 Denke967dc12011-10-05 22:48:12 +0200875 pend = strchr(p, ';');
Wolfgang Denk58daebd2010-04-28 10:58:10 +0200876 debug("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100877 id->type, MTD_DEV_TYPE(id->type),
878 id->num, id->mtd_id);
Vasili Galka38c3fff2014-08-26 13:45:48 +0300879 debug("parsing partitions %.*s\n", (int)(pend ? pend - p : strlen(p)), p);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100880
Stefan Roeseb1423dd2009-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 Denk58daebd2010-04-28 10:58:10 +0200918 debug("\ntotal partitions: %d\n", num_parts);
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100919
920 /* check for next device presence */
921 if (p) {
922 if (*p == ';') {
Wolfgang Denkdc6e65c2010-04-28 10:53:47 +0200923 if (ret)
924 *ret = ++p;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100925 } else if (*p == '\0') {
Wolfgang Denkdc6e65c2010-04-28 10:53:47 +0200926 if (ret)
927 *ret = p;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100928 } else {
929 printf("unexpected character '%c' at the end of device\n", *p);
Wolfgang Denkdc6e65c2010-04-28 10:53:47 +0200930 if (ret)
931 *ret = NULL;
Stefan Roeseb1423dd2009-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 Denk58daebd2010-04-28 10:58:10 +0200959 debug("===\n\n");
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100960 return 0;
961}
962
963/**
964 * Initialize global device list.
965 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100966 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100967 */
968static int mtd_devices_init(void)
969{
970 last_parts[0] = '\0';
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200971 current_mtd_dev = NULL;
Stefan Roeseb1423dd2009-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 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100980 * Return: pointer to the id if it exists, NULL otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100981 */
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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001004 * Return: pointer to the id if it exists, NULL otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001005 */
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 Denk58daebd2010-04-28 10:58:10 +02001011 debug("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
Stefan Roeseb1423dd2009-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 Galka38c3fff2014-08-26 13:45:48 +03001017 debug("entry: '%s' (len = %zu)\n",
Stefan Roeseb1423dd2009-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/**
Miquel Raynalceb9cae2018-09-06 09:08:45 +02001030 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'|'spi-nand'<dev-num>,
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001031 * 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)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001037 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001038 */
Kim Phillipsdc00a682012-10-29 13:34:31 +00001039int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type,
1040 u8 *dev_num)
Stefan Roeseb1423dd2009-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;
Miquel Raynalceb9cae2018-09-06 09:08:45 +02001054 } else if (strncmp(p, "spi-nand", 8) == 0) {
1055 *dev_type = MTD_DEV_TYPE_SPINAND;
1056 p += 8;
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001057 } else {
1058 printf("incorrect device type in %s\n", id);
1059 return 1;
1060 }
1061
1062 if (!isdigit(*p)) {
1063 printf("incorrect device number in %s\n", id);
1064 return 1;
1065 }
1066
1067 *dev_num = simple_strtoul(p, (char **)&p, 0);
1068 if (ret_id)
1069 *ret_id = p;
1070 return 0;
1071}
1072
1073/**
1074 * Process all devices and generate corresponding mtdparts string describing
1075 * all partitions on all devices.
1076 *
1077 * @param buf output buffer holding generated mtdparts string (output)
1078 * @param buflen buffer size
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001079 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001080 */
1081static int generate_mtdparts(char *buf, u32 buflen)
1082{
1083 struct list_head *pentry, *dentry;
1084 struct mtd_device *dev;
1085 struct part_info *part, *prev_part;
1086 char *p = buf;
1087 char tmpbuf[32];
Paul Burton5f578df2013-09-04 15:16:57 +01001088 u64 size, offset;
1089 u32 len, part_cnt;
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001090 u32 maxlen = buflen - 1;
1091
Wolfgang Denk58daebd2010-04-28 10:58:10 +02001092 debug("--- generate_mtdparts ---\n");
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001093
1094 if (list_empty(&devices)) {
1095 buf[0] = '\0';
1096 return 0;
1097 }
1098
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001099 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
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001129 /* add offset only when there is a gap between
1130 * partitions */
1131 if ((!prev_part && (offset != 0)) ||
1132 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1133
1134 memsize_format(tmpbuf, offset);
1135 len = strlen(tmpbuf) + 1;
1136 if (len > maxlen)
1137 goto cleanup;
1138 *(p++) = '@';
1139 memcpy(p, tmpbuf, len - 1);
1140 p += len - 1;
1141 maxlen -= len;
1142 }
1143
1144 /* copy name only if user supplied */
1145 if(!part->auto_name) {
1146 len = strlen(part->name) + 2;
1147 if (len > maxlen)
1148 goto cleanup;
1149
1150 *(p++) = '(';
1151 memcpy(p, part->name, len - 2);
1152 p += len - 2;
1153 *(p++) = ')';
1154 maxlen -= len;
1155 }
1156
1157 /* ro mask flag */
1158 if (part->mask_flags && MTD_WRITEABLE_CMD) {
1159 len = 2;
1160 if (len > maxlen)
1161 goto cleanup;
1162 *(p++) = 'r';
1163 *(p++) = 'o';
1164 maxlen -= 2;
1165 }
1166
1167 /* print ',' separator if there are other partitions
1168 * following */
1169 if (dev->num_parts > part_cnt) {
1170 if (1 > maxlen)
1171 goto cleanup;
1172 *(p++) = ',';
1173 maxlen--;
1174 }
1175 prev_part = part;
1176 }
1177 /* print ';' separator if there are other devices following */
1178 if (dentry->next != &devices) {
1179 if (1 > maxlen)
1180 goto cleanup;
1181 *(p++) = ';';
1182 maxlen--;
1183 }
1184 }
1185
1186 /* we still have at least one char left, as we decremented maxlen at
1187 * the begining */
1188 *p = '\0';
1189
1190 return 0;
1191
1192cleanup:
1193 last_parts[0] = '\0';
1194 return 1;
1195}
1196
1197/**
1198 * Call generate_mtdparts to process all devices and generate corresponding
1199 * mtdparts string, save it in mtdparts environment variable.
1200 *
1201 * @param buf output buffer holding generated mtdparts string (output)
1202 * @param buflen buffer size
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001203 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001204 */
1205static int generate_mtdparts_save(char *buf, u32 buflen)
1206{
1207 int ret;
1208
1209 ret = generate_mtdparts(buf, buflen);
1210
1211 if ((buf[0] != '\0') && (ret == 0))
Simon Glass6a38e412017-08-03 12:22:09 -06001212 env_set("mtdparts", buf);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001213 else
Simon Glass6a38e412017-08-03 12:22:09 -06001214 env_set("mtdparts", NULL);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001215
1216 return ret;
1217}
1218
Ben Gardinera9b57292010-08-31 17:48:02 -04001219#if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001220/**
Ben Gardinera9b57292010-08-31 17:48:02 -04001221 * Get the net size (w/o bad blocks) of the given partition.
1222 *
1223 * @param mtd the mtd info
1224 * @param part the partition
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001225 * Return: the calculated net size of this partition
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001226 */
Ben Gardinera9b57292010-08-31 17:48:02 -04001227static uint64_t net_part_size(struct mtd_info *mtd, struct part_info *part)
1228{
Scott Wood10390ce2010-09-09 15:40:03 -05001229 uint64_t i, net_size = 0;
1230
Alexander Dahl6cae4f42019-10-30 16:53:55 +01001231 if (!mtd->_block_isbad)
Ben Gardinera9b57292010-08-31 17:48:02 -04001232 return part->size;
1233
Ben Gardinera9b57292010-08-31 17:48:02 -04001234 for (i = 0; i < part->size; i += mtd->erasesize) {
Alexander Dahl6cae4f42019-10-30 16:53:55 +01001235 if (!mtd->_block_isbad(mtd, part->offset + i))
Ben Gardinera9b57292010-08-31 17:48:02 -04001236 net_size += mtd->erasesize;
1237 }
Scott Wood10390ce2010-09-09 15:40:03 -05001238
Ben Gardinera9b57292010-08-31 17:48:02 -04001239 return net_size;
1240}
1241#endif
1242
1243static void print_partition_table(void)
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001244{
1245 struct list_head *dentry, *pentry;
1246 struct part_info *part;
1247 struct mtd_device *dev;
1248 int part_num;
1249
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001250 list_for_each(dentry, &devices) {
1251 dev = list_entry(dentry, struct mtd_device, link);
Ben Gardinera9b57292010-08-31 17:48:02 -04001252 /* list partitions for given device */
1253 part_num = 0;
1254#if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
1255 struct mtd_info *mtd;
1256
1257 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
1258 return;
1259
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001260 printf("\ndevice %s%d <%s>, # parts = %d\n",
1261 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1262 dev->id->mtd_id, dev->num_parts);
Ben Gardinera9b57292010-08-31 17:48:02 -04001263 printf(" #: name\t\tsize\t\tnet size\toffset\t\tmask_flags\n");
1264
1265 list_for_each(pentry, &dev->parts) {
1266 u32 net_size;
1267 char *size_note;
1268
1269 part = list_entry(pentry, struct part_info, link);
1270 net_size = net_part_size(mtd, part);
1271 size_note = part->size == net_size ? " " : " (!)";
Alexander Dahl6cae4f42019-10-30 16:53:55 +01001272 printf("%2d: %-20s0x%08llx\t0x%08x%s\t0x%08llx\t%d\n",
Ben Gardinera9b57292010-08-31 17:48:02 -04001273 part_num, part->name, part->size,
1274 net_size, size_note, part->offset,
1275 part->mask_flags);
1276#else /* !defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
1277 printf("\ndevice %s%d <%s>, # parts = %d\n",
1278 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1279 dev->id->mtd_id, dev->num_parts);
David Brownelld8a6b812009-04-16 19:55:48 -07001280 printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001281
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001282 list_for_each(pentry, &dev->parts) {
1283 part = list_entry(pentry, struct part_info, link);
Paul Burton5f578df2013-09-04 15:16:57 +01001284 printf("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001285 part_num, part->name, part->size,
1286 part->offset, part->mask_flags);
Ben Gardinera9b57292010-08-31 17:48:02 -04001287#endif /* defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001288 part_num++;
1289 }
1290 }
Ben Gardinera9b57292010-08-31 17:48:02 -04001291
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001292 if (list_empty(&devices))
1293 printf("no partitions defined\n");
Ben Gardinera9b57292010-08-31 17:48:02 -04001294}
1295
1296/**
1297 * Format and print out a partition list for each device from global device
1298 * list.
1299 */
1300static void list_partitions(void)
1301{
1302 struct part_info *part;
1303
1304 debug("\n---list_partitions---\n");
1305 print_partition_table();
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001306
Stefan Roese4fa1f2d2009-05-16 12:04:22 +02001307 /* current_mtd_dev is not NULL only when we have non empty device list */
1308 if (current_mtd_dev) {
1309 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001310 if (part) {
Paul Burton5f578df2013-09-04 15:16:57 +01001311 printf("\nactive partition: %s%d,%d - (%s) 0x%08llx @ 0x%08llx\n",
Stefan Roese4fa1f2d2009-05-16 12:04:22 +02001312 MTD_DEV_TYPE(current_mtd_dev->id->type),
1313 current_mtd_dev->id->num, current_mtd_partnum,
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001314 part->name, part->size, part->offset);
1315 } else {
1316 printf("could not get current partition info\n\n");
1317 }
1318 }
1319
1320 printf("\ndefaults:\n");
Wolfgang Denk21c0a542009-05-17 16:01:54 +02001321 printf("mtdids : %s\n",
1322 mtdids_default ? mtdids_default : "none");
Anatolij Gustschin99695c22010-02-24 00:29:44 +01001323 /*
1324 * Using printf() here results in printbuffer overflow
1325 * if default mtdparts string is greater than console
1326 * printbuffer. Use puts() to prevent system crashes.
1327 */
1328 puts("mtdparts: ");
1329 puts(mtdparts_default ? mtdparts_default : "none");
1330 puts("\n");
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001331}
1332
1333/**
1334 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1335 * corresponding device and verify partition number.
1336 *
1337 * @param id string describing device and partition or partition name
1338 * @param dev pointer to the requested device (output)
1339 * @param part_num verified partition number (output)
1340 * @param part pointer to requested partition (output)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001341 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001342 */
1343int find_dev_and_part(const char *id, struct mtd_device **dev,
1344 u8 *part_num, struct part_info **part)
1345{
1346 struct list_head *dentry, *pentry;
1347 u8 type, dnum, pnum;
1348 const char *p;
1349
Wolfgang Denk58daebd2010-04-28 10:58:10 +02001350 debug("--- find_dev_and_part ---\nid = %s\n", id);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001351
1352 list_for_each(dentry, &devices) {
1353 *part_num = 0;
1354 *dev = list_entry(dentry, struct mtd_device, link);
1355 list_for_each(pentry, &(*dev)->parts) {
1356 *part = list_entry(pentry, struct part_info, link);
1357 if (strcmp((*part)->name, id) == 0)
1358 return 0;
1359 (*part_num)++;
1360 }
1361 }
1362
1363 p = id;
1364 *dev = NULL;
1365 *part = NULL;
1366 *part_num = 0;
1367
1368 if (mtd_id_parse(p, &p, &type, &dnum) != 0)
1369 return 1;
1370
1371 if ((*p++ != ',') || (*p == '\0')) {
1372 printf("no partition number specified\n");
1373 return 1;
1374 }
1375 pnum = simple_strtoul(p, (char **)&p, 0);
1376 if (*p != '\0') {
1377 printf("unexpected trailing character '%c'\n", *p);
1378 return 1;
1379 }
1380
1381 if ((*dev = device_find(type, dnum)) == NULL) {
1382 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1383 return 1;
1384 }
1385
1386 if ((*part = mtd_part_info(*dev, pnum)) == NULL) {
1387 printf("no such partition\n");
1388 *dev = NULL;
1389 return 1;
1390 }
1391
1392 *part_num = pnum;
1393
1394 return 0;
1395}
1396
1397/**
1398 * Find and delete partition. For partition id format see find_dev_and_part().
1399 *
1400 * @param id string describing device and partition
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001401 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001402 */
1403static int delete_partition(const char *id)
1404{
1405 u8 pnum;
1406 struct mtd_device *dev;
1407 struct part_info *part;
1408
1409 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1410
Paul Burton5f578df2013-09-04 15:16:57 +01001411 debug("delete_partition: device = %s%d, partition %d = (%s) 0x%08llx@0x%08llx\n",
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001412 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1413 part->name, part->size, part->offset);
1414
1415 if (part_del(dev, part) != 0)
1416 return 1;
1417
1418 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
Robert P. J. Day02ce7162013-02-04 13:51:10 +00001419 printf("generated mtdparts too long, resetting to null\n");
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001420 return 1;
1421 }
1422 return 0;
1423 }
1424
1425 printf("partition %s not found\n", id);
1426 return 1;
1427}
1428
Ben Gardinere6064f52010-08-31 17:48:03 -04001429#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
1430/**
1431 * Increase the size of the given partition so that it's net size is at least
1432 * as large as the size member and such that the next partition would start on a
1433 * good block if it were adjacent to this partition.
1434 *
1435 * @param mtd the mtd device
1436 * @param part the partition
1437 * @param next_offset pointer to the offset of the next partition after this
1438 * partition's size has been modified (output)
1439 */
1440static void spread_partition(struct mtd_info *mtd, struct part_info *part,
1441 uint64_t *next_offset)
1442{
1443 uint64_t net_size, padding_size = 0;
1444 int truncated;
1445
1446 mtd_get_len_incl_bad(mtd, part->offset, part->size, &net_size,
1447 &truncated);
1448
1449 /*
1450 * Absorb bad blocks immediately following this
1451 * partition also into the partition, such that
1452 * the next partition starts with a good block.
1453 */
1454 if (!truncated) {
1455 mtd_get_len_incl_bad(mtd, part->offset + net_size,
1456 mtd->erasesize, &padding_size, &truncated);
1457 if (truncated)
1458 padding_size = 0;
1459 else
1460 padding_size -= mtd->erasesize;
1461 }
1462
1463 if (truncated) {
1464 printf("truncated partition %s to %lld bytes\n", part->name,
1465 (uint64_t) net_size + padding_size);
1466 }
1467
1468 part->size = net_size + padding_size;
1469 *next_offset = part->offset + part->size;
1470}
1471
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001472/**
Ben Gardinere6064f52010-08-31 17:48:03 -04001473 * Adjust all of the partition sizes, such that all partitions are at least
1474 * as big as their mtdparts environment variable sizes and they each start
1475 * on a good block.
1476 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001477 * Return: 0 on success, 1 otherwise
Ben Gardinere6064f52010-08-31 17:48:03 -04001478 */
1479static int spread_partitions(void)
1480{
1481 struct list_head *dentry, *pentry;
1482 struct mtd_device *dev;
1483 struct part_info *part;
1484 struct mtd_info *mtd;
1485 int part_num;
1486 uint64_t cur_offs;
1487
1488 list_for_each(dentry, &devices) {
1489 dev = list_entry(dentry, struct mtd_device, link);
1490
1491 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
1492 return 1;
1493
1494 part_num = 0;
1495 cur_offs = 0;
1496 list_for_each(pentry, &dev->parts) {
1497 part = list_entry(pentry, struct part_info, link);
1498
1499 debug("spread_partitions: device = %s%d, partition %d ="
Steve Rae6eb14ff2016-06-29 13:50:59 -07001500 " (%s) 0x%08llx@0x%08llx\n",
Ben Gardinere6064f52010-08-31 17:48:03 -04001501 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1502 part_num, part->name, part->size,
1503 part->offset);
1504
1505 if (cur_offs > part->offset)
1506 part->offset = cur_offs;
1507
1508 spread_partition(mtd, part, &cur_offs);
1509
1510 part_num++;
1511 }
1512 }
1513
1514 index_partitions();
1515
1516 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
Robert P. J. Day02ce7162013-02-04 13:51:10 +00001517 printf("generated mtdparts too long, resetting to null\n");
Ben Gardinere6064f52010-08-31 17:48:03 -04001518 return 1;
1519 }
1520 return 0;
1521}
1522#endif /* CONFIG_CMD_MTDPARTS_SPREAD */
1523
1524/**
Ladislav Michl02c2d6a2016-07-12 20:28:24 +02001525 * The mtdparts variable tends to be long. If we need to access it
1526 * before the env is relocated, then we need to use our own stack
1527 * buffer. gd->env_buf will be too small.
1528 *
1529 * @param buf temporary buffer pointer MTDPARTS_MAXLEN long
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001530 * Return: mtdparts variable string, NULL if not found
Ladislav Michl02c2d6a2016-07-12 20:28:24 +02001531 */
Simon Glassda1a1342017-08-03 12:22:15 -06001532static const char *env_get_mtdparts(char *buf)
Ladislav Michl02c2d6a2016-07-12 20:28:24 +02001533{
1534 if (gd->flags & GD_FLG_ENV_READY)
Simon Glass64b723f2017-08-03 12:22:12 -06001535 return env_get("mtdparts");
1536 if (env_get_f("mtdparts", buf, MTDPARTS_MAXLEN) != -1)
Ladislav Michl02c2d6a2016-07-12 20:28:24 +02001537 return buf;
1538 return NULL;
1539}
1540
1541/**
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001542 * Accept character string describing mtd partitions and call device_parse()
1543 * for each entry. Add created devices to the global devices list.
1544 *
1545 * @param mtdparts string specifing mtd partitions
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001546 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001547 */
1548static int parse_mtdparts(const char *const mtdparts)
1549{
Ladislav Michl760042a2016-07-12 20:28:23 +02001550 const char *p;
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001551 struct mtd_device *dev;
1552 int err = 1;
Joe Hershberger3902c602013-04-08 10:32:50 +00001553 char tmp_parts[MTDPARTS_MAXLEN];
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001554
Lothar Waßmann037166d2017-06-08 14:04:03 +02001555 debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", mtdparts);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001556
1557 /* delete all devices and partitions */
1558 if (mtd_devices_init() != 0) {
1559 printf("could not initialise device list\n");
1560 return err;
1561 }
1562
1563 /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
Simon Glassda1a1342017-08-03 12:22:15 -06001564 p = env_get_mtdparts(tmp_parts);
Ladislav Michl760042a2016-07-12 20:28:23 +02001565 if (!p)
1566 p = mtdparts;
1567
Miquel Raynal24de4a52018-09-06 09:08:46 +02001568 /* Skip the useless prefix, if any */
1569 if (strncmp(p, "mtdparts=", 9) == 0)
1570 p += 9;
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001571
Ladislav Michl760042a2016-07-12 20:28:23 +02001572 while (*p != '\0') {
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001573 err = 1;
1574 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1575 break;
1576
Wolfgang Denk58daebd2010-04-28 10:58:10 +02001577 debug("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001578 dev->id->num, dev->id->mtd_id);
1579
1580 /* check if parsed device is already on the list */
1581 if (device_find(dev->id->type, dev->id->num) != NULL) {
1582 printf("device %s%d redefined, please correct mtdparts variable\n",
1583 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1584 break;
1585 }
1586
1587 list_add_tail(&dev->link, &devices);
1588 err = 0;
1589 }
Tom Rini4a786c92017-08-14 20:42:27 -04001590 if (err == 1) {
1591 free(dev);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001592 device_delall(&devices);
Tom Rini4a786c92017-08-14 20:42:27 -04001593 }
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001594
Ladislav Michl760042a2016-07-12 20:28:23 +02001595 return err;
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001596}
1597
1598/**
1599 * Parse provided string describing mtdids mapping (see file header for mtdids
1600 * variable format). Allocate memory for each entry and add all found entries
1601 * to the global mtdids list.
1602 *
1603 * @param ids mapping string
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001604 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001605 */
1606static int parse_mtdids(const char *const ids)
1607{
1608 const char *p = ids;
1609 const char *mtd_id;
1610 int mtd_id_len;
1611 struct mtdids *id;
1612 struct list_head *entry, *n;
1613 struct mtdids *id_tmp;
1614 u8 type, num;
Paul Burton5f578df2013-09-04 15:16:57 +01001615 u64 size;
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001616 int ret = 1;
1617
Wolfgang Denk58daebd2010-04-28 10:58:10 +02001618 debug("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001619
1620 /* clean global mtdids list */
1621 list_for_each_safe(entry, n, &mtdids) {
1622 id_tmp = list_entry(entry, struct mtdids, link);
Wolfgang Denk58daebd2010-04-28 10:58:10 +02001623 debug("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001624 list_del(entry);
1625 free(id_tmp);
1626 }
1627 last_ids[0] = '\0';
1628 INIT_LIST_HEAD(&mtdids);
1629
1630 while(p && (*p != '\0')) {
1631
1632 ret = 1;
Miquel Raynalceb9cae2018-09-06 09:08:45 +02001633 /* parse 'nor'|'nand'|'onenand'|'spi-nand'<dev-num> */
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001634 if (mtd_id_parse(p, &p, &type, &num) != 0)
1635 break;
1636
1637 if (*p != '=') {
1638 printf("mtdids: incorrect <dev-num>\n");
1639 break;
1640 }
1641 p++;
1642
1643 /* check if requested device exists */
1644 if (mtd_device_validate(type, num, &size) != 0)
1645 return 1;
1646
1647 /* locate <mtd-id> */
1648 mtd_id = p;
1649 if ((p = strchr(mtd_id, ',')) != NULL) {
1650 mtd_id_len = p - mtd_id + 1;
1651 p++;
1652 } else {
1653 mtd_id_len = strlen(mtd_id) + 1;
1654 }
1655 if (mtd_id_len == 0) {
1656 printf("mtdids: no <mtd-id> identifier\n");
1657 break;
1658 }
1659
1660 /* check if this id is already on the list */
1661 int double_entry = 0;
1662 list_for_each(entry, &mtdids) {
1663 id_tmp = list_entry(entry, struct mtdids, link);
1664 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1665 double_entry = 1;
1666 break;
1667 }
1668 }
1669 if (double_entry) {
1670 printf("device id %s%d redefined, please correct mtdids variable\n",
1671 MTD_DEV_TYPE(type), num);
1672 break;
1673 }
1674
1675 /* allocate mtdids structure */
1676 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1677 printf("out of memory\n");
1678 break;
1679 }
1680 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1681 id->num = num;
1682 id->type = type;
1683 id->size = size;
1684 id->mtd_id = (char *)(id + 1);
1685 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1686 id->mtd_id[mtd_id_len - 1] = '\0';
1687 INIT_LIST_HEAD(&id->link);
1688
Paul Burton5f578df2013-09-04 15:16:57 +01001689 debug("+ id %s%d\t%16lld bytes\t%s\n",
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001690 MTD_DEV_TYPE(id->type), id->num,
1691 id->size, id->mtd_id);
1692
1693 list_add_tail(&id->link, &mtdids);
1694 ret = 0;
1695 }
1696 if (ret == 1) {
1697 /* clean mtdids list and free allocated memory */
1698 list_for_each_safe(entry, n, &mtdids) {
1699 id_tmp = list_entry(entry, struct mtdids, link);
1700 list_del(entry);
1701 free(id_tmp);
1702 }
1703 return 1;
1704 }
1705
1706 return 0;
1707}
1708
1709/**
1710 * Parse and initialize global mtdids mapping and create global
1711 * device/partition list.
1712 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001713 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001714 */
1715int mtdparts_init(void)
1716{
1717 static int initialized = 0;
1718 const char *ids, *parts;
1719 const char *current_partition;
1720 int ids_changed;
Tom Rini11d267f2017-08-20 20:05:40 -04001721 char tmp_ep[PARTITION_MAXLEN + 1];
Joe Hershberger3902c602013-04-08 10:32:50 +00001722 char tmp_parts[MTDPARTS_MAXLEN];
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001723
Wolfgang Denk58daebd2010-04-28 10:58:10 +02001724 debug("\n---mtdparts_init---\n");
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001725 if (!initialized) {
1726 INIT_LIST_HEAD(&mtdids);
1727 INIT_LIST_HEAD(&devices);
Tom Rini4a786c92017-08-14 20:42:27 -04001728 memset(last_ids, 0, sizeof(last_ids));
1729 memset(last_parts, 0, sizeof(last_parts));
1730 memset(last_partition, 0, sizeof(last_partition));
Ladislav Michl207350b2016-07-12 20:28:26 +02001731#if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
1732 board_mtdparts_default(&mtdids_default, &mtdparts_default);
1733#endif
Ladislav Michla91260e2016-07-12 20:28:25 +02001734 use_defaults = 1;
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001735 initialized = 1;
1736 }
1737
1738 /* get variables */
Simon Glass64b723f2017-08-03 12:22:12 -06001739 ids = env_get("mtdids");
Simon Glassda1a1342017-08-03 12:22:15 -06001740 parts = env_get_mtdparts(tmp_parts);
Simon Glass64b723f2017-08-03 12:22:12 -06001741 current_partition = env_get("partition");
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001742
1743 /* save it for later parsing, cannot rely on current partition pointer
1744 * as 'partition' variable may be updated during init */
Tom Rini11d267f2017-08-20 20:05:40 -04001745 memset(tmp_parts, 0, sizeof(tmp_parts));
Tom Rinifd0dd1c2017-08-26 16:59:24 -04001746 memset(tmp_ep, 0, sizeof(tmp_ep));
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001747 if (current_partition)
1748 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1749
Wolfgang Denk58daebd2010-04-28 10:58:10 +02001750 debug("last_ids : %s\n", last_ids);
1751 debug("env_ids : %s\n", ids);
1752 debug("last_parts: %s\n", last_parts);
1753 debug("env_parts : %s\n\n", parts);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001754
Wolfgang Denk58daebd2010-04-28 10:58:10 +02001755 debug("last_partition : %s\n", last_partition);
1756 debug("env_partition : %s\n", current_partition);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001757
Robert P. J. Day8c60f922016-05-04 04:47:31 -04001758 /* if mtdids variable is empty try to use defaults */
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001759 if (!ids) {
1760 if (mtdids_default) {
Wolfgang Denk58daebd2010-04-28 10:58:10 +02001761 debug("mtdids variable not defined, using default\n");
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001762 ids = mtdids_default;
Simon Glass6a38e412017-08-03 12:22:09 -06001763 env_set("mtdids", (char *)ids);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001764 } else {
1765 printf("mtdids not defined, no default present\n");
1766 return 1;
1767 }
1768 }
1769 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1770 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1771 return 1;
1772 }
1773
Ladislav Michla91260e2016-07-12 20:28:25 +02001774 /* use defaults when mtdparts variable is not defined
1775 * once mtdparts is saved environment, drop use_defaults flag */
1776 if (!parts) {
1777 if (mtdparts_default && use_defaults) {
1778 parts = mtdparts_default;
Simon Glass6a38e412017-08-03 12:22:09 -06001779 if (env_set("mtdparts", (char *)parts) == 0)
Ladislav Michla91260e2016-07-12 20:28:25 +02001780 use_defaults = 0;
1781 } else
1782 printf("mtdparts variable not set, see 'help mtdparts'\n");
1783 }
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001784
1785 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1786 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1787 return 1;
1788 }
1789
1790 /* check if we have already parsed those mtdids */
1791 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1792 ids_changed = 0;
1793 } else {
1794 ids_changed = 1;
1795
1796 if (parse_mtdids(ids) != 0) {
1797 mtd_devices_init();
1798 return 1;
1799 }
1800
1801 /* ok it's good, save new ids */
1802 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1803 }
1804
1805 /* parse partitions if either mtdparts or mtdids were updated */
1806 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1807 if (parse_mtdparts(parts) != 0)
1808 return 1;
1809
1810 if (list_empty(&devices)) {
1811 printf("mtdparts_init: no valid partitions\n");
1812 return 1;
1813 }
1814
1815 /* ok it's good, save new parts */
1816 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1817
1818 /* reset first partition from first dev from the list as current */
Stefan Roese4fa1f2d2009-05-16 12:04:22 +02001819 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
1820 current_mtd_partnum = 0;
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001821 current_save();
1822
Wolfgang Denk58daebd2010-04-28 10:58:10 +02001823 debug("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n",
Stefan Roese4fa1f2d2009-05-16 12:04:22 +02001824 MTD_DEV_TYPE(current_mtd_dev->id->type),
1825 current_mtd_dev->id->num, current_mtd_partnum);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001826 }
1827
1828 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1829 if (!parts && (last_parts[0] != '\0'))
1830 return mtd_devices_init();
1831
1832 /* do not process current partition if mtdparts variable is null */
1833 if (!parts)
1834 return 0;
1835
1836 /* is current partition set in environment? if so, use it */
1837 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1838 struct part_info *p;
1839 struct mtd_device *cdev;
1840 u8 pnum;
1841
Wolfgang Denk58daebd2010-04-28 10:58:10 +02001842 debug("--- getting current partition: %s\n", tmp_ep);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001843
1844 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
Stefan Roese4fa1f2d2009-05-16 12:04:22 +02001845 current_mtd_dev = cdev;
1846 current_mtd_partnum = pnum;
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001847 current_save();
1848 }
Simon Glass64b723f2017-08-03 12:22:12 -06001849 } else if (env_get("partition") == NULL) {
Wolfgang Denk58daebd2010-04-28 10:58:10 +02001850 debug("no partition variable set, setting...\n");
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001851 current_save();
1852 }
1853
1854 return 0;
1855}
1856
1857/**
1858 * Return pointer to the partition of a requested number from a requested
1859 * device.
1860 *
1861 * @param dev device that is to be searched for a partition
1862 * @param part_num requested partition number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001863 * Return: pointer to the part_info, NULL otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001864 */
1865static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
1866{
1867 struct list_head *entry;
1868 struct part_info *part;
1869 int num;
1870
1871 if (!dev)
1872 return NULL;
1873
Wolfgang Denk58daebd2010-04-28 10:58:10 +02001874 debug("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001875 part_num, MTD_DEV_TYPE(dev->id->type),
1876 dev->id->num, dev->id->mtd_id);
1877
1878 if (part_num >= dev->num_parts) {
1879 printf("invalid partition number %d for device %s%d (%s)\n",
1880 part_num, MTD_DEV_TYPE(dev->id->type),
1881 dev->id->num, dev->id->mtd_id);
1882 return NULL;
1883 }
1884
1885 /* locate partition number, return it */
1886 num = 0;
1887 list_for_each(entry, &dev->parts) {
1888 part = list_entry(entry, struct part_info, link);
1889
1890 if (part_num == num++) {
1891 return part;
1892 }
1893 }
1894
1895 return NULL;
1896}
1897
1898/***************************************************/
Bin Meng75574052016-02-05 19:30:11 -08001899/* U-Boot commands */
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001900/***************************************************/
1901/* command line only */
1902/**
1903 * Routine implementing u-boot chpart command. Sets new current partition based
1904 * on the user supplied partition id. For partition id format see find_dev_and_part().
1905 *
1906 * @param cmdtp command internal data
1907 * @param flag command flag
1908 * @param argc number of arguments supplied to the command
1909 * @param argv arguments list
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001910 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001911 */
Simon Glassed38aef2020-05-10 11:40:03 -06001912static int do_chpart(struct cmd_tbl *cmdtp, int flag, int argc,
1913 char *const argv[])
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001914{
1915/* command line only */
1916 struct mtd_device *dev;
1917 struct part_info *part;
1918 u8 pnum;
1919
1920 if (mtdparts_init() !=0)
1921 return 1;
1922
1923 if (argc < 2) {
1924 printf("no partition id specified\n");
1925 return 1;
1926 }
1927
1928 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
1929 return 1;
1930
Stefan Roese4fa1f2d2009-05-16 12:04:22 +02001931 current_mtd_dev = dev;
1932 current_mtd_partnum = pnum;
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001933 current_save();
1934
1935 printf("partition changed to %s%d,%d\n",
1936 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
1937
1938 return 0;
1939}
1940
1941/**
1942 * Routine implementing u-boot mtdparts command. Initialize/update default global
1943 * partition list and process user partition request (list, add, del).
1944 *
1945 * @param cmdtp command internal data
1946 * @param flag command flag
1947 * @param argc number of arguments supplied to the command
1948 * @param argv arguments list
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001949 * Return: 0 on success, 1 otherwise
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001950 */
Simon Glassed38aef2020-05-10 11:40:03 -06001951static int do_mtdparts(struct cmd_tbl *cmdtp, int flag, int argc,
1952 char *const argv[])
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001953{
1954 if (argc == 2) {
1955 if (strcmp(argv[1], "default") == 0) {
Simon Glass6a38e412017-08-03 12:22:09 -06001956 env_set("mtdids", NULL);
1957 env_set("mtdparts", NULL);
1958 env_set("partition", NULL);
Ladislav Michla91260e2016-07-12 20:28:25 +02001959 use_defaults = 1;
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001960
1961 mtdparts_init();
1962 return 0;
1963 } else if (strcmp(argv[1], "delall") == 0) {
1964 /* this may be the first run, initialize lists if needed */
1965 mtdparts_init();
1966
Simon Glass6a38e412017-08-03 12:22:09 -06001967 env_set("mtdparts", NULL);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001968
1969 /* mtd_devices_init() calls current_save() */
1970 return mtd_devices_init();
1971 }
1972 }
1973
1974 /* make sure we are in sync with env variables */
1975 if (mtdparts_init() != 0)
1976 return 1;
1977
1978 if (argc == 1) {
1979 list_partitions();
1980 return 0;
1981 }
1982
1983 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
Ben Gardiner5063d2e2010-08-31 17:48:04 -04001984 if (((argc == 5) || (argc == 6)) && (strncmp(argv[1], "add", 3) == 0)) {
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001985#define PART_ADD_DESC_MAXLEN 64
1986 char tmpbuf[PART_ADD_DESC_MAXLEN];
Ben Gardiner5063d2e2010-08-31 17:48:04 -04001987#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
1988 struct mtd_info *mtd;
1989 uint64_t next_offset;
1990#endif
Stefan Roeseb1423dd2009-03-19 13:30:36 +01001991 u8 type, num, len;
1992 struct mtd_device *dev;
1993 struct mtd_device *dev_tmp;
1994 struct mtdids *id;
1995 struct part_info *p;
1996
1997 if (mtd_id_parse(argv[2], NULL, &type, &num) != 0)
1998 return 1;
1999
2000 if ((id = id_find(type, num)) == NULL) {
2001 printf("no such device %s defined in mtdids variable\n", argv[2]);
2002 return 1;
2003 }
2004
2005 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
2006 len += strlen(argv[3]); /* size@offset */
2007 len += strlen(argv[4]) + 2; /* '(' name ')' */
2008 if (argv[5] && (strlen(argv[5]) == 2))
2009 len += 2; /* 'ro' */
2010
2011 if (len >= PART_ADD_DESC_MAXLEN) {
2012 printf("too long partition description\n");
2013 return 1;
2014 }
2015 sprintf(tmpbuf, "%s:%s(%s)%s",
2016 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
Wolfgang Denk58daebd2010-04-28 10:58:10 +02002017 debug("add tmpbuf: %s\n", tmpbuf);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002018
2019 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
2020 return 1;
2021
Wolfgang Denk58daebd2010-04-28 10:58:10 +02002022 debug("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002023 dev->id->num, dev->id->mtd_id);
2024
Ben Gardiner5063d2e2010-08-31 17:48:04 -04002025 p = list_entry(dev->parts.next, struct part_info, link);
2026
2027#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2028 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
2029 return 1;
2030
2031 if (!strcmp(&argv[1][3], ".spread")) {
2032 spread_partition(mtd, p, &next_offset);
Steve Rae6eb14ff2016-06-29 13:50:59 -07002033 debug("increased %s to %llu bytes\n", p->name, p->size);
Ben Gardiner5063d2e2010-08-31 17:48:04 -04002034 }
2035#endif
2036
2037 dev_tmp = device_find(dev->id->type, dev->id->num);
2038 if (dev_tmp == NULL) {
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002039 device_add(dev);
Ben Gardiner5063d2e2010-08-31 17:48:04 -04002040 } else if (part_add(dev_tmp, p) != 0) {
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002041 /* merge new partition with existing ones*/
Ben Gardiner5063d2e2010-08-31 17:48:04 -04002042 device_del(dev);
2043 return 1;
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002044 }
2045
2046 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
Robert P. J. Day02ce7162013-02-04 13:51:10 +00002047 printf("generated mtdparts too long, resetting to null\n");
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002048 return 1;
2049 }
2050
2051 return 0;
2052 }
2053
2054 /* mtdparts del part-id */
2055 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
Wolfgang Denk58daebd2010-04-28 10:58:10 +02002056 debug("del: part-id = %s\n", argv[2]);
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002057
2058 return delete_partition(argv[2]);
2059 }
2060
Ben Gardinere6064f52010-08-31 17:48:03 -04002061#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2062 if ((argc == 2) && (strcmp(argv[1], "spread") == 0))
2063 return spread_partitions();
2064#endif /* CONFIG_CMD_MTDPARTS_SPREAD */
2065
Simon Glassa06dfc72011-12-10 08:44:01 +00002066 return CMD_RET_USAGE;
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002067}
2068
2069/***************************************************/
2070U_BOOT_CMD(
2071 chpart, 2, 0, do_chpart,
Adarsh Babu Kalepalli65208602021-05-24 13:35:39 +05302072 "change active partition of a MTD device",
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002073 "part-id\n"
Adarsh Babu Kalepalli65208602021-05-24 13:35:39 +05302074 " - change active partition (e.g. part-id = nand0,1) of a MTD device"
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002075);
2076
Tom Rini03f146c2023-10-07 15:13:08 -04002077U_BOOT_LONGHELP(mtdparts,
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002078 "\n"
2079 " - list partition table\n"
2080 "mtdparts delall\n"
2081 " - delete all partitions\n"
2082 "mtdparts del part-id\n"
2083 " - delete partition (e.g. part-id = nand0,1)\n"
2084 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2085 " - add partition\n"
Ben Gardiner5063d2e2010-08-31 17:48:04 -04002086#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2087 "mtdparts add.spread <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2088 " - add partition, padding size by skipping bad blocks\n"
2089#endif
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002090 "mtdparts default\n"
Ben Gardinere6064f52010-08-31 17:48:03 -04002091 " - reset partition table to defaults\n"
2092#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2093 "mtdparts spread\n"
2094 " - adjust the sizes of the partitions so they are\n"
2095 " at least as big as the mtdparts variable specifies\n"
2096 " and they each start on a good block\n\n"
2097#else
2098 "\n"
2099#endif /* CONFIG_CMD_MTDPARTS_SPREAD */
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002100 "-----\n\n"
2101 "this command uses three environment variables:\n\n"
2102 "'partition' - keeps current partition identifier\n\n"
2103 "partition := <part-id>\n"
2104 "<part-id> := <dev-id>,part_num\n\n"
2105 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2106 "mtdids=<idmap>[,<idmap>,...]\n\n"
2107 "<idmap> := <dev-id>=<mtd-id>\n"
Miquel Raynalceb9cae2018-09-06 09:08:45 +02002108 "<dev-id> := 'nand'|'nor'|'onenand'|'spi-nand'<dev-num>\n"
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002109 "<dev-num> := mtd device number, 0...\n"
2110 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2111 "'mtdparts' - partition list\n\n"
2112 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2113 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2114 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2115 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2116 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2117 "<offset> := partition start offset within the device\n"
2118 "<name> := '(' NAME ')'\n"
Tom Rini03f146c2023-10-07 15:13:08 -04002119 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)");
Kim Phillipsdc00a682012-10-29 13:34:31 +00002120
2121U_BOOT_CMD(
2122 mtdparts, 6, 0, do_mtdparts,
2123 "define flash/nand partitions", mtdparts_help_text
Stefan Roeseb1423dd2009-03-19 13:30:36 +01002124);
2125/***************************************************/