diff options
author | Leah Rowe <leah@libreboot.org> | 2022-12-24 00:52:05 +0000 |
---|---|---|
committer | lbmkplaceholder <placeholder@lbmkplaceholder.com> | 2022-12-24 01:10:55 +0000 |
commit | d726b16f5f223073e97856bac2c1e35d30f30e66 (patch) | |
tree | bea1d099dad13f0c37a0e835fc6141670014eede | |
parent | 448ee5105de59e1eb4324ed62070bc130bd32c7a (diff) |
util/nvmutil: more robust pointer handling
i didn't like the previous commits, they felt really hacky
running malloc and then changing the pointer directly just rubs
me the wrong way
fix that
-rw-r--r-- | util/nvmutil/nvmutil.c | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index 76273550..7614a030 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -57,7 +57,9 @@ void writeGbeFile(int *fd, const char *filename); #define SIZE_4KB 0x1000 #define SIZE_8KB 0x2000 -uint8_t *gbe = NULL, *gbe2 = NULL; +uint8_t *buf = NULL; +size_t gbe[2]; + int part, gbeFileModified = 0; uint8_t nvmPartModified[2]; @@ -73,9 +75,10 @@ main(int argc, char *argv[]) char *strRMac = "??:??:??:??:??:??"; void (*cmd)(void) = NULL; - if ((gbe = (uint8_t *) malloc(SIZE_8KB)) == NULL) + if ((buf = (uint8_t *) malloc(SIZE_8KB)) == NULL) err(errno, NULL); - gbe2 = gbe + SIZE_4KB; + gbe[0] = gbe[1] = (size_t) buf; + gbe[1] += SIZE_4KB; nvmPartModified[0] = 0; nvmPartModified[1] = 0; @@ -115,7 +118,7 @@ main(int argc, char *argv[]) if ((strMac == NULL) && (cmd == NULL)) errno = EINVAL; - else if (readFromFile(&fd, gbe, FILENAME, flags, SIZE_8KB) != SIZE_8KB) + else if (readFromFile(&fd, buf, FILENAME, flags, SIZE_8KB) != SIZE_8KB) goto nvmutil_exit; if (errno == 0) { @@ -355,16 +358,14 @@ void cmd_swap(void) { int part0, part1; - size_t ptr; part0 = validChecksum(0); part1 = validChecksum(1); if (part0 || part1) { - gbe2 = gbe; - ptr = (size_t) gbe; - ptr |= SIZE_4KB; - gbe = (uint8_t *) ptr; + gbe[0] ^= gbe[1]; + gbe[1] ^= gbe[0]; + gbe[0] ^= gbe[1]; gbeFileModified = 1; nvmPartModified[0] = 1; @@ -378,10 +379,7 @@ void cmd_copy(void) { if (validChecksum(part)) { - if (part) - gbe = gbe2; - else - gbe2 = gbe; + gbe[part ^ 1] = gbe[part]; gbeFileModified = 1; nvmPartModified[part ^ 1] = 1; @@ -408,7 +406,7 @@ validChecksum(int partnum) uint16_t word(int pos16, int partnum) { - uint16_t val16 = ((uint16_t *) gbe)[pos16 + (partnum << 11)]; + uint16_t val16 = ((uint16_t *) buf)[pos16 + (partnum << 11)]; if (!little_endian) byteswap((uint8_t *) &val16); @@ -418,9 +416,9 @@ word(int pos16, int partnum) void setWord(int pos16, int partnum, uint16_t val) { - ((uint16_t *) gbe)[pos16 + (partnum << 11)] = val; + ((uint16_t *) buf)[pos16 + (partnum << 11)] = val; if (!little_endian) - byteswap(gbe + (pos16 << 1) + (partnum << 12)); + byteswap(buf + (pos16 << 1) + (partnum << 12)); gbeFileModified = 1; nvmPartModified[partnum] = 1; @@ -440,9 +438,9 @@ writeGbeFile(int *fd, const char *filename) int partnum; errno = 0; - if (pwrite((*fd), gbe, SIZE_4KB, 0) != SIZE_4KB) + if (pwrite((*fd), (uint8_t *) gbe[0], SIZE_4KB, 0) != SIZE_4KB) err(errno, "%s", filename); - if (pwrite((*fd), gbe2, SIZE_4KB, SIZE_4KB) != SIZE_4KB) + if (pwrite((*fd), (uint8_t *) gbe[1], SIZE_4KB, SIZE_4KB) != SIZE_4KB) err(errno, "%s", filename); if (close((*fd))) err(errno, "%s", filename); |