1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Operating System Interface
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.
8 * Copyright (c) 2011 The Chromium OS Authors.
14 #include <linux/types.h>
20 * Access to the OS read() system call
22 * @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
27 ssize_t os_read(int fd, void *buf, size_t count);
30 * Access to the OS write() system call
32 * @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
37 ssize_t os_write(int fd, const void *buf, size_t count);
40 * Access to the OS lseek() system call
42 * @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
47 off_t os_lseek(int fd, off_t offset, int whence);
49 /* Defines for "whence" in os_lseek() */
55 * Access to the OS open() system call
57 * @pathname: Pathname of file to open
58 * @flags: Flags, like OS_O_RDONLY, OS_O_RDWR
59 * Return: file descriptor, or -1 on error
61 int os_open(const char *pathname, int flags);
66 #define OS_O_MASK 3 /* Mask for read/write flags */
67 #define OS_O_CREAT 0100
68 #define OS_O_TRUNC 01000
71 * os_close() - access to the OS close() system call
73 * @fd: File descriptor to close
74 * Return: 0 on success, -1 on error
79 * os_unlink() - access to the OS unlink() system call
81 * @pathname: Path of file to delete
82 * Return: 0 for success, other for error
84 int os_unlink(const char *pathname);
87 * os_exit() - access to the OS exit() system call
89 * This exits with the supplied return code, which should be 0 to indicate
92 * @exit_code: exit code for U-Boot
94 void os_exit(int exit_code) __attribute__((noreturn));
97 * os_tty_raw() - put tty into raw mode to mimic serial console better
99 * @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
103 void os_tty_raw(int fd, bool allow_sigs);
106 * os_fs_restore() - restore the tty to its original mode
108 * Call this to restore the original terminal mode, after it has been changed
109 * by os_tty_raw(). This is an internal function.
111 void os_fd_restore(void);
114 * os_malloc() - aquires some memory from the underlying os.
116 * @length: Number of bytes to be allocated
117 * Return: Pointer to length bytes or NULL if @length is 0 or on error
119 void *os_malloc(size_t length);
122 * os_free() - free memory previous allocated with os_malloc()
124 * This returns the memory to the OS.
126 * @ptr: Pointer to memory block to free. If this is NULL then this
127 * function does nothing
129 void os_free(void *ptr);
132 * os_realloc() - reallocate memory
134 * This follows the semantics of realloc(), so can perform an os_malloc() or
135 * os_free() depending on @ptr and @length.
137 * @ptr: pointer to previously allocated memory of NULL
138 * @length: number of bytes to allocate
139 * Return: pointer to reallocated memory or NULL if @length is 0
141 void *os_realloc(void *ptr, size_t length);
144 * os_usleep() - access to the usleep function of the os
146 * @usec: time to sleep in micro seconds
148 void os_usleep(unsigned long usec);
151 * Gets a monotonic increasing number of nano seconds from the OS
153 * Return: a monotonic increasing time scaled in nano seconds
155 uint64_t os_get_nsec(void);
158 * Parse arguments and update sandbox state.
160 * @state: sandbox state to update
161 * @argc: argument count
162 * @argv: argument vector
164 * * 0 if ok, and program should continue
165 * * 1 if ok, but program should stop
166 * * -1 on error: program should terminate
168 int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
171 * enum os_dirent_t - type of directory entry
173 * Types of directory entry that we support. See also os_dirent_typename in
178 * @OS_FILET_REG: regular file
182 * @OS_FILET_LNK: symbolic link
186 * @OS_FILET_DIR: directory
190 * @OS_FILET_UNKNOWN: something else
194 * @OS_FILET_COUNT: number of directory entry types
200 * struct os_dirent_node - directory node
202 * A directory entry node, containing information about a single dirent
205 struct os_dirent_node {
207 * @next: pointer to next node, or NULL
209 struct os_dirent_node *next;
211 * @size: size of file in bytes
215 * @type: type of entry
217 enum os_dirent_t type;
219 * @name: name of entry
225 * os_dirent_ls() - get a directory listing
227 * This allocates and returns a linked list containing the directory listing.
229 * @dirname: directory to examine
230 * @headp: on return pointer to head of linked list, or NULL if none
231 * Return: 0 if ok, -ve on error
233 int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
236 * os_dirent_free() - free directory list
238 * This frees a linked list containing a directory listing.
240 * @node: pointer to head of linked list
242 void os_dirent_free(struct os_dirent_node *node);
245 * os_dirent_get_typename() - get the name of a directory entry type
247 * @type: type to check
249 * string containing the name of that type,
250 * or "???" if none/invalid
252 const char *os_dirent_get_typename(enum os_dirent_t type);
255 * os_get_filesize() - get the size of a file
257 * @fname: filename to check
258 * @size: size of file is returned if no error
259 * Return: 0 on success or -1 if an error ocurred
261 int os_get_filesize(const char *fname, loff_t *size);
264 * os_putc() - write a character to the controlling OS terminal
266 * This bypasses the U-Boot console support and writes directly to the OS
267 * stdout file descriptor.
269 * @ch: haracter to write
271 void os_putc(int ch);
274 * os_puts() - write a string to the controlling OS terminal
276 * This bypasses the U-Boot console support and writes directly to the OS
277 * stdout file descriptor.
279 * @str: string to write (note that \n is not appended)
281 void os_puts(const char *str);
284 * os_write_ram_buf() - write the sandbox RAM buffer to a existing file
286 * @fname: filename to write memory to (simple binary format)
287 * Return: 0 if OK, -ve on error
289 int os_write_ram_buf(const char *fname);
292 * os_read_ram_buf() - read the sandbox RAM buffer from an existing file
294 * @fname: filename containing memory (simple binary format)
295 * Return: 0 if OK, -ve on error
297 int os_read_ram_buf(const char *fname);
300 * os_jump_to_image() - jump to a new executable image
302 * This uses exec() to run a new executable image, after putting it in a
303 * temporary file. The same arguments and environment are passed to this
304 * new image, with the addition of:
306 * -j <filename> Specifies the filename the image was written to. The
307 * calling image may want to delete this at some point.
308 * -m <filename> Specifies the file containing the sandbox memory
309 * (ram_buf) from this image, so that the new image can
310 * have access to this. It also means that the original
311 * memory filename passed to U-Boot will be left intact.
313 * @dest: buffer containing executable image
314 * @size: size of buffer
315 * Return: 0 if OK, -ve on error
317 int os_jump_to_image(const void *dest, int size);
320 * os_find_u_boot() - determine the path to U-Boot proper
322 * This function is intended to be called from within sandbox SPL. It uses
323 * a few heuristics to find U-Boot proper. Normally it is either in the same
324 * directory, or the directory above (since u-boot-spl is normally in an
325 * spl/ subdirectory when built).
327 * @fname: place to put full path to U-Boot
328 * @maxlen: maximum size of @fname
329 * @use_img: select the 'u-boot.img' file instead of the 'u-boot' ELF file
330 * Return: 0 if OK, -NOSPC if the filename is too large, -ENOENT if not found
332 int os_find_u_boot(char *fname, int maxlen, bool use_img);
335 * os_spl_to_uboot() - Run U-Boot proper
337 * When called from SPL, this runs U-Boot proper. The filename is obtained by
338 * calling os_find_u_boot().
340 * @fname: full pathname to U-Boot executable
341 * Return: 0 if OK, -ve on error
343 int os_spl_to_uboot(const char *fname);
346 * os_localtime() - read the current system time
348 * This reads the current Local Time and places it into the provided
351 * @rt: place to put system time
353 void os_localtime(struct rtc_time *rt);
356 * os_abort() - raise SIGABRT to exit sandbox (e.g. to debugger)
358 void os_abort(void) __attribute__((noreturn));
361 * os_mprotect_allow() - Remove write-protection on a region of memory
363 * The start and length will be page-aligned before use.
365 * @start: Region start
366 * @len: Region length in bytes
367 * Return: 0 if OK, -1 on error from mprotect()
369 int os_mprotect_allow(void *start, size_t len);
372 * os_write_file() - write a file to the host filesystem
374 * This can be useful when debugging for writing data out of sandbox for
375 * inspection by external tools.
377 * @name: File path to write to
378 * @buf: Data to write
379 * @size: Size of data to write
380 * Return: 0 if OK, -ve on error
382 int os_write_file(const char *name, const void *buf, int size);
385 * os_read_file() - Read a file from the host filesystem
387 * This can be useful when reading test data into sandbox for use by test
388 * routines. The data is allocated using os_malloc() and should be freed by
391 * @name: File path to read from
392 * @bufp: Returns buffer containing data read
393 * @sizep: Returns size of data
394 * Return: 0 if OK, -ve on error
396 int os_read_file(const char *name, void **bufp, int *sizep);
399 * os_find_text_base() - Find the text section in this running process
401 * This tries to find the address of the text section in this running process.
402 * It can be useful to map the address of functions to the address listed in
403 * the u-boot.map file.
405 * Return: address if found, else NULL
407 void *os_find_text_base(void);
410 * os_relaunch() - restart the sandbox
412 * This functions is used to implement the cold reboot of the sand box.
413 * @argv\[0] specifies the binary that is started while the calling process
414 * stops immediately. If the new binary cannot be started, the process is
415 * terminated and 1 is set as shell return code.
417 * The PID of the process stays the same. All file descriptors that have not
418 * been opened with O_CLOEXEC stay open including stdin, stdout, stderr.
420 * @argv: NULL terminated list of command line parameters
422 void os_relaunch(char *argv[]);
425 * os_setup_signal_handlers() - setup signal handlers
427 * Install signal handlers for SIGBUS and SIGSEGV.
429 * Return: 0 for success
431 int os_setup_signal_handlers(void);
434 * os_signal_action() - handle a signal
437 * @pc: program counter
439 void os_signal_action(int sig, unsigned long pc);
442 * os_get_time_offset() - get time offset
444 * Get the time offset from environment variable UBOOT_SB_TIME_OFFSET.
446 * Return: offset in seconds
448 long os_get_time_offset(void);
451 * os_set_time_offset() - set time offset
453 * Save the time offset in environment variable UBOOT_SB_TIME_OFFSET.
455 * @offset: offset in seconds
457 void os_set_time_offset(long offset);