diff options
author | Leah Rowe <leah@libreboot.org> | 2022-12-03 12:27:53 +0000 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2022-12-03 12:28:20 +0000 |
commit | 036d710776ba0065560f42f28d081047d552dd0e (patch) | |
tree | 37746d3106d1b53b51c54024a258867349e5fbc7 /util | |
parent | 851892b4642d7247f7929f1f0ceef2425f463adf (diff) |
util/nvmutil: don't initialise rbuf unless needed
previously, it was always initialised, but now it's only
initialised if '?' is used on a mac address character in
command `setmac`
this is done by simply moving mac address character
randomisation to a separate function
Diffstat (limited to 'util')
-rw-r--r-- | util/nvmutil/nvmutil.c | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index bf06b4cd..b45e9064 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -36,6 +36,7 @@ ssize_t readFromFile(int *fd, uint8_t *buf, const char *path, int flags, size_t size); void cmd_setmac(const char *strMac); uint8_t hextonum(char chs); +uint8_t rhex(void); void cmd_dump(void); void showmac(int partnum); void hexdump(int partnum); @@ -217,6 +218,26 @@ uint8_t hextonum(char chs) { uint8_t val8, ch; + ch = (uint8_t) chs; + + if ((ch >= '0') && ch <= '9') { + val8 = ch - '0'; + } else if ((ch >= 'A') && (ch <= 'F')) { + val8 = ch - 'A' + 10; + } else if ((ch >= 'a') && (ch <= 'f')) { + val8 = ch - 'a' + 10; + } else if (ch == '?') { + val8 = rhex(); + } else { + return 16; + } + + return val8; +} + +uint8_t +rhex(void) +{ static int rfd = -1; static uint8_t *rbuf = NULL; static size_t rindex = BUFSIZ; @@ -237,21 +258,7 @@ hextonum(char chs) } } - ch = (uint8_t) chs; - - if ((ch >= '0') && ch <= '9') { - val8 = ch - '0'; - } else if ((ch >= 'A') && (ch <= 'F')) { - val8 = ch - 'A' + 10; - } else if ((ch >= 'a') && (ch <= 'f')) { - val8 = ch - 'a' + 10; - } else if (ch == '?') { - val8 = rbuf[rindex++] & 0xf; - } else { - return 16; - } - - return val8; + return rbuf[rindex++] & 0xf; } void |