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