util/nvmutil: improved error handling
parent
55a951a718
commit
a94bac81f3
|
@ -37,7 +37,6 @@
|
||||||
ssize_t readFromFile(int *fd, uint8_t *buf, const char *path, int flags,
|
ssize_t readFromFile(int *fd, uint8_t *buf, const char *path, int flags,
|
||||||
size_t size);
|
size_t size);
|
||||||
void setmac(const char *strMac);
|
void setmac(const char *strMac);
|
||||||
void cmd(const char *command);
|
|
||||||
void cmd_dump(void);
|
void cmd_dump(void);
|
||||||
void showmac(int partnum);
|
void showmac(int partnum);
|
||||||
void hexdump(int partnum);
|
void hexdump(int partnum);
|
||||||
|
@ -72,6 +71,8 @@ main(int argc, char *argv[])
|
||||||
int flags = O_RDWR;
|
int flags = O_RDWR;
|
||||||
char *strMac = NULL;
|
char *strMac = NULL;
|
||||||
char *strRMac = "??:??:??:??:??:??";
|
char *strRMac = "??:??:??:??:??:??";
|
||||||
|
void (*cmd)(void) = NULL;
|
||||||
|
|
||||||
nvmPartModified[0] = 0;
|
nvmPartModified[0] = 0;
|
||||||
nvmPartModified[1] = 0;
|
nvmPartModified[1] = 0;
|
||||||
|
|
||||||
|
@ -89,15 +90,27 @@ main(int argc, char *argv[])
|
||||||
err(1, "pledge");
|
err(1, "pledge");
|
||||||
#endif
|
#endif
|
||||||
flags = O_RDONLY;
|
flags = O_RDONLY;
|
||||||
} else if (strcmp(COMMAND, "setmac") == 0) {
|
cmd = &cmd_dump;
|
||||||
|
} else if (strcmp(COMMAND, "setmac") == 0)
|
||||||
strMac = strRMac;
|
strMac = strRMac;
|
||||||
}
|
else if (strcmp(COMMAND, "swap") == 0)
|
||||||
|
cmd = &cmd_swap;
|
||||||
|
else
|
||||||
|
errno = EINVAL;
|
||||||
} else if (argc == 4) {
|
} else if (argc == 4) {
|
||||||
if (strcmp(COMMAND, "setmac") == 0)
|
if (strcmp(COMMAND, "setmac") == 0)
|
||||||
strMac = MAC_ADDRESS;
|
strMac = MAC_ADDRESS;
|
||||||
else if ((!((part = PARTNUM[0] - '0') == 0 || part == 1))
|
else if ((!((part = PARTNUM[0] - '0') == 0 || part == 1))
|
||||||
|| PARTNUM[1])
|
|| PARTNUM[1])
|
||||||
errno = EINVAL;
|
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
|
} else
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
|
|
||||||
|
@ -107,17 +120,22 @@ main(int argc, char *argv[])
|
||||||
if (readFromFile(&fd, gbe, FILENAME, flags, SIZE_8KB) != SIZE_8KB)
|
if (readFromFile(&fd, gbe, FILENAME, flags, SIZE_8KB) != SIZE_8KB)
|
||||||
goto nvmutil_exit;
|
goto nvmutil_exit;
|
||||||
|
|
||||||
|
if (errno == ENOTDIR)
|
||||||
|
errno = 0;
|
||||||
|
if (errno != 0)
|
||||||
|
goto nvmutil_exit;
|
||||||
|
|
||||||
if (strMac != NULL)
|
if (strMac != NULL)
|
||||||
setmac(strMac);
|
setmac(strMac);
|
||||||
|
else if (cmd != NULL)
|
||||||
|
(*cmd)();
|
||||||
else
|
else
|
||||||
cmd(COMMAND);
|
errno = EINVAL;
|
||||||
|
|
||||||
if (gbeFileModified)
|
if (gbeFileModified)
|
||||||
writeGbeFile(&fd, FILENAME);
|
writeGbeFile(&fd, FILENAME);
|
||||||
|
|
||||||
nvmutil_exit:
|
nvmutil_exit:
|
||||||
if (errno == ENOTDIR)
|
|
||||||
errno = 0;
|
|
||||||
if (!((errno == ECANCELED) && (flags == O_RDONLY)))
|
if (!((errno == ECANCELED) && (flags == O_RDONLY)))
|
||||||
if (errno != 0)
|
if (errno != 0)
|
||||||
fprintf(stderr, "%s\n", strerror(errno));
|
fprintf(stderr, "%s\n", strerror(errno));
|
||||||
|
@ -228,7 +246,7 @@ setmac(const char *strMac)
|
||||||
for (o = 0; o < 3; o++)
|
for (o = 0; o < 3; o++)
|
||||||
setWord(o, partnum, mac[o]);
|
setWord(o, partnum, mac[o]);
|
||||||
part = partnum;
|
part = partnum;
|
||||||
cmd("setchecksum");
|
cmd_setchecksum();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
invalid_mac_address:
|
invalid_mac_address:
|
||||||
|
@ -237,23 +255,6 @@ invalid_mac_address:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
void
|
||||||
cmd_dump(void)
|
cmd_dump(void)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue