mirror of
https://codeberg.org/libreboot/lbmk.git
synced 2026-07-16 13:16:47 +02:00
util/nvmutil: unified file read error handling
it must be read perfectly, or else Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
+20
-28
@@ -28,7 +28,8 @@ static void check_mac_separator(int);
|
|||||||
static void set_mac_nib(int, int);
|
static void set_mac_nib(int, int);
|
||||||
static uint8_t hextonum(char);
|
static uint8_t hextonum(char);
|
||||||
static uint8_t rhex(void);
|
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 *,
|
static int check_read_or_die(const char *,
|
||||||
ssize_t, size_t, int, const char *);
|
ssize_t, size_t, int, const char *);
|
||||||
static int write_mac_part(int);
|
static int write_mac_part(int);
|
||||||
@@ -293,23 +294,10 @@ read_gbe(void)
|
|||||||
static void
|
static void
|
||||||
read_gbe_part(int p, int invert)
|
read_gbe_part(int p, int invert)
|
||||||
{
|
{
|
||||||
int retry;
|
read_file_PERFECTLY_or_die(fd, buf + (SIZE_4KB * (p ^ invert)),
|
||||||
ssize_t rval;
|
SIZE_4KB, ((off_t)p) * partsize, fname, "pread");
|
||||||
|
|
||||||
for (retry = 0; retry < MAX_RETRY_READ; retry++) {
|
swap(p ^ invert);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -412,29 +400,33 @@ rhex(void)
|
|||||||
|
|
||||||
if (!n) {
|
if (!n) {
|
||||||
n = sizeof(rnum) - 1;
|
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;
|
return rnum[n--] & 0xf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
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;
|
ssize_t rval;
|
||||||
|
|
||||||
for (retry = 0; retry < MAX_RETRY_READ; retry++) {
|
for (retry = 0; retry < MAX_RETRY_READ; retry++) {
|
||||||
rval = read(rfd, rnum, rsize);
|
if (op == NULL)
|
||||||
if (check_read_or_die("/dev/urandom", rval,
|
rval = read(fd, buf, len);
|
||||||
rsize, retry, "read"))
|
else
|
||||||
break;
|
rval = pread(fd, buf, len, off);
|
||||||
|
|
||||||
|
if (check_read_or_die(path, rval, len, retry,
|
||||||
|
op ? op : "read"))
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errno)
|
err(EINTR, "%s: max retries exceeded on file: %s",
|
||||||
err(errno, "Unhandled error on read of file '/dev/urandom'");
|
op ? op : "read", path);
|
||||||
if (rval != (ssize_t)rsize)
|
|
||||||
err(ECANCELED, "Unknown error on read of file '/dev/urandom'");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
|||||||
Reference in New Issue
Block a user