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