1
0

syscalls.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 0; /* success */
  147. #else
  148. return 0; /* error */
  149. #endif /* RT_USING_POSIX_DEVIO */
  150. }
  151. else if (fh == STDOUT || fh == STDERR)
  152. {
  153. return 0; /* error */
  154. }
  155. else
  156. {
  157. size = read(fh, buf, len);
  158. if (size >= 0)
  159. return len - size; /* success */
  160. else
  161. return 0; /* error */
  162. }
  163. #else
  164. return 0; /* error */
  165. #endif /* DFS_USING_POSIX */
  166. }
  167. /*
  168. * Write to a file. Returns 0 on success, negative on error, and
  169. * the number of characters _not_ written on partial success.
  170. * `mode' exists for historical reasons and must be ignored.
  171. * The return value is either:
  172. * A positive number representing the number of characters not written
  173. * (so any nonzero return value denotes a failure of some sort).
  174. * A negative number indicating an error.
  175. */
  176. int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
  177. {
  178. #ifdef DFS_USING_POSIX
  179. int size;
  180. #endif /* DFS_USING_POSIX */
  181. if (fh == STDOUT || fh == STDERR)
  182. {
  183. #ifdef RT_USING_CONSOLE
  184. rt_device_t console;
  185. console = rt_console_get_device();
  186. if (console)
  187. {
  188. rt_device_write(console, -1, buf, len);
  189. }
  190. return 0; /* success */
  191. #else
  192. return 0; /* error */
  193. #endif /* RT_USING_CONSOLE */
  194. }
  195. else if (fh == STDIN)
  196. {
  197. return 0; /* error */
  198. }
  199. else
  200. {
  201. #ifdef DFS_USING_POSIX
  202. size = write(fh, buf, len);
  203. if (size >= 0)
  204. return 0; /* success */
  205. else
  206. return 0; /* error */
  207. #else
  208. return 0; /* error */
  209. #endif /* DFS_USING_POSIX */
  210. }
  211. }
  212. /*
  213. * Move the file position to a given offset from the file start.
  214. * Returns >=0 on success, <0 on failure.
  215. */
  216. int _sys_seek(FILEHANDLE fh, long pos)
  217. {
  218. #ifdef DFS_USING_POSIX
  219. if (fh < STDERR)
  220. return 0; /* error */
  221. /* position is relative to the start of file fh */
  222. return lseek(fh, pos, 0);
  223. #else
  224. return 0; /* error */
  225. #endif /* DFS_USING_POSIX */
  226. }
  227. /**
  228. * used by tmpnam() or tmpfile()
  229. */
  230. int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
  231. {
  232. rt_snprintf(name, maxlength, "tem%03d", fileno);
  233. return 1;
  234. }
  235. char *_sys_command_string(char *cmd, int len)
  236. {
  237. /* no support */
  238. return RT_NULL;
  239. }
  240. /* This function writes a character to the console. */
  241. void _ttywrch(int ch)
  242. {
  243. #ifdef RT_USING_CONSOLE
  244. char c;
  245. c = (char)ch;
  246. rt_kprintf(&c);
  247. #endif /* RT_USING_CONSOLE */
  248. }
  249. /* for exit() and abort() */
  250. RT_WEAK void _sys_exit(int return_code)
  251. {
  252. extern void __rt_libc_exit(int status);
  253. __rt_libc_exit(return_code);
  254. while(1);
  255. }
  256. /**
  257. * return current length of file.
  258. *
  259. * @param fh - file handle
  260. * @return file length, or -1 on failed
  261. */
  262. long _sys_flen(FILEHANDLE fh)
  263. {
  264. #ifdef DFS_USING_POSIX
  265. struct stat stat;
  266. if (fh < STDERR)
  267. return 0; /* error */
  268. fstat(fh, &stat);
  269. return stat.st_size;
  270. #else
  271. return 0;
  272. #endif /* DFS_USING_POSIX */
  273. }
  274. int _sys_istty(FILEHANDLE fh)
  275. {
  276. if((STDIN <= fh) && (fh <= STDERR))
  277. return 1;
  278. else
  279. return 0;
  280. }
  281. int remove(const char *filename)
  282. {
  283. #ifdef DFS_USING_POSIX
  284. return unlink(filename);
  285. #else
  286. return 0; /* error */
  287. #endif /* DFS_USING_POSIX */
  288. }
  289. #ifdef __MICROLIB
  290. #include <stdio.h>
  291. int fputc(int c, FILE *f)
  292. {
  293. #ifdef RT_USING_CONSOLE
  294. char ch[2] = {0};
  295. ch[0] = c;
  296. rt_kprintf(&ch[0]);
  297. return 1;
  298. #else
  299. return 0; /* error */
  300. #endif /* RT_USING_CONSOLE */
  301. }
  302. int fgetc(FILE *f)
  303. {
  304. #ifdef RT_USING_POSIX_DEVIO
  305. char ch;
  306. if (libc_stdio_get_console() < 0)
  307. {
  308. LOG_W("Do not invoke standard output before initializing libc");
  309. return 0;
  310. }
  311. if(read(STDIN_FILENO, &ch, 1) == 1)
  312. return ch;
  313. #endif /* RT_USING_POSIX_DEVIO */
  314. return 0; /* error */
  315. }
  316. #endif /* __MICROLIB */