util/nvmutil: minor code cleanup
also removed some unnecessary checks fixed the check of pwrite's return value (it should check for -1)fsdg20230625
parent
2044bf32d3
commit
01e2ed3034
|
@ -22,7 +22,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the Libreboot project. Documentation available here:
|
* This file is part of Libreboot. See:
|
||||||
* https://libreboot.org/docs/install/nvmutil.html
|
* https://libreboot.org/docs/install/nvmutil.html
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -82,9 +82,9 @@ main(int argc, char *argv[])
|
||||||
void (*cmd)(void) = NULL;
|
void (*cmd)(void) = NULL;
|
||||||
const char *strMac = NULL, *strRMac = "??:??:??:??:??:??";
|
const char *strMac = NULL, *strRMac = "??:??:??:??:??:??";
|
||||||
|
|
||||||
#ifdef HAVE_PLEDGE
|
#ifdef HAVE_PLEDGE /* openbsd */
|
||||||
if (pledge("stdio wpath", NULL) == -1) /* only used on openbsd */
|
if (pledge("stdio wpath", NULL) == -1)
|
||||||
err(errno, "pledge"); /* https://man.openbsd.org/pledge.2 */
|
err(errno, "pledge");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
buf = (uint8_t *) &buf16;
|
buf = (uint8_t *) &buf16;
|
||||||
|
@ -95,8 +95,8 @@ main(int argc, char *argv[])
|
||||||
|
|
||||||
if (argc == 3) {
|
if (argc == 3) {
|
||||||
if (strcmp(COMMAND, "dump") == 0) {
|
if (strcmp(COMMAND, "dump") == 0) {
|
||||||
#ifdef HAVE_PLEDGE
|
#ifdef HAVE_PLEDGE /* openbsd */
|
||||||
if (pledge("stdio rpath", NULL) == -1) /* openbsd */
|
if (pledge("stdio rpath", NULL) == -1)
|
||||||
err(errno, "pledge");
|
err(errno, "pledge");
|
||||||
#endif
|
#endif
|
||||||
flags = O_RDONLY;
|
flags = O_RDONLY;
|
||||||
|
@ -122,7 +122,7 @@ main(int argc, char *argv[])
|
||||||
if ((strMac == NULL) && (cmd == NULL))
|
if ((strMac == NULL) && (cmd == NULL))
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
if (errno != 0)
|
if (errno != 0)
|
||||||
goto nvmutil_exit;
|
err(errno, NULL);
|
||||||
|
|
||||||
nr = SIZE_4KB; /* copy/swap commands need everything to be read */
|
nr = SIZE_4KB; /* copy/swap commands need everything to be read */
|
||||||
if ((cmd != &cmd_copy) && (cmd != &cmd_swap))
|
if ((cmd != &cmd_copy) && (cmd != &cmd_swap))
|
||||||
|
@ -144,7 +144,6 @@ main(int argc, char *argv[])
|
||||||
else if (gbeWriteAttempted && (cmd != &cmd_dump))
|
else if (gbeWriteAttempted && (cmd != &cmd_dump))
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
||||||
nvmutil_exit:
|
|
||||||
if ((errno != 0) && (cmd != &cmd_dump))
|
if ((errno != 0) && (cmd != &cmd_dump))
|
||||||
err(errno, NULL);
|
err(errno, NULL);
|
||||||
return errno;
|
return errno;
|
||||||
|
@ -154,8 +153,6 @@ void
|
||||||
readGbeFile(int *fd, const char *path, int flags, size_t nr)
|
readGbeFile(int *fd, const char *path, int flags, size_t nr)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
int p, r;
|
|
||||||
|
|
||||||
if (opendir(path) != NULL)
|
if (opendir(path) != NULL)
|
||||||
err(errno = EISDIR, "%s", path);
|
err(errno = EISDIR, "%s", path);
|
||||||
else if (((*fd) = open(path, flags)) == -1)
|
else if (((*fd) = open(path, flags)) == -1)
|
||||||
|
@ -166,13 +163,9 @@ readGbeFile(int *fd, const char *path, int flags, size_t nr)
|
||||||
err(errno = ECANCELED, "File \"%s\" not of size 8KiB", path);
|
err(errno = ECANCELED, "File \"%s\" not of size 8KiB", path);
|
||||||
else if (errno == ENOTDIR)
|
else if (errno == ENOTDIR)
|
||||||
errno = 0;
|
errno = 0;
|
||||||
else if (errno != 0)
|
|
||||||
err(errno, "%s", path);
|
|
||||||
|
|
||||||
for (p = 0; p < 2; p++) {
|
for (int p = 0; (p < 2) && (!skipread[p]); p++) {
|
||||||
if (skipread[p])
|
if (pread((*fd), (uint8_t *) gbe[p], nr, p << 12) == -1)
|
||||||
continue;
|
|
||||||
if ((r = pread((*fd), (uint8_t *) gbe[p], nr, p << 12)) == -1)
|
|
||||||
err(errno, "%s", path);
|
err(errno, "%s", path);
|
||||||
if (big_endian)
|
if (big_endian)
|
||||||
byteswap(nr, p);
|
byteswap(nr, p);
|
||||||
|
@ -213,7 +206,6 @@ parseMacAddress(const char *strMac, uint16_t *mac)
|
||||||
for (int nib = 0; nib < 2; nib++, total += h) {
|
for (int nib = 0; nib < 2; nib++, total += h) {
|
||||||
if ((h = hextonum(strMac[i + nib])) > 15)
|
if ((h = hextonum(strMac[i + nib])) > 15)
|
||||||
return -1;
|
return -1;
|
||||||
/* ensure local, unicast mac address if random: */
|
|
||||||
if ((byte == 0) && (nib == 1))
|
if ((byte == 0) && (nib == 1))
|
||||||
if (strMac[i + nib] == '?')
|
if (strMac[i + nib] == '?')
|
||||||
h = (h & 0xE) | 2; /* local, unicast */
|
h = (h & 0xE) | 2; /* local, unicast */
|
||||||
|
@ -222,7 +214,7 @@ parseMacAddress(const char *strMac, uint16_t *mac)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ((total == 0) || (mac[0] & 0x100))
|
return ((total == 0) || (mac[0] & 0x100))
|
||||||
? -1 : 0; /* disallow multicast/zero mac addresses */
|
? -1 : 0; /* multicast/all-zero not permitted */
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t
|
uint8_t
|
||||||
|
@ -279,10 +271,7 @@ showmac(int partnum)
|
||||||
for (int c = 0; c < 3; c++) {
|
for (int c = 0; c < 3; c++) {
|
||||||
val16 = word(c, partnum);
|
val16 = word(c, partnum);
|
||||||
printf("%02x:%02x", val16 & 0xff, val16 >> 8);
|
printf("%02x:%02x", val16 & 0xff, val16 >> 8);
|
||||||
if (c == 2)
|
printf(c == 2 ? "\n" : ":");
|
||||||
printf("\n");
|
|
||||||
else
|
|
||||||
printf(":");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -323,9 +312,7 @@ cmd_swap(void)
|
||||||
gbe[0] ^= gbe[1]; /* speedhack: xorswap pointers, not words */
|
gbe[0] ^= gbe[1]; /* speedhack: xorswap pointers, not words */
|
||||||
gbe[1] ^= gbe[0];
|
gbe[1] ^= gbe[0];
|
||||||
gbe[0] ^= gbe[1];
|
gbe[0] ^= gbe[1];
|
||||||
gbeFileModified = 1; /* not using setWord, so must set these */
|
gbeFileModified = nvmPartModified[0] = nvmPartModified[1] = 1;
|
||||||
nvmPartModified[0] = 1;
|
|
||||||
nvmPartModified[1] = 1;
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -335,8 +322,7 @@ cmd_copy(void)
|
||||||
{
|
{
|
||||||
if (validChecksum(part)) {
|
if (validChecksum(part)) {
|
||||||
gbe[part ^ 1] = gbe[part]; /* speedhack: copy ptr, not words */
|
gbe[part ^ 1] = gbe[part]; /* speedhack: copy ptr, not words */
|
||||||
gbeFileModified = 1; /* not using setWord, so must set these */
|
gbeFileModified = nvmPartModified[part ^ 1] = 1;
|
||||||
nvmPartModified[part ^ 1] = 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -348,10 +334,8 @@ validChecksum(int partnum)
|
||||||
total += word(w, partnum);
|
total += word(w, partnum);
|
||||||
if (total == 0xBABA)
|
if (total == 0xBABA)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum);
|
fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum);
|
||||||
errno = ECANCELED;
|
return (errno = ECANCELED) & 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t
|
uint16_t
|
||||||
|
@ -367,8 +351,7 @@ setWord(int pos16, int partnum, uint16_t val16)
|
||||||
if (word(pos16, partnum) == val16)
|
if (word(pos16, partnum) == val16)
|
||||||
return;
|
return;
|
||||||
buf16[pos16 + (partnum << 11)] = val16;
|
buf16[pos16 + (partnum << 11)] = val16;
|
||||||
gbeFileModified = 1;
|
gbeFileModified = nvmPartModified[partnum] = 1;
|
||||||
nvmPartModified[partnum] = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -399,7 +382,7 @@ writeGbeFile(int *fd, const char *filename)
|
||||||
goto next_part;
|
goto next_part;
|
||||||
if (big_endian)
|
if (big_endian)
|
||||||
byteswap(nw, p);
|
byteswap(nw, p);
|
||||||
if (pwrite((*fd), (uint8_t *) gbe[p], nw, p << 12) != nw)
|
if (pwrite((*fd), (uint8_t *) gbe[p], nw, p << 12) == -1)
|
||||||
err(errno, "%s", filename);
|
err(errno, "%s", filename);
|
||||||
next_part:
|
next_part:
|
||||||
if (gbe[0] > gbe[1])
|
if (gbe[0] > gbe[1])
|
||||||
|
|
Loading…
Reference in New Issue