Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 2 | /* |
Simon Glass | d916515 | 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 | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 8 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mike Frysinger | 4f345d5 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 11 | #ifndef __OS_H__ |
| 12 | #define __OS_H__ |
| 13 | |
Simon Glass | 2a54d15 | 2013-05-19 16:45:35 -0700 | [diff] [blame] | 14 | #include <linux/types.h> |
| 15 | |
Simon Glass | 94eefde | 2015-04-20 12:37:22 -0600 | [diff] [blame] | 16 | struct rtc_time; |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 17 | struct sandbox_state; |
| 18 | |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 19 | /** |
| 20 | * Access to the OS read() system call |
| 21 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 22 | * @fd: File descriptor as returned by os_open() |
| 23 | * @buf: Buffer to place data |
| 24 | * @count: Number of bytes to read |
| 25 | * Return: number of bytes read, or -1 on error |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 26 | */ |
| 27 | ssize_t os_read(int fd, void *buf, size_t count); |
| 28 | |
| 29 | /** |
| 30 | * Access to the OS write() system call |
| 31 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 32 | * @fd: File descriptor as returned by os_open() |
| 33 | * @buf: Buffer containing data to write |
| 34 | * @count: Number of bytes to write |
| 35 | * Return: number of bytes written, or -1 on error |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 36 | */ |
| 37 | ssize_t os_write(int fd, const void *buf, size_t count); |
| 38 | |
| 39 | /** |
Mike Frysinger | e2dcefc | 2011-10-25 13:02:58 +0200 | [diff] [blame] | 40 | * Access to the OS lseek() system call |
| 41 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 42 | * @fd: File descriptor as returned by os_open() |
| 43 | * @offset: File offset (based on whence) |
| 44 | * @whence: Position offset is relative to (see below) |
| 45 | * Return: new file offset |
Mike Frysinger | e2dcefc | 2011-10-25 13:02:58 +0200 | [diff] [blame] | 46 | */ |
| 47 | off_t os_lseek(int fd, off_t offset, int whence); |
| 48 | |
| 49 | /* Defines for "whence" in os_lseek() */ |
| 50 | #define OS_SEEK_SET 0 |
| 51 | #define OS_SEEK_CUR 1 |
| 52 | #define OS_SEEK_END 2 |
| 53 | |
| 54 | /** |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 55 | * Access to the OS open() system call |
| 56 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 57 | * @pathname: Pathname of file to open |
| 58 | * @flags: Flags, like OS_O_RDONLY, OS_O_RDWR |
| 59 | * Return: file descriptor, or -1 on error |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 60 | */ |
| 61 | int os_open(const char *pathname, int flags); |
| 62 | |
Simon Glass | d916515 | 2012-02-20 23:56:58 -0500 | [diff] [blame] | 63 | #define OS_O_RDONLY 0 |
| 64 | #define OS_O_WRONLY 1 |
| 65 | #define OS_O_RDWR 2 |
| 66 | #define OS_O_MASK 3 /* Mask for read/write flags */ |
| 67 | #define OS_O_CREAT 0100 |
Simon Glass | 50b288a | 2018-10-01 11:55:07 -0600 | [diff] [blame] | 68 | #define OS_O_TRUNC 01000 |
Simon Glass | d916515 | 2012-02-20 23:56:58 -0500 | [diff] [blame] | 69 | |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 70 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 71 | * os_close() - access to the OS close() system call |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 72 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 73 | * @fd: File descriptor to close |
| 74 | * Return: 0 on success, -1 on error |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 75 | */ |
| 76 | int os_close(int fd); |
| 77 | |
| 78 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 79 | * os_unlink() - access to the OS unlink() system call |
Stephen Warren | cfd13e8 | 2014-03-01 22:18:00 -0700 | [diff] [blame] | 80 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 81 | * @pathname: Path of file to delete |
| 82 | * Return: 0 for success, other for error |
Stephen Warren | cfd13e8 | 2014-03-01 22:18:00 -0700 | [diff] [blame] | 83 | */ |
| 84 | int os_unlink(const char *pathname); |
| 85 | |
| 86 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 87 | * os_exit() - access to the OS exit() system call |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 88 | * |
| 89 | * This exits with the supplied return code, which should be 0 to indicate |
| 90 | * success. |
| 91 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 92 | * @exit_code: exit code for U-Boot |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 93 | */ |
Mike Frysinger | 9d72e67 | 2012-02-26 17:46:30 -0500 | [diff] [blame] | 94 | void os_exit(int exit_code) __attribute__((noreturn)); |
Mike Frysinger | ab06a75 | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 95 | |
| 96 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 97 | * os_tty_raw() - put tty into raw mode to mimic serial console better |
Simon Glass | ffb8790 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 98 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 99 | * @fd: File descriptor of stdin (normally 0) |
| 100 | * @allow_sigs: Allow Ctrl-C, Ctrl-Z to generate signals rather than |
| 101 | * be handled by U-Boot |
Mike Frysinger | ab06a75 | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 102 | */ |
Simon Glass | ffb8790 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 103 | void os_tty_raw(int fd, bool allow_sigs); |
Matthias Weisser | 21899b1 | 2011-11-05 11:40:34 +0100 | [diff] [blame] | 104 | |
| 105 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 106 | * os_fs_restore() - restore the tty to its original mode |
Simon Glass | 8939df0 | 2015-05-10 21:07:27 -0600 | [diff] [blame] | 107 | * |
| 108 | * Call this to restore the original terminal mode, after it has been changed |
| 109 | * by os_tty_raw(). This is an internal function. |
| 110 | */ |
| 111 | void os_fd_restore(void); |
| 112 | |
| 113 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 114 | * os_malloc() - aquires some memory from the underlying os. |
Matthias Weisser | 21899b1 | 2011-11-05 11:40:34 +0100 | [diff] [blame] | 115 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 116 | * @length: Number of bytes to be allocated |
Simon Glass | 14e46df | 2021-02-06 09:57:32 -0700 | [diff] [blame] | 117 | * Return: Pointer to length bytes or NULL if @length is 0 or on error |
Matthias Weisser | 21899b1 | 2011-11-05 11:40:34 +0100 | [diff] [blame] | 118 | */ |
| 119 | void *os_malloc(size_t length); |
Matthias Weisser | d99a687 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 120 | |
| 121 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 122 | * os_free() - free memory previous allocated with os_malloc() |
Simon Glass | 77595c6 | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 123 | * |
| 124 | * This returns the memory to the OS. |
| 125 | * |
Simon Glass | b308d9f | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 126 | * @ptr: Pointer to memory block to free. If this is NULL then this |
| 127 | * function does nothing |
Simon Glass | 77595c6 | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 128 | */ |
Masahiro Yamada | 347d06d | 2014-01-15 13:06:41 +0900 | [diff] [blame] | 129 | void os_free(void *ptr); |
Simon Glass | 77595c6 | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 130 | |
| 131 | /** |
Simon Glass | 14e46df | 2021-02-06 09:57:32 -0700 | [diff] [blame] | 132 | * os_realloc() - reallocate memory |
| 133 | * |
| 134 | * This follows the semantics of realloc(), so can perform an os_malloc() or |
| 135 | * os_free() depending on @ptr and @length. |
| 136 | * |
Heinrich Schuchardt | 4c30d18 | 2021-03-28 11:05:00 +0200 | [diff] [blame] | 137 | * @ptr: pointer to previously allocated memory of NULL |
| 138 | * @length: number of bytes to allocate |
| 139 | * Return: pointer to reallocated memory or NULL if @length is 0 |
Simon Glass | 14e46df | 2021-02-06 09:57:32 -0700 | [diff] [blame] | 140 | */ |
| 141 | void *os_realloc(void *ptr, size_t length); |
| 142 | |
| 143 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 144 | * os_usleep() - access to the usleep function of the os |
Matthias Weisser | d99a687 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 145 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 146 | * @usec: time to sleep in micro seconds |
Matthias Weisser | d99a687 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 147 | */ |
| 148 | void os_usleep(unsigned long usec); |
| 149 | |
| 150 | /** |
| 151 | * Gets a monotonic increasing number of nano seconds from the OS |
| 152 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 153 | * Return: a monotonic increasing time scaled in nano seconds |
Matthias Weisser | d99a687 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 154 | */ |
Simon Glass | 2a54d15 | 2013-05-19 16:45:35 -0700 | [diff] [blame] | 155 | uint64_t os_get_nsec(void); |
Mike Frysinger | 4f345d5 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 156 | |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 157 | /** |
| 158 | * Parse arguments and update sandbox state. |
| 159 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 160 | * @state: sandbox state to update |
| 161 | * @argc: argument count |
| 162 | * @argv: argument vector |
| 163 | * Return: |
| 164 | * * 0 if ok, and program should continue |
| 165 | * * 1 if ok, but program should stop |
| 166 | * * -1 on error: program should terminate |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 167 | */ |
| 168 | int os_parse_args(struct sandbox_state *state, int argc, char *argv[]); |
| 169 | |
Simon Glass | 62584db | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 170 | /* |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 171 | * enum os_dirent_t - type of directory entry |
| 172 | * |
Simon Glass | 62584db | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 173 | * Types of directory entry that we support. See also os_dirent_typename in |
| 174 | * the C file. |
| 175 | */ |
| 176 | enum os_dirent_t { |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 177 | /** |
| 178 | * @OS_FILET_REG: regular file |
| 179 | */ |
| 180 | OS_FILET_REG, |
| 181 | /** |
| 182 | * @OS_FILET_LNK: symbolic link |
| 183 | */ |
| 184 | OS_FILET_LNK, |
| 185 | /** |
| 186 | * @OS_FILET_DIR: directory |
| 187 | */ |
| 188 | OS_FILET_DIR, |
| 189 | /** |
| 190 | * @OS_FILET_UNKNOWN: something else |
| 191 | */ |
| 192 | OS_FILET_UNKNOWN, |
| 193 | /** |
| 194 | * @OS_FILET_COUNT: number of directory entry types |
| 195 | */ |
Simon Glass | 62584db | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 196 | OS_FILET_COUNT, |
| 197 | }; |
| 198 | |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 199 | /** |
| 200 | * struct os_dirent_node - directory node |
| 201 | * |
| 202 | * A directory entry node, containing information about a single dirent |
| 203 | * |
| 204 | */ |
Simon Glass | 62584db | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 205 | struct os_dirent_node { |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 206 | /** |
| 207 | * @next: pointer to next node, or NULL |
| 208 | */ |
| 209 | struct os_dirent_node *next; |
| 210 | /** |
| 211 | * @size: size of file in bytes |
| 212 | */ |
| 213 | ulong size; |
| 214 | /** |
| 215 | * @type: type of entry |
| 216 | */ |
| 217 | enum os_dirent_t type; |
| 218 | /** |
| 219 | * @name: name of entry |
| 220 | */ |
| 221 | char name[0]; |
Simon Glass | 62584db | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 225 | * os_dirent_ls() - get a directory listing |
Simon Glass | 62584db | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 226 | * |
| 227 | * This allocates and returns a linked list containing the directory listing. |
| 228 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 229 | * @dirname: directory to examine |
| 230 | * @headp: on return pointer to head of linked list, or NULL if none |
| 231 | * Return: 0 if ok, -ve on error |
Simon Glass | 62584db | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 232 | */ |
| 233 | int os_dirent_ls(const char *dirname, struct os_dirent_node **headp); |
| 234 | |
| 235 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 236 | * os_dirent_free() - free directory list |
Stefan Brüns | 8616708 | 2016-10-01 20:41:38 +0200 | [diff] [blame] | 237 | * |
| 238 | * This frees a linked list containing a directory listing. |
| 239 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 240 | * @node: pointer to head of linked list |
Stefan Brüns | 8616708 | 2016-10-01 20:41:38 +0200 | [diff] [blame] | 241 | */ |
| 242 | void os_dirent_free(struct os_dirent_node *node); |
| 243 | |
| 244 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 245 | * os_dirent_get_typename() - get the name of a directory entry type |
Simon Glass | 62584db | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 246 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 247 | * @type: type to check |
| 248 | * Return: |
| 249 | * string containing the name of that type, |
| 250 | * or "???" if none/invalid |
Simon Glass | 62584db | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 251 | */ |
| 252 | const char *os_dirent_get_typename(enum os_dirent_t type); |
| 253 | |
| 254 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 255 | * os_get_filesize() - get the size of a file |
Simon Glass | 62584db | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 256 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 257 | * @fname: filename to check |
| 258 | * @size: size of file is returned if no error |
| 259 | * Return: 0 on success or -1 if an error ocurred |
Simon Glass | 62584db | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 260 | */ |
Suriyan Ramasami | 96b1046 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 261 | int os_get_filesize(const char *fname, loff_t *size); |
Simon Glass | 62584db | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 262 | |
Simon Glass | 91b136c | 2013-11-10 10:27:01 -0700 | [diff] [blame] | 263 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 264 | * os_putc() - write a character to the controlling OS terminal |
Simon Glass | 0b189b6 | 2017-12-04 13:48:17 -0700 | [diff] [blame] | 265 | * |
| 266 | * This bypasses the U-Boot console support and writes directly to the OS |
| 267 | * stdout file descriptor. |
| 268 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 269 | * @ch: haracter to write |
Simon Glass | 0b189b6 | 2017-12-04 13:48:17 -0700 | [diff] [blame] | 270 | */ |
| 271 | void os_putc(int ch); |
| 272 | |
| 273 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 274 | * os_puts() - write a string to the controlling OS terminal |
Simon Glass | 0b189b6 | 2017-12-04 13:48:17 -0700 | [diff] [blame] | 275 | * |
| 276 | * This bypasses the U-Boot console support and writes directly to the OS |
| 277 | * stdout file descriptor. |
| 278 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 279 | * @str: string to write (note that \n is not appended) |
Simon Glass | 0b189b6 | 2017-12-04 13:48:17 -0700 | [diff] [blame] | 280 | */ |
| 281 | void os_puts(const char *str); |
| 282 | |
| 283 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 284 | * os_write_ram_buf() - write the sandbox RAM buffer to a existing file |
Simon Glass | 5c2859c | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 285 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 286 | * @fname: filename to write memory to (simple binary format) |
| 287 | * Return: 0 if OK, -ve on error |
Simon Glass | 5c2859c | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 288 | */ |
| 289 | int os_write_ram_buf(const char *fname); |
| 290 | |
| 291 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 292 | * os_read_ram_buf() - read the sandbox RAM buffer from an existing file |
Simon Glass | 5c2859c | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 293 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 294 | * @fname: filename containing memory (simple binary format) |
| 295 | * Return: 0 if OK, -ve on error |
Simon Glass | 5c2859c | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 296 | */ |
| 297 | int os_read_ram_buf(const char *fname); |
| 298 | |
Simon Glass | 47f5fcf | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 299 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 300 | * os_jump_to_image() - jump to a new executable image |
Simon Glass | 47f5fcf | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 301 | * |
| 302 | * This uses exec() to run a new executable image, after putting it in a |
| 303 | * temporary file. The same arguments and environment are passed to this |
| 304 | * new image, with the addition of: |
| 305 | * |
| 306 | * -j <filename> Specifies the filename the image was written to. The |
| 307 | * calling image may want to delete this at some point. |
| 308 | * -m <filename> Specifies the file containing the sandbox memory |
| 309 | * (ram_buf) from this image, so that the new image can |
| 310 | * have access to this. It also means that the original |
| 311 | * memory filename passed to U-Boot will be left intact. |
| 312 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 313 | * @dest: buffer containing executable image |
| 314 | * @size: size of buffer |
| 315 | * Return: 0 if OK, -ve on error |
Simon Glass | 47f5fcf | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 316 | */ |
| 317 | int os_jump_to_image(const void *dest, int size); |
| 318 | |
Simon Glass | 94eefde | 2015-04-20 12:37:22 -0600 | [diff] [blame] | 319 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 320 | * os_find_u_boot() - determine the path to U-Boot proper |
Simon Glass | d4e33f5 | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 321 | * |
| 322 | * This function is intended to be called from within sandbox SPL. It uses |
| 323 | * a few heuristics to find U-Boot proper. Normally it is either in the same |
| 324 | * directory, or the directory above (since u-boot-spl is normally in an |
| 325 | * spl/ subdirectory when built). |
| 326 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 327 | * @fname: place to put full path to U-Boot |
| 328 | * @maxlen: maximum size of @fname |
Simon Glass | 01ad9f7 | 2021-03-07 17:35:13 -0700 | [diff] [blame] | 329 | * @use_img: select the 'u-boot.img' file instead of the 'u-boot' ELF file |
Simon Glass | f178beb | 2021-07-05 16:32:45 -0600 | [diff] [blame] | 330 | * @cur_prefix: prefix of current executable, e.g. "spl" or "tpl" |
| 331 | * @next_prefix: prefix of executable to find, e.g. "spl" or "" |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 332 | * Return: 0 if OK, -NOSPC if the filename is too large, -ENOENT if not found |
Simon Glass | d4e33f5 | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 333 | */ |
Simon Glass | f178beb | 2021-07-05 16:32:45 -0600 | [diff] [blame] | 334 | int os_find_u_boot(char *fname, int maxlen, bool use_img, |
| 335 | const char *cur_prefix, const char *next_prefix); |
Simon Glass | d4e33f5 | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 336 | |
| 337 | /** |
| 338 | * os_spl_to_uboot() - Run U-Boot proper |
| 339 | * |
| 340 | * When called from SPL, this runs U-Boot proper. The filename is obtained by |
| 341 | * calling os_find_u_boot(). |
| 342 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 343 | * @fname: full pathname to U-Boot executable |
| 344 | * Return: 0 if OK, -ve on error |
Simon Glass | d4e33f5 | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 345 | */ |
| 346 | int os_spl_to_uboot(const char *fname); |
| 347 | |
| 348 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 349 | * os_localtime() - read the current system time |
Simon Glass | 94eefde | 2015-04-20 12:37:22 -0600 | [diff] [blame] | 350 | * |
| 351 | * This reads the current Local Time and places it into the provided |
| 352 | * structure. |
| 353 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 354 | * @rt: place to put system time |
Simon Glass | 94eefde | 2015-04-20 12:37:22 -0600 | [diff] [blame] | 355 | */ |
| 356 | void os_localtime(struct rtc_time *rt); |
| 357 | |
Simon Glass | 30eef21 | 2018-05-16 09:42:22 -0600 | [diff] [blame] | 358 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 359 | * os_abort() - raise SIGABRT to exit sandbox (e.g. to debugger) |
Simon Glass | fe938fb | 2018-09-15 00:50:55 -0600 | [diff] [blame] | 360 | */ |
Heinrich Schuchardt | c30a709 | 2021-02-01 01:24:10 +0100 | [diff] [blame] | 361 | void os_abort(void) __attribute__((noreturn)); |
Simon Glass | 9f8037e | 2018-10-01 21:12:32 -0600 | [diff] [blame] | 362 | |
| 363 | /** |
| 364 | * os_mprotect_allow() - Remove write-protection on a region of memory |
| 365 | * |
| 366 | * The start and length will be page-aligned before use. |
| 367 | * |
| 368 | * @start: Region start |
| 369 | * @len: Region length in bytes |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 370 | * Return: 0 if OK, -1 on error from mprotect() |
Simon Glass | 9f8037e | 2018-10-01 21:12:32 -0600 | [diff] [blame] | 371 | */ |
| 372 | int os_mprotect_allow(void *start, size_t len); |
| 373 | |
Simon Glass | 056a5ce | 2018-10-01 11:55:08 -0600 | [diff] [blame] | 374 | /** |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 375 | * os_write_file() - write a file to the host filesystem |
Simon Glass | 056a5ce | 2018-10-01 11:55:08 -0600 | [diff] [blame] | 376 | * |
| 377 | * This can be useful when debugging for writing data out of sandbox for |
| 378 | * inspection by external tools. |
| 379 | * |
| 380 | * @name: File path to write to |
| 381 | * @buf: Data to write |
| 382 | * @size: Size of data to write |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 383 | * Return: 0 if OK, -ve on error |
Simon Glass | 056a5ce | 2018-10-01 11:55:08 -0600 | [diff] [blame] | 384 | */ |
| 385 | int os_write_file(const char *name, const void *buf, int size); |
| 386 | |
Simon Glass | 566bf3a | 2018-11-06 15:21:25 -0700 | [diff] [blame] | 387 | /** |
| 388 | * os_read_file() - Read a file from the host filesystem |
| 389 | * |
| 390 | * This can be useful when reading test data into sandbox for use by test |
| 391 | * routines. The data is allocated using os_malloc() and should be freed by |
| 392 | * the caller. |
| 393 | * |
| 394 | * @name: File path to read from |
| 395 | * @bufp: Returns buffer containing data read |
| 396 | * @sizep: Returns size of data |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 397 | * Return: 0 if OK, -ve on error |
Simon Glass | 566bf3a | 2018-11-06 15:21:25 -0700 | [diff] [blame] | 398 | */ |
| 399 | int os_read_file(const char *name, void **bufp, int *sizep); |
| 400 | |
Simon Glass | 001d188 | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 401 | /* |
| 402 | * os_find_text_base() - Find the text section in this running process |
| 403 | * |
| 404 | * This tries to find the address of the text section in this running process. |
| 405 | * It can be useful to map the address of functions to the address listed in |
| 406 | * the u-boot.map file. |
| 407 | * |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 408 | * Return: address if found, else NULL |
Simon Glass | 001d188 | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 409 | */ |
| 410 | void *os_find_text_base(void); |
| 411 | |
Heinrich Schuchardt | 329dccc | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 412 | /** |
| 413 | * os_relaunch() - restart the sandbox |
| 414 | * |
| 415 | * This functions is used to implement the cold reboot of the sand box. |
Heinrich Schuchardt | 063790c | 2020-10-27 20:29:27 +0100 | [diff] [blame] | 416 | * @argv\[0] specifies the binary that is started while the calling process |
Heinrich Schuchardt | 329dccc | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 417 | * stops immediately. If the new binary cannot be started, the process is |
| 418 | * terminated and 1 is set as shell return code. |
| 419 | * |
| 420 | * The PID of the process stays the same. All file descriptors that have not |
| 421 | * been opened with O_CLOEXEC stay open including stdin, stdout, stderr. |
| 422 | * |
| 423 | * @argv: NULL terminated list of command line parameters |
| 424 | */ |
| 425 | void os_relaunch(char *argv[]); |
| 426 | |
Heinrich Schuchardt | b46f30a | 2020-11-12 00:29:56 +0100 | [diff] [blame] | 427 | /** |
| 428 | * os_setup_signal_handlers() - setup signal handlers |
| 429 | * |
| 430 | * Install signal handlers for SIGBUS and SIGSEGV. |
| 431 | * |
| 432 | * Return: 0 for success |
| 433 | */ |
| 434 | int os_setup_signal_handlers(void); |
| 435 | |
| 436 | /** |
| 437 | * os_signal_action() - handle a signal |
| 438 | * |
| 439 | * @sig: signal |
| 440 | * @pc: program counter |
| 441 | */ |
| 442 | void os_signal_action(int sig, unsigned long pc); |
| 443 | |
Heinrich Schuchardt | 43db075 | 2020-12-30 18:07:48 +0100 | [diff] [blame] | 444 | /** |
| 445 | * os_get_time_offset() - get time offset |
| 446 | * |
| 447 | * Get the time offset from environment variable UBOOT_SB_TIME_OFFSET. |
| 448 | * |
| 449 | * Return: offset in seconds |
| 450 | */ |
| 451 | long os_get_time_offset(void); |
| 452 | |
| 453 | /** |
| 454 | * os_set_time_offset() - set time offset |
| 455 | * |
| 456 | * Save the time offset in environment variable UBOOT_SB_TIME_OFFSET. |
| 457 | * |
| 458 | * @offset: offset in seconds |
| 459 | */ |
| 460 | void os_set_time_offset(long offset); |
| 461 | |
Mike Frysinger | 4f345d5 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 462 | #endif |