mirror of
https://codeberg.org/libreboot/lbmk.git
synced 2026-07-12 22:42:54 +02:00
util/nvmutil: consistent variable/function naming
use the same naming scheme throughout Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
+67
-67
@@ -14,16 +14,15 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
void cmd_setchecksum(void), cmd_brick(void), swap(int partnum), writeGbe(void),
|
void cmd_setchecksum(void), cmd_brick(void), swap(int partnum), cmd_copy(void),
|
||||||
cmd_dump(void), cmd_setmac(void), readGbe(void), print_mac_address(int),
|
set_cmd(int, char **), set_word(int, int, uint16_t), check_bound(int, int),
|
||||||
hexdump(int), set_mac_nib(int, int, uint8_t *), checkdir(const char *),
|
xopen(int *, const char *, int p, struct stat *), check_mac_separator(int),
|
||||||
parseMacString(const char *strMac, uint16_t *mac), cmd_swap(void),
|
cmd_dump(void), cmd_setmac(void), read_gbe(void), print_mac_address(int),
|
||||||
openFiles(void), cmd_copy(void), writeGbe_part(int), readGbe_part(int, int),
|
parse_mac_string(void), cmd_swap(void), err_if(int), write_gbe_part(int),
|
||||||
set_cmd(int, char **), setWord(int, int, uint16_t), check_bounds(int, int),
|
|
||||||
xopen(int *, const char *, int p, struct stat *), checkMacSeparator(int),
|
|
||||||
set_mac_byte(int, uint64_t *), usage(char*), set_io_flags(int, char **),
|
set_mac_byte(int, uint64_t *), usage(char*), set_io_flags(int, char **),
|
||||||
err_if(int);
|
hexdump(int), set_mac_nib(int, int, uint8_t *), checkdir(const char *),
|
||||||
int goodChecksum(int partnum), write_mac_part(int), set_err(int);
|
openFiles(void), read_gbe_part(int, int), write_gbe(void);
|
||||||
|
int good_checksum(int partnum), write_mac_part(int), set_err(int);
|
||||||
uint8_t hextonum(char chs), rhex(void);
|
uint8_t hextonum(char chs), rhex(void);
|
||||||
uint16_t word(int, int);
|
uint16_t word(int, int);
|
||||||
|
|
||||||
@@ -40,12 +39,12 @@ uint16_t word(int, int);
|
|||||||
#define SIZE_128KB 0x20000
|
#define SIZE_128KB 0x20000
|
||||||
|
|
||||||
uint8_t buf[SIZE_8KB];
|
uint8_t buf[SIZE_8KB];
|
||||||
uint16_t mac[3] = {0, 0, 0};
|
uint16_t macbuf[3] = {0, 0, 0};
|
||||||
size_t partsize;
|
size_t partsize;
|
||||||
int flags, rfd, fd, part, e = 1, invert = 0;
|
int flags, rfd, fd, part, e = 1, invert = 0;
|
||||||
int nvmPartModified[2] = {0, 0};
|
int part_modified[2] = {0, 0};
|
||||||
|
|
||||||
const char *strMac = NULL, *strRMac = "xx:xx:xx:xx:xx:xx", *fname = "";
|
const char *mac = NULL, *rmac = "xx:xx:xx:xx:xx:xx", *fname = "";
|
||||||
|
|
||||||
typedef struct op {
|
typedef struct op {
|
||||||
char *str;
|
char *str;
|
||||||
@@ -88,9 +87,9 @@ main(int argc, char *argv[])
|
|||||||
#ifdef __OpenBSD__
|
#ifdef __OpenBSD__
|
||||||
err_if(pledge("stdio", NULL) == -1);
|
err_if(pledge("stdio", NULL) == -1);
|
||||||
#endif
|
#endif
|
||||||
readGbe();
|
read_gbe();
|
||||||
(*cmd)();
|
(*cmd)();
|
||||||
writeGbe();
|
write_gbe();
|
||||||
|
|
||||||
err_if((errno != 0) && (cmd != cmd_dump));
|
err_if((errno != 0) && (cmd != cmd_dump));
|
||||||
return errno ? EXIT_FAILURE : EXIT_SUCCESS;
|
return errno ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||||
@@ -117,12 +116,12 @@ set_cmd(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((cmd == NULL) && (argc > 2)) { /* nvm gbe [MAC] */
|
if ((cmd == NULL) && (argc > 2)) { /* nvm gbe [MAC] */
|
||||||
strMac = COMMAND;
|
mac = COMMAND;
|
||||||
cmd = cmd_setmac;
|
cmd = cmd_setmac;
|
||||||
} else if (cmd == cmd_setmac) { /* nvm gbe setmac [MAC] */
|
} else if (cmd == cmd_setmac) { /* nvm gbe setmac [MAC] */
|
||||||
strMac = strRMac; /* random MAC */
|
mac = rmac; /* random MAC */
|
||||||
if (argc > 3)
|
if (argc > 3)
|
||||||
strMac = MAC_ADDRESS;
|
mac = MAC_ADDRESS;
|
||||||
} else if ((cmd != NULL) && (argc > 3)) { /* user-supplied partnum */
|
} else if ((cmd != NULL) && (argc > 3)) { /* user-supplied partnum */
|
||||||
err_if((errno = (!((part = PARTN[0] - '0') == 0 || part == 1))
|
err_if((errno = (!((part = PARTN[0] - '0') == 0 || part == 1))
|
||||||
|| PARTN[1] ? EINVAL : errno)); /* only allow '0' or '1' */
|
|| PARTN[1] ? EINVAL : errno)); /* only allow '0' or '1' */
|
||||||
@@ -183,7 +182,7 @@ xopen(int *f, const char *l, int p, struct stat *st)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
readGbe(void)
|
read_gbe(void)
|
||||||
{
|
{
|
||||||
int do_read[2] = {1, 1};
|
int do_read[2] = {1, 1};
|
||||||
|
|
||||||
@@ -199,11 +198,11 @@ readGbe(void)
|
|||||||
|
|
||||||
for (int p = 0; p < 2; p++)
|
for (int p = 0; p < 2; p++)
|
||||||
if (do_read[p])
|
if (do_read[p])
|
||||||
readGbe_part(p, invert);
|
read_gbe_part(p, invert);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
readGbe_part(int p, int invert)
|
read_gbe_part(int p, int invert)
|
||||||
{
|
{
|
||||||
if (pread(fd, buf + (SIZE_4KB * (p ^ invert)), SIZE_4KB, p * partsize)
|
if (pread(fd, buf + (SIZE_4KB * (p ^ invert)), SIZE_4KB, p * partsize)
|
||||||
!= SIZE_4KB)
|
!= SIZE_4KB)
|
||||||
@@ -216,9 +215,9 @@ void
|
|||||||
cmd_setmac(void)
|
cmd_setmac(void)
|
||||||
{
|
{
|
||||||
int mac_updated = 0;
|
int mac_updated = 0;
|
||||||
parseMacString(strMac, mac);
|
parse_mac_string();
|
||||||
|
|
||||||
printf("MAC address to be written: %s\n", strMac);
|
printf("MAC address to be written: %s\n", mac);
|
||||||
|
|
||||||
for (int partnum = 0; partnum < 2; partnum++)
|
for (int partnum = 0; partnum < 2; partnum++)
|
||||||
mac_updated |= write_mac_part(partnum);
|
mac_updated |= write_mac_part(partnum);
|
||||||
@@ -227,59 +226,59 @@ cmd_setmac(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
parseMacString(const char *strMac, uint16_t *mac)
|
parse_mac_string(void)
|
||||||
{
|
{
|
||||||
uint64_t total = 0;
|
uint64_t total = 0;
|
||||||
if (strnlen(strMac, 20) != 17)
|
if (strnlen(mac, 20) != 17)
|
||||||
err(set_err(EINVAL), "Invalid MAC address string length");
|
err(set_err(EINVAL), "Invalid MAC address string length");
|
||||||
|
|
||||||
for (int strMacPos = 0; strMacPos < 16; strMacPos += 3)
|
for (int mac_pos = 0; mac_pos < 16; mac_pos += 3)
|
||||||
set_mac_byte(strMacPos, &total);
|
set_mac_byte(mac_pos, &total);
|
||||||
|
|
||||||
if (total == 0)
|
if (total == 0)
|
||||||
err(set_err(EINVAL), "Invalid MAC (all-zero MAC address)");
|
err(set_err(EINVAL), "Invalid MAC (all-zero MAC address)");
|
||||||
if (mac[0] & 1)
|
if (macbuf[0] & 1)
|
||||||
err(set_err(EINVAL), "Invalid MAC (multicast bit set)");
|
err(set_err(EINVAL), "Invalid MAC (multicast bit set)");
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
set_mac_byte(int strMacPos, uint64_t *total)
|
set_mac_byte(int mac_pos, uint64_t *total)
|
||||||
{
|
{
|
||||||
uint8_t h = 0;
|
uint8_t h = 0;
|
||||||
checkMacSeparator(strMacPos);
|
check_mac_separator(mac_pos);
|
||||||
|
|
||||||
for (int nib = 0; nib < 2; nib++, *total += h)
|
for (int nib = 0; nib < 2; nib++, *total += h)
|
||||||
set_mac_nib(strMacPos, nib, &h);
|
set_mac_nib(mac_pos, nib, &h);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
checkMacSeparator(int strMacPos)
|
check_mac_separator(int mac_pos)
|
||||||
{
|
{
|
||||||
if (strMacPos == 15)
|
if (mac_pos == 15)
|
||||||
return;
|
return;
|
||||||
char separator = strMac[strMacPos + 2];
|
char separator = mac[mac_pos + 2];
|
||||||
if (separator == ':')
|
if (separator == ':')
|
||||||
return;
|
return;
|
||||||
err(set_err(EINVAL), "Invalid MAC address separator '%c'", separator);
|
err(set_err(EINVAL), "Invalid MAC address separator '%c'", separator);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
set_mac_nib(int strMacPos, int nib, uint8_t *h)
|
set_mac_nib(int mac_pos, int nib, uint8_t *h)
|
||||||
{
|
{
|
||||||
int byte = strMacPos / 3;
|
int byte = mac_pos / 3;
|
||||||
|
|
||||||
if ((*h = hextonum(strMac[strMacPos + nib])) > 15)
|
if ((*h = hextonum(mac[mac_pos + nib])) > 15)
|
||||||
err(set_err(EINVAL), "Invalid character '%c'",
|
err(set_err(EINVAL), "Invalid character '%c'",
|
||||||
strMac[strMacPos + nib]);
|
mac[mac_pos + nib]);
|
||||||
|
|
||||||
/* If random, ensure that local/unicast bits are set */
|
/* If random, ensure that local/unicast bits are set */
|
||||||
if ((byte == 0) && (nib == 1))
|
if ((byte == 0) && (nib == 1))
|
||||||
if ((strMac[strMacPos + nib] == '?') ||
|
if ((mac[mac_pos + nib] == '?') ||
|
||||||
(strMac[strMacPos + nib] == 'x') ||
|
(mac[mac_pos + nib] == 'x') ||
|
||||||
(strMac[strMacPos + nib] == 'X')) /* random */
|
(mac[mac_pos + nib] == 'X')) /* random */
|
||||||
*h = (*h & 0xE) | 2; /* local, unicast */
|
*h = (*h & 0xE) | 2; /* local, unicast */
|
||||||
|
|
||||||
mac[byte >> 1] |= ((uint16_t ) *h) << ((8 * (byte % 2)) +
|
macbuf[byte >> 1] |= ((uint16_t ) *h) << ((8 * (byte % 2)) +
|
||||||
(4 * (nib ^ 1)));
|
(4 * (nib ^ 1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -310,11 +309,11 @@ int
|
|||||||
write_mac_part(int partnum)
|
write_mac_part(int partnum)
|
||||||
{
|
{
|
||||||
part = partnum;
|
part = partnum;
|
||||||
if (!goodChecksum(partnum))
|
if (!good_checksum(partnum))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
for (int w = 0; w < 3; w++)
|
for (int w = 0; w < 3; w++)
|
||||||
setWord(w, partnum, mac[w]);
|
set_word(w, partnum, macbuf[w]);
|
||||||
|
|
||||||
printf("Wrote MAC address to part %d: ", partnum);
|
printf("Wrote MAC address to part %d: ", partnum);
|
||||||
print_mac_address(partnum);
|
print_mac_address(partnum);
|
||||||
@@ -326,18 +325,18 @@ write_mac_part(int partnum)
|
|||||||
void
|
void
|
||||||
cmd_dump(void)
|
cmd_dump(void)
|
||||||
{
|
{
|
||||||
int numInvalid = 0;
|
int num_invalid = 0;
|
||||||
|
|
||||||
for (int partnum = 0; partnum < 2; partnum++) {
|
for (int partnum = 0; partnum < 2; partnum++) {
|
||||||
if (!goodChecksum(partnum))
|
if (!good_checksum(partnum))
|
||||||
++numInvalid;
|
++num_invalid;
|
||||||
|
|
||||||
printf("MAC (part %d): ", partnum);
|
printf("MAC (part %d): ", partnum);
|
||||||
print_mac_address(partnum);
|
print_mac_address(partnum);
|
||||||
hexdump(partnum);
|
hexdump(partnum);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((numInvalid < 2))
|
if ((num_invalid < 2))
|
||||||
errno = 0;
|
errno = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -376,34 +375,34 @@ cmd_setchecksum(void)
|
|||||||
for (int c = 0; c < NVM_CHECKSUM_WORD; c++)
|
for (int c = 0; c < NVM_CHECKSUM_WORD; c++)
|
||||||
val16 += word(c, part);
|
val16 += word(c, part);
|
||||||
|
|
||||||
setWord(NVM_CHECKSUM_WORD, part, NVM_CHECKSUM - val16);
|
set_word(NVM_CHECKSUM_WORD, part, NVM_CHECKSUM - val16);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
cmd_brick(void)
|
cmd_brick(void)
|
||||||
{
|
{
|
||||||
if (goodChecksum(part))
|
if (good_checksum(part))
|
||||||
setWord(NVM_CHECKSUM_WORD, part,
|
set_word(NVM_CHECKSUM_WORD, part,
|
||||||
((word(NVM_CHECKSUM_WORD, part)) ^ 0xFF));
|
((word(NVM_CHECKSUM_WORD, part)) ^ 0xFF));
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
cmd_copy(void)
|
cmd_copy(void)
|
||||||
{
|
{
|
||||||
err_if(!goodChecksum(part ^ 1));
|
err_if(!good_checksum(part ^ 1));
|
||||||
nvmPartModified[part ^ 1] = 1;
|
part_modified[part ^ 1] = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
cmd_swap(void) {
|
cmd_swap(void) {
|
||||||
err_if(!(goodChecksum(0) || goodChecksum(1)));
|
err_if(!(good_checksum(0) || good_checksum(1)));
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
||||||
nvmPartModified[1] = nvmPartModified[0] = 1;
|
part_modified[1] = part_modified[0] = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
goodChecksum(int partnum)
|
good_checksum(int partnum)
|
||||||
{
|
{
|
||||||
uint16_t total = 0;
|
uint16_t total = 0;
|
||||||
for(int w = 0; w <= NVM_CHECKSUM_WORD; w++)
|
for(int w = 0; w <= NVM_CHECKSUM_WORD; w++)
|
||||||
@@ -420,42 +419,43 @@ goodChecksum(int partnum)
|
|||||||
uint16_t
|
uint16_t
|
||||||
word(int pos16, int p)
|
word(int pos16, int p)
|
||||||
{
|
{
|
||||||
check_bounds(pos16, p);
|
check_bound(pos16, p);
|
||||||
return ((uint16_t *) (buf + (SIZE_4KB * p)))[pos16];
|
return ((uint16_t *) (buf + (SIZE_4KB * p)))[pos16];
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
setWord(int pos16, int p, uint16_t val16)
|
set_word(int pos16, int p, uint16_t val16)
|
||||||
{
|
{
|
||||||
check_bounds(pos16, p);
|
check_bound(pos16, p);
|
||||||
nvmPartModified[p] = 1;
|
part_modified[p] = 1;
|
||||||
((uint16_t *) (buf + (SIZE_4KB * p)))[pos16] = val16;
|
((uint16_t *) (buf + (SIZE_4KB * p)))[pos16] = val16;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
check_bounds(int c, int p)
|
check_bound(int c, int p)
|
||||||
{
|
{
|
||||||
if ((p != 0) && (p != 1))
|
if ((p != 0) && (p != 1))
|
||||||
err(set_err(EINVAL), "check_bounds: invalid partnum %d", p);
|
err(set_err(EINVAL), "check_bound: invalid partnum %d", p);
|
||||||
if ((c < 0) || (c >= (SIZE_4KB >> 1)))
|
if ((c < 0) || (c >= (SIZE_4KB >> 1)))
|
||||||
err(set_err(EINVAL), "check_bounds: out of bounds %d", c);
|
err(set_err(EINVAL), "check_bound: out of bounds %d", c);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
writeGbe(void)
|
write_gbe(void)
|
||||||
{
|
{
|
||||||
if (flags != O_RDONLY)
|
if (flags != O_RDONLY)
|
||||||
for (int p = 0; p < 2; p++)
|
for (int p = 0; p < 2; p++)
|
||||||
if (nvmPartModified[p])
|
if (part_modified[p])
|
||||||
writeGbe_part(p);
|
write_gbe_part(p);
|
||||||
err_if(close(fd) == -1);
|
err_if(close(fd) == -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
writeGbe_part(int p)
|
write_gbe_part(int p)
|
||||||
{
|
{
|
||||||
swap(p); /* swap bytes on big-endian host CPUs */
|
swap(p); /* swap bytes on big-endian host CPUs */
|
||||||
if(pwrite(fd, buf + (SIZE_4KB * p), SIZE_4KB, p * partsize) != SIZE_4KB)
|
if(pwrite(fd, buf + (SIZE_4KB * p), SIZE_4KB, p * partsize)
|
||||||
|
!= SIZE_4KB)
|
||||||
err(set_err(ECANCELED),
|
err(set_err(ECANCELED),
|
||||||
"Can't write %d b to '%s' p%d", SIZE_4KB, fname, p);
|
"Can't write %d b to '%s' p%d", SIZE_4KB, fname, p);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user