os.c 14 KB

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