iPXE
xferbuf.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <errno.h>
00029 #include <ipxe/xfer.h>
00030 #include <ipxe/iobuf.h>
00031 #include <ipxe/umalloc.h>
00032 #include <ipxe/profile.h>
00033 #include <ipxe/xferbuf.h>
00034 
00035 /** @file
00036  *
00037  * Data transfer buffer
00038  *
00039  */
00040 
00041 /** Data delivery profiler */
00042 static struct profiler xferbuf_deliver_profiler __profiler =
00043         { .name = "xferbuf.deliver" };
00044 
00045 /** Data write profiler */
00046 static struct profiler xferbuf_write_profiler __profiler =
00047         { .name = "xferbuf.write" };
00048 
00049 /** Data read profiler */
00050 static struct profiler xferbuf_read_profiler __profiler =
00051         { .name = "xferbuf.read" };
00052 
00053 /**
00054  * Free data transfer buffer
00055  *
00056  * @v xferbuf           Data transfer buffer
00057  */
00058 void xferbuf_free ( struct xfer_buffer *xferbuf ) {
00059 
00060         xferbuf->op->realloc ( xferbuf, 0 );
00061         xferbuf->len = 0;
00062         xferbuf->pos = 0;
00063 }
00064 
00065 /**
00066  * Ensure that data transfer buffer is large enough for the specified size
00067  *
00068  * @v xferbuf           Data transfer buffer
00069  * @v len               Required minimum size
00070  * @ret rc              Return status code
00071  */
00072 static int xferbuf_ensure_size ( struct xfer_buffer *xferbuf, size_t len ) {
00073         int rc;
00074 
00075         /* If buffer is already large enough, do nothing */
00076         if ( len <= xferbuf->len )
00077                 return 0;
00078 
00079         /* Extend buffer */
00080         if ( ( rc = xferbuf->op->realloc ( xferbuf, len ) ) != 0 ) {
00081                 DBGC ( xferbuf, "XFERBUF %p could not extend buffer to "
00082                        "%zd bytes: %s\n", xferbuf, len, strerror ( rc ) );
00083                 return rc;
00084         }
00085         xferbuf->len = len;
00086 
00087         return 0;
00088 }
00089 
00090 /**
00091  * Write to data transfer buffer
00092  *
00093  * @v xferbuf           Data transfer buffer
00094  * @v offset            Starting offset
00095  * @v data              Data to write
00096  * @v len               Length of data
00097  */
00098 int xferbuf_write ( struct xfer_buffer *xferbuf, size_t offset,
00099                     const void *data, size_t len ) {
00100         size_t max_len;
00101         int rc;
00102 
00103         /* Check for overflow */
00104         max_len = ( offset + len );
00105         if ( max_len < offset )
00106                 return -EOVERFLOW;
00107 
00108         /* Ensure buffer is large enough to contain this write */
00109         if ( ( rc = xferbuf_ensure_size ( xferbuf, max_len ) ) != 0 )
00110                 return rc;
00111 
00112         /* Copy data to buffer */
00113         profile_start ( &xferbuf_write_profiler );
00114         xferbuf->op->write ( xferbuf, offset, data, len );
00115         profile_stop ( &xferbuf_write_profiler );
00116 
00117         return 0;
00118 }
00119 
00120 /**
00121  * Read from data transfer buffer
00122  *
00123  * @v xferbuf           Data transfer buffer
00124  * @v offset            Starting offset
00125  * @v data              Data to write
00126  * @v len               Length of data
00127  */
00128 int xferbuf_read ( struct xfer_buffer *xferbuf, size_t offset,
00129                    void *data, size_t len ) {
00130 
00131         /* Check that read is within buffer range */
00132         if ( ( offset > xferbuf->len ) ||
00133              ( len > ( xferbuf->len - offset ) ) )
00134                 return -ENOENT;
00135 
00136         /* Copy data from buffer */
00137         profile_start ( &xferbuf_read_profiler );
00138         xferbuf->op->read ( xferbuf, offset, data, len );
00139         profile_stop ( &xferbuf_read_profiler );
00140 
00141         return 0;
00142 }
00143 
00144 /**
00145  * Add received data to data transfer buffer
00146  *
00147  * @v xferbuf           Data transfer buffer
00148  * @v iobuf             I/O buffer
00149  * @v meta              Data transfer metadata
00150  * @ret rc              Return status code
00151  */
00152 int xferbuf_deliver ( struct xfer_buffer *xferbuf, struct io_buffer *iobuf,
00153                       struct xfer_metadata *meta ) {
00154         size_t len = iob_len ( iobuf );
00155         size_t pos;
00156         int rc;
00157 
00158         /* Start profiling */
00159         profile_start ( &xferbuf_deliver_profiler );
00160 
00161         /* Calculate new buffer position */
00162         pos = xferbuf->pos;
00163         if ( meta->flags & XFER_FL_ABS_OFFSET )
00164                 pos = 0;
00165         pos += meta->offset;
00166 
00167         /* Write data to buffer */
00168         if ( ( rc = xferbuf_write ( xferbuf, pos, iobuf->data, len ) ) != 0 )
00169                 goto done;
00170 
00171         /* Update current buffer position */
00172         xferbuf->pos = ( pos + len );
00173 
00174  done:
00175         free_iob ( iobuf );
00176         profile_stop ( &xferbuf_deliver_profiler );
00177         return rc;
00178 }
00179 
00180 /**
00181  * Reallocate malloc()-based data buffer
00182  *
00183  * @v xferbuf           Data transfer buffer
00184  * @v len               New length (or zero to free buffer)
00185  * @ret rc              Return status code
00186  */
00187 static int xferbuf_malloc_realloc ( struct xfer_buffer *xferbuf, size_t len ) {
00188         void *new_data;
00189 
00190         new_data = realloc ( xferbuf->data, len );
00191         if ( ! new_data )
00192                 return -ENOSPC;
00193         xferbuf->data = new_data;
00194         return 0;
00195 }
00196 
00197 /**
00198  * Write data to malloc()-based data buffer
00199  *
00200  * @v xferbuf           Data transfer buffer
00201  * @v offset            Starting offset
00202  * @v data              Data to copy
00203  * @v len               Length of data
00204  */
00205 static void xferbuf_malloc_write ( struct xfer_buffer *xferbuf, size_t offset,
00206                                    const void *data, size_t len ) {
00207 
00208         memcpy ( ( xferbuf->data + offset ), data, len );
00209 }
00210 
00211 /**
00212  * Read data from malloc()-based data buffer
00213  *
00214  * @v xferbuf           Data transfer buffer
00215  * @v offset            Starting offset
00216  * @v data              Data to read
00217  * @v len               Length of data
00218  */
00219 static void xferbuf_malloc_read ( struct xfer_buffer *xferbuf, size_t offset,
00220                                   void *data, size_t len ) {
00221 
00222         memcpy ( data, ( xferbuf->data + offset ), len );
00223 }
00224 
00225 /** malloc()-based data buffer operations */
00226 struct xfer_buffer_operations xferbuf_malloc_operations = {
00227         .realloc = xferbuf_malloc_realloc,
00228         .write = xferbuf_malloc_write,
00229         .read = xferbuf_malloc_read,
00230 };
00231 
00232 /**
00233  * Reallocate umalloc()-based data buffer
00234  *
00235  * @v xferbuf           Data transfer buffer
00236  * @v len               New length (or zero to free buffer)
00237  * @ret rc              Return status code
00238  */
00239 static int xferbuf_umalloc_realloc ( struct xfer_buffer *xferbuf, size_t len ) {
00240         userptr_t *udata = xferbuf->data;
00241         userptr_t new_udata;
00242 
00243         new_udata = urealloc ( *udata, len );
00244         if ( ! new_udata )
00245                 return -ENOSPC;
00246         *udata = new_udata;
00247         return 0;
00248 }
00249 
00250 /**
00251  * Write data to umalloc()-based data buffer
00252  *
00253  * @v xferbuf           Data transfer buffer
00254  * @v offset            Starting offset
00255  * @v data              Data to copy
00256  * @v len               Length of data
00257  */
00258 static void xferbuf_umalloc_write ( struct xfer_buffer *xferbuf, size_t offset,
00259                                     const void *data, size_t len ) {
00260         userptr_t *udata = xferbuf->data;
00261 
00262         copy_to_user ( *udata, offset, data, len );
00263 }
00264 
00265 /**
00266  * Read data from umalloc()-based data buffer
00267  *
00268  * @v xferbuf           Data transfer buffer
00269  * @v offset            Starting offset
00270  * @v data              Data to read
00271  * @v len               Length of data
00272  */
00273 static void xferbuf_umalloc_read ( struct xfer_buffer *xferbuf, size_t offset,
00274                                    void *data, size_t len ) {
00275         userptr_t *udata = xferbuf->data;
00276 
00277         copy_from_user ( data, *udata, offset, len );
00278 }
00279 
00280 /** umalloc()-based data buffer operations */
00281 struct xfer_buffer_operations xferbuf_umalloc_operations = {
00282         .realloc = xferbuf_umalloc_realloc,
00283         .write = xferbuf_umalloc_write,
00284         .read = xferbuf_umalloc_read,
00285 };
00286 
00287 /**
00288  * Get underlying data transfer buffer
00289  *
00290  * @v interface         Data transfer interface
00291  * @ret xferbuf         Data transfer buffer, or NULL on error
00292  *
00293  * This call will check that the xfer_buffer() handler belongs to the
00294  * destination interface which also provides xfer_deliver() for this
00295  * interface.
00296  *
00297  * This is done to prevent accidental accesses to a data transfer
00298  * buffer which may be located behind a non-transparent datapath via a
00299  * series of pass-through interfaces.
00300  */
00301 struct xfer_buffer * xfer_buffer ( struct interface *intf ) {
00302         struct interface *dest;
00303         xfer_buffer_TYPE ( void * ) *op =
00304                 intf_get_dest_op ( intf, xfer_buffer, &dest );
00305         void *object = intf_object ( dest );
00306         struct interface *xfer_deliver_dest;
00307         struct xfer_buffer *xferbuf;
00308 
00309         /* Check that this operation is provided by the same interface
00310          * which handles xfer_deliver().
00311          */
00312         ( void ) intf_get_dest_op ( intf, xfer_deliver, &xfer_deliver_dest );
00313 
00314         if ( op && ( dest == xfer_deliver_dest ) ) {
00315                 xferbuf = op ( object );
00316         } else {
00317                 /* Default is to not have a data transfer buffer */
00318                 xferbuf = NULL;
00319         }
00320 
00321         intf_put ( xfer_deliver_dest );
00322         intf_put ( dest );
00323         return xferbuf;
00324 }