All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Anderson <sean.anderson@seco.com>
To: Simon Glass <sjg@chromium.org>
Cc: Linus Walleij <linus.walleij@linaro.org>,
	Tom Rini <trini@konsulko.com>,
	u-boot@lists.denx.de, Sean Anderson <sean.anderson@seco.com>
Subject: [PATCH 07/17] arm: smh: Return errno on error
Date: Thu,  3 Mar 2022 15:43:50 -0500	[thread overview]
Message-ID: <20220303204400.2787389-8-sean.anderson@seco.com> (raw)
In-Reply-To: <20220303204400.2787389-1-sean.anderson@seco.com>

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>
---

 arch/arm/lib/semihosting.c | 67 ++++++++++++++++++++------------------
 1 file changed, 36 insertions(+), 31 deletions(-)

diff --git a/arch/arm/lib/semihosting.c b/arch/arm/lib/semihosting.c
index b983cc3935..1686457685 100644
--- a/arch/arm/lib/semihosting.c
+++ b/arch/arm/lib/semihosting.c
@@ -21,6 +21,7 @@
 #define SYSCLOSE	0x02
 #define SYSREAD		0x06
 #define SYSFLEN		0x0C
+#define SYSERRNO	0x13
 
 /*
  * Call the handler
@@ -39,10 +40,24 @@ static noinline long smh_trap(unsigned int sysnum, void *addr)
 	return result;
 }
 
-/*
- * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
- * descriptor or -1 on error.
+/**
+ * smh_errno() - Read the host's errno
+ *
+ * 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 fd;
@@ -61,9 +76,7 @@ long smh_open(const char *fname, enum smh_open_mode mode)
 	/* Open the file on the host */
 	fd = smh_trap(SYSOPEN, &open);
 	if (fd == -1)
-		printf("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd,
-		       fname);
-
+		return smh_errno();
 	return fd;
 }
 
@@ -86,19 +99,9 @@ long smh_read(long fd, void *memp, size_t len)
 	read.len = len;
 
 	ret = smh_trap(SYSREAD, &read);
-	if (ret < 0) {
-		/*
-		 * The ARM handler allows for returning partial lengths,
-		 * 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;
+	if (ret < 0)
+		return smh_errno();
+	return len - ret;
 }
 
 /*
@@ -112,9 +115,8 @@ long smh_close(long fd)
 
 	ret = smh_trap(SYSCLOSE, &fd);
 	if (ret == -1)
-		printf("%s: ERROR fd %ld\n", __func__, fd);
-
-	return ret;
+		return smh_errno();
+	return 0;
 }
 
 /*
@@ -128,8 +130,7 @@ long smh_flen(long fd)
 
 	ret = smh_trap(SYSFLEN, &fd);
 	if (ret == -1)
-		printf("%s: ERROR ret %ld, fd %ld\n", __func__, ret, fd);
-
+		return smh_errno();
 	return ret;
 }
 
@@ -141,28 +142,32 @@ static int smh_load_file(const char * const name, ulong load_addr,
 	long ret;
 
 	fd = smh_open(name, MODE_READ | MODE_BINARY);
-	if (fd == -1)
-		return -1;
+	if (fd < 0)
+		return fd;
 
 	len = smh_flen(fd);
 	if (len < 0) {
 		smh_close(fd);
-		return -1;
+		return len;
 	}
 
 	ret = smh_read(fd, (void *)load_addr, len);
 	smh_close(fd);
 
-	if (ret == 0) {
+	if (ret == len) {
 		*end_addr = load_addr + len - 1;
 		printf("loaded file %s from %08lX to %08lX, %08lX bytes\n",
 		       name,
 		       load_addr,
 		       *end_addr,
 		       len);
-	} else {
-		printf("read failed\n");
-		return 0;
+	} else if (ret >= 0) {
+		ret = -EAGAIN;
+	}
+
+	if (ret < 0) {
+		printf("read failed: %ld\n", ret);
+		return ret;
 	}
 
 	return 0;
-- 
2.25.1


  parent reply	other threads:[~2022-03-03 20:45 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-03 20:43 [PATCH 00/17] arm: semihosting: Cleanups and new features Sean Anderson
2022-03-03 20:43 ` [PATCH 01/17] doc: Convert semihosting readme to rST Sean Anderson
2022-03-03 20:43 ` [PATCH 02/17] nxp: ls1046ardb: Convert README " Sean Anderson
2022-03-03 20:43 ` [PATCH 03/17] doc: ls1046ardb: Expand boot mode section Sean Anderson
2022-03-03 20:43 ` [PATCH 04/17] arm: smh: Add semihosting entry to MAINTAINERS Sean Anderson
2022-03-03 20:43 ` [PATCH 05/17] arm: smh: Export semihosting functions Sean Anderson
2022-03-03 20:43 ` [PATCH 06/17] arm: smh: Use numeric modes for smh_open Sean Anderson
2022-03-03 20:43 ` Sean Anderson [this message]
2022-03-03 20:43 ` [PATCH 08/17] arm: smh: Document functions in header Sean Anderson
2022-03-03 20:43 ` [PATCH 09/17] arm: smh: Add some file manipulation commands Sean Anderson
2022-03-03 20:43 ` [PATCH 10/17] spl: Add semihosting boot method Sean Anderson
2022-03-03 20:43 ` [PATCH 11/17] fs: Add semihosting filesystem Sean Anderson
2022-03-03 20:43 ` [PATCH 12/17] cmd: fdt: Use start/size for chosen instead of start/end Sean Anderson
2022-03-03 20:43 ` [PATCH 13/17] arm: smh: Remove smhload command Sean Anderson
2022-03-03 20:43 ` [PATCH 14/17] arm: smh: Add some functions for working with the host console Sean Anderson
2022-03-03 20:43 ` [PATCH 15/17] serial: Add semihosting driver Sean Anderson
2022-03-03 20:43 ` [PATCH 16/17] doc: smh: Update semihosting documentation Sean Anderson
2022-03-03 20:44 ` [PATCH 17/17] ls1046ardb: Add support for JTAG boot Sean Anderson
2022-03-04  1:06 ` [PATCH 00/17] arm: semihosting: Cleanups and new features Linus Walleij
2022-03-04 11:47   ` Andre Przywara
2022-03-04 17:19     ` Sean Anderson
2022-03-04 18:46       ` Tom Rini
2022-03-10 16:48         ` Sean Anderson
2022-03-10 17:01           ` Andre Przywara
2022-03-10 17:06             ` Sean Anderson
2022-03-10 17:16               ` Tom Rini
2022-03-11 13:10                 ` Andre Przywara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220303204400.2787389-8-sean.anderson@seco.com \
    --to=sean.anderson@seco.com \
    --cc=linus.walleij@linaro.org \
    --cc=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.