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