2007-01-11 05:25:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
2007-05-19 18:39:40 +00:00
|
|
|
* I/O buffer padding
|
2007-01-11 05:25:50 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
2007-05-19 18:39:40 +00:00
|
|
|
#include <gpxe/iobuf.h>
|
2007-01-11 05:25:50 +00:00
|
|
|
|
|
|
|
/**
|
2007-05-19 18:39:40 +00:00
|
|
|
* Pad I/O buffer
|
2007-01-11 05:25:50 +00:00
|
|
|
*
|
2007-05-19 18:39:40 +00:00
|
|
|
* @v iobuf I/O buffer
|
2007-01-11 05:25:50 +00:00
|
|
|
* @v min_len Minimum length
|
|
|
|
*
|
2007-05-19 18:39:40 +00:00
|
|
|
* This function pads and aligns I/O buffers, for devices that
|
2007-01-11 05:25:50 +00:00
|
|
|
* aren't capable of padding in hardware, or that require specific
|
|
|
|
* alignment in TX buffers. The packet data will end up aligned to a
|
2007-05-19 18:39:40 +00:00
|
|
|
* multiple of @c IOB_ALIGN.
|
2007-01-11 05:25:50 +00:00
|
|
|
*
|
2007-05-19 18:39:40 +00:00
|
|
|
* @c min_len must not exceed @v IOB_ZLEN.
|
2007-01-11 05:25:50 +00:00
|
|
|
*/
|
2007-05-19 18:39:40 +00:00
|
|
|
void iob_pad ( struct io_buffer *iobuf, size_t min_len ) {
|
2007-01-11 05:25:50 +00:00
|
|
|
void *data;
|
|
|
|
size_t len;
|
|
|
|
size_t headroom;
|
|
|
|
signed int pad_len;
|
|
|
|
|
2007-05-19 18:39:40 +00:00
|
|
|
assert ( min_len <= IOB_ZLEN );
|
2007-01-11 05:25:50 +00:00
|
|
|
|
2007-05-19 18:39:40 +00:00
|
|
|
/* Move packet data to start of I/O buffer. This will both
|
|
|
|
* align the data (since I/O buffers are aligned to
|
|
|
|
* IOB_ALIGN) and give us sufficient space for the
|
2007-01-11 05:25:50 +00:00
|
|
|
* zero-padding
|
|
|
|
*/
|
2007-05-19 18:39:40 +00:00
|
|
|
data = iobuf->data;
|
|
|
|
len = iob_len ( iobuf );
|
|
|
|
headroom = iob_headroom ( iobuf );
|
|
|
|
iob_push ( iobuf, headroom );
|
|
|
|
memmove ( iobuf->data, data, len );
|
|
|
|
iob_unput ( iobuf, headroom );
|
2007-01-11 05:25:50 +00:00
|
|
|
|
|
|
|
/* Pad to minimum packet length */
|
2007-05-19 18:39:40 +00:00
|
|
|
pad_len = ( min_len - iob_len ( iobuf ) );
|
2007-01-11 05:25:50 +00:00
|
|
|
if ( pad_len > 0 )
|
2007-05-19 18:39:40 +00:00
|
|
|
memset ( iob_put ( iobuf, pad_len ), 0, pad_len );
|
2007-01-11 05:25:50 +00:00
|
|
|
}
|