In order to add filesystem support, we will need to be able to seek and
write files. Add the appropriate helper functions.

Signed-off-by: Sean Anderson <sean.ander...@seco.com>
---

(no changes since v1)

 arch/arm/lib/semihosting.c | 67 +++++++++++++++++++++++++++++++-------
 include/semihosting.h      | 20 ++++++++++++
 2 files changed, 76 insertions(+), 11 deletions(-)

diff --git a/arch/arm/lib/semihosting.c b/arch/arm/lib/semihosting.c
index 2943f7b82f..d08003cef1 100644
--- a/arch/arm/lib/semihosting.c
+++ b/arch/arm/lib/semihosting.c
@@ -1,15 +1,13 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
+ * Copyright (C) 2022 Sean Anderson <sean.ander...@seco.com>
  * Copyright 2014 Broadcom Corporation
  */
 
 /*
- * Minimal semihosting implementation for reading files into memory. If more
- * features like writing files or console output are required they can be
- * added later. This code has been tested on arm64/aarch64 fastmodel only.
- * An untested placeholder exists for armv7 architectures, but since they
- * are commonly available in silicon now, fastmodel usage makes less sense
- * for them.
+ * This code has been tested on arm64/aarch64 fastmodel only.  An untested
+ * placeholder exists for armv7 architectures, but since they are commonly
+ * available in silicon now, fastmodel usage makes less sense for them.
  */
 #include <common.h>
 #include <command.h>
@@ -19,7 +17,9 @@
 
 #define SYSOPEN                0x01
 #define SYSCLOSE       0x02
+#define SYSWRITE       0x05
 #define SYSREAD                0x06
+#define SYSSEEK                0x0A
 #define SYSFLEN                0x0C
 #define SYSERRNO       0x13
 
@@ -80,14 +80,22 @@ long smh_open(const char *fname, enum smh_open_mode mode)
        return fd;
 }
 
+/**
+ * struct smg_rdwr_s - Arguments for read and write
+ * @fd: A file descriptor returned from smh_open()
+ * @memp: Pointer to a buffer of memory of at least @len bytes
+ * @len: The number of bytes to read or write
+ */
+struct smh_rdwr_s {
+       long fd;
+       void *memp;
+       size_t len;
+};
+
 long smh_read(long fd, void *memp, size_t len)
 {
        long ret;
-       struct smh_read_s {
-               long fd;
-               void *memp;
-               size_t len;
-       } read;
+       struct smh_rdwr_s read;
 
        debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len);
 
@@ -101,6 +109,24 @@ long smh_read(long fd, void *memp, size_t len)
        return len - ret;
 }
 
+long smh_write(long fd, const void *memp, size_t len, ulong *written)
+{
+       long ret;
+       struct smh_rdwr_s write;
+
+       debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len);
+
+       write.fd = fd;
+       write.memp = (void *)memp;
+       write.len = len;
+
+       ret = smh_trap(SYSWRITE, &write);
+       *written = len - ret;
+       if (ret)
+               return smh_errno();
+       return 0;
+}
+
 long smh_close(long fd)
 {
        long ret;
@@ -125,6 +151,25 @@ long smh_flen(long fd)
        return ret;
 }
 
+long smh_seek(long fd, long pos)
+{
+       long ret;
+       struct smh_seek_s {
+               long fd;
+               long pos;
+       } seek;
+
+       debug("%s: fd %ld pos %ld\n", __func__, fd, pos);
+
+       seek.fd = fd;
+       seek.pos = pos;
+
+       ret = smh_trap(SYSSEEK, &seek);
+       if (ret)
+               return smh_errno();
+       return 0;
+}
+
 static int smh_load_file(const char * const name, ulong load_addr,
                         ulong *end_addr)
 {
diff --git a/include/semihosting.h b/include/semihosting.h
index d8337b6269..b53c650444 100644
--- a/include/semihosting.h
+++ b/include/semihosting.h
@@ -50,6 +50,17 @@ long smh_open(const char *fname, enum smh_open_mode mode);
  */
 long smh_read(long fd, void *memp, size_t len);
 
+/**
+ * smh_write() - Write data to a file
+ * @fd: A file descriptor returned from smh_open()
+ * @memp: Pointer to a buffer of memory of at least @len bytes
+ * @len: The number of bytes to read
+ * @written: Pointer which will be updated with the actual bytes written
+ *
+ * Return: 0 on success or negative error on failure
+ */
+long smh_write(long fd, const void *memp, size_t len, ulong *written);
+
 /**
  * smh_close() - Close an open file
  * @fd: A file descriptor returned from smh_open()
@@ -66,4 +77,13 @@ long smh_close(long fd);
  */
 long smh_flen(long fd);
 
+/**
+ * smh_seek() - Seek to a position in a file
+ * @fd: A file descriptor returned from smh_open()
+ * @pos: The offset (in bytes) to seek to
+ *
+ * Return: 0 on success or negative error on failure
+ */
+long smh_seek(long fd, long pos);
+
 #endif /* _SEMIHOSTING_H */
-- 
2.25.1

Reply via email to