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 | /* |
Simon Glass | 3196d75 | 2012-02-20 23:56:58 -0500 | [diff] [blame] | 3 | * Operating System Interface |
| 4 | * |
| 5 | * This provides access to useful OS routines for the sandbox architecture. |
| 6 | * They are kept in a separate file so we can include system headers. |
| 7 | * |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 8 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mike Frysinger | 999a579 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 11 | #ifndef __OS_H__ |
| 12 | #define __OS_H__ |
| 13 | |
Simon Glass | fb4b4e8 | 2013-05-19 16:45:35 -0700 | [diff] [blame] | 14 | #include <linux/types.h> |
| 15 | |
Simon Glass | 504548f | 2015-04-20 12:37:22 -0600 | [diff] [blame] | 16 | struct rtc_time; |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 17 | struct sandbox_state; |
| 18 | |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 19 | /** |
Heinrich Schuchardt | bb63ea8 | 2022-04-04 22:45:03 +0200 | [diff] [blame] | 20 | * os_printf() - print directly to OS console |
| 21 | * |
| 22 | * @format: format string |
| 23 | */ |
| 24 | int os_printf(const char *format, ...); |
| 25 | |
| 26 | /** |
Heinrich Schuchardt | 1f4091c | 2024-09-18 23:54:08 +0200 | [diff] [blame] | 27 | * os_read() - access the OS read() system call |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 28 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 29 | * @fd: File descriptor as returned by os_open() |
| 30 | * @buf: Buffer to place data |
| 31 | * @count: Number of bytes to read |
Simon Glass | 3dbd4ae | 2024-08-07 16:47:24 -0600 | [diff] [blame] | 32 | * Return: number of bytes read, or -errno on error |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 33 | */ |
| 34 | ssize_t os_read(int fd, void *buf, size_t count); |
| 35 | |
| 36 | /** |
Heinrich Schuchardt | 1f4091c | 2024-09-18 23:54:08 +0200 | [diff] [blame] | 37 | * os_write() - access the OS write() system call |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 38 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 39 | * @fd: File descriptor as returned by os_open() |
| 40 | * @buf: Buffer containing data to write |
| 41 | * @count: Number of bytes to write |
Simon Glass | 3dbd4ae | 2024-08-07 16:47:24 -0600 | [diff] [blame] | 42 | * Return: number of bytes written, or -errno on error |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 43 | */ |
| 44 | ssize_t os_write(int fd, const void *buf, size_t count); |
| 45 | |
| 46 | /** |
Heinrich Schuchardt | 1f4091c | 2024-09-18 23:54:08 +0200 | [diff] [blame] | 47 | * os_lseek() - access the OS lseek() system call |
Mike Frysinger | 60addac | 2011-10-25 13:02:58 +0200 | [diff] [blame] | 48 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 49 | * @fd: File descriptor as returned by os_open() |
| 50 | * @offset: File offset (based on whence) |
| 51 | * @whence: Position offset is relative to (see below) |
Simon Glass | 3dbd4ae | 2024-08-07 16:47:24 -0600 | [diff] [blame] | 52 | * Return: new file offset, or -errno on error |
Mike Frysinger | 60addac | 2011-10-25 13:02:58 +0200 | [diff] [blame] | 53 | */ |
| 54 | off_t os_lseek(int fd, off_t offset, int whence); |
| 55 | |
| 56 | /* Defines for "whence" in os_lseek() */ |
| 57 | #define OS_SEEK_SET 0 |
| 58 | #define OS_SEEK_CUR 1 |
| 59 | #define OS_SEEK_END 2 |
| 60 | |
| 61 | /** |
Simon Glass | 7b9cf84f | 2021-08-18 21:40:30 -0600 | [diff] [blame] | 62 | * os_filesize() - Calculate the size of a file |
| 63 | * |
| 64 | * @fd: File descriptor as returned by os_open() |
| 65 | * Return: file size or negative error code |
| 66 | */ |
Heinrich Schuchardt | 6db1b65 | 2023-04-05 11:34:15 +0200 | [diff] [blame] | 67 | off_t os_filesize(int fd); |
Simon Glass | 7b9cf84f | 2021-08-18 21:40:30 -0600 | [diff] [blame] | 68 | |
| 69 | /** |
Heinrich Schuchardt | 1f4091c | 2024-09-18 23:54:08 +0200 | [diff] [blame] | 70 | * os_open() - access the OS open() system call |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 71 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 72 | * @pathname: Pathname of file to open |
| 73 | * @flags: Flags, like OS_O_RDONLY, OS_O_RDWR |
| 74 | * Return: file descriptor, or -1 on error |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 75 | */ |
| 76 | int os_open(const char *pathname, int flags); |
| 77 | |
Simon Glass | 3196d75 | 2012-02-20 23:56:58 -0500 | [diff] [blame] | 78 | #define OS_O_RDONLY 0 |
| 79 | #define OS_O_WRONLY 1 |
| 80 | #define OS_O_RDWR 2 |
| 81 | #define OS_O_MASK 3 /* Mask for read/write flags */ |
| 82 | #define OS_O_CREAT 0100 |
Simon Glass | ce55a11 | 2018-10-01 11:55:07 -0600 | [diff] [blame] | 83 | #define OS_O_TRUNC 01000 |
Simon Glass | 3196d75 | 2012-02-20 23:56:58 -0500 | [diff] [blame] | 84 | |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 85 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 86 | * os_close() - access to the OS close() system call |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 87 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 88 | * @fd: File descriptor to close |
| 89 | * Return: 0 on success, -1 on error |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 90 | */ |
| 91 | int os_close(int fd); |
| 92 | |
| 93 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 94 | * os_unlink() - access to the OS unlink() system call |
Stephen Warren | cd5edba | 2014-03-01 22:18:00 -0700 | [diff] [blame] | 95 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 96 | * @pathname: Path of file to delete |
| 97 | * Return: 0 for success, other for error |
Stephen Warren | cd5edba | 2014-03-01 22:18:00 -0700 | [diff] [blame] | 98 | */ |
| 99 | int os_unlink(const char *pathname); |
| 100 | |
Simon Glass | 528a0f6 | 2023-08-24 13:55:37 -0600 | [diff] [blame] | 101 | /** os_persistent_fname() - Find the path to a test file |
| 102 | * |
| 103 | * @buf: Buffer to hold path |
| 104 | * @maxsize: Maximum size of buffer |
| 105 | * @fname: Leaf filename to find |
| 106 | * Returns: 0 on success, -ENOENT if file is not found, -ENOSPC if the buffer is |
| 107 | * too small |
| 108 | */ |
| 109 | int os_persistent_file(char *buf, int maxsize, const char *fname); |
| 110 | |
Stephen Warren | cd5edba | 2014-03-01 22:18:00 -0700 | [diff] [blame] | 111 | /** |
Sean Anderson | 988996f | 2023-11-04 16:37:51 -0400 | [diff] [blame] | 112 | * os_mktemp() - Create a temporary file |
| 113 | * @fname: The template to use for the file name. This must end with 6 Xs. It |
| 114 | * will be modified to the opened filename on success. |
| 115 | * @size: The size of the file |
| 116 | * |
| 117 | * Create a temporary file using @fname as a template, unlink it, and truncate |
| 118 | * it to @size. |
| 119 | * |
| 120 | * Return: A file descriptor, or negative errno on error |
| 121 | */ |
| 122 | int os_mktemp(char *fname, off_t size); |
| 123 | |
| 124 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 125 | * os_exit() - access to the OS exit() system call |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 126 | * |
| 127 | * This exits with the supplied return code, which should be 0 to indicate |
| 128 | * success. |
| 129 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 130 | * @exit_code: exit code for U-Boot |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 131 | */ |
Mike Frysinger | bc036d3 | 2012-02-26 17:46:30 -0500 | [diff] [blame] | 132 | void os_exit(int exit_code) __attribute__((noreturn)); |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 133 | |
| 134 | /** |
Rasmus Villemoes | ae400f3 | 2022-09-27 11:54:04 +0200 | [diff] [blame] | 135 | * os_alarm() - access to the OS alarm() system call |
Simon Glass | 5feb043 | 2022-10-29 19:47:04 -0600 | [diff] [blame] | 136 | * |
| 137 | * @seconds: number of seconds before the signal is sent |
| 138 | * Returns: number of seconds remaining until any previously scheduled alarm was |
| 139 | * due to be delivered; 0 if there was no previously scheduled alarm |
Rasmus Villemoes | ae400f3 | 2022-09-27 11:54:04 +0200 | [diff] [blame] | 140 | */ |
| 141 | unsigned int os_alarm(unsigned int seconds); |
| 142 | |
| 143 | /** |
| 144 | * os_set_alarm_handler() - set handler for SIGALRM |
| 145 | * |
| 146 | * @handler: The handler function. Pass NULL for SIG_DFL. |
| 147 | */ |
| 148 | void os_set_alarm_handler(void (*handler)(int)); |
| 149 | |
| 150 | /** |
| 151 | * os_raise_sigalrm() - do raise(SIGALRM) |
| 152 | */ |
| 153 | void os_raise_sigalrm(void); |
| 154 | |
| 155 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 156 | * os_tty_raw() - put tty into raw mode to mimic serial console better |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 157 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 158 | * @fd: File descriptor of stdin (normally 0) |
| 159 | * @allow_sigs: Allow Ctrl-C, Ctrl-Z to generate signals rather than |
| 160 | * be handled by U-Boot |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 161 | */ |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 162 | void os_tty_raw(int fd, bool allow_sigs); |
Matthias Weisser | b5f7b47 | 2011-11-05 11:40:34 +0100 | [diff] [blame] | 163 | |
| 164 | /** |
Heinrich Schuchardt | 1f4091c | 2024-09-18 23:54:08 +0200 | [diff] [blame] | 165 | * os_fd_restore() - restore the tty to its original mode |
Simon Glass | 9c3b7d6 | 2015-05-10 21:07:27 -0600 | [diff] [blame] | 166 | * |
| 167 | * Call this to restore the original terminal mode, after it has been changed |
| 168 | * by os_tty_raw(). This is an internal function. |
| 169 | */ |
| 170 | void os_fd_restore(void); |
| 171 | |
| 172 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 173 | * os_malloc() - aquires some memory from the underlying os. |
Matthias Weisser | b5f7b47 | 2011-11-05 11:40:34 +0100 | [diff] [blame] | 174 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 175 | * @length: Number of bytes to be allocated |
Simon Glass | 4c902fa | 2021-02-06 09:57:32 -0700 | [diff] [blame] | 176 | * Return: Pointer to length bytes or NULL if @length is 0 or on error |
Matthias Weisser | b5f7b47 | 2011-11-05 11:40:34 +0100 | [diff] [blame] | 177 | */ |
| 178 | void *os_malloc(size_t length); |
Matthias Weisser | 0d3dd14 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 179 | |
| 180 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 181 | * os_free() - free memory previous allocated with os_malloc() |
Simon Glass | 1e6594c | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 182 | * |
| 183 | * This returns the memory to the OS. |
| 184 | * |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 185 | * @ptr: Pointer to memory block to free. If this is NULL then this |
| 186 | * function does nothing |
Simon Glass | 1e6594c | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 187 | */ |
Masahiro Yamada | ee95728 | 2014-01-15 13:06:41 +0900 | [diff] [blame] | 188 | void os_free(void *ptr); |
Simon Glass | 1e6594c | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 189 | |
| 190 | /** |
Simon Glass | 4c902fa | 2021-02-06 09:57:32 -0700 | [diff] [blame] | 191 | * os_realloc() - reallocate memory |
| 192 | * |
| 193 | * This follows the semantics of realloc(), so can perform an os_malloc() or |
| 194 | * os_free() depending on @ptr and @length. |
| 195 | * |
Heinrich Schuchardt | 70e2999 | 2021-03-28 11:05:00 +0200 | [diff] [blame] | 196 | * @ptr: pointer to previously allocated memory of NULL |
| 197 | * @length: number of bytes to allocate |
| 198 | * Return: pointer to reallocated memory or NULL if @length is 0 |
Simon Glass | 4c902fa | 2021-02-06 09:57:32 -0700 | [diff] [blame] | 199 | */ |
| 200 | void *os_realloc(void *ptr, size_t length); |
| 201 | |
| 202 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 203 | * os_usleep() - access to the usleep function of the os |
Matthias Weisser | 0d3dd14 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 204 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 205 | * @usec: time to sleep in micro seconds |
Matthias Weisser | 0d3dd14 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 206 | */ |
| 207 | void os_usleep(unsigned long usec); |
| 208 | |
| 209 | /** |
Heinrich Schuchardt | 1f4091c | 2024-09-18 23:54:08 +0200 | [diff] [blame] | 210 | * os_get_nsec() - get monotonically increasing number of nano seconds from OS |
Matthias Weisser | 0d3dd14 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 211 | * |
Heinrich Schuchardt | 1f4091c | 2024-09-18 23:54:08 +0200 | [diff] [blame] | 212 | * Return: a monotoniccally increasing time scaled in nano seconds |
Matthias Weisser | 0d3dd14 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 213 | */ |
Simon Glass | fb4b4e8 | 2013-05-19 16:45:35 -0700 | [diff] [blame] | 214 | uint64_t os_get_nsec(void); |
Mike Frysinger | 999a579 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 215 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 216 | /** |
Heinrich Schuchardt | 1f4091c | 2024-09-18 23:54:08 +0200 | [diff] [blame] | 217 | * os_parse_args() - parse arguments and update sandbox state. |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 218 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 219 | * @state: sandbox state to update |
| 220 | * @argc: argument count |
| 221 | * @argv: argument vector |
| 222 | * Return: |
| 223 | * * 0 if ok, and program should continue |
| 224 | * * 1 if ok, but program should stop |
| 225 | * * -1 on error: program should terminate |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 226 | */ |
| 227 | int os_parse_args(struct sandbox_state *state, int argc, char *argv[]); |
| 228 | |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 229 | /* |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 230 | * enum os_dirent_t - type of directory entry |
| 231 | * |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 232 | * Types of directory entry that we support. See also os_dirent_typename in |
| 233 | * the C file. |
| 234 | */ |
| 235 | enum os_dirent_t { |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 236 | /** |
| 237 | * @OS_FILET_REG: regular file |
| 238 | */ |
| 239 | OS_FILET_REG, |
| 240 | /** |
| 241 | * @OS_FILET_LNK: symbolic link |
| 242 | */ |
| 243 | OS_FILET_LNK, |
| 244 | /** |
| 245 | * @OS_FILET_DIR: directory |
| 246 | */ |
| 247 | OS_FILET_DIR, |
| 248 | /** |
| 249 | * @OS_FILET_UNKNOWN: something else |
| 250 | */ |
| 251 | OS_FILET_UNKNOWN, |
| 252 | /** |
| 253 | * @OS_FILET_COUNT: number of directory entry types |
| 254 | */ |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 255 | OS_FILET_COUNT, |
| 256 | }; |
| 257 | |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 258 | /** |
| 259 | * struct os_dirent_node - directory node |
| 260 | * |
| 261 | * A directory entry node, containing information about a single dirent |
| 262 | * |
| 263 | */ |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 264 | struct os_dirent_node { |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 265 | /** |
| 266 | * @next: pointer to next node, or NULL |
| 267 | */ |
| 268 | struct os_dirent_node *next; |
| 269 | /** |
| 270 | * @size: size of file in bytes |
| 271 | */ |
| 272 | ulong size; |
| 273 | /** |
| 274 | * @type: type of entry |
| 275 | */ |
| 276 | enum os_dirent_t type; |
| 277 | /** |
| 278 | * @name: name of entry |
| 279 | */ |
| 280 | char name[0]; |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 284 | * os_dirent_ls() - get a directory listing |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 285 | * |
| 286 | * This allocates and returns a linked list containing the directory listing. |
| 287 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 288 | * @dirname: directory to examine |
| 289 | * @headp: on return pointer to head of linked list, or NULL if none |
| 290 | * Return: 0 if ok, -ve on error |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 291 | */ |
| 292 | int os_dirent_ls(const char *dirname, struct os_dirent_node **headp); |
| 293 | |
| 294 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 295 | * os_dirent_free() - free directory list |
Stefan Brüns | e058284 | 2016-10-01 20:41:38 +0200 | [diff] [blame] | 296 | * |
| 297 | * This frees a linked list containing a directory listing. |
| 298 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 299 | * @node: pointer to head of linked list |
Stefan Brüns | e058284 | 2016-10-01 20:41:38 +0200 | [diff] [blame] | 300 | */ |
| 301 | void os_dirent_free(struct os_dirent_node *node); |
| 302 | |
| 303 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 304 | * os_dirent_get_typename() - get the name of a directory entry type |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 305 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 306 | * @type: type to check |
| 307 | * Return: |
| 308 | * string containing the name of that type, |
| 309 | * or "???" if none/invalid |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 310 | */ |
| 311 | const char *os_dirent_get_typename(enum os_dirent_t type); |
| 312 | |
| 313 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 314 | * os_get_filesize() - get the size of a file |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 315 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 316 | * @fname: filename to check |
| 317 | * @size: size of file is returned if no error |
| 318 | * Return: 0 on success or -1 if an error ocurred |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 319 | */ |
Heinrich Schuchardt | 011a1e0 | 2022-01-11 01:50:24 +0100 | [diff] [blame] | 320 | int os_get_filesize(const char *fname, long long *size); |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 321 | |
Simon Glass | 3e9fd24 | 2013-11-10 10:27:01 -0700 | [diff] [blame] | 322 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 323 | * os_putc() - write a character to the controlling OS terminal |
Simon Glass | 29d1143 | 2017-12-04 13:48:17 -0700 | [diff] [blame] | 324 | * |
| 325 | * This bypasses the U-Boot console support and writes directly to the OS |
| 326 | * stdout file descriptor. |
| 327 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 328 | * @ch: haracter to write |
Simon Glass | 29d1143 | 2017-12-04 13:48:17 -0700 | [diff] [blame] | 329 | */ |
| 330 | void os_putc(int ch); |
| 331 | |
| 332 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 333 | * os_puts() - write a string to the controlling OS terminal |
Simon Glass | 29d1143 | 2017-12-04 13:48:17 -0700 | [diff] [blame] | 334 | * |
| 335 | * This bypasses the U-Boot console support and writes directly to the OS |
| 336 | * stdout file descriptor. |
| 337 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 338 | * @str: string to write (note that \n is not appended) |
Simon Glass | 29d1143 | 2017-12-04 13:48:17 -0700 | [diff] [blame] | 339 | */ |
| 340 | void os_puts(const char *str); |
| 341 | |
| 342 | /** |
Pali Rohár | 4acd1a0 | 2022-09-05 11:31:16 +0200 | [diff] [blame] | 343 | * os_flush() - flush controlling OS terminal |
| 344 | * |
| 345 | * This bypasses the U-Boot console support and flushes directly the OS |
| 346 | * stdout file descriptor. |
| 347 | */ |
| 348 | void os_flush(void); |
| 349 | |
| 350 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 351 | * os_write_ram_buf() - write the sandbox RAM buffer to a existing file |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 352 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 353 | * @fname: filename to write memory to (simple binary format) |
| 354 | * Return: 0 if OK, -ve on error |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 355 | */ |
| 356 | int os_write_ram_buf(const char *fname); |
| 357 | |
| 358 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 359 | * os_read_ram_buf() - read the sandbox RAM buffer from an existing file |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 360 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 361 | * @fname: filename containing memory (simple binary format) |
| 362 | * Return: 0 if OK, -ve on error |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 363 | */ |
| 364 | int os_read_ram_buf(const char *fname); |
| 365 | |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 366 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 367 | * os_jump_to_image() - jump to a new executable image |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 368 | * |
| 369 | * This uses exec() to run a new executable image, after putting it in a |
| 370 | * temporary file. The same arguments and environment are passed to this |
| 371 | * new image, with the addition of: |
| 372 | * |
| 373 | * -j <filename> Specifies the filename the image was written to. The |
| 374 | * calling image may want to delete this at some point. |
| 375 | * -m <filename> Specifies the file containing the sandbox memory |
| 376 | * (ram_buf) from this image, so that the new image can |
| 377 | * have access to this. It also means that the original |
| 378 | * memory filename passed to U-Boot will be left intact. |
| 379 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 380 | * @dest: buffer containing executable image |
| 381 | * @size: size of buffer |
| 382 | * Return: 0 if OK, -ve on error |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 383 | */ |
| 384 | int os_jump_to_image(const void *dest, int size); |
| 385 | |
Simon Glass | 504548f | 2015-04-20 12:37:22 -0600 | [diff] [blame] | 386 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 387 | * os_find_u_boot() - determine the path to U-Boot proper |
Simon Glass | 2c931ba | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 388 | * |
| 389 | * This function is intended to be called from within sandbox SPL. It uses |
| 390 | * a few heuristics to find U-Boot proper. Normally it is either in the same |
| 391 | * directory, or the directory above (since u-boot-spl is normally in an |
| 392 | * spl/ subdirectory when built). |
| 393 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 394 | * @fname: place to put full path to U-Boot |
| 395 | * @maxlen: maximum size of @fname |
Simon Glass | b90e487 | 2021-03-07 17:35:13 -0700 | [diff] [blame] | 396 | * @use_img: select the 'u-boot.img' file instead of the 'u-boot' ELF file |
Simon Glass | 1cd0600 | 2021-07-05 16:32:45 -0600 | [diff] [blame] | 397 | * @cur_prefix: prefix of current executable, e.g. "spl" or "tpl" |
| 398 | * @next_prefix: prefix of executable to find, e.g. "spl" or "" |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 399 | * Return: 0 if OK, -NOSPC if the filename is too large, -ENOENT if not found |
Simon Glass | 2c931ba | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 400 | */ |
Simon Glass | 1cd0600 | 2021-07-05 16:32:45 -0600 | [diff] [blame] | 401 | int os_find_u_boot(char *fname, int maxlen, bool use_img, |
| 402 | const char *cur_prefix, const char *next_prefix); |
Simon Glass | 2c931ba | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 403 | |
| 404 | /** |
| 405 | * os_spl_to_uboot() - Run U-Boot proper |
| 406 | * |
| 407 | * When called from SPL, this runs U-Boot proper. The filename is obtained by |
| 408 | * calling os_find_u_boot(). |
| 409 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 410 | * @fname: full pathname to U-Boot executable |
| 411 | * Return: 0 if OK, -ve on error |
Simon Glass | 2c931ba | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 412 | */ |
| 413 | int os_spl_to_uboot(const char *fname); |
| 414 | |
| 415 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 416 | * os_localtime() - read the current system time |
Simon Glass | 504548f | 2015-04-20 12:37:22 -0600 | [diff] [blame] | 417 | * |
| 418 | * This reads the current Local Time and places it into the provided |
| 419 | * structure. |
| 420 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 421 | * @rt: place to put system time |
Simon Glass | 504548f | 2015-04-20 12:37:22 -0600 | [diff] [blame] | 422 | */ |
| 423 | void os_localtime(struct rtc_time *rt); |
| 424 | |
Simon Glass | 5dccd15 | 2018-05-16 09:42:22 -0600 | [diff] [blame] | 425 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 426 | * os_abort() - raise SIGABRT to exit sandbox (e.g. to debugger) |
Simon Glass | b7255ef | 2018-09-15 00:50:55 -0600 | [diff] [blame] | 427 | */ |
Heinrich Schuchardt | 2165d0c | 2021-02-01 01:24:10 +0100 | [diff] [blame] | 428 | void os_abort(void) __attribute__((noreturn)); |
Simon Glass | 4e766c2 | 2018-10-01 21:12:32 -0600 | [diff] [blame] | 429 | |
| 430 | /** |
| 431 | * os_mprotect_allow() - Remove write-protection on a region of memory |
| 432 | * |
| 433 | * The start and length will be page-aligned before use. |
| 434 | * |
| 435 | * @start: Region start |
| 436 | * @len: Region length in bytes |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 437 | * Return: 0 if OK, -1 on error from mprotect() |
Simon Glass | 4e766c2 | 2018-10-01 21:12:32 -0600 | [diff] [blame] | 438 | */ |
| 439 | int os_mprotect_allow(void *start, size_t len); |
| 440 | |
Simon Glass | cbfa845 | 2018-10-01 11:55:08 -0600 | [diff] [blame] | 441 | /** |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 442 | * os_write_file() - write a file to the host filesystem |
Simon Glass | cbfa845 | 2018-10-01 11:55:08 -0600 | [diff] [blame] | 443 | * |
| 444 | * This can be useful when debugging for writing data out of sandbox for |
| 445 | * inspection by external tools. |
| 446 | * |
| 447 | * @name: File path to write to |
| 448 | * @buf: Data to write |
| 449 | * @size: Size of data to write |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 450 | * Return: 0 if OK, -ve on error |
Simon Glass | cbfa845 | 2018-10-01 11:55:08 -0600 | [diff] [blame] | 451 | */ |
| 452 | int os_write_file(const char *name, const void *buf, int size); |
| 453 | |
Simon Glass | 8d176d8 | 2018-11-06 15:21:25 -0700 | [diff] [blame] | 454 | /** |
| 455 | * os_read_file() - Read a file from the host filesystem |
| 456 | * |
| 457 | * This can be useful when reading test data into sandbox for use by test |
| 458 | * routines. The data is allocated using os_malloc() and should be freed by |
| 459 | * the caller. |
| 460 | * |
| 461 | * @name: File path to read from |
| 462 | * @bufp: Returns buffer containing data read |
| 463 | * @sizep: Returns size of data |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 464 | * Return: 0 if OK, -ve on error |
Simon Glass | 8d176d8 | 2018-11-06 15:21:25 -0700 | [diff] [blame] | 465 | */ |
| 466 | int os_read_file(const char *name, void **bufp, int *sizep); |
| 467 | |
Simon Glass | e4c25c8 | 2021-08-18 21:40:31 -0600 | [diff] [blame] | 468 | /** |
| 469 | * os_map_file() - Map a file from the host filesystem into memory |
| 470 | * |
| 471 | * This can be useful when to provide a backing store for an emulated device |
| 472 | * |
| 473 | * @pathname: File pathname to map |
| 474 | * @os_flags: Flags, like OS_O_RDONLY, OS_O_RDWR |
| 475 | * @bufp: Returns buffer containing the file |
| 476 | * @sizep: Returns size of data |
| 477 | * Return: 0 if OK, -ve on error |
| 478 | */ |
| 479 | int os_map_file(const char *pathname, int os_flags, void **bufp, int *sizep); |
| 480 | |
Simon Glass | 5c1fd58 | 2021-10-23 17:25:58 -0600 | [diff] [blame] | 481 | /** |
| 482 | * os_unmap() - Unmap a file previously mapped |
| 483 | * |
| 484 | * @buf: Mapped address |
| 485 | * @size: Size in bytes |
| 486 | * Return: 0 if OK, -ve on error |
| 487 | */ |
| 488 | int os_unmap(void *buf, int size); |
| 489 | |
Simon Glass | 752707a | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 490 | /* |
| 491 | * os_find_text_base() - Find the text section in this running process |
| 492 | * |
| 493 | * This tries to find the address of the text section in this running process. |
| 494 | * It can be useful to map the address of functions to the address listed in |
| 495 | * the u-boot.map file. |
| 496 | * |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 497 | * Return: address if found, else NULL |
Simon Glass | 752707a | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 498 | */ |
| 499 | void *os_find_text_base(void); |
| 500 | |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 501 | /** |
| 502 | * os_relaunch() - restart the sandbox |
| 503 | * |
| 504 | * This functions is used to implement the cold reboot of the sand box. |
Heinrich Schuchardt | 1ffe004 | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 505 | * @argv\[0] specifies the binary that is started while the calling process |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 506 | * stops immediately. If the new binary cannot be started, the process is |
| 507 | * terminated and 1 is set as shell return code. |
| 508 | * |
| 509 | * The PID of the process stays the same. All file descriptors that have not |
| 510 | * been opened with O_CLOEXEC stay open including stdin, stdout, stderr. |
| 511 | * |
| 512 | * @argv: NULL terminated list of command line parameters |
| 513 | */ |
| 514 | void os_relaunch(char *argv[]); |
| 515 | |
Heinrich Schuchardt | 28eb509 | 2020-11-12 00:29:56 +0100 | [diff] [blame] | 516 | /** |
| 517 | * os_setup_signal_handlers() - setup signal handlers |
| 518 | * |
| 519 | * Install signal handlers for SIGBUS and SIGSEGV. |
| 520 | * |
| 521 | * Return: 0 for success |
| 522 | */ |
| 523 | int os_setup_signal_handlers(void); |
| 524 | |
| 525 | /** |
| 526 | * os_signal_action() - handle a signal |
| 527 | * |
| 528 | * @sig: signal |
| 529 | * @pc: program counter |
| 530 | */ |
| 531 | void os_signal_action(int sig, unsigned long pc); |
| 532 | |
Heinrich Schuchardt | c0d1a00 | 2020-12-30 18:07:48 +0100 | [diff] [blame] | 533 | /** |
| 534 | * os_get_time_offset() - get time offset |
| 535 | * |
| 536 | * Get the time offset from environment variable UBOOT_SB_TIME_OFFSET. |
| 537 | * |
| 538 | * Return: offset in seconds |
| 539 | */ |
| 540 | long os_get_time_offset(void); |
| 541 | |
| 542 | /** |
| 543 | * os_set_time_offset() - set time offset |
| 544 | * |
| 545 | * Save the time offset in environment variable UBOOT_SB_TIME_OFFSET. |
| 546 | * |
| 547 | * @offset: offset in seconds |
| 548 | */ |
| 549 | void os_set_time_offset(long offset); |
| 550 | |
Mike Frysinger | 999a579 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 551 | #endif |