util/nvmutil: re-factor to reduce code indentation

This commit is contained in:
Leah Rowe
2022-11-26 11:26:07 +00:00
parent 0bbd4f1f26
commit 0ae00e881e
+34 -32
View File
@@ -97,17 +97,16 @@ main(int argc, char *argv[])
if (errno != 0) if (errno != 0)
goto nvmutil_exit; goto nvmutil_exit;
if (readFromFile(&fd, gbe, FILENAME, flags, SIZE_8KB) if (readFromFile(&fd, gbe, FILENAME, flags, SIZE_8KB) != SIZE_8KB)
== SIZE_8KB) goto nvmutil_exit;
{
if (strMac != NULL)
setmac(strMac);
else
cmd(COMMAND);
if (gbeFileModified) if (strMac != NULL)
writeGbeFile(&fd, FILENAME); setmac(strMac);
} else
cmd(COMMAND);
if (gbeFileModified)
writeGbeFile(&fd, FILENAME);
nvmutil_exit: nvmutil_exit:
if (errno == ENOTDIR) if (errno == ENOTDIR)
@@ -126,16 +125,17 @@ writeGbeFile(int *fd, const char *filename)
if (pwrite((*fd), gbe, SIZE_8KB, 0) == SIZE_8KB) if (pwrite((*fd), gbe, SIZE_8KB, 0) == SIZE_8KB)
close((*fd)); close((*fd));
if (errno == 0) { if (errno != 0)
for (partnum = 0; partnum < 2; partnum++) { return;
if (nvmPartModified[partnum])
printf("Part %d modified\n", partnum); for (partnum = 0; partnum < 2; partnum++) {
else if (nvmPartModified[partnum])
fprintf (stderr, printf("Part %d modified\n", partnum);
"Part %d NOT modified\n", partnum); else
} fprintf (stderr,
printf("File `%s` successfully modified\n", filename); "Part %d NOT modified\n", partnum);
} }
printf("File `%s` successfully modified\n", filename);
} }
ssize_t ssize_t
@@ -171,8 +171,10 @@ setmac(const char *strMac)
if (readFromFile(&macfd, rmac, "/dev/urandom", O_RDONLY, 12) != 12) if (readFromFile(&macfd, rmac, "/dev/urandom", O_RDONLY, 12) != 12)
return; return;
else if (strnlen(strMac, 20) != 17)
if (strnlen(strMac, 20) != 17)
goto invalid_mac_address; goto invalid_mac_address;
for (o = 0, random = 0; o < 16; o += 3) { for (o = 0, random = 0; o < 16; o += 3) {
if (o != 15) if (o != 15)
if (strMac[o + 2] != ':') if (strMac[o + 2] != ':')
@@ -214,12 +216,12 @@ setmac(const char *strMac)
byteswap((uint8_t *) &mac[o]); byteswap((uint8_t *) &mac[o]);
for (partnum = 0; partnum < 2; partnum++) { for (partnum = 0; partnum < 2; partnum++) {
if (validChecksum(partnum)) { if (!validChecksum(partnum))
for (o = 0; o < 3; o++) continue;
setWord(o, partnum, mac[o]); for (o = 0; o < 3; o++)
part = partnum; setWord(o, partnum, mac[o]);
cmd("setchecksum"); part = partnum;
} cmd("setchecksum");
} }
return; return;
invalid_mac_address: invalid_mac_address:
@@ -306,12 +308,12 @@ validChecksum(int partnum)
for(w = 0; w <= 0x3F; w++) for(w = 0; w <= 0x3F; w++)
total += word(w, partnum); total += word(w, partnum);
if (total != 0xBABA) { if (total == 0xBABA)
fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum); return 1;
errno = ECANCELED;
return 0; fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum);
} errno = ECANCELED;
return 1; return 0;
} }
uint16_t uint16_t