os.c 13 KB

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