sandbox: Add a way to write data to the host filesystem
[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  * \param fd    File descriptor as returned by os_open()
23  * \param buf   Buffer to place data
24  * \param 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 read() system call with non-blocking access
31  *
32  * \param fd    File descriptor as returned by os_open()
33  * \param buf   Buffer to place data
34  * \param count Number of bytes to read
35  * \return number of bytes read, or -1 on error
36  */
37 ssize_t os_read_no_block(int fd, void *buf, size_t count);
38
39 /**
40  * Access to the OS write() system call
41  *
42  * \param fd    File descriptor as returned by os_open()
43  * \param buf   Buffer containing data to write
44  * \param count Number of bytes to write
45  * \return number of bytes written, or -1 on error
46  */
47 ssize_t os_write(int fd, const void *buf, size_t count);
48
49 /**
50  * Access to the OS lseek() system call
51  *
52  * \param fd    File descriptor as returned by os_open()
53  * \param offset        File offset (based on whence)
54  * \param whence        Position offset is relative to (see below)
55  * \return new file offset
56  */
57 off_t os_lseek(int fd, off_t offset, int whence);
58
59 /* Defines for "whence" in os_lseek() */
60 #define OS_SEEK_SET     0
61 #define OS_SEEK_CUR     1
62 #define OS_SEEK_END     2
63
64 /**
65  * Access to the OS open() system call
66  *
67  * \param pathname      Pathname of file to open
68  * \param flags         Flags, like OS_O_RDONLY, OS_O_RDWR
69  * \return file descriptor, or -1 on error
70  */
71 int os_open(const char *pathname, int flags);
72
73 #define OS_O_RDONLY     0
74 #define OS_O_WRONLY     1
75 #define OS_O_RDWR       2
76 #define OS_O_MASK       3       /* Mask for read/write flags */
77 #define OS_O_CREAT      0100
78 #define OS_O_TRUNC      01000
79
80 /**
81  * Access to the OS close() system call
82  *
83  * \param fd    File descriptor to close
84  * \return 0 on success, -1 on error
85  */
86 int os_close(int fd);
87
88 /**
89  * Access to the OS unlink() system call
90  *
91  * \param pathname Path of file to delete
92  * \return 0 for success, other for error
93  */
94 int os_unlink(const char *pathname);
95
96 /**
97  * Access to the OS exit() system call
98  *
99  * This exits with the supplied return code, which should be 0 to indicate
100  * success.
101  *
102  * @param exit_code     exit code for U-Boot
103  */
104 void os_exit(int exit_code) __attribute__((noreturn));
105
106 /**
107  * Put tty into raw mode to mimic serial console better
108  *
109  * @param fd            File descriptor of stdin (normally 0)
110  * @param allow_sigs    Allow Ctrl-C, Ctrl-Z to generate signals rather than
111  *                      be handled by U-Boot
112  */
113 void os_tty_raw(int fd, bool allow_sigs);
114
115 /**
116  * Restore the tty to its original mode
117  *
118  * Call this to restore the original terminal mode, after it has been changed
119  * by os_tty_raw(). This is an internal function.
120  */
121 void os_fd_restore(void);
122
123 /**
124  * Acquires some memory from the underlying os.
125  *
126  * \param length        Number of bytes to be allocated
127  * \return Pointer to length bytes or NULL on error
128  */
129 void *os_malloc(size_t length);
130
131 /**
132  * Free memory previous allocated with os_malloc()/os_realloc()
133  *
134  * This returns the memory to the OS.
135  *
136  * \param ptr           Pointer to memory block to free
137  */
138 void os_free(void *ptr);
139
140 /**
141  * Reallocate previously-allocated memory to increase/decrease space
142  *
143  * This works in a similar way to the C library realloc() function. If
144  * length is 0, then ptr is freed. Otherwise the space used by ptr is
145  * expanded or reduced depending on whether length is larger or smaller
146  * than before.
147  *
148  * If ptr is NULL, then this is similar to calling os_malloc().
149  *
150  * This function may need to move the memory block to make room for any
151  * extra space, in which case the new pointer is returned.
152  *
153  * \param ptr           Pointer to memory block to reallocate
154  * \param length        New length for memory block
155  * \return pointer to new memory block, or NULL on failure or if length
156  *      is 0.
157  */
158 void *os_realloc(void *ptr, size_t length);
159
160 /**
161  * Access to the usleep function of the os
162  *
163  * \param usec Time to sleep in micro seconds
164  */
165 void os_usleep(unsigned long usec);
166
167 /**
168  * Gets a monotonic increasing number of nano seconds from the OS
169  *
170  * \return A monotonic increasing time scaled in nano seconds
171  */
172 uint64_t os_get_nsec(void);
173
174 /**
175  * Parse arguments and update sandbox state.
176  *
177  * @param state         Sandbox state to update
178  * @param argc          Argument count
179  * @param argv          Argument vector
180  * @return 0 if ok, and program should continue;
181  *      1 if ok, but program should stop;
182  *      -1 on error: program should terminate.
183  */
184 int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
185
186 /*
187  * Types of directory entry that we support. See also os_dirent_typename in
188  * the C file.
189  */
190 enum os_dirent_t {
191         OS_FILET_REG,           /* Regular file */
192         OS_FILET_LNK,           /* Symbolic link */
193         OS_FILET_DIR,           /* Directory */
194         OS_FILET_UNKNOWN,       /* Something else */
195
196         OS_FILET_COUNT,
197 };
198
199 /** A directory entry node, containing information about a single dirent */
200 struct os_dirent_node {
201         struct os_dirent_node *next;    /* Pointer to next node, or NULL */
202         ulong size;                     /* Size of file in bytes */
203         enum os_dirent_t type;          /* Type of entry */
204         char name[0];                   /* Name of entry */
205 };
206
207 /**
208  * Get a directionry listing
209  *
210  * This allocates and returns a linked list containing the directory listing.
211  *
212  * @param dirname       Directory to examine
213  * @param headp         Returns pointer to head of linked list, or NULL if none
214  * @return 0 if ok, -ve on error
215  */
216 int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
217
218 /**
219  * Free directory list
220  *
221  * This frees a linked list containing a directory listing.
222  *
223  * @param node          Pointer to head of linked list
224  */
225 void os_dirent_free(struct os_dirent_node *node);
226
227 /**
228  * Get the name of a directory entry type
229  *
230  * @param type          Type to check
231  * @return string containing the name of that type, or "???" if none/invalid
232  */
233 const char *os_dirent_get_typename(enum os_dirent_t type);
234
235 /**
236  * Get the size of a file
237  *
238  * @param fname         Filename to check
239  * @param size          size of file is returned if no error
240  * @return 0 on success or -1 if an error ocurred
241  */
242 int os_get_filesize(const char *fname, loff_t *size);
243
244 /**
245  * Write a character to the controlling OS terminal
246  *
247  * This bypasses the U-Boot console support and writes directly to the OS
248  * stdout file descriptor.
249  *
250  * @param ch    Character to write
251  */
252 void os_putc(int ch);
253
254 /**
255  * Write a string to the controlling OS terminal
256  *
257  * This bypasses the U-Boot console support and writes directly to the OS
258  * stdout file descriptor.
259  *
260  * @param str   String to write (note that \n is not appended)
261  */
262 void os_puts(const char *str);
263
264 /**
265  * Write the sandbox RAM buffer to a existing file
266  *
267  * @param fname         Filename to write memory to (simple binary format)
268  * @return 0 if OK, -ve on error
269  */
270 int os_write_ram_buf(const char *fname);
271
272 /**
273  * Read the sandbox RAM buffer from an existing file
274  *
275  * @param fname         Filename containing memory (simple binary format)
276  * @return 0 if OK, -ve on error
277  */
278 int os_read_ram_buf(const char *fname);
279
280 /**
281  * Jump to a new executable image
282  *
283  * This uses exec() to run a new executable image, after putting it in a
284  * temporary file. The same arguments and environment are passed to this
285  * new image, with the addition of:
286  *
287  *      -j <filename>   Specifies the filename the image was written to. The
288  *                      calling image may want to delete this at some point.
289  *      -m <filename>   Specifies the file containing the sandbox memory
290  *                      (ram_buf) from this image, so that the new image can
291  *                      have access to this. It also means that the original
292  *                      memory filename passed to U-Boot will be left intact.
293  *
294  * @param dest          Buffer containing executable image
295  * @param size          Size of buffer
296  */
297 int os_jump_to_image(const void *dest, int size);
298
299 /**
300  * os_find_u_boot() - Determine the path to U-Boot proper
301  *
302  * This function is intended to be called from within sandbox SPL. It uses
303  * a few heuristics to find U-Boot proper. Normally it is either in the same
304  * directory, or the directory above (since u-boot-spl is normally in an
305  * spl/ subdirectory when built).
306  *
307  * @fname:      Place to put full path to U-Boot
308  * @maxlen:     Maximum size of @fname
309  * @return 0 if OK, -NOSPC if the filename is too large, -ENOENT if not found
310  */
311 int os_find_u_boot(char *fname, int maxlen);
312
313 /**
314  * os_spl_to_uboot() - Run U-Boot proper
315  *
316  * When called from SPL, this runs U-Boot proper. The filename is obtained by
317  * calling os_find_u_boot().
318  *
319  * @fname:      Full pathname to U-Boot executable
320  * @return 0 if OK, -ve on error
321  */
322 int os_spl_to_uboot(const char *fname);
323
324 /**
325  * Read the current system time
326  *
327  * This reads the current Local Time and places it into the provided
328  * structure.
329  *
330  * @param rt            Place to put system time
331  */
332 void os_localtime(struct rtc_time *rt);
333
334 /**
335  * os_abort() - Raise SIGABRT to exit sandbox (e.g. to debugger)
336  */
337 void os_abort(void);
338
339 /**
340  * os_mprotect_allow() - Remove write-protection on a region of memory
341  *
342  * The start and length will be page-aligned before use.
343  *
344  * @start:      Region start
345  * @len:        Region length in bytes
346  * @return 0 if OK, -1 on error from mprotect()
347  */
348 int os_mprotect_allow(void *start, size_t len);
349
350 /**
351  * os_write_file() - Write a file to the host filesystem
352  *
353  * This can be useful when debugging for writing data out of sandbox for
354  * inspection by external tools.
355  *
356  * @name:       File path to write to
357  * @buf:        Data to write
358  * @size:       Size of data to write
359  * @return 0 if OK, -ve on error
360  */
361 int os_write_file(const char *name, const void *buf, int size);
362
363 #endif