blob: c461fb0db0ed9d0c8a164987c188b36d6940248b [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
Simon Glass62584db2012-12-26 09:53:34 +00006#include <dirent.h>
Simon Glasse1012472012-01-10 15:54:05 -08007#include <errno.h>
Simon Glass7a9219c2011-10-03 19:26:44 +00008#include <fcntl.h>
Simon Glass70db4212012-02-15 15:51:16 -08009#include <getopt.h>
Simon Glass30eef212018-05-16 09:42:22 -060010#include <setjmp.h>
Rasmus Villemoes29601072020-02-14 10:58:37 +000011#include <signal.h>
Simon Glass62584db2012-12-26 09:53:34 +000012#include <stdio.h>
Simon Glass2a54d152013-05-19 16:45:35 -070013#include <stdint.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000014#include <stdlib.h>
Simon Glass62584db2012-12-26 09:53:34 +000015#include <string.h>
Mike Frysingerab06a752011-10-26 00:21:29 +000016#include <termios.h>
Matthias Weisserd99a6872011-11-29 12:16:40 +010017#include <time.h>
Simon Glasse1012472012-01-10 15:54:05 -080018#include <unistd.h>
Matthias Weisser21899b12011-11-05 11:40:34 +010019#include <sys/mman.h>
Simon Glasse1012472012-01-10 15:54:05 -080020#include <sys/stat.h>
Simon Glass3bdf56b2012-01-10 15:54:06 -080021#include <sys/time.h>
Simon Glasse1012472012-01-10 15:54:05 -080022#include <sys/types.h>
Matthias Weisserd99a6872011-11-29 12:16:40 +010023#include <linux/types.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000024
Simon Glass70db4212012-02-15 15:51:16 -080025#include <asm/getopt.h>
26#include <asm/sections.h>
27#include <asm/state.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000028#include <os.h>
Simon Glass94eefde2015-04-20 12:37:22 -060029#include <rtc_def.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000030
31/* Operating System Interface */
32
Simon Glass77595c62013-11-10 10:26:57 -070033struct os_mem_hdr {
34 size_t length; /* number of bytes in the block */
35};
36
Simon Glass7a9219c2011-10-03 19:26:44 +000037ssize_t os_read(int fd, void *buf, size_t count)
38{
39 return read(fd, buf, count);
40}
41
42ssize_t os_write(int fd, const void *buf, size_t count)
43{
44 return write(fd, buf, count);
45}
46
Mike Frysingere2dcefc2011-10-25 13:02:58 +020047off_t os_lseek(int fd, off_t offset, int whence)
48{
49 if (whence == OS_SEEK_SET)
50 whence = SEEK_SET;
51 else if (whence == OS_SEEK_CUR)
52 whence = SEEK_CUR;
53 else if (whence == OS_SEEK_END)
54 whence = SEEK_END;
55 else
56 os_exit(1);
57 return lseek(fd, offset, whence);
58}
59
Simon Glassd9165152012-02-20 23:56:58 -050060int os_open(const char *pathname, int os_flags)
Simon Glass7a9219c2011-10-03 19:26:44 +000061{
Simon Glassd9165152012-02-20 23:56:58 -050062 int flags;
63
64 switch (os_flags & OS_O_MASK) {
65 case OS_O_RDONLY:
66 default:
67 flags = O_RDONLY;
68 break;
69
70 case OS_O_WRONLY:
71 flags = O_WRONLY;
72 break;
73
74 case OS_O_RDWR:
75 flags = O_RDWR;
76 break;
77 }
78
79 if (os_flags & OS_O_CREAT)
80 flags |= O_CREAT;
Simon Glass50b288a2018-10-01 11:55:07 -060081 if (os_flags & OS_O_TRUNC)
82 flags |= O_TRUNC;
Simon Glassd9165152012-02-20 23:56:58 -050083
84 return open(pathname, flags, 0777);
Simon Glass7a9219c2011-10-03 19:26:44 +000085}
86
87int os_close(int fd)
88{
Heinrich Schuchardt6eec4b02020-10-27 20:29:21 +010089 /* Do not close the console input */
90 if (fd)
91 return close(fd);
92 return -1;
Simon Glass7a9219c2011-10-03 19:26:44 +000093}
94
Stephen Warrencfd13e82014-03-01 22:18:00 -070095int os_unlink(const char *pathname)
96{
97 return unlink(pathname);
98}
99
Simon Glass7a9219c2011-10-03 19:26:44 +0000100void os_exit(int exit_code)
101{
102 exit(exit_code);
103}
Mike Frysingerab06a752011-10-26 00:21:29 +0000104
Simon Glass566bf3a2018-11-06 15:21:25 -0700105int os_write_file(const char *fname, const void *buf, int size)
Simon Glass056a5ce2018-10-01 11:55:08 -0600106{
Simon Glass056a5ce2018-10-01 11:55:08 -0600107 int fd;
108
109 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC);
110 if (fd < 0) {
111 printf("Cannot open file '%s'\n", fname);
112 return -EIO;
113 }
114 if (os_write(fd, buf, size) != size) {
115 printf("Cannot write to file '%s'\n", fname);
Simon Glass566bf3a2018-11-06 15:21:25 -0700116 os_close(fd);
Simon Glass056a5ce2018-10-01 11:55:08 -0600117 return -EIO;
118 }
119 os_close(fd);
Simon Glass056a5ce2018-10-01 11:55:08 -0600120
121 return 0;
122}
123
Simon Glass566bf3a2018-11-06 15:21:25 -0700124int os_read_file(const char *fname, void **bufp, int *sizep)
125{
126 off_t size;
127 int ret = -EIO;
128 int fd;
129
130 fd = os_open(fname, OS_O_RDONLY);
131 if (fd < 0) {
132 printf("Cannot open file '%s'\n", fname);
133 goto err;
134 }
135 size = os_lseek(fd, 0, OS_SEEK_END);
136 if (size < 0) {
137 printf("Cannot seek to end of file '%s'\n", fname);
138 goto err;
139 }
140 if (os_lseek(fd, 0, OS_SEEK_SET) < 0) {
141 printf("Cannot seek to start of file '%s'\n", fname);
142 goto err;
143 }
Simon Glass0db1b432020-02-03 07:36:02 -0700144 *bufp = malloc(size);
Simon Glass566bf3a2018-11-06 15:21:25 -0700145 if (!*bufp) {
146 printf("Not enough memory to read file '%s'\n", fname);
147 ret = -ENOMEM;
148 goto err;
149 }
150 if (os_read(fd, *bufp, size) != size) {
151 printf("Cannot read from file '%s'\n", fname);
152 goto err;
153 }
154 os_close(fd);
155 *sizep = size;
156
157 return 0;
158err:
159 os_close(fd);
160 return ret;
161}
162
Mike Frysingerab06a752011-10-26 00:21:29 +0000163/* Restore tty state when we exit */
164static struct termios orig_term;
Simon Glassffb87902014-02-27 13:26:22 -0700165static bool term_setup;
Simon Glass4af3e9a2018-10-01 11:55:20 -0600166static bool term_nonblock;
Mike Frysingerab06a752011-10-26 00:21:29 +0000167
Simon Glass8939df02015-05-10 21:07:27 -0600168void os_fd_restore(void)
Mike Frysingerab06a752011-10-26 00:21:29 +0000169{
Simon Glass8939df02015-05-10 21:07:27 -0600170 if (term_setup) {
Simon Glass4af3e9a2018-10-01 11:55:20 -0600171 int flags;
172
Simon Glassffb87902014-02-27 13:26:22 -0700173 tcsetattr(0, TCSANOW, &orig_term);
Simon Glass4af3e9a2018-10-01 11:55:20 -0600174 if (term_nonblock) {
175 flags = fcntl(0, F_GETFL, 0);
176 fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
177 }
Simon Glass8939df02015-05-10 21:07:27 -0600178 term_setup = false;
179 }
Mike Frysingerab06a752011-10-26 00:21:29 +0000180}
181
Rasmus Villemoes29601072020-02-14 10:58:37 +0000182static void os_sigint_handler(int sig)
183{
184 os_fd_restore();
185 signal(SIGINT, SIG_DFL);
186 raise(SIGINT);
187}
188
Mike Frysingerab06a752011-10-26 00:21:29 +0000189/* Put tty into raw mode so <tab> and <ctrl+c> work */
Simon Glassffb87902014-02-27 13:26:22 -0700190void os_tty_raw(int fd, bool allow_sigs)
Mike Frysingerab06a752011-10-26 00:21:29 +0000191{
Mike Frysingerab06a752011-10-26 00:21:29 +0000192 struct termios term;
Simon Glass4af3e9a2018-10-01 11:55:20 -0600193 int flags;
Mike Frysingerab06a752011-10-26 00:21:29 +0000194
Simon Glassffb87902014-02-27 13:26:22 -0700195 if (term_setup)
Mike Frysingerab06a752011-10-26 00:21:29 +0000196 return;
Mike Frysingerab06a752011-10-26 00:21:29 +0000197
198 /* If not a tty, don't complain */
199 if (tcgetattr(fd, &orig_term))
200 return;
201
202 term = orig_term;
203 term.c_iflag = IGNBRK | IGNPAR;
204 term.c_oflag = OPOST | ONLCR;
205 term.c_cflag = CS8 | CREAD | CLOCAL;
Simon Glassffb87902014-02-27 13:26:22 -0700206 term.c_lflag = allow_sigs ? ISIG : 0;
Mike Frysingerab06a752011-10-26 00:21:29 +0000207 if (tcsetattr(fd, TCSANOW, &term))
208 return;
209
Simon Glass4af3e9a2018-10-01 11:55:20 -0600210 flags = fcntl(fd, F_GETFL, 0);
211 if (!(flags & O_NONBLOCK)) {
212 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
213 return;
214 term_nonblock = true;
215 }
216
Simon Glass8939df02015-05-10 21:07:27 -0600217 term_setup = true;
Mike Frysingerab06a752011-10-26 00:21:29 +0000218 atexit(os_fd_restore);
Rasmus Villemoes29601072020-02-14 10:58:37 +0000219 signal(SIGINT, os_sigint_handler);
Mike Frysingerab06a752011-10-26 00:21:29 +0000220}
Matthias Weisser21899b12011-11-05 11:40:34 +0100221
222void *os_malloc(size_t length)
223{
Simon Glass61318502018-09-15 00:50:54 -0600224 int page_size = getpagesize();
Simon Glass4a6409b2019-04-08 13:20:42 -0600225 struct os_mem_hdr *hdr;
Simon Glass77595c62013-11-10 10:26:57 -0700226
Simon Glassbd8b7452018-06-17 08:57:43 -0600227 /*
228 * Use an address that is hopefully available to us so that pointers
229 * to this memory are fairly obvious. If we end up with a different
230 * address, that's fine too.
231 */
232 hdr = mmap((void *)0x10000000, length + page_size,
Alexander Graf6e6e4b22018-06-22 14:44:13 +0200233 PROT_READ | PROT_WRITE | PROT_EXEC,
234 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Simon Glass77595c62013-11-10 10:26:57 -0700235 if (hdr == MAP_FAILED)
236 return NULL;
237 hdr->length = length;
238
Simon Glass61318502018-09-15 00:50:54 -0600239 return (void *)hdr + page_size;
Simon Glass77595c62013-11-10 10:26:57 -0700240}
241
Masahiro Yamada347d06d2014-01-15 13:06:41 +0900242void os_free(void *ptr)
Simon Glass77595c62013-11-10 10:26:57 -0700243{
Simon Glass4a6409b2019-04-08 13:20:42 -0600244 int page_size = getpagesize();
245 struct os_mem_hdr *hdr;
Simon Glass77595c62013-11-10 10:26:57 -0700246
Simon Glass4a6409b2019-04-08 13:20:42 -0600247 if (ptr) {
248 hdr = ptr - page_size;
249 munmap(hdr, hdr->length + page_size);
250 }
Simon Glass77595c62013-11-10 10:26:57 -0700251}
252
Matthias Weisserd99a6872011-11-29 12:16:40 +0100253void os_usleep(unsigned long usec)
254{
255 usleep(usec);
256}
257
Simon Glass2a54d152013-05-19 16:45:35 -0700258uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
Matthias Weisserd99a6872011-11-29 12:16:40 +0100259{
260#if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
261 struct timespec tp;
262 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
263 struct timeval tv;
264
265 gettimeofday(&tv, NULL);
266 tp.tv_sec = tv.tv_sec;
267 tp.tv_nsec = tv.tv_usec * 1000;
268 }
269 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
270#else
271 struct timeval tv;
272 gettimeofday(&tv, NULL);
273 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
274#endif
275}
Simon Glass70db4212012-02-15 15:51:16 -0800276
277static char *short_opts;
278static struct option *long_opts;
279
280int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
281{
Simon Glass7b3efc62013-12-03 16:43:23 -0700282 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass70db4212012-02-15 15:51:16 -0800283 size_t num_options = __u_boot_sandbox_option_count();
284 size_t i;
285
286 int hidden_short_opt;
287 size_t si;
288
289 int c;
290
291 if (short_opts || long_opts)
292 return 1;
293
294 state->argc = argc;
295 state->argv = argv;
296
297 /* dynamically construct the arguments to the system getopt_long */
Simon Glass0db1b432020-02-03 07:36:02 -0700298 short_opts = malloc(sizeof(*short_opts) * num_options * 2 + 1);
Simon Glass1c8c47e2020-02-03 07:36:04 -0700299 long_opts = malloc(sizeof(*long_opts) * (num_options + 1));
Simon Glass70db4212012-02-15 15:51:16 -0800300 if (!short_opts || !long_opts)
301 return 1;
302
303 /*
304 * getopt_long requires "val" to be unique (since that is what the
305 * func returns), so generate unique values automatically for flags
306 * that don't have a short option. pick 0x100 as that is above the
307 * single byte range (where ASCII/ISO-XXXX-X charsets live).
308 */
309 hidden_short_opt = 0x100;
310 si = 0;
311 for (i = 0; i < num_options; ++i) {
312 long_opts[i].name = sb_opt[i]->flag;
313 long_opts[i].has_arg = sb_opt[i]->has_arg ?
314 required_argument : no_argument;
315 long_opts[i].flag = NULL;
316
317 if (sb_opt[i]->flag_short) {
318 short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
319 if (long_opts[i].has_arg == required_argument)
320 short_opts[si++] = ':';
321 } else
322 long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
323 }
324 short_opts[si] = '\0';
325
326 /* we need to handle output ourselves since u-boot provides printf */
327 opterr = 0;
328
Simon Glass1c8c47e2020-02-03 07:36:04 -0700329 memset(&long_opts[num_options], '\0', sizeof(*long_opts));
Simon Glass70db4212012-02-15 15:51:16 -0800330 /*
331 * walk all of the options the user gave us on the command line,
332 * figure out what u-boot option structure they belong to (via
333 * the unique short val key), and call the appropriate callback.
334 */
335 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
336 for (i = 0; i < num_options; ++i) {
337 if (sb_opt[i]->flag_short == c) {
338 if (sb_opt[i]->callback(state, optarg)) {
339 state->parse_err = sb_opt[i]->flag;
340 return 0;
341 }
342 break;
343 }
344 }
345 if (i == num_options) {
346 /*
347 * store the faulting flag for later display. we have to
348 * store the flag itself as the getopt parsing itself is
349 * tricky: need to handle the following flags (assume all
350 * of the below are unknown):
351 * -a optopt='a' optind=<next>
352 * -abbbb optopt='a' optind=<this>
353 * -aaaaa optopt='a' optind=<this>
354 * --a optopt=0 optind=<this>
355 * as you can see, it is impossible to determine the exact
356 * faulting flag without doing the parsing ourselves, so
357 * we just report the specific flag that failed.
358 */
359 if (optopt) {
360 static char parse_err[3] = { '-', 0, '\0', };
361 parse_err[1] = optopt;
362 state->parse_err = parse_err;
363 } else
364 state->parse_err = argv[optind - 1];
365 break;
366 }
367 }
368
369 return 0;
370}
Simon Glass62584db2012-12-26 09:53:34 +0000371
372void os_dirent_free(struct os_dirent_node *node)
373{
374 struct os_dirent_node *next;
375
376 while (node) {
377 next = node->next;
Simon Glass0db1b432020-02-03 07:36:02 -0700378 free(node);
Simon Glass62584db2012-12-26 09:53:34 +0000379 node = next;
380 }
381}
382
383int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
384{
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200385 struct dirent *entry;
Simon Glass62584db2012-12-26 09:53:34 +0000386 struct os_dirent_node *head, *node, *next;
387 struct stat buf;
388 DIR *dir;
389 int ret;
390 char *fname;
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200391 char *old_fname;
Simon Glass62584db2012-12-26 09:53:34 +0000392 int len;
Stefan Brünsf189899c2016-10-01 20:41:40 +0200393 int dirlen;
Simon Glass62584db2012-12-26 09:53:34 +0000394
395 *headp = NULL;
396 dir = opendir(dirname);
397 if (!dir)
398 return -1;
399
Stefan Brünsf189899c2016-10-01 20:41:40 +0200400 /* Create a buffer upfront, with typically sufficient size */
401 dirlen = strlen(dirname) + 2;
402 len = dirlen + 256;
Simon Glass0db1b432020-02-03 07:36:02 -0700403 fname = malloc(len);
Simon Glass62584db2012-12-26 09:53:34 +0000404 if (!fname) {
405 ret = -ENOMEM;
406 goto done;
407 }
408
409 for (node = head = NULL;; node = next) {
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200410 errno = 0;
411 entry = readdir(dir);
412 if (!entry) {
413 ret = errno;
Simon Glass62584db2012-12-26 09:53:34 +0000414 break;
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200415 }
Simon Glass0db1b432020-02-03 07:36:02 -0700416 next = malloc(sizeof(*node) + strlen(entry->d_name) + 1);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200417 if (!next) {
Simon Glass62584db2012-12-26 09:53:34 +0000418 os_dirent_free(head);
419 ret = -ENOMEM;
420 goto done;
421 }
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200422 if (dirlen + strlen(entry->d_name) > len) {
423 len = dirlen + strlen(entry->d_name);
424 old_fname = fname;
Simon Glass0db1b432020-02-03 07:36:02 -0700425 fname = realloc(fname, len);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200426 if (!fname) {
Simon Glass0db1b432020-02-03 07:36:02 -0700427 free(old_fname);
428 free(next);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200429 os_dirent_free(head);
430 ret = -ENOMEM;
431 goto done;
432 }
433 }
Stephen Warren9c38c072014-06-11 10:26:23 -0600434 next->next = NULL;
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200435 strcpy(next->name, entry->d_name);
436 switch (entry->d_type) {
Simon Glass62584db2012-12-26 09:53:34 +0000437 case DT_REG:
438 next->type = OS_FILET_REG;
439 break;
440 case DT_DIR:
441 next->type = OS_FILET_DIR;
442 break;
443 case DT_LNK:
444 next->type = OS_FILET_LNK;
445 break;
Stefan Brüns2f159402016-10-04 21:46:35 +0200446 default:
447 next->type = OS_FILET_UNKNOWN;
Simon Glass62584db2012-12-26 09:53:34 +0000448 }
449 next->size = 0;
450 snprintf(fname, len, "%s/%s", dirname, next->name);
451 if (!stat(fname, &buf))
452 next->size = buf.st_size;
453 if (node)
454 node->next = next;
Stefan Brünsce2ec192016-10-01 20:41:39 +0200455 else
456 head = next;
Simon Glass62584db2012-12-26 09:53:34 +0000457 }
458 *headp = head;
459
460done:
461 closedir(dir);
Simon Glass0db1b432020-02-03 07:36:02 -0700462 free(fname);
Simon Glass62584db2012-12-26 09:53:34 +0000463 return ret;
464}
465
466const char *os_dirent_typename[OS_FILET_COUNT] = {
467 " ",
468 "SYM",
469 "DIR",
470 "???",
471};
472
473const char *os_dirent_get_typename(enum os_dirent_t type)
474{
Tom Rinie2bc87d2017-05-13 20:11:30 -0400475 if (type >= OS_FILET_REG && type < OS_FILET_COUNT)
Simon Glass62584db2012-12-26 09:53:34 +0000476 return os_dirent_typename[type];
477
478 return os_dirent_typename[OS_FILET_UNKNOWN];
479}
480
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800481int os_get_filesize(const char *fname, loff_t *size)
Simon Glass62584db2012-12-26 09:53:34 +0000482{
483 struct stat buf;
484 int ret;
485
486 ret = stat(fname, &buf);
487 if (ret)
488 return ret;
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800489 *size = buf.st_size;
490 return 0;
Simon Glass62584db2012-12-26 09:53:34 +0000491}
Simon Glass91b136c2013-11-10 10:27:01 -0700492
Simon Glass0b189b62017-12-04 13:48:17 -0700493void os_putc(int ch)
494{
495 putchar(ch);
496}
497
498void os_puts(const char *str)
499{
500 while (*str)
501 os_putc(*str++);
502}
503
Simon Glass5c2859c2013-11-10 10:27:03 -0700504int os_write_ram_buf(const char *fname)
505{
506 struct sandbox_state *state = state_get_current();
507 int fd, ret;
508
509 fd = open(fname, O_CREAT | O_WRONLY, 0777);
510 if (fd < 0)
511 return -ENOENT;
512 ret = write(fd, state->ram_buf, state->ram_size);
513 close(fd);
514 if (ret != state->ram_size)
515 return -EIO;
516
517 return 0;
518}
519
520int os_read_ram_buf(const char *fname)
521{
522 struct sandbox_state *state = state_get_current();
523 int fd, ret;
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800524 loff_t size;
Simon Glass5c2859c2013-11-10 10:27:03 -0700525
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800526 ret = os_get_filesize(fname, &size);
527 if (ret < 0)
528 return ret;
Simon Glass5c2859c2013-11-10 10:27:03 -0700529 if (size != state->ram_size)
530 return -ENOSPC;
531 fd = open(fname, O_RDONLY);
532 if (fd < 0)
533 return -ENOENT;
534
535 ret = read(fd, state->ram_buf, state->ram_size);
536 close(fd);
537 if (ret != state->ram_size)
538 return -EIO;
539
540 return 0;
541}
Simon Glass47f5fcf2014-02-27 13:26:15 -0700542
543static int make_exec(char *fname, const void *data, int size)
544{
545 int fd;
546
547 strcpy(fname, "/tmp/u-boot.jump.XXXXXX");
548 fd = mkstemp(fname);
549 if (fd < 0)
550 return -ENOENT;
551 if (write(fd, data, size) < 0)
552 return -EIO;
553 close(fd);
554 if (chmod(fname, 0777))
555 return -ENOEXEC;
556
557 return 0;
558}
559
Simon Glass7b5ea142018-11-15 18:44:05 -0700560/**
561 * add_args() - Allocate a new argv with the given args
562 *
563 * This is used to create a new argv array with all the old arguments and some
564 * new ones that are passed in
565 *
566 * @argvp: Returns newly allocated args list
567 * @add_args: Arguments to add, each a string
568 * @count: Number of arguments in @add_args
569 * @return 0 if OK, -ENOMEM if out of memory
570 */
571static int add_args(char ***argvp, char *add_args[], int count)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700572{
Simon Glass65f3b1f2018-11-15 18:44:07 -0700573 char **argv, **ap;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700574 int argc;
575
Simon Glass65f3b1f2018-11-15 18:44:07 -0700576 for (argc = 0; (*argvp)[argc]; argc++)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700577 ;
578
Simon Glass0db1b432020-02-03 07:36:02 -0700579 argv = malloc((argc + count + 1) * sizeof(char *));
Simon Glass47f5fcf2014-02-27 13:26:15 -0700580 if (!argv) {
581 printf("Out of memory for %d argv\n", count);
582 return -ENOMEM;
583 }
Simon Glass65f3b1f2018-11-15 18:44:07 -0700584 for (ap = *argvp, argc = 0; *ap; ap++) {
585 char *arg = *ap;
586
587 /* Drop args that we don't want to propagate */
588 if (*arg == '-' && strlen(arg) == 2) {
589 switch (arg[1]) {
590 case 'j':
591 case 'm':
592 ap++;
593 continue;
594 }
595 } else if (!strcmp(arg, "--rm_memory")) {
596 ap++;
597 continue;
598 }
599 argv[argc++] = arg;
600 }
601
Simon Glass47f5fcf2014-02-27 13:26:15 -0700602 memcpy(argv + argc, add_args, count * sizeof(char *));
603 argv[argc + count] = NULL;
604
605 *argvp = argv;
606 return 0;
607}
608
Simon Glass7b5ea142018-11-15 18:44:05 -0700609/**
610 * os_jump_to_file() - Jump to a new program
611 *
612 * This saves the memory buffer, sets up arguments to the new process, then
613 * execs it.
614 *
615 * @fname: Filename to exec
616 * @return does not return on success, any return value is an error
617 */
618static int os_jump_to_file(const char *fname)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700619{
620 struct sandbox_state *state = state_get_current();
Simon Glass7b5ea142018-11-15 18:44:05 -0700621 char mem_fname[30];
Simon Glass47f5fcf2014-02-27 13:26:15 -0700622 int fd, err;
Simon Glass7b5ea142018-11-15 18:44:05 -0700623 char *extra_args[5];
Simon Glass47f5fcf2014-02-27 13:26:15 -0700624 char **argv = state->argv;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700625 int argc;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700626#ifdef DEBUG
Simon Glass7b5ea142018-11-15 18:44:05 -0700627 int i;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700628#endif
629
Simon Glass47f5fcf2014-02-27 13:26:15 -0700630 strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
631 fd = mkstemp(mem_fname);
632 if (fd < 0)
633 return -ENOENT;
634 close(fd);
635 err = os_write_ram_buf(mem_fname);
636 if (err)
637 return err;
638
639 os_fd_restore();
640
641 extra_args[0] = "-j";
Simon Glass7b5ea142018-11-15 18:44:05 -0700642 extra_args[1] = (char *)fname;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700643 extra_args[2] = "-m";
644 extra_args[3] = mem_fname;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700645 argc = 4;
646 if (state->ram_buf_rm)
647 extra_args[argc++] = "--rm_memory";
648 err = add_args(&argv, extra_args, argc);
Simon Glass47f5fcf2014-02-27 13:26:15 -0700649 if (err)
650 return err;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700651 argv[0] = (char *)fname;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700652
653#ifdef DEBUG
654 for (i = 0; argv[i]; i++)
655 printf("%d %s\n", i, argv[i]);
656#endif
657
658 if (state_uninit())
659 os_exit(2);
660
661 err = execv(fname, argv);
Simon Glass0db1b432020-02-03 07:36:02 -0700662 free(argv);
Simon Glass27028f12018-11-15 18:44:08 -0700663 if (err) {
664 perror("Unable to run image");
Simon Glass6b5e4202018-11-23 21:29:24 -0700665 printf("Image filename '%s'\n", fname);
Simon Glass47f5fcf2014-02-27 13:26:15 -0700666 return err;
Simon Glass27028f12018-11-15 18:44:08 -0700667 }
Simon Glass47f5fcf2014-02-27 13:26:15 -0700668
669 return unlink(fname);
670}
Simon Glass94eefde2015-04-20 12:37:22 -0600671
Simon Glass7b5ea142018-11-15 18:44:05 -0700672int os_jump_to_image(const void *dest, int size)
673{
674 char fname[30];
675 int err;
676
677 err = make_exec(fname, dest, size);
678 if (err)
679 return err;
680
681 return os_jump_to_file(fname);
682}
683
Simon Glassd4e33f52016-07-04 11:57:45 -0600684int os_find_u_boot(char *fname, int maxlen)
685{
686 struct sandbox_state *state = state_get_current();
687 const char *progname = state->argv[0];
688 int len = strlen(progname);
Simon Glass69bc15d2018-10-01 11:55:10 -0600689 const char *suffix;
Simon Glassd4e33f52016-07-04 11:57:45 -0600690 char *p;
691 int fd;
692
693 if (len >= maxlen || len < 4)
694 return -ENOSPC;
695
Simon Glassd4e33f52016-07-04 11:57:45 -0600696 strcpy(fname, progname);
Simon Glass69bc15d2018-10-01 11:55:10 -0600697 suffix = fname + len - 4;
698
699 /* If we are TPL, boot to SPL */
700 if (!strcmp(suffix, "-tpl")) {
701 fname[len - 3] = 's';
702 fd = os_open(fname, O_RDONLY);
703 if (fd >= 0) {
704 close(fd);
705 return 0;
706 }
707
708 /* Look for 'u-boot-tpl' in the tpl/ directory */
709 p = strstr(fname, "/tpl/");
710 if (p) {
711 p[1] = 's';
712 fd = os_open(fname, O_RDONLY);
713 if (fd >= 0) {
714 close(fd);
715 return 0;
716 }
717 }
718 return -ENOENT;
719 }
720
721 /* Look for 'u-boot' in the same directory as 'u-boot-spl' */
722 if (!strcmp(suffix, "-spl")) {
Simon Glassd4e33f52016-07-04 11:57:45 -0600723 fname[len - 4] = '\0';
724 fd = os_open(fname, O_RDONLY);
725 if (fd >= 0) {
726 close(fd);
727 return 0;
728 }
729 }
730
731 /* Look for 'u-boot' in the parent directory of spl/ */
Simon Glassb847c142018-11-13 15:55:20 -0700732 p = strstr(fname, "spl/");
Simon Glassd4e33f52016-07-04 11:57:45 -0600733 if (p) {
Simon Glassb847c142018-11-13 15:55:20 -0700734 /* Remove the "spl" characters */
735 memmove(p, p + 4, strlen(p + 4) + 1);
Simon Glassd4e33f52016-07-04 11:57:45 -0600736 fd = os_open(fname, O_RDONLY);
737 if (fd >= 0) {
738 close(fd);
739 return 0;
740 }
741 }
742
743 return -ENOENT;
744}
745
746int os_spl_to_uboot(const char *fname)
747{
Simon Glass27028f12018-11-15 18:44:08 -0700748 return os_jump_to_file(fname);
Simon Glassd4e33f52016-07-04 11:57:45 -0600749}
750
Simon Glass94eefde2015-04-20 12:37:22 -0600751void os_localtime(struct rtc_time *rt)
752{
753 time_t t = time(NULL);
754 struct tm *tm;
755
756 tm = localtime(&t);
757 rt->tm_sec = tm->tm_sec;
758 rt->tm_min = tm->tm_min;
759 rt->tm_hour = tm->tm_hour;
760 rt->tm_mday = tm->tm_mday;
761 rt->tm_mon = tm->tm_mon + 1;
762 rt->tm_year = tm->tm_year + 1900;
763 rt->tm_wday = tm->tm_wday;
764 rt->tm_yday = tm->tm_yday;
765 rt->tm_isdst = tm->tm_isdst;
766}
Simon Glass30eef212018-05-16 09:42:22 -0600767
Simon Glassfe938fb2018-09-15 00:50:55 -0600768void os_abort(void)
769{
770 abort();
771}
Simon Glass9f8037e2018-10-01 21:12:32 -0600772
773int os_mprotect_allow(void *start, size_t len)
774{
775 int page_size = getpagesize();
776
777 /* Move start to the start of a page, len to the end */
778 start = (void *)(((ulong)start) & ~(page_size - 1));
779 len = (len + page_size * 2) & ~(page_size - 1);
780
781 return mprotect(start, len, PROT_READ | PROT_WRITE);
782}
Simon Glass001d1882019-04-08 13:20:41 -0600783
784void *os_find_text_base(void)
785{
786 char line[500];
787 void *base = NULL;
788 int len;
789 int fd;
790
791 /*
792 * This code assumes that the first line of /proc/self/maps holds
793 * information about the text, for example:
794 *
795 * 5622d9907000-5622d9a55000 r-xp 00000000 08:01 15067168 u-boot
796 *
797 * The first hex value is assumed to be the address.
798 *
799 * This is tested in Linux 4.15.
800 */
801 fd = open("/proc/self/maps", O_RDONLY);
802 if (fd == -1)
803 return NULL;
804 len = read(fd, line, sizeof(line));
805 if (len > 0) {
806 char *end = memchr(line, '-', len);
807
808 if (end) {
Heinrich Schuchardt4ecb5062019-10-26 23:17:44 +0200809 uintptr_t addr;
Simon Glass001d1882019-04-08 13:20:41 -0600810
811 *end = '\0';
Heinrich Schuchardt4ecb5062019-10-26 23:17:44 +0200812 if (sscanf(line, "%zx", &addr) == 1)
Simon Glass001d1882019-04-08 13:20:41 -0600813 base = (void *)addr;
814 }
815 }
816 close(fd);
817
818 return base;
819}