Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Heinrich Schuchardt | 28eb509 | 2020-11-12 00:29:56 +0100 | [diff] [blame] | 6 | #define _GNU_SOURCE |
| 7 | |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 8 | #include <dirent.h> |
Simon Glass | e8015a6 | 2012-01-10 15:54:05 -0800 | [diff] [blame] | 9 | #include <errno.h> |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 10 | #include <fcntl.h> |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 11 | #include <getopt.h> |
Simon Glass | 5dccd15 | 2018-05-16 09:42:22 -0600 | [diff] [blame] | 12 | #include <setjmp.h> |
Rasmus Villemoes | 2b72ad2 | 2020-02-14 10:58:37 +0000 | [diff] [blame] | 13 | #include <signal.h> |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
Simon Glass | fb4b4e8 | 2013-05-19 16:45:35 -0700 | [diff] [blame] | 15 | #include <stdint.h> |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 16 | #include <stdlib.h> |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 17 | #include <string.h> |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 18 | #include <termios.h> |
Matthias Weisser | 0d3dd14 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 19 | #include <time.h> |
Heinrich Schuchardt | 28eb509 | 2020-11-12 00:29:56 +0100 | [diff] [blame] | 20 | #include <ucontext.h> |
Simon Glass | e8015a6 | 2012-01-10 15:54:05 -0800 | [diff] [blame] | 21 | #include <unistd.h> |
Matthias Weisser | b5f7b47 | 2011-11-05 11:40:34 +0100 | [diff] [blame] | 22 | #include <sys/mman.h> |
Simon Glass | e8015a6 | 2012-01-10 15:54:05 -0800 | [diff] [blame] | 23 | #include <sys/stat.h> |
Simon Glass | 17064c0 | 2012-01-10 15:54:06 -0800 | [diff] [blame] | 24 | #include <sys/time.h> |
Simon Glass | e8015a6 | 2012-01-10 15:54:05 -0800 | [diff] [blame] | 25 | #include <sys/types.h> |
Heinrich Schuchardt | 28eb509 | 2020-11-12 00:29:56 +0100 | [diff] [blame] | 26 | #include <linux/compiler_attributes.h> |
Matthias Weisser | 0d3dd14 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 27 | #include <linux/types.h> |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 28 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 29 | #include <asm/getopt.h> |
| 30 | #include <asm/sections.h> |
| 31 | #include <asm/state.h> |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 32 | #include <os.h> |
Simon Glass | 504548f | 2015-04-20 12:37:22 -0600 | [diff] [blame] | 33 | #include <rtc_def.h> |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 34 | |
Heinrich Schuchardt | c0d1a00 | 2020-12-30 18:07:48 +0100 | [diff] [blame] | 35 | /* Environment variable for time offset */ |
| 36 | #define ENV_TIME_OFFSET "UBOOT_SB_TIME_OFFSET" |
| 37 | |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 38 | /* Operating System Interface */ |
| 39 | |
Simon Glass | 1e6594c | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 40 | struct os_mem_hdr { |
| 41 | size_t length; /* number of bytes in the block */ |
| 42 | }; |
| 43 | |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 44 | ssize_t os_read(int fd, void *buf, size_t count) |
| 45 | { |
| 46 | return read(fd, buf, count); |
| 47 | } |
| 48 | |
| 49 | ssize_t os_write(int fd, const void *buf, size_t count) |
| 50 | { |
| 51 | return write(fd, buf, count); |
| 52 | } |
| 53 | |
Mike Frysinger | 60addac | 2011-10-25 13:02:58 +0200 | [diff] [blame] | 54 | off_t os_lseek(int fd, off_t offset, int whence) |
| 55 | { |
| 56 | if (whence == OS_SEEK_SET) |
| 57 | whence = SEEK_SET; |
| 58 | else if (whence == OS_SEEK_CUR) |
| 59 | whence = SEEK_CUR; |
| 60 | else if (whence == OS_SEEK_END) |
| 61 | whence = SEEK_END; |
| 62 | else |
| 63 | os_exit(1); |
| 64 | return lseek(fd, offset, whence); |
| 65 | } |
| 66 | |
Simon Glass | 3196d75 | 2012-02-20 23:56:58 -0500 | [diff] [blame] | 67 | int os_open(const char *pathname, int os_flags) |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 68 | { |
Simon Glass | 3196d75 | 2012-02-20 23:56:58 -0500 | [diff] [blame] | 69 | int flags; |
| 70 | |
| 71 | switch (os_flags & OS_O_MASK) { |
| 72 | case OS_O_RDONLY: |
| 73 | default: |
| 74 | flags = O_RDONLY; |
| 75 | break; |
| 76 | |
| 77 | case OS_O_WRONLY: |
| 78 | flags = O_WRONLY; |
| 79 | break; |
| 80 | |
| 81 | case OS_O_RDWR: |
| 82 | flags = O_RDWR; |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | if (os_flags & OS_O_CREAT) |
| 87 | flags |= O_CREAT; |
Simon Glass | ce55a11 | 2018-10-01 11:55:07 -0600 | [diff] [blame] | 88 | if (os_flags & OS_O_TRUNC) |
| 89 | flags |= O_TRUNC; |
Heinrich Schuchardt | fc96df6 | 2020-10-27 20:29:24 +0100 | [diff] [blame] | 90 | /* |
| 91 | * During a cold reset execv() is used to relaunch the U-Boot binary. |
| 92 | * We must ensure that all files are closed in this case. |
| 93 | */ |
| 94 | flags |= O_CLOEXEC; |
Simon Glass | 3196d75 | 2012-02-20 23:56:58 -0500 | [diff] [blame] | 95 | |
| 96 | return open(pathname, flags, 0777); |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | int os_close(int fd) |
| 100 | { |
Heinrich Schuchardt | 69db2ee | 2020-10-27 20:29:21 +0100 | [diff] [blame] | 101 | /* Do not close the console input */ |
| 102 | if (fd) |
| 103 | return close(fd); |
| 104 | return -1; |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Stephen Warren | cd5edba | 2014-03-01 22:18:00 -0700 | [diff] [blame] | 107 | int os_unlink(const char *pathname) |
| 108 | { |
| 109 | return unlink(pathname); |
| 110 | } |
| 111 | |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 112 | void os_exit(int exit_code) |
| 113 | { |
| 114 | exit(exit_code); |
| 115 | } |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 116 | |
Simon Glass | 8d176d8 | 2018-11-06 15:21:25 -0700 | [diff] [blame] | 117 | int os_write_file(const char *fname, const void *buf, int size) |
Simon Glass | cbfa845 | 2018-10-01 11:55:08 -0600 | [diff] [blame] | 118 | { |
Simon Glass | cbfa845 | 2018-10-01 11:55:08 -0600 | [diff] [blame] | 119 | int fd; |
| 120 | |
| 121 | fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC); |
| 122 | if (fd < 0) { |
| 123 | printf("Cannot open file '%s'\n", fname); |
| 124 | return -EIO; |
| 125 | } |
| 126 | if (os_write(fd, buf, size) != size) { |
| 127 | printf("Cannot write to file '%s'\n", fname); |
Simon Glass | 8d176d8 | 2018-11-06 15:21:25 -0700 | [diff] [blame] | 128 | os_close(fd); |
Simon Glass | cbfa845 | 2018-10-01 11:55:08 -0600 | [diff] [blame] | 129 | return -EIO; |
| 130 | } |
| 131 | os_close(fd); |
Simon Glass | cbfa845 | 2018-10-01 11:55:08 -0600 | [diff] [blame] | 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Simon Glass | 8d176d8 | 2018-11-06 15:21:25 -0700 | [diff] [blame] | 136 | int os_read_file(const char *fname, void **bufp, int *sizep) |
| 137 | { |
| 138 | off_t size; |
| 139 | int ret = -EIO; |
| 140 | int fd; |
| 141 | |
| 142 | fd = os_open(fname, OS_O_RDONLY); |
| 143 | if (fd < 0) { |
| 144 | printf("Cannot open file '%s'\n", fname); |
| 145 | goto err; |
| 146 | } |
| 147 | size = os_lseek(fd, 0, OS_SEEK_END); |
| 148 | if (size < 0) { |
| 149 | printf("Cannot seek to end of file '%s'\n", fname); |
| 150 | goto err; |
| 151 | } |
| 152 | if (os_lseek(fd, 0, OS_SEEK_SET) < 0) { |
| 153 | printf("Cannot seek to start of file '%s'\n", fname); |
| 154 | goto err; |
| 155 | } |
Simon Glass | fa25226 | 2020-02-03 07:36:02 -0700 | [diff] [blame] | 156 | *bufp = malloc(size); |
Simon Glass | 8d176d8 | 2018-11-06 15:21:25 -0700 | [diff] [blame] | 157 | if (!*bufp) { |
| 158 | printf("Not enough memory to read file '%s'\n", fname); |
| 159 | ret = -ENOMEM; |
| 160 | goto err; |
| 161 | } |
| 162 | if (os_read(fd, *bufp, size) != size) { |
| 163 | printf("Cannot read from file '%s'\n", fname); |
| 164 | goto err; |
| 165 | } |
| 166 | os_close(fd); |
| 167 | *sizep = size; |
| 168 | |
| 169 | return 0; |
| 170 | err: |
| 171 | os_close(fd); |
| 172 | return ret; |
| 173 | } |
| 174 | |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 175 | /* Restore tty state when we exit */ |
| 176 | static struct termios orig_term; |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 177 | static bool term_setup; |
Simon Glass | ae50ec7 | 2018-10-01 11:55:20 -0600 | [diff] [blame] | 178 | static bool term_nonblock; |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 179 | |
Simon Glass | 9c3b7d6 | 2015-05-10 21:07:27 -0600 | [diff] [blame] | 180 | void os_fd_restore(void) |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 181 | { |
Simon Glass | 9c3b7d6 | 2015-05-10 21:07:27 -0600 | [diff] [blame] | 182 | if (term_setup) { |
Simon Glass | ae50ec7 | 2018-10-01 11:55:20 -0600 | [diff] [blame] | 183 | int flags; |
| 184 | |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 185 | tcsetattr(0, TCSANOW, &orig_term); |
Simon Glass | ae50ec7 | 2018-10-01 11:55:20 -0600 | [diff] [blame] | 186 | if (term_nonblock) { |
| 187 | flags = fcntl(0, F_GETFL, 0); |
| 188 | fcntl(0, F_SETFL, flags & ~O_NONBLOCK); |
| 189 | } |
Simon Glass | 9c3b7d6 | 2015-05-10 21:07:27 -0600 | [diff] [blame] | 190 | term_setup = false; |
| 191 | } |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Rasmus Villemoes | 2b72ad2 | 2020-02-14 10:58:37 +0000 | [diff] [blame] | 194 | static void os_sigint_handler(int sig) |
| 195 | { |
| 196 | os_fd_restore(); |
| 197 | signal(SIGINT, SIG_DFL); |
| 198 | raise(SIGINT); |
| 199 | } |
| 200 | |
Heinrich Schuchardt | 28eb509 | 2020-11-12 00:29:56 +0100 | [diff] [blame] | 201 | static void os_signal_handler(int sig, siginfo_t *info, void *con) |
| 202 | { |
| 203 | ucontext_t __maybe_unused *context = con; |
| 204 | unsigned long pc; |
| 205 | |
| 206 | #if defined(__x86_64__) |
| 207 | pc = context->uc_mcontext.gregs[REG_RIP]; |
| 208 | #elif defined(__aarch64__) |
| 209 | pc = context->uc_mcontext.pc; |
| 210 | #elif defined(__riscv) |
| 211 | pc = context->uc_mcontext.__gregs[REG_PC]; |
| 212 | #else |
| 213 | const char msg[] = |
| 214 | "\nUnsupported architecture, cannot read program counter\n"; |
| 215 | |
| 216 | os_write(1, msg, sizeof(msg)); |
| 217 | pc = 0; |
| 218 | #endif |
| 219 | |
| 220 | os_signal_action(sig, pc); |
| 221 | } |
| 222 | |
| 223 | int os_setup_signal_handlers(void) |
| 224 | { |
| 225 | struct sigaction act; |
| 226 | |
| 227 | act.sa_sigaction = os_signal_handler; |
| 228 | sigemptyset(&act.sa_mask); |
| 229 | act.sa_flags = SA_SIGINFO | SA_NODEFER; |
| 230 | if (sigaction(SIGILL, &act, NULL) || |
| 231 | sigaction(SIGBUS, &act, NULL) || |
| 232 | sigaction(SIGSEGV, &act, NULL)) |
| 233 | return -1; |
| 234 | return 0; |
| 235 | } |
| 236 | |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 237 | /* Put tty into raw mode so <tab> and <ctrl+c> work */ |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 238 | void os_tty_raw(int fd, bool allow_sigs) |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 239 | { |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 240 | struct termios term; |
Simon Glass | ae50ec7 | 2018-10-01 11:55:20 -0600 | [diff] [blame] | 241 | int flags; |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 242 | |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 243 | if (term_setup) |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 244 | return; |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 245 | |
| 246 | /* If not a tty, don't complain */ |
| 247 | if (tcgetattr(fd, &orig_term)) |
| 248 | return; |
| 249 | |
| 250 | term = orig_term; |
| 251 | term.c_iflag = IGNBRK | IGNPAR; |
| 252 | term.c_oflag = OPOST | ONLCR; |
| 253 | term.c_cflag = CS8 | CREAD | CLOCAL; |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 254 | term.c_lflag = allow_sigs ? ISIG : 0; |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 255 | if (tcsetattr(fd, TCSANOW, &term)) |
| 256 | return; |
| 257 | |
Simon Glass | ae50ec7 | 2018-10-01 11:55:20 -0600 | [diff] [blame] | 258 | flags = fcntl(fd, F_GETFL, 0); |
| 259 | if (!(flags & O_NONBLOCK)) { |
| 260 | if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) |
| 261 | return; |
| 262 | term_nonblock = true; |
| 263 | } |
| 264 | |
Simon Glass | 9c3b7d6 | 2015-05-10 21:07:27 -0600 | [diff] [blame] | 265 | term_setup = true; |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 266 | atexit(os_fd_restore); |
Rasmus Villemoes | 2b72ad2 | 2020-02-14 10:58:37 +0000 | [diff] [blame] | 267 | signal(SIGINT, os_sigint_handler); |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 268 | } |
Matthias Weisser | b5f7b47 | 2011-11-05 11:40:34 +0100 | [diff] [blame] | 269 | |
Simon Glass | 4c902fa | 2021-02-06 09:57:32 -0700 | [diff] [blame^] | 270 | /* |
| 271 | * Provide our own malloc so we don't use space in the sandbox ram_buf for |
| 272 | * allocations that are internal to sandbox, or need to be done before U-Boot's |
| 273 | * malloc() is ready. |
| 274 | */ |
Matthias Weisser | b5f7b47 | 2011-11-05 11:40:34 +0100 | [diff] [blame] | 275 | void *os_malloc(size_t length) |
| 276 | { |
Simon Glass | be005d8 | 2018-09-15 00:50:54 -0600 | [diff] [blame] | 277 | int page_size = getpagesize(); |
Simon Glass | fc2dde8 | 2019-04-08 13:20:42 -0600 | [diff] [blame] | 278 | struct os_mem_hdr *hdr; |
Simon Glass | 1e6594c | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 279 | |
Simon Glass | 4c902fa | 2021-02-06 09:57:32 -0700 | [diff] [blame^] | 280 | if (!length) |
| 281 | return NULL; |
Simon Glass | 57ba942 | 2018-06-17 08:57:43 -0600 | [diff] [blame] | 282 | /* |
| 283 | * Use an address that is hopefully available to us so that pointers |
| 284 | * to this memory are fairly obvious. If we end up with a different |
| 285 | * address, that's fine too. |
| 286 | */ |
| 287 | hdr = mmap((void *)0x10000000, length + page_size, |
Alexander Graf | 934a545 | 2018-06-22 14:44:13 +0200 | [diff] [blame] | 288 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 289 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Simon Glass | 1e6594c | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 290 | if (hdr == MAP_FAILED) |
| 291 | return NULL; |
| 292 | hdr->length = length; |
| 293 | |
Simon Glass | be005d8 | 2018-09-15 00:50:54 -0600 | [diff] [blame] | 294 | return (void *)hdr + page_size; |
Simon Glass | 1e6594c | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Masahiro Yamada | ee95728 | 2014-01-15 13:06:41 +0900 | [diff] [blame] | 297 | void os_free(void *ptr) |
Simon Glass | 1e6594c | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 298 | { |
Simon Glass | fc2dde8 | 2019-04-08 13:20:42 -0600 | [diff] [blame] | 299 | int page_size = getpagesize(); |
| 300 | struct os_mem_hdr *hdr; |
Simon Glass | 1e6594c | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 301 | |
Simon Glass | fc2dde8 | 2019-04-08 13:20:42 -0600 | [diff] [blame] | 302 | if (ptr) { |
| 303 | hdr = ptr - page_size; |
| 304 | munmap(hdr, hdr->length + page_size); |
| 305 | } |
Simon Glass | 4c902fa | 2021-02-06 09:57:32 -0700 | [diff] [blame^] | 306 | } |
| 307 | |
| 308 | /* These macros are from kernel.h but not accessible in this file */ |
| 309 | #define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a) - 1) |
| 310 | #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) |
| 311 | |
| 312 | /* |
| 313 | * Provide our own malloc so we don't use space in the sandbox ram_buf for |
| 314 | * allocations that are internal to sandbox, or need to be done before U-Boot's |
| 315 | * malloc() is ready. |
| 316 | */ |
| 317 | void *os_realloc(void *ptr, size_t length) |
| 318 | { |
| 319 | int page_size = getpagesize(); |
| 320 | struct os_mem_hdr *hdr; |
| 321 | void *new_ptr; |
| 322 | |
| 323 | /* Reallocating a NULL pointer is just an alloc */ |
| 324 | if (!ptr) |
| 325 | return os_malloc(length); |
| 326 | |
| 327 | /* Changing a length to 0 is just a free */ |
| 328 | if (length) { |
| 329 | os_free(ptr); |
| 330 | return NULL; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * If the new size is the same number of pages as the old, nothing to |
| 335 | * do. There isn't much point in shrinking things |
| 336 | */ |
| 337 | hdr = ptr - page_size; |
| 338 | if (ALIGN(length, page_size) <= ALIGN(hdr->length, page_size)) |
| 339 | return ptr; |
| 340 | |
| 341 | /* We have to grow it, so allocate something new */ |
| 342 | new_ptr = os_malloc(length); |
| 343 | memcpy(new_ptr, ptr, hdr->length); |
| 344 | os_free(ptr); |
| 345 | |
| 346 | return new_ptr; |
Simon Glass | 1e6594c | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Matthias Weisser | 0d3dd14 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 349 | void os_usleep(unsigned long usec) |
| 350 | { |
| 351 | usleep(usec); |
| 352 | } |
| 353 | |
Simon Glass | fb4b4e8 | 2013-05-19 16:45:35 -0700 | [diff] [blame] | 354 | uint64_t __attribute__((no_instrument_function)) os_get_nsec(void) |
Matthias Weisser | 0d3dd14 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 355 | { |
| 356 | #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK) |
| 357 | struct timespec tp; |
| 358 | if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) { |
| 359 | struct timeval tv; |
| 360 | |
| 361 | gettimeofday(&tv, NULL); |
| 362 | tp.tv_sec = tv.tv_sec; |
| 363 | tp.tv_nsec = tv.tv_usec * 1000; |
| 364 | } |
| 365 | return tp.tv_sec * 1000000000ULL + tp.tv_nsec; |
| 366 | #else |
| 367 | struct timeval tv; |
| 368 | gettimeofday(&tv, NULL); |
| 369 | return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000; |
| 370 | #endif |
| 371 | } |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 372 | |
| 373 | static char *short_opts; |
| 374 | static struct option *long_opts; |
| 375 | |
| 376 | int os_parse_args(struct sandbox_state *state, int argc, char *argv[]) |
| 377 | { |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 378 | struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start; |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 379 | size_t num_options = __u_boot_sandbox_option_count(); |
| 380 | size_t i; |
| 381 | |
| 382 | int hidden_short_opt; |
| 383 | size_t si; |
| 384 | |
| 385 | int c; |
| 386 | |
| 387 | if (short_opts || long_opts) |
| 388 | return 1; |
| 389 | |
| 390 | state->argc = argc; |
| 391 | state->argv = argv; |
| 392 | |
| 393 | /* dynamically construct the arguments to the system getopt_long */ |
Simon Glass | fa25226 | 2020-02-03 07:36:02 -0700 | [diff] [blame] | 394 | short_opts = malloc(sizeof(*short_opts) * num_options * 2 + 1); |
Simon Glass | 0a4eabb | 2020-02-03 07:36:04 -0700 | [diff] [blame] | 395 | long_opts = malloc(sizeof(*long_opts) * (num_options + 1)); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 396 | if (!short_opts || !long_opts) |
| 397 | return 1; |
| 398 | |
| 399 | /* |
| 400 | * getopt_long requires "val" to be unique (since that is what the |
| 401 | * func returns), so generate unique values automatically for flags |
| 402 | * that don't have a short option. pick 0x100 as that is above the |
| 403 | * single byte range (where ASCII/ISO-XXXX-X charsets live). |
| 404 | */ |
| 405 | hidden_short_opt = 0x100; |
| 406 | si = 0; |
| 407 | for (i = 0; i < num_options; ++i) { |
| 408 | long_opts[i].name = sb_opt[i]->flag; |
| 409 | long_opts[i].has_arg = sb_opt[i]->has_arg ? |
| 410 | required_argument : no_argument; |
| 411 | long_opts[i].flag = NULL; |
| 412 | |
| 413 | if (sb_opt[i]->flag_short) { |
| 414 | short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short; |
| 415 | if (long_opts[i].has_arg == required_argument) |
| 416 | short_opts[si++] = ':'; |
| 417 | } else |
| 418 | long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++; |
| 419 | } |
| 420 | short_opts[si] = '\0'; |
| 421 | |
| 422 | /* we need to handle output ourselves since u-boot provides printf */ |
| 423 | opterr = 0; |
| 424 | |
Simon Glass | 0a4eabb | 2020-02-03 07:36:04 -0700 | [diff] [blame] | 425 | memset(&long_opts[num_options], '\0', sizeof(*long_opts)); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 426 | /* |
| 427 | * walk all of the options the user gave us on the command line, |
| 428 | * figure out what u-boot option structure they belong to (via |
| 429 | * the unique short val key), and call the appropriate callback. |
| 430 | */ |
| 431 | while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { |
| 432 | for (i = 0; i < num_options; ++i) { |
| 433 | if (sb_opt[i]->flag_short == c) { |
| 434 | if (sb_opt[i]->callback(state, optarg)) { |
| 435 | state->parse_err = sb_opt[i]->flag; |
| 436 | return 0; |
| 437 | } |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | if (i == num_options) { |
| 442 | /* |
| 443 | * store the faulting flag for later display. we have to |
| 444 | * store the flag itself as the getopt parsing itself is |
| 445 | * tricky: need to handle the following flags (assume all |
| 446 | * of the below are unknown): |
| 447 | * -a optopt='a' optind=<next> |
| 448 | * -abbbb optopt='a' optind=<this> |
| 449 | * -aaaaa optopt='a' optind=<this> |
| 450 | * --a optopt=0 optind=<this> |
| 451 | * as you can see, it is impossible to determine the exact |
| 452 | * faulting flag without doing the parsing ourselves, so |
| 453 | * we just report the specific flag that failed. |
| 454 | */ |
| 455 | if (optopt) { |
| 456 | static char parse_err[3] = { '-', 0, '\0', }; |
| 457 | parse_err[1] = optopt; |
| 458 | state->parse_err = parse_err; |
| 459 | } else |
| 460 | state->parse_err = argv[optind - 1]; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | return 0; |
| 466 | } |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 467 | |
| 468 | void os_dirent_free(struct os_dirent_node *node) |
| 469 | { |
| 470 | struct os_dirent_node *next; |
| 471 | |
| 472 | while (node) { |
| 473 | next = node->next; |
Simon Glass | fa25226 | 2020-02-03 07:36:02 -0700 | [diff] [blame] | 474 | free(node); |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 475 | node = next; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | int os_dirent_ls(const char *dirname, struct os_dirent_node **headp) |
| 480 | { |
Stefan Brüns | ae71ede | 2016-10-01 20:41:42 +0200 | [diff] [blame] | 481 | struct dirent *entry; |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 482 | struct os_dirent_node *head, *node, *next; |
| 483 | struct stat buf; |
| 484 | DIR *dir; |
| 485 | int ret; |
| 486 | char *fname; |
Heinrich Schuchardt | f53b5ea | 2017-09-21 12:56:07 +0200 | [diff] [blame] | 487 | char *old_fname; |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 488 | int len; |
Stefan Brüns | b212990 | 2016-10-01 20:41:40 +0200 | [diff] [blame] | 489 | int dirlen; |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 490 | |
| 491 | *headp = NULL; |
| 492 | dir = opendir(dirname); |
| 493 | if (!dir) |
| 494 | return -1; |
| 495 | |
Stefan Brüns | b212990 | 2016-10-01 20:41:40 +0200 | [diff] [blame] | 496 | /* Create a buffer upfront, with typically sufficient size */ |
| 497 | dirlen = strlen(dirname) + 2; |
| 498 | len = dirlen + 256; |
Simon Glass | fa25226 | 2020-02-03 07:36:02 -0700 | [diff] [blame] | 499 | fname = malloc(len); |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 500 | if (!fname) { |
| 501 | ret = -ENOMEM; |
| 502 | goto done; |
| 503 | } |
| 504 | |
| 505 | for (node = head = NULL;; node = next) { |
Stefan Brüns | ae71ede | 2016-10-01 20:41:42 +0200 | [diff] [blame] | 506 | errno = 0; |
| 507 | entry = readdir(dir); |
| 508 | if (!entry) { |
| 509 | ret = errno; |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 510 | break; |
Stefan Brüns | ae71ede | 2016-10-01 20:41:42 +0200 | [diff] [blame] | 511 | } |
Simon Glass | fa25226 | 2020-02-03 07:36:02 -0700 | [diff] [blame] | 512 | next = malloc(sizeof(*node) + strlen(entry->d_name) + 1); |
Heinrich Schuchardt | f53b5ea | 2017-09-21 12:56:07 +0200 | [diff] [blame] | 513 | if (!next) { |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 514 | os_dirent_free(head); |
| 515 | ret = -ENOMEM; |
| 516 | goto done; |
| 517 | } |
Heinrich Schuchardt | f53b5ea | 2017-09-21 12:56:07 +0200 | [diff] [blame] | 518 | if (dirlen + strlen(entry->d_name) > len) { |
| 519 | len = dirlen + strlen(entry->d_name); |
| 520 | old_fname = fname; |
Simon Glass | fa25226 | 2020-02-03 07:36:02 -0700 | [diff] [blame] | 521 | fname = realloc(fname, len); |
Heinrich Schuchardt | f53b5ea | 2017-09-21 12:56:07 +0200 | [diff] [blame] | 522 | if (!fname) { |
Simon Glass | fa25226 | 2020-02-03 07:36:02 -0700 | [diff] [blame] | 523 | free(old_fname); |
| 524 | free(next); |
Heinrich Schuchardt | f53b5ea | 2017-09-21 12:56:07 +0200 | [diff] [blame] | 525 | os_dirent_free(head); |
| 526 | ret = -ENOMEM; |
| 527 | goto done; |
| 528 | } |
| 529 | } |
Stephen Warren | 9671c67 | 2014-06-11 10:26:23 -0600 | [diff] [blame] | 530 | next->next = NULL; |
Stefan Brüns | ae71ede | 2016-10-01 20:41:42 +0200 | [diff] [blame] | 531 | strcpy(next->name, entry->d_name); |
| 532 | switch (entry->d_type) { |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 533 | case DT_REG: |
| 534 | next->type = OS_FILET_REG; |
| 535 | break; |
| 536 | case DT_DIR: |
| 537 | next->type = OS_FILET_DIR; |
| 538 | break; |
| 539 | case DT_LNK: |
| 540 | next->type = OS_FILET_LNK; |
| 541 | break; |
Stefan Brüns | b7ffa82 | 2016-10-04 21:46:35 +0200 | [diff] [blame] | 542 | default: |
| 543 | next->type = OS_FILET_UNKNOWN; |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 544 | } |
| 545 | next->size = 0; |
| 546 | snprintf(fname, len, "%s/%s", dirname, next->name); |
| 547 | if (!stat(fname, &buf)) |
| 548 | next->size = buf.st_size; |
| 549 | if (node) |
| 550 | node->next = next; |
Stefan Brüns | d4cb888 | 2016-10-01 20:41:39 +0200 | [diff] [blame] | 551 | else |
| 552 | head = next; |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 553 | } |
| 554 | *headp = head; |
| 555 | |
| 556 | done: |
| 557 | closedir(dir); |
Simon Glass | fa25226 | 2020-02-03 07:36:02 -0700 | [diff] [blame] | 558 | free(fname); |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | const char *os_dirent_typename[OS_FILET_COUNT] = { |
| 563 | " ", |
| 564 | "SYM", |
| 565 | "DIR", |
| 566 | "???", |
| 567 | }; |
| 568 | |
| 569 | const char *os_dirent_get_typename(enum os_dirent_t type) |
| 570 | { |
Tom Rini | b6f605e | 2017-05-13 20:11:30 -0400 | [diff] [blame] | 571 | if (type >= OS_FILET_REG && type < OS_FILET_COUNT) |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 572 | return os_dirent_typename[type]; |
| 573 | |
| 574 | return os_dirent_typename[OS_FILET_UNKNOWN]; |
| 575 | } |
| 576 | |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 577 | int os_get_filesize(const char *fname, loff_t *size) |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 578 | { |
| 579 | struct stat buf; |
| 580 | int ret; |
| 581 | |
| 582 | ret = stat(fname, &buf); |
| 583 | if (ret) |
| 584 | return ret; |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 585 | *size = buf.st_size; |
| 586 | return 0; |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 587 | } |
Simon Glass | 3e9fd24 | 2013-11-10 10:27:01 -0700 | [diff] [blame] | 588 | |
Simon Glass | 29d1143 | 2017-12-04 13:48:17 -0700 | [diff] [blame] | 589 | void os_putc(int ch) |
| 590 | { |
| 591 | putchar(ch); |
| 592 | } |
| 593 | |
| 594 | void os_puts(const char *str) |
| 595 | { |
| 596 | while (*str) |
| 597 | os_putc(*str++); |
| 598 | } |
| 599 | |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 600 | int os_write_ram_buf(const char *fname) |
| 601 | { |
| 602 | struct sandbox_state *state = state_get_current(); |
| 603 | int fd, ret; |
| 604 | |
| 605 | fd = open(fname, O_CREAT | O_WRONLY, 0777); |
| 606 | if (fd < 0) |
| 607 | return -ENOENT; |
| 608 | ret = write(fd, state->ram_buf, state->ram_size); |
| 609 | close(fd); |
| 610 | if (ret != state->ram_size) |
| 611 | return -EIO; |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | int os_read_ram_buf(const char *fname) |
| 617 | { |
| 618 | struct sandbox_state *state = state_get_current(); |
| 619 | int fd, ret; |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 620 | loff_t size; |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 621 | |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 622 | ret = os_get_filesize(fname, &size); |
| 623 | if (ret < 0) |
| 624 | return ret; |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 625 | if (size != state->ram_size) |
| 626 | return -ENOSPC; |
| 627 | fd = open(fname, O_RDONLY); |
| 628 | if (fd < 0) |
| 629 | return -ENOENT; |
| 630 | |
| 631 | ret = read(fd, state->ram_buf, state->ram_size); |
| 632 | close(fd); |
| 633 | if (ret != state->ram_size) |
| 634 | return -EIO; |
| 635 | |
| 636 | return 0; |
| 637 | } |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 638 | |
| 639 | static int make_exec(char *fname, const void *data, int size) |
| 640 | { |
| 641 | int fd; |
| 642 | |
| 643 | strcpy(fname, "/tmp/u-boot.jump.XXXXXX"); |
| 644 | fd = mkstemp(fname); |
| 645 | if (fd < 0) |
| 646 | return -ENOENT; |
| 647 | if (write(fd, data, size) < 0) |
| 648 | return -EIO; |
| 649 | close(fd); |
| 650 | if (chmod(fname, 0777)) |
| 651 | return -ENOEXEC; |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
Simon Glass | 7dafd02 | 2018-11-15 18:44:05 -0700 | [diff] [blame] | 656 | /** |
| 657 | * add_args() - Allocate a new argv with the given args |
| 658 | * |
| 659 | * This is used to create a new argv array with all the old arguments and some |
| 660 | * new ones that are passed in |
| 661 | * |
| 662 | * @argvp: Returns newly allocated args list |
| 663 | * @add_args: Arguments to add, each a string |
| 664 | * @count: Number of arguments in @add_args |
| 665 | * @return 0 if OK, -ENOMEM if out of memory |
| 666 | */ |
| 667 | static int add_args(char ***argvp, char *add_args[], int count) |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 668 | { |
Simon Glass | 7465f00 | 2018-11-15 18:44:07 -0700 | [diff] [blame] | 669 | char **argv, **ap; |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 670 | int argc; |
| 671 | |
Simon Glass | 7465f00 | 2018-11-15 18:44:07 -0700 | [diff] [blame] | 672 | for (argc = 0; (*argvp)[argc]; argc++) |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 673 | ; |
| 674 | |
Simon Glass | fa25226 | 2020-02-03 07:36:02 -0700 | [diff] [blame] | 675 | argv = malloc((argc + count + 1) * sizeof(char *)); |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 676 | if (!argv) { |
| 677 | printf("Out of memory for %d argv\n", count); |
| 678 | return -ENOMEM; |
| 679 | } |
Simon Glass | 7465f00 | 2018-11-15 18:44:07 -0700 | [diff] [blame] | 680 | for (ap = *argvp, argc = 0; *ap; ap++) { |
| 681 | char *arg = *ap; |
| 682 | |
| 683 | /* Drop args that we don't want to propagate */ |
| 684 | if (*arg == '-' && strlen(arg) == 2) { |
| 685 | switch (arg[1]) { |
| 686 | case 'j': |
| 687 | case 'm': |
| 688 | ap++; |
| 689 | continue; |
| 690 | } |
| 691 | } else if (!strcmp(arg, "--rm_memory")) { |
| 692 | ap++; |
| 693 | continue; |
| 694 | } |
| 695 | argv[argc++] = arg; |
| 696 | } |
| 697 | |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 698 | memcpy(argv + argc, add_args, count * sizeof(char *)); |
| 699 | argv[argc + count] = NULL; |
| 700 | |
| 701 | *argvp = argv; |
| 702 | return 0; |
| 703 | } |
| 704 | |
Simon Glass | 7dafd02 | 2018-11-15 18:44:05 -0700 | [diff] [blame] | 705 | /** |
| 706 | * os_jump_to_file() - Jump to a new program |
| 707 | * |
| 708 | * This saves the memory buffer, sets up arguments to the new process, then |
| 709 | * execs it. |
| 710 | * |
| 711 | * @fname: Filename to exec |
| 712 | * @return does not return on success, any return value is an error |
| 713 | */ |
| 714 | static int os_jump_to_file(const char *fname) |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 715 | { |
| 716 | struct sandbox_state *state = state_get_current(); |
Simon Glass | 7dafd02 | 2018-11-15 18:44:05 -0700 | [diff] [blame] | 717 | char mem_fname[30]; |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 718 | int fd, err; |
Simon Glass | 7dafd02 | 2018-11-15 18:44:05 -0700 | [diff] [blame] | 719 | char *extra_args[5]; |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 720 | char **argv = state->argv; |
Simon Glass | 7465f00 | 2018-11-15 18:44:07 -0700 | [diff] [blame] | 721 | int argc; |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 722 | #ifdef DEBUG |
Simon Glass | 7dafd02 | 2018-11-15 18:44:05 -0700 | [diff] [blame] | 723 | int i; |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 724 | #endif |
| 725 | |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 726 | strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX"); |
| 727 | fd = mkstemp(mem_fname); |
| 728 | if (fd < 0) |
| 729 | return -ENOENT; |
| 730 | close(fd); |
| 731 | err = os_write_ram_buf(mem_fname); |
| 732 | if (err) |
| 733 | return err; |
| 734 | |
| 735 | os_fd_restore(); |
| 736 | |
| 737 | extra_args[0] = "-j"; |
Simon Glass | 7dafd02 | 2018-11-15 18:44:05 -0700 | [diff] [blame] | 738 | extra_args[1] = (char *)fname; |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 739 | extra_args[2] = "-m"; |
| 740 | extra_args[3] = mem_fname; |
Simon Glass | 7465f00 | 2018-11-15 18:44:07 -0700 | [diff] [blame] | 741 | argc = 4; |
| 742 | if (state->ram_buf_rm) |
| 743 | extra_args[argc++] = "--rm_memory"; |
| 744 | err = add_args(&argv, extra_args, argc); |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 745 | if (err) |
| 746 | return err; |
Simon Glass | 7465f00 | 2018-11-15 18:44:07 -0700 | [diff] [blame] | 747 | argv[0] = (char *)fname; |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 748 | |
| 749 | #ifdef DEBUG |
| 750 | for (i = 0; argv[i]; i++) |
| 751 | printf("%d %s\n", i, argv[i]); |
| 752 | #endif |
| 753 | |
| 754 | if (state_uninit()) |
| 755 | os_exit(2); |
| 756 | |
| 757 | err = execv(fname, argv); |
Simon Glass | fa25226 | 2020-02-03 07:36:02 -0700 | [diff] [blame] | 758 | free(argv); |
Simon Glass | cca2552 | 2018-11-15 18:44:08 -0700 | [diff] [blame] | 759 | if (err) { |
| 760 | perror("Unable to run image"); |
Simon Glass | 23eabbb | 2018-11-23 21:29:24 -0700 | [diff] [blame] | 761 | printf("Image filename '%s'\n", fname); |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 762 | return err; |
Simon Glass | cca2552 | 2018-11-15 18:44:08 -0700 | [diff] [blame] | 763 | } |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 764 | |
| 765 | return unlink(fname); |
| 766 | } |
Simon Glass | 504548f | 2015-04-20 12:37:22 -0600 | [diff] [blame] | 767 | |
Simon Glass | 7dafd02 | 2018-11-15 18:44:05 -0700 | [diff] [blame] | 768 | int os_jump_to_image(const void *dest, int size) |
| 769 | { |
| 770 | char fname[30]; |
| 771 | int err; |
| 772 | |
| 773 | err = make_exec(fname, dest, size); |
| 774 | if (err) |
| 775 | return err; |
| 776 | |
| 777 | return os_jump_to_file(fname); |
| 778 | } |
| 779 | |
Simon Glass | 2c931ba | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 780 | int os_find_u_boot(char *fname, int maxlen) |
| 781 | { |
| 782 | struct sandbox_state *state = state_get_current(); |
| 783 | const char *progname = state->argv[0]; |
| 784 | int len = strlen(progname); |
Simon Glass | dc4d8eb | 2018-10-01 11:55:10 -0600 | [diff] [blame] | 785 | const char *suffix; |
Simon Glass | 2c931ba | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 786 | char *p; |
| 787 | int fd; |
| 788 | |
| 789 | if (len >= maxlen || len < 4) |
| 790 | return -ENOSPC; |
| 791 | |
Simon Glass | 2c931ba | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 792 | strcpy(fname, progname); |
Simon Glass | dc4d8eb | 2018-10-01 11:55:10 -0600 | [diff] [blame] | 793 | suffix = fname + len - 4; |
| 794 | |
| 795 | /* If we are TPL, boot to SPL */ |
| 796 | if (!strcmp(suffix, "-tpl")) { |
| 797 | fname[len - 3] = 's'; |
| 798 | fd = os_open(fname, O_RDONLY); |
| 799 | if (fd >= 0) { |
| 800 | close(fd); |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | /* Look for 'u-boot-tpl' in the tpl/ directory */ |
| 805 | p = strstr(fname, "/tpl/"); |
| 806 | if (p) { |
| 807 | p[1] = 's'; |
| 808 | fd = os_open(fname, O_RDONLY); |
| 809 | if (fd >= 0) { |
| 810 | close(fd); |
| 811 | return 0; |
| 812 | } |
| 813 | } |
| 814 | return -ENOENT; |
| 815 | } |
| 816 | |
| 817 | /* Look for 'u-boot' in the same directory as 'u-boot-spl' */ |
| 818 | if (!strcmp(suffix, "-spl")) { |
Simon Glass | 2c931ba | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 819 | fname[len - 4] = '\0'; |
| 820 | fd = os_open(fname, O_RDONLY); |
| 821 | if (fd >= 0) { |
| 822 | close(fd); |
| 823 | return 0; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | /* Look for 'u-boot' in the parent directory of spl/ */ |
Simon Glass | f2af76b | 2018-11-13 15:55:20 -0700 | [diff] [blame] | 828 | p = strstr(fname, "spl/"); |
Simon Glass | 2c931ba | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 829 | if (p) { |
Simon Glass | f2af76b | 2018-11-13 15:55:20 -0700 | [diff] [blame] | 830 | /* Remove the "spl" characters */ |
| 831 | memmove(p, p + 4, strlen(p + 4) + 1); |
Simon Glass | 2c931ba | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 832 | fd = os_open(fname, O_RDONLY); |
| 833 | if (fd >= 0) { |
| 834 | close(fd); |
| 835 | return 0; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | return -ENOENT; |
| 840 | } |
| 841 | |
| 842 | int os_spl_to_uboot(const char *fname) |
| 843 | { |
Patrick Delaunay | 6daf905 | 2020-11-20 09:48:33 +0100 | [diff] [blame] | 844 | struct sandbox_state *state = state_get_current(); |
| 845 | |
| 846 | printf("%s\n", __func__); |
| 847 | /* U-Boot will delete ram buffer after read: "--rm_memory"*/ |
| 848 | state->ram_buf_rm = true; |
Simon Glass | cca2552 | 2018-11-15 18:44:08 -0700 | [diff] [blame] | 849 | return os_jump_to_file(fname); |
Simon Glass | 2c931ba | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 850 | } |
| 851 | |
Heinrich Schuchardt | c0d1a00 | 2020-12-30 18:07:48 +0100 | [diff] [blame] | 852 | long os_get_time_offset(void) |
| 853 | { |
| 854 | const char *offset; |
| 855 | |
| 856 | offset = getenv(ENV_TIME_OFFSET); |
| 857 | if (offset) |
| 858 | return strtol(offset, NULL, 0); |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | void os_set_time_offset(long offset) |
| 863 | { |
| 864 | char buf[21]; |
| 865 | int ret; |
| 866 | |
| 867 | snprintf(buf, sizeof(buf), "%ld", offset); |
| 868 | ret = setenv(ENV_TIME_OFFSET, buf, true); |
| 869 | if (ret) |
| 870 | printf("Could not set environment variable %s\n", |
| 871 | ENV_TIME_OFFSET); |
| 872 | } |
| 873 | |
Simon Glass | 504548f | 2015-04-20 12:37:22 -0600 | [diff] [blame] | 874 | void os_localtime(struct rtc_time *rt) |
| 875 | { |
| 876 | time_t t = time(NULL); |
| 877 | struct tm *tm; |
| 878 | |
| 879 | tm = localtime(&t); |
| 880 | rt->tm_sec = tm->tm_sec; |
| 881 | rt->tm_min = tm->tm_min; |
| 882 | rt->tm_hour = tm->tm_hour; |
| 883 | rt->tm_mday = tm->tm_mday; |
| 884 | rt->tm_mon = tm->tm_mon + 1; |
| 885 | rt->tm_year = tm->tm_year + 1900; |
| 886 | rt->tm_wday = tm->tm_wday; |
| 887 | rt->tm_yday = tm->tm_yday; |
| 888 | rt->tm_isdst = tm->tm_isdst; |
| 889 | } |
Simon Glass | 5dccd15 | 2018-05-16 09:42:22 -0600 | [diff] [blame] | 890 | |
Simon Glass | b7255ef | 2018-09-15 00:50:55 -0600 | [diff] [blame] | 891 | void os_abort(void) |
| 892 | { |
| 893 | abort(); |
| 894 | } |
Simon Glass | 4e766c2 | 2018-10-01 21:12:32 -0600 | [diff] [blame] | 895 | |
| 896 | int os_mprotect_allow(void *start, size_t len) |
| 897 | { |
| 898 | int page_size = getpagesize(); |
| 899 | |
| 900 | /* Move start to the start of a page, len to the end */ |
| 901 | start = (void *)(((ulong)start) & ~(page_size - 1)); |
| 902 | len = (len + page_size * 2) & ~(page_size - 1); |
| 903 | |
| 904 | return mprotect(start, len, PROT_READ | PROT_WRITE); |
| 905 | } |
Simon Glass | 752707a | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 906 | |
| 907 | void *os_find_text_base(void) |
| 908 | { |
| 909 | char line[500]; |
| 910 | void *base = NULL; |
| 911 | int len; |
| 912 | int fd; |
| 913 | |
| 914 | /* |
| 915 | * This code assumes that the first line of /proc/self/maps holds |
| 916 | * information about the text, for example: |
| 917 | * |
| 918 | * 5622d9907000-5622d9a55000 r-xp 00000000 08:01 15067168 u-boot |
| 919 | * |
| 920 | * The first hex value is assumed to be the address. |
| 921 | * |
| 922 | * This is tested in Linux 4.15. |
| 923 | */ |
| 924 | fd = open("/proc/self/maps", O_RDONLY); |
| 925 | if (fd == -1) |
| 926 | return NULL; |
| 927 | len = read(fd, line, sizeof(line)); |
| 928 | if (len > 0) { |
| 929 | char *end = memchr(line, '-', len); |
| 930 | |
| 931 | if (end) { |
Heinrich Schuchardt | 05a1684 | 2019-10-26 23:17:44 +0200 | [diff] [blame] | 932 | uintptr_t addr; |
Simon Glass | 752707a | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 933 | |
| 934 | *end = '\0'; |
Heinrich Schuchardt | 05a1684 | 2019-10-26 23:17:44 +0200 | [diff] [blame] | 935 | if (sscanf(line, "%zx", &addr) == 1) |
Simon Glass | 752707a | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 936 | base = (void *)addr; |
| 937 | } |
| 938 | } |
| 939 | close(fd); |
| 940 | |
| 941 | return base; |
| 942 | } |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 943 | |
| 944 | void os_relaunch(char *argv[]) |
| 945 | { |
| 946 | execv(argv[0], argv); |
| 947 | os_exit(1); |
| 948 | } |