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 |
| 24 | |
| 25 | #define MODE_READ 0x0 |
| 26 | #define MODE_READBIN 0x1 |
| 27 | |
| 28 | /* |
| 29 | * Call the handler |
| 30 | */ |
Linus Walleij | 1c2e27e | 2015-03-23 11:06:10 +0100 | [diff] [blame] | 31 | static noinline long smh_trap(unsigned int sysnum, void *addr) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 32 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 33 | register long result asm("r0"); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 34 | #if defined(CONFIG_ARM64) |
| 35 | asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr)); |
Vadzim Dambrouski | 98d3894 | 2015-10-19 19:40:14 +0300 | [diff] [blame] | 36 | #elif defined(CONFIG_CPU_V7M) |
| 37 | asm volatile ("bkpt #0xAB" : "=r" (result) : "0"(sysnum), "r"(addr)); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 38 | #else |
| 39 | /* Note - untested placeholder */ |
| 40 | asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr)); |
| 41 | #endif |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | /* |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 46 | * Open a file on the host. Mode is "r" or "rb" currently. Returns a file |
| 47 | * descriptor or -1 on error. |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 48 | */ |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame^] | 49 | long smh_open(const char *fname, char *modestr) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 50 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 51 | long fd; |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 52 | unsigned long mode; |
| 53 | struct smh_open_s { |
| 54 | const char *fname; |
| 55 | unsigned long mode; |
| 56 | size_t len; |
| 57 | } open; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 58 | |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 59 | debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 60 | |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 61 | /* Check the file mode */ |
| 62 | if (!(strcmp(modestr, "r"))) { |
| 63 | mode = MODE_READ; |
| 64 | } else if (!(strcmp(modestr, "rb"))) { |
| 65 | mode = MODE_READBIN; |
| 66 | } else { |
| 67 | printf("%s: ERROR mode \'%s\' not supported\n", __func__, |
| 68 | modestr); |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 69 | return -1; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 70 | } |
| 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) |
| 79 | printf("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd, |
| 80 | fname); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 81 | |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 82 | return fd; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure |
| 87 | */ |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame^] | 88 | long smh_read(long fd, void *memp, size_t len) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 89 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 90 | long ret; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 91 | struct smh_read_s { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 92 | long fd; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 93 | void *memp; |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 94 | size_t len; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 95 | } read; |
| 96 | |
Vadzim Dambrouski | 2738215 | 2015-10-19 19:40:15 +0300 | [diff] [blame] | 97 | 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] | 98 | |
| 99 | read.fd = fd; |
| 100 | read.memp = memp; |
| 101 | read.len = len; |
| 102 | |
| 103 | ret = smh_trap(SYSREAD, &read); |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 104 | if (ret < 0) { |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 105 | /* |
| 106 | * The ARM handler allows for returning partial lengths, |
| 107 | * but in practice this never happens so rather than create |
| 108 | * hard to maintain partial read loops and such, just fail |
| 109 | * with an error message. |
| 110 | */ |
Vadzim Dambrouski | 2738215 | 2015-10-19 19:40:15 +0300 | [diff] [blame] | 111 | printf("%s: ERROR ret %ld, fd %ld, len %zu memp %p\n", |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 112 | __func__, ret, fd, len, memp); |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 113 | return -1; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 114 | } |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 115 | |
| 116 | return 0; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 120 | * Close the file using the file descriptor |
| 121 | */ |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame^] | 122 | long smh_close(long fd) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 123 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 124 | long ret; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 125 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 126 | debug("%s: fd %ld\n", __func__, fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 127 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 128 | ret = smh_trap(SYSCLOSE, &fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 129 | if (ret == -1) |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 130 | printf("%s: ERROR fd %ld\n", __func__, fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 131 | |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Get the file length from the file descriptor |
| 137 | */ |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame^] | 138 | long smh_flen(long fd) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 139 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 140 | long ret; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 141 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 142 | debug("%s: fd %ld\n", __func__, fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 143 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 144 | ret = smh_trap(SYSFLEN, &fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 145 | if (ret == -1) |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 146 | printf("%s: ERROR ret %ld, fd %ld\n", __func__, ret, fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 151 | static int smh_load_file(const char * const name, ulong load_addr, |
| 152 | ulong *end_addr) |
| 153 | { |
| 154 | long fd; |
| 155 | long len; |
| 156 | long ret; |
| 157 | |
| 158 | fd = smh_open(name, "rb"); |
| 159 | if (fd == -1) |
| 160 | return -1; |
| 161 | |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame^] | 162 | len = smh_flen(fd); |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 163 | if (len < 0) { |
| 164 | smh_close(fd); |
| 165 | return -1; |
| 166 | } |
| 167 | |
| 168 | ret = smh_read(fd, (void *)load_addr, len); |
| 169 | smh_close(fd); |
| 170 | |
| 171 | if (ret == 0) { |
| 172 | *end_addr = load_addr + len - 1; |
| 173 | printf("loaded file %s from %08lX to %08lX, %08lX bytes\n", |
| 174 | name, |
| 175 | load_addr, |
| 176 | *end_addr, |
| 177 | len); |
| 178 | } else { |
| 179 | printf("read failed\n"); |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 186 | static int do_smhload(struct cmd_tbl *cmdtp, int flag, int argc, |
| 187 | char *const argv[]) |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 188 | { |
| 189 | if (argc == 3 || argc == 4) { |
| 190 | ulong load_addr; |
| 191 | ulong end_addr = 0; |
Ryan Harkin | b564d52 | 2017-03-02 17:45:16 +0000 | [diff] [blame] | 192 | int ret; |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 193 | char end_str[64]; |
| 194 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 195 | load_addr = hextoul(argv[2], NULL); |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 196 | if (!load_addr) |
| 197 | return -1; |
| 198 | |
| 199 | ret = smh_load_file(argv[1], load_addr, &end_addr); |
| 200 | if (ret < 0) |
Ryan Harkin | b564d52 | 2017-03-02 17:45:16 +0000 | [diff] [blame] | 201 | return CMD_RET_FAILURE; |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 202 | |
| 203 | /* Optionally save returned end to the environment */ |
| 204 | if (argc == 4) { |
| 205 | sprintf(end_str, "0x%08lx", end_addr); |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 206 | env_set(argv[3], end_str); |
Linus Walleij | 6cf1671 | 2015-03-23 11:06:11 +0100 | [diff] [blame] | 207 | } |
| 208 | } else { |
| 209 | return CMD_RET_USAGE; |
| 210 | } |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | U_BOOT_CMD(smhload, 4, 0, do_smhload, "load a file using semihosting", |
| 215 | "<file> 0x<address> [end var]\n" |
| 216 | " - load a semihosted file to the address specified\n" |
| 217 | " if the optional [end var] is specified, the end\n" |
| 218 | " address of the file will be stored in this environment\n" |
| 219 | " variable.\n"); |