1 #ifndef _IPXE_POSIX_IO_H
2 #define _IPXE_POSIX_IO_H
3 
4 /** @file
5  *
6  * POSIX-like I/O
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <stdint.h>
13 #include <ipxe/uaccess.h>
14 
15 /** Minimum file descriptor that will ever be allocated */
16 #define POSIX_FD_MIN ( 1 )
17 
18 /** Maximum file descriptor that will ever be allocated */
19 #define POSIX_FD_MAX ( 31 )
20 
21 /** File descriptor set as used for select() */
22 typedef uint32_t fd_set;
23 
24 extern int open ( const char *uri_string );
25 extern ssize_t read_user ( int fd, userptr_t buffer,
26 			   off_t offset, size_t len );
27 extern int select ( fd_set *readfds, int wait );
28 extern ssize_t fsize ( int fd );
29 extern int close ( int fd );
30 
31 /**
32  * Zero a file descriptor set
33  *
34  * @v set		File descriptor set
35  */
36 static inline __attribute__ (( always_inline )) void
FD_ZERO(fd_set * set)37 FD_ZERO ( fd_set *set ) {
38 	*set = 0;
39 }
40 
41 /**
42  * Set a bit within a file descriptor set
43  *
44  * @v fd		File descriptor
45  * @v set		File descriptor set
46  */
47 static inline __attribute__ (( always_inline )) void
FD_SET(int fd,fd_set * set)48 FD_SET ( int fd, fd_set *set ) {
49 	*set |= ( 1 << fd );
50 }
51 
52 /**
53  * Clear a bit within a file descriptor set
54  *
55  * @v fd		File descriptor
56  * @v set		File descriptor set
57  */
58 static inline __attribute__ (( always_inline )) void
FD_CLR(int fd,fd_set * set)59 FD_CLR ( int fd, fd_set *set ) {
60 	*set &= ~( 1 << fd );
61 }
62 
63 /**
64  * Test a bit within a file descriptor set
65  *
66  * @v fd		File descriptor
67  * @v set		File descriptor set
68  * @ret is_set		Corresponding bit is set
69  */
70 static inline __attribute__ (( always_inline )) int
FD_ISSET(int fd,fd_set * set)71 FD_ISSET ( int fd, fd_set *set ) {
72 	return ( *set & ( 1 << fd ) );
73 }
74 
75 /**
76  * Read data from file
77  *
78  * @v fd		File descriptor
79  * @v buf		Data buffer
80  * @v len		Maximum length to read
81  * @ret len		Actual length read, or negative error number
82  */
read(int fd,void * buf,size_t len)83 static inline ssize_t read ( int fd, void *buf, size_t len ) {
84 	return read_user ( fd, virt_to_user ( buf ), 0, len );
85 }
86 
87 #endif /* _IPXE_POSIX_IO_H */
88