os.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 *fname, const void *buf, int size)
  85. {
  86. int fd;
  87. fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC);
  88. if (fd < 0) {
  89. printf("Cannot open file '%s'\n", fname);
  90. return -EIO;
  91. }
  92. if (os_write(fd, buf, size) != size) {
  93. printf("Cannot write to file '%s'\n", fname);
  94. os_close(fd);
  95. return -EIO;
  96. }
  97. os_close(fd);
  98. return 0;
  99. }
  100. int os_read_file(const char *fname, void **bufp, int *sizep)
  101. {
  102. off_t size;
  103. int ret = -EIO;
  104. int fd;
  105. fd = os_open(fname, OS_O_RDONLY);
  106. if (fd < 0) {
  107. printf("Cannot open file '%s'\n", fname);
  108. goto err;
  109. }
  110. size = os_lseek(fd, 0, OS_SEEK_END);
  111. if (size < 0) {
  112. printf("Cannot seek to end of file '%s'\n", fname);
  113. goto err;
  114. }
  115. if (os_lseek(fd, 0, OS_SEEK_SET) < 0) {
  116. printf("Cannot seek to start of file '%s'\n", fname);
  117. goto err;
  118. }
  119. *bufp = malloc(size);
  120. if (!*bufp) {
  121. printf("Not enough memory to read file '%s'\n", fname);
  122. ret = -ENOMEM;
  123. goto err;
  124. }
  125. if (os_read(fd, *bufp, size) != size) {
  126. printf("Cannot read from file '%s'\n", fname);
  127. goto err;
  128. }
  129. os_close(fd);
  130. *sizep = size;
  131. return 0;
  132. err:
  133. os_close(fd);
  134. return ret;
  135. }
  136. /* Restore tty state when we exit */
  137. static struct termios orig_term;
  138. static bool term_setup;
  139. static bool term_nonblock;
  140. void os_fd_restore(void)
  141. {
  142. if (term_setup) {
  143. int flags;
  144. tcsetattr(0, TCSANOW, &orig_term);
  145. if (term_nonblock) {
  146. flags = fcntl(0, F_GETFL, 0);
  147. fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
  148. }
  149. term_setup = false;
  150. }
  151. }
  152. /* Put tty into raw mode so <tab> and <ctrl+c> work */
  153. void os_tty_raw(int fd, bool allow_sigs)
  154. {
  155. struct termios term;
  156. int flags;
  157. if (term_setup)
  158. return;
  159. /* If not a tty, don't complain */
  160. if (tcgetattr(fd, &orig_term))
  161. return;
  162. term = orig_term;
  163. term.c_iflag = IGNBRK | IGNPAR;
  164. term.c_oflag = OPOST | ONLCR;
  165. term.c_cflag = CS8 | CREAD | CLOCAL;
  166. term.c_lflag = allow_sigs ? ISIG : 0;
  167. if (tcsetattr(fd, TCSANOW, &term))
  168. return;
  169. flags = fcntl(fd, F_GETFL, 0);
  170. if (!(flags & O_NONBLOCK)) {
  171. if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
  172. return;
  173. term_nonblock = true;
  174. }
  175. term_setup = true;
  176. atexit(os_fd_restore);
  177. }
  178. void *os_malloc(size_t length)
  179. {
  180. int page_size = getpagesize();
  181. struct os_mem_hdr *hdr;
  182. /*
  183. * Use an address that is hopefully available to us so that pointers
  184. * to this memory are fairly obvious. If we end up with a different
  185. * address, that's fine too.
  186. */
  187. hdr = mmap((void *)0x10000000, length + page_size,
  188. PROT_READ | PROT_WRITE | PROT_EXEC,
  189. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  190. if (hdr == MAP_FAILED)
  191. return NULL;
  192. hdr->length = length;
  193. return (void *)hdr + page_size;
  194. }
  195. void os_free(void *ptr)
  196. {
  197. int page_size = getpagesize();
  198. struct os_mem_hdr *hdr;
  199. if (ptr) {
  200. hdr = ptr - page_size;
  201. munmap(hdr, hdr->length + page_size);
  202. }
  203. }
  204. void os_usleep(unsigned long usec)
  205. {
  206. usleep(usec);
  207. }
  208. uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
  209. {
  210. #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
  211. struct timespec tp;
  212. if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
  213. struct timeval tv;
  214. gettimeofday(&tv, NULL);
  215. tp.tv_sec = tv.tv_sec;
  216. tp.tv_nsec = tv.tv_usec * 1000;
  217. }
  218. return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
  219. #else
  220. struct timeval tv;
  221. gettimeofday(&tv, NULL);
  222. return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
  223. #endif
  224. }
  225. static char *short_opts;
  226. static struct option *long_opts;
  227. int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
  228. {
  229. struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
  230. size_t num_options = __u_boot_sandbox_option_count();
  231. size_t i;
  232. int hidden_short_opt;
  233. size_t si;
  234. int c;
  235. if (short_opts || long_opts)
  236. return 1;
  237. state->argc = argc;
  238. state->argv = argv;
  239. /* dynamically construct the arguments to the system getopt_long */
  240. short_opts = malloc(sizeof(*short_opts) * num_options * 2 + 1);
  241. long_opts = malloc(sizeof(*long_opts) * num_options);
  242. if (!short_opts || !long_opts)
  243. return 1;
  244. /*
  245. * getopt_long requires "val" to be unique (since that is what the
  246. * func returns), so generate unique values automatically for flags
  247. * that don't have a short option. pick 0x100 as that is above the
  248. * single byte range (where ASCII/ISO-XXXX-X charsets live).
  249. */
  250. hidden_short_opt = 0x100;
  251. si = 0;
  252. for (i = 0; i < num_options; ++i) {
  253. long_opts[i].name = sb_opt[i]->flag;
  254. long_opts[i].has_arg = sb_opt[i]->has_arg ?
  255. required_argument : no_argument;
  256. long_opts[i].flag = NULL;
  257. if (sb_opt[i]->flag_short) {
  258. short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
  259. if (long_opts[i].has_arg == required_argument)
  260. short_opts[si++] = ':';
  261. } else
  262. long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
  263. }
  264. short_opts[si] = '\0';
  265. /* we need to handle output ourselves since u-boot provides printf */
  266. opterr = 0;
  267. /*
  268. * walk all of the options the user gave us on the command line,
  269. * figure out what u-boot option structure they belong to (via
  270. * the unique short val key), and call the appropriate callback.
  271. */
  272. while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
  273. for (i = 0; i < num_options; ++i) {
  274. if (sb_opt[i]->flag_short == c) {
  275. if (sb_opt[i]->callback(state, optarg)) {
  276. state->parse_err = sb_opt[i]->flag;
  277. return 0;
  278. }
  279. break;
  280. }
  281. }
  282. if (i == num_options) {
  283. /*
  284. * store the faulting flag for later display. we have to
  285. * store the flag itself as the getopt parsing itself is
  286. * tricky: need to handle the following flags (assume all
  287. * of the below are unknown):
  288. * -a optopt='a' optind=<next>
  289. * -abbbb optopt='a' optind=<this>
  290. * -aaaaa optopt='a' optind=<this>
  291. * --a optopt=0 optind=<this>
  292. * as you can see, it is impossible to determine the exact
  293. * faulting flag without doing the parsing ourselves, so
  294. * we just report the specific flag that failed.
  295. */
  296. if (optopt) {
  297. static char parse_err[3] = { '-', 0, '\0', };
  298. parse_err[1] = optopt;
  299. state->parse_err = parse_err;
  300. } else
  301. state->parse_err = argv[optind - 1];
  302. break;
  303. }
  304. }
  305. return 0;
  306. }
  307. void os_dirent_free(struct os_dirent_node *node)
  308. {
  309. struct os_dirent_node *next;
  310. while (node) {
  311. next = node->next;
  312. free(node);
  313. node = next;
  314. }
  315. }
  316. int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
  317. {
  318. struct dirent *entry;
  319. struct os_dirent_node *head, *node, *next;
  320. struct stat buf;
  321. DIR *dir;
  322. int ret;
  323. char *fname;
  324. char *old_fname;
  325. int len;
  326. int dirlen;
  327. *headp = NULL;
  328. dir = opendir(dirname);
  329. if (!dir)
  330. return -1;
  331. /* Create a buffer upfront, with typically sufficient size */
  332. dirlen = strlen(dirname) + 2;
  333. len = dirlen + 256;
  334. fname = malloc(len);
  335. if (!fname) {
  336. ret = -ENOMEM;
  337. goto done;
  338. }
  339. for (node = head = NULL;; node = next) {
  340. errno = 0;
  341. entry = readdir(dir);
  342. if (!entry) {
  343. ret = errno;
  344. break;
  345. }
  346. next = malloc(sizeof(*node) + strlen(entry->d_name) + 1);
  347. if (!next) {
  348. os_dirent_free(head);
  349. ret = -ENOMEM;
  350. goto done;
  351. }
  352. if (dirlen + strlen(entry->d_name) > len) {
  353. len = dirlen + strlen(entry->d_name);
  354. old_fname = fname;
  355. fname = realloc(fname, len);
  356. if (!fname) {
  357. free(old_fname);
  358. free(next);
  359. os_dirent_free(head);
  360. ret = -ENOMEM;
  361. goto done;
  362. }
  363. }
  364. next->next = NULL;
  365. strcpy(next->name, entry->d_name);
  366. switch (entry->d_type) {
  367. case DT_REG:
  368. next->type = OS_FILET_REG;
  369. break;
  370. case DT_DIR:
  371. next->type = OS_FILET_DIR;
  372. break;
  373. case DT_LNK:
  374. next->type = OS_FILET_LNK;
  375. break;
  376. default:
  377. next->type = OS_FILET_UNKNOWN;
  378. }
  379. next->size = 0;
  380. snprintf(fname, len, "%s/%s", dirname, next->name);
  381. if (!stat(fname, &buf))
  382. next->size = buf.st_size;
  383. if (node)
  384. node->next = next;
  385. else
  386. head = next;
  387. }
  388. *headp = head;
  389. done:
  390. closedir(dir);
  391. free(fname);
  392. return ret;
  393. }
  394. const char *os_dirent_typename[OS_FILET_COUNT] = {
  395. " ",
  396. "SYM",
  397. "DIR",
  398. "???",
  399. };
  400. const char *os_dirent_get_typename(enum os_dirent_t type)
  401. {
  402. if (type >= OS_FILET_REG && type < OS_FILET_COUNT)
  403. return os_dirent_typename[type];
  404. return os_dirent_typename[OS_FILET_UNKNOWN];
  405. }
  406. int os_get_filesize(const char *fname, loff_t *size)
  407. {
  408. struct stat buf;
  409. int ret;
  410. ret = stat(fname, &buf);
  411. if (ret)
  412. return ret;
  413. *size = buf.st_size;
  414. return 0;
  415. }
  416. void os_putc(int ch)
  417. {
  418. putchar(ch);
  419. }
  420. void os_puts(const char *str)
  421. {
  422. while (*str)
  423. os_putc(*str++);
  424. }
  425. int os_write_ram_buf(const char *fname)
  426. {
  427. struct sandbox_state *state = state_get_current();
  428. int fd, ret;
  429. fd = open(fname, O_CREAT | O_WRONLY, 0777);
  430. if (fd < 0)
  431. return -ENOENT;
  432. ret = write(fd, state->ram_buf, state->ram_size);
  433. close(fd);
  434. if (ret != state->ram_size)
  435. return -EIO;
  436. return 0;
  437. }
  438. int os_read_ram_buf(const char *fname)
  439. {
  440. struct sandbox_state *state = state_get_current();
  441. int fd, ret;
  442. loff_t size;
  443. ret = os_get_filesize(fname, &size);
  444. if (ret < 0)
  445. return ret;
  446. if (size != state->ram_size)
  447. return -ENOSPC;
  448. fd = open(fname, O_RDONLY);
  449. if (fd < 0)
  450. return -ENOENT;
  451. ret = read(fd, state->ram_buf, state->ram_size);
  452. close(fd);
  453. if (ret != state->ram_size)
  454. return -EIO;
  455. return 0;
  456. }
  457. static int make_exec(char *fname, const void *data, int size)
  458. {
  459. int fd;
  460. strcpy(fname, "/tmp/u-boot.jump.XXXXXX");
  461. fd = mkstemp(fname);
  462. if (fd < 0)
  463. return -ENOENT;
  464. if (write(fd, data, size) < 0)
  465. return -EIO;
  466. close(fd);
  467. if (chmod(fname, 0777))
  468. return -ENOEXEC;
  469. return 0;
  470. }
  471. /**
  472. * add_args() - Allocate a new argv with the given args
  473. *
  474. * This is used to create a new argv array with all the old arguments and some
  475. * new ones that are passed in
  476. *
  477. * @argvp: Returns newly allocated args list
  478. * @add_args: Arguments to add, each a string
  479. * @count: Number of arguments in @add_args
  480. * @return 0 if OK, -ENOMEM if out of memory
  481. */
  482. static int add_args(char ***argvp, char *add_args[], int count)
  483. {
  484. char **argv, **ap;
  485. int argc;
  486. for (argc = 0; (*argvp)[argc]; argc++)
  487. ;
  488. argv = malloc((argc + count + 1) * sizeof(char *));
  489. if (!argv) {
  490. printf("Out of memory for %d argv\n", count);
  491. return -ENOMEM;
  492. }
  493. for (ap = *argvp, argc = 0; *ap; ap++) {
  494. char *arg = *ap;
  495. /* Drop args that we don't want to propagate */
  496. if (*arg == '-' && strlen(arg) == 2) {
  497. switch (arg[1]) {
  498. case 'j':
  499. case 'm':
  500. ap++;
  501. continue;
  502. }
  503. } else if (!strcmp(arg, "--rm_memory")) {
  504. ap++;
  505. continue;
  506. }
  507. argv[argc++] = arg;
  508. }
  509. memcpy(argv + argc, add_args, count * sizeof(char *));
  510. argv[argc + count] = NULL;
  511. *argvp = argv;
  512. return 0;
  513. }
  514. /**
  515. * os_jump_to_file() - Jump to a new program
  516. *
  517. * This saves the memory buffer, sets up arguments to the new process, then
  518. * execs it.
  519. *
  520. * @fname: Filename to exec
  521. * @return does not return on success, any return value is an error
  522. */
  523. static int os_jump_to_file(const char *fname)
  524. {
  525. struct sandbox_state *state = state_get_current();
  526. char mem_fname[30];
  527. int fd, err;
  528. char *extra_args[5];
  529. char **argv = state->argv;
  530. int argc;
  531. #ifdef DEBUG
  532. int i;
  533. #endif
  534. strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
  535. fd = mkstemp(mem_fname);
  536. if (fd < 0)
  537. return -ENOENT;
  538. close(fd);
  539. err = os_write_ram_buf(mem_fname);
  540. if (err)
  541. return err;
  542. os_fd_restore();
  543. extra_args[0] = "-j";
  544. extra_args[1] = (char *)fname;
  545. extra_args[2] = "-m";
  546. extra_args[3] = mem_fname;
  547. argc = 4;
  548. if (state->ram_buf_rm)
  549. extra_args[argc++] = "--rm_memory";
  550. err = add_args(&argv, extra_args, argc);
  551. if (err)
  552. return err;
  553. argv[0] = (char *)fname;
  554. #ifdef DEBUG
  555. for (i = 0; argv[i]; i++)
  556. printf("%d %s\n", i, argv[i]);
  557. #endif
  558. if (state_uninit())
  559. os_exit(2);
  560. err = execv(fname, argv);
  561. free(argv);
  562. if (err) {
  563. perror("Unable to run image");
  564. printf("Image filename '%s'\n", fname);
  565. return err;
  566. }
  567. return unlink(fname);
  568. }
  569. int os_jump_to_image(const void *dest, int size)
  570. {
  571. char fname[30];
  572. int err;
  573. err = make_exec(fname, dest, size);
  574. if (err)
  575. return err;
  576. return os_jump_to_file(fname);
  577. }
  578. int os_find_u_boot(char *fname, int maxlen)
  579. {
  580. struct sandbox_state *state = state_get_current();
  581. const char *progname = state->argv[0];
  582. int len = strlen(progname);
  583. const char *suffix;
  584. char *p;
  585. int fd;
  586. if (len >= maxlen || len < 4)
  587. return -ENOSPC;
  588. strcpy(fname, progname);
  589. suffix = fname + len - 4;
  590. /* If we are TPL, boot to SPL */
  591. if (!strcmp(suffix, "-tpl")) {
  592. fname[len - 3] = 's';
  593. fd = os_open(fname, O_RDONLY);
  594. if (fd >= 0) {
  595. close(fd);
  596. return 0;
  597. }
  598. /* Look for 'u-boot-tpl' in the tpl/ directory */
  599. p = strstr(fname, "/tpl/");
  600. if (p) {
  601. p[1] = 's';
  602. fd = os_open(fname, O_RDONLY);
  603. if (fd >= 0) {
  604. close(fd);
  605. return 0;
  606. }
  607. }
  608. return -ENOENT;
  609. }
  610. /* Look for 'u-boot' in the same directory as 'u-boot-spl' */
  611. if (!strcmp(suffix, "-spl")) {
  612. fname[len - 4] = '\0';
  613. fd = os_open(fname, O_RDONLY);
  614. if (fd >= 0) {
  615. close(fd);
  616. return 0;
  617. }
  618. }
  619. /* Look for 'u-boot' in the parent directory of spl/ */
  620. p = strstr(fname, "spl/");
  621. if (p) {
  622. /* Remove the "spl" characters */
  623. memmove(p, p + 4, strlen(p + 4) + 1);
  624. fd = os_open(fname, O_RDONLY);
  625. if (fd >= 0) {
  626. close(fd);
  627. return 0;
  628. }
  629. }
  630. return -ENOENT;
  631. }
  632. int os_spl_to_uboot(const char *fname)
  633. {
  634. return os_jump_to_file(fname);
  635. }
  636. void os_localtime(struct rtc_time *rt)
  637. {
  638. time_t t = time(NULL);
  639. struct tm *tm;
  640. tm = localtime(&t);
  641. rt->tm_sec = tm->tm_sec;
  642. rt->tm_min = tm->tm_min;
  643. rt->tm_hour = tm->tm_hour;
  644. rt->tm_mday = tm->tm_mday;
  645. rt->tm_mon = tm->tm_mon + 1;
  646. rt->tm_year = tm->tm_year + 1900;
  647. rt->tm_wday = tm->tm_wday;
  648. rt->tm_yday = tm->tm_yday;
  649. rt->tm_isdst = tm->tm_isdst;
  650. }
  651. void os_abort(void)
  652. {
  653. abort();
  654. }
  655. int os_mprotect_allow(void *start, size_t len)
  656. {
  657. int page_size = getpagesize();
  658. /* Move start to the start of a page, len to the end */
  659. start = (void *)(((ulong)start) & ~(page_size - 1));
  660. len = (len + page_size * 2) & ~(page_size - 1);
  661. return mprotect(start, len, PROT_READ | PROT_WRITE);
  662. }
  663. void *os_find_text_base(void)
  664. {
  665. char line[500];
  666. void *base = NULL;
  667. int len;
  668. int fd;
  669. /*
  670. * This code assumes that the first line of /proc/self/maps holds
  671. * information about the text, for example:
  672. *
  673. * 5622d9907000-5622d9a55000 r-xp 00000000 08:01 15067168 u-boot
  674. *
  675. * The first hex value is assumed to be the address.
  676. *
  677. * This is tested in Linux 4.15.
  678. */
  679. fd = open("/proc/self/maps", O_RDONLY);
  680. if (fd == -1)
  681. return NULL;
  682. len = read(fd, line, sizeof(line));
  683. if (len > 0) {
  684. char *end = memchr(line, '-', len);
  685. if (end) {
  686. uintptr_t addr;
  687. *end = '\0';
  688. if (sscanf(line, "%zx", &addr) == 1)
  689. base = (void *)addr;
  690. }
  691. }
  692. close(fd);
  693. return base;
  694. }