blob: 699682a4089beed0869c88fbf5974243aa647221 [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.
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 Frysinger999a5792012-01-19 22:57:29 -050027#ifndef __OS_H__
28#define __OS_H__
29
Simon Glass8a3e0352012-02-15 15:51:16 -080030struct sandbox_state;
31
Simon Glasscd0684f2011-10-03 19:26:44 +000032/**
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 */
40ssize_t os_read(int fd, void *buf, size_t count);
41
42/**
43 * Access to the OS write() system call
44 *
45 * \param fd File descriptor as returned by os_open()
46 * \param buf Buffer containing data to write
47 * \param count Number of bytes to write
48 * \return number of bytes written, or -1 on error
49 */
50ssize_t os_write(int fd, const void *buf, size_t count);
51
52/**
Mike Frysinger60addac2011-10-25 13:02:58 +020053 * Access to the OS lseek() system call
54 *
55 * \param fd File descriptor as returned by os_open()
56 * \param offset File offset (based on whence)
57 * \param whence Position offset is relative to (see below)
58 * \return new file offset
59 */
60off_t os_lseek(int fd, off_t offset, int whence);
61
62/* Defines for "whence" in os_lseek() */
63#define OS_SEEK_SET 0
64#define OS_SEEK_CUR 1
65#define OS_SEEK_END 2
66
67/**
Simon Glasscd0684f2011-10-03 19:26:44 +000068 * Access to the OS open() system call
69 *
70 * \param pathname Pathname of file to open
71 * \param flags Flags, like O_RDONLY, O_RDWR
72 * \return file descriptor, or -1 on error
73 */
74int os_open(const char *pathname, int flags);
75
Simon Glass3196d752012-02-20 23:56:58 -050076#define OS_O_RDONLY 0
77#define OS_O_WRONLY 1
78#define OS_O_RDWR 2
79#define OS_O_MASK 3 /* Mask for read/write flags */
80#define OS_O_CREAT 0100
81
Simon Glasscd0684f2011-10-03 19:26:44 +000082/**
83 * Access to the OS close() system call
84 *
85 * \param fd File descriptor to close
86 * \return 0 on success, -1 on error
87 */
88int os_close(int fd);
89
90/**
91 * Access to the OS exit() system call
92 *
93 * This exits with the supplied return code, which should be 0 to indicate
94 * success.
95 *
96 * @param exit_code exit code for U-Boot
97 */
Mike Frysingerbc036d32012-02-26 17:46:30 -050098void os_exit(int exit_code) __attribute__((noreturn));
Mike Frysingera5baaee2011-10-26 00:21:29 +000099
100/**
101 * Put tty into raw mode to mimic serial console better
102 */
103void os_tty_raw(int fd);
Matthias Weisserb5f7b472011-11-05 11:40:34 +0100104
105/**
106 * Acquires some memory from the underlying os.
107 *
108 * \param length Number of bytes to be allocated
109 * \return Pointer to length bytes or NULL on error
110 */
111void *os_malloc(size_t length);
Matthias Weisser0d3dd142011-11-29 12:16:40 +0100112
113/**
114 * Access to the usleep function of the os
115 *
116 * \param usec Time to sleep in micro seconds
117 */
118void os_usleep(unsigned long usec);
119
120/**
121 * Gets a monotonic increasing number of nano seconds from the OS
122 *
123 * \return A monotonic increasing time scaled in nano seconds
124 */
125u64 os_get_nsec(void);
Mike Frysinger999a5792012-01-19 22:57:29 -0500126
Simon Glass8a3e0352012-02-15 15:51:16 -0800127/**
128 * Parse arguments and update sandbox state.
129 *
130 * @param state Sandbox state to update
131 * @param argc Argument count
132 * @param argv Argument vector
133 * @return 0 if ok, and program should continue;
134 * 1 if ok, but program should stop;
135 * -1 on error: program should terminate.
136 */
137int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
138
Mike Frysinger999a5792012-01-19 22:57:29 -0500139#endif