blob: edacb11877319342b1bf8cd0ac9c2d8cd4c97226 [file] [log] [blame]
Darwin Rambod32d4112014-06-09 11:12:59 -07001/*
2 * Copyright 2014 Broadcom Corporation
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7/*
8 * Minimal semihosting implementation for reading files into memory. If more
9 * features like writing files or console output are required they can be
10 * added later. This code has been tested on arm64/aarch64 fastmodel only.
11 * An untested placeholder exists for armv7 architectures, but since they
12 * are commonly available in silicon now, fastmodel usage makes less sense
13 * for them.
14 */
15#include <common.h>
Linus Walleij6cf16712015-03-23 11:06:11 +010016#include <command.h>
Darwin Rambod32d4112014-06-09 11:12:59 -070017#include <asm/semihosting.h>
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 Walleij1c2e27e2015-03-23 11:06:10 +010030static noinline long smh_trap(unsigned int sysnum, void *addr)
Darwin Rambod32d4112014-06-09 11:12:59 -070031{
Linus Walleij6fda2652014-12-15 11:05:56 +010032 register long result asm("r0");
Darwin Rambod32d4112014-06-09 11:12:59 -070033#if defined(CONFIG_ARM64)
34 asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr));
35#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
Linus Walleij6fda2652014-12-15 11:05:56 +010094 debug("%s: fd %ld, memp %p, len %lu\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 */
Linus Walleij6fda2652014-12-15 11:05:56 +0100108 printf("%s: ERROR ret %ld, fd %ld, len %lu 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
148/*
Linus Walleij7cc8ca82014-12-15 11:06:05 +0100149 * Open, load a file into memory, and close it. Check that the available space
150 * is sufficient to store the entire file. Return the bytes actually read from
151 * the file as seen by the read function. The verbose flag enables some extra
152 * printing of successful read status.
153 */
154int smh_load(const char *fname, void *memp, int avail, int verbose)
155{
156 long ret;
157 long fd;
158 size_t len;
159
160 ret = -1;
161
162 debug("%s: fname \'%s\', avail %u, memp %p\n", __func__, fname,
163 avail, memp);
164
165 /* Open the file */
166 fd = smh_open(fname, "rb");
167 if (fd == -1)
168 return -1;
169
170 /* Get the file length */
171 ret = smh_len_fd(fd);
172 if (ret == -1) {
173 smh_close(fd);
174 return -1;
175 }
176
177 /* Check that the file will fit in the supplied buffer */
178 if (ret > avail) {
179 printf("%s: ERROR ret %ld, avail %u\n", __func__, ret,
180 avail);
181 smh_close(fd);
182 return -1;
183 }
184
185 len = ret;
186
187 /* Read the file into the buffer */
188 ret = smh_read(fd, memp, len);
189 if (ret == 0) {
190 /* Print successful load information if requested */
191 if (verbose) {
192 printf("\n%s\n", fname);
193 printf(" 0x%8p dest\n", memp);
194 printf(" 0x%08lx size\n", len);
195 printf(" 0x%08x avail\n", avail);
196 }
197 }
198
199 /* Close the file */
200 smh_close(fd);
201
202 return ret;
203}
204
205/*
Darwin Rambod32d4112014-06-09 11:12:59 -0700206 * Get the file length from the filename
207 */
Linus Walleij6fda2652014-12-15 11:05:56 +0100208long smh_len(const char *fname)
Darwin Rambod32d4112014-06-09 11:12:59 -0700209{
Linus Walleij6fda2652014-12-15 11:05:56 +0100210 long ret;
211 long fd;
212 long len;
Darwin Rambod32d4112014-06-09 11:12:59 -0700213
214 debug("%s: file \'%s\'\n", __func__, fname);
215
216 /* Open the file */
217 fd = smh_open(fname, "rb");
Linus Walleij6fda2652014-12-15 11:05:56 +0100218 if (fd < 0)
Darwin Rambod32d4112014-06-09 11:12:59 -0700219 return fd;
220
221 /* Get the file length */
222 len = smh_len_fd(fd);
Linus Walleij6fda2652014-12-15 11:05:56 +0100223 if (len < 0) {
224 smh_close(fd);
225 return len;
226 }
Darwin Rambod32d4112014-06-09 11:12:59 -0700227
228 /* Close the file */
229 ret = smh_close(fd);
Linus Walleij6fda2652014-12-15 11:05:56 +0100230 if (ret < 0)
Darwin Rambod32d4112014-06-09 11:12:59 -0700231 return ret;
232
Linus Walleij6fda2652014-12-15 11:05:56 +0100233 debug("%s: returning len %ld\n", __func__, len);
Darwin Rambod32d4112014-06-09 11:12:59 -0700234
235 /* Return the file length (or -1 error indication) */
236 return len;
237}
Linus Walleij6cf16712015-03-23 11:06:11 +0100238
239static int smh_load_file(const char * const name, ulong load_addr,
240 ulong *end_addr)
241{
242 long fd;
243 long len;
244 long ret;
245
246 fd = smh_open(name, "rb");
247 if (fd == -1)
248 return -1;
249
250 len = smh_len_fd(fd);
251 if (len < 0) {
252 smh_close(fd);
253 return -1;
254 }
255
256 ret = smh_read(fd, (void *)load_addr, len);
257 smh_close(fd);
258
259 if (ret == 0) {
260 *end_addr = load_addr + len - 1;
261 printf("loaded file %s from %08lX to %08lX, %08lX bytes\n",
262 name,
263 load_addr,
264 *end_addr,
265 len);
266 } else {
267 printf("read failed\n");
268 return 0;
269 }
270
271 return 0;
272}
273
274static int do_smhload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
275{
276 if (argc == 3 || argc == 4) {
277 ulong load_addr;
278 ulong end_addr = 0;
279 ulong ret;
280 char end_str[64];
281
282 load_addr = simple_strtoul(argv[2], NULL, 16);
283 if (!load_addr)
284 return -1;
285
286 ret = smh_load_file(argv[1], load_addr, &end_addr);
287 if (ret < 0)
288 return 1;
289
290 /* Optionally save returned end to the environment */
291 if (argc == 4) {
292 sprintf(end_str, "0x%08lx", end_addr);
293 setenv(argv[3], end_str);
294 }
295 } else {
296 return CMD_RET_USAGE;
297 }
298 return 0;
299}
300
301U_BOOT_CMD(smhload, 4, 0, do_smhload, "load a file using semihosting",
302 "<file> 0x<address> [end var]\n"
303 " - load a semihosted file to the address specified\n"
304 " if the optional [end var] is specified, the end\n"
305 " address of the file will be stored in this environment\n"
306 " variable.\n");