6 * A buffer consists of a single, contiguous area of memory, some of
7 * which is "filled" and the remainder of which is "free". The
8 * "filled" and "free" spaces are not necessarily contiguous.
10 * When a buffer is initialised via init_buffer(), it consists of a
11 * single free space. As data is added to the buffer via
12 * fill_buffer(), this free space decreases and can become fragmented.
14 * Each free block within a buffer starts with a "tail byte". If the
15 * tail byte is non-zero, this indicates that the free block is the
16 * tail of the buffer, i.e. occupies all the remaining space up to the
17 * end of the buffer. When the tail byte is non-zero, it indicates
18 * that a descriptor (a @c struct @c buffer_free_block) follows the
19 * tail byte. The descriptor describes the size of the free block and
20 * the address of the next free block.
22 * We cannot simply always start a free block with a descriptor,
23 * because it is conceivable that we will, at some point, encounter a
24 * situation in which the final free block of a buffer is too small to
25 * contain a descriptor. Consider a protocol with a blocksize of 512
26 * downloading a 1025-byte file into a 1025-byte buffer. Suppose that
27 * the first two blocks are received; we have now filled 1024 of the
28 * 1025 bytes in the buffer, and our only free block consists of the
29 * 1025th byte. Using a "tail byte" solves this problem.
32 * Note that the rather convoluted way of manipulating the buffer
33 * descriptors (using copy_{to,from}_phys rather than straightforward
34 * pointers) is needed to cope with operation as a PXE stack, when we
35 * may be running in real mode or 16-bit protected mode, and therefore
36 * cannot directly access arbitrary areas of memory using simple
49 * Initialise a buffer.
51 * @v buffer The buffer to be initialised
55 * Set @c buffer->start and @c buffer->end before calling init_buffer().
56 * init_buffer() will initialise the buffer to the state of being
60 void init_buffer ( struct buffer
*buffer
) {
64 if ( buffer
->end
!= buffer
->start
)
65 copy_to_phys ( buffer
->start
, &tail
, sizeof ( tail
) );
67 DBG ( "BUFFER [%x,%x) initialised\n", buffer
->start
, buffer
->end
);
71 * Move to the next block in the free list
73 * @v block The current free block
74 * @v buffer The buffer
75 * @ret True Successfully moved to the next free block
76 * @ret False There are no more free blocks
77 * @ret block The next free block
80 * Move to the next block in the free block list, filling in @c block
81 * with the descriptor for this next block. If the next block is the
82 * tail block, @c block will be filled with the values calculated for
83 * the tail block, otherwise the descriptor will be read from the free
86 * If there are no more free blocks, next_free_block() returns False
87 * and leaves @c block with invalid contents.
89 * Set <tt> block->next = buffer->start + buffer->fill </tt> for the
90 * first call to next_free_block().
92 static inline int next_free_block ( struct buffer_free_block
*block
,
93 struct buffer
*buffer
) {
94 /* Move to next block */
95 block
->start
= block
->next
;
97 /* If at end of buffer, return 0 */
98 if ( block
->start
>= buffer
->end
)
101 /* Set up ->next and ->end as for a tail block */
102 block
->next
= block
->end
= buffer
->end
;
104 /* Read tail marker from block */
105 copy_from_phys ( &block
->tail
, block
->start
, sizeof ( block
->tail
) );
107 /* If not a tail block, read whole block descriptor from block */
108 if ( ! block
->tail
) {
109 copy_from_phys ( block
, block
->start
, sizeof ( *block
) );
116 * Store a free block descriptor
118 * @v block The free block descriptor to store
122 * Writes a free block descriptor back to a free block. If the block
123 * is a tail block, only the tail marker will be written, otherwise
124 * the whole block descriptor will be written.
126 static inline void store_free_block ( struct buffer_free_block
*block
) {
127 copy_to_phys ( block
->start
, block
,
129 sizeof ( block
->tail
) : sizeof ( *block
) ) );
133 * Write data into a buffer.
135 * @v buffer The buffer into which to write the data
136 * @v data The data to be written
137 * @v offset Offset within the buffer at which to write the data
138 * @v len Length of data to be written
139 * @ret True Data was successfully written
140 * @ret False Data was not written
141 * @err ENOMEM Buffer is too small to contain the data
143 * Writes a block of data into the buffer. The block need not be
144 * aligned to any particular boundary, or be of any particular size,
145 * and it may overlap blocks already in the buffer (i.e. duplicate
146 * calls to fill_buffer() are explicitly permitted).
148 * @c buffer->fill will be updated to indicate the fill level of the
149 * buffer, i.e. the offset to the first gap within the buffer. If the
150 * filesize is known (e.g. as with the SLAM protocol), you can test
151 * for end-of-file by checking for @c buffer->fill==filesize. If the
152 * filesize is not known, but there is a well-defined end-of-file test
153 * (e.g. as with the TFTP protocol), you can read @c buffer->fill to
154 * determine the final filesize. If blocks are known to be delivered
155 * in a strictly sequential order with no packet loss or duplication,
156 * then you can pass in @c offset==buffer->fill.
158 * @b NOTE: It is the caller's responsibility to ensure that the
159 * boundaries between data blocks are more than @c sizeof(struct @c
160 * buffer_free_block) apart. If this condition is not satisfied, data
161 * corruption will occur.
163 * In practice this is not a problem. Callers of fill_buffer() will
164 * be download protocols such as TFTP, and very few protocols have a
165 * block size smaller than @c sizeof(struct @c buffer_free_block).
168 int fill_buffer ( struct buffer
*buffer
, const void *data
,
169 off_t offset
, size_t len
) {
170 struct buffer_free_block block
, before
, after
;
171 physaddr_t data_start
, data_end
;
173 /* Calculate start and end addresses of data */
174 data_start
= buffer
->start
+ offset
;
175 data_end
= data_start
+ len
;
176 DBG ( "BUFFER [%x,%x) writing portion [%x,%x)\n",
177 buffer
->start
, buffer
->end
, data_start
, data_end
);
179 /* Check buffer bounds */
180 if ( data_end
> buffer
->end
) {
181 DBG ( "BUFFER [%x,%x) too small for data!\n",
182 buffer
->start
, buffer
->end
);
187 /* Find 'before' and 'after' blocks, if any */
188 before
.start
= before
.end
= 0;
189 after
.start
= after
.end
= buffer
->end
;
190 block
.next
= buffer
->start
+ buffer
->fill
;
191 while ( next_free_block ( &block
, buffer
) ) {
192 if ( ( block
.start
< data_start
) &&
193 ( block
.start
>= before
.start
) )
194 memcpy ( &before
, &block
, sizeof ( before
) );
195 if ( ( block
.end
> data_end
) &&
196 ( block
.end
<= after
.end
) )
197 memcpy ( &after
, &block
, sizeof ( after
) );
200 /* Truncate 'before' and 'after' blocks around data. */
201 if ( data_start
< before
.end
)
202 before
.end
= data_start
;
203 if ( data_end
> after
.start
)
204 after
.start
= data_end
;
206 /* Link 'after' block to 'before' block */
207 before
.next
= after
.start
;
209 /* Write back 'before' block, if any */
210 if ( before
.start
) {
212 assert ( ( before
.end
- before
.start
) >=
213 sizeof ( struct buffer_free_block
) );
214 store_free_block ( &before
);
216 buffer
->fill
= before
.next
- buffer
->start
;
219 /* Write back 'after' block, if any */
220 if ( after
.start
< buffer
->end
) {
221 assert ( after
.tail
||
222 ( ( after
.end
- after
.start
) >=
223 sizeof ( struct buffer_free_block
) ) );
224 store_free_block ( &after
);
227 DBG ( "BUFFER [%x,%x) before [%x,%x) after [%x,%x)\n",
228 buffer
->start
, buffer
->end
, before
.start
, before
.end
,
229 after
.start
, after
.end
);
231 /* Copy data into buffer */
232 copy_to_phys ( data_start
, data
, len
);
234 DBG ( "BUFFER [%x,%x) full up to %x\n",
235 buffer
->start
, buffer
->end
, buffer
->start
+ buffer
->fill
);