blob: 0230a7f40da07893ff2d9d92f1929e7b9cabc1d2 [file] [log] [blame]
Simon Glasscd0684f2011-10-03 19:26:44 +00001/*
Simon Glass3196d752012-02-20 23:56:58 -05002 * 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 Glasscd0684f2011-10-03 19:26:44 +00007 * Copyright (c) 2011 The Chromium OS Authors.
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Simon Glasscd0684f2011-10-03 19:26:44 +00009 */
10
Mike Frysinger999a5792012-01-19 22:57:29 -050011#ifndef __OS_H__
12#define __OS_H__
13
Simon Glassfb4b4e82013-05-19 16:45:35 -070014#include <linux/types.h>
15
Simon Glass8a3e0352012-02-15 15:51:16 -080016struct sandbox_state;
17
Simon Glasscd0684f2011-10-03 19:26:44 +000018/**
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 */
26ssize_t os_read(int fd, void *buf, size_t count);
27
28/**
Taylor Hutt868aa3a2013-02-24 17:33:13 +000029 * 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 */
36ssize_t os_read_no_block(int fd, void *buf, size_t count);
37
38/**
Simon Glasscd0684f2011-10-03 19:26:44 +000039 * 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 */
46ssize_t os_write(int fd, const void *buf, size_t count);
47
48/**
Mike Frysinger60addac2011-10-25 13:02:58 +020049 * 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 */
56off_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 Glasscd0684f2011-10-03 19:26:44 +000064 * 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 */
70int os_open(const char *pathname, int flags);
71
Simon Glass3196d752012-02-20 23:56:58 -050072#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 Glasscd0684f2011-10-03 19:26:44 +000078/**
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 */
84int os_close(int fd);
85
86/**
Stephen Warrencd5edba2014-03-01 22:18:00 -070087 * 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 */
92int os_unlink(const char *pathname);
93
94/**
Simon Glasscd0684f2011-10-03 19:26:44 +000095 * 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 Frysingerbc036d32012-02-26 17:46:30 -0500102void os_exit(int exit_code) __attribute__((noreturn));
Mike Frysingera5baaee2011-10-26 00:21:29 +0000103
104/**
105 * Put tty into raw mode to mimic serial console better
Simon Glass678ef472014-02-27 13:26:22 -0700106 *
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
Mike Frysingera5baaee2011-10-26 00:21:29 +0000110 */
Simon Glass678ef472014-02-27 13:26:22 -0700111void os_tty_raw(int fd, bool allow_sigs);
Matthias Weisserb5f7b472011-11-05 11:40:34 +0100112
113/**
114 * Acquires some memory from the underlying os.
115 *
116 * \param length Number of bytes to be allocated
117 * \return Pointer to length bytes or NULL on error
118 */
119void *os_malloc(size_t length);
Matthias Weisser0d3dd142011-11-29 12:16:40 +0100120
121/**
Simon Glass1e6594c2013-11-10 10:26:57 -0700122 * Free memory previous allocated with os_malloc()/os_realloc()
123 *
124 * This returns the memory to the OS.
125 *
126 * \param ptr Pointer to memory block to free
127 */
Masahiro Yamadaee957282014-01-15 13:06:41 +0900128void os_free(void *ptr);
Simon Glass1e6594c2013-11-10 10:26:57 -0700129
130/**
131 * Reallocate previously-allocated memory to increase/decrease space
132 *
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
136 * than before.
137 *
138 * If ptr is NULL, then this is similar to calling os_malloc().
139 *
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.
142 *
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
146 * is 0.
147 */
148void *os_realloc(void *ptr, size_t length);
149
150/**
Matthias Weisser0d3dd142011-11-29 12:16:40 +0100151 * Access to the usleep function of the os
152 *
153 * \param usec Time to sleep in micro seconds
154 */
155void os_usleep(unsigned long usec);
156
157/**
158 * Gets a monotonic increasing number of nano seconds from the OS
159 *
160 * \return A monotonic increasing time scaled in nano seconds
161 */
Simon Glassfb4b4e82013-05-19 16:45:35 -0700162uint64_t os_get_nsec(void);
Mike Frysinger999a5792012-01-19 22:57:29 -0500163
Simon Glass8a3e0352012-02-15 15:51:16 -0800164/**
165 * Parse arguments and update sandbox state.
166 *
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.
173 */
174int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
175
Simon Glassf1c45c82012-12-26 09:53:34 +0000176/*
177 * Types of directory entry that we support. See also os_dirent_typename in
178 * the C file.
179 */
180enum os_dirent_t {
181 OS_FILET_REG, /* Regular file */
182 OS_FILET_LNK, /* Symbolic link */
183 OS_FILET_DIR, /* Directory */
184 OS_FILET_UNKNOWN, /* Something else */
185
186 OS_FILET_COUNT,
187};
188
189/** A directory entry node, containing information about a single dirent */
190struct 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 */
195};
196
197/**
198 * Get a directionry listing
199 *
200 * This allocates and returns a linked list containing the directory listing.
201 *
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
205 */
206int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
207
208/**
209 * Get the name of a directory entry type
210 *
211 * @param type Type to cehck
212 * @return string containing the name of that type, or "???" if none/invalid
213 */
214const char *os_dirent_get_typename(enum os_dirent_t type);
215
216/**
217 * Get the size of a file
218 *
219 * @param fname Filename to check
220 * @return size of file, or -1 if an error ocurred
221 */
222ssize_t os_get_filesize(const char *fname);
223
Simon Glass3e9fd242013-11-10 10:27:01 -0700224/**
225 * Write a character to the controlling OS terminal
226 *
227 * This bypasses the U-Boot console support and writes directly to the OS
228 * stdout file descriptor.
229 *
230 * @param ch Character to write
231 */
232void os_putc(int ch);
233
234/**
235 * Write a string to the controlling OS terminal
236 *
237 * This bypasses the U-Boot console support and writes directly to the OS
238 * stdout file descriptor.
239 *
240 * @param str String to write (note that \n is not appended)
241 */
242void os_puts(const char *str);
243
Simon Glass9dd10bf2013-11-10 10:27:03 -0700244/**
245 * Write the sandbox RAM buffer to a existing file
246 *
247 * @param fname Filename to write memory to (simple binary format)
248 * @return 0 if OK, -ve on error
249 */
250int os_write_ram_buf(const char *fname);
251
252/**
253 * Read the sandbox RAM buffer from an existing file
254 *
255 * @param fname Filename containing memory (simple binary format)
256 * @return 0 if OK, -ve on error
257 */
258int os_read_ram_buf(const char *fname);
259
Simon Glass860b7d92014-02-27 13:26:15 -0700260/**
261 * Jump to a new executable image
262 *
263 * This uses exec() to run a new executable image, after putting it in a
264 * temporary file. The same arguments and environment are passed to this
265 * new image, with the addition of:
266 *
267 * -j <filename> Specifies the filename the image was written to. The
268 * calling image may want to delete this at some point.
269 * -m <filename> Specifies the file containing the sandbox memory
270 * (ram_buf) from this image, so that the new image can
271 * have access to this. It also means that the original
272 * memory filename passed to U-Boot will be left intact.
273 *
274 * @param dest Buffer containing executable image
275 * @param size Size of buffer
276 */
277int os_jump_to_image(const void *dest, int size);
278
Mike Frysinger999a5792012-01-19 22:57:29 -0500279#endif