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 * os_printf() - print directly to OS console
22 * @format: format string
24 int os_printf(const char *format, ...);
27 * Access to the OS read() system call
29 * @fd: File descriptor as returned by os_open()
30 * @buf: Buffer to place data
31 * @count: Number of bytes to read
32 * Return: number of bytes read, or -1 on error
34 ssize_t os_read(int fd, void *buf, size_t count);
37 * Access to the OS write() system call
39 * @fd: File descriptor as returned by os_open()
40 * @buf: Buffer containing data to write
41 * @count: Number of bytes to write
42 * Return: number of bytes written, or -1 on error
44 ssize_t os_write(int fd, const void *buf, size_t count);
47 * Access to the OS lseek() system call
49 * @fd: File descriptor as returned by os_open()
50 * @offset: File offset (based on whence)
51 * @whence: Position offset is relative to (see below)
52 * Return: new file offset
54 off_t os_lseek(int fd, off_t offset, int whence);
56 /* Defines for "whence" in os_lseek() */
62 * os_filesize() - Calculate the size of a file
64 * @fd: File descriptor as returned by os_open()
65 * Return: file size or negative error code
67 int os_filesize(int fd);
70 * Access to the OS open() system call
72 * @pathname: Pathname of file to open
73 * @flags: Flags, like OS_O_RDONLY, OS_O_RDWR
74 * Return: file descriptor, or -1 on error
76 int os_open(const char *pathname, int flags);
81 #define OS_O_MASK 3 /* Mask for read/write flags */
82 #define OS_O_CREAT 0100
83 #define OS_O_TRUNC 01000
86 * os_close() - access to the OS close() system call
88 * @fd: File descriptor to close
89 * Return: 0 on success, -1 on error
94 * os_unlink() - access to the OS unlink() system call
96 * @pathname: Path of file to delete
97 * Return: 0 for success, other for error
99 int os_unlink(const char *pathname);
102 * os_exit() - access to the OS exit() system call
104 * This exits with the supplied return code, which should be 0 to indicate
107 * @exit_code: exit code for U-Boot
109 void os_exit(int exit_code) __attribute__((noreturn));
112 * os_alarm() - access to the OS alarm() system call
114 * @seconds: number of seconds before the signal is sent
115 * Returns: number of seconds remaining until any previously scheduled alarm was
116 * due to be delivered; 0 if there was no previously scheduled alarm
118 unsigned int os_alarm(unsigned int seconds);
121 * os_set_alarm_handler() - set handler for SIGALRM
123 * @handler: The handler function. Pass NULL for SIG_DFL.
125 void os_set_alarm_handler(void (*handler)(int));
128 * os_raise_sigalrm() - do raise(SIGALRM)
130 void os_raise_sigalrm(void);
133 * os_tty_raw() - put tty into raw mode to mimic serial console better
135 * @fd: File descriptor of stdin (normally 0)
136 * @allow_sigs: Allow Ctrl-C, Ctrl-Z to generate signals rather than
137 * be handled by U-Boot
139 void os_tty_raw(int fd, bool allow_sigs);
142 * os_fs_restore() - restore the tty to its original mode
144 * Call this to restore the original terminal mode, after it has been changed
145 * by os_tty_raw(). This is an internal function.
147 void os_fd_restore(void);
150 * os_malloc() - aquires some memory from the underlying os.
152 * @length: Number of bytes to be allocated
153 * Return: Pointer to length bytes or NULL if @length is 0 or on error
155 void *os_malloc(size_t length);
158 * os_free() - free memory previous allocated with os_malloc()
160 * This returns the memory to the OS.
162 * @ptr: Pointer to memory block to free. If this is NULL then this
163 * function does nothing
165 void os_free(void *ptr);
168 * os_realloc() - reallocate memory
170 * This follows the semantics of realloc(), so can perform an os_malloc() or
171 * os_free() depending on @ptr and @length.
173 * @ptr: pointer to previously allocated memory of NULL
174 * @length: number of bytes to allocate
175 * Return: pointer to reallocated memory or NULL if @length is 0
177 void *os_realloc(void *ptr, size_t length);
180 * os_usleep() - access to the usleep function of the os
182 * @usec: time to sleep in micro seconds
184 void os_usleep(unsigned long usec);
187 * Gets a monotonic increasing number of nano seconds from the OS
189 * Return: a monotonic increasing time scaled in nano seconds
191 uint64_t os_get_nsec(void);
194 * Parse arguments and update sandbox state.
196 * @state: sandbox state to update
197 * @argc: argument count
198 * @argv: argument vector
200 * * 0 if ok, and program should continue
201 * * 1 if ok, but program should stop
202 * * -1 on error: program should terminate
204 int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
207 * enum os_dirent_t - type of directory entry
209 * Types of directory entry that we support. See also os_dirent_typename in
214 * @OS_FILET_REG: regular file
218 * @OS_FILET_LNK: symbolic link
222 * @OS_FILET_DIR: directory
226 * @OS_FILET_UNKNOWN: something else
230 * @OS_FILET_COUNT: number of directory entry types
236 * struct os_dirent_node - directory node
238 * A directory entry node, containing information about a single dirent
241 struct os_dirent_node {
243 * @next: pointer to next node, or NULL
245 struct os_dirent_node *next;
247 * @size: size of file in bytes
251 * @type: type of entry
253 enum os_dirent_t type;
255 * @name: name of entry
261 * os_dirent_ls() - get a directory listing
263 * This allocates and returns a linked list containing the directory listing.
265 * @dirname: directory to examine
266 * @headp: on return pointer to head of linked list, or NULL if none
267 * Return: 0 if ok, -ve on error
269 int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
272 * os_dirent_free() - free directory list
274 * This frees a linked list containing a directory listing.
276 * @node: pointer to head of linked list
278 void os_dirent_free(struct os_dirent_node *node);
281 * os_dirent_get_typename() - get the name of a directory entry type
283 * @type: type to check
285 * string containing the name of that type,
286 * or "???" if none/invalid
288 const char *os_dirent_get_typename(enum os_dirent_t type);
291 * os_get_filesize() - get the size of a file
293 * @fname: filename to check
294 * @size: size of file is returned if no error
295 * Return: 0 on success or -1 if an error ocurred
297 int os_get_filesize(const char *fname, long long *size);
300 * os_putc() - write a character to the controlling OS terminal
302 * This bypasses the U-Boot console support and writes directly to the OS
303 * stdout file descriptor.
305 * @ch: haracter to write
307 void os_putc(int ch);
310 * os_puts() - write a string to the controlling OS terminal
312 * This bypasses the U-Boot console support and writes directly to the OS
313 * stdout file descriptor.
315 * @str: string to write (note that \n is not appended)
317 void os_puts(const char *str);
320 * os_flush() - flush controlling OS terminal
322 * This bypasses the U-Boot console support and flushes directly the OS
323 * stdout file descriptor.
328 * os_write_ram_buf() - write the sandbox RAM buffer to a existing file
330 * @fname: filename to write memory to (simple binary format)
331 * Return: 0 if OK, -ve on error
333 int os_write_ram_buf(const char *fname);
336 * os_read_ram_buf() - read the sandbox RAM buffer from an existing file
338 * @fname: filename containing memory (simple binary format)
339 * Return: 0 if OK, -ve on error
341 int os_read_ram_buf(const char *fname);
344 * os_jump_to_image() - jump to a new executable image
346 * This uses exec() to run a new executable image, after putting it in a
347 * temporary file. The same arguments and environment are passed to this
348 * new image, with the addition of:
350 * -j <filename> Specifies the filename the image was written to. The
351 * calling image may want to delete this at some point.
352 * -m <filename> Specifies the file containing the sandbox memory
353 * (ram_buf) from this image, so that the new image can
354 * have access to this. It also means that the original
355 * memory filename passed to U-Boot will be left intact.
357 * @dest: buffer containing executable image
358 * @size: size of buffer
359 * Return: 0 if OK, -ve on error
361 int os_jump_to_image(const void *dest, int size);
364 * os_find_u_boot() - determine the path to U-Boot proper
366 * This function is intended to be called from within sandbox SPL. It uses
367 * a few heuristics to find U-Boot proper. Normally it is either in the same
368 * directory, or the directory above (since u-boot-spl is normally in an
369 * spl/ subdirectory when built).
371 * @fname: place to put full path to U-Boot
372 * @maxlen: maximum size of @fname
373 * @use_img: select the 'u-boot.img' file instead of the 'u-boot' ELF file
374 * @cur_prefix: prefix of current executable, e.g. "spl" or "tpl"
375 * @next_prefix: prefix of executable to find, e.g. "spl" or ""
376 * Return: 0 if OK, -NOSPC if the filename is too large, -ENOENT if not found
378 int os_find_u_boot(char *fname, int maxlen, bool use_img,
379 const char *cur_prefix, const char *next_prefix);
382 * os_spl_to_uboot() - Run U-Boot proper
384 * When called from SPL, this runs U-Boot proper. The filename is obtained by
385 * calling os_find_u_boot().
387 * @fname: full pathname to U-Boot executable
388 * Return: 0 if OK, -ve on error
390 int os_spl_to_uboot(const char *fname);
393 * os_localtime() - read the current system time
395 * This reads the current Local Time and places it into the provided
398 * @rt: place to put system time
400 void os_localtime(struct rtc_time *rt);
403 * os_abort() - raise SIGABRT to exit sandbox (e.g. to debugger)
405 void os_abort(void) __attribute__((noreturn));
408 * os_mprotect_allow() - Remove write-protection on a region of memory
410 * The start and length will be page-aligned before use.
412 * @start: Region start
413 * @len: Region length in bytes
414 * Return: 0 if OK, -1 on error from mprotect()
416 int os_mprotect_allow(void *start, size_t len);
419 * os_write_file() - write a file to the host filesystem
421 * This can be useful when debugging for writing data out of sandbox for
422 * inspection by external tools.
424 * @name: File path to write to
425 * @buf: Data to write
426 * @size: Size of data to write
427 * Return: 0 if OK, -ve on error
429 int os_write_file(const char *name, const void *buf, int size);
432 * os_read_file() - Read a file from the host filesystem
434 * This can be useful when reading test data into sandbox for use by test
435 * routines. The data is allocated using os_malloc() and should be freed by
438 * @name: File path to read from
439 * @bufp: Returns buffer containing data read
440 * @sizep: Returns size of data
441 * Return: 0 if OK, -ve on error
443 int os_read_file(const char *name, void **bufp, int *sizep);
446 * os_map_file() - Map a file from the host filesystem into memory
448 * This can be useful when to provide a backing store for an emulated device
450 * @pathname: File pathname to map
451 * @os_flags: Flags, like OS_O_RDONLY, OS_O_RDWR
452 * @bufp: Returns buffer containing the file
453 * @sizep: Returns size of data
454 * Return: 0 if OK, -ve on error
456 int os_map_file(const char *pathname, int os_flags, void **bufp, int *sizep);
459 * os_unmap() - Unmap a file previously mapped
461 * @buf: Mapped address
462 * @size: Size in bytes
463 * Return: 0 if OK, -ve on error
465 int os_unmap(void *buf, int size);
468 * os_find_text_base() - Find the text section in this running process
470 * This tries to find the address of the text section in this running process.
471 * It can be useful to map the address of functions to the address listed in
472 * the u-boot.map file.
474 * Return: address if found, else NULL
476 void *os_find_text_base(void);
479 * os_relaunch() - restart the sandbox
481 * This functions is used to implement the cold reboot of the sand box.
482 * @argv\[0] specifies the binary that is started while the calling process
483 * stops immediately. If the new binary cannot be started, the process is
484 * terminated and 1 is set as shell return code.
486 * The PID of the process stays the same. All file descriptors that have not
487 * been opened with O_CLOEXEC stay open including stdin, stdout, stderr.
489 * @argv: NULL terminated list of command line parameters
491 void os_relaunch(char *argv[]);
494 * os_setup_signal_handlers() - setup signal handlers
496 * Install signal handlers for SIGBUS and SIGSEGV.
498 * Return: 0 for success
500 int os_setup_signal_handlers(void);
503 * os_signal_action() - handle a signal
506 * @pc: program counter
508 void os_signal_action(int sig, unsigned long pc);
511 * os_get_time_offset() - get time offset
513 * Get the time offset from environment variable UBOOT_SB_TIME_OFFSET.
515 * Return: offset in seconds
517 long os_get_time_offset(void);
520 * os_set_time_offset() - set time offset
522 * Save the time offset in environment variable UBOOT_SB_TIME_OFFSET.
524 * @offset: offset in seconds
526 void os_set_time_offset(long offset);