arm: smh: Add some file manipulation commands
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.anderson@seco.com>
This commit is contained in:
@@ -1,15 +1,13 @@
|
|||||||
// SPDX-License-Identifier: GPL-2.0+
|
// SPDX-License-Identifier: GPL-2.0+
|
||||||
/*
|
/*
|
||||||
|
* Copyright (C) 2022 Sean Anderson <sean.anderson@seco.com>
|
||||||
* Copyright 2014 Broadcom Corporation
|
* Copyright 2014 Broadcom Corporation
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Minimal semihosting implementation for reading files into memory. If more
|
* This code has been tested on arm64/aarch64 fastmodel only. An untested
|
||||||
* features like writing files or console output are required they can be
|
* placeholder exists for armv7 architectures, but since they are commonly
|
||||||
* added later. This code has been tested on arm64/aarch64 fastmodel only.
|
* available in silicon now, fastmodel usage makes less sense for them.
|
||||||
* 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 <common.h>
|
||||||
#include <command.h>
|
#include <command.h>
|
||||||
@@ -19,7 +17,9 @@
|
|||||||
|
|
||||||
#define SYSOPEN 0x01
|
#define SYSOPEN 0x01
|
||||||
#define SYSCLOSE 0x02
|
#define SYSCLOSE 0x02
|
||||||
|
#define SYSWRITE 0x05
|
||||||
#define SYSREAD 0x06
|
#define SYSREAD 0x06
|
||||||
|
#define SYSSEEK 0x0A
|
||||||
#define SYSFLEN 0x0C
|
#define SYSFLEN 0x0C
|
||||||
#define SYSERRNO 0x13
|
#define SYSERRNO 0x13
|
||||||
|
|
||||||
@@ -80,14 +80,22 @@ long smh_open(const char *fname, enum smh_open_mode mode)
|
|||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
long smh_read(long fd, void *memp, size_t len)
|
/**
|
||||||
{
|
* struct smg_rdwr_s - Arguments for read and write
|
||||||
long ret;
|
* @fd: A file descriptor returned from smh_open()
|
||||||
struct smh_read_s {
|
* @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;
|
long fd;
|
||||||
void *memp;
|
void *memp;
|
||||||
size_t len;
|
size_t len;
|
||||||
} read;
|
};
|
||||||
|
|
||||||
|
long smh_read(long fd, void *memp, size_t len)
|
||||||
|
{
|
||||||
|
long ret;
|
||||||
|
struct smh_rdwr_s read;
|
||||||
|
|
||||||
debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len);
|
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;
|
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 smh_close(long fd)
|
||||||
{
|
{
|
||||||
long ret;
|
long ret;
|
||||||
@@ -125,6 +151,25 @@ long smh_flen(long fd)
|
|||||||
return ret;
|
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,
|
static int smh_load_file(const char * const name, ulong load_addr,
|
||||||
ulong *end_addr)
|
ulong *end_addr)
|
||||||
{
|
{
|
||||||
|
@@ -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);
|
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
|
* smh_close() - Close an open file
|
||||||
* @fd: A file descriptor returned from smh_open()
|
* @fd: A file descriptor returned from smh_open()
|
||||||
@@ -66,4 +77,13 @@ long smh_close(long fd);
|
|||||||
*/
|
*/
|
||||||
long smh_flen(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 */
|
#endif /* _SEMIHOSTING_H */
|
||||||
|
Reference in New Issue
Block a user