mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 4/6] firmware: semihosting: translate return values in wrappers
Date: Tue, 11 Jun 2024 08:59:21 +0200	[thread overview]
Message-ID: <20240611065923.2900168-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240611065923.2900168-1-a.fatoum@pengutronix.de>

It's needlessly verbose to expect callers of the semihosting wrappers to
call semihosting_errno() on error, so let's just do that inside the
wrappers so they behave like other functions.

While at it, also have semihosting_read() and semihosting_write() return
the number of bytes sent. The semihosting API returns the number of
bytes that were _not_ sent instead, so a return value of 0 is success.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/firmware/semihosting.c | 36 ++++++++++++++++++++++------------
 fs/smhfs.c                     | 33 +++++++++----------------------
 2 files changed, 33 insertions(+), 36 deletions(-)

diff --git a/drivers/firmware/semihosting.c b/drivers/firmware/semihosting.c
index 1dfbdf219727..9663959aa49f 100644
--- a/drivers/firmware/semihosting.c
+++ b/drivers/firmware/semihosting.c
@@ -43,6 +43,12 @@ enum {
 
 long semihosting_trap(ulong sysnum, void *addr);
 
+static long semihosting_call(ulong sysnum, void *addr)
+{
+	long ret = semihosting_trap(sysnum, addr);
+	return ret >= 0 ? ret : -semihosting_errno();
+}
+
 static ulong semihosting_flags_to_mode(int flags)
 {
 	static const int semihosting_open_modeflags[12] = {
@@ -81,13 +87,13 @@ int semihosting_open(const char *fname, int flags)
 		.mode = semihosting_flags_to_mode(flags),
 	};
 
-	return semihosting_trap(SEMIHOSTING_SYS_OPEN, &open);
+	return semihosting_call(SEMIHOSTING_SYS_OPEN, &open);
 }
 EXPORT_SYMBOL(semihosting_open);
 
 int semihosting_close(int fd)
 {
-	return semihosting_trap(SEMIHOSTING_SYS_CLOSE, &fd);
+	return semihosting_call(SEMIHOSTING_SYS_CLOSE, &fd);
 }
 EXPORT_SYMBOL(semihosting_close);
 
@@ -111,37 +117,43 @@ struct __packed semihosting_file_io {
 
 ssize_t semihosting_write(int fd, const void *buf, size_t count)
 {
+	ssize_t ret;
 	struct semihosting_file_io write = {
 		.fd = fd,
 		.memp = virt_to_phys(buf),
 		.len = count,
 	};
 
-	return semihosting_trap(SEMIHOSTING_SYS_WRITE, &write);
+	ret = semihosting_call(SEMIHOSTING_SYS_WRITE, &write);
+
+	return ret >= 0 ? count - ret : ret;
 }
 EXPORT_SYMBOL(semihosting_write);
 
 ssize_t semihosting_read(int fd, void *buf, size_t count)
 {
+	ssize_t ret;
 	struct semihosting_file_io read = {
 		.fd = fd,
 		.memp = virt_to_phys(buf),
 		.len = count,
 	};
 
-	return semihosting_trap(SEMIHOSTING_SYS_READ, &read);
+	ret = semihosting_call(SEMIHOSTING_SYS_READ, &read);
+
+	return ret >= 0 ? count - ret : ret;
 }
 EXPORT_SYMBOL(semihosting_read);
 
 int semihosting_readc(void)
 {
-	return semihosting_trap(SEMIHOSTING_SYS_READC, NULL);
+	return semihosting_call(SEMIHOSTING_SYS_READC, NULL);
 }
 EXPORT_SYMBOL(semihosting_readc);
 
 int semihosting_isatty(int fd)
 {
-	return semihosting_trap(SEMIHOSTING_SYS_ISATTY, &fd);
+	return semihosting_call(SEMIHOSTING_SYS_ISATTY, &fd);
 }
 EXPORT_SYMBOL(semihosting_isatty);
 
@@ -155,12 +167,12 @@ off_t semihosting_seek(int fd, off_t pos)
 		.pos = pos,
 	};
 
-	return semihosting_trap(SEMIHOSTING_SYS_SEEK, &seek);
+	return semihosting_call(SEMIHOSTING_SYS_SEEK, &seek);
 }
 EXPORT_SYMBOL(semihosting_seek);
 off_t semihosting_flen(int fd)
 {
-	return semihosting_trap(SEMIHOSTING_SYS_FLEN, &fd);
+	return semihosting_call(SEMIHOSTING_SYS_FLEN, &fd);
 }
 EXPORT_SYMBOL(semihosting_flen);
 
@@ -174,7 +186,7 @@ int semihosting_remove(const char *fname)
 		.fname_length = strlen(fname),
 	};
 
-	return semihosting_trap(SEMIHOSTING_SYS_REMOVE, &remove);
+	return semihosting_call(SEMIHOSTING_SYS_REMOVE, &remove);
 }
 EXPORT_SYMBOL(semihosting_remove);
 
@@ -192,13 +204,13 @@ int semihosting_rename(const char *fname1, const char *fname2)
 		.fname2_length = strlen(fname2),
 	};
 
-	return semihosting_trap(SEMIHOSTING_SYS_RENAME, &rename);
+	return semihosting_call(SEMIHOSTING_SYS_RENAME, &rename);
 }
 EXPORT_SYMBOL(semihosting_rename);
 
 int semihosting_errno(void)
 {
-	return semihosting_trap(SEMIHOSTING_SYS_ERRNO, NULL);
+	return semihosting_call(SEMIHOSTING_SYS_ERRNO, NULL);
 }
 EXPORT_SYMBOL(semihosting_errno);
 
@@ -213,6 +225,6 @@ int semihosting_system(const char *command)
 		.cmd_len = strlen(command),
 	};
 
-	return semihosting_trap(SEMIHOSTING_SYS_SYSTEM, &system);
+	return semihosting_call(SEMIHOSTING_SYS_SYSTEM, &system);
 }
 EXPORT_SYMBOL(semihosting_system);
diff --git a/fs/smhfs.c b/fs/smhfs.c
index da285b5be52f..ce027f203e23 100644
--- a/fs/smhfs.c
+++ b/fs/smhfs.c
@@ -48,10 +48,7 @@ static int smhfs_rm(struct device __always_unused *dev,
 	/* Get rid of leading '/' */
 	pathname = &pathname[1];
 
-	if (semihosting_remove(pathname) != 0)
-		return -semihosting_errno();
-	else
-		return 0;
+	return semihosting_remove(pathname);
 }
 
 static int smhfs_truncate(struct device __always_unused *dev,
@@ -70,52 +67,40 @@ static int smhfs_open(struct device __always_unused *dev,
 
 	fd = semihosting_open(filename, file->flags);
 	if (fd < 0)
-		goto error;
+		return fd;
 
 	file->priv = (void *)(uintptr_t)fd;
 	file->size = semihosting_flen(fd);
 	if (file->size < 0)
-		goto error;
+		return file->size;
 
 	return 0;
-error:
-	return -semihosting_errno();
 }
 
 static int smhfs_close(struct device __always_unused *dev,
 		       FILE *f)
 {
-	if (semihosting_close(file_to_fd(f)))
-		return -semihosting_errno();
-	else
-		return 0;
+	return semihosting_close(file_to_fd(f));
 }
 
 static int smhfs_write(struct device __always_unused *dev,
 		       FILE *f, const void *inbuf, size_t insize)
 {
-	if (semihosting_write(file_to_fd(f), inbuf, insize))
-		return -semihosting_errno();
-	else
-		return insize;
+	long ret = semihosting_write(file_to_fd(f), inbuf, insize);
+	return ret < 0 ? ret : insize;
 }
 
 static int smhfs_read(struct device __always_unused *dev,
 		      FILE *f, void *buf, size_t insize)
 {
-	if (!semihosting_read(file_to_fd(f), buf, insize))
-		return insize;
-	else
-		return -semihosting_errno();
+	long ret = semihosting_read(file_to_fd(f), buf, insize);
+	return ret < 0 ? ret : insize;
 }
 
 static int smhfs_lseek(struct device __always_unused *dev,
 			  FILE *f, loff_t pos)
 {
-	if (semihosting_seek(file_to_fd(f), pos))
-		return -semihosting_errno();
-
-	return 0;
+	return semihosting_seek(file_to_fd(f), pos);
 }
 
 static DIR* smhfs_opendir(struct device __always_unused *dev,
-- 
2.39.2




  parent reply	other threads:[~2024-06-11  7:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-11  6:59 [PATCH 0/6] semihosting: extend support for ARM64, console Ahmad Fatoum
2024-06-11  6:59 ` [PATCH 1/6] ARM: lib32: semihosting: prepare for more general use Ahmad Fatoum
2024-06-11  6:59 ` [PATCH 2/6] firmware: semihosting: add ARMv8-A semihosting support Ahmad Fatoum
2024-06-11  6:59 ` [PATCH 3/6] firmware: semihosting: don't return error code from writec/write0 Ahmad Fatoum
2024-06-11  6:59 ` Ahmad Fatoum [this message]
2024-06-11  6:59 ` [PATCH 5/6] ARM: semihosting: add DEBUG_LL implementation Ahmad Fatoum
2024-06-11  6:59 ` [PATCH 6/6] serial: add semihosting console Ahmad Fatoum
2024-06-13  7:18 ` [PATCH 0/6] semihosting: extend support for ARM64, console Sascha Hauer

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=20240611065923.2900168-5-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox