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
16 #include <asm/semihosting.h>
24 #define MODE_READBIN 0x1
29 static 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));
35 /* Note - untested placeholder */
36 asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr));
42 * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
43 * descriptor or -1 on error.
45 static long smh_open(const char *fname, char *modestr)
55 debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
57 /* Check the file mode */
58 if (!(strcmp(modestr, "r"))) {
60 } else if (!(strcmp(modestr, "rb"))) {
63 printf("%s: ERROR mode \'%s\' not supported\n", __func__,
69 open.len = strlen(fname);
72 /* Open the file on the host */
73 fd = smh_trap(SYSOPEN, &open);
75 printf("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd,
82 * Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure
84 static long smh_read(long fd, void *memp, size_t len)
93 debug("%s: fd %ld, memp %p, len %lu\n", __func__, fd, memp, len);
99 ret = smh_trap(SYSREAD, &read);
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.
107 printf("%s: ERROR ret %ld, fd %ld, len %lu memp %p\n",
108 __func__, ret, fd, len, memp);
116 * Close the file using the file descriptor
118 static long smh_close(long fd)
122 debug("%s: fd %ld\n", __func__, fd);
124 ret = smh_trap(SYSCLOSE, &fd);
126 printf("%s: ERROR fd %ld\n", __func__, fd);
132 * Get the file length from the file descriptor
134 static long smh_len_fd(long fd)
138 debug("%s: fd %ld\n", __func__, fd);
140 ret = smh_trap(SYSFLEN, &fd);
142 printf("%s: ERROR ret %ld, fd %ld\n", __func__, ret, fd);
148 * Open, load a file into memory, and close it. Check that the available space
149 * is sufficient to store the entire file. Return the bytes actually read from
150 * the file as seen by the read function. The verbose flag enables some extra
151 * printing of successful read status.
153 int smh_load(const char *fname, void *memp, int avail, int verbose)
161 debug("%s: fname \'%s\', avail %u, memp %p\n", __func__, fname,
165 fd = smh_open(fname, "rb");
169 /* Get the file length */
170 ret = smh_len_fd(fd);
176 /* Check that the file will fit in the supplied buffer */
178 printf("%s: ERROR ret %ld, avail %u\n", __func__, ret,
186 /* Read the file into the buffer */
187 ret = smh_read(fd, memp, len);
189 /* Print successful load information if requested */
191 printf("\n%s\n", fname);
192 printf(" 0x%8p dest\n", memp);
193 printf(" 0x%08lx size\n", len);
194 printf(" 0x%08x avail\n", avail);
205 * Get the file length from the filename
207 long smh_len(const char *fname)
213 debug("%s: file \'%s\'\n", __func__, fname);
216 fd = smh_open(fname, "rb");
220 /* Get the file length */
221 len = smh_len_fd(fd);
232 debug("%s: returning len %ld\n", __func__, len);
234 /* Return the file length (or -1 error indication) */