util/nvmutil: split up rw_file_exact()

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-10 16:43:47 +00:00
parent 454af12153
commit 69cf4fe6ed
+31 -36
View File
@@ -211,6 +211,8 @@ static off_t gbe_x_offset(size_t part, const char *f_op,
const char *d_type, off_t nsize, off_t ncmp); const char *d_type, off_t nsize, off_t ncmp);
static ssize_t rw_file_exact(int fd, uint8_t *mem, size_t len, static ssize_t rw_file_exact(int fd, uint8_t *mem, size_t len,
off_t off, int rw_type); off_t off, int rw_type);
static ssize_t do_rw(int fd,
uint8_t *mem, size_t len, off_t off, int rw_type);
static ssize_t prw(int fd, void *mem, size_t nrw, static ssize_t prw(int fd, void *mem, size_t nrw,
off_t off, int rw_type); off_t off, int rw_type);
static off_t lseek_eintr(int fd, off_t off, int whence); static off_t lseek_eintr(int fd, off_t off, int whence);
@@ -1404,56 +1406,49 @@ rw_file_exact(int fd, uint8_t *mem, size_t len,
ssize_t rval = 0; ssize_t rval = 0;
size_t rc = 0; size_t rc = 0;
if (fd < 0) { if (fd < 0 || !len || len > (size_t)SSIZE_MAX) {
set_err_if_unset(EIO);
return -1;
}
if (!len) {
set_err_if_unset(EIO);
return -1;
}
if (len > (size_t)SSIZE_MAX) {
set_err_if_unset(EIO); set_err_if_unset(EIO);
return -1; return -1;
} }
while (rc < len) { while (rc < len) {
if (rw_type == PSCHREIB) { rval = do_rw(fd, mem, len, off, rw_type);
rval = prw(fd, mem + rc, len - rc,
off + rc, rw_type); if (rval < 0 && errno == EINTR) {
} else if (rw_type == SCHREIB) { continue;
rval = write(fd, mem + rc, len - rc); } else if (rval < 0) {
} else if (rw_type == PLESEN) { set_err_if_unset(EIO);
rval = prw(fd, mem + rc, len - rc, return -1;
off + rc, rw_type); }
} else if (rw_type == LESEN) { if ((size_t)rval > (len - rc) /* Prevent overflow */
rval = read(fd, mem + rc, len - rc); || rval == 0) { /* Prevent infinite 0-byte loop */
} else {
set_err_if_unset(EIO); set_err_if_unset(EIO);
return -1; return -1;
} }
if (rval >= 0) { rc += (size_t)rval;
if ((size_t)rval > (len - rc) /* Prevent overflow */
|| rval == 0) { /* Prevent infinite 0-byte loop */
set_err_if_unset(EIO);
return -1;
}
rc += (size_t)rval;
continue;
}
if (rval < 0 && errno == EINTR)
continue;
set_err_if_unset(EIO);
return -1;
} }
return rc; return rc;
} }
static ssize_t
do_rw(int fd, uint8_t *mem,
size_t len, off_t off, int rw_type)
{
if (rw_type == LESEN)
return read(fd, mem, len);
if (rw_type == SCHREIB)
return write(fd, mem, len);
if (rw_type == PLESEN || rw_type == PSCHREIB)
return prw(fd, mem, len, off, rw_type);
set_err_if_unset(EINVAL);
return -1;
}
/* /*
* This implements a portable analog of pwrite() * This implements a portable analog of pwrite()
* and pread() - note that this version is not * and pread() - note that this version is not