blob: d2a4afeca0f06a12a01265e8515d31a56dc66f66 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass7a9219c2011-10-03 19:26:44 +00002/*
Simon Glassd9165152012-02-20 23:56:58 -05003 * 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 *
Simon Glass7a9219c2011-10-03 19:26:44 +00008 * Copyright (c) 2011 The Chromium OS Authors.
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 Glass94eefde2015-04-20 12:37:22 -060016struct rtc_time;
Simon Glass70db4212012-02-15 15:51:16 -080017struct sandbox_state;
18
Simon Glass7a9219c2011-10-03 19:26:44 +000019/**
20 * Access to the OS read() system call
21 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +010022 * @fd: File descriptor as returned by os_open()
23 * @buf: Buffer to place data
24 * @count: Number of bytes to read
25 * Return: number of bytes read, or -1 on error
Simon Glass7a9219c2011-10-03 19:26:44 +000026 */
27ssize_t os_read(int fd, void *buf, size_t count);
28
29/**
30 * Access to the OS write() system call
31 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +010032 * @fd: File descriptor as returned by os_open()
33 * @buf: Buffer containing data to write
34 * @count: Number of bytes to write
35 * Return: number of bytes written, or -1 on error
Simon Glass7a9219c2011-10-03 19:26:44 +000036 */
37ssize_t os_write(int fd, const void *buf, size_t count);
38
39/**
Mike Frysingere2dcefc2011-10-25 13:02:58 +020040 * Access to the OS lseek() system call
41 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +010042 * @fd: File descriptor as returned by os_open()
43 * @offset: File offset (based on whence)
44 * @whence: Position offset is relative to (see below)
45 * Return: new file offset
Mike Frysingere2dcefc2011-10-25 13:02:58 +020046 */
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
54/**
Simon Glass7a9219c2011-10-03 19:26:44 +000055 * Access to the OS open() system call
56 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +010057 * @pathname: Pathname of file to open
58 * @flags: Flags, like OS_O_RDONLY, OS_O_RDWR
59 * Return: file descriptor, or -1 on error
Simon Glass7a9219c2011-10-03 19:26:44 +000060 */
61int os_open(const char *pathname, int flags);
62
Simon Glassd9165152012-02-20 23:56:58 -050063#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
Simon Glass50b288a2018-10-01 11:55:07 -060068#define OS_O_TRUNC 01000
Simon Glassd9165152012-02-20 23:56:58 -050069
Simon Glass7a9219c2011-10-03 19:26:44 +000070/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +010071 * os_close() - access to the OS close() system call
Simon Glass7a9219c2011-10-03 19:26:44 +000072 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +010073 * @fd: File descriptor to close
74 * Return: 0 on success, -1 on error
Simon Glass7a9219c2011-10-03 19:26:44 +000075 */
76int os_close(int fd);
77
78/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +010079 * os_unlink() - access to the OS unlink() system call
Stephen Warrencfd13e82014-03-01 22:18:00 -070080 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +010081 * @pathname: Path of file to delete
82 * Return: 0 for success, other for error
Stephen Warrencfd13e82014-03-01 22:18:00 -070083 */
84int os_unlink(const char *pathname);
85
86/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +010087 * os_exit() - access to the OS exit() system call
Simon Glass7a9219c2011-10-03 19:26:44 +000088 *
89 * This exits with the supplied return code, which should be 0 to indicate
90 * success.
91 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +010092 * @exit_code: exit code for U-Boot
Simon Glass7a9219c2011-10-03 19:26:44 +000093 */
Mike Frysinger9d72e672012-02-26 17:46:30 -050094void os_exit(int exit_code) __attribute__((noreturn));
Mike Frysingerab06a752011-10-26 00:21:29 +000095
96/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +010097 * os_tty_raw() - put tty into raw mode to mimic serial console better
Simon Glassffb87902014-02-27 13:26:22 -070098 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +010099 * @fd: File descriptor of stdin (normally 0)
100 * @allow_sigs: Allow Ctrl-C, Ctrl-Z to generate signals rather than
101 * be handled by U-Boot
Mike Frysingerab06a752011-10-26 00:21:29 +0000102 */
Simon Glassffb87902014-02-27 13:26:22 -0700103void os_tty_raw(int fd, bool allow_sigs);
Matthias Weisser21899b12011-11-05 11:40:34 +0100104
105/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100106 * os_fs_restore() - restore the tty to its original mode
Simon Glass8939df02015-05-10 21:07:27 -0600107 *
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
113/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100114 * os_malloc() - aquires some memory from the underlying os.
Matthias Weisser21899b12011-11-05 11:40:34 +0100115 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100116 * @length: Number of bytes to be allocated
Simon Glass14e46df2021-02-06 09:57:32 -0700117 * Return: Pointer to length bytes or NULL if @length is 0 or on error
Matthias Weisser21899b12011-11-05 11:40:34 +0100118 */
119void *os_malloc(size_t length);
Matthias Weisserd99a6872011-11-29 12:16:40 +0100120
121/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100122 * os_free() - free memory previous allocated with os_malloc()
Simon Glass77595c62013-11-10 10:26:57 -0700123 *
124 * This returns the memory to the OS.
125 *
Simon Glassb308d9f2021-02-06 09:57:33 -0700126 * @ptr: Pointer to memory block to free. If this is NULL then this
127 * function does nothing
Simon Glass77595c62013-11-10 10:26:57 -0700128 */
Masahiro Yamada347d06d2014-01-15 13:06:41 +0900129void os_free(void *ptr);
Simon Glass77595c62013-11-10 10:26:57 -0700130
131/**
Simon Glass14e46df2021-02-06 09:57:32 -0700132 * os_realloc() - reallocate memory
133 *
134 * This follows the semantics of realloc(), so can perform an os_malloc() or
135 * os_free() depending on @ptr and @length.
136 *
137 * Return: Pointer to reallocated memory or NULL if @length is 0
138 */
139void *os_realloc(void *ptr, size_t length);
140
141/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100142 * os_usleep() - access to the usleep function of the os
Matthias Weisserd99a6872011-11-29 12:16:40 +0100143 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100144 * @usec: time to sleep in micro seconds
Matthias Weisserd99a6872011-11-29 12:16:40 +0100145 */
146void os_usleep(unsigned long usec);
147
148/**
149 * Gets a monotonic increasing number of nano seconds from the OS
150 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100151 * Return: a monotonic increasing time scaled in nano seconds
Matthias Weisserd99a6872011-11-29 12:16:40 +0100152 */
Simon Glass2a54d152013-05-19 16:45:35 -0700153uint64_t os_get_nsec(void);
Mike Frysinger4f345d52012-01-19 22:57:29 -0500154
Simon Glass70db4212012-02-15 15:51:16 -0800155/**
156 * Parse arguments and update sandbox state.
157 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100158 * @state: sandbox state to update
159 * @argc: argument count
160 * @argv: argument vector
161 * Return:
162 * * 0 if ok, and program should continue
163 * * 1 if ok, but program should stop
164 * * -1 on error: program should terminate
Simon Glass70db4212012-02-15 15:51:16 -0800165 */
166int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
167
Simon Glass62584db2012-12-26 09:53:34 +0000168/*
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100169 * enum os_dirent_t - type of directory entry
170 *
Simon Glass62584db2012-12-26 09:53:34 +0000171 * Types of directory entry that we support. See also os_dirent_typename in
172 * the C file.
173 */
174enum os_dirent_t {
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100175 /**
176 * @OS_FILET_REG: regular file
177 */
178 OS_FILET_REG,
179 /**
180 * @OS_FILET_LNK: symbolic link
181 */
182 OS_FILET_LNK,
183 /**
184 * @OS_FILET_DIR: directory
185 */
186 OS_FILET_DIR,
187 /**
188 * @OS_FILET_UNKNOWN: something else
189 */
190 OS_FILET_UNKNOWN,
191 /**
192 * @OS_FILET_COUNT: number of directory entry types
193 */
Simon Glass62584db2012-12-26 09:53:34 +0000194 OS_FILET_COUNT,
195};
196
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100197/**
198 * struct os_dirent_node - directory node
199 *
200 * A directory entry node, containing information about a single dirent
201 *
202 */
Simon Glass62584db2012-12-26 09:53:34 +0000203struct os_dirent_node {
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100204 /**
205 * @next: pointer to next node, or NULL
206 */
207 struct os_dirent_node *next;
208 /**
209 * @size: size of file in bytes
210 */
211 ulong size;
212 /**
213 * @type: type of entry
214 */
215 enum os_dirent_t type;
216 /**
217 * @name: name of entry
218 */
219 char name[0];
Simon Glass62584db2012-12-26 09:53:34 +0000220};
221
222/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100223 * os_dirent_ls() - get a directory listing
Simon Glass62584db2012-12-26 09:53:34 +0000224 *
225 * This allocates and returns a linked list containing the directory listing.
226 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100227 * @dirname: directory to examine
228 * @headp: on return pointer to head of linked list, or NULL if none
229 * Return: 0 if ok, -ve on error
Simon Glass62584db2012-12-26 09:53:34 +0000230 */
231int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
232
233/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100234 * os_dirent_free() - free directory list
Stefan Brüns86167082016-10-01 20:41:38 +0200235 *
236 * This frees a linked list containing a directory listing.
237 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100238 * @node: pointer to head of linked list
Stefan Brüns86167082016-10-01 20:41:38 +0200239 */
240void os_dirent_free(struct os_dirent_node *node);
241
242/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100243 * os_dirent_get_typename() - get the name of a directory entry type
Simon Glass62584db2012-12-26 09:53:34 +0000244 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100245 * @type: type to check
246 * Return:
247 * string containing the name of that type,
248 * or "???" if none/invalid
Simon Glass62584db2012-12-26 09:53:34 +0000249 */
250const char *os_dirent_get_typename(enum os_dirent_t type);
251
252/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100253 * os_get_filesize() - get the size of a file
Simon Glass62584db2012-12-26 09:53:34 +0000254 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100255 * @fname: filename to check
256 * @size: size of file is returned if no error
257 * Return: 0 on success or -1 if an error ocurred
Simon Glass62584db2012-12-26 09:53:34 +0000258 */
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800259int os_get_filesize(const char *fname, loff_t *size);
Simon Glass62584db2012-12-26 09:53:34 +0000260
Simon Glass91b136c2013-11-10 10:27:01 -0700261/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100262 * os_putc() - write a character to the controlling OS terminal
Simon Glass0b189b62017-12-04 13:48:17 -0700263 *
264 * This bypasses the U-Boot console support and writes directly to the OS
265 * stdout file descriptor.
266 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100267 * @ch: haracter to write
Simon Glass0b189b62017-12-04 13:48:17 -0700268 */
269void os_putc(int ch);
270
271/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100272 * os_puts() - write a string to the controlling OS terminal
Simon Glass0b189b62017-12-04 13:48:17 -0700273 *
274 * This bypasses the U-Boot console support and writes directly to the OS
275 * stdout file descriptor.
276 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100277 * @str: string to write (note that \n is not appended)
Simon Glass0b189b62017-12-04 13:48:17 -0700278 */
279void os_puts(const char *str);
280
281/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100282 * os_write_ram_buf() - write the sandbox RAM buffer to a existing file
Simon Glass5c2859c2013-11-10 10:27:03 -0700283 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100284 * @fname: filename to write memory to (simple binary format)
285 * Return: 0 if OK, -ve on error
Simon Glass5c2859c2013-11-10 10:27:03 -0700286 */
287int os_write_ram_buf(const char *fname);
288
289/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100290 * os_read_ram_buf() - read the sandbox RAM buffer from an existing file
Simon Glass5c2859c2013-11-10 10:27:03 -0700291 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100292 * @fname: filename containing memory (simple binary format)
293 * Return: 0 if OK, -ve on error
Simon Glass5c2859c2013-11-10 10:27:03 -0700294 */
295int os_read_ram_buf(const char *fname);
296
Simon Glass47f5fcf2014-02-27 13:26:15 -0700297/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100298 * os_jump_to_image() - jump to a new executable image
Simon Glass47f5fcf2014-02-27 13:26:15 -0700299 *
300 * This uses exec() to run a new executable image, after putting it in a
301 * temporary file. The same arguments and environment are passed to this
302 * new image, with the addition of:
303 *
304 * -j <filename> Specifies the filename the image was written to. The
305 * calling image may want to delete this at some point.
306 * -m <filename> Specifies the file containing the sandbox memory
307 * (ram_buf) from this image, so that the new image can
308 * have access to this. It also means that the original
309 * memory filename passed to U-Boot will be left intact.
310 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100311 * @dest: buffer containing executable image
312 * @size: size of buffer
313 * Return: 0 if OK, -ve on error
Simon Glass47f5fcf2014-02-27 13:26:15 -0700314 */
315int os_jump_to_image(const void *dest, int size);
316
Simon Glass94eefde2015-04-20 12:37:22 -0600317/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100318 * os_find_u_boot() - determine the path to U-Boot proper
Simon Glassd4e33f52016-07-04 11:57:45 -0600319 *
320 * This function is intended to be called from within sandbox SPL. It uses
321 * a few heuristics to find U-Boot proper. Normally it is either in the same
322 * directory, or the directory above (since u-boot-spl is normally in an
323 * spl/ subdirectory when built).
324 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100325 * @fname: place to put full path to U-Boot
326 * @maxlen: maximum size of @fname
327 * Return: 0 if OK, -NOSPC if the filename is too large, -ENOENT if not found
Simon Glassd4e33f52016-07-04 11:57:45 -0600328 */
329int os_find_u_boot(char *fname, int maxlen);
330
331/**
332 * os_spl_to_uboot() - Run U-Boot proper
333 *
334 * When called from SPL, this runs U-Boot proper. The filename is obtained by
335 * calling os_find_u_boot().
336 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100337 * @fname: full pathname to U-Boot executable
338 * Return: 0 if OK, -ve on error
Simon Glassd4e33f52016-07-04 11:57:45 -0600339 */
340int os_spl_to_uboot(const char *fname);
341
342/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100343 * os_localtime() - read the current system time
Simon Glass94eefde2015-04-20 12:37:22 -0600344 *
345 * This reads the current Local Time and places it into the provided
346 * structure.
347 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100348 * @rt: place to put system time
Simon Glass94eefde2015-04-20 12:37:22 -0600349 */
350void os_localtime(struct rtc_time *rt);
351
Simon Glass30eef212018-05-16 09:42:22 -0600352/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100353 * os_abort() - raise SIGABRT to exit sandbox (e.g. to debugger)
Simon Glassfe938fb2018-09-15 00:50:55 -0600354 */
Heinrich Schuchardtc30a7092021-02-01 01:24:10 +0100355void os_abort(void) __attribute__((noreturn));
Simon Glass9f8037e2018-10-01 21:12:32 -0600356
357/**
358 * os_mprotect_allow() - Remove write-protection on a region of memory
359 *
360 * The start and length will be page-aligned before use.
361 *
362 * @start: Region start
363 * @len: Region length in bytes
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100364 * Return: 0 if OK, -1 on error from mprotect()
Simon Glass9f8037e2018-10-01 21:12:32 -0600365 */
366int os_mprotect_allow(void *start, size_t len);
367
Simon Glass056a5ce2018-10-01 11:55:08 -0600368/**
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100369 * os_write_file() - write a file to the host filesystem
Simon Glass056a5ce2018-10-01 11:55:08 -0600370 *
371 * This can be useful when debugging for writing data out of sandbox for
372 * inspection by external tools.
373 *
374 * @name: File path to write to
375 * @buf: Data to write
376 * @size: Size of data to write
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100377 * Return: 0 if OK, -ve on error
Simon Glass056a5ce2018-10-01 11:55:08 -0600378 */
379int os_write_file(const char *name, const void *buf, int size);
380
Simon Glass566bf3a2018-11-06 15:21:25 -0700381/**
382 * os_read_file() - Read a file from the host filesystem
383 *
384 * This can be useful when reading test data into sandbox for use by test
385 * routines. The data is allocated using os_malloc() and should be freed by
386 * the caller.
387 *
388 * @name: File path to read from
389 * @bufp: Returns buffer containing data read
390 * @sizep: Returns size of data
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100391 * Return: 0 if OK, -ve on error
Simon Glass566bf3a2018-11-06 15:21:25 -0700392 */
393int os_read_file(const char *name, void **bufp, int *sizep);
394
Simon Glass001d1882019-04-08 13:20:41 -0600395/*
396 * os_find_text_base() - Find the text section in this running process
397 *
398 * This tries to find the address of the text section in this running process.
399 * It can be useful to map the address of functions to the address listed in
400 * the u-boot.map file.
401 *
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100402 * Return: address if found, else NULL
Simon Glass001d1882019-04-08 13:20:41 -0600403 */
404void *os_find_text_base(void);
405
Heinrich Schuchardt329dccc2020-10-27 20:29:25 +0100406/**
407 * os_relaunch() - restart the sandbox
408 *
409 * This functions is used to implement the cold reboot of the sand box.
Heinrich Schuchardt063790c2020-10-27 20:29:27 +0100410 * @argv\[0] specifies the binary that is started while the calling process
Heinrich Schuchardt329dccc2020-10-27 20:29:25 +0100411 * stops immediately. If the new binary cannot be started, the process is
412 * terminated and 1 is set as shell return code.
413 *
414 * The PID of the process stays the same. All file descriptors that have not
415 * been opened with O_CLOEXEC stay open including stdin, stdout, stderr.
416 *
417 * @argv: NULL terminated list of command line parameters
418 */
419void os_relaunch(char *argv[]);
420
Heinrich Schuchardtb46f30a2020-11-12 00:29:56 +0100421/**
422 * os_setup_signal_handlers() - setup signal handlers
423 *
424 * Install signal handlers for SIGBUS and SIGSEGV.
425 *
426 * Return: 0 for success
427 */
428int os_setup_signal_handlers(void);
429
430/**
431 * os_signal_action() - handle a signal
432 *
433 * @sig: signal
434 * @pc: program counter
435 */
436void os_signal_action(int sig, unsigned long pc);
437
Heinrich Schuchardt43db0752020-12-30 18:07:48 +0100438/**
439 * os_get_time_offset() - get time offset
440 *
441 * Get the time offset from environment variable UBOOT_SB_TIME_OFFSET.
442 *
443 * Return: offset in seconds
444 */
445long os_get_time_offset(void);
446
447/**
448 * os_set_time_offset() - set time offset
449 *
450 * Save the time offset in environment variable UBOOT_SB_TIME_OFFSET.
451 *
452 * @offset: offset in seconds
453 */
454void os_set_time_offset(long offset);
455
Mike Frysinger4f345d52012-01-19 22:57:29 -0500456#endif