summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2023-06-01 12:58:33 +0100
committerLeah Rowe <leah@libreboot.org>2023-06-01 12:58:33 +0100
commitadf3aece6faa134379bde6f72ae0521b7012f707 (patch)
treeabdcc0e1995021a7ec87f1b87c37970a7d54d52a /util
parentb49da12dad4916a5678e35b1909bdc529a6d8474 (diff)
util/nvmutil: fix faulty fd check
i screwed up in an earlier commit this change fixes a bug where on rhex(), each call would re-open /dev/urandom, resetting rfd Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util')
-rw-r--r--util/nvmutil/nvmutil.c7
-rw-r--r--util/nvmutil/nvmutil.h4
2 files changed, 6 insertions, 5 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c
index 77b33aa2..38d34436 100644
--- a/util/nvmutil/nvmutil.c
+++ b/util/nvmutil/nvmutil.c
@@ -141,9 +141,12 @@ rhex(void)
{
static int rfd = -1, n = 0;
static uint8_t rnum[16];
- xopen(rfd, "/dev/urandom", O_RDONLY);
- if (!n)
+ if (!n) {
+ if (rfd == -1) {
+ xopen(rfd, "/dev/urandom", O_RDONLY);
+ }
xpread(rfd, (uint8_t *) &rnum, (n = 15) + 1, 0, "/dev/urandom");
+ }
return rnum[n--] & 0xf;
}
diff --git a/util/nvmutil/nvmutil.h b/util/nvmutil/nvmutil.h
index 1b928a34..043e0f39 100644
--- a/util/nvmutil/nvmutil.h
+++ b/util/nvmutil/nvmutil.h
@@ -51,9 +51,7 @@ int big_endian;
#define ERR() errno = errno ? errno : ECANCELED
#define err_if(x) if (x) err(ERR(), NULL)
-#define xopen(f,l,p) \
- if (f == -1) \
- if (opendir(l) != NULL) err(errno = EISDIR, "%s", l); \
+#define xopen(f,l,p) if (opendir(l) != NULL) err(errno = EISDIR, "%s", l); \
if ((f = open(l, p)) == -1) err(ERR(), "%s", l); \
struct stat st; if (fstat(f, &st) == -1) err(ERR(), "%s", l)
#define xpread(f, b, n, o, l) if (pread(f, b, n, o) == -1) err(ERR(), "%s", l)