From 566ae72ca3c86b31b7379204877cdbd726f61b10 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Tue, 3 Mar 2026 00:51:10 +0000 Subject: util/nvmutil: remove variable nvmPartChanged pointless optimisation. we know that when a user requests an operation that would write, it will probably result in a change. therefore, this change is the real optimisation. to avoid writing the same half of a file twice, when using cmd_copy, we check (in writeGbe) whether gbe part 0 and 1 are the same; if they are, then we only loop once. this is important, because otherwise we would call swap() twice. this means that the optimisations in cmd_copy and cmd_swap must be removed. the point of this and other changes is to improve memory safety in nvmutil, so frivolous use of pointers has to go. Signed-off-by: Leah Rowe --- util/nvmutil/nvmutil.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) (limited to 'util') diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index 19217df8..6f0bc17d 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -42,7 +42,6 @@ uint8_t buf[SIZE_8KB]; uint16_t mac[3] = {0, 0, 0}; size_t partsize; uint8_t *gbe[2]; -uint8_t nvmPartChanged[2] = {0, 0}; int flags, rfd, fd, part, e = 1; const char *strMac = NULL, *strRMac = "xx:xx:xx:xx:xx:xx", *fname = NULL; @@ -295,10 +294,6 @@ void cmd_dump(void) { for (int partnum = 0, numInvalid = 0; partnum < 2; partnum++) { - if ((cmd != cmd_dump) && (flags != O_RDONLY) && - (!nvmPartChanged[partnum])) - continue; - if (!goodChecksum(partnum)) ++numInvalid; @@ -361,9 +356,8 @@ void cmd_copy(void) { err_if(!goodChecksum(part)); - - gbe[part ^ 1] = gbe[part]; - nvmPartChanged[part ^ 1] = 1; + for (int c = 0; c < SIZE_4KB; c++) + gbe[part ^ 1][c] = gbe[part][c]; } void @@ -371,11 +365,11 @@ cmd_swap(void) { err_if(!(goodChecksum(0) || goodChecksum(1))); errno = 0; - uint8_t *chg = gbe[0]; - gbe[0] = gbe[1]; - gbe[1] = chg; - - nvmPartChanged[0] = nvmPartChanged[1] = 1; + for (int c = 0; c < SIZE_4KB; c++) { + uint8_t chg = gbe[0][c]; + gbe[0][c] = gbe[1][c]; + gbe[1][c] = chg; + } } int @@ -404,10 +398,7 @@ void setWord(int pos16, int p, uint16_t val16) { check_bounds(pos16, p); - if (((uint16_t *) gbe[p])[pos16] != val16) { - nvmPartChanged[p] = 1; - ((uint16_t *) gbe[p])[pos16] = val16; - } + ((uint16_t *) gbe[p])[pos16] = val16; } void @@ -422,8 +413,8 @@ check_bounds(int c, int p) void writeGbe(void) { - for (int p = 0; p < 2; p++) - if ((nvmPartChanged[p]) && (flags != O_RDONLY)) + if (flags != O_RDONLY) + for (int p = 0; p < 2; p++) writeGbe_part(p); err_if(close(fd) == -1); } @@ -435,7 +426,6 @@ writeGbe_part(int p) if(pwrite(fd, (uint8_t *) gbe[p], SIZE_4KB, p * partsize) != SIZE_4KB) err(set_err(ECANCELED), "Can't write %d b to '%s' p%d", SIZE_4KB, fname, p); - } void -- cgit v1.2.1