bloblist.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * This provides a standard way of passing information between boot phases
  4. * (TPL -> SPL -> U-Boot proper.)
  5. *
  6. * A list of blobs of data, tagged with their owner. The list resides in memory
  7. * and can be updated by SPL, U-Boot, etc.
  8. *
  9. * Copyright 2018 Google, Inc
  10. * Written by Simon Glass <sjg@chromium.org>
  11. */
  12. #ifndef __BLOBLIST_H
  13. #define __BLOBLIST_H
  14. enum {
  15. BLOBLIST_VERSION = 0,
  16. BLOBLIST_MAGIC = 0xb00757a3,
  17. BLOBLIST_ALIGN = 16,
  18. };
  19. enum bloblist_tag_t {
  20. BLOBLISTT_NONE = 0,
  21. /* Vendor-specific tags are permitted here */
  22. BLOBLISTT_EC_HOSTEVENT, /* Chromium OS EC host-event mask */
  23. BLOBLISTT_SPL_HANDOFF, /* Hand-off info from SPL */
  24. BLOBLISTT_VBOOT_CTX, /* Chromium OS verified boot context */
  25. BLOBLISTT_VBOOT_HANDOFF, /* Chromium OS internal handoff info */
  26. };
  27. /**
  28. * struct bloblist_hdr - header for the bloblist
  29. *
  30. * This is stored at the start of the bloblist which is always on a 16-byte
  31. * boundary. Records follow this header. The bloblist normally stays in the
  32. * same place in memory as SPL and U-Boot execute, but it can be safely moved
  33. * around.
  34. *
  35. * None of the bloblist structures contain pointers but it is possible to put
  36. * pointers inside a bloblist record if desired. This is not encouraged,
  37. * since it can make part of the bloblist inaccessible if the pointer is
  38. * no-longer valid. It is better to just store all the data inside a bloblist
  39. * record.
  40. *
  41. * Each bloblist record is aligned to a 16-byte boundary and follows immediately
  42. * from the last.
  43. *
  44. * @version: BLOBLIST_VERSION
  45. * @hdr_size: Size of this header, normally sizeof(struct bloblist_hdr). The
  46. * first bloblist_rec starts at this offset from the start of the header
  47. * @flags: Space for BLOBLISTF_... flags (none yet)
  48. * @magic: BLOBLIST_MAGIC
  49. * @size: Total size of all records (non-zero if valid) including this header.
  50. * The bloblist extends for this many bytes from the start of this header.
  51. * @alloced: Total size allocated for this bloblist. When adding new records,
  52. * the bloblist can grow up to this size. This starts out as
  53. * sizeof(bloblist_hdr) since we need at least that much space to store a
  54. * valid bloblist
  55. * @spare: Space space
  56. * @chksum: CRC32 for the entire bloblist allocated area. Since any of the
  57. * blobs can be altered after being created, this checksum is only valid
  58. * when the bloblist is finalised before jumping to the next stage of boot.
  59. * Note: @chksum is last to make it easier to exclude it from the checksum
  60. * calculation.
  61. */
  62. struct bloblist_hdr {
  63. u32 version;
  64. u32 hdr_size;
  65. u32 flags;
  66. u32 magic;
  67. u32 size;
  68. u32 alloced;
  69. u32 spare;
  70. u32 chksum;
  71. };
  72. /**
  73. * struct bloblist_rec - record for the bloblist
  74. *
  75. * NOTE: Only exported for testing purposes. Do not use this struct.
  76. *
  77. * The bloblist contains a number of records each consisting of this record
  78. * structure followed by the data contained. Each records is 16-byte aligned.
  79. *
  80. * @tag: Tag indicating what the record contains
  81. * @hdr_size: Size of this header, normally sizeof(struct bloblist_rec). The
  82. * record's data starts at this offset from the start of the record
  83. * @size: Size of record in bytes, excluding the header size. This does not
  84. * need to be aligned (e.g. 3 is OK).
  85. * @spare: Spare space for other things
  86. */
  87. struct bloblist_rec {
  88. u32 tag;
  89. u32 hdr_size;
  90. u32 size;
  91. u32 spare;
  92. };
  93. /**
  94. * bloblist_find() - Find a blob
  95. *
  96. * Searches the bloblist and returns the blob with the matching tag
  97. *
  98. * @tag: Tag to search for (enum bloblist_tag_t)
  99. * @size: Expected size of the blob
  100. * @return pointer to blob if found, or NULL if not found, or a blob was found
  101. * but it is the wrong size
  102. */
  103. void *bloblist_find(uint tag, int size);
  104. /**
  105. * bloblist_add() - Add a new blob
  106. *
  107. * Add a new blob to the bloblist
  108. *
  109. * This should only be called if you konw there is no existing blob for a
  110. * particular tag. It is typically safe to call in the first phase of U-Boot
  111. * (e.g. TPL or SPL). After that, bloblist_ensure() should be used instead.
  112. *
  113. * @tag: Tag to add (enum bloblist_tag_t)
  114. * @size: Size of the blob
  115. * @return pointer to the newly added block, or NULL if there is not enough
  116. * space for the blob
  117. */
  118. void *bloblist_add(uint tag, int size);
  119. /**
  120. * bloblist_ensure_size() - Find or add a blob
  121. *
  122. * Find an existing blob, or add a new one if not found
  123. *
  124. * @tag: Tag to add (enum bloblist_tag_t)
  125. * @size: Size of the blob
  126. * @blobp: Returns a pointer to blob on success
  127. * @return 0 if OK, -ENOSPC if it is missing and could not be added due to lack
  128. * of space, or -ESPIPE it exists but has the wrong size
  129. */
  130. int bloblist_ensure_size(uint tag, int size, void **blobp);
  131. /**
  132. * bloblist_ensure() - Find or add a blob
  133. *
  134. * Find an existing blob, or add a new one if not found
  135. *
  136. * @tag: Tag to add (enum bloblist_tag_t)
  137. * @size: Size of the blob
  138. * @return pointer to blob, or NULL if it is missing and could not be added due
  139. * to lack of space, or it exists but has the wrong size
  140. */
  141. void *bloblist_ensure(uint tag, int size);
  142. /**
  143. * bloblist_new() - Create a new, empty bloblist of a given size
  144. *
  145. * @addr: Address of bloblist
  146. * @size: Initial size for bloblist
  147. * @flags: Flags to use for bloblist
  148. * @return 0 if OK, -EFAULT if addr is not aligned correctly, -ENOSPC is the
  149. * area is not large enough
  150. */
  151. int bloblist_new(ulong addr, uint size, uint flags);
  152. /**
  153. * bloblist_check() - Check if a bloblist exists
  154. *
  155. * @addr: Address of bloblist
  156. * @size: Expected size of blobsize, or 0 to detect the size
  157. * @return 0 if OK, -ENOENT if the magic number doesn't match (indicating that
  158. * there problem is no bloblist at the given address), -EPROTONOSUPPORT
  159. * if the version does not match, -EIO if the checksum does not match,
  160. * -EFBIG if the expected size does not match the detected size
  161. */
  162. int bloblist_check(ulong addr, uint size);
  163. /**
  164. * bloblist_finish() - Set up the bloblist for the next U-Boot part
  165. *
  166. * This sets the correct checksum for the bloblist. This ensures that the
  167. * bloblist will be detected correctly by the next phase of U-Boot.
  168. *
  169. * @return 0
  170. */
  171. int bloblist_finish(void);
  172. /**
  173. * bloblist_init() - Init the bloblist system with a single bloblist
  174. *
  175. * This uses CONFIG_BLOBLIST_ADDR and CONFIG_BLOBLIST_SIZE to set up a bloblist
  176. * for use by U-Boot.
  177. */
  178. int bloblist_init(void);
  179. #endif /* __BLOBLIST_H */