blob: e7ec892bdf0714dd9a56db4ff730d8ce971ec592 [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{
89 return close(fd);
90}
91
Stephen Warrencfd13e82014-03-01 22:18:00 -070092int os_unlink(const char *pathname)
93{
94 return unlink(pathname);
95}
96
Simon Glass7a9219c2011-10-03 19:26:44 +000097void os_exit(int exit_code)
98{
99 exit(exit_code);
100}
Mike Frysingerab06a752011-10-26 00:21:29 +0000101
Simon Glass566bf3a2018-11-06 15:21:25 -0700102int os_write_file(const char *fname, const void *buf, int size)
Simon Glass056a5ce2018-10-01 11:55:08 -0600103{
Simon Glass056a5ce2018-10-01 11:55:08 -0600104 int fd;
105
106 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC);
107 if (fd < 0) {
108 printf("Cannot open file '%s'\n", fname);
109 return -EIO;
110 }
111 if (os_write(fd, buf, size) != size) {
112 printf("Cannot write to file '%s'\n", fname);
Simon Glass566bf3a2018-11-06 15:21:25 -0700113 os_close(fd);
Simon Glass056a5ce2018-10-01 11:55:08 -0600114 return -EIO;
115 }
116 os_close(fd);
Simon Glass056a5ce2018-10-01 11:55:08 -0600117
118 return 0;
119}
120
Simon Glass566bf3a2018-11-06 15:21:25 -0700121int os_read_file(const char *fname, void **bufp, int *sizep)
122{
123 off_t size;
124 int ret = -EIO;
125 int fd;
126
127 fd = os_open(fname, OS_O_RDONLY);
128 if (fd < 0) {
129 printf("Cannot open file '%s'\n", fname);
130 goto err;
131 }
132 size = os_lseek(fd, 0, OS_SEEK_END);
133 if (size < 0) {
134 printf("Cannot seek to end of file '%s'\n", fname);
135 goto err;
136 }
137 if (os_lseek(fd, 0, OS_SEEK_SET) < 0) {
138 printf("Cannot seek to start of file '%s'\n", fname);
139 goto err;
140 }
Simon Glass0db1b432020-02-03 07:36:02 -0700141 *bufp = malloc(size);
Simon Glass566bf3a2018-11-06 15:21:25 -0700142 if (!*bufp) {
143 printf("Not enough memory to read file '%s'\n", fname);
144 ret = -ENOMEM;
145 goto err;
146 }
147 if (os_read(fd, *bufp, size) != size) {
148 printf("Cannot read from file '%s'\n", fname);
149 goto err;
150 }
151 os_close(fd);
152 *sizep = size;
153
154 return 0;
155err:
156 os_close(fd);
157 return ret;
158}
159
Mike Frysingerab06a752011-10-26 00:21:29 +0000160/* Restore tty state when we exit */
161static struct termios orig_term;
Simon Glassffb87902014-02-27 13:26:22 -0700162static bool term_setup;
Simon Glass4af3e9a2018-10-01 11:55:20 -0600163static bool term_nonblock;
Mike Frysingerab06a752011-10-26 00:21:29 +0000164
Simon Glass8939df02015-05-10 21:07:27 -0600165void os_fd_restore(void)
Mike Frysingerab06a752011-10-26 00:21:29 +0000166{
Simon Glass8939df02015-05-10 21:07:27 -0600167 if (term_setup) {
Simon Glass4af3e9a2018-10-01 11:55:20 -0600168 int flags;
169
Simon Glassffb87902014-02-27 13:26:22 -0700170 tcsetattr(0, TCSANOW, &orig_term);
Simon Glass4af3e9a2018-10-01 11:55:20 -0600171 if (term_nonblock) {
172 flags = fcntl(0, F_GETFL, 0);
173 fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
174 }
Simon Glass8939df02015-05-10 21:07:27 -0600175 term_setup = false;
176 }
Mike Frysingerab06a752011-10-26 00:21:29 +0000177}
178
Rasmus Villemoes29601072020-02-14 10:58:37 +0000179static void os_sigint_handler(int sig)
180{
181 os_fd_restore();
182 signal(SIGINT, SIG_DFL);
183 raise(SIGINT);
184}
185
Mike Frysingerab06a752011-10-26 00:21:29 +0000186/* Put tty into raw mode so <tab> and <ctrl+c> work */
Simon Glassffb87902014-02-27 13:26:22 -0700187void os_tty_raw(int fd, bool allow_sigs)
Mike Frysingerab06a752011-10-26 00:21:29 +0000188{
Mike Frysingerab06a752011-10-26 00:21:29 +0000189 struct termios term;
Simon Glass4af3e9a2018-10-01 11:55:20 -0600190 int flags;
Mike Frysingerab06a752011-10-26 00:21:29 +0000191
Simon Glassffb87902014-02-27 13:26:22 -0700192 if (term_setup)
Mike Frysingerab06a752011-10-26 00:21:29 +0000193 return;
Mike Frysingerab06a752011-10-26 00:21:29 +0000194
195 /* If not a tty, don't complain */
196 if (tcgetattr(fd, &orig_term))
197 return;
198
199 term = orig_term;
200 term.c_iflag = IGNBRK | IGNPAR;
201 term.c_oflag = OPOST | ONLCR;
202 term.c_cflag = CS8 | CREAD | CLOCAL;
Simon Glassffb87902014-02-27 13:26:22 -0700203 term.c_lflag = allow_sigs ? ISIG : 0;
Mike Frysingerab06a752011-10-26 00:21:29 +0000204 if (tcsetattr(fd, TCSANOW, &term))
205 return;
206
Simon Glass4af3e9a2018-10-01 11:55:20 -0600207 flags = fcntl(fd, F_GETFL, 0);
208 if (!(flags & O_NONBLOCK)) {
209 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
210 return;
211 term_nonblock = true;
212 }
213
Simon Glass8939df02015-05-10 21:07:27 -0600214 term_setup = true;
Mike Frysingerab06a752011-10-26 00:21:29 +0000215 atexit(os_fd_restore);
Rasmus Villemoes29601072020-02-14 10:58:37 +0000216 signal(SIGINT, os_sigint_handler);
Mike Frysingerab06a752011-10-26 00:21:29 +0000217}
Matthias Weisser21899b12011-11-05 11:40:34 +0100218
219void *os_malloc(size_t length)
220{
Simon Glass61318502018-09-15 00:50:54 -0600221 int page_size = getpagesize();
Simon Glass4a6409b2019-04-08 13:20:42 -0600222 struct os_mem_hdr *hdr;
Simon Glass77595c62013-11-10 10:26:57 -0700223
Simon Glassbd8b7452018-06-17 08:57:43 -0600224 /*
225 * Use an address that is hopefully available to us so that pointers
226 * to this memory are fairly obvious. If we end up with a different
227 * address, that's fine too.
228 */
229 hdr = mmap((void *)0x10000000, length + page_size,
Alexander Graf6e6e4b22018-06-22 14:44:13 +0200230 PROT_READ | PROT_WRITE | PROT_EXEC,
231 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Simon Glass77595c62013-11-10 10:26:57 -0700232 if (hdr == MAP_FAILED)
233 return NULL;
234 hdr->length = length;
235
Simon Glass61318502018-09-15 00:50:54 -0600236 return (void *)hdr + page_size;
Simon Glass77595c62013-11-10 10:26:57 -0700237}
238
Masahiro Yamada347d06d2014-01-15 13:06:41 +0900239void os_free(void *ptr)
Simon Glass77595c62013-11-10 10:26:57 -0700240{
Simon Glass4a6409b2019-04-08 13:20:42 -0600241 int page_size = getpagesize();
242 struct os_mem_hdr *hdr;
Simon Glass77595c62013-11-10 10:26:57 -0700243
Simon Glass4a6409b2019-04-08 13:20:42 -0600244 if (ptr) {
245 hdr = ptr - page_size;
246 munmap(hdr, hdr->length + page_size);
247 }
Simon Glass77595c62013-11-10 10:26:57 -0700248}
249
Matthias Weisserd99a6872011-11-29 12:16:40 +0100250void os_usleep(unsigned long usec)
251{
252 usleep(usec);
253}
254
Simon Glass2a54d152013-05-19 16:45:35 -0700255uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
Matthias Weisserd99a6872011-11-29 12:16:40 +0100256{
257#if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
258 struct timespec tp;
259 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
260 struct timeval tv;
261
262 gettimeofday(&tv, NULL);
263 tp.tv_sec = tv.tv_sec;
264 tp.tv_nsec = tv.tv_usec * 1000;
265 }
266 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
267#else
268 struct timeval tv;
269 gettimeofday(&tv, NULL);
270 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
271#endif
272}
Simon Glass70db4212012-02-15 15:51:16 -0800273
274static char *short_opts;
275static struct option *long_opts;
276
277int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
278{
Simon Glass7b3efc62013-12-03 16:43:23 -0700279 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass70db4212012-02-15 15:51:16 -0800280 size_t num_options = __u_boot_sandbox_option_count();
281 size_t i;
282
283 int hidden_short_opt;
284 size_t si;
285
286 int c;
287
288 if (short_opts || long_opts)
289 return 1;
290
291 state->argc = argc;
292 state->argv = argv;
293
294 /* dynamically construct the arguments to the system getopt_long */
Simon Glass0db1b432020-02-03 07:36:02 -0700295 short_opts = malloc(sizeof(*short_opts) * num_options * 2 + 1);
Simon Glass1c8c47e2020-02-03 07:36:04 -0700296 long_opts = malloc(sizeof(*long_opts) * (num_options + 1));
Simon Glass70db4212012-02-15 15:51:16 -0800297 if (!short_opts || !long_opts)
298 return 1;
299
300 /*
301 * getopt_long requires "val" to be unique (since that is what the
302 * func returns), so generate unique values automatically for flags
303 * that don't have a short option. pick 0x100 as that is above the
304 * single byte range (where ASCII/ISO-XXXX-X charsets live).
305 */
306 hidden_short_opt = 0x100;
307 si = 0;
308 for (i = 0; i < num_options; ++i) {
309 long_opts[i].name = sb_opt[i]->flag;
310 long_opts[i].has_arg = sb_opt[i]->has_arg ?
311 required_argument : no_argument;
312 long_opts[i].flag = NULL;
313
314 if (sb_opt[i]->flag_short) {
315 short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
316 if (long_opts[i].has_arg == required_argument)
317 short_opts[si++] = ':';
318 } else
319 long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
320 }
321 short_opts[si] = '\0';
322
323 /* we need to handle output ourselves since u-boot provides printf */
324 opterr = 0;
325
Simon Glass1c8c47e2020-02-03 07:36:04 -0700326 memset(&long_opts[num_options], '\0', sizeof(*long_opts));
Simon Glass70db4212012-02-15 15:51:16 -0800327 /*
328 * walk all of the options the user gave us on the command line,
329 * figure out what u-boot option structure they belong to (via
330 * the unique short val key), and call the appropriate callback.
331 */
332 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
333 for (i = 0; i < num_options; ++i) {
334 if (sb_opt[i]->flag_short == c) {
335 if (sb_opt[i]->callback(state, optarg)) {
336 state->parse_err = sb_opt[i]->flag;
337 return 0;
338 }
339 break;
340 }
341 }
342 if (i == num_options) {
343 /*
344 * store the faulting flag for later display. we have to
345 * store the flag itself as the getopt parsing itself is
346 * tricky: need to handle the following flags (assume all
347 * of the below are unknown):
348 * -a optopt='a' optind=<next>
349 * -abbbb optopt='a' optind=<this>
350 * -aaaaa optopt='a' optind=<this>
351 * --a optopt=0 optind=<this>
352 * as you can see, it is impossible to determine the exact
353 * faulting flag without doing the parsing ourselves, so
354 * we just report the specific flag that failed.
355 */
356 if (optopt) {
357 static char parse_err[3] = { '-', 0, '\0', };
358 parse_err[1] = optopt;
359 state->parse_err = parse_err;
360 } else
361 state->parse_err = argv[optind - 1];
362 break;
363 }
364 }
365
366 return 0;
367}
Simon Glass62584db2012-12-26 09:53:34 +0000368
369void os_dirent_free(struct os_dirent_node *node)
370{
371 struct os_dirent_node *next;
372
373 while (node) {
374 next = node->next;
Simon Glass0db1b432020-02-03 07:36:02 -0700375 free(node);
Simon Glass62584db2012-12-26 09:53:34 +0000376 node = next;
377 }
378}
379
380int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
381{
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200382 struct dirent *entry;
Simon Glass62584db2012-12-26 09:53:34 +0000383 struct os_dirent_node *head, *node, *next;
384 struct stat buf;
385 DIR *dir;
386 int ret;
387 char *fname;
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200388 char *old_fname;
Simon Glass62584db2012-12-26 09:53:34 +0000389 int len;
Stefan Brünsf189899c2016-10-01 20:41:40 +0200390 int dirlen;
Simon Glass62584db2012-12-26 09:53:34 +0000391
392 *headp = NULL;
393 dir = opendir(dirname);
394 if (!dir)
395 return -1;
396
Stefan Brünsf189899c2016-10-01 20:41:40 +0200397 /* Create a buffer upfront, with typically sufficient size */
398 dirlen = strlen(dirname) + 2;
399 len = dirlen + 256;
Simon Glass0db1b432020-02-03 07:36:02 -0700400 fname = malloc(len);
Simon Glass62584db2012-12-26 09:53:34 +0000401 if (!fname) {
402 ret = -ENOMEM;
403 goto done;
404 }
405
406 for (node = head = NULL;; node = next) {
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200407 errno = 0;
408 entry = readdir(dir);
409 if (!entry) {
410 ret = errno;
Simon Glass62584db2012-12-26 09:53:34 +0000411 break;
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200412 }
Simon Glass0db1b432020-02-03 07:36:02 -0700413 next = malloc(sizeof(*node) + strlen(entry->d_name) + 1);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200414 if (!next) {
Simon Glass62584db2012-12-26 09:53:34 +0000415 os_dirent_free(head);
416 ret = -ENOMEM;
417 goto done;
418 }
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200419 if (dirlen + strlen(entry->d_name) > len) {
420 len = dirlen + strlen(entry->d_name);
421 old_fname = fname;
Simon Glass0db1b432020-02-03 07:36:02 -0700422 fname = realloc(fname, len);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200423 if (!fname) {
Simon Glass0db1b432020-02-03 07:36:02 -0700424 free(old_fname);
425 free(next);
Heinrich Schuchardt04d0da52017-09-21 12:56:07 +0200426 os_dirent_free(head);
427 ret = -ENOMEM;
428 goto done;
429 }
430 }
Stephen Warren9c38c072014-06-11 10:26:23 -0600431 next->next = NULL;
Stefan Brünsbf635ed2016-10-01 20:41:42 +0200432 strcpy(next->name, entry->d_name);
433 switch (entry->d_type) {
Simon Glass62584db2012-12-26 09:53:34 +0000434 case DT_REG:
435 next->type = OS_FILET_REG;
436 break;
437 case DT_DIR:
438 next->type = OS_FILET_DIR;
439 break;
440 case DT_LNK:
441 next->type = OS_FILET_LNK;
442 break;
Stefan Brüns2f159402016-10-04 21:46:35 +0200443 default:
444 next->type = OS_FILET_UNKNOWN;
Simon Glass62584db2012-12-26 09:53:34 +0000445 }
446 next->size = 0;
447 snprintf(fname, len, "%s/%s", dirname, next->name);
448 if (!stat(fname, &buf))
449 next->size = buf.st_size;
450 if (node)
451 node->next = next;
Stefan Brünsce2ec192016-10-01 20:41:39 +0200452 else
453 head = next;
Simon Glass62584db2012-12-26 09:53:34 +0000454 }
455 *headp = head;
456
457done:
458 closedir(dir);
Simon Glass0db1b432020-02-03 07:36:02 -0700459 free(fname);
Simon Glass62584db2012-12-26 09:53:34 +0000460 return ret;
461}
462
463const char *os_dirent_typename[OS_FILET_COUNT] = {
464 " ",
465 "SYM",
466 "DIR",
467 "???",
468};
469
470const char *os_dirent_get_typename(enum os_dirent_t type)
471{
Tom Rinie2bc87d2017-05-13 20:11:30 -0400472 if (type >= OS_FILET_REG && type < OS_FILET_COUNT)
Simon Glass62584db2012-12-26 09:53:34 +0000473 return os_dirent_typename[type];
474
475 return os_dirent_typename[OS_FILET_UNKNOWN];
476}
477
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800478int os_get_filesize(const char *fname, loff_t *size)
Simon Glass62584db2012-12-26 09:53:34 +0000479{
480 struct stat buf;
481 int ret;
482
483 ret = stat(fname, &buf);
484 if (ret)
485 return ret;
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800486 *size = buf.st_size;
487 return 0;
Simon Glass62584db2012-12-26 09:53:34 +0000488}
Simon Glass91b136c2013-11-10 10:27:01 -0700489
Simon Glass0b189b62017-12-04 13:48:17 -0700490void os_putc(int ch)
491{
492 putchar(ch);
493}
494
495void os_puts(const char *str)
496{
497 while (*str)
498 os_putc(*str++);
499}
500
Simon Glass5c2859c2013-11-10 10:27:03 -0700501int os_write_ram_buf(const char *fname)
502{
503 struct sandbox_state *state = state_get_current();
504 int fd, ret;
505
506 fd = open(fname, O_CREAT | O_WRONLY, 0777);
507 if (fd < 0)
508 return -ENOENT;
509 ret = write(fd, state->ram_buf, state->ram_size);
510 close(fd);
511 if (ret != state->ram_size)
512 return -EIO;
513
514 return 0;
515}
516
517int os_read_ram_buf(const char *fname)
518{
519 struct sandbox_state *state = state_get_current();
520 int fd, ret;
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800521 loff_t size;
Simon Glass5c2859c2013-11-10 10:27:03 -0700522
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800523 ret = os_get_filesize(fname, &size);
524 if (ret < 0)
525 return ret;
Simon Glass5c2859c2013-11-10 10:27:03 -0700526 if (size != state->ram_size)
527 return -ENOSPC;
528 fd = open(fname, O_RDONLY);
529 if (fd < 0)
530 return -ENOENT;
531
532 ret = read(fd, state->ram_buf, state->ram_size);
533 close(fd);
534 if (ret != state->ram_size)
535 return -EIO;
536
537 return 0;
538}
Simon Glass47f5fcf2014-02-27 13:26:15 -0700539
540static int make_exec(char *fname, const void *data, int size)
541{
542 int fd;
543
544 strcpy(fname, "/tmp/u-boot.jump.XXXXXX");
545 fd = mkstemp(fname);
546 if (fd < 0)
547 return -ENOENT;
548 if (write(fd, data, size) < 0)
549 return -EIO;
550 close(fd);
551 if (chmod(fname, 0777))
552 return -ENOEXEC;
553
554 return 0;
555}
556
Simon Glass7b5ea142018-11-15 18:44:05 -0700557/**
558 * add_args() - Allocate a new argv with the given args
559 *
560 * This is used to create a new argv array with all the old arguments and some
561 * new ones that are passed in
562 *
563 * @argvp: Returns newly allocated args list
564 * @add_args: Arguments to add, each a string
565 * @count: Number of arguments in @add_args
566 * @return 0 if OK, -ENOMEM if out of memory
567 */
568static int add_args(char ***argvp, char *add_args[], int count)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700569{
Simon Glass65f3b1f2018-11-15 18:44:07 -0700570 char **argv, **ap;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700571 int argc;
572
Simon Glass65f3b1f2018-11-15 18:44:07 -0700573 for (argc = 0; (*argvp)[argc]; argc++)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700574 ;
575
Simon Glass0db1b432020-02-03 07:36:02 -0700576 argv = malloc((argc + count + 1) * sizeof(char *));
Simon Glass47f5fcf2014-02-27 13:26:15 -0700577 if (!argv) {
578 printf("Out of memory for %d argv\n", count);
579 return -ENOMEM;
580 }
Simon Glass65f3b1f2018-11-15 18:44:07 -0700581 for (ap = *argvp, argc = 0; *ap; ap++) {
582 char *arg = *ap;
583
584 /* Drop args that we don't want to propagate */
585 if (*arg == '-' && strlen(arg) == 2) {
586 switch (arg[1]) {
587 case 'j':
588 case 'm':
589 ap++;
590 continue;
591 }
592 } else if (!strcmp(arg, "--rm_memory")) {
593 ap++;
594 continue;
595 }
596 argv[argc++] = arg;
597 }
598
Simon Glass47f5fcf2014-02-27 13:26:15 -0700599 memcpy(argv + argc, add_args, count * sizeof(char *));
600 argv[argc + count] = NULL;
601
602 *argvp = argv;
603 return 0;
604}
605
Simon Glass7b5ea142018-11-15 18:44:05 -0700606/**
607 * os_jump_to_file() - Jump to a new program
608 *
609 * This saves the memory buffer, sets up arguments to the new process, then
610 * execs it.
611 *
612 * @fname: Filename to exec
613 * @return does not return on success, any return value is an error
614 */
615static int os_jump_to_file(const char *fname)
Simon Glass47f5fcf2014-02-27 13:26:15 -0700616{
617 struct sandbox_state *state = state_get_current();
Simon Glass7b5ea142018-11-15 18:44:05 -0700618 char mem_fname[30];
Simon Glass47f5fcf2014-02-27 13:26:15 -0700619 int fd, err;
Simon Glass7b5ea142018-11-15 18:44:05 -0700620 char *extra_args[5];
Simon Glass47f5fcf2014-02-27 13:26:15 -0700621 char **argv = state->argv;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700622 int argc;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700623#ifdef DEBUG
Simon Glass7b5ea142018-11-15 18:44:05 -0700624 int i;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700625#endif
626
Simon Glass47f5fcf2014-02-27 13:26:15 -0700627 strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
628 fd = mkstemp(mem_fname);
629 if (fd < 0)
630 return -ENOENT;
631 close(fd);
632 err = os_write_ram_buf(mem_fname);
633 if (err)
634 return err;
635
636 os_fd_restore();
637
638 extra_args[0] = "-j";
Simon Glass7b5ea142018-11-15 18:44:05 -0700639 extra_args[1] = (char *)fname;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700640 extra_args[2] = "-m";
641 extra_args[3] = mem_fname;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700642 argc = 4;
643 if (state->ram_buf_rm)
644 extra_args[argc++] = "--rm_memory";
645 err = add_args(&argv, extra_args, argc);
Simon Glass47f5fcf2014-02-27 13:26:15 -0700646 if (err)
647 return err;
Simon Glass65f3b1f2018-11-15 18:44:07 -0700648 argv[0] = (char *)fname;
Simon Glass47f5fcf2014-02-27 13:26:15 -0700649
650#ifdef DEBUG
651 for (i = 0; argv[i]; i++)
652 printf("%d %s\n", i, argv[i]);
653#endif
654
655 if (state_uninit())
656 os_exit(2);
657
658 err = execv(fname, argv);
Simon Glass0db1b432020-02-03 07:36:02 -0700659 free(argv);
Simon Glass27028f12018-11-15 18:44:08 -0700660 if (err) {
661 perror("Unable to run image");
Simon Glass6b5e4202018-11-23 21:29:24 -0700662 printf("Image filename '%s'\n", fname);
Simon Glass47f5fcf2014-02-27 13:26:15 -0700663 return err;
Simon Glass27028f12018-11-15 18:44:08 -0700664 }
Simon Glass47f5fcf2014-02-27 13:26:15 -0700665
666 return unlink(fname);
667}
Simon Glass94eefde2015-04-20 12:37:22 -0600668
Simon Glass7b5ea142018-11-15 18:44:05 -0700669int os_jump_to_image(const void *dest, int size)
670{
671 char fname[30];
672 int err;
673
674 err = make_exec(fname, dest, size);
675 if (err)
676 return err;
677
678 return os_jump_to_file(fname);
679}
680
Simon Glassd4e33f52016-07-04 11:57:45 -0600681int os_find_u_boot(char *fname, int maxlen)
682{
683 struct sandbox_state *state = state_get_current();
684 const char *progname = state->argv[0];
685 int len = strlen(progname);
Simon Glass69bc15d2018-10-01 11:55:10 -0600686 const char *suffix;
Simon Glassd4e33f52016-07-04 11:57:45 -0600687 char *p;
688 int fd;
689
690 if (len >= maxlen || len < 4)
691 return -ENOSPC;
692
Simon Glassd4e33f52016-07-04 11:57:45 -0600693 strcpy(fname, progname);
Simon Glass69bc15d2018-10-01 11:55:10 -0600694 suffix = fname + len - 4;
695
696 /* If we are TPL, boot to SPL */
697 if (!strcmp(suffix, "-tpl")) {
698 fname[len - 3] = 's';
699 fd = os_open(fname, O_RDONLY);
700 if (fd >= 0) {
701 close(fd);
702 return 0;
703 }
704
705 /* Look for 'u-boot-tpl' in the tpl/ directory */
706 p = strstr(fname, "/tpl/");
707 if (p) {
708 p[1] = 's';
709 fd = os_open(fname, O_RDONLY);
710 if (fd >= 0) {
711 close(fd);
712 return 0;
713 }
714 }
715 return -ENOENT;
716 }
717
718 /* Look for 'u-boot' in the same directory as 'u-boot-spl' */
719 if (!strcmp(suffix, "-spl")) {
Simon Glassd4e33f52016-07-04 11:57:45 -0600720 fname[len - 4] = '\0';
721 fd = os_open(fname, O_RDONLY);
722 if (fd >= 0) {
723 close(fd);
724 return 0;
725 }
726 }
727
728 /* Look for 'u-boot' in the parent directory of spl/ */
Simon Glassb847c142018-11-13 15:55:20 -0700729 p = strstr(fname, "spl/");
Simon Glassd4e33f52016-07-04 11:57:45 -0600730 if (p) {
Simon Glassb847c142018-11-13 15:55:20 -0700731 /* Remove the "spl" characters */
732 memmove(p, p + 4, strlen(p + 4) + 1);
Simon Glassd4e33f52016-07-04 11:57:45 -0600733 fd = os_open(fname, O_RDONLY);
734 if (fd >= 0) {
735 close(fd);
736 return 0;
737 }
738 }
739
740 return -ENOENT;
741}
742
743int os_spl_to_uboot(const char *fname)
744{
Simon Glass27028f12018-11-15 18:44:08 -0700745 return os_jump_to_file(fname);
Simon Glassd4e33f52016-07-04 11:57:45 -0600746}
747
Simon Glass94eefde2015-04-20 12:37:22 -0600748void os_localtime(struct rtc_time *rt)
749{
750 time_t t = time(NULL);
751 struct tm *tm;
752
753 tm = localtime(&t);
754 rt->tm_sec = tm->tm_sec;
755 rt->tm_min = tm->tm_min;
756 rt->tm_hour = tm->tm_hour;
757 rt->tm_mday = tm->tm_mday;
758 rt->tm_mon = tm->tm_mon + 1;
759 rt->tm_year = tm->tm_year + 1900;
760 rt->tm_wday = tm->tm_wday;
761 rt->tm_yday = tm->tm_yday;
762 rt->tm_isdst = tm->tm_isdst;
763}
Simon Glass30eef212018-05-16 09:42:22 -0600764
Simon Glassfe938fb2018-09-15 00:50:55 -0600765void os_abort(void)
766{
767 abort();
768}
Simon Glass9f8037e2018-10-01 21:12:32 -0600769
770int os_mprotect_allow(void *start, size_t len)
771{
772 int page_size = getpagesize();
773
774 /* Move start to the start of a page, len to the end */
775 start = (void *)(((ulong)start) & ~(page_size - 1));
776 len = (len + page_size * 2) & ~(page_size - 1);
777
778 return mprotect(start, len, PROT_READ | PROT_WRITE);
779}
Simon Glass001d1882019-04-08 13:20:41 -0600780
781void *os_find_text_base(void)
782{
783 char line[500];
784 void *base = NULL;
785 int len;
786 int fd;
787
788 /*
789 * This code assumes that the first line of /proc/self/maps holds
790 * information about the text, for example:
791 *
792 * 5622d9907000-5622d9a55000 r-xp 00000000 08:01 15067168 u-boot
793 *
794 * The first hex value is assumed to be the address.
795 *
796 * This is tested in Linux 4.15.
797 */
798 fd = open("/proc/self/maps", O_RDONLY);
799 if (fd == -1)
800 return NULL;
801 len = read(fd, line, sizeof(line));
802 if (len > 0) {
803 char *end = memchr(line, '-', len);
804
805 if (end) {
Heinrich Schuchardt4ecb5062019-10-26 23:17:44 +0200806 uintptr_t addr;
Simon Glass001d1882019-04-08 13:20:41 -0600807
808 *end = '\0';
Heinrich Schuchardt4ecb5062019-10-26 23:17:44 +0200809 if (sscanf(line, "%zx", &addr) == 1)
Simon Glass001d1882019-04-08 13:20:41 -0600810 base = (void *)addr;
811 }
812 }
813 close(fd);
814
815 return base;
816}