blob: 5e66304e2b9dfdd544d99563b0a56b186d83e051 [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
Simon Glass7b9cf84f2021-08-18 21:40:30 -0600169int os_filesize(int fd)
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 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;
221 int size;
222 int ifd;
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);
232 return -EIO;
233 }
234
235 ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
236 if (ptr == MAP_FAILED) {
237 printf("Can't map file '%s': %s\n", pathname, strerror(errno));
238 return -EPERM;
239 }
240
241 *bufp = ptr;
242 *sizep = size;
243
244 return 0;
245}
246
Simon Glass5c1fd582021-10-23 17:25:58 -0600247int os_unmap(void *buf, int size)
248{
249 if (munmap(buf, size)) {
250 printf("Can't unmap %p %x\n", buf, size);
251 return -EIO;
252 }
253
254 return 0;
255}
256
Mike Frysingera5baaee2011-10-26 00:21:29 +0000257/* Restore tty state when we exit */
258static struct termios orig_term;
Simon Glass678ef472014-02-27 13:26:22 -0700259static bool term_setup;
Simon Glassae50ec72018-10-01 11:55:20 -0600260static bool term_nonblock;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000261
Simon Glass9c3b7d62015-05-10 21:07:27 -0600262void os_fd_restore(void)
Mike Frysingera5baaee2011-10-26 00:21:29 +0000263{
Simon Glass9c3b7d62015-05-10 21:07:27 -0600264 if (term_setup) {
Simon Glassae50ec72018-10-01 11:55:20 -0600265 int flags;
266
Simon Glass678ef472014-02-27 13:26:22 -0700267 tcsetattr(0, TCSANOW, &orig_term);
Simon Glassae50ec72018-10-01 11:55:20 -0600268 if (term_nonblock) {
269 flags = fcntl(0, F_GETFL, 0);
270 fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
271 }
Simon Glass9c3b7d62015-05-10 21:07:27 -0600272 term_setup = false;
273 }
Mike Frysingera5baaee2011-10-26 00:21:29 +0000274}
275
Rasmus Villemoes2b72ad22020-02-14 10:58:37 +0000276static void os_sigint_handler(int sig)
277{
278 os_fd_restore();
279 signal(SIGINT, SIG_DFL);
280 raise(SIGINT);
281}
282
Heinrich Schuchardt28eb5092020-11-12 00:29:56 +0100283static void os_signal_handler(int sig, siginfo_t *info, void *con)
284{
285 ucontext_t __maybe_unused *context = con;
286 unsigned long pc;
287
288#if defined(__x86_64__)
289 pc = context->uc_mcontext.gregs[REG_RIP];
290#elif defined(__aarch64__)
291 pc = context->uc_mcontext.pc;
292#elif defined(__riscv)
293 pc = context->uc_mcontext.__gregs[REG_PC];
294#else
295 const char msg[] =
296 "\nUnsupported architecture, cannot read program counter\n";
297
298 os_write(1, msg, sizeof(msg));
299 pc = 0;
300#endif
301
302 os_signal_action(sig, pc);
303}
304
305int os_setup_signal_handlers(void)
306{
307 struct sigaction act;
308
309 act.sa_sigaction = os_signal_handler;
310 sigemptyset(&act.sa_mask);
Heinrich Schuchardtd3741bc2021-07-05 19:43:00 +0200311 act.sa_flags = SA_SIGINFO;
Heinrich Schuchardt28eb5092020-11-12 00:29:56 +0100312 if (sigaction(SIGILL, &act, NULL) ||
313 sigaction(SIGBUS, &act, NULL) ||
314 sigaction(SIGSEGV, &act, NULL))
315 return -1;
316 return 0;
317}
318
Mike Frysingera5baaee2011-10-26 00:21:29 +0000319/* Put tty into raw mode so <tab> and <ctrl+c> work */
Simon Glass678ef472014-02-27 13:26:22 -0700320void os_tty_raw(int fd, bool allow_sigs)
Mike Frysingera5baaee2011-10-26 00:21:29 +0000321{
Mike Frysingera5baaee2011-10-26 00:21:29 +0000322 struct termios term;
Simon Glassae50ec72018-10-01 11:55:20 -0600323 int flags;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000324
Simon Glass678ef472014-02-27 13:26:22 -0700325 if (term_setup)
Mike Frysingera5baaee2011-10-26 00:21:29 +0000326 return;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000327
328 /* If not a tty, don't complain */
329 if (tcgetattr(fd, &orig_term))
330 return;
331
332 term = orig_term;
333 term.c_iflag = IGNBRK | IGNPAR;
334 term.c_oflag = OPOST | ONLCR;
335 term.c_cflag = CS8 | CREAD | CLOCAL;
Simon Glass678ef472014-02-27 13:26:22 -0700336 term.c_lflag = allow_sigs ? ISIG : 0;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000337 if (tcsetattr(fd, TCSANOW, &term))
338 return;
339
Simon Glassae50ec72018-10-01 11:55:20 -0600340 flags = fcntl(fd, F_GETFL, 0);
341 if (!(flags & O_NONBLOCK)) {
342 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
343 return;
344 term_nonblock = true;
345 }
346
Simon Glass9c3b7d62015-05-10 21:07:27 -0600347 term_setup = true;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000348 atexit(os_fd_restore);
Rasmus Villemoes2b72ad22020-02-14 10:58:37 +0000349 signal(SIGINT, os_sigint_handler);
Mike Frysingera5baaee2011-10-26 00:21:29 +0000350}
Matthias Weisserb5f7b472011-11-05 11:40:34 +0100351
Simon Glass4c902fa2021-02-06 09:57:32 -0700352/*
353 * Provide our own malloc so we don't use space in the sandbox ram_buf for
354 * allocations that are internal to sandbox, or need to be done before U-Boot's
355 * malloc() is ready.
356 */
Matthias Weisserb5f7b472011-11-05 11:40:34 +0100357void *os_malloc(size_t length)
358{
Simon Glassbe005d82018-09-15 00:50:54 -0600359 int page_size = getpagesize();
Simon Glassfc2dde82019-04-08 13:20:42 -0600360 struct os_mem_hdr *hdr;
Simon Glass1e6594c2013-11-10 10:26:57 -0700361
Simon Glass4c902fa2021-02-06 09:57:32 -0700362 if (!length)
363 return NULL;
Simon Glass57ba9422018-06-17 08:57:43 -0600364 /*
365 * Use an address that is hopefully available to us so that pointers
366 * to this memory are fairly obvious. If we end up with a different
367 * address, that's fine too.
368 */
369 hdr = mmap((void *)0x10000000, length + page_size,
Alexander Graf934a5452018-06-22 14:44:13 +0200370 PROT_READ | PROT_WRITE | PROT_EXEC,
371 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Simon Glass1e6594c2013-11-10 10:26:57 -0700372 if (hdr == MAP_FAILED)
373 return NULL;
374 hdr->length = length;
375
Simon Glassbe005d82018-09-15 00:50:54 -0600376 return (void *)hdr + page_size;
Simon Glass1e6594c2013-11-10 10:26:57 -0700377}
378
Masahiro Yamadaee957282014-01-15 13:06:41 +0900379void os_free(void *ptr)
Simon Glass1e6594c2013-11-10 10:26:57 -0700380{
Simon Glassfc2dde82019-04-08 13:20:42 -0600381 int page_size = getpagesize();
382 struct os_mem_hdr *hdr;
Simon Glass1e6594c2013-11-10 10:26:57 -0700383
Simon Glassfc2dde82019-04-08 13:20:42 -0600384 if (ptr) {
385 hdr = ptr - page_size;
386 munmap(hdr, hdr->length + page_size);
387 }
Simon Glass4c902fa2021-02-06 09:57:32 -0700388}
389
390/* These macros are from kernel.h but not accessible in this file */
391#define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a) - 1)
392#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
393
394/*
395 * Provide our own malloc so we don't use space in the sandbox ram_buf for
396 * allocations that are internal to sandbox, or need to be done before U-Boot's
397 * malloc() is ready.
398 */
399void *os_realloc(void *ptr, size_t length)
400{
401 int page_size = getpagesize();
402 struct os_mem_hdr *hdr;
403 void *new_ptr;
404
405 /* Reallocating a NULL pointer is just an alloc */
406 if (!ptr)
407 return os_malloc(length);
408
409 /* Changing a length to 0 is just a free */
410 if (length) {
411 os_free(ptr);
412 return NULL;
413 }
414
415 /*
416 * If the new size is the same number of pages as the old, nothing to
417 * do. There isn't much point in shrinking things
418 */
419 hdr = ptr - page_size;
420 if (ALIGN(length, page_size) <= ALIGN(hdr->length, page_size))
421 return ptr;
422
423 /* We have to grow it, so allocate something new */
424 new_ptr = os_malloc(length);
425 memcpy(new_ptr, ptr, hdr->length);
426 os_free(ptr);
427
428 return new_ptr;
Simon Glass1e6594c2013-11-10 10:26:57 -0700429}
430
Matthias Weisser0d3dd142011-11-29 12:16:40 +0100431void os_usleep(unsigned long usec)
432{
433 usleep(usec);
434}
435
Simon Glassfb4b4e82013-05-19 16:45:35 -0700436uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
Matthias Weisser0d3dd142011-11-29 12:16:40 +0100437{
438#if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
439 struct timespec tp;
440 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
441 struct timeval tv;
442
443 gettimeofday(&tv, NULL);
444 tp.tv_sec = tv.tv_sec;
445 tp.tv_nsec = tv.tv_usec * 1000;
446 }
447 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
448#else
449 struct timeval tv;
450 gettimeofday(&tv, NULL);
451 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
452#endif
453}
Simon Glass8a3e0352012-02-15 15:51:16 -0800454
455static char *short_opts;
456static struct option *long_opts;
457
458int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
459{
Marek Behún184c4af2021-05-20 13:24:06 +0200460 struct sandbox_cmdline_option **sb_opt =
461 __u_boot_sandbox_option_start();
Simon Glass8a3e0352012-02-15 15:51:16 -0800462 size_t num_options = __u_boot_sandbox_option_count();
463 size_t i;
464
465 int hidden_short_opt;
466 size_t si;
467
468 int c;
469
470 if (short_opts || long_opts)
471 return 1;
472
473 state->argc = argc;
474 state->argv = argv;
475
476 /* dynamically construct the arguments to the system getopt_long */
Simon Glassedd094e2021-02-06 09:57:33 -0700477 short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
478 long_opts = os_malloc(sizeof(*long_opts) * (num_options + 1));
Simon Glass8a3e0352012-02-15 15:51:16 -0800479 if (!short_opts || !long_opts)
480 return 1;
481
482 /*
483 * getopt_long requires "val" to be unique (since that is what the
484 * func returns), so generate unique values automatically for flags
485 * that don't have a short option. pick 0x100 as that is above the
486 * single byte range (where ASCII/ISO-XXXX-X charsets live).
487 */
488 hidden_short_opt = 0x100;
489 si = 0;
490 for (i = 0; i < num_options; ++i) {
491 long_opts[i].name = sb_opt[i]->flag;
492 long_opts[i].has_arg = sb_opt[i]->has_arg ?
493 required_argument : no_argument;
494 long_opts[i].flag = NULL;
495
496 if (sb_opt[i]->flag_short) {
497 short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
498 if (long_opts[i].has_arg == required_argument)
499 short_opts[si++] = ':';
500 } else
501 long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
502 }
503 short_opts[si] = '\0';
504
505 /* we need to handle output ourselves since u-boot provides printf */
506 opterr = 0;
507
Simon Glass0a4eabb2020-02-03 07:36:04 -0700508 memset(&long_opts[num_options], '\0', sizeof(*long_opts));
Simon Glass8a3e0352012-02-15 15:51:16 -0800509 /*
510 * walk all of the options the user gave us on the command line,
511 * figure out what u-boot option structure they belong to (via
512 * the unique short val key), and call the appropriate callback.
513 */
514 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
515 for (i = 0; i < num_options; ++i) {
516 if (sb_opt[i]->flag_short == c) {
517 if (sb_opt[i]->callback(state, optarg)) {
518 state->parse_err = sb_opt[i]->flag;
519 return 0;
520 }
521 break;
522 }
523 }
524 if (i == num_options) {
525 /*
526 * store the faulting flag for later display. we have to
527 * store the flag itself as the getopt parsing itself is
528 * tricky: need to handle the following flags (assume all
529 * of the below are unknown):
530 * -a optopt='a' optind=<next>
531 * -abbbb optopt='a' optind=<this>
532 * -aaaaa optopt='a' optind=<this>
533 * --a optopt=0 optind=<this>
534 * as you can see, it is impossible to determine the exact
535 * faulting flag without doing the parsing ourselves, so
536 * we just report the specific flag that failed.
537 */
538 if (optopt) {
539 static char parse_err[3] = { '-', 0, '\0', };
540 parse_err[1] = optopt;
541 state->parse_err = parse_err;
542 } else
543 state->parse_err = argv[optind - 1];
544 break;
545 }
546 }
547
548 return 0;
549}
Simon Glassf1c45c82012-12-26 09:53:34 +0000550
551void os_dirent_free(struct os_dirent_node *node)
552{
553 struct os_dirent_node *next;
554
555 while (node) {
556 next = node->next;
Simon Glassedd094e2021-02-06 09:57:33 -0700557 os_free(node);
Simon Glassf1c45c82012-12-26 09:53:34 +0000558 node = next;
559 }
560}
561
562int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
563{
Stefan Brünsae71ede2016-10-01 20:41:42 +0200564 struct dirent *entry;
Simon Glassf1c45c82012-12-26 09:53:34 +0000565 struct os_dirent_node *head, *node, *next;
566 struct stat buf;
567 DIR *dir;
568 int ret;
569 char *fname;
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200570 char *old_fname;
Simon Glassf1c45c82012-12-26 09:53:34 +0000571 int len;
Stefan Brünsb2129902016-10-01 20:41:40 +0200572 int dirlen;
Simon Glassf1c45c82012-12-26 09:53:34 +0000573
574 *headp = NULL;
575 dir = opendir(dirname);
576 if (!dir)
577 return -1;
578
Stefan Brünsb2129902016-10-01 20:41:40 +0200579 /* Create a buffer upfront, with typically sufficient size */
580 dirlen = strlen(dirname) + 2;
581 len = dirlen + 256;
Simon Glassedd094e2021-02-06 09:57:33 -0700582 fname = os_malloc(len);
Simon Glassf1c45c82012-12-26 09:53:34 +0000583 if (!fname) {
584 ret = -ENOMEM;
585 goto done;
586 }
587
588 for (node = head = NULL;; node = next) {
Stefan Brünsae71ede2016-10-01 20:41:42 +0200589 errno = 0;
590 entry = readdir(dir);
591 if (!entry) {
592 ret = errno;
Simon Glassf1c45c82012-12-26 09:53:34 +0000593 break;
Stefan Brünsae71ede2016-10-01 20:41:42 +0200594 }
Simon Glassedd094e2021-02-06 09:57:33 -0700595 next = os_malloc(sizeof(*node) + strlen(entry->d_name) + 1);
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200596 if (!next) {
Simon Glassf1c45c82012-12-26 09:53:34 +0000597 os_dirent_free(head);
598 ret = -ENOMEM;
599 goto done;
600 }
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200601 if (dirlen + strlen(entry->d_name) > len) {
602 len = dirlen + strlen(entry->d_name);
603 old_fname = fname;
Simon Glassedd094e2021-02-06 09:57:33 -0700604 fname = os_realloc(fname, len);
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200605 if (!fname) {
Simon Glassedd094e2021-02-06 09:57:33 -0700606 os_free(old_fname);
607 os_free(next);
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200608 os_dirent_free(head);
609 ret = -ENOMEM;
610 goto done;
611 }
612 }
Stephen Warren9671c672014-06-11 10:26:23 -0600613 next->next = NULL;
Stefan Brünsae71ede2016-10-01 20:41:42 +0200614 strcpy(next->name, entry->d_name);
615 switch (entry->d_type) {
Simon Glassf1c45c82012-12-26 09:53:34 +0000616 case DT_REG:
617 next->type = OS_FILET_REG;
618 break;
619 case DT_DIR:
620 next->type = OS_FILET_DIR;
621 break;
622 case DT_LNK:
623 next->type = OS_FILET_LNK;
624 break;
Stefan Brünsb7ffa822016-10-04 21:46:35 +0200625 default:
626 next->type = OS_FILET_UNKNOWN;
Simon Glassf1c45c82012-12-26 09:53:34 +0000627 }
628 next->size = 0;
629 snprintf(fname, len, "%s/%s", dirname, next->name);
630 if (!stat(fname, &buf))
631 next->size = buf.st_size;
632 if (node)
633 node->next = next;
Stefan Brünsd4cb8882016-10-01 20:41:39 +0200634 else
635 head = next;
Simon Glassf1c45c82012-12-26 09:53:34 +0000636 }
637 *headp = head;
638
639done:
640 closedir(dir);
Simon Glassedd094e2021-02-06 09:57:33 -0700641 os_free(fname);
Simon Glassf1c45c82012-12-26 09:53:34 +0000642 return ret;
643}
644
645const char *os_dirent_typename[OS_FILET_COUNT] = {
646 " ",
647 "SYM",
648 "DIR",
649 "???",
650};
651
652const char *os_dirent_get_typename(enum os_dirent_t type)
653{
Tom Rinib6f605e2017-05-13 20:11:30 -0400654 if (type >= OS_FILET_REG && type < OS_FILET_COUNT)
Simon Glassf1c45c82012-12-26 09:53:34 +0000655 return os_dirent_typename[type];
656
657 return os_dirent_typename[OS_FILET_UNKNOWN];
658}
659
Heinrich Schuchardt011a1e02022-01-11 01:50:24 +0100660/*
661 * For compatibility reasons avoid loff_t here.
662 * U-Boot defines loff_t as long long.
663 * But /usr/include/linux/types.h may not define it at all.
664 * Alpine Linux being one example.
665 */
666int os_get_filesize(const char *fname, long long *size)
Simon Glassf1c45c82012-12-26 09:53:34 +0000667{
668 struct stat buf;
669 int ret;
670
671 ret = stat(fname, &buf);
672 if (ret)
673 return ret;
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800674 *size = buf.st_size;
675 return 0;
Simon Glassf1c45c82012-12-26 09:53:34 +0000676}
Simon Glass3e9fd242013-11-10 10:27:01 -0700677
Simon Glass29d11432017-12-04 13:48:17 -0700678void os_putc(int ch)
679{
Simon Glassd56c6f42022-03-27 14:26:14 -0600680 os_write(1, &ch, 1);
Simon Glass29d11432017-12-04 13:48:17 -0700681}
682
683void os_puts(const char *str)
684{
685 while (*str)
686 os_putc(*str++);
687}
688
Pali Rohár4acd1a02022-09-05 11:31:16 +0200689void os_flush(void)
690{
691 fflush(stdout);
692}
693
Simon Glass9dd10bf2013-11-10 10:27:03 -0700694int os_write_ram_buf(const char *fname)
695{
696 struct sandbox_state *state = state_get_current();
697 int fd, ret;
698
699 fd = open(fname, O_CREAT | O_WRONLY, 0777);
700 if (fd < 0)
701 return -ENOENT;
702 ret = write(fd, state->ram_buf, state->ram_size);
703 close(fd);
704 if (ret != state->ram_size)
705 return -EIO;
706
707 return 0;
708}
709
710int os_read_ram_buf(const char *fname)
711{
712 struct sandbox_state *state = state_get_current();
713 int fd, ret;
Heinrich Schuchardt011a1e02022-01-11 01:50:24 +0100714 long long size;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700715
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800716 ret = os_get_filesize(fname, &size);
717 if (ret < 0)
718 return ret;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700719 if (size != state->ram_size)
720 return -ENOSPC;
721 fd = open(fname, O_RDONLY);
722 if (fd < 0)
723 return -ENOENT;
724
725 ret = read(fd, state->ram_buf, state->ram_size);
726 close(fd);
727 if (ret != state->ram_size)
728 return -EIO;
729
730 return 0;
731}
Simon Glass860b7d92014-02-27 13:26:15 -0700732
733static int make_exec(char *fname, const void *data, int size)
734{
735 int fd;
736
737 strcpy(fname, "/tmp/u-boot.jump.XXXXXX");
738 fd = mkstemp(fname);
739 if (fd < 0)
740 return -ENOENT;
741 if (write(fd, data, size) < 0)
742 return -EIO;
743 close(fd);
744 if (chmod(fname, 0777))
745 return -ENOEXEC;
746
747 return 0;
748}
749
Simon Glass7dafd022018-11-15 18:44:05 -0700750/**
751 * add_args() - Allocate a new argv with the given args
752 *
753 * This is used to create a new argv array with all the old arguments and some
754 * new ones that are passed in
755 *
756 * @argvp: Returns newly allocated args list
757 * @add_args: Arguments to add, each a string
758 * @count: Number of arguments in @add_args
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100759 * Return: 0 if OK, -ENOMEM if out of memory
Simon Glass7dafd022018-11-15 18:44:05 -0700760 */
761static int add_args(char ***argvp, char *add_args[], int count)
Simon Glass860b7d92014-02-27 13:26:15 -0700762{
Simon Glass7465f002018-11-15 18:44:07 -0700763 char **argv, **ap;
Simon Glass860b7d92014-02-27 13:26:15 -0700764 int argc;
765
Simon Glass7465f002018-11-15 18:44:07 -0700766 for (argc = 0; (*argvp)[argc]; argc++)
Simon Glass860b7d92014-02-27 13:26:15 -0700767 ;
768
Simon Glassedd094e2021-02-06 09:57:33 -0700769 argv = os_malloc((argc + count + 1) * sizeof(char *));
Simon Glass860b7d92014-02-27 13:26:15 -0700770 if (!argv) {
771 printf("Out of memory for %d argv\n", count);
772 return -ENOMEM;
773 }
Simon Glass7465f002018-11-15 18:44:07 -0700774 for (ap = *argvp, argc = 0; *ap; ap++) {
775 char *arg = *ap;
776
777 /* Drop args that we don't want to propagate */
778 if (*arg == '-' && strlen(arg) == 2) {
779 switch (arg[1]) {
780 case 'j':
781 case 'm':
782 ap++;
783 continue;
784 }
785 } else if (!strcmp(arg, "--rm_memory")) {
Simon Glass7465f002018-11-15 18:44:07 -0700786 continue;
787 }
788 argv[argc++] = arg;
789 }
790
Simon Glass860b7d92014-02-27 13:26:15 -0700791 memcpy(argv + argc, add_args, count * sizeof(char *));
792 argv[argc + count] = NULL;
793
794 *argvp = argv;
795 return 0;
796}
797
Simon Glass7dafd022018-11-15 18:44:05 -0700798/**
799 * os_jump_to_file() - Jump to a new program
800 *
801 * This saves the memory buffer, sets up arguments to the new process, then
802 * execs it.
803 *
804 * @fname: Filename to exec
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100805 * Return: does not return on success, any return value is an error
Simon Glass7dafd022018-11-15 18:44:05 -0700806 */
Simon Glassdafc5d72021-03-15 18:11:07 +1300807static int os_jump_to_file(const char *fname, bool delete_it)
Simon Glass860b7d92014-02-27 13:26:15 -0700808{
809 struct sandbox_state *state = state_get_current();
Simon Glass7dafd022018-11-15 18:44:05 -0700810 char mem_fname[30];
Simon Glass860b7d92014-02-27 13:26:15 -0700811 int fd, err;
Simon Glass7dafd022018-11-15 18:44:05 -0700812 char *extra_args[5];
Simon Glass860b7d92014-02-27 13:26:15 -0700813 char **argv = state->argv;
Simon Glass7465f002018-11-15 18:44:07 -0700814 int argc;
Simon Glass860b7d92014-02-27 13:26:15 -0700815#ifdef DEBUG
Simon Glass7dafd022018-11-15 18:44:05 -0700816 int i;
Simon Glass860b7d92014-02-27 13:26:15 -0700817#endif
818
Simon Glass860b7d92014-02-27 13:26:15 -0700819 strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
820 fd = mkstemp(mem_fname);
821 if (fd < 0)
822 return -ENOENT;
823 close(fd);
824 err = os_write_ram_buf(mem_fname);
825 if (err)
826 return err;
827
828 os_fd_restore();
829
Simon Glassdafc5d72021-03-15 18:11:07 +1300830 argc = 0;
831 if (delete_it) {
832 extra_args[argc++] = "-j";
833 extra_args[argc++] = (char *)fname;
834 }
835 extra_args[argc++] = "-m";
836 extra_args[argc++] = mem_fname;
Simon Glass7465f002018-11-15 18:44:07 -0700837 if (state->ram_buf_rm)
838 extra_args[argc++] = "--rm_memory";
839 err = add_args(&argv, extra_args, argc);
Simon Glass860b7d92014-02-27 13:26:15 -0700840 if (err)
841 return err;
Simon Glass7465f002018-11-15 18:44:07 -0700842 argv[0] = (char *)fname;
Simon Glass860b7d92014-02-27 13:26:15 -0700843
844#ifdef DEBUG
845 for (i = 0; argv[i]; i++)
846 printf("%d %s\n", i, argv[i]);
847#endif
848
849 if (state_uninit())
850 os_exit(2);
851
852 err = execv(fname, argv);
Simon Glassedd094e2021-02-06 09:57:33 -0700853 os_free(argv);
Simon Glasscca25522018-11-15 18:44:08 -0700854 if (err) {
855 perror("Unable to run image");
Simon Glass23eabbb2018-11-23 21:29:24 -0700856 printf("Image filename '%s'\n", fname);
Simon Glass860b7d92014-02-27 13:26:15 -0700857 return err;
Simon Glasscca25522018-11-15 18:44:08 -0700858 }
Simon Glass860b7d92014-02-27 13:26:15 -0700859
Simon Glassdafc5d72021-03-15 18:11:07 +1300860 if (delete_it)
861 return unlink(fname);
862
863 return -EFAULT;
Simon Glass860b7d92014-02-27 13:26:15 -0700864}
Simon Glass504548f2015-04-20 12:37:22 -0600865
Simon Glass7dafd022018-11-15 18:44:05 -0700866int os_jump_to_image(const void *dest, int size)
867{
868 char fname[30];
869 int err;
870
871 err = make_exec(fname, dest, size);
872 if (err)
873 return err;
874
Simon Glassdafc5d72021-03-15 18:11:07 +1300875 return os_jump_to_file(fname, true);
Simon Glass7dafd022018-11-15 18:44:05 -0700876}
877
Simon Glass1cd06002021-07-05 16:32:45 -0600878int os_find_u_boot(char *fname, int maxlen, bool use_img,
879 const char *cur_prefix, const char *next_prefix)
Simon Glass2c931ba2016-07-04 11:57:45 -0600880{
881 struct sandbox_state *state = state_get_current();
882 const char *progname = state->argv[0];
883 int len = strlen(progname);
Simon Glass1cd06002021-07-05 16:32:45 -0600884 char subdir[10];
885 char *suffix;
Simon Glass2c931ba2016-07-04 11:57:45 -0600886 char *p;
887 int fd;
888
889 if (len >= maxlen || len < 4)
890 return -ENOSPC;
891
Simon Glass2c931ba2016-07-04 11:57:45 -0600892 strcpy(fname, progname);
Simon Glassdc4d8eb2018-10-01 11:55:10 -0600893 suffix = fname + len - 4;
894
Simon Glass1cd06002021-07-05 16:32:45 -0600895 /* Change the existing suffix to the new one */
896 if (*suffix != '-')
897 return -EINVAL;
Simon Glassdc4d8eb2018-10-01 11:55:10 -0600898
Simon Glass1cd06002021-07-05 16:32:45 -0600899 if (*next_prefix)
900 strcpy(suffix + 1, next_prefix); /* e.g. "-tpl" to "-spl" */
901 else
902 *suffix = '\0'; /* e.g. "-spl" to "" */
903 fd = os_open(fname, O_RDONLY);
904 if (fd >= 0) {
905 close(fd);
906 return 0;
Simon Glassdc4d8eb2018-10-01 11:55:10 -0600907 }
908
Simon Glass1cd06002021-07-05 16:32:45 -0600909 /*
910 * We didn't find it, so try looking for 'u-boot-xxx' in the xxx/
911 * directory. Replace the old dirname with the new one.
912 */
913 snprintf(subdir, sizeof(subdir), "/%s/", cur_prefix);
914 p = strstr(fname, subdir);
Simon Glass2c931ba2016-07-04 11:57:45 -0600915 if (p) {
Simon Glass1cd06002021-07-05 16:32:45 -0600916 if (*next_prefix)
917 /* e.g. ".../tpl/u-boot-spl" to "../spl/u-boot-spl" */
918 memcpy(p + 1, next_prefix, strlen(next_prefix));
919 else
920 /* e.g. ".../spl/u-boot" to ".../u-boot" */
921 strcpy(p, p + 1 + strlen(cur_prefix));
Simon Glassb90e4872021-03-07 17:35:13 -0700922 if (use_img)
923 strcat(p, ".img");
Simon Glass1cd06002021-07-05 16:32:45 -0600924
Simon Glass2c931ba2016-07-04 11:57:45 -0600925 fd = os_open(fname, O_RDONLY);
926 if (fd >= 0) {
927 close(fd);
928 return 0;
929 }
930 }
931
932 return -ENOENT;
933}
934
935int os_spl_to_uboot(const char *fname)
936{
Patrick Delaunay6daf9052020-11-20 09:48:33 +0100937 struct sandbox_state *state = state_get_current();
938
Patrick Delaunay6daf9052020-11-20 09:48:33 +0100939 /* U-Boot will delete ram buffer after read: "--rm_memory"*/
940 state->ram_buf_rm = true;
Simon Glassdafc5d72021-03-15 18:11:07 +1300941
942 return os_jump_to_file(fname, false);
Simon Glass2c931ba2016-07-04 11:57:45 -0600943}
944
Heinrich Schuchardtc0d1a002020-12-30 18:07:48 +0100945long os_get_time_offset(void)
946{
947 const char *offset;
948
949 offset = getenv(ENV_TIME_OFFSET);
950 if (offset)
951 return strtol(offset, NULL, 0);
952 return 0;
953}
954
955void os_set_time_offset(long offset)
956{
957 char buf[21];
958 int ret;
959
960 snprintf(buf, sizeof(buf), "%ld", offset);
961 ret = setenv(ENV_TIME_OFFSET, buf, true);
962 if (ret)
963 printf("Could not set environment variable %s\n",
964 ENV_TIME_OFFSET);
965}
966
Simon Glass504548f2015-04-20 12:37:22 -0600967void os_localtime(struct rtc_time *rt)
968{
969 time_t t = time(NULL);
970 struct tm *tm;
971
972 tm = localtime(&t);
973 rt->tm_sec = tm->tm_sec;
974 rt->tm_min = tm->tm_min;
975 rt->tm_hour = tm->tm_hour;
976 rt->tm_mday = tm->tm_mday;
977 rt->tm_mon = tm->tm_mon + 1;
978 rt->tm_year = tm->tm_year + 1900;
979 rt->tm_wday = tm->tm_wday;
980 rt->tm_yday = tm->tm_yday;
981 rt->tm_isdst = tm->tm_isdst;
982}
Simon Glass5dccd152018-05-16 09:42:22 -0600983
Simon Glassb7255ef2018-09-15 00:50:55 -0600984void os_abort(void)
985{
986 abort();
987}
Simon Glass4e766c22018-10-01 21:12:32 -0600988
989int os_mprotect_allow(void *start, size_t len)
990{
991 int page_size = getpagesize();
992
993 /* Move start to the start of a page, len to the end */
994 start = (void *)(((ulong)start) & ~(page_size - 1));
995 len = (len + page_size * 2) & ~(page_size - 1);
996
997 return mprotect(start, len, PROT_READ | PROT_WRITE);
998}
Simon Glass752707a2019-04-08 13:20:41 -0600999
1000void *os_find_text_base(void)
1001{
1002 char line[500];
1003 void *base = NULL;
1004 int len;
1005 int fd;
1006
1007 /*
1008 * This code assumes that the first line of /proc/self/maps holds
1009 * information about the text, for example:
1010 *
1011 * 5622d9907000-5622d9a55000 r-xp 00000000 08:01 15067168 u-boot
1012 *
1013 * The first hex value is assumed to be the address.
1014 *
1015 * This is tested in Linux 4.15.
1016 */
1017 fd = open("/proc/self/maps", O_RDONLY);
1018 if (fd == -1)
1019 return NULL;
1020 len = read(fd, line, sizeof(line));
1021 if (len > 0) {
1022 char *end = memchr(line, '-', len);
1023
1024 if (end) {
Heinrich Schuchardt05a16842019-10-26 23:17:44 +02001025 uintptr_t addr;
Simon Glass752707a2019-04-08 13:20:41 -06001026
1027 *end = '\0';
Heinrich Schuchardt05a16842019-10-26 23:17:44 +02001028 if (sscanf(line, "%zx", &addr) == 1)
Simon Glass752707a2019-04-08 13:20:41 -06001029 base = (void *)addr;
1030 }
1031 }
1032 close(fd);
1033
1034 return base;
1035}
Heinrich Schuchardt1c678442020-10-27 20:29:25 +01001036
Heinrich Schuchardt79cb2412022-09-02 02:32:25 +02001037/**
1038 * os_unblock_signals() - unblock all signals
1039 *
1040 * If we are relaunching the sandbox in a signal handler, we have to unblock
1041 * the respective signal before calling execv(). See signal(7) man-page.
1042 */
1043static void os_unblock_signals(void)
1044{
1045 sigset_t sigs;
1046
1047 sigfillset(&sigs);
1048 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
1049}
1050
Heinrich Schuchardt1c678442020-10-27 20:29:25 +01001051void os_relaunch(char *argv[])
1052{
Heinrich Schuchardt79cb2412022-09-02 02:32:25 +02001053 os_unblock_signals();
1054
Heinrich Schuchardt1c678442020-10-27 20:29:25 +01001055 execv(argv[0], argv);
1056 os_exit(1);
1057}
Andrew Scullca5d1372022-05-30 10:00:10 +00001058
Andrew Scull2b40f802022-05-30 10:00:11 +00001059
1060#ifdef CONFIG_FUZZ
1061static void *fuzzer_thread(void * ptr)
1062{
1063 char cmd[64];
1064 char *argv[5] = {"./u-boot", "-T", "-c", cmd, NULL};
1065 const char *fuzz_test;
1066
1067 /* Find which test to run from an environment variable. */
1068 fuzz_test = getenv("UBOOT_SB_FUZZ_TEST");
1069 if (!fuzz_test)
1070 os_abort();
1071
1072 snprintf(cmd, sizeof(cmd), "fuzz %s", fuzz_test);
1073
1074 sandbox_main(4, argv);
1075 os_abort();
1076 return NULL;
1077}
1078
1079static bool fuzzer_initialized = false;
1080static pthread_mutex_t fuzzer_mutex = PTHREAD_MUTEX_INITIALIZER;
1081static pthread_cond_t fuzzer_cond = PTHREAD_COND_INITIALIZER;
1082static const uint8_t *fuzzer_data;
1083static size_t fuzzer_size;
1084
1085int sandbox_fuzzing_engine_get_input(const uint8_t **data, size_t *size)
1086{
1087 if (!fuzzer_initialized)
1088 return -ENOSYS;
1089
1090 /* Tell the main thread we need new inputs then wait for them. */
1091 pthread_mutex_lock(&fuzzer_mutex);
1092 pthread_cond_signal(&fuzzer_cond);
1093 pthread_cond_wait(&fuzzer_cond, &fuzzer_mutex);
1094 *data = fuzzer_data;
1095 *size = fuzzer_size;
1096 pthread_mutex_unlock(&fuzzer_mutex);
1097 return 0;
1098}
1099
1100int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
1101{
1102 static pthread_t tid;
1103
1104 pthread_mutex_lock(&fuzzer_mutex);
1105
1106 /* Initialize the sandbox on another thread. */
1107 if (!fuzzer_initialized) {
1108 fuzzer_initialized = true;
1109 if (pthread_create(&tid, NULL, fuzzer_thread, NULL))
1110 os_abort();
1111 pthread_cond_wait(&fuzzer_cond, &fuzzer_mutex);
1112 }
1113
1114 /* Hand over the input. */
1115 fuzzer_data = data;
1116 fuzzer_size = size;
1117 pthread_cond_signal(&fuzzer_cond);
1118
1119 /* Wait for the inputs to be finished with. */
1120 pthread_cond_wait(&fuzzer_cond, &fuzzer_mutex);
1121 pthread_mutex_unlock(&fuzzer_mutex);
1122
1123 return 0;
1124}
1125#else
Andrew Scullca5d1372022-05-30 10:00:10 +00001126int main(int argc, char *argv[])
1127{
1128 return sandbox_main(argc, argv);
1129}
Andrew Scull2b40f802022-05-30 10:00:11 +00001130#endif