xref: /rk3399_rockchip-uboot/include/os.h (revision 1a4596601fd395f3afb8f82f3f840c5e00bdd57a)
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.
8*1a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
97a9219c1SSimon Glass  */
107a9219c1SSimon Glass 
114f345d56SMike Frysinger #ifndef __OS_H__
124f345d56SMike Frysinger #define __OS_H__
134f345d56SMike Frysinger 
1470db4212SSimon Glass struct sandbox_state;
1570db4212SSimon Glass 
167a9219c1SSimon Glass /**
177a9219c1SSimon Glass  * Access to the OS read() system call
187a9219c1SSimon Glass  *
197a9219c1SSimon Glass  * \param fd	File descriptor as returned by os_open()
207a9219c1SSimon Glass  * \param buf	Buffer to place data
217a9219c1SSimon Glass  * \param count	Number of bytes to read
227a9219c1SSimon Glass  * \return number of bytes read, or -1 on error
237a9219c1SSimon Glass  */
247a9219c1SSimon Glass ssize_t os_read(int fd, void *buf, size_t count);
257a9219c1SSimon Glass 
267a9219c1SSimon Glass /**
27e101550aSTaylor Hutt  * Access to the OS read() system call with non-blocking access
28e101550aSTaylor Hutt  *
29e101550aSTaylor Hutt  * \param fd	File descriptor as returned by os_open()
30e101550aSTaylor Hutt  * \param buf	Buffer to place data
31e101550aSTaylor Hutt  * \param count	Number of bytes to read
32e101550aSTaylor Hutt  * \return number of bytes read, or -1 on error
33e101550aSTaylor Hutt  */
34e101550aSTaylor Hutt ssize_t os_read_no_block(int fd, void *buf, size_t count);
35e101550aSTaylor Hutt 
36e101550aSTaylor Hutt /**
377a9219c1SSimon Glass  * Access to the OS write() system call
387a9219c1SSimon Glass  *
397a9219c1SSimon Glass  * \param fd	File descriptor as returned by os_open()
407a9219c1SSimon Glass  * \param buf	Buffer containing data to write
417a9219c1SSimon Glass  * \param count	Number of bytes to write
427a9219c1SSimon Glass  * \return number of bytes written, or -1 on error
437a9219c1SSimon Glass  */
447a9219c1SSimon Glass ssize_t os_write(int fd, const void *buf, size_t count);
457a9219c1SSimon Glass 
467a9219c1SSimon Glass /**
47e2dcefcbSMike Frysinger  * Access to the OS lseek() system call
48e2dcefcbSMike Frysinger  *
49e2dcefcbSMike Frysinger  * \param fd	File descriptor as returned by os_open()
50e2dcefcbSMike Frysinger  * \param offset	File offset (based on whence)
51e2dcefcbSMike Frysinger  * \param whence	Position offset is relative to (see below)
52e2dcefcbSMike Frysinger  * \return new file offset
53e2dcefcbSMike Frysinger  */
54e2dcefcbSMike Frysinger off_t os_lseek(int fd, off_t offset, int whence);
55e2dcefcbSMike Frysinger 
56e2dcefcbSMike Frysinger /* Defines for "whence" in os_lseek() */
57e2dcefcbSMike Frysinger #define OS_SEEK_SET	0
58e2dcefcbSMike Frysinger #define OS_SEEK_CUR	1
59e2dcefcbSMike Frysinger #define OS_SEEK_END	2
60e2dcefcbSMike Frysinger 
61e2dcefcbSMike Frysinger /**
627a9219c1SSimon Glass  * Access to the OS open() system call
637a9219c1SSimon Glass  *
647a9219c1SSimon Glass  * \param pathname	Pathname of file to open
657a9219c1SSimon Glass  * \param flags		Flags, like O_RDONLY, O_RDWR
667a9219c1SSimon Glass  * \return file descriptor, or -1 on error
677a9219c1SSimon Glass  */
687a9219c1SSimon Glass int os_open(const char *pathname, int flags);
697a9219c1SSimon Glass 
70d9165153SSimon Glass #define OS_O_RDONLY	0
71d9165153SSimon Glass #define OS_O_WRONLY	1
72d9165153SSimon Glass #define OS_O_RDWR	2
73d9165153SSimon Glass #define OS_O_MASK	3	/* Mask for read/write flags */
74d9165153SSimon Glass #define OS_O_CREAT	0100
75d9165153SSimon Glass 
767a9219c1SSimon Glass /**
777a9219c1SSimon Glass  * Access to the OS close() system call
787a9219c1SSimon Glass  *
797a9219c1SSimon Glass  * \param fd	File descriptor to close
807a9219c1SSimon Glass  * \return 0 on success, -1 on error
817a9219c1SSimon Glass  */
827a9219c1SSimon Glass int os_close(int fd);
837a9219c1SSimon Glass 
847a9219c1SSimon Glass /**
857a9219c1SSimon Glass  * Access to the OS exit() system call
867a9219c1SSimon Glass  *
877a9219c1SSimon Glass  * This exits with the supplied return code, which should be 0 to indicate
887a9219c1SSimon Glass  * success.
897a9219c1SSimon Glass  *
907a9219c1SSimon Glass  * @param exit_code	exit code for U-Boot
917a9219c1SSimon Glass  */
929d72e67bSMike Frysinger void os_exit(int exit_code) __attribute__((noreturn));
93ab06a758SMike Frysinger 
94ab06a758SMike Frysinger /**
95ab06a758SMike Frysinger  * Put tty into raw mode to mimic serial console better
96ab06a758SMike Frysinger  */
97ab06a758SMike Frysinger void os_tty_raw(int fd);
9821899b10SMatthias Weisser 
9921899b10SMatthias Weisser /**
10021899b10SMatthias Weisser  * Acquires some memory from the underlying os.
10121899b10SMatthias Weisser  *
10221899b10SMatthias Weisser  * \param length	Number of bytes to be allocated
10321899b10SMatthias Weisser  * \return Pointer to length bytes or NULL on error
10421899b10SMatthias Weisser  */
10521899b10SMatthias Weisser void *os_malloc(size_t length);
106d99a6874SMatthias Weisser 
107d99a6874SMatthias Weisser /**
108d99a6874SMatthias Weisser  * Access to the usleep function of the os
109d99a6874SMatthias Weisser  *
110d99a6874SMatthias Weisser  * \param usec Time to sleep in micro seconds
111d99a6874SMatthias Weisser  */
112d99a6874SMatthias Weisser void os_usleep(unsigned long usec);
113d99a6874SMatthias Weisser 
114d99a6874SMatthias Weisser /**
115d99a6874SMatthias Weisser  * Gets a monotonic increasing number of nano seconds from the OS
116d99a6874SMatthias Weisser  *
117d99a6874SMatthias Weisser  * \return A monotonic increasing time scaled in nano seconds
118d99a6874SMatthias Weisser  */
119d99a6874SMatthias Weisser u64 os_get_nsec(void);
1204f345d56SMike Frysinger 
12170db4212SSimon Glass /**
12270db4212SSimon Glass  * Parse arguments and update sandbox state.
12370db4212SSimon Glass  *
12470db4212SSimon Glass  * @param state		Sandbox state to update
12570db4212SSimon Glass  * @param argc		Argument count
12670db4212SSimon Glass  * @param argv		Argument vector
12770db4212SSimon Glass  * @return 0 if ok, and program should continue;
12870db4212SSimon Glass  *	1 if ok, but program should stop;
12970db4212SSimon Glass  *	-1 on error: program should terminate.
13070db4212SSimon Glass  */
13170db4212SSimon Glass int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
13270db4212SSimon Glass 
13362584db1SSimon Glass /*
13462584db1SSimon Glass  * Types of directory entry that we support. See also os_dirent_typename in
13562584db1SSimon Glass  * the C file.
13662584db1SSimon Glass  */
13762584db1SSimon Glass enum os_dirent_t {
13862584db1SSimon Glass 	OS_FILET_REG,		/* Regular file */
13962584db1SSimon Glass 	OS_FILET_LNK,		/* Symbolic link */
14062584db1SSimon Glass 	OS_FILET_DIR,		/* Directory */
14162584db1SSimon Glass 	OS_FILET_UNKNOWN,	/* Something else */
14262584db1SSimon Glass 
14362584db1SSimon Glass 	OS_FILET_COUNT,
14462584db1SSimon Glass };
14562584db1SSimon Glass 
14662584db1SSimon Glass /** A directory entry node, containing information about a single dirent */
14762584db1SSimon Glass struct os_dirent_node {
14862584db1SSimon Glass 	struct os_dirent_node *next;	/* Pointer to next node, or NULL */
14962584db1SSimon Glass 	ulong size;			/* Size of file in bytes */
15062584db1SSimon Glass 	enum os_dirent_t type;		/* Type of entry */
15162584db1SSimon Glass 	char name[0];			/* Name of entry */
15262584db1SSimon Glass };
15362584db1SSimon Glass 
15462584db1SSimon Glass /**
15562584db1SSimon Glass  * Get a directionry listing
15662584db1SSimon Glass  *
15762584db1SSimon Glass  * This allocates and returns a linked list containing the directory listing.
15862584db1SSimon Glass  *
15962584db1SSimon Glass  * @param dirname	Directory to examine
16062584db1SSimon Glass  * @param headp		Returns pointer to head of linked list, or NULL if none
16162584db1SSimon Glass  * @return 0 if ok, -ve on error
16262584db1SSimon Glass  */
16362584db1SSimon Glass int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
16462584db1SSimon Glass 
16562584db1SSimon Glass /**
16662584db1SSimon Glass  * Get the name of a directory entry type
16762584db1SSimon Glass  *
16862584db1SSimon Glass  * @param type		Type to cehck
16962584db1SSimon Glass  * @return string containing the name of that type, or "???" if none/invalid
17062584db1SSimon Glass  */
17162584db1SSimon Glass const char *os_dirent_get_typename(enum os_dirent_t type);
17262584db1SSimon Glass 
17362584db1SSimon Glass /**
17462584db1SSimon Glass  * Get the size of a file
17562584db1SSimon Glass  *
17662584db1SSimon Glass  * @param fname		Filename to check
17762584db1SSimon Glass  * @return size of file, or -1 if an error ocurred
17862584db1SSimon Glass  */
17962584db1SSimon Glass ssize_t os_get_filesize(const char *fname);
18062584db1SSimon Glass 
1814f345d56SMike Frysinger #endif
182