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