blob: 2658026cf49ee5619304d36a566f6c60030c8709 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Darwin Rambod32d4112014-06-09 11:12:59 -07002/*
3 * Copyright 2014 Broadcom Corporation
Darwin Rambod32d4112014-06-09 11:12:59 -07004 */
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 Walleij6cf16712015-03-23 11:06:11 +010015#include <command.h>
Darwin Rambod32d4112014-06-09 11:12:59 -070016
17#define SYSOPEN 0x01
18#define SYSCLOSE 0x02
19#define SYSREAD 0x06
20#define SYSFLEN 0x0C
21
22#define MODE_READ 0x0
23#define MODE_READBIN 0x1
24
25/*
26 * Call the handler
27 */
Linus Walleij1c2e27e2015-03-23 11:06:10 +010028static noinline long smh_trap(unsigned int sysnum, void *addr)
Darwin Rambod32d4112014-06-09 11:12:59 -070029{
Linus Walleij6fda2652014-12-15 11:05:56 +010030 register long result asm("r0");
Darwin Rambod32d4112014-06-09 11:12:59 -070031#if defined(CONFIG_ARM64)
32 asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr));
Vadzim Dambrouski98d38942015-10-19 19:40:14 +030033#elif defined(CONFIG_CPU_V7M)
34 asm volatile ("bkpt #0xAB" : "=r" (result) : "0"(sysnum), "r"(addr));
Darwin Rambod32d4112014-06-09 11:12:59 -070035#else
36 /* Note - untested placeholder */
37 asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr));
38#endif
39 return result;
40}
41
42/*
Linus Walleij7cc8ca82014-12-15 11:06:05 +010043 * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
44 * descriptor or -1 on error.
Darwin Rambod32d4112014-06-09 11:12:59 -070045 */
Linus Walleij7cc8ca82014-12-15 11:06:05 +010046static long smh_open(const char *fname, char *modestr)
Darwin Rambod32d4112014-06-09 11:12:59 -070047{
Linus Walleij6fda2652014-12-15 11:05:56 +010048 long fd;
Linus Walleij7cc8ca82014-12-15 11:06:05 +010049 unsigned long mode;
50 struct smh_open_s {
51 const char *fname;
52 unsigned long mode;
53 size_t len;
54 } open;
Darwin Rambod32d4112014-06-09 11:12:59 -070055
Linus Walleij7cc8ca82014-12-15 11:06:05 +010056 debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
Darwin Rambod32d4112014-06-09 11:12:59 -070057
Linus Walleij7cc8ca82014-12-15 11:06:05 +010058 /* Check the file mode */
59 if (!(strcmp(modestr, "r"))) {
60 mode = MODE_READ;
61 } else if (!(strcmp(modestr, "rb"))) {
62 mode = MODE_READBIN;
63 } else {
64 printf("%s: ERROR mode \'%s\' not supported\n", __func__,
65 modestr);
Linus Walleij6fda2652014-12-15 11:05:56 +010066 return -1;
Darwin Rambod32d4112014-06-09 11:12:59 -070067 }
68
Linus Walleij7cc8ca82014-12-15 11:06:05 +010069 open.fname = fname;
70 open.len = strlen(fname);
71 open.mode = mode;
Darwin Rambod32d4112014-06-09 11:12:59 -070072
Linus Walleij7cc8ca82014-12-15 11:06:05 +010073 /* Open the file on the host */
74 fd = smh_trap(SYSOPEN, &open);
75 if (fd == -1)
76 printf("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd,
77 fname);
Darwin Rambod32d4112014-06-09 11:12:59 -070078
Linus Walleij7cc8ca82014-12-15 11:06:05 +010079 return fd;
Darwin Rambod32d4112014-06-09 11:12:59 -070080}
81
82/*
83 * Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure
84 */
Linus Walleij6fda2652014-12-15 11:05:56 +010085static long smh_read(long fd, void *memp, size_t len)
Darwin Rambod32d4112014-06-09 11:12:59 -070086{
Linus Walleij6fda2652014-12-15 11:05:56 +010087 long ret;
Darwin Rambod32d4112014-06-09 11:12:59 -070088 struct smh_read_s {
Linus Walleij6fda2652014-12-15 11:05:56 +010089 long fd;
Darwin Rambod32d4112014-06-09 11:12:59 -070090 void *memp;
Linus Walleij6fda2652014-12-15 11:05:56 +010091 size_t len;
Darwin Rambod32d4112014-06-09 11:12:59 -070092 } read;
93
Vadzim Dambrouski27382152015-10-19 19:40:15 +030094 debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len);
Darwin Rambod32d4112014-06-09 11:12:59 -070095
96 read.fd = fd;
97 read.memp = memp;
98 read.len = len;
99
100 ret = smh_trap(SYSREAD, &read);
Linus Walleij6fda2652014-12-15 11:05:56 +0100101 if (ret < 0) {
Darwin Rambod32d4112014-06-09 11:12:59 -0700102 /*
103 * The ARM handler allows for returning partial lengths,
104 * but in practice this never happens so rather than create
105 * hard to maintain partial read loops and such, just fail
106 * with an error message.
107 */
Vadzim Dambrouski27382152015-10-19 19:40:15 +0300108 printf("%s: ERROR ret %ld, fd %ld, len %zu memp %p\n",
Darwin Rambod32d4112014-06-09 11:12:59 -0700109 __func__, ret, fd, len, memp);
Linus Walleij6fda2652014-12-15 11:05:56 +0100110 return -1;
Darwin Rambod32d4112014-06-09 11:12:59 -0700111 }
Linus Walleij6fda2652014-12-15 11:05:56 +0100112
113 return 0;
Darwin Rambod32d4112014-06-09 11:12:59 -0700114}
115
116/*
Darwin Rambod32d4112014-06-09 11:12:59 -0700117 * Close the file using the file descriptor
118 */
Linus Walleij6fda2652014-12-15 11:05:56 +0100119static long smh_close(long fd)
Darwin Rambod32d4112014-06-09 11:12:59 -0700120{
Linus Walleij6fda2652014-12-15 11:05:56 +0100121 long ret;
Darwin Rambod32d4112014-06-09 11:12:59 -0700122
Linus Walleij6fda2652014-12-15 11:05:56 +0100123 debug("%s: fd %ld\n", __func__, fd);
Darwin Rambod32d4112014-06-09 11:12:59 -0700124
Linus Walleij6fda2652014-12-15 11:05:56 +0100125 ret = smh_trap(SYSCLOSE, &fd);
Darwin Rambod32d4112014-06-09 11:12:59 -0700126 if (ret == -1)
Linus Walleij6fda2652014-12-15 11:05:56 +0100127 printf("%s: ERROR fd %ld\n", __func__, fd);
Darwin Rambod32d4112014-06-09 11:12:59 -0700128
129 return ret;
130}
131
132/*
133 * Get the file length from the file descriptor
134 */
Linus Walleij6fda2652014-12-15 11:05:56 +0100135static long smh_len_fd(long fd)
Darwin Rambod32d4112014-06-09 11:12:59 -0700136{
Linus Walleij6fda2652014-12-15 11:05:56 +0100137 long ret;
Darwin Rambod32d4112014-06-09 11:12:59 -0700138
Linus Walleij6fda2652014-12-15 11:05:56 +0100139 debug("%s: fd %ld\n", __func__, fd);
Darwin Rambod32d4112014-06-09 11:12:59 -0700140
Linus Walleij6fda2652014-12-15 11:05:56 +0100141 ret = smh_trap(SYSFLEN, &fd);
Darwin Rambod32d4112014-06-09 11:12:59 -0700142 if (ret == -1)
Linus Walleij6fda2652014-12-15 11:05:56 +0100143 printf("%s: ERROR ret %ld, fd %ld\n", __func__, ret, fd);
Darwin Rambod32d4112014-06-09 11:12:59 -0700144
145 return ret;
146}
147
Linus Walleij6cf16712015-03-23 11:06:11 +0100148static int smh_load_file(const char * const name, ulong load_addr,
149 ulong *end_addr)
150{
151 long fd;
152 long len;
153 long ret;
154
155 fd = smh_open(name, "rb");
156 if (fd == -1)
157 return -1;
158
159 len = smh_len_fd(fd);
160 if (len < 0) {
161 smh_close(fd);
162 return -1;
163 }
164
165 ret = smh_read(fd, (void *)load_addr, len);
166 smh_close(fd);
167
168 if (ret == 0) {
169 *end_addr = load_addr + len - 1;
170 printf("loaded file %s from %08lX to %08lX, %08lX bytes\n",
171 name,
172 load_addr,
173 *end_addr,
174 len);
175 } else {
176 printf("read failed\n");
177 return 0;
178 }
179
180 return 0;
181}
182
183static int do_smhload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
184{
185 if (argc == 3 || argc == 4) {
186 ulong load_addr;
187 ulong end_addr = 0;
Ryan Harkinb564d522017-03-02 17:45:16 +0000188 int ret;
Linus Walleij6cf16712015-03-23 11:06:11 +0100189 char end_str[64];
190
191 load_addr = simple_strtoul(argv[2], NULL, 16);
192 if (!load_addr)
193 return -1;
194
195 ret = smh_load_file(argv[1], load_addr, &end_addr);
196 if (ret < 0)
Ryan Harkinb564d522017-03-02 17:45:16 +0000197 return CMD_RET_FAILURE;
Linus Walleij6cf16712015-03-23 11:06:11 +0100198
199 /* Optionally save returned end to the environment */
200 if (argc == 4) {
201 sprintf(end_str, "0x%08lx", end_addr);
Simon Glass6a38e412017-08-03 12:22:09 -0600202 env_set(argv[3], end_str);
Linus Walleij6cf16712015-03-23 11:06:11 +0100203 }
204 } else {
205 return CMD_RET_USAGE;
206 }
207 return 0;
208}
209
210U_BOOT_CMD(smhload, 4, 0, do_smhload, "load a file using semihosting",
211 "<file> 0x<address> [end var]\n"
212 " - load a semihosted file to the address specified\n"
213 " if the optional [end var] is specified, the end\n"
214 " address of the file will be stored in this environment\n"
215 " variable.\n");