Attention is currently required from: Patrick Rudolph. Hello Patrick Rudolph,
I'd like you to do a code review. Please visit
https://review.coreboot.org/c/flashrom/+/63135
to review the following change.
Change subject: linux_mtd: Allow to open parition by name ......................................................................
linux_mtd: Allow to open parition by name
MTD device numbers might change, while the name keeps the same. Add new programmer parameter 'name', which is used when 'dev' isn't specified and try to locate a single MTD partition matching the name.
If more than one partition is found it's assumed to be an error.
Change-Id: I566b9809950cbf47fbb01ae89305641c4e259f68 Signed-off-by: Patrick Rudolph patrick.rudolph@9elements.com --- M flashrom.8.tmpl M linux_mtd.c 2 files changed, 86 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/35/63135/1
diff --git a/flashrom.8.tmpl b/flashrom.8.tmpl index fe2d9d6..6302c36 100644 --- a/flashrom.8.tmpl +++ b/flashrom.8.tmpl @@ -1182,6 +1182,14 @@ is the Linux device node for your MTD device. If left unspecified the first MTD device found (e.g. /dev/mtd0) will be used by default. .sp +You may specify the name of the MTD device to use with the +.sp +.B " flashrom -p linux_mtd:name=Y" +.sp +syntax where +.B Y +is the unique name identifying your MTD device. +.sp Please note that the linux_mtd driver only works on Linux. .SS .BR "linux_spi " programmer diff --git a/linux_mtd.c b/linux_mtd.c index 9d80a51..d9c7c6b 100644 --- a/linux_mtd.c +++ b/linux_mtd.c @@ -15,6 +15,7 @@ */
#include <ctype.h> +#include <dirent.h> #include <errno.h> #include <fcntl.h> #include <stdio.h> @@ -370,6 +371,77 @@ return ret; }
+static int linux_mtd_by_name(char *name, int *dev_num) +{ + char sysfs_path[32], device_name[32]; + struct dirent *dir; + char *n, *endptr; + int matches, dev; + DIR *d; + + *dev_num = -1; + matches = 0; + + d = opendir(LINUX_MTD_SYSFS_ROOT); + if (!d) { + msg_perr("Cannot access %s.\n", LINUX_MTD_SYSFS_ROOT); + return -1; + } + + while ((dir = readdir(d)) != NULL) { + n = dir->d_name; + + /* Must have mtdX format */ + if (strlen(n) < 4) + continue; + + /* Skip non mtd devices */ + if (memcmp(n, "mtd", 3) != 0) + continue; + + n += 3; + + /* Skip read only partitions */ + if (strlen(n) > 2 && strcmp(&n[strlen(n)- 2], "ro") == 0) + continue; + + /* Get the device number */ + dev = strtol(n, &endptr, 0); + if ((*endptr != '\0') || (dev < 0)) + continue; + + /* so far so good, get more info from other files in this dir */ + if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d/", LINUX_MTD_SYSFS_ROOT, dev) < 0) + continue; + + /* Device name */ + if (read_sysfs_string(sysfs_path, "name", device_name, sizeof(device_name))) + return 1; + + if (strcmp(device_name, name) == 0) { + msg_pdbg("Found %s on device MTD%d.\n", name, dev); + matches += 1; + if (matches > 1) + break; + *dev_num = dev; + } + } + closedir(d); + + if (matches == 0) { + msg_perr("Invalid device name %s. Use flashrom -p " + "linux_mtd:name=N where N is a valid MTD\n" + "device name.\n", name); + return -1; + } else if (matches > 1) { + msg_perr("Duplicated device name %s. Consider using " + "device numbers instead.\n", name); + return -1; + } + + return 0; +} + static int linux_mtd_init(void) { char *param; @@ -388,6 +460,12 @@ "device number.\n", param); goto linux_mtd_init_exit; } + } else { + param = extract_programmer_param("name"); + if (param) { + if (linux_mtd_by_name(param, &dev_num)) + goto linux_mtd_init_exit; + } }
/*