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