syscalls.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (c) 2006-2022, 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. #include <compiler_private.h>
  24. #ifdef RT_USING_POSIX_STDIO
  25. #include <posix/stdio.h>
  26. #endif /* RT_USING_POSIX_STDIO */
  27. #include <posix/stdlib.h>
  28. #define DBG_TAG "armlibc.syscalls"
  29. #define DBG_LVL DBG_INFO
  30. #include <rtdbg.h>
  31. #ifdef __clang__
  32. __asm(".global __use_no_semihosting\n\t");
  33. #else
  34. #pragma import(__use_no_semihosting_swi)
  35. #endif
  36. /* Standard IO device handles. */
  37. #define STDIN 0
  38. #define STDOUT 1
  39. #define STDERR 2
  40. /* Standard IO device name defines. */
  41. const char __stdin_name[] = "STDIN";
  42. const char __stdout_name[] = "STDOUT";
  43. const char __stderr_name[] = "STDERR";
  44. /**
  45. * required by fopen() and freopen().
  46. *
  47. * @param name - file name with path.
  48. * @param openmode - a bitmap hose bits mostly correspond directly to
  49. * the ISO mode specification.
  50. * @return -1 if an error occurs.
  51. */
  52. FILEHANDLE _sys_open(const char *name, int openmode)
  53. {
  54. #ifdef DFS_USING_POSIX
  55. int fd;
  56. int mode = O_RDONLY;
  57. #endif /* DFS_USING_POSIX */
  58. /* Register standard Input Output devices. */
  59. if (strcmp(name, __stdin_name) == 0)
  60. return (STDIN);
  61. if (strcmp(name, __stdout_name) == 0)
  62. return (STDOUT);
  63. if (strcmp(name, __stderr_name) == 0)
  64. return (STDERR);
  65. #ifndef DFS_USING_POSIX
  66. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  67. return -1; /* error */
  68. #else
  69. /* Correct openmode from fopen to open */
  70. if (openmode & OPEN_PLUS)
  71. {
  72. if (openmode & OPEN_W)
  73. {
  74. mode |= (O_RDWR | O_TRUNC | O_CREAT);
  75. }
  76. else if (openmode & OPEN_A)
  77. {
  78. mode |= (O_RDWR | O_APPEND | O_CREAT);
  79. }
  80. else
  81. mode |= O_RDWR;
  82. }
  83. else
  84. {
  85. if (openmode & OPEN_W)
  86. {
  87. mode |= (O_WRONLY | O_TRUNC | O_CREAT);
  88. }
  89. else if (openmode & OPEN_A)
  90. {
  91. mode |= (O_WRONLY | O_APPEND | O_CREAT);
  92. }
  93. }
  94. fd = open(name, mode, 0);
  95. if (fd < 0)
  96. return -1; /* error */
  97. else
  98. return fd;
  99. #endif /* DFS_USING_POSIX */
  100. }
  101. int _sys_close(FILEHANDLE fh)
  102. {
  103. #ifdef DFS_USING_POSIX
  104. if (fh <= STDERR)
  105. return 0; /* error */
  106. return close(fh);
  107. #else
  108. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  109. return 0; /* error */
  110. #endif /* DFS_USING_POSIX */
  111. }
  112. /*
  113. * Read from a file. Can return:
  114. * - zero if the read was completely successful
  115. * - the number of bytes _not_ read, if the read was partially successful
  116. * - the number of bytes not read, plus the top bit set (0x80000000), if
  117. * the read was partially successful due to end of file
  118. * - -1 if some error other than EOF occurred
  119. *
  120. * It is also legal to signal EOF by returning no data but
  121. * signalling no error (i.e. the top-bit-set mechanism need never
  122. * be used).
  123. *
  124. * So if (for example) the user is trying to read 8 bytes at a time
  125. * from a file in which only 5 remain, this routine can do three
  126. * equally valid things:
  127. *
  128. * - it can return 0x80000003 (3 bytes not read due to EOF)
  129. * - OR it can return 3 (3 bytes not read), and then return
  130. * 0x80000008 (8 bytes not read due to EOF) on the next attempt
  131. * - OR it can return 3 (3 bytes not read), and then return
  132. * 8 (8 bytes not read, meaning 0 read, meaning EOF) on the next
  133. * attempt
  134. *
  135. * `mode' exists for historical reasons and must be ignored.
  136. */
  137. int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
  138. {
  139. #ifdef DFS_USING_POSIX
  140. int size;
  141. if (fh == STDIN)
  142. {
  143. #ifdef RT_USING_POSIX_STDIO
  144. if (rt_posix_stdio_get_console() < 0)
  145. {
  146. LOG_W("Do not invoke standard output before initializing Compiler");
  147. return 0; /* error, but keep going */
  148. }
  149. size = read(STDIN_FILENO, buf, len);
  150. return len - size; /* success */
  151. #else
  152. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_STDIO);
  153. return 0; /* error */
  154. #endif /* RT_USING_POSIX_STDIO */
  155. }
  156. else if (fh == STDOUT || fh == STDERR)
  157. {
  158. return -1; /* 100% error */
  159. }
  160. else
  161. {
  162. size = read(fh, buf, len);
  163. if (size >= 0)
  164. {
  165. return len - size; /* success */
  166. }
  167. else
  168. {
  169. return 0; /* error */
  170. }
  171. }
  172. #else
  173. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  174. return 0; /* error */
  175. #endif /* DFS_USING_POSIX */
  176. }
  177. /*
  178. * Write to a file. Returns 0 on success, negative on error, and
  179. * the number of characters _not_ written on partial success.
  180. * `mode' exists for historical reasons and must be ignored.
  181. * The return value is either:
  182. * A positive number representing the number of characters not written
  183. * (so any nonzero return value denotes a failure of some sort).
  184. * A negative number indicating an error.
  185. */
  186. int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
  187. {
  188. #ifdef DFS_USING_POSIX
  189. int size;
  190. #endif /* DFS_USING_POSIX */
  191. if (fh == STDOUT || fh == STDERR)
  192. {
  193. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  194. rt_device_t console;
  195. console = rt_console_get_device();
  196. if (console)
  197. {
  198. rt_device_write(console, -1, buf, len);
  199. }
  200. return 0; /* success */
  201. #else
  202. return 0; /* error */
  203. #endif /* defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE) */
  204. }
  205. else if (fh == STDIN)
  206. {
  207. return -1; /* 100% error */
  208. }
  209. else
  210. {
  211. #ifdef DFS_USING_POSIX
  212. size = write(fh, buf, len);
  213. if (size >= 0)
  214. {
  215. /*
  216. fflush doesn't have a good solution in Keil-MDK,
  217. so it has to sync/flush when for each writen.
  218. */
  219. fsync(fh);
  220. return len - size; /* success */
  221. }
  222. else
  223. {
  224. return 0; /* error */
  225. }
  226. #else
  227. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  228. return 0; /* error */
  229. #endif /* DFS_USING_POSIX */
  230. }
  231. }
  232. /*
  233. * Flush any OS buffers associated with fh, ensuring that the file
  234. * is up to date on disk. Result is >=0 if OK, negative for an
  235. * error.
  236. * This function is deprecated. It is never called by any other library function,
  237. * and you are not required to re-implement it if you are retargeting standard I/O (stdio).
  238. */
  239. int _sys_ensure(FILEHANDLE fh)
  240. {
  241. #ifdef DFS_USING_POSIX
  242. return fsync(fh);
  243. #else
  244. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  245. return 0; /* error */
  246. #endif /* DFS_USING_POSIX */
  247. }
  248. /*
  249. * Move the file position to a given offset from the file start.
  250. * Returns >=0 on success, <0 on failure.
  251. */
  252. int _sys_seek(FILEHANDLE fh, long pos)
  253. {
  254. #ifdef DFS_USING_POSIX
  255. if (fh < STDERR)
  256. return 0; /* error */
  257. /* position is relative to the start of file fh */
  258. return lseek(fh, pos, 0);
  259. #else
  260. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  261. return 0; /* error */
  262. #endif /* DFS_USING_POSIX */
  263. }
  264. /**
  265. * used by tmpnam() or tmpfile()
  266. */
  267. #if __ARMCC_VERSION >= 6190000
  268. void _sys_tmpnam(char *name, int fileno, unsigned maxlength)
  269. {
  270. rt_snprintf(name, maxlength, "tem%03d", fileno);
  271. }
  272. #else
  273. int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
  274. {
  275. rt_snprintf(name, maxlength, "tem%03d", fileno);
  276. return 1;
  277. }
  278. #endif /* __ARMCC_VERSION >= 6190000 */
  279. char *_sys_command_string(char *cmd, int len)
  280. {
  281. /* no support */
  282. return RT_NULL;
  283. }
  284. /* This function writes a character to the console. */
  285. void _ttywrch(int ch)
  286. {
  287. #ifdef RT_USING_CONSOLE
  288. rt_kprintf("%c", (char)ch);
  289. #endif /* RT_USING_CONSOLE */
  290. }
  291. /* for exit() and abort() */
  292. rt_weak void _sys_exit(int return_code)
  293. {
  294. __rt_libc_exit(return_code);
  295. while (1);
  296. }
  297. /**
  298. * return current length of file.
  299. *
  300. * @param fh - file handle
  301. * @return file length, or -1 on failed
  302. */
  303. long _sys_flen(FILEHANDLE fh)
  304. {
  305. #ifdef DFS_USING_POSIX
  306. struct stat stat;
  307. if (fh < STDERR)
  308. return 0; /* error */
  309. fstat(fh, &stat);
  310. return stat.st_size;
  311. #else
  312. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  313. return 0;
  314. #endif /* DFS_USING_POSIX */
  315. }
  316. int _sys_istty(FILEHANDLE fh)
  317. {
  318. if ((STDIN <= fh) && (fh <= STDERR))
  319. return 1;
  320. else
  321. return 0;
  322. }
  323. int remove(const char *filename)
  324. {
  325. #ifdef DFS_USING_POSIX
  326. return unlink(filename);
  327. #else
  328. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
  329. return 0; /* error */
  330. #endif /* DFS_USING_POSIX */
  331. }
  332. #ifdef __MICROLIB
  333. #include <stdio.h>
  334. int fputc(int c, FILE *f)
  335. {
  336. #ifdef RT_USING_CONSOLE
  337. rt_kprintf("%c", (char)c);
  338. return 1;
  339. #else
  340. return 0; /* error */
  341. #endif /* RT_USING_CONSOLE */
  342. }
  343. int fgetc(FILE *f)
  344. {
  345. #ifdef RT_USING_POSIX_STDIO
  346. char ch;
  347. if (rt_posix_stdio_get_console() < 0)
  348. {
  349. LOG_W("Do not invoke standard output before initializing Compiler");
  350. return 0;
  351. }
  352. if (read(STDIN_FILENO, &ch, 1) == 1)
  353. return ch;
  354. #endif /* RT_USING_POSIX_STDIO */
  355. LOG_W("%s: %s", __func__, _WARNING_WITHOUT_STDIO);
  356. return 0; /* error */
  357. }
  358. #endif /* __MICROLIB */