summaryrefslogtreecommitdiff
path: root/util/nvmutil/nvmutil.c
diff options
context:
space:
mode:
authorlbmkplaceholder <placeholder@lbmkplaceholder.com>2022-12-23 08:41:18 +0000
committerlbmkplaceholder <placeholder@lbmkplaceholder.com>2022-12-23 08:41:18 +0000
commit6e5828e4a8d11ce7f6d8a4868a6e0afa54e6ebf0 (patch)
tree2e67af6610db9a8fad18cd4b3c6b4b888c429e99 /util/nvmutil/nvmutil.c
parent7aafc62bf7f4d062ce006bea2a6d80e28e5c0f9a (diff)
util/nvmutil: greatly optimise cmd_swap()
instead of XOR-swapping every byte, have pointers to the two parts and *XOR swap the pointers*. at the end of the program execution, when writing, pwrite the two parts into the same file
Diffstat (limited to 'util/nvmutil/nvmutil.c')
-rw-r--r--util/nvmutil/nvmutil.c27
1 files changed, 18 insertions, 9 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c
index 80d515f7..b80616ae 100644
--- a/util/nvmutil/nvmutil.c
+++ b/util/nvmutil/nvmutil.c
@@ -57,7 +57,7 @@ void writeGbeFile(int *fd, const char *filename);
#define SIZE_4KB 0x1000
#define SIZE_8KB 0x2000
-uint8_t gbe[SIZE_8KB];
+uint8_t *gbe = NULL, *gbe2 = NULL;
int part, gbeFileModified = 0;
uint8_t nvmPartModified[2];
@@ -73,6 +73,10 @@ main(int argc, char *argv[])
char *strRMac = "??:??:??:??:??:??";
void (*cmd)(void) = NULL;
+ if ((gbe = (uint8_t *) malloc(SIZE_8KB)) == NULL)
+ err(errno, NULL);
+ gbe2 = gbe + SIZE_4KB;
+
nvmPartModified[0] = 0;
nvmPartModified[1] = 0;
@@ -351,16 +355,18 @@ void
cmd_swap(void)
{
int part0, part1;
+ size_t g1 = (size_t) gbe;
+ size_t g2 = (size_t) gbe2;
part0 = validChecksum(0);
part1 = validChecksum(1);
if (part0 || part1) {
- for(part0 = 0; part0 < SIZE_4KB; part0++) {
- gbe[part0] ^= gbe[part1 = (part0 | SIZE_4KB)];
- gbe[part1] ^= gbe[part0];
- gbe[part0] ^= gbe[part1];
- }
+ g1 ^= g2;
+ g2 ^= g1;
+ g1 ^= g2;
+ gbe = (uint8_t *) g1;
+ gbe2 = (uint8_t *) g2;
gbeFileModified = 1;
nvmPartModified[0] = 1;
@@ -438,9 +444,12 @@ writeGbeFile(int *fd, const char *filename)
int partnum;
errno = 0;
- if (pwrite((*fd), gbe, SIZE_8KB, 0) == SIZE_8KB)
- if (close((*fd)))
- err(errno, "%s", filename);
+ if (pwrite((*fd), gbe, SIZE_4KB, 0) != SIZE_4KB)
+ err(errno, "%s", filename);
+ if (pwrite((*fd), gbe2, SIZE_4KB, SIZE_4KB) != SIZE_4KB)
+ err(errno, "%s", filename);
+ if (close((*fd)))
+ err(errno, "%s", filename);
if (errno != 0)
return;