rt-thread-official/components/libc/compilers/armlibc/syscalls.c

392 lines
9.5 KiB
C
Raw Permalink Normal View History

/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
2018-10-14 19:28:18 +08:00
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2012-11-23 Yihui The first version
2013-11-24 23:11:17 +08:00
* 2013-11-24 aozima fixed _sys_read()/_sys_write() issues.
2015-01-28 14:14:30 +08:00
* 2014-08-03 bernard If using msh, use system() implementation
* in msh.
2021-03-08 18:19:04 +08:00
* 2020-08-05 Meco Man fixed _sys_flen() compiling-warning when
* RT_USING_DFS is not defined
2021-02-13 13:30:40 +08:00
* 2020-02-13 Meco Man re-implement exit() and abort()
2021-02-14 18:46:55 +08:00
* 2020-02-14 Meco Man implement _sys_tmpnam()
*/
#include <rt_sys.h>
2021-02-14 18:47:48 +08:00
#include <rtthread.h>
#include <string.h>
2021-10-26 12:51:32 +08:00
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
2021-12-26 22:41:24 +08:00
#include <compiler_private.h>
2022-01-03 06:51:44 +08:00
#ifdef RT_USING_POSIX_STDIO
#include <posix/stdio.h>
2022-01-03 06:51:44 +08:00
#endif /* RT_USING_POSIX_STDIO */
2023-12-30 21:52:52 +08:00
#include <posix/stdlib.h>
#define DBG_TAG "armlibc.syscalls"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#ifdef __clang__
2022-07-25 10:21:18 +08:00
__asm(".global __use_no_semihosting\n\t");
2018-09-23 12:08:44 +08:00
#else
2022-07-25 10:21:18 +08:00
#pragma import(__use_no_semihosting_swi)
#endif
/* Standard IO device handles. */
#define STDIN 0
#define STDOUT 1
#define STDERR 2
/* Standard IO device name defines. */
const char __stdin_name[] = "STDIN";
const char __stdout_name[] = "STDOUT";
const char __stderr_name[] = "STDERR";
/**
* required by fopen() and freopen().
*
* @param name - file name with path.
* @param openmode - a bitmap hose bits mostly correspond directly to
* the ISO mode specification.
* @return -1 if an error occurs.
*/
FILEHANDLE _sys_open(const char *name, int openmode)
{
2021-11-16 00:22:49 +08:00
#ifdef DFS_USING_POSIX
2013-11-24 23:11:17 +08:00
int fd;
int mode = O_RDONLY;
2021-11-16 00:22:49 +08:00
#endif /* DFS_USING_POSIX */
2015-01-28 14:14:30 +08:00
/* Register standard Input Output devices. */
if (strcmp(name, __stdin_name) == 0)
return (STDIN);
if (strcmp(name, __stdout_name) == 0)
return (STDOUT);
if (strcmp(name, __stderr_name) == 0)
return (STDERR);
2021-11-16 00:22:49 +08:00
#ifndef DFS_USING_POSIX
2021-12-29 20:48:36 +08:00
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
2022-06-22 11:51:49 +08:00
return -1; /* error */
#else
2015-01-28 14:14:30 +08:00
/* Correct openmode from fopen to open */
if (openmode & OPEN_PLUS)
{
if (openmode & OPEN_W)
{
mode |= (O_RDWR | O_TRUNC | O_CREAT);
}
else if (openmode & OPEN_A)
{
mode |= (O_RDWR | O_APPEND | O_CREAT);
}
else
mode |= O_RDWR;
}
else
{
if (openmode & OPEN_W)
{
mode |= (O_WRONLY | O_TRUNC | O_CREAT);
}
else if (openmode & OPEN_A)
{
mode |= (O_WRONLY | O_APPEND | O_CREAT);
2015-01-28 14:14:30 +08:00
}
}
fd = open(name, mode, 0);
2015-01-28 14:14:30 +08:00
if (fd < 0)
2022-06-22 11:51:49 +08:00
return -1; /* error */
2013-11-24 23:11:17 +08:00
else
return fd;
2021-11-16 00:22:49 +08:00
#endif /* DFS_USING_POSIX */
}
int _sys_close(FILEHANDLE fh)
{
2021-11-16 00:22:49 +08:00
#ifdef DFS_USING_POSIX
2021-10-26 12:51:32 +08:00
if (fh <= STDERR)
return 0; /* error */
2013-11-24 23:11:17 +08:00
return close(fh);
2021-10-26 12:51:32 +08:00
#else
2021-12-29 20:48:36 +08:00
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
2022-06-22 11:51:49 +08:00
return 0; /* error */
2021-11-16 00:22:49 +08:00
#endif /* DFS_USING_POSIX */
}
/*
* Read from a file. Can return:
* - zero if the read was completely successful
* - the number of bytes _not_ read, if the read was partially successful
* - the number of bytes not read, plus the top bit set (0x80000000), if
* the read was partially successful due to end of file
* - -1 if some error other than EOF occurred
*
* It is also legal to signal EOF by returning no data but
* signalling no error (i.e. the top-bit-set mechanism need never
* be used).
*
* So if (for example) the user is trying to read 8 bytes at a time
* from a file in which only 5 remain, this routine can do three
* equally valid things:
*
* - it can return 0x80000003 (3 bytes not read due to EOF)
* - OR it can return 3 (3 bytes not read), and then return
* 0x80000008 (8 bytes not read due to EOF) on the next attempt
* - OR it can return 3 (3 bytes not read), and then return
* 8 (8 bytes not read, meaning 0 read, meaning EOF) on the next
* attempt
*
* `mode' exists for historical reasons and must be ignored.
*/
int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
{
2021-11-16 00:22:49 +08:00
#ifdef DFS_USING_POSIX
2013-11-24 23:11:17 +08:00
int size;
2015-01-28 14:14:30 +08:00
if (fh == STDIN)
{
2022-01-03 06:51:44 +08:00
#ifdef RT_USING_POSIX_STDIO
if (rt_posix_stdio_get_console() < 0)
{
2021-12-29 12:25:16 +08:00
LOG_W("Do not invoke standard output before initializing Compiler");
2021-11-13 05:47:32 +08:00
return 0; /* error, but keep going */
}
size = read(STDIN_FILENO, buf, len);
return len - size; /* success */
2021-11-13 05:47:32 +08:00
#else
2022-01-03 06:51:44 +08:00
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_STDIO);
2021-11-13 05:47:32 +08:00
return 0; /* error */
2022-01-03 06:51:44 +08:00
#endif /* RT_USING_POSIX_STDIO */
}
2021-11-13 05:47:32 +08:00
else if (fh == STDOUT || fh == STDERR)
{
return -1; /* 100% error */
}
2013-11-24 23:11:17 +08:00
else
2021-11-13 05:47:32 +08:00
{
size = read(fh, buf, len);
if (size >= 0)
{
2021-11-13 05:47:32 +08:00
return len - size; /* success */
}
2021-11-13 05:47:32 +08:00
else
{
2021-11-13 05:47:32 +08:00
return 0; /* error */
}
2021-11-13 05:47:32 +08:00
}
2021-10-26 12:51:32 +08:00
#else
2021-12-29 20:48:36 +08:00
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
2021-10-26 12:51:32 +08:00
return 0; /* error */
2021-11-16 00:22:49 +08:00
#endif /* DFS_USING_POSIX */
}
/*
* Write to a file. Returns 0 on success, negative on error, and
* the number of characters _not_ written on partial success.
* `mode' exists for historical reasons and must be ignored.
2021-11-13 05:47:32 +08:00
* The return value is either:
* A positive number representing the number of characters not written
* (so any nonzero return value denotes a failure of some sort).
* A negative number indicating an error.
*/
int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
{
2021-11-16 00:22:49 +08:00
#ifdef DFS_USING_POSIX
2013-11-24 23:11:17 +08:00
int size;
2021-11-16 00:22:49 +08:00
#endif /* DFS_USING_POSIX */
2015-01-28 14:14:30 +08:00
2021-11-13 05:47:32 +08:00
if (fh == STDOUT || fh == STDERR)
{
#if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
2021-11-13 05:47:32 +08:00
rt_device_t console;
console = rt_console_get_device();
if (console)
{
2021-11-13 05:47:32 +08:00
rt_device_write(console, -1, buf, len);
}
2021-11-13 05:47:32 +08:00
return 0; /* success */
#else
2021-10-26 12:51:32 +08:00
return 0; /* error */
#endif /* defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE) */
}
else if (fh == STDIN)
{
return -1; /* 100% error */
}
2013-11-24 23:11:17 +08:00
else
2021-11-13 05:47:32 +08:00
{
2021-11-16 00:22:49 +08:00
#ifdef DFS_USING_POSIX
2021-11-13 05:47:32 +08:00
size = write(fh, buf, len);
if (size >= 0)
{
/*
fflush doesn't have a good solution in Keil-MDK,
so it has to sync/flush when for each writen.
*/
fsync(fh);
return len - size; /* success */
}
2021-11-13 05:47:32 +08:00
else
{
2021-11-13 05:47:32 +08:00
return 0; /* error */
}
2021-10-26 12:51:32 +08:00
#else
2021-12-29 20:48:36 +08:00
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
2021-11-13 05:47:32 +08:00
return 0; /* error */
2021-11-16 00:22:49 +08:00
#endif /* DFS_USING_POSIX */
2021-11-13 05:47:32 +08:00
}
}
/*
* Flush any OS buffers associated with fh, ensuring that the file
* is up to date on disk. Result is >=0 if OK, negative for an
* error.
* This function is deprecated. It is never called by any other library function,
* and you are not required to re-implement it if you are retargeting standard I/O (stdio).
*/
int _sys_ensure(FILEHANDLE fh)
{
#ifdef DFS_USING_POSIX
return fsync(fh);
#else
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
return 0; /* error */
#endif /* DFS_USING_POSIX */
}
/*
* Move the file position to a given offset from the file start.
* Returns >=0 on success, <0 on failure.
*/
int _sys_seek(FILEHANDLE fh, long pos)
{
2021-11-16 00:22:49 +08:00
#ifdef DFS_USING_POSIX
2013-11-24 23:11:17 +08:00
if (fh < STDERR)
2021-10-26 12:51:32 +08:00
return 0; /* error */
2013-11-24 23:11:17 +08:00
/* position is relative to the start of file fh */
return lseek(fh, pos, 0);
2021-10-26 12:51:32 +08:00
#else
2021-12-29 20:48:36 +08:00
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
2021-10-26 12:51:32 +08:00
return 0; /* error */
2021-11-16 00:22:49 +08:00
#endif /* DFS_USING_POSIX */
}
/**
* used by tmpnam() or tmpfile()
*/
#if __ARMCC_VERSION >= 6190000
void _sys_tmpnam(char *name, int fileno, unsigned maxlength)
{
rt_snprintf(name, maxlength, "tem%03d", fileno);
}
#else
int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
{
2021-02-14 18:46:55 +08:00
rt_snprintf(name, maxlength, "tem%03d", fileno);
return 1;
}
#endif /* __ARMCC_VERSION >= 6190000 */
char *_sys_command_string(char *cmd, int len)
{
/* no support */
return RT_NULL;
}
/* This function writes a character to the console. */
void _ttywrch(int ch)
{
#ifdef RT_USING_CONSOLE
2021-12-30 02:59:46 +08:00
rt_kprintf("%c", (char)ch);
2021-10-26 12:51:32 +08:00
#endif /* RT_USING_CONSOLE */
}
2021-02-20 11:31:58 +08:00
/* for exit() and abort() */
rt_weak void _sys_exit(int return_code)
{
2021-02-17 23:50:12 +08:00
__rt_libc_exit(return_code);
2022-07-25 10:21:18 +08:00
while (1);
}
/**
2013-11-24 23:11:17 +08:00
* return current length of file.
*
* @param fh - file handle
* @return file length, or -1 on failed
*/
long _sys_flen(FILEHANDLE fh)
{
2021-11-16 00:22:49 +08:00
#ifdef DFS_USING_POSIX
2019-01-24 09:05:41 +08:00
struct stat stat;
2020-08-05 09:28:10 +08:00
2019-01-24 09:05:41 +08:00
if (fh < STDERR)
2021-10-26 12:51:32 +08:00
return 0; /* error */
2019-01-24 09:05:41 +08:00
fstat(fh, &stat);
return stat.st_size;
2020-08-05 09:28:10 +08:00
#else
2021-12-29 20:48:36 +08:00
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
2021-10-26 12:51:32 +08:00
return 0;
2021-11-16 00:22:49 +08:00
#endif /* DFS_USING_POSIX */
}
int _sys_istty(FILEHANDLE fh)
{
2022-07-25 10:21:18 +08:00
if ((STDIN <= fh) && (fh <= STDERR))
return 1;
else
return 0;
}
int remove(const char *filename)
{
2021-11-16 00:22:49 +08:00
#ifdef DFS_USING_POSIX
return unlink(filename);
2021-10-26 12:51:32 +08:00
#else
2021-12-29 20:48:36 +08:00
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
2021-10-26 12:51:32 +08:00
return 0; /* error */
2021-11-16 00:22:49 +08:00
#endif /* DFS_USING_POSIX */
}
#ifdef __MICROLIB
#include <stdio.h>
2021-03-08 18:19:04 +08:00
int fputc(int c, FILE *f)
{
2021-10-26 12:51:32 +08:00
#ifdef RT_USING_CONSOLE
2021-12-30 02:59:46 +08:00
rt_kprintf("%c", (char)c);
return 1;
2021-10-26 12:51:32 +08:00
#else
return 0; /* error */
#endif /* RT_USING_CONSOLE */
}
2021-03-08 18:19:04 +08:00
int fgetc(FILE *f)
{
2022-01-03 06:51:44 +08:00
#ifdef RT_USING_POSIX_STDIO
char ch;
if (rt_posix_stdio_get_console() < 0)
{
2021-12-29 12:25:16 +08:00
LOG_W("Do not invoke standard output before initializing Compiler");
2021-10-26 12:51:32 +08:00
return 0;
}
2022-07-25 10:21:18 +08:00
if (read(STDIN_FILENO, &ch, 1) == 1)
return ch;
2022-01-03 06:51:44 +08:00
#endif /* RT_USING_POSIX_STDIO */
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_STDIO);
2021-10-26 12:51:32 +08:00
return 0; /* error */
}
2021-10-26 12:51:32 +08:00
#endif /* __MICROLIB */