diff options
author | Leah Rowe <leah@libreboot.org> | 2022-11-27 00:27:07 +0000 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2022-11-27 00:27:07 +0000 |
commit | a94bac81f3186fced1011de861f0e243e31584e0 (patch) | |
tree | 8919d91c3e5a2d3694a6092e0b1a8d67f02ce5db /util/nvmutil/nvmutil.c | |
parent | 55a951a718e93e29df26f4ebf0eda336dedf9684 (diff) |
util/nvmutil: improved error handling
Diffstat (limited to 'util/nvmutil/nvmutil.c')
-rw-r--r-- | util/nvmutil/nvmutil.c | 49 |
1 files changed, 25 insertions, 24 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index ad9e24e4..56477b9a 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -37,7 +37,6 @@ ssize_t readFromFile(int *fd, uint8_t *buf, const char *path, int flags, size_t size); void setmac(const char *strMac); -void cmd(const char *command); void cmd_dump(void); void showmac(int partnum); void hexdump(int partnum); @@ -72,6 +71,8 @@ main(int argc, char *argv[]) int flags = O_RDWR; char *strMac = NULL; char *strRMac = "??:??:??:??:??:??"; + void (*cmd)(void) = NULL; + nvmPartModified[0] = 0; nvmPartModified[1] = 0; @@ -89,15 +90,27 @@ main(int argc, char *argv[]) err(1, "pledge"); #endif flags = O_RDONLY; - } else if (strcmp(COMMAND, "setmac") == 0) { + cmd = &cmd_dump; + } else if (strcmp(COMMAND, "setmac") == 0) strMac = strRMac; - } + else if (strcmp(COMMAND, "swap") == 0) + cmd = &cmd_swap; + else + errno = EINVAL; } else if (argc == 4) { if (strcmp(COMMAND, "setmac") == 0) strMac = MAC_ADDRESS; else if ((!((part = PARTNUM[0] - '0') == 0 || part == 1)) || PARTNUM[1]) errno = EINVAL; + else if (strcmp(COMMAND, "setchecksum") == 0) + cmd = &cmd_setchecksum; + else if (strcmp(COMMAND, "brick") == 0) + cmd = &cmd_brick; + else if (strcmp(COMMAND, "copy") == 0) + cmd = &cmd_copy; + else + errno = EINVAL; } else errno = EINVAL; @@ -107,17 +120,22 @@ main(int argc, char *argv[]) if (readFromFile(&fd, gbe, FILENAME, flags, SIZE_8KB) != SIZE_8KB) goto nvmutil_exit; + if (errno == ENOTDIR) + errno = 0; + if (errno != 0) + goto nvmutil_exit; + if (strMac != NULL) setmac(strMac); + else if (cmd != NULL) + (*cmd)(); else - cmd(COMMAND); + errno = EINVAL; if (gbeFileModified) writeGbeFile(&fd, FILENAME); nvmutil_exit: - if (errno == ENOTDIR) - errno = 0; if (!((errno == ECANCELED) && (flags == O_RDONLY))) if (errno != 0) fprintf(stderr, "%s\n", strerror(errno)); @@ -228,7 +246,7 @@ setmac(const char *strMac) for (o = 0; o < 3; o++) setWord(o, partnum, mac[o]); part = partnum; - cmd("setchecksum"); + cmd_setchecksum(); } return; invalid_mac_address: @@ -238,23 +256,6 @@ invalid_mac_address: } void -cmd(const char *command) -{ - if (strcmp(command, "dump") == 0) { - cmd_dump(); - } else if (strcmp(command, "setchecksum") == 0) { - cmd_setchecksum(); - } else if (strcmp(command, "brick") == 0) { - cmd_brick(); - } else if (strcmp(command, "swap") == 0) { - cmd_swap(); - } else if (strcmp(command, "copy") == 0) { - cmd_copy(); - } else - errno = EINVAL; -} - -void cmd_dump(void) { int numInvalid, partnum; |