summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-04 02:32:50 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-04 02:34:44 +0000
commitaceafd684a37a6a35bdd4f0aa4aecdf29c9711db (patch)
treea036b650ddfe82cf36d9725b1176e8323ed00e24
parent947211fc3c13412e3f65d17fef240989fda39b23 (diff)
util/nvmutil: unified file read error handling
it must be read perfectly, or else Signed-off-by: Leah Rowe <leah@libreboot.org>
-rw-r--r--util/nvmutil/nvmutil.c48
1 files changed, 20 insertions, 28 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c
index dabf8e83..319fe552 100644
--- a/util/nvmutil/nvmutil.c
+++ b/util/nvmutil/nvmutil.c
@@ -28,7 +28,8 @@ static void check_mac_separator(int);
static void set_mac_nib(int, int);
static uint8_t hextonum(char);
static uint8_t rhex(void);
-static void read_urandom(uint8_t *, size_t);
+static void read_file_PERFECTLY_or_die(int, void *, size_t,
+ off_t, const char *, const char *);
static int check_read_or_die(const char *,
ssize_t, size_t, int, const char *);
static int write_mac_part(int);
@@ -293,23 +294,10 @@ read_gbe(void)
static void
read_gbe_part(int p, int invert)
{
- int retry;
- ssize_t rval;
-
- for (retry = 0; retry < MAX_RETRY_READ; retry++) {
- rval = pread(fd, buf + (SIZE_4KB * (p ^ invert)),
- SIZE_4KB, (off_t)p * partsize);
-
- if (check_read_or_die(fname, rval, SIZE_4KB, retry, "pread"))
- break;
- }
+ read_file_PERFECTLY_or_die(fd, buf + (SIZE_4KB * (p ^ invert)),
+ SIZE_4KB, ((off_t)p) * partsize, fname, "pread");
- if (errno)
- err(errno, "Unhandled error on read of file '%s'", fname);
- if (rval != (ssize_t)SIZE_4KB)
- err(ECANCELED, "Unknown error on read of file '%s'", fname);
-
- swap(p ^ invert); /* handle big-endian host CPU */
+ swap(p ^ invert);
}
static void
@@ -412,29 +400,33 @@ rhex(void)
if (!n) {
n = sizeof(rnum) - 1;
- read_urandom(rnum, sizeof(rnum));
+ read_file_PERFECTLY_or_die(rfd, rnum, sizeof(rnum),
+ 0, "/dev/urandom", NULL);
}
return rnum[n--] & 0xf;
}
static void
-read_urandom(uint8_t *rnum, size_t rsize)
+read_file_PERFECTLY_or_die(int fd, void *buf, size_t len,
+ off_t off, const char *path, const char *op)
{
- int retry = 0;
+ int retry;
ssize_t rval;
for (retry = 0; retry < MAX_RETRY_READ; retry++) {
- rval = read(rfd, rnum, rsize);
- if (check_read_or_die("/dev/urandom", rval,
- rsize, retry, "read"))
- break;
+ if (op == NULL)
+ rval = read(fd, buf, len);
+ else
+ rval = pread(fd, buf, len, off);
+
+ if (check_read_or_die(path, rval, len, retry,
+ op ? op : "read"))
+ return;
}
- if (errno)
- err(errno, "Unhandled error on read of file '/dev/urandom'");
- if (rval != (ssize_t)rsize)
- err(ECANCELED, "Unknown error on read of file '/dev/urandom'");
+ err(EINTR, "%s: max retries exceeded on file: %s",
+ op ? op : "read", path);
}
static int