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