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>
diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index da96ebe..f5c9a8a 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -47,12 +47,24 @@
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 @@
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 @@
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)
diff --git a/arch/sandbox/cpu/spl.c b/arch/sandbox/cpu/spl.c
index 49abe02..e86193d 100644
--- a/arch/sandbox/cpu/spl.c
+++ b/arch/sandbox/cpu/spl.c
@@ -195,16 +195,14 @@
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 @@
}
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;