os.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  4. */
  5. #include <dirent.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <getopt.h>
  9. #include <setjmp.h>
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <termios.h>
  15. #include <time.h>
  16. #include <unistd.h>
  17. #include <sys/mman.h>
  18. #include <sys/stat.h>
  19. #include <sys/time.h>
  20. #include <sys/types.h>
  21. #include <linux/types.h>
  22. #include <asm/getopt.h>
  23. #include <asm/sections.h>
  24. #include <asm/state.h>
  25. #include <os.h>
  26. #include <rtc_def.h>
  27. /* Operating System Interface */
  28. struct os_mem_hdr {
  29. size_t length; /* number of bytes in the block */
  30. };
  31. ssize_t os_read(int fd, void *buf, size_t count)
  32. {
  33. return read(fd, buf, count);
  34. }
  35. ssize_t os_read_no_block(int fd, void *buf, size_t count)
  36. {
  37. const int flags = fcntl(fd, F_GETFL, 0);
  38. fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  39. return os_read(fd, buf, count);
  40. }
  41. ssize_t os_write(int fd, const void *buf, size_t count)
  42. {
  43. return write(fd, buf, count);
  44. }
  45. off_t os_lseek(int fd, off_t offset, int whence)
  46. {
  47. if (whence == OS_SEEK_SET)
  48. whence = SEEK_SET;
  49. else if (whence == OS_SEEK_CUR)
  50. whence = SEEK_CUR;
  51. else if (whence == OS_SEEK_END)
  52. whence = SEEK_END;
  53. else
  54. os_exit(1);
  55. return lseek(fd, offset, whence);
  56. }
  57. int os_open(const char *pathname, int os_flags)
  58. {
  59. int flags;
  60. switch (os_flags & OS_O_MASK) {
  61. case OS_O_RDONLY:
  62. default:
  63. flags = O_RDONLY;
  64. break;
  65. case OS_O_WRONLY:
  66. flags = O_WRONLY;
  67. break;
  68. case OS_O_RDWR:
  69. flags = O_RDWR;
  70. break;
  71. }
  72. if (os_flags & OS_O_CREAT)
  73. flags |= O_CREAT;
  74. return open(pathname, flags, 0777);
  75. }
  76. int os_close(int fd)
  77. {
  78. return close(fd);
  79. }
  80. int os_unlink(const char *pathname)
  81. {
  82. return unlink(pathname);
  83. }
  84. void os_exit(int exit_code)
  85. {
  86. exit(exit_code);
  87. }
  88. /* Restore tty state when we exit */
  89. static struct termios orig_term;
  90. static bool term_setup;
  91. void os_fd_restore(void)
  92. {
  93. if (term_setup) {
  94. tcsetattr(0, TCSANOW, &orig_term);
  95. term_setup = false;
  96. }
  97. }
  98. /* Put tty into raw mode so <tab> and <ctrl+c> work */
  99. void os_tty_raw(int fd, bool allow_sigs)
  100. {
  101. struct termios term;
  102. if (term_setup)
  103. return;
  104. /* If not a tty, don't complain */
  105. if (tcgetattr(fd, &orig_term))
  106. return;
  107. term = orig_term;
  108. term.c_iflag = IGNBRK | IGNPAR;
  109. term.c_oflag = OPOST | ONLCR;
  110. term.c_cflag = CS8 | CREAD | CLOCAL;
  111. term.c_lflag = allow_sigs ? ISIG : 0;
  112. if (tcsetattr(fd, TCSANOW, &term))
  113. return;
  114. term_setup = true;
  115. atexit(os_fd_restore);
  116. }
  117. void *os_malloc(size_t length)
  118. {
  119. struct os_mem_hdr *hdr;
  120. int page_size = getpagesize();
  121. hdr = mmap(NULL, length + page_size,
  122. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  123. if (hdr == MAP_FAILED)
  124. return NULL;
  125. hdr->length = length;
  126. return (void *)hdr + page_size;
  127. }
  128. void os_free(void *ptr)
  129. {
  130. struct os_mem_hdr *hdr = ptr;
  131. hdr--;
  132. if (ptr)
  133. munmap(hdr, hdr->length + sizeof(*hdr));
  134. }
  135. void *os_realloc(void *ptr, size_t length)
  136. {
  137. struct os_mem_hdr *hdr = ptr;
  138. void *buf = NULL;
  139. hdr--;
  140. if (length != 0) {
  141. buf = os_malloc(length);
  142. if (!buf)
  143. return buf;
  144. if (ptr) {
  145. if (length > hdr->length)
  146. length = hdr->length;
  147. memcpy(buf, ptr, length);
  148. }
  149. }
  150. os_free(ptr);
  151. return buf;
  152. }
  153. void os_usleep(unsigned long usec)
  154. {
  155. usleep(usec);
  156. }
  157. uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
  158. {
  159. #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
  160. struct timespec tp;
  161. if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
  162. struct timeval tv;
  163. gettimeofday(&tv, NULL);
  164. tp.tv_sec = tv.tv_sec;
  165. tp.tv_nsec = tv.tv_usec * 1000;
  166. }
  167. return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
  168. #else
  169. struct timeval tv;
  170. gettimeofday(&tv, NULL);
  171. return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
  172. #endif
  173. }
  174. static char *short_opts;
  175. static struct option *long_opts;
  176. int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
  177. {
  178. struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
  179. size_t num_options = __u_boot_sandbox_option_count();
  180. size_t i;
  181. int hidden_short_opt;
  182. size_t si;
  183. int c;
  184. if (short_opts || long_opts)
  185. return 1;
  186. state->argc = argc;
  187. state->argv = argv;
  188. /* dynamically construct the arguments to the system getopt_long */
  189. short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
  190. long_opts = os_malloc(sizeof(*long_opts) * num_options);
  191. if (!short_opts || !long_opts)
  192. return 1;
  193. /*
  194. * getopt_long requires "val" to be unique (since that is what the
  195. * func returns), so generate unique values automatically for flags
  196. * that don't have a short option. pick 0x100 as that is above the
  197. * single byte range (where ASCII/ISO-XXXX-X charsets live).
  198. */
  199. hidden_short_opt = 0x100;
  200. si = 0;
  201. for (i = 0; i < num_options; ++i) {
  202. long_opts[i].name = sb_opt[i]->flag;
  203. long_opts[i].has_arg = sb_opt[i]->has_arg ?
  204. required_argument : no_argument;
  205. long_opts[i].flag = NULL;
  206. if (sb_opt[i]->flag_short) {
  207. short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
  208. if (long_opts[i].has_arg == required_argument)
  209. short_opts[si++] = ':';
  210. } else
  211. long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
  212. }
  213. short_opts[si] = '\0';
  214. /* we need to handle output ourselves since u-boot provides printf */
  215. opterr = 0;
  216. /*
  217. * walk all of the options the user gave us on the command line,
  218. * figure out what u-boot option structure they belong to (via
  219. * the unique short val key), and call the appropriate callback.
  220. */
  221. while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
  222. for (i = 0; i < num_options; ++i) {
  223. if (sb_opt[i]->flag_short == c) {
  224. if (sb_opt[i]->callback(state, optarg)) {
  225. state->parse_err = sb_opt[i]->flag;
  226. return 0;
  227. }
  228. break;
  229. }
  230. }
  231. if (i == num_options) {
  232. /*
  233. * store the faulting flag for later display. we have to
  234. * store the flag itself as the getopt parsing itself is
  235. * tricky: need to handle the following flags (assume all
  236. * of the below are unknown):
  237. * -a optopt='a' optind=<next>
  238. * -abbbb optopt='a' optind=<this>
  239. * -aaaaa optopt='a' optind=<this>
  240. * --a optopt=0 optind=<this>
  241. * as you can see, it is impossible to determine the exact
  242. * faulting flag without doing the parsing ourselves, so
  243. * we just report the specific flag that failed.
  244. */
  245. if (optopt) {
  246. static char parse_err[3] = { '-', 0, '\0', };
  247. parse_err[1] = optopt;
  248. state->parse_err = parse_err;
  249. } else
  250. state->parse_err = argv[optind - 1];
  251. break;
  252. }
  253. }
  254. return 0;
  255. }
  256. void os_dirent_free(struct os_dirent_node *node)
  257. {
  258. struct os_dirent_node *next;
  259. while (node) {
  260. next = node->next;
  261. free(node);
  262. node = next;
  263. }
  264. }
  265. int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
  266. {
  267. struct dirent *entry;
  268. struct os_dirent_node *head, *node, *next;
  269. struct stat buf;
  270. DIR *dir;
  271. int ret;
  272. char *fname;
  273. char *old_fname;
  274. int len;
  275. int dirlen;
  276. *headp = NULL;
  277. dir = opendir(dirname);
  278. if (!dir)
  279. return -1;
  280. /* Create a buffer upfront, with typically sufficient size */
  281. dirlen = strlen(dirname) + 2;
  282. len = dirlen + 256;
  283. fname = malloc(len);
  284. if (!fname) {
  285. ret = -ENOMEM;
  286. goto done;
  287. }
  288. for (node = head = NULL;; node = next) {
  289. errno = 0;
  290. entry = readdir(dir);
  291. if (!entry) {
  292. ret = errno;
  293. break;
  294. }
  295. next = malloc(sizeof(*node) + strlen(entry->d_name) + 1);
  296. if (!next) {
  297. os_dirent_free(head);
  298. ret = -ENOMEM;
  299. goto done;
  300. }
  301. if (dirlen + strlen(entry->d_name) > len) {
  302. len = dirlen + strlen(entry->d_name);
  303. old_fname = fname;
  304. fname = realloc(fname, len);
  305. if (!fname) {
  306. free(old_fname);
  307. free(next);
  308. os_dirent_free(head);
  309. ret = -ENOMEM;
  310. goto done;
  311. }
  312. }
  313. next->next = NULL;
  314. strcpy(next->name, entry->d_name);
  315. switch (entry->d_type) {
  316. case DT_REG:
  317. next->type = OS_FILET_REG;
  318. break;
  319. case DT_DIR:
  320. next->type = OS_FILET_DIR;
  321. break;
  322. case DT_LNK:
  323. next->type = OS_FILET_LNK;
  324. break;
  325. default:
  326. next->type = OS_FILET_UNKNOWN;
  327. }
  328. next->size = 0;
  329. snprintf(fname, len, "%s/%s", dirname, next->name);
  330. if (!stat(fname, &buf))
  331. next->size = buf.st_size;
  332. if (node)
  333. node->next = next;
  334. else
  335. head = next;
  336. }
  337. *headp = head;
  338. done:
  339. closedir(dir);
  340. free(fname);
  341. return ret;
  342. }
  343. const char *os_dirent_typename[OS_FILET_COUNT] = {
  344. " ",
  345. "SYM",
  346. "DIR",
  347. "???",
  348. };
  349. const char *os_dirent_get_typename(enum os_dirent_t type)
  350. {
  351. if (type >= OS_FILET_REG && type < OS_FILET_COUNT)
  352. return os_dirent_typename[type];
  353. return os_dirent_typename[OS_FILET_UNKNOWN];
  354. }
  355. int os_get_filesize(const char *fname, loff_t *size)
  356. {
  357. struct stat buf;
  358. int ret;
  359. ret = stat(fname, &buf);
  360. if (ret)
  361. return ret;
  362. *size = buf.st_size;
  363. return 0;
  364. }
  365. void os_putc(int ch)
  366. {
  367. putchar(ch);
  368. }
  369. void os_puts(const char *str)
  370. {
  371. while (*str)
  372. os_putc(*str++);
  373. }
  374. int os_write_ram_buf(const char *fname)
  375. {
  376. struct sandbox_state *state = state_get_current();
  377. int fd, ret;
  378. fd = open(fname, O_CREAT | O_WRONLY, 0777);
  379. if (fd < 0)
  380. return -ENOENT;
  381. ret = write(fd, state->ram_buf, state->ram_size);
  382. close(fd);
  383. if (ret != state->ram_size)
  384. return -EIO;
  385. return 0;
  386. }
  387. int os_read_ram_buf(const char *fname)
  388. {
  389. struct sandbox_state *state = state_get_current();
  390. int fd, ret;
  391. loff_t size;
  392. ret = os_get_filesize(fname, &size);
  393. if (ret < 0)
  394. return ret;
  395. if (size != state->ram_size)
  396. return -ENOSPC;
  397. fd = open(fname, O_RDONLY);
  398. if (fd < 0)
  399. return -ENOENT;
  400. ret = read(fd, state->ram_buf, state->ram_size);
  401. close(fd);
  402. if (ret != state->ram_size)
  403. return -EIO;
  404. return 0;
  405. }
  406. static int make_exec(char *fname, const void *data, int size)
  407. {
  408. int fd;
  409. strcpy(fname, "/tmp/u-boot.jump.XXXXXX");
  410. fd = mkstemp(fname);
  411. if (fd < 0)
  412. return -ENOENT;
  413. if (write(fd, data, size) < 0)
  414. return -EIO;
  415. close(fd);
  416. if (chmod(fname, 0777))
  417. return -ENOEXEC;
  418. return 0;
  419. }
  420. static int add_args(char ***argvp, const char *add_args[], int count)
  421. {
  422. char **argv;
  423. int argc;
  424. for (argv = *argvp, argc = 0; (*argvp)[argc]; argc++)
  425. ;
  426. argv = malloc((argc + count + 1) * sizeof(char *));
  427. if (!argv) {
  428. printf("Out of memory for %d argv\n", count);
  429. return -ENOMEM;
  430. }
  431. memcpy(argv, *argvp, argc * sizeof(char *));
  432. memcpy(argv + argc, add_args, count * sizeof(char *));
  433. argv[argc + count] = NULL;
  434. *argvp = argv;
  435. return 0;
  436. }
  437. int os_jump_to_image(const void *dest, int size)
  438. {
  439. struct sandbox_state *state = state_get_current();
  440. char fname[30], mem_fname[30];
  441. int fd, err;
  442. const char *extra_args[5];
  443. char **argv = state->argv;
  444. #ifdef DEBUG
  445. int argc, i;
  446. #endif
  447. err = make_exec(fname, dest, size);
  448. if (err)
  449. return err;
  450. strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
  451. fd = mkstemp(mem_fname);
  452. if (fd < 0)
  453. return -ENOENT;
  454. close(fd);
  455. err = os_write_ram_buf(mem_fname);
  456. if (err)
  457. return err;
  458. os_fd_restore();
  459. extra_args[0] = "-j";
  460. extra_args[1] = fname;
  461. extra_args[2] = "-m";
  462. extra_args[3] = mem_fname;
  463. extra_args[4] = "--rm_memory";
  464. err = add_args(&argv, extra_args,
  465. sizeof(extra_args) / sizeof(extra_args[0]));
  466. if (err)
  467. return err;
  468. #ifdef DEBUG
  469. for (i = 0; argv[i]; i++)
  470. printf("%d %s\n", i, argv[i]);
  471. #endif
  472. if (state_uninit())
  473. os_exit(2);
  474. err = execv(fname, argv);
  475. free(argv);
  476. if (err)
  477. return err;
  478. return unlink(fname);
  479. }
  480. int os_find_u_boot(char *fname, int maxlen)
  481. {
  482. struct sandbox_state *state = state_get_current();
  483. const char *progname = state->argv[0];
  484. int len = strlen(progname);
  485. char *p;
  486. int fd;
  487. if (len >= maxlen || len < 4)
  488. return -ENOSPC;
  489. /* Look for 'u-boot' in the same directory as 'u-boot-spl' */
  490. strcpy(fname, progname);
  491. if (!strcmp(fname + len - 4, "-spl")) {
  492. fname[len - 4] = '\0';
  493. fd = os_open(fname, O_RDONLY);
  494. if (fd >= 0) {
  495. close(fd);
  496. return 0;
  497. }
  498. }
  499. /* Look for 'u-boot' in the parent directory of spl/ */
  500. p = strstr(fname, "/spl/");
  501. if (p) {
  502. strcpy(p, p + 4);
  503. fd = os_open(fname, O_RDONLY);
  504. if (fd >= 0) {
  505. close(fd);
  506. return 0;
  507. }
  508. }
  509. return -ENOENT;
  510. }
  511. int os_spl_to_uboot(const char *fname)
  512. {
  513. struct sandbox_state *state = state_get_current();
  514. char *argv[state->argc + 1];
  515. int ret;
  516. memcpy(argv, state->argv, sizeof(char *) * (state->argc + 1));
  517. argv[0] = (char *)fname;
  518. ret = execv(fname, argv);
  519. if (ret)
  520. return ret;
  521. return unlink(fname);
  522. }
  523. void os_localtime(struct rtc_time *rt)
  524. {
  525. time_t t = time(NULL);
  526. struct tm *tm;
  527. tm = localtime(&t);
  528. rt->tm_sec = tm->tm_sec;
  529. rt->tm_min = tm->tm_min;
  530. rt->tm_hour = tm->tm_hour;
  531. rt->tm_mday = tm->tm_mday;
  532. rt->tm_mon = tm->tm_mon + 1;
  533. rt->tm_year = tm->tm_year + 1900;
  534. rt->tm_wday = tm->tm_wday;
  535. rt->tm_yday = tm->tm_yday;
  536. rt->tm_isdst = tm->tm_isdst;
  537. }
  538. void os_abort(void)
  539. {
  540. abort();
  541. }