arm: smh: Return errno on error

Instead of printing in what are now library functions, try to return a
numeric error code. This also adjust some functions (such as read) to
behave more similarly to read(2). For example, we now return the number
of bytes read instead of failing immediately on a short read.

Signed-off-by: Sean Anderson <sean.anderson@seco.com>
This commit is contained in:
Sean Anderson
2022-03-22 16:59:16 -04:00
committed by Tom Rini
parent eff77c3a24
commit 80e62ccfa6

View File

@@ -21,6 +21,7 @@
#define SYSCLOSE 0x02 #define SYSCLOSE 0x02
#define SYSREAD 0x06 #define SYSREAD 0x06
#define SYSFLEN 0x0C #define SYSFLEN 0x0C
#define SYSERRNO 0x13
/* /*
* Call the handler * Call the handler
@@ -39,10 +40,24 @@ static noinline long smh_trap(unsigned int sysnum, void *addr)
return result; return result;
} }
/* /**
* Open a file on the host. Mode is "r" or "rb" currently. Returns a file * smh_errno() - Read the host's errno
* descriptor or -1 on error. *
* This gets the value of the host's errno and negates it. The host's errno may
* or may not be set, so only call this function if a previous semihosting call
* has failed.
*
* Return: a negative error value
*/ */
static int smh_errno(void)
{
long ret = smh_trap(SYSERRNO, NULL);
if (ret > 0 && ret < INT_MAX)
return -ret;
return -EIO;
}
long smh_open(const char *fname, enum smh_open_mode mode) long smh_open(const char *fname, enum smh_open_mode mode)
{ {
long fd; long fd;
@@ -61,9 +76,7 @@ long smh_open(const char *fname, enum smh_open_mode mode)
/* Open the file on the host */ /* Open the file on the host */
fd = smh_trap(SYSOPEN, &open); fd = smh_trap(SYSOPEN, &open);
if (fd == -1) if (fd == -1)
printf("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd, return smh_errno();
fname);
return fd; return fd;
} }
@@ -86,19 +99,9 @@ long smh_read(long fd, void *memp, size_t len)
read.len = len; read.len = len;
ret = smh_trap(SYSREAD, &read); ret = smh_trap(SYSREAD, &read);
if (ret < 0) { if (ret < 0)
/* return smh_errno();
* The ARM handler allows for returning partial lengths, return len - ret;
* but in practice this never happens so rather than create
* hard to maintain partial read loops and such, just fail
* with an error message.
*/
printf("%s: ERROR ret %ld, fd %ld, len %zu memp %p\n",
__func__, ret, fd, len, memp);
return -1;
}
return 0;
} }
/* /*
@@ -112,9 +115,8 @@ long smh_close(long fd)
ret = smh_trap(SYSCLOSE, &fd); ret = smh_trap(SYSCLOSE, &fd);
if (ret == -1) if (ret == -1)
printf("%s: ERROR fd %ld\n", __func__, fd); return smh_errno();
return 0;
return ret;
} }
/* /*
@@ -128,8 +130,7 @@ long smh_flen(long fd)
ret = smh_trap(SYSFLEN, &fd); ret = smh_trap(SYSFLEN, &fd);
if (ret == -1) if (ret == -1)
printf("%s: ERROR ret %ld, fd %ld\n", __func__, ret, fd); return smh_errno();
return ret; return ret;
} }
@@ -141,28 +142,32 @@ static int smh_load_file(const char * const name, ulong load_addr,
long ret; long ret;
fd = smh_open(name, MODE_READ | MODE_BINARY); fd = smh_open(name, MODE_READ | MODE_BINARY);
if (fd == -1) if (fd < 0)
return -1; return fd;
len = smh_flen(fd); len = smh_flen(fd);
if (len < 0) { if (len < 0) {
smh_close(fd); smh_close(fd);
return -1; return len;
} }
ret = smh_read(fd, (void *)load_addr, len); ret = smh_read(fd, (void *)load_addr, len);
smh_close(fd); smh_close(fd);
if (ret == 0) { if (ret == len) {
*end_addr = load_addr + len - 1; *end_addr = load_addr + len - 1;
printf("loaded file %s from %08lX to %08lX, %08lX bytes\n", printf("loaded file %s from %08lX to %08lX, %08lX bytes\n",
name, name,
load_addr, load_addr,
*end_addr, *end_addr,
len); len);
} else { } else if (ret >= 0) {
printf("read failed\n"); ret = -EAGAIN;
return 0; }
if (ret < 0) {
printf("read failed: %ld\n", ret);
return ret;
} }
return 0; return 0;