Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2014 Broadcom Corporation |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Minimal semihosting implementation for reading files into memory. If more |
| 8 | * features like writing files or console output are required they can be |
| 9 | * added later. This code has been tested on arm64/aarch64 fastmodel only. |
| 10 | * An untested placeholder exists for armv7 architectures, but since they |
| 11 | * are commonly available in silicon now, fastmodel usage makes less sense |
| 12 | * for them. |
| 13 | */ |
| 14 | #include <common.h> |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 15 | #include <command.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 16 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 17 | #include <log.h> |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 18 | #include <semihosting.h> |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 19 | |
| 20 | #define SYSOPEN 0x01 |
| 21 | #define SYSCLOSE 0x02 |
| 22 | #define SYSREAD 0x06 |
| 23 | #define SYSFLEN 0x0C |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame^] | 24 | #define SYSERRNO 0x13 |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 25 | |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 26 | /* |
| 27 | * Call the handler |
| 28 | */ |
Linus Walleij | 1c2e27e | 2015-03-23 11:06:10 +0100 | [diff] [blame] | 29 | static noinline long smh_trap(unsigned int sysnum, void *addr) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 30 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 31 | register long result asm("r0"); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 32 | #if defined(CONFIG_ARM64) |
| 33 | asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr)); |
Vadzim Dambrouski | 98d3894 | 2015-10-19 19:40:14 +0300 | [diff] [blame] | 34 | #elif defined(CONFIG_CPU_V7M) |
| 35 | asm volatile ("bkpt #0xAB" : "=r" (result) : "0"(sysnum), "r"(addr)); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 36 | #else |
| 37 | /* Note - untested placeholder */ |
| 38 | asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr)); |
| 39 | #endif |
| 40 | return result; |
| 41 | } |
| 42 | |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame^] | 43 | /** |
| 44 | * smh_errno() - Read the host's errno |
| 45 | * |
| 46 | * This gets the value of the host's errno and negates it. The host's errno may |
| 47 | * or may not be set, so only call this function if a previous semihosting call |
| 48 | * has failed. |
| 49 | * |
| 50 | * Return: a negative error value |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 51 | */ |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame^] | 52 | static int smh_errno(void) |
| 53 | { |
| 54 | long ret = smh_trap(SYSERRNO, NULL); |
| 55 | |
| 56 | if (ret > 0 && ret < INT_MAX) |
| 57 | return -ret; |
| 58 | return -EIO; |
| 59 | } |
| 60 | |
Sean Anderson | a3a6c69 | 2022-03-22 16:59:15 -0400 | [diff] [blame] | 61 | long smh_open(const char *fname, enum smh_open_mode mode) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 62 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 63 | long fd; |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 64 | struct smh_open_s { |
| 65 | const char *fname; |
| 66 | unsigned long mode; |
| 67 | size_t len; |
| 68 | } open; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 69 | |
Sean Anderson | a3a6c69 | 2022-03-22 16:59:15 -0400 | [diff] [blame] | 70 | debug("%s: file \'%s\', mode \'%u\'\n", __func__, fname, mode); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 71 | |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 72 | open.fname = fname; |
| 73 | open.len = strlen(fname); |
| 74 | open.mode = mode; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 75 | |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 76 | /* Open the file on the host */ |
| 77 | fd = smh_trap(SYSOPEN, &open); |
| 78 | if (fd == -1) |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame^] | 79 | return smh_errno(); |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 80 | return fd; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure |
| 85 | */ |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 86 | long smh_read(long fd, void *memp, size_t len) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 87 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 88 | long ret; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 89 | struct smh_read_s { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 90 | long fd; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 91 | void *memp; |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 92 | size_t len; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 93 | } read; |
| 94 | |
Vadzim Dambrouski | 2738215 | 2015-10-19 19:40:15 +0300 | [diff] [blame] | 95 | debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 96 | |
| 97 | read.fd = fd; |
| 98 | read.memp = memp; |
| 99 | read.len = len; |
| 100 | |
| 101 | ret = smh_trap(SYSREAD, &read); |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame^] | 102 | if (ret < 0) |
| 103 | return smh_errno(); |
| 104 | return len - ret; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 108 | * Close the file using the file descriptor |
| 109 | */ |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 110 | long smh_close(long fd) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 111 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 112 | long ret; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 113 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 114 | debug("%s: fd %ld\n", __func__, fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 115 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 116 | ret = smh_trap(SYSCLOSE, &fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 117 | if (ret == -1) |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame^] | 118 | return smh_errno(); |
| 119 | return 0; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Get the file length from the file descriptor |
| 124 | */ |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 125 | long smh_flen(long fd) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 126 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 127 | long ret; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 128 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 129 | debug("%s: fd %ld\n", __func__, fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 130 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 131 | ret = smh_trap(SYSFLEN, &fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 132 | if (ret == -1) |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame^] | 133 | return smh_errno(); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 134 | return ret; |
| 135 | } |
| 136 | |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 137 | static int smh_load_file(const char * const name, ulong load_addr, |
| 138 | ulong *end_addr) |
| 139 | { |
| 140 | long fd; |
| 141 | long len; |
| 142 | long ret; |
| 143 | |
Sean Anderson | a3a6c69 | 2022-03-22 16:59:15 -0400 | [diff] [blame] | 144 | fd = smh_open(name, MODE_READ | MODE_BINARY); |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame^] | 145 | if (fd < 0) |
| 146 | return fd; |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 147 | |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 148 | len = smh_flen(fd); |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 149 | if (len < 0) { |
| 150 | smh_close(fd); |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame^] | 151 | return len; |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | ret = smh_read(fd, (void *)load_addr, len); |
| 155 | smh_close(fd); |
| 156 | |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame^] | 157 | if (ret == len) { |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 158 | *end_addr = load_addr + len - 1; |
| 159 | printf("loaded file %s from %08lX to %08lX, %08lX bytes\n", |
| 160 | name, |
| 161 | load_addr, |
| 162 | *end_addr, |
| 163 | len); |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame^] | 164 | } else if (ret >= 0) { |
| 165 | ret = -EAGAIN; |
| 166 | } |
| 167 | |
| 168 | if (ret < 0) { |
| 169 | printf("read failed: %ld\n", ret); |
| 170 | return ret; |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 176 | static int do_smhload(struct cmd_tbl *cmdtp, int flag, int argc, |
| 177 | char *const argv[]) |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 178 | { |
| 179 | if (argc == 3 || argc == 4) { |
| 180 | ulong load_addr; |
| 181 | ulong end_addr = 0; |
Ryan Harkin | b564d52 | 2017-03-02 17:45:16 +0000 | [diff] [blame] | 182 | int ret; |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 183 | char end_str[64]; |
| 184 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 185 | load_addr = hextoul(argv[2], NULL); |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 186 | if (!load_addr) |
| 187 | return -1; |
| 188 | |
| 189 | ret = smh_load_file(argv[1], load_addr, &end_addr); |
| 190 | if (ret < 0) |
Ryan Harkin | b564d52 | 2017-03-02 17:45:16 +0000 | [diff] [blame] | 191 | return CMD_RET_FAILURE; |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 192 | |
| 193 | /* Optionally save returned end to the environment */ |
| 194 | if (argc == 4) { |
| 195 | sprintf(end_str, "0x%08lx", end_addr); |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 196 | env_set(argv[3], end_str); |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 197 | } |
| 198 | } else { |
| 199 | return CMD_RET_USAGE; |
| 200 | } |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | U_BOOT_CMD(smhload, 4, 0, do_smhload, "load a file using semihosting", |
| 205 | "<file> 0x<address> [end var]\n" |
| 206 | " - load a semihosted file to the address specified\n" |
| 207 | " if the optional [end var] is specified, the end\n" |
| 208 | " address of the file will be stored in this environment\n" |
| 209 | " variable.\n"); |