xref: /rk3399_rockchip-uboot/include/os.h (revision ffb87905cb3883c84598b87ca05384c17d59dee1)
17a9219c1SSimon Glass /*
2d9165153SSimon Glass  * Operating System Interface
3d9165153SSimon Glass  *
4d9165153SSimon Glass  * This provides access to useful OS routines for the sandbox architecture.
5d9165153SSimon Glass  * They are kept in a separate file so we can include system headers.
6d9165153SSimon Glass  *
77a9219c1SSimon Glass  * Copyright (c) 2011 The Chromium OS Authors.
81a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
97a9219c1SSimon Glass  */
107a9219c1SSimon Glass 
114f345d56SMike Frysinger #ifndef __OS_H__
124f345d56SMike Frysinger #define __OS_H__
134f345d56SMike Frysinger 
142a54d159SSimon Glass #include <linux/types.h>
152a54d159SSimon Glass 
1670db4212SSimon Glass struct sandbox_state;
1770db4212SSimon Glass 
187a9219c1SSimon Glass /**
197a9219c1SSimon Glass  * Access to the OS read() system call
207a9219c1SSimon Glass  *
217a9219c1SSimon Glass  * \param fd	File descriptor as returned by os_open()
227a9219c1SSimon Glass  * \param buf	Buffer to place data
237a9219c1SSimon Glass  * \param count	Number of bytes to read
247a9219c1SSimon Glass  * \return number of bytes read, or -1 on error
257a9219c1SSimon Glass  */
267a9219c1SSimon Glass ssize_t os_read(int fd, void *buf, size_t count);
277a9219c1SSimon Glass 
287a9219c1SSimon Glass /**
29e101550aSTaylor Hutt  * Access to the OS read() system call with non-blocking access
30e101550aSTaylor Hutt  *
31e101550aSTaylor Hutt  * \param fd	File descriptor as returned by os_open()
32e101550aSTaylor Hutt  * \param buf	Buffer to place data
33e101550aSTaylor Hutt  * \param count	Number of bytes to read
34e101550aSTaylor Hutt  * \return number of bytes read, or -1 on error
35e101550aSTaylor Hutt  */
36e101550aSTaylor Hutt ssize_t os_read_no_block(int fd, void *buf, size_t count);
37e101550aSTaylor Hutt 
38e101550aSTaylor Hutt /**
397a9219c1SSimon Glass  * Access to the OS write() system call
407a9219c1SSimon Glass  *
417a9219c1SSimon Glass  * \param fd	File descriptor as returned by os_open()
427a9219c1SSimon Glass  * \param buf	Buffer containing data to write
437a9219c1SSimon Glass  * \param count	Number of bytes to write
447a9219c1SSimon Glass  * \return number of bytes written, or -1 on error
457a9219c1SSimon Glass  */
467a9219c1SSimon Glass ssize_t os_write(int fd, const void *buf, size_t count);
477a9219c1SSimon Glass 
487a9219c1SSimon Glass /**
49e2dcefcbSMike Frysinger  * Access to the OS lseek() system call
50e2dcefcbSMike Frysinger  *
51e2dcefcbSMike Frysinger  * \param fd	File descriptor as returned by os_open()
52e2dcefcbSMike Frysinger  * \param offset	File offset (based on whence)
53e2dcefcbSMike Frysinger  * \param whence	Position offset is relative to (see below)
54e2dcefcbSMike Frysinger  * \return new file offset
55e2dcefcbSMike Frysinger  */
56e2dcefcbSMike Frysinger off_t os_lseek(int fd, off_t offset, int whence);
57e2dcefcbSMike Frysinger 
58e2dcefcbSMike Frysinger /* Defines for "whence" in os_lseek() */
59e2dcefcbSMike Frysinger #define OS_SEEK_SET	0
60e2dcefcbSMike Frysinger #define OS_SEEK_CUR	1
61e2dcefcbSMike Frysinger #define OS_SEEK_END	2
62e2dcefcbSMike Frysinger 
63e2dcefcbSMike Frysinger /**
647a9219c1SSimon Glass  * Access to the OS open() system call
657a9219c1SSimon Glass  *
667a9219c1SSimon Glass  * \param pathname	Pathname of file to open
677a9219c1SSimon Glass  * \param flags		Flags, like O_RDONLY, O_RDWR
687a9219c1SSimon Glass  * \return file descriptor, or -1 on error
697a9219c1SSimon Glass  */
707a9219c1SSimon Glass int os_open(const char *pathname, int flags);
717a9219c1SSimon Glass 
72d9165153SSimon Glass #define OS_O_RDONLY	0
73d9165153SSimon Glass #define OS_O_WRONLY	1
74d9165153SSimon Glass #define OS_O_RDWR	2
75d9165153SSimon Glass #define OS_O_MASK	3	/* Mask for read/write flags */
76d9165153SSimon Glass #define OS_O_CREAT	0100
77d9165153SSimon Glass 
787a9219c1SSimon Glass /**
797a9219c1SSimon Glass  * Access to the OS close() system call
807a9219c1SSimon Glass  *
817a9219c1SSimon Glass  * \param fd	File descriptor to close
827a9219c1SSimon Glass  * \return 0 on success, -1 on error
837a9219c1SSimon Glass  */
847a9219c1SSimon Glass int os_close(int fd);
857a9219c1SSimon Glass 
867a9219c1SSimon Glass /**
87cfd13e8dSStephen Warren  * Access to the OS unlink() system call
88cfd13e8dSStephen Warren  *
89cfd13e8dSStephen Warren  * \param pathname Path of file to delete
90cfd13e8dSStephen Warren  * \return 0 for success, other for error
91cfd13e8dSStephen Warren  */
92cfd13e8dSStephen Warren int os_unlink(const char *pathname);
93cfd13e8dSStephen Warren 
94cfd13e8dSStephen Warren /**
957a9219c1SSimon Glass  * Access to the OS exit() system call
967a9219c1SSimon Glass  *
977a9219c1SSimon Glass  * This exits with the supplied return code, which should be 0 to indicate
987a9219c1SSimon Glass  * success.
997a9219c1SSimon Glass  *
1007a9219c1SSimon Glass  * @param exit_code	exit code for U-Boot
1017a9219c1SSimon Glass  */
1029d72e67bSMike Frysinger void os_exit(int exit_code) __attribute__((noreturn));
103ab06a758SMike Frysinger 
104ab06a758SMike Frysinger /**
105ab06a758SMike Frysinger  * Put tty into raw mode to mimic serial console better
106*ffb87905SSimon Glass  *
107*ffb87905SSimon Glass  * @param fd		File descriptor of stdin (normally 0)
108*ffb87905SSimon Glass  * @param allow_sigs	Allow Ctrl-C, Ctrl-Z to generate signals rather than
109*ffb87905SSimon Glass  *			be handled by U-Boot
110ab06a758SMike Frysinger  */
111*ffb87905SSimon Glass void os_tty_raw(int fd, bool allow_sigs);
11221899b10SMatthias Weisser 
11321899b10SMatthias Weisser /**
11421899b10SMatthias Weisser  * Acquires some memory from the underlying os.
11521899b10SMatthias Weisser  *
11621899b10SMatthias Weisser  * \param length	Number of bytes to be allocated
11721899b10SMatthias Weisser  * \return Pointer to length bytes or NULL on error
11821899b10SMatthias Weisser  */
11921899b10SMatthias Weisser void *os_malloc(size_t length);
120d99a6874SMatthias Weisser 
121d99a6874SMatthias Weisser /**
12277595c6dSSimon Glass  * Free memory previous allocated with os_malloc()/os_realloc()
12377595c6dSSimon Glass  *
12477595c6dSSimon Glass  * This returns the memory to the OS.
12577595c6dSSimon Glass  *
12677595c6dSSimon Glass  * \param ptr		Pointer to memory block to free
12777595c6dSSimon Glass  */
128347d06deSMasahiro Yamada void os_free(void *ptr);
12977595c6dSSimon Glass 
13077595c6dSSimon Glass /**
13177595c6dSSimon Glass  * Reallocate previously-allocated memory to increase/decrease space
13277595c6dSSimon Glass  *
13377595c6dSSimon Glass  * This works in a similar way to the C library realloc() function. If
13477595c6dSSimon Glass  * length is 0, then ptr is freed. Otherwise the space used by ptr is
13577595c6dSSimon Glass  * expanded or reduced depending on whether length is larger or smaller
13677595c6dSSimon Glass  * than before.
13777595c6dSSimon Glass  *
13877595c6dSSimon Glass  * If ptr is NULL, then this is similar to calling os_malloc().
13977595c6dSSimon Glass  *
14077595c6dSSimon Glass  * This function may need to move the memory block to make room for any
14177595c6dSSimon Glass  * extra space, in which case the new pointer is returned.
14277595c6dSSimon Glass  *
14377595c6dSSimon Glass  * \param ptr		Pointer to memory block to reallocate
14477595c6dSSimon Glass  * \param length	New length for memory block
14577595c6dSSimon Glass  * \return pointer to new memory block, or NULL on failure or if length
14677595c6dSSimon Glass  *	is 0.
14777595c6dSSimon Glass  */
14877595c6dSSimon Glass void *os_realloc(void *ptr, size_t length);
14977595c6dSSimon Glass 
15077595c6dSSimon Glass /**
151d99a6874SMatthias Weisser  * Access to the usleep function of the os
152d99a6874SMatthias Weisser  *
153d99a6874SMatthias Weisser  * \param usec Time to sleep in micro seconds
154d99a6874SMatthias Weisser  */
155d99a6874SMatthias Weisser void os_usleep(unsigned long usec);
156d99a6874SMatthias Weisser 
157d99a6874SMatthias Weisser /**
158d99a6874SMatthias Weisser  * Gets a monotonic increasing number of nano seconds from the OS
159d99a6874SMatthias Weisser  *
160d99a6874SMatthias Weisser  * \return A monotonic increasing time scaled in nano seconds
161d99a6874SMatthias Weisser  */
1622a54d159SSimon Glass uint64_t os_get_nsec(void);
1634f345d56SMike Frysinger 
16470db4212SSimon Glass /**
16570db4212SSimon Glass  * Parse arguments and update sandbox state.
16670db4212SSimon Glass  *
16770db4212SSimon Glass  * @param state		Sandbox state to update
16870db4212SSimon Glass  * @param argc		Argument count
16970db4212SSimon Glass  * @param argv		Argument vector
17070db4212SSimon Glass  * @return 0 if ok, and program should continue;
17170db4212SSimon Glass  *	1 if ok, but program should stop;
17270db4212SSimon Glass  *	-1 on error: program should terminate.
17370db4212SSimon Glass  */
17470db4212SSimon Glass int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
17570db4212SSimon Glass 
17662584db1SSimon Glass /*
17762584db1SSimon Glass  * Types of directory entry that we support. See also os_dirent_typename in
17862584db1SSimon Glass  * the C file.
17962584db1SSimon Glass  */
18062584db1SSimon Glass enum os_dirent_t {
18162584db1SSimon Glass 	OS_FILET_REG,		/* Regular file */
18262584db1SSimon Glass 	OS_FILET_LNK,		/* Symbolic link */
18362584db1SSimon Glass 	OS_FILET_DIR,		/* Directory */
18462584db1SSimon Glass 	OS_FILET_UNKNOWN,	/* Something else */
18562584db1SSimon Glass 
18662584db1SSimon Glass 	OS_FILET_COUNT,
18762584db1SSimon Glass };
18862584db1SSimon Glass 
18962584db1SSimon Glass /** A directory entry node, containing information about a single dirent */
19062584db1SSimon Glass struct os_dirent_node {
19162584db1SSimon Glass 	struct os_dirent_node *next;	/* Pointer to next node, or NULL */
19262584db1SSimon Glass 	ulong size;			/* Size of file in bytes */
19362584db1SSimon Glass 	enum os_dirent_t type;		/* Type of entry */
19462584db1SSimon Glass 	char name[0];			/* Name of entry */
19562584db1SSimon Glass };
19662584db1SSimon Glass 
19762584db1SSimon Glass /**
19862584db1SSimon Glass  * Get a directionry listing
19962584db1SSimon Glass  *
20062584db1SSimon Glass  * This allocates and returns a linked list containing the directory listing.
20162584db1SSimon Glass  *
20262584db1SSimon Glass  * @param dirname	Directory to examine
20362584db1SSimon Glass  * @param headp		Returns pointer to head of linked list, or NULL if none
20462584db1SSimon Glass  * @return 0 if ok, -ve on error
20562584db1SSimon Glass  */
20662584db1SSimon Glass int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
20762584db1SSimon Glass 
20862584db1SSimon Glass /**
20962584db1SSimon Glass  * Get the name of a directory entry type
21062584db1SSimon Glass  *
21162584db1SSimon Glass  * @param type		Type to cehck
21262584db1SSimon Glass  * @return string containing the name of that type, or "???" if none/invalid
21362584db1SSimon Glass  */
21462584db1SSimon Glass const char *os_dirent_get_typename(enum os_dirent_t type);
21562584db1SSimon Glass 
21662584db1SSimon Glass /**
21762584db1SSimon Glass  * Get the size of a file
21862584db1SSimon Glass  *
21962584db1SSimon Glass  * @param fname		Filename to check
22062584db1SSimon Glass  * @return size of file, or -1 if an error ocurred
22162584db1SSimon Glass  */
22262584db1SSimon Glass ssize_t os_get_filesize(const char *fname);
22362584db1SSimon Glass 
22491b136c7SSimon Glass /**
22591b136c7SSimon Glass  * Write a character to the controlling OS terminal
22691b136c7SSimon Glass  *
22791b136c7SSimon Glass  * This bypasses the U-Boot console support and writes directly to the OS
22891b136c7SSimon Glass  * stdout file descriptor.
22991b136c7SSimon Glass  *
23091b136c7SSimon Glass  * @param ch	Character to write
23191b136c7SSimon Glass  */
23291b136c7SSimon Glass void os_putc(int ch);
23391b136c7SSimon Glass 
23491b136c7SSimon Glass /**
23591b136c7SSimon Glass  * Write a string to the controlling OS terminal
23691b136c7SSimon Glass  *
23791b136c7SSimon Glass  * This bypasses the U-Boot console support and writes directly to the OS
23891b136c7SSimon Glass  * stdout file descriptor.
23991b136c7SSimon Glass  *
24091b136c7SSimon Glass  * @param str	String to write (note that \n is not appended)
24191b136c7SSimon Glass  */
24291b136c7SSimon Glass void os_puts(const char *str);
24391b136c7SSimon Glass 
2445c2859cdSSimon Glass /**
2455c2859cdSSimon Glass  * Write the sandbox RAM buffer to a existing file
2465c2859cdSSimon Glass  *
2475c2859cdSSimon Glass  * @param fname		Filename to write memory to (simple binary format)
2485c2859cdSSimon Glass  * @return 0 if OK, -ve on error
2495c2859cdSSimon Glass  */
2505c2859cdSSimon Glass int os_write_ram_buf(const char *fname);
2515c2859cdSSimon Glass 
2525c2859cdSSimon Glass /**
2535c2859cdSSimon Glass  * Read the sandbox RAM buffer from an existing file
2545c2859cdSSimon Glass  *
2555c2859cdSSimon Glass  * @param fname		Filename containing memory (simple binary format)
2565c2859cdSSimon Glass  * @return 0 if OK, -ve on error
2575c2859cdSSimon Glass  */
2585c2859cdSSimon Glass int os_read_ram_buf(const char *fname);
2595c2859cdSSimon Glass 
26047f5fcfbSSimon Glass /**
26147f5fcfbSSimon Glass  * Jump to a new executable image
26247f5fcfbSSimon Glass  *
26347f5fcfbSSimon Glass  * This uses exec() to run a new executable image, after putting it in a
26447f5fcfbSSimon Glass  * temporary file. The same arguments and environment are passed to this
26547f5fcfbSSimon Glass  * new image, with the addition of:
26647f5fcfbSSimon Glass  *
26747f5fcfbSSimon Glass  *	-j <filename>	Specifies the filename the image was written to. The
26847f5fcfbSSimon Glass  *			calling image may want to delete this at some point.
26947f5fcfbSSimon Glass  *	-m <filename>	Specifies the file containing the sandbox memory
27047f5fcfbSSimon Glass  *			(ram_buf) from this image, so that the new image can
27147f5fcfbSSimon Glass  *			have access to this. It also means that the original
27247f5fcfbSSimon Glass  *			memory filename passed to U-Boot will be left intact.
27347f5fcfbSSimon Glass  *
27447f5fcfbSSimon Glass  * @param dest		Buffer containing executable image
27547f5fcfbSSimon Glass  * @param size		Size of buffer
27647f5fcfbSSimon Glass  */
27747f5fcfbSSimon Glass int os_jump_to_image(const void *dest, int size);
27847f5fcfbSSimon Glass 
2794f345d56SMike Frysinger #endif
280