Merge tag 'dm-pull-28jun22' of https://source.denx.de/u-boot/custodians/u-boot-dm...
[platform/kernel/u-boot.git] / include / os.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
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  *
8  * Copyright (c) 2011 The Chromium OS Authors.
9  */
10
11 #ifndef __OS_H__
12 #define __OS_H__
13
14 #include <linux/types.h>
15
16 struct rtc_time;
17 struct sandbox_state;
18
19 /**
20  * os_printf() - print directly to OS console
21  *
22  * @format: format string
23  */
24 int os_printf(const char *format, ...);
25
26 /**
27  * Access to the OS read() system call
28  *
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
33  */
34 ssize_t os_read(int fd, void *buf, size_t count);
35
36 /**
37  * Access to the OS write() system call
38  *
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
43  */
44 ssize_t os_write(int fd, const void *buf, size_t count);
45
46 /**
47  * Access to the OS lseek() system call
48  *
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
53  */
54 off_t os_lseek(int fd, off_t offset, int whence);
55
56 /* Defines for "whence" in os_lseek() */
57 #define OS_SEEK_SET     0
58 #define OS_SEEK_CUR     1
59 #define OS_SEEK_END     2
60
61 /**
62  * os_filesize() - Calculate the size of a file
63  *
64  * @fd:         File descriptor as returned by os_open()
65  * Return:      file size or negative error code
66  */
67 int os_filesize(int fd);
68
69 /**
70  * Access to the OS open() system call
71  *
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
75  */
76 int os_open(const char *pathname, int flags);
77
78 #define OS_O_RDONLY     0
79 #define OS_O_WRONLY     1
80 #define OS_O_RDWR       2
81 #define OS_O_MASK       3       /* Mask for read/write flags */
82 #define OS_O_CREAT      0100
83 #define OS_O_TRUNC      01000
84
85 /**
86  * os_close() - access to the OS close() system call
87  *
88  * @fd:         File descriptor to close
89  * Return:      0 on success, -1 on error
90  */
91 int os_close(int fd);
92
93 /**
94  * os_unlink() - access to the OS unlink() system call
95  *
96  * @pathname:   Path of file to delete
97  * Return:      0 for success, other for error
98  */
99 int os_unlink(const char *pathname);
100
101 /**
102  * os_exit() - access to the OS exit() system call
103  *
104  * This exits with the supplied return code, which should be 0 to indicate
105  * success.
106  *
107  * @exit_code:  exit code for U-Boot
108  */
109 void os_exit(int exit_code) __attribute__((noreturn));
110
111 /**
112  * os_tty_raw() - put tty into raw mode to mimic serial console better
113  *
114  * @fd:         File descriptor of stdin (normally 0)
115  * @allow_sigs: Allow Ctrl-C, Ctrl-Z to generate signals rather than
116  *              be handled by U-Boot
117  */
118 void os_tty_raw(int fd, bool allow_sigs);
119
120 /**
121  * os_fs_restore() - restore the tty to its original mode
122  *
123  * Call this to restore the original terminal mode, after it has been changed
124  * by os_tty_raw(). This is an internal function.
125  */
126 void os_fd_restore(void);
127
128 /**
129  * os_malloc() - aquires some memory from the underlying os.
130  *
131  * @length:     Number of bytes to be allocated
132  * Return:      Pointer to length bytes or NULL if @length is 0 or on error
133  */
134 void *os_malloc(size_t length);
135
136 /**
137  * os_free() - free memory previous allocated with os_malloc()
138  *
139  * This returns the memory to the OS.
140  *
141  * @ptr:        Pointer to memory block to free. If this is NULL then this
142  *              function does nothing
143  */
144 void os_free(void *ptr);
145
146 /**
147  * os_realloc() - reallocate memory
148  *
149  * This follows the semantics of realloc(), so can perform an os_malloc() or
150  * os_free() depending on @ptr and @length.
151  *
152  * @ptr:        pointer to previously allocated memory of NULL
153  * @length:     number of bytes to allocate
154  * Return:      pointer to reallocated memory or NULL if @length is 0
155  */
156 void *os_realloc(void *ptr, size_t length);
157
158 /**
159  * os_usleep() - access to the usleep function of the os
160  *
161  * @usec:       time to sleep in micro seconds
162  */
163 void os_usleep(unsigned long usec);
164
165 /**
166  * Gets a monotonic increasing number of nano seconds from the OS
167  *
168  * Return:      a monotonic increasing time scaled in nano seconds
169  */
170 uint64_t os_get_nsec(void);
171
172 /**
173  * Parse arguments and update sandbox state.
174  *
175  * @state:      sandbox state to update
176  * @argc:       argument count
177  * @argv:       argument vector
178  * Return:
179  * *  0 if ok, and program should continue
180  * *  1 if ok, but program should stop
181  * * -1 on error: program should terminate
182  */
183 int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
184
185 /*
186  * enum os_dirent_t - type of directory entry
187  *
188  * Types of directory entry that we support. See also os_dirent_typename in
189  * the C file.
190  */
191 enum os_dirent_t {
192         /**
193          * @OS_FILET_REG:       regular file
194          */
195         OS_FILET_REG,
196         /**
197          * @OS_FILET_LNK:       symbolic link
198          */
199         OS_FILET_LNK,
200         /**
201          * @OS_FILET_DIR:       directory
202          */
203         OS_FILET_DIR,
204         /**
205          * @OS_FILET_UNKNOWN:   something else
206          */
207         OS_FILET_UNKNOWN,
208         /**
209          * @OS_FILET_COUNT:     number of directory entry types
210          */
211         OS_FILET_COUNT,
212 };
213
214 /**
215  * struct os_dirent_node - directory node
216  *
217  * A directory entry node, containing information about a single dirent
218  *
219  */
220 struct os_dirent_node {
221         /**
222          * @next:       pointer to next node, or NULL
223          */
224         struct os_dirent_node *next;
225         /**
226          * @size:       size of file in bytes
227          */
228         ulong size;
229         /**
230          * @type:       type of entry
231          */
232         enum os_dirent_t type;
233         /**
234          * @name:       name of entry
235          */
236         char name[0];
237 };
238
239 /**
240  * os_dirent_ls() - get a directory listing
241  *
242  * This allocates and returns a linked list containing the directory listing.
243  *
244  * @dirname:    directory to examine
245  * @headp:      on return pointer to head of linked list, or NULL if none
246  * Return:      0 if ok, -ve on error
247  */
248 int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
249
250 /**
251  * os_dirent_free() - free directory list
252  *
253  * This frees a linked list containing a directory listing.
254  *
255  * @node:       pointer to head of linked list
256  */
257 void os_dirent_free(struct os_dirent_node *node);
258
259 /**
260  * os_dirent_get_typename() - get the name of a directory entry type
261  *
262  * @type:       type to check
263  * Return:
264  * string containing the name of that type,
265  * or "???" if none/invalid
266  */
267 const char *os_dirent_get_typename(enum os_dirent_t type);
268
269 /**
270  * os_get_filesize() - get the size of a file
271  *
272  * @fname:      filename to check
273  * @size:       size of file is returned if no error
274  * Return:      0 on success or -1 if an error ocurred
275  */
276 int os_get_filesize(const char *fname, long long *size);
277
278 /**
279  * os_putc() - write a character to the controlling OS terminal
280  *
281  * This bypasses the U-Boot console support and writes directly to the OS
282  * stdout file descriptor.
283  *
284  * @ch:         haracter to write
285  */
286 void os_putc(int ch);
287
288 /**
289  * os_puts() - write a string to the controlling OS terminal
290  *
291  * This bypasses the U-Boot console support and writes directly to the OS
292  * stdout file descriptor.
293  *
294  * @str:        string to write (note that \n is not appended)
295  */
296 void os_puts(const char *str);
297
298 /**
299  * os_write_ram_buf() - write the sandbox RAM buffer to a existing file
300  *
301  * @fname:      filename to write memory to (simple binary format)
302  * Return:      0 if OK, -ve on error
303  */
304 int os_write_ram_buf(const char *fname);
305
306 /**
307  * os_read_ram_buf() - read the sandbox RAM buffer from an existing file
308  *
309  * @fname:      filename containing memory (simple binary format)
310  * Return:      0 if OK, -ve on error
311  */
312 int os_read_ram_buf(const char *fname);
313
314 /**
315  * os_jump_to_image() - jump to a new executable image
316  *
317  * This uses exec() to run a new executable image, after putting it in a
318  * temporary file. The same arguments and environment are passed to this
319  * new image, with the addition of:
320  *
321  *      -j <filename>   Specifies the filename the image was written to. The
322  *                      calling image may want to delete this at some point.
323  *      -m <filename>   Specifies the file containing the sandbox memory
324  *                      (ram_buf) from this image, so that the new image can
325  *                      have access to this. It also means that the original
326  *                      memory filename passed to U-Boot will be left intact.
327  *
328  * @dest:       buffer containing executable image
329  * @size:       size of buffer
330  * Return:      0 if OK, -ve on error
331  */
332 int os_jump_to_image(const void *dest, int size);
333
334 /**
335  * os_find_u_boot() - determine the path to U-Boot proper
336  *
337  * This function is intended to be called from within sandbox SPL. It uses
338  * a few heuristics to find U-Boot proper. Normally it is either in the same
339  * directory, or the directory above (since u-boot-spl is normally in an
340  * spl/ subdirectory when built).
341  *
342  * @fname:      place to put full path to U-Boot
343  * @maxlen:     maximum size of @fname
344  * @use_img:    select the 'u-boot.img' file instead of the 'u-boot' ELF file
345  * @cur_prefix: prefix of current executable, e.g. "spl" or "tpl"
346  * @next_prefix: prefix of executable to find, e.g. "spl" or ""
347  * Return:      0 if OK, -NOSPC if the filename is too large, -ENOENT if not found
348  */
349 int os_find_u_boot(char *fname, int maxlen, bool use_img,
350                    const char *cur_prefix, const char *next_prefix);
351
352 /**
353  * os_spl_to_uboot() - Run U-Boot proper
354  *
355  * When called from SPL, this runs U-Boot proper. The filename is obtained by
356  * calling os_find_u_boot().
357  *
358  * @fname:      full pathname to U-Boot executable
359  * Return:      0 if OK, -ve on error
360  */
361 int os_spl_to_uboot(const char *fname);
362
363 /**
364  * os_localtime() - read the current system time
365  *
366  * This reads the current Local Time and places it into the provided
367  * structure.
368  *
369  * @rt:         place to put system time
370  */
371 void os_localtime(struct rtc_time *rt);
372
373 /**
374  * os_abort() - raise SIGABRT to exit sandbox (e.g. to debugger)
375  */
376 void os_abort(void) __attribute__((noreturn));
377
378 /**
379  * os_mprotect_allow() - Remove write-protection on a region of memory
380  *
381  * The start and length will be page-aligned before use.
382  *
383  * @start:      Region start
384  * @len:        Region length in bytes
385  * Return:      0 if OK, -1 on error from mprotect()
386  */
387 int os_mprotect_allow(void *start, size_t len);
388
389 /**
390  * os_write_file() - write a file to the host filesystem
391  *
392  * This can be useful when debugging for writing data out of sandbox for
393  * inspection by external tools.
394  *
395  * @name:       File path to write to
396  * @buf:        Data to write
397  * @size:       Size of data to write
398  * Return:      0 if OK, -ve on error
399  */
400 int os_write_file(const char *name, const void *buf, int size);
401
402 /**
403  * os_read_file() - Read a file from the host filesystem
404  *
405  * This can be useful when reading test data into sandbox for use by test
406  * routines. The data is allocated using os_malloc() and should be freed by
407  * the caller.
408  *
409  * @name:       File path to read from
410  * @bufp:       Returns buffer containing data read
411  * @sizep:      Returns size of data
412  * Return:      0 if OK, -ve on error
413  */
414 int os_read_file(const char *name, void **bufp, int *sizep);
415
416 /**
417  * os_map_file() - Map a file from the host filesystem into memory
418  *
419  * This can be useful when to provide a backing store for an emulated device
420  *
421  * @pathname:   File pathname to map
422  * @os_flags:   Flags, like OS_O_RDONLY, OS_O_RDWR
423  * @bufp:       Returns buffer containing the file
424  * @sizep:      Returns size of data
425  * Return:      0 if OK, -ve on error
426  */
427 int os_map_file(const char *pathname, int os_flags, void **bufp, int *sizep);
428
429 /**
430  * os_unmap() - Unmap a file previously mapped
431  *
432  * @buf: Mapped address
433  * @size: Size in bytes
434  * Return:      0 if OK, -ve on error
435  */
436 int os_unmap(void *buf, int size);
437
438 /*
439  * os_find_text_base() - Find the text section in this running process
440  *
441  * This tries to find the address of the text section in this running process.
442  * It can be useful to map the address of functions to the address listed in
443  * the u-boot.map file.
444  *
445  * Return:      address if found, else NULL
446  */
447 void *os_find_text_base(void);
448
449 /**
450  * os_relaunch() - restart the sandbox
451  *
452  * This functions is used to implement the cold reboot of the sand box.
453  * @argv\[0] specifies the binary that is started while the calling process
454  * stops immediately. If the new binary cannot be started, the process is
455  * terminated and 1 is set as shell return code.
456  *
457  * The PID of the process stays the same. All file descriptors that have not
458  * been opened with O_CLOEXEC stay open including stdin, stdout, stderr.
459  *
460  * @argv:       NULL terminated list of command line parameters
461  */
462 void os_relaunch(char *argv[]);
463
464 /**
465  * os_setup_signal_handlers() - setup signal handlers
466  *
467  * Install signal handlers for SIGBUS and SIGSEGV.
468  *
469  * Return:      0 for success
470  */
471 int os_setup_signal_handlers(void);
472
473 /**
474  * os_signal_action() - handle a signal
475  *
476  * @sig:        signal
477  * @pc:         program counter
478  */
479 void os_signal_action(int sig, unsigned long pc);
480
481 /**
482  * os_get_time_offset() - get time offset
483  *
484  * Get the time offset from environment variable UBOOT_SB_TIME_OFFSET.
485  *
486  * Return:      offset in seconds
487  */
488 long os_get_time_offset(void);
489
490 /**
491  * os_set_time_offset() - set time offset
492  *
493  * Save the time offset in environment variable UBOOT_SB_TIME_OFFSET.
494  *
495  * @offset:     offset in seconds
496  */
497 void os_set_time_offset(long offset);
498
499 #endif