iPXE
posix_io.h
Go to the documentation of this file.
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
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
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
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
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  */
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 */
int open(const char *uri_string)
Open file.
Definition: posix_io.c:176
uint32_t buffer
Buffer index (or NETVSC_RNDIS_NO_BUFFER)
Definition: netvsc.h:16
Access to external ("user") memory.
static fd_set * set
Definition: posix_io.h:48
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static __attribute__((always_inline)) void FD_ZERO(fd_set *set)
Zero a file descriptor set.
Definition: posix_io.h:36
uint32_t fd_set
File descriptor set as used for select()
Definition: posix_io.h:22
ssize_t read_user(int fd, userptr_t buffer, off_t offset, size_t len)
Read data from file.
Definition: posix_io.c:265
unsigned int uint32_t
Definition: stdint.h:12
signed long off_t
Definition: stdint.h:8
static ssize_t read(int fd, void *buf, size_t len)
Read data from file.
Definition: posix_io.h:83
uint32_t len
Length.
Definition: ena.h:14
userptr_t virt_to_user(volatile const void *addr)
Convert virtual address to user pointer.
int select(fd_set *readfds, int wait)
Check file descriptors for readiness.
Definition: posix_io.c:229
signed long ssize_t
Definition: stdint.h:7
ssize_t fsize(int fd)
Determine file size.
Definition: posix_io.c:311
int close(int fd)
Close file.
Definition: posix_io.c:328
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33