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