29#pragma clang diagnostic ignored "-Wempty-translation-unit"
34#if defined(__ARM_EABI__)
38#include <cmsis-plus/os-app-config.h>
41#if defined(OS_USE_SEMIHOSTING_SYSCALLS)
51#include "cmsis_device.h"
115#if !defined(OS_INTEGER_SEMIHOSTING_MAX_OPEN_FILES)
116#define OS_INTEGER_SEMIHOSTING_MAX_OPEN_FILES (20)
127__semihosting_findslot (
int fd)
136 if (openfiles[fd].handle == -1)
142 return &openfiles[fd];
148__semihosting_newslot (
void)
153 if (openfiles[i].handle == -1)
168__semihosting_get_errno (
void)
175__semihosting_error (
int result)
177 errno = __semihosting_get_errno ();
183__semihosting_checkerror (
int result)
187 return __semihosting_error (-1);
195__semihosting_lseek (
int fd,
int ptr,
int dir)
200 pfd = __semihosting_findslot (fd);
208 if ((dir != SEEK_CUR) && (dir != SEEK_SET) && (dir != SEEK_END))
217 ptr = pfd->pos + ptr;
222 if ((pfd->pos > 0) && (ptr > 0))
236 block[0] = pfd->handle;
246 block[0] = pfd->handle;
263__semihosting_stat (
int fd,
struct stat* st)
266 pfd = __semihosting_findslot (fd);
275 st->st_mode |= S_IFCHR;
276 st->st_blksize = 1024;
279 res = __semihosting_checkerror (
307 int fd = __semihosting_newslot ();
315 if ((oflag & O_CREAT) && (oflag & O_EXCL))
330 if (oflag & O_BINARY)
343 if ((oflag & O_CREAT) || (oflag & O_TRUNC) || (oflag & O_WRONLY))
348 if (oflag & O_APPEND)
356 block[0] = (uint32_t) path;
357 block[2] = std::strlen (path);
358 block[1] = (uint32_t) aflags;
365 openfiles[fd].handle = fh;
366 openfiles[fd].pos = 0;
371 return __semihosting_error (fh);
379 pfd = __semihosting_findslot (fildes);
387 if ((fildes == 1 || fildes == 2)
388 && (openfiles[1].handle == openfiles[2].handle))
395 block[0] = pfd->handle;
420 pfd = __semihosting_findslot (fildes);
428 block[0] = pfd->handle;
429 block[1] = (int) buf;
440 pfd->pos += nbyte - res;
451 pfd = __semihosting_findslot (fildes);
460 block[0] = pfd->handle;
461 block[1] = (int) buf;
473 pfd->pos += nbyte - res;
477 if ((nbyte - res) == 0)
479 return __semihosting_error (0);
482 return (nbyte - res);
488 return __semihosting_lseek (fildes, offset, whence);
500 pfd = __semihosting_findslot (fildes);
515 errno = __semihosting_get_errno ();
522 memset (buf, 0,
sizeof(*buf));
523 return __semihosting_stat (fildes, buf);
533 memset (buf, 0,
sizeof(*buf));
540 buf->st_mode |= S_IFREG | S_IREAD;
541 int res = __semihosting_stat (fd, buf);
551 block[0] = (uint32_t) existing;
552 block[1] = std::strlen (existing);
553 block[2] = (uint32_t) _new;
554 block[3] = std::strlen (_new);
564 block[0] = (uint32_t) path;
565 block[1] = strlen (path);
571 return __semihosting_error (res);
582 if (command ==
nullptr)
588 block[0] = (uint32_t) command;
589 block[1] = strlen (command);
591 if ((e >= 0) && (e < 256))
598 for (exit_code = e; (e != 0) && (WEXITSTATUS (e) != exit_code); e <<= 1)
609 struct timezone* tzp = (
struct timezone*) ptimezone;
614 ptimeval->tv_usec = 0;
620 tzp->tz_minuteswest = 0;
643 buf->tms_utime = timeval;
656 strncpy (buf,
"/tmp", size);
660#pragma GCC diagnostic push
661#pragma GCC diagnostic ignored "-Wunused-parameter"
714 return ((ssize_t) -1);
731#pragma GCC diagnostic pop
736#pragma GCC diagnostic push
737#pragma GCC diagnostic ignored "-Wunused-parameter"
860 const void* option_value,
socklen_t option_len)
880#pragma GCC diagnostic pop
893#pragma GCC diagnostic push
894#pragma GCC diagnostic ignored "-Wunused-parameter"
899int __attribute__((weak))
906int __attribute__((weak))
914__posix_select (
int nfds, fd_set* readfds, fd_set* writefds, fd_set* errorfds,
915 struct timeval* timeout)
989 const struct termios *termios_p)
1027__posix_execve (
const char* path,
char*
const argv[],
char*
const envp[])
1037 return ((pid_t) -1);
1064 return ((pid_t) -1);
1092 return ((ssize_t) -1);
1099 return ((ssize_t) -1);
1106 return ((ssize_t) -1);
1109#pragma GCC diagnostic pop
1122__attribute__ ((noreturn,weak))
1142#define ARGS_BUF_ARRAY_SIZE 80
1143#define ARGV_BUF_ARRAY_SIZE 10
1145typedef struct command_line_block_s
1149} command_line_block_t;
1155 static char args_buf[ARGS_BUF_ARRAY_SIZE];
1159 static char* argv_buf[ARGV_BUF_ARRAY_SIZE];
1162 int is_in_argument = 0;
1164 command_line_block_t cmd_block;
1165 cmd_block.command_line = args_buf;
1166 cmd_block.size =
sizeof(args_buf) - 1;
1173 args_buf[ARGS_BUF_ARRAY_SIZE - 1] =
'\0';
1176 char* p = cmd_block.command_line;
1181 while ((ch = *p) !=
'\0')
1183 if (is_in_argument == 0)
1188 >= (
int) ((
sizeof(argv_buf) /
sizeof(argv_buf[0])) - 1))
1191 if (ch ==
'"' || ch ==
'\'')
1200 argv_buf[argc++] = p;
1204 else if (delim !=
'\0')
1213 else if (isblank (ch))
1227 argv_buf[0] = &args_buf[0];
1232 argv_buf[argc] = NULL;
1235 *p_argv = &argv_buf[0];
1243static int monitor_stdin;
1244static int monitor_stdout;
1245static int monitor_stderr;
1261 int volatile block[3];
1263 block[0] = (int)
":tt";
1268 block[0] = (int)
":tt";
1273 block[0] = (int)
":tt";
1279 if (monitor_stderr == -1)
1281 monitor_stderr = monitor_stdout;
1286 openfiles[i].handle = -1;
1289 openfiles[0].handle = monitor_stdin;
1290 openfiles[0].pos = 0;
1291 openfiles[1].handle = monitor_stdout;
1292 openfiles[1].pos = 0;
1293 openfiles[2].handle = monitor_stderr;
1294 openfiles[2].pos = 0;
1299#if (__STDC_HOSTED__ != 0)
1304#if defined(OS_INCLUDE_NEWLIB_POSIX_FUNCTIONS)
void initialise_monitor_handles(void)
#define OS_INTEGER_SEMIHOSTING_MAX_OPEN_FILES
Define the maximum number of semihosting open files.
void os_terminate(int code)
Terminate the application. There is no more life after this.
int stat(const char *path, struct stat *buf)
clock_t times(struct tms *buf)
int socket(int domain, int type, int protocol)
#define __posix_sockatmark
#define __posix_getsockopt
#define __posix_rewinddir
#define __posix_readdir_r
#define __posix_getsockname
#define __posix_gettimeofday
#define __posix_setsockopt
#define __posix_socketpair
#define __posix_ftruncate
#define __posix_getpeername
static void report_exception(int reason)
@ ADP_Stopped_RunTimeError
@ SEMIHOSTING_SYS_GET_CMDLINE
@ ADP_Stopped_ApplicationExit
static int call_host(int reason, void *arg)
int __posix_tcgetattr(int fildes, struct termios *termios_p)
int __posix_statvfs(const char *path, struct statvfs *buf)
int __posix_tcflush(int fildes, int queue_selector)
int __posix_fstatvfs(int fildes, struct statvfs *buf)
void os_startup_initialize_args(int *p_argc, char ***p_argv)
int __posix_tcdrain(int fildes)
int __posix_tcsetattr(int fildes, int optional_actions, const struct termios *termios_p)
int __posix_tcsendbreak(int fildes, int duration)