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