util/nvmutil: massive code cleanup

Signed-off-by: Leah Rowe <leah@libreboot.org>
fsdg20230625
Leah Rowe 2023-06-01 10:31:21 +01:00
parent f0846134b7
commit c2c31677a3
2 changed files with 40 additions and 50 deletions

View File

@ -7,15 +7,9 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
xpledge("stdio rpath wpath unveil", NULL); xpledge("stdio rpath wpath unveil", NULL);
size_t nr = 128; int flags = O_RDWR;
int fd, flags = O_RDWR;
void (*cmd)(void) = NULL; void (*cmd)(void) = NULL;
const char *strMac = NULL, *strRMac = "??:??:??:??:??:??"; const char *strMac = NULL, *strRMac = "??:??:??:??:??:??";
buf = (uint8_t *) &buf16;
gbe[1] = (gbe[0] = (size_t) buf) + SIZE_4KB;
test = 1;
big_endian = ((uint8_t *) &test)[0] ^ 1;
if (argc == 3) { if (argc == 3) {
if (strcmp(COMMAND, "dump") == 0) { if (strcmp(COMMAND, "dump") == 0) {
@ -26,7 +20,7 @@ main(int argc, char *argv[])
strMac = (char *) strRMac; /* random mac address */ strMac = (char *) strRMac; /* random mac address */
} else if (strcmp(COMMAND, "swap") == 0) { } else if (strcmp(COMMAND, "swap") == 0) {
cmd = &cmd_swap; cmd = &cmd_swap;
nr = SIZE_4KB; nf = SIZE_4KB;
} }
} else if (argc == 4) { } else if (argc == 4) {
if (strcmp(COMMAND, "setmac") == 0) { if (strcmp(COMMAND, "setmac") == 0) {
@ -40,7 +34,7 @@ main(int argc, char *argv[])
cmd = &cmd_brick; cmd = &cmd_brick;
} else if (strcmp(COMMAND, "copy") == 0) { } else if (strcmp(COMMAND, "copy") == 0) {
cmd = &cmd_copy; cmd = &cmd_copy;
nr = SIZE_4KB; nf = SIZE_4KB;
} }
} }
@ -48,7 +42,7 @@ main(int argc, char *argv[])
skipread[part ^ 1] = (cmd == &cmd_copy) | skipread[part ^ 1] = (cmd == &cmd_copy) |
(cmd == &cmd_setchecksum) | (cmd == &cmd_brick); (cmd == &cmd_setchecksum) | (cmd == &cmd_brick);
readGbeFile(&fd, FILENAME, flags, nr); readGbeFile(FILENAME, flags);
(void)rhex(); (void)rhex();
xunveil("/dev/urandom", "r"); xunveil("/dev/urandom", "r");
if (flags == O_RDONLY) { if (flags == O_RDONLY) {
@ -62,33 +56,27 @@ main(int argc, char *argv[])
cmd_setmac(strMac); /* nvm gbe.bin setmac */ cmd_setmac(strMac); /* nvm gbe.bin setmac */
else if (cmd != NULL) else if (cmd != NULL)
(*cmd)(); /* all other commands except setmac */ (*cmd)(); /* all other commands except setmac */
writeGbeFile(&fd, FILENAME, nr); writeGbeFile(FILENAME);
err_if((errno != 0) && (cmd != &cmd_dump)); err_if((errno != 0) && (cmd != &cmd_dump));
return errno; return errno;
} }
void void
readGbeFile(int *fd, const char *path, int flags, size_t nr) readGbeFile(const char *path, int flags)
{ {
struct stat st; xopen(fd, path, flags);
if (opendir(path) != NULL) if ((st.st_size != SIZE_8KB))
err(errno = EISDIR, "%s", path);
xopen(*fd, path, flags);
if (fstat((*fd), &st) == -1)
err(ERR(), "%s", path);
else if ((st.st_size != SIZE_8KB))
err(errno = ECANCELED, "File `%s` not 8KiB", path); err(errno = ECANCELED, "File `%s` not 8KiB", path);
else if (errno == ENOTDIR) errno = errno != ENOTDIR ? errno : 0;
errno = 0;
big_endian = ((uint8_t *) &test)[0] ^ 1;
gbe[1] = (gbe[0] = (size_t) buf) + SIZE_4KB;
for (int p = 0; p < 2; p++) { for (int p = 0; p < 2; p++) {
if (skipread[p]) if (skipread[p])
continue; continue;
if (pread((*fd), (uint8_t *) gbe[p], nr, p << 12) == -1) xpread(fd, (uint8_t *) gbe[p], nf, p << 12, path);
err(ERR(), "%s", path); handle_endianness();
if (big_endian)
xorswap_buf(nr, p);
} }
} }
@ -153,10 +141,8 @@ rhex(void)
static int rfd = -1, n = 0; static int rfd = -1, n = 0;
static uint8_t rnum[16]; static uint8_t rnum[16];
if (!n) { if (!n) {
if (rfd == -1)
xopen(rfd, "/dev/urandom", O_RDONLY); xopen(rfd, "/dev/urandom", O_RDONLY);
if (read(rfd, (uint8_t *) &rnum, (n = 15) + 1) == -1) xpread(rfd, (uint8_t *) &rnum, (n = 15) + 1, 0, "/dev/urandom");
err(ERR(), "/dev/urandom");
} }
return rnum[n--] & 0xf; return rnum[n--] & 0xf;
} }
@ -251,15 +237,15 @@ setWord(int pos16, int partnum, uint16_t val16)
} }
void void
xorswap_buf(int n, int partnum) xorswap_buf(int partnum)
{ {
uint8_t *nbuf = (uint8_t *) gbe[partnum]; uint8_t *nbuf = (uint8_t *) gbe[partnum];
for (int w = 0; w < (n >> 1); w++) for (size_t w = 0; w < (nf >> 1); w++)
xorswap(nbuf[w << 1], nbuf[(w << 1) + 1]); xorswap(nbuf[w << 1], nbuf[(w << 1) + 1]);
} }
void void
writeGbeFile(int *fd, const char *filename, size_t nw) writeGbeFile(const char *filename)
{ {
if (gbeFileModified) if (gbeFileModified)
errno = 0; errno = 0;
@ -268,15 +254,12 @@ writeGbeFile(int *fd, const char *filename, size_t nw)
p ^= 1; /* speedhack: write sequentially on-disk */ p ^= 1; /* speedhack: write sequentially on-disk */
if (!nvmPartModified[p]) if (!nvmPartModified[p])
goto next_part; goto next_part;
if (big_endian) handle_endianness();
xorswap_buf(nw, p); xpwrite(fd, (uint8_t *) gbe[p], nf, p << 12, filename);
if (pwrite((*fd), (uint8_t *) gbe[p], nw, p << 12) == -1)
err(ERR(), "%s", filename);
next_part: next_part:
if (gbe[0] > gbe[1]) if (gbe[0] > gbe[1])
p ^= 1; /* speedhack: write sequentially on-disk */ p ^= 1; /* speedhack: write sequentially on-disk */
} }
if (close((*fd))) xclose(fd, filename);
err(ERR(), "%s", filename);
xpledge("stdio", NULL); xpledge("stdio", NULL);
} }

View File

@ -13,8 +13,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
void readGbeFile(int *fd, const char *path, int flags, void readGbeFile(const char *path, int flags);
size_t nr);
void cmd_setmac(const char *strMac); void cmd_setmac(const char *strMac);
int invalidMacAddress(const char *strMac, uint16_t *mac); int invalidMacAddress(const char *strMac, uint16_t *mac);
uint8_t hextonum(char chs); uint8_t hextonum(char chs);
@ -28,8 +27,8 @@ void cmd_swap(void);
void cmd_copy(void); void cmd_copy(void);
int validChecksum(int partnum); int validChecksum(int partnum);
void setWord(int pos16, int partnum, uint16_t val16); void setWord(int pos16, int partnum, uint16_t val16);
void xorswap_buf(int n, int partnum); void xorswap_buf(int partnum);
void writeGbeFile(int *fd, const char *filename, size_t nw); void writeGbeFile(const char *filename);
#define FILENAME argv[1] #define FILENAME argv[1]
#define COMMAND argv[2] #define COMMAND argv[2]
@ -39,22 +38,30 @@ void writeGbeFile(int *fd, const char *filename, size_t nw);
#define SIZE_8KB 0x2000 #define SIZE_8KB 0x2000
uint16_t buf16[SIZE_4KB]; uint16_t buf16[SIZE_4KB];
uint8_t *buf; uint8_t *buf = (uint8_t *) &buf16;
size_t gbe[2]; size_t nf = 128, gbe[2];
uint8_t skipread[2] = {0, 0}; uint8_t skipread[2] = {0, 0};
int part, gbeFileModified = 0; int fd = -1, part, gbeFileModified = 0;
uint8_t nvmPartModified[2] = {0, 0}; uint8_t nvmPartModified[2] = {0, 0};
uint16_t test; int test = 1;
uint8_t big_endian; int big_endian;
#define word(pos16, partnum) buf16[pos16 + (partnum << 11)]
#define ERR() errno = errno ? errno : ECANCELED #define ERR() errno = errno ? errno : ECANCELED
#define xorswap(x, y) x ^= y, y ^= x, x ^= y
#define xopen(fd, loc, p) if ((fd = open(loc, p)) == -1) err(ERR(), "%s", loc)
#define err_if(x) if (x) err(ERR(), NULL) #define err_if(x) if (x) err(ERR(), NULL)
#define xopen(f,l,p) if (opendir(l) != NULL) err(errno = EISDIR, "%s", l); \
if (f == -1) if ((f = open(l, p)) == -1) err(ERR(), "%s", l); \
struct stat st; if (fstat(f, &st) == -1) err(ERR(), "%s", l)
#define xpread(f, b, n, o, l) if (pread(f, b, n, o) == -1) err(ERR(), "%s", l)
#define handle_endianness() if (big_endian) xorswap_buf(p)
#define xpwrite(f, b, n, o, l) if (pwrite(f, b, n, o) == -1) err(ERR(), "%s", l)
#define xclose(f, l) if (close(f)) err(ERR(), "%s", l)
#define xorswap(x, y) x ^= y, y ^= x, x ^= y
#define word(pos16, partnum) buf16[pos16 + (partnum << 11)]
void void
xpledge(const char *promises, const char *execpromises) xpledge(const char *promises, const char *execpromises)
{ {