diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-29 07:21:08 +0100 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-29 07:22:34 +0100 |
| commit | afe2e71c014e291b9915271d45c1fd933f2fa55f (patch) | |
| tree | 5263dcf1b0e2c5f1824635630c4517be60087442 /util | |
| parent | 546565f32103da7fc66ddddeaabe9cbcc30c831a (diff) | |
util/nvmutil: better hexdump
this is a more generic one that i implemented
for "lottery.c" (which is really just a tester
of my rset function in lib/rand.c)
i could probably actually write a full hexdump
program in libreboot-utils to be honest.
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util')
| -rw-r--r-- | util/libreboot-utils/include/common.h | 2 | ||||
| -rw-r--r-- | util/libreboot-utils/lib/command.c | 32 | ||||
| -rw-r--r-- | util/libreboot-utils/lib/num.c | 44 | ||||
| -rw-r--r-- | util/libreboot-utils/lottery.c | 45 |
4 files changed, 47 insertions, 76 deletions
diff --git a/util/libreboot-utils/include/common.h b/util/libreboot-utils/include/common.h index 6956adb1..abd153be 100644 --- a/util/libreboot-utils/include/common.h +++ b/util/libreboot-utils/include/common.h @@ -395,6 +395,7 @@ int dcat(const char *s, size_t n, */ unsigned short hextonum(char ch_s); +void spew_hex(const void *data, size_t len); void *mkrbuf(size_t n); void rset(void *buf, size_t n); void *mkrbuf(size_t n); @@ -406,7 +407,6 @@ size_t rsize(size_t n); void cmd_helper_dump(void); void print_mac_from_nvm(size_t partnum); -void hexdump(size_t partnum); /* Helper functions for command: swap */ diff --git a/util/libreboot-utils/lib/command.c b/util/libreboot-utils/lib/command.c index d0f783dd..a1e46e5f 100644 --- a/util/libreboot-utils/lib/command.c +++ b/util/libreboot-utils/lib/command.c @@ -364,8 +364,7 @@ cmd_helper_dump(void) (size_t)p); print_mac_from_nvm(p); - - hexdump(p); + spew_hex(f->buf + (p * GBE_PART_SIZE), NVM_SIZE); } } @@ -391,35 +390,6 @@ print_mac_from_nvm(size_t partnum) } void -hexdump(size_t partnum) -{ - size_t c; - size_t row; - unsigned short val16; - - for (row = 0; row < 8; row++) { - - printf("%08lx ", - (size_t)((size_t)row << 4)); - - for (c = 0; c < 8; c++) { - - val16 = nvm_word((row << 3) + c, partnum); - - if (c == 4) - printf(" "); - - printf(" %02x %02x", - (unsigned int)(val16 & 0xff), - (unsigned int)(val16 >> 8)); - - } - - printf("\n"); - } -} - -void cmd_helper_swap(void) { struct xstate *x = xstatus(); diff --git a/util/libreboot-utils/lib/num.c b/util/libreboot-utils/lib/num.c index 66fc26f1..e4d0ce6b 100644 --- a/util/libreboot-utils/lib/num.c +++ b/util/libreboot-utils/lib/num.c @@ -16,8 +16,10 @@ defined(__NetBSD__) || defined(__APPLE__)) #include <fcntl.h> /* if not arc4random: /dev/urandom */ #endif +#include <ctype.h> #include <limits.h> #include <stddef.h> +#include <stdio.h> #include <string.h> #include <unistd.h> @@ -47,6 +49,48 @@ hextonum(char ch_s) return 16; } +/* basically hexdump -C */ +void +spew_hex(const void *data, size_t len) +{ + const unsigned char *buf = (const unsigned char *)data; + unsigned char c; + size_t i; + size_t j; + + if (buf == NULL || + len == 0) + return; + + for (i = 0; i < len; i += 16) { + + printf("%08zx ", i); + + for (j = 0; j < 16; j++) { + + if (i + j < len) + printf("%02x ", buf[i + j]); + else + printf(" "); + + if (j == 7) + printf(" "); + } + + printf(" |"); + + for (j = 0; j < 16 && i + j < len; j++) { + + c = buf[i + j]; + printf("%c", isprint(c) ? c : '.'); + } + + printf("|\n"); + } + + printf("%08zx\n", len); +} + void check_bin(size_t a, const char *a_name) { diff --git a/util/libreboot-utils/lottery.c b/util/libreboot-utils/lottery.c index 2ead8563..0b3719a4 100644 --- a/util/libreboot-utils/lottery.c +++ b/util/libreboot-utils/lottery.c @@ -13,9 +13,6 @@ static void exit_cleanup(void); -static void -spew_buf(const void *data, size_t len); - int main(int argc, char **argv) { @@ -35,7 +32,7 @@ main(int argc, char **argv) same = 1; if (argc < 2) /* no spew */ - spew_buf(buf, BUFSIZ); + spew_hex(buf, BUFSIZ); free(buf); fprintf(stderr, "\n%s\n", same ? "You win!" : "You lose!"); @@ -43,46 +40,6 @@ main(int argc, char **argv) } static void -spew_buf(const void *data, size_t len) -{ - const unsigned char *buf = data; - unsigned char c; - size_t i, j; - - if (buf == NULL || - len == 0) - return; - - for (i = 0; i < len; i += 16) { - - printf("%08zx ", i); - - for (j = 0; j < 16; j++) { - - if (i + j < len) - printf("%02x ", buf[i + j]); - else - printf(" "); - - if (j == 7) - printf(" "); - } - - printf(" |"); - - for (j = 0; j < 16 && i + j < len; j++) { - - c = buf[i + j]; - printf("%c", isprint(c) ? c : '.'); - } - - printf("|\n"); - } - - printf("%08zx\n", len); -} - -static void exit_cleanup(void) { #if defined(__OpenBSD__) |
