blob: b56fa04a34b5cd497a04c79ffd4200e11767ad1c [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>
Simon Glass8a3e0352012-02-15 15:51:16 -080011#include <getopt.h>
Simon Glass5dccd152018-05-16 09:42:22 -060012#include <setjmp.h>
Rasmus Villemoes2b72ad22020-02-14 10:58:37 +000013#include <signal.h>
Simon Glassf1c45c82012-12-26 09:53:34 +000014#include <stdio.h>
Simon Glassfb4b4e82013-05-19 16:45:35 -070015#include <stdint.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000016#include <stdlib.h>
Simon Glassf1c45c82012-12-26 09:53:34 +000017#include <string.h>
Mike Frysingera5baaee2011-10-26 00:21:29 +000018#include <termios.h>
Matthias Weisser0d3dd142011-11-29 12:16:40 +010019#include <time.h>
Heinrich Schuchardt28eb5092020-11-12 00:29:56 +010020#include <ucontext.h>
Simon Glasse8015a62012-01-10 15:54:05 -080021#include <unistd.h>
Matthias Weisserb5f7b472011-11-05 11:40:34 +010022#include <sys/mman.h>
Simon Glasse8015a62012-01-10 15:54:05 -080023#include <sys/stat.h>
Simon Glass17064c02012-01-10 15:54:06 -080024#include <sys/time.h>
Simon Glasse8015a62012-01-10 15:54:05 -080025#include <sys/types.h>
Heinrich Schuchardt28eb5092020-11-12 00:29:56 +010026#include <linux/compiler_attributes.h>
Matthias Weisser0d3dd142011-11-29 12:16:40 +010027#include <linux/types.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000028
Simon Glass8a3e0352012-02-15 15:51:16 -080029#include <asm/getopt.h>
30#include <asm/sections.h>
31#include <asm/state.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000032#include <os.h>
Simon Glass504548f2015-04-20 12:37:22 -060033#include <rtc_def.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000034
35/* Operating System Interface */
36
Simon Glass1e6594c2013-11-10 10:26:57 -070037struct os_mem_hdr {
38 size_t length; /* number of bytes in the block */
39};
40
Simon Glasscd0684f2011-10-03 19:26:44 +000041ssize_t os_read(int fd, void *buf, size_t count)
42{
43 return read(fd, buf, count);
44}
45
46ssize_t os_write(int fd, const void *buf, size_t count)
47{
48 return write(fd, buf, count);
49}
50
Mike Frysinger60addac2011-10-25 13:02:58 +020051off_t os_lseek(int fd, off_t offset, int whence)
52{
53 if (whence == OS_SEEK_SET)
54 whence = SEEK_SET;
55 else if (whence == OS_SEEK_CUR)
56 whence = SEEK_CUR;
57 else if (whence == OS_SEEK_END)
58 whence = SEEK_END;
59 else
60 os_exit(1);
61 return lseek(fd, offset, whence);
62}
63
Simon Glass3196d752012-02-20 23:56:58 -050064int os_open(const char *pathname, int os_flags)
Simon Glasscd0684f2011-10-03 19:26:44 +000065{
Simon Glass3196d752012-02-20 23:56:58 -050066 int flags;
67
68 switch (os_flags & OS_O_MASK) {
69 case OS_O_RDONLY:
70 default:
71 flags = O_RDONLY;
72 break;
73
74 case OS_O_WRONLY:
75 flags = O_WRONLY;
76 break;
77
78 case OS_O_RDWR:
79 flags = O_RDWR;
80 break;
81 }
82
83 if (os_flags & OS_O_CREAT)
84 flags |= O_CREAT;
Simon Glassce55a112018-10-01 11:55:07 -060085 if (os_flags & OS_O_TRUNC)
86 flags |= O_TRUNC;
Heinrich Schuchardtfc96df62020-10-27 20:29:24 +010087 /*
88 * During a cold reset execv() is used to relaunch the U-Boot binary.
89 * We must ensure that all files are closed in this case.
90 */
91 flags |= O_CLOEXEC;
Simon Glass3196d752012-02-20 23:56:58 -050092
93 return open(pathname, flags, 0777);
Simon Glasscd0684f2011-10-03 19:26:44 +000094}
95
96int os_close(int fd)
97{
Heinrich Schuchardt69db2ee2020-10-27 20:29:21 +010098 /* Do not close the console input */
99 if (fd)
100 return close(fd);
101 return -1;
Simon Glasscd0684f2011-10-03 19:26:44 +0000102}
103
Stephen Warrencd5edba2014-03-01 22:18:00 -0700104int os_unlink(const char *pathname)
105{
106 return unlink(pathname);
107}
108
Simon Glasscd0684f2011-10-03 19:26:44 +0000109void os_exit(int exit_code)
110{
111 exit(exit_code);
112}
Mike Frysingera5baaee2011-10-26 00:21:29 +0000113
Simon Glass8d176d82018-11-06 15:21:25 -0700114int os_write_file(const char *fname, const void *buf, int size)
Simon Glasscbfa8452018-10-01 11:55:08 -0600115{
Simon Glasscbfa8452018-10-01 11:55:08 -0600116 int fd;
117
118 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC);
119 if (fd < 0) {
120 printf("Cannot open file '%s'\n", fname);
121 return -EIO;
122 }
123 if (os_write(fd, buf, size) != size) {
124 printf("Cannot write to file '%s'\n", fname);
Simon Glass8d176d82018-11-06 15:21:25 -0700125 os_close(fd);
Simon Glasscbfa8452018-10-01 11:55:08 -0600126 return -EIO;
127 }
128 os_close(fd);
Simon Glasscbfa8452018-10-01 11:55:08 -0600129
130 return 0;
131}
132
Simon Glass8d176d82018-11-06 15:21:25 -0700133int os_read_file(const char *fname, void **bufp, int *sizep)
134{
135 off_t size;
136 int ret = -EIO;
137 int fd;
138
139 fd = os_open(fname, OS_O_RDONLY);
140 if (fd < 0) {
141 printf("Cannot open file '%s'\n", fname);
142 goto err;
143 }
144 size = os_lseek(fd, 0, OS_SEEK_END);
145 if (size < 0) {
146 printf("Cannot seek to end of file '%s'\n", fname);
147 goto err;
148 }
149 if (os_lseek(fd, 0, OS_SEEK_SET) < 0) {
150 printf("Cannot seek to start of file '%s'\n", fname);
151 goto err;
152 }
Simon Glassfa252262020-02-03 07:36:02 -0700153 *bufp = malloc(size);
Simon Glass8d176d82018-11-06 15:21:25 -0700154 if (!*bufp) {
155 printf("Not enough memory to read file '%s'\n", fname);
156 ret = -ENOMEM;
157 goto err;
158 }
159 if (os_read(fd, *bufp, size) != size) {
160 printf("Cannot read from file '%s'\n", fname);
161 goto err;
162 }
163 os_close(fd);
164 *sizep = size;
165
166 return 0;
167err:
168 os_close(fd);
169 return ret;
170}
171
Mike Frysingera5baaee2011-10-26 00:21:29 +0000172/* Restore tty state when we exit */
173static struct termios orig_term;
Simon Glass678ef472014-02-27 13:26:22 -0700174static bool term_setup;
Simon Glassae50ec72018-10-01 11:55:20 -0600175static bool term_nonblock;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000176
Simon Glass9c3b7d62015-05-10 21:07:27 -0600177void os_fd_restore(void)
Mike Frysingera5baaee2011-10-26 00:21:29 +0000178{
Simon Glass9c3b7d62015-05-10 21:07:27 -0600179 if (term_setup) {
Simon Glassae50ec72018-10-01 11:55:20 -0600180 int flags;
181
Simon Glass678ef472014-02-27 13:26:22 -0700182 tcsetattr(0, TCSANOW, &orig_term);
Simon Glassae50ec72018-10-01 11:55:20 -0600183 if (term_nonblock) {
184 flags = fcntl(0, F_GETFL, 0);
185 fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
186 }
Simon Glass9c3b7d62015-05-10 21:07:27 -0600187 term_setup = false;
188 }
Mike Frysingera5baaee2011-10-26 00:21:29 +0000189}
190
Rasmus Villemoes2b72ad22020-02-14 10:58:37 +0000191static void os_sigint_handler(int sig)
192{
193 os_fd_restore();
194 signal(SIGINT, SIG_DFL);
195 raise(SIGINT);
196}
197
Heinrich Schuchardt28eb5092020-11-12 00:29:56 +0100198static void os_signal_handler(int sig, siginfo_t *info, void *con)
199{
200 ucontext_t __maybe_unused *context = con;
201 unsigned long pc;
202
203#if defined(__x86_64__)
204 pc = context->uc_mcontext.gregs[REG_RIP];
205#elif defined(__aarch64__)
206 pc = context->uc_mcontext.pc;
207#elif defined(__riscv)
208 pc = context->uc_mcontext.__gregs[REG_PC];
209#else
210 const char msg[] =
211 "\nUnsupported architecture, cannot read program counter\n";
212
213 os_write(1, msg, sizeof(msg));
214 pc = 0;
215#endif
216
217 os_signal_action(sig, pc);
218}
219
220int os_setup_signal_handlers(void)
221{
222 struct sigaction act;
223
224 act.sa_sigaction = os_signal_handler;
225 sigemptyset(&act.sa_mask);
226 act.sa_flags = SA_SIGINFO | SA_NODEFER;
227 if (sigaction(SIGILL, &act, NULL) ||
228 sigaction(SIGBUS, &act, NULL) ||
229 sigaction(SIGSEGV, &act, NULL))
230 return -1;
231 return 0;
232}
233
Mike Frysingera5baaee2011-10-26 00:21:29 +0000234/* Put tty into raw mode so <tab> and <ctrl+c> work */
Simon Glass678ef472014-02-27 13:26:22 -0700235void os_tty_raw(int fd, bool allow_sigs)
Mike Frysingera5baaee2011-10-26 00:21:29 +0000236{
Mike Frysingera5baaee2011-10-26 00:21:29 +0000237 struct termios term;
Simon Glassae50ec72018-10-01 11:55:20 -0600238 int flags;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000239
Simon Glass678ef472014-02-27 13:26:22 -0700240 if (term_setup)
Mike Frysingera5baaee2011-10-26 00:21:29 +0000241 return;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000242
243 /* If not a tty, don't complain */
244 if (tcgetattr(fd, &orig_term))
245 return;
246
247 term = orig_term;
248 term.c_iflag = IGNBRK | IGNPAR;
249 term.c_oflag = OPOST | ONLCR;
250 term.c_cflag = CS8 | CREAD | CLOCAL;
Simon Glass678ef472014-02-27 13:26:22 -0700251 term.c_lflag = allow_sigs ? ISIG : 0;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000252 if (tcsetattr(fd, TCSANOW, &term))
253 return;
254
Simon Glassae50ec72018-10-01 11:55:20 -0600255 flags = fcntl(fd, F_GETFL, 0);
256 if (!(flags & O_NONBLOCK)) {
257 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
258 return;
259 term_nonblock = true;
260 }
261
Simon Glass9c3b7d62015-05-10 21:07:27 -0600262 term_setup = true;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000263 atexit(os_fd_restore);
Rasmus Villemoes2b72ad22020-02-14 10:58:37 +0000264 signal(SIGINT, os_sigint_handler);
Mike Frysingera5baaee2011-10-26 00:21:29 +0000265}
Matthias Weisserb5f7b472011-11-05 11:40:34 +0100266
267void *os_malloc(size_t length)
268{
Simon Glassbe005d82018-09-15 00:50:54 -0600269 int page_size = getpagesize();
Simon Glassfc2dde82019-04-08 13:20:42 -0600270 struct os_mem_hdr *hdr;
Simon Glass1e6594c2013-11-10 10:26:57 -0700271
Simon Glass57ba9422018-06-17 08:57:43 -0600272 /*
273 * Use an address that is hopefully available to us so that pointers
274 * to this memory are fairly obvious. If we end up with a different
275 * address, that's fine too.
276 */
277 hdr = mmap((void *)0x10000000, length + page_size,
Alexander Graf934a5452018-06-22 14:44:13 +0200278 PROT_READ | PROT_WRITE | PROT_EXEC,
279 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Simon Glass1e6594c2013-11-10 10:26:57 -0700280 if (hdr == MAP_FAILED)
281 return NULL;
282 hdr->length = length;
283
Simon Glassbe005d82018-09-15 00:50:54 -0600284 return (void *)hdr + page_size;
Simon Glass1e6594c2013-11-10 10:26:57 -0700285}
286
Masahiro Yamadaee957282014-01-15 13:06:41 +0900287void os_free(void *ptr)
Simon Glass1e6594c2013-11-10 10:26:57 -0700288{
Simon Glassfc2dde82019-04-08 13:20:42 -0600289 int page_size = getpagesize();
290 struct os_mem_hdr *hdr;
Simon Glass1e6594c2013-11-10 10:26:57 -0700291
Simon Glassfc2dde82019-04-08 13:20:42 -0600292 if (ptr) {
293 hdr = ptr - page_size;
294 munmap(hdr, hdr->length + page_size);
295 }
Simon Glass1e6594c2013-11-10 10:26:57 -0700296}
297
Matthias Weisser0d3dd142011-11-29 12:16:40 +0100298void os_usleep(unsigned long usec)
299{
300 usleep(usec);
301}
302
Simon Glassfb4b4e82013-05-19 16:45:35 -0700303uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
Matthias Weisser0d3dd142011-11-29 12:16:40 +0100304{
305#if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
306 struct timespec tp;
307 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
308 struct timeval tv;
309
310 gettimeofday(&tv, NULL);
311 tp.tv_sec = tv.tv_sec;
312 tp.tv_nsec = tv.tv_usec * 1000;
313 }
314 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
315#else
316 struct timeval tv;
317 gettimeofday(&tv, NULL);
318 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
319#endif
320}
Simon Glass8a3e0352012-02-15 15:51:16 -0800321
322static char *short_opts;
323static struct option *long_opts;
324
325int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
326{
Simon Glass64367c82013-12-03 16:43:23 -0700327 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass8a3e0352012-02-15 15:51:16 -0800328 size_t num_options = __u_boot_sandbox_option_count();
329 size_t i;
330
331 int hidden_short_opt;
332 size_t si;
333
334 int c;
335
336 if (short_opts || long_opts)
337 return 1;
338
339 state->argc = argc;
340 state->argv = argv;
341
342 /* dynamically construct the arguments to the system getopt_long */
Simon Glassfa252262020-02-03 07:36:02 -0700343 short_opts = malloc(sizeof(*short_opts) * num_options * 2 + 1);
Simon Glass0a4eabb2020-02-03 07:36:04 -0700344 long_opts = malloc(sizeof(*long_opts) * (num_options + 1));
Simon Glass8a3e0352012-02-15 15:51:16 -0800345 if (!short_opts || !long_opts)
346 return 1;
347
348 /*
349 * getopt_long requires "val" to be unique (since that is what the
350 * func returns), so generate unique values automatically for flags
351 * that don't have a short option. pick 0x100 as that is above the
352 * single byte range (where ASCII/ISO-XXXX-X charsets live).
353 */
354 hidden_short_opt = 0x100;
355 si = 0;
356 for (i = 0; i < num_options; ++i) {
357 long_opts[i].name = sb_opt[i]->flag;
358 long_opts[i].has_arg = sb_opt[i]->has_arg ?
359 required_argument : no_argument;
360 long_opts[i].flag = NULL;
361
362 if (sb_opt[i]->flag_short) {
363 short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
364 if (long_opts[i].has_arg == required_argument)
365 short_opts[si++] = ':';
366 } else
367 long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
368 }
369 short_opts[si] = '\0';
370
371 /* we need to handle output ourselves since u-boot provides printf */
372 opterr = 0;
373
Simon Glass0a4eabb2020-02-03 07:36:04 -0700374 memset(&long_opts[num_options], '\0', sizeof(*long_opts));
Simon Glass8a3e0352012-02-15 15:51:16 -0800375 /*
376 * walk all of the options the user gave us on the command line,
377 * figure out what u-boot option structure they belong to (via
378 * the unique short val key), and call the appropriate callback.
379 */
380 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
381 for (i = 0; i < num_options; ++i) {
382 if (sb_opt[i]->flag_short == c) {
383 if (sb_opt[i]->callback(state, optarg)) {
384 state->parse_err = sb_opt[i]->flag;
385 return 0;
386 }
387 break;
388 }
389 }
390 if (i == num_options) {
391 /*
392 * store the faulting flag for later display. we have to
393 * store the flag itself as the getopt parsing itself is
394 * tricky: need to handle the following flags (assume all
395 * of the below are unknown):
396 * -a optopt='a' optind=<next>
397 * -abbbb optopt='a' optind=<this>
398 * -aaaaa optopt='a' optind=<this>
399 * --a optopt=0 optind=<this>
400 * as you can see, it is impossible to determine the exact
401 * faulting flag without doing the parsing ourselves, so
402 * we just report the specific flag that failed.
403 */
404 if (optopt) {
405 static char parse_err[3] = { '-', 0, '\0', };
406 parse_err[1] = optopt;
407 state->parse_err = parse_err;
408 } else
409 state->parse_err = argv[optind - 1];
410 break;
411 }
412 }
413
414 return 0;
415}
Simon Glassf1c45c82012-12-26 09:53:34 +0000416
417void os_dirent_free(struct os_dirent_node *node)
418{
419 struct os_dirent_node *next;
420
421 while (node) {
422 next = node->next;
Simon Glassfa252262020-02-03 07:36:02 -0700423 free(node);
Simon Glassf1c45c82012-12-26 09:53:34 +0000424 node = next;
425 }
426}
427
428int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
429{
Stefan Brünsae71ede2016-10-01 20:41:42 +0200430 struct dirent *entry;
Simon Glassf1c45c82012-12-26 09:53:34 +0000431 struct os_dirent_node *head, *node, *next;
432 struct stat buf;
433 DIR *dir;
434 int ret;
435 char *fname;
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200436 char *old_fname;
Simon Glassf1c45c82012-12-26 09:53:34 +0000437 int len;
Stefan Brünsb2129902016-10-01 20:41:40 +0200438 int dirlen;
Simon Glassf1c45c82012-12-26 09:53:34 +0000439
440 *headp = NULL;
441 dir = opendir(dirname);
442 if (!dir)
443 return -1;
444
Stefan Brünsb2129902016-10-01 20:41:40 +0200445 /* Create a buffer upfront, with typically sufficient size */
446 dirlen = strlen(dirname) + 2;
447 len = dirlen + 256;
Simon Glassfa252262020-02-03 07:36:02 -0700448 fname = malloc(len);
Simon Glassf1c45c82012-12-26 09:53:34 +0000449 if (!fname) {
450 ret = -ENOMEM;
451 goto done;
452 }
453
454 for (node = head = NULL;; node = next) {
Stefan Brünsae71ede2016-10-01 20:41:42 +0200455 errno = 0;
456 entry = readdir(dir);
457 if (!entry) {
458 ret = errno;
Simon Glassf1c45c82012-12-26 09:53:34 +0000459 break;
Stefan Brünsae71ede2016-10-01 20:41:42 +0200460 }
Simon Glassfa252262020-02-03 07:36:02 -0700461 next = malloc(sizeof(*node) + strlen(entry->d_name) + 1);
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200462 if (!next) {
Simon Glassf1c45c82012-12-26 09:53:34 +0000463 os_dirent_free(head);
464 ret = -ENOMEM;
465 goto done;
466 }
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200467 if (dirlen + strlen(entry->d_name) > len) {
468 len = dirlen + strlen(entry->d_name);
469 old_fname = fname;
Simon Glassfa252262020-02-03 07:36:02 -0700470 fname = realloc(fname, len);
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200471 if (!fname) {
Simon Glassfa252262020-02-03 07:36:02 -0700472 free(old_fname);
473 free(next);
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200474 os_dirent_free(head);
475 ret = -ENOMEM;
476 goto done;
477 }
478 }
Stephen Warren9671c672014-06-11 10:26:23 -0600479 next->next = NULL;
Stefan Brünsae71ede2016-10-01 20:41:42 +0200480 strcpy(next->name, entry->d_name);
481 switch (entry->d_type) {
Simon Glassf1c45c82012-12-26 09:53:34 +0000482 case DT_REG:
483 next->type = OS_FILET_REG;
484 break;
485 case DT_DIR:
486 next->type = OS_FILET_DIR;
487 break;
488 case DT_LNK:
489 next->type = OS_FILET_LNK;
490 break;
Stefan Brünsb7ffa822016-10-04 21:46:35 +0200491 default:
492 next->type = OS_FILET_UNKNOWN;
Simon Glassf1c45c82012-12-26 09:53:34 +0000493 }
494 next->size = 0;
495 snprintf(fname, len, "%s/%s", dirname, next->name);
496 if (!stat(fname, &buf))
497 next->size = buf.st_size;
498 if (node)
499 node->next = next;
Stefan Brünsd4cb8882016-10-01 20:41:39 +0200500 else
501 head = next;
Simon Glassf1c45c82012-12-26 09:53:34 +0000502 }
503 *headp = head;
504
505done:
506 closedir(dir);
Simon Glassfa252262020-02-03 07:36:02 -0700507 free(fname);
Simon Glassf1c45c82012-12-26 09:53:34 +0000508 return ret;
509}
510
511const char *os_dirent_typename[OS_FILET_COUNT] = {
512 " ",
513 "SYM",
514 "DIR",
515 "???",
516};
517
518const char *os_dirent_get_typename(enum os_dirent_t type)
519{
Tom Rinib6f605e2017-05-13 20:11:30 -0400520 if (type >= OS_FILET_REG && type < OS_FILET_COUNT)
Simon Glassf1c45c82012-12-26 09:53:34 +0000521 return os_dirent_typename[type];
522
523 return os_dirent_typename[OS_FILET_UNKNOWN];
524}
525
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800526int os_get_filesize(const char *fname, loff_t *size)
Simon Glassf1c45c82012-12-26 09:53:34 +0000527{
528 struct stat buf;
529 int ret;
530
531 ret = stat(fname, &buf);
532 if (ret)
533 return ret;
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800534 *size = buf.st_size;
535 return 0;
Simon Glassf1c45c82012-12-26 09:53:34 +0000536}
Simon Glass3e9fd242013-11-10 10:27:01 -0700537
Simon Glass29d11432017-12-04 13:48:17 -0700538void os_putc(int ch)
539{
540 putchar(ch);
541}
542
543void os_puts(const char *str)
544{
545 while (*str)
546 os_putc(*str++);
547}
548
Simon Glass9dd10bf2013-11-10 10:27:03 -0700549int os_write_ram_buf(const char *fname)
550{
551 struct sandbox_state *state = state_get_current();
552 int fd, ret;
553
554 fd = open(fname, O_CREAT | O_WRONLY, 0777);
555 if (fd < 0)
556 return -ENOENT;
557 ret = write(fd, state->ram_buf, state->ram_size);
558 close(fd);
559 if (ret != state->ram_size)
560 return -EIO;
561
562 return 0;
563}
564
565int os_read_ram_buf(const char *fname)
566{
567 struct sandbox_state *state = state_get_current();
568 int fd, ret;
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800569 loff_t size;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700570
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800571 ret = os_get_filesize(fname, &size);
572 if (ret < 0)
573 return ret;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700574 if (size != state->ram_size)
575 return -ENOSPC;
576 fd = open(fname, O_RDONLY);
577 if (fd < 0)
578 return -ENOENT;
579
580 ret = read(fd, state->ram_buf, state->ram_size);
581 close(fd);
582 if (ret != state->ram_size)
583 return -EIO;
584
585 return 0;
586}
Simon Glass860b7d92014-02-27 13:26:15 -0700587
588static int make_exec(char *fname, const void *data, int size)
589{
590 int fd;
591
592 strcpy(fname, "/tmp/u-boot.jump.XXXXXX");
593 fd = mkstemp(fname);
594 if (fd < 0)
595 return -ENOENT;
596 if (write(fd, data, size) < 0)
597 return -EIO;
598 close(fd);
599 if (chmod(fname, 0777))
600 return -ENOEXEC;
601
602 return 0;
603}
604
Simon Glass7dafd022018-11-15 18:44:05 -0700605/**
606 * add_args() - Allocate a new argv with the given args
607 *
608 * This is used to create a new argv array with all the old arguments and some
609 * new ones that are passed in
610 *
611 * @argvp: Returns newly allocated args list
612 * @add_args: Arguments to add, each a string
613 * @count: Number of arguments in @add_args
614 * @return 0 if OK, -ENOMEM if out of memory
615 */
616static int add_args(char ***argvp, char *add_args[], int count)
Simon Glass860b7d92014-02-27 13:26:15 -0700617{
Simon Glass7465f002018-11-15 18:44:07 -0700618 char **argv, **ap;
Simon Glass860b7d92014-02-27 13:26:15 -0700619 int argc;
620
Simon Glass7465f002018-11-15 18:44:07 -0700621 for (argc = 0; (*argvp)[argc]; argc++)
Simon Glass860b7d92014-02-27 13:26:15 -0700622 ;
623
Simon Glassfa252262020-02-03 07:36:02 -0700624 argv = malloc((argc + count + 1) * sizeof(char *));
Simon Glass860b7d92014-02-27 13:26:15 -0700625 if (!argv) {
626 printf("Out of memory for %d argv\n", count);
627 return -ENOMEM;
628 }
Simon Glass7465f002018-11-15 18:44:07 -0700629 for (ap = *argvp, argc = 0; *ap; ap++) {
630 char *arg = *ap;
631
632 /* Drop args that we don't want to propagate */
633 if (*arg == '-' && strlen(arg) == 2) {
634 switch (arg[1]) {
635 case 'j':
636 case 'm':
637 ap++;
638 continue;
639 }
640 } else if (!strcmp(arg, "--rm_memory")) {
641 ap++;
642 continue;
643 }
644 argv[argc++] = arg;
645 }
646
Simon Glass860b7d92014-02-27 13:26:15 -0700647 memcpy(argv + argc, add_args, count * sizeof(char *));
648 argv[argc + count] = NULL;
649
650 *argvp = argv;
651 return 0;
652}
653
Simon Glass7dafd022018-11-15 18:44:05 -0700654/**
655 * os_jump_to_file() - Jump to a new program
656 *
657 * This saves the memory buffer, sets up arguments to the new process, then
658 * execs it.
659 *
660 * @fname: Filename to exec
661 * @return does not return on success, any return value is an error
662 */
663static int os_jump_to_file(const char *fname)
Simon Glass860b7d92014-02-27 13:26:15 -0700664{
665 struct sandbox_state *state = state_get_current();
Simon Glass7dafd022018-11-15 18:44:05 -0700666 char mem_fname[30];
Simon Glass860b7d92014-02-27 13:26:15 -0700667 int fd, err;
Simon Glass7dafd022018-11-15 18:44:05 -0700668 char *extra_args[5];
Simon Glass860b7d92014-02-27 13:26:15 -0700669 char **argv = state->argv;
Simon Glass7465f002018-11-15 18:44:07 -0700670 int argc;
Simon Glass860b7d92014-02-27 13:26:15 -0700671#ifdef DEBUG
Simon Glass7dafd022018-11-15 18:44:05 -0700672 int i;
Simon Glass860b7d92014-02-27 13:26:15 -0700673#endif
674
Simon Glass860b7d92014-02-27 13:26:15 -0700675 strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
676 fd = mkstemp(mem_fname);
677 if (fd < 0)
678 return -ENOENT;
679 close(fd);
680 err = os_write_ram_buf(mem_fname);
681 if (err)
682 return err;
683
684 os_fd_restore();
685
686 extra_args[0] = "-j";
Simon Glass7dafd022018-11-15 18:44:05 -0700687 extra_args[1] = (char *)fname;
Simon Glass860b7d92014-02-27 13:26:15 -0700688 extra_args[2] = "-m";
689 extra_args[3] = mem_fname;
Simon Glass7465f002018-11-15 18:44:07 -0700690 argc = 4;
691 if (state->ram_buf_rm)
692 extra_args[argc++] = "--rm_memory";
693 err = add_args(&argv, extra_args, argc);
Simon Glass860b7d92014-02-27 13:26:15 -0700694 if (err)
695 return err;
Simon Glass7465f002018-11-15 18:44:07 -0700696 argv[0] = (char *)fname;
Simon Glass860b7d92014-02-27 13:26:15 -0700697
698#ifdef DEBUG
699 for (i = 0; argv[i]; i++)
700 printf("%d %s\n", i, argv[i]);
701#endif
702
703 if (state_uninit())
704 os_exit(2);
705
706 err = execv(fname, argv);
Simon Glassfa252262020-02-03 07:36:02 -0700707 free(argv);
Simon Glasscca25522018-11-15 18:44:08 -0700708 if (err) {
709 perror("Unable to run image");
Simon Glass23eabbb2018-11-23 21:29:24 -0700710 printf("Image filename '%s'\n", fname);
Simon Glass860b7d92014-02-27 13:26:15 -0700711 return err;
Simon Glasscca25522018-11-15 18:44:08 -0700712 }
Simon Glass860b7d92014-02-27 13:26:15 -0700713
714 return unlink(fname);
715}
Simon Glass504548f2015-04-20 12:37:22 -0600716
Simon Glass7dafd022018-11-15 18:44:05 -0700717int os_jump_to_image(const void *dest, int size)
718{
719 char fname[30];
720 int err;
721
722 err = make_exec(fname, dest, size);
723 if (err)
724 return err;
725
726 return os_jump_to_file(fname);
727}
728
Simon Glass2c931ba2016-07-04 11:57:45 -0600729int os_find_u_boot(char *fname, int maxlen)
730{
731 struct sandbox_state *state = state_get_current();
732 const char *progname = state->argv[0];
733 int len = strlen(progname);
Simon Glassdc4d8eb2018-10-01 11:55:10 -0600734 const char *suffix;
Simon Glass2c931ba2016-07-04 11:57:45 -0600735 char *p;
736 int fd;
737
738 if (len >= maxlen || len < 4)
739 return -ENOSPC;
740
Simon Glass2c931ba2016-07-04 11:57:45 -0600741 strcpy(fname, progname);
Simon Glassdc4d8eb2018-10-01 11:55:10 -0600742 suffix = fname + len - 4;
743
744 /* If we are TPL, boot to SPL */
745 if (!strcmp(suffix, "-tpl")) {
746 fname[len - 3] = 's';
747 fd = os_open(fname, O_RDONLY);
748 if (fd >= 0) {
749 close(fd);
750 return 0;
751 }
752
753 /* Look for 'u-boot-tpl' in the tpl/ directory */
754 p = strstr(fname, "/tpl/");
755 if (p) {
756 p[1] = 's';
757 fd = os_open(fname, O_RDONLY);
758 if (fd >= 0) {
759 close(fd);
760 return 0;
761 }
762 }
763 return -ENOENT;
764 }
765
766 /* Look for 'u-boot' in the same directory as 'u-boot-spl' */
767 if (!strcmp(suffix, "-spl")) {
Simon Glass2c931ba2016-07-04 11:57:45 -0600768 fname[len - 4] = '\0';
769 fd = os_open(fname, O_RDONLY);
770 if (fd >= 0) {
771 close(fd);
772 return 0;
773 }
774 }
775
776 /* Look for 'u-boot' in the parent directory of spl/ */
Simon Glassf2af76b2018-11-13 15:55:20 -0700777 p = strstr(fname, "spl/");
Simon Glass2c931ba2016-07-04 11:57:45 -0600778 if (p) {
Simon Glassf2af76b2018-11-13 15:55:20 -0700779 /* Remove the "spl" characters */
780 memmove(p, p + 4, strlen(p + 4) + 1);
Simon Glass2c931ba2016-07-04 11:57:45 -0600781 fd = os_open(fname, O_RDONLY);
782 if (fd >= 0) {
783 close(fd);
784 return 0;
785 }
786 }
787
788 return -ENOENT;
789}
790
791int os_spl_to_uboot(const char *fname)
792{
Simon Glasscca25522018-11-15 18:44:08 -0700793 return os_jump_to_file(fname);
Simon Glass2c931ba2016-07-04 11:57:45 -0600794}
795
Simon Glass504548f2015-04-20 12:37:22 -0600796void os_localtime(struct rtc_time *rt)
797{
798 time_t t = time(NULL);
799 struct tm *tm;
800
801 tm = localtime(&t);
802 rt->tm_sec = tm->tm_sec;
803 rt->tm_min = tm->tm_min;
804 rt->tm_hour = tm->tm_hour;
805 rt->tm_mday = tm->tm_mday;
806 rt->tm_mon = tm->tm_mon + 1;
807 rt->tm_year = tm->tm_year + 1900;
808 rt->tm_wday = tm->tm_wday;
809 rt->tm_yday = tm->tm_yday;
810 rt->tm_isdst = tm->tm_isdst;
811}
Simon Glass5dccd152018-05-16 09:42:22 -0600812
Simon Glassb7255ef2018-09-15 00:50:55 -0600813void os_abort(void)
814{
815 abort();
816}
Simon Glass4e766c22018-10-01 21:12:32 -0600817
818int os_mprotect_allow(void *start, size_t len)
819{
820 int page_size = getpagesize();
821
822 /* Move start to the start of a page, len to the end */
823 start = (void *)(((ulong)start) & ~(page_size - 1));
824 len = (len + page_size * 2) & ~(page_size - 1);
825
826 return mprotect(start, len, PROT_READ | PROT_WRITE);
827}
Simon Glass752707a2019-04-08 13:20:41 -0600828
829void *os_find_text_base(void)
830{
831 char line[500];
832 void *base = NULL;
833 int len;
834 int fd;
835
836 /*
837 * This code assumes that the first line of /proc/self/maps holds
838 * information about the text, for example:
839 *
840 * 5622d9907000-5622d9a55000 r-xp 00000000 08:01 15067168 u-boot
841 *
842 * The first hex value is assumed to be the address.
843 *
844 * This is tested in Linux 4.15.
845 */
846 fd = open("/proc/self/maps", O_RDONLY);
847 if (fd == -1)
848 return NULL;
849 len = read(fd, line, sizeof(line));
850 if (len > 0) {
851 char *end = memchr(line, '-', len);
852
853 if (end) {
Heinrich Schuchardt05a16842019-10-26 23:17:44 +0200854 uintptr_t addr;
Simon Glass752707a2019-04-08 13:20:41 -0600855
856 *end = '\0';
Heinrich Schuchardt05a16842019-10-26 23:17:44 +0200857 if (sscanf(line, "%zx", &addr) == 1)
Simon Glass752707a2019-04-08 13:20:41 -0600858 base = (void *)addr;
859 }
860 }
861 close(fd);
862
863 return base;
864}
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100865
866void os_relaunch(char *argv[])
867{
868 execv(argv[0], argv);
869 os_exit(1);
870}