blob: 0d21827e1b73f7eeccb20a75715a5572b8440248 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass7a9219c2011-10-03 19:26:44 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glass7a9219c2011-10-03 19:26:44 +00004 */
5
Heinrich Schuchardtb46f30a2020-11-12 00:29:56 +01006#define _GNU_SOURCE
7
Simon Glass62584db2012-12-26 09:53:34 +00008#include <dirent.h>
Simon Glasse1012472012-01-10 15:54:05 -08009#include <errno.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000010#include <fcntl.h>
Simon Glass70db4212012-02-15 15:51:16 -080011#include <getopt.h>
Simon Glass30eef212018-05-16 09:42:22 -060012#include <setjmp.h>
Rasmus Villemoes29601072020-02-14 10:58:37 +000013#include <signal.h>
Simon Glass62584db2012-12-26 09:53:34 +000014#include <stdio.h>
Simon Glass2a54d152013-05-19 16:45:35 -070015#include <stdint.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000016#include <stdlib.h>
Simon Glass62584db2012-12-26 09:53:34 +000017#include <string.h>
Mike Frysingerab06a752011-10-26 00:21:29 +000018#include <termios.h>
Matthias Weisserd99a6872011-11-29 12:16:40 +010019#include <time.h>
Heinrich Schuchardtb46f30a2020-11-12 00:29:56 +010020#include <ucontext.h>
Simon Glasse1012472012-01-10 15:54:05 -080021#include <unistd.h>
Matthias Weisser21899b12011-11-05 11:40:34 +010022#include <sys/mman.h>
Simon Glasse1012472012-01-10 15:54:05 -080023#include <sys/stat.h>
Simon Glass3bdf56b2012-01-10 15:54:06 -080024#include <sys/time.h>
Simon Glasse1012472012-01-10 15:54:05 -080025#include <sys/types.h>
Heinrich Schuchardtb46f30a2020-11-12 00:29:56 +010026#include <linux/compiler_attributes.h>
Matthias Weisserd99a6872011-11-29 12:16:40 +010027#include <linux/types.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000028
Simon Glass70db4212012-02-15 15:51:16 -080029#include <asm/getopt.h>
30#include <asm/sections.h>
31#include <asm/state.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000032#include <os.h>
Simon Glass94eefde2015-04-20 12:37:22 -060033#include <rtc_def.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000034
Heinrich Schuchardt43db0752020-12-30 18:07:48 +010035/* Environment variable for time offset */
36#define ENV_TIME_OFFSET "UBOOT_SB_TIME_OFFSET"
37
Simon Glass7a9219c2011-10-03 19:26:44 +000038/* Operating System Interface */
39
Simon Glass77595c62013-11-10 10:26:57 -070040struct os_mem_hdr {
41 size_t length; /* number of bytes in the block */
42};
43
Simon Glass7a9219c2011-10-03 19:26:44 +000044ssize_t os_read(int fd, void *buf, size_t count)
45{
46 return read(fd, buf, count);
47}
48
49ssize_t os_write(int fd, const void *buf, size_t count)
50{
51 return write(fd, buf, count);
52}
53
Mike Frysingere2dcefc2011-10-25 13:02:58 +020054off_t os_lseek(int fd, off_t offset, int whence)
55{
56 if (whence == OS_SEEK_SET)
57 whence = SEEK_SET;
58 else if (whence == OS_SEEK_CUR)
59 whence = SEEK_CUR;
60 else if (whence == OS_SEEK_END)
61 whence = SEEK_END;
62 else
63 os_exit(1);
64 return lseek(fd, offset, whence);
65}
66
Simon Glassd9165152012-02-20 23:56:58 -050067int os_open(const char *pathname, int os_flags)
Simon Glass7a9219c2011-10-03 19:26:44 +000068{
Simon Glassd9165152012-02-20 23:56:58 -050069 int flags;
70
71 switch (os_flags & OS_O_MASK) {
72 case OS_O_RDONLY:
73 default:
74 flags = O_RDONLY;
75 break;
76
77 case OS_O_WRONLY:
78 flags = O_WRONLY;
79 break;
80
81 case OS_O_RDWR:
82 flags = O_RDWR;
83 break;
84 }
85
86 if (os_flags & OS_O_CREAT)
87 flags |= O_CREAT;
Simon Glass50b288a2018-10-01 11:55:07 -060088 if (os_flags & OS_O_TRUNC)
89 flags |= O_TRUNC;
Heinrich Schuchardtc0b19f22020-10-27 20:29:24 +010090 /*
91 * During a cold reset execv() is used to relaunch the U-Boot binary.
92 * We must ensure that all files are closed in this case.
93 */
94 flags |= O_CLOEXEC;
Simon Glassd9165152012-02-20 23:56:58 -050095
96 return open(pathname, flags, 0777);
Simon Glass7a9219c2011-10-03 19:26:44 +000097}
98
99int os_close(int fd)
100{
Heinrich Schuchardt6eec4b02020-10-27 20:29:21 +0100101 /* Do not close the console input */
102 if (fd)
103 return close(fd);
104 return -1;
Simon Glass7a9219c2011-10-03 19:26:44 +0000105}
106
Stephen Warrencfd13e82014-03-01 22:18:00 -0700107int os_unlink(const char *pathname)
108{
109 return unlink(pathname);
110}
111
Simon Glass7a9219c2011-10-03 19:26:44 +0000112void os_exit(int exit_code)
113{
114 exit(exit_code);
115}
Mike Frysingerab06a752011-10-26 00:21:29 +0000116
Simon Glass566bf3a2018-11-06 15:21:25 -0700117int os_write_file(const char *fname, const void *buf, int size)
Simon Glass056a5ce2018-10-01 11:55:08 -0600118{
Simon Glass056a5ce2018-10-01 11:55:08 -0600119 int fd;
120
121 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC);
122 if (fd < 0) {
123 printf("Cannot open file '%s'\n", fname);
124 return -EIO;
125 }
126 if (os_write(fd, buf, size) != size) {
127 printf("Cannot write to file '%s'\n", fname);
Simon Glass566bf3a2018-11-06 15:21:25 -0700128 os_close(fd);
Simon Glass056a5ce2018-10-01 11:55:08 -0600129 return -EIO;
130 }
131 os_close(fd);
Simon Glass056a5ce2018-10-01 11:55:08 -0600132
133 return 0;
134}
135
Simon Glass566bf3a2018-11-06 15:21:25 -0700136int os_read_file(const char *fname, void **bufp, int *sizep)
137{
138 off_t size;
139 int ret = -EIO;
140 int fd;
141
142 fd = os_open(fname, OS_O_RDONLY);
143 if (fd < 0) {
144 printf("Cannot open file '%s'\n", fname);
145 goto err;
146 }
147 size = os_lseek(fd, 0, OS_SEEK_END);
148 if (size < 0) {
149 printf("Cannot seek to end of file '%s'\n", fname);
150 goto err;
151 }
152 if (os_lseek(fd, 0, OS_SEEK_SET) < 0) {
153 printf("Cannot seek to start of file '%s'\n", fname);
154 goto err;
155 }
Simon Glassb308d9f2021-02-06 09:57:33 -0700156 *bufp = os_malloc(size);
Simon Glass566bf3a2018-11-06 15:21:25 -0700157 if (!*bufp) {
158 printf("Not enough memory to read file '%s'\n", fname);
159 ret = -ENOMEM;
160 goto err;
161 }
162 if (os_read(fd, *bufp, size) != size) {
163 printf("Cannot read from file '%s'\n", fname);
164 goto err;
165 }
166 os_close(fd);
167 *sizep = size;
168
169 return 0;
170err:
171 os_close(fd);
172 return ret;
173}
174
Mike Frysingerab06a752011-10-26 00:21:29 +0000175/* Restore tty state when we exit */
176static struct termios orig_term;
Simon Glassffb87902014-02-27 13:26:22 -0700177static bool term_setup;
Simon Glass4af3e9a2018-10-01 11:55:20 -0600178static bool term_nonblock;
Mike Frysingerab06a752011-10-26 00:21:29 +0000179
Simon Glass8939df02015-05-10 21:07:27 -0600180void os_fd_restore(void)
Mike Frysingerab06a752011-10-26 00:21:29 +0000181{
Simon Glass8939df02015-05-10 21:07:27 -0600182 if (term_setup) {
Simon Glass4af3e9a2018-10-01 11:55:20 -0600183 int flags;
184
Simon Glassffb87902014-02-27 13:26:22 -0700185 tcsetattr(0, TCSANOW, &orig_term);
Simon Glass4af3e9a2018-10-01 11:55:20 -0600186 if (term_nonblock) {
187 flags = fcntl(0, F_GETFL, 0);
188 fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
189 }
Simon Glass8939df02015-05-10 21:07:27 -0600190 term_setup = false;
191 }
Mike Frysingerab06a752011-10-26 00:21:29 +0000192}
193
Rasmus Villemoes29601072020-02-14 10:58:37 +0000194static void os_sigint_handler(int sig)
195{
196 os_fd_restore();
197 signal(SIGINT, SIG_DFL);
198 raise(SIGINT);
199}
200
Heinrich Schuchardtb46f30a2020-11-12 00:29:56 +0100201static void os_signal_handler(int sig, siginfo_t *info, void *con)
202{
203 ucontext_t __maybe_unused *context = con;
204 unsigned long pc;
205
206#if defined(__x86_64__)
207 pc = context->uc_mcontext.gregs[REG_RIP];
208#elif defined(__aarch64__)
209 pc = context->uc_mcontext.pc;
210#elif defined(__riscv)
211 pc = context->uc_mcontext.__gregs[REG_PC];
212#else
213 const char msg[] =
214 "\nUnsupported architecture, cannot read program counter\n";
215
216 os_write(1, msg, sizeof(msg));
217 pc = 0;
218#endif
219
220 os_signal_action(sig, pc);
221}
222
223int os_setup_signal_handlers(void)
224{
225 struct sigaction act;
226
227 act.sa_sigaction = os_signal_handler;
228 sigemptyset(&act.sa_mask);
229 act.sa_flags = SA_SIGINFO | SA_NODEFER;
230 if (sigaction(SIGILL, &act, NULL) ||
231 sigaction(SIGBUS, &act, NULL) ||
232 sigaction(SIGSEGV, &act, NULL))
233 return -1;
234 return 0;
235}
236
Mike Frysingerab06a752011-10-26 00:21:29 +0000237/* Put tty into raw mode so <tab> and <ctrl+c> work */
Simon Glassffb87902014-02-27 13:26:22 -0700238void os_tty_raw(int fd, bool allow_sigs)
Mike Frysingerab06a752011-10-26 00:21:29 +0000239{
Mike Frysingerab06a752011-10-26 00:21:29 +0000240 struct termios term;
Simon Glass4af3e9a2018-10-01 11:55:20 -0600241 int flags;
Mike Frysingerab06a752011-10-26 00:21:29 +0000242
Simon Glassffb87902014-02-27 13:26:22 -0700243 if (term_setup)
Mike Frysingerab06a752011-10-26 00:21:29 +0000244 return;
Mike Frysingerab06a752011-10-26 00:21:29 +0000245
246 /* If not a tty, don't complain */
247 if (tcgetattr(fd, &orig_term))
248 return;
249
250 term = orig_term;
251 term.c_iflag = IGNBRK | IGNPAR;
252 term.c_oflag = OPOST | ONLCR;
253 term.c_cflag = CS8 | CREAD | CLOCAL;
Simon Glassffb87902014-02-27 13:26:22 -0700254 term.c_lflag = allow_sigs ? ISIG : 0;
Mike Frysingerab06a752011-10-26 00:21:29 +0000255 if (tcsetattr(fd, TCSANOW, &term))
256 return;
257
Simon Glass4af3e9a2018-10-01 11:55:20 -0600258 flags = fcntl(fd, F_GETFL, 0);
259 if (!(flags & O_NONBLOCK)) {
260 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
261 return;
262 term_nonblock = true;
263 }
264
Simon Glass8939df02015-05-10 21:07:27 -0600265 term_setup = true;
Mike Frysingerab06a752011-10-26 00:21:29 +0000266 atexit(os_fd_restore);
Rasmus Villemoes29601072020-02-14 10:58:37 +0000267 signal(SIGINT, os_sigint_handler);
Mike Frysingerab06a752011-10-26 00:21:29 +0000268}
Matthias Weisser21899b12011-11-05 11:40:34 +0100269
Simon Glass14e46df2021-02-06 09:57:32 -0700270/*
271 * Provide our own malloc so we don't use space in the sandbox ram_buf for
272 * allocations that are internal to sandbox, or need to be done before U-Boot's
273 * malloc() is ready.
274 */
Matthias Weisser21899b12011-11-05 11:40:34 +0100275void *os_malloc(size_t length)
276{
Simon Glass61318502018-09-15 00:50:54 -0600277 int page_size = getpagesize();
Simon Glass4a6409b2019-04-08 13:20:42 -0600278 struct os_mem_hdr *hdr;
Simon Glass77595c62013-11-10 10:26:57 -0700279
Simon Glass14e46df2021-02-06 09:57:32 -0700280 if (!length)
281 return NULL;
Simon Glassbd8b7452018-06-17 08:57:43 -0600282 /*
283 * Use an address that is hopefully available to us so that pointers
284 * to this memory are fairly obvious. If we end up with a different
285 * address, that's fine too.
286 */
287 hdr = mmap((void *)0x10000000, length + page_size,
Alexander Graf6e6e4b22018-06-22 14:44:13 +0200288 PROT_READ | PROT_WRITE | PROT_EXEC,
289 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Simon Glass77595c62013-11-10 10:26:57 -0700290 if (hdr == MAP_FAILED)
291 return NULL;
292 hdr->length = length;
293
Simon Glass61318502018-09-15 00:50:54 -0600294 return (void *)hdr + page_size;
Simon Glass77595c62013-11-10 10:26:57 -0700295}
296
Masahiro Yamada347d06d2014-01-15 13:06:41 +0900297void os_free(void *ptr)
Simon Glass77595c62013-11-10 10:26:57 -0700298{
Simon Glass4a6409b2019-04-08 13:20:42 -0600299 int page_size = getpagesize();
300 struct os_mem_hdr *hdr;
Simon Glass77595c62013-11-10 10:26:57 -0700301
Simon Glass4a6409b2019-04-08 13:20:42 -0600302 if (ptr) {
303 hdr = ptr - page_size;
304 munmap(hdr, hdr->length + page_size);
305 }
Simon Glass77595c62013-11-10 10:26:57 -0700306}
307
Simon Glass14e46df2021-02-06 09:57:32 -0700308/* These macros are from kernel.h but not accessible in this file */
309#define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a) - 1)
310#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
311
312/*
313 * Provide our own malloc so we don't use space in the sandbox ram_buf for
314 * allocations that are internal to sandbox, or need to be done before U-Boot's
315 * malloc() is ready.
316 */
317void *os_realloc(void *ptr, size_t length)
318{
319 int page_size = getpagesize();
320 struct os_mem_hdr *hdr;
321 void *new_ptr;
322
323 /* Reallocating a NULL pointer is just an alloc */
324 if (!ptr)
325 return os_malloc(length);
326
327 /* Changing a length to 0 is just a free */
328 if (length) {
329 os_free(ptr);
330 return NULL;
331 }
332
333 /*
334 * If the new size is the same number of pages as the old, nothing to
335 * do. There isn't much point in shrinking things
336 */
337 hdr = ptr - page_size;
338 if (ALIGN(length, page_size) <= ALIGN(hdr->length, page_size))
339 return ptr;
340
341 /* We have to grow it, so allocate something new */
342 new_ptr = os_malloc(length);
343 memcpy(new_ptr, ptr, hdr->length);
344 os_free(ptr);
345
346 return new_ptr;
347}
348
Matthias Weisserd99a6872011-11-29 12:16:40 +0100349void os_usleep(unsigned long usec)
350{
351 usleep(usec);
352}
353
Simon Glass2a54d152013-05-19 16:45:35 -0700354uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
Matthias Weisserd99a6872011-11-29 12:16:40 +0100355{
356#if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
357 struct timespec tp;
358 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
359 struct timeval tv;
360
361 gettimeofday(&tv, NULL);
362 tp.tv_sec = tv.tv_sec;
363 tp.tv_nsec = tv.tv_usec * 1000;
364 }
365 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
366#else
367 struct timeval tv;
368 gettimeofday(&tv, NULL);
369 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
370#endif
371}
Simon Glass70db4212012-02-15 15:51:16 -0800372
373static char *short_opts;
374static struct option *long_opts;
375
376int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
377{
Marek Behúnd1f81fd2021-05-20 13:24:06 +0200378 struct sandbox_cmdline_option **sb_opt =
379 __u_boot_sandbox_option_start();
Simon Glass70db4212012-02-15 15:51:16 -0800380 size_t num_options = __u_boot_sandbox_option_count();
381 size_t i;
382
383 int hidden_short_opt;
384 size_t si;
385
386 int c;
387
388 if (short_opts || long_opts)
389 return 1;
390
391 state->argc = argc;
392 state->argv = argv;
393
394 /* dynamically construct the arguments to the system getopt_long */
Simon Glassb308d9f2021-02-06 09:57:33 -0700395 short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
396 long_opts = os_malloc(sizeof(*long_opts) * (num_options + 1));
Simon Glass70db4212012-02-15 15:51:16 -0800397 if (!short_opts || !long_opts)
398 return 1;
399
400 /*
401 * getopt_long requires "val" to be unique (since that is what the
402 * func returns), so generate unique values automatically for flags
403 * that don't have a short option. pick 0x100 as that is above the
404 * single byte range (where ASCII/ISO-XXXX-X charsets live).
405 */
406 hidden_short_opt = 0x100;
407 si = 0;
408 for (i = 0; i < num_options; ++i) {
409 long_opts[i].name = sb_opt[i]->flag;
410 long_opts[i].has_arg = sb_opt[i]->has_arg ?
411 required_argument : no_argument;
412 long_opts[i].flag = NULL;
413
414 if (sb_opt[i]->flag_short) {
415 short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
416 if (long_opts[i].has_arg == required_argument)
417 short_opts[si++] = ':';
418 } else
419 long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
420 }
421 short_opts[si] = '\0';
422
423 /* we need to handle output ourselves since u-boot provides printf */
424 opterr = 0;
425
Simon Glass1c8c47e2020-02-03 07:36:04 -0700426 memset(&long_opts[num_options], '\0', sizeof(*long_opts));
Simon Glass70db4212012-02-15 15:51:16 -0800427 /*
428 * walk all of the options the user gave us on the command line,
429 * figure out what u-boot option structure they belong to (via
430 * the unique short val key), and call the appropriate callback.
431 */
432 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
433 for (i = 0; i < num_options; ++i) {
434 if (sb_opt[i]->flag_short == c) {
435 if (sb_opt[i]->callback(state, optarg)) {
436 state->parse_err = sb_opt[i]->flag;
437 return 0;
438 }
439 break;
440 }
441 }
442 if (i == num_options) {
443 /*
444 * store the faulting flag for later display. we have to
445 * store the flag itself as the getopt parsing itself is
446 * tricky: need to handle the following flags (assume all
447 * of the below are unknown):
448 * -a optopt='a' optind=<next>
449 * -abbbb optopt='a' optind=<this>
450 * -aaaaa optopt='a' optind=<this>
451 * --a optopt=0 optind=<this>
452 * as you can see, it is impossible to determine the exact
453 * faulting flag without doing the parsing ourselves, so
454 * we just report the specific flag that failed.
455 */
456 if (optopt) {
457 static char parse_err[3] = { '-', 0, '\0', };
458 parse_err[1] = optopt;
459 state->parse_err = parse_err;
460 } else
461 state->parse_err = argv[optind - 1];
462 break;
463 }
464 }
465
466 return 0;
467}
Simon Glass62584db2012-12-26 09:53:34 +0000468
469void os_dirent_free(struct os_dirent_node *node)
470{
471 struct os_dirent_node *next;
472
473 while (node) {
474 next = node->next;
Simon Glassb308d9f2021-02-06 09:57:33 -0700475 os_free(node);
Simon Glass62584db2012-12-26 09:53:34 +0000476 node = next;
477 }
478}
479
480int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
481{
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200482 struct dirent *entry;
Simon Glass62584db2012-12-26 09:53:34 +0000483 struct os_dirent_node *head, *node, *next;
484 struct stat buf;
485 DIR *dir;
486 int ret;
487 char *fname;
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200488 char *old_fname;
Simon Glass62584db2012-12-26 09:53:34 +0000489 int len;
Stefan Brünsf189899c2016-10-01 20:41:40 +0200490 int dirlen;
Simon Glass62584db2012-12-26 09:53:34 +0000491
492 *headp = NULL;
493 dir = opendir(dirname);
494 if (!dir)
495 return -1;
496
Stefan Brünsf189899c2016-10-01 20:41:40 +0200497 /* Create a buffer upfront, with typically sufficient size */
498 dirlen = strlen(dirname) + 2;
499 len = dirlen + 256;
Simon Glassb308d9f2021-02-06 09:57:33 -0700500 fname = os_malloc(len);
Simon Glass62584db2012-12-26 09:53:34 +0000501 if (!fname) {
502 ret = -ENOMEM;
503 goto done;
504 }
505
506 for (node = head = NULL;; node = next) {
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200507 errno = 0;
508 entry = readdir(dir);
509 if (!entry) {
510 ret = errno;
Simon Glass62584db2012-12-26 09:53:34 +0000511 break;
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200512 }
Simon Glassb308d9f2021-02-06 09:57:33 -0700513 next = os_malloc(sizeof(*node) + strlen(entry->d_name) + 1);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200514 if (!next) {
Simon Glass62584db2012-12-26 09:53:34 +0000515 os_dirent_free(head);
516 ret = -ENOMEM;
517 goto done;
518 }
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200519 if (dirlen + strlen(entry->d_name) > len) {
520 len = dirlen + strlen(entry->d_name);
521 old_fname = fname;
Simon Glassb308d9f2021-02-06 09:57:33 -0700522 fname = os_realloc(fname, len);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200523 if (!fname) {
Simon Glassb308d9f2021-02-06 09:57:33 -0700524 os_free(old_fname);
525 os_free(next);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200526 os_dirent_free(head);
527 ret = -ENOMEM;
528 goto done;
529 }
530 }
Stephen Warren9c38c072014-06-11 10:26:23 -0600531 next->next = NULL;
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200532 strcpy(next->name, entry->d_name);
533 switch (entry->d_type) {
Simon Glass62584db2012-12-26 09:53:34 +0000534 case DT_REG:
535 next->type = OS_FILET_REG;
536 break;
537 case DT_DIR:
538 next->type = OS_FILET_DIR;
539 break;
540 case DT_LNK:
541 next->type = OS_FILET_LNK;
542 break;
Stefan Brüns2f159402016-10-04 21:46:35 +0200543 default:
544 next->type = OS_FILET_UNKNOWN;
Simon Glass62584db2012-12-26 09:53:34 +0000545 }
546 next->size = 0;
547 snprintf(fname, len, "%s/%s", dirname, next->name);
548 if (!stat(fname, &buf))
549 next->size = buf.st_size;
550 if (node)
551 node->next = next;
Stefan Brünsce2ec192016-10-01 20:41:39 +0200552 else
553 head = next;
Simon Glass62584db2012-12-26 09:53:34 +0000554 }
555 *headp = head;
556
557done:
558 closedir(dir);
Simon Glassb308d9f2021-02-06 09:57:33 -0700559 os_free(fname);
Simon Glass62584db2012-12-26 09:53:34 +0000560 return ret;
561}
562
563const char *os_dirent_typename[OS_FILET_COUNT] = {
564 " ",
565 "SYM",
566 "DIR",
567 "???",
568};
569
570const char *os_dirent_get_typename(enum os_dirent_t type)
571{
Tom Rinie2bc87d2017-05-13 20:11:30 -0400572 if (type >= OS_FILET_REG && type < OS_FILET_COUNT)
Simon Glass62584db2012-12-26 09:53:34 +0000573 return os_dirent_typename[type];
574
575 return os_dirent_typename[OS_FILET_UNKNOWN];
576}
577
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800578int os_get_filesize(const char *fname, loff_t *size)
Simon Glass62584db2012-12-26 09:53:34 +0000579{
580 struct stat buf;
581 int ret;
582
583 ret = stat(fname, &buf);
584 if (ret)
585 return ret;
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800586 *size = buf.st_size;
587 return 0;
Simon Glass62584db2012-12-26 09:53:34 +0000588}
Simon Glass91b136c2013-11-10 10:27:01 -0700589
Simon Glass0b189b62017-12-04 13:48:17 -0700590void os_putc(int ch)
591{
592 putchar(ch);
593}
594
595void os_puts(const char *str)
596{
597 while (*str)
598 os_putc(*str++);
599}
600
Simon Glass5c2859c2013-11-10 10:27:03 -0700601int os_write_ram_buf(const char *fname)
602{
603 struct sandbox_state *state = state_get_current();
604 int fd, ret;
605
606 fd = open(fname, O_CREAT | O_WRONLY, 0777);
607 if (fd < 0)
608 return -ENOENT;
609 ret = write(fd, state->ram_buf, state->ram_size);
610 close(fd);
611 if (ret != state->ram_size)
612 return -EIO;
613
614 return 0;
615}
616
617int os_read_ram_buf(const char *fname)
618{
619 struct sandbox_state *state = state_get_current();
620 int fd, ret;
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800621 loff_t size;
Simon Glass5c2859c2013-11-10 10:27:03 -0700622
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800623 ret = os_get_filesize(fname, &size);
624 if (ret < 0)
625 return ret;
Simon Glass5c2859c2013-11-10 10:27:03 -0700626 if (size != state->ram_size)
627 return -ENOSPC;
628 fd = open(fname, O_RDONLY);
629 if (fd < 0)
630 return -ENOENT;
631
632 ret = read(fd, state->ram_buf, state->ram_size);
633 close(fd);
634 if (ret != state->ram_size)
635 return -EIO;
636
637 return 0;
638}
Simon Glass47f5fcf2014-02-27 13:26:15 -0700639
640static int make_exec(char *fname, const void *data, int size)
641{
642 int fd;
643
644 strcpy(fname, "/tmp/u-boot.jump.XXXXXX");
645 fd = mkstemp(fname);
646 if (fd < 0)
647 return -ENOENT;
648 if (write(fd, data, size) < 0)
649 return -EIO;
650 close(fd);
651 if (chmod(fname, 0777))
652 return -ENOEXEC;
653
654 return 0;
655}
656
Simon Glass7b5ea142018-11-15 18:44:05 -0700657/**
658 * add_args() - Allocate a new argv with the given args
659 *
660 * This is used to create a new argv array with all the old arguments and some
661 * new ones that are passed in
662 *
663 * @argvp: Returns newly allocated args list
664 * @add_args: Arguments to add, each a string
665 * @count: Number of arguments in @add_args
666 * @return 0 if OK, -ENOMEM if out of memory
667 */
668static int add_args(char ***argvp, char *add_args[], int count)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700669{
Simon Glass65f3b1f2018-11-15 18:44:07 -0700670 char **argv, **ap;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700671 int argc;
672
Simon Glass65f3b1f2018-11-15 18:44:07 -0700673 for (argc = 0; (*argvp)[argc]; argc++)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700674 ;
675
Simon Glassb308d9f2021-02-06 09:57:33 -0700676 argv = os_malloc((argc + count + 1) * sizeof(char *));
Simon Glass47f5fcf2014-02-27 13:26:15 -0700677 if (!argv) {
678 printf("Out of memory for %d argv\n", count);
679 return -ENOMEM;
680 }
Simon Glass65f3b1f2018-11-15 18:44:07 -0700681 for (ap = *argvp, argc = 0; *ap; ap++) {
682 char *arg = *ap;
683
684 /* Drop args that we don't want to propagate */
685 if (*arg == '-' && strlen(arg) == 2) {
686 switch (arg[1]) {
687 case 'j':
688 case 'm':
689 ap++;
690 continue;
691 }
692 } else if (!strcmp(arg, "--rm_memory")) {
693 ap++;
694 continue;
695 }
696 argv[argc++] = arg;
697 }
698
Simon Glass47f5fcf2014-02-27 13:26:15 -0700699 memcpy(argv + argc, add_args, count * sizeof(char *));
700 argv[argc + count] = NULL;
701
702 *argvp = argv;
703 return 0;
704}
705
Simon Glass7b5ea142018-11-15 18:44:05 -0700706/**
707 * os_jump_to_file() - Jump to a new program
708 *
709 * This saves the memory buffer, sets up arguments to the new process, then
710 * execs it.
711 *
712 * @fname: Filename to exec
713 * @return does not return on success, any return value is an error
714 */
Simon Glass5d8c3792021-03-15 18:11:07 +1300715static int os_jump_to_file(const char *fname, bool delete_it)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700716{
717 struct sandbox_state *state = state_get_current();
Simon Glass7b5ea142018-11-15 18:44:05 -0700718 char mem_fname[30];
Simon Glass47f5fcf2014-02-27 13:26:15 -0700719 int fd, err;
Simon Glass7b5ea142018-11-15 18:44:05 -0700720 char *extra_args[5];
Simon Glass47f5fcf2014-02-27 13:26:15 -0700721 char **argv = state->argv;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700722 int argc;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700723#ifdef DEBUG
Simon Glass7b5ea142018-11-15 18:44:05 -0700724 int i;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700725#endif
726
Simon Glass47f5fcf2014-02-27 13:26:15 -0700727 strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
728 fd = mkstemp(mem_fname);
729 if (fd < 0)
730 return -ENOENT;
731 close(fd);
732 err = os_write_ram_buf(mem_fname);
733 if (err)
734 return err;
735
736 os_fd_restore();
737
Simon Glass5d8c3792021-03-15 18:11:07 +1300738 argc = 0;
739 if (delete_it) {
740 extra_args[argc++] = "-j";
741 extra_args[argc++] = (char *)fname;
742 }
743 extra_args[argc++] = "-m";
744 extra_args[argc++] = mem_fname;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700745 if (state->ram_buf_rm)
746 extra_args[argc++] = "--rm_memory";
747 err = add_args(&argv, extra_args, argc);
Simon Glass47f5fcf2014-02-27 13:26:15 -0700748 if (err)
749 return err;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700750 argv[0] = (char *)fname;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700751
752#ifdef DEBUG
753 for (i = 0; argv[i]; i++)
754 printf("%d %s\n", i, argv[i]);
755#endif
756
757 if (state_uninit())
758 os_exit(2);
759
760 err = execv(fname, argv);
Simon Glassb308d9f2021-02-06 09:57:33 -0700761 os_free(argv);
Simon Glass27028f12018-11-15 18:44:08 -0700762 if (err) {
763 perror("Unable to run image");
Simon Glass6b5e4202018-11-23 21:29:24 -0700764 printf("Image filename '%s'\n", fname);
Simon Glass47f5fcf2014-02-27 13:26:15 -0700765 return err;
Simon Glass27028f12018-11-15 18:44:08 -0700766 }
Simon Glass47f5fcf2014-02-27 13:26:15 -0700767
Simon Glass5d8c3792021-03-15 18:11:07 +1300768 if (delete_it)
769 return unlink(fname);
770
771 return -EFAULT;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700772}
Simon Glass94eefde2015-04-20 12:37:22 -0600773
Simon Glass7b5ea142018-11-15 18:44:05 -0700774int os_jump_to_image(const void *dest, int size)
775{
776 char fname[30];
777 int err;
778
779 err = make_exec(fname, dest, size);
780 if (err)
781 return err;
782
Simon Glass5d8c3792021-03-15 18:11:07 +1300783 return os_jump_to_file(fname, true);
Simon Glass7b5ea142018-11-15 18:44:05 -0700784}
785
Simon Glass01ad9f72021-03-07 17:35:13 -0700786int os_find_u_boot(char *fname, int maxlen, bool use_img)
Simon Glassd4e33f52016-07-04 11:57:45 -0600787{
788 struct sandbox_state *state = state_get_current();
789 const char *progname = state->argv[0];
790 int len = strlen(progname);
Simon Glass69bc15d2018-10-01 11:55:10 -0600791 const char *suffix;
Simon Glassd4e33f52016-07-04 11:57:45 -0600792 char *p;
793 int fd;
794
795 if (len >= maxlen || len < 4)
796 return -ENOSPC;
797
Simon Glassd4e33f52016-07-04 11:57:45 -0600798 strcpy(fname, progname);
Simon Glass69bc15d2018-10-01 11:55:10 -0600799 suffix = fname + len - 4;
800
801 /* If we are TPL, boot to SPL */
802 if (!strcmp(suffix, "-tpl")) {
803 fname[len - 3] = 's';
804 fd = os_open(fname, O_RDONLY);
805 if (fd >= 0) {
806 close(fd);
807 return 0;
808 }
809
Simon Glass01ad9f72021-03-07 17:35:13 -0700810 /* Look for 'u-boot-spl' in the spl/ directory */
811 p = strstr(fname, "/spl/");
Simon Glass69bc15d2018-10-01 11:55:10 -0600812 if (p) {
813 p[1] = 's';
814 fd = os_open(fname, O_RDONLY);
815 if (fd >= 0) {
816 close(fd);
817 return 0;
818 }
819 }
820 return -ENOENT;
821 }
822
823 /* Look for 'u-boot' in the same directory as 'u-boot-spl' */
824 if (!strcmp(suffix, "-spl")) {
Simon Glassd4e33f52016-07-04 11:57:45 -0600825 fname[len - 4] = '\0';
826 fd = os_open(fname, O_RDONLY);
827 if (fd >= 0) {
828 close(fd);
829 return 0;
830 }
831 }
832
833 /* Look for 'u-boot' in the parent directory of spl/ */
Simon Glassb847c142018-11-13 15:55:20 -0700834 p = strstr(fname, "spl/");
Simon Glassd4e33f52016-07-04 11:57:45 -0600835 if (p) {
Simon Glassb847c142018-11-13 15:55:20 -0700836 /* Remove the "spl" characters */
837 memmove(p, p + 4, strlen(p + 4) + 1);
Simon Glass01ad9f72021-03-07 17:35:13 -0700838 if (use_img)
839 strcat(p, ".img");
Simon Glassd4e33f52016-07-04 11:57:45 -0600840 fd = os_open(fname, O_RDONLY);
841 if (fd >= 0) {
842 close(fd);
843 return 0;
844 }
845 }
846
847 return -ENOENT;
848}
849
850int os_spl_to_uboot(const char *fname)
851{
Patrick Delaunay10bb90f2020-11-20 09:48:33 +0100852 struct sandbox_state *state = state_get_current();
853
Patrick Delaunay10bb90f2020-11-20 09:48:33 +0100854 /* U-Boot will delete ram buffer after read: "--rm_memory"*/
855 state->ram_buf_rm = true;
Simon Glass5d8c3792021-03-15 18:11:07 +1300856
857 return os_jump_to_file(fname, false);
Simon Glassd4e33f52016-07-04 11:57:45 -0600858}
859
Heinrich Schuchardt43db0752020-12-30 18:07:48 +0100860long os_get_time_offset(void)
861{
862 const char *offset;
863
864 offset = getenv(ENV_TIME_OFFSET);
865 if (offset)
866 return strtol(offset, NULL, 0);
867 return 0;
868}
869
870void os_set_time_offset(long offset)
871{
872 char buf[21];
873 int ret;
874
875 snprintf(buf, sizeof(buf), "%ld", offset);
876 ret = setenv(ENV_TIME_OFFSET, buf, true);
877 if (ret)
878 printf("Could not set environment variable %s\n",
879 ENV_TIME_OFFSET);
880}
881
Simon Glass94eefde2015-04-20 12:37:22 -0600882void os_localtime(struct rtc_time *rt)
883{
884 time_t t = time(NULL);
885 struct tm *tm;
886
887 tm = localtime(&t);
888 rt->tm_sec = tm->tm_sec;
889 rt->tm_min = tm->tm_min;
890 rt->tm_hour = tm->tm_hour;
891 rt->tm_mday = tm->tm_mday;
892 rt->tm_mon = tm->tm_mon + 1;
893 rt->tm_year = tm->tm_year + 1900;
894 rt->tm_wday = tm->tm_wday;
895 rt->tm_yday = tm->tm_yday;
896 rt->tm_isdst = tm->tm_isdst;
897}
Simon Glass30eef212018-05-16 09:42:22 -0600898
Simon Glassfe938fb2018-09-15 00:50:55 -0600899void os_abort(void)
900{
901 abort();
902}
Simon Glass9f8037e2018-10-01 21:12:32 -0600903
904int os_mprotect_allow(void *start, size_t len)
905{
906 int page_size = getpagesize();
907
908 /* Move start to the start of a page, len to the end */
909 start = (void *)(((ulong)start) & ~(page_size - 1));
910 len = (len + page_size * 2) & ~(page_size - 1);
911
912 return mprotect(start, len, PROT_READ | PROT_WRITE);
913}
Simon Glass001d1882019-04-08 13:20:41 -0600914
915void *os_find_text_base(void)
916{
917 char line[500];
918 void *base = NULL;
919 int len;
920 int fd;
921
922 /*
923 * This code assumes that the first line of /proc/self/maps holds
924 * information about the text, for example:
925 *
926 * 5622d9907000-5622d9a55000 r-xp 00000000 08:01 15067168 u-boot
927 *
928 * The first hex value is assumed to be the address.
929 *
930 * This is tested in Linux 4.15.
931 */
932 fd = open("/proc/self/maps", O_RDONLY);
933 if (fd == -1)
934 return NULL;
935 len = read(fd, line, sizeof(line));
936 if (len > 0) {
937 char *end = memchr(line, '-', len);
938
939 if (end) {
Heinrich Schuchardt4ecb5062019-10-26 23:17:44 +0200940 uintptr_t addr;
Simon Glass001d1882019-04-08 13:20:41 -0600941
942 *end = '\0';
Heinrich Schuchardt4ecb5062019-10-26 23:17:44 +0200943 if (sscanf(line, "%zx", &addr) == 1)
Simon Glass001d1882019-04-08 13:20:41 -0600944 base = (void *)addr;
945 }
946 }
947 close(fd);
948
949 return base;
950}
Heinrich Schuchardt329dccc2020-10-27 20:29:25 +0100951
952void os_relaunch(char *argv[])
953{
954 execv(argv[0], argv);
955 os_exit(1);
956}