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 | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 16 | struct sandbox_state; |
| 17 | |
Simon Glass | cd0684f | 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 | 868aa3a | 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 | cd0684f | 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 | 60addac | 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 | cd0684f | 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 | 3196d75 | 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 | cd0684f | 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 | /** |
| 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 |
| 98 | */ |
| 99 | void os_tty_raw(int fd); |
Matthias Weisser | b5f7b47 | 2011-11-05 11:40:34 +0100 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Acquires some memory from the underlying os. |
| 103 | * |
| 104 | * \param length Number of bytes to be allocated |
| 105 | * \return Pointer to length bytes or NULL on error |
| 106 | */ |
| 107 | void *os_malloc(size_t length); |
Matthias Weisser | 0d3dd14 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Access to the usleep function of the os |
| 111 | * |
| 112 | * \param usec Time to sleep in micro seconds |
| 113 | */ |
| 114 | void os_usleep(unsigned long usec); |
| 115 | |
| 116 | /** |
| 117 | * Gets a monotonic increasing number of nano seconds from the OS |
| 118 | * |
| 119 | * \return A monotonic increasing time scaled in nano seconds |
| 120 | */ |
Simon Glass | fb4b4e8 | 2013-05-19 16:45:35 -0700 | [diff] [blame] | 121 | uint64_t os_get_nsec(void); |
Mike Frysinger | 999a579 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 122 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 123 | /** |
| 124 | * Parse arguments and update sandbox state. |
| 125 | * |
| 126 | * @param state Sandbox state to update |
| 127 | * @param argc Argument count |
| 128 | * @param argv Argument vector |
| 129 | * @return 0 if ok, and program should continue; |
| 130 | * 1 if ok, but program should stop; |
| 131 | * -1 on error: program should terminate. |
| 132 | */ |
| 133 | int os_parse_args(struct sandbox_state *state, int argc, char *argv[]); |
| 134 | |
Simon Glass | f1c45c8 | 2012-12-26 09:53:34 +0000 | [diff] [blame] | 135 | /* |
| 136 | * Types of directory entry that we support. See also os_dirent_typename in |
| 137 | * the C file. |
| 138 | */ |
| 139 | enum os_dirent_t { |
| 140 | OS_FILET_REG, /* Regular file */ |
| 141 | OS_FILET_LNK, /* Symbolic link */ |
| 142 | OS_FILET_DIR, /* Directory */ |
| 143 | OS_FILET_UNKNOWN, /* Something else */ |
| 144 | |
| 145 | OS_FILET_COUNT, |
| 146 | }; |
| 147 | |
| 148 | /** A directory entry node, containing information about a single dirent */ |
| 149 | struct os_dirent_node { |
| 150 | struct os_dirent_node *next; /* Pointer to next node, or NULL */ |
| 151 | ulong size; /* Size of file in bytes */ |
| 152 | enum os_dirent_t type; /* Type of entry */ |
| 153 | char name[0]; /* Name of entry */ |
| 154 | }; |
| 155 | |
| 156 | /** |
| 157 | * Get a directionry listing |
| 158 | * |
| 159 | * This allocates and returns a linked list containing the directory listing. |
| 160 | * |
| 161 | * @param dirname Directory to examine |
| 162 | * @param headp Returns pointer to head of linked list, or NULL if none |
| 163 | * @return 0 if ok, -ve on error |
| 164 | */ |
| 165 | int os_dirent_ls(const char *dirname, struct os_dirent_node **headp); |
| 166 | |
| 167 | /** |
| 168 | * Get the name of a directory entry type |
| 169 | * |
| 170 | * @param type Type to cehck |
| 171 | * @return string containing the name of that type, or "???" if none/invalid |
| 172 | */ |
| 173 | const char *os_dirent_get_typename(enum os_dirent_t type); |
| 174 | |
| 175 | /** |
| 176 | * Get the size of a file |
| 177 | * |
| 178 | * @param fname Filename to check |
| 179 | * @return size of file, or -1 if an error ocurred |
| 180 | */ |
| 181 | ssize_t os_get_filesize(const char *fname); |
| 182 | |
Mike Frysinger | 999a579 | 2012-01-19 22:57:29 -0500 | [diff] [blame] | 183 | #endif |