mirror of
https://codeberg.org/libreboot/lbmk.git
synced 2026-07-21 19:26:22 +02:00
util/nvmutil: don't declare variables in for loops
Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
+36
-14
@@ -114,10 +114,12 @@ main(int argc, char *argv[])
|
|||||||
void
|
void
|
||||||
set_cmd(int argc, char *argv[])
|
set_cmd(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
usage(argv[0]);
|
usage(argv[0]);
|
||||||
} else if (argc > 2) {
|
} else if (argc > 2) {
|
||||||
for (size_t i = 0; (i < items(op)) && (cmd == NULL); i++) {
|
for (i = 0; (i < items(op)) && (cmd == NULL); i++) {
|
||||||
if (strcmp(COMMAND, op[i].str) != 0)
|
if (strcmp(COMMAND, op[i].str) != 0)
|
||||||
continue;
|
continue;
|
||||||
if (argc >= op[i].args) {
|
if (argc >= op[i].args) {
|
||||||
@@ -200,6 +202,7 @@ xopen(int *f, const char *l, int p, struct stat *st)
|
|||||||
void
|
void
|
||||||
read_gbe(void)
|
read_gbe(void)
|
||||||
{
|
{
|
||||||
|
int p;
|
||||||
int do_read[2] = {1, 1};
|
int do_read[2] = {1, 1};
|
||||||
|
|
||||||
if ((cmd == cmd_copy) || (cmd == cmd_brick) ||
|
if ((cmd == cmd_copy) || (cmd == cmd_brick) ||
|
||||||
@@ -213,7 +216,7 @@ read_gbe(void)
|
|||||||
if ((cmd == cmd_copy) || (cmd == cmd_swap))
|
if ((cmd == cmd_copy) || (cmd == cmd_swap))
|
||||||
invert = 1;
|
invert = 1;
|
||||||
|
|
||||||
for (int p = 0; p < 2; p++)
|
for (p = 0; p < 2; p++)
|
||||||
if (do_read[p])
|
if (do_read[p])
|
||||||
read_gbe_part(p, invert);
|
read_gbe_part(p, invert);
|
||||||
}
|
}
|
||||||
@@ -231,12 +234,13 @@ read_gbe_part(int p, int invert)
|
|||||||
void
|
void
|
||||||
cmd_setmac(void)
|
cmd_setmac(void)
|
||||||
{
|
{
|
||||||
|
int partnum;
|
||||||
int mac_updated = 0;
|
int mac_updated = 0;
|
||||||
parse_mac_string();
|
parse_mac_string();
|
||||||
|
|
||||||
printf("MAC address to be written: %s\n", mac);
|
printf("MAC address to be written: %s\n", mac);
|
||||||
|
|
||||||
for (int partnum = 0; partnum < 2; partnum++)
|
for (partnum = 0; partnum < 2; partnum++)
|
||||||
mac_updated |= write_mac_part(partnum);
|
mac_updated |= write_mac_part(partnum);
|
||||||
if (mac_updated)
|
if (mac_updated)
|
||||||
errno = 0;
|
errno = 0;
|
||||||
@@ -245,11 +249,13 @@ cmd_setmac(void)
|
|||||||
void
|
void
|
||||||
parse_mac_string(void)
|
parse_mac_string(void)
|
||||||
{
|
{
|
||||||
|
int mac_pos;
|
||||||
uint64_t total = 0;
|
uint64_t total = 0;
|
||||||
|
|
||||||
if (strnlen(mac, 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 mac_pos = 0; mac_pos < 16; mac_pos += 3)
|
for (mac_pos = 0; mac_pos < 16; mac_pos += 3)
|
||||||
set_mac_byte(mac_pos, &total);
|
set_mac_byte(mac_pos, &total);
|
||||||
|
|
||||||
if (total == 0)
|
if (total == 0)
|
||||||
@@ -261,10 +267,11 @@ parse_mac_string(void)
|
|||||||
void
|
void
|
||||||
set_mac_byte(int mac_pos, uint64_t *total)
|
set_mac_byte(int mac_pos, uint64_t *total)
|
||||||
{
|
{
|
||||||
|
int nib;
|
||||||
uint8_t h = 0;
|
uint8_t h = 0;
|
||||||
check_mac_separator(mac_pos);
|
check_mac_separator(mac_pos);
|
||||||
|
|
||||||
for (int nib = 0; nib < 2; nib++, *total += h)
|
for (nib = 0; nib < 2; nib++, *total += h)
|
||||||
set_mac_nib(mac_pos, nib, &h);
|
set_mac_nib(mac_pos, nib, &h);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -325,11 +332,13 @@ rhex(void)
|
|||||||
int
|
int
|
||||||
write_mac_part(int partnum)
|
write_mac_part(int partnum)
|
||||||
{
|
{
|
||||||
|
int w;
|
||||||
|
|
||||||
part = partnum;
|
part = partnum;
|
||||||
if (!good_checksum(partnum))
|
if (!good_checksum(partnum))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
for (int w = 0; w < 3; w++)
|
for (w = 0; w < 3; w++)
|
||||||
set_word(w, partnum, macbuf[w]);
|
set_word(w, partnum, macbuf[w]);
|
||||||
|
|
||||||
printf("Wrote MAC address to part %d: ", partnum);
|
printf("Wrote MAC address to part %d: ", partnum);
|
||||||
@@ -342,9 +351,10 @@ write_mac_part(int partnum)
|
|||||||
void
|
void
|
||||||
cmd_dump(void)
|
cmd_dump(void)
|
||||||
{
|
{
|
||||||
|
int partnum;
|
||||||
int num_invalid = 0;
|
int num_invalid = 0;
|
||||||
|
|
||||||
for (int partnum = 0; partnum < 2; partnum++) {
|
for (partnum = 0; partnum < 2; partnum++) {
|
||||||
if (!good_checksum(partnum))
|
if (!good_checksum(partnum))
|
||||||
++num_invalid;
|
++num_invalid;
|
||||||
|
|
||||||
@@ -360,7 +370,8 @@ cmd_dump(void)
|
|||||||
void
|
void
|
||||||
print_mac_address(int partnum)
|
print_mac_address(int partnum)
|
||||||
{
|
{
|
||||||
for (int c = 0; c < 3; c++) {
|
int c;
|
||||||
|
for (c = 0; c < 3; c++) {
|
||||||
uint16_t val16 = word(c, partnum);
|
uint16_t val16 = word(c, partnum);
|
||||||
printf("%02x:%02x", val16 & 0xff, val16 >> 8);
|
printf("%02x:%02x", val16 & 0xff, val16 >> 8);
|
||||||
if (c == 2)
|
if (c == 2)
|
||||||
@@ -373,9 +384,12 @@ print_mac_address(int partnum)
|
|||||||
void
|
void
|
||||||
hexdump(int partnum)
|
hexdump(int partnum)
|
||||||
{
|
{
|
||||||
for (int row = 0; row < 8; row++) {
|
int c;
|
||||||
|
int row;
|
||||||
|
|
||||||
|
for (row = 0; row < 8; row++) {
|
||||||
printf("%08x ", row << 4);
|
printf("%08x ", row << 4);
|
||||||
for (int c = 0; c < 8; c++) {
|
for (c = 0; c < 8; c++) {
|
||||||
uint16_t val16 = word((row << 3) + c, partnum);
|
uint16_t val16 = word((row << 3) + c, partnum);
|
||||||
if (c == 4)
|
if (c == 4)
|
||||||
printf(" ");
|
printf(" ");
|
||||||
@@ -388,8 +402,10 @@ hexdump(int partnum)
|
|||||||
void
|
void
|
||||||
cmd_setchecksum(void)
|
cmd_setchecksum(void)
|
||||||
{
|
{
|
||||||
|
int c;
|
||||||
uint16_t val16 = 0;
|
uint16_t val16 = 0;
|
||||||
for (int c = 0; c < NVM_CHECKSUM_WORD; c++)
|
|
||||||
|
for (c = 0; c < NVM_CHECKSUM_WORD; c++)
|
||||||
val16 += word(c, part);
|
val16 += word(c, part);
|
||||||
|
|
||||||
set_word(NVM_CHECKSUM_WORD, part, NVM_CHECKSUM - val16);
|
set_word(NVM_CHECKSUM_WORD, part, NVM_CHECKSUM - val16);
|
||||||
@@ -421,8 +437,9 @@ cmd_swap(void) {
|
|||||||
int
|
int
|
||||||
good_checksum(int partnum)
|
good_checksum(int partnum)
|
||||||
{
|
{
|
||||||
|
int w;
|
||||||
uint16_t total = 0;
|
uint16_t total = 0;
|
||||||
for(int w = 0; w <= NVM_CHECKSUM_WORD; w++)
|
for (w = 0; w <= NVM_CHECKSUM_WORD; w++)
|
||||||
total += word(w, partnum);
|
total += word(w, partnum);
|
||||||
|
|
||||||
if (total == NVM_CHECKSUM)
|
if (total == NVM_CHECKSUM)
|
||||||
@@ -461,8 +478,10 @@ check_bound(int c, int p)
|
|||||||
void
|
void
|
||||||
write_gbe(void)
|
write_gbe(void)
|
||||||
{
|
{
|
||||||
|
int p;
|
||||||
|
|
||||||
if (flags != O_RDONLY)
|
if (flags != O_RDONLY)
|
||||||
for (int p = 0; p < 2; p++)
|
for (p = 0; p < 2; p++)
|
||||||
if (part_modified[p])
|
if (part_modified[p])
|
||||||
write_gbe_part(p);
|
write_gbe_part(p);
|
||||||
err_if (close(fd) == -1);
|
err_if (close(fd) == -1);
|
||||||
@@ -481,10 +500,13 @@ write_gbe_part(int p)
|
|||||||
void
|
void
|
||||||
swap(int partnum)
|
swap(int partnum)
|
||||||
{
|
{
|
||||||
|
size_t w;
|
||||||
|
size_t x;
|
||||||
|
|
||||||
int e = 1;
|
int e = 1;
|
||||||
uint8_t *n = buf + (SIZE_4KB * partnum);
|
uint8_t *n = buf + (SIZE_4KB * partnum);
|
||||||
|
|
||||||
for (size_t w = NVM_SIZE * ((uint8_t *) &e)[0], x = 1;
|
for (w = NVM_SIZE * ((uint8_t *) &e)[0], x = 1;
|
||||||
w < NVM_SIZE; w += 2, x += 2) {
|
w < NVM_SIZE; w += 2, x += 2) {
|
||||||
uint8_t chg = n[w];
|
uint8_t chg = n[w];
|
||||||
n[w] = n[x];
|
n[w] = n[x];
|
||||||
|
|||||||
Reference in New Issue
Block a user