summaryrefslogtreecommitdiff
path: root/util/nvmutil
diff options
context:
space:
mode:
Diffstat (limited to 'util/nvmutil')
-rw-r--r--util/nvmutil/nvmutil.c46
1 files changed, 25 insertions, 21 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c
index da6d4cab..c9d9b4ca 100644
--- a/util/nvmutil/nvmutil.c
+++ b/util/nvmutil/nvmutil.c
@@ -43,8 +43,8 @@ static void parse_mac_string(void);
static void set_mac_byte(size_t mac_byte_pos);
static void set_mac_nib(size_t mac_str_pos,
size_t mac_byte_pos, size_t mac_nib_pos);
-static uint8_t hextonum(char ch_s);
-static uint8_t rhex(void);
+static uint16_t hextonum(char ch_s);
+static uint16_t rhex(void);
static void read_file_exact(int fd, void *buf, size_t len,
off_t off, const char *path, const char *op);
static int write_mac_part(size_t partnum);
@@ -113,9 +113,9 @@ static void set_err(int errval);
*/
#define items(x) (sizeof((x)) / sizeof((x)[0]))
+#ifndef HAVE_ARC4RANDOM_BUF
static const char newrandom[] = "/dev/urandom";
static const char oldrandom[] = "/dev/random"; /* fallback on OLD unix */
-#ifndef HAVE_ARC4RANDOM_BUF
static const char *rname = NULL;
#endif
@@ -357,21 +357,24 @@ open_dev_urandom(void)
{
struct stat st_urandom_fd;
+ /*
+ * Try /dev/urandom first
+ */
rname = newrandom;
+ if ((urandom_fd = open(rname, O_RDONLY)) != -1)
+ return;
- if ((urandom_fd = open(rname, O_RDONLY)) == -1) {
- /*
- * Fall back to /dev/random on old platforms
- * where /dev/urandom does not exist.
- *
- * We must reset the error condition first,
- * to prevent stale error status later.
- */
- errno = 0;
+ /*
+ * Fall back to /dev/random on old platforms
+ * where /dev/urandom does not exist.
+ *
+ * We must reset the error condition first,
+ * to prevent stale error status later.
+ */
+ errno = 0;
- rname = oldrandom;
- xopen(&urandom_fd, rname, O_RDONLY, &st_urandom_fd);
- }
+ rname = oldrandom;
+ xopen(&urandom_fd, rname, O_RDONLY, &st_urandom_fd);
}
#endif
@@ -510,8 +513,7 @@ set_mac_nib(size_t mac_str_pos,
mac_ch = mac_str[mac_str_pos + mac_nib_pos];
- hex_num = hextonum(mac_ch);
- if (hex_num > 15)
+ if ((hex_num = hextonum(mac_ch)) > 15)
err(EINVAL, "Invalid character '%c'",
mac_str[mac_str_pos + mac_nib_pos]);
@@ -537,7 +539,7 @@ set_mac_nib(size_t mac_str_pos,
| ((mac_nib_pos ^ 1) << 2)); /* left or right nib? */
}
-static uint8_t
+static uint16_t
hextonum(char ch_s)
{
/*
@@ -560,7 +562,7 @@ hextonum(char ch_s)
return 16; /* invalid character */
}
-static uint8_t
+static uint16_t
rhex(void)
{
static size_t n = 0;
@@ -575,7 +577,7 @@ rhex(void)
#endif
}
- return rnum[--n] & 0xf;
+ return (uint16_t)(rnum[--n] & 0xf);
}
static void
@@ -873,8 +875,10 @@ write_gbe_file_part(size_t p)
static off_t
gbe_file_offset(size_t p, const char *f_op)
{
+ off_t gbe_file_half_size = gbe_file_size >> 1;
+
return gbe_x_offset(p, f_op, "file",
- gbe_file_size >> 1, gbe_file_size);
+ gbe_file_half_size, gbe_file_size);
}
/*