Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 7e5a16c | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2014 |
| 4 | * Heiko Schocher, DENX Software Engineering, hs@denx.de. |
Heiko Schocher | 7e5a16c | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 5 | */ |
Simon Glass | db22961 | 2019-08-01 09:46:42 -0600 | [diff] [blame] | 6 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 8 | #include <malloc.h> |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 9 | #include <dm/device.h> |
| 10 | #include <dm/uclass-internal.h> |
Marek Behún | 3246ae2 | 2021-05-26 14:08:22 +0200 | [diff] [blame] | 11 | #include <dm/uclass.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 12 | #include <linux/err.h> |
Heiko Schocher | 7e5a16c | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 13 | #include <linux/mtd/mtd.h> |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 14 | #include <linux/mtd/partitions.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 16 | #include <mtd.h> |
| 17 | |
| 18 | #define MTD_NAME_MAX_LEN 20 |
| 19 | |
Boris Brezillon | dde6ec7 | 2018-12-02 10:54:26 +0100 | [diff] [blame] | 20 | void board_mtdparts_default(const char **mtdids, const char **mtdparts); |
| 21 | |
| 22 | static const char *get_mtdids(void) |
| 23 | { |
| 24 | __maybe_unused const char *mtdparts = NULL; |
| 25 | const char *mtdids = env_get("mtdids"); |
| 26 | |
| 27 | if (mtdids) |
| 28 | return mtdids; |
| 29 | |
| 30 | #if defined(CONFIG_SYS_MTDPARTS_RUNTIME) |
| 31 | board_mtdparts_default(&mtdids, &mtdparts); |
Boris Brezillon | dde6ec7 | 2018-12-02 10:54:26 +0100 | [diff] [blame] | 32 | #elif defined(CONFIG_MTDIDS_DEFAULT) |
| 33 | mtdids = CONFIG_MTDIDS_DEFAULT; |
| 34 | #endif |
| 35 | |
| 36 | if (mtdids) |
| 37 | env_set("mtdids", mtdids); |
| 38 | |
| 39 | return mtdids; |
| 40 | } |
Miquel Raynal | 1a08e99 | 2018-09-29 12:58:26 +0200 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * mtd_search_alternate_name - Search an alternate name for @mtdname thanks to |
| 44 | * the mtdids legacy environment variable. |
| 45 | * |
| 46 | * The mtdids string is a list of comma-separated 'dev_id=mtd_id' tupples. |
| 47 | * Check if one of the mtd_id matches mtdname, in this case save dev_id in |
| 48 | * altname. |
| 49 | * |
| 50 | * @mtdname: Current MTD device name |
| 51 | * @altname: Alternate name to return |
| 52 | * @max_len: Length of the alternate name buffer |
| 53 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 54 | * Return: 0 on success, an error otherwise. |
Miquel Raynal | 1a08e99 | 2018-09-29 12:58:26 +0200 | [diff] [blame] | 55 | */ |
| 56 | int mtd_search_alternate_name(const char *mtdname, char *altname, |
| 57 | unsigned int max_len) |
| 58 | { |
| 59 | const char *mtdids, *equal, *comma, *dev_id, *mtd_id; |
| 60 | int dev_id_len, mtd_id_len; |
| 61 | |
Boris Brezillon | dde6ec7 | 2018-12-02 10:54:26 +0100 | [diff] [blame] | 62 | mtdids = get_mtdids(); |
Miquel Raynal | 1a08e99 | 2018-09-29 12:58:26 +0200 | [diff] [blame] | 63 | if (!mtdids) |
| 64 | return -EINVAL; |
| 65 | |
| 66 | do { |
| 67 | /* Find the '=' sign */ |
| 68 | dev_id = mtdids; |
| 69 | equal = strchr(dev_id, '='); |
| 70 | if (!equal) |
| 71 | break; |
| 72 | dev_id_len = equal - mtdids; |
| 73 | mtd_id = equal + 1; |
| 74 | |
| 75 | /* Find the end of the tupple */ |
| 76 | comma = strchr(mtdids, ','); |
| 77 | if (comma) |
| 78 | mtd_id_len = comma - mtd_id; |
| 79 | else |
| 80 | mtd_id_len = &mtdids[strlen(mtdids)] - mtd_id + 1; |
| 81 | |
| 82 | if (!dev_id_len || !mtd_id_len) |
| 83 | return -EINVAL; |
| 84 | |
| 85 | if (dev_id_len + 1 > max_len) |
| 86 | continue; |
| 87 | |
| 88 | /* Compare the name we search with the current mtd_id */ |
| 89 | if (!strncmp(mtdname, mtd_id, mtd_id_len)) { |
| 90 | strncpy(altname, dev_id, dev_id_len); |
| 91 | altname[dev_id_len] = 0; |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | /* Go to the next tupple */ |
| 97 | mtdids = comma + 1; |
| 98 | } while (comma); |
| 99 | |
| 100 | return -EINVAL; |
| 101 | } |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 102 | |
Miquel Raynal | a903be4 | 2019-10-03 19:50:04 +0200 | [diff] [blame] | 103 | #if IS_ENABLED(CONFIG_DM_MTD) |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 104 | static void mtd_probe_uclass_mtd_devs(void) |
| 105 | { |
| 106 | struct udevice *dev; |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 107 | |
Marek Behún | 3246ae2 | 2021-05-26 14:08:22 +0200 | [diff] [blame] | 108 | uclass_foreach_dev_probe(UCLASS_MTD, dev) |
| 109 | ; |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 110 | } |
| 111 | #else |
| 112 | static void mtd_probe_uclass_mtd_devs(void) { } |
| 113 | #endif |
| 114 | |
Marek Behún | db3ce1f | 2021-05-26 14:08:23 +0200 | [diff] [blame] | 115 | #if IS_ENABLED(CONFIG_DM_SPI_FLASH) && IS_ENABLED(CONFIG_SPI_FLASH_MTD) |
| 116 | static void mtd_probe_uclass_spi_nor_devs(void) |
| 117 | { |
| 118 | struct udevice *dev; |
| 119 | |
| 120 | uclass_foreach_dev_probe(UCLASS_SPI_FLASH, dev) |
| 121 | ; |
| 122 | } |
| 123 | #else |
| 124 | static void mtd_probe_uclass_spi_nor_devs(void) { } |
| 125 | #endif |
| 126 | |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 127 | #if defined(CONFIG_MTD_PARTITIONS) |
Boris Brezillon | 7899afc | 2018-11-13 12:43:09 +0100 | [diff] [blame] | 128 | |
| 129 | #define MTDPARTS_MAXLEN 512 |
| 130 | |
| 131 | static const char *get_mtdparts(void) |
| 132 | { |
| 133 | __maybe_unused const char *mtdids = NULL; |
| 134 | static char tmp_parts[MTDPARTS_MAXLEN]; |
Boris Brezillon | 7899afc | 2018-11-13 12:43:09 +0100 | [diff] [blame] | 135 | const char *mtdparts = NULL; |
| 136 | |
| 137 | if (gd->flags & GD_FLG_ENV_READY) |
| 138 | mtdparts = env_get("mtdparts"); |
| 139 | else if (env_get_f("mtdparts", tmp_parts, sizeof(tmp_parts)) != -1) |
| 140 | mtdparts = tmp_parts; |
| 141 | |
Patrice Chotard | a3cbb93 | 2019-01-23 18:12:48 +0100 | [diff] [blame] | 142 | if (mtdparts) |
Boris Brezillon | 7899afc | 2018-11-13 12:43:09 +0100 | [diff] [blame] | 143 | return mtdparts; |
| 144 | |
| 145 | #if defined(CONFIG_SYS_MTDPARTS_RUNTIME) |
| 146 | board_mtdparts_default(&mtdids, &mtdparts); |
Boris Brezillon | 7899afc | 2018-11-13 12:43:09 +0100 | [diff] [blame] | 147 | #elif defined(CONFIG_MTDPARTS_DEFAULT) |
| 148 | mtdparts = CONFIG_MTDPARTS_DEFAULT; |
| 149 | #endif |
| 150 | |
| 151 | if (mtdparts) |
| 152 | env_set("mtdparts", mtdparts); |
| 153 | |
Boris Brezillon | 7899afc | 2018-11-13 12:43:09 +0100 | [diff] [blame] | 154 | return mtdparts; |
| 155 | } |
| 156 | |
Boris Brezillon | 7dfad31 | 2018-12-02 10:54:30 +0100 | [diff] [blame] | 157 | static int mtd_del_parts(struct mtd_info *mtd, bool quiet) |
| 158 | { |
| 159 | int ret; |
| 160 | |
| 161 | if (!mtd_has_partitions(mtd)) |
| 162 | return 0; |
| 163 | |
| 164 | /* do not delete partitions if they are in use. */ |
| 165 | if (mtd_partitions_used(mtd)) { |
| 166 | if (!quiet) |
| 167 | printf("\"%s\" partitions still in use, can't delete them\n", |
| 168 | mtd->name); |
| 169 | return -EACCES; |
| 170 | } |
| 171 | |
| 172 | ret = del_mtd_partitions(mtd); |
| 173 | if (ret) |
| 174 | return ret; |
| 175 | |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | static bool mtd_del_all_parts_failed; |
| 180 | |
| 181 | static void mtd_del_all_parts(void) |
| 182 | { |
| 183 | struct mtd_info *mtd; |
| 184 | int ret = 0; |
| 185 | |
| 186 | mtd_del_all_parts_failed = false; |
| 187 | |
| 188 | /* |
| 189 | * It is not safe to remove entries from the mtd_for_each_device loop |
| 190 | * as it uses idr indexes and the partitions removal is done in bulk |
| 191 | * (all partitions of one device at the same time), so break and |
| 192 | * iterate from start each time a new partition is found and deleted. |
| 193 | */ |
| 194 | do { |
| 195 | mtd_for_each_device(mtd) { |
| 196 | ret = mtd_del_parts(mtd, false); |
| 197 | if (ret > 0) |
| 198 | break; |
| 199 | else if (ret < 0) |
| 200 | mtd_del_all_parts_failed = true; |
| 201 | } |
| 202 | } while (ret > 0); |
| 203 | } |
| 204 | |
Marek Behún | 544a8c9 | 2021-05-26 14:08:19 +0200 | [diff] [blame] | 205 | static int parse_mtdparts(const char *mtdparts, const char *mtdids) |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 206 | { |
Marek Behún | 544a8c9 | 2021-05-26 14:08:19 +0200 | [diff] [blame] | 207 | const char *mtdparts_next; |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 208 | struct mtd_info *mtd; |
| 209 | |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 210 | /* Start the parsing by ignoring the extra 'mtdparts=' prefix, if any */ |
Boris Brezillon | 1d2c149 | 2018-12-02 10:54:27 +0100 | [diff] [blame] | 211 | if (!strncmp(mtdparts, "mtdparts=", sizeof("mtdparts=") - 1)) |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 212 | mtdparts += 9; |
| 213 | |
| 214 | /* For each MTD device in mtdparts */ |
Boris Brezillon | 46ba2e8 | 2018-12-02 10:54:29 +0100 | [diff] [blame] | 215 | for (; mtdparts[0] != '\0'; mtdparts = mtdparts_next) { |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 216 | char mtd_name[MTD_NAME_MAX_LEN], *colon; |
| 217 | struct mtd_partition *parts; |
Boris Brezillon | a24d985 | 2018-12-02 10:54:28 +0100 | [diff] [blame] | 218 | unsigned int mtd_name_len; |
| 219 | int nparts, ret; |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 220 | |
Boris Brezillon | 46ba2e8 | 2018-12-02 10:54:29 +0100 | [diff] [blame] | 221 | mtdparts_next = strchr(mtdparts, ';'); |
| 222 | if (!mtdparts_next) |
| 223 | mtdparts_next = mtdparts + strlen(mtdparts); |
| 224 | else |
| 225 | mtdparts_next++; |
| 226 | |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 227 | colon = strchr(mtdparts, ':'); |
Boris Brezillon | 46ba2e8 | 2018-12-02 10:54:29 +0100 | [diff] [blame] | 228 | if (colon > mtdparts_next) |
| 229 | colon = NULL; |
| 230 | |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 231 | if (!colon) { |
| 232 | printf("Wrong mtdparts: %s\n", mtdparts); |
| 233 | return -EINVAL; |
| 234 | } |
| 235 | |
Boris Brezillon | a24d985 | 2018-12-02 10:54:28 +0100 | [diff] [blame] | 236 | mtd_name_len = (unsigned int)(colon - mtdparts); |
| 237 | if (mtd_name_len + 1 > sizeof(mtd_name)) { |
| 238 | printf("MTD name too long: %s\n", mtdparts); |
| 239 | return -EINVAL; |
| 240 | } |
| 241 | |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 242 | strncpy(mtd_name, mtdparts, mtd_name_len); |
| 243 | mtd_name[mtd_name_len] = '\0'; |
| 244 | /* Move the pointer forward (including the ':') */ |
| 245 | mtdparts += mtd_name_len + 1; |
| 246 | mtd = get_mtd_device_nm(mtd_name); |
| 247 | if (IS_ERR_OR_NULL(mtd)) { |
| 248 | char linux_name[MTD_NAME_MAX_LEN]; |
| 249 | |
| 250 | /* |
| 251 | * The MTD device named "mtd_name" does not exist. Try |
| 252 | * to find a correspondance with an MTD device having |
| 253 | * the same type and number as defined in the mtdids. |
| 254 | */ |
| 255 | debug("No device named %s\n", mtd_name); |
| 256 | ret = mtd_search_alternate_name(mtd_name, linux_name, |
| 257 | MTD_NAME_MAX_LEN); |
| 258 | if (!ret) |
| 259 | mtd = get_mtd_device_nm(linux_name); |
| 260 | |
| 261 | /* |
| 262 | * If no device could be found, move the mtdparts |
| 263 | * pointer forward until the next set of partitions. |
| 264 | */ |
| 265 | if (ret || IS_ERR_OR_NULL(mtd)) { |
| 266 | printf("Could not find a valid device for %s\n", |
| 267 | mtd_name); |
Boris Brezillon | 46ba2e8 | 2018-12-02 10:54:29 +0100 | [diff] [blame] | 268 | mtdparts = mtdparts_next; |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 269 | continue; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* |
Boris Brezillon | 7dfad31 | 2018-12-02 10:54:30 +0100 | [diff] [blame] | 274 | * Call mtd_del_parts() again, even if it's already been called |
| 275 | * in mtd_del_all_parts(). We need to know if old partitions are |
| 276 | * still around (because they are still being used by someone), |
| 277 | * and if they are, we shouldn't create new partitions, so just |
| 278 | * skip this MTD device and try the next one. |
| 279 | */ |
| 280 | ret = mtd_del_parts(mtd, true); |
| 281 | if (ret < 0) |
| 282 | continue; |
| 283 | |
| 284 | /* |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 285 | * Parse the MTD device partitions. It will update the mtdparts |
| 286 | * pointer, create an array of parts (that must be freed), and |
| 287 | * return the number of partition structures in the array. |
| 288 | */ |
| 289 | ret = mtd_parse_partitions(mtd, &mtdparts, &parts, &nparts); |
| 290 | if (ret) { |
| 291 | printf("Could not parse device %s\n", mtd->name); |
| 292 | put_mtd_device(mtd); |
| 293 | return -EINVAL; |
| 294 | } |
| 295 | |
| 296 | if (!nparts) |
| 297 | continue; |
| 298 | |
| 299 | /* Create the new MTD partitions */ |
| 300 | add_mtd_partitions(mtd, parts, nparts); |
| 301 | |
| 302 | /* Free the structures allocated during the parsing */ |
| 303 | mtd_free_parsed_partitions(parts, nparts); |
| 304 | |
| 305 | put_mtd_device(mtd); |
| 306 | } |
| 307 | |
Marek Behún | 544a8c9 | 2021-05-26 14:08:19 +0200 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | int mtd_probe_devices(void) |
| 312 | { |
| 313 | static char *old_mtdparts; |
| 314 | static char *old_mtdids; |
| 315 | const char *mtdparts = get_mtdparts(); |
| 316 | const char *mtdids = get_mtdids(); |
| 317 | struct mtd_info *mtd; |
| 318 | |
| 319 | mtd_probe_uclass_mtd_devs(); |
Marek Behún | db3ce1f | 2021-05-26 14:08:23 +0200 | [diff] [blame] | 320 | mtd_probe_uclass_spi_nor_devs(); |
Marek Behún | 544a8c9 | 2021-05-26 14:08:19 +0200 | [diff] [blame] | 321 | |
| 322 | /* |
| 323 | * Check if mtdparts/mtdids changed, if the MTD dev list was updated |
| 324 | * or if our previous attempt to delete existing partititions failed. |
| 325 | * In any of these cases we want to update the partitions, otherwise, |
| 326 | * everything is up-to-date and we can return 0 directly. |
| 327 | */ |
| 328 | if ((!mtdparts && !old_mtdparts && !mtdids && !old_mtdids) || |
| 329 | (mtdparts && old_mtdparts && mtdids && old_mtdids && |
| 330 | !mtd_dev_list_updated() && !mtd_del_all_parts_failed && |
| 331 | !strcmp(mtdparts, old_mtdparts) && |
| 332 | !strcmp(mtdids, old_mtdids))) |
| 333 | return 0; |
| 334 | |
| 335 | /* Update the local copy of mtdparts */ |
| 336 | free(old_mtdparts); |
| 337 | free(old_mtdids); |
| 338 | old_mtdparts = strdup(mtdparts); |
| 339 | old_mtdids = strdup(mtdids); |
| 340 | |
| 341 | /* |
| 342 | * Remove all old parts. Note that partition removal can fail in case |
| 343 | * one of the partition is still being used by an MTD user, so this |
| 344 | * does not guarantee that all old partitions are gone. |
| 345 | */ |
| 346 | mtd_del_all_parts(); |
| 347 | |
| 348 | /* |
| 349 | * Call mtd_dev_list_updated() to clear updates generated by our own |
| 350 | * parts removal loop. |
| 351 | */ |
| 352 | mtd_dev_list_updated(); |
| 353 | |
| 354 | /* If both mtdparts and mtdids are non-empty, parse */ |
| 355 | if (mtdparts && mtdids) { |
| 356 | if (parse_mtdparts(mtdparts, mtdids) < 0) |
| 357 | printf("Failed parsing MTD partitions from mtdparts!\n"); |
| 358 | } |
| 359 | |
| 360 | /* Fallback to OF partitions */ |
| 361 | mtd_for_each_device(mtd) { |
| 362 | if (list_empty(&mtd->partitions)) { |
| 363 | if (add_mtd_partitions_of(mtd) < 0) |
| 364 | printf("Failed parsing MTD %s OF partitions!\n", |
| 365 | mtd->name); |
| 366 | } |
| 367 | } |
| 368 | |
Boris Brezillon | b132875 | 2018-12-02 10:54:23 +0100 | [diff] [blame] | 369 | /* |
| 370 | * Call mtd_dev_list_updated() to clear updates generated by our own |
| 371 | * parts registration loop. |
| 372 | */ |
| 373 | mtd_dev_list_updated(); |
| 374 | |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 375 | return 0; |
| 376 | } |
| 377 | #else |
| 378 | int mtd_probe_devices(void) |
| 379 | { |
| 380 | mtd_probe_uclass_mtd_devs(); |
Marek Behún | db3ce1f | 2021-05-26 14:08:23 +0200 | [diff] [blame] | 381 | mtd_probe_uclass_spi_nor_devs(); |
Miquel Raynal | 741f4c7 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | #endif /* defined(CONFIG_MTD_PARTITIONS) */ |