2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/list.h>
26 #include <ipxe/iobuf.h>
27 #include <ipxe/xfer.h>
28 #include <ipxe/open.h>
29 #include <ipxe/process.h>
30 #include <ipxe/posix_io.h>
36 * These functions provide traditional blocking I/O semantics. They
37 * are designed to be used by the PXE TFTP API. Because they block,
38 * they may not be used by most other portions of the iPXE codebase.
43 /** Reference count for this object */
45 /** List of open files */
46 struct list_head list;
47 /** File descriptor */
51 * Set to -EINPROGRESS while data transfer is in progress.
54 /** Data transfer interface */
55 struct interface xfer;
56 /** Current seek position */
60 /** Received data queue */
61 struct list_head data;
64 /** List of open files */
65 static LIST_HEAD ( posix_files );
70 * @v refcnt Reference counter
72 static void posix_file_free ( struct refcnt *refcnt ) {
73 struct posix_file *file =
74 container_of ( refcnt, struct posix_file, refcnt );
75 struct io_buffer *iobuf;
76 struct io_buffer *tmp;
78 list_for_each_entry_safe ( iobuf, tmp, &file->data, list ) {
79 list_del ( &iobuf->list );
86 * Terminate file data transfer
89 * @v rc Reason for termination
91 static void posix_file_finished ( struct posix_file *file, int rc ) {
92 intf_shutdown ( &file->xfer, rc );
97 * Handle deliver_iob() event
100 * @v iobuf I/O buffer
101 * @v meta Data transfer metadata
102 * @ret rc Return status code
104 static int posix_file_xfer_deliver ( struct posix_file *file,
105 struct io_buffer *iobuf,
106 struct xfer_metadata *meta ) {
108 /* Keep track of file position solely for the filesize */
109 if ( meta->flags & XFER_FL_ABS_OFFSET )
111 file->pos += meta->offset;
112 if ( file->filesize < file->pos )
113 file->filesize = file->pos;
115 if ( iob_len ( iobuf ) ) {
116 list_add_tail ( &iobuf->list, &file->data );
124 /** POSIX file data transfer interface operations */
125 static struct interface_operation posix_file_xfer_operations[] = {
126 INTF_OP ( xfer_deliver, struct posix_file *, posix_file_xfer_deliver ),
127 INTF_OP ( intf_close, struct posix_file *, posix_file_finished ),
130 /** POSIX file data transfer interface descriptor */
131 static struct interface_descriptor posix_file_xfer_desc =
132 INTF_DESC ( struct posix_file, xfer, posix_file_xfer_operations );
135 * Identify file by file descriptor
137 * @v fd File descriptor
138 * @ret file Corresponding file, or NULL
140 static struct posix_file * posix_fd_to_file ( int fd ) {
141 struct posix_file *file;
143 list_for_each_entry ( file, &posix_files, list ) {
144 if ( file->fd == fd )
151 * Find an available file descriptor
153 * @ret fd File descriptor, or negative error number
155 static int posix_find_free_fd ( void ) {
158 for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
159 if ( ! posix_fd_to_file ( fd ) )
162 DBG ( "POSIX could not find free file descriptor\n" );
169 * @v uri_string URI string
170 * @ret fd File descriptor, or negative error number
172 int open ( const char *uri_string ) {
173 struct posix_file *file;
177 /* Find a free file descriptor to use */
178 fd = posix_find_free_fd();
182 /* Allocate and initialise structure */
183 file = zalloc ( sizeof ( *file ) );
186 ref_init ( &file->refcnt, posix_file_free );
188 file->rc = -EINPROGRESS;
189 intf_init ( &file->xfer, &posix_file_xfer_desc, &file->refcnt );
190 INIT_LIST_HEAD ( &file->data );
192 /* Open URI on data transfer interface */
193 if ( ( rc = xfer_open_uri_string ( &file->xfer, uri_string ) ) != 0 )
196 /* Wait for open to succeed or fail */
197 while ( list_empty ( &file->data ) ) {
201 if ( file->rc != -EINPROGRESS ) {
207 /* Add to list of open files. List takes reference ownership. */
208 list_add ( &file->list, &posix_files );
209 DBG ( "POSIX opened %s as file %d\n", uri_string, fd );
213 posix_file_finished ( file, rc );
214 ref_put ( &file->refcnt );
219 * Check file descriptors for readiness
221 * @v readfds File descriptors to check
222 * @v wait Wait until data is ready
223 * @ret nready Number of ready file descriptors
225 int select ( fd_set *readfds, int wait ) {
226 struct posix_file *file;
230 for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
231 if ( ! FD_ISSET ( fd, readfds ) )
233 file = posix_fd_to_file ( fd );
236 if ( ( list_empty ( &file->data ) ) &&
237 ( file->rc == -EINPROGRESS ) )
239 /* Data is available or status has changed */
241 FD_SET ( fd, readfds );
251 * Read data from file
253 * @v buffer Data buffer
254 * @v offset Starting offset within data buffer
255 * @v len Maximum length to read
256 * @ret len Actual length read, or negative error number
258 * This call is non-blocking; if no data is available to read then
259 * -EWOULDBLOCK will be returned.
261 ssize_t read_user ( int fd, userptr_t buffer, off_t offset, size_t max_len ) {
262 struct posix_file *file;
263 struct io_buffer *iobuf;
267 file = posix_fd_to_file ( fd );
271 /* Try to fetch more data if none available */
272 if ( list_empty ( &file->data ) )
275 /* Dequeue at most one received I/O buffer into user buffer */
276 list_for_each_entry ( iobuf, &file->data, list ) {
277 len = iob_len ( iobuf );
280 copy_to_user ( buffer, offset, iobuf->data, len );
281 iob_pull ( iobuf, len );
282 if ( ! iob_len ( iobuf ) ) {
283 list_del ( &iobuf->list );
291 /* If file has completed, return (after returning all data) */
292 if ( file->rc != -EINPROGRESS ) {
293 assert ( list_empty ( &file->data ) );
297 /* No data ready and file still in progress; return -WOULDBLOCK */
302 * Determine file size
304 * @v fd File descriptor
305 * @ret size File size, or negative error number
307 ssize_t fsize ( int fd ) {
308 struct posix_file *file;
311 file = posix_fd_to_file ( fd );
315 return file->filesize;
321 * @v fd File descriptor
322 * @ret rc Return status code
324 int close ( int fd ) {
325 struct posix_file *file;
328 file = posix_fd_to_file ( fd );
332 /* Terminate data transfer */
333 posix_file_finished ( file, 0 );
335 /* Remove from list of open files and drop reference */
336 list_del ( &file->list );
337 ref_put ( &file->refcnt );