diff options
author | Leah Rowe <leah@libreboot.org> | 2023-09-19 15:56:55 +0100 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2023-09-19 15:56:55 +0100 |
commit | 137a548b045b61cf3383b5dacb6fa3c88f399504 (patch) | |
tree | ea5a56ac462ed4de1d166e114597e7036299da55 /util | |
parent | 4d4482016368614b2187727489d7508dbdc94309 (diff) |
util/nvmutil: optimise swap command
handle it exclusively in writeGbeFile()
this reduces nvmutil.c sloccount to 261, versus 265
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util')
-rw-r--r-- | util/nvmutil/nvmutil.c | 27 |
1 files changed, 11 insertions, 16 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index 55d7e5f6..b6deaeaa 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -24,7 +24,6 @@ void showmac(int partnum); void hexdump(int partnum); void cmd_setchecksum(void); void cmd_brick(void); -void cmd_swap(void); void cmd_copy(void); int validChecksum(int partnum); void setWord(int pos16, int partnum, uint16_t val16); @@ -52,7 +51,7 @@ typedef struct op { op_t op[] = { { .str = "dump", .cmd = cmd_dump, .args = 3}, { .str = "setmac", .cmd = cmd_setmac, .args = 3}, -{ .str = "swap", .cmd = cmd_swap, .args = 3}, +{ .str = "swap", .cmd = writeGbeFile, .args = 3}, { .str = "copy", .cmd = cmd_copy, .args = 4}, { .str = "brick", .cmd = cmd_brick, .args = 4}, { .str = "setchecksum", .cmd = cmd_setchecksum, .args = 4}, @@ -102,7 +101,7 @@ main(int argc, char *argv[]) readGbeFile(); (*cmd)(); - if ((gbeFileModified) && (flags != O_RDONLY)) + if ((gbeFileModified) && (flags != O_RDONLY) && (cmd != writeGbeFile)) writeGbeFile(); err_if((errno != 0) && (cmd != &cmd_dump)); return errno; @@ -122,7 +121,7 @@ openFiles(const char *path) void readGbeFile(void) { - nf = ((cmd == cmd_swap) || (cmd == cmd_copy)) ? SIZE_4KB : nf; + nf = ((cmd == writeGbeFile) || (cmd == cmd_copy)) ? SIZE_4KB : nf; skipread[part ^ 1] = (cmd == &cmd_copy) | (cmd == &cmd_setchecksum) | (cmd == &cmd_brick); gbe[1] = (gbe[0] = (size_t) buf) + SIZE_4KB; @@ -243,14 +242,6 @@ cmd_brick(void) } void -cmd_swap(void) -{ - if ((gbeFileModified = nvmPartModified[0] = nvmPartModified[1] - = validChecksum(1) | validChecksum(0))) - xorswap(gbe[0], gbe[1]); /* speedhack: swap ptr, not words */ -} - -void cmd_copy(void) { if ((gbeFileModified = nvmPartModified[part ^ 1] = validChecksum(part))) @@ -288,11 +279,15 @@ void writeGbeFile(void) { errno = 0; - for (int x = gbe[0] > gbe[1] ? 1 : 0, p = 0; p < 2; p++, x ^= 1) { - if (!nvmPartModified[x]) + if (cmd == writeGbeFile) { + err_if(!(validChecksum(0) || validChecksum(1))); + xorswap(gbe[0], gbe[1]); + } + for (int p = 0; p < 2; p++) { + if ((!nvmPartModified[p]) && (gbe[0] <= gbe[1])) continue; - handle_endianness(x); - err_if(pwrite(fd, (uint8_t *) gbe[x], nf, x << 12) == -1); + handle_endianness(p); + err_if(pwrite(fd, (uint8_t *) gbe[p], nf, p << 12) == -1); } err_if(close(fd) == -1); } |