util/nvmutil: less annoying comments

added a few that were more useful

deleted a few obnoxious ones
This commit is contained in:
Leah Rowe
2023-04-06 20:48:28 +01:00
parent 8771551162
commit 2356f89f27
+16 -37
View File
@@ -91,13 +91,13 @@ main(int argc, char *argv[])
gbe[1] = (gbe[0] = (size_t) buf) + SIZE_4KB; gbe[1] = (gbe[0] = (size_t) buf) + SIZE_4KB;
test = 1; test = 1;
big_endian = ((uint8_t *) &test)[0] ^ 1; /* check host endianness */ big_endian = ((uint8_t *) &test)[0] ^ 1;
if (argc == 3) { if (argc == 3) {
if (strcmp(COMMAND, "dump") == 0) { if (strcmp(COMMAND, "dump") == 0) {
#ifdef HAVE_PLEDGE #ifdef HAVE_PLEDGE
if (pledge("stdio rpath", NULL) == -1) if (pledge("stdio rpath", NULL) == -1) /* openbsd */
err(errno, "pledge"); /* not very puffy */ err(errno, "pledge");
#endif #endif
flags = O_RDONLY; flags = O_RDONLY;
cmd = &cmd_dump; cmd = &cmd_dump;
@@ -126,19 +126,18 @@ main(int argc, char *argv[])
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))
nr = 128; /* read only the nvm part */ nr = 128; /* speedhack: read only the nvm part */
if ((cmd == &cmd_copy) || (cmd == &cmd_setchecksum) || if ((cmd == &cmd_copy) || (cmd == &cmd_setchecksum) ||
(cmd == &cmd_brick)) (cmd == &cmd_brick))
skipread[part ^ 1] = 1; /* skip reading the unused part */ skipread[part ^ 1] = 1; /* speedhack: don't read unused part */
readGbeFile(&fd, FILENAME, flags, nr); readGbeFile(&fd, FILENAME, flags, nr);
/* perform operation specified by the user */
if (strMac != NULL) if (strMac != NULL)
cmd_setmac(strMac); cmd_setmac(strMac); /* nvm gbe.bin setmac */
else if (cmd != NULL) else if (cmd != NULL)
(*cmd)(); (*cmd)(); /* all other commands except setmac */
if (gbeFileModified) if (gbeFileModified)
writeGbeFile(&fd, FILENAME); writeGbeFile(&fd, FILENAME);
@@ -151,7 +150,6 @@ nvmutil_exit:
return errno; return errno;
} }
/* load the gbe file into memory */
void void
readGbeFile(int *fd, const char *path, int flags, size_t nr) readGbeFile(int *fd, const char *path, int flags, size_t nr)
{ {
@@ -181,7 +179,6 @@ readGbeFile(int *fd, const char *path, int flags, size_t nr)
} }
} }
/* set mac address on valid nvm parts, per user input */
void void
cmd_setmac(const char *strMac) cmd_setmac(const char *strMac)
{ {
@@ -193,20 +190,18 @@ cmd_setmac(const char *strMac)
if (!validChecksum(partnum)) if (!validChecksum(partnum))
continue; continue;
for (int w = 0; w < 3; w++) for (int w = 0; w < 3; w++)
setWord(w, partnum, mac[w]); /* do not use memcpy! */ setWord(w, partnum, mac[w]);
byteswap(6, partnum); /* mac words are stored big-endian */ byteswap(6, partnum); /* mac words are stored big-endian */
part = partnum; part = partnum;
cmd_setchecksum(); cmd_setchecksum();
} }
} }
/* read mac address from user input */
int int
parseMacAddress(const char *strMac, uint16_t *mac) parseMacAddress(const char *strMac, uint16_t *mac)
{ {
uint8_t h; uint8_t h;
uint64_t total = 0; uint64_t total = 0;
if (strnlen(strMac, 20) != 17) if (strnlen(strMac, 20) != 17)
return -1; return -1;
@@ -226,10 +221,8 @@ parseMacAddress(const char *strMac, uint16_t *mac)
<< ((8 * ((byte % 2) ^ 1)) + (4 * (nib ^ 1))); << ((8 * ((byte % 2) ^ 1)) + (4 * (nib ^ 1)));
} }
} }
/* Disallow multicast and all-zero MAC addresses */
return ((total == 0) || (mac[0] & 0x100)) return ((total == 0) || (mac[0] & 0x100))
? -1 : 0; ? -1 : 0; /* disallow multicast/zero mac addresses */
} }
uint8_t uint8_t
@@ -247,7 +240,6 @@ hextonum(char ch)
return 16; return 16;
} }
/* generate a random number from 0 to 15, the unix way */
uint8_t uint8_t
rhex(void) rhex(void)
{ {
@@ -265,7 +257,6 @@ rhex(void)
return rval; return rval;
} }
/* show mac address and hexdump of each nvm part */
void void
cmd_dump(void) cmd_dump(void)
{ {
@@ -281,7 +272,6 @@ cmd_dump(void)
errno = 0; errno = 0;
} }
/* show formatted mac address contained within the nvm part */
void void
showmac(int partnum) showmac(int partnum)
{ {
@@ -296,7 +286,6 @@ showmac(int partnum)
} }
} }
/* output a hexdump of both nvm parts, bytes reversed to big-endian order */
void void
hexdump(int partnum) hexdump(int partnum)
{ {
@@ -311,7 +300,6 @@ hexdump(int partnum)
} }
} }
/* set a valid checksum on a given part */
void void
cmd_setchecksum(void) cmd_setchecksum(void)
{ {
@@ -321,7 +309,6 @@ cmd_setchecksum(void)
setWord(0x3F, part, 0xBABA - val16); setWord(0x3F, part, 0xBABA - val16);
} }
/* set invalid checksum, intentionally */
void void
cmd_brick(void) cmd_brick(void)
{ {
@@ -329,12 +316,11 @@ cmd_brick(void)
setWord(0x3F, part, (word(0x3F, part)) ^ 0xFF); setWord(0x3F, part, (word(0x3F, part)) ^ 0xFF);
} }
/* swap the words of each part with each other */
void void
cmd_swap(void) cmd_swap(void)
{ {
if (validChecksum(1) || validChecksum(0)) { if (validChecksum(1) || validChecksum(0)) {
gbe[0] ^= gbe[1]; /* speed hack: 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 = 1; /* not using setWord, so must set these */
@@ -344,12 +330,11 @@ cmd_swap(void)
} }
} }
/* overwrite one part with the other */
void void
cmd_copy(void) cmd_copy(void)
{ {
if (validChecksum(part)) { if (validChecksum(part)) {
gbe[part ^ 1] = gbe[part]; /* pointer-based speed hack */ gbe[part ^ 1] = gbe[part]; /* speedhack: copy ptr, not words */
gbeFileModified = 1; /* not using setWord, so must set these */ gbeFileModified = 1; /* not using setWord, so must set these */
nvmPartModified[part ^ 1] = 1; nvmPartModified[part ^ 1] = 1;
} }
@@ -369,14 +354,12 @@ validChecksum(int partnum)
return 0; return 0;
} }
/* fetch a word from gbe buffer */
uint16_t uint16_t
word(int pos16, int partnum) word(int pos16, int partnum)
{ {
return buf16[pos16 + (partnum << 11)]; return buf16[pos16 + (partnum << 11)];
} }
/* write a word to gbe buffer, and record that a write has occured */
void void
setWord(int pos16, int partnum, uint16_t val16) setWord(int pos16, int partnum, uint16_t val16)
{ {
@@ -388,7 +371,6 @@ setWord(int pos16, int partnum, uint16_t val16)
nvmPartModified[partnum] = 1; nvmPartModified[partnum] = 1;
} }
/* big-endian host compatibility: called post-read and again pre-write */
void void
byteswap(int n, int partnum) byteswap(int n, int partnum)
{ {
@@ -396,28 +378,25 @@ byteswap(int n, int partnum)
uint8_t *nbuf = (uint8_t *) gbe[partnum]; uint8_t *nbuf = (uint8_t *) gbe[partnum];
for (int w = 0; w < wcount; w++) { for (int w = 0; w < wcount; w++) {
b1 = b2 = w << 1; b1 = b2 = w << 1;
nbuf[b1] ^= nbuf[++b2]; /* xor swap */
/* xor-based swap method */
nbuf[b1] ^= nbuf[++b2];
nbuf[b2] ^= nbuf[b1]; nbuf[b2] ^= nbuf[b1];
nbuf[b1] ^= nbuf[b2]; nbuf[b1] ^= nbuf[b2];
} }
} }
/* called after all operations are done, if gbe was modified */
void void
writeGbeFile(int *fd, const char *filename) writeGbeFile(int *fd, const char *filename)
{ {
int p, nw; int p, nw;
errno = 0; errno = 0;
if ((gbe[0] != gbe[1]) && (gbe[0] < gbe[1])) if ((gbe[0] != gbe[1]) && (gbe[0] < gbe[1]))
nw = 128; /* write only the nvm part */ nw = 128; /* speedhack: write only the nvm part */
else else
nw = SIZE_4KB; /* copy/swap, so only write everything */ nw = SIZE_4KB; /* copy/swap, so only write everything */
for (p = 0; p < 2; p++) { for (p = 0; p < 2; p++) {
if (gbe[0] > gbe[1]) /* write sequentially on-disk */ if (gbe[0] > gbe[1])
p ^= 1; p ^= 1; /* speedhack: write sequentially on-disk */
if (!nvmPartModified[p]) if (!nvmPartModified[p])
goto next_part; goto next_part;
if (big_endian) if (big_endian)
@@ -426,7 +405,7 @@ writeGbeFile(int *fd, const char *filename)
err(errno, "%s", filename); err(errno, "%s", filename);
next_part: next_part:
if (gbe[0] > gbe[1]) if (gbe[0] > gbe[1])
p ^= 1; p ^= 1; /* speedhack: write sequentially on-disk */
} }
if (close((*fd))) if (close((*fd)))
err(errno, "%s", filename); err(errno, "%s", filename);