blob: 95c26d855ab0e21ee7fcef265e4ed14539f6760b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasscd0684f2011-10-03 19:26:44 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glasscd0684f2011-10-03 19:26:44 +00004 */
5
Heinrich Schuchardt28eb5092020-11-12 00:29:56 +01006#define _GNU_SOURCE
7
Simon Glassf1c45c82012-12-26 09:53:34 +00008#include <dirent.h>
Simon Glasse8015a62012-01-10 15:54:05 -08009#include <errno.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000010#include <fcntl.h>
Andrew Scull2b40f802022-05-30 10:00:11 +000011#include <pthread.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080012#include <getopt.h>
Simon Glass5dccd152018-05-16 09:42:22 -060013#include <setjmp.h>
Rasmus Villemoes2b72ad22020-02-14 10:58:37 +000014#include <signal.h>
Heinrich Schuchardtbb63ea82022-04-04 22:45:03 +020015#include <stdarg.h>
Simon Glassf1c45c82012-12-26 09:53:34 +000016#include <stdio.h>
Simon Glassfb4b4e82013-05-19 16:45:35 -070017#include <stdint.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000018#include <stdlib.h>
Simon Glassf1c45c82012-12-26 09:53:34 +000019#include <string.h>
Mike Frysingera5baaee2011-10-26 00:21:29 +000020#include <termios.h>
Matthias Weisser0d3dd142011-11-29 12:16:40 +010021#include <time.h>
Heinrich Schuchardt28eb5092020-11-12 00:29:56 +010022#include <ucontext.h>
Simon Glasse8015a62012-01-10 15:54:05 -080023#include <unistd.h>
Matthias Weisserb5f7b472011-11-05 11:40:34 +010024#include <sys/mman.h>
Simon Glasse8015a62012-01-10 15:54:05 -080025#include <sys/stat.h>
Simon Glass17064c02012-01-10 15:54:06 -080026#include <sys/time.h>
Simon Glasse8015a62012-01-10 15:54:05 -080027#include <sys/types.h>
Heinrich Schuchardt28eb5092020-11-12 00:29:56 +010028#include <linux/compiler_attributes.h>
Matthias Weisser0d3dd142011-11-29 12:16:40 +010029#include <linux/types.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000030
Andrew Scull2b40f802022-05-30 10:00:11 +000031#include <asm/fuzzing_engine.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080032#include <asm/getopt.h>
Andrew Scullca5d1372022-05-30 10:00:10 +000033#include <asm/main.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080034#include <asm/sections.h>
35#include <asm/state.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000036#include <os.h>
Simon Glass504548f2015-04-20 12:37:22 -060037#include <rtc_def.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000038
Heinrich Schuchardtc0d1a002020-12-30 18:07:48 +010039/* Environment variable for time offset */
40#define ENV_TIME_OFFSET "UBOOT_SB_TIME_OFFSET"
41
Simon Glasscd0684f2011-10-03 19:26:44 +000042/* Operating System Interface */
43
Simon Glass1e6594c2013-11-10 10:26:57 -070044struct os_mem_hdr {
45 size_t length; /* number of bytes in the block */
46};
47
Simon Glasscd0684f2011-10-03 19:26:44 +000048ssize_t os_read(int fd, void *buf, size_t count)
49{
50 return read(fd, buf, count);
51}
52
53ssize_t os_write(int fd, const void *buf, size_t count)
54{
55 return write(fd, buf, count);
56}
57
Heinrich Schuchardtbb63ea82022-04-04 22:45:03 +020058int 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 Frysinger60addac2011-10-25 13:02:58 +020070off_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 Glass3196d752012-02-20 23:56:58 -050083int os_open(const char *pathname, int os_flags)
Simon Glasscd0684f2011-10-03 19:26:44 +000084{
Simon Glass3196d752012-02-20 23:56:58 -050085 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 Glassce55a112018-10-01 11:55:07 -0600104 if (os_flags & OS_O_TRUNC)
105 flags |= O_TRUNC;
Heinrich Schuchardtfc96df62020-10-27 20:29:24 +0100106 /*
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 Glass3196d752012-02-20 23:56:58 -0500111
112 return open(pathname, flags, 0777);
Simon Glasscd0684f2011-10-03 19:26:44 +0000113}
114
115int os_close(int fd)
116{
Heinrich Schuchardt69db2ee2020-10-27 20:29:21 +0100117 /* Do not close the console input */
118 if (fd)
119 return close(fd);
120 return -1;
Simon Glasscd0684f2011-10-03 19:26:44 +0000121}
122
Stephen Warrencd5edba2014-03-01 22:18:00 -0700123int os_unlink(const char *pathname)
124{
125 return unlink(pathname);
126}
127
Simon Glasscd0684f2011-10-03 19:26:44 +0000128void os_exit(int exit_code)
129{
130 exit(exit_code);
131}
Mike Frysingera5baaee2011-10-26 00:21:29 +0000132
Rasmus Villemoesae400f32022-09-27 11:54:04 +0200133unsigned int os_alarm(unsigned int seconds)
134{
135 return alarm(seconds);
136}
137
138void os_set_alarm_handler(void (*handler)(int))
139{
140 if (!handler)
141 handler = SIG_DFL;
142 signal(SIGALRM, handler);
143}
144
145void os_raise_sigalrm(void)
146{
147 raise(SIGALRM);
148}
149
Simon Glass8d176d82018-11-06 15:21:25 -0700150int os_write_file(const char *fname, const void *buf, int size)
Simon Glasscbfa8452018-10-01 11:55:08 -0600151{
Simon Glasscbfa8452018-10-01 11:55:08 -0600152 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 Glass8d176d82018-11-06 15:21:25 -0700161 os_close(fd);
Simon Glasscbfa8452018-10-01 11:55:08 -0600162 return -EIO;
163 }
164 os_close(fd);
Simon Glasscbfa8452018-10-01 11:55:08 -0600165
166 return 0;
167}
168
Heinrich Schuchardt6db1b652023-04-05 11:34:15 +0200169off_t os_filesize(int fd)
Simon Glass7b9cf84f2021-08-18 21:40:30 -0600170{
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 Glass8d176d82018-11-06 15:21:25 -0700182int 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);
191 goto err;
192 }
Simon Glass7b9cf84f2021-08-18 21:40:30 -0600193 size = os_filesize(fd);
Simon Glass8d176d82018-11-06 15:21:25 -0700194 if (size < 0) {
Simon Glass7b9cf84f2021-08-18 21:40:30 -0600195 printf("Cannot get file size of '%s'\n", fname);
Simon Glass8d176d82018-11-06 15:21:25 -0700196 goto err;
197 }
Simon Glass7b9cf84f2021-08-18 21:40:30 -0600198
Simon Glassedd094e2021-02-06 09:57:33 -0700199 *bufp = os_malloc(size);
Simon Glass8d176d82018-11-06 15:21:25 -0700200 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;
213err:
214 os_close(fd);
215 return ret;
216}
217
Simon Glasse4c25c82021-08-18 21:40:31 -0600218int os_map_file(const char *pathname, int os_flags, void **bufp, int *sizep)
219{
220 void *ptr;
Heinrich Schuchardt6db1b652023-04-05 11:34:15 +0200221 off_t size;
Sean Andersonc7bc6cd2023-11-04 15:57:33 -0400222 int ifd, ret = 0;
Simon Glasse4c25c82021-08-18 21:40:31 -0600223
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 Andersonc7bc6cd2023-11-04 15:57:33 -0400232 ret = -EIO;
233 goto out;
Simon Glasse4c25c82021-08-18 21:40:31 -0600234 }
Heinrich Schuchardt6db1b652023-04-05 11:34:15 +0200235 if ((unsigned long long)size > (unsigned long long)SIZE_MAX) {
236 printf("File '%s' too large to map\n", pathname);
Sean Andersonc7bc6cd2023-11-04 15:57:33 -0400237 ret = -EIO;
238 goto out;
Heinrich Schuchardt6db1b652023-04-05 11:34:15 +0200239 }
Simon Glasse4c25c82021-08-18 21:40:31 -0600240
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 Andersonc7bc6cd2023-11-04 15:57:33 -0400244 ret = -EPERM;
245 goto out;
Simon Glasse4c25c82021-08-18 21:40:31 -0600246 }
247
248 *bufp = ptr;
249 *sizep = size;
250
Sean Andersonc7bc6cd2023-11-04 15:57:33 -0400251out:
252 os_close(ifd);
253 return ret;
Simon Glasse4c25c82021-08-18 21:40:31 -0600254}
255
Simon Glass5c1fd582021-10-23 17:25:58 -0600256int 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 Glass528a0f62023-08-24 13:55:37 -0600266int 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
Mike Frysingera5baaee2011-10-26 00:21:29 +0000290/* Restore tty state when we exit */
291static struct termios orig_term;
Simon Glass678ef472014-02-27 13:26:22 -0700292static bool term_setup;
Simon Glassae50ec72018-10-01 11:55:20 -0600293static bool term_nonblock;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000294
Simon Glass9c3b7d62015-05-10 21:07:27 -0600295void os_fd_restore(void)
Mike Frysingera5baaee2011-10-26 00:21:29 +0000296{
Simon Glass9c3b7d62015-05-10 21:07:27 -0600297 if (term_setup) {
Simon Glassae50ec72018-10-01 11:55:20 -0600298 int flags;
299
Simon Glass678ef472014-02-27 13:26:22 -0700300 tcsetattr(0, TCSANOW, &orig_term);
Simon Glassae50ec72018-10-01 11:55:20 -0600301 if (term_nonblock) {
302 flags = fcntl(0, F_GETFL, 0);
303 fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
304 }
Simon Glass9c3b7d62015-05-10 21:07:27 -0600305 term_setup = false;
306 }
Mike Frysingera5baaee2011-10-26 00:21:29 +0000307}
308
Rasmus Villemoes2b72ad22020-02-14 10:58:37 +0000309static void os_sigint_handler(int sig)
310{
311 os_fd_restore();
312 signal(SIGINT, SIG_DFL);
313 raise(SIGINT);
314}
315
Heinrich Schuchardt28eb5092020-11-12 00:29:56 +0100316static void os_signal_handler(int sig, siginfo_t *info, void *con)
317{
318 ucontext_t __maybe_unused *context = con;
319 unsigned long pc;
320
321#if defined(__x86_64__)
322 pc = context->uc_mcontext.gregs[REG_RIP];
323#elif defined(__aarch64__)
324 pc = context->uc_mcontext.pc;
325#elif defined(__riscv)
326 pc = context->uc_mcontext.__gregs[REG_PC];
327#else
328 const char msg[] =
329 "\nUnsupported architecture, cannot read program counter\n";
330
331 os_write(1, msg, sizeof(msg));
332 pc = 0;
333#endif
334
335 os_signal_action(sig, pc);
336}
337
338int os_setup_signal_handlers(void)
339{
340 struct sigaction act;
341
342 act.sa_sigaction = os_signal_handler;
343 sigemptyset(&act.sa_mask);
Heinrich Schuchardtd3741bc2021-07-05 19:43:00 +0200344 act.sa_flags = SA_SIGINFO;
Heinrich Schuchardt28eb5092020-11-12 00:29:56 +0100345 if (sigaction(SIGILL, &act, NULL) ||
346 sigaction(SIGBUS, &act, NULL) ||
347 sigaction(SIGSEGV, &act, NULL))
348 return -1;
349 return 0;
350}
351
Mike Frysingera5baaee2011-10-26 00:21:29 +0000352/* Put tty into raw mode so <tab> and <ctrl+c> work */
Simon Glass678ef472014-02-27 13:26:22 -0700353void os_tty_raw(int fd, bool allow_sigs)
Mike Frysingera5baaee2011-10-26 00:21:29 +0000354{
Mike Frysingera5baaee2011-10-26 00:21:29 +0000355 struct termios term;
Simon Glassae50ec72018-10-01 11:55:20 -0600356 int flags;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000357
Simon Glass678ef472014-02-27 13:26:22 -0700358 if (term_setup)
Mike Frysingera5baaee2011-10-26 00:21:29 +0000359 return;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000360
361 /* If not a tty, don't complain */
362 if (tcgetattr(fd, &orig_term))
363 return;
364
365 term = orig_term;
366 term.c_iflag = IGNBRK | IGNPAR;
367 term.c_oflag = OPOST | ONLCR;
368 term.c_cflag = CS8 | CREAD | CLOCAL;
Simon Glass678ef472014-02-27 13:26:22 -0700369 term.c_lflag = allow_sigs ? ISIG : 0;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000370 if (tcsetattr(fd, TCSANOW, &term))
371 return;
372
Simon Glassae50ec72018-10-01 11:55:20 -0600373 flags = fcntl(fd, F_GETFL, 0);
374 if (!(flags & O_NONBLOCK)) {
375 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
376 return;
377 term_nonblock = true;
378 }
379
Simon Glass9c3b7d62015-05-10 21:07:27 -0600380 term_setup = true;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000381 atexit(os_fd_restore);
Rasmus Villemoes2b72ad22020-02-14 10:58:37 +0000382 signal(SIGINT, os_sigint_handler);
Mike Frysingera5baaee2011-10-26 00:21:29 +0000383}
Matthias Weisserb5f7b472011-11-05 11:40:34 +0100384
Simon Glass4c902fa2021-02-06 09:57:32 -0700385/*
386 * Provide our own malloc so we don't use space in the sandbox ram_buf for
387 * allocations that are internal to sandbox, or need to be done before U-Boot's
388 * malloc() is ready.
389 */
Matthias Weisserb5f7b472011-11-05 11:40:34 +0100390void *os_malloc(size_t length)
391{
Simon Glassbe005d82018-09-15 00:50:54 -0600392 int page_size = getpagesize();
Simon Glassfc2dde82019-04-08 13:20:42 -0600393 struct os_mem_hdr *hdr;
Simon Glass1e6594c2013-11-10 10:26:57 -0700394
Simon Glass4c902fa2021-02-06 09:57:32 -0700395 if (!length)
396 return NULL;
Simon Glass57ba9422018-06-17 08:57:43 -0600397 /*
398 * Use an address that is hopefully available to us so that pointers
399 * to this memory are fairly obvious. If we end up with a different
400 * address, that's fine too.
401 */
402 hdr = mmap((void *)0x10000000, length + page_size,
Alexander Graf934a5452018-06-22 14:44:13 +0200403 PROT_READ | PROT_WRITE | PROT_EXEC,
404 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Simon Glass1e6594c2013-11-10 10:26:57 -0700405 if (hdr == MAP_FAILED)
406 return NULL;
407 hdr->length = length;
408
Simon Glassbe005d82018-09-15 00:50:54 -0600409 return (void *)hdr + page_size;
Simon Glass1e6594c2013-11-10 10:26:57 -0700410}
411
Masahiro Yamadaee957282014-01-15 13:06:41 +0900412void os_free(void *ptr)
Simon Glass1e6594c2013-11-10 10:26:57 -0700413{
Simon Glassfc2dde82019-04-08 13:20:42 -0600414 int page_size = getpagesize();
415 struct os_mem_hdr *hdr;
Simon Glass1e6594c2013-11-10 10:26:57 -0700416
Simon Glassfc2dde82019-04-08 13:20:42 -0600417 if (ptr) {
418 hdr = ptr - page_size;
419 munmap(hdr, hdr->length + page_size);
420 }
Simon Glass4c902fa2021-02-06 09:57:32 -0700421}
422
423/* These macros are from kernel.h but not accessible in this file */
424#define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a) - 1)
425#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
426
427/*
428 * Provide our own malloc so we don't use space in the sandbox ram_buf for
429 * allocations that are internal to sandbox, or need to be done before U-Boot's
430 * malloc() is ready.
431 */
432void *os_realloc(void *ptr, size_t length)
433{
434 int page_size = getpagesize();
435 struct os_mem_hdr *hdr;
436 void *new_ptr;
437
438 /* Reallocating a NULL pointer is just an alloc */
439 if (!ptr)
440 return os_malloc(length);
441
442 /* Changing a length to 0 is just a free */
443 if (length) {
444 os_free(ptr);
445 return NULL;
446 }
447
448 /*
449 * If the new size is the same number of pages as the old, nothing to
450 * do. There isn't much point in shrinking things
451 */
452 hdr = ptr - page_size;
453 if (ALIGN(length, page_size) <= ALIGN(hdr->length, page_size))
454 return ptr;
455
456 /* We have to grow it, so allocate something new */
457 new_ptr = os_malloc(length);
458 memcpy(new_ptr, ptr, hdr->length);
459 os_free(ptr);
460
461 return new_ptr;
Simon Glass1e6594c2013-11-10 10:26:57 -0700462}
463
Matthias Weisser0d3dd142011-11-29 12:16:40 +0100464void os_usleep(unsigned long usec)
465{
466 usleep(usec);
467}
468
Simon Glassfb4b4e82013-05-19 16:45:35 -0700469uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
Matthias Weisser0d3dd142011-11-29 12:16:40 +0100470{
471#if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
472 struct timespec tp;
473 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
474 struct timeval tv;
475
476 gettimeofday(&tv, NULL);
477 tp.tv_sec = tv.tv_sec;
478 tp.tv_nsec = tv.tv_usec * 1000;
479 }
480 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
481#else
482 struct timeval tv;
483 gettimeofday(&tv, NULL);
484 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
485#endif
486}
Simon Glass8a3e0352012-02-15 15:51:16 -0800487
488static char *short_opts;
489static struct option *long_opts;
490
491int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
492{
Marek Behún184c4af2021-05-20 13:24:06 +0200493 struct sandbox_cmdline_option **sb_opt =
494 __u_boot_sandbox_option_start();
Simon Glass8a3e0352012-02-15 15:51:16 -0800495 size_t num_options = __u_boot_sandbox_option_count();
496 size_t i;
497
498 int hidden_short_opt;
499 size_t si;
500
501 int c;
502
503 if (short_opts || long_opts)
504 return 1;
505
506 state->argc = argc;
507 state->argv = argv;
508
509 /* dynamically construct the arguments to the system getopt_long */
Simon Glassedd094e2021-02-06 09:57:33 -0700510 short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
511 long_opts = os_malloc(sizeof(*long_opts) * (num_options + 1));
Simon Glass8a3e0352012-02-15 15:51:16 -0800512 if (!short_opts || !long_opts)
513 return 1;
514
515 /*
516 * getopt_long requires "val" to be unique (since that is what the
517 * func returns), so generate unique values automatically for flags
518 * that don't have a short option. pick 0x100 as that is above the
519 * single byte range (where ASCII/ISO-XXXX-X charsets live).
520 */
521 hidden_short_opt = 0x100;
522 si = 0;
523 for (i = 0; i < num_options; ++i) {
524 long_opts[i].name = sb_opt[i]->flag;
525 long_opts[i].has_arg = sb_opt[i]->has_arg ?
526 required_argument : no_argument;
527 long_opts[i].flag = NULL;
528
529 if (sb_opt[i]->flag_short) {
530 short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
531 if (long_opts[i].has_arg == required_argument)
532 short_opts[si++] = ':';
533 } else
534 long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
535 }
536 short_opts[si] = '\0';
537
538 /* we need to handle output ourselves since u-boot provides printf */
539 opterr = 0;
540
Simon Glass0a4eabb2020-02-03 07:36:04 -0700541 memset(&long_opts[num_options], '\0', sizeof(*long_opts));
Simon Glass8a3e0352012-02-15 15:51:16 -0800542 /*
543 * walk all of the options the user gave us on the command line,
544 * figure out what u-boot option structure they belong to (via
545 * the unique short val key), and call the appropriate callback.
546 */
547 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
548 for (i = 0; i < num_options; ++i) {
549 if (sb_opt[i]->flag_short == c) {
550 if (sb_opt[i]->callback(state, optarg)) {
551 state->parse_err = sb_opt[i]->flag;
552 return 0;
553 }
554 break;
555 }
556 }
557 if (i == num_options) {
558 /*
559 * store the faulting flag for later display. we have to
560 * store the flag itself as the getopt parsing itself is
561 * tricky: need to handle the following flags (assume all
562 * of the below are unknown):
563 * -a optopt='a' optind=<next>
564 * -abbbb optopt='a' optind=<this>
565 * -aaaaa optopt='a' optind=<this>
566 * --a optopt=0 optind=<this>
567 * as you can see, it is impossible to determine the exact
568 * faulting flag without doing the parsing ourselves, so
569 * we just report the specific flag that failed.
570 */
571 if (optopt) {
572 static char parse_err[3] = { '-', 0, '\0', };
573 parse_err[1] = optopt;
574 state->parse_err = parse_err;
575 } else
576 state->parse_err = argv[optind - 1];
577 break;
578 }
579 }
580
581 return 0;
582}
Simon Glassf1c45c82012-12-26 09:53:34 +0000583
584void os_dirent_free(struct os_dirent_node *node)
585{
586 struct os_dirent_node *next;
587
588 while (node) {
589 next = node->next;
Simon Glassedd094e2021-02-06 09:57:33 -0700590 os_free(node);
Simon Glassf1c45c82012-12-26 09:53:34 +0000591 node = next;
592 }
593}
594
595int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
596{
Stefan Brünsae71ede2016-10-01 20:41:42 +0200597 struct dirent *entry;
Simon Glassf1c45c82012-12-26 09:53:34 +0000598 struct os_dirent_node *head, *node, *next;
599 struct stat buf;
600 DIR *dir;
601 int ret;
602 char *fname;
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200603 char *old_fname;
Simon Glassf1c45c82012-12-26 09:53:34 +0000604 int len;
Stefan Brünsb2129902016-10-01 20:41:40 +0200605 int dirlen;
Simon Glassf1c45c82012-12-26 09:53:34 +0000606
607 *headp = NULL;
608 dir = opendir(dirname);
609 if (!dir)
610 return -1;
611
Stefan Brünsb2129902016-10-01 20:41:40 +0200612 /* Create a buffer upfront, with typically sufficient size */
613 dirlen = strlen(dirname) + 2;
614 len = dirlen + 256;
Simon Glassedd094e2021-02-06 09:57:33 -0700615 fname = os_malloc(len);
Simon Glassf1c45c82012-12-26 09:53:34 +0000616 if (!fname) {
617 ret = -ENOMEM;
618 goto done;
619 }
620
621 for (node = head = NULL;; node = next) {
Stefan Brünsae71ede2016-10-01 20:41:42 +0200622 errno = 0;
623 entry = readdir(dir);
624 if (!entry) {
625 ret = errno;
Simon Glassf1c45c82012-12-26 09:53:34 +0000626 break;
Stefan Brünsae71ede2016-10-01 20:41:42 +0200627 }
Simon Glassedd094e2021-02-06 09:57:33 -0700628 next = os_malloc(sizeof(*node) + strlen(entry->d_name) + 1);
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200629 if (!next) {
Simon Glassf1c45c82012-12-26 09:53:34 +0000630 os_dirent_free(head);
631 ret = -ENOMEM;
632 goto done;
633 }
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200634 if (dirlen + strlen(entry->d_name) > len) {
635 len = dirlen + strlen(entry->d_name);
636 old_fname = fname;
Simon Glassedd094e2021-02-06 09:57:33 -0700637 fname = os_realloc(fname, len);
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200638 if (!fname) {
Simon Glassedd094e2021-02-06 09:57:33 -0700639 os_free(old_fname);
640 os_free(next);
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200641 os_dirent_free(head);
642 ret = -ENOMEM;
643 goto done;
644 }
645 }
Stephen Warren9671c672014-06-11 10:26:23 -0600646 next->next = NULL;
Stefan Brünsae71ede2016-10-01 20:41:42 +0200647 strcpy(next->name, entry->d_name);
648 switch (entry->d_type) {
Simon Glassf1c45c82012-12-26 09:53:34 +0000649 case DT_REG:
650 next->type = OS_FILET_REG;
651 break;
652 case DT_DIR:
653 next->type = OS_FILET_DIR;
654 break;
655 case DT_LNK:
656 next->type = OS_FILET_LNK;
657 break;
Stefan Brünsb7ffa822016-10-04 21:46:35 +0200658 default:
659 next->type = OS_FILET_UNKNOWN;
Simon Glassf1c45c82012-12-26 09:53:34 +0000660 }
661 next->size = 0;
662 snprintf(fname, len, "%s/%s", dirname, next->name);
663 if (!stat(fname, &buf))
664 next->size = buf.st_size;
665 if (node)
666 node->next = next;
Stefan Brünsd4cb8882016-10-01 20:41:39 +0200667 else
668 head = next;
Simon Glassf1c45c82012-12-26 09:53:34 +0000669 }
670 *headp = head;
671
672done:
673 closedir(dir);
Simon Glassedd094e2021-02-06 09:57:33 -0700674 os_free(fname);
Simon Glassf1c45c82012-12-26 09:53:34 +0000675 return ret;
676}
677
678const char *os_dirent_typename[OS_FILET_COUNT] = {
679 " ",
680 "SYM",
681 "DIR",
682 "???",
683};
684
685const char *os_dirent_get_typename(enum os_dirent_t type)
686{
Tom Rinib6f605e2017-05-13 20:11:30 -0400687 if (type >= OS_FILET_REG && type < OS_FILET_COUNT)
Simon Glassf1c45c82012-12-26 09:53:34 +0000688 return os_dirent_typename[type];
689
690 return os_dirent_typename[OS_FILET_UNKNOWN];
691}
692
Heinrich Schuchardt011a1e02022-01-11 01:50:24 +0100693/*
694 * For compatibility reasons avoid loff_t here.
695 * U-Boot defines loff_t as long long.
696 * But /usr/include/linux/types.h may not define it at all.
697 * Alpine Linux being one example.
698 */
699int os_get_filesize(const char *fname, long long *size)
Simon Glassf1c45c82012-12-26 09:53:34 +0000700{
701 struct stat buf;
702 int ret;
703
704 ret = stat(fname, &buf);
705 if (ret)
706 return ret;
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800707 *size = buf.st_size;
708 return 0;
Simon Glassf1c45c82012-12-26 09:53:34 +0000709}
Simon Glass3e9fd242013-11-10 10:27:01 -0700710
Simon Glass29d11432017-12-04 13:48:17 -0700711void os_putc(int ch)
712{
Simon Glassd56c6f42022-03-27 14:26:14 -0600713 os_write(1, &ch, 1);
Simon Glass29d11432017-12-04 13:48:17 -0700714}
715
716void os_puts(const char *str)
717{
718 while (*str)
719 os_putc(*str++);
720}
721
Pali Rohár4acd1a02022-09-05 11:31:16 +0200722void os_flush(void)
723{
724 fflush(stdout);
725}
726
Simon Glass9dd10bf2013-11-10 10:27:03 -0700727int os_write_ram_buf(const char *fname)
728{
729 struct sandbox_state *state = state_get_current();
730 int fd, ret;
731
732 fd = open(fname, O_CREAT | O_WRONLY, 0777);
733 if (fd < 0)
734 return -ENOENT;
735 ret = write(fd, state->ram_buf, state->ram_size);
736 close(fd);
737 if (ret != state->ram_size)
738 return -EIO;
739
740 return 0;
741}
742
743int os_read_ram_buf(const char *fname)
744{
745 struct sandbox_state *state = state_get_current();
746 int fd, ret;
Heinrich Schuchardt011a1e02022-01-11 01:50:24 +0100747 long long size;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700748
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800749 ret = os_get_filesize(fname, &size);
750 if (ret < 0)
751 return ret;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700752 if (size != state->ram_size)
753 return -ENOSPC;
754 fd = open(fname, O_RDONLY);
755 if (fd < 0)
756 return -ENOENT;
757
758 ret = read(fd, state->ram_buf, state->ram_size);
759 close(fd);
760 if (ret != state->ram_size)
761 return -EIO;
762
763 return 0;
764}
Simon Glass860b7d92014-02-27 13:26:15 -0700765
766static int make_exec(char *fname, const void *data, int size)
767{
768 int fd;
769
770 strcpy(fname, "/tmp/u-boot.jump.XXXXXX");
771 fd = mkstemp(fname);
772 if (fd < 0)
773 return -ENOENT;
774 if (write(fd, data, size) < 0)
775 return -EIO;
776 close(fd);
777 if (chmod(fname, 0777))
778 return -ENOEXEC;
779
780 return 0;
781}
782
Simon Glass7dafd022018-11-15 18:44:05 -0700783/**
784 * add_args() - Allocate a new argv with the given args
785 *
786 * This is used to create a new argv array with all the old arguments and some
787 * new ones that are passed in
788 *
789 * @argvp: Returns newly allocated args list
790 * @add_args: Arguments to add, each a string
791 * @count: Number of arguments in @add_args
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100792 * Return: 0 if OK, -ENOMEM if out of memory
Simon Glass7dafd022018-11-15 18:44:05 -0700793 */
794static int add_args(char ***argvp, char *add_args[], int count)
Simon Glass860b7d92014-02-27 13:26:15 -0700795{
Simon Glass7465f002018-11-15 18:44:07 -0700796 char **argv, **ap;
Simon Glass860b7d92014-02-27 13:26:15 -0700797 int argc;
798
Simon Glass7465f002018-11-15 18:44:07 -0700799 for (argc = 0; (*argvp)[argc]; argc++)
Simon Glass860b7d92014-02-27 13:26:15 -0700800 ;
801
Simon Glassedd094e2021-02-06 09:57:33 -0700802 argv = os_malloc((argc + count + 1) * sizeof(char *));
Simon Glass860b7d92014-02-27 13:26:15 -0700803 if (!argv) {
804 printf("Out of memory for %d argv\n", count);
805 return -ENOMEM;
806 }
Simon Glass7465f002018-11-15 18:44:07 -0700807 for (ap = *argvp, argc = 0; *ap; ap++) {
808 char *arg = *ap;
809
810 /* Drop args that we don't want to propagate */
811 if (*arg == '-' && strlen(arg) == 2) {
812 switch (arg[1]) {
813 case 'j':
814 case 'm':
815 ap++;
816 continue;
817 }
818 } else if (!strcmp(arg, "--rm_memory")) {
Simon Glass7465f002018-11-15 18:44:07 -0700819 continue;
820 }
821 argv[argc++] = arg;
822 }
823
Simon Glass860b7d92014-02-27 13:26:15 -0700824 memcpy(argv + argc, add_args, count * sizeof(char *));
825 argv[argc + count] = NULL;
826
827 *argvp = argv;
828 return 0;
829}
830
Simon Glass7dafd022018-11-15 18:44:05 -0700831/**
832 * os_jump_to_file() - Jump to a new program
833 *
834 * This saves the memory buffer, sets up arguments to the new process, then
835 * execs it.
836 *
837 * @fname: Filename to exec
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100838 * Return: does not return on success, any return value is an error
Simon Glass7dafd022018-11-15 18:44:05 -0700839 */
Simon Glassdafc5d72021-03-15 18:11:07 +1300840static int os_jump_to_file(const char *fname, bool delete_it)
Simon Glass860b7d92014-02-27 13:26:15 -0700841{
842 struct sandbox_state *state = state_get_current();
Simon Glass7dafd022018-11-15 18:44:05 -0700843 char mem_fname[30];
Simon Glass860b7d92014-02-27 13:26:15 -0700844 int fd, err;
Simon Glass7dafd022018-11-15 18:44:05 -0700845 char *extra_args[5];
Simon Glass860b7d92014-02-27 13:26:15 -0700846 char **argv = state->argv;
Simon Glass7465f002018-11-15 18:44:07 -0700847 int argc;
Simon Glass860b7d92014-02-27 13:26:15 -0700848#ifdef DEBUG
Simon Glass7dafd022018-11-15 18:44:05 -0700849 int i;
Simon Glass860b7d92014-02-27 13:26:15 -0700850#endif
851
Simon Glass860b7d92014-02-27 13:26:15 -0700852 strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
853 fd = mkstemp(mem_fname);
854 if (fd < 0)
855 return -ENOENT;
856 close(fd);
857 err = os_write_ram_buf(mem_fname);
858 if (err)
859 return err;
860
861 os_fd_restore();
862
Simon Glassdafc5d72021-03-15 18:11:07 +1300863 argc = 0;
864 if (delete_it) {
865 extra_args[argc++] = "-j";
866 extra_args[argc++] = (char *)fname;
867 }
868 extra_args[argc++] = "-m";
869 extra_args[argc++] = mem_fname;
Simon Glass7465f002018-11-15 18:44:07 -0700870 if (state->ram_buf_rm)
871 extra_args[argc++] = "--rm_memory";
872 err = add_args(&argv, extra_args, argc);
Simon Glass860b7d92014-02-27 13:26:15 -0700873 if (err)
874 return err;
Simon Glass7465f002018-11-15 18:44:07 -0700875 argv[0] = (char *)fname;
Simon Glass860b7d92014-02-27 13:26:15 -0700876
877#ifdef DEBUG
878 for (i = 0; argv[i]; i++)
879 printf("%d %s\n", i, argv[i]);
880#endif
881
882 if (state_uninit())
883 os_exit(2);
884
885 err = execv(fname, argv);
Simon Glassedd094e2021-02-06 09:57:33 -0700886 os_free(argv);
Simon Glasscca25522018-11-15 18:44:08 -0700887 if (err) {
888 perror("Unable to run image");
Simon Glass23eabbb2018-11-23 21:29:24 -0700889 printf("Image filename '%s'\n", fname);
Simon Glass860b7d92014-02-27 13:26:15 -0700890 return err;
Simon Glasscca25522018-11-15 18:44:08 -0700891 }
Simon Glass860b7d92014-02-27 13:26:15 -0700892
Simon Glassdafc5d72021-03-15 18:11:07 +1300893 if (delete_it)
894 return unlink(fname);
895
896 return -EFAULT;
Simon Glass860b7d92014-02-27 13:26:15 -0700897}
Simon Glass504548f2015-04-20 12:37:22 -0600898
Simon Glass7dafd022018-11-15 18:44:05 -0700899int os_jump_to_image(const void *dest, int size)
900{
901 char fname[30];
902 int err;
903
904 err = make_exec(fname, dest, size);
905 if (err)
906 return err;
907
Simon Glassdafc5d72021-03-15 18:11:07 +1300908 return os_jump_to_file(fname, true);
Simon Glass7dafd022018-11-15 18:44:05 -0700909}
910
Simon Glass1cd06002021-07-05 16:32:45 -0600911int os_find_u_boot(char *fname, int maxlen, bool use_img,
912 const char *cur_prefix, const char *next_prefix)
Simon Glass2c931ba2016-07-04 11:57:45 -0600913{
914 struct sandbox_state *state = state_get_current();
915 const char *progname = state->argv[0];
916 int len = strlen(progname);
Simon Glass1cd06002021-07-05 16:32:45 -0600917 char subdir[10];
918 char *suffix;
Simon Glass2c931ba2016-07-04 11:57:45 -0600919 char *p;
920 int fd;
921
922 if (len >= maxlen || len < 4)
923 return -ENOSPC;
924
Simon Glass2c931ba2016-07-04 11:57:45 -0600925 strcpy(fname, progname);
Simon Glassdc4d8eb2018-10-01 11:55:10 -0600926 suffix = fname + len - 4;
927
Simon Glass1cd06002021-07-05 16:32:45 -0600928 /* Change the existing suffix to the new one */
929 if (*suffix != '-')
930 return -EINVAL;
Simon Glassdc4d8eb2018-10-01 11:55:10 -0600931
Simon Glass1cd06002021-07-05 16:32:45 -0600932 if (*next_prefix)
933 strcpy(suffix + 1, next_prefix); /* e.g. "-tpl" to "-spl" */
934 else
935 *suffix = '\0'; /* e.g. "-spl" to "" */
936 fd = os_open(fname, O_RDONLY);
937 if (fd >= 0) {
938 close(fd);
939 return 0;
Simon Glassdc4d8eb2018-10-01 11:55:10 -0600940 }
941
Simon Glass1cd06002021-07-05 16:32:45 -0600942 /*
943 * We didn't find it, so try looking for 'u-boot-xxx' in the xxx/
944 * directory. Replace the old dirname with the new one.
945 */
946 snprintf(subdir, sizeof(subdir), "/%s/", cur_prefix);
947 p = strstr(fname, subdir);
Simon Glass2c931ba2016-07-04 11:57:45 -0600948 if (p) {
Simon Glass1cd06002021-07-05 16:32:45 -0600949 if (*next_prefix)
950 /* e.g. ".../tpl/u-boot-spl" to "../spl/u-boot-spl" */
951 memcpy(p + 1, next_prefix, strlen(next_prefix));
952 else
953 /* e.g. ".../spl/u-boot" to ".../u-boot" */
954 strcpy(p, p + 1 + strlen(cur_prefix));
Simon Glassb90e4872021-03-07 17:35:13 -0700955 if (use_img)
956 strcat(p, ".img");
Simon Glass1cd06002021-07-05 16:32:45 -0600957
Simon Glass2c931ba2016-07-04 11:57:45 -0600958 fd = os_open(fname, O_RDONLY);
959 if (fd >= 0) {
960 close(fd);
961 return 0;
962 }
963 }
964
965 return -ENOENT;
966}
967
968int os_spl_to_uboot(const char *fname)
969{
Patrick Delaunay6daf9052020-11-20 09:48:33 +0100970 struct sandbox_state *state = state_get_current();
971
Patrick Delaunay6daf9052020-11-20 09:48:33 +0100972 /* U-Boot will delete ram buffer after read: "--rm_memory"*/
973 state->ram_buf_rm = true;
Simon Glassdafc5d72021-03-15 18:11:07 +1300974
975 return os_jump_to_file(fname, false);
Simon Glass2c931ba2016-07-04 11:57:45 -0600976}
977
Heinrich Schuchardtc0d1a002020-12-30 18:07:48 +0100978long os_get_time_offset(void)
979{
980 const char *offset;
981
982 offset = getenv(ENV_TIME_OFFSET);
983 if (offset)
984 return strtol(offset, NULL, 0);
985 return 0;
986}
987
988void os_set_time_offset(long offset)
989{
990 char buf[21];
991 int ret;
992
993 snprintf(buf, sizeof(buf), "%ld", offset);
994 ret = setenv(ENV_TIME_OFFSET, buf, true);
995 if (ret)
996 printf("Could not set environment variable %s\n",
997 ENV_TIME_OFFSET);
998}
999
Simon Glass504548f2015-04-20 12:37:22 -06001000void os_localtime(struct rtc_time *rt)
1001{
1002 time_t t = time(NULL);
1003 struct tm *tm;
1004
1005 tm = localtime(&t);
1006 rt->tm_sec = tm->tm_sec;
1007 rt->tm_min = tm->tm_min;
1008 rt->tm_hour = tm->tm_hour;
1009 rt->tm_mday = tm->tm_mday;
1010 rt->tm_mon = tm->tm_mon + 1;
1011 rt->tm_year = tm->tm_year + 1900;
1012 rt->tm_wday = tm->tm_wday;
1013 rt->tm_yday = tm->tm_yday;
1014 rt->tm_isdst = tm->tm_isdst;
1015}
Simon Glass5dccd152018-05-16 09:42:22 -06001016
Simon Glassb7255ef2018-09-15 00:50:55 -06001017void os_abort(void)
1018{
1019 abort();
1020}
Simon Glass4e766c22018-10-01 21:12:32 -06001021
1022int os_mprotect_allow(void *start, size_t len)
1023{
1024 int page_size = getpagesize();
1025
1026 /* Move start to the start of a page, len to the end */
1027 start = (void *)(((ulong)start) & ~(page_size - 1));
1028 len = (len + page_size * 2) & ~(page_size - 1);
1029
1030 return mprotect(start, len, PROT_READ | PROT_WRITE);
1031}
Simon Glass752707a2019-04-08 13:20:41 -06001032
1033void *os_find_text_base(void)
1034{
1035 char line[500];
1036 void *base = NULL;
1037 int len;
1038 int fd;
1039
1040 /*
1041 * This code assumes that the first line of /proc/self/maps holds
1042 * information about the text, for example:
1043 *
1044 * 5622d9907000-5622d9a55000 r-xp 00000000 08:01 15067168 u-boot
1045 *
1046 * The first hex value is assumed to be the address.
1047 *
1048 * This is tested in Linux 4.15.
1049 */
1050 fd = open("/proc/self/maps", O_RDONLY);
1051 if (fd == -1)
1052 return NULL;
1053 len = read(fd, line, sizeof(line));
1054 if (len > 0) {
1055 char *end = memchr(line, '-', len);
1056
1057 if (end) {
Heinrich Schuchardt05a16842019-10-26 23:17:44 +02001058 uintptr_t addr;
Simon Glass752707a2019-04-08 13:20:41 -06001059
1060 *end = '\0';
Heinrich Schuchardt05a16842019-10-26 23:17:44 +02001061 if (sscanf(line, "%zx", &addr) == 1)
Simon Glass752707a2019-04-08 13:20:41 -06001062 base = (void *)addr;
1063 }
1064 }
1065 close(fd);
1066
1067 return base;
1068}
Heinrich Schuchardt1c678442020-10-27 20:29:25 +01001069
Heinrich Schuchardt79cb2412022-09-02 02:32:25 +02001070/**
1071 * os_unblock_signals() - unblock all signals
1072 *
1073 * If we are relaunching the sandbox in a signal handler, we have to unblock
1074 * the respective signal before calling execv(). See signal(7) man-page.
1075 */
1076static void os_unblock_signals(void)
1077{
1078 sigset_t sigs;
1079
1080 sigfillset(&sigs);
1081 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
1082}
1083
Heinrich Schuchardt1c678442020-10-27 20:29:25 +01001084void os_relaunch(char *argv[])
1085{
Heinrich Schuchardt79cb2412022-09-02 02:32:25 +02001086 os_unblock_signals();
1087
Heinrich Schuchardt1c678442020-10-27 20:29:25 +01001088 execv(argv[0], argv);
1089 os_exit(1);
1090}
Andrew Scullca5d1372022-05-30 10:00:10 +00001091
Andrew Scull2b40f802022-05-30 10:00:11 +00001092
1093#ifdef CONFIG_FUZZ
1094static void *fuzzer_thread(void * ptr)
1095{
1096 char cmd[64];
1097 char *argv[5] = {"./u-boot", "-T", "-c", cmd, NULL};
1098 const char *fuzz_test;
1099
1100 /* Find which test to run from an environment variable. */
1101 fuzz_test = getenv("UBOOT_SB_FUZZ_TEST");
1102 if (!fuzz_test)
1103 os_abort();
1104
1105 snprintf(cmd, sizeof(cmd), "fuzz %s", fuzz_test);
1106
1107 sandbox_main(4, argv);
1108 os_abort();
1109 return NULL;
1110}
1111
1112static bool fuzzer_initialized = false;
1113static pthread_mutex_t fuzzer_mutex = PTHREAD_MUTEX_INITIALIZER;
1114static pthread_cond_t fuzzer_cond = PTHREAD_COND_INITIALIZER;
1115static const uint8_t *fuzzer_data;
1116static size_t fuzzer_size;
1117
1118int sandbox_fuzzing_engine_get_input(const uint8_t **data, size_t *size)
1119{
1120 if (!fuzzer_initialized)
1121 return -ENOSYS;
1122
1123 /* Tell the main thread we need new inputs then wait for them. */
1124 pthread_mutex_lock(&fuzzer_mutex);
1125 pthread_cond_signal(&fuzzer_cond);
1126 pthread_cond_wait(&fuzzer_cond, &fuzzer_mutex);
1127 *data = fuzzer_data;
1128 *size = fuzzer_size;
1129 pthread_mutex_unlock(&fuzzer_mutex);
1130 return 0;
1131}
1132
1133int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
1134{
1135 static pthread_t tid;
1136
1137 pthread_mutex_lock(&fuzzer_mutex);
1138
1139 /* Initialize the sandbox on another thread. */
1140 if (!fuzzer_initialized) {
1141 fuzzer_initialized = true;
1142 if (pthread_create(&tid, NULL, fuzzer_thread, NULL))
1143 os_abort();
1144 pthread_cond_wait(&fuzzer_cond, &fuzzer_mutex);
1145 }
1146
1147 /* Hand over the input. */
1148 fuzzer_data = data;
1149 fuzzer_size = size;
1150 pthread_cond_signal(&fuzzer_cond);
1151
1152 /* Wait for the inputs to be finished with. */
1153 pthread_cond_wait(&fuzzer_cond, &fuzzer_mutex);
1154 pthread_mutex_unlock(&fuzzer_mutex);
1155
1156 return 0;
1157}
1158#else
Andrew Scullca5d1372022-05-30 10:00:10 +00001159int main(int argc, char *argv[])
1160{
1161 return sandbox_main(argc, argv);
1162}
Andrew Scull2b40f802022-05-30 10:00:11 +00001163#endif