summaryrefslogtreecommitdiff
path: root/util/nvmutil/nvmutil.c
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-18 19:30:32 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-19 04:25:43 +0000
commit2ed8db3adc19dd922e31082634146e159f65af2e (patch)
treedd242e1f7a178ee0c925cd080d61bac22fd681f5 /util/nvmutil/nvmutil.c
parent6ccd54635fdec85f44a9960b93c52fde89c07f41 (diff)
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 <leah@libreboot.org>
Diffstat (limited to 'util/nvmutil/nvmutil.c')
-rw-r--r--util/nvmutil/nvmutil.c117
1 files changed, 9 insertions, 108 deletions
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();