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