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 int smh_read(int fd, void *memp, int len);
27 static int smh_open(const char *fname, char *modestr);
28 static int smh_close(int fd);
29 static int smh_len_fd(int fd);
34 static int smh_trap(unsigned int sysnum, void *addr)
36 register int 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)
58 debug("%s: fname \'%s\', avail %u, memp %p\n", __func__, fname,
62 fd = smh_open(fname, "rb");
66 /* Get the file length */
73 /* Check that the file will fit in the supplied buffer */
75 printf("%s: ERROR ret %d, avail %u\n", __func__, ret,
83 /* Read the file into the buffer */
84 ret = smh_read(fd, memp, len);
86 /* Print successful load information if requested */
88 printf("\n%s\n", fname);
89 printf(" 0x%8p dest\n", memp);
90 printf(" 0x%08x size\n", len);
91 printf(" 0x%08x avail\n", avail);
102 * Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure
104 static int smh_read(int fd, void *memp, int len)
113 debug("%s: fd %d, memp %p, len %d\n", __func__, fd, memp, len);
119 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 %d, fd %d, len %u memp %p\n",
130 __func__, ret, fd, len, memp);
136 * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
137 * descriptor or -1 on error.
139 static int smh_open(const char *fname, char *modestr)
148 debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
152 /* Check the file mode */
153 if (!(strcmp(modestr, "r"))) {
155 } else if (!(strcmp(modestr, "rb"))) {
158 printf("%s: ERROR mode \'%s\' not supported\n", __func__,
164 open.len = strlen(fname);
167 /* Open the file on the host */
168 fd = smh_trap(SYSOPEN, &open);
170 printf("%s: ERROR fd %d for file \'%s\'\n", __func__, fd,
177 * Close the file using the file descriptor
179 static int smh_close(int fd)
184 debug("%s: fd %d\n", __func__, fd);
187 ret = smh_trap(SYSCLOSE, &fdlong);
189 printf("%s: ERROR fd %d\n", __func__, fd);
195 * Get the file length from the file descriptor
197 static int smh_len_fd(int fd)
202 debug("%s: fd %d\n", __func__, fd);
205 ret = smh_trap(SYSFLEN, &fdlong);
207 printf("%s: ERROR ret %d\n", __func__, ret);
213 * Get the file length from the filename
215 int smh_len(const char *fname)
219 debug("%s: file \'%s\'\n", __func__, fname);
222 fd = smh_open(fname, "rb");
226 /* Get the file length */
227 len = smh_len_fd(fd);
234 debug("%s: returning len %d\n", __func__, len);
236 /* Return the file length (or -1 error indication) */