os.c 14 KB

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