os.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. int os_unlink(const char *pathname)
  79. {
  80. return unlink(pathname);
  81. }
  82. void os_exit(int exit_code)
  83. {
  84. exit(exit_code);
  85. }
  86. /* Restore tty state when we exit */
  87. static struct termios orig_term;
  88. static bool term_setup;
  89. static void os_fd_restore(void)
  90. {
  91. if (term_setup)
  92. tcsetattr(0, TCSANOW, &orig_term);
  93. }
  94. /* Put tty into raw mode so <tab> and <ctrl+c> work */
  95. void os_tty_raw(int fd, bool allow_sigs)
  96. {
  97. struct termios term;
  98. if (term_setup)
  99. return;
  100. term_setup = true;
  101. /* If not a tty, don't complain */
  102. if (tcgetattr(fd, &orig_term))
  103. return;
  104. term = orig_term;
  105. term.c_iflag = IGNBRK | IGNPAR;
  106. term.c_oflag = OPOST | ONLCR;
  107. term.c_cflag = CS8 | CREAD | CLOCAL;
  108. term.c_lflag = allow_sigs ? ISIG : 0;
  109. if (tcsetattr(fd, TCSANOW, &term))
  110. return;
  111. atexit(os_fd_restore);
  112. }
  113. void *os_malloc(size_t length)
  114. {
  115. struct os_mem_hdr *hdr;
  116. hdr = mmap(NULL, length + sizeof(*hdr), PROT_READ | PROT_WRITE,
  117. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  118. if (hdr == MAP_FAILED)
  119. return NULL;
  120. hdr->length = length;
  121. return hdr + 1;
  122. }
  123. void os_free(void *ptr)
  124. {
  125. struct os_mem_hdr *hdr = ptr;
  126. hdr--;
  127. if (ptr)
  128. munmap(hdr, hdr->length + sizeof(*hdr));
  129. }
  130. void *os_realloc(void *ptr, size_t length)
  131. {
  132. struct os_mem_hdr *hdr = ptr;
  133. void *buf = NULL;
  134. hdr--;
  135. if (length != 0) {
  136. buf = os_malloc(length);
  137. if (!buf)
  138. return buf;
  139. if (ptr) {
  140. if (length > hdr->length)
  141. length = hdr->length;
  142. memcpy(buf, ptr, length);
  143. }
  144. }
  145. os_free(ptr);
  146. return buf;
  147. }
  148. void os_usleep(unsigned long usec)
  149. {
  150. usleep(usec);
  151. }
  152. uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
  153. {
  154. #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
  155. struct timespec tp;
  156. if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
  157. struct timeval tv;
  158. gettimeofday(&tv, NULL);
  159. tp.tv_sec = tv.tv_sec;
  160. tp.tv_nsec = tv.tv_usec * 1000;
  161. }
  162. return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
  163. #else
  164. struct timeval tv;
  165. gettimeofday(&tv, NULL);
  166. return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
  167. #endif
  168. }
  169. static char *short_opts;
  170. static struct option *long_opts;
  171. int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
  172. {
  173. struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
  174. size_t num_options = __u_boot_sandbox_option_count();
  175. size_t i;
  176. int hidden_short_opt;
  177. size_t si;
  178. int c;
  179. if (short_opts || long_opts)
  180. return 1;
  181. state->argc = argc;
  182. state->argv = argv;
  183. /* dynamically construct the arguments to the system getopt_long */
  184. short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
  185. long_opts = os_malloc(sizeof(*long_opts) * num_options);
  186. if (!short_opts || !long_opts)
  187. return 1;
  188. /*
  189. * getopt_long requires "val" to be unique (since that is what the
  190. * func returns), so generate unique values automatically for flags
  191. * that don't have a short option. pick 0x100 as that is above the
  192. * single byte range (where ASCII/ISO-XXXX-X charsets live).
  193. */
  194. hidden_short_opt = 0x100;
  195. si = 0;
  196. for (i = 0; i < num_options; ++i) {
  197. long_opts[i].name = sb_opt[i]->flag;
  198. long_opts[i].has_arg = sb_opt[i]->has_arg ?
  199. required_argument : no_argument;
  200. long_opts[i].flag = NULL;
  201. if (sb_opt[i]->flag_short) {
  202. short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
  203. if (long_opts[i].has_arg == required_argument)
  204. short_opts[si++] = ':';
  205. } else
  206. long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
  207. }
  208. short_opts[si] = '\0';
  209. /* we need to handle output ourselves since u-boot provides printf */
  210. opterr = 0;
  211. /*
  212. * walk all of the options the user gave us on the command line,
  213. * figure out what u-boot option structure they belong to (via
  214. * the unique short val key), and call the appropriate callback.
  215. */
  216. while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
  217. for (i = 0; i < num_options; ++i) {
  218. if (sb_opt[i]->flag_short == c) {
  219. if (sb_opt[i]->callback(state, optarg)) {
  220. state->parse_err = sb_opt[i]->flag;
  221. return 0;
  222. }
  223. break;
  224. }
  225. }
  226. if (i == num_options) {
  227. /*
  228. * store the faulting flag for later display. we have to
  229. * store the flag itself as the getopt parsing itself is
  230. * tricky: need to handle the following flags (assume all
  231. * of the below are unknown):
  232. * -a optopt='a' optind=<next>
  233. * -abbbb optopt='a' optind=<this>
  234. * -aaaaa optopt='a' optind=<this>
  235. * --a optopt=0 optind=<this>
  236. * as you can see, it is impossible to determine the exact
  237. * faulting flag without doing the parsing ourselves, so
  238. * we just report the specific flag that failed.
  239. */
  240. if (optopt) {
  241. static char parse_err[3] = { '-', 0, '\0', };
  242. parse_err[1] = optopt;
  243. state->parse_err = parse_err;
  244. } else
  245. state->parse_err = argv[optind - 1];
  246. break;
  247. }
  248. }
  249. return 0;
  250. }
  251. void os_dirent_free(struct os_dirent_node *node)
  252. {
  253. struct os_dirent_node *next;
  254. while (node) {
  255. next = node->next;
  256. free(node);
  257. node = next;
  258. }
  259. }
  260. int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
  261. {
  262. struct dirent entry, *result;
  263. struct os_dirent_node *head, *node, *next;
  264. struct stat buf;
  265. DIR *dir;
  266. int ret;
  267. char *fname;
  268. int len;
  269. *headp = NULL;
  270. dir = opendir(dirname);
  271. if (!dir)
  272. return -1;
  273. /* Create a buffer for the maximum filename length */
  274. len = sizeof(entry.d_name) + strlen(dirname) + 2;
  275. fname = malloc(len);
  276. if (!fname) {
  277. ret = -ENOMEM;
  278. goto done;
  279. }
  280. for (node = head = NULL;; node = next) {
  281. ret = readdir_r(dir, &entry, &result);
  282. if (ret || !result)
  283. break;
  284. next = malloc(sizeof(*node) + strlen(entry.d_name) + 1);
  285. if (!next) {
  286. os_dirent_free(head);
  287. ret = -ENOMEM;
  288. goto done;
  289. }
  290. strcpy(next->name, entry.d_name);
  291. switch (entry.d_type) {
  292. case DT_REG:
  293. next->type = OS_FILET_REG;
  294. break;
  295. case DT_DIR:
  296. next->type = OS_FILET_DIR;
  297. break;
  298. case DT_LNK:
  299. next->type = OS_FILET_LNK;
  300. break;
  301. }
  302. next->size = 0;
  303. snprintf(fname, len, "%s/%s", dirname, next->name);
  304. if (!stat(fname, &buf))
  305. next->size = buf.st_size;
  306. if (node)
  307. node->next = next;
  308. if (!head)
  309. head = node;
  310. }
  311. *headp = head;
  312. done:
  313. closedir(dir);
  314. return ret;
  315. }
  316. const char *os_dirent_typename[OS_FILET_COUNT] = {
  317. " ",
  318. "SYM",
  319. "DIR",
  320. "???",
  321. };
  322. const char *os_dirent_get_typename(enum os_dirent_t type)
  323. {
  324. if (type >= 0 && type < OS_FILET_COUNT)
  325. return os_dirent_typename[type];
  326. return os_dirent_typename[OS_FILET_UNKNOWN];
  327. }
  328. ssize_t os_get_filesize(const char *fname)
  329. {
  330. struct stat buf;
  331. int ret;
  332. ret = stat(fname, &buf);
  333. if (ret)
  334. return ret;
  335. return buf.st_size;
  336. }
  337. void os_putc(int ch)
  338. {
  339. putchar(ch);
  340. }
  341. void os_puts(const char *str)
  342. {
  343. while (*str)
  344. os_putc(*str++);
  345. }
  346. int os_write_ram_buf(const char *fname)
  347. {
  348. struct sandbox_state *state = state_get_current();
  349. int fd, ret;
  350. fd = open(fname, O_CREAT | O_WRONLY, 0777);
  351. if (fd < 0)
  352. return -ENOENT;
  353. ret = write(fd, state->ram_buf, state->ram_size);
  354. close(fd);
  355. if (ret != state->ram_size)
  356. return -EIO;
  357. return 0;
  358. }
  359. int os_read_ram_buf(const char *fname)
  360. {
  361. struct sandbox_state *state = state_get_current();
  362. int fd, ret;
  363. int size;
  364. size = os_get_filesize(fname);
  365. if (size < 0)
  366. return -ENOENT;
  367. if (size != state->ram_size)
  368. return -ENOSPC;
  369. fd = open(fname, O_RDONLY);
  370. if (fd < 0)
  371. return -ENOENT;
  372. ret = read(fd, state->ram_buf, state->ram_size);
  373. close(fd);
  374. if (ret != state->ram_size)
  375. return -EIO;
  376. return 0;
  377. }
  378. static int make_exec(char *fname, const void *data, int size)
  379. {
  380. int fd;
  381. strcpy(fname, "/tmp/u-boot.jump.XXXXXX");
  382. fd = mkstemp(fname);
  383. if (fd < 0)
  384. return -ENOENT;
  385. if (write(fd, data, size) < 0)
  386. return -EIO;
  387. close(fd);
  388. if (chmod(fname, 0777))
  389. return -ENOEXEC;
  390. return 0;
  391. }
  392. static int add_args(char ***argvp, const char *add_args[], int count)
  393. {
  394. char **argv;
  395. int argc;
  396. for (argv = *argvp, argc = 0; (*argvp)[argc]; argc++)
  397. ;
  398. argv = malloc((argc + count + 1) * sizeof(char *));
  399. if (!argv) {
  400. printf("Out of memory for %d argv\n", count);
  401. return -ENOMEM;
  402. }
  403. memcpy(argv, *argvp, argc * sizeof(char *));
  404. memcpy(argv + argc, add_args, count * sizeof(char *));
  405. argv[argc + count] = NULL;
  406. *argvp = argv;
  407. return 0;
  408. }
  409. int os_jump_to_image(const void *dest, int size)
  410. {
  411. struct sandbox_state *state = state_get_current();
  412. char fname[30], mem_fname[30];
  413. int fd, err;
  414. const char *extra_args[5];
  415. char **argv = state->argv;
  416. #ifdef DEBUG
  417. int argc, i;
  418. #endif
  419. err = make_exec(fname, dest, size);
  420. if (err)
  421. return err;
  422. strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
  423. fd = mkstemp(mem_fname);
  424. if (fd < 0)
  425. return -ENOENT;
  426. close(fd);
  427. err = os_write_ram_buf(mem_fname);
  428. if (err)
  429. return err;
  430. os_fd_restore();
  431. extra_args[0] = "-j";
  432. extra_args[1] = fname;
  433. extra_args[2] = "-m";
  434. extra_args[3] = mem_fname;
  435. extra_args[4] = "--rm_memory";
  436. err = add_args(&argv, extra_args,
  437. sizeof(extra_args) / sizeof(extra_args[0]));
  438. if (err)
  439. return err;
  440. #ifdef DEBUG
  441. for (i = 0; argv[i]; i++)
  442. printf("%d %s\n", i, argv[i]);
  443. #endif
  444. if (state_uninit())
  445. os_exit(2);
  446. err = execv(fname, argv);
  447. free(argv);
  448. if (err)
  449. return err;
  450. return unlink(fname);
  451. }