/firmware/src/Motherboard/lib_sd/partition.h

http://github.com/makerbot/G3Firmware · C Header · 212 lines · 41 code · 14 blank · 157 comment · 0 complexity · 079f67414f927b2791350f18e950cdb4 MD5 · raw file

  1. /*
  2. * Copyright (c) 2006-2010 by Roland Riegel <feedback@roland-riegel.de>
  3. *
  4. * This file is free software; you can redistribute it and/or modify
  5. * it under the terms of either the GNU General Public License version 2
  6. * or the GNU Lesser General Public License version 2.1, both as
  7. * published by the Free Software Foundation.
  8. */
  9. #ifndef PARTITION_H
  10. #define PARTITION_H
  11. #include <stdint.h>
  12. #include "sd_raw_config.h"
  13. #include "partition_config.h"
  14. #ifdef __cplusplus
  15. extern "C"
  16. {
  17. #endif
  18. /**
  19. * \addtogroup partition
  20. *
  21. * @{
  22. */
  23. /**
  24. * \file
  25. * Partition table header (license: GPLv2 or LGPLv2.1)
  26. *
  27. * \author Roland Riegel
  28. */
  29. /**
  30. * The partition table entry is not used.
  31. */
  32. #define PARTITION_TYPE_FREE 0x00
  33. /**
  34. * The partition contains a FAT12 filesystem.
  35. */
  36. #define PARTITION_TYPE_FAT12 0x01
  37. /**
  38. * The partition contains a FAT16 filesystem with 32MB maximum.
  39. */
  40. #define PARTITION_TYPE_FAT16_32MB 0x04
  41. /**
  42. * The partition is an extended partition with its own partition table.
  43. */
  44. #define PARTITION_TYPE_EXTENDED 0x05
  45. /**
  46. * The partition contains a FAT16 filesystem.
  47. */
  48. #define PARTITION_TYPE_FAT16 0x06
  49. /**
  50. * The partition contains a FAT32 filesystem.
  51. */
  52. #define PARTITION_TYPE_FAT32 0x0b
  53. /**
  54. * The partition contains a FAT32 filesystem with LBA.
  55. */
  56. #define PARTITION_TYPE_FAT32_LBA 0x0c
  57. /**
  58. * The partition contains a FAT16 filesystem with LBA.
  59. */
  60. #define PARTITION_TYPE_FAT16_LBA 0x0e
  61. /**
  62. * The partition is an extended partition with LBA.
  63. */
  64. #define PARTITION_TYPE_EXTENDED_LBA 0x0f
  65. /**
  66. * The partition has an unknown type.
  67. */
  68. #define PARTITION_TYPE_UNKNOWN 0xff
  69. /**
  70. * A function pointer used to read from the partition.
  71. *
  72. * \param[in] offset The offset on the device where to start reading.
  73. * \param[out] buffer The buffer into which to place the data.
  74. * \param[in] length The count of bytes to read.
  75. */
  76. typedef uint8_t (*device_read_t)(offset_t offset, uint8_t* buffer, uintptr_t length);
  77. /**
  78. * A function pointer passed to a \c device_read_interval_t.
  79. *
  80. * \param[in] buffer The buffer which contains the data just read.
  81. * \param[in] offset The offset from which the data in \c buffer was read.
  82. * \param[in] p An opaque pointer.
  83. * \see device_read_interval_t
  84. */
  85. typedef uint8_t (*device_read_callback_t)(uint8_t* buffer, offset_t offset, void* p);
  86. /**
  87. * A function pointer used to continuously read units of \c interval bytes
  88. * and call a callback function.
  89. *
  90. * This function starts reading at the specified offset. Every \c interval bytes,
  91. * it calls the callback function with the associated data buffer.
  92. *
  93. * By returning zero, the callback may stop reading.
  94. *
  95. * \param[in] offset Offset from which to start reading.
  96. * \param[in] buffer Pointer to a buffer which is at least interval bytes in size.
  97. * \param[in] interval Number of bytes to read before calling the callback function.
  98. * \param[in] length Number of bytes to read altogether.
  99. * \param[in] callback The function to call every interval bytes.
  100. * \param[in] p An opaque pointer directly passed to the callback function.
  101. * \returns 0 on failure, 1 on success
  102. * \see device_read_t
  103. */
  104. typedef uint8_t (*device_read_interval_t)(offset_t offset, uint8_t* buffer, uintptr_t interval, uintptr_t length, device_read_callback_t callback, void* p);
  105. /**
  106. * A function pointer used to write to the partition.
  107. *
  108. * \param[in] offset The offset on the device where to start writing.
  109. * \param[in] buffer The buffer which to write.
  110. * \param[in] length The count of bytes to write.
  111. */
  112. typedef uint8_t (*device_write_t)(offset_t offset, const uint8_t* buffer, uintptr_t length);
  113. /**
  114. * A function pointer passed to a \c device_write_interval_t.
  115. *
  116. * \param[in] buffer The buffer which receives the data to write.
  117. * \param[in] offset The offset to which the data in \c buffer will be written.
  118. * \param[in] p An opaque pointer.
  119. * \returns The number of bytes put into \c buffer
  120. * \see device_write_interval_t
  121. */
  122. typedef uintptr_t (*device_write_callback_t)(uint8_t* buffer, offset_t offset, void* p);
  123. /**
  124. * A function pointer used to continuously write a data stream obtained from
  125. * a callback function.
  126. *
  127. * This function starts writing at the specified offset. To obtain the
  128. * next bytes to write, it calls the callback function. The callback fills the
  129. * provided data buffer and returns the number of bytes it has put into the buffer.
  130. *
  131. * By returning zero, the callback may stop writing.
  132. *
  133. * \param[in] offset Offset where to start writing.
  134. * \param[in] buffer Pointer to a buffer which is used for the callback function.
  135. * \param[in] length Number of bytes to write in total. May be zero for endless writes.
  136. * \param[in] callback The function used to obtain the bytes to write.
  137. * \param[in] p An opaque pointer directly passed to the callback function.
  138. * \returns 0 on failure, 1 on success
  139. * \see device_write_t
  140. */
  141. typedef uint8_t (*device_write_interval_t)(offset_t offset, uint8_t* buffer, uintptr_t length, device_write_callback_t callback, void* p);
  142. /**
  143. * Describes a partition.
  144. */
  145. struct partition_struct
  146. {
  147. /**
  148. * The function which reads data from the partition.
  149. *
  150. * \note The offset given to this function is relative to the whole disk,
  151. * not to the start of the partition.
  152. */
  153. device_read_t device_read;
  154. /**
  155. * The function which repeatedly reads a constant amount of data from the partition.
  156. *
  157. * \note The offset given to this function is relative to the whole disk,
  158. * not to the start of the partition.
  159. */
  160. device_read_interval_t device_read_interval;
  161. /**
  162. * The function which writes data to the partition.
  163. *
  164. * \note The offset given to this function is relative to the whole disk,
  165. * not to the start of the partition.
  166. */
  167. device_write_t device_write;
  168. /**
  169. * The function which repeatedly writes data to the partition.
  170. *
  171. * \note The offset given to this function is relative to the whole disk,
  172. * not to the start of the partition.
  173. */
  174. device_write_interval_t device_write_interval;
  175. /**
  176. * The type of the partition.
  177. *
  178. * Compare this value to the PARTITION_TYPE_* constants.
  179. */
  180. uint8_t type;
  181. /**
  182. * The offset in blocks on the disk where this partition starts.
  183. */
  184. uint32_t offset;
  185. /**
  186. * The length in blocks of this partition.
  187. */
  188. uint32_t length;
  189. };
  190. struct partition_struct* partition_open(device_read_t device_read, device_read_interval_t device_read_interval, device_write_t device_write, device_write_interval_t device_write_interval, int8_t index);
  191. uint8_t partition_close(struct partition_struct* partition);
  192. /**
  193. * @}
  194. */
  195. #ifdef __cplusplus
  196. }
  197. #endif
  198. #endif