os.c 13 KB

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