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