cbfs.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  4. */
  5. #ifndef __CBFS_H
  6. #define __CBFS_H
  7. #include <compiler.h>
  8. #include <linux/compiler.h>
  9. struct cbfs_priv;
  10. enum cbfs_result {
  11. CBFS_SUCCESS = 0,
  12. CBFS_NOT_INITIALIZED,
  13. CBFS_BAD_HEADER,
  14. CBFS_BAD_FILE,
  15. CBFS_FILE_NOT_FOUND
  16. };
  17. enum cbfs_filetype {
  18. CBFS_TYPE_BOOTBLOCK = 0x01,
  19. CBFS_TYPE_CBFSHEADER = 0x02,
  20. CBFS_TYPE_STAGE = 0x10,
  21. CBFS_TYPE_PAYLOAD = 0x20,
  22. CBFS_TYPE_FIT = 0x21,
  23. CBFS_TYPE_OPTIONROM = 0x30,
  24. CBFS_TYPE_BOOTSPLASH = 0x40,
  25. CBFS_TYPE_RAW = 0x50,
  26. CBFS_TYPE_VSA = 0x51,
  27. CBFS_TYPE_MBI = 0x52,
  28. CBFS_TYPE_MICROCODE = 0x53,
  29. CBFS_TYPE_FSP = 0x60,
  30. CBFS_TYPE_MRC = 0x61,
  31. CBFS_TYPE_MMA = 0x62,
  32. CBFS_TYPE_EFI = 0x63,
  33. CBFS_TYPE_STRUCT = 0x70,
  34. CBFS_TYPE_CMOS_DEFAULT = 0xaa,
  35. CBFS_TYPE_SPD = 0xab,
  36. CBFS_TYPE_MRC_CACHE = 0xac,
  37. CBFS_TYPE_CMOS_LAYOUT = 0x01aa
  38. };
  39. enum {
  40. CBFS_HEADER_MAGIC = 0x4f524243,
  41. CBFS_SIZE_UNKNOWN = 0xffffffff,
  42. CBFS_ALIGN_SIZE = 0x40,
  43. };
  44. /**
  45. * struct cbfs_header - header at the start of a CBFS region
  46. *
  47. * All fields use big-endian format.
  48. *
  49. * @magic: Magic number (CBFS_HEADER_MAGIC)
  50. */
  51. struct cbfs_header {
  52. u32 magic;
  53. u32 version;
  54. u32 rom_size;
  55. u32 boot_block_size;
  56. u32 align;
  57. u32 offset;
  58. u32 pad[2];
  59. } __packed;
  60. struct cbfs_fileheader {
  61. u8 magic[8];
  62. u32 len;
  63. u32 type;
  64. /* offset to struct cbfs_file_attribute or 0 */
  65. u32 attributes_offset;
  66. u32 offset;
  67. char filename[];
  68. } __packed;
  69. /**
  70. * These are standard values for the known compression alogrithms that coreboot
  71. * knows about for stages and payloads. Of course, other CBFS users can use
  72. * whatever values they want, as long as they understand them.
  73. */
  74. #define CBFS_COMPRESS_NONE 0
  75. #define CBFS_COMPRESS_LZMA 1
  76. #define CBFS_COMPRESS_LZ4 2
  77. /*
  78. * Depending on how the header was initialized, it may be backed with 0x00 or
  79. * 0xff, so support both
  80. */
  81. #define CBFS_FILE_ATTR_TAG_UNUSED 0
  82. #define CBFS_FILE_ATTR_TAG_UNUSED2 0xffffffff
  83. #define CBFS_FILE_ATTR_TAG_COMPRESSION 0x42435a4c
  84. #define CBFS_FILE_ATTR_TAG_HASH 0x68736148
  85. /*
  86. * The common fields of extended cbfs file attributes. Attributes are expected
  87. * to start with tag/len, then append their specific fields
  88. */
  89. struct cbfs_file_attribute {
  90. u32 tag;
  91. /* len covers the whole structure, incl. tag and len */
  92. u32 len;
  93. u8 data[0];
  94. } __packed;
  95. struct cbfs_file_attr_compression {
  96. u32 tag;
  97. u32 len;
  98. /* whole file compression format. 0 if no compression. */
  99. u32 compression;
  100. u32 decompressed_size;
  101. } __packed;
  102. struct cbfs_file_attr_hash {
  103. u32 tag;
  104. u32 len;
  105. u32 hash_type;
  106. /* hash_data is len - sizeof(struct) bytes */
  107. u8 hash_data[];
  108. } __packed;
  109. struct cbfs_cachenode {
  110. struct cbfs_cachenode *next;
  111. void *data;
  112. char *name;
  113. u32 type;
  114. u32 data_length;
  115. u32 name_length;
  116. u32 attr_offset;
  117. u32 comp_algo;
  118. u32 decomp_size;
  119. };
  120. /**
  121. * file_cbfs_error() - Return a string describing the most recent error
  122. * condition.
  123. *
  124. * @return A pointer to the constant string.
  125. */
  126. const char *file_cbfs_error(void);
  127. /**
  128. * cbfs_get_result() - Get the result of the last CBFS operation
  129. *
  130. *@return last result
  131. */
  132. enum cbfs_result cbfs_get_result(void);
  133. /**
  134. * file_cbfs_init() - Initialize the CBFS driver and load metadata into RAM.
  135. *
  136. * @end_of_rom: Points to the end of the ROM the CBFS should be read from
  137. * @return 0 if OK, -ve on error
  138. */
  139. int file_cbfs_init(ulong end_of_rom);
  140. /**
  141. * file_cbfs_get_header() - Get the header structure for the current CBFS.
  142. *
  143. * @return A pointer to the constant structure, or NULL if there is none.
  144. */
  145. const struct cbfs_header *file_cbfs_get_header(void);
  146. /**
  147. * cbfs_get_first() - Get the first file in a CBFS
  148. *
  149. * @return pointer to first file, or NULL if it is empty
  150. */
  151. const struct cbfs_cachenode *cbfs_get_first(const struct cbfs_priv *priv);
  152. /**
  153. * cbfs_get_next() - Get the next file in a CBFS
  154. *
  155. * @filep: Pointer to current file; updated to point to the next file, if any,
  156. * else NULL
  157. */
  158. void cbfs_get_next(const struct cbfs_cachenode **filep);
  159. /**
  160. * file_cbfs_get_first() - Get a handle for the first file in CBFS.
  161. *
  162. * @return A handle for the first file in CBFS, NULL on error.
  163. */
  164. const struct cbfs_cachenode *file_cbfs_get_first(void);
  165. /**
  166. * file_cbfs_get_next() - Get a handle to the file after this one in CBFS.
  167. *
  168. * @file: A pointer to the handle to advance.
  169. */
  170. void file_cbfs_get_next(const struct cbfs_cachenode **file);
  171. /**
  172. * file_cbfs_find() - Find a file with a particular name in CBFS.
  173. *
  174. * @name: The name to search for.
  175. *
  176. * @return A handle to the file, or NULL on error.
  177. */
  178. const struct cbfs_cachenode *file_cbfs_find(const char *name);
  179. /**
  180. * cbfs_find_file() - Find a file in a given CBFS
  181. *
  182. * @cbfs: CBFS to look in (use cbfs_init_mem() to set it up)
  183. * @name: Filename to look for
  184. * @return pointer to CBFS node if found, else NULL
  185. */
  186. const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *cbfs,
  187. const char *name);
  188. /**
  189. * cbfs_init_mem() - Set up a new CBFS
  190. *
  191. * @base: Base address of CBFS
  192. * @size: Size of CBFS if known, else CBFS_SIZE_UNKNOWN
  193. * @require_header: true to read a header at the start, false to not require one
  194. * @cbfsp: Returns a pointer to CBFS on success
  195. * @return 0 if OK, -ve on error
  196. */
  197. int cbfs_init_mem(ulong base, ulong size, bool require_hdr,
  198. struct cbfs_priv **privp);
  199. /***************************************************************************/
  200. /* All of the functions below can be used without first initializing CBFS. */
  201. /***************************************************************************/
  202. /**
  203. * file_cbfs_find_uncached() - Find a file in CBFS given the end of the ROM
  204. *
  205. * Note that @node should be declared by the caller. This design is to avoid
  206. * the need for allocation here.
  207. *
  208. * @end_of_rom: Points to the end of the ROM the CBFS should be read from
  209. * @name: The name to search for
  210. * @node: Returns the contents of the node if found (i.e. copied into *node)
  211. * @return 0 on success, -ENOENT if not found, -EFAULT on bad header
  212. */
  213. int file_cbfs_find_uncached(ulong end_of_rom, const char *name,
  214. struct cbfs_cachenode *node);
  215. /**
  216. * file_cbfs_find_uncached_base() - Find a file in CBFS given the base address
  217. *
  218. * Note that @node should be declared by the caller. This design is to avoid
  219. * the need for allocation here.
  220. *
  221. * @base: Points to the base of the CBFS
  222. * @name: The name to search for
  223. * @node: Returns the contents of the node if found (i.e. copied into *node)
  224. * @return 0 on success, -ENOENT if not found, -EFAULT on bad header
  225. */
  226. int file_cbfs_find_uncached_base(ulong base, const char *name,
  227. struct cbfs_cachenode *node);
  228. /**
  229. * file_cbfs_name() - Get the name of a file in CBFS.
  230. *
  231. * @file: The handle to the file.
  232. *
  233. * @return The name of the file, NULL on error.
  234. */
  235. const char *file_cbfs_name(const struct cbfs_cachenode *file);
  236. /**
  237. * file_cbfs_size() - Get the size of a file in CBFS.
  238. *
  239. * @file: The handle to the file.
  240. *
  241. * @return The size of the file, zero on error.
  242. */
  243. u32 file_cbfs_size(const struct cbfs_cachenode *file);
  244. /**
  245. * file_cbfs_type() - Get the type of a file in CBFS.
  246. *
  247. * @file: The handle to the file.
  248. *
  249. * @return The type of the file, zero on error.
  250. */
  251. u32 file_cbfs_type(const struct cbfs_cachenode *file);
  252. /**
  253. * file_cbfs_read() - Read a file from CBFS into RAM
  254. *
  255. * @file: A handle to the file to read.
  256. * @buffer: Where to read it into memory.
  257. * @maxsize: Maximum number of bytes to read
  258. *
  259. * @return If positive or zero, the number of characters read. If negative, an
  260. * error occurred.
  261. */
  262. long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
  263. unsigned long maxsize);
  264. #endif /* __CBFS_H */