os.c 12 KB

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