2 * Copyright 2014 Broadcom Corporation
4 * SPDX-License-Identifier: GPL-2.0+
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
17 #include <asm/semihosting.h>
25 #define MODE_READBIN 0x1
30 static noinline long smh_trap(unsigned int sysnum, void *addr)
32 register long result asm("r0");
33 #if defined(CONFIG_ARM64)
34 asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr));
36 /* Note - untested placeholder */
37 asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr));
43 * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
44 * descriptor or -1 on error.
46 static long smh_open(const char *fname, char *modestr)
56 debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
58 /* Check the file mode */
59 if (!(strcmp(modestr, "r"))) {
61 } else if (!(strcmp(modestr, "rb"))) {
64 printf("%s: ERROR mode \'%s\' not supported\n", __func__,
70 open.len = strlen(fname);
73 /* Open the file on the host */
74 fd = smh_trap(SYSOPEN, &open);
76 printf("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd,
83 * Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure
85 static long smh_read(long fd, void *memp, size_t len)
94 debug("%s: fd %ld, memp %p, len %lu\n", __func__, fd, memp, len);
100 ret = smh_trap(SYSREAD, &read);
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.
108 printf("%s: ERROR ret %ld, fd %ld, len %lu memp %p\n",
109 __func__, ret, fd, len, memp);
117 * Close the file using the file descriptor
119 static long smh_close(long fd)
123 debug("%s: fd %ld\n", __func__, fd);
125 ret = smh_trap(SYSCLOSE, &fd);
127 printf("%s: ERROR fd %ld\n", __func__, fd);
133 * Get the file length from the file descriptor
135 static long smh_len_fd(long fd)
139 debug("%s: fd %ld\n", __func__, fd);
141 ret = smh_trap(SYSFLEN, &fd);
143 printf("%s: ERROR ret %ld, fd %ld\n", __func__, ret, fd);
149 * 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.
154 int smh_load(const char *fname, void *memp, int avail, int verbose)
162 debug("%s: fname \'%s\', avail %u, memp %p\n", __func__, fname,
166 fd = smh_open(fname, "rb");
170 /* Get the file length */
171 ret = smh_len_fd(fd);
177 /* Check that the file will fit in the supplied buffer */
179 printf("%s: ERROR ret %ld, avail %u\n", __func__, ret,
187 /* Read the file into the buffer */
188 ret = smh_read(fd, memp, len);
190 /* Print successful load information if requested */
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);
206 * Get the file length from the filename
208 long smh_len(const char *fname)
214 debug("%s: file \'%s\'\n", __func__, fname);
217 fd = smh_open(fname, "rb");
221 /* Get the file length */
222 len = smh_len_fd(fd);
233 debug("%s: returning len %ld\n", __func__, len);
235 /* Return the file length (or -1 error indication) */
239 static int smh_load_file(const char * const name, ulong load_addr,
246 fd = smh_open(name, "rb");
250 len = smh_len_fd(fd);
256 ret = smh_read(fd, (void *)load_addr, len);
260 *end_addr = load_addr + len - 1;
261 printf("loaded file %s from %08lX to %08lX, %08lX bytes\n",
267 printf("read failed\n");
274 static int do_smhload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
276 if (argc == 3 || argc == 4) {
282 load_addr = simple_strtoul(argv[2], NULL, 16);
286 ret = smh_load_file(argv[1], load_addr, &end_addr);
290 /* Optionally save returned end to the environment */
292 sprintf(end_str, "0x%08lx", end_addr);
293 setenv(argv[3], end_str);
296 return CMD_RET_USAGE;
301 U_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"