1 // Copyright 2020 Espressif Systems (Shanghai) Co. Ltd.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "esp_vfs.h"
16 #include "freertos/FreeRTOS.h"
17 #include "freertos/task.h"
18 #include "soc/cpu.h"
19 #include <stdarg.h>
20 #include <stdbool.h>
21 #include <string.h>
22 #include <sys/errno.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 
26 #ifndef CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS
27 #define CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS 1
28 #endif
29 
30 #ifndef CONFIG_VFS_SEMIHOSTFS_HOST_PATH_MAX_LEN
31 #define CONFIG_VFS_SEMIHOSTFS_HOST_PATH_MAX_LEN 128
32 #endif
33 
34 #ifdef VFS_SUPPRESS_SEMIHOSTING_LOG
35 #define LOG_LOCAL_LEVEL ESP_LOG_NONE
36 #endif //VFS_SUPPRESS_SEMIHOSTING_LOG
37 
38 #include "esp_log.h"
39 const static char *TAG = "esp_semihost";
40 
41 /* current semihosting implementation version */
42 #define DRIVER_SEMIHOSTING_VERSION 0x1
43 
44 /* syscalls */
45 #define SYSCALL_INSTR           "break 1,1\n"
46 #define SYS_OPEN                0x01
47 #define SYS_CLOSE               0x02
48 #define SYS_WRITE               0x05
49 #define SYS_READ                0x06
50 #define SYS_SEEK                0x0A
51 
52 #define SYS_DRVINFO             0xE0
53 
54 /* additional open flags */
55 #define O_BINARY 0  // there is no binary flag in our toolchain, as well as in Linux OS
56                     // but we are leaving it to have an identical to OOCD flags table
57 /** ESP-specific file open flag. Indicates that path passed to open() is absolute host path. */
58 #define ESP_O_SEMIHOST_ABSPATH  0x80000000
59 
60 /* The table is identical to semihosting_common's one from OpenOCD */
61 static const int open_modeflags[12] = {
62     O_RDONLY,
63     O_RDONLY | O_BINARY,
64     O_RDWR,
65     O_RDWR | O_BINARY,
66     O_WRONLY | O_CREAT | O_TRUNC,
67     O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
68     O_RDWR | O_CREAT | O_TRUNC,
69     O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
70     O_WRONLY | O_CREAT | O_APPEND,
71     O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
72     O_RDWR | O_CREAT | O_APPEND,
73     O_RDWR | O_CREAT | O_APPEND | O_BINARY
74 };
75 
76 /**
77  * @brief semihosting driver information
78  *
79  */
80 typedef struct {
81     int ver;
82 } drv_info_t;
83 
84 /**
85  * @brief Get the number of appropriate file open mode set from open_modeflags and add some esp flags to them
86  *
87  * @param flags value, every bit of which reflects state of some open-file flag
88  * @return int
89 *                             -1 - there is no appropriate entry of open_modeflags[]
90  *          esp_flags | (0...11) - esp-specific flags and number of flag set for oocd from @ref open_modeflags[]
91  */
get_o_mode(int flags)92 static inline int get_o_mode(int flags) {
93     uint32_t esp_flags = flags & 0xfff00000; // that bits are not used, so let's use it for our espressif's purposes
94     uint32_t semi_comm_flags = flags & 0x000fffff;
95     if (semi_comm_flags & O_EXCL) { // bypassing lacking of this at table above
96         semi_comm_flags &= ~(O_EXCL);
97         semi_comm_flags |= O_CREAT;
98     }
99 
100     for (int i = 0; i < sizeof(open_modeflags) / sizeof(open_modeflags[0]); i++) {
101         if (semi_comm_flags == open_modeflags[i])
102             return (esp_flags | i);
103     }
104     return -1; // there is no corresponding mode in the table
105 }
106 
107 typedef struct {
108     char base_path[ESP_VFS_PATH_MAX + 1];                    /* base path in VFS where host semihosting dir is mounted */
109     char host_path[CONFIG_VFS_SEMIHOSTFS_HOST_PATH_MAX_LEN + 1]; /* host path to use as base dir for open files */
110 } vfs_semihost_ctx_t;
111 
112 static vfs_semihost_ctx_t s_semhost_ctx[CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS];
113 
114 
generic_syscall(int sys_nr,int arg1,int arg2,int arg3,int arg4,int * ret_errno)115 static inline int generic_syscall(int sys_nr, int arg1, int arg2, int arg3, int arg4, int* ret_errno)
116 {
117 #if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2 // TODO ESP32-C3 reenable semihost in C3 IDF-2287
118     int host_ret, host_errno;
119 
120     if (!esp_cpu_in_ocd_debug_mode()) {
121         *ret_errno = EIO;
122         return -1;
123     }
124     __asm__ volatile (
125         "mov a2, %[sys_nr]\n" \
126         "mov a3, %[arg1]\n" \
127         "mov a4, %[arg2]\n" \
128         "mov a5, %[arg3]\n" \
129         "mov a6, %[arg4]\n" \
130         SYSCALL_INSTR \
131         "mov %[host_ret], a2\n" \
132         "mov %[host_errno], a3\n" \
133         :[host_ret]"=r"(host_ret),[host_errno]"=r"(host_errno)
134         :[sys_nr]"r"(sys_nr),[arg1]"r"(arg1),[arg2]"r"(arg2),[arg3]"r"(arg3),[arg4]"r"(arg4)
135         :"a2","a3","a4","a5","a6");
136     *ret_errno = host_errno;
137     return host_ret;
138 #else
139     return 0;
140 #endif
141 
142 }
143 
ctx_is_unused(const vfs_semihost_ctx_t * ctx)144 inline bool ctx_is_unused(const vfs_semihost_ctx_t* ctx)
145 {
146     return ctx->base_path[0] == 0;
147 }
148 
ctx_uses_abspath(const vfs_semihost_ctx_t * ctx)149 inline bool ctx_uses_abspath(const vfs_semihost_ctx_t* ctx)
150 {
151     return ctx->host_path[0];
152 }
153 
154 /**
155  * @brief Send a custom syscall SYS_DRVINFO to the host for determining
156  *
157  * @param ctx context
158  * @return error
159  */
vfs_semihost_drvinfo(vfs_semihost_ctx_t * ctx)160 static esp_err_t vfs_semihost_drvinfo(vfs_semihost_ctx_t *ctx) {
161     drv_info_t drv_info = {
162         .ver = DRIVER_SEMIHOSTING_VERSION
163     };
164 
165     int host_err = 0;
166     size_t ret = -1;
167 
168     ESP_LOGV(TAG, "%s: s_ver: %x, flags:  %x, par3:  %x, par4:  %x", __func__, (int)&drv_info, sizeof(drv_info), 0, 0);
169 
170     ret = generic_syscall(SYS_DRVINFO, (int)&drv_info, sizeof(drv_info), 0, 0, &host_err);
171 
172     /* Recognizing the version */
173     ESP_LOGV(TAG, "Trying to determine semihosting's version...");
174     if (ret == -1) { /* there is no such syscall -  old semihosting */
175         ret = ESP_ERR_INVALID_VERSION;
176     } else {
177         ESP_LOGI(TAG, "OpenOCD Semihosting v.%d [Read from an OpenOCD response]", drv_info.ver);
178         ESP_LOGV(TAG, "[Version was read from an OpenOCD response]");
179     }
180     return ret;
181 }
182 
vfs_semihost_open(void * ctx,const char * path,int flags,int mode)183 static int vfs_semihost_open(void* ctx, const char* path, int flags, int mode) {
184     int ret_fd = -1, o_mode = 0, host_err = 0;
185     char *host_path;
186     vfs_semihost_ctx_t *semi_ctx = ctx;
187     ESP_LOGV(TAG, "%s: %p '%s 0x%x 0x%x'", __func__, semi_ctx, path, flags, mode);
188 
189     /* flags processing */
190     if (ctx_uses_abspath(semi_ctx)) {
191         flags |= ESP_O_SEMIHOST_ABSPATH;
192     }
193     o_mode = get_o_mode(flags);
194 
195     if (o_mode == -1) { /* if wrong flags - error */
196         errno = EINVAL;
197     } else { /* if ok - host_path processing */
198         if (ctx_uses_abspath(semi_ctx)) {
199             host_path = malloc(strlen(semi_ctx->host_path) + strlen(path) + 1);
200             if (host_path == NULL) { /* if no valid pointer - error and return */
201                 errno = ENOMEM;
202                 return -1;
203             }
204             strcpy(host_path, semi_ctx->host_path);
205             strcat(host_path, path);
206         } else {
207             host_path = (char *)path;
208         }
209         /* everything is ready: syscall and cleanup */
210         ret_fd = generic_syscall(SYS_OPEN, (int)host_path, o_mode, strlen(host_path), mode, &host_err);
211         if (ret_fd == -1) {
212             errno = host_err;
213         }
214         if (ctx_uses_abspath(semi_ctx)) {
215             free(host_path);
216         }
217     }
218     return ret_fd;
219 }
220 
vfs_semihost_write(void * ctx,int fd,const void * data,size_t size)221 static ssize_t vfs_semihost_write(void* ctx, int fd, const void * data, size_t size)
222 {
223     int host_err = 0;
224     size_t ret = -1;
225 
226     ESP_LOGV(TAG, "%s: %d %u bytes", __func__, fd, size);
227     ret = generic_syscall(SYS_WRITE, fd, (int)data, size, 0, &host_err);
228     if (ret == -1) {
229         errno = host_err;
230     }
231     return size - (ssize_t)ret; /* Write syscall returns the number of bytes NOT written */
232 }
233 
vfs_semihost_read(void * ctx,int fd,void * data,size_t size)234 static ssize_t vfs_semihost_read(void* ctx, int fd, void* data, size_t size)
235 {
236     int host_err = 0;
237     size_t ret = -1;
238 
239     ESP_LOGV(TAG, "%s: %d %u bytes", __func__, fd, size);
240     ret = generic_syscall(SYS_READ, fd, (int)data, size, 0, &host_err);
241     if (ret == -1) {
242         errno = host_err;
243         return ret;
244     }
245     return size - (ssize_t)ret; /* Read syscall returns the number of bytes NOT read */
246 
247 }
248 
249 
vfs_semihost_close(void * ctx,int fd)250 static int vfs_semihost_close(void* ctx, int fd)
251 {
252     int ret = -1, host_err = 0;
253 
254     ESP_LOGV(TAG, "%s: %d", __func__, fd);
255     ret = generic_syscall(SYS_CLOSE, fd, 0, 0, 0, &host_err);
256     if (ret == -1) {
257         errno = host_err;
258     }
259     return ret;
260 }
261 
vfs_semihost_lseek(void * ctx,int fd,off_t offset,int mode)262 static off_t vfs_semihost_lseek(void* ctx, int fd, off_t offset, int mode)
263 {
264     int ret = -1, host_err = 0;
265 
266     ESP_LOGV(TAG, "%s: %d %ld %d", __func__, fd, offset, mode);
267     ret = generic_syscall(SYS_SEEK, fd, offset, mode, 0, &host_err);
268     if (ret == -1) {
269         errno = host_err;
270     }
271     return (off_t)ret;
272 }
273 
esp_vfs_semihost_register(const char * base_path,const char * host_path)274 esp_err_t esp_vfs_semihost_register(const char* base_path, const char* host_path)
275 {
276     const esp_vfs_t vfs = {
277         .flags = ESP_VFS_FLAG_CONTEXT_PTR,
278         .write_p = &vfs_semihost_write,
279         .open_p = &vfs_semihost_open,
280         .close_p = &vfs_semihost_close,
281         .read_p = &vfs_semihost_read,
282         .lseek_p = &vfs_semihost_lseek,
283     };
284     ESP_LOGD(TAG, "Register semihosting driver '%s' -> '%s'", base_path, host_path ? host_path : "null");
285     if (!esp_cpu_in_ocd_debug_mode()) {
286         ESP_LOGE(TAG, "OpenOCD is not connected!");
287         return ESP_ERR_NOT_SUPPORTED;
288     }
289     int i = 0;
290     for (i = 0; i < CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS; i++) {
291         if (ctx_is_unused(&s_semhost_ctx[i])) {
292             break;
293         }
294         if (strcmp(base_path, s_semhost_ctx[i].base_path) == 0) {
295             return ESP_ERR_INVALID_STATE;
296         }
297     }
298     if (i == CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS) {
299         return ESP_ERR_NO_MEM;
300     }
301     strlcpy(s_semhost_ctx[i].base_path, base_path, sizeof(s_semhost_ctx[i].base_path) - 1);
302     if (host_path) {
303         strlcpy(s_semhost_ctx[i].host_path, host_path, sizeof(s_semhost_ctx[i].host_path) - 1);
304     }
305     ESP_LOGD(TAG, "Register semihosting driver %d %p", i, &s_semhost_ctx[i]);
306     esp_err_t err = vfs_semihost_drvinfo(&s_semhost_ctx[i]); // define semihosting version
307     if (err != ESP_OK) {
308         ESP_LOGE(TAG, "Incompatible OpenOCD version detected. Please follow the getting started guides to install the required version.");
309     }
310     err = esp_vfs_register(base_path, &vfs, &s_semhost_ctx[i]);
311     if (err != ESP_OK) {
312         ESP_LOGE(TAG, "Can't register the semihosting! Error: %s", esp_err_to_name(err));
313         return err;
314     }
315     return err;
316 }
317 
esp_vfs_semihost_unregister(const char * base_path)318 esp_err_t esp_vfs_semihost_unregister(const char* base_path)
319 {
320     ESP_LOGD(TAG, "Unregister semihosting driver @ '%s'", base_path);
321     int i = 0;
322     for (i = 0; i < CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS; i++) {
323         if (s_semhost_ctx[i].base_path[0] != 0 && strcmp(base_path, s_semhost_ctx[i].base_path) == 0) {
324             break;
325         }
326     }
327     if (i == CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS) {
328         return ESP_ERR_INVALID_ARG;
329     }
330     esp_err_t ret = esp_vfs_unregister(s_semhost_ctx[i].base_path);
331     if (ret != ESP_OK) {
332         return ret;
333     }
334     s_semhost_ctx[i].base_path[0] = 0;
335     s_semhost_ctx[i].host_path[0] = 0;
336     ESP_LOGD(TAG, "Unregistered semihosting driver @ '%s'", base_path);
337     return ESP_OK;
338 }
339