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