diff options
author | Leah Rowe <leah@libreboot.org> | 2023-06-01 08:21:25 +0100 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2023-06-01 08:21:25 +0100 |
commit | a2136933af50988978aaf96919e9d013b1901f8f (patch) | |
tree | a14547d78f4fc058ab6a5ab4025e4a5d179b0198 /util/nvmutil | |
parent | 5a9fac2a63d0d11e7116f365c0cc5f6d101166cd (diff) |
util/nvmutil: use even more macros (code cleanup)
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/nvmutil')
-rw-r--r-- | util/nvmutil/nvmutil.c | 31 |
1 files changed, 11 insertions, 20 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index e6b4ddfb..0b3cce7a 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -57,7 +57,6 @@ void xorswap_buf(int n, int partnum); void writeGbeFile(int *fd, const char *filename, size_t nw); void xpledge(const char *promises, const char *execpromises); void xunveil(const char *path, const char *permissions); -void err_if(int condition); #define FILENAME argv[1] #define COMMAND argv[2] @@ -66,10 +65,6 @@ void err_if(int condition); #define SIZE_4KB 0x1000 #define SIZE_8KB 0x2000 -#define word(pos16, partnum) buf16[pos16 + (partnum << 11)] -#define ERR() errno = errno ? errno : ECANCELED -#define xorswap(x, y) x ^= y, y ^= x, x ^= y - uint16_t buf16[SIZE_4KB]; uint8_t *buf; size_t gbe[2]; @@ -81,6 +76,12 @@ uint8_t nvmPartModified[2] = {0, 0}; uint16_t test; uint8_t big_endian; +#define word(pos16, partnum) buf16[pos16 + (partnum << 11)] +#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) + int main(int argc, char *argv[]) { @@ -122,7 +123,7 @@ main(int argc, char *argv[]) } } - err_if(errno = ((strMac == NULL) && (cmd == NULL)) ? EINVAL : errno); + err_if((errno = ((strMac == NULL) && (cmd == NULL)) ? EINVAL : errno)); skipread[part ^ 1] = (cmd == &cmd_copy) | (cmd == &cmd_setchecksum) | (cmd == &cmd_brick); @@ -152,9 +153,8 @@ readGbeFile(int *fd, const char *path, int flags, size_t nr) struct stat st; if (opendir(path) != NULL) err(errno = EISDIR, "%s", path); - else if (((*fd) = open(path, flags)) == -1) - err(ERR(), "%s", path); - else if (fstat((*fd), &st) == -1) + 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); @@ -233,8 +233,7 @@ rhex(void) static uint8_t rnum[16]; if (!n) { if (rfd == -1) - if ((rfd = open("/dev/urandom", O_RDONLY)) == -1) - err(ERR(), "/dev/urandom"); + xopen(rfd, "/dev/urandom", O_RDONLY); if (read(rfd, (uint8_t *) &rnum, (n = 15) + 1) == -1) err(ERR(), "/dev/urandom"); } @@ -326,8 +325,7 @@ validChecksum(int partnum) void setWord(int pos16, int partnum, uint16_t val16) { - gbeFileModified = 1; - if (word(pos16, partnum) != val16) + if ((gbeFileModified = 1) && word(pos16, partnum) != val16) nvmPartModified[partnum] = 1 | (word(pos16, partnum) = val16); } @@ -381,10 +379,3 @@ xunveil(const char *path, const char *permissions) err(ERR(), "unveil"); #endif } - -void -err_if(int condition) -{ - if (condition) - err(ERR(), NULL); -} |