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) 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) 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, ...) 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 os_lseek(int fd, off_t offset, int whence)
{ {
off_t ret;
if (whence == OS_SEEK_SET) if (whence == OS_SEEK_SET)
whence = SEEK_SET; whence = SEEK_SET;
else if (whence == OS_SEEK_CUR) else if (whence == OS_SEEK_CUR)
@ -77,7 +91,11 @@ off_t os_lseek(int fd, off_t offset, int whence)
whence = SEEK_END; whence = SEEK_END;
else else
os_exit(1); 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) 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); ret = os_lseek(load_ctx->fd, offset, OS_SEEK_SET);
if (ret < 0) { if (ret < 0) {
printf("Failed to seek to %zx, got %zx (errno=%d)\n", offset, printf("Failed to seek to %zx, got %zx\n", offset, ret);
ret, errno); return log_msg_ret("lse", ret);
return log_msg_ret("lse", -errno);
} }
res = os_read(load_ctx->fd, buf, size); res = os_read(load_ctx->fd, buf, size);
if (res < 0) { if (res < 0) {
printf("Failed to read %lx bytes, got %ld (errno=%d)\n", printf("Failed to read %lx bytes, got %ld\n", size, res);
size, res, errno); return log_msg_ret("osr", res);
return log_msg_ret("osr", -errno);
} }
return size; 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)); ret = os_read(fd, header, sizeof(*header));
if (ret != sizeof(*header)) { if (ret != sizeof(*header)) {
printf("Failed to read %lx bytes, got %ld (errno=%d)\n", printf("Failed to read %lx bytes, got %d\n", sizeof(*header),
sizeof(*header), ret, -errno); ret);
return log_msg_ret("rea", -errno); return log_msg_ret("rea", ret);
} }
load_ctx.fd = fd; 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 udevice *host_dev = dev_get_parent(dev);
struct host_sb_plat *plat = dev_get_plat(host_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); printf("ERROR: Invalid block %lx\n", start);
return -1; return -1;
} }
@ -44,7 +44,7 @@ static unsigned long host_block_write(struct udevice *dev,
struct udevice *host_dev = dev_get_parent(dev); struct udevice *host_dev = dev_get_parent(dev);
struct host_sb_plat *plat = dev_get_plat(host_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); printf("ERROR: Invalid block %lx\n", start);
return -1; return -1;
} }

View file

@ -196,7 +196,7 @@ static int handle_ufi_command(struct sandbox_flash_priv *priv, const void *buff,
priv->fd != -1) { priv->fd != -1) {
offset = os_lseek(priv->fd, info->seek_block * info->block_size, offset = os_lseek(priv->fd, info->seek_block * info->block_size,
OS_SEEK_SET); OS_SEEK_SET);
if (offset == (off_t)-1) if (offset < 0)
setup_fail_response(priv); setup_fail_response(priv);
else else
setup_response(priv); 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) if (fd < 0)
return fd; return fd;
ret = os_lseek(fd, pos, OS_SEEK_SET); ret = os_lseek(fd, pos, OS_SEEK_SET);
if (ret == -1) { if (ret < 0) {
os_close(fd); os_close(fd);
return ret; return ret;
} }
@ -65,14 +65,14 @@ int sandbox_fs_write_at(const char *filename, loff_t pos, void *buffer,
if (fd < 0) if (fd < 0)
return fd; return fd;
ret = os_lseek(fd, pos, OS_SEEK_SET); ret = os_lseek(fd, pos, OS_SEEK_SET);
if (ret == -1) { if (ret < 0) {
os_close(fd); os_close(fd);
return ret; return ret;
} }
size = os_write(fd, buffer, towrite); size = os_write(fd, buffer, towrite);
os_close(fd); os_close(fd);
if (size == -1) { if (size < 0) {
ret = -1; ret = -1;
} else { } else {
ret = 0; ret = 0;

View file

@ -29,7 +29,7 @@ int os_printf(const char *format, ...);
* @fd: File descriptor as returned by os_open() * @fd: File descriptor as returned by os_open()
* @buf: Buffer to place data * @buf: Buffer to place data
* @count: Number of bytes to read * @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); 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() * @fd: File descriptor as returned by os_open()
* @buf: Buffer containing data to write * @buf: Buffer containing data to write
* @count: Number of bytes 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); 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() * @fd: File descriptor as returned by os_open()
* @offset: File offset (based on whence) * @offset: File offset (based on whence)
* @whence: Position offset is relative to (see below) * @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); off_t os_lseek(int fd, off_t offset, int whence);