summaryrefslogtreecommitdiff
path: root/util/nvmutil
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-03 02:25:11 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-03 02:25:11 +0000
commit707fabab380f296bb91de698113fda3501ab8361 (patch)
tree566f4179eb2c8f9783c4765e7daa0cca0db90f4b /util/nvmutil
parentd9c307d5a3eeb7869cdd1e0cbc4a45325d728f6c (diff)
util/nvmutil: split up parseMacString
split it into smaller, more readable functions Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/nvmutil')
-rw-r--r--util/nvmutil/nvmutil.c72
1 files changed, 46 insertions, 26 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c
index 6c1d929e..6e296007 100644
--- a/util/nvmutil/nvmutil.c
+++ b/util/nvmutil/nvmutil.c
@@ -16,12 +16,14 @@
void cmd_setchecksum(void), cmd_brick(void), swap(int partnum), writeGbe(void),
cmd_dump(void), cmd_setmac(void), readGbe(void),
+ set_mac_nib(int, int, uint8_t *),
checkdir(const char *path), macf(int partnum), hexdump(int partnum),
parseMacString(const char *strMac, uint16_t *mac), cmd_swap(void),
openFiles(void), cmd_copy(void), writeGbe_part(int),
readGbe_part(int), usage(char*), set_io_flags(int, char **),
set_cmd(int, char **), setWord(int, int, uint16_t), check_bounds(int, int),
- xopen (int *, const char *, int p, struct stat *);
+ xopen (int *, const char *, int p, struct stat *),
+ set_mac_byte(int, uint64_t *), check_mac_separator(int);
int goodChecksum(int partnum);
uint8_t hextonum(char chs), rhex(void);
uint16_t word(int, int);
@@ -231,31 +233,8 @@ parseMacString(const char *strMac, uint16_t *mac)
if (strnlen(strMac, 20) != 17)
err(set_err(EINVAL), "Invalid MAC address string length");
- for (uint8_t h, i = 0; i < 16; i += 3) {
- if (i != 15)
- if (strMac[i + 2] != ':')
- err(set_err(EINVAL),
- "Invalid MAC address separator '%c'",
- strMac[i + 2]);
-
- int byte = i / 3;
-
- for (int nib = 0; nib < 2; nib++, total += h) {
- if ((h = hextonum(strMac[i + nib])) > 15)
- err(set_err(EINVAL), "Invalid character '%c'",
- strMac[i + nib]);
-
- /* If random, ensure that local/unicast bits are set */
- if ((byte == 0) && (nib == 1))
- if ((strMac[i + nib] == '?') ||
- (strMac[i + nib] == 'x') ||
- (strMac[i + nib] == 'X')) /* random */
- h = (h & 0xE) | 2; /* local, unicast */
-
- mac[byte >> 1] |= ((uint16_t ) h)
- << ((8 * (byte % 2)) + (4 * (nib ^ 1)));
- }
- }
+ for (int strMacPos = 0; strMacPos < 16; strMacPos += 3)
+ set_mac_byte(strMacPos, &total);
if (total == 0)
err(set_err(EINVAL), "Invalid MAC (all-zero MAC address)");
@@ -263,6 +242,47 @@ parseMacString(const char *strMac, uint16_t *mac)
err(set_err(EINVAL), "Invalid MAC (multicast bit set)");
}
+void
+set_mac_byte(int strMacPos, uint64_t *total)
+{
+ uint8_t h = 0;
+ check_mac_separator(strMacPos);
+
+ for (int nib = 0; nib < 2; nib++, *total += h)
+ set_mac_nib(strMacPos, nib, &h);
+}
+
+void
+check_mac_separator(int strMacPos)
+{
+ if (strMacPos == 15)
+ return;
+ char separator = strMac[strMacPos + 2];
+ if (separator == ':')
+ return;
+ err(set_err(EINVAL), "Invalid MAC address separator '%c'", separator);
+}
+
+void
+set_mac_nib(int strMacPos, int nib, uint8_t *h)
+{
+ int byte = strMacPos / 3;
+
+ if ((*h = hextonum(strMac[strMacPos + nib])) > 15)
+ err(set_err(EINVAL), "Invalid character '%c'",
+ strMac[strMacPos + nib]);
+
+ /* If random, ensure that local/unicast bits are set */
+ if ((byte == 0) && (nib == 1))
+ if ((strMac[strMacPos + nib] == '?') ||
+ (strMac[strMacPos + nib] == 'x') ||
+ (strMac[strMacPos + nib] == 'X')) /* random */
+ *h = (*h & 0xE) | 2; /* local, unicast */
+
+ mac[byte >> 1] |= ((uint16_t ) *h) << ((8 * (byte % 2)) +
+ (4 * (nib ^ 1)));
+}
+
uint8_t
hextonum(char ch)
{