blob: f7c73e3a0b15b785022365fe99196f51bae16650 [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>
Simon Glass62584db2012-12-26 09:53:34 +000011#include <stdio.h>
Simon Glass2a54d152013-05-19 16:45:35 -070012#include <stdint.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000013#include <stdlib.h>
Simon Glass62584db2012-12-26 09:53:34 +000014#include <string.h>
Mike Frysingerab06a752011-10-26 00:21:29 +000015#include <termios.h>
Matthias Weisserd99a6872011-11-29 12:16:40 +010016#include <time.h>
Simon Glasse1012472012-01-10 15:54:05 -080017#include <unistd.h>
Matthias Weisser21899b12011-11-05 11:40:34 +010018#include <sys/mman.h>
Simon Glasse1012472012-01-10 15:54:05 -080019#include <sys/stat.h>
Simon Glass3bdf56b2012-01-10 15:54:06 -080020#include <sys/time.h>
Simon Glasse1012472012-01-10 15:54:05 -080021#include <sys/types.h>
Matthias Weisserd99a6872011-11-29 12:16:40 +010022#include <linux/types.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000023
Simon Glass70db4212012-02-15 15:51:16 -080024#include <asm/getopt.h>
25#include <asm/sections.h>
26#include <asm/state.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000027#include <os.h>
Simon Glass94eefde2015-04-20 12:37:22 -060028#include <rtc_def.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000029
30/* Operating System Interface */
31
Simon Glass77595c62013-11-10 10:26:57 -070032struct os_mem_hdr {
33 size_t length; /* number of bytes in the block */
34};
35
Simon Glass7a9219c2011-10-03 19:26:44 +000036ssize_t os_read(int fd, void *buf, size_t count)
37{
38 return read(fd, buf, count);
39}
40
41ssize_t os_write(int fd, const void *buf, size_t count)
42{
43 return write(fd, buf, count);
44}
45
Mike Frysingere2dcefc2011-10-25 13:02:58 +020046off_t os_lseek(int fd, off_t offset, int whence)
47{
48 if (whence == OS_SEEK_SET)
49 whence = SEEK_SET;
50 else if (whence == OS_SEEK_CUR)
51 whence = SEEK_CUR;
52 else if (whence == OS_SEEK_END)
53 whence = SEEK_END;
54 else
55 os_exit(1);
56 return lseek(fd, offset, whence);
57}
58
Simon Glassd9165152012-02-20 23:56:58 -050059int os_open(const char *pathname, int os_flags)
Simon Glass7a9219c2011-10-03 19:26:44 +000060{
Simon Glassd9165152012-02-20 23:56:58 -050061 int flags;
62
63 switch (os_flags & OS_O_MASK) {
64 case OS_O_RDONLY:
65 default:
66 flags = O_RDONLY;
67 break;
68
69 case OS_O_WRONLY:
70 flags = O_WRONLY;
71 break;
72
73 case OS_O_RDWR:
74 flags = O_RDWR;
75 break;
76 }
77
78 if (os_flags & OS_O_CREAT)
79 flags |= O_CREAT;
Simon Glass50b288a2018-10-01 11:55:07 -060080 if (os_flags & OS_O_TRUNC)
81 flags |= O_TRUNC;
Simon Glassd9165152012-02-20 23:56:58 -050082
83 return open(pathname, flags, 0777);
Simon Glass7a9219c2011-10-03 19:26:44 +000084}
85
86int os_close(int fd)
87{
88 return close(fd);
89}
90
Stephen Warrencfd13e82014-03-01 22:18:00 -070091int os_unlink(const char *pathname)
92{
93 return unlink(pathname);
94}
95
Simon Glass7a9219c2011-10-03 19:26:44 +000096void os_exit(int exit_code)
97{
98 exit(exit_code);
99}
Mike Frysingerab06a752011-10-26 00:21:29 +0000100
Simon Glass566bf3a2018-11-06 15:21:25 -0700101int os_write_file(const char *fname, const void *buf, int size)
Simon Glass056a5ce2018-10-01 11:55:08 -0600102{
Simon Glass056a5ce2018-10-01 11:55:08 -0600103 int fd;
104
105 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC);
106 if (fd < 0) {
107 printf("Cannot open file '%s'\n", fname);
108 return -EIO;
109 }
110 if (os_write(fd, buf, size) != size) {
111 printf("Cannot write to file '%s'\n", fname);
Simon Glass566bf3a2018-11-06 15:21:25 -0700112 os_close(fd);
Simon Glass056a5ce2018-10-01 11:55:08 -0600113 return -EIO;
114 }
115 os_close(fd);
Simon Glass056a5ce2018-10-01 11:55:08 -0600116
117 return 0;
118}
119
Simon Glass566bf3a2018-11-06 15:21:25 -0700120int os_read_file(const char *fname, void **bufp, int *sizep)
121{
122 off_t size;
123 int ret = -EIO;
124 int fd;
125
126 fd = os_open(fname, OS_O_RDONLY);
127 if (fd < 0) {
128 printf("Cannot open file '%s'\n", fname);
129 goto err;
130 }
131 size = os_lseek(fd, 0, OS_SEEK_END);
132 if (size < 0) {
133 printf("Cannot seek to end of file '%s'\n", fname);
134 goto err;
135 }
136 if (os_lseek(fd, 0, OS_SEEK_SET) < 0) {
137 printf("Cannot seek to start of file '%s'\n", fname);
138 goto err;
139 }
Simon Glass0db1b432020-02-03 07:36:02 -0700140 *bufp = malloc(size);
Simon Glass566bf3a2018-11-06 15:21:25 -0700141 if (!*bufp) {
142 printf("Not enough memory to read file '%s'\n", fname);
143 ret = -ENOMEM;
144 goto err;
145 }
146 if (os_read(fd, *bufp, size) != size) {
147 printf("Cannot read from file '%s'\n", fname);
148 goto err;
149 }
150 os_close(fd);
151 *sizep = size;
152
153 return 0;
154err:
155 os_close(fd);
156 return ret;
157}
158
Mike Frysingerab06a752011-10-26 00:21:29 +0000159/* Restore tty state when we exit */
160static struct termios orig_term;
Simon Glassffb87902014-02-27 13:26:22 -0700161static bool term_setup;
Simon Glass4af3e9a2018-10-01 11:55:20 -0600162static bool term_nonblock;
Mike Frysingerab06a752011-10-26 00:21:29 +0000163
Simon Glass8939df02015-05-10 21:07:27 -0600164void os_fd_restore(void)
Mike Frysingerab06a752011-10-26 00:21:29 +0000165{
Simon Glass8939df02015-05-10 21:07:27 -0600166 if (term_setup) {
Simon Glass4af3e9a2018-10-01 11:55:20 -0600167 int flags;
168
Simon Glassffb87902014-02-27 13:26:22 -0700169 tcsetattr(0, TCSANOW, &orig_term);
Simon Glass4af3e9a2018-10-01 11:55:20 -0600170 if (term_nonblock) {
171 flags = fcntl(0, F_GETFL, 0);
172 fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
173 }
Simon Glass8939df02015-05-10 21:07:27 -0600174 term_setup = false;
175 }
Mike Frysingerab06a752011-10-26 00:21:29 +0000176}
177
178/* Put tty into raw mode so <tab> and <ctrl+c> work */
Simon Glassffb87902014-02-27 13:26:22 -0700179void os_tty_raw(int fd, bool allow_sigs)
Mike Frysingerab06a752011-10-26 00:21:29 +0000180{
Mike Frysingerab06a752011-10-26 00:21:29 +0000181 struct termios term;
Simon Glass4af3e9a2018-10-01 11:55:20 -0600182 int flags;
Mike Frysingerab06a752011-10-26 00:21:29 +0000183
Simon Glassffb87902014-02-27 13:26:22 -0700184 if (term_setup)
Mike Frysingerab06a752011-10-26 00:21:29 +0000185 return;
Mike Frysingerab06a752011-10-26 00:21:29 +0000186
187 /* If not a tty, don't complain */
188 if (tcgetattr(fd, &orig_term))
189 return;
190
191 term = orig_term;
192 term.c_iflag = IGNBRK | IGNPAR;
193 term.c_oflag = OPOST | ONLCR;
194 term.c_cflag = CS8 | CREAD | CLOCAL;
Simon Glassffb87902014-02-27 13:26:22 -0700195 term.c_lflag = allow_sigs ? ISIG : 0;
Mike Frysingerab06a752011-10-26 00:21:29 +0000196 if (tcsetattr(fd, TCSANOW, &term))
197 return;
198
Simon Glass4af3e9a2018-10-01 11:55:20 -0600199 flags = fcntl(fd, F_GETFL, 0);
200 if (!(flags & O_NONBLOCK)) {
201 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
202 return;
203 term_nonblock = true;
204 }
205
Simon Glass8939df02015-05-10 21:07:27 -0600206 term_setup = true;
Mike Frysingerab06a752011-10-26 00:21:29 +0000207 atexit(os_fd_restore);
208}
Matthias Weisser21899b12011-11-05 11:40:34 +0100209
210void *os_malloc(size_t length)
211{
Simon Glass61318502018-09-15 00:50:54 -0600212 int page_size = getpagesize();
Simon Glass4a6409b2019-04-08 13:20:42 -0600213 struct os_mem_hdr *hdr;
Simon Glass77595c62013-11-10 10:26:57 -0700214
Simon Glassbd8b7452018-06-17 08:57:43 -0600215 /*
216 * Use an address that is hopefully available to us so that pointers
217 * to this memory are fairly obvious. If we end up with a different
218 * address, that's fine too.
219 */
220 hdr = mmap((void *)0x10000000, length + page_size,
Alexander Graf6e6e4b22018-06-22 14:44:13 +0200221 PROT_READ | PROT_WRITE | PROT_EXEC,
222 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Simon Glass77595c62013-11-10 10:26:57 -0700223 if (hdr == MAP_FAILED)
224 return NULL;
225 hdr->length = length;
226
Simon Glass61318502018-09-15 00:50:54 -0600227 return (void *)hdr + page_size;
Simon Glass77595c62013-11-10 10:26:57 -0700228}
229
Masahiro Yamada347d06d2014-01-15 13:06:41 +0900230void os_free(void *ptr)
Simon Glass77595c62013-11-10 10:26:57 -0700231{
Simon Glass4a6409b2019-04-08 13:20:42 -0600232 int page_size = getpagesize();
233 struct os_mem_hdr *hdr;
Simon Glass77595c62013-11-10 10:26:57 -0700234
Simon Glass4a6409b2019-04-08 13:20:42 -0600235 if (ptr) {
236 hdr = ptr - page_size;
237 munmap(hdr, hdr->length + page_size);
238 }
Simon Glass77595c62013-11-10 10:26:57 -0700239}
240
Matthias Weisserd99a6872011-11-29 12:16:40 +0100241void os_usleep(unsigned long usec)
242{
243 usleep(usec);
244}
245
Simon Glass2a54d152013-05-19 16:45:35 -0700246uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
Matthias Weisserd99a6872011-11-29 12:16:40 +0100247{
248#if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
249 struct timespec tp;
250 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
251 struct timeval tv;
252
253 gettimeofday(&tv, NULL);
254 tp.tv_sec = tv.tv_sec;
255 tp.tv_nsec = tv.tv_usec * 1000;
256 }
257 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
258#else
259 struct timeval tv;
260 gettimeofday(&tv, NULL);
261 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
262#endif
263}
Simon Glass70db4212012-02-15 15:51:16 -0800264
265static char *short_opts;
266static struct option *long_opts;
267
268int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
269{
Simon Glass7b3efc62013-12-03 16:43:23 -0700270 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass70db4212012-02-15 15:51:16 -0800271 size_t num_options = __u_boot_sandbox_option_count();
272 size_t i;
273
274 int hidden_short_opt;
275 size_t si;
276
277 int c;
278
279 if (short_opts || long_opts)
280 return 1;
281
282 state->argc = argc;
283 state->argv = argv;
284
285 /* dynamically construct the arguments to the system getopt_long */
Simon Glass0db1b432020-02-03 07:36:02 -0700286 short_opts = malloc(sizeof(*short_opts) * num_options * 2 + 1);
Simon Glass1c8c47e2020-02-03 07:36:04 -0700287 long_opts = malloc(sizeof(*long_opts) * (num_options + 1));
Simon Glass70db4212012-02-15 15:51:16 -0800288 if (!short_opts || !long_opts)
289 return 1;
290
291 /*
292 * getopt_long requires "val" to be unique (since that is what the
293 * func returns), so generate unique values automatically for flags
294 * that don't have a short option. pick 0x100 as that is above the
295 * single byte range (where ASCII/ISO-XXXX-X charsets live).
296 */
297 hidden_short_opt = 0x100;
298 si = 0;
299 for (i = 0; i < num_options; ++i) {
300 long_opts[i].name = sb_opt[i]->flag;
301 long_opts[i].has_arg = sb_opt[i]->has_arg ?
302 required_argument : no_argument;
303 long_opts[i].flag = NULL;
304
305 if (sb_opt[i]->flag_short) {
306 short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
307 if (long_opts[i].has_arg == required_argument)
308 short_opts[si++] = ':';
309 } else
310 long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
311 }
312 short_opts[si] = '\0';
313
314 /* we need to handle output ourselves since u-boot provides printf */
315 opterr = 0;
316
Simon Glass1c8c47e2020-02-03 07:36:04 -0700317 memset(&long_opts[num_options], '\0', sizeof(*long_opts));
Simon Glass70db4212012-02-15 15:51:16 -0800318 /*
319 * walk all of the options the user gave us on the command line,
320 * figure out what u-boot option structure they belong to (via
321 * the unique short val key), and call the appropriate callback.
322 */
323 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
324 for (i = 0; i < num_options; ++i) {
325 if (sb_opt[i]->flag_short == c) {
326 if (sb_opt[i]->callback(state, optarg)) {
327 state->parse_err = sb_opt[i]->flag;
328 return 0;
329 }
330 break;
331 }
332 }
333 if (i == num_options) {
334 /*
335 * store the faulting flag for later display. we have to
336 * store the flag itself as the getopt parsing itself is
337 * tricky: need to handle the following flags (assume all
338 * of the below are unknown):
339 * -a optopt='a' optind=<next>
340 * -abbbb optopt='a' optind=<this>
341 * -aaaaa optopt='a' optind=<this>
342 * --a optopt=0 optind=<this>
343 * as you can see, it is impossible to determine the exact
344 * faulting flag without doing the parsing ourselves, so
345 * we just report the specific flag that failed.
346 */
347 if (optopt) {
348 static char parse_err[3] = { '-', 0, '\0', };
349 parse_err[1] = optopt;
350 state->parse_err = parse_err;
351 } else
352 state->parse_err = argv[optind - 1];
353 break;
354 }
355 }
356
357 return 0;
358}
Simon Glass62584db2012-12-26 09:53:34 +0000359
360void os_dirent_free(struct os_dirent_node *node)
361{
362 struct os_dirent_node *next;
363
364 while (node) {
365 next = node->next;
Simon Glass0db1b432020-02-03 07:36:02 -0700366 free(node);
Simon Glass62584db2012-12-26 09:53:34 +0000367 node = next;
368 }
369}
370
371int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
372{
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200373 struct dirent *entry;
Simon Glass62584db2012-12-26 09:53:34 +0000374 struct os_dirent_node *head, *node, *next;
375 struct stat buf;
376 DIR *dir;
377 int ret;
378 char *fname;
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200379 char *old_fname;
Simon Glass62584db2012-12-26 09:53:34 +0000380 int len;
Stefan Brünsf189899c2016-10-01 20:41:40 +0200381 int dirlen;
Simon Glass62584db2012-12-26 09:53:34 +0000382
383 *headp = NULL;
384 dir = opendir(dirname);
385 if (!dir)
386 return -1;
387
Stefan Brünsf189899c2016-10-01 20:41:40 +0200388 /* Create a buffer upfront, with typically sufficient size */
389 dirlen = strlen(dirname) + 2;
390 len = dirlen + 256;
Simon Glass0db1b432020-02-03 07:36:02 -0700391 fname = malloc(len);
Simon Glass62584db2012-12-26 09:53:34 +0000392 if (!fname) {
393 ret = -ENOMEM;
394 goto done;
395 }
396
397 for (node = head = NULL;; node = next) {
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200398 errno = 0;
399 entry = readdir(dir);
400 if (!entry) {
401 ret = errno;
Simon Glass62584db2012-12-26 09:53:34 +0000402 break;
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200403 }
Simon Glass0db1b432020-02-03 07:36:02 -0700404 next = malloc(sizeof(*node) + strlen(entry->d_name) + 1);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200405 if (!next) {
Simon Glass62584db2012-12-26 09:53:34 +0000406 os_dirent_free(head);
407 ret = -ENOMEM;
408 goto done;
409 }
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200410 if (dirlen + strlen(entry->d_name) > len) {
411 len = dirlen + strlen(entry->d_name);
412 old_fname = fname;
Simon Glass0db1b432020-02-03 07:36:02 -0700413 fname = realloc(fname, len);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200414 if (!fname) {
Simon Glass0db1b432020-02-03 07:36:02 -0700415 free(old_fname);
416 free(next);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200417 os_dirent_free(head);
418 ret = -ENOMEM;
419 goto done;
420 }
421 }
Stephen Warren9c38c072014-06-11 10:26:23 -0600422 next->next = NULL;
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200423 strcpy(next->name, entry->d_name);
424 switch (entry->d_type) {
Simon Glass62584db2012-12-26 09:53:34 +0000425 case DT_REG:
426 next->type = OS_FILET_REG;
427 break;
428 case DT_DIR:
429 next->type = OS_FILET_DIR;
430 break;
431 case DT_LNK:
432 next->type = OS_FILET_LNK;
433 break;
Stefan Brüns2f159402016-10-04 21:46:35 +0200434 default:
435 next->type = OS_FILET_UNKNOWN;
Simon Glass62584db2012-12-26 09:53:34 +0000436 }
437 next->size = 0;
438 snprintf(fname, len, "%s/%s", dirname, next->name);
439 if (!stat(fname, &buf))
440 next->size = buf.st_size;
441 if (node)
442 node->next = next;
Stefan Brünsce2ec192016-10-01 20:41:39 +0200443 else
444 head = next;
Simon Glass62584db2012-12-26 09:53:34 +0000445 }
446 *headp = head;
447
448done:
449 closedir(dir);
Simon Glass0db1b432020-02-03 07:36:02 -0700450 free(fname);
Simon Glass62584db2012-12-26 09:53:34 +0000451 return ret;
452}
453
454const char *os_dirent_typename[OS_FILET_COUNT] = {
455 " ",
456 "SYM",
457 "DIR",
458 "???",
459};
460
461const char *os_dirent_get_typename(enum os_dirent_t type)
462{
Tom Rinie2bc87d2017-05-13 20:11:30 -0400463 if (type >= OS_FILET_REG && type < OS_FILET_COUNT)
Simon Glass62584db2012-12-26 09:53:34 +0000464 return os_dirent_typename[type];
465
466 return os_dirent_typename[OS_FILET_UNKNOWN];
467}
468
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800469int os_get_filesize(const char *fname, loff_t *size)
Simon Glass62584db2012-12-26 09:53:34 +0000470{
471 struct stat buf;
472 int ret;
473
474 ret = stat(fname, &buf);
475 if (ret)
476 return ret;
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800477 *size = buf.st_size;
478 return 0;
Simon Glass62584db2012-12-26 09:53:34 +0000479}
Simon Glass91b136c2013-11-10 10:27:01 -0700480
Simon Glass0b189b62017-12-04 13:48:17 -0700481void os_putc(int ch)
482{
483 putchar(ch);
484}
485
486void os_puts(const char *str)
487{
488 while (*str)
489 os_putc(*str++);
490}
491
Simon Glass5c2859c2013-11-10 10:27:03 -0700492int os_write_ram_buf(const char *fname)
493{
494 struct sandbox_state *state = state_get_current();
495 int fd, ret;
496
497 fd = open(fname, O_CREAT | O_WRONLY, 0777);
498 if (fd < 0)
499 return -ENOENT;
500 ret = write(fd, state->ram_buf, state->ram_size);
501 close(fd);
502 if (ret != state->ram_size)
503 return -EIO;
504
505 return 0;
506}
507
508int os_read_ram_buf(const char *fname)
509{
510 struct sandbox_state *state = state_get_current();
511 int fd, ret;
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800512 loff_t size;
Simon Glass5c2859c2013-11-10 10:27:03 -0700513
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800514 ret = os_get_filesize(fname, &size);
515 if (ret < 0)
516 return ret;
Simon Glass5c2859c2013-11-10 10:27:03 -0700517 if (size != state->ram_size)
518 return -ENOSPC;
519 fd = open(fname, O_RDONLY);
520 if (fd < 0)
521 return -ENOENT;
522
523 ret = read(fd, state->ram_buf, state->ram_size);
524 close(fd);
525 if (ret != state->ram_size)
526 return -EIO;
527
528 return 0;
529}
Simon Glass47f5fcf2014-02-27 13:26:15 -0700530
531static int make_exec(char *fname, const void *data, int size)
532{
533 int fd;
534
535 strcpy(fname, "/tmp/u-boot.jump.XXXXXX");
536 fd = mkstemp(fname);
537 if (fd < 0)
538 return -ENOENT;
539 if (write(fd, data, size) < 0)
540 return -EIO;
541 close(fd);
542 if (chmod(fname, 0777))
543 return -ENOEXEC;
544
545 return 0;
546}
547
Simon Glass7b5ea142018-11-15 18:44:05 -0700548/**
549 * add_args() - Allocate a new argv with the given args
550 *
551 * This is used to create a new argv array with all the old arguments and some
552 * new ones that are passed in
553 *
554 * @argvp: Returns newly allocated args list
555 * @add_args: Arguments to add, each a string
556 * @count: Number of arguments in @add_args
557 * @return 0 if OK, -ENOMEM if out of memory
558 */
559static int add_args(char ***argvp, char *add_args[], int count)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700560{
Simon Glass65f3b1f2018-11-15 18:44:07 -0700561 char **argv, **ap;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700562 int argc;
563
Simon Glass65f3b1f2018-11-15 18:44:07 -0700564 for (argc = 0; (*argvp)[argc]; argc++)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700565 ;
566
Simon Glass0db1b432020-02-03 07:36:02 -0700567 argv = malloc((argc + count + 1) * sizeof(char *));
Simon Glass47f5fcf2014-02-27 13:26:15 -0700568 if (!argv) {
569 printf("Out of memory for %d argv\n", count);
570 return -ENOMEM;
571 }
Simon Glass65f3b1f2018-11-15 18:44:07 -0700572 for (ap = *argvp, argc = 0; *ap; ap++) {
573 char *arg = *ap;
574
575 /* Drop args that we don't want to propagate */
576 if (*arg == '-' && strlen(arg) == 2) {
577 switch (arg[1]) {
578 case 'j':
579 case 'm':
580 ap++;
581 continue;
582 }
583 } else if (!strcmp(arg, "--rm_memory")) {
584 ap++;
585 continue;
586 }
587 argv[argc++] = arg;
588 }
589
Simon Glass47f5fcf2014-02-27 13:26:15 -0700590 memcpy(argv + argc, add_args, count * sizeof(char *));
591 argv[argc + count] = NULL;
592
593 *argvp = argv;
594 return 0;
595}
596
Simon Glass7b5ea142018-11-15 18:44:05 -0700597/**
598 * os_jump_to_file() - Jump to a new program
599 *
600 * This saves the memory buffer, sets up arguments to the new process, then
601 * execs it.
602 *
603 * @fname: Filename to exec
604 * @return does not return on success, any return value is an error
605 */
606static int os_jump_to_file(const char *fname)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700607{
608 struct sandbox_state *state = state_get_current();
Simon Glass7b5ea142018-11-15 18:44:05 -0700609 char mem_fname[30];
Simon Glass47f5fcf2014-02-27 13:26:15 -0700610 int fd, err;
Simon Glass7b5ea142018-11-15 18:44:05 -0700611 char *extra_args[5];
Simon Glass47f5fcf2014-02-27 13:26:15 -0700612 char **argv = state->argv;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700613 int argc;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700614#ifdef DEBUG
Simon Glass7b5ea142018-11-15 18:44:05 -0700615 int i;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700616#endif
617
Simon Glass47f5fcf2014-02-27 13:26:15 -0700618 strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
619 fd = mkstemp(mem_fname);
620 if (fd < 0)
621 return -ENOENT;
622 close(fd);
623 err = os_write_ram_buf(mem_fname);
624 if (err)
625 return err;
626
627 os_fd_restore();
628
629 extra_args[0] = "-j";
Simon Glass7b5ea142018-11-15 18:44:05 -0700630 extra_args[1] = (char *)fname;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700631 extra_args[2] = "-m";
632 extra_args[3] = mem_fname;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700633 argc = 4;
634 if (state->ram_buf_rm)
635 extra_args[argc++] = "--rm_memory";
636 err = add_args(&argv, extra_args, argc);
Simon Glass47f5fcf2014-02-27 13:26:15 -0700637 if (err)
638 return err;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700639 argv[0] = (char *)fname;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700640
641#ifdef DEBUG
642 for (i = 0; argv[i]; i++)
643 printf("%d %s\n", i, argv[i]);
644#endif
645
646 if (state_uninit())
647 os_exit(2);
648
649 err = execv(fname, argv);
Simon Glass0db1b432020-02-03 07:36:02 -0700650 free(argv);
Simon Glass27028f12018-11-15 18:44:08 -0700651 if (err) {
652 perror("Unable to run image");
Simon Glass6b5e4202018-11-23 21:29:24 -0700653 printf("Image filename '%s'\n", fname);
Simon Glass47f5fcf2014-02-27 13:26:15 -0700654 return err;
Simon Glass27028f12018-11-15 18:44:08 -0700655 }
Simon Glass47f5fcf2014-02-27 13:26:15 -0700656
657 return unlink(fname);
658}
Simon Glass94eefde2015-04-20 12:37:22 -0600659
Simon Glass7b5ea142018-11-15 18:44:05 -0700660int os_jump_to_image(const void *dest, int size)
661{
662 char fname[30];
663 int err;
664
665 err = make_exec(fname, dest, size);
666 if (err)
667 return err;
668
669 return os_jump_to_file(fname);
670}
671
Simon Glassd4e33f52016-07-04 11:57:45 -0600672int os_find_u_boot(char *fname, int maxlen)
673{
674 struct sandbox_state *state = state_get_current();
675 const char *progname = state->argv[0];
676 int len = strlen(progname);
Simon Glass69bc15d2018-10-01 11:55:10 -0600677 const char *suffix;
Simon Glassd4e33f52016-07-04 11:57:45 -0600678 char *p;
679 int fd;
680
681 if (len >= maxlen || len < 4)
682 return -ENOSPC;
683
Simon Glassd4e33f52016-07-04 11:57:45 -0600684 strcpy(fname, progname);
Simon Glass69bc15d2018-10-01 11:55:10 -0600685 suffix = fname + len - 4;
686
687 /* If we are TPL, boot to SPL */
688 if (!strcmp(suffix, "-tpl")) {
689 fname[len - 3] = 's';
690 fd = os_open(fname, O_RDONLY);
691 if (fd >= 0) {
692 close(fd);
693 return 0;
694 }
695
696 /* Look for 'u-boot-tpl' in the tpl/ directory */
697 p = strstr(fname, "/tpl/");
698 if (p) {
699 p[1] = 's';
700 fd = os_open(fname, O_RDONLY);
701 if (fd >= 0) {
702 close(fd);
703 return 0;
704 }
705 }
706 return -ENOENT;
707 }
708
709 /* Look for 'u-boot' in the same directory as 'u-boot-spl' */
710 if (!strcmp(suffix, "-spl")) {
Simon Glassd4e33f52016-07-04 11:57:45 -0600711 fname[len - 4] = '\0';
712 fd = os_open(fname, O_RDONLY);
713 if (fd >= 0) {
714 close(fd);
715 return 0;
716 }
717 }
718
719 /* Look for 'u-boot' in the parent directory of spl/ */
Simon Glassb847c142018-11-13 15:55:20 -0700720 p = strstr(fname, "spl/");
Simon Glassd4e33f52016-07-04 11:57:45 -0600721 if (p) {
Simon Glassb847c142018-11-13 15:55:20 -0700722 /* Remove the "spl" characters */
723 memmove(p, p + 4, strlen(p + 4) + 1);
Simon Glassd4e33f52016-07-04 11:57:45 -0600724 fd = os_open(fname, O_RDONLY);
725 if (fd >= 0) {
726 close(fd);
727 return 0;
728 }
729 }
730
731 return -ENOENT;
732}
733
734int os_spl_to_uboot(const char *fname)
735{
Simon Glass27028f12018-11-15 18:44:08 -0700736 return os_jump_to_file(fname);
Simon Glassd4e33f52016-07-04 11:57:45 -0600737}
738
Simon Glass94eefde2015-04-20 12:37:22 -0600739void os_localtime(struct rtc_time *rt)
740{
741 time_t t = time(NULL);
742 struct tm *tm;
743
744 tm = localtime(&t);
745 rt->tm_sec = tm->tm_sec;
746 rt->tm_min = tm->tm_min;
747 rt->tm_hour = tm->tm_hour;
748 rt->tm_mday = tm->tm_mday;
749 rt->tm_mon = tm->tm_mon + 1;
750 rt->tm_year = tm->tm_year + 1900;
751 rt->tm_wday = tm->tm_wday;
752 rt->tm_yday = tm->tm_yday;
753 rt->tm_isdst = tm->tm_isdst;
754}
Simon Glass30eef212018-05-16 09:42:22 -0600755
Simon Glassfe938fb2018-09-15 00:50:55 -0600756void os_abort(void)
757{
758 abort();
759}
Simon Glass9f8037e2018-10-01 21:12:32 -0600760
761int os_mprotect_allow(void *start, size_t len)
762{
763 int page_size = getpagesize();
764
765 /* Move start to the start of a page, len to the end */
766 start = (void *)(((ulong)start) & ~(page_size - 1));
767 len = (len + page_size * 2) & ~(page_size - 1);
768
769 return mprotect(start, len, PROT_READ | PROT_WRITE);
770}
Simon Glass001d1882019-04-08 13:20:41 -0600771
772void *os_find_text_base(void)
773{
774 char line[500];
775 void *base = NULL;
776 int len;
777 int fd;
778
779 /*
780 * This code assumes that the first line of /proc/self/maps holds
781 * information about the text, for example:
782 *
783 * 5622d9907000-5622d9a55000 r-xp 00000000 08:01 15067168 u-boot
784 *
785 * The first hex value is assumed to be the address.
786 *
787 * This is tested in Linux 4.15.
788 */
789 fd = open("/proc/self/maps", O_RDONLY);
790 if (fd == -1)
791 return NULL;
792 len = read(fd, line, sizeof(line));
793 if (len > 0) {
794 char *end = memchr(line, '-', len);
795
796 if (end) {
Heinrich Schuchardt4ecb5062019-10-26 23:17:44 +0200797 uintptr_t addr;
Simon Glass001d1882019-04-08 13:20:41 -0600798
799 *end = '\0';
Heinrich Schuchardt4ecb5062019-10-26 23:17:44 +0200800 if (sscanf(line, "%zx", &addr) == 1)
Simon Glass001d1882019-04-08 13:20:41 -0600801 base = (void *)addr;
802 }
803 }
804 close(fd);
805
806 return base;
807}