1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
99
100
101
102
103
104
105
106
107
108
109
110
111
112
121
122
123
124
125
126
127
128
131
132
133
134
135
136
137
138
139
143
146
147
148
149
150
151
152
153
154
158
161
162
163
164
165
166
167
168
169
172
173
174
175
176
177
178
179
180
183
184
185
186
187
188
189
192
193
194
195
196
197
198
199
200
201
202
203
206
207
208
209
210
211
212
213
214
217
218
219
220
221
222
223
224
225
226
229
230
231
232
233
234
235
236
237
240
241
242
243
244
245
246
247
248
251
252
253
254
255
256
257
258
259
262
263
264
265
266
267
268
269
270
273
274
275
276
277
278
279
280
281
284
285
286
287
288
289
290
291
292
295
296
297
298
299
300
301
302
303
306
307
308
309
310
311
312
313
314
317
318
319
320
321
322
323
324
325
328
329
330
331
332
333
334
335
336
339
340
341
342
343
344
345
346
347
350
351
352
353
354
355
356
357
358
361
362
363
364
365
366
367
368
371
372
373
374
375
376
377
/* ... */
#pragma once
#include <string.h>
#include <sys/errno.h>
#ifdef __XTENSA__
#include "xtensa/semihosting.h"
#elif __riscv
#include "riscv/semihosting.h"
#else
#error Unsupported architecture
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* ... */
#define SEMIHOSTING_SYS_OPEN 0x01
#define SEMIHOSTING_SYS_CLOSE 0x02
#define SEMIHOSTING_SYS_WRITEC 0x03
#define SEMIHOSTING_SYS_WRITE0 0x04
#define SEMIHOSTING_SYS_WRITE 0x05
#define SEMIHOSTING_SYS_READ 0x06
#define SEMIHOSTING_SYS_READC 0x07
#define SEMIHOSTING_SYS_ISERROR 0x08
#define SEMIHOSTING_SYS_ISTTY 0x09
#define SEMIHOSTING_SYS_SEEK 0x0A
#define SEMIHOSTING_SYS_FLEN 0x0C
#define SEMIHOSTING_SYS_REMOVE 0x0E
#define SEMIHOSTING_SYS_RENAME 0x0F
#define SEMIHOSTING_SYS_CLOCK 0x10
#define SEMIHOSTING_SYS_TIME 0x11
#define SEMIHOSTING_SYS_SYSTEM 0x12
#define SEMIHOSTING_SYS_ERRNO 0x13
#define SEMIHOSTING_SYS_GET_CMDLINE 0x15
#define SEMIHOSTING_SYS_HEAPINFO 0x16
#define SEMIHOSTING_SYS_EXIT 0x18
#define SEMIHOSTING_SYS_EXIT_EXTENDED 0x20
/* ... */
#define ESP_SEMIHOSTING_SYS_DRV_INFO 0x100
#define ESP_SEMIHOSTING_SYS_SEEK 0x105
#define ESP_SEMIHOSTING_SYS_MKDIR 0x106
#define ESP_SEMIHOSTING_SYS_OPENDIR 0x107
#define ESP_SEMIHOSTING_SYS_READDIR 0x108
#define ESP_SEMIHOSTING_SYS_READDIR_R 0x109
#define ESP_SEMIHOSTING_SYS_SEEKDIR 0x10A
#define ESP_SEMIHOSTING_SYS_TELLDIR 0x10B
#define ESP_SEMIHOSTING_SYS_CLOSEDIR 0x10C
#define ESP_SEMIHOSTING_SYS_RMDIR 0x10D
#define ESP_SEMIHOSTING_SYS_ACCESS 0x10E
#define ESP_SEMIHOSTING_SYS_TRUNCATE 0x10F
#define ESP_SEMIHOSTING_SYS_UTIME 0x110
#define ESP_SEMIHOSTING_SYS_FSTAT 0x111
#define ESP_SEMIHOSTING_SYS_STAT 0x112
#define ESP_SEMIHOSTING_SYS_FSYNC 0x113
#define ESP_SEMIHOSTING_SYS_LINK 0x114
#define ESP_SEMIHOSTING_SYS_UNLINK 0x115
/* ... */
#define SEMIHOSTING_DRV_VERSION 240 defines
/* ... */
static inline long semihosting_call(long id, long *data, int *out_errno)
{
long ret = semihosting_call_noerrno(id, data);
if (ret < 0) {
const int semihosting_sys_errno = SEMIHOSTING_SYS_ERRNO;
*out_errno = (int)semihosting_call_noerrno(semihosting_sys_errno, NULL);
}{...}
return ret;
}{ ... }
static inline int semihosting_open(const char *path, int open_mode, int mode)
{
int host_errno = 0;
long args[] = {(long) path, open_mode, strlen(path), 0};
(void) mode;
int result = (int)semihosting_call(SEMIHOSTING_SYS_OPEN, args, &host_errno);
if (result < 0) {
errno = host_errno;
}{...}
return result;
}{ ... }
static inline ssize_t semihosting_write(int fd, const void *data, size_t size)
{
int host_errno = 0;
long args[] = {fd, (long) data, size, 0};
ssize_t ret = (ssize_t)semihosting_call(SEMIHOSTING_SYS_WRITE, args, &host_errno);
if (ret < 0) {
errno = host_errno;
return ret;
}{...}
/* ... */
return size - (ssize_t)ret;
}{ ... }
static inline ssize_t semihosting_read(int fd, void *data, size_t size)
{
int host_errno = 0;
long args[] = {fd, (long) data, size, 0};
ssize_t ret = (ssize_t)semihosting_call(SEMIHOSTING_SYS_READ, args, &host_errno);
if (ret < 0) {
errno = host_errno;
return ret;
}{...}
/* ... */
return size - (ssize_t)ret;
}{ ... }
static inline int semihosting_close(int fd)
{
int host_errno = 0;
long args[] = {fd, 0, 0, 0};
int ret = (int)semihosting_call(SEMIHOSTING_SYS_CLOSE, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline off_t semihosting_seek(int fd, off_t offset, int mode)
{
int host_errno = 0;
long args[] = {fd, offset, mode, 0};
off_t ret = (off_t)semihosting_call(ESP_SEMIHOSTING_SYS_SEEK, args, &host_errno);
if (ret == -1) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline int semihosting_ver_info(void)
{
int host_errno = 0;
struct {
int version;
}{ ... } ver_info = { SEMIHOSTING_DRV_VERSION };
long args[] = {(long) &ver_info, sizeof(ver_info), 0, 0};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_DRV_INFO, args, &host_errno);
(void) host_errno;
return ret;
}{ ... }
static inline int semihosting_fstat(int fd, struct stat *restrict statbuf)
{
int host_errno = 0;
long args[] = {fd, (int)statbuf, 0, 0};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_FSTAT, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline int semihosting_fsync(int fd)
{
int host_errno = 0;
long args[] = {fd, 0, 0, 0};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_FSYNC, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
#ifdef CONFIG_VFS_SUPPORT_DIR
static inline int semihosting_mkdir(const char *host_path, mode_t mode)
{
int host_errno = 0;
long args[] = {(long)host_path, mode, strlen(host_path), 0};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_MKDIR, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline int semihosting_rmdir(const char *host_path)
{
int host_errno = 0;
long args[] = {(long)host_path, strlen(host_path), 0, 0};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_RMDIR, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline int semihosting_access(const char *host_path, int mode)
{
int host_errno = 0;
long args[] = {(long)host_path, strlen(host_path), mode, 0};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_ACCESS, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline int semihosting_truncate(const char *host_path, off_t truncate_length)
{
int host_errno = 0;
long args[] = {(long)host_path, strlen(host_path), truncate_length, 0};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_TRUNCATE, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline int semihosting_utime(const char *host_path, const struct utimbuf *times)
{
int host_errno = 0;
long args[] = {(long)host_path, strlen(host_path), times->actime, times->modtime};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_UTIME, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline int semihosting_stat(const char *host_path, struct stat *restrict statbuf)
{
int host_errno = 0;
long args[] = {(long)host_path, strlen(host_path), (long)statbuf, 0};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_STAT, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline int semihosting_rename(const char *old_path, const char *new_path)
{
int host_errno = 0;
long args[] = {(long)old_path, strlen(old_path), (long)new_path, strlen(new_path)};
int ret = (int)semihosting_call(SEMIHOSTING_SYS_RENAME, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline int semihosting_link(const char *path1, const char *path2)
{
int host_errno = 0;
long args[] = {(long)path1, strlen(path1), (long)path2, strlen(path2)};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_LINK, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline int semihosting_unlink(const char *path)
{
int host_errno = 0;
long args[] = {(long)path, strlen(path), 0, 0};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_UNLINK, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline int semihosting_opendir(const char *path, long offset)
{
int host_errno = 0;
long args[] = {(long)path, strlen(path), offset, 0};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_OPENDIR, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline int semihosting_readdir(long struct_dirent_ptr, long offset)
{
int host_errno = 0;
long args[] = {struct_dirent_ptr, offset, 0, 0};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_READDIR, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline int semihosting_closedir(long id)
{
int host_errno = 0;
long args[] = {id, 0, 0, 0};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_CLOSEDIR, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline long semihosting_telldir(long id)
{
int host_errno = 0;
long args[] = {id, 0, 0, 0};
long ret = semihosting_call(ESP_SEMIHOSTING_SYS_TELLDIR, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
static inline int semihosting_seekdir(long id, long offset)
{
int host_errno = 0;
long args[] = {id, offset, 0, 0};
int ret = (int)semihosting_call(ESP_SEMIHOSTING_SYS_SEEKDIR, args, &host_errno);
if (ret < 0) {
errno = host_errno;
}{...}
return ret;
}{ ... }
/* ... */#endif
#ifdef __cplusplus
}{...}
#endif