Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2 | /* |
| 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 Gardiner | e6064f5 | 2010-08-31 17:48:03 -0400 | [diff] [blame] | 19 | * (C) Copyright 2008 |
| 20 | * Harald Welte, OpenMoko, Inc., Harald Welte <laforge@openmoko.org> |
| 21 | * |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 22 | * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $ |
| 23 | * Copyright 2002 SYSGO Real-Time Solutions GmbH |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 24 | */ |
| 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 Raynal | ceb9cae | 2018-09-06 09:08:45 +0200 | [diff] [blame] | 40 | * <dev-id> := 'nand'|'nor'|'onenand'|'spi-nand'<dev-num> |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 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 | * |
Miquel Raynal | 24de4a5 | 2018-09-06 09:08:46 +0200 | [diff] [blame] | 47 | * mtdparts=[mtdparts=]<mtd-def>[;<mtd-def>...] |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 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 |
Miquel Raynal | 24de4a5 | 2018-09-06 09:08:46 +0200 | [diff] [blame] | 65 | * mtdparts=[mtdparts=]edb7312-nor:- |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 66 | * |
| 67 | * 1 NOR Flash with 2 partitions, 1 NAND with one |
| 68 | * mtdids=nor0=edb7312-nor,nand0=edb7312-nand |
Miquel Raynal | 24de4a5 | 2018-09-06 09:08:46 +0200 | [diff] [blame] | 69 | * mtdparts=[mtdparts=]edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 70 | * |
| 71 | */ |
| 72 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 73 | #include <command.h> |
Simon Glass | db22961 | 2019-08-01 09:46:42 -0600 | [diff] [blame] | 74 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 75 | #include <log.h> |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 76 | #include <malloc.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 77 | #include <asm/global_data.h> |
Ladislav Michl | 816af31 | 2009-03-23 12:06:07 +0100 | [diff] [blame] | 78 | #include <jffs2/load_kernel.h> |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 79 | #include <linux/list.h> |
| 80 | #include <linux/ctype.h> |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 81 | #include <linux/err.h> |
| 82 | #include <linux/mtd/mtd.h> |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 83 | |
| 84 | #if defined(CONFIG_CMD_NAND) |
Masahiro Yamada | 2b7a873 | 2017-11-30 13:45:24 +0900 | [diff] [blame] | 85 | #include <linux/mtd/rawnand.h> |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 86 | #include <nand.h> |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 87 | #endif |
| 88 | |
| 89 | #if defined(CONFIG_CMD_ONENAND) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 90 | #include <linux/mtd/onenand.h> |
| 91 | #include <onenand_uboot.h> |
| 92 | #endif |
| 93 | |
Joe Hershberger | 3902c60 | 2013-04-08 10:32:50 +0000 | [diff] [blame] | 94 | DECLARE_GLOBAL_DATA_PTR; |
| 95 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 96 | /* special size referring to all the remaining space in a partition */ |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 97 | #define SIZE_REMAINING (~0llu) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 98 | |
| 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 Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 103 | #define OFFSET_NOT_SPECIFIED (~0llu) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 104 | |
| 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 Ripard | bf2fbd1 | 2017-02-27 18:22:04 +0100 | [diff] [blame] | 113 | #ifdef CONFIG_MTDIDS_DEFAULT |
| 114 | #define MTDIDS_DEFAULT CONFIG_MTDIDS_DEFAULT |
| 115 | #else |
Ladislav Michl | 207350b | 2016-07-12 20:28:26 +0200 | [diff] [blame] | 116 | #define MTDIDS_DEFAULT NULL |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 117 | #endif |
Maxime Ripard | bf2fbd1 | 2017-02-27 18:22:04 +0100 | [diff] [blame] | 118 | #ifdef CONFIG_MTDPARTS_DEFAULT |
| 119 | #define MTDPARTS_DEFAULT CONFIG_MTDPARTS_DEFAULT |
| 120 | #else |
Ladislav Michl | 207350b | 2016-07-12 20:28:26 +0200 | [diff] [blame] | 121 | #define MTDPARTS_DEFAULT NULL |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 122 | #endif |
Patrick Delaunay | b281fd0 | 2022-12-07 09:26:44 +0100 | [diff] [blame] | 123 | |
Ladislav Michl | 207350b | 2016-07-12 20:28:26 +0200 | [diff] [blame] | 124 | #if defined(CONFIG_SYS_MTDPARTS_RUNTIME) |
| 125 | extern void board_mtdparts_default(const char **mtdids, const char **mtdparts); |
| 126 | #endif |
| 127 | static const char *mtdids_default = MTDIDS_DEFAULT; |
| 128 | static const char *mtdparts_default = MTDPARTS_DEFAULT; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 129 | |
| 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 Rini | 4a786c9 | 2017-08-14 20:42:27 -0400 | [diff] [blame] | 134 | static char last_ids[MTDIDS_MAXLEN + 1]; |
| 135 | static char last_parts[MTDPARTS_MAXLEN + 1]; |
| 136 | static char last_partition[PARTITION_MAXLEN + 1]; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 137 | |
| 138 | /* low level jffs2 cache cleaning routine */ |
| 139 | extern void jffs2_free_cache(struct part_info *part); |
| 140 | |
| 141 | /* mtdids mapping list, filled by parse_ids() */ |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 142 | static struct list_head mtdids; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 143 | |
| 144 | /* device/partition list, parse_cmdline() parses into here */ |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 145 | static struct list_head devices; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 146 | |
| 147 | /* current active device and partition number */ |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 148 | struct mtd_device *current_mtd_dev = NULL; |
| 149 | u8 current_mtd_partnum = 0; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 150 | |
Ladislav Michl | a91260e | 2016-07-12 20:28:25 +0200 | [diff] [blame] | 151 | u8 use_defaults; |
| 152 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 153 | static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num); |
| 154 | |
| 155 | /* command line only routines */ |
| 156 | static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len); |
| 157 | static 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 169 | * Return: resulting unsigned int |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 170 | */ |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 171 | static u64 memsize_parse (const char *const ptr, const char **retptr) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 172 | { |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 173 | u64 ret = simple_strtoull(ptr, (char **)retptr, 0); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 174 | |
| 175 | switch (**retptr) { |
| 176 | case 'G': |
| 177 | case 'g': |
| 178 | ret <<= 10; |
Miquel Raynal | d989b58 | 2018-08-16 17:30:21 +0200 | [diff] [blame] | 179 | /* Fallthrough */ |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 180 | case 'M': |
| 181 | case 'm': |
| 182 | ret <<= 10; |
Miquel Raynal | d989b58 | 2018-08-16 17:30:21 +0200 | [diff] [blame] | 183 | /* Fallthrough */ |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 184 | case 'K': |
| 185 | case 'k': |
| 186 | ret <<= 10; |
| 187 | (*retptr)++; |
Miquel Raynal | d989b58 | 2018-08-16 17:30:21 +0200 | [diff] [blame] | 188 | /* Fallthrough */ |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 189 | 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 Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 207 | static void memsize_format(char *buf, u64 size) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 208 | { |
| 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 Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 214 | sprintf(buf, "%llug", size/SIZE_GB); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 215 | else if ((size % SIZE_MB) == 0) |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 216 | sprintf(buf, "%llum", size/SIZE_MB); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 217 | else if (size % SIZE_KB == 0) |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 218 | sprintf(buf, "%lluk", size/SIZE_KB); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 219 | else |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 220 | sprintf(buf, "%llu", size); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 221 | } |
| 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 | */ |
| 228 | static void index_partitions(void) |
| 229 | { |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 230 | u16 mtddevnum; |
| 231 | struct part_info *part; |
| 232 | struct list_head *dentry; |
| 233 | struct mtd_device *dev; |
| 234 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 235 | debug("--- index partitions ---\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 236 | |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 237 | if (current_mtd_dev) { |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 238 | mtddevnum = 0; |
| 239 | list_for_each(dentry, &devices) { |
| 240 | dev = list_entry(dentry, struct mtd_device, link); |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 241 | if (dev == current_mtd_dev) { |
| 242 | mtddevnum += current_mtd_partnum; |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 243 | env_set_ulong("mtddevnum", mtddevnum); |
Tom Rini | 4a786c9 | 2017-08-14 20:42:27 -0400 | [diff] [blame] | 244 | debug("=> mtddevnum %d,\n", mtddevnum); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 245 | break; |
| 246 | } |
| 247 | mtddevnum += dev->num_parts; |
| 248 | } |
| 249 | |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 250 | part = mtd_part_info(current_mtd_dev, current_mtd_partnum); |
Tom Rini | 4a786c9 | 2017-08-14 20:42:27 -0400 | [diff] [blame] | 251 | if (part) { |
| 252 | env_set("mtddevname", part->name); |
| 253 | |
| 254 | debug("=> mtddevname %s\n", part->name); |
| 255 | } else { |
| 256 | env_set("mtddevname", NULL); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 257 | |
Tom Rini | 4a786c9 | 2017-08-14 20:42:27 -0400 | [diff] [blame] | 258 | debug("=> mtddevname NULL\n"); |
| 259 | } |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 260 | } else { |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 261 | env_set("mtddevnum", NULL); |
| 262 | env_set("mtddevname", NULL); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 263 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 264 | debug("=> mtddevnum NULL\n=> mtddevname NULL\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Save current device and partition in environment variable 'partition'. |
| 270 | */ |
| 271 | static void current_save(void) |
| 272 | { |
| 273 | char buf[16]; |
| 274 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 275 | debug("--- current_save ---\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 276 | |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 277 | 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 Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 280 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 281 | env_set("partition", buf); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 282 | strncpy(last_partition, buf, 16); |
| 283 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 284 | debug("=> partition %s\n", buf); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 285 | } else { |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 286 | env_set("partition", NULL); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 287 | last_partition[0] = '\0'; |
| 288 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 289 | debug("=> partition NULL\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 290 | } |
| 291 | index_partitions(); |
| 292 | } |
| 293 | |
Ben Gardiner | f37bf3f | 2010-08-31 17:48:00 -0400 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * Produce a mtd_info given a type and num. |
| 297 | * |
| 298 | * @param type mtd type |
| 299 | * @param num mtd number |
| 300 | * @param mtd a pointer to an mtd_info instance (output) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 301 | * Return: 0 if device is valid, 1 otherwise |
Ben Gardiner | f37bf3f | 2010-08-31 17:48:00 -0400 | [diff] [blame] | 302 | */ |
| 303 | static int get_mtd_info(u8 type, u8 num, struct mtd_info **mtd) |
| 304 | { |
| 305 | char mtd_dev[16]; |
| 306 | |
| 307 | sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(type), num); |
| 308 | *mtd = get_mtd_device_nm(mtd_dev); |
| 309 | if (IS_ERR(*mtd)) { |
| 310 | printf("Device %s not found!\n", mtd_dev); |
| 311 | return 1; |
| 312 | } |
Heiko Schocher | 7bc79e0 | 2014-07-14 09:17:11 +0200 | [diff] [blame] | 313 | put_mtd_device(*mtd); |
Ben Gardiner | f37bf3f | 2010-08-31 17:48:00 -0400 | [diff] [blame] | 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 318 | /** |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 319 | * Performs sanity check for supplied flash partition. |
| 320 | * Table of existing MTD flash devices is searched and partition device |
| 321 | * is located. Alignment with the granularity of nand erasesize is verified. |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 322 | * |
| 323 | * @param id of the parent device |
| 324 | * @param part partition to validate |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 325 | * Return: 0 if partition is valid, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 326 | */ |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 327 | static int part_validate_eraseblock(struct mtdids *id, struct part_info *part) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 328 | { |
Ben Gardiner | f37bf3f | 2010-08-31 17:48:00 -0400 | [diff] [blame] | 329 | struct mtd_info *mtd = NULL; |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 330 | int i, j; |
| 331 | ulong start; |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 332 | u64 offset, size; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 333 | |
Ben Gardiner | f37bf3f | 2010-08-31 17:48:00 -0400 | [diff] [blame] | 334 | if (get_mtd_info(id->type, id->num, &mtd)) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 335 | return 1; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 336 | |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 337 | part->sector_size = mtd->erasesize; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 338 | |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 339 | if (!mtd->numeraseregions) { |
| 340 | /* |
Miquel Raynal | ceb9cae | 2018-09-06 09:08:45 +0200 | [diff] [blame] | 341 | * Only one eraseregion (NAND, SPI-NAND, OneNAND or uniform NOR), |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 342 | * checking for alignment is easy here |
| 343 | */ |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 344 | offset = part->offset; |
| 345 | if (do_div(offset, mtd->erasesize)) { |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 346 | printf("%s%d: partition (%s) start offset" |
| 347 | "alignment incorrect\n", |
| 348 | MTD_DEV_TYPE(id->type), id->num, part->name); |
| 349 | return 1; |
| 350 | } |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 351 | |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 352 | size = part->size; |
| 353 | if (do_div(size, mtd->erasesize)) { |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 354 | printf("%s%d: partition (%s) size alignment incorrect\n", |
| 355 | MTD_DEV_TYPE(id->type), id->num, part->name); |
| 356 | return 1; |
| 357 | } |
| 358 | } else { |
| 359 | /* |
| 360 | * Multiple eraseregions (non-uniform NOR), |
| 361 | * checking for alignment is more complex here |
| 362 | */ |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 363 | |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 364 | /* Check start alignment */ |
| 365 | for (i = 0; i < mtd->numeraseregions; i++) { |
| 366 | start = mtd->eraseregions[i].offset; |
| 367 | for (j = 0; j < mtd->eraseregions[i].numblocks; j++) { |
| 368 | if (part->offset == start) |
| 369 | goto start_ok; |
| 370 | start += mtd->eraseregions[i].erasesize; |
| 371 | } |
| 372 | } |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 373 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 374 | printf("%s%d: partition (%s) start offset alignment incorrect\n", |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 375 | MTD_DEV_TYPE(id->type), id->num, part->name); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 376 | return 1; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 377 | |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 378 | start_ok: |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 379 | |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 380 | /* Check end/size alignment */ |
| 381 | for (i = 0; i < mtd->numeraseregions; i++) { |
| 382 | start = mtd->eraseregions[i].offset; |
| 383 | for (j = 0; j < mtd->eraseregions[i].numblocks; j++) { |
| 384 | if ((part->offset + part->size) == start) |
| 385 | goto end_ok; |
| 386 | start += mtd->eraseregions[i].erasesize; |
| 387 | } |
| 388 | } |
| 389 | /* Check last sector alignment */ |
| 390 | if ((part->offset + part->size) == start) |
| 391 | goto end_ok; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 392 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 393 | printf("%s%d: partition (%s) size alignment incorrect\n", |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 394 | MTD_DEV_TYPE(id->type), id->num, part->name); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 395 | return 1; |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 396 | |
| 397 | end_ok: |
| 398 | return 0; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | return 0; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | |
| 405 | /** |
Otavio Salvador | 6bd89e4 | 2013-10-03 08:04:38 -0300 | [diff] [blame] | 406 | * Performs sanity check for supplied partition. Offset and size are |
| 407 | * verified to be within valid range. Partition type is checked and |
| 408 | * part_validate_eraseblock() is called with the argument of part. |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 409 | * |
| 410 | * @param id of the parent device |
| 411 | * @param part partition to validate |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 412 | * Return: 0 if partition is valid, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 413 | */ |
| 414 | static int part_validate(struct mtdids *id, struct part_info *part) |
| 415 | { |
| 416 | if (part->size == SIZE_REMAINING) |
| 417 | part->size = id->size - part->offset; |
| 418 | |
| 419 | if (part->offset > id->size) { |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 420 | printf("%s: offset %08llx beyond flash size %08llx\n", |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 421 | id->mtd_id, part->offset, id->size); |
| 422 | return 1; |
| 423 | } |
| 424 | |
| 425 | if ((part->offset + part->size) <= part->offset) { |
| 426 | printf("%s%d: partition (%s) size too big\n", |
| 427 | MTD_DEV_TYPE(id->type), id->num, part->name); |
| 428 | return 1; |
| 429 | } |
| 430 | |
| 431 | if (part->offset + part->size > id->size) { |
| 432 | printf("%s: partitioning exceeds flash size\n", id->mtd_id); |
| 433 | return 1; |
| 434 | } |
| 435 | |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 436 | /* |
| 437 | * Now we need to check if the partition starts and ends on |
| 438 | * sector (eraseblock) regions |
| 439 | */ |
| 440 | return part_validate_eraseblock(id, part); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /** |
Robert P. J. Day | e3f2c2e | 2013-08-21 11:39:19 -0400 | [diff] [blame] | 444 | * Delete selected partition from the partition list of the specified device. |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 445 | * |
| 446 | * @param dev device to delete partition from |
| 447 | * @param part partition to delete |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 448 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 449 | */ |
| 450 | static int part_del(struct mtd_device *dev, struct part_info *part) |
| 451 | { |
| 452 | u8 current_save_needed = 0; |
| 453 | |
| 454 | /* if there is only one partition, remove whole device */ |
| 455 | if (dev->num_parts == 1) |
| 456 | return device_del(dev); |
| 457 | |
| 458 | /* otherwise just delete this partition */ |
| 459 | |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 460 | if (dev == current_mtd_dev) { |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 461 | /* we are modyfing partitions for the current device, |
| 462 | * update current */ |
| 463 | struct part_info *curr_pi; |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 464 | curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 465 | |
| 466 | if (curr_pi) { |
| 467 | if (curr_pi == part) { |
| 468 | printf("current partition deleted, resetting current to 0\n"); |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 469 | current_mtd_partnum = 0; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 470 | } else if (part->offset <= curr_pi->offset) { |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 471 | current_mtd_partnum--; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 472 | } |
| 473 | current_save_needed = 1; |
| 474 | } |
| 475 | } |
| 476 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 477 | list_del(&part->link); |
| 478 | free(part); |
| 479 | dev->num_parts--; |
| 480 | |
| 481 | if (current_save_needed > 0) |
| 482 | current_save(); |
| 483 | else |
| 484 | index_partitions(); |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Delete all partitions from parts head list, free memory. |
| 491 | * |
| 492 | * @param head list of partitions to delete |
| 493 | */ |
| 494 | static void part_delall(struct list_head *head) |
| 495 | { |
| 496 | struct list_head *entry, *n; |
| 497 | struct part_info *part_tmp; |
| 498 | |
| 499 | /* clean tmp_list and free allocated memory */ |
| 500 | list_for_each_safe(entry, n, head) { |
| 501 | part_tmp = list_entry(entry, struct part_info, link); |
| 502 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 503 | list_del(entry); |
| 504 | free(part_tmp); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Add new partition to the supplied partition list. Make sure partitions are |
| 510 | * sorted by offset in ascending order. |
| 511 | * |
| 512 | * @param head list this partition is to be added to |
| 513 | * @param new partition to be added |
| 514 | */ |
| 515 | static int part_sort_add(struct mtd_device *dev, struct part_info *part) |
| 516 | { |
| 517 | struct list_head *entry; |
| 518 | struct part_info *new_pi, *curr_pi; |
| 519 | |
| 520 | /* link partition to parrent dev */ |
| 521 | part->dev = dev; |
| 522 | |
| 523 | if (list_empty(&dev->parts)) { |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 524 | debug("part_sort_add: list empty\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 525 | list_add(&part->link, &dev->parts); |
| 526 | dev->num_parts++; |
| 527 | index_partitions(); |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | new_pi = list_entry(&part->link, struct part_info, link); |
| 532 | |
| 533 | /* get current partition info if we are updating current device */ |
| 534 | curr_pi = NULL; |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 535 | if (dev == current_mtd_dev) |
| 536 | curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 537 | |
| 538 | list_for_each(entry, &dev->parts) { |
| 539 | struct part_info *pi; |
| 540 | |
| 541 | pi = list_entry(entry, struct part_info, link); |
| 542 | |
| 543 | /* be compliant with kernel cmdline, allow only one partition at offset zero */ |
| 544 | if ((new_pi->offset == pi->offset) && (pi->offset == 0)) { |
| 545 | printf("cannot add second partition at offset 0\n"); |
| 546 | return 1; |
| 547 | } |
| 548 | |
| 549 | if (new_pi->offset <= pi->offset) { |
| 550 | list_add_tail(&part->link, entry); |
| 551 | dev->num_parts++; |
| 552 | |
| 553 | if (curr_pi && (pi->offset <= curr_pi->offset)) { |
| 554 | /* we are modyfing partitions for the current |
| 555 | * device, update current */ |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 556 | current_mtd_partnum++; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 557 | current_save(); |
| 558 | } else { |
| 559 | index_partitions(); |
| 560 | } |
| 561 | return 0; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | list_add_tail(&part->link, &dev->parts); |
| 566 | dev->num_parts++; |
| 567 | index_partitions(); |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Add provided partition to the partition list of a given device. |
| 573 | * |
| 574 | * @param dev device to which partition is added |
| 575 | * @param part partition to be added |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 576 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 577 | */ |
| 578 | static int part_add(struct mtd_device *dev, struct part_info *part) |
| 579 | { |
| 580 | /* verify alignment and size */ |
| 581 | if (part_validate(dev->id, part) != 0) |
| 582 | return 1; |
| 583 | |
| 584 | /* partition is ok, add it to the list */ |
| 585 | if (part_sort_add(dev, part) != 0) |
| 586 | return 1; |
| 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * Parse one partition definition, allocate memory and return pointer to this |
| 593 | * location in retpart. |
| 594 | * |
| 595 | * @param partdef pointer to the partition definition string i.e. <part-def> |
| 596 | * @param ret output pointer to next char after parse completes (output) |
| 597 | * @param retpart pointer to the allocated partition (output) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 598 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 599 | */ |
| 600 | static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart) |
| 601 | { |
| 602 | struct part_info *part; |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 603 | u64 size; |
| 604 | u64 offset; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 605 | const char *name; |
| 606 | int name_len; |
| 607 | unsigned int mask_flags; |
| 608 | const char *p; |
| 609 | |
| 610 | p = partdef; |
| 611 | *retpart = NULL; |
| 612 | *ret = NULL; |
| 613 | |
| 614 | /* fetch the partition size */ |
| 615 | if (*p == '-') { |
| 616 | /* assign all remaining space to this partition */ |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 617 | debug("'-': remaining size assigned\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 618 | size = SIZE_REMAINING; |
| 619 | p++; |
| 620 | } else { |
| 621 | size = memsize_parse(p, &p); |
| 622 | if (size < MIN_PART_SIZE) { |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 623 | printf("partition size too small (%llx)\n", size); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 624 | return 1; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /* check for offset */ |
| 629 | offset = OFFSET_NOT_SPECIFIED; |
| 630 | if (*p == '@') { |
| 631 | p++; |
| 632 | offset = memsize_parse(p, &p); |
| 633 | } |
| 634 | |
| 635 | /* now look for the name */ |
| 636 | if (*p == '(') { |
| 637 | name = ++p; |
| 638 | if ((p = strchr(name, ')')) == NULL) { |
| 639 | printf("no closing ) found in partition name\n"); |
| 640 | return 1; |
| 641 | } |
| 642 | name_len = p - name + 1; |
| 643 | if ((name_len - 1) == 0) { |
| 644 | printf("empty partition name\n"); |
| 645 | return 1; |
| 646 | } |
| 647 | p++; |
| 648 | } else { |
| 649 | /* 0x00000000@0x00000000 */ |
| 650 | name_len = 22; |
| 651 | name = NULL; |
| 652 | } |
| 653 | |
| 654 | /* test for options */ |
| 655 | mask_flags = 0; |
| 656 | if (strncmp(p, "ro", 2) == 0) { |
| 657 | mask_flags |= MTD_WRITEABLE_CMD; |
| 658 | p += 2; |
| 659 | } |
| 660 | |
| 661 | /* check for next partition definition */ |
| 662 | if (*p == ',') { |
| 663 | if (size == SIZE_REMAINING) { |
| 664 | *ret = NULL; |
| 665 | printf("no partitions allowed after a fill-up partition\n"); |
| 666 | return 1; |
| 667 | } |
| 668 | *ret = ++p; |
| 669 | } else if ((*p == ';') || (*p == '\0')) { |
| 670 | *ret = p; |
| 671 | } else { |
| 672 | printf("unexpected character '%c' at the end of partition\n", *p); |
| 673 | *ret = NULL; |
| 674 | return 1; |
| 675 | } |
| 676 | |
| 677 | /* allocate memory */ |
| 678 | part = (struct part_info *)malloc(sizeof(struct part_info) + name_len); |
| 679 | if (!part) { |
| 680 | printf("out of memory\n"); |
| 681 | return 1; |
| 682 | } |
| 683 | memset(part, 0, sizeof(struct part_info) + name_len); |
| 684 | part->size = size; |
| 685 | part->offset = offset; |
| 686 | part->mask_flags = mask_flags; |
| 687 | part->name = (char *)(part + 1); |
| 688 | |
| 689 | if (name) { |
| 690 | /* copy user provided name */ |
| 691 | strncpy(part->name, name, name_len - 1); |
| 692 | part->auto_name = 0; |
| 693 | } else { |
| 694 | /* auto generated name in form of size@offset */ |
Kay Potthoff | 79694d6 | 2018-07-17 08:19:39 +0200 | [diff] [blame] | 695 | snprintf(part->name, name_len, "0x%08llx@0x%08llx", size, offset); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 696 | part->auto_name = 1; |
| 697 | } |
| 698 | |
| 699 | part->name[name_len - 1] = '\0'; |
| 700 | INIT_LIST_HEAD(&part->link); |
| 701 | |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 702 | debug("+ partition: name %-22s size 0x%08llx offset 0x%08llx mask flags %d\n", |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 703 | part->name, part->size, |
| 704 | part->offset, part->mask_flags); |
| 705 | |
| 706 | *retpart = part; |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * Check device number to be within valid range for given device type. |
| 712 | * |
Ben Gardiner | f37bf3f | 2010-08-31 17:48:00 -0400 | [diff] [blame] | 713 | * @param type mtd type |
| 714 | * @param num mtd number |
| 715 | * @param size a pointer to the size of the mtd device (output) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 716 | * Return: 0 if device is valid, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 717 | */ |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 718 | static int mtd_device_validate(u8 type, u8 num, u64 *size) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 719 | { |
Ben Gardiner | f37bf3f | 2010-08-31 17:48:00 -0400 | [diff] [blame] | 720 | struct mtd_info *mtd = NULL; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 721 | |
Ben Gardiner | f37bf3f | 2010-08-31 17:48:00 -0400 | [diff] [blame] | 722 | if (get_mtd_info(type, num, &mtd)) |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 723 | return 1; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 724 | |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 725 | *size = mtd->size; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 726 | |
Stefan Roese | 8e66ea7 | 2009-05-12 14:31:56 +0200 | [diff] [blame] | 727 | return 0; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | /** |
| 731 | * Delete all mtd devices from a supplied devices list, free memory allocated for |
| 732 | * each device and delete all device partitions. |
| 733 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 734 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 735 | */ |
| 736 | static int device_delall(struct list_head *head) |
| 737 | { |
| 738 | struct list_head *entry, *n; |
| 739 | struct mtd_device *dev_tmp; |
| 740 | |
| 741 | /* clean devices list */ |
| 742 | list_for_each_safe(entry, n, head) { |
| 743 | dev_tmp = list_entry(entry, struct mtd_device, link); |
| 744 | list_del(entry); |
| 745 | part_delall(&dev_tmp->parts); |
| 746 | free(dev_tmp); |
| 747 | } |
| 748 | INIT_LIST_HEAD(&devices); |
| 749 | |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * If provided device exists it's partitions are deleted, device is removed |
| 755 | * from device list and device memory is freed. |
| 756 | * |
| 757 | * @param dev device to be deleted |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 758 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 759 | */ |
| 760 | static int device_del(struct mtd_device *dev) |
| 761 | { |
| 762 | part_delall(&dev->parts); |
| 763 | list_del(&dev->link); |
| 764 | free(dev); |
| 765 | |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 766 | if (dev == current_mtd_dev) { |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 767 | /* we just deleted current device */ |
| 768 | if (list_empty(&devices)) { |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 769 | current_mtd_dev = NULL; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 770 | } else { |
| 771 | /* reset first partition from first dev from the |
| 772 | * devices list as current */ |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 773 | current_mtd_dev = list_entry(devices.next, struct mtd_device, link); |
| 774 | current_mtd_partnum = 0; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 775 | } |
| 776 | current_save(); |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | index_partitions(); |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * Search global device list and return pointer to the device of type and num |
| 786 | * specified. |
| 787 | * |
| 788 | * @param type device type |
| 789 | * @param num device number |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 790 | * Return: NULL if requested device does not exist |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 791 | */ |
Anatolij Gustschin | 6c44c38 | 2010-03-16 17:10:05 +0100 | [diff] [blame] | 792 | struct mtd_device *device_find(u8 type, u8 num) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 793 | { |
| 794 | struct list_head *entry; |
| 795 | struct mtd_device *dev_tmp; |
| 796 | |
| 797 | list_for_each(entry, &devices) { |
| 798 | dev_tmp = list_entry(entry, struct mtd_device, link); |
| 799 | |
| 800 | if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num)) |
| 801 | return dev_tmp; |
| 802 | } |
| 803 | |
| 804 | return NULL; |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * Add specified device to the global device list. |
| 809 | * |
| 810 | * @param dev device to be added |
| 811 | */ |
| 812 | static void device_add(struct mtd_device *dev) |
| 813 | { |
| 814 | u8 current_save_needed = 0; |
| 815 | |
| 816 | if (list_empty(&devices)) { |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 817 | current_mtd_dev = dev; |
| 818 | current_mtd_partnum = 0; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 819 | current_save_needed = 1; |
| 820 | } |
| 821 | |
| 822 | list_add_tail(&dev->link, &devices); |
| 823 | |
| 824 | if (current_save_needed > 0) |
| 825 | current_save(); |
| 826 | else |
| 827 | index_partitions(); |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * Parse device type, name and mtd-id. If syntax is ok allocate memory and |
| 832 | * return pointer to the device structure. |
| 833 | * |
| 834 | * @param mtd_dev pointer to the device definition string i.e. <mtd-dev> |
| 835 | * @param ret output pointer to next char after parse completes (output) |
| 836 | * @param retdev pointer to the allocated device (output) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 837 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 838 | */ |
| 839 | static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev) |
| 840 | { |
| 841 | struct mtd_device *dev; |
| 842 | struct part_info *part; |
| 843 | struct mtdids *id; |
| 844 | const char *mtd_id; |
| 845 | unsigned int mtd_id_len; |
Wolfgang Denk | e967dc1 | 2011-10-05 22:48:12 +0200 | [diff] [blame] | 846 | const char *p; |
Wolfgang Denk | e967dc1 | 2011-10-05 22:48:12 +0200 | [diff] [blame] | 847 | const char *pend; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 848 | LIST_HEAD(tmp_list); |
| 849 | struct list_head *entry, *n; |
| 850 | u16 num_parts; |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 851 | u64 offset; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 852 | int err = 1; |
| 853 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 854 | debug("===device_parse===\n"); |
Wolfgang Denk | dc6e65c | 2010-04-28 10:53:47 +0200 | [diff] [blame] | 855 | |
| 856 | assert(retdev); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 857 | *retdev = NULL; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 858 | |
Wolfgang Denk | dc6e65c | 2010-04-28 10:53:47 +0200 | [diff] [blame] | 859 | if (ret) |
| 860 | *ret = NULL; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 861 | |
| 862 | /* fetch <mtd-id> */ |
Wolfgang Denk | dc6e65c | 2010-04-28 10:53:47 +0200 | [diff] [blame] | 863 | mtd_id = p = mtd_dev; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 864 | if (!(p = strchr(mtd_id, ':'))) { |
| 865 | printf("no <mtd-id> identifier\n"); |
| 866 | return 1; |
| 867 | } |
| 868 | mtd_id_len = p - mtd_id + 1; |
| 869 | p++; |
| 870 | |
| 871 | /* verify if we have a valid device specified */ |
| 872 | if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) { |
| 873 | printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id); |
| 874 | return 1; |
| 875 | } |
| 876 | |
Wolfgang Denk | e967dc1 | 2011-10-05 22:48:12 +0200 | [diff] [blame] | 877 | pend = strchr(p, ';'); |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 878 | debug("dev type = %d (%s), dev num = %d, mtd-id = %s\n", |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 879 | id->type, MTD_DEV_TYPE(id->type), |
| 880 | id->num, id->mtd_id); |
Vasili Galka | 38c3fff | 2014-08-26 13:45:48 +0300 | [diff] [blame] | 881 | debug("parsing partitions %.*s\n", (int)(pend ? pend - p : strlen(p)), p); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 882 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 883 | /* parse partitions */ |
| 884 | num_parts = 0; |
| 885 | |
| 886 | offset = 0; |
| 887 | if ((dev = device_find(id->type, id->num)) != NULL) { |
| 888 | /* if device already exists start at the end of the last partition */ |
| 889 | part = list_entry(dev->parts.prev, struct part_info, link); |
| 890 | offset = part->offset + part->size; |
| 891 | } |
| 892 | |
| 893 | while (p && (*p != '\0') && (*p != ';')) { |
| 894 | err = 1; |
| 895 | if ((part_parse(p, &p, &part) != 0) || (!part)) |
| 896 | break; |
| 897 | |
| 898 | /* calculate offset when not specified */ |
| 899 | if (part->offset == OFFSET_NOT_SPECIFIED) |
| 900 | part->offset = offset; |
| 901 | else |
| 902 | offset = part->offset; |
| 903 | |
| 904 | /* verify alignment and size */ |
| 905 | if (part_validate(id, part) != 0) |
| 906 | break; |
| 907 | |
| 908 | offset += part->size; |
| 909 | |
| 910 | /* partition is ok, add it to the list */ |
| 911 | list_add_tail(&part->link, &tmp_list); |
| 912 | num_parts++; |
| 913 | err = 0; |
| 914 | } |
| 915 | if (err == 1) { |
| 916 | part_delall(&tmp_list); |
| 917 | return 1; |
| 918 | } |
| 919 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 920 | debug("\ntotal partitions: %d\n", num_parts); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 921 | |
| 922 | /* check for next device presence */ |
| 923 | if (p) { |
| 924 | if (*p == ';') { |
Wolfgang Denk | dc6e65c | 2010-04-28 10:53:47 +0200 | [diff] [blame] | 925 | if (ret) |
| 926 | *ret = ++p; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 927 | } else if (*p == '\0') { |
Wolfgang Denk | dc6e65c | 2010-04-28 10:53:47 +0200 | [diff] [blame] | 928 | if (ret) |
| 929 | *ret = p; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 930 | } else { |
| 931 | printf("unexpected character '%c' at the end of device\n", *p); |
Wolfgang Denk | dc6e65c | 2010-04-28 10:53:47 +0200 | [diff] [blame] | 932 | if (ret) |
| 933 | *ret = NULL; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 934 | return 1; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | /* allocate memory for mtd_device structure */ |
| 939 | if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) { |
| 940 | printf("out of memory\n"); |
| 941 | return 1; |
| 942 | } |
| 943 | memset(dev, 0, sizeof(struct mtd_device)); |
| 944 | dev->id = id; |
| 945 | dev->num_parts = 0; /* part_sort_add increments num_parts */ |
| 946 | INIT_LIST_HEAD(&dev->parts); |
| 947 | INIT_LIST_HEAD(&dev->link); |
| 948 | |
| 949 | /* move partitions from tmp_list to dev->parts */ |
| 950 | list_for_each_safe(entry, n, &tmp_list) { |
| 951 | part = list_entry(entry, struct part_info, link); |
| 952 | list_del(entry); |
| 953 | if (part_sort_add(dev, part) != 0) { |
| 954 | device_del(dev); |
| 955 | return 1; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | *retdev = dev; |
| 960 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 961 | debug("===\n\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * Initialize global device list. |
| 967 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 968 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 969 | */ |
| 970 | static int mtd_devices_init(void) |
| 971 | { |
| 972 | last_parts[0] = '\0'; |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 973 | current_mtd_dev = NULL; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 974 | current_save(); |
| 975 | |
| 976 | return device_delall(&devices); |
| 977 | } |
| 978 | |
| 979 | /* |
| 980 | * Search global mtdids list and find id of requested type and number. |
| 981 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 982 | * Return: pointer to the id if it exists, NULL otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 983 | */ |
| 984 | static struct mtdids* id_find(u8 type, u8 num) |
| 985 | { |
| 986 | struct list_head *entry; |
| 987 | struct mtdids *id; |
| 988 | |
| 989 | list_for_each(entry, &mtdids) { |
| 990 | id = list_entry(entry, struct mtdids, link); |
| 991 | |
| 992 | if ((id->type == type) && (id->num == num)) |
| 993 | return id; |
| 994 | } |
| 995 | |
| 996 | return NULL; |
| 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * Search global mtdids list and find id of a requested mtd_id. |
| 1001 | * |
| 1002 | * Note: first argument is not null terminated. |
| 1003 | * |
| 1004 | * @param mtd_id string containing requested mtd_id |
| 1005 | * @param mtd_id_len length of supplied mtd_id |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1006 | * Return: pointer to the id if it exists, NULL otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1007 | */ |
| 1008 | static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len) |
| 1009 | { |
| 1010 | struct list_head *entry; |
| 1011 | struct mtdids *id; |
| 1012 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 1013 | debug("--- id_find_by_mtd_id: '%.*s' (len = %d)\n", |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1014 | mtd_id_len, mtd_id, mtd_id_len); |
| 1015 | |
| 1016 | list_for_each(entry, &mtdids) { |
| 1017 | id = list_entry(entry, struct mtdids, link); |
| 1018 | |
Vasili Galka | 38c3fff | 2014-08-26 13:45:48 +0300 | [diff] [blame] | 1019 | debug("entry: '%s' (len = %zu)\n", |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1020 | id->mtd_id, strlen(id->mtd_id)); |
| 1021 | |
| 1022 | if (mtd_id_len != strlen(id->mtd_id)) |
| 1023 | continue; |
| 1024 | if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0) |
| 1025 | return id; |
| 1026 | } |
| 1027 | |
| 1028 | return NULL; |
| 1029 | } |
| 1030 | |
| 1031 | /** |
Miquel Raynal | ceb9cae | 2018-09-06 09:08:45 +0200 | [diff] [blame] | 1032 | * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'|'spi-nand'<dev-num>, |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1033 | * return device type and number. |
| 1034 | * |
| 1035 | * @param id string describing device id |
| 1036 | * @param ret_id output pointer to next char after parse completes (output) |
| 1037 | * @param dev_type parsed device type (output) |
| 1038 | * @param dev_num parsed device number (output) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1039 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1040 | */ |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 1041 | int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, |
| 1042 | u8 *dev_num) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1043 | { |
| 1044 | const char *p = id; |
| 1045 | |
| 1046 | *dev_type = 0; |
| 1047 | if (strncmp(p, "nand", 4) == 0) { |
| 1048 | *dev_type = MTD_DEV_TYPE_NAND; |
| 1049 | p += 4; |
| 1050 | } else if (strncmp(p, "nor", 3) == 0) { |
| 1051 | *dev_type = MTD_DEV_TYPE_NOR; |
| 1052 | p += 3; |
| 1053 | } else if (strncmp(p, "onenand", 7) == 0) { |
| 1054 | *dev_type = MTD_DEV_TYPE_ONENAND; |
| 1055 | p += 7; |
Miquel Raynal | ceb9cae | 2018-09-06 09:08:45 +0200 | [diff] [blame] | 1056 | } else if (strncmp(p, "spi-nand", 8) == 0) { |
| 1057 | *dev_type = MTD_DEV_TYPE_SPINAND; |
| 1058 | p += 8; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1059 | } else { |
| 1060 | printf("incorrect device type in %s\n", id); |
| 1061 | return 1; |
| 1062 | } |
| 1063 | |
| 1064 | if (!isdigit(*p)) { |
| 1065 | printf("incorrect device number in %s\n", id); |
| 1066 | return 1; |
| 1067 | } |
| 1068 | |
| 1069 | *dev_num = simple_strtoul(p, (char **)&p, 0); |
| 1070 | if (ret_id) |
| 1071 | *ret_id = p; |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
| 1075 | /** |
| 1076 | * Process all devices and generate corresponding mtdparts string describing |
| 1077 | * all partitions on all devices. |
| 1078 | * |
| 1079 | * @param buf output buffer holding generated mtdparts string (output) |
| 1080 | * @param buflen buffer size |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1081 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1082 | */ |
| 1083 | static int generate_mtdparts(char *buf, u32 buflen) |
| 1084 | { |
| 1085 | struct list_head *pentry, *dentry; |
| 1086 | struct mtd_device *dev; |
| 1087 | struct part_info *part, *prev_part; |
| 1088 | char *p = buf; |
| 1089 | char tmpbuf[32]; |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 1090 | u64 size, offset; |
| 1091 | u32 len, part_cnt; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1092 | u32 maxlen = buflen - 1; |
| 1093 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 1094 | debug("--- generate_mtdparts ---\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1095 | |
| 1096 | if (list_empty(&devices)) { |
| 1097 | buf[0] = '\0'; |
| 1098 | return 0; |
| 1099 | } |
| 1100 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1101 | list_for_each(dentry, &devices) { |
| 1102 | dev = list_entry(dentry, struct mtd_device, link); |
| 1103 | |
| 1104 | /* copy mtd_id */ |
| 1105 | len = strlen(dev->id->mtd_id) + 1; |
| 1106 | if (len > maxlen) |
| 1107 | goto cleanup; |
| 1108 | memcpy(p, dev->id->mtd_id, len - 1); |
| 1109 | p += len - 1; |
| 1110 | *(p++) = ':'; |
| 1111 | maxlen -= len; |
| 1112 | |
| 1113 | /* format partitions */ |
| 1114 | prev_part = NULL; |
| 1115 | part_cnt = 0; |
| 1116 | list_for_each(pentry, &dev->parts) { |
| 1117 | part = list_entry(pentry, struct part_info, link); |
| 1118 | size = part->size; |
| 1119 | offset = part->offset; |
| 1120 | part_cnt++; |
| 1121 | |
| 1122 | /* partition size */ |
| 1123 | memsize_format(tmpbuf, size); |
| 1124 | len = strlen(tmpbuf); |
| 1125 | if (len > maxlen) |
| 1126 | goto cleanup; |
| 1127 | memcpy(p, tmpbuf, len); |
| 1128 | p += len; |
| 1129 | maxlen -= len; |
| 1130 | |
| 1131 | |
| 1132 | /* add offset only when there is a gap between |
| 1133 | * partitions */ |
| 1134 | if ((!prev_part && (offset != 0)) || |
| 1135 | (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) { |
| 1136 | |
| 1137 | memsize_format(tmpbuf, offset); |
| 1138 | len = strlen(tmpbuf) + 1; |
| 1139 | if (len > maxlen) |
| 1140 | goto cleanup; |
| 1141 | *(p++) = '@'; |
| 1142 | memcpy(p, tmpbuf, len - 1); |
| 1143 | p += len - 1; |
| 1144 | maxlen -= len; |
| 1145 | } |
| 1146 | |
| 1147 | /* copy name only if user supplied */ |
| 1148 | if(!part->auto_name) { |
| 1149 | len = strlen(part->name) + 2; |
| 1150 | if (len > maxlen) |
| 1151 | goto cleanup; |
| 1152 | |
| 1153 | *(p++) = '('; |
| 1154 | memcpy(p, part->name, len - 2); |
| 1155 | p += len - 2; |
| 1156 | *(p++) = ')'; |
| 1157 | maxlen -= len; |
| 1158 | } |
| 1159 | |
| 1160 | /* ro mask flag */ |
| 1161 | if (part->mask_flags && MTD_WRITEABLE_CMD) { |
| 1162 | len = 2; |
| 1163 | if (len > maxlen) |
| 1164 | goto cleanup; |
| 1165 | *(p++) = 'r'; |
| 1166 | *(p++) = 'o'; |
| 1167 | maxlen -= 2; |
| 1168 | } |
| 1169 | |
| 1170 | /* print ',' separator if there are other partitions |
| 1171 | * following */ |
| 1172 | if (dev->num_parts > part_cnt) { |
| 1173 | if (1 > maxlen) |
| 1174 | goto cleanup; |
| 1175 | *(p++) = ','; |
| 1176 | maxlen--; |
| 1177 | } |
| 1178 | prev_part = part; |
| 1179 | } |
| 1180 | /* print ';' separator if there are other devices following */ |
| 1181 | if (dentry->next != &devices) { |
| 1182 | if (1 > maxlen) |
| 1183 | goto cleanup; |
| 1184 | *(p++) = ';'; |
| 1185 | maxlen--; |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | /* we still have at least one char left, as we decremented maxlen at |
| 1190 | * the begining */ |
| 1191 | *p = '\0'; |
| 1192 | |
| 1193 | return 0; |
| 1194 | |
| 1195 | cleanup: |
| 1196 | last_parts[0] = '\0'; |
| 1197 | return 1; |
| 1198 | } |
| 1199 | |
| 1200 | /** |
| 1201 | * Call generate_mtdparts to process all devices and generate corresponding |
| 1202 | * mtdparts string, save it in mtdparts environment variable. |
| 1203 | * |
| 1204 | * @param buf output buffer holding generated mtdparts string (output) |
| 1205 | * @param buflen buffer size |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1206 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1207 | */ |
| 1208 | static int generate_mtdparts_save(char *buf, u32 buflen) |
| 1209 | { |
| 1210 | int ret; |
| 1211 | |
| 1212 | ret = generate_mtdparts(buf, buflen); |
| 1213 | |
| 1214 | if ((buf[0] != '\0') && (ret == 0)) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 1215 | env_set("mtdparts", buf); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1216 | else |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 1217 | env_set("mtdparts", NULL); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1218 | |
| 1219 | return ret; |
| 1220 | } |
| 1221 | |
Ben Gardiner | a9b5729 | 2010-08-31 17:48:02 -0400 | [diff] [blame] | 1222 | #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1223 | /** |
Ben Gardiner | a9b5729 | 2010-08-31 17:48:02 -0400 | [diff] [blame] | 1224 | * Get the net size (w/o bad blocks) of the given partition. |
| 1225 | * |
| 1226 | * @param mtd the mtd info |
| 1227 | * @param part the partition |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1228 | * Return: the calculated net size of this partition |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1229 | */ |
Ben Gardiner | a9b5729 | 2010-08-31 17:48:02 -0400 | [diff] [blame] | 1230 | static uint64_t net_part_size(struct mtd_info *mtd, struct part_info *part) |
| 1231 | { |
Scott Wood | 10390ce | 2010-09-09 15:40:03 -0500 | [diff] [blame] | 1232 | uint64_t i, net_size = 0; |
| 1233 | |
Alexander Dahl | 6cae4f4 | 2019-10-30 16:53:55 +0100 | [diff] [blame] | 1234 | if (!mtd->_block_isbad) |
Ben Gardiner | a9b5729 | 2010-08-31 17:48:02 -0400 | [diff] [blame] | 1235 | return part->size; |
| 1236 | |
Ben Gardiner | a9b5729 | 2010-08-31 17:48:02 -0400 | [diff] [blame] | 1237 | for (i = 0; i < part->size; i += mtd->erasesize) { |
Alexander Dahl | 6cae4f4 | 2019-10-30 16:53:55 +0100 | [diff] [blame] | 1238 | if (!mtd->_block_isbad(mtd, part->offset + i)) |
Ben Gardiner | a9b5729 | 2010-08-31 17:48:02 -0400 | [diff] [blame] | 1239 | net_size += mtd->erasesize; |
| 1240 | } |
Scott Wood | 10390ce | 2010-09-09 15:40:03 -0500 | [diff] [blame] | 1241 | |
Ben Gardiner | a9b5729 | 2010-08-31 17:48:02 -0400 | [diff] [blame] | 1242 | return net_size; |
| 1243 | } |
| 1244 | #endif |
| 1245 | |
| 1246 | static void print_partition_table(void) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1247 | { |
| 1248 | struct list_head *dentry, *pentry; |
| 1249 | struct part_info *part; |
| 1250 | struct mtd_device *dev; |
| 1251 | int part_num; |
| 1252 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1253 | list_for_each(dentry, &devices) { |
| 1254 | dev = list_entry(dentry, struct mtd_device, link); |
Ben Gardiner | a9b5729 | 2010-08-31 17:48:02 -0400 | [diff] [blame] | 1255 | /* list partitions for given device */ |
| 1256 | part_num = 0; |
| 1257 | #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) |
| 1258 | struct mtd_info *mtd; |
| 1259 | |
| 1260 | if (get_mtd_info(dev->id->type, dev->id->num, &mtd)) |
| 1261 | return; |
| 1262 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1263 | printf("\ndevice %s%d <%s>, # parts = %d\n", |
| 1264 | MTD_DEV_TYPE(dev->id->type), dev->id->num, |
| 1265 | dev->id->mtd_id, dev->num_parts); |
Ben Gardiner | a9b5729 | 2010-08-31 17:48:02 -0400 | [diff] [blame] | 1266 | printf(" #: name\t\tsize\t\tnet size\toffset\t\tmask_flags\n"); |
| 1267 | |
| 1268 | list_for_each(pentry, &dev->parts) { |
| 1269 | u32 net_size; |
| 1270 | char *size_note; |
| 1271 | |
| 1272 | part = list_entry(pentry, struct part_info, link); |
| 1273 | net_size = net_part_size(mtd, part); |
| 1274 | size_note = part->size == net_size ? " " : " (!)"; |
Alexander Dahl | 6cae4f4 | 2019-10-30 16:53:55 +0100 | [diff] [blame] | 1275 | printf("%2d: %-20s0x%08llx\t0x%08x%s\t0x%08llx\t%d\n", |
Ben Gardiner | a9b5729 | 2010-08-31 17:48:02 -0400 | [diff] [blame] | 1276 | part_num, part->name, part->size, |
| 1277 | net_size, size_note, part->offset, |
| 1278 | part->mask_flags); |
| 1279 | #else /* !defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */ |
| 1280 | printf("\ndevice %s%d <%s>, # parts = %d\n", |
| 1281 | MTD_DEV_TYPE(dev->id->type), dev->id->num, |
| 1282 | dev->id->mtd_id, dev->num_parts); |
David Brownell | d8a6b81 | 2009-04-16 19:55:48 -0700 | [diff] [blame] | 1283 | printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1284 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1285 | list_for_each(pentry, &dev->parts) { |
| 1286 | part = list_entry(pentry, struct part_info, link); |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 1287 | printf("%2d: %-20s0x%08llx\t0x%08llx\t%d\n", |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1288 | part_num, part->name, part->size, |
| 1289 | part->offset, part->mask_flags); |
Ben Gardiner | a9b5729 | 2010-08-31 17:48:02 -0400 | [diff] [blame] | 1290 | #endif /* defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */ |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1291 | part_num++; |
| 1292 | } |
| 1293 | } |
Ben Gardiner | a9b5729 | 2010-08-31 17:48:02 -0400 | [diff] [blame] | 1294 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1295 | if (list_empty(&devices)) |
| 1296 | printf("no partitions defined\n"); |
Ben Gardiner | a9b5729 | 2010-08-31 17:48:02 -0400 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | /** |
| 1300 | * Format and print out a partition list for each device from global device |
| 1301 | * list. |
| 1302 | */ |
| 1303 | static void list_partitions(void) |
| 1304 | { |
| 1305 | struct part_info *part; |
| 1306 | |
| 1307 | debug("\n---list_partitions---\n"); |
| 1308 | print_partition_table(); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1309 | |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 1310 | /* current_mtd_dev is not NULL only when we have non empty device list */ |
| 1311 | if (current_mtd_dev) { |
| 1312 | part = mtd_part_info(current_mtd_dev, current_mtd_partnum); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1313 | if (part) { |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 1314 | printf("\nactive partition: %s%d,%d - (%s) 0x%08llx @ 0x%08llx\n", |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 1315 | MTD_DEV_TYPE(current_mtd_dev->id->type), |
| 1316 | current_mtd_dev->id->num, current_mtd_partnum, |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1317 | part->name, part->size, part->offset); |
| 1318 | } else { |
| 1319 | printf("could not get current partition info\n\n"); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | printf("\ndefaults:\n"); |
Wolfgang Denk | 21c0a54 | 2009-05-17 16:01:54 +0200 | [diff] [blame] | 1324 | printf("mtdids : %s\n", |
| 1325 | mtdids_default ? mtdids_default : "none"); |
Anatolij Gustschin | 99695c2 | 2010-02-24 00:29:44 +0100 | [diff] [blame] | 1326 | /* |
| 1327 | * Using printf() here results in printbuffer overflow |
| 1328 | * if default mtdparts string is greater than console |
| 1329 | * printbuffer. Use puts() to prevent system crashes. |
| 1330 | */ |
| 1331 | puts("mtdparts: "); |
| 1332 | puts(mtdparts_default ? mtdparts_default : "none"); |
| 1333 | puts("\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /** |
| 1337 | * Given partition identifier in form of <dev_type><dev_num>,<part_num> find |
| 1338 | * corresponding device and verify partition number. |
| 1339 | * |
| 1340 | * @param id string describing device and partition or partition name |
| 1341 | * @param dev pointer to the requested device (output) |
| 1342 | * @param part_num verified partition number (output) |
| 1343 | * @param part pointer to requested partition (output) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1344 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1345 | */ |
| 1346 | int find_dev_and_part(const char *id, struct mtd_device **dev, |
| 1347 | u8 *part_num, struct part_info **part) |
| 1348 | { |
| 1349 | struct list_head *dentry, *pentry; |
| 1350 | u8 type, dnum, pnum; |
| 1351 | const char *p; |
| 1352 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 1353 | debug("--- find_dev_and_part ---\nid = %s\n", id); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1354 | |
| 1355 | list_for_each(dentry, &devices) { |
| 1356 | *part_num = 0; |
| 1357 | *dev = list_entry(dentry, struct mtd_device, link); |
| 1358 | list_for_each(pentry, &(*dev)->parts) { |
| 1359 | *part = list_entry(pentry, struct part_info, link); |
| 1360 | if (strcmp((*part)->name, id) == 0) |
| 1361 | return 0; |
| 1362 | (*part_num)++; |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | p = id; |
| 1367 | *dev = NULL; |
| 1368 | *part = NULL; |
| 1369 | *part_num = 0; |
| 1370 | |
| 1371 | if (mtd_id_parse(p, &p, &type, &dnum) != 0) |
| 1372 | return 1; |
| 1373 | |
| 1374 | if ((*p++ != ',') || (*p == '\0')) { |
| 1375 | printf("no partition number specified\n"); |
| 1376 | return 1; |
| 1377 | } |
| 1378 | pnum = simple_strtoul(p, (char **)&p, 0); |
| 1379 | if (*p != '\0') { |
| 1380 | printf("unexpected trailing character '%c'\n", *p); |
| 1381 | return 1; |
| 1382 | } |
| 1383 | |
| 1384 | if ((*dev = device_find(type, dnum)) == NULL) { |
| 1385 | printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum); |
| 1386 | return 1; |
| 1387 | } |
| 1388 | |
| 1389 | if ((*part = mtd_part_info(*dev, pnum)) == NULL) { |
| 1390 | printf("no such partition\n"); |
| 1391 | *dev = NULL; |
| 1392 | return 1; |
| 1393 | } |
| 1394 | |
| 1395 | *part_num = pnum; |
| 1396 | |
| 1397 | return 0; |
| 1398 | } |
| 1399 | |
| 1400 | /** |
| 1401 | * Find and delete partition. For partition id format see find_dev_and_part(). |
| 1402 | * |
| 1403 | * @param id string describing device and partition |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1404 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1405 | */ |
| 1406 | static int delete_partition(const char *id) |
| 1407 | { |
| 1408 | u8 pnum; |
| 1409 | struct mtd_device *dev; |
| 1410 | struct part_info *part; |
| 1411 | |
| 1412 | if (find_dev_and_part(id, &dev, &pnum, &part) == 0) { |
| 1413 | |
Paul Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 1414 | debug("delete_partition: device = %s%d, partition %d = (%s) 0x%08llx@0x%08llx\n", |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1415 | MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum, |
| 1416 | part->name, part->size, part->offset); |
| 1417 | |
| 1418 | if (part_del(dev, part) != 0) |
| 1419 | return 1; |
| 1420 | |
| 1421 | if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) { |
Robert P. J. Day | 02ce716 | 2013-02-04 13:51:10 +0000 | [diff] [blame] | 1422 | printf("generated mtdparts too long, resetting to null\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1423 | return 1; |
| 1424 | } |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
| 1428 | printf("partition %s not found\n", id); |
| 1429 | return 1; |
| 1430 | } |
| 1431 | |
Ben Gardiner | e6064f5 | 2010-08-31 17:48:03 -0400 | [diff] [blame] | 1432 | #if defined(CONFIG_CMD_MTDPARTS_SPREAD) |
| 1433 | /** |
| 1434 | * Increase the size of the given partition so that it's net size is at least |
| 1435 | * as large as the size member and such that the next partition would start on a |
| 1436 | * good block if it were adjacent to this partition. |
| 1437 | * |
| 1438 | * @param mtd the mtd device |
| 1439 | * @param part the partition |
| 1440 | * @param next_offset pointer to the offset of the next partition after this |
| 1441 | * partition's size has been modified (output) |
| 1442 | */ |
| 1443 | static void spread_partition(struct mtd_info *mtd, struct part_info *part, |
| 1444 | uint64_t *next_offset) |
| 1445 | { |
| 1446 | uint64_t net_size, padding_size = 0; |
| 1447 | int truncated; |
| 1448 | |
| 1449 | mtd_get_len_incl_bad(mtd, part->offset, part->size, &net_size, |
| 1450 | &truncated); |
| 1451 | |
| 1452 | /* |
| 1453 | * Absorb bad blocks immediately following this |
| 1454 | * partition also into the partition, such that |
| 1455 | * the next partition starts with a good block. |
| 1456 | */ |
| 1457 | if (!truncated) { |
| 1458 | mtd_get_len_incl_bad(mtd, part->offset + net_size, |
| 1459 | mtd->erasesize, &padding_size, &truncated); |
| 1460 | if (truncated) |
| 1461 | padding_size = 0; |
| 1462 | else |
| 1463 | padding_size -= mtd->erasesize; |
| 1464 | } |
| 1465 | |
| 1466 | if (truncated) { |
| 1467 | printf("truncated partition %s to %lld bytes\n", part->name, |
| 1468 | (uint64_t) net_size + padding_size); |
| 1469 | } |
| 1470 | |
| 1471 | part->size = net_size + padding_size; |
| 1472 | *next_offset = part->offset + part->size; |
| 1473 | } |
| 1474 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1475 | /** |
Ben Gardiner | e6064f5 | 2010-08-31 17:48:03 -0400 | [diff] [blame] | 1476 | * Adjust all of the partition sizes, such that all partitions are at least |
| 1477 | * as big as their mtdparts environment variable sizes and they each start |
| 1478 | * on a good block. |
| 1479 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1480 | * Return: 0 on success, 1 otherwise |
Ben Gardiner | e6064f5 | 2010-08-31 17:48:03 -0400 | [diff] [blame] | 1481 | */ |
| 1482 | static int spread_partitions(void) |
| 1483 | { |
| 1484 | struct list_head *dentry, *pentry; |
| 1485 | struct mtd_device *dev; |
| 1486 | struct part_info *part; |
| 1487 | struct mtd_info *mtd; |
| 1488 | int part_num; |
| 1489 | uint64_t cur_offs; |
| 1490 | |
| 1491 | list_for_each(dentry, &devices) { |
| 1492 | dev = list_entry(dentry, struct mtd_device, link); |
| 1493 | |
| 1494 | if (get_mtd_info(dev->id->type, dev->id->num, &mtd)) |
| 1495 | return 1; |
| 1496 | |
| 1497 | part_num = 0; |
| 1498 | cur_offs = 0; |
| 1499 | list_for_each(pentry, &dev->parts) { |
| 1500 | part = list_entry(pentry, struct part_info, link); |
| 1501 | |
| 1502 | debug("spread_partitions: device = %s%d, partition %d =" |
Steve Rae | 6eb14ff | 2016-06-29 13:50:59 -0700 | [diff] [blame] | 1503 | " (%s) 0x%08llx@0x%08llx\n", |
Ben Gardiner | e6064f5 | 2010-08-31 17:48:03 -0400 | [diff] [blame] | 1504 | MTD_DEV_TYPE(dev->id->type), dev->id->num, |
| 1505 | part_num, part->name, part->size, |
| 1506 | part->offset); |
| 1507 | |
| 1508 | if (cur_offs > part->offset) |
| 1509 | part->offset = cur_offs; |
| 1510 | |
| 1511 | spread_partition(mtd, part, &cur_offs); |
| 1512 | |
| 1513 | part_num++; |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | index_partitions(); |
| 1518 | |
| 1519 | if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) { |
Robert P. J. Day | 02ce716 | 2013-02-04 13:51:10 +0000 | [diff] [blame] | 1520 | printf("generated mtdparts too long, resetting to null\n"); |
Ben Gardiner | e6064f5 | 2010-08-31 17:48:03 -0400 | [diff] [blame] | 1521 | return 1; |
| 1522 | } |
| 1523 | return 0; |
| 1524 | } |
| 1525 | #endif /* CONFIG_CMD_MTDPARTS_SPREAD */ |
| 1526 | |
| 1527 | /** |
Ladislav Michl | 02c2d6a | 2016-07-12 20:28:24 +0200 | [diff] [blame] | 1528 | * The mtdparts variable tends to be long. If we need to access it |
| 1529 | * before the env is relocated, then we need to use our own stack |
| 1530 | * buffer. gd->env_buf will be too small. |
| 1531 | * |
| 1532 | * @param buf temporary buffer pointer MTDPARTS_MAXLEN long |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1533 | * Return: mtdparts variable string, NULL if not found |
Ladislav Michl | 02c2d6a | 2016-07-12 20:28:24 +0200 | [diff] [blame] | 1534 | */ |
Simon Glass | da1a134 | 2017-08-03 12:22:15 -0600 | [diff] [blame] | 1535 | static const char *env_get_mtdparts(char *buf) |
Ladislav Michl | 02c2d6a | 2016-07-12 20:28:24 +0200 | [diff] [blame] | 1536 | { |
| 1537 | if (gd->flags & GD_FLG_ENV_READY) |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 1538 | return env_get("mtdparts"); |
| 1539 | if (env_get_f("mtdparts", buf, MTDPARTS_MAXLEN) != -1) |
Ladislav Michl | 02c2d6a | 2016-07-12 20:28:24 +0200 | [diff] [blame] | 1540 | return buf; |
| 1541 | return NULL; |
| 1542 | } |
| 1543 | |
| 1544 | /** |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1545 | * Accept character string describing mtd partitions and call device_parse() |
| 1546 | * for each entry. Add created devices to the global devices list. |
| 1547 | * |
| 1548 | * @param mtdparts string specifing mtd partitions |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1549 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1550 | */ |
| 1551 | static int parse_mtdparts(const char *const mtdparts) |
| 1552 | { |
Ladislav Michl | 760042a | 2016-07-12 20:28:23 +0200 | [diff] [blame] | 1553 | const char *p; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1554 | struct mtd_device *dev; |
| 1555 | int err = 1; |
Joe Hershberger | 3902c60 | 2013-04-08 10:32:50 +0000 | [diff] [blame] | 1556 | char tmp_parts[MTDPARTS_MAXLEN]; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1557 | |
Lothar Waßmann | 037166d | 2017-06-08 14:04:03 +0200 | [diff] [blame] | 1558 | debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", mtdparts); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1559 | |
| 1560 | /* delete all devices and partitions */ |
| 1561 | if (mtd_devices_init() != 0) { |
| 1562 | printf("could not initialise device list\n"); |
| 1563 | return err; |
| 1564 | } |
| 1565 | |
| 1566 | /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */ |
Simon Glass | da1a134 | 2017-08-03 12:22:15 -0600 | [diff] [blame] | 1567 | p = env_get_mtdparts(tmp_parts); |
Ladislav Michl | 760042a | 2016-07-12 20:28:23 +0200 | [diff] [blame] | 1568 | if (!p) |
| 1569 | p = mtdparts; |
| 1570 | |
Miquel Raynal | 24de4a5 | 2018-09-06 09:08:46 +0200 | [diff] [blame] | 1571 | /* Skip the useless prefix, if any */ |
| 1572 | if (strncmp(p, "mtdparts=", 9) == 0) |
| 1573 | p += 9; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1574 | |
Ladislav Michl | 760042a | 2016-07-12 20:28:23 +0200 | [diff] [blame] | 1575 | while (*p != '\0') { |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1576 | err = 1; |
| 1577 | if ((device_parse(p, &p, &dev) != 0) || (!dev)) |
| 1578 | break; |
| 1579 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 1580 | debug("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type), |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1581 | 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 Rini | 4a786c9 | 2017-08-14 20:42:27 -0400 | [diff] [blame] | 1593 | if (err == 1) { |
| 1594 | free(dev); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1595 | device_delall(&devices); |
Tom Rini | 4a786c9 | 2017-08-14 20:42:27 -0400 | [diff] [blame] | 1596 | } |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1597 | |
Ladislav Michl | 760042a | 2016-07-12 20:28:23 +0200 | [diff] [blame] | 1598 | return err; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1599 | } |
| 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 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1607 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1608 | */ |
| 1609 | static 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 Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 1618 | u64 size; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1619 | int ret = 1; |
| 1620 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 1621 | debug("\n---parse_mtdids---\nmtdids = %s\n\n", ids); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1622 | |
| 1623 | /* clean global mtdids list */ |
| 1624 | list_for_each_safe(entry, n, &mtdids) { |
| 1625 | id_tmp = list_entry(entry, struct mtdids, link); |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 1626 | debug("mtdids del: %d %d\n", id_tmp->type, id_tmp->num); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1627 | 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; |
Miquel Raynal | ceb9cae | 2018-09-06 09:08:45 +0200 | [diff] [blame] | 1636 | /* parse 'nor'|'nand'|'onenand'|'spi-nand'<dev-num> */ |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 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 Burton | 5f578df | 2013-09-04 15:16:57 +0100 | [diff] [blame] | 1692 | debug("+ id %s%d\t%16lld bytes\t%s\n", |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1693 | 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 Michl | 02c2d6a | 2016-07-12 20:28:24 +0200 | [diff] [blame] | 1712 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1713 | /** |
| 1714 | * Parse and initialize global mtdids mapping and create global |
| 1715 | * device/partition list. |
| 1716 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1717 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1718 | */ |
| 1719 | int mtdparts_init(void) |
| 1720 | { |
| 1721 | static int initialized = 0; |
| 1722 | const char *ids, *parts; |
| 1723 | const char *current_partition; |
| 1724 | int ids_changed; |
Tom Rini | 11d267f | 2017-08-20 20:05:40 -0400 | [diff] [blame] | 1725 | char tmp_ep[PARTITION_MAXLEN + 1]; |
Joe Hershberger | 3902c60 | 2013-04-08 10:32:50 +0000 | [diff] [blame] | 1726 | char tmp_parts[MTDPARTS_MAXLEN]; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1727 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 1728 | debug("\n---mtdparts_init---\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1729 | if (!initialized) { |
| 1730 | INIT_LIST_HEAD(&mtdids); |
| 1731 | INIT_LIST_HEAD(&devices); |
Tom Rini | 4a786c9 | 2017-08-14 20:42:27 -0400 | [diff] [blame] | 1732 | memset(last_ids, 0, sizeof(last_ids)); |
| 1733 | memset(last_parts, 0, sizeof(last_parts)); |
| 1734 | memset(last_partition, 0, sizeof(last_partition)); |
Ladislav Michl | 207350b | 2016-07-12 20:28:26 +0200 | [diff] [blame] | 1735 | #if defined(CONFIG_SYS_MTDPARTS_RUNTIME) |
| 1736 | board_mtdparts_default(&mtdids_default, &mtdparts_default); |
| 1737 | #endif |
Ladislav Michl | a91260e | 2016-07-12 20:28:25 +0200 | [diff] [blame] | 1738 | use_defaults = 1; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1739 | initialized = 1; |
| 1740 | } |
| 1741 | |
| 1742 | /* get variables */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 1743 | ids = env_get("mtdids"); |
Simon Glass | da1a134 | 2017-08-03 12:22:15 -0600 | [diff] [blame] | 1744 | parts = env_get_mtdparts(tmp_parts); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 1745 | current_partition = env_get("partition"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1746 | |
| 1747 | /* save it for later parsing, cannot rely on current partition pointer |
| 1748 | * as 'partition' variable may be updated during init */ |
Tom Rini | 11d267f | 2017-08-20 20:05:40 -0400 | [diff] [blame] | 1749 | memset(tmp_parts, 0, sizeof(tmp_parts)); |
Tom Rini | fd0dd1c | 2017-08-26 16:59:24 -0400 | [diff] [blame] | 1750 | memset(tmp_ep, 0, sizeof(tmp_ep)); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1751 | if (current_partition) |
| 1752 | strncpy(tmp_ep, current_partition, PARTITION_MAXLEN); |
| 1753 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 1754 | 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 Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1758 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 1759 | debug("last_partition : %s\n", last_partition); |
| 1760 | debug("env_partition : %s\n", current_partition); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1761 | |
Robert P. J. Day | 8c60f92 | 2016-05-04 04:47:31 -0400 | [diff] [blame] | 1762 | /* if mtdids variable is empty try to use defaults */ |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1763 | if (!ids) { |
| 1764 | if (mtdids_default) { |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 1765 | debug("mtdids variable not defined, using default\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1766 | ids = mtdids_default; |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 1767 | env_set("mtdids", (char *)ids); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1768 | } 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 Michl | a91260e | 2016-07-12 20:28:25 +0200 | [diff] [blame] | 1778 | /* 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 Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 1783 | if (env_set("mtdparts", (char *)parts) == 0) |
Ladislav Michl | a91260e | 2016-07-12 20:28:25 +0200 | [diff] [blame] | 1784 | use_defaults = 0; |
| 1785 | } else |
| 1786 | printf("mtdparts variable not set, see 'help mtdparts'\n"); |
| 1787 | } |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1788 | |
| 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 Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 1823 | current_mtd_dev = list_entry(devices.next, struct mtd_device, link); |
| 1824 | current_mtd_partnum = 0; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1825 | current_save(); |
| 1826 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 1827 | debug("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n", |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 1828 | MTD_DEV_TYPE(current_mtd_dev->id->type), |
| 1829 | current_mtd_dev->id->num, current_mtd_partnum); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1830 | } |
| 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 Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 1846 | debug("--- getting current partition: %s\n", tmp_ep); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1847 | |
| 1848 | if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) { |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 1849 | current_mtd_dev = cdev; |
| 1850 | current_mtd_partnum = pnum; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1851 | current_save(); |
| 1852 | } |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 1853 | } else if (env_get("partition") == NULL) { |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 1854 | debug("no partition variable set, setting...\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1855 | 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 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1867 | * Return: pointer to the part_info, NULL otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1868 | */ |
| 1869 | static 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 Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 1878 | debug("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n", |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1879 | 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 Meng | 7557405 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 1903 | /* U-Boot commands */ |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1904 | /***************************************************/ |
| 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 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1914 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1915 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1916 | static int do_chpart(struct cmd_tbl *cmdtp, int flag, int argc, |
| 1917 | char *const argv[]) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1918 | { |
| 1919 | /* command line only */ |
| 1920 | struct mtd_device *dev; |
| 1921 | struct part_info *part; |
| 1922 | u8 pnum; |
| 1923 | |
| 1924 | if (mtdparts_init() !=0) |
| 1925 | return 1; |
| 1926 | |
| 1927 | if (argc < 2) { |
| 1928 | printf("no partition id specified\n"); |
| 1929 | return 1; |
| 1930 | } |
| 1931 | |
| 1932 | if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0) |
| 1933 | return 1; |
| 1934 | |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 1935 | current_mtd_dev = dev; |
| 1936 | current_mtd_partnum = pnum; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1937 | current_save(); |
| 1938 | |
| 1939 | printf("partition changed to %s%d,%d\n", |
| 1940 | MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum); |
| 1941 | |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
| 1945 | /** |
| 1946 | * Routine implementing u-boot mtdparts command. Initialize/update default global |
| 1947 | * partition list and process user partition request (list, add, del). |
| 1948 | * |
| 1949 | * @param cmdtp command internal data |
| 1950 | * @param flag command flag |
| 1951 | * @param argc number of arguments supplied to the command |
| 1952 | * @param argv arguments list |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1953 | * Return: 0 on success, 1 otherwise |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1954 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1955 | static int do_mtdparts(struct cmd_tbl *cmdtp, int flag, int argc, |
| 1956 | char *const argv[]) |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1957 | { |
| 1958 | if (argc == 2) { |
| 1959 | if (strcmp(argv[1], "default") == 0) { |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 1960 | env_set("mtdids", NULL); |
| 1961 | env_set("mtdparts", NULL); |
| 1962 | env_set("partition", NULL); |
Ladislav Michl | a91260e | 2016-07-12 20:28:25 +0200 | [diff] [blame] | 1963 | use_defaults = 1; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1964 | |
| 1965 | mtdparts_init(); |
| 1966 | return 0; |
| 1967 | } else if (strcmp(argv[1], "delall") == 0) { |
| 1968 | /* this may be the first run, initialize lists if needed */ |
| 1969 | mtdparts_init(); |
| 1970 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 1971 | env_set("mtdparts", NULL); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1972 | |
| 1973 | /* mtd_devices_init() calls current_save() */ |
| 1974 | return mtd_devices_init(); |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | /* make sure we are in sync with env variables */ |
| 1979 | if (mtdparts_init() != 0) |
| 1980 | return 1; |
| 1981 | |
| 1982 | if (argc == 1) { |
| 1983 | list_partitions(); |
| 1984 | return 0; |
| 1985 | } |
| 1986 | |
| 1987 | /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */ |
Ben Gardiner | 5063d2e | 2010-08-31 17:48:04 -0400 | [diff] [blame] | 1988 | if (((argc == 5) || (argc == 6)) && (strncmp(argv[1], "add", 3) == 0)) { |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1989 | #define PART_ADD_DESC_MAXLEN 64 |
| 1990 | char tmpbuf[PART_ADD_DESC_MAXLEN]; |
Ben Gardiner | 5063d2e | 2010-08-31 17:48:04 -0400 | [diff] [blame] | 1991 | #if defined(CONFIG_CMD_MTDPARTS_SPREAD) |
| 1992 | struct mtd_info *mtd; |
| 1993 | uint64_t next_offset; |
| 1994 | #endif |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 1995 | u8 type, num, len; |
| 1996 | struct mtd_device *dev; |
| 1997 | struct mtd_device *dev_tmp; |
| 1998 | struct mtdids *id; |
| 1999 | struct part_info *p; |
| 2000 | |
| 2001 | if (mtd_id_parse(argv[2], NULL, &type, &num) != 0) |
| 2002 | return 1; |
| 2003 | |
| 2004 | if ((id = id_find(type, num)) == NULL) { |
| 2005 | printf("no such device %s defined in mtdids variable\n", argv[2]); |
| 2006 | return 1; |
| 2007 | } |
| 2008 | |
| 2009 | len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */ |
| 2010 | len += strlen(argv[3]); /* size@offset */ |
| 2011 | len += strlen(argv[4]) + 2; /* '(' name ')' */ |
| 2012 | if (argv[5] && (strlen(argv[5]) == 2)) |
| 2013 | len += 2; /* 'ro' */ |
| 2014 | |
| 2015 | if (len >= PART_ADD_DESC_MAXLEN) { |
| 2016 | printf("too long partition description\n"); |
| 2017 | return 1; |
| 2018 | } |
| 2019 | sprintf(tmpbuf, "%s:%s(%s)%s", |
| 2020 | id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : ""); |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 2021 | debug("add tmpbuf: %s\n", tmpbuf); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2022 | |
| 2023 | if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev)) |
| 2024 | return 1; |
| 2025 | |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 2026 | debug("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type), |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2027 | dev->id->num, dev->id->mtd_id); |
| 2028 | |
Ben Gardiner | 5063d2e | 2010-08-31 17:48:04 -0400 | [diff] [blame] | 2029 | p = list_entry(dev->parts.next, struct part_info, link); |
| 2030 | |
| 2031 | #if defined(CONFIG_CMD_MTDPARTS_SPREAD) |
| 2032 | if (get_mtd_info(dev->id->type, dev->id->num, &mtd)) |
| 2033 | return 1; |
| 2034 | |
| 2035 | if (!strcmp(&argv[1][3], ".spread")) { |
| 2036 | spread_partition(mtd, p, &next_offset); |
Steve Rae | 6eb14ff | 2016-06-29 13:50:59 -0700 | [diff] [blame] | 2037 | debug("increased %s to %llu bytes\n", p->name, p->size); |
Ben Gardiner | 5063d2e | 2010-08-31 17:48:04 -0400 | [diff] [blame] | 2038 | } |
| 2039 | #endif |
| 2040 | |
| 2041 | dev_tmp = device_find(dev->id->type, dev->id->num); |
| 2042 | if (dev_tmp == NULL) { |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2043 | device_add(dev); |
Ben Gardiner | 5063d2e | 2010-08-31 17:48:04 -0400 | [diff] [blame] | 2044 | } else if (part_add(dev_tmp, p) != 0) { |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2045 | /* merge new partition with existing ones*/ |
Ben Gardiner | 5063d2e | 2010-08-31 17:48:04 -0400 | [diff] [blame] | 2046 | device_del(dev); |
| 2047 | return 1; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) { |
Robert P. J. Day | 02ce716 | 2013-02-04 13:51:10 +0000 | [diff] [blame] | 2051 | printf("generated mtdparts too long, resetting to null\n"); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2052 | return 1; |
| 2053 | } |
| 2054 | |
| 2055 | return 0; |
| 2056 | } |
| 2057 | |
| 2058 | /* mtdparts del part-id */ |
| 2059 | if ((argc == 3) && (strcmp(argv[1], "del") == 0)) { |
Wolfgang Denk | 58daebd | 2010-04-28 10:58:10 +0200 | [diff] [blame] | 2060 | debug("del: part-id = %s\n", argv[2]); |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2061 | |
| 2062 | return delete_partition(argv[2]); |
| 2063 | } |
| 2064 | |
Ben Gardiner | e6064f5 | 2010-08-31 17:48:03 -0400 | [diff] [blame] | 2065 | #if defined(CONFIG_CMD_MTDPARTS_SPREAD) |
| 2066 | if ((argc == 2) && (strcmp(argv[1], "spread") == 0)) |
| 2067 | return spread_partitions(); |
| 2068 | #endif /* CONFIG_CMD_MTDPARTS_SPREAD */ |
| 2069 | |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 2070 | return CMD_RET_USAGE; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | /***************************************************/ |
| 2074 | U_BOOT_CMD( |
| 2075 | chpart, 2, 0, do_chpart, |
Adarsh Babu Kalepalli | 6520860 | 2021-05-24 13:35:39 +0530 | [diff] [blame] | 2076 | "change active partition of a MTD device", |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2077 | "part-id\n" |
Adarsh Babu Kalepalli | 6520860 | 2021-05-24 13:35:39 +0530 | [diff] [blame] | 2078 | " - change active partition (e.g. part-id = nand0,1) of a MTD device" |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2079 | ); |
| 2080 | |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 2081 | U_BOOT_LONGHELP(mtdparts, |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2082 | "\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 Gardiner | 5063d2e | 2010-08-31 17:48:04 -0400 | [diff] [blame] | 2090 | #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 Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2094 | "mtdparts default\n" |
Ben Gardiner | e6064f5 | 2010-08-31 17:48:03 -0400 | [diff] [blame] | 2095 | " - 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 Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2104 | "-----\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" |
Miquel Raynal | ceb9cae | 2018-09-06 09:08:45 +0200 | [diff] [blame] | 2112 | "<dev-id> := 'nand'|'nor'|'onenand'|'spi-nand'<dev-num>\n" |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 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" |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 2123 | "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)"); |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 2124 | |
| 2125 | U_BOOT_CMD( |
| 2126 | mtdparts, 6, 0, do_mtdparts, |
| 2127 | "define flash/nand partitions", mtdparts_help_text |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 2128 | ); |
| 2129 | /***************************************************/ |