1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * (C) Copyright 2014 4 * Heiko Schocher, DENX Software Engineering, hs@denx.de. 5 */ 6 #include <common.h> 7 #include <dm/device.h> 8 #include <dm/uclass-internal.h> 9 #include <jffs2/jffs2.h> /* LEGACY */ 10 #include <linux/mtd/mtd.h> 11 #include <linux/mtd/partitions.h> 12 #include <mtd.h> 13 14 #define MTD_NAME_MAX_LEN 20 15 16 void board_mtdparts_default(const char **mtdids, const char **mtdparts); 17 18 static const char *get_mtdids(void) 19 { 20 __maybe_unused const char *mtdparts = NULL; 21 const char *mtdids = env_get("mtdids"); 22 23 if (mtdids) 24 return mtdids; 25 26 #if defined(CONFIG_SYS_MTDPARTS_RUNTIME) 27 board_mtdparts_default(&mtdids, &mtdparts); 28 #elif defined(MTDIDS_DEFAULT) 29 mtdids = MTDIDS_DEFAULT; 30 #elif defined(CONFIG_MTDIDS_DEFAULT) 31 mtdids = CONFIG_MTDIDS_DEFAULT; 32 #endif 33 34 if (mtdids) 35 env_set("mtdids", mtdids); 36 37 return mtdids; 38 } 39 40 /** 41 * mtd_search_alternate_name - Search an alternate name for @mtdname thanks to 42 * the mtdids legacy environment variable. 43 * 44 * The mtdids string is a list of comma-separated 'dev_id=mtd_id' tupples. 45 * Check if one of the mtd_id matches mtdname, in this case save dev_id in 46 * altname. 47 * 48 * @mtdname: Current MTD device name 49 * @altname: Alternate name to return 50 * @max_len: Length of the alternate name buffer 51 * 52 * @return 0 on success, an error otherwise. 53 */ 54 int mtd_search_alternate_name(const char *mtdname, char *altname, 55 unsigned int max_len) 56 { 57 const char *mtdids, *equal, *comma, *dev_id, *mtd_id; 58 int dev_id_len, mtd_id_len; 59 60 mtdids = get_mtdids(); 61 if (!mtdids) 62 return -EINVAL; 63 64 do { 65 /* Find the '=' sign */ 66 dev_id = mtdids; 67 equal = strchr(dev_id, '='); 68 if (!equal) 69 break; 70 dev_id_len = equal - mtdids; 71 mtd_id = equal + 1; 72 73 /* Find the end of the tupple */ 74 comma = strchr(mtdids, ','); 75 if (comma) 76 mtd_id_len = comma - mtd_id; 77 else 78 mtd_id_len = &mtdids[strlen(mtdids)] - mtd_id + 1; 79 80 if (!dev_id_len || !mtd_id_len) 81 return -EINVAL; 82 83 if (dev_id_len + 1 > max_len) 84 continue; 85 86 /* Compare the name we search with the current mtd_id */ 87 if (!strncmp(mtdname, mtd_id, mtd_id_len)) { 88 strncpy(altname, dev_id, dev_id_len); 89 altname[dev_id_len] = 0; 90 91 return 0; 92 } 93 94 /* Go to the next tupple */ 95 mtdids = comma + 1; 96 } while (comma); 97 98 return -EINVAL; 99 } 100 101 #if IS_ENABLED(CONFIG_MTD) 102 static void mtd_probe_uclass_mtd_devs(void) 103 { 104 struct udevice *dev; 105 int idx = 0; 106 107 /* Probe devices with DM compliant drivers */ 108 while (!uclass_find_device(UCLASS_MTD, idx, &dev) && dev) { 109 mtd_probe(dev); 110 idx++; 111 } 112 } 113 #else 114 static void mtd_probe_uclass_mtd_devs(void) { } 115 #endif 116 117 #if defined(CONFIG_MTD_PARTITIONS) 118 119 #define MTDPARTS_MAXLEN 512 120 121 static const char *get_mtdparts(void) 122 { 123 __maybe_unused const char *mtdids = NULL; 124 static char tmp_parts[MTDPARTS_MAXLEN]; 125 static bool use_defaults = true; 126 const char *mtdparts = NULL; 127 128 if (gd->flags & GD_FLG_ENV_READY) 129 mtdparts = env_get("mtdparts"); 130 else if (env_get_f("mtdparts", tmp_parts, sizeof(tmp_parts)) != -1) 131 mtdparts = tmp_parts; 132 133 if (mtdparts || !use_defaults) 134 return mtdparts; 135 136 #if defined(CONFIG_SYS_MTDPARTS_RUNTIME) 137 board_mtdparts_default(&mtdids, &mtdparts); 138 #elif defined(MTDPARTS_DEFAULT) 139 mtdparts = MTDPARTS_DEFAULT; 140 #elif defined(CONFIG_MTDPARTS_DEFAULT) 141 mtdparts = CONFIG_MTDPARTS_DEFAULT; 142 #endif 143 144 if (mtdparts) 145 env_set("mtdparts", mtdparts); 146 147 use_defaults = false; 148 149 return mtdparts; 150 } 151 152 int mtd_probe_devices(void) 153 { 154 static char *old_mtdparts; 155 static char *old_mtdids; 156 const char *mtdparts = get_mtdparts(); 157 const char *mtdids = get_mtdids(); 158 const char *mtdparts_next = mtdparts; 159 bool remaining_partitions = true; 160 struct mtd_info *mtd; 161 162 mtd_probe_uclass_mtd_devs(); 163 164 /* 165 * Check if mtdparts/mtdids changed or if the MTD dev list was updated 166 * since last call, otherwise: exit 167 */ 168 if ((!mtdparts && !old_mtdparts && !mtdids && !old_mtdids) || 169 (mtdparts && old_mtdparts && mtdids && old_mtdids && 170 !mtd_dev_list_updated() && 171 !strcmp(mtdparts, old_mtdparts) && 172 !strcmp(mtdids, old_mtdids))) 173 return 0; 174 175 /* Update the local copy of mtdparts */ 176 free(old_mtdparts); 177 free(old_mtdids); 178 old_mtdparts = strdup(mtdparts); 179 old_mtdids = strdup(mtdids); 180 181 /* If at least one partition is still in use, do not delete anything */ 182 mtd_for_each_device(mtd) { 183 if (mtd->usecount) { 184 printf("Partition \"%s\" already in use, aborting\n", 185 mtd->name); 186 return -EACCES; 187 } 188 } 189 190 /* 191 * Everything looks clear, remove all partitions. It is not safe to 192 * remove entries from the mtd_for_each_device loop as it uses idr 193 * indexes and the partitions removal is done in bulk (all partitions of 194 * one device at the same time), so break and iterate from start each 195 * time a new partition is found and deleted. 196 */ 197 while (remaining_partitions) { 198 remaining_partitions = false; 199 mtd_for_each_device(mtd) { 200 if (!mtd_is_partition(mtd) && mtd_has_partitions(mtd)) { 201 del_mtd_partitions(mtd); 202 remaining_partitions = true; 203 break; 204 } 205 } 206 } 207 208 /* 209 * Call mtd_dev_list_updated() to clear updates generated by our own 210 * parts removal loop. 211 */ 212 mtd_dev_list_updated(); 213 214 /* If either mtdparts or mtdids is empty, then exit */ 215 if (!mtdparts || !mtdids) 216 return 0; 217 218 /* Start the parsing by ignoring the extra 'mtdparts=' prefix, if any */ 219 if (!strncmp(mtdparts, "mtdparts=", sizeof("mtdparts=") - 1)) 220 mtdparts += 9; 221 222 /* For each MTD device in mtdparts */ 223 for (; mtdparts[0] != '\0'; mtdparts = mtdparts_next) { 224 char mtd_name[MTD_NAME_MAX_LEN], *colon; 225 struct mtd_partition *parts; 226 unsigned int mtd_name_len; 227 int nparts, ret; 228 229 mtdparts_next = strchr(mtdparts, ';'); 230 if (!mtdparts_next) 231 mtdparts_next = mtdparts + strlen(mtdparts); 232 else 233 mtdparts_next++; 234 235 colon = strchr(mtdparts, ':'); 236 if (colon > mtdparts_next) 237 colon = NULL; 238 239 if (!colon) { 240 printf("Wrong mtdparts: %s\n", mtdparts); 241 return -EINVAL; 242 } 243 244 mtd_name_len = (unsigned int)(colon - mtdparts); 245 if (mtd_name_len + 1 > sizeof(mtd_name)) { 246 printf("MTD name too long: %s\n", mtdparts); 247 return -EINVAL; 248 } 249 250 strncpy(mtd_name, mtdparts, mtd_name_len); 251 mtd_name[mtd_name_len] = '\0'; 252 /* Move the pointer forward (including the ':') */ 253 mtdparts += mtd_name_len + 1; 254 mtd = get_mtd_device_nm(mtd_name); 255 if (IS_ERR_OR_NULL(mtd)) { 256 char linux_name[MTD_NAME_MAX_LEN]; 257 258 /* 259 * The MTD device named "mtd_name" does not exist. Try 260 * to find a correspondance with an MTD device having 261 * the same type and number as defined in the mtdids. 262 */ 263 debug("No device named %s\n", mtd_name); 264 ret = mtd_search_alternate_name(mtd_name, linux_name, 265 MTD_NAME_MAX_LEN); 266 if (!ret) 267 mtd = get_mtd_device_nm(linux_name); 268 269 /* 270 * If no device could be found, move the mtdparts 271 * pointer forward until the next set of partitions. 272 */ 273 if (ret || IS_ERR_OR_NULL(mtd)) { 274 printf("Could not find a valid device for %s\n", 275 mtd_name); 276 mtdparts = mtdparts_next; 277 continue; 278 } 279 } 280 281 /* 282 * Parse the MTD device partitions. It will update the mtdparts 283 * pointer, create an array of parts (that must be freed), and 284 * return the number of partition structures in the array. 285 */ 286 ret = mtd_parse_partitions(mtd, &mtdparts, &parts, &nparts); 287 if (ret) { 288 printf("Could not parse device %s\n", mtd->name); 289 put_mtd_device(mtd); 290 return -EINVAL; 291 } 292 293 if (!nparts) 294 continue; 295 296 /* Create the new MTD partitions */ 297 add_mtd_partitions(mtd, parts, nparts); 298 299 /* Free the structures allocated during the parsing */ 300 mtd_free_parsed_partitions(parts, nparts); 301 302 put_mtd_device(mtd); 303 } 304 305 /* 306 * Call mtd_dev_list_updated() to clear updates generated by our own 307 * parts registration loop. 308 */ 309 mtd_dev_list_updated(); 310 311 return 0; 312 } 313 #else 314 int mtd_probe_devices(void) 315 { 316 mtd_probe_uclass_mtd_devs(); 317 318 return 0; 319 } 320 #endif /* defined(CONFIG_MTD_PARTITIONS) */ 321 322 /* Legacy */ 323 324 static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size, 325 loff_t *maxsize, int devtype) 326 { 327 #ifdef CONFIG_CMD_MTDPARTS 328 struct mtd_device *dev; 329 struct part_info *part; 330 u8 pnum; 331 int ret; 332 333 ret = mtdparts_init(); 334 if (ret) 335 return ret; 336 337 ret = find_dev_and_part(partname, &dev, &pnum, &part); 338 if (ret) 339 return ret; 340 341 if (dev->id->type != devtype) { 342 printf("not same typ %d != %d\n", dev->id->type, devtype); 343 return -1; 344 } 345 346 *off = part->offset; 347 *size = part->size; 348 *maxsize = part->size; 349 *idx = dev->id->num; 350 351 return 0; 352 #else 353 puts("mtdparts support missing.\n"); 354 return -1; 355 #endif 356 } 357 358 int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size, 359 loff_t *maxsize, int devtype, uint64_t chipsize) 360 { 361 if (!str2off(arg, off)) 362 return get_part(arg, idx, off, size, maxsize, devtype); 363 364 if (*off >= chipsize) { 365 puts("Offset exceeds device limit\n"); 366 return -1; 367 } 368 369 *maxsize = chipsize - *off; 370 *size = *maxsize; 371 return 0; 372 } 373 374 int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off, 375 loff_t *size, loff_t *maxsize, int devtype, 376 uint64_t chipsize) 377 { 378 int ret; 379 380 if (argc == 0) { 381 *off = 0; 382 *size = chipsize; 383 *maxsize = *size; 384 goto print; 385 } 386 387 ret = mtd_arg_off(argv[0], idx, off, size, maxsize, devtype, 388 chipsize); 389 if (ret) 390 return ret; 391 392 if (argc == 1) 393 goto print; 394 395 if (!str2off(argv[1], size)) { 396 printf("'%s' is not a number\n", argv[1]); 397 return -1; 398 } 399 400 if (*size > *maxsize) { 401 puts("Size exceeds partition or device limit\n"); 402 return -1; 403 } 404 405 print: 406 printf("device %d ", *idx); 407 if (*size == chipsize) 408 puts("whole chip\n"); 409 else 410 printf("offset 0x%llx, size 0x%llx\n", 411 (unsigned long long)*off, (unsigned long long)*size); 412 return 0; 413 } 414