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
24 #define MODE_READBIN 0x1
29 static noinline long smh_trap(unsigned int sysnum, void *addr)
31 register long result asm("r0");
32 #if defined(CONFIG_ARM64)
33 asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr));
34 #elif defined(CONFIG_CPU_V7M)
35 asm volatile ("bkpt #0xAB" : "=r" (result) : "0"(sysnum), "r"(addr));
37 /* Note - untested placeholder */
38 asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr));
44 * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
45 * descriptor or -1 on error.
47 static long smh_open(const char *fname, char *modestr)
57 debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
59 /* Check the file mode */
60 if (!(strcmp(modestr, "r"))) {
62 } else if (!(strcmp(modestr, "rb"))) {
65 printf("%s: ERROR mode \'%s\' not supported\n", __func__,
71 open.len = strlen(fname);
74 /* Open the file on the host */
75 fd = smh_trap(SYSOPEN, &open);
77 printf("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd,
84 * Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure
86 static long smh_read(long fd, void *memp, size_t len)
95 debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len);
101 ret = smh_trap(SYSREAD, &read);
104 * The ARM handler allows for returning partial lengths,
105 * but in practice this never happens so rather than create
106 * hard to maintain partial read loops and such, just fail
107 * with an error message.
109 printf("%s: ERROR ret %ld, fd %ld, len %zu memp %p\n",
110 __func__, ret, fd, len, memp);
118 * Close the file using the file descriptor
120 static long smh_close(long fd)
124 debug("%s: fd %ld\n", __func__, fd);
126 ret = smh_trap(SYSCLOSE, &fd);
128 printf("%s: ERROR fd %ld\n", __func__, fd);
134 * Get the file length from the file descriptor
136 static long smh_len_fd(long fd)
140 debug("%s: fd %ld\n", __func__, fd);
142 ret = smh_trap(SYSFLEN, &fd);
144 printf("%s: ERROR ret %ld, fd %ld\n", __func__, ret, fd);
149 static int smh_load_file(const char * const name, ulong load_addr,
156 fd = smh_open(name, "rb");
160 len = smh_len_fd(fd);
166 ret = smh_read(fd, (void *)load_addr, len);
170 *end_addr = load_addr + len - 1;
171 printf("loaded file %s from %08lX to %08lX, %08lX bytes\n",
177 printf("read failed\n");
184 static int do_smhload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
186 if (argc == 3 || argc == 4) {
192 load_addr = simple_strtoul(argv[2], NULL, 16);
196 ret = smh_load_file(argv[1], load_addr, &end_addr);
200 /* Optionally save returned end to the environment */
202 sprintf(end_str, "0x%08lx", end_addr);
203 setenv(argv[3], end_str);
206 return CMD_RET_USAGE;
211 U_BOOT_CMD(smhload, 4, 0, do_smhload, "load a file using semihosting",
212 "<file> 0x<address> [end var]\n"
213 " - load a semihosted file to the address specified\n"
214 " if the optional [end var] is specified, the end\n"
215 " address of the file will be stored in this environment\n"