os.c 14 KB

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