summaryrefslogtreecommitdiff
path: root/util/nvmutil/nvmutil.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/nvmutil/nvmutil.c')
-rw-r--r--util/nvmutil/nvmutil.c86
1 files changed, 54 insertions, 32 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c
index 5cb53be8..86e9c814 100644
--- a/util/nvmutil/nvmutil.c
+++ b/util/nvmutil/nvmutil.c
@@ -42,6 +42,7 @@ int validChecksum(int partnum);
uint16_t word(int pos16, int partnum);
void setWord(int pos16, int partnum, uint16_t val);
void byteswap(uint8_t *byte);
+void writeGbeFile(int *fd, const char *filename);
#define FILENAME argv[1]
#define COMMAND argv[2]
@@ -51,7 +52,8 @@ void byteswap(uint8_t *byte);
#define SIZE_8KB 0x2000
uint8_t gbe[SIZE_8KB];
-int part, modified = 0;
+int part, gbeFileModified = 0;
+uint8_t nvmPartModified[2];
uint16_t test;
uint8_t little_endian;
@@ -63,6 +65,8 @@ main(int argc, char *argv[])
int flags = O_RDWR;
char *strMac = NULL;
char *strRMac = "??:??:??:??:??:??";
+ nvmPartModified[0] = 0;
+ nvmPartModified[1] = 0;
test = 1;
little_endian = ((uint8_t *) &test)[0];
@@ -93,22 +97,16 @@ main(int argc, char *argv[])
if (errno != 0)
goto nvmutil_exit;
- if (readFromFile(&fd, gbe, FILENAME, flags, SIZE_8KB)
- == SIZE_8KB)
- {
- if (strMac != NULL)
- setmac(strMac);
- else
- cmd(COMMAND);
+ if (readFromFile(&fd, gbe, FILENAME, flags, SIZE_8KB) != SIZE_8KB)
+ goto nvmutil_exit;
- if (modified) {
- errno = 0;
- if (pwrite(fd, gbe, SIZE_8KB, 0) == SIZE_8KB)
- close(fd);
- if (errno == 0)
- printf("%s successfully modified\n", FILENAME);
- }
- }
+ if (strMac != NULL)
+ setmac(strMac);
+ else
+ cmd(COMMAND);
+
+ if (gbeFileModified)
+ writeGbeFile(&fd, FILENAME);
nvmutil_exit:
if (errno == ENOTDIR)
@@ -119,6 +117,27 @@ nvmutil_exit:
return errno;
}
+void
+writeGbeFile(int *fd, const char *filename)
+{
+ int partnum;
+ errno = 0;
+
+ if (pwrite((*fd), gbe, SIZE_8KB, 0) == SIZE_8KB)
+ close((*fd));
+ if (errno != 0)
+ return;
+
+ for (partnum = 0; partnum < 2; partnum++) {
+ if (nvmPartModified[partnum])
+ printf("Part %d modified\n", partnum);
+ else
+ fprintf (stderr,
+ "Part %d NOT modified\n", partnum);
+ }
+ printf("File `%s` successfully modified\n", filename);
+}
+
ssize_t
readFromFile(int *fd, uint8_t *buf, const char *path, int flags, size_t size)
{
@@ -152,8 +171,10 @@ setmac(const char *strMac)
if (readFromFile(&macfd, rmac, "/dev/urandom", O_RDONLY, 12) != 12)
return;
- else if (strnlen(strMac, 20) != 17)
+
+ if (strnlen(strMac, 20) != 17)
goto invalid_mac_address;
+
for (o = 0, random = 0; o < 16; o += 3) {
if (o != 15)
if (strMac[o + 2] != ':')
@@ -195,12 +216,12 @@ setmac(const char *strMac)
byteswap((uint8_t *) &mac[o]);
for (partnum = 0; partnum < 2; partnum++) {
- if (validChecksum(partnum)) {
- for (o = 0; o < 3; o++)
- setWord(o, partnum, mac[o]);
- part = partnum;
- cmd("setchecksum");
- }
+ if (!validChecksum(partnum))
+ continue;
+ for (o = 0; o < 3; o++)
+ setWord(o, partnum, mac[o]);
+ part = partnum;
+ cmd("setchecksum");
}
return;
invalid_mac_address:
@@ -263,7 +284,7 @@ cmd(const char *command)
} else if (strcmp(command, "swap") == 0) {
part0 = validChecksum(0);
part1 = validChecksum(1);
- if ((modified = (part0 | part1))) {
+ if ((gbeFileModified = (part0 | part1))) {
for(part0 = 0; part0 < SIZE_4KB; part0++) {
gbe[part0] ^= gbe[part1 = (part0 | SIZE_4KB)];
gbe[part1] ^= gbe[part0];
@@ -272,7 +293,7 @@ cmd(const char *command)
}
} else if (strcmp(command, "copy") == 0) {
if (validChecksum(part))
- memcpy(gbe + ((part ^ (modified = 1)) << 12),
+ memcpy(gbe + ((part ^ (gbeFileModified = 1)) << 12),
gbe + (part << 12), SIZE_4KB);
} else
errno = EINVAL;
@@ -287,12 +308,12 @@ validChecksum(int partnum)
for(w = 0; w <= 0x3F; w++)
total += word(w, partnum);
- if (total != 0xBABA) {
- fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum);
- errno = ECANCELED;
- return 0;
- }
- return 1;
+ if (total == 0xBABA)
+ return 1;
+
+ fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum);
+ errno = ECANCELED;
+ return 0;
}
uint16_t
@@ -310,7 +331,8 @@ setWord(int pos16, int partnum, uint16_t val)
((uint16_t *) gbe)[pos16 + (partnum << 11)] = val;
if (!little_endian)
byteswap(gbe + (pos16 << 1) + (partnum << 12));
- modified = 1;
+ gbeFileModified = 1;
+ nvmPartModified[partnum] = 1;
}
void