1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Broadcom Corporation
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
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));
35 #elif defined(CONFIG_CPU_V7M)
36 asm volatile ("bkpt #0xAB" : "=r" (result) : "0"(sysnum), "r"(addr));
38 /* Note - untested placeholder */
39 asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr));
45 * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
46 * descriptor or -1 on error.
48 static long smh_open(const char *fname, char *modestr)
58 debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
60 /* Check the file mode */
61 if (!(strcmp(modestr, "r"))) {
63 } else if (!(strcmp(modestr, "rb"))) {
66 printf("%s: ERROR mode \'%s\' not supported\n", __func__,
72 open.len = strlen(fname);
75 /* Open the file on the host */
76 fd = smh_trap(SYSOPEN, &open);
78 printf("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd,
85 * Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure
87 static long smh_read(long fd, void *memp, size_t len)
96 debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len);
102 ret = smh_trap(SYSREAD, &read);
105 * The ARM handler allows for returning partial lengths,
106 * but in practice this never happens so rather than create
107 * hard to maintain partial read loops and such, just fail
108 * with an error message.
110 printf("%s: ERROR ret %ld, fd %ld, len %zu memp %p\n",
111 __func__, ret, fd, len, memp);
119 * Close the file using the file descriptor
121 static long smh_close(long fd)
125 debug("%s: fd %ld\n", __func__, fd);
127 ret = smh_trap(SYSCLOSE, &fd);
129 printf("%s: ERROR fd %ld\n", __func__, fd);
135 * Get the file length from the file descriptor
137 static long smh_len_fd(long fd)
141 debug("%s: fd %ld\n", __func__, fd);
143 ret = smh_trap(SYSFLEN, &fd);
145 printf("%s: ERROR ret %ld, fd %ld\n", __func__, ret, fd);
150 static int smh_load_file(const char * const name, ulong load_addr,
157 fd = smh_open(name, "rb");
161 len = smh_len_fd(fd);
167 ret = smh_read(fd, (void *)load_addr, len);
171 *end_addr = load_addr + len - 1;
172 printf("loaded file %s from %08lX to %08lX, %08lX bytes\n",
178 printf("read failed\n");
185 static int do_smhload(struct cmd_tbl *cmdtp, int flag, int argc,
188 if (argc == 3 || argc == 4) {
194 load_addr = simple_strtoul(argv[2], NULL, 16);
198 ret = smh_load_file(argv[1], load_addr, &end_addr);
200 return CMD_RET_FAILURE;
202 /* Optionally save returned end to the environment */
204 sprintf(end_str, "0x%08lx", end_addr);
205 env_set(argv[3], end_str);
208 return CMD_RET_USAGE;
213 U_BOOT_CMD(smhload, 4, 0, do_smhload, "load a file using semihosting",
214 "<file> 0x<address> [end var]\n"
215 " - load a semihosted file to the address specified\n"
216 " if the optional [end var] is specified, the end\n"
217 " address of the file will be stored in this environment\n"