diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-18 13:13:27 +0000 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-18 13:37:06 +0000 |
| commit | 27371af4bc6d3b1dfec6a498585769ccdb3b15f6 (patch) | |
| tree | 97759cb4b95a11e42bef0f9df93b297fd19b0b96 /util/nvmutil/word.c | |
| parent | 722ed03179ec43d7b71f927f1a53579ccf5ebff8 (diff) | |
nvmutil: split nvmutil.c into multiple files
this is a big program now. act like it.
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/nvmutil/word.c')
| -rw-r--r-- | util/nvmutil/word.c | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/util/nvmutil/word.c b/util/nvmutil/word.c new file mode 100644 index 00000000..5d202544 --- /dev/null +++ b/util/nvmutil/word.c @@ -0,0 +1,98 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (c) 2022-2026 Leah Rowe <leah@libreboot.org> + * + * Manipulate sixteen-bit little-endian + * words on Intel GbE NVM configurations. + */ + +#ifdef __OpenBSD__ +#include <sys/param.h> +#endif +#include <sys/types.h> +#include <sys/stat.h> + +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> + +#include "include/common.h" + +/* + * GbE NVM files store 16-bit (2-byte) little-endian words. + * We must therefore swap the order when reading or writing. + * + * NOTE: The MAC address words are stored big-endian in the + * file, but we assume otherwise and adapt accordingly. + */ + +unsigned short +nvm_word(unsigned long pos16, unsigned long p) +{ + struct xstate *x = xstatus(); + struct xfile *f; + + unsigned long pos; + + f = &x->f; + + check_nvm_bound(pos16, p); + pos = (pos16 << 1) + (p * GBE_PART_SIZE); + + return (unsigned short)f->buf[pos] | + ((unsigned short)f->buf[pos + 1] << 8); +} + +void +set_nvm_word(unsigned long pos16, unsigned long p, unsigned short val16) +{ + struct xstate *x = xstatus(); + struct xfile *f; + + unsigned long pos; + + f = &x->f; + + check_nvm_bound(pos16, p); + pos = (pos16 << 1) + (p * GBE_PART_SIZE); + + f->buf[pos] = (unsigned char)(val16 & 0xff); + f->buf[pos + 1] = (unsigned char)(val16 >> 8); + + set_part_modified(p); +} + +void +set_part_modified(unsigned long p) +{ + struct xstate *x = xstatus(); + struct xfile *f; + + f = &x->f; + + check_bin(p, "part number"); + f->part_modified[p] = 1; +} + +void +check_nvm_bound(unsigned long c, unsigned long p) +{ + /* + * NVM_SIZE assumed as the limit, because this + * current design assumes that we will only + * ever modified the NVM area. + */ + + check_bin(p, "part number"); + + if (c >= NVM_WORDS) + err(ECANCELED, "check_nvm_bound: out of bounds %lu", + (unsigned long)c); +} |
