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. |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
Mike Frysinger | 999a579 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 27 | #ifndef __OS_H__ |
| 28 | #define __OS_H__ |
| 29 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 30 | struct sandbox_state; |
| 31 | |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 32 | /** |
| 33 | * Access to the OS read() system call |
| 34 | * |
| 35 | * \param fd File descriptor as returned by os_open() |
| 36 | * \param buf Buffer to place data |
| 37 | * \param count Number of bytes to read |
| 38 | * \return number of bytes read, or -1 on error |
| 39 | */ |
| 40 | ssize_t os_read(int fd, void *buf, size_t count); |
| 41 | |
| 42 | /** |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 43 | * Access to the OS read() system call with non-blocking access |
| 44 | * |
| 45 | * \param fd File descriptor as returned by os_open() |
| 46 | * \param buf Buffer to place data |
| 47 | * \param count Number of bytes to read |
| 48 | * \return number of bytes read, or -1 on error |
| 49 | */ |
| 50 | ssize_t os_read_no_block(int fd, void *buf, size_t count); |
| 51 | |
| 52 | /** |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 53 | * Access to the OS write() system call |
| 54 | * |
| 55 | * \param fd File descriptor as returned by os_open() |
| 56 | * \param buf Buffer containing data to write |
| 57 | * \param count Number of bytes to write |
| 58 | * \return number of bytes written, or -1 on error |
| 59 | */ |
| 60 | ssize_t os_write(int fd, const void *buf, size_t count); |
| 61 | |
| 62 | /** |
Mike Frysinger | 60addac | 2011-10-25 13:02:58 +0200 | [diff] [blame] | 63 | * Access to the OS lseek() system call |
| 64 | * |
| 65 | * \param fd File descriptor as returned by os_open() |
| 66 | * \param offset File offset (based on whence) |
| 67 | * \param whence Position offset is relative to (see below) |
| 68 | * \return new file offset |
| 69 | */ |
| 70 | off_t os_lseek(int fd, off_t offset, int whence); |
| 71 | |
| 72 | /* Defines for "whence" in os_lseek() */ |
| 73 | #define OS_SEEK_SET 0 |
| 74 | #define OS_SEEK_CUR 1 |
| 75 | #define OS_SEEK_END 2 |
| 76 | |
| 77 | /** |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 78 | * Access to the OS open() system call |
| 79 | * |
| 80 | * \param pathname Pathname of file to open |
| 81 | * \param flags Flags, like O_RDONLY, O_RDWR |
| 82 | * \return file descriptor, or -1 on error |
| 83 | */ |
| 84 | int os_open(const char *pathname, int flags); |
| 85 | |
Simon Glass | 3196d75 | 2012-02-20 23:56:58 -0500 | [diff] [blame] | 86 | #define OS_O_RDONLY 0 |
| 87 | #define OS_O_WRONLY 1 |
| 88 | #define OS_O_RDWR 2 |
| 89 | #define OS_O_MASK 3 /* Mask for read/write flags */ |
| 90 | #define OS_O_CREAT 0100 |
| 91 | |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 92 | /** |
| 93 | * Access to the OS close() system call |
| 94 | * |
| 95 | * \param fd File descriptor to close |
| 96 | * \return 0 on success, -1 on error |
| 97 | */ |
| 98 | int os_close(int fd); |
| 99 | |
| 100 | /** |
| 101 | * Access to the OS exit() system call |
| 102 | * |
| 103 | * This exits with the supplied return code, which should be 0 to indicate |
| 104 | * success. |
| 105 | * |
| 106 | * @param exit_code exit code for U-Boot |
| 107 | */ |
Mike Frysinger | bc036d3 | 2012-02-26 17:46:30 -0500 | [diff] [blame] | 108 | void os_exit(int exit_code) __attribute__((noreturn)); |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * Put tty into raw mode to mimic serial console better |
| 112 | */ |
| 113 | void os_tty_raw(int fd); |
Matthias Weisser | b5f7b47 | 2011-11-05 11:40:34 +0100 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Acquires some memory from the underlying os. |
| 117 | * |
| 118 | * \param length Number of bytes to be allocated |
| 119 | * \return Pointer to length bytes or NULL on error |
| 120 | */ |
| 121 | void *os_malloc(size_t length); |
Matthias Weisser | 0d3dd14 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * Access to the usleep function of the os |
| 125 | * |
| 126 | * \param usec Time to sleep in micro seconds |
| 127 | */ |
| 128 | void os_usleep(unsigned long usec); |
| 129 | |
| 130 | /** |
| 131 | * Gets a monotonic increasing number of nano seconds from the OS |
| 132 | * |
| 133 | * \return A monotonic increasing time scaled in nano seconds |
| 134 | */ |
| 135 | u64 os_get_nsec(void); |
Mike Frysinger | 999a579 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 136 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 137 | /** |
| 138 | * Parse arguments and update sandbox state. |
| 139 | * |
| 140 | * @param state Sandbox state to update |
| 141 | * @param argc Argument count |
| 142 | * @param argv Argument vector |
| 143 | * @return 0 if ok, and program should continue; |
| 144 | * 1 if ok, but program should stop; |
| 145 | * -1 on error: program should terminate. |
| 146 | */ |
| 147 | int os_parse_args(struct sandbox_state *state, int argc, char *argv[]); |
| 148 | |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 149 | /* |
| 150 | * Types of directory entry that we support. See also os_dirent_typename in |
| 151 | * the C file. |
| 152 | */ |
| 153 | enum os_dirent_t { |
| 154 | OS_FILET_REG, /* Regular file */ |
| 155 | OS_FILET_LNK, /* Symbolic link */ |
| 156 | OS_FILET_DIR, /* Directory */ |
| 157 | OS_FILET_UNKNOWN, /* Something else */ |
| 158 | |
| 159 | OS_FILET_COUNT, |
| 160 | }; |
| 161 | |
| 162 | /** A directory entry node, containing information about a single dirent */ |
| 163 | struct os_dirent_node { |
| 164 | struct os_dirent_node *next; /* Pointer to next node, or NULL */ |
| 165 | ulong size; /* Size of file in bytes */ |
| 166 | enum os_dirent_t type; /* Type of entry */ |
| 167 | char name[0]; /* Name of entry */ |
| 168 | }; |
| 169 | |
| 170 | /** |
| 171 | * Get a directionry listing |
| 172 | * |
| 173 | * This allocates and returns a linked list containing the directory listing. |
| 174 | * |
| 175 | * @param dirname Directory to examine |
| 176 | * @param headp Returns pointer to head of linked list, or NULL if none |
| 177 | * @return 0 if ok, -ve on error |
| 178 | */ |
| 179 | int os_dirent_ls(const char *dirname, struct os_dirent_node **headp); |
| 180 | |
| 181 | /** |
| 182 | * Get the name of a directory entry type |
| 183 | * |
| 184 | * @param type Type to cehck |
| 185 | * @return string containing the name of that type, or "???" if none/invalid |
| 186 | */ |
| 187 | const char *os_dirent_get_typename(enum os_dirent_t type); |
| 188 | |
| 189 | /** |
| 190 | * Get the size of a file |
| 191 | * |
| 192 | * @param fname Filename to check |
| 193 | * @return size of file, or -1 if an error ocurred |
| 194 | */ |
| 195 | ssize_t os_get_filesize(const char *fname); |
| 196 | |
Mike Frysinger | 999a579 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 197 | #endif |