From 2ed8db3adc19dd922e31082634146e159f65af2e Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Wed, 18 Mar 2026 19:30:32 +0000 Subject: util/nvmutil: major cleanup handle init in xstatus() it's now a singleton design also tidied up some other code also removed todo.c. bloat. will do all those anyway. too much change. i just kept touching the code until it looked good Signed-off-by: Leah Rowe --- util/nvmutil/nvmutil.c | 117 ++++--------------------------------------------- 1 file changed, 9 insertions(+), 108 deletions(-) (limited to 'util/nvmutil/nvmutil.c') diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index 5217dd5f..8b4f01d1 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -5,9 +5,6 @@ * This tool lets you modify Intel GbE NVM (Gigabit Ethernet * Non-Volatile Memory) images, e.g. change the MAC address. * These images configure your Intel Gigabit Ethernet adapter. - * - * This code is designed to be portable, running on as many - * Unix and Unix-like systems as possible (mainly BSD/Linux). */ #ifdef __OpenBSD__ @@ -32,115 +29,19 @@ int main(int argc, char *argv[]) { - struct xstate *x; - struct commands *cmd; - struct xfile *f; - unsigned long i; - - char *tmp_path; - - struct stat st; - int fd; - - tmp_path = NULL; - -#ifndef S_ISREG - err(ECANCELED, "Can't determine file types (S_ISREG undefined)"); -#endif - -#ifndef CHAR_BIT - err(ECANCELED, "Unknown char size"); -#else - if (CHAR_BIT != 8) - err(EINVAL, "Unsupported char size"); -#endif - - if (argc < 3) - usage(); - -#ifdef NVMUTIL_UNVEIL - /* - * if global tmp is a different filesystem, - * unveil would trap on final file rename - * and we can't know the path in advance - */ - tmp_path = new_tmpfile(&fd, 1, NULL); -#else - tmp_path = new_tmpfile(&fd, 0, NULL); -#endif - - if (tmp_path == NULL) - err(errno, "Can't create tmpfile"); - -#ifdef NVMUTIL_PLEDGE -#ifdef NVMUTIL_UNVEIL - if (pledge("stdio flock rpath wpath cpath unveil", NULL) == -1) - err(errno, "pledge, unveil"); - if (unveil("/dev/null", "r") == -1) - err(errno, "unveil: /dev/null"); -#else - if (pledge("stdio flock rpath wpath cpath", NULL) == -1) - err(errno, "pledge"); -#endif -#endif - - x = xstatus(); - - if (x == NULL) - err(errno, NULL); - if (x->f.buf == NULL) - err(EINVAL, "Work buffer not initialised"); - - x->argv0 = argv[0]; - f = &x->f; - - f->fname = argv[1]; - f->tname = tmp_path; - f->tmp_fd = fd; - - if(fstat(fd, &st) < 0) - err(errno, "can't stat tmpfile"); - - f->tmp_dev = st.st_dev; - f->tmp_ino = st.st_ino; - - sanitize_command_list(); - - set_cmd(argc, argv); - set_cmd_args(argc, argv); - - i = x->i; - cmd = &x->cmd[i]; - -#ifdef NVMUTIL_UNVEIL - if ((cmd->flags & O_ACCMODE) == O_RDONLY) { - if (unveil(f->fname, "r") == -1) - err(errno, "%s: unveil r", f->fname); - } else { - if (unveil(f->tname, "rwc") == -1) - err(errno, "%s: unveil rw", f->tname); - } - - if (unveil(f->tname, "rwc") == -1) - err(errno, "%s: unveil rwc", f->tname); - - if (unveil(NULL, NULL) == -1) - err(errno, "unveil block (rw)"); - - if (pledge("stdio flock rpath wpath cpath", NULL) == -1) - err(errno, "pledge (kill unveil)"); -#endif - - open_gbe_file(); + struct xstate *x = xstatus(argc, argv); + struct commands *cmd = &x->cmd[x->i]; + struct xfile *f = &x->f; - memset(f->buf, 0, GBE_BUF_SIZE); - memset(f->bufcmp, 0, GBE_BUF_SIZE); + unsigned long c; - copy_gbe(); + if (cmd->run == NULL) + err(errno, "Command not set"); - read_checksums(); + cmd->run(); - run_cmd(); + for (c = 0; c < items(x->cmd); c++) + x->cmd[c].run = cmd_helper_err; if ((cmd->flags & O_ACCMODE) == O_RDWR) write_to_gbe_bin(); -- cgit v1.2.1