os.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  4. */
  5. #define _GNU_SOURCE
  6. #include <dirent.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <getopt.h>
  10. #include <setjmp.h>
  11. #include <signal.h>
  12. #include <stdio.h>
  13. #include <stdint.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <termios.h>
  17. #include <time.h>
  18. #include <ucontext.h>
  19. #include <unistd.h>
  20. #include <sys/mman.h>
  21. #include <sys/stat.h>
  22. #include <sys/time.h>
  23. #include <sys/types.h>
  24. #include <linux/compiler_attributes.h>
  25. #include <linux/types.h>
  26. #include <asm/getopt.h>
  27. #include <asm/sections.h>
  28. #include <asm/state.h>
  29. #include <os.h>
  30. #include <rtc_def.h>
  31. /* Environment variable for time offset */
  32. #define ENV_TIME_OFFSET "UBOOT_SB_TIME_OFFSET"
  33. /* Operating System Interface */
  34. struct os_mem_hdr {
  35. size_t length; /* number of bytes in the block */
  36. };
  37. ssize_t os_read(int fd, void *buf, size_t count)
  38. {
  39. return 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. /*
  77. * During a cold reset execv() is used to relaunch the U-Boot binary.
  78. * We must ensure that all files are closed in this case.
  79. */
  80. flags |= O_CLOEXEC;
  81. return open(pathname, flags, 0777);
  82. }
  83. int os_close(int fd)
  84. {
  85. /* Do not close the console input */
  86. if (fd)
  87. return close(fd);
  88. return -1;
  89. }
  90. int os_unlink(const char *pathname)
  91. {
  92. return unlink(pathname);
  93. }
  94. void os_exit(int exit_code)
  95. {
  96. exit(exit_code);
  97. }
  98. int os_write_file(const char *fname, const void *buf, int size)
  99. {
  100. int fd;
  101. fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC);
  102. if (fd < 0) {
  103. printf("Cannot open file '%s'\n", fname);
  104. return -EIO;
  105. }
  106. if (os_write(fd, buf, size) != size) {
  107. printf("Cannot write to file '%s'\n", fname);
  108. os_close(fd);
  109. return -EIO;
  110. }
  111. os_close(fd);
  112. return 0;
  113. }
  114. int os_read_file(const char *fname, void **bufp, int *sizep)
  115. {
  116. off_t size;
  117. int ret = -EIO;
  118. int fd;
  119. fd = os_open(fname, OS_O_RDONLY);
  120. if (fd < 0) {
  121. printf("Cannot open file '%s'\n", fname);
  122. goto err;
  123. }
  124. size = os_lseek(fd, 0, OS_SEEK_END);
  125. if (size < 0) {
  126. printf("Cannot seek to end of file '%s'\n", fname);
  127. goto err;
  128. }
  129. if (os_lseek(fd, 0, OS_SEEK_SET) < 0) {
  130. printf("Cannot seek to start of file '%s'\n", fname);
  131. goto err;
  132. }
  133. *bufp = os_malloc(size);
  134. if (!*bufp) {
  135. printf("Not enough memory to read file '%s'\n", fname);
  136. ret = -ENOMEM;
  137. goto err;
  138. }
  139. if (os_read(fd, *bufp, size) != size) {
  140. printf("Cannot read from file '%s'\n", fname);
  141. goto err;
  142. }
  143. os_close(fd);
  144. *sizep = size;
  145. return 0;
  146. err:
  147. os_close(fd);
  148. return ret;
  149. }
  150. /* Restore tty state when we exit */
  151. static struct termios orig_term;
  152. static bool term_setup;
  153. static bool term_nonblock;
  154. void os_fd_restore(void)
  155. {
  156. if (term_setup) {
  157. int flags;
  158. tcsetattr(0, TCSANOW, &orig_term);
  159. if (term_nonblock) {
  160. flags = fcntl(0, F_GETFL, 0);
  161. fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
  162. }
  163. term_setup = false;
  164. }
  165. }
  166. static void os_sigint_handler(int sig)
  167. {
  168. os_fd_restore();
  169. signal(SIGINT, SIG_DFL);
  170. raise(SIGINT);
  171. }
  172. static void os_signal_handler(int sig, siginfo_t *info, void *con)
  173. {
  174. ucontext_t __maybe_unused *context = con;
  175. unsigned long pc;
  176. #if defined(__x86_64__)
  177. pc = context->uc_mcontext.gregs[REG_RIP];
  178. #elif defined(__aarch64__)
  179. pc = context->uc_mcontext.pc;
  180. #elif defined(__riscv)
  181. pc = context->uc_mcontext.__gregs[REG_PC];
  182. #else
  183. const char msg[] =
  184. "\nUnsupported architecture, cannot read program counter\n";
  185. os_write(1, msg, sizeof(msg));
  186. pc = 0;
  187. #endif
  188. os_signal_action(sig, pc);
  189. }
  190. int os_setup_signal_handlers(void)
  191. {
  192. struct sigaction act;
  193. act.sa_sigaction = os_signal_handler;
  194. sigemptyset(&act.sa_mask);
  195. act.sa_flags = SA_SIGINFO | SA_NODEFER;
  196. if (sigaction(SIGILL, &act, NULL) ||
  197. sigaction(SIGBUS, &act, NULL) ||
  198. sigaction(SIGSEGV, &act, NULL))
  199. return -1;
  200. return 0;
  201. }
  202. /* Put tty into raw mode so <tab> and <ctrl+c> work */
  203. void os_tty_raw(int fd, bool allow_sigs)
  204. {
  205. struct termios term;
  206. int flags;
  207. if (term_setup)
  208. return;
  209. /* If not a tty, don't complain */
  210. if (tcgetattr(fd, &orig_term))
  211. return;
  212. term = orig_term;
  213. term.c_iflag = IGNBRK | IGNPAR;
  214. term.c_oflag = OPOST | ONLCR;
  215. term.c_cflag = CS8 | CREAD | CLOCAL;
  216. term.c_lflag = allow_sigs ? ISIG : 0;
  217. if (tcsetattr(fd, TCSANOW, &term))
  218. return;
  219. flags = fcntl(fd, F_GETFL, 0);
  220. if (!(flags & O_NONBLOCK)) {
  221. if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
  222. return;
  223. term_nonblock = true;
  224. }
  225. term_setup = true;
  226. atexit(os_fd_restore);
  227. signal(SIGINT, os_sigint_handler);
  228. }
  229. /*
  230. * Provide our own malloc so we don't use space in the sandbox ram_buf for
  231. * allocations that are internal to sandbox, or need to be done before U-Boot's
  232. * malloc() is ready.
  233. */
  234. void *os_malloc(size_t length)
  235. {
  236. int page_size = getpagesize();
  237. struct os_mem_hdr *hdr;
  238. if (!length)
  239. return NULL;
  240. /*
  241. * Use an address that is hopefully available to us so that pointers
  242. * to this memory are fairly obvious. If we end up with a different
  243. * address, that's fine too.
  244. */
  245. hdr = mmap((void *)0x10000000, length + page_size,
  246. PROT_READ | PROT_WRITE | PROT_EXEC,
  247. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  248. if (hdr == MAP_FAILED)
  249. return NULL;
  250. hdr->length = length;
  251. return (void *)hdr + page_size;
  252. }
  253. void os_free(void *ptr)
  254. {
  255. int page_size = getpagesize();
  256. struct os_mem_hdr *hdr;
  257. if (ptr) {
  258. hdr = ptr - page_size;
  259. munmap(hdr, hdr->length + page_size);
  260. }
  261. }
  262. /* These macros are from kernel.h but not accessible in this file */
  263. #define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a) - 1)
  264. #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
  265. /*
  266. * Provide our own malloc so we don't use space in the sandbox ram_buf for
  267. * allocations that are internal to sandbox, or need to be done before U-Boot's
  268. * malloc() is ready.
  269. */
  270. void *os_realloc(void *ptr, size_t length)
  271. {
  272. int page_size = getpagesize();
  273. struct os_mem_hdr *hdr;
  274. void *new_ptr;
  275. /* Reallocating a NULL pointer is just an alloc */
  276. if (!ptr)
  277. return os_malloc(length);
  278. /* Changing a length to 0 is just a free */
  279. if (length) {
  280. os_free(ptr);
  281. return NULL;
  282. }
  283. /*
  284. * If the new size is the same number of pages as the old, nothing to
  285. * do. There isn't much point in shrinking things
  286. */
  287. hdr = ptr - page_size;
  288. if (ALIGN(length, page_size) <= ALIGN(hdr->length, page_size))
  289. return ptr;
  290. /* We have to grow it, so allocate something new */
  291. new_ptr = os_malloc(length);
  292. memcpy(new_ptr, ptr, hdr->length);
  293. os_free(ptr);
  294. return new_ptr;
  295. }
  296. void os_usleep(unsigned long usec)
  297. {
  298. usleep(usec);
  299. }
  300. uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
  301. {
  302. #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
  303. struct timespec tp;
  304. if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
  305. struct timeval tv;
  306. gettimeofday(&tv, NULL);
  307. tp.tv_sec = tv.tv_sec;
  308. tp.tv_nsec = tv.tv_usec * 1000;
  309. }
  310. return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
  311. #else
  312. struct timeval tv;
  313. gettimeofday(&tv, NULL);
  314. return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
  315. #endif
  316. }
  317. static char *short_opts;
  318. static struct option *long_opts;
  319. int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
  320. {
  321. struct sandbox_cmdline_option **sb_opt =
  322. __u_boot_sandbox_option_start();
  323. size_t num_options = __u_boot_sandbox_option_count();
  324. size_t i;
  325. int hidden_short_opt;
  326. size_t si;
  327. int c;
  328. if (short_opts || long_opts)
  329. return 1;
  330. state->argc = argc;
  331. state->argv = argv;
  332. /* dynamically construct the arguments to the system getopt_long */
  333. short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
  334. long_opts = os_malloc(sizeof(*long_opts) * (num_options + 1));
  335. if (!short_opts || !long_opts)
  336. return 1;
  337. /*
  338. * getopt_long requires "val" to be unique (since that is what the
  339. * func returns), so generate unique values automatically for flags
  340. * that don't have a short option. pick 0x100 as that is above the
  341. * single byte range (where ASCII/ISO-XXXX-X charsets live).
  342. */
  343. hidden_short_opt = 0x100;
  344. si = 0;
  345. for (i = 0; i < num_options; ++i) {
  346. long_opts[i].name = sb_opt[i]->flag;
  347. long_opts[i].has_arg = sb_opt[i]->has_arg ?
  348. required_argument : no_argument;
  349. long_opts[i].flag = NULL;
  350. if (sb_opt[i]->flag_short) {
  351. short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
  352. if (long_opts[i].has_arg == required_argument)
  353. short_opts[si++] = ':';
  354. } else
  355. long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
  356. }
  357. short_opts[si] = '\0';
  358. /* we need to handle output ourselves since u-boot provides printf */
  359. opterr = 0;
  360. memset(&long_opts[num_options], '\0', sizeof(*long_opts));
  361. /*
  362. * walk all of the options the user gave us on the command line,
  363. * figure out what u-boot option structure they belong to (via
  364. * the unique short val key), and call the appropriate callback.
  365. */
  366. while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
  367. for (i = 0; i < num_options; ++i) {
  368. if (sb_opt[i]->flag_short == c) {
  369. if (sb_opt[i]->callback(state, optarg)) {
  370. state->parse_err = sb_opt[i]->flag;
  371. return 0;
  372. }
  373. break;
  374. }
  375. }
  376. if (i == num_options) {
  377. /*
  378. * store the faulting flag for later display. we have to
  379. * store the flag itself as the getopt parsing itself is
  380. * tricky: need to handle the following flags (assume all
  381. * of the below are unknown):
  382. * -a optopt='a' optind=<next>
  383. * -abbbb optopt='a' optind=<this>
  384. * -aaaaa optopt='a' optind=<this>
  385. * --a optopt=0 optind=<this>
  386. * as you can see, it is impossible to determine the exact
  387. * faulting flag without doing the parsing ourselves, so
  388. * we just report the specific flag that failed.
  389. */
  390. if (optopt) {
  391. static char parse_err[3] = { '-', 0, '\0', };
  392. parse_err[1] = optopt;
  393. state->parse_err = parse_err;
  394. } else
  395. state->parse_err = argv[optind - 1];
  396. break;
  397. }
  398. }
  399. return 0;
  400. }
  401. void os_dirent_free(struct os_dirent_node *node)
  402. {
  403. struct os_dirent_node *next;
  404. while (node) {
  405. next = node->next;
  406. os_free(node);
  407. node = next;
  408. }
  409. }
  410. int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
  411. {
  412. struct dirent *entry;
  413. struct os_dirent_node *head, *node, *next;
  414. struct stat buf;
  415. DIR *dir;
  416. int ret;
  417. char *fname;
  418. char *old_fname;
  419. int len;
  420. int dirlen;
  421. *headp = NULL;
  422. dir = opendir(dirname);
  423. if (!dir)
  424. return -1;
  425. /* Create a buffer upfront, with typically sufficient size */
  426. dirlen = strlen(dirname) + 2;
  427. len = dirlen + 256;
  428. fname = os_malloc(len);
  429. if (!fname) {
  430. ret = -ENOMEM;
  431. goto done;
  432. }
  433. for (node = head = NULL;; node = next) {
  434. errno = 0;
  435. entry = readdir(dir);
  436. if (!entry) {
  437. ret = errno;
  438. break;
  439. }
  440. next = os_malloc(sizeof(*node) + strlen(entry->d_name) + 1);
  441. if (!next) {
  442. os_dirent_free(head);
  443. ret = -ENOMEM;
  444. goto done;
  445. }
  446. if (dirlen + strlen(entry->d_name) > len) {
  447. len = dirlen + strlen(entry->d_name);
  448. old_fname = fname;
  449. fname = os_realloc(fname, len);
  450. if (!fname) {
  451. os_free(old_fname);
  452. os_free(next);
  453. os_dirent_free(head);
  454. ret = -ENOMEM;
  455. goto done;
  456. }
  457. }
  458. next->next = NULL;
  459. strcpy(next->name, entry->d_name);
  460. switch (entry->d_type) {
  461. case DT_REG:
  462. next->type = OS_FILET_REG;
  463. break;
  464. case DT_DIR:
  465. next->type = OS_FILET_DIR;
  466. break;
  467. case DT_LNK:
  468. next->type = OS_FILET_LNK;
  469. break;
  470. default:
  471. next->type = OS_FILET_UNKNOWN;
  472. }
  473. next->size = 0;
  474. snprintf(fname, len, "%s/%s", dirname, next->name);
  475. if (!stat(fname, &buf))
  476. next->size = buf.st_size;
  477. if (node)
  478. node->next = next;
  479. else
  480. head = next;
  481. }
  482. *headp = head;
  483. done:
  484. closedir(dir);
  485. os_free(fname);
  486. return ret;
  487. }
  488. const char *os_dirent_typename[OS_FILET_COUNT] = {
  489. " ",
  490. "SYM",
  491. "DIR",
  492. "???",
  493. };
  494. const char *os_dirent_get_typename(enum os_dirent_t type)
  495. {
  496. if (type >= OS_FILET_REG && type < OS_FILET_COUNT)
  497. return os_dirent_typename[type];
  498. return os_dirent_typename[OS_FILET_UNKNOWN];
  499. }
  500. int os_get_filesize(const char *fname, loff_t *size)
  501. {
  502. struct stat buf;
  503. int ret;
  504. ret = stat(fname, &buf);
  505. if (ret)
  506. return ret;
  507. *size = buf.st_size;
  508. return 0;
  509. }
  510. void os_putc(int ch)
  511. {
  512. putchar(ch);
  513. }
  514. void os_puts(const char *str)
  515. {
  516. while (*str)
  517. os_putc(*str++);
  518. }
  519. int os_write_ram_buf(const char *fname)
  520. {
  521. struct sandbox_state *state = state_get_current();
  522. int fd, ret;
  523. fd = open(fname, O_CREAT | O_WRONLY, 0777);
  524. if (fd < 0)
  525. return -ENOENT;
  526. ret = write(fd, state->ram_buf, state->ram_size);
  527. close(fd);
  528. if (ret != state->ram_size)
  529. return -EIO;
  530. return 0;
  531. }
  532. int os_read_ram_buf(const char *fname)
  533. {
  534. struct sandbox_state *state = state_get_current();
  535. int fd, ret;
  536. loff_t size;
  537. ret = os_get_filesize(fname, &size);
  538. if (ret < 0)
  539. return ret;
  540. if (size != state->ram_size)
  541. return -ENOSPC;
  542. fd = open(fname, O_RDONLY);
  543. if (fd < 0)
  544. return -ENOENT;
  545. ret = read(fd, state->ram_buf, state->ram_size);
  546. close(fd);
  547. if (ret != state->ram_size)
  548. return -EIO;
  549. return 0;
  550. }
  551. static int make_exec(char *fname, const void *data, int size)
  552. {
  553. int fd;
  554. strcpy(fname, "/tmp/u-boot.jump.XXXXXX");
  555. fd = mkstemp(fname);
  556. if (fd < 0)
  557. return -ENOENT;
  558. if (write(fd, data, size) < 0)
  559. return -EIO;
  560. close(fd);
  561. if (chmod(fname, 0777))
  562. return -ENOEXEC;
  563. return 0;
  564. }
  565. /**
  566. * add_args() - Allocate a new argv with the given args
  567. *
  568. * This is used to create a new argv array with all the old arguments and some
  569. * new ones that are passed in
  570. *
  571. * @argvp: Returns newly allocated args list
  572. * @add_args: Arguments to add, each a string
  573. * @count: Number of arguments in @add_args
  574. * @return 0 if OK, -ENOMEM if out of memory
  575. */
  576. static int add_args(char ***argvp, char *add_args[], int count)
  577. {
  578. char **argv, **ap;
  579. int argc;
  580. for (argc = 0; (*argvp)[argc]; argc++)
  581. ;
  582. argv = os_malloc((argc + count + 1) * sizeof(char *));
  583. if (!argv) {
  584. printf("Out of memory for %d argv\n", count);
  585. return -ENOMEM;
  586. }
  587. for (ap = *argvp, argc = 0; *ap; ap++) {
  588. char *arg = *ap;
  589. /* Drop args that we don't want to propagate */
  590. if (*arg == '-' && strlen(arg) == 2) {
  591. switch (arg[1]) {
  592. case 'j':
  593. case 'm':
  594. ap++;
  595. continue;
  596. }
  597. } else if (!strcmp(arg, "--rm_memory")) {
  598. ap++;
  599. continue;
  600. }
  601. argv[argc++] = arg;
  602. }
  603. memcpy(argv + argc, add_args, count * sizeof(char *));
  604. argv[argc + count] = NULL;
  605. *argvp = argv;
  606. return 0;
  607. }
  608. /**
  609. * os_jump_to_file() - Jump to a new program
  610. *
  611. * This saves the memory buffer, sets up arguments to the new process, then
  612. * execs it.
  613. *
  614. * @fname: Filename to exec
  615. * @return does not return on success, any return value is an error
  616. */
  617. static int os_jump_to_file(const char *fname, bool delete_it)
  618. {
  619. struct sandbox_state *state = state_get_current();
  620. char mem_fname[30];
  621. int fd, err;
  622. char *extra_args[5];
  623. char **argv = state->argv;
  624. int argc;
  625. #ifdef DEBUG
  626. int i;
  627. #endif
  628. strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
  629. fd = mkstemp(mem_fname);
  630. if (fd < 0)
  631. return -ENOENT;
  632. close(fd);
  633. err = os_write_ram_buf(mem_fname);
  634. if (err)
  635. return err;
  636. os_fd_restore();
  637. argc = 0;
  638. if (delete_it) {
  639. extra_args[argc++] = "-j";
  640. extra_args[argc++] = (char *)fname;
  641. }
  642. extra_args[argc++] = "-m";
  643. extra_args[argc++] = mem_fname;
  644. if (state->ram_buf_rm)
  645. extra_args[argc++] = "--rm_memory";
  646. err = add_args(&argv, extra_args, argc);
  647. if (err)
  648. return err;
  649. argv[0] = (char *)fname;
  650. #ifdef DEBUG
  651. for (i = 0; argv[i]; i++)
  652. printf("%d %s\n", i, argv[i]);
  653. #endif
  654. if (state_uninit())
  655. os_exit(2);
  656. err = execv(fname, argv);
  657. os_free(argv);
  658. if (err) {
  659. perror("Unable to run image");
  660. printf("Image filename '%s'\n", fname);
  661. return err;
  662. }
  663. if (delete_it)
  664. return unlink(fname);
  665. return -EFAULT;
  666. }
  667. int os_jump_to_image(const void *dest, int size)
  668. {
  669. char fname[30];
  670. int err;
  671. err = make_exec(fname, dest, size);
  672. if (err)
  673. return err;
  674. return os_jump_to_file(fname, true);
  675. }
  676. int os_find_u_boot(char *fname, int maxlen, bool use_img)
  677. {
  678. struct sandbox_state *state = state_get_current();
  679. const char *progname = state->argv[0];
  680. int len = strlen(progname);
  681. const char *suffix;
  682. char *p;
  683. int fd;
  684. if (len >= maxlen || len < 4)
  685. return -ENOSPC;
  686. strcpy(fname, progname);
  687. suffix = fname + len - 4;
  688. /* If we are TPL, boot to SPL */
  689. if (!strcmp(suffix, "-tpl")) {
  690. fname[len - 3] = 's';
  691. fd = os_open(fname, O_RDONLY);
  692. if (fd >= 0) {
  693. close(fd);
  694. return 0;
  695. }
  696. /* Look for 'u-boot-spl' in the spl/ directory */
  697. p = strstr(fname, "/spl/");
  698. if (p) {
  699. p[1] = 's';
  700. fd = os_open(fname, O_RDONLY);
  701. if (fd >= 0) {
  702. close(fd);
  703. return 0;
  704. }
  705. }
  706. return -ENOENT;
  707. }
  708. /* Look for 'u-boot' in the same directory as 'u-boot-spl' */
  709. if (!strcmp(suffix, "-spl")) {
  710. fname[len - 4] = '\0';
  711. fd = os_open(fname, O_RDONLY);
  712. if (fd >= 0) {
  713. close(fd);
  714. return 0;
  715. }
  716. }
  717. /* Look for 'u-boot' in the parent directory of spl/ */
  718. p = strstr(fname, "spl/");
  719. if (p) {
  720. /* Remove the "spl" characters */
  721. memmove(p, p + 4, strlen(p + 4) + 1);
  722. if (use_img)
  723. strcat(p, ".img");
  724. fd = os_open(fname, O_RDONLY);
  725. if (fd >= 0) {
  726. close(fd);
  727. return 0;
  728. }
  729. }
  730. return -ENOENT;
  731. }
  732. int os_spl_to_uboot(const char *fname)
  733. {
  734. struct sandbox_state *state = state_get_current();
  735. /* U-Boot will delete ram buffer after read: "--rm_memory"*/
  736. state->ram_buf_rm = true;
  737. return os_jump_to_file(fname, false);
  738. }
  739. long os_get_time_offset(void)
  740. {
  741. const char *offset;
  742. offset = getenv(ENV_TIME_OFFSET);
  743. if (offset)
  744. return strtol(offset, NULL, 0);
  745. return 0;
  746. }
  747. void os_set_time_offset(long offset)
  748. {
  749. char buf[21];
  750. int ret;
  751. snprintf(buf, sizeof(buf), "%ld", offset);
  752. ret = setenv(ENV_TIME_OFFSET, buf, true);
  753. if (ret)
  754. printf("Could not set environment variable %s\n",
  755. ENV_TIME_OFFSET);
  756. }
  757. void os_localtime(struct rtc_time *rt)
  758. {
  759. time_t t = time(NULL);
  760. struct tm *tm;
  761. tm = localtime(&t);
  762. rt->tm_sec = tm->tm_sec;
  763. rt->tm_min = tm->tm_min;
  764. rt->tm_hour = tm->tm_hour;
  765. rt->tm_mday = tm->tm_mday;
  766. rt->tm_mon = tm->tm_mon + 1;
  767. rt->tm_year = tm->tm_year + 1900;
  768. rt->tm_wday = tm->tm_wday;
  769. rt->tm_yday = tm->tm_yday;
  770. rt->tm_isdst = tm->tm_isdst;
  771. }
  772. void os_abort(void)
  773. {
  774. abort();
  775. }
  776. int os_mprotect_allow(void *start, size_t len)
  777. {
  778. int page_size = getpagesize();
  779. /* Move start to the start of a page, len to the end */
  780. start = (void *)(((ulong)start) & ~(page_size - 1));
  781. len = (len + page_size * 2) & ~(page_size - 1);
  782. return mprotect(start, len, PROT_READ | PROT_WRITE);
  783. }
  784. void *os_find_text_base(void)
  785. {
  786. char line[500];
  787. void *base = NULL;
  788. int len;
  789. int fd;
  790. /*
  791. * This code assumes that the first line of /proc/self/maps holds
  792. * information about the text, for example:
  793. *
  794. * 5622d9907000-5622d9a55000 r-xp 00000000 08:01 15067168 u-boot
  795. *
  796. * The first hex value is assumed to be the address.
  797. *
  798. * This is tested in Linux 4.15.
  799. */
  800. fd = open("/proc/self/maps", O_RDONLY);
  801. if (fd == -1)
  802. return NULL;
  803. len = read(fd, line, sizeof(line));
  804. if (len > 0) {
  805. char *end = memchr(line, '-', len);
  806. if (end) {
  807. uintptr_t addr;
  808. *end = '\0';
  809. if (sscanf(line, "%zx", &addr) == 1)
  810. base = (void *)addr;
  811. }
  812. }
  813. close(fd);
  814. return base;
  815. }
  816. void os_relaunch(char *argv[])
  817. {
  818. execv(argv[0], argv);
  819. os_exit(1);
  820. }