iPXE
iobpad.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 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 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 /**
00027  * @file
00028  *
00029  * I/O buffer padding
00030  *
00031  */
00032 
00033 #include <string.h>
00034 #include <ipxe/iobuf.h>
00035 
00036 /**
00037  * Pad I/O buffer
00038  *
00039  * @v iobuf             I/O buffer
00040  * @v min_len           Minimum length
00041  *
00042  * This function pads and aligns I/O buffers, for devices that
00043  * aren't capable of padding in hardware, or that require specific
00044  * alignment in TX buffers.  The packet data will end up aligned to a
00045  * multiple of @c IOB_ALIGN.
00046  *
00047  * @c min_len must not exceed @v IOB_ZLEN.
00048  */
00049 void iob_pad ( struct io_buffer *iobuf, size_t min_len ) {
00050         void *data;
00051         size_t len;
00052         size_t headroom;
00053         signed int pad_len;
00054 
00055         assert ( min_len <= IOB_ZLEN );
00056 
00057         /* Move packet data to start of I/O buffer.  This will both
00058          * align the data (since I/O buffers are aligned to
00059          * IOB_ALIGN) and give us sufficient space for the
00060          * zero-padding
00061          */
00062         data = iobuf->data;
00063         len = iob_len ( iobuf );
00064         headroom = iob_headroom ( iobuf );
00065         iob_push ( iobuf, headroom );
00066         memmove ( iobuf->data, data, len );
00067         iob_unput ( iobuf, headroom );
00068 
00069         /* Pad to minimum packet length */
00070         pad_len = ( min_len - iob_len ( iobuf ) );
00071         if ( pad_len > 0 )
00072                 memset ( iob_put ( iobuf, pad_len ), 0, pad_len );
00073 }