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 | /** |
| 20 | * Access to the OS read() system call |
| 21 | * |
| 22 | * \param fd File descriptor as returned by os_open() |
| 23 | * \param buf Buffer to place data |
| 24 | * \param count Number of bytes to read |
| 25 | * \return number of bytes read, or -1 on error |
| 26 | */ |
| 27 | ssize_t os_read(int fd, void *buf, size_t count); |
| 28 | |
| 29 | /** |
| 30 | * Access to the OS write() system call |
| 31 | * |
| 32 | * \param fd File descriptor as returned by os_open() |
| 33 | * \param buf Buffer containing data to write |
| 34 | * \param count Number of bytes to write |
| 35 | * \return number of bytes written, or -1 on error |
| 36 | */ |
| 37 | ssize_t os_write(int fd, const void *buf, size_t count); |
| 38 | |
| 39 | /** |
Mike Frysinger | 60addac | 2011-10-25 13:02:58 +0200 | [diff] [blame] | 40 | * Access to the OS lseek() system call |
| 41 | * |
| 42 | * \param fd File descriptor as returned by os_open() |
| 43 | * \param offset File offset (based on whence) |
| 44 | * \param whence Position offset is relative to (see below) |
| 45 | * \return new file offset |
| 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 | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 55 | * Access to the OS open() system call |
| 56 | * |
| 57 | * \param pathname Pathname of file to open |
Simon Glass | 574323f | 2015-03-25 12:21:50 -0600 | [diff] [blame] | 58 | * \param flags Flags, like OS_O_RDONLY, OS_O_RDWR |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 59 | * \return file descriptor, or -1 on error |
| 60 | */ |
| 61 | int os_open(const char *pathname, int flags); |
| 62 | |
Simon Glass | 3196d75 | 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 | ce55a11 | 2018-10-01 11:55:07 -0600 | [diff] [blame] | 68 | #define OS_O_TRUNC 01000 |
Simon Glass | 3196d75 | 2012-02-20 23:56:58 -0500 | [diff] [blame] | 69 | |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 70 | /** |
| 71 | * Access to the OS close() system call |
| 72 | * |
| 73 | * \param fd File descriptor to close |
| 74 | * \return 0 on success, -1 on error |
| 75 | */ |
| 76 | int os_close(int fd); |
| 77 | |
| 78 | /** |
Stephen Warren | cd5edba | 2014-03-01 22:18:00 -0700 | [diff] [blame] | 79 | * Access to the OS unlink() system call |
| 80 | * |
| 81 | * \param pathname Path of file to delete |
| 82 | * \return 0 for success, other for error |
| 83 | */ |
| 84 | int os_unlink(const char *pathname); |
| 85 | |
| 86 | /** |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 87 | * Access to the OS exit() system call |
| 88 | * |
| 89 | * This exits with the supplied return code, which should be 0 to indicate |
| 90 | * success. |
| 91 | * |
| 92 | * @param exit_code exit code for U-Boot |
| 93 | */ |
Mike Frysinger | bc036d3 | 2012-02-26 17:46:30 -0500 | [diff] [blame] | 94 | void os_exit(int exit_code) __attribute__((noreturn)); |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Put tty into raw mode to mimic serial console better |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 98 | * |
| 99 | * @param fd File descriptor of stdin (normally 0) |
| 100 | * @param allow_sigs Allow Ctrl-C, Ctrl-Z to generate signals rather than |
| 101 | * be handled by U-Boot |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 102 | */ |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 103 | void os_tty_raw(int fd, bool allow_sigs); |
Matthias Weisser | b5f7b47 | 2011-11-05 11:40:34 +0100 | [diff] [blame] | 104 | |
| 105 | /** |
Simon Glass | 9c3b7d6 | 2015-05-10 21:07:27 -0600 | [diff] [blame] | 106 | * Restore the tty to its original mode |
| 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 | /** |
Matthias Weisser | b5f7b47 | 2011-11-05 11:40:34 +0100 | [diff] [blame] | 114 | * Acquires some memory from the underlying os. |
| 115 | * |
| 116 | * \param length Number of bytes to be allocated |
| 117 | * \return Pointer to length bytes or NULL on error |
| 118 | */ |
| 119 | void *os_malloc(size_t length); |
Matthias Weisser | 0d3dd14 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 120 | |
| 121 | /** |
Simon Glass | 1e6594c | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 122 | * Free memory previous allocated with os_malloc()/os_realloc() |
| 123 | * |
| 124 | * This returns the memory to the OS. |
| 125 | * |
| 126 | * \param ptr Pointer to memory block to free |
| 127 | */ |
Masahiro Yamada | ee95728 | 2014-01-15 13:06:41 +0900 | [diff] [blame] | 128 | void os_free(void *ptr); |
Simon Glass | 1e6594c | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Reallocate previously-allocated memory to increase/decrease space |
| 132 | * |
| 133 | * This works in a similar way to the C library realloc() function. If |
| 134 | * length is 0, then ptr is freed. Otherwise the space used by ptr is |
| 135 | * expanded or reduced depending on whether length is larger or smaller |
| 136 | * than before. |
| 137 | * |
| 138 | * If ptr is NULL, then this is similar to calling os_malloc(). |
| 139 | * |
| 140 | * This function may need to move the memory block to make room for any |
| 141 | * extra space, in which case the new pointer is returned. |
| 142 | * |
| 143 | * \param ptr Pointer to memory block to reallocate |
| 144 | * \param length New length for memory block |
| 145 | * \return pointer to new memory block, or NULL on failure or if length |
| 146 | * is 0. |
| 147 | */ |
| 148 | void *os_realloc(void *ptr, size_t length); |
| 149 | |
| 150 | /** |
Matthias Weisser | 0d3dd14 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 151 | * Access to the usleep function of the os |
| 152 | * |
| 153 | * \param usec Time to sleep in micro seconds |
| 154 | */ |
| 155 | void os_usleep(unsigned long usec); |
| 156 | |
| 157 | /** |
| 158 | * Gets a monotonic increasing number of nano seconds from the OS |
| 159 | * |
| 160 | * \return A monotonic increasing time scaled in nano seconds |
| 161 | */ |
Simon Glass | fb4b4e8 | 2013-05-19 16:45:35 -0700 | [diff] [blame] | 162 | uint64_t os_get_nsec(void); |
Mike Frysinger | 999a579 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 163 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 164 | /** |
| 165 | * Parse arguments and update sandbox state. |
| 166 | * |
| 167 | * @param state Sandbox state to update |
| 168 | * @param argc Argument count |
| 169 | * @param argv Argument vector |
| 170 | * @return 0 if ok, and program should continue; |
| 171 | * 1 if ok, but program should stop; |
| 172 | * -1 on error: program should terminate. |
| 173 | */ |
| 174 | int os_parse_args(struct sandbox_state *state, int argc, char *argv[]); |
| 175 | |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 176 | /* |
| 177 | * Types of directory entry that we support. See also os_dirent_typename in |
| 178 | * the C file. |
| 179 | */ |
| 180 | enum os_dirent_t { |
| 181 | OS_FILET_REG, /* Regular file */ |
| 182 | OS_FILET_LNK, /* Symbolic link */ |
| 183 | OS_FILET_DIR, /* Directory */ |
| 184 | OS_FILET_UNKNOWN, /* Something else */ |
| 185 | |
| 186 | OS_FILET_COUNT, |
| 187 | }; |
| 188 | |
| 189 | /** A directory entry node, containing information about a single dirent */ |
| 190 | struct os_dirent_node { |
| 191 | struct os_dirent_node *next; /* Pointer to next node, or NULL */ |
| 192 | ulong size; /* Size of file in bytes */ |
| 193 | enum os_dirent_t type; /* Type of entry */ |
| 194 | char name[0]; /* Name of entry */ |
| 195 | }; |
| 196 | |
| 197 | /** |
| 198 | * Get a directionry listing |
| 199 | * |
| 200 | * This allocates and returns a linked list containing the directory listing. |
| 201 | * |
| 202 | * @param dirname Directory to examine |
| 203 | * @param headp Returns pointer to head of linked list, or NULL if none |
| 204 | * @return 0 if ok, -ve on error |
| 205 | */ |
| 206 | int os_dirent_ls(const char *dirname, struct os_dirent_node **headp); |
| 207 | |
| 208 | /** |
Stefan Brüns | e058284 | 2016-10-01 20:41:38 +0200 | [diff] [blame] | 209 | * Free directory list |
| 210 | * |
| 211 | * This frees a linked list containing a directory listing. |
| 212 | * |
| 213 | * @param node Pointer to head of linked list |
| 214 | */ |
| 215 | void os_dirent_free(struct os_dirent_node *node); |
| 216 | |
| 217 | /** |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 218 | * Get the name of a directory entry type |
| 219 | * |
Stefan Brüns | e058284 | 2016-10-01 20:41:38 +0200 | [diff] [blame] | 220 | * @param type Type to check |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 221 | * @return string containing the name of that type, or "???" if none/invalid |
| 222 | */ |
| 223 | const char *os_dirent_get_typename(enum os_dirent_t type); |
| 224 | |
| 225 | /** |
| 226 | * Get the size of a file |
| 227 | * |
| 228 | * @param fname Filename to check |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 229 | * @param size size of file is returned if no error |
| 230 | * @return 0 on success or -1 if an error ocurred |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 231 | */ |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 232 | int os_get_filesize(const char *fname, loff_t *size); |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 233 | |
Simon Glass | 3e9fd24 | 2013-11-10 10:27:01 -0700 | [diff] [blame] | 234 | /** |
Simon Glass | 29d1143 | 2017-12-04 13:48:17 -0700 | [diff] [blame] | 235 | * Write a character to the controlling OS terminal |
| 236 | * |
| 237 | * This bypasses the U-Boot console support and writes directly to the OS |
| 238 | * stdout file descriptor. |
| 239 | * |
| 240 | * @param ch Character to write |
| 241 | */ |
| 242 | void os_putc(int ch); |
| 243 | |
| 244 | /** |
| 245 | * Write a string to the controlling OS terminal |
| 246 | * |
| 247 | * This bypasses the U-Boot console support and writes directly to the OS |
| 248 | * stdout file descriptor. |
| 249 | * |
| 250 | * @param str String to write (note that \n is not appended) |
| 251 | */ |
| 252 | void os_puts(const char *str); |
| 253 | |
| 254 | /** |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 255 | * Write the sandbox RAM buffer to a existing file |
| 256 | * |
| 257 | * @param fname Filename to write memory to (simple binary format) |
| 258 | * @return 0 if OK, -ve on error |
| 259 | */ |
| 260 | int os_write_ram_buf(const char *fname); |
| 261 | |
| 262 | /** |
| 263 | * Read the sandbox RAM buffer from an existing file |
| 264 | * |
| 265 | * @param fname Filename containing memory (simple binary format) |
| 266 | * @return 0 if OK, -ve on error |
| 267 | */ |
| 268 | int os_read_ram_buf(const char *fname); |
| 269 | |
Simon Glass | 860b7d9 | 2014-02-27 13:26:15 -0700 | [diff] [blame] | 270 | /** |
| 271 | * Jump to a new executable image |
| 272 | * |
| 273 | * This uses exec() to run a new executable image, after putting it in a |
| 274 | * temporary file. The same arguments and environment are passed to this |
| 275 | * new image, with the addition of: |
| 276 | * |
| 277 | * -j <filename> Specifies the filename the image was written to. The |
| 278 | * calling image may want to delete this at some point. |
| 279 | * -m <filename> Specifies the file containing the sandbox memory |
| 280 | * (ram_buf) from this image, so that the new image can |
| 281 | * have access to this. It also means that the original |
| 282 | * memory filename passed to U-Boot will be left intact. |
| 283 | * |
| 284 | * @param dest Buffer containing executable image |
| 285 | * @param size Size of buffer |
| 286 | */ |
| 287 | int os_jump_to_image(const void *dest, int size); |
| 288 | |
Simon Glass | 504548f | 2015-04-20 12:37:22 -0600 | [diff] [blame] | 289 | /** |
Simon Glass | 2c931ba | 2016-07-04 11:57:45 -0600 | [diff] [blame] | 290 | * os_find_u_boot() - Determine the path to U-Boot proper |
| 291 | * |
| 292 | * This function is intended to be called from within sandbox SPL. It uses |
| 293 | * a few heuristics to find U-Boot proper. Normally it is either in the same |
| 294 | * directory, or the directory above (since u-boot-spl is normally in an |
| 295 | * spl/ subdirectory when built). |
| 296 | * |
| 297 | * @fname: Place to put full path to U-Boot |
| 298 | * @maxlen: Maximum size of @fname |
| 299 | * @return 0 if OK, -NOSPC if the filename is too large, -ENOENT if not found |
| 300 | */ |
| 301 | int os_find_u_boot(char *fname, int maxlen); |
| 302 | |
| 303 | /** |
| 304 | * os_spl_to_uboot() - Run U-Boot proper |
| 305 | * |
| 306 | * When called from SPL, this runs U-Boot proper. The filename is obtained by |
| 307 | * calling os_find_u_boot(). |
| 308 | * |
| 309 | * @fname: Full pathname to U-Boot executable |
| 310 | * @return 0 if OK, -ve on error |
| 311 | */ |
| 312 | int os_spl_to_uboot(const char *fname); |
| 313 | |
| 314 | /** |
Simon Glass | 504548f | 2015-04-20 12:37:22 -0600 | [diff] [blame] | 315 | * Read the current system time |
| 316 | * |
| 317 | * This reads the current Local Time and places it into the provided |
| 318 | * structure. |
| 319 | * |
| 320 | * @param rt Place to put system time |
| 321 | */ |
| 322 | void os_localtime(struct rtc_time *rt); |
| 323 | |
Simon Glass | 5dccd15 | 2018-05-16 09:42:22 -0600 | [diff] [blame] | 324 | /** |
Simon Glass | b7255ef | 2018-09-15 00:50:55 -0600 | [diff] [blame] | 325 | * os_abort() - Raise SIGABRT to exit sandbox (e.g. to debugger) |
| 326 | */ |
| 327 | void os_abort(void); |
Simon Glass | 4e766c2 | 2018-10-01 21:12:32 -0600 | [diff] [blame] | 328 | |
| 329 | /** |
| 330 | * os_mprotect_allow() - Remove write-protection on a region of memory |
| 331 | * |
| 332 | * The start and length will be page-aligned before use. |
| 333 | * |
| 334 | * @start: Region start |
| 335 | * @len: Region length in bytes |
| 336 | * @return 0 if OK, -1 on error from mprotect() |
| 337 | */ |
| 338 | int os_mprotect_allow(void *start, size_t len); |
| 339 | |
Simon Glass | cbfa845 | 2018-10-01 11:55:08 -0600 | [diff] [blame] | 340 | /** |
| 341 | * os_write_file() - Write a file to the host filesystem |
| 342 | * |
| 343 | * This can be useful when debugging for writing data out of sandbox for |
| 344 | * inspection by external tools. |
| 345 | * |
| 346 | * @name: File path to write to |
| 347 | * @buf: Data to write |
| 348 | * @size: Size of data to write |
| 349 | * @return 0 if OK, -ve on error |
| 350 | */ |
| 351 | int os_write_file(const char *name, const void *buf, int size); |
| 352 | |
Simon Glass | 8d176d8 | 2018-11-06 15:21:25 -0700 | [diff] [blame] | 353 | /** |
| 354 | * os_read_file() - Read a file from the host filesystem |
| 355 | * |
| 356 | * This can be useful when reading test data into sandbox for use by test |
| 357 | * routines. The data is allocated using os_malloc() and should be freed by |
| 358 | * the caller. |
| 359 | * |
| 360 | * @name: File path to read from |
| 361 | * @bufp: Returns buffer containing data read |
| 362 | * @sizep: Returns size of data |
| 363 | * @return 0 if OK, -ve on error |
| 364 | */ |
| 365 | int os_read_file(const char *name, void **bufp, int *sizep); |
| 366 | |
Mike Frysinger | 999a579 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 367 | #endif |