2005-05-09 10:11:11 +00:00
|
|
|
#ifndef BUFFER_H
|
|
|
|
#define BUFFER_H
|
|
|
|
|
2005-05-20 10:27:02 +00:00
|
|
|
#include "compiler.h" /* for doxygen */
|
2005-05-19 23:21:18 +00:00
|
|
|
#include "stdint.h"
|
2005-05-09 13:24:01 +00:00
|
|
|
|
2005-05-19 18:32:04 +00:00
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Buffers for loading files.
|
|
|
|
*
|
|
|
|
* This file provides routines for filling a buffer with data received
|
|
|
|
* piecemeal, where the size of the data is not necessarily known in
|
|
|
|
* advance.
|
|
|
|
*
|
|
|
|
* Some protocols do not provide a mechanism for us to know the size
|
|
|
|
* of the file before we happen to receive a particular block
|
|
|
|
* (e.g. the final block in an MTFTP transfer). In addition, some
|
|
|
|
* protocols (all the multicast protocols plus any TCP-based protocol)
|
|
|
|
* can, in theory, provide the data in any order.
|
|
|
|
*
|
|
|
|
* Rather than requiring each protocol to implement its own equivalent
|
|
|
|
* of "dd" to arrange the data into well-sized pieces before handing
|
|
|
|
* off to the image loader, we provide these generic buffer functions
|
|
|
|
* which assemble a file into a single contiguous block. The whole
|
|
|
|
* block is then passed to the image loader.
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
*
|
|
|
|
* struct buffer my_buffer;
|
|
|
|
* void *data;
|
|
|
|
* off_t offset;
|
|
|
|
* size_t len;
|
|
|
|
*
|
|
|
|
* // We have an area of memory [buf_start,buf_end) into which we want
|
|
|
|
* // to load a file, where buf_start and buf_end are physical addresses.
|
|
|
|
* buffer->start = buf_start;
|
|
|
|
* buffer->end = buf_end;
|
|
|
|
* init_buffer ( &buffer );
|
|
|
|
* ...
|
|
|
|
* while ( get_file_block ( ... ) ) {
|
|
|
|
* // Downloaded block is stored in [data,data+len), and represents
|
|
|
|
* // the portion of the file at offsets [offset,offset+len)
|
|
|
|
* if ( ! fill_buffer ( &buffer, data, offset, len ) ) {
|
|
|
|
* // An error occurred
|
|
|
|
* return 0;
|
|
|
|
* }
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
* ...
|
|
|
|
* // The whole file is now present at [buf_start,buf_start+filesize),
|
|
|
|
* // where buf_start is a physical address. The struct buffer can simply
|
|
|
|
* // be discarded; there is no done_buffer() call.
|
|
|
|
*
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* For a description of the internal operation, see buffer.c.
|
|
|
|
*
|
|
|
|
*/
|
2005-05-19 11:54:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A buffer
|
|
|
|
*
|
2005-05-20 10:27:02 +00:00
|
|
|
* #start and #end denote the real boundaries of the buffer, and are
|
|
|
|
* physical addresses. #fill denotes the offset to the first free
|
|
|
|
* block in the buffer. (If the buffer is full, #fill will equal
|
|
|
|
* #end-#start.)
|
2005-05-09 14:26:10 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
struct buffer {
|
2005-05-19 11:54:41 +00:00
|
|
|
physaddr_t start; /**< Start of buffer in memory */
|
|
|
|
physaddr_t end; /**< End of buffer in memory */
|
|
|
|
off_t fill; /**< Offset to first gap in buffer */
|
2005-05-09 14:26:10 +00:00
|
|
|
};
|
|
|
|
|
2005-05-19 11:54:41 +00:00
|
|
|
/**
|
|
|
|
* A free block descriptor.
|
2005-05-09 13:24:01 +00:00
|
|
|
*
|
2005-05-19 18:32:04 +00:00
|
|
|
* See buffer.c for a full description of the fields.
|
2005-05-09 13:24:01 +00:00
|
|
|
*
|
|
|
|
*/
|
2005-05-09 10:11:11 +00:00
|
|
|
struct buffer_free_block {
|
2005-05-19 11:54:41 +00:00
|
|
|
char tail; /**< Tail byte marker */
|
2005-06-06 11:42:14 +00:00
|
|
|
char reserved[3]; /**< Padding */
|
|
|
|
physaddr_t start; /**< Address of this free block */
|
|
|
|
physaddr_t next; /**< Address of next free block */
|
2005-05-19 11:54:41 +00:00
|
|
|
physaddr_t end; /**< End of this block */
|
2005-05-09 13:24:01 +00:00
|
|
|
} __attribute__ (( packed ));
|
2005-05-09 10:11:11 +00:00
|
|
|
|
2005-05-09 13:24:01 +00:00
|
|
|
/* Functions in buffer.c */
|
|
|
|
|
2005-05-09 18:03:44 +00:00
|
|
|
extern void init_buffer ( struct buffer *buffer );
|
2005-05-17 14:34:46 +00:00
|
|
|
extern int fill_buffer ( struct buffer *buffer, const void *data,
|
2005-05-09 14:26:10 +00:00
|
|
|
off_t offset, size_t len );
|
2005-05-09 13:24:01 +00:00
|
|
|
|
2005-05-09 10:11:11 +00:00
|
|
|
#endif /* BUFFER_H */
|