blob: b65fba430144d2587c068756fd8bfbd001799354 [file] [log] [blame]
Simon Glass7a9219c2011-10-03 19:26:44 +00001/*
Simon Glassd9165152012-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 Glass7a9219c2011-10-03 19:26:44 +00007 * Copyright (c) 2011 The Chromium OS Authors.
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Simon Glass7a9219c2011-10-03 19:26:44 +00009 */
10
Mike Frysinger4f345d52012-01-19 22:57:29 -050011#ifndef __OS_H__
12#define __OS_H__
13
Simon Glass2a54d152013-05-19 16:45:35 -070014#include <linux/types.h>
15
Simon Glass70db4212012-02-15 15:51:16 -080016struct sandbox_state;
17
Simon Glass7a9219c2011-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 Hutte1015502013-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 Glass7a9219c2011-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 Frysingere2dcefc2011-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 Glass7a9219c2011-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 Glassd9165152012-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 Glass7a9219c2011-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/**
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 Frysinger9d72e672012-02-26 17:46:30 -050094void os_exit(int exit_code) __attribute__((noreturn));
Mike Frysingerab06a752011-10-26 00:21:29 +000095
96/**
97 * Put tty into raw mode to mimic serial console better
98 */
99void os_tty_raw(int fd);
Matthias Weisser21899b12011-11-05 11:40:34 +0100100
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 */
107void *os_malloc(size_t length);
Matthias Weisserd99a6872011-11-29 12:16:40 +0100108
109/**
Simon Glass77595c62013-11-10 10:26:57 -0700110 * Free memory previous allocated with os_malloc()/os_realloc()
111 *
112 * This returns the memory to the OS.
113 *
114 * \param ptr Pointer to memory block to free
115 */
116void *os_free(void *ptr);
117
118/**
119 * Reallocate previously-allocated memory to increase/decrease space
120 *
121 * This works in a similar way to the C library realloc() function. If
122 * length is 0, then ptr is freed. Otherwise the space used by ptr is
123 * expanded or reduced depending on whether length is larger or smaller
124 * than before.
125 *
126 * If ptr is NULL, then this is similar to calling os_malloc().
127 *
128 * This function may need to move the memory block to make room for any
129 * extra space, in which case the new pointer is returned.
130 *
131 * \param ptr Pointer to memory block to reallocate
132 * \param length New length for memory block
133 * \return pointer to new memory block, or NULL on failure or if length
134 * is 0.
135 */
136void *os_realloc(void *ptr, size_t length);
137
138/**
Matthias Weisserd99a6872011-11-29 12:16:40 +0100139 * Access to the usleep function of the os
140 *
141 * \param usec Time to sleep in micro seconds
142 */
143void os_usleep(unsigned long usec);
144
145/**
146 * Gets a monotonic increasing number of nano seconds from the OS
147 *
148 * \return A monotonic increasing time scaled in nano seconds
149 */
Simon Glass2a54d152013-05-19 16:45:35 -0700150uint64_t os_get_nsec(void);
Mike Frysinger4f345d52012-01-19 22:57:29 -0500151
Simon Glass70db4212012-02-15 15:51:16 -0800152/**
153 * Parse arguments and update sandbox state.
154 *
155 * @param state Sandbox state to update
156 * @param argc Argument count
157 * @param argv Argument vector
158 * @return 0 if ok, and program should continue;
159 * 1 if ok, but program should stop;
160 * -1 on error: program should terminate.
161 */
162int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
163
Simon Glass62584db2012-12-26 09:53:34 +0000164/*
165 * Types of directory entry that we support. See also os_dirent_typename in
166 * the C file.
167 */
168enum os_dirent_t {
169 OS_FILET_REG, /* Regular file */
170 OS_FILET_LNK, /* Symbolic link */
171 OS_FILET_DIR, /* Directory */
172 OS_FILET_UNKNOWN, /* Something else */
173
174 OS_FILET_COUNT,
175};
176
177/** A directory entry node, containing information about a single dirent */
178struct os_dirent_node {
179 struct os_dirent_node *next; /* Pointer to next node, or NULL */
180 ulong size; /* Size of file in bytes */
181 enum os_dirent_t type; /* Type of entry */
182 char name[0]; /* Name of entry */
183};
184
185/**
186 * Get a directionry listing
187 *
188 * This allocates and returns a linked list containing the directory listing.
189 *
190 * @param dirname Directory to examine
191 * @param headp Returns pointer to head of linked list, or NULL if none
192 * @return 0 if ok, -ve on error
193 */
194int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
195
196/**
197 * Get the name of a directory entry type
198 *
199 * @param type Type to cehck
200 * @return string containing the name of that type, or "???" if none/invalid
201 */
202const char *os_dirent_get_typename(enum os_dirent_t type);
203
204/**
205 * Get the size of a file
206 *
207 * @param fname Filename to check
208 * @return size of file, or -1 if an error ocurred
209 */
210ssize_t os_get_filesize(const char *fname);
211
Simon Glass91b136c2013-11-10 10:27:01 -0700212/**
213 * Write a character to the controlling OS terminal
214 *
215 * This bypasses the U-Boot console support and writes directly to the OS
216 * stdout file descriptor.
217 *
218 * @param ch Character to write
219 */
220void os_putc(int ch);
221
222/**
223 * Write a string to the controlling OS terminal
224 *
225 * This bypasses the U-Boot console support and writes directly to the OS
226 * stdout file descriptor.
227 *
228 * @param str String to write (note that \n is not appended)
229 */
230void os_puts(const char *str);
231
Simon Glass5c2859c2013-11-10 10:27:03 -0700232/**
233 * Write the sandbox RAM buffer to a existing file
234 *
235 * @param fname Filename to write memory to (simple binary format)
236 * @return 0 if OK, -ve on error
237 */
238int os_write_ram_buf(const char *fname);
239
240/**
241 * Read the sandbox RAM buffer from an existing file
242 *
243 * @param fname Filename containing memory (simple binary format)
244 * @return 0 if OK, -ve on error
245 */
246int os_read_ram_buf(const char *fname);
247
Mike Frysinger4f345d52012-01-19 22:57:29 -0500248#endif