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.h 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef BUFFER_H
  2. #define BUFFER_H
  3. #include "etherboot.h"
  4. /** @file
  5. *
  6. * Buffers for loading files.
  7. *
  8. * This file provides routines for filling a buffer with data received
  9. * piecemeal, where the size of the data is not necessarily known in
  10. * advance.
  11. *
  12. * Some protocols do not provide a mechanism for us to know the size
  13. * of the file before we happen to receive a particular block
  14. * (e.g. the final block in an MTFTP transfer). In addition, some
  15. * protocols (all the multicast protocols plus any TCP-based protocol)
  16. * can, in theory, provide the data in any order.
  17. *
  18. * Rather than requiring each protocol to implement its own equivalent
  19. * of "dd" to arrange the data into well-sized pieces before handing
  20. * off to the image loader, we provide these generic buffer functions
  21. * which assemble a file into a single contiguous block. The whole
  22. * block is then passed to the image loader.
  23. *
  24. * Example usage:
  25. *
  26. * @code
  27. *
  28. * struct buffer my_buffer;
  29. * void *data;
  30. * off_t offset;
  31. * size_t len;
  32. *
  33. * // We have an area of memory [buf_start,buf_end) into which we want
  34. * // to load a file, where buf_start and buf_end are physical addresses.
  35. * buffer->start = buf_start;
  36. * buffer->end = buf_end;
  37. * init_buffer ( &buffer );
  38. * ...
  39. * while ( get_file_block ( ... ) ) {
  40. * // Downloaded block is stored in [data,data+len), and represents
  41. * // the portion of the file at offsets [offset,offset+len)
  42. * if ( ! fill_buffer ( &buffer, data, offset, len ) ) {
  43. * // An error occurred
  44. * return 0;
  45. * }
  46. * ...
  47. * }
  48. * ...
  49. * // The whole file is now present at [buf_start,buf_start+filesize),
  50. * // where buf_start is a physical address. The struct buffer can simply
  51. * // be discarded; there is no done_buffer() call.
  52. *
  53. * @endcode
  54. *
  55. * For a description of the internal operation, see buffer.c.
  56. *
  57. */
  58. /**
  59. * A buffer
  60. *
  61. * @c start and @c end denote the real boundaries of the buffer, and
  62. * are physical addresses. @c fill denotes the offset to the first
  63. * free block in the buffer. (If the buffer is full, @c fill will
  64. * equal @c end-start.)
  65. *
  66. */
  67. struct buffer {
  68. physaddr_t start; /**< Start of buffer in memory */
  69. physaddr_t end; /**< End of buffer in memory */
  70. off_t fill; /**< Offset to first gap in buffer */
  71. };
  72. /**
  73. * A free block descriptor.
  74. *
  75. * See buffer.c for a full description of the fields.
  76. *
  77. */
  78. struct buffer_free_block {
  79. char tail; /**< Tail byte marker */
  80. physaddr_t next_free; /**< Address of next free block */
  81. physaddr_t end; /**< End of this block */
  82. } __attribute__ (( packed ));
  83. /* Functions in buffer.c */
  84. extern void init_buffer ( struct buffer *buffer );
  85. extern int fill_buffer ( struct buffer *buffer, const void *data,
  86. off_t offset, size_t len );
  87. #endif /* BUFFER_H */