[PATCH v3 07/29] arm: smh: Use numeric modes for smh_open
Sean Anderson
sean.anderson at seco.com
Tue Mar 22 21:59:15 CET 2022
There's no point in using string constants for smh_open if we are just
going to have to parse them. Instead, use numeric modes. The user needs
to be a bit careful with these, since they are much closer semantically
to string modes used by fopen(3) than the numeric modes used with
open(2).
Signed-off-by: Sean Anderson <sean.anderson at seco.com>
---
(no changes since v1)
arch/arm/lib/semihosting.c | 21 +++------------------
include/semihosting.h | 25 ++++++++++++++++++++++++-
2 files changed, 27 insertions(+), 19 deletions(-)
diff --git a/arch/arm/lib/semihosting.c b/arch/arm/lib/semihosting.c
index c38892fdd8..b983cc3935 100644
--- a/arch/arm/lib/semihosting.c
+++ b/arch/arm/lib/semihosting.c
@@ -22,9 +22,6 @@
#define SYSREAD 0x06
#define SYSFLEN 0x0C
-#define MODE_READ 0x0
-#define MODE_READBIN 0x1
-
/*
* Call the handler
*/
@@ -46,28 +43,16 @@ static noinline long smh_trap(unsigned int sysnum, void *addr)
* Open a file on the host. Mode is "r" or "rb" currently. Returns a file
* descriptor or -1 on error.
*/
-long smh_open(const char *fname, char *modestr)
+long smh_open(const char *fname, enum smh_open_mode mode)
{
long fd;
- unsigned long mode;
struct smh_open_s {
const char *fname;
unsigned long mode;
size_t len;
} open;
- debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
-
- /* Check the file mode */
- if (!(strcmp(modestr, "r"))) {
- mode = MODE_READ;
- } else if (!(strcmp(modestr, "rb"))) {
- mode = MODE_READBIN;
- } else {
- printf("%s: ERROR mode \'%s\' not supported\n", __func__,
- modestr);
- return -1;
- }
+ debug("%s: file \'%s\', mode \'%u\'\n", __func__, fname, mode);
open.fname = fname;
open.len = strlen(fname);
@@ -155,7 +140,7 @@ static int smh_load_file(const char * const name, ulong load_addr,
long len;
long ret;
- fd = smh_open(name, "rb");
+ fd = smh_open(name, MODE_READ | MODE_BINARY);
if (fd == -1)
return -1;
diff --git a/include/semihosting.h b/include/semihosting.h
index 3843863046..cf54819192 100644
--- a/include/semihosting.h
+++ b/include/semihosting.h
@@ -6,7 +6,30 @@
#ifndef _SEMIHOSTING_H
#define _SEMIHOSTING_H
-long smh_open(const char *fname, char *modestr);
+/**
+ * enum smh_open_mode - Numeric file modes for use with smh_open()
+ * MODE_READ: 'r'
+ * MODE_BINARY: 'b'
+ * MODE_PLUS: '+'
+ * MODE_WRITE: 'w'
+ * MODE_APPEND: 'a'
+ *
+ * These modes represent the mode string used by fopen(3) in a form which can
+ * be passed to smh_open(). These do NOT correspond directly to %O_RDONLY,
+ * %O_CREAT, etc; see fopen(3) for details. In particular, @MODE_PLUS
+ * effectively results in adding %O_RDWR, and @MODE_WRITE will add %O_TRUNC.
+ * For compatibility, @MODE_BINARY should be added when opening non-text files
+ * (such as images).
+ */
+enum smh_open_mode {
+ MODE_READ = 0x0,
+ MODE_BINARY = 0x1,
+ MODE_PLUS = 0x2,
+ MODE_WRITE = 0x4,
+ MODE_APPEND = 0x8,
+};
+
+long smh_open(const char *fname, enum smh_open_mode mode);
long smh_read(long fd, void *memp, size_t len);
long smh_close(long fd);
long smh_flen(long fd);
--
2.25.1
More information about the U-Boot
mailing list