13#pragma clang diagnostic ignored "-Wempty-translation-unit"
14#elif defined(__GNUC__)
15#pragma GCC diagnostic ignored "-Wold-style-cast"
16#pragma GCC diagnostic ignored "-Wredundant-tags"
17#pragma GCC diagnostic ignored "-Wcast-qual"
18#pragma GCC diagnostic ignored "-Wuseless-cast"
19#pragma GCC diagnostic ignored "-Wsign-conversion"
24#if defined(__ARM_EABI__)
28#if defined(OS_USE_OS_APP_CONFIG_H)
29#include <cmsis-plus/os-app-config.h>
34#if defined(OS_USE_SEMIHOSTING_SYSCALLS)
44#include "cmsis_device.h"
108#if !defined(OS_INTEGER_SEMIHOSTING_MAX_OPEN_FILES)
109#define OS_INTEGER_SEMIHOSTING_MAX_OPEN_FILES (20)
120__semihosting_findslot (
int fd)
129 if (openfiles[fd].handle == -1)
135 return &openfiles[fd];
141__semihosting_newslot (
void)
146 if (openfiles[i].handle == -1)
161__semihosting_get_errno (
void)
168__semihosting_error (
int result)
170 errno = __semihosting_get_errno ();
176__semihosting_checkerror (
int result)
180 return __semihosting_error (-1);
188__semihosting_lseek (
int fd,
int ptr,
int dir)
193 pfd = __semihosting_findslot (fd);
201 if ((dir != SEEK_CUR) && (dir != SEEK_SET) && (dir != SEEK_END))
210 ptr = pfd->pos + ptr;
215 if ((pfd->pos > 0) && (ptr > 0))
229 block[0] = pfd->handle;
239 block[0] = pfd->handle;
256__semihosting_stat (
int fd,
struct stat* st)
259 pfd = __semihosting_findslot (fd);
268 st->st_mode |= S_IFCHR;
269 st->st_blksize = 1024;
272 res = __semihosting_checkerror (
300 int fd = __semihosting_newslot ();
308 if ((oflag & O_CREAT) && (oflag & O_EXCL))
323 if (oflag & O_BINARY)
336 if ((oflag & O_CREAT) || (oflag & O_TRUNC) || (oflag & O_WRONLY))
341 if (oflag & O_APPEND)
349 block[0] = (uint32_t)path;
350 block[2] = std::strlen (path);
351 block[1] = (uint32_t)aflags;
358 openfiles[fd].handle = fh;
359 openfiles[fd].pos = 0;
364 return __semihosting_error (fh);
372 pfd = __semihosting_findslot (fildes);
380 if ((fildes == 1 || fildes == 2)
381 && (openfiles[1].handle == openfiles[2].handle))
388 block[0] = pfd->handle;
413 pfd = __semihosting_findslot (fildes);
421 block[0] = pfd->handle;
433 pfd->pos += nbyte - res;
444 pfd = __semihosting_findslot (fildes);
453 block[0] = pfd->handle;
466 pfd->pos += nbyte - res;
470 if ((nbyte - res) == 0)
472 return __semihosting_error (0);
475 return (nbyte - res);
481 return __semihosting_lseek (fildes, offset, whence);
493 pfd = __semihosting_findslot (fildes);
508 errno = __semihosting_get_errno ();
515 memset (buf, 0,
sizeof (*buf));
516 return __semihosting_stat (fildes, buf);
526 memset (buf, 0,
sizeof (*buf));
533 buf->st_mode |= S_IFREG | S_IREAD;
534 int res = __semihosting_stat (fd, buf);
544 block[0] = (uint32_t)existing;
545 block[1] = std::strlen (existing);
546 block[2] = (uint32_t)_new;
547 block[3] = std::strlen (_new);
557 block[0] = (uint32_t)path;
558 block[1] = strlen (path);
564 return __semihosting_error (res);
575 if (command ==
nullptr)
581 block[0] = (uint32_t)command;
582 block[1] = strlen (command);
584 if ((e >= 0) && (e < 256))
591 for (exit_code = e; (e != 0) && (WEXITSTATUS (e) != exit_code); e <<= 1)
602 struct timezone* tzp = (
struct timezone*)ptimezone;
607 ptimeval->tv_usec = 0;
613 tzp->tz_minuteswest = 0;
636 buf->tms_utime = timeval;
649 strncpy (buf,
"/tmp", size);
653#pragma GCC diagnostic push
654#if defined(__clang__)
655#elif defined(__GNUC__)
656#pragma GCC diagnostic ignored "-Wunused-parameter"
710 return ((ssize_t) -1);
727#pragma GCC diagnostic pop
732#pragma GCC diagnostic push
733#if defined(__clang__)
734#elif defined(__GNUC__)
735#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[])
1095 return ((ssize_t)-1);
1102 return ((ssize_t)-1);
1109 return ((ssize_t)-1);
1112#pragma GCC diagnostic pop
1124void __attribute__ ((noreturn, weak))
1144#define ARGS_BUF_ARRAY_SIZE 80
1145#define ARGV_BUF_ARRAY_SIZE 10
1147typedef struct command_line_block_s
1151} command_line_block_t;
1157 static char args_buf[ARGS_BUF_ARRAY_SIZE];
1161 static char* argv_buf[ARGV_BUF_ARRAY_SIZE];
1164 int is_in_argument = 0;
1166 command_line_block_t cmd_block;
1167 cmd_block.command_line = args_buf;
1168 cmd_block.size =
sizeof (args_buf) - 1;
1175 args_buf[ARGS_BUF_ARRAY_SIZE - 1] =
'\0';
1178 char* p = cmd_block.command_line;
1183 while ((ch = *p) !=
'\0')
1185 if (is_in_argument == 0)
1190 >= (
int)((
sizeof (argv_buf) /
sizeof (argv_buf[0])) - 1))
1193 if (ch ==
'"' || ch ==
'\'')
1202 argv_buf[argc++] = p;
1206 else if (delim !=
'\0')
1215 else if (isblank (ch))
1229 argv_buf[0] = &args_buf[0];
1234 argv_buf[argc] = NULL;
1237 *p_argv = &argv_buf[0];
1245static int monitor_stdin;
1246static int monitor_stdout;
1247static int monitor_stderr;
1263 int volatile block[3];
1265 block[0] = (int)
":tt";
1270 block[0] = (int)
":tt";
1275 block[0] = (int)
":tt";
1281 if (monitor_stderr == -1)
1283 monitor_stderr = monitor_stdout;
1288 openfiles[i].handle = -1;
1291 openfiles[0].handle = monitor_stdin;
1292 openfiles[0].pos = 0;
1293 openfiles[1].handle = monitor_stdout;
1294 openfiles[1].pos = 0;
1295 openfiles[2].handle = monitor_stderr;
1296 openfiles[2].pos = 0;
1301#if (__STDC_HOSTED__ != 0)
1306#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)