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