summaryrefslogtreecommitdiff
path: root/util/nvmutil
diff options
context:
space:
mode:
Diffstat (limited to 'util/nvmutil')
-rw-r--r--util/nvmutil/AUTHORS1
-rw-r--r--util/nvmutil/COPYING3
-rw-r--r--util/nvmutil/Makefile20
-rw-r--r--util/nvmutil/nvmutil.c91
4 files changed, 67 insertions, 48 deletions
diff --git a/util/nvmutil/AUTHORS b/util/nvmutil/AUTHORS
index f3c00385..f38ea210 100644
--- a/util/nvmutil/AUTHORS
+++ b/util/nvmutil/AUTHORS
@@ -1 +1,2 @@
Leah Rowe
+Riku Viitanen
diff --git a/util/nvmutil/COPYING b/util/nvmutil/COPYING
index 784581dd..a6ecf266 100644
--- a/util/nvmutil/COPYING
+++ b/util/nvmutil/COPYING
@@ -1,4 +1,5 @@
-Copyright (C) 2022, 2023 Leah Rowe <leah@libreboot.org>
+Copyright (C) 2022-2025 Leah Rowe <leah@libreboot.org>
+Copyright (c) 2023 Riku Viitanen <riku.viitanen@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
diff --git a/util/nvmutil/Makefile b/util/nvmutil/Makefile
index f25f6dd5..b8ec2ad3 100644
--- a/util/nvmutil/Makefile
+++ b/util/nvmutil/Makefile
@@ -1,16 +1,24 @@
# SPDX-License-Identifier: MIT
-# SPDX-FileCopyrightText: 2022 Leah Rowe <leah@libreboot.org>
+# SPDX-FileCopyrightText: 2022,2025 Leah Rowe <leah@libreboot.org>
# SPDX-FileCopyrightText: 2023 Riku Viitanen <riku.viitanen@protonmail.com>
-CC=cc
-CFLAGS=-Os -Wall -Wextra -Werror -pedantic
-PREFIX?=/usr/bin
+CC?=cc
+CFLAGS?=-Os -Wall -Wextra -Werror -pedantic
+DESTDIR?=
+PREFIX?=/usr/local
+INSTALL?=install
nvm: nvmutil.c
$(CC) $(CFLAGS) nvmutil.c -o nvm
-install: nvm
- install nvm $(PREFIX)/nvm
+install:
+ $(INSTALL) nvm $(DESTDIR)$(PREFIX)/bin/nvm
+
+uninstall:
+ rm -f $(DESTDIR)$(PREFIX)/bin/nvm
+
+distclean:
+ rm -f nvm
clean:
rm -f nvm
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c
index 35abbfae..68bb95da 100644
--- a/util/nvmutil/nvmutil.c
+++ b/util/nvmutil/nvmutil.c
@@ -1,6 +1,6 @@
/* SPDX-License-Identifier: MIT */
-/* SPDX-FileCopyrightText: 2022, 2023 Leah Rowe <leah@libreboot.org> */
-/* SPDX-FileCopyrightText: 2023 Riku Viitanen <riku.viitanen@protonmail.com> */
+/* Copyright (c) 2022-2025 Leah Rowe <leah@libreboot.org> */
+/* Copyright (c) 2023 Riku Viitanen <riku.viitanen@protonmail.com> */
#include <sys/stat.h>
@@ -14,22 +14,23 @@
#include <string.h>
#include <unistd.h>
-void cmd_setchecksum(void), cmd_brick(void), cmd_copy(void), writeGbeFile(void),
- cmd_dump(void), cmd_setmac(void), readGbeFile(void), showmac(int partnum),
- hexdump(int partnum), handle_endianness(int partnum), openFiles(const char *path);
-int macAddress(const char *strMac, uint16_t *mac), validChecksum(int partnum);
+void cmd_setchecksum(void), cmd_brick(void), swap(int partnum), writeGbe(void),
+ cmd_dump(void), cmd_setmac(void), readGbe(void), cmd_copy(void),
+ macf(int partnum), hexdump(int partnum), openFiles(const char *path);
+int macAddress(const char *strMac, uint16_t *mac), goodChecksum(int partnum);
uint8_t hextonum(char chs), rhex(void);
#define COMMAND argv[2]
#define MAC_ADDRESS argv[3]
-#define PARTNUM argv[3]
+#define PARTN argv[3]
#define SIZE_4KB 0x1000
+#define NVM_CHECKSUM 0xBABA
uint16_t buf16[SIZE_4KB], mac[3] = {0, 0, 0};
uint8_t *buf = (uint8_t *) &buf16;
size_t nf = 128, gbe[2];
-uint8_t nvmPartModified[2] = {0, 0}, skipread[2] = {0, 0};
-int e = 1, flags = O_RDWR, rfd, fd, part, gbeFileModified = 0;
+uint8_t nvmPartChanged[2] = {0, 0}, skipread[2] = {0, 0};
+int e = 1, flags = O_RDWR, rfd, fd, part, gbeFileChanged = 0;
const char *strMac = NULL, *strRMac = "??:??:??:??:??:??", *filename = NULL;
@@ -41,7 +42,7 @@ typedef struct op {
op_t op[] = {
{ .str = "dump", .cmd = cmd_dump, .args = 3},
{ .str = "setmac", .cmd = cmd_setmac, .args = 3},
-{ .str = "swap", .cmd = writeGbeFile, .args = 3},
+{ .str = "swap", .cmd = writeGbe, .args = 3},
{ .str = "copy", .cmd = cmd_copy, .args = 4},
{ .str = "brick", .cmd = cmd_brick, .args = 4},
{ .str = "setchecksum", .cmd = cmd_setchecksum, .args = 4},
@@ -56,13 +57,14 @@ void (*cmd)(void) = NULL;
if (fstat(f, &st) == -1) err(ERR(), "%s", l)
#define word(pos16, partnum) buf16[pos16 + (partnum << 11)]
-#define setWord(pos16, p, val16) if ((gbeFileModified = 1) && \
- word(pos16, p) != val16) nvmPartModified[p] = 1 | (word(pos16, p) = val16)
+#define setWord(pos16, p, val16) if ((gbeFileChanged = 1) && \
+ word(pos16, p) != val16) nvmPartChanged[p] = 1 | (word(pos16, p) = val16)
int
main(int argc, char *argv[])
{
if (argc < 3) {
+ fprintf(stderr, "Modify Intel GbE NVM images e.g. set MAC\n");
fprintf(stderr, "USAGE:\n");
fprintf(stderr, " %s FILE dump\n", argv[0]);
fprintf(stderr, " %s FILE setmac [MAC]\n", argv[0]);
@@ -76,9 +78,13 @@ main(int argc, char *argv[])
filename = argv[1];
#ifdef __OpenBSD__
err_if(unveil("/dev/urandom", "r") == -1);
- err_if(unveil(filename, flags == O_RDONLY ? "r" : "rw") == -1);
- err_if(pledge(flags == O_RDONLY ? "stdio rpath" : "stdio rpath wpath",
- NULL) == -1);
+ if (flags == O_RDONLY) {
+ err_if(unveil(filename, "r") == -1);
+ err_if(pledge("stdio rpath", NULL) == -1);
+ } else {
+ err_if(unveil(filename, "rw") == -1);
+ err_if(pledge("stdio rpath wpath", NULL) == -1);
+ }
#endif
openFiles(filename);
#ifdef __OpenBSD__
@@ -92,15 +98,15 @@ main(int argc, char *argv[])
if (cmd == cmd_setmac)
strMac = (argc > 3) ? MAC_ADDRESS : strRMac;
else if ((cmd != NULL) && (argc > 3))
- err_if((errno = (!((part = PARTNUM[0] - '0') == 0 || part == 1))
- || PARTNUM[1] ? EINVAL : errno));
+ err_if((errno = (!((part = PARTN[0] - '0') == 0 || part == 1))
+ || PARTN[1] ? EINVAL : errno));
err_if((errno = (cmd == NULL) ? EINVAL : errno));
- readGbeFile();
+ readGbe();
(*cmd)();
- if ((gbeFileModified) && (flags != O_RDONLY) && (cmd != writeGbeFile))
- writeGbeFile();
+ if ((gbeFileChanged) && (flags != O_RDONLY) && (cmd != writeGbe))
+ writeGbe();
err_if((errno != 0) && (cmd != cmd_dump));
return errno;
}
@@ -117,9 +123,9 @@ openFiles(const char *path)
}
void
-readGbeFile(void)
+readGbe(void)
{
- nf = ((cmd == writeGbeFile) || (cmd == cmd_copy)) ? SIZE_4KB : nf;
+ nf = ((cmd == writeGbe) || (cmd == cmd_copy)) ? SIZE_4KB : nf;
skipread[part ^ 1] = (cmd == cmd_copy) | (cmd == cmd_setchecksum)
| (cmd == cmd_brick);
gbe[1] = (gbe[0] = (size_t) buf) + SIZE_4KB;
@@ -127,7 +133,7 @@ readGbeFile(void)
if (skipread[p])
continue;
err_if(pread(fd, (uint8_t *) gbe[p], nf, p << 12) == -1);
- handle_endianness(p);
+ swap(p);
}
}
@@ -137,7 +143,7 @@ cmd_setmac(void)
if (macAddress(strMac, mac))
err(errno = ECANCELED, "Bad MAC address");
for (int partnum = 0; partnum < 2; partnum++) {
- if (!validChecksum(part = partnum))
+ if (!goodChecksum(part = partnum))
continue;
for (int w = 0; w < 3; w++)
setWord(w, partnum, mac[w]);
@@ -193,16 +199,16 @@ void
cmd_dump(void)
{
for (int partnum = 0, numInvalid = 0; partnum < 2; partnum++) {
- if (!validChecksum(partnum))
+ if (!goodChecksum(partnum))
++numInvalid;
printf("MAC (part %d): ", partnum);
- showmac(partnum), hexdump(partnum);
+ macf(partnum), hexdump(partnum);
errno = ((numInvalid < 2) && (partnum)) ? 0 : errno;
}
}
void
-showmac(int partnum)
+macf(int partnum)
{
for (int c = 0; c < 3; c++) {
uint16_t val16 = word(c, partnum);
@@ -215,10 +221,12 @@ void
hexdump(int partnum)
{
for (int row = 0; row < 8; row++) {
- printf("%07x", row << 4);
+ printf("%08x ", row << 4);
for (int c = 0; c < 8; c++) {
uint16_t val16 = word((row << 3) + c, partnum);
- printf(" %02x%02x", val16 >> 8, val16 & 0xff);
+ if (c == 4)
+ printf(" ");
+ printf(" %02x %02x", val16 & 0xff, val16 >> 8);
} printf("\n");
}
}
@@ -229,43 +237,43 @@ cmd_setchecksum(void)
uint16_t val16 = 0;
for (int c = 0; c < 0x3F; c++)
val16 += word(c, part);
- setWord(0x3F, part, 0xBABA - val16);
+ setWord(0x3F, part, NVM_CHECKSUM - val16);
}
void
cmd_brick(void)
{
- if (validChecksum(part))
+ if (goodChecksum(part))
setWord(0x3F, part, ((word(0x3F, part)) ^ 0xFF));
}
void
cmd_copy(void)
{
- if ((gbeFileModified = nvmPartModified[part ^ 1] = validChecksum(part)))
+ if ((gbeFileChanged = nvmPartChanged[part ^ 1] = goodChecksum(part)))
gbe[part ^ 1] = gbe[part]; /* speedhack: copy ptr, not words */
}
int
-validChecksum(int partnum)
+goodChecksum(int partnum)
{
uint16_t total = 0;
for(int w = 0; w <= 0x3F; w++)
total += word(w, partnum);
- if (total == 0xBABA)
+ if (total == NVM_CHECKSUM)
return 1;
fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum);
return (errno = ECANCELED) & 0;
}
void
-writeGbeFile(void)
+writeGbe(void)
{
- err_if((cmd == writeGbeFile) && !(validChecksum(0) || validChecksum(1)));
- for (int p = 0, x = (cmd == writeGbeFile) ? 1 : 0; p < 2; p++) {
- if ((!nvmPartModified[p]) && (cmd != writeGbeFile))
+ err_if((cmd == writeGbe) && !(goodChecksum(0) || goodChecksum(1)));
+ for (int p = 0, x = (cmd == writeGbe) ? 1 : 0; p < 2; p++) {
+ if ((!nvmPartChanged[p]) && (cmd != writeGbe))
continue;
- handle_endianness(p^x);
+ swap(p^x);
err_if(pwrite(fd, (uint8_t *) gbe[p^x], nf, p << 12) == -1);
}
errno = 0;
@@ -273,9 +281,10 @@ writeGbeFile(void)
}
void
-handle_endianness(int partnum)
+swap(int partnum)
{
+ size_t w, x;
uint8_t *n = (uint8_t *) gbe[partnum];
- for (size_t w = nf * ((uint8_t *) &e)[0], x = 1; w < nf; w += 2, x += 2)
+ for (w = nf * ((uint8_t *) &e)[0], x = 1; w < nf; w += 2, x += 2)
n[w] ^= n[x], n[x] ^= n[w], n[w] ^= n[x];
}