sandbox: Return error code from read/write/seek

The existing API for these functions is different from the rest of
U-Boot, in that any error code must be obtained from the errno variable
on failure. This variable is part of the C library, so accessing it
outside of the special 'sandbox' shim-functions is not ideal.

Adjust the API to return an error code, to avoid this. Update existing
uses to check for any negative value, rather than just -1.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2024-08-07 16:47:24 -06:00 committed by Tom Rini
parent d8289e7dfe
commit b254a8359e
6 changed files with 37 additions and 21 deletions

View file

@ -47,12 +47,24 @@ struct os_mem_hdr {
ssize_t os_read(int fd, void *buf, size_t count)
{
return read(fd, buf, count);
ssize_t ret;
ret = read(fd, buf, count);
if (ret == -1)
return -errno;
return ret;
}
ssize_t os_write(int fd, const void *buf, size_t count)
{
return write(fd, buf, count);
ssize_t ret;
ret = write(fd, buf, count);
if (ret == -1)
return -errno;
return ret;
}
int os_printf(const char *fmt, ...)
@ -69,6 +81,8 @@ int os_printf(const char *fmt, ...)
off_t os_lseek(int fd, off_t offset, int whence)
{
off_t ret;
if (whence == OS_SEEK_SET)
whence = SEEK_SET;
else if (whence == OS_SEEK_CUR)
@ -77,7 +91,11 @@ off_t os_lseek(int fd, off_t offset, int whence)
whence = SEEK_END;
else
os_exit(1);
return lseek(fd, offset, whence);
ret = lseek(fd, offset, whence);
if (ret == -1)
return -errno;
return ret;
}
int os_open(const char *pathname, int os_flags)

View file

@ -195,16 +195,14 @@ static ulong read_fit_image(struct spl_load_info *load, ulong offset,
ret = os_lseek(load_ctx->fd, offset, OS_SEEK_SET);
if (ret < 0) {
printf("Failed to seek to %zx, got %zx (errno=%d)\n", offset,
ret, errno);
return log_msg_ret("lse", -errno);
printf("Failed to seek to %zx, got %zx\n", offset, ret);
return log_msg_ret("lse", ret);
}
res = os_read(load_ctx->fd, buf, size);
if (res < 0) {
printf("Failed to read %lx bytes, got %ld (errno=%d)\n",
size, res, errno);
return log_msg_ret("osr", -errno);
printf("Failed to read %lx bytes, got %ld\n", size, res);
return log_msg_ret("osr", res);
}
return size;
@ -238,9 +236,9 @@ int sandbox_spl_load_fit(char *fname, int maxlen, struct spl_image_info *image)
}
ret = os_read(fd, header, sizeof(*header));
if (ret != sizeof(*header)) {
printf("Failed to read %lx bytes, got %ld (errno=%d)\n",
sizeof(*header), ret, -errno);
return log_msg_ret("rea", -errno);
printf("Failed to read %lx bytes, got %d\n", sizeof(*header),
ret);
return log_msg_ret("rea", ret);
}
load_ctx.fd = fd;

View file

@ -25,7 +25,7 @@ static unsigned long host_block_read(struct udevice *dev,
struct udevice *host_dev = dev_get_parent(dev);
struct host_sb_plat *plat = dev_get_plat(host_dev);
if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) {
if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) < 0) {
printf("ERROR: Invalid block %lx\n", start);
return -1;
}
@ -44,7 +44,7 @@ static unsigned long host_block_write(struct udevice *dev,
struct udevice *host_dev = dev_get_parent(dev);
struct host_sb_plat *plat = dev_get_plat(host_dev);
if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) == -1) {
if (os_lseek(plat->fd, start * desc->blksz, OS_SEEK_SET) < 0) {
printf("ERROR: Invalid block %lx\n", start);
return -1;
}

View file

@ -196,7 +196,7 @@ static int handle_ufi_command(struct sandbox_flash_priv *priv, const void *buff,
priv->fd != -1) {
offset = os_lseek(priv->fd, info->seek_block * info->block_size,
OS_SEEK_SET);
if (offset == (off_t)-1)
if (offset < 0)
setup_fail_response(priv);
else
setup_response(priv);

View file

@ -28,7 +28,7 @@ int sandbox_fs_read_at(const char *filename, loff_t pos, void *buffer,
if (fd < 0)
return fd;
ret = os_lseek(fd, pos, OS_SEEK_SET);
if (ret == -1) {
if (ret < 0) {
os_close(fd);
return ret;
}
@ -65,14 +65,14 @@ int sandbox_fs_write_at(const char *filename, loff_t pos, void *buffer,
if (fd < 0)
return fd;
ret = os_lseek(fd, pos, OS_SEEK_SET);
if (ret == -1) {
if (ret < 0) {
os_close(fd);
return ret;
}
size = os_write(fd, buffer, towrite);
os_close(fd);
if (size == -1) {
if (size < 0) {
ret = -1;
} else {
ret = 0;

View file

@ -29,7 +29,7 @@ int os_printf(const char *format, ...);
* @fd: File descriptor as returned by os_open()
* @buf: Buffer to place data
* @count: Number of bytes to read
* Return: number of bytes read, or -1 on error
* Return: number of bytes read, or -errno on error
*/
ssize_t os_read(int fd, void *buf, size_t count);
@ -39,7 +39,7 @@ ssize_t os_read(int fd, void *buf, size_t count);
* @fd: File descriptor as returned by os_open()
* @buf: Buffer containing data to write
* @count: Number of bytes to write
* Return: number of bytes written, or -1 on error
* Return: number of bytes written, or -errno on error
*/
ssize_t os_write(int fd, const void *buf, size_t count);
@ -49,7 +49,7 @@ ssize_t os_write(int fd, const void *buf, size_t count);
* @fd: File descriptor as returned by os_open()
* @offset: File offset (based on whence)
* @whence: Position offset is relative to (see below)
* Return: new file offset
* Return: new file offset, or -errno on error
*/
off_t os_lseek(int fd, off_t offset, int whence);