Attention is currently required from: Nikolai Artemiev.
Edward O'Callaghan has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/73495 )
Change subject: linux_mtd.c: Factor out detection phases from setup func ......................................................................
linux_mtd.c: Factor out detection phases from setup func
The setup phase function is performing detection logic, move this to a earlier phase in the init sequence such that it can be used seperately.
Change-Id: I15915b71e1a030548c5f082cde78c42ece28bb37 Signed-off-by: Edward O'Callaghan quasisec@google.com --- M linux_mtd.c 1 file changed, 70 insertions(+), 59 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/95/73495/1
diff --git a/linux_mtd.c b/linux_mtd.c index b3be72a..708e340 100644 --- a/linux_mtd.c +++ b/linux_mtd.c @@ -443,57 +443,6 @@ .wp_get_ranges = linux_mtd_wp_get_available_ranges, };
-/* Returns 0 if setup is successful, non-zero to indicate error */ -static int linux_mtd_setup(int dev_num, struct linux_mtd_data *data) -{ - char sysfs_path[32]; - int ret = 1; - - /* Start by checking /sys/class/mtd/mtdN/type which should be "nor" for NOR flash */ - if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d/", LINUX_MTD_SYSFS_ROOT, dev_num) < 0) - goto linux_mtd_setup_exit; - - char buf[4] = { 0 }; - if (read_sysfs_string(sysfs_path, "type", buf, sizeof(buf))) - return 1; - - if (strcmp(buf, "nor")) { - msg_perr("MTD device %d type is not "nor"\n", dev_num); - goto linux_mtd_setup_exit; - } - - /* sysfs shows the correct device type, see if corresponding device node exists */ - char dev_path[32]; - struct stat s; - snprintf(dev_path, sizeof(dev_path), "%s/mtd%d", LINUX_DEV_ROOT, dev_num); - errno = 0; - if (stat(dev_path, &s) < 0) { - msg_pdbg("Cannot stat "%s": %s\n", dev_path, strerror(errno)); - goto linux_mtd_setup_exit; - } - - /* 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_num) < 0) - goto linux_mtd_setup_exit; - if (get_mtd_info(sysfs_path, data)) - goto linux_mtd_setup_exit; - - /* open file stream and go! */ - if ((data->dev_fp = fopen(dev_path, "r+")) == NULL) { - msg_perr("Cannot open file stream for %s\n", dev_path); - goto linux_mtd_setup_exit; - } - ret = setvbuf(data->dev_fp, NULL, _IONBF, 0); - if (ret) - msg_pwarn("Failed to set MTD device to unbuffered: %d\n", ret); - - msg_pinfo("Opened %s successfully\n", dev_path); - - ret = 0; -linux_mtd_setup_exit: - return ret; -} - static int get_params(const struct programmer_cfg *cfg, int *dev_num) { char *param_str = extract_programmer_param_str(cfg, "dev"); @@ -516,17 +465,18 @@ return 0; }
-static int check_sysfs_devnum_path(int dev_num) +static int detect_spinor_in_sysfs(int dev_num, char *dev_path, size_t dp_len) { + /* Start by checking /sys/class/mtd/mtdN/type which should be "nor" for NOR flash */ + char sysfs_path[32]; + if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d", LINUX_MTD_SYSFS_ROOT, dev_num) < 0) + return -1; + /* * If user specified the MTD device number then error out if it doesn't * appear to exist. Otherwise assume the error is benign and print a * debug message. Bail out in either case. */ - char sysfs_path[32]; - if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d", LINUX_MTD_SYSFS_ROOT, dev_num) < 0) - return -1; - struct stat s; if (stat(sysfs_path, &s) < 0) { if (dev_num) @@ -535,17 +485,64 @@ msg_pdbg("%s does not exist\n", sysfs_path); return -1; } + + /* Check sysfs shows the correct device type. */ + char buf[4] = { 0 }; + if (read_sysfs_string(sysfs_path, "type", buf, sizeof(buf))) + return -1; + + if (strcmp(buf, "nor")) { + msg_perr("MTD device %d type is not "nor"\n", dev_num); + return -1; + } + + /* Check if the corresponding device node exists. */ + snprintf(dev_path, dp_len, "%s/mtd%d", LINUX_DEV_ROOT, dev_num); + errno = 0; + if (stat(dev_path, &s) < 0) { + msg_pdbg("Cannot stat "%s": %s\n", dev_path, strerror(errno)); + return -1; + } + return 0; }
+/* Returns 0 if setup is successful, non-zero to indicate error */ +static int linux_mtd_setup(int dev_num, const char *dev_path, struct linux_mtd_data *data) +{ + char sysfs_path[32]; + int ret = 1; + + /* Get more info from other files in the sysfs_path dir. */ + if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d/", LINUX_MTD_SYSFS_ROOT, dev_num) < 0) + goto linux_mtd_setup_exit; + if (get_mtd_info(sysfs_path, data)) + goto linux_mtd_setup_exit; + + /* open file stream and go! */ + if ((data->dev_fp = fopen(dev_path, "r+")) == NULL) { + msg_perr("Cannot open file stream for %s\n", dev_path); + goto linux_mtd_setup_exit; + } + ret = setvbuf(data->dev_fp, NULL, _IONBF, 0); + if (ret) + msg_pwarn("Failed to set MTD device to unbuffered: %d\n", ret); + + msg_pinfo("Opened %s successfully\n", dev_path); + + ret = 0; +linux_mtd_setup_exit: + return ret; +} + static int linux_mtd_init(const struct programmer_cfg *cfg) { int dev_num; - if (get_params(cfg, &dev_num) < 0) return 1;
- if (check_sysfs_devnum_path(dev_num) < 0) + char dev_path[32]; + if (detect_spinor_in_sysfs(dev_num, dev_path, sizeof(dev_path)) < 0) return 1;
struct linux_mtd_data *data = calloc(1, sizeof(*data)); @@ -555,7 +552,7 @@ }
/* Get MTD info and store it in `data` */ - if (linux_mtd_setup(dev_num, data)) { + if (linux_mtd_setup(dev_num, dev_path, data)) { free(data); return 1; }