blob: c3e964eabc130cc978a25396b4f53b08102cfd59 [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
18#define SYSOPEN 0x01
19#define SYSCLOSE 0x02
20#define SYSREAD 0x06
21#define SYSFLEN 0x0C
22
23#define MODE_READ 0x0
24#define MODE_READBIN 0x1
25
26/*
27 * Call the handler
28 */
Linus Walleij1c2e27e2015-03-23 11:06:10 +010029static noinline long smh_trap(unsigned int sysnum, void *addr)
Darwin Rambod32d4112014-06-09 11:12:59 -070030{
Linus Walleij6fda2652014-12-15 11:05:56 +010031 register long result asm("r0");
Darwin Rambod32d4112014-06-09 11:12:59 -070032#if defined(CONFIG_ARM64)
33 asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr));
34#else
35 /* Note - untested placeholder */
36 asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr));
37#endif
38 return result;
39}
40
41/*
Linus Walleij7cc8ca82014-12-15 11:06:05 +010042 * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
43 * descriptor or -1 on error.
Darwin Rambod32d4112014-06-09 11:12:59 -070044 */
Linus Walleij7cc8ca82014-12-15 11:06:05 +010045static long smh_open(const char *fname, char *modestr)
Darwin Rambod32d4112014-06-09 11:12:59 -070046{
Linus Walleij6fda2652014-12-15 11:05:56 +010047 long fd;
Linus Walleij7cc8ca82014-12-15 11:06:05 +010048 unsigned long mode;
49 struct smh_open_s {
50 const char *fname;
51 unsigned long mode;
52 size_t len;
53 } open;
Darwin Rambod32d4112014-06-09 11:12:59 -070054
Linus Walleij7cc8ca82014-12-15 11:06:05 +010055 debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
Darwin Rambod32d4112014-06-09 11:12:59 -070056
Linus Walleij7cc8ca82014-12-15 11:06:05 +010057 /* Check the file mode */
58 if (!(strcmp(modestr, "r"))) {
59 mode = MODE_READ;
60 } else if (!(strcmp(modestr, "rb"))) {
61 mode = MODE_READBIN;
62 } else {
63 printf("%s: ERROR mode \'%s\' not supported\n", __func__,
64 modestr);
Linus Walleij6fda2652014-12-15 11:05:56 +010065 return -1;
Darwin Rambod32d4112014-06-09 11:12:59 -070066 }
67
Linus Walleij7cc8ca82014-12-15 11:06:05 +010068 open.fname = fname;
69 open.len = strlen(fname);
70 open.mode = mode;
Darwin Rambod32d4112014-06-09 11:12:59 -070071
Linus Walleij7cc8ca82014-12-15 11:06:05 +010072 /* Open the file on the host */
73 fd = smh_trap(SYSOPEN, &open);
74 if (fd == -1)
75 printf("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd,
76 fname);
Darwin Rambod32d4112014-06-09 11:12:59 -070077
Linus Walleij7cc8ca82014-12-15 11:06:05 +010078 return fd;
Darwin Rambod32d4112014-06-09 11:12:59 -070079}
80
81/*
82 * Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure
83 */
Linus Walleij6fda2652014-12-15 11:05:56 +010084static long smh_read(long fd, void *memp, size_t len)
Darwin Rambod32d4112014-06-09 11:12:59 -070085{
Linus Walleij6fda2652014-12-15 11:05:56 +010086 long ret;
Darwin Rambod32d4112014-06-09 11:12:59 -070087 struct smh_read_s {
Linus Walleij6fda2652014-12-15 11:05:56 +010088 long fd;
Darwin Rambod32d4112014-06-09 11:12:59 -070089 void *memp;
Linus Walleij6fda2652014-12-15 11:05:56 +010090 size_t len;
Darwin Rambod32d4112014-06-09 11:12:59 -070091 } read;
92
Linus Walleij6fda2652014-12-15 11:05:56 +010093 debug("%s: fd %ld, memp %p, len %lu\n", __func__, fd, memp, len);
Darwin Rambod32d4112014-06-09 11:12:59 -070094
95 read.fd = fd;
96 read.memp = memp;
97 read.len = len;
98
99 ret = smh_trap(SYSREAD, &read);
Linus Walleij6fda2652014-12-15 11:05:56 +0100100 if (ret < 0) {
Darwin Rambod32d4112014-06-09 11:12:59 -0700101 /*
102 * The ARM handler allows for returning partial lengths,
103 * but in practice this never happens so rather than create
104 * hard to maintain partial read loops and such, just fail
105 * with an error message.
106 */
Linus Walleij6fda2652014-12-15 11:05:56 +0100107 printf("%s: ERROR ret %ld, fd %ld, len %lu memp %p\n",
Darwin Rambod32d4112014-06-09 11:12:59 -0700108 __func__, ret, fd, len, memp);
Linus Walleij6fda2652014-12-15 11:05:56 +0100109 return -1;
Darwin Rambod32d4112014-06-09 11:12:59 -0700110 }
Linus Walleij6fda2652014-12-15 11:05:56 +0100111
112 return 0;
Darwin Rambod32d4112014-06-09 11:12:59 -0700113}
114
115/*
Darwin Rambod32d4112014-06-09 11:12:59 -0700116 * Close the file using the file descriptor
117 */
Linus Walleij6fda2652014-12-15 11:05:56 +0100118static long smh_close(long fd)
Darwin Rambod32d4112014-06-09 11:12:59 -0700119{
Linus Walleij6fda2652014-12-15 11:05:56 +0100120 long ret;
Darwin Rambod32d4112014-06-09 11:12:59 -0700121
Linus Walleij6fda2652014-12-15 11:05:56 +0100122 debug("%s: fd %ld\n", __func__, fd);
Darwin Rambod32d4112014-06-09 11:12:59 -0700123
Linus Walleij6fda2652014-12-15 11:05:56 +0100124 ret = smh_trap(SYSCLOSE, &fd);
Darwin Rambod32d4112014-06-09 11:12:59 -0700125 if (ret == -1)
Linus Walleij6fda2652014-12-15 11:05:56 +0100126 printf("%s: ERROR fd %ld\n", __func__, fd);
Darwin Rambod32d4112014-06-09 11:12:59 -0700127
128 return ret;
129}
130
131/*
132 * Get the file length from the file descriptor
133 */
Linus Walleij6fda2652014-12-15 11:05:56 +0100134static long smh_len_fd(long fd)
Darwin Rambod32d4112014-06-09 11:12:59 -0700135{
Linus Walleij6fda2652014-12-15 11:05:56 +0100136 long ret;
Darwin Rambod32d4112014-06-09 11:12:59 -0700137
Linus Walleij6fda2652014-12-15 11:05:56 +0100138 debug("%s: fd %ld\n", __func__, fd);
Darwin Rambod32d4112014-06-09 11:12:59 -0700139
Linus Walleij6fda2652014-12-15 11:05:56 +0100140 ret = smh_trap(SYSFLEN, &fd);
Darwin Rambod32d4112014-06-09 11:12:59 -0700141 if (ret == -1)
Linus Walleij6fda2652014-12-15 11:05:56 +0100142 printf("%s: ERROR ret %ld, fd %ld\n", __func__, ret, fd);
Darwin Rambod32d4112014-06-09 11:12:59 -0700143
144 return ret;
145}
146
Linus Walleij6cf16712015-03-23 11:06:11 +0100147static int smh_load_file(const char * const name, ulong load_addr,
148 ulong *end_addr)
149{
150 long fd;
151 long len;
152 long ret;
153
154 fd = smh_open(name, "rb");
155 if (fd == -1)
156 return -1;
157
158 len = smh_len_fd(fd);
159 if (len < 0) {
160 smh_close(fd);
161 return -1;
162 }
163
164 ret = smh_read(fd, (void *)load_addr, len);
165 smh_close(fd);
166
167 if (ret == 0) {
168 *end_addr = load_addr + len - 1;
169 printf("loaded file %s from %08lX to %08lX, %08lX bytes\n",
170 name,
171 load_addr,
172 *end_addr,
173 len);
174 } else {
175 printf("read failed\n");
176 return 0;
177 }
178
179 return 0;
180}
181
182static int do_smhload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
183{
184 if (argc == 3 || argc == 4) {
185 ulong load_addr;
186 ulong end_addr = 0;
187 ulong ret;
188 char end_str[64];
189
190 load_addr = simple_strtoul(argv[2], NULL, 16);
191 if (!load_addr)
192 return -1;
193
194 ret = smh_load_file(argv[1], load_addr, &end_addr);
195 if (ret < 0)
196 return 1;
197
198 /* Optionally save returned end to the environment */
199 if (argc == 4) {
200 sprintf(end_str, "0x%08lx", end_addr);
201 setenv(argv[3], end_str);
202 }
203 } else {
204 return CMD_RET_USAGE;
205 }
206 return 0;
207}
208
209U_BOOT_CMD(smhload, 4, 0, do_smhload, "load a file using semihosting",
210 "<file> 0x<address> [end var]\n"
211 " - load a semihosted file to the address specified\n"
212 " if the optional [end var] is specified, the end\n"
213 " address of the file will be stored in this environment\n"
214 " variable.\n");