Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 1 | /* |
Simon Glass | d916515 | 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 | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 7 | * Copyright (c) 2011 The Chromium OS Authors. |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
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 | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 16 | struct sandbox_state; |
| 17 | |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 18 | /** |
| 19 | * Access to the OS read() system call |
| 20 | * |
| 21 | * \param fd File descriptor as returned by os_open() |
| 22 | * \param buf Buffer to place data |
| 23 | * \param count Number of bytes to read |
| 24 | * \return number of bytes read, or -1 on error |
| 25 | */ |
| 26 | ssize_t os_read(int fd, void *buf, size_t count); |
| 27 | |
| 28 | /** |
Taylor Hutt | e101550 | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 29 | * Access to the OS read() system call with non-blocking access |
| 30 | * |
| 31 | * \param fd File descriptor as returned by os_open() |
| 32 | * \param buf Buffer to place data |
| 33 | * \param count Number of bytes to read |
| 34 | * \return number of bytes read, or -1 on error |
| 35 | */ |
| 36 | ssize_t os_read_no_block(int fd, void *buf, size_t count); |
| 37 | |
| 38 | /** |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 39 | * Access to the OS write() system call |
| 40 | * |
| 41 | * \param fd File descriptor as returned by os_open() |
| 42 | * \param buf Buffer containing data to write |
| 43 | * \param count Number of bytes to write |
| 44 | * \return number of bytes written, or -1 on error |
| 45 | */ |
| 46 | ssize_t os_write(int fd, const void *buf, size_t count); |
| 47 | |
| 48 | /** |
Mike Frysinger | e2dcefc | 2011-10-25 13:02:58 +0200 | [diff] [blame] | 49 | * Access to the OS lseek() system call |
| 50 | * |
| 51 | * \param fd File descriptor as returned by os_open() |
| 52 | * \param offset File offset (based on whence) |
| 53 | * \param whence Position offset is relative to (see below) |
| 54 | * \return new file offset |
| 55 | */ |
| 56 | off_t os_lseek(int fd, off_t offset, int whence); |
| 57 | |
| 58 | /* Defines for "whence" in os_lseek() */ |
| 59 | #define OS_SEEK_SET 0 |
| 60 | #define OS_SEEK_CUR 1 |
| 61 | #define OS_SEEK_END 2 |
| 62 | |
| 63 | /** |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 64 | * Access to the OS open() system call |
| 65 | * |
| 66 | * \param pathname Pathname of file to open |
| 67 | * \param flags Flags, like O_RDONLY, O_RDWR |
| 68 | * \return file descriptor, or -1 on error |
| 69 | */ |
| 70 | int os_open(const char *pathname, int flags); |
| 71 | |
Simon Glass | d916515 | 2012-02-20 23:56:58 -0500 | [diff] [blame] | 72 | #define OS_O_RDONLY 0 |
| 73 | #define OS_O_WRONLY 1 |
| 74 | #define OS_O_RDWR 2 |
| 75 | #define OS_O_MASK 3 /* Mask for read/write flags */ |
| 76 | #define OS_O_CREAT 0100 |
| 77 | |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 78 | /** |
| 79 | * Access to the OS close() system call |
| 80 | * |
| 81 | * \param fd File descriptor to close |
| 82 | * \return 0 on success, -1 on error |
| 83 | */ |
| 84 | int os_close(int fd); |
| 85 | |
| 86 | /** |
Stephen Warren | cfd13e8 | 2014-03-01 22:18:00 -0700 | [diff] [blame] | 87 | * Access to the OS unlink() system call |
| 88 | * |
| 89 | * \param pathname Path of file to delete |
| 90 | * \return 0 for success, other for error |
| 91 | */ |
| 92 | int os_unlink(const char *pathname); |
| 93 | |
| 94 | /** |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 95 | * Access to the OS exit() system call |
| 96 | * |
| 97 | * This exits with the supplied return code, which should be 0 to indicate |
| 98 | * success. |
| 99 | * |
| 100 | * @param exit_code exit code for U-Boot |
| 101 | */ |
Mike Frysinger | 9d72e67 | 2012-02-26 17:46:30 -0500 | [diff] [blame] | 102 | void os_exit(int exit_code) __attribute__((noreturn)); |
Mike Frysinger | ab06a75 | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Put tty into raw mode to mimic serial console better |
| 106 | */ |
| 107 | void os_tty_raw(int fd); |
Matthias Weisser | 21899b1 | 2011-11-05 11:40:34 +0100 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Acquires some memory from the underlying os. |
| 111 | * |
| 112 | * \param length Number of bytes to be allocated |
| 113 | * \return Pointer to length bytes or NULL on error |
| 114 | */ |
| 115 | void *os_malloc(size_t length); |
Matthias Weisser | d99a687 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 116 | |
| 117 | /** |
Simon Glass | 77595c6 | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 118 | * Free memory previous allocated with os_malloc()/os_realloc() |
| 119 | * |
| 120 | * This returns the memory to the OS. |
| 121 | * |
| 122 | * \param ptr Pointer to memory block to free |
| 123 | */ |
Masahiro Yamada | 347d06d | 2014-01-15 13:06:41 +0900 | [diff] [blame] | 124 | void os_free(void *ptr); |
Simon Glass | 77595c6 | 2013-11-10 10:26:57 -0700 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * Reallocate previously-allocated memory to increase/decrease space |
| 128 | * |
| 129 | * This works in a similar way to the C library realloc() function. If |
| 130 | * length is 0, then ptr is freed. Otherwise the space used by ptr is |
| 131 | * expanded or reduced depending on whether length is larger or smaller |
| 132 | * than before. |
| 133 | * |
| 134 | * If ptr is NULL, then this is similar to calling os_malloc(). |
| 135 | * |
| 136 | * This function may need to move the memory block to make room for any |
| 137 | * extra space, in which case the new pointer is returned. |
| 138 | * |
| 139 | * \param ptr Pointer to memory block to reallocate |
| 140 | * \param length New length for memory block |
| 141 | * \return pointer to new memory block, or NULL on failure or if length |
| 142 | * is 0. |
| 143 | */ |
| 144 | void *os_realloc(void *ptr, size_t length); |
| 145 | |
| 146 | /** |
Matthias Weisser | d99a687 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 147 | * Access to the usleep function of the os |
| 148 | * |
| 149 | * \param usec Time to sleep in micro seconds |
| 150 | */ |
| 151 | void os_usleep(unsigned long usec); |
| 152 | |
| 153 | /** |
| 154 | * Gets a monotonic increasing number of nano seconds from the OS |
| 155 | * |
| 156 | * \return A monotonic increasing time scaled in nano seconds |
| 157 | */ |
Simon Glass | 2a54d15 | 2013-05-19 16:45:35 -0700 | [diff] [blame] | 158 | uint64_t os_get_nsec(void); |
Mike Frysinger | 4f345d5 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 159 | |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 160 | /** |
| 161 | * Parse arguments and update sandbox state. |
| 162 | * |
| 163 | * @param state Sandbox state to update |
| 164 | * @param argc Argument count |
| 165 | * @param argv Argument vector |
| 166 | * @return 0 if ok, and program should continue; |
| 167 | * 1 if ok, but program should stop; |
| 168 | * -1 on error: program should terminate. |
| 169 | */ |
| 170 | int os_parse_args(struct sandbox_state *state, int argc, char *argv[]); |
| 171 | |
Simon Glass | 62584db | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 172 | /* |
| 173 | * Types of directory entry that we support. See also os_dirent_typename in |
| 174 | * the C file. |
| 175 | */ |
| 176 | enum os_dirent_t { |
| 177 | OS_FILET_REG, /* Regular file */ |
| 178 | OS_FILET_LNK, /* Symbolic link */ |
| 179 | OS_FILET_DIR, /* Directory */ |
| 180 | OS_FILET_UNKNOWN, /* Something else */ |
| 181 | |
| 182 | OS_FILET_COUNT, |
| 183 | }; |
| 184 | |
| 185 | /** A directory entry node, containing information about a single dirent */ |
| 186 | struct os_dirent_node { |
| 187 | struct os_dirent_node *next; /* Pointer to next node, or NULL */ |
| 188 | ulong size; /* Size of file in bytes */ |
| 189 | enum os_dirent_t type; /* Type of entry */ |
| 190 | char name[0]; /* Name of entry */ |
| 191 | }; |
| 192 | |
| 193 | /** |
| 194 | * Get a directionry listing |
| 195 | * |
| 196 | * This allocates and returns a linked list containing the directory listing. |
| 197 | * |
| 198 | * @param dirname Directory to examine |
| 199 | * @param headp Returns pointer to head of linked list, or NULL if none |
| 200 | * @return 0 if ok, -ve on error |
| 201 | */ |
| 202 | int os_dirent_ls(const char *dirname, struct os_dirent_node **headp); |
| 203 | |
| 204 | /** |
| 205 | * Get the name of a directory entry type |
| 206 | * |
| 207 | * @param type Type to cehck |
| 208 | * @return string containing the name of that type, or "???" if none/invalid |
| 209 | */ |
| 210 | const char *os_dirent_get_typename(enum os_dirent_t type); |
| 211 | |
| 212 | /** |
| 213 | * Get the size of a file |
| 214 | * |
| 215 | * @param fname Filename to check |
| 216 | * @return size of file, or -1 if an error ocurred |
| 217 | */ |
| 218 | ssize_t os_get_filesize(const char *fname); |
| 219 | |
Simon Glass | 91b136c | 2013-11-10 10:27:01 -0700 | [diff] [blame] | 220 | /** |
| 221 | * Write a character to the controlling OS terminal |
| 222 | * |
| 223 | * This bypasses the U-Boot console support and writes directly to the OS |
| 224 | * stdout file descriptor. |
| 225 | * |
| 226 | * @param ch Character to write |
| 227 | */ |
| 228 | void os_putc(int ch); |
| 229 | |
| 230 | /** |
| 231 | * Write a string to the controlling OS terminal |
| 232 | * |
| 233 | * This bypasses the U-Boot console support and writes directly to the OS |
| 234 | * stdout file descriptor. |
| 235 | * |
| 236 | * @param str String to write (note that \n is not appended) |
| 237 | */ |
| 238 | void os_puts(const char *str); |
| 239 | |
Simon Glass | 5c2859c | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 240 | /** |
| 241 | * Write the sandbox RAM buffer to a existing file |
| 242 | * |
| 243 | * @param fname Filename to write memory to (simple binary format) |
| 244 | * @return 0 if OK, -ve on error |
| 245 | */ |
| 246 | int os_write_ram_buf(const char *fname); |
| 247 | |
| 248 | /** |
| 249 | * Read the sandbox RAM buffer from an existing file |
| 250 | * |
| 251 | * @param fname Filename containing memory (simple binary format) |
| 252 | * @return 0 if OK, -ve on error |
| 253 | */ |
| 254 | int os_read_ram_buf(const char *fname); |
| 255 | |
Mike Frysinger | 4f345d5 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 256 | #endif |