os.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * SPDX-License-Identifier: GPL-2.0+
  4. */
  5. #include <dirent.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <getopt.h>
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <termios.h>
  14. #include <time.h>
  15. #include <unistd.h>
  16. #include <sys/mman.h>
  17. #include <sys/stat.h>
  18. #include <sys/time.h>
  19. #include <sys/types.h>
  20. #include <linux/types.h>
  21. #include <asm/getopt.h>
  22. #include <asm/sections.h>
  23. #include <asm/state.h>
  24. #include <os.h>
  25. /* Operating System Interface */
  26. ssize_t os_read(int fd, void *buf, size_t count)
  27. {
  28. return read(fd, buf, count);
  29. }
  30. ssize_t os_read_no_block(int fd, void *buf, size_t count)
  31. {
  32. const int flags = fcntl(fd, F_GETFL, 0);
  33. fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  34. return os_read(fd, buf, count);
  35. }
  36. ssize_t os_write(int fd, const void *buf, size_t count)
  37. {
  38. return write(fd, buf, count);
  39. }
  40. off_t os_lseek(int fd, off_t offset, int whence)
  41. {
  42. if (whence == OS_SEEK_SET)
  43. whence = SEEK_SET;
  44. else if (whence == OS_SEEK_CUR)
  45. whence = SEEK_CUR;
  46. else if (whence == OS_SEEK_END)
  47. whence = SEEK_END;
  48. else
  49. os_exit(1);
  50. return lseek(fd, offset, whence);
  51. }
  52. int os_open(const char *pathname, int os_flags)
  53. {
  54. int flags;
  55. switch (os_flags & OS_O_MASK) {
  56. case OS_O_RDONLY:
  57. default:
  58. flags = O_RDONLY;
  59. break;
  60. case OS_O_WRONLY:
  61. flags = O_WRONLY;
  62. break;
  63. case OS_O_RDWR:
  64. flags = O_RDWR;
  65. break;
  66. }
  67. if (os_flags & OS_O_CREAT)
  68. flags |= O_CREAT;
  69. return open(pathname, flags, 0777);
  70. }
  71. int os_close(int fd)
  72. {
  73. return close(fd);
  74. }
  75. void os_exit(int exit_code)
  76. {
  77. exit(exit_code);
  78. }
  79. /* Restore tty state when we exit */
  80. static struct termios orig_term;
  81. static void os_fd_restore(void)
  82. {
  83. tcsetattr(0, TCSANOW, &orig_term);
  84. }
  85. /* Put tty into raw mode so <tab> and <ctrl+c> work */
  86. void os_tty_raw(int fd)
  87. {
  88. static int setup = 0;
  89. struct termios term;
  90. if (setup)
  91. return;
  92. setup = 1;
  93. /* If not a tty, don't complain */
  94. if (tcgetattr(fd, &orig_term))
  95. return;
  96. term = orig_term;
  97. term.c_iflag = IGNBRK | IGNPAR;
  98. term.c_oflag = OPOST | ONLCR;
  99. term.c_cflag = CS8 | CREAD | CLOCAL;
  100. term.c_lflag = 0;
  101. if (tcsetattr(fd, TCSANOW, &term))
  102. return;
  103. atexit(os_fd_restore);
  104. }
  105. void *os_malloc(size_t length)
  106. {
  107. return mmap(NULL, length, PROT_READ | PROT_WRITE,
  108. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  109. }
  110. void os_usleep(unsigned long usec)
  111. {
  112. usleep(usec);
  113. }
  114. uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
  115. {
  116. #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
  117. struct timespec tp;
  118. if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
  119. struct timeval tv;
  120. gettimeofday(&tv, NULL);
  121. tp.tv_sec = tv.tv_sec;
  122. tp.tv_nsec = tv.tv_usec * 1000;
  123. }
  124. return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
  125. #else
  126. struct timeval tv;
  127. gettimeofday(&tv, NULL);
  128. return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
  129. #endif
  130. }
  131. static char *short_opts;
  132. static struct option *long_opts;
  133. int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
  134. {
  135. struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
  136. size_t num_options = __u_boot_sandbox_option_count();
  137. size_t i;
  138. int hidden_short_opt;
  139. size_t si;
  140. int c;
  141. if (short_opts || long_opts)
  142. return 1;
  143. state->argc = argc;
  144. state->argv = argv;
  145. /* dynamically construct the arguments to the system getopt_long */
  146. short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
  147. long_opts = os_malloc(sizeof(*long_opts) * num_options);
  148. if (!short_opts || !long_opts)
  149. return 1;
  150. /*
  151. * getopt_long requires "val" to be unique (since that is what the
  152. * func returns), so generate unique values automatically for flags
  153. * that don't have a short option. pick 0x100 as that is above the
  154. * single byte range (where ASCII/ISO-XXXX-X charsets live).
  155. */
  156. hidden_short_opt = 0x100;
  157. si = 0;
  158. for (i = 0; i < num_options; ++i) {
  159. long_opts[i].name = sb_opt[i]->flag;
  160. long_opts[i].has_arg = sb_opt[i]->has_arg ?
  161. required_argument : no_argument;
  162. long_opts[i].flag = NULL;
  163. if (sb_opt[i]->flag_short) {
  164. short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
  165. if (long_opts[i].has_arg == required_argument)
  166. short_opts[si++] = ':';
  167. } else
  168. long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
  169. }
  170. short_opts[si] = '\0';
  171. /* we need to handle output ourselves since u-boot provides printf */
  172. opterr = 0;
  173. /*
  174. * walk all of the options the user gave us on the command line,
  175. * figure out what u-boot option structure they belong to (via
  176. * the unique short val key), and call the appropriate callback.
  177. */
  178. while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
  179. for (i = 0; i < num_options; ++i) {
  180. if (sb_opt[i]->flag_short == c) {
  181. if (sb_opt[i]->callback(state, optarg)) {
  182. state->parse_err = sb_opt[i]->flag;
  183. return 0;
  184. }
  185. break;
  186. }
  187. }
  188. if (i == num_options) {
  189. /*
  190. * store the faulting flag for later display. we have to
  191. * store the flag itself as the getopt parsing itself is
  192. * tricky: need to handle the following flags (assume all
  193. * of the below are unknown):
  194. * -a optopt='a' optind=<next>
  195. * -abbbb optopt='a' optind=<this>
  196. * -aaaaa optopt='a' optind=<this>
  197. * --a optopt=0 optind=<this>
  198. * as you can see, it is impossible to determine the exact
  199. * faulting flag without doing the parsing ourselves, so
  200. * we just report the specific flag that failed.
  201. */
  202. if (optopt) {
  203. static char parse_err[3] = { '-', 0, '\0', };
  204. parse_err[1] = optopt;
  205. state->parse_err = parse_err;
  206. } else
  207. state->parse_err = argv[optind - 1];
  208. break;
  209. }
  210. }
  211. return 0;
  212. }
  213. void os_dirent_free(struct os_dirent_node *node)
  214. {
  215. struct os_dirent_node *next;
  216. while (node) {
  217. next = node->next;
  218. free(node);
  219. node = next;
  220. }
  221. }
  222. int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
  223. {
  224. struct dirent entry, *result;
  225. struct os_dirent_node *head, *node, *next;
  226. struct stat buf;
  227. DIR *dir;
  228. int ret;
  229. char *fname;
  230. int len;
  231. *headp = NULL;
  232. dir = opendir(dirname);
  233. if (!dir)
  234. return -1;
  235. /* Create a buffer for the maximum filename length */
  236. len = sizeof(entry.d_name) + strlen(dirname) + 2;
  237. fname = malloc(len);
  238. if (!fname) {
  239. ret = -ENOMEM;
  240. goto done;
  241. }
  242. for (node = head = NULL;; node = next) {
  243. ret = readdir_r(dir, &entry, &result);
  244. if (ret || !result)
  245. break;
  246. next = malloc(sizeof(*node) + strlen(entry.d_name) + 1);
  247. if (!next) {
  248. os_dirent_free(head);
  249. ret = -ENOMEM;
  250. goto done;
  251. }
  252. strcpy(next->name, entry.d_name);
  253. switch (entry.d_type) {
  254. case DT_REG:
  255. next->type = OS_FILET_REG;
  256. break;
  257. case DT_DIR:
  258. next->type = OS_FILET_DIR;
  259. break;
  260. case DT_LNK:
  261. next->type = OS_FILET_LNK;
  262. break;
  263. }
  264. next->size = 0;
  265. snprintf(fname, len, "%s/%s", dirname, next->name);
  266. if (!stat(fname, &buf))
  267. next->size = buf.st_size;
  268. if (node)
  269. node->next = next;
  270. if (!head)
  271. head = node;
  272. }
  273. *headp = head;
  274. done:
  275. closedir(dir);
  276. return ret;
  277. }
  278. const char *os_dirent_typename[OS_FILET_COUNT] = {
  279. " ",
  280. "SYM",
  281. "DIR",
  282. "???",
  283. };
  284. const char *os_dirent_get_typename(enum os_dirent_t type)
  285. {
  286. if (type >= 0 && type < OS_FILET_COUNT)
  287. return os_dirent_typename[type];
  288. return os_dirent_typename[OS_FILET_UNKNOWN];
  289. }
  290. ssize_t os_get_filesize(const char *fname)
  291. {
  292. struct stat buf;
  293. int ret;
  294. ret = stat(fname, &buf);
  295. if (ret)
  296. return ret;
  297. return buf.st_size;
  298. }