blob: 07e46471fe5b67c374a343c7577e84527f9a182d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasscd0684f2011-10-03 19:26:44 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glasscd0684f2011-10-03 19:26:44 +00004 */
5
Simon Glassf1c45c82012-12-26 09:53:34 +00006#include <dirent.h>
Simon Glasse8015a62012-01-10 15:54:05 -08007#include <errno.h>
Simon Glasscd0684f2011-10-03 19:26:44 +00008#include <fcntl.h>
Simon Glass8a3e0352012-02-15 15:51:16 -08009#include <getopt.h>
Simon Glass5dccd152018-05-16 09:42:22 -060010#include <setjmp.h>
Simon Glassf1c45c82012-12-26 09:53:34 +000011#include <stdio.h>
Simon Glassfb4b4e82013-05-19 16:45:35 -070012#include <stdint.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000013#include <stdlib.h>
Simon Glassf1c45c82012-12-26 09:53:34 +000014#include <string.h>
Mike Frysingera5baaee2011-10-26 00:21:29 +000015#include <termios.h>
Matthias Weisser0d3dd142011-11-29 12:16:40 +010016#include <time.h>
Simon Glasse8015a62012-01-10 15:54:05 -080017#include <unistd.h>
Matthias Weisserb5f7b472011-11-05 11:40:34 +010018#include <sys/mman.h>
Simon Glasse8015a62012-01-10 15:54:05 -080019#include <sys/stat.h>
Simon Glass17064c02012-01-10 15:54:06 -080020#include <sys/time.h>
Simon Glasse8015a62012-01-10 15:54:05 -080021#include <sys/types.h>
Matthias Weisser0d3dd142011-11-29 12:16:40 +010022#include <linux/types.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000023
Simon Glass8a3e0352012-02-15 15:51:16 -080024#include <asm/getopt.h>
25#include <asm/sections.h>
26#include <asm/state.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000027#include <os.h>
Simon Glass504548f2015-04-20 12:37:22 -060028#include <rtc_def.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000029
30/* Operating System Interface */
31
Simon Glass1e6594c2013-11-10 10:26:57 -070032struct os_mem_hdr {
33 size_t length; /* number of bytes in the block */
34};
35
Simon Glasscd0684f2011-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 Frysinger60addac2011-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 Glass3196d752012-02-20 23:56:58 -050059int os_open(const char *pathname, int os_flags)
Simon Glasscd0684f2011-10-03 19:26:44 +000060{
Simon Glass3196d752012-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 Glassce55a112018-10-01 11:55:07 -060080 if (os_flags & OS_O_TRUNC)
81 flags |= O_TRUNC;
Simon Glass3196d752012-02-20 23:56:58 -050082
83 return open(pathname, flags, 0777);
Simon Glasscd0684f2011-10-03 19:26:44 +000084}
85
86int os_close(int fd)
87{
88 return close(fd);
89}
90
Stephen Warrencd5edba2014-03-01 22:18:00 -070091int os_unlink(const char *pathname)
92{
93 return unlink(pathname);
94}
95
Simon Glasscd0684f2011-10-03 19:26:44 +000096void os_exit(int exit_code)
97{
98 exit(exit_code);
99}
Mike Frysingera5baaee2011-10-26 00:21:29 +0000100
Simon Glasscbfa8452018-10-01 11:55:08 -0600101int os_write_file(const char *name, const void *buf, int size)
102{
103 char fname[256];
104 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);
113 return -EIO;
114 }
115 os_close(fd);
116 printf("Write '%s', size %#x (%d)\n", name, size, size);
117
118 return 0;
119}
120
Mike Frysingera5baaee2011-10-26 00:21:29 +0000121/* Restore tty state when we exit */
122static struct termios orig_term;
Simon Glass678ef472014-02-27 13:26:22 -0700123static bool term_setup;
Simon Glassae50ec72018-10-01 11:55:20 -0600124static bool term_nonblock;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000125
Simon Glass9c3b7d62015-05-10 21:07:27 -0600126void os_fd_restore(void)
Mike Frysingera5baaee2011-10-26 00:21:29 +0000127{
Simon Glass9c3b7d62015-05-10 21:07:27 -0600128 if (term_setup) {
Simon Glassae50ec72018-10-01 11:55:20 -0600129 int flags;
130
Simon Glass678ef472014-02-27 13:26:22 -0700131 tcsetattr(0, TCSANOW, &orig_term);
Simon Glassae50ec72018-10-01 11:55:20 -0600132 if (term_nonblock) {
133 flags = fcntl(0, F_GETFL, 0);
134 fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
135 }
Simon Glass9c3b7d62015-05-10 21:07:27 -0600136 term_setup = false;
137 }
Mike Frysingera5baaee2011-10-26 00:21:29 +0000138}
139
140/* Put tty into raw mode so <tab> and <ctrl+c> work */
Simon Glass678ef472014-02-27 13:26:22 -0700141void os_tty_raw(int fd, bool allow_sigs)
Mike Frysingera5baaee2011-10-26 00:21:29 +0000142{
Mike Frysingera5baaee2011-10-26 00:21:29 +0000143 struct termios term;
Simon Glassae50ec72018-10-01 11:55:20 -0600144 int flags;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000145
Simon Glass678ef472014-02-27 13:26:22 -0700146 if (term_setup)
Mike Frysingera5baaee2011-10-26 00:21:29 +0000147 return;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000148
149 /* If not a tty, don't complain */
150 if (tcgetattr(fd, &orig_term))
151 return;
152
153 term = orig_term;
154 term.c_iflag = IGNBRK | IGNPAR;
155 term.c_oflag = OPOST | ONLCR;
156 term.c_cflag = CS8 | CREAD | CLOCAL;
Simon Glass678ef472014-02-27 13:26:22 -0700157 term.c_lflag = allow_sigs ? ISIG : 0;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000158 if (tcsetattr(fd, TCSANOW, &term))
159 return;
160
Simon Glassae50ec72018-10-01 11:55:20 -0600161 flags = fcntl(fd, F_GETFL, 0);
162 if (!(flags & O_NONBLOCK)) {
163 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
164 return;
165 term_nonblock = true;
166 }
167
Simon Glass9c3b7d62015-05-10 21:07:27 -0600168 term_setup = true;
Mike Frysingera5baaee2011-10-26 00:21:29 +0000169 atexit(os_fd_restore);
170}
Matthias Weisserb5f7b472011-11-05 11:40:34 +0100171
172void *os_malloc(size_t length)
173{
Simon Glass1e6594c2013-11-10 10:26:57 -0700174 struct os_mem_hdr *hdr;
Simon Glassbe005d82018-09-15 00:50:54 -0600175 int page_size = getpagesize();
Simon Glass1e6594c2013-11-10 10:26:57 -0700176
Simon Glassbe005d82018-09-15 00:50:54 -0600177 hdr = mmap(NULL, length + page_size,
Alexander Graf934a5452018-06-22 14:44:13 +0200178 PROT_READ | PROT_WRITE | PROT_EXEC,
179 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Simon Glass1e6594c2013-11-10 10:26:57 -0700180 if (hdr == MAP_FAILED)
181 return NULL;
182 hdr->length = length;
183
Simon Glassbe005d82018-09-15 00:50:54 -0600184 return (void *)hdr + page_size;
Simon Glass1e6594c2013-11-10 10:26:57 -0700185}
186
Masahiro Yamadaee957282014-01-15 13:06:41 +0900187void os_free(void *ptr)
Simon Glass1e6594c2013-11-10 10:26:57 -0700188{
189 struct os_mem_hdr *hdr = ptr;
190
191 hdr--;
192 if (ptr)
193 munmap(hdr, hdr->length + sizeof(*hdr));
194}
195
196void *os_realloc(void *ptr, size_t length)
197{
198 struct os_mem_hdr *hdr = ptr;
199 void *buf = NULL;
200
201 hdr--;
202 if (length != 0) {
203 buf = os_malloc(length);
204 if (!buf)
205 return buf;
206 if (ptr) {
207 if (length > hdr->length)
208 length = hdr->length;
209 memcpy(buf, ptr, length);
210 }
211 }
212 os_free(ptr);
213
214 return buf;
Matthias Weisserb5f7b472011-11-05 11:40:34 +0100215}
Matthias Weisser0d3dd142011-11-29 12:16:40 +0100216
217void os_usleep(unsigned long usec)
218{
219 usleep(usec);
220}
221
Simon Glassfb4b4e82013-05-19 16:45:35 -0700222uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
Matthias Weisser0d3dd142011-11-29 12:16:40 +0100223{
224#if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
225 struct timespec tp;
226 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
227 struct timeval tv;
228
229 gettimeofday(&tv, NULL);
230 tp.tv_sec = tv.tv_sec;
231 tp.tv_nsec = tv.tv_usec * 1000;
232 }
233 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
234#else
235 struct timeval tv;
236 gettimeofday(&tv, NULL);
237 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
238#endif
239}
Simon Glass8a3e0352012-02-15 15:51:16 -0800240
241static char *short_opts;
242static struct option *long_opts;
243
244int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
245{
Simon Glass64367c82013-12-03 16:43:23 -0700246 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass8a3e0352012-02-15 15:51:16 -0800247 size_t num_options = __u_boot_sandbox_option_count();
248 size_t i;
249
250 int hidden_short_opt;
251 size_t si;
252
253 int c;
254
255 if (short_opts || long_opts)
256 return 1;
257
258 state->argc = argc;
259 state->argv = argv;
260
261 /* dynamically construct the arguments to the system getopt_long */
262 short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
263 long_opts = os_malloc(sizeof(*long_opts) * num_options);
264 if (!short_opts || !long_opts)
265 return 1;
266
267 /*
268 * getopt_long requires "val" to be unique (since that is what the
269 * func returns), so generate unique values automatically for flags
270 * that don't have a short option. pick 0x100 as that is above the
271 * single byte range (where ASCII/ISO-XXXX-X charsets live).
272 */
273 hidden_short_opt = 0x100;
274 si = 0;
275 for (i = 0; i < num_options; ++i) {
276 long_opts[i].name = sb_opt[i]->flag;
277 long_opts[i].has_arg = sb_opt[i]->has_arg ?
278 required_argument : no_argument;
279 long_opts[i].flag = NULL;
280
281 if (sb_opt[i]->flag_short) {
282 short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
283 if (long_opts[i].has_arg == required_argument)
284 short_opts[si++] = ':';
285 } else
286 long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
287 }
288 short_opts[si] = '\0';
289
290 /* we need to handle output ourselves since u-boot provides printf */
291 opterr = 0;
292
293 /*
294 * walk all of the options the user gave us on the command line,
295 * figure out what u-boot option structure they belong to (via
296 * the unique short val key), and call the appropriate callback.
297 */
298 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
299 for (i = 0; i < num_options; ++i) {
300 if (sb_opt[i]->flag_short == c) {
301 if (sb_opt[i]->callback(state, optarg)) {
302 state->parse_err = sb_opt[i]->flag;
303 return 0;
304 }
305 break;
306 }
307 }
308 if (i == num_options) {
309 /*
310 * store the faulting flag for later display. we have to
311 * store the flag itself as the getopt parsing itself is
312 * tricky: need to handle the following flags (assume all
313 * of the below are unknown):
314 * -a optopt='a' optind=<next>
315 * -abbbb optopt='a' optind=<this>
316 * -aaaaa optopt='a' optind=<this>
317 * --a optopt=0 optind=<this>
318 * as you can see, it is impossible to determine the exact
319 * faulting flag without doing the parsing ourselves, so
320 * we just report the specific flag that failed.
321 */
322 if (optopt) {
323 static char parse_err[3] = { '-', 0, '\0', };
324 parse_err[1] = optopt;
325 state->parse_err = parse_err;
326 } else
327 state->parse_err = argv[optind - 1];
328 break;
329 }
330 }
331
332 return 0;
333}
Simon Glassf1c45c82012-12-26 09:53:34 +0000334
335void os_dirent_free(struct os_dirent_node *node)
336{
337 struct os_dirent_node *next;
338
339 while (node) {
340 next = node->next;
341 free(node);
342 node = next;
343 }
344}
345
346int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
347{
Stefan Brünsae71ede2016-10-01 20:41:42 +0200348 struct dirent *entry;
Simon Glassf1c45c82012-12-26 09:53:34 +0000349 struct os_dirent_node *head, *node, *next;
350 struct stat buf;
351 DIR *dir;
352 int ret;
353 char *fname;
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200354 char *old_fname;
Simon Glassf1c45c82012-12-26 09:53:34 +0000355 int len;
Stefan Brünsb2129902016-10-01 20:41:40 +0200356 int dirlen;
Simon Glassf1c45c82012-12-26 09:53:34 +0000357
358 *headp = NULL;
359 dir = opendir(dirname);
360 if (!dir)
361 return -1;
362
Stefan Brünsb2129902016-10-01 20:41:40 +0200363 /* Create a buffer upfront, with typically sufficient size */
364 dirlen = strlen(dirname) + 2;
365 len = dirlen + 256;
Simon Glassf1c45c82012-12-26 09:53:34 +0000366 fname = malloc(len);
367 if (!fname) {
368 ret = -ENOMEM;
369 goto done;
370 }
371
372 for (node = head = NULL;; node = next) {
Stefan Brünsae71ede2016-10-01 20:41:42 +0200373 errno = 0;
374 entry = readdir(dir);
375 if (!entry) {
376 ret = errno;
Simon Glassf1c45c82012-12-26 09:53:34 +0000377 break;
Stefan Brünsae71ede2016-10-01 20:41:42 +0200378 }
379 next = malloc(sizeof(*node) + strlen(entry->d_name) + 1);
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200380 if (!next) {
Simon Glassf1c45c82012-12-26 09:53:34 +0000381 os_dirent_free(head);
382 ret = -ENOMEM;
383 goto done;
384 }
Heinrich Schuchardtf53b5ea2017-09-21 12:56:07 +0200385 if (dirlen + strlen(entry->d_name) > len) {
386 len = dirlen + strlen(entry->d_name);
387 old_fname = fname;
388 fname = realloc(fname, len);
389 if (!fname) {
390 free(old_fname);
391 free(next);
392 os_dirent_free(head);
393 ret = -ENOMEM;
394 goto done;
395 }
396 }
Stephen Warren9671c672014-06-11 10:26:23 -0600397 next->next = NULL;
Stefan Brünsae71ede2016-10-01 20:41:42 +0200398 strcpy(next->name, entry->d_name);
399 switch (entry->d_type) {
Simon Glassf1c45c82012-12-26 09:53:34 +0000400 case DT_REG:
401 next->type = OS_FILET_REG;
402 break;
403 case DT_DIR:
404 next->type = OS_FILET_DIR;
405 break;
406 case DT_LNK:
407 next->type = OS_FILET_LNK;
408 break;
Stefan Brünsb7ffa822016-10-04 21:46:35 +0200409 default:
410 next->type = OS_FILET_UNKNOWN;
Simon Glassf1c45c82012-12-26 09:53:34 +0000411 }
412 next->size = 0;
413 snprintf(fname, len, "%s/%s", dirname, next->name);
414 if (!stat(fname, &buf))
415 next->size = buf.st_size;
416 if (node)
417 node->next = next;
Stefan Brünsd4cb8882016-10-01 20:41:39 +0200418 else
419 head = next;
Simon Glassf1c45c82012-12-26 09:53:34 +0000420 }
421 *headp = head;
422
423done:
424 closedir(dir);
Simon Glassc043e032014-11-11 12:47:08 -0700425 free(fname);
Simon Glassf1c45c82012-12-26 09:53:34 +0000426 return ret;
427}
428
429const char *os_dirent_typename[OS_FILET_COUNT] = {
430 " ",
431 "SYM",
432 "DIR",
433 "???",
434};
435
436const char *os_dirent_get_typename(enum os_dirent_t type)
437{
Tom Rinib6f605e2017-05-13 20:11:30 -0400438 if (type >= OS_FILET_REG && type < OS_FILET_COUNT)
Simon Glassf1c45c82012-12-26 09:53:34 +0000439 return os_dirent_typename[type];
440
441 return os_dirent_typename[OS_FILET_UNKNOWN];
442}
443
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800444int os_get_filesize(const char *fname, loff_t *size)
Simon Glassf1c45c82012-12-26 09:53:34 +0000445{
446 struct stat buf;
447 int ret;
448
449 ret = stat(fname, &buf);
450 if (ret)
451 return ret;
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800452 *size = buf.st_size;
453 return 0;
Simon Glassf1c45c82012-12-26 09:53:34 +0000454}
Simon Glass3e9fd242013-11-10 10:27:01 -0700455
Simon Glass29d11432017-12-04 13:48:17 -0700456void os_putc(int ch)
457{
458 putchar(ch);
459}
460
461void os_puts(const char *str)
462{
463 while (*str)
464 os_putc(*str++);
465}
466
Simon Glass9dd10bf2013-11-10 10:27:03 -0700467int os_write_ram_buf(const char *fname)
468{
469 struct sandbox_state *state = state_get_current();
470 int fd, ret;
471
472 fd = open(fname, O_CREAT | O_WRONLY, 0777);
473 if (fd < 0)
474 return -ENOENT;
475 ret = write(fd, state->ram_buf, state->ram_size);
476 close(fd);
477 if (ret != state->ram_size)
478 return -EIO;
479
480 return 0;
481}
482
483int os_read_ram_buf(const char *fname)
484{
485 struct sandbox_state *state = state_get_current();
486 int fd, ret;
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800487 loff_t size;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700488
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800489 ret = os_get_filesize(fname, &size);
490 if (ret < 0)
491 return ret;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700492 if (size != state->ram_size)
493 return -ENOSPC;
494 fd = open(fname, O_RDONLY);
495 if (fd < 0)
496 return -ENOENT;
497
498 ret = read(fd, state->ram_buf, state->ram_size);
499 close(fd);
500 if (ret != state->ram_size)
501 return -EIO;
502
503 return 0;
504}
Simon Glass860b7d92014-02-27 13:26:15 -0700505
506static int make_exec(char *fname, const void *data, int size)
507{
508 int fd;
509
510 strcpy(fname, "/tmp/u-boot.jump.XXXXXX");
511 fd = mkstemp(fname);
512 if (fd < 0)
513 return -ENOENT;
514 if (write(fd, data, size) < 0)
515 return -EIO;
516 close(fd);
517 if (chmod(fname, 0777))
518 return -ENOEXEC;
519
520 return 0;
521}
522
523static int add_args(char ***argvp, const char *add_args[], int count)
524{
525 char **argv;
526 int argc;
527
528 for (argv = *argvp, argc = 0; (*argvp)[argc]; argc++)
529 ;
530
531 argv = malloc((argc + count + 1) * sizeof(char *));
532 if (!argv) {
533 printf("Out of memory for %d argv\n", count);
534 return -ENOMEM;
535 }
536 memcpy(argv, *argvp, argc * sizeof(char *));
537 memcpy(argv + argc, add_args, count * sizeof(char *));
538 argv[argc + count] = NULL;
539
540 *argvp = argv;
541 return 0;
542}
543
544int os_jump_to_image(const void *dest, int size)
545{
546 struct sandbox_state *state = state_get_current();
547 char fname[30], mem_fname[30];
548 int fd, err;
Simon Glass47acfc62014-02-27 13:26:23 -0700549 const char *extra_args[5];
Simon Glass860b7d92014-02-27 13:26:15 -0700550 char **argv = state->argv;
551#ifdef DEBUG
552 int argc, i;
553#endif
554
555 err = make_exec(fname, dest, size);
556 if (err)
557 return err;
558
559 strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
560 fd = mkstemp(mem_fname);
561 if (fd < 0)
562 return -ENOENT;
563 close(fd);
564 err = os_write_ram_buf(mem_fname);
565 if (err)
566 return err;
567
568 os_fd_restore();
569
570 extra_args[0] = "-j";
571 extra_args[1] = fname;
572 extra_args[2] = "-m";
573 extra_args[3] = mem_fname;
Simon Glass47acfc62014-02-27 13:26:23 -0700574 extra_args[4] = "--rm_memory";
Simon Glass860b7d92014-02-27 13:26:15 -0700575 err = add_args(&argv, extra_args,
576 sizeof(extra_args) / sizeof(extra_args[0]));
577 if (err)
578 return err;
579
580#ifdef DEBUG
581 for (i = 0; argv[i]; i++)
582 printf("%d %s\n", i, argv[i]);
583#endif
584
585 if (state_uninit())
586 os_exit(2);
587
588 err = execv(fname, argv);
589 free(argv);
590 if (err)
591 return err;
592
593 return unlink(fname);
594}
Simon Glass504548f2015-04-20 12:37:22 -0600595
Simon Glass2c931ba2016-07-04 11:57:45 -0600596int os_find_u_boot(char *fname, int maxlen)
597{
598 struct sandbox_state *state = state_get_current();
599 const char *progname = state->argv[0];
600 int len = strlen(progname);
Simon Glassdc4d8eb2018-10-01 11:55:10 -0600601 const char *suffix;
Simon Glass2c931ba2016-07-04 11:57:45 -0600602 char *p;
603 int fd;
604
605 if (len >= maxlen || len < 4)
606 return -ENOSPC;
607
Simon Glass2c931ba2016-07-04 11:57:45 -0600608 strcpy(fname, progname);
Simon Glassdc4d8eb2018-10-01 11:55:10 -0600609 suffix = fname + len - 4;
610
611 /* If we are TPL, boot to SPL */
612 if (!strcmp(suffix, "-tpl")) {
613 fname[len - 3] = 's';
614 fd = os_open(fname, O_RDONLY);
615 if (fd >= 0) {
616 close(fd);
617 return 0;
618 }
619
620 /* Look for 'u-boot-tpl' in the tpl/ directory */
621 p = strstr(fname, "/tpl/");
622 if (p) {
623 p[1] = 's';
624 fd = os_open(fname, O_RDONLY);
625 if (fd >= 0) {
626 close(fd);
627 return 0;
628 }
629 }
630 return -ENOENT;
631 }
632
633 /* Look for 'u-boot' in the same directory as 'u-boot-spl' */
634 if (!strcmp(suffix, "-spl")) {
Simon Glass2c931ba2016-07-04 11:57:45 -0600635 fname[len - 4] = '\0';
636 fd = os_open(fname, O_RDONLY);
637 if (fd >= 0) {
638 close(fd);
639 return 0;
640 }
641 }
642
643 /* Look for 'u-boot' in the parent directory of spl/ */
644 p = strstr(fname, "/spl/");
645 if (p) {
646 strcpy(p, p + 4);
647 fd = os_open(fname, O_RDONLY);
648 if (fd >= 0) {
649 close(fd);
650 return 0;
651 }
652 }
653
654 return -ENOENT;
655}
656
657int os_spl_to_uboot(const char *fname)
658{
659 struct sandbox_state *state = state_get_current();
660 char *argv[state->argc + 1];
661 int ret;
662
663 memcpy(argv, state->argv, sizeof(char *) * (state->argc + 1));
664 argv[0] = (char *)fname;
665 ret = execv(fname, argv);
666 if (ret)
667 return ret;
668
669 return unlink(fname);
670}
671
Simon Glass504548f2015-04-20 12:37:22 -0600672void os_localtime(struct rtc_time *rt)
673{
674 time_t t = time(NULL);
675 struct tm *tm;
676
677 tm = localtime(&t);
678 rt->tm_sec = tm->tm_sec;
679 rt->tm_min = tm->tm_min;
680 rt->tm_hour = tm->tm_hour;
681 rt->tm_mday = tm->tm_mday;
682 rt->tm_mon = tm->tm_mon + 1;
683 rt->tm_year = tm->tm_year + 1900;
684 rt->tm_wday = tm->tm_wday;
685 rt->tm_yday = tm->tm_yday;
686 rt->tm_isdst = tm->tm_isdst;
687}
Simon Glass5dccd152018-05-16 09:42:22 -0600688
Simon Glassb7255ef2018-09-15 00:50:55 -0600689void os_abort(void)
690{
691 abort();
692}
Simon Glass4e766c22018-10-01 21:12:32 -0600693
694int os_mprotect_allow(void *start, size_t len)
695{
696 int page_size = getpagesize();
697
698 /* Move start to the start of a page, len to the end */
699 start = (void *)(((ulong)start) & ~(page_size - 1));
700 len = (len + page_size * 2) & ~(page_size - 1);
701
702 return mprotect(start, len, PROT_READ | PROT_WRITE);
703}