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