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