xref: /rk3399_rockchip-uboot/include/os.h (revision 62584db191013f13133be0f6702d0c935a7c85a6)
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.
87a9219c1SSimon Glass  * See file CREDITS for list of people who contributed to this
97a9219c1SSimon Glass  * project.
107a9219c1SSimon Glass  *
117a9219c1SSimon Glass  * This program is free software; you can redistribute it and/or
127a9219c1SSimon Glass  * modify it under the terms of the GNU General Public License as
137a9219c1SSimon Glass  * published by the Free Software Foundation; either version 2 of
147a9219c1SSimon Glass  * the License, or (at your option) any later version.
157a9219c1SSimon Glass  *
167a9219c1SSimon Glass  * This program is distributed in the hope that it will be useful,
177a9219c1SSimon Glass  * but WITHOUT ANY WARRANTY; without even the implied warranty of
187a9219c1SSimon Glass  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
197a9219c1SSimon Glass  * GNU General Public License for more details.
207a9219c1SSimon Glass  *
217a9219c1SSimon Glass  * You should have received a copy of the GNU General Public License
227a9219c1SSimon Glass  * along with this program; if not, write to the Free Software
237a9219c1SSimon Glass  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
247a9219c1SSimon Glass  * MA 02111-1307 USA
257a9219c1SSimon Glass  */
267a9219c1SSimon Glass 
274f345d56SMike Frysinger #ifndef __OS_H__
284f345d56SMike Frysinger #define __OS_H__
294f345d56SMike Frysinger 
3070db4212SSimon Glass struct sandbox_state;
3170db4212SSimon Glass 
327a9219c1SSimon Glass /**
337a9219c1SSimon Glass  * Access to the OS read() system call
347a9219c1SSimon Glass  *
357a9219c1SSimon Glass  * \param fd	File descriptor as returned by os_open()
367a9219c1SSimon Glass  * \param buf	Buffer to place data
377a9219c1SSimon Glass  * \param count	Number of bytes to read
387a9219c1SSimon Glass  * \return number of bytes read, or -1 on error
397a9219c1SSimon Glass  */
407a9219c1SSimon Glass ssize_t os_read(int fd, void *buf, size_t count);
417a9219c1SSimon Glass 
427a9219c1SSimon Glass /**
43e101550aSTaylor Hutt  * Access to the OS read() system call with non-blocking access
44e101550aSTaylor Hutt  *
45e101550aSTaylor Hutt  * \param fd	File descriptor as returned by os_open()
46e101550aSTaylor Hutt  * \param buf	Buffer to place data
47e101550aSTaylor Hutt  * \param count	Number of bytes to read
48e101550aSTaylor Hutt  * \return number of bytes read, or -1 on error
49e101550aSTaylor Hutt  */
50e101550aSTaylor Hutt ssize_t os_read_no_block(int fd, void *buf, size_t count);
51e101550aSTaylor Hutt 
52e101550aSTaylor Hutt /**
537a9219c1SSimon Glass  * Access to the OS write() system call
547a9219c1SSimon Glass  *
557a9219c1SSimon Glass  * \param fd	File descriptor as returned by os_open()
567a9219c1SSimon Glass  * \param buf	Buffer containing data to write
577a9219c1SSimon Glass  * \param count	Number of bytes to write
587a9219c1SSimon Glass  * \return number of bytes written, or -1 on error
597a9219c1SSimon Glass  */
607a9219c1SSimon Glass ssize_t os_write(int fd, const void *buf, size_t count);
617a9219c1SSimon Glass 
627a9219c1SSimon Glass /**
63e2dcefcbSMike Frysinger  * Access to the OS lseek() system call
64e2dcefcbSMike Frysinger  *
65e2dcefcbSMike Frysinger  * \param fd	File descriptor as returned by os_open()
66e2dcefcbSMike Frysinger  * \param offset	File offset (based on whence)
67e2dcefcbSMike Frysinger  * \param whence	Position offset is relative to (see below)
68e2dcefcbSMike Frysinger  * \return new file offset
69e2dcefcbSMike Frysinger  */
70e2dcefcbSMike Frysinger off_t os_lseek(int fd, off_t offset, int whence);
71e2dcefcbSMike Frysinger 
72e2dcefcbSMike Frysinger /* Defines for "whence" in os_lseek() */
73e2dcefcbSMike Frysinger #define OS_SEEK_SET	0
74e2dcefcbSMike Frysinger #define OS_SEEK_CUR	1
75e2dcefcbSMike Frysinger #define OS_SEEK_END	2
76e2dcefcbSMike Frysinger 
77e2dcefcbSMike Frysinger /**
787a9219c1SSimon Glass  * Access to the OS open() system call
797a9219c1SSimon Glass  *
807a9219c1SSimon Glass  * \param pathname	Pathname of file to open
817a9219c1SSimon Glass  * \param flags		Flags, like O_RDONLY, O_RDWR
827a9219c1SSimon Glass  * \return file descriptor, or -1 on error
837a9219c1SSimon Glass  */
847a9219c1SSimon Glass int os_open(const char *pathname, int flags);
857a9219c1SSimon Glass 
86d9165153SSimon Glass #define OS_O_RDONLY	0
87d9165153SSimon Glass #define OS_O_WRONLY	1
88d9165153SSimon Glass #define OS_O_RDWR	2
89d9165153SSimon Glass #define OS_O_MASK	3	/* Mask for read/write flags */
90d9165153SSimon Glass #define OS_O_CREAT	0100
91d9165153SSimon Glass 
927a9219c1SSimon Glass /**
937a9219c1SSimon Glass  * Access to the OS close() system call
947a9219c1SSimon Glass  *
957a9219c1SSimon Glass  * \param fd	File descriptor to close
967a9219c1SSimon Glass  * \return 0 on success, -1 on error
977a9219c1SSimon Glass  */
987a9219c1SSimon Glass int os_close(int fd);
997a9219c1SSimon Glass 
1007a9219c1SSimon Glass /**
1017a9219c1SSimon Glass  * Access to the OS exit() system call
1027a9219c1SSimon Glass  *
1037a9219c1SSimon Glass  * This exits with the supplied return code, which should be 0 to indicate
1047a9219c1SSimon Glass  * success.
1057a9219c1SSimon Glass  *
1067a9219c1SSimon Glass  * @param exit_code	exit code for U-Boot
1077a9219c1SSimon Glass  */
1089d72e67bSMike Frysinger void os_exit(int exit_code) __attribute__((noreturn));
109ab06a758SMike Frysinger 
110ab06a758SMike Frysinger /**
111ab06a758SMike Frysinger  * Put tty into raw mode to mimic serial console better
112ab06a758SMike Frysinger  */
113ab06a758SMike Frysinger void os_tty_raw(int fd);
11421899b10SMatthias Weisser 
11521899b10SMatthias Weisser /**
11621899b10SMatthias Weisser  * Acquires some memory from the underlying os.
11721899b10SMatthias Weisser  *
11821899b10SMatthias Weisser  * \param length	Number of bytes to be allocated
11921899b10SMatthias Weisser  * \return Pointer to length bytes or NULL on error
12021899b10SMatthias Weisser  */
12121899b10SMatthias Weisser void *os_malloc(size_t length);
122d99a6874SMatthias Weisser 
123d99a6874SMatthias Weisser /**
124d99a6874SMatthias Weisser  * Access to the usleep function of the os
125d99a6874SMatthias Weisser  *
126d99a6874SMatthias Weisser  * \param usec Time to sleep in micro seconds
127d99a6874SMatthias Weisser  */
128d99a6874SMatthias Weisser void os_usleep(unsigned long usec);
129d99a6874SMatthias Weisser 
130d99a6874SMatthias Weisser /**
131d99a6874SMatthias Weisser  * Gets a monotonic increasing number of nano seconds from the OS
132d99a6874SMatthias Weisser  *
133d99a6874SMatthias Weisser  * \return A monotonic increasing time scaled in nano seconds
134d99a6874SMatthias Weisser  */
135d99a6874SMatthias Weisser u64 os_get_nsec(void);
1364f345d56SMike Frysinger 
13770db4212SSimon Glass /**
13870db4212SSimon Glass  * Parse arguments and update sandbox state.
13970db4212SSimon Glass  *
14070db4212SSimon Glass  * @param state		Sandbox state to update
14170db4212SSimon Glass  * @param argc		Argument count
14270db4212SSimon Glass  * @param argv		Argument vector
14370db4212SSimon Glass  * @return 0 if ok, and program should continue;
14470db4212SSimon Glass  *	1 if ok, but program should stop;
14570db4212SSimon Glass  *	-1 on error: program should terminate.
14670db4212SSimon Glass  */
14770db4212SSimon Glass int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
14870db4212SSimon Glass 
149*62584db1SSimon Glass /*
150*62584db1SSimon Glass  * Types of directory entry that we support. See also os_dirent_typename in
151*62584db1SSimon Glass  * the C file.
152*62584db1SSimon Glass  */
153*62584db1SSimon Glass enum os_dirent_t {
154*62584db1SSimon Glass 	OS_FILET_REG,		/* Regular file */
155*62584db1SSimon Glass 	OS_FILET_LNK,		/* Symbolic link */
156*62584db1SSimon Glass 	OS_FILET_DIR,		/* Directory */
157*62584db1SSimon Glass 	OS_FILET_UNKNOWN,	/* Something else */
158*62584db1SSimon Glass 
159*62584db1SSimon Glass 	OS_FILET_COUNT,
160*62584db1SSimon Glass };
161*62584db1SSimon Glass 
162*62584db1SSimon Glass /** A directory entry node, containing information about a single dirent */
163*62584db1SSimon Glass struct os_dirent_node {
164*62584db1SSimon Glass 	struct os_dirent_node *next;	/* Pointer to next node, or NULL */
165*62584db1SSimon Glass 	ulong size;			/* Size of file in bytes */
166*62584db1SSimon Glass 	enum os_dirent_t type;		/* Type of entry */
167*62584db1SSimon Glass 	char name[0];			/* Name of entry */
168*62584db1SSimon Glass };
169*62584db1SSimon Glass 
170*62584db1SSimon Glass /**
171*62584db1SSimon Glass  * Get a directionry listing
172*62584db1SSimon Glass  *
173*62584db1SSimon Glass  * This allocates and returns a linked list containing the directory listing.
174*62584db1SSimon Glass  *
175*62584db1SSimon Glass  * @param dirname	Directory to examine
176*62584db1SSimon Glass  * @param headp		Returns pointer to head of linked list, or NULL if none
177*62584db1SSimon Glass  * @return 0 if ok, -ve on error
178*62584db1SSimon Glass  */
179*62584db1SSimon Glass int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
180*62584db1SSimon Glass 
181*62584db1SSimon Glass /**
182*62584db1SSimon Glass  * Get the name of a directory entry type
183*62584db1SSimon Glass  *
184*62584db1SSimon Glass  * @param type		Type to cehck
185*62584db1SSimon Glass  * @return string containing the name of that type, or "???" if none/invalid
186*62584db1SSimon Glass  */
187*62584db1SSimon Glass const char *os_dirent_get_typename(enum os_dirent_t type);
188*62584db1SSimon Glass 
189*62584db1SSimon Glass /**
190*62584db1SSimon Glass  * Get the size of a file
191*62584db1SSimon Glass  *
192*62584db1SSimon Glass  * @param fname		Filename to check
193*62584db1SSimon Glass  * @return size of file, or -1 if an error ocurred
194*62584db1SSimon Glass  */
195*62584db1SSimon Glass ssize_t os_get_filesize(const char *fname);
196*62584db1SSimon Glass 
1974f345d56SMike Frysinger #endif
198