diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-03 16:37:10 +0000 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-03 16:37:55 +0000 |
| commit | faf28691b81294a564c315d51e064fc159ecc8cc (patch) | |
| tree | ab2036e2c13c5687f6beafbf433b53dc4574f61c | |
| parent | 624589fcb1ed342c47df0cda41f45f92f19c64f9 (diff) | |
util/nvmutil: make swap() easier to understand
the swap function reverses the byte order in memory, of
a loaded GbE after after reading it, or before writing
it. this is required (as detected) on big-endian CPUs,
because GbE files store bytes in little-endian order.
Signed-off-by: Leah Rowe <leah@libreboot.org>
| -rw-r--r-- | util/nvmutil/nvmutil.c | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index eef4f6df..9d7b817d 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -540,8 +540,15 @@ swap(int partnum) int e = 1; uint8_t *n = buf + (SIZE_4KB * partnum); - for (w = NVM_SIZE * ((uint8_t *) &e)[0], x = 1; - w < NVM_SIZE; w += 2, x += 2) { + if (((uint8_t *) &e)[0] == 1) + return; /* Little-endian host CPU; no swap needed. */ + + /* + * The host CPU stores bytes in big-endian order. + * GbE files store bytes in little-endian order. + * We will therefore reverse the order in memory: + */ + for (w = 0, x = 1; w < NVM_SIZE; w += 2, x += 2) { uint8_t chg = n[w]; n[w] = n[x]; n[x] = chg; |
