Attention is currently required from: Nikolai Artemiev.

Edward O'Callaghan has uploaded this change for review.

View Change

linux_mtd.c: Split linux_mtd_init() into sub-functions

Here we split up the linux_mtd_init() entry-point into its
constituent phases;
get_params -> check_sysfs_path ->
alloc resources -> setup drv -> register.

The motivation is that we wish for linux_mtd to have a pre_init
hook for the purposes of detection only. This is a requirement
for the internal programmer via the current try_mtd() hack.

Change-Id: Ic81068adbc7da8b28de07a161b5cec2fff0fce49
Signed-off-by: Edward O'Callaghan <quasisec@google.com>
---
M linux_mtd.c
1 file changed, 56 insertions(+), 26 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/94/73494/1
diff --git a/linux_mtd.c b/linux_mtd.c
index 495db9a..b3be72a 100644
--- a/linux_mtd.c
+++ b/linux_mtd.c
@@ -494,26 +494,30 @@
return ret;
}

-static int linux_mtd_init(const struct programmer_cfg *cfg)
+static int get_params(const struct programmer_cfg *cfg, int *dev_num)
{
- char *param_str;
- int dev_num = 0;
- int ret = 1;
- struct linux_mtd_data *data = NULL;
-
- param_str = extract_programmer_param_str(cfg, "dev");
- if (param_str) {
- char *endptr;
-
- dev_num = strtol(param_str, &endptr, 0);
- if ((*endptr != '\0') || (dev_num < 0)) {
- msg_perr("Invalid device number %s. Use flashrom -p "
- "linux_mtd:dev=N where N is a valid MTD\n"
- "device number.\n", param_str);
- goto linux_mtd_init_exit;
- }
+ char *param_str = extract_programmer_param_str(cfg, "dev");
+ if (!param_str) {
+ *dev_num = 0;
+ return 0;
}

+ char *endptr;
+ *dev_num = strtol(param_str, &endptr, 0);
+ if ((*endptr != '\0') || (*dev_num < 0)) {
+ msg_perr("Invalid device number %s. Use flashrom -p "
+ "linux_mtd:dev=N where N is a valid MTD\n"
+ "device number.\n", param_str);
+ free(param_str);
+ return -1;
+ }
+
+ free(param_str);
+ return 0;
+}
+
+static int check_sysfs_devnum_path(int dev_num)
+{
/*
* 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
@@ -521,19 +525,30 @@
*/
char sysfs_path[32];
if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d", LINUX_MTD_SYSFS_ROOT, dev_num) < 0)
- goto linux_mtd_init_exit;
+ return -1;

struct stat s;
if (stat(sysfs_path, &s) < 0) {
- if (param_str)
+ if (dev_num)
msg_perr("%s does not exist\n", sysfs_path);
else
msg_pdbg("%s does not exist\n", sysfs_path);
- goto linux_mtd_init_exit;
+ return -1;
}
- free(param_str);
+ return 0;
+}

- data = calloc(1, sizeof(*data));
+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)
+ return 1;
+
+ struct linux_mtd_data *data = calloc(1, sizeof(*data));
if (!data) {
msg_perr("Unable to allocate memory for linux_mtd_data\n");
return 1;
@@ -546,10 +561,6 @@
}

return register_opaque_master(&linux_mtd_opaque_master, data);
-
-linux_mtd_init_exit:
- free(param_str);
- return ret;
}

const struct programmer_entry programmer_linux_mtd = {

To view, visit change 73494. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: Ic81068adbc7da8b28de07a161b5cec2fff0fce49
Gerrit-Change-Number: 73494
Gerrit-PatchSet: 1
Gerrit-Owner: Edward O'Callaghan <quasisec@chromium.org>
Gerrit-Reviewer: Nikolai Artemiev <nartemiev@google.com>
Gerrit-Attention: Nikolai Artemiev <nartemiev@google.com>
Gerrit-MessageType: newchange