* for them.
*/
#include <common.h>
-#include <asm/semihosting.h>
+#include <command.h>
#define SYSOPEN 0x01
#define SYSCLOSE 0x02
/*
* Call the handler
*/
-static int smh_trap(unsigned int sysnum, void *addr)
+static noinline long smh_trap(unsigned int sysnum, void *addr)
{
- register int result asm("r0");
+ register long result asm("r0");
#if defined(CONFIG_ARM64)
asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr));
#else
}
/*
- * Open, load a file into memory, and close it. Check that the available space
- * is sufficient to store the entire file. Return the bytes actually read from
- * the file as seen by the read function. The verbose flag enables some extra
- * printing of successful read status.
+ * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
+ * descriptor or -1 on error.
*/
-int smh_load(const char *fname, void *memp, int avail, int verbose)
+static long smh_open(const char *fname, char *modestr)
{
- int ret, fd, len;
-
- ret = -1;
-
- debug("%s: fname \'%s\', avail %u, memp %p\n", __func__, fname,
- avail, memp);
-
- /* Open the file */
- fd = smh_open(fname, "rb");
- if (fd == -1)
- return ret;
+ long fd;
+ unsigned long mode;
+ struct smh_open_s {
+ const char *fname;
+ unsigned long mode;
+ size_t len;
+ } open;
- /* Get the file length */
- ret = smh_len_fd(fd);
- if (ret == -1) {
- smh_close(fd);
- return ret;
- }
+ debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
- /* Check that the file will fit in the supplied buffer */
- if (ret > avail) {
- printf("%s: ERROR ret %d, avail %u\n", __func__, ret,
- avail);
- smh_close(fd);
- return ret;
+ /* 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;
}
- len = ret;
-
- /* Read the file into the buffer */
- ret = smh_read(fd, memp, len);
- if (ret == 0) {
- /* Print successful load information if requested */
- if (verbose) {
- printf("\n%s\n", fname);
- printf(" 0x%8p dest\n", memp);
- printf(" 0x%08x size\n", len);
- printf(" 0x%08x avail\n", avail);
- }
- }
+ open.fname = fname;
+ open.len = strlen(fname);
+ open.mode = mode;
- /* Close the file */
- smh_close(fd);
+ /* 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 ret;
+ return fd;
}
/*
* Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure
*/
-int smh_read(int fd, void *memp, int len)
+static long smh_read(long fd, void *memp, size_t len)
{
- int ret;
+ long ret;
struct smh_read_s {
- int fd;
+ long fd;
void *memp;
- int len;
+ size_t len;
} read;
- debug("%s: fd %d, memp %p, len %d\n", __func__, fd, memp, len);
+ debug("%s: fd %ld, memp %p, len %lu\n", __func__, fd, memp, len);
read.fd = fd;
read.memp = memp;
read.len = len;
ret = smh_trap(SYSREAD, &read);
- if (ret == 0) {
- return 0;
- } else {
+ 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 %d, fd %d, len %u memp %p\n",
+ printf("%s: ERROR ret %ld, fd %ld, len %lu memp %p\n",
__func__, ret, fd, len, memp);
- }
- return ret;
-}
-
-/*
- * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
- * descriptor or -1 on error.
- */
-int smh_open(const char *fname, char *modestr)
-{
- int ret, fd, mode;
- struct smh_open_s {
- const char *fname;
- unsigned int mode;
- unsigned int len;
- } open;
-
- debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
-
- ret = -1;
-
- /* 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 ret;
+ return -1;
}
- open.fname = fname;
- open.len = strlen(fname);
- open.mode = mode;
-
- /* Open the file on the host */
- fd = smh_trap(SYSOPEN, &open);
- if (fd == -1)
- printf("%s: ERROR fd %d for file \'%s\'\n", __func__, fd,
- fname);
-
- return fd;
+ return 0;
}
/*
* Close the file using the file descriptor
*/
-int smh_close(int fd)
+static long smh_close(long fd)
{
- int ret;
- long fdlong;
+ long ret;
- debug("%s: fd %d\n", __func__, fd);
+ debug("%s: fd %ld\n", __func__, fd);
- fdlong = (long)fd;
- ret = smh_trap(SYSCLOSE, &fdlong);
+ ret = smh_trap(SYSCLOSE, &fd);
if (ret == -1)
- printf("%s: ERROR fd %d\n", __func__, fd);
+ printf("%s: ERROR fd %ld\n", __func__, fd);
return ret;
}
/*
* Get the file length from the file descriptor
*/
-int smh_len_fd(int fd)
+static long smh_len_fd(long fd)
{
- int ret;
- long fdlong;
+ long ret;
- debug("%s: fd %d\n", __func__, fd);
+ debug("%s: fd %ld\n", __func__, fd);
- fdlong = (long)fd;
- ret = smh_trap(SYSFLEN, &fdlong);
+ ret = smh_trap(SYSFLEN, &fd);
if (ret == -1)
- printf("%s: ERROR ret %d\n", __func__, ret);
+ printf("%s: ERROR ret %ld, fd %ld\n", __func__, ret, fd);
return ret;
}
-/*
- * Get the file length from the filename
- */
-int smh_len(const char *fname)
+static int smh_load_file(const char * const name, ulong load_addr,
+ ulong *end_addr)
{
- int ret, fd, len;
+ long fd;
+ long len;
+ long ret;
- debug("%s: file \'%s\'\n", __func__, fname);
-
- /* Open the file */
- fd = smh_open(fname, "rb");
+ fd = smh_open(name, "rb");
if (fd == -1)
- return fd;
+ return -1;
- /* Get the file length */
len = smh_len_fd(fd);
+ if (len < 0) {
+ smh_close(fd);
+ return -1;
+ }
- /* Close the file */
- ret = smh_close(fd);
- if (ret == -1)
- return ret;
+ ret = smh_read(fd, (void *)load_addr, len);
+ smh_close(fd);
- debug("%s: returning len %d\n", __func__, len);
+ if (ret == 0) {
+ *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;
+ }
- /* Return the file length (or -1 error indication) */
- return len;
+ return 0;
}
+
+static int do_smhload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ if (argc == 3 || argc == 4) {
+ ulong load_addr;
+ ulong end_addr = 0;
+ ulong ret;
+ char end_str[64];
+
+ load_addr = simple_strtoul(argv[2], NULL, 16);
+ if (!load_addr)
+ return -1;
+
+ ret = smh_load_file(argv[1], load_addr, &end_addr);
+ if (ret < 0)
+ return 1;
+
+ /* Optionally save returned end to the environment */
+ if (argc == 4) {
+ sprintf(end_str, "0x%08lx", end_addr);
+ setenv(argv[3], end_str);
+ }
+ } else {
+ return CMD_RET_USAGE;
+ }
+ return 0;
+}
+
+U_BOOT_CMD(smhload, 4, 0, do_smhload, "load a file using semihosting",
+ "<file> 0x<address> [end var]\n"
+ " - load a semihosted file to the address specified\n"
+ " if the optional [end var] is specified, the end\n"
+ " address of the file will be stored in this environment\n"
+ " variable.\n");