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
26 static long smh_read(long fd, void *memp, size_t len);
27 static long smh_open(const char *fname, char *modestr);
28 static long smh_close(long fd);
29 static long smh_len_fd(long fd);
34 static long smh_trap(unsigned int sysnum, void *addr)
36 register long result asm("r0");
37 #if defined(CONFIG_ARM64)
38 asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr));
40 /* Note - untested placeholder */
41 asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr));
47 * Open, load a file into memory, and close it. Check that the available space
48 * is sufficient to store the entire file. Return the bytes actually read from
49 * the file as seen by the read function. The verbose flag enables some extra
50 * printing of successful read status.
52 int smh_load(const char *fname, void *memp, int avail, int verbose)
60 debug("%s: fname \'%s\', avail %u, memp %p\n", __func__, fname,
64 fd = smh_open(fname, "rb");
68 /* Get the file length */
75 /* Check that the file will fit in the supplied buffer */
77 printf("%s: ERROR ret %ld, avail %u\n", __func__, ret,
85 /* Read the file into the buffer */
86 ret = smh_read(fd, memp, len);
88 /* Print successful load information if requested */
90 printf("\n%s\n", fname);
91 printf(" 0x%8p dest\n", memp);
92 printf(" 0x%08lx size\n", len);
93 printf(" 0x%08x avail\n", avail);
104 * Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure
106 static long smh_read(long fd, void *memp, size_t len)
115 debug("%s: fd %ld, memp %p, len %lu\n", __func__, fd, memp, len);
121 ret = smh_trap(SYSREAD, &read);
124 * The ARM handler allows for returning partial lengths,
125 * but in practice this never happens so rather than create
126 * hard to maintain partial read loops and such, just fail
127 * with an error message.
129 printf("%s: ERROR ret %ld, fd %ld, len %lu memp %p\n",
130 __func__, ret, fd, len, memp);
138 * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
139 * descriptor or -1 on error.
141 static long smh_open(const char *fname, char *modestr)
151 debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
153 /* Check the file mode */
154 if (!(strcmp(modestr, "r"))) {
156 } else if (!(strcmp(modestr, "rb"))) {
159 printf("%s: ERROR mode \'%s\' not supported\n", __func__,
165 open.len = strlen(fname);
168 /* Open the file on the host */
169 fd = smh_trap(SYSOPEN, &open);
171 printf("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd,
178 * Close the file using the file descriptor
180 static long smh_close(long fd)
184 debug("%s: fd %ld\n", __func__, fd);
186 ret = smh_trap(SYSCLOSE, &fd);
188 printf("%s: ERROR fd %ld\n", __func__, fd);
194 * Get the file length from the file descriptor
196 static long smh_len_fd(long fd)
200 debug("%s: fd %ld\n", __func__, fd);
202 ret = smh_trap(SYSFLEN, &fd);
204 printf("%s: ERROR ret %ld, fd %ld\n", __func__, ret, fd);
210 * Get the file length from the filename
212 long smh_len(const char *fname)
218 debug("%s: file \'%s\'\n", __func__, fname);
221 fd = smh_open(fname, "rb");
225 /* Get the file length */
226 len = smh_len_fd(fd);
237 debug("%s: returning len %ld\n", __func__, len);
239 /* Return the file length (or -1 error indication) */