]> git.ipfire.org Git - thirdparty/u-boot.git/blame - include/os.h
cbfs: Use ulong consistently
[thirdparty/u-boot.git] / include / os.h
CommitLineData
83d290c5 1/* SPDX-License-Identifier: GPL-2.0+ */
7a9219c1 2/*
d9165153
SG
3 * Operating System Interface
4 *
5 * This provides access to useful OS routines for the sandbox architecture.
6 * They are kept in a separate file so we can include system headers.
7 *
7a9219c1 8 * Copyright (c) 2011 The Chromium OS Authors.
7a9219c1
SG
9 */
10
4f345d56
MF
11#ifndef __OS_H__
12#define __OS_H__
13
2a54d159
SG
14#include <linux/types.h>
15
94eefdee 16struct rtc_time;
70db4212
SG
17struct sandbox_state;
18
7a9219c1
SG
19/**
20 * Access to the OS read() system call
21 *
22 * \param fd File descriptor as returned by os_open()
23 * \param buf Buffer to place data
24 * \param count Number of bytes to read
25 * \return number of bytes read, or -1 on error
26 */
27ssize_t os_read(int fd, void *buf, size_t count);
28
29/**
30 * Access to the OS write() system call
31 *
32 * \param fd File descriptor as returned by os_open()
33 * \param buf Buffer containing data to write
34 * \param count Number of bytes to write
35 * \return number of bytes written, or -1 on error
36 */
37ssize_t os_write(int fd, const void *buf, size_t count);
38
e2dcefcb
MF
39/**
40 * Access to the OS lseek() system call
41 *
42 * \param fd File descriptor as returned by os_open()
43 * \param offset File offset (based on whence)
44 * \param whence Position offset is relative to (see below)
45 * \return new file offset
46 */
47off_t os_lseek(int fd, off_t offset, int whence);
48
49/* Defines for "whence" in os_lseek() */
50#define OS_SEEK_SET 0
51#define OS_SEEK_CUR 1
52#define OS_SEEK_END 2
53
7a9219c1
SG
54/**
55 * Access to the OS open() system call
56 *
57 * \param pathname Pathname of file to open
887bd416 58 * \param flags Flags, like OS_O_RDONLY, OS_O_RDWR
7a9219c1
SG
59 * \return file descriptor, or -1 on error
60 */
61int os_open(const char *pathname, int flags);
62
d9165153
SG
63#define OS_O_RDONLY 0
64#define OS_O_WRONLY 1
65#define OS_O_RDWR 2
66#define OS_O_MASK 3 /* Mask for read/write flags */
67#define OS_O_CREAT 0100
50b288ac 68#define OS_O_TRUNC 01000
d9165153 69
7a9219c1
SG
70/**
71 * Access to the OS close() system call
72 *
73 * \param fd File descriptor to close
74 * \return 0 on success, -1 on error
75 */
76int os_close(int fd);
77
cfd13e8d
SW
78/**
79 * Access to the OS unlink() system call
80 *
81 * \param pathname Path of file to delete
82 * \return 0 for success, other for error
83 */
84int os_unlink(const char *pathname);
85
7a9219c1
SG
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 */
9d72e67b 94void os_exit(int exit_code) __attribute__((noreturn));
ab06a758
MF
95
96/**
97 * Put tty into raw mode to mimic serial console better
ffb87905
SG
98 *
99 * @param fd File descriptor of stdin (normally 0)
100 * @param allow_sigs Allow Ctrl-C, Ctrl-Z to generate signals rather than
101 * be handled by U-Boot
ab06a758 102 */
ffb87905 103void os_tty_raw(int fd, bool allow_sigs);
21899b10 104
8939df09
SG
105/**
106 * Restore the tty to its original mode
107 *
108 * Call this to restore the original terminal mode, after it has been changed
109 * by os_tty_raw(). This is an internal function.
110 */
111void os_fd_restore(void);
112
21899b10
MW
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);
d99a6874 120
77595c6d 121/**
89cdb0b5 122 * Free memory previous allocated with os_malloc()
77595c6d
SG
123 *
124 * This returns the memory to the OS.
125 *
126 * \param ptr Pointer to memory block to free
127 */
347d06de 128void os_free(void *ptr);
77595c6d 129
d99a6874
MW
130/**
131 * Access to the usleep function of the os
132 *
133 * \param usec Time to sleep in micro seconds
134 */
135void os_usleep(unsigned long usec);
136
137/**
138 * Gets a monotonic increasing number of nano seconds from the OS
139 *
140 * \return A monotonic increasing time scaled in nano seconds
141 */
2a54d159 142uint64_t os_get_nsec(void);
4f345d56 143
70db4212
SG
144/**
145 * Parse arguments and update sandbox state.
146 *
147 * @param state Sandbox state to update
148 * @param argc Argument count
149 * @param argv Argument vector
150 * @return 0 if ok, and program should continue;
151 * 1 if ok, but program should stop;
152 * -1 on error: program should terminate.
153 */
154int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
155
62584db1
SG
156/*
157 * Types of directory entry that we support. See also os_dirent_typename in
158 * the C file.
159 */
160enum os_dirent_t {
161 OS_FILET_REG, /* Regular file */
162 OS_FILET_LNK, /* Symbolic link */
163 OS_FILET_DIR, /* Directory */
164 OS_FILET_UNKNOWN, /* Something else */
165
166 OS_FILET_COUNT,
167};
168
169/** A directory entry node, containing information about a single dirent */
170struct os_dirent_node {
171 struct os_dirent_node *next; /* Pointer to next node, or NULL */
172 ulong size; /* Size of file in bytes */
173 enum os_dirent_t type; /* Type of entry */
174 char name[0]; /* Name of entry */
175};
176
177/**
178 * Get a directionry listing
179 *
180 * This allocates and returns a linked list containing the directory listing.
181 *
182 * @param dirname Directory to examine
183 * @param headp Returns pointer to head of linked list, or NULL if none
184 * @return 0 if ok, -ve on error
185 */
186int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
187
86167089
SB
188/**
189 * Free directory list
190 *
191 * This frees a linked list containing a directory listing.
192 *
193 * @param node Pointer to head of linked list
194 */
195void os_dirent_free(struct os_dirent_node *node);
196
62584db1
SG
197/**
198 * Get the name of a directory entry type
199 *
86167089 200 * @param type Type to check
62584db1
SG
201 * @return string containing the name of that type, or "???" if none/invalid
202 */
203const char *os_dirent_get_typename(enum os_dirent_t type);
204
205/**
206 * Get the size of a file
207 *
208 * @param fname Filename to check
96b1046d
SR
209 * @param size size of file is returned if no error
210 * @return 0 on success or -1 if an error ocurred
62584db1 211 */
96b1046d 212int os_get_filesize(const char *fname, loff_t *size);
62584db1 213
0b189b6c
SG
214/**
215 * Write a character to the controlling OS terminal
216 *
217 * This bypasses the U-Boot console support and writes directly to the OS
218 * stdout file descriptor.
219 *
220 * @param ch Character to write
221 */
222void os_putc(int ch);
223
224/**
225 * Write a string 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 str String to write (note that \n is not appended)
231 */
232void os_puts(const char *str);
233
5c2859cd
SG
234/**
235 * Write the sandbox RAM buffer to a existing file
236 *
237 * @param fname Filename to write memory to (simple binary format)
238 * @return 0 if OK, -ve on error
239 */
240int os_write_ram_buf(const char *fname);
241
242/**
243 * Read the sandbox RAM buffer from an existing file
244 *
245 * @param fname Filename containing memory (simple binary format)
246 * @return 0 if OK, -ve on error
247 */
248int os_read_ram_buf(const char *fname);
249
47f5fcfb
SG
250/**
251 * Jump to a new executable image
252 *
253 * This uses exec() to run a new executable image, after putting it in a
254 * temporary file. The same arguments and environment are passed to this
255 * new image, with the addition of:
256 *
257 * -j <filename> Specifies the filename the image was written to. The
258 * calling image may want to delete this at some point.
259 * -m <filename> Specifies the file containing the sandbox memory
260 * (ram_buf) from this image, so that the new image can
261 * have access to this. It also means that the original
262 * memory filename passed to U-Boot will be left intact.
263 *
264 * @param dest Buffer containing executable image
265 * @param size Size of buffer
266 */
267int os_jump_to_image(const void *dest, int size);
268
d4e33f5a
SG
269/**
270 * os_find_u_boot() - Determine the path to U-Boot proper
271 *
272 * This function is intended to be called from within sandbox SPL. It uses
273 * a few heuristics to find U-Boot proper. Normally it is either in the same
274 * directory, or the directory above (since u-boot-spl is normally in an
275 * spl/ subdirectory when built).
276 *
277 * @fname: Place to put full path to U-Boot
278 * @maxlen: Maximum size of @fname
279 * @return 0 if OK, -NOSPC if the filename is too large, -ENOENT if not found
280 */
281int os_find_u_boot(char *fname, int maxlen);
282
283/**
284 * os_spl_to_uboot() - Run U-Boot proper
285 *
286 * When called from SPL, this runs U-Boot proper. The filename is obtained by
287 * calling os_find_u_boot().
288 *
289 * @fname: Full pathname to U-Boot executable
290 * @return 0 if OK, -ve on error
291 */
292int os_spl_to_uboot(const char *fname);
293
94eefdee
SG
294/**
295 * Read the current system time
296 *
297 * This reads the current Local Time and places it into the provided
298 * structure.
299 *
300 * @param rt Place to put system time
301 */
302void os_localtime(struct rtc_time *rt);
303
fe938fb0
SG
304/**
305 * os_abort() - Raise SIGABRT to exit sandbox (e.g. to debugger)
306 */
307void os_abort(void);
9f8037ea
SG
308
309/**
310 * os_mprotect_allow() - Remove write-protection on a region of memory
311 *
312 * The start and length will be page-aligned before use.
313 *
314 * @start: Region start
315 * @len: Region length in bytes
316 * @return 0 if OK, -1 on error from mprotect()
317 */
318int os_mprotect_allow(void *start, size_t len);
319
056a5cea
SG
320/**
321 * os_write_file() - Write a file to the host filesystem
322 *
323 * This can be useful when debugging for writing data out of sandbox for
324 * inspection by external tools.
325 *
326 * @name: File path to write to
327 * @buf: Data to write
328 * @size: Size of data to write
329 * @return 0 if OK, -ve on error
330 */
331int os_write_file(const char *name, const void *buf, int size);
332
566bf3a8
SG
333/**
334 * os_read_file() - Read a file from the host filesystem
335 *
336 * This can be useful when reading test data into sandbox for use by test
337 * routines. The data is allocated using os_malloc() and should be freed by
338 * the caller.
339 *
340 * @name: File path to read from
341 * @bufp: Returns buffer containing data read
342 * @sizep: Returns size of data
343 * @return 0 if OK, -ve on error
344 */
345int os_read_file(const char *name, void **bufp, int *sizep);
346
001d1885
SG
347/*
348 * os_find_text_base() - Find the text section in this running process
349 *
350 * This tries to find the address of the text section in this running process.
351 * It can be useful to map the address of functions to the address listed in
352 * the u-boot.map file.
353 *
354 * @return address if found, else NULL
355 */
356void *os_find_text_base(void);
357
4f345d56 358#endif