blob: 7e474d63644270a87c48800d35af83bf8bedf8d6 [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;
Heinrich Schuchardtc0b19f22020-10-27 20:29:24 +010083 /*
84 * During a cold reset execv() is used to relaunch the U-Boot binary.
85 * We must ensure that all files are closed in this case.
86 */
87 flags |= O_CLOEXEC;
Simon Glassd9165152012-02-20 23:56:58 -050088
89 return open(pathname, flags, 0777);
Simon Glass7a9219c2011-10-03 19:26:44 +000090}
91
92int os_close(int fd)
93{
Heinrich Schuchardt6eec4b02020-10-27 20:29:21 +010094 /* Do not close the console input */
95 if (fd)
96 return close(fd);
97 return -1;
Simon Glass7a9219c2011-10-03 19:26:44 +000098}
99
Stephen Warrencfd13e82014-03-01 22:18:00 -0700100int os_unlink(const char *pathname)
101{
102 return unlink(pathname);
103}
104
Simon Glass7a9219c2011-10-03 19:26:44 +0000105void os_exit(int exit_code)
106{
107 exit(exit_code);
108}
Mike Frysingerab06a752011-10-26 00:21:29 +0000109
Simon Glass566bf3a2018-11-06 15:21:25 -0700110int os_write_file(const char *fname, const void *buf, int size)
Simon Glass056a5ce2018-10-01 11:55:08 -0600111{
Simon Glass056a5ce2018-10-01 11:55:08 -0600112 int fd;
113
114 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC);
115 if (fd < 0) {
116 printf("Cannot open file '%s'\n", fname);
117 return -EIO;
118 }
119 if (os_write(fd, buf, size) != size) {
120 printf("Cannot write to file '%s'\n", fname);
Simon Glass566bf3a2018-11-06 15:21:25 -0700121 os_close(fd);
Simon Glass056a5ce2018-10-01 11:55:08 -0600122 return -EIO;
123 }
124 os_close(fd);
Simon Glass056a5ce2018-10-01 11:55:08 -0600125
126 return 0;
127}
128
Simon Glass566bf3a2018-11-06 15:21:25 -0700129int os_read_file(const char *fname, void **bufp, int *sizep)
130{
131 off_t size;
132 int ret = -EIO;
133 int fd;
134
135 fd = os_open(fname, OS_O_RDONLY);
136 if (fd < 0) {
137 printf("Cannot open file '%s'\n", fname);
138 goto err;
139 }
140 size = os_lseek(fd, 0, OS_SEEK_END);
141 if (size < 0) {
142 printf("Cannot seek to end of file '%s'\n", fname);
143 goto err;
144 }
145 if (os_lseek(fd, 0, OS_SEEK_SET) < 0) {
146 printf("Cannot seek to start of file '%s'\n", fname);
147 goto err;
148 }
Simon Glass0db1b432020-02-03 07:36:02 -0700149 *bufp = malloc(size);
Simon Glass566bf3a2018-11-06 15:21:25 -0700150 if (!*bufp) {
151 printf("Not enough memory to read file '%s'\n", fname);
152 ret = -ENOMEM;
153 goto err;
154 }
155 if (os_read(fd, *bufp, size) != size) {
156 printf("Cannot read from file '%s'\n", fname);
157 goto err;
158 }
159 os_close(fd);
160 *sizep = size;
161
162 return 0;
163err:
164 os_close(fd);
165 return ret;
166}
167
Mike Frysingerab06a752011-10-26 00:21:29 +0000168/* Restore tty state when we exit */
169static struct termios orig_term;
Simon Glassffb87902014-02-27 13:26:22 -0700170static bool term_setup;
Simon Glass4af3e9a2018-10-01 11:55:20 -0600171static bool term_nonblock;
Mike Frysingerab06a752011-10-26 00:21:29 +0000172
Simon Glass8939df02015-05-10 21:07:27 -0600173void os_fd_restore(void)
Mike Frysingerab06a752011-10-26 00:21:29 +0000174{
Simon Glass8939df02015-05-10 21:07:27 -0600175 if (term_setup) {
Simon Glass4af3e9a2018-10-01 11:55:20 -0600176 int flags;
177
Simon Glassffb87902014-02-27 13:26:22 -0700178 tcsetattr(0, TCSANOW, &orig_term);
Simon Glass4af3e9a2018-10-01 11:55:20 -0600179 if (term_nonblock) {
180 flags = fcntl(0, F_GETFL, 0);
181 fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
182 }
Simon Glass8939df02015-05-10 21:07:27 -0600183 term_setup = false;
184 }
Mike Frysingerab06a752011-10-26 00:21:29 +0000185}
186
Rasmus Villemoes29601072020-02-14 10:58:37 +0000187static void os_sigint_handler(int sig)
188{
189 os_fd_restore();
190 signal(SIGINT, SIG_DFL);
191 raise(SIGINT);
192}
193
Mike Frysingerab06a752011-10-26 00:21:29 +0000194/* Put tty into raw mode so <tab> and <ctrl+c> work */
Simon Glassffb87902014-02-27 13:26:22 -0700195void os_tty_raw(int fd, bool allow_sigs)
Mike Frysingerab06a752011-10-26 00:21:29 +0000196{
Mike Frysingerab06a752011-10-26 00:21:29 +0000197 struct termios term;
Simon Glass4af3e9a2018-10-01 11:55:20 -0600198 int flags;
Mike Frysingerab06a752011-10-26 00:21:29 +0000199
Simon Glassffb87902014-02-27 13:26:22 -0700200 if (term_setup)
Mike Frysingerab06a752011-10-26 00:21:29 +0000201 return;
Mike Frysingerab06a752011-10-26 00:21:29 +0000202
203 /* If not a tty, don't complain */
204 if (tcgetattr(fd, &orig_term))
205 return;
206
207 term = orig_term;
208 term.c_iflag = IGNBRK | IGNPAR;
209 term.c_oflag = OPOST | ONLCR;
210 term.c_cflag = CS8 | CREAD | CLOCAL;
Simon Glassffb87902014-02-27 13:26:22 -0700211 term.c_lflag = allow_sigs ? ISIG : 0;
Mike Frysingerab06a752011-10-26 00:21:29 +0000212 if (tcsetattr(fd, TCSANOW, &term))
213 return;
214
Simon Glass4af3e9a2018-10-01 11:55:20 -0600215 flags = fcntl(fd, F_GETFL, 0);
216 if (!(flags & O_NONBLOCK)) {
217 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
218 return;
219 term_nonblock = true;
220 }
221
Simon Glass8939df02015-05-10 21:07:27 -0600222 term_setup = true;
Mike Frysingerab06a752011-10-26 00:21:29 +0000223 atexit(os_fd_restore);
Rasmus Villemoes29601072020-02-14 10:58:37 +0000224 signal(SIGINT, os_sigint_handler);
Mike Frysingerab06a752011-10-26 00:21:29 +0000225}
Matthias Weisser21899b12011-11-05 11:40:34 +0100226
227void *os_malloc(size_t length)
228{
Simon Glass61318502018-09-15 00:50:54 -0600229 int page_size = getpagesize();
Simon Glass4a6409b2019-04-08 13:20:42 -0600230 struct os_mem_hdr *hdr;
Simon Glass77595c62013-11-10 10:26:57 -0700231
Simon Glassbd8b7452018-06-17 08:57:43 -0600232 /*
233 * Use an address that is hopefully available to us so that pointers
234 * to this memory are fairly obvious. If we end up with a different
235 * address, that's fine too.
236 */
237 hdr = mmap((void *)0x10000000, length + page_size,
Alexander Graf6e6e4b22018-06-22 14:44:13 +0200238 PROT_READ | PROT_WRITE | PROT_EXEC,
239 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Simon Glass77595c62013-11-10 10:26:57 -0700240 if (hdr == MAP_FAILED)
241 return NULL;
242 hdr->length = length;
243
Simon Glass61318502018-09-15 00:50:54 -0600244 return (void *)hdr + page_size;
Simon Glass77595c62013-11-10 10:26:57 -0700245}
246
Masahiro Yamada347d06d2014-01-15 13:06:41 +0900247void os_free(void *ptr)
Simon Glass77595c62013-11-10 10:26:57 -0700248{
Simon Glass4a6409b2019-04-08 13:20:42 -0600249 int page_size = getpagesize();
250 struct os_mem_hdr *hdr;
Simon Glass77595c62013-11-10 10:26:57 -0700251
Simon Glass4a6409b2019-04-08 13:20:42 -0600252 if (ptr) {
253 hdr = ptr - page_size;
254 munmap(hdr, hdr->length + page_size);
255 }
Simon Glass77595c62013-11-10 10:26:57 -0700256}
257
Matthias Weisserd99a6872011-11-29 12:16:40 +0100258void os_usleep(unsigned long usec)
259{
260 usleep(usec);
261}
262
Simon Glass2a54d152013-05-19 16:45:35 -0700263uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
Matthias Weisserd99a6872011-11-29 12:16:40 +0100264{
265#if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
266 struct timespec tp;
267 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
268 struct timeval tv;
269
270 gettimeofday(&tv, NULL);
271 tp.tv_sec = tv.tv_sec;
272 tp.tv_nsec = tv.tv_usec * 1000;
273 }
274 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
275#else
276 struct timeval tv;
277 gettimeofday(&tv, NULL);
278 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
279#endif
280}
Simon Glass70db4212012-02-15 15:51:16 -0800281
282static char *short_opts;
283static struct option *long_opts;
284
285int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
286{
Simon Glass7b3efc62013-12-03 16:43:23 -0700287 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass70db4212012-02-15 15:51:16 -0800288 size_t num_options = __u_boot_sandbox_option_count();
289 size_t i;
290
291 int hidden_short_opt;
292 size_t si;
293
294 int c;
295
296 if (short_opts || long_opts)
297 return 1;
298
299 state->argc = argc;
300 state->argv = argv;
301
302 /* dynamically construct the arguments to the system getopt_long */
Simon Glass0db1b432020-02-03 07:36:02 -0700303 short_opts = malloc(sizeof(*short_opts) * num_options * 2 + 1);
Simon Glass1c8c47e2020-02-03 07:36:04 -0700304 long_opts = malloc(sizeof(*long_opts) * (num_options + 1));
Simon Glass70db4212012-02-15 15:51:16 -0800305 if (!short_opts || !long_opts)
306 return 1;
307
308 /*
309 * getopt_long requires "val" to be unique (since that is what the
310 * func returns), so generate unique values automatically for flags
311 * that don't have a short option. pick 0x100 as that is above the
312 * single byte range (where ASCII/ISO-XXXX-X charsets live).
313 */
314 hidden_short_opt = 0x100;
315 si = 0;
316 for (i = 0; i < num_options; ++i) {
317 long_opts[i].name = sb_opt[i]->flag;
318 long_opts[i].has_arg = sb_opt[i]->has_arg ?
319 required_argument : no_argument;
320 long_opts[i].flag = NULL;
321
322 if (sb_opt[i]->flag_short) {
323 short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
324 if (long_opts[i].has_arg == required_argument)
325 short_opts[si++] = ':';
326 } else
327 long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
328 }
329 short_opts[si] = '\0';
330
331 /* we need to handle output ourselves since u-boot provides printf */
332 opterr = 0;
333
Simon Glass1c8c47e2020-02-03 07:36:04 -0700334 memset(&long_opts[num_options], '\0', sizeof(*long_opts));
Simon Glass70db4212012-02-15 15:51:16 -0800335 /*
336 * walk all of the options the user gave us on the command line,
337 * figure out what u-boot option structure they belong to (via
338 * the unique short val key), and call the appropriate callback.
339 */
340 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
341 for (i = 0; i < num_options; ++i) {
342 if (sb_opt[i]->flag_short == c) {
343 if (sb_opt[i]->callback(state, optarg)) {
344 state->parse_err = sb_opt[i]->flag;
345 return 0;
346 }
347 break;
348 }
349 }
350 if (i == num_options) {
351 /*
352 * store the faulting flag for later display. we have to
353 * store the flag itself as the getopt parsing itself is
354 * tricky: need to handle the following flags (assume all
355 * of the below are unknown):
356 * -a optopt='a' optind=<next>
357 * -abbbb optopt='a' optind=<this>
358 * -aaaaa optopt='a' optind=<this>
359 * --a optopt=0 optind=<this>
360 * as you can see, it is impossible to determine the exact
361 * faulting flag without doing the parsing ourselves, so
362 * we just report the specific flag that failed.
363 */
364 if (optopt) {
365 static char parse_err[3] = { '-', 0, '\0', };
366 parse_err[1] = optopt;
367 state->parse_err = parse_err;
368 } else
369 state->parse_err = argv[optind - 1];
370 break;
371 }
372 }
373
374 return 0;
375}
Simon Glass62584db2012-12-26 09:53:34 +0000376
377void os_dirent_free(struct os_dirent_node *node)
378{
379 struct os_dirent_node *next;
380
381 while (node) {
382 next = node->next;
Simon Glass0db1b432020-02-03 07:36:02 -0700383 free(node);
Simon Glass62584db2012-12-26 09:53:34 +0000384 node = next;
385 }
386}
387
388int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
389{
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200390 struct dirent *entry;
Simon Glass62584db2012-12-26 09:53:34 +0000391 struct os_dirent_node *head, *node, *next;
392 struct stat buf;
393 DIR *dir;
394 int ret;
395 char *fname;
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200396 char *old_fname;
Simon Glass62584db2012-12-26 09:53:34 +0000397 int len;
Stefan Brünsf189899c2016-10-01 20:41:40 +0200398 int dirlen;
Simon Glass62584db2012-12-26 09:53:34 +0000399
400 *headp = NULL;
401 dir = opendir(dirname);
402 if (!dir)
403 return -1;
404
Stefan Brünsf189899c2016-10-01 20:41:40 +0200405 /* Create a buffer upfront, with typically sufficient size */
406 dirlen = strlen(dirname) + 2;
407 len = dirlen + 256;
Simon Glass0db1b432020-02-03 07:36:02 -0700408 fname = malloc(len);
Simon Glass62584db2012-12-26 09:53:34 +0000409 if (!fname) {
410 ret = -ENOMEM;
411 goto done;
412 }
413
414 for (node = head = NULL;; node = next) {
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200415 errno = 0;
416 entry = readdir(dir);
417 if (!entry) {
418 ret = errno;
Simon Glass62584db2012-12-26 09:53:34 +0000419 break;
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200420 }
Simon Glass0db1b432020-02-03 07:36:02 -0700421 next = malloc(sizeof(*node) + strlen(entry->d_name) + 1);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200422 if (!next) {
Simon Glass62584db2012-12-26 09:53:34 +0000423 os_dirent_free(head);
424 ret = -ENOMEM;
425 goto done;
426 }
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200427 if (dirlen + strlen(entry->d_name) > len) {
428 len = dirlen + strlen(entry->d_name);
429 old_fname = fname;
Simon Glass0db1b432020-02-03 07:36:02 -0700430 fname = realloc(fname, len);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200431 if (!fname) {
Simon Glass0db1b432020-02-03 07:36:02 -0700432 free(old_fname);
433 free(next);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200434 os_dirent_free(head);
435 ret = -ENOMEM;
436 goto done;
437 }
438 }
Stephen Warren9c38c072014-06-11 10:26:23 -0600439 next->next = NULL;
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200440 strcpy(next->name, entry->d_name);
441 switch (entry->d_type) {
Simon Glass62584db2012-12-26 09:53:34 +0000442 case DT_REG:
443 next->type = OS_FILET_REG;
444 break;
445 case DT_DIR:
446 next->type = OS_FILET_DIR;
447 break;
448 case DT_LNK:
449 next->type = OS_FILET_LNK;
450 break;
Stefan Brüns2f159402016-10-04 21:46:35 +0200451 default:
452 next->type = OS_FILET_UNKNOWN;
Simon Glass62584db2012-12-26 09:53:34 +0000453 }
454 next->size = 0;
455 snprintf(fname, len, "%s/%s", dirname, next->name);
456 if (!stat(fname, &buf))
457 next->size = buf.st_size;
458 if (node)
459 node->next = next;
Stefan Brünsce2ec192016-10-01 20:41:39 +0200460 else
461 head = next;
Simon Glass62584db2012-12-26 09:53:34 +0000462 }
463 *headp = head;
464
465done:
466 closedir(dir);
Simon Glass0db1b432020-02-03 07:36:02 -0700467 free(fname);
Simon Glass62584db2012-12-26 09:53:34 +0000468 return ret;
469}
470
471const char *os_dirent_typename[OS_FILET_COUNT] = {
472 " ",
473 "SYM",
474 "DIR",
475 "???",
476};
477
478const char *os_dirent_get_typename(enum os_dirent_t type)
479{
Tom Rinie2bc87d2017-05-13 20:11:30 -0400480 if (type >= OS_FILET_REG && type < OS_FILET_COUNT)
Simon Glass62584db2012-12-26 09:53:34 +0000481 return os_dirent_typename[type];
482
483 return os_dirent_typename[OS_FILET_UNKNOWN];
484}
485
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800486int os_get_filesize(const char *fname, loff_t *size)
Simon Glass62584db2012-12-26 09:53:34 +0000487{
488 struct stat buf;
489 int ret;
490
491 ret = stat(fname, &buf);
492 if (ret)
493 return ret;
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800494 *size = buf.st_size;
495 return 0;
Simon Glass62584db2012-12-26 09:53:34 +0000496}
Simon Glass91b136c2013-11-10 10:27:01 -0700497
Simon Glass0b189b62017-12-04 13:48:17 -0700498void os_putc(int ch)
499{
500 putchar(ch);
501}
502
503void os_puts(const char *str)
504{
505 while (*str)
506 os_putc(*str++);
507}
508
Simon Glass5c2859c2013-11-10 10:27:03 -0700509int os_write_ram_buf(const char *fname)
510{
511 struct sandbox_state *state = state_get_current();
512 int fd, ret;
513
514 fd = open(fname, O_CREAT | O_WRONLY, 0777);
515 if (fd < 0)
516 return -ENOENT;
517 ret = write(fd, state->ram_buf, state->ram_size);
518 close(fd);
519 if (ret != state->ram_size)
520 return -EIO;
521
522 return 0;
523}
524
525int os_read_ram_buf(const char *fname)
526{
527 struct sandbox_state *state = state_get_current();
528 int fd, ret;
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800529 loff_t size;
Simon Glass5c2859c2013-11-10 10:27:03 -0700530
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800531 ret = os_get_filesize(fname, &size);
532 if (ret < 0)
533 return ret;
Simon Glass5c2859c2013-11-10 10:27:03 -0700534 if (size != state->ram_size)
535 return -ENOSPC;
536 fd = open(fname, O_RDONLY);
537 if (fd < 0)
538 return -ENOENT;
539
540 ret = read(fd, state->ram_buf, state->ram_size);
541 close(fd);
542 if (ret != state->ram_size)
543 return -EIO;
544
545 return 0;
546}
Simon Glass47f5fcf2014-02-27 13:26:15 -0700547
548static int make_exec(char *fname, const void *data, int size)
549{
550 int fd;
551
552 strcpy(fname, "/tmp/u-boot.jump.XXXXXX");
553 fd = mkstemp(fname);
554 if (fd < 0)
555 return -ENOENT;
556 if (write(fd, data, size) < 0)
557 return -EIO;
558 close(fd);
559 if (chmod(fname, 0777))
560 return -ENOEXEC;
561
562 return 0;
563}
564
Simon Glass7b5ea142018-11-15 18:44:05 -0700565/**
566 * add_args() - Allocate a new argv with the given args
567 *
568 * This is used to create a new argv array with all the old arguments and some
569 * new ones that are passed in
570 *
571 * @argvp: Returns newly allocated args list
572 * @add_args: Arguments to add, each a string
573 * @count: Number of arguments in @add_args
574 * @return 0 if OK, -ENOMEM if out of memory
575 */
576static int add_args(char ***argvp, char *add_args[], int count)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700577{
Simon Glass65f3b1f2018-11-15 18:44:07 -0700578 char **argv, **ap;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700579 int argc;
580
Simon Glass65f3b1f2018-11-15 18:44:07 -0700581 for (argc = 0; (*argvp)[argc]; argc++)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700582 ;
583
Simon Glass0db1b432020-02-03 07:36:02 -0700584 argv = malloc((argc + count + 1) * sizeof(char *));
Simon Glass47f5fcf2014-02-27 13:26:15 -0700585 if (!argv) {
586 printf("Out of memory for %d argv\n", count);
587 return -ENOMEM;
588 }
Simon Glass65f3b1f2018-11-15 18:44:07 -0700589 for (ap = *argvp, argc = 0; *ap; ap++) {
590 char *arg = *ap;
591
592 /* Drop args that we don't want to propagate */
593 if (*arg == '-' && strlen(arg) == 2) {
594 switch (arg[1]) {
595 case 'j':
596 case 'm':
597 ap++;
598 continue;
599 }
600 } else if (!strcmp(arg, "--rm_memory")) {
601 ap++;
602 continue;
603 }
604 argv[argc++] = arg;
605 }
606
Simon Glass47f5fcf2014-02-27 13:26:15 -0700607 memcpy(argv + argc, add_args, count * sizeof(char *));
608 argv[argc + count] = NULL;
609
610 *argvp = argv;
611 return 0;
612}
613
Simon Glass7b5ea142018-11-15 18:44:05 -0700614/**
615 * os_jump_to_file() - Jump to a new program
616 *
617 * This saves the memory buffer, sets up arguments to the new process, then
618 * execs it.
619 *
620 * @fname: Filename to exec
621 * @return does not return on success, any return value is an error
622 */
623static int os_jump_to_file(const char *fname)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700624{
625 struct sandbox_state *state = state_get_current();
Simon Glass7b5ea142018-11-15 18:44:05 -0700626 char mem_fname[30];
Simon Glass47f5fcf2014-02-27 13:26:15 -0700627 int fd, err;
Simon Glass7b5ea142018-11-15 18:44:05 -0700628 char *extra_args[5];
Simon Glass47f5fcf2014-02-27 13:26:15 -0700629 char **argv = state->argv;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700630 int argc;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700631#ifdef DEBUG
Simon Glass7b5ea142018-11-15 18:44:05 -0700632 int i;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700633#endif
634
Simon Glass47f5fcf2014-02-27 13:26:15 -0700635 strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
636 fd = mkstemp(mem_fname);
637 if (fd < 0)
638 return -ENOENT;
639 close(fd);
640 err = os_write_ram_buf(mem_fname);
641 if (err)
642 return err;
643
644 os_fd_restore();
645
646 extra_args[0] = "-j";
Simon Glass7b5ea142018-11-15 18:44:05 -0700647 extra_args[1] = (char *)fname;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700648 extra_args[2] = "-m";
649 extra_args[3] = mem_fname;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700650 argc = 4;
651 if (state->ram_buf_rm)
652 extra_args[argc++] = "--rm_memory";
653 err = add_args(&argv, extra_args, argc);
Simon Glass47f5fcf2014-02-27 13:26:15 -0700654 if (err)
655 return err;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700656 argv[0] = (char *)fname;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700657
658#ifdef DEBUG
659 for (i = 0; argv[i]; i++)
660 printf("%d %s\n", i, argv[i]);
661#endif
662
663 if (state_uninit())
664 os_exit(2);
665
666 err = execv(fname, argv);
Simon Glass0db1b432020-02-03 07:36:02 -0700667 free(argv);
Simon Glass27028f12018-11-15 18:44:08 -0700668 if (err) {
669 perror("Unable to run image");
Simon Glass6b5e4202018-11-23 21:29:24 -0700670 printf("Image filename '%s'\n", fname);
Simon Glass47f5fcf2014-02-27 13:26:15 -0700671 return err;
Simon Glass27028f12018-11-15 18:44:08 -0700672 }
Simon Glass47f5fcf2014-02-27 13:26:15 -0700673
674 return unlink(fname);
675}
Simon Glass94eefde2015-04-20 12:37:22 -0600676
Simon Glass7b5ea142018-11-15 18:44:05 -0700677int os_jump_to_image(const void *dest, int size)
678{
679 char fname[30];
680 int err;
681
682 err = make_exec(fname, dest, size);
683 if (err)
684 return err;
685
686 return os_jump_to_file(fname);
687}
688
Simon Glassd4e33f52016-07-04 11:57:45 -0600689int os_find_u_boot(char *fname, int maxlen)
690{
691 struct sandbox_state *state = state_get_current();
692 const char *progname = state->argv[0];
693 int len = strlen(progname);
Simon Glass69bc15d2018-10-01 11:55:10 -0600694 const char *suffix;
Simon Glassd4e33f52016-07-04 11:57:45 -0600695 char *p;
696 int fd;
697
698 if (len >= maxlen || len < 4)
699 return -ENOSPC;
700
Simon Glassd4e33f52016-07-04 11:57:45 -0600701 strcpy(fname, progname);
Simon Glass69bc15d2018-10-01 11:55:10 -0600702 suffix = fname + len - 4;
703
704 /* If we are TPL, boot to SPL */
705 if (!strcmp(suffix, "-tpl")) {
706 fname[len - 3] = 's';
707 fd = os_open(fname, O_RDONLY);
708 if (fd >= 0) {
709 close(fd);
710 return 0;
711 }
712
713 /* Look for 'u-boot-tpl' in the tpl/ directory */
714 p = strstr(fname, "/tpl/");
715 if (p) {
716 p[1] = 's';
717 fd = os_open(fname, O_RDONLY);
718 if (fd >= 0) {
719 close(fd);
720 return 0;
721 }
722 }
723 return -ENOENT;
724 }
725
726 /* Look for 'u-boot' in the same directory as 'u-boot-spl' */
727 if (!strcmp(suffix, "-spl")) {
Simon Glassd4e33f52016-07-04 11:57:45 -0600728 fname[len - 4] = '\0';
729 fd = os_open(fname, O_RDONLY);
730 if (fd >= 0) {
731 close(fd);
732 return 0;
733 }
734 }
735
736 /* Look for 'u-boot' in the parent directory of spl/ */
Simon Glassb847c142018-11-13 15:55:20 -0700737 p = strstr(fname, "spl/");
Simon Glassd4e33f52016-07-04 11:57:45 -0600738 if (p) {
Simon Glassb847c142018-11-13 15:55:20 -0700739 /* Remove the "spl" characters */
740 memmove(p, p + 4, strlen(p + 4) + 1);
Simon Glassd4e33f52016-07-04 11:57:45 -0600741 fd = os_open(fname, O_RDONLY);
742 if (fd >= 0) {
743 close(fd);
744 return 0;
745 }
746 }
747
748 return -ENOENT;
749}
750
751int os_spl_to_uboot(const char *fname)
752{
Simon Glass27028f12018-11-15 18:44:08 -0700753 return os_jump_to_file(fname);
Simon Glassd4e33f52016-07-04 11:57:45 -0600754}
755
Simon Glass94eefde2015-04-20 12:37:22 -0600756void os_localtime(struct rtc_time *rt)
757{
758 time_t t = time(NULL);
759 struct tm *tm;
760
761 tm = localtime(&t);
762 rt->tm_sec = tm->tm_sec;
763 rt->tm_min = tm->tm_min;
764 rt->tm_hour = tm->tm_hour;
765 rt->tm_mday = tm->tm_mday;
766 rt->tm_mon = tm->tm_mon + 1;
767 rt->tm_year = tm->tm_year + 1900;
768 rt->tm_wday = tm->tm_wday;
769 rt->tm_yday = tm->tm_yday;
770 rt->tm_isdst = tm->tm_isdst;
771}
Simon Glass30eef212018-05-16 09:42:22 -0600772
Simon Glassfe938fb2018-09-15 00:50:55 -0600773void os_abort(void)
774{
775 abort();
776}
Simon Glass9f8037e2018-10-01 21:12:32 -0600777
778int os_mprotect_allow(void *start, size_t len)
779{
780 int page_size = getpagesize();
781
782 /* Move start to the start of a page, len to the end */
783 start = (void *)(((ulong)start) & ~(page_size - 1));
784 len = (len + page_size * 2) & ~(page_size - 1);
785
786 return mprotect(start, len, PROT_READ | PROT_WRITE);
787}
Simon Glass001d1882019-04-08 13:20:41 -0600788
789void *os_find_text_base(void)
790{
791 char line[500];
792 void *base = NULL;
793 int len;
794 int fd;
795
796 /*
797 * This code assumes that the first line of /proc/self/maps holds
798 * information about the text, for example:
799 *
800 * 5622d9907000-5622d9a55000 r-xp 00000000 08:01 15067168 u-boot
801 *
802 * The first hex value is assumed to be the address.
803 *
804 * This is tested in Linux 4.15.
805 */
806 fd = open("/proc/self/maps", O_RDONLY);
807 if (fd == -1)
808 return NULL;
809 len = read(fd, line, sizeof(line));
810 if (len > 0) {
811 char *end = memchr(line, '-', len);
812
813 if (end) {
Heinrich Schuchardt4ecb5062019-10-26 23:17:44 +0200814 uintptr_t addr;
Simon Glass001d1882019-04-08 13:20:41 -0600815
816 *end = '\0';
Heinrich Schuchardt4ecb5062019-10-26 23:17:44 +0200817 if (sscanf(line, "%zx", &addr) == 1)
Simon Glass001d1882019-04-08 13:20:41 -0600818 base = (void *)addr;
819 }
820 }
821 close(fd);
822
823 return base;
824}