You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

buffer.c 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stddef.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <assert.h>
  22. #include <gpxe/uaccess.h>
  23. #include <gpxe/buffer.h>
  24. /** @file
  25. *
  26. * Buffer internals.
  27. *
  28. * A buffer consists of a single, contiguous area of memory, some of
  29. * which is "filled" and the remainder of which is "free". The
  30. * "filled" and "free" spaces are not necessarily contiguous.
  31. *
  32. * At the start of a buffer's life, it consists of a single free
  33. * space. As data is added to the buffer via fill_buffer(), this free
  34. * space decreases and can become fragmented.
  35. *
  36. * Each free block within a buffer (except the last) starts with a @c
  37. * struct @c buffer_free_block. This describes the size of the free
  38. * block, and the offset to the next free block.
  39. *
  40. * We cannot simply start every free block (including the last) with a
  41. * descriptor, because it is conceivable that we will, at some point,
  42. * encounter a situation in which the final free block of a buffer is
  43. * too small to contain a descriptor. Consider a protocol with a
  44. * blocksize of 512 downloading a 1025-byte file into a 1025-byte
  45. * buffer. Suppose that the first two blocks are received; we have
  46. * now filled 1024 of the 1025 bytes in the buffer, and our only free
  47. * block consists of the 1025th byte.
  48. *
  49. * Note that the rather convoluted way of manipulating the buffer
  50. * descriptors (using copy_{to,from}_user rather than straightforward
  51. * pointers) is needed to cope with operation as a PXE stack, when we
  52. * may be running in real mode or 16-bit protected mode, and therefore
  53. * cannot directly access arbitrary areas of memory using simple
  54. * pointers.
  55. *
  56. */
  57. /**
  58. * A free block descriptor
  59. *
  60. * This is the data structure that is found at the start of a free
  61. * block within a data buffer.
  62. */
  63. struct buffer_free_block {
  64. /** Starting offset of the free block */
  65. size_t start;
  66. /** Ending offset of the free block */
  67. size_t end;
  68. /** Offset of next free block */
  69. size_t next;
  70. };
  71. /**
  72. * Get next free block within the buffer
  73. *
  74. * @v buffer Data buffer
  75. * @v block Previous free block descriptor
  76. * @ret block Next free block descriptor
  77. * @ret rc Return status code
  78. *
  79. * Set @c block->next=buffer->fill before first call to
  80. * get_next_free_block().
  81. */
  82. static int get_next_free_block ( struct buffer *buffer,
  83. struct buffer_free_block *block ) {
  84. /* Check for end of buffer */
  85. if ( block->next >= buffer->len )
  86. return -ENOENT;
  87. /* Move to next block */
  88. block->start = block->next;
  89. if ( block->start >= buffer->free ) {
  90. /* Final block; no in-band descriptor */
  91. block->next = block->end = buffer->len;
  92. } else {
  93. /* Retrieve block descriptor */
  94. copy_from_user ( block, buffer->addr, block->start,
  95. sizeof ( *block ) );
  96. }
  97. return 0;
  98. }
  99. /**
  100. * Write free block descriptor back to buffer
  101. *
  102. * @v buffer Data buffer
  103. * @v block Free block descriptor
  104. */
  105. static void store_free_block ( struct buffer *buffer,
  106. struct buffer_free_block *block ) {
  107. size_t free_block_size = ( block->end - block->start );
  108. assert ( free_block_size >= sizeof ( *block ) );
  109. copy_to_user ( buffer->addr, block->start, block, sizeof ( *block ) );
  110. }
  111. /**
  112. * Write data into a buffer
  113. *
  114. * @v buffer Data buffer
  115. * @v data Data to be written
  116. * @v offset Offset within the buffer at which to write the data
  117. * @v len Length of data to be written
  118. * @ret rc Return status code
  119. *
  120. * Writes a block of data into the buffer. The block need not be
  121. * aligned to any particular boundary, or be of any particular size,
  122. * and it may overlap blocks already in the buffer (i.e. duplicate
  123. * calls to fill_buffer() are explicitly permitted).
  124. *
  125. * @c buffer->fill will be updated to indicate the fill level of the
  126. * buffer, i.e. the offset to the first gap within the buffer. If the
  127. * filesize is known (e.g. as with the SLAM protocol), you can test
  128. * for end-of-file by checking for @c buffer->fill==filesize. If the
  129. * filesize is not known, but there is a well-defined end-of-file test
  130. * (e.g. as with the TFTP protocol), you can read @c buffer->fill to
  131. * determine the final filesize. If blocks are known to be delivered
  132. * in a strictly sequential order with no packet loss or duplication,
  133. * then you can pass in @c offset==buffer->fill.
  134. *
  135. * @b NOTE: It is the caller's responsibility to ensure that the
  136. * boundaries between data blocks are more than @c sizeof(struct @c
  137. * buffer_free_block) apart. If this condition is not satisfied, data
  138. * corruption will occur.
  139. *
  140. * In practice this is not a problem. Callers of fill_buffer() will
  141. * be download protocols such as TFTP, and very few protocols have a
  142. * block size smaller than @c sizeof(struct @c buffer_free_block).
  143. *
  144. */
  145. int fill_buffer ( struct buffer *buffer, const void *data,
  146. size_t offset, size_t len ) {
  147. struct buffer_free_block block, before, after;
  148. size_t data_start = offset;
  149. size_t data_end = ( data_start + len );
  150. int rc;
  151. DBGC2 ( buffer, "BUFFER %p [%lx,%lx) filling portion [%lx,%lx)\n",
  152. buffer, user_to_phys ( buffer->addr, 0 ),
  153. user_to_phys ( buffer->addr, buffer->len ),
  154. user_to_phys ( buffer->addr, data_start ),
  155. user_to_phys ( buffer->addr, data_end ) );
  156. /* Check that block fits within buffer, expand if necessary */
  157. if ( data_end > buffer->len ) {
  158. if ( ( rc = expand_buffer ( buffer, data_end ) ) != 0 )
  159. return rc;
  160. assert ( buffer->len >= data_end );
  161. }
  162. /* Find 'before' and 'after' blocks, if any */
  163. before.start = before.end = 0;
  164. after.start = after.end = buffer->len;
  165. block.next = buffer->fill;
  166. while ( get_next_free_block ( buffer, &block ) == 0 ) {
  167. if ( ( block.start < data_start ) &&
  168. ( block.start >= before.start ) )
  169. memcpy ( &before, &block, sizeof ( before ) );
  170. if ( ( block.end > data_end ) &&
  171. ( block.end <= after.end ) )
  172. memcpy ( &after, &block, sizeof ( after ) );
  173. }
  174. /* Truncate 'before' and 'after' blocks around data. */
  175. if ( data_start < before.end )
  176. before.end = data_start;
  177. if ( data_end > after.start )
  178. after.start = data_end;
  179. /* Link 'after' block to 'before' block */
  180. before.next = after.start;
  181. DBGC2 ( buffer, "BUFFER %p split before [%lx,%lx) after [%lx,%lx)\n",
  182. buffer, user_to_phys ( buffer->addr, before.start ),
  183. user_to_phys ( buffer->addr, before.end ),
  184. user_to_phys ( buffer->addr, after.start ),
  185. user_to_phys ( buffer->addr, after.end ) );
  186. /* Write back 'before' block, if any */
  187. if ( before.end == 0 ) {
  188. /* No 'before' block: update buffer->fill */
  189. buffer->fill = after.start;
  190. DBGC2 ( buffer, "BUFFER %p full up to %lx\n", buffer,
  191. user_to_phys ( buffer->addr, buffer->fill ) );
  192. } else {
  193. /* Write back 'before' block */
  194. store_free_block ( buffer, &before );
  195. }
  196. /* Write back 'after' block */
  197. if ( after.end == buffer->len ) {
  198. /* 'After' block is the final block: update buffer->free */
  199. buffer->free = after.start;
  200. DBGC2 ( buffer, "BUFFER %p free from %lx onwards\n", buffer,
  201. user_to_phys ( buffer->addr, buffer->free ) );
  202. } else {
  203. /* Write back 'after' block */
  204. store_free_block ( buffer, &after );
  205. }
  206. /* Copy data into buffer */
  207. copy_to_user ( buffer->addr, data_start, data, len );
  208. return 0;
  209. }
  210. /** Expand data buffer
  211. *
  212. * @v buffer Data buffer
  213. * @v new_len New length
  214. * @ret rc Return status code
  215. *
  216. * Expand the data buffer to accommodate more data. Some buffers may
  217. * not support being expanded.
  218. */
  219. int expand_buffer ( struct buffer *buffer, size_t new_len ) {
  220. int rc;
  221. if ( new_len <= buffer->len )
  222. return 0;
  223. DBGC ( buffer, "BUFFER %p attempting to expand from length %zx to "
  224. "length %zx\n", buffer, buffer->len, new_len );
  225. if ( ! buffer->expand ) {
  226. DBGC ( buffer, "BUFFER %p is not expandable\n", buffer );
  227. return -ENOBUFS;
  228. }
  229. if ( ( rc = buffer->expand ( buffer, new_len ) ) != 0 ) {
  230. DBGC ( buffer, "BUFFER %p could not expand: %s\n",
  231. buffer, strerror ( rc ) );
  232. return rc;
  233. }
  234. DBGC ( buffer, "BUFFER %p expanded to [%lx,%lx)\n", buffer,
  235. user_to_phys ( buffer->addr, 0 ),
  236. user_to_phys ( buffer->addr, buffer->len ) );
  237. return 0;
  238. }