syscalls.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-11-23 Yihui The first version
  9. * 2013-11-24 aozima fixed _sys_read()/_sys_write() issues.
  10. * 2014-08-03 bernard If using msh, use system() implementation
  11. * in msh.
  12. * 2020-08-05 Meco Man fixed _sys_flen() compiling-warning when
  13. * RT_USING_DFS is not defined
  14. * 2020-02-13 Meco Man re-implement exit() and abort()
  15. * 2020-02-14 Meco Man implement _sys_tmpnam()
  16. */
  17. #include <rt_sys.h>
  18. #include <rtthread.h>
  19. #include <string.h>
  20. #include <fcntl.h>
  21. #include <unistd.h>
  22. #include <sys/stat.h>
  23. #ifdef RT_USING_POSIX_DEVIO
  24. #include "libc.h"
  25. #endif /* RT_USING_POSIX_DEVIO */
  26. #define DBG_TAG "armlibc.syscalls"
  27. #define DBG_LVL DBG_INFO
  28. #include <rtdbg.h>
  29. #ifdef __CLANG_ARM
  30. __asm(".global __use_no_semihosting\n\t");
  31. #else
  32. #pragma import(__use_no_semihosting_swi)
  33. #endif /* __CLANG_ARM */
  34. /* Standard IO device handles. */
  35. #define STDIN 0
  36. #define STDOUT 1
  37. #define STDERR 2
  38. /* Standard IO device name defines. */
  39. const char __stdin_name[] = "STDIN";
  40. const char __stdout_name[] = "STDOUT";
  41. const char __stderr_name[] = "STDERR";
  42. /**
  43. * required by fopen() and freopen().
  44. *
  45. * @param name - file name with path.
  46. * @param openmode - a bitmap hose bits mostly correspond directly to
  47. * the ISO mode specification.
  48. * @return -1 if an error occurs.
  49. */
  50. FILEHANDLE _sys_open(const char *name, int openmode)
  51. {
  52. #ifdef DFS_USING_POSIX
  53. int fd;
  54. int mode = O_RDONLY;
  55. #endif /* DFS_USING_POSIX */
  56. /* Register standard Input Output devices. */
  57. if (strcmp(name, __stdin_name) == 0)
  58. return (STDIN);
  59. if (strcmp(name, __stdout_name) == 0)
  60. return (STDOUT);
  61. if (strcmp(name, __stderr_name) == 0)
  62. return (STDERR);
  63. #ifndef DFS_USING_POSIX
  64. return 0; /* error */
  65. #else
  66. /* Correct openmode from fopen to open */
  67. if (openmode & OPEN_PLUS)
  68. {
  69. if (openmode & OPEN_W)
  70. {
  71. mode |= (O_RDWR | O_TRUNC | O_CREAT);
  72. }
  73. else if (openmode & OPEN_A)
  74. {
  75. mode |= (O_RDWR | O_APPEND | O_CREAT);
  76. }
  77. else
  78. mode |= O_RDWR;
  79. }
  80. else
  81. {
  82. if (openmode & OPEN_W)
  83. {
  84. mode |= (O_WRONLY | O_TRUNC | O_CREAT);
  85. }
  86. else if (openmode & OPEN_A)
  87. {
  88. mode |= (O_WRONLY | O_APPEND | O_CREAT);
  89. }
  90. }
  91. fd = open(name, mode, 0);
  92. if (fd < 0)
  93. return 0; /* error */
  94. else
  95. return fd;
  96. #endif /* DFS_USING_POSIX */
  97. }
  98. int _sys_close(FILEHANDLE fh)
  99. {
  100. #ifdef DFS_USING_POSIX
  101. if (fh <= STDERR)
  102. return 0; /* error */
  103. return close(fh);
  104. #else
  105. return 0;
  106. #endif /* DFS_USING_POSIX */
  107. }
  108. /*
  109. * Read from a file. Can return:
  110. * - zero if the read was completely successful
  111. * - the number of bytes _not_ read, if the read was partially successful
  112. * - the number of bytes not read, plus the top bit set (0x80000000), if
  113. * the read was partially successful due to end of file
  114. * - -1 if some error other than EOF occurred
  115. *
  116. * It is also legal to signal EOF by returning no data but
  117. * signalling no error (i.e. the top-bit-set mechanism need never
  118. * be used).
  119. *
  120. * So if (for example) the user is trying to read 8 bytes at a time
  121. * from a file in which only 5 remain, this routine can do three
  122. * equally valid things:
  123. *
  124. * - it can return 0x80000003 (3 bytes not read due to EOF)
  125. * - OR it can return 3 (3 bytes not read), and then return
  126. * 0x80000008 (8 bytes not read due to EOF) on the next attempt
  127. * - OR it can return 3 (3 bytes not read), and then return
  128. * 8 (8 bytes not read, meaning 0 read, meaning EOF) on the next
  129. * attempt
  130. *
  131. * `mode' exists for historical reasons and must be ignored.
  132. */
  133. int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
  134. {
  135. #ifdef DFS_USING_POSIX
  136. int size;
  137. if (fh == STDIN)
  138. {
  139. #ifdef RT_USING_POSIX_DEVIO
  140. if (libc_stdio_get_console() < 0)
  141. {
  142. LOG_W("Do not invoke standard output before initializing libc");
  143. return 0; /* error, but keep going */
  144. }
  145. size = read(STDIN_FILENO, buf, len);
  146. return len - size; /* success */
  147. #else
  148. return 0; /* error */
  149. #endif /* RT_USING_POSIX_DEVIO */
  150. }
  151. else if (fh == STDOUT || fh == STDERR)
  152. {
  153. return -1; /* 100% error */
  154. }
  155. else
  156. {
  157. size = read(fh, buf, len);
  158. if (size >= 0)
  159. {
  160. return len - size; /* success */
  161. }
  162. else
  163. {
  164. return 0; /* error */
  165. }
  166. }
  167. #else
  168. return 0; /* error */
  169. #endif /* DFS_USING_POSIX */
  170. }
  171. /*
  172. * Write to a file. Returns 0 on success, negative on error, and
  173. * the number of characters _not_ written on partial success.
  174. * `mode' exists for historical reasons and must be ignored.
  175. * The return value is either:
  176. * A positive number representing the number of characters not written
  177. * (so any nonzero return value denotes a failure of some sort).
  178. * A negative number indicating an error.
  179. */
  180. int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
  181. {
  182. #ifdef DFS_USING_POSIX
  183. int size;
  184. #endif /* DFS_USING_POSIX */
  185. if (fh == STDOUT || fh == STDERR)
  186. {
  187. #ifdef RT_USING_CONSOLE
  188. rt_device_t console;
  189. console = rt_console_get_device();
  190. if (console)
  191. {
  192. rt_device_write(console, -1, buf, len);
  193. }
  194. return 0; /* success */
  195. #else
  196. return 0; /* error */
  197. #endif /* RT_USING_CONSOLE */
  198. }
  199. else if (fh == STDIN)
  200. {
  201. return -1; /* 100% error */
  202. }
  203. else
  204. {
  205. #ifdef DFS_USING_POSIX
  206. size = write(fh, buf, len);
  207. if (size >= 0)
  208. {
  209. return len - size; /* success */
  210. }
  211. else
  212. {
  213. return 0; /* error */
  214. }
  215. #else
  216. return 0; /* error */
  217. #endif /* DFS_USING_POSIX */
  218. }
  219. }
  220. /*
  221. * Move the file position to a given offset from the file start.
  222. * Returns >=0 on success, <0 on failure.
  223. */
  224. int _sys_seek(FILEHANDLE fh, long pos)
  225. {
  226. #ifdef DFS_USING_POSIX
  227. if (fh < STDERR)
  228. return 0; /* error */
  229. /* position is relative to the start of file fh */
  230. return lseek(fh, pos, 0);
  231. #else
  232. return 0; /* error */
  233. #endif /* DFS_USING_POSIX */
  234. }
  235. /**
  236. * used by tmpnam() or tmpfile()
  237. */
  238. int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
  239. {
  240. rt_snprintf(name, maxlength, "tem%03d", fileno);
  241. return 1;
  242. }
  243. char *_sys_command_string(char *cmd, int len)
  244. {
  245. /* no support */
  246. return RT_NULL;
  247. }
  248. /* This function writes a character to the console. */
  249. void _ttywrch(int ch)
  250. {
  251. #ifdef RT_USING_CONSOLE
  252. char c;
  253. c = (char)ch;
  254. rt_kprintf(&c);
  255. #endif /* RT_USING_CONSOLE */
  256. }
  257. /* for exit() and abort() */
  258. RT_WEAK void _sys_exit(int return_code)
  259. {
  260. extern void __rt_libc_exit(int status);
  261. __rt_libc_exit(return_code);
  262. while(1);
  263. }
  264. /**
  265. * return current length of file.
  266. *
  267. * @param fh - file handle
  268. * @return file length, or -1 on failed
  269. */
  270. long _sys_flen(FILEHANDLE fh)
  271. {
  272. #ifdef DFS_USING_POSIX
  273. struct stat stat;
  274. if (fh < STDERR)
  275. return 0; /* error */
  276. fstat(fh, &stat);
  277. return stat.st_size;
  278. #else
  279. return 0;
  280. #endif /* DFS_USING_POSIX */
  281. }
  282. int _sys_istty(FILEHANDLE fh)
  283. {
  284. if((STDIN <= fh) && (fh <= STDERR))
  285. return 1;
  286. else
  287. return 0;
  288. }
  289. int remove(const char *filename)
  290. {
  291. #ifdef DFS_USING_POSIX
  292. return unlink(filename);
  293. #else
  294. return 0; /* error */
  295. #endif /* DFS_USING_POSIX */
  296. }
  297. #ifdef __MICROLIB
  298. #include <stdio.h>
  299. int fputc(int c, FILE *f)
  300. {
  301. #ifdef RT_USING_CONSOLE
  302. char ch[2] = {0};
  303. ch[0] = c;
  304. rt_kprintf(&ch[0]);
  305. return 1;
  306. #else
  307. return 0; /* error */
  308. #endif /* RT_USING_CONSOLE */
  309. }
  310. int fgetc(FILE *f)
  311. {
  312. #ifdef RT_USING_POSIX_DEVIO
  313. char ch;
  314. if (libc_stdio_get_console() < 0)
  315. {
  316. LOG_W("Do not invoke standard output before initializing libc");
  317. return 0;
  318. }
  319. if(read(STDIN_FILENO, &ch, 1) == 1)
  320. return ch;
  321. #endif /* RT_USING_POSIX_DEVIO */
  322. return 0; /* error */
  323. }
  324. #endif /* __MICROLIB */