mirror of
https://codeberg.org/libreboot/lbmk.git
synced 2026-07-20 05:36:23 +02:00
util/nvmutil: unified urandom/gbe file reading
like before, but with the newly correct logic Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
+24
-58
@@ -96,8 +96,6 @@ static void open_gbe_file(void);
|
|||||||
static void xopen(int *fd, const char *path, int flags, struct stat *st);
|
static void xopen(int *fd, const char *path, int flags, struct stat *st);
|
||||||
static void read_gbe_file(void);
|
static void read_gbe_file(void);
|
||||||
static void read_gbe_file_part(size_t part);
|
static void read_gbe_file_part(size_t part);
|
||||||
static void read_gbe_file_exact(int fd, uint8_t *buf, size_t len,
|
|
||||||
off_t off);
|
|
||||||
static void read_checksums(void);
|
static void read_checksums(void);
|
||||||
static int good_checksum(size_t partnum);
|
static int good_checksum(size_t partnum);
|
||||||
static void run_cmd(size_t c);
|
static void run_cmd(size_t c);
|
||||||
@@ -111,10 +109,8 @@ static void set_mac_nib(size_t mac_str_pos,
|
|||||||
size_t mac_byte_pos, size_t mac_nib_pos);
|
size_t mac_byte_pos, size_t mac_nib_pos);
|
||||||
static uint16_t hextonum(char ch_s);
|
static uint16_t hextonum(char ch_s);
|
||||||
static uint16_t rhex(void);
|
static uint16_t rhex(void);
|
||||||
#ifndef NVMUTIL_ARC4RANDOM_BUF
|
static void read_file_exact(int fd, uint8_t *mem, size_t len,
|
||||||
static ssize_t read_dev_urandom(int fd, void *buf,
|
off_t off, uint8_t plesen, const char *path);
|
||||||
size_t len);
|
|
||||||
#endif
|
|
||||||
static void write_mac_part(size_t partnum);
|
static void write_mac_part(size_t partnum);
|
||||||
static void cmd_helper_dump(void);
|
static void cmd_helper_dump(void);
|
||||||
static void print_mac_from_nvm(size_t partnum);
|
static void print_mac_from_nvm(size_t partnum);
|
||||||
@@ -702,32 +698,9 @@ read_gbe_file_part(size_t p)
|
|||||||
uint8_t *mem_offset =
|
uint8_t *mem_offset =
|
||||||
gbe_mem_offset(p ^ command[cmd_index].invert, "pread");
|
gbe_mem_offset(p ^ command[cmd_index].invert, "pread");
|
||||||
|
|
||||||
read_gbe_file_exact(gbe_fd, mem_offset,
|
read_file_exact(gbe_fd, mem_offset,
|
||||||
gbe_rw_size, gbe_file_offset(p, "pread"));
|
gbe_rw_size, gbe_file_offset(p, "pread"),
|
||||||
}
|
1, fname);
|
||||||
|
|
||||||
static void
|
|
||||||
read_gbe_file_exact(int fd,
|
|
||||||
uint8_t *buf, size_t len, off_t off)
|
|
||||||
{
|
|
||||||
ssize_t rval = -1;
|
|
||||||
ssize_t rc = 0;
|
|
||||||
|
|
||||||
if (fd == -1)
|
|
||||||
err(ECANCELED, "Trying to open bad fd: %s", fname);
|
|
||||||
|
|
||||||
for (rc = 0; rc != (ssize_t)len; rc += rval) {
|
|
||||||
if ((rval = pread(fd, buf + rc, len - rc, off + rc)) > -1) {
|
|
||||||
if (!rval) /* prevent infinite loop */
|
|
||||||
err(EIO, "%s: pread of 0 bytes", fname);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (errno != EINTR || rval < -1)
|
|
||||||
err(EIO, "%s", fname);
|
|
||||||
|
|
||||||
errno = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -961,52 +934,45 @@ rhex(void)
|
|||||||
static uint8_t rnum[12];
|
static uint8_t rnum[12];
|
||||||
|
|
||||||
if (!n) {
|
if (!n) {
|
||||||
#ifdef NVMUTIL_ARC4RANDOM_BUF
|
|
||||||
n = sizeof(rnum);
|
n = sizeof(rnum);
|
||||||
|
#ifdef NVMUTIL_ARC4RANDOM_BUF
|
||||||
arc4random_buf(rnum, n);
|
arc4random_buf(rnum, n);
|
||||||
#else
|
#else
|
||||||
n = (size_t)read_dev_urandom(
|
read_file_exact(urandom_fd, rnum, n, 0, 0, rname);
|
||||||
urandom_fd, rnum, sizeof(rnum));
|
|
||||||
|
|
||||||
if (!n || n > sizeof(rnum))
|
|
||||||
err(ECANCELED, "Randomisation failure");
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
return (uint16_t)(rnum[--n] & 0xf);
|
return (uint16_t)(rnum[--n] & 0xf);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef NVMUTIL_ARC4RANDOM_BUF
|
static void
|
||||||
static ssize_t
|
read_file_exact(int fd, uint8_t *mem,
|
||||||
read_dev_urandom(int fd, void *buf, size_t len)
|
size_t len, off_t off, uint8_t plesen, const char *path)
|
||||||
{
|
{
|
||||||
int retry;
|
ssize_t rval = -1;
|
||||||
ssize_t rval;
|
ssize_t rc = 0;
|
||||||
|
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
err(ECANCELED, "Trying to open bad fd: %s", rname);
|
err(ECANCELED, "Trying to open bad fd: %s", path);
|
||||||
|
|
||||||
for (retry = 0; retry < MAX_RETRY_RW; retry++) {
|
for (rc = 0; rc != (ssize_t)len; rc += rval) {
|
||||||
rval = read(fd, buf, len);
|
if (plesen)
|
||||||
|
rval = pread(fd, mem + rc, len - rc, off + rc);
|
||||||
|
else
|
||||||
|
rval = read(fd, mem + rc, len - rc);
|
||||||
|
|
||||||
if (rval == -1) {
|
if (rval > -1) {
|
||||||
if (errno == EINTR)
|
if (!rval) /* prevent infinite loop */
|
||||||
continue;
|
err(EIO, "%s: read of 0 bytes", path);
|
||||||
err(errno, "%s", rname);
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!rval || (size_t)rval > len)
|
if (errno != EINTR || rval < -1)
|
||||||
continue;
|
err(EIO, "%s", path);
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
return rval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err(EINTR, "%s: read: max retries exceeded: %s", rname);
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
write_mac_part(size_t partnum)
|
write_mac_part(size_t partnum)
|
||||||
|
|||||||
Reference in New Issue
Block a user