os.c 7.9 KB

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