14#pragma clang diagnostic ignored "-Wempty-translation-unit"
15#elif defined(__GNUC__)
16#pragma GCC diagnostic ignored "-Wold-style-cast"
17#pragma GCC diagnostic ignored "-Wredundant-tags"
18#pragma GCC diagnostic ignored "-Wcast-qual"
19#pragma GCC diagnostic ignored "-Wuseless-cast"
20#pragma GCC diagnostic ignored "-Wsign-conversion"
25#if defined(__ARM_EABI__)
29#if defined(OS_USE_OS_APP_CONFIG_H)
30#include <cmsis-plus/os-app-config.h>
35#if defined(OS_USE_SEMIHOSTING_SYSCALLS)
45#include "cmsis_device.h"
109#if !defined(OS_INTEGER_SEMIHOSTING_MAX_OPEN_FILES)
110#define OS_INTEGER_SEMIHOSTING_MAX_OPEN_FILES (20)
121__semihosting_findslot (
int fd)
130 if (openfiles[fd].handle == -1)
136 return &openfiles[fd];
142__semihosting_newslot (
void)
147 if (openfiles[i].handle == -1)
162__semihosting_get_errno (
void)
169__semihosting_error (
int result)
171 errno = __semihosting_get_errno ();
177__semihosting_checkerror (
int result)
181 return __semihosting_error (-1);
189__semihosting_lseek (
int fd,
int ptr,
int dir)
194 pfd = __semihosting_findslot (fd);
202 if ((dir != SEEK_CUR) && (dir != SEEK_SET) && (dir != SEEK_END))
211 ptr = pfd->pos + ptr;
216 if ((pfd->pos > 0) && (ptr > 0))
230 block[0] = pfd->handle;
240 block[0] = pfd->handle;
257__semihosting_stat (
int fd,
struct stat* st)
260 pfd = __semihosting_findslot (fd);
269 st->st_mode |= S_IFCHR;
270 st->st_blksize = 1024;
273 res = __semihosting_checkerror (
301 int fd = __semihosting_newslot ();
309 if ((oflag & O_CREAT) && (oflag & O_EXCL))
324 if (oflag & O_BINARY)
337 if ((oflag & O_CREAT) || (oflag & O_TRUNC) || (oflag & O_WRONLY))
342 if (oflag & O_APPEND)
350 block[0] = (uint32_t) path;
351 block[2] = std::strlen (path);
352 block[1] = (uint32_t) aflags;
359 openfiles[fd].handle = fh;
360 openfiles[fd].pos = 0;
365 return __semihosting_error (fh);
373 pfd = __semihosting_findslot (fildes);
381 if ((fildes == 1 || fildes == 2)
382 && (openfiles[1].handle == openfiles[2].handle))
389 block[0] = pfd->handle;
414 pfd = __semihosting_findslot (fildes);
422 block[0] = pfd->handle;
423 block[1] = (int) buf;
434 pfd->pos += nbyte - res;
445 pfd = __semihosting_findslot (fildes);
454 block[0] = pfd->handle;
455 block[1] = (int) buf;
467 pfd->pos += nbyte - res;
471 if ((nbyte - res) == 0)
473 return __semihosting_error (0);
476 return (nbyte - res);
482 return __semihosting_lseek (fildes, offset, whence);
494 pfd = __semihosting_findslot (fildes);
509 errno = __semihosting_get_errno ();
516 memset (buf, 0,
sizeof(*buf));
517 return __semihosting_stat (fildes, buf);
527 memset (buf, 0,
sizeof(*buf));
534 buf->st_mode |= S_IFREG | S_IREAD;
535 int res = __semihosting_stat (fd, buf);
545 block[0] = (uint32_t) existing;
546 block[1] = std::strlen (existing);
547 block[2] = (uint32_t) _new;
548 block[3] = std::strlen (_new);
558 block[0] = (uint32_t) path;
559 block[1] = strlen (path);
565 return __semihosting_error (res);
576 if (command ==
nullptr)
582 block[0] = (uint32_t) command;
583 block[1] = strlen (command);
585 if ((e >= 0) && (e < 256))
592 for (exit_code = e; (e != 0) && (WEXITSTATUS (e) != exit_code); e <<= 1)
603 struct timezone* tzp = (
struct timezone*) ptimezone;
608 ptimeval->tv_usec = 0;
614 tzp->tz_minuteswest = 0;
637 buf->tms_utime = timeval;
650 strncpy (buf,
"/tmp", size);
654#pragma GCC diagnostic push
655#if defined(__clang__)
656#elif defined(__GNUC__)
657#pragma GCC diagnostic ignored "-Wunused-parameter"
711 return ((ssize_t) -1);
728#pragma GCC diagnostic pop
733#pragma GCC diagnostic push
734#if defined(__clang__)
735#elif defined(__GNUC__)
736#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#if defined(__clang__)
895#elif defined(__GNUC__)
896#pragma GCC diagnostic ignored "-Wunused-parameter"
902int __attribute__((weak))
909int __attribute__((weak))
917__posix_select (
int nfds, fd_set* readfds, fd_set* writefds, fd_set* errorfds,
918 struct timeval* timeout)
992 const struct termios *termios_p)
1030__posix_execve (
const char* path,
char*
const argv[],
char*
const envp[])
1040 return ((pid_t) -1);
1067 return ((pid_t) -1);
1095 return ((ssize_t) -1);
1102 return ((ssize_t) -1);
1109 return ((ssize_t) -1);
1112#pragma GCC diagnostic pop
1125__attribute__ ((noreturn,weak))
1145#define ARGS_BUF_ARRAY_SIZE 80
1146#define ARGV_BUF_ARRAY_SIZE 10
1148typedef struct command_line_block_s
1152} command_line_block_t;
1158 static char args_buf[ARGS_BUF_ARRAY_SIZE];
1162 static char* argv_buf[ARGV_BUF_ARRAY_SIZE];
1165 int is_in_argument = 0;
1167 command_line_block_t cmd_block;
1168 cmd_block.command_line = args_buf;
1169 cmd_block.size =
sizeof(args_buf) - 1;
1176 args_buf[ARGS_BUF_ARRAY_SIZE - 1] =
'\0';
1179 char* p = cmd_block.command_line;
1184 while ((ch = *p) !=
'\0')
1186 if (is_in_argument == 0)
1191 >= (
int) ((
sizeof(argv_buf) /
sizeof(argv_buf[0])) - 1))
1194 if (ch ==
'"' || ch ==
'\'')
1203 argv_buf[argc++] = p;
1207 else if (delim !=
'\0')
1216 else if (isblank (ch))
1230 argv_buf[0] = &args_buf[0];
1235 argv_buf[argc] = NULL;
1238 *p_argv = &argv_buf[0];
1246static int monitor_stdin;
1247static int monitor_stdout;
1248static int monitor_stderr;
1264 int volatile block[3];
1266 block[0] = (int)
":tt";
1271 block[0] = (int)
":tt";
1276 block[0] = (int)
":tt";
1282 if (monitor_stderr == -1)
1284 monitor_stderr = monitor_stdout;
1289 openfiles[i].handle = -1;
1292 openfiles[0].handle = monitor_stdin;
1293 openfiles[0].pos = 0;
1294 openfiles[1].handle = monitor_stdout;
1295 openfiles[1].pos = 0;
1296 openfiles[2].handle = monitor_stderr;
1297 openfiles[2].pos = 0;
1302#if (__STDC_HOSTED__ != 0)
1307#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.
void os_startup_initialize_args(int *p_argc, char ***p_argv)
Initialise arguments.
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)
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)