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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 #include <ipxe/list.h>
30 #include <ipxe/iobuf.h>
31 #include <ipxe/xfer.h>
32 #include <ipxe/open.h>
33 #include <ipxe/process.h>
34 #include <ipxe/posix_io.h>
40 * These functions provide traditional blocking I/O semantics. They
41 * are designed to be used by the PXE TFTP API. Because they block,
42 * they may not be used by most other portions of the iPXE codebase.
47 /** Reference count for this object */
49 /** List of open files */
50 struct list_head list;
51 /** File descriptor */
55 * Set to -EINPROGRESS while data transfer is in progress.
58 /** Data transfer interface */
59 struct interface xfer;
60 /** Current seek position */
64 /** Received data queue */
65 struct list_head data;
68 /** List of open files */
69 static LIST_HEAD ( posix_files );
74 * @v refcnt Reference counter
76 static void posix_file_free ( struct refcnt *refcnt ) {
77 struct posix_file *file =
78 container_of ( refcnt, struct posix_file, refcnt );
79 struct io_buffer *iobuf;
80 struct io_buffer *tmp;
82 list_for_each_entry_safe ( iobuf, tmp, &file->data, list ) {
83 list_del ( &iobuf->list );
90 * Terminate file data transfer
93 * @v rc Reason for termination
95 static void posix_file_finished ( struct posix_file *file, int rc ) {
96 intf_shutdown ( &file->xfer, rc );
101 * Handle deliver_iob() event
104 * @v iobuf I/O buffer
105 * @v meta Data transfer metadata
106 * @ret rc Return status code
108 static int posix_file_xfer_deliver ( struct posix_file *file,
109 struct io_buffer *iobuf,
110 struct xfer_metadata *meta ) {
112 /* Keep track of file position solely for the filesize */
113 if ( meta->flags & XFER_FL_ABS_OFFSET )
115 file->pos += meta->offset;
116 if ( file->filesize < file->pos )
117 file->filesize = file->pos;
119 if ( iob_len ( iobuf ) ) {
120 list_add_tail ( &iobuf->list, &file->data );
128 /** POSIX file data transfer interface operations */
129 static struct interface_operation posix_file_xfer_operations[] = {
130 INTF_OP ( xfer_deliver, struct posix_file *, posix_file_xfer_deliver ),
131 INTF_OP ( intf_close, struct posix_file *, posix_file_finished ),
134 /** POSIX file data transfer interface descriptor */
135 static struct interface_descriptor posix_file_xfer_desc =
136 INTF_DESC ( struct posix_file, xfer, posix_file_xfer_operations );
139 * Identify file by file descriptor
141 * @v fd File descriptor
142 * @ret file Corresponding file, or NULL
144 static struct posix_file * posix_fd_to_file ( int fd ) {
145 struct posix_file *file;
147 list_for_each_entry ( file, &posix_files, list ) {
148 if ( file->fd == fd )
155 * Find an available file descriptor
157 * @ret fd File descriptor, or negative error number
159 static int posix_find_free_fd ( void ) {
162 for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
163 if ( ! posix_fd_to_file ( fd ) )
166 DBG ( "POSIX could not find free file descriptor\n" );
173 * @v uri_string URI string
174 * @ret fd File descriptor, or negative error number
176 int open ( const char *uri_string ) {
177 struct posix_file *file;
181 /* Find a free file descriptor to use */
182 fd = posix_find_free_fd();
186 /* Allocate and initialise structure */
187 file = zalloc ( sizeof ( *file ) );
190 ref_init ( &file->refcnt, posix_file_free );
192 file->rc = -EINPROGRESS;
193 intf_init ( &file->xfer, &posix_file_xfer_desc, &file->refcnt );
194 INIT_LIST_HEAD ( &file->data );
196 /* Open URI on data transfer interface */
197 if ( ( rc = xfer_open_uri_string ( &file->xfer, uri_string ) ) != 0 )
200 /* Wait for open to succeed or fail */
201 while ( list_empty ( &file->data ) ) {
205 if ( file->rc != -EINPROGRESS ) {
211 /* Add to list of open files. List takes reference ownership. */
212 list_add ( &file->list, &posix_files );
213 DBG ( "POSIX opened %s as file %d\n", uri_string, fd );
217 posix_file_finished ( file, rc );
218 ref_put ( &file->refcnt );
223 * Check file descriptors for readiness
225 * @v readfds File descriptors to check
226 * @v wait Wait until data is ready
227 * @ret nready Number of ready file descriptors
229 int select ( fd_set *readfds, int wait ) {
230 struct posix_file *file;
234 for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
235 if ( ! FD_ISSET ( fd, readfds ) )
237 file = posix_fd_to_file ( fd );
240 if ( ( list_empty ( &file->data ) ) &&
241 ( file->rc == -EINPROGRESS ) )
243 /* Data is available or status has changed */
245 FD_SET ( fd, readfds );
255 * Read data from file
257 * @v buffer Data buffer
258 * @v offset Starting offset within data buffer
259 * @v len Maximum length to read
260 * @ret len Actual length read, or negative error number
262 * This call is non-blocking; if no data is available to read then
263 * -EWOULDBLOCK will be returned.
265 ssize_t read_user ( int fd, userptr_t buffer, off_t offset, size_t max_len ) {
266 struct posix_file *file;
267 struct io_buffer *iobuf;
271 file = posix_fd_to_file ( fd );
275 /* Try to fetch more data if none available */
276 if ( list_empty ( &file->data ) )
279 /* Dequeue at most one received I/O buffer into user buffer */
280 list_for_each_entry ( iobuf, &file->data, list ) {
281 len = iob_len ( iobuf );
284 copy_to_user ( buffer, offset, iobuf->data, len );
285 iob_pull ( iobuf, len );
286 if ( ! iob_len ( iobuf ) ) {
287 list_del ( &iobuf->list );
295 /* If file has completed, return (after returning all data) */
296 if ( file->rc != -EINPROGRESS ) {
297 assert ( list_empty ( &file->data ) );
301 /* No data ready and file still in progress; return -WOULDBLOCK */
306 * Determine file size
308 * @v fd File descriptor
309 * @ret size File size, or negative error number
311 ssize_t fsize ( int fd ) {
312 struct posix_file *file;
315 file = posix_fd_to_file ( fd );
319 return file->filesize;
325 * @v fd File descriptor
326 * @ret rc Return status code
328 int close ( int fd ) {
329 struct posix_file *file;
332 file = posix_fd_to_file ( fd );
336 /* Terminate data transfer */
337 posix_file_finished ( file, 0 );
339 /* Remove from list of open files and drop reference */
340 list_del ( &file->list );
341 ref_put ( &file->refcnt );