arm: semihosting: staticize internal functions
[oweals/u-boot.git] / arch / arm / lib / semihosting.c
1 /*
2  * Copyright 2014 Broadcom Corporation
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 /*
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
13  * for them.
14  */
15 #include <common.h>
16 #include <asm/semihosting.h>
17
18 #define SYSOPEN         0x01
19 #define SYSCLOSE        0x02
20 #define SYSREAD         0x06
21 #define SYSFLEN         0x0C
22
23 #define MODE_READ       0x0
24 #define MODE_READBIN    0x1
25
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);
30
31 /*
32  * Call the handler
33  */
34 static int smh_trap(unsigned int sysnum, void *addr)
35 {
36         register int result asm("r0");
37 #if defined(CONFIG_ARM64)
38         asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr));
39 #else
40         /* Note - untested placeholder */
41         asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr));
42 #endif
43         return result;
44 }
45
46 /*
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.
51  */
52 int smh_load(const char *fname, void *memp, int avail, int verbose)
53 {
54         int ret, fd, len;
55
56         ret = -1;
57
58         debug("%s: fname \'%s\', avail %u, memp %p\n", __func__, fname,
59               avail, memp);
60
61         /* Open the file */
62         fd = smh_open(fname, "rb");
63         if (fd == -1)
64                 return ret;
65
66         /* Get the file length */
67         ret = smh_len_fd(fd);
68         if (ret == -1) {
69                 smh_close(fd);
70                 return ret;
71         }
72
73         /* Check that the file will fit in the supplied buffer */
74         if (ret > avail) {
75                 printf("%s: ERROR ret %d, avail %u\n", __func__, ret,
76                        avail);
77                 smh_close(fd);
78                 return ret;
79         }
80
81         len = ret;
82
83         /* Read the file into the buffer */
84         ret = smh_read(fd, memp, len);
85         if (ret == 0) {
86                 /* Print successful load information if requested */
87                 if (verbose) {
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);
92                 }
93         }
94
95         /* Close the file */
96         smh_close(fd);
97
98         return ret;
99 }
100
101 /*
102  * Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure
103  */
104 static int smh_read(int fd, void *memp, int len)
105 {
106         int ret;
107         struct smh_read_s {
108                 int fd;
109                 void *memp;
110                 int len;
111         } read;
112
113         debug("%s: fd %d, memp %p, len %d\n", __func__, fd, memp, len);
114
115         read.fd = fd;
116         read.memp = memp;
117         read.len = len;
118
119         ret = smh_trap(SYSREAD, &read);
120         if (ret == 0) {
121                 return 0;
122         } else {
123                 /*
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.
128                  */
129                 printf("%s: ERROR ret %d, fd %d, len %u memp %p\n",
130                        __func__, ret, fd, len, memp);
131         }
132         return ret;
133 }
134
135 /*
136  * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
137  * descriptor or -1 on error.
138  */
139 static int smh_open(const char *fname, char *modestr)
140 {
141         int ret, fd, mode;
142         struct smh_open_s {
143                 const char *fname;
144                 unsigned int mode;
145                 unsigned int len;
146         } open;
147
148         debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
149
150         ret = -1;
151
152         /* Check the file mode */
153         if (!(strcmp(modestr, "r"))) {
154                 mode = MODE_READ;
155         } else if (!(strcmp(modestr, "rb"))) {
156                 mode = MODE_READBIN;
157         } else {
158                 printf("%s: ERROR mode \'%s\' not supported\n", __func__,
159                        modestr);
160                 return ret;
161         }
162
163         open.fname = fname;
164         open.len = strlen(fname);
165         open.mode = mode;
166
167         /* Open the file on the host */
168         fd = smh_trap(SYSOPEN, &open);
169         if (fd == -1)
170                 printf("%s: ERROR fd %d for file \'%s\'\n", __func__, fd,
171                        fname);
172
173         return fd;
174 }
175
176 /*
177  * Close the file using the file descriptor
178  */
179 static int smh_close(int fd)
180 {
181         int ret;
182         long fdlong;
183
184         debug("%s: fd %d\n", __func__, fd);
185
186         fdlong = (long)fd;
187         ret = smh_trap(SYSCLOSE, &fdlong);
188         if (ret == -1)
189                 printf("%s: ERROR fd %d\n", __func__, fd);
190
191         return ret;
192 }
193
194 /*
195  * Get the file length from the file descriptor
196  */
197 static int smh_len_fd(int fd)
198 {
199         int ret;
200         long fdlong;
201
202         debug("%s: fd %d\n", __func__, fd);
203
204         fdlong = (long)fd;
205         ret = smh_trap(SYSFLEN, &fdlong);
206         if (ret == -1)
207                 printf("%s: ERROR ret %d\n", __func__, ret);
208
209         return ret;
210 }
211
212 /*
213  * Get the file length from the filename
214  */
215 int smh_len(const char *fname)
216 {
217         int ret, fd, len;
218
219         debug("%s: file \'%s\'\n", __func__, fname);
220
221         /* Open the file */
222         fd = smh_open(fname, "rb");
223         if (fd == -1)
224                 return fd;
225
226         /* Get the file length */
227         len = smh_len_fd(fd);
228
229         /* Close the file */
230         ret = smh_close(fd);
231         if (ret == -1)
232                 return ret;
233
234         debug("%s: returning len %d\n", __func__, len);
235
236         /* Return the file length (or -1 error indication) */
237         return len;
238 }