From 1f3316422d50158a56183672088d1991c02bede6 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Thu, 12 Oct 2023 15:53:29 +0100 Subject: [PATCH] nvmutil: simplify endianness handling Signed-off-by: Leah Rowe --- util/nvmutil/nvmutil.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index e99c6ade..0ccfaf32 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -15,7 +15,7 @@ void cmd_setchecksum(void), cmd_brick(void), cmd_copy(void), writeGbeFile(void), cmd_dump(void), cmd_setmac(void), readGbeFile(void), showmac(int partnum), - hexdump(int partnum), xorswap_buf(int partnum), openFiles(const char *path); + hexdump(int partnum), handle_endianness(int partnum), openFiles(const char *path); int macAddress(const char *strMac, uint16_t *mac), validChecksum(int partnum); uint8_t hextonum(char chs), rhex(void); @@ -28,7 +28,7 @@ uint16_t buf16[SIZE_4KB], mac[3] = {0, 0, 0}; uint8_t *buf = (uint8_t *) &buf16; size_t nf = 128, gbe[2]; uint8_t nvmPartModified[2] = {0, 0}, skipread[2] = {0, 0}; -int endian = 1, flags = O_RDWR, rfd, fd, part, gbeFileModified = 0; +int e = 1, flags = O_RDWR, rfd, fd, part, gbeFileModified = 0; const char *strMac = NULL, *strRMac = "??:??:??:??:??:??", *filename = NULL; @@ -53,7 +53,6 @@ void (*cmd)(void) = NULL; #define xopen(f,l,p) if (opendir(l) != NULL) err(errno = EISDIR, "%s", l); \ if ((f = open(l, p)) == -1) err(ERR(), "%s", l); \ if (fstat(f, &st) == -1) err(ERR(), "%s", l) -#define handle_endianness(r) if (((uint8_t *) &endian)[0] ^ 1) xorswap_buf(r) #define word(pos16, partnum) buf16[pos16 + (partnum << 11)] #define setWord(pos16, p, val16) if ((gbeFileModified = 1) && \ @@ -250,14 +249,6 @@ validChecksum(int partnum) return (errno = ECANCELED) & 0; } -void -xorswap_buf(int partnum) -{ - uint8_t *n = (uint8_t *) gbe[partnum]; - for (size_t w = 0, x = 1; w < nf; w += 2, x += 2) - n[w] ^= n[x], n[x] ^= n[w], n[w] ^= n[x]; -} - void writeGbeFile(void) { @@ -271,3 +262,11 @@ writeGbeFile(void) errno = 0; err_if(close(fd) == -1); } + +void +handle_endianness(int partnum) +{ + uint8_t *n = (uint8_t *) gbe[partnum]; + for (size_t w = nf * ((uint8_t *) &e)[0], x = 1; w < nf; w += 2, x += 2) + n[w] ^= n[x], n[x] ^= n[w], n[w] ^= n[x]; +}