2 * Operating System Interface
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.
7 * Copyright (c) 2011 The Chromium OS Authors.
8 * SPDX-License-Identifier: GPL-2.0+
14 #include <linux/types.h>
19 * Access to the OS read() system call
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
26 ssize_t os_read(int fd, void *buf, size_t count);
29 * Access to the OS read() system call with non-blocking access
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
36 ssize_t os_read_no_block(int fd, void *buf, size_t count);
39 * Access to the OS write() system call
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
46 ssize_t os_write(int fd, const void *buf, size_t count);
49 * Access to the OS lseek() system call
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
56 off_t os_lseek(int fd, off_t offset, int whence);
58 /* Defines for "whence" in os_lseek() */
64 * Access to the OS open() system call
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
70 int os_open(const char *pathname, int flags);
75 #define OS_O_MASK 3 /* Mask for read/write flags */
76 #define OS_O_CREAT 0100
79 * Access to the OS close() system call
81 * \param fd File descriptor to close
82 * \return 0 on success, -1 on error
87 * Access to the OS unlink() system call
89 * \param pathname Path of file to delete
90 * \return 0 for success, other for error
92 int os_unlink(const char *pathname);
95 * Access to the OS exit() system call
97 * This exits with the supplied return code, which should be 0 to indicate
100 * @param exit_code exit code for U-Boot
102 void os_exit(int exit_code) __attribute__((noreturn));
105 * Put tty into raw mode to mimic serial console better
107 * @param fd File descriptor of stdin (normally 0)
108 * @param allow_sigs Allow Ctrl-C, Ctrl-Z to generate signals rather than
109 * be handled by U-Boot
111 void os_tty_raw(int fd, bool allow_sigs);
114 * Acquires some memory from the underlying os.
116 * \param length Number of bytes to be allocated
117 * \return Pointer to length bytes or NULL on error
119 void *os_malloc(size_t length);
122 * Free memory previous allocated with os_malloc()/os_realloc()
124 * This returns the memory to the OS.
126 * \param ptr Pointer to memory block to free
128 void os_free(void *ptr);
131 * Reallocate previously-allocated memory to increase/decrease space
133 * This works in a similar way to the C library realloc() function. If
134 * length is 0, then ptr is freed. Otherwise the space used by ptr is
135 * expanded or reduced depending on whether length is larger or smaller
138 * If ptr is NULL, then this is similar to calling os_malloc().
140 * This function may need to move the memory block to make room for any
141 * extra space, in which case the new pointer is returned.
143 * \param ptr Pointer to memory block to reallocate
144 * \param length New length for memory block
145 * \return pointer to new memory block, or NULL on failure or if length
148 void *os_realloc(void *ptr, size_t length);
151 * Access to the usleep function of the os
153 * \param usec Time to sleep in micro seconds
155 void os_usleep(unsigned long usec);
158 * Gets a monotonic increasing number of nano seconds from the OS
160 * \return A monotonic increasing time scaled in nano seconds
162 uint64_t os_get_nsec(void);
165 * Parse arguments and update sandbox state.
167 * @param state Sandbox state to update
168 * @param argc Argument count
169 * @param argv Argument vector
170 * @return 0 if ok, and program should continue;
171 * 1 if ok, but program should stop;
172 * -1 on error: program should terminate.
174 int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
177 * Types of directory entry that we support. See also os_dirent_typename in
181 OS_FILET_REG, /* Regular file */
182 OS_FILET_LNK, /* Symbolic link */
183 OS_FILET_DIR, /* Directory */
184 OS_FILET_UNKNOWN, /* Something else */
189 /** A directory entry node, containing information about a single dirent */
190 struct os_dirent_node {
191 struct os_dirent_node *next; /* Pointer to next node, or NULL */
192 ulong size; /* Size of file in bytes */
193 enum os_dirent_t type; /* Type of entry */
194 char name[0]; /* Name of entry */
198 * Get a directionry listing
200 * This allocates and returns a linked list containing the directory listing.
202 * @param dirname Directory to examine
203 * @param headp Returns pointer to head of linked list, or NULL if none
204 * @return 0 if ok, -ve on error
206 int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
209 * Get the name of a directory entry type
211 * @param type Type to cehck
212 * @return string containing the name of that type, or "???" if none/invalid
214 const char *os_dirent_get_typename(enum os_dirent_t type);
217 * Get the size of a file
219 * @param fname Filename to check
220 * @param size size of file is returned if no error
221 * @return 0 on success or -1 if an error ocurred
223 int os_get_filesize(const char *fname, loff_t *size);
226 * Write a character to the controlling OS terminal
228 * This bypasses the U-Boot console support and writes directly to the OS
229 * stdout file descriptor.
231 * @param ch Character to write
233 void os_putc(int ch);
236 * Write a string to the controlling OS terminal
238 * This bypasses the U-Boot console support and writes directly to the OS
239 * stdout file descriptor.
241 * @param str String to write (note that \n is not appended)
243 void os_puts(const char *str);
246 * Write the sandbox RAM buffer to a existing file
248 * @param fname Filename to write memory to (simple binary format)
249 * @return 0 if OK, -ve on error
251 int os_write_ram_buf(const char *fname);
254 * Read the sandbox RAM buffer from an existing file
256 * @param fname Filename containing memory (simple binary format)
257 * @return 0 if OK, -ve on error
259 int os_read_ram_buf(const char *fname);
262 * Jump to a new executable image
264 * This uses exec() to run a new executable image, after putting it in a
265 * temporary file. The same arguments and environment are passed to this
266 * new image, with the addition of:
268 * -j <filename> Specifies the filename the image was written to. The
269 * calling image may want to delete this at some point.
270 * -m <filename> Specifies the file containing the sandbox memory
271 * (ram_buf) from this image, so that the new image can
272 * have access to this. It also means that the original
273 * memory filename passed to U-Boot will be left intact.
275 * @param dest Buffer containing executable image
276 * @param size Size of buffer
278 int os_jump_to_image(const void *dest, int size);