cbfs.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. enum cbfs_result {
  10. CBFS_SUCCESS = 0,
  11. CBFS_NOT_INITIALIZED,
  12. CBFS_BAD_HEADER,
  13. CBFS_BAD_FILE,
  14. CBFS_FILE_NOT_FOUND
  15. };
  16. enum cbfs_filetype {
  17. CBFS_TYPE_BOOTBLOCK = 0x01,
  18. CBFS_TYPE_CBFSHEADER = 0x02,
  19. CBFS_TYPE_STAGE = 0x10,
  20. CBFS_TYPE_PAYLOAD = 0x20,
  21. CBFS_TYPE_FIT = 0x21,
  22. CBFS_TYPE_OPTIONROM = 0x30,
  23. CBFS_TYPE_BOOTSPLASH = 0x40,
  24. CBFS_TYPE_RAW = 0x50,
  25. CBFS_TYPE_VSA = 0x51,
  26. CBFS_TYPE_MBI = 0x52,
  27. CBFS_TYPE_MICROCODE = 0x53,
  28. CBFS_TYPE_FSP = 0x60,
  29. CBFS_TYPE_MRC = 0x61,
  30. CBFS_TYPE_MMA = 0x62,
  31. CBFS_TYPE_EFI = 0x63,
  32. CBFS_TYPE_STRUCT = 0x70,
  33. CBFS_TYPE_CMOS_DEFAULT = 0xaa,
  34. CBFS_TYPE_SPD = 0xab,
  35. CBFS_TYPE_MRC_CACHE = 0xac,
  36. CBFS_TYPE_CMOS_LAYOUT = 0x01aa
  37. };
  38. enum {
  39. CBFS_HEADER_MAGIC = 0x4f524243,
  40. };
  41. /**
  42. * struct cbfs_header - header at the start of a CBFS region
  43. *
  44. * All fields use big-endian format.
  45. *
  46. * @magic: Magic number (CBFS_HEADER_MAGIC)
  47. */
  48. struct cbfs_header {
  49. u32 magic;
  50. u32 version;
  51. u32 rom_size;
  52. u32 boot_block_size;
  53. u32 align;
  54. u32 offset;
  55. u32 pad[2];
  56. } __packed;
  57. struct cbfs_fileheader {
  58. u8 magic[8];
  59. u32 len;
  60. u32 type;
  61. /* offset to struct cbfs_file_attribute or 0 */
  62. u32 attributes_offset;
  63. u32 offset;
  64. } __packed;
  65. struct cbfs_cachenode {
  66. struct cbfs_cachenode *next;
  67. void *data;
  68. char *name;
  69. u32 type;
  70. u32 data_length;
  71. u32 name_length;
  72. u32 attributes_offset;
  73. };
  74. /**
  75. * file_cbfs_error() - Return a string describing the most recent error
  76. * condition.
  77. *
  78. * @return A pointer to the constant string.
  79. */
  80. const char *file_cbfs_error(void);
  81. /**
  82. * cbfs_get_result() - Get the result of the last CBFS operation
  83. *
  84. *@return last result
  85. */
  86. enum cbfs_result cbfs_get_result(void);
  87. /**
  88. * file_cbfs_init() - Initialize the CBFS driver and load metadata into RAM.
  89. *
  90. * @end_of_rom: Points to the end of the ROM the CBFS should be read from
  91. * @return 0 if OK, -ve on error
  92. */
  93. int file_cbfs_init(ulong end_of_rom);
  94. /**
  95. * file_cbfs_get_header() - Get the header structure for the current CBFS.
  96. *
  97. * @return A pointer to the constant structure, or NULL if there is none.
  98. */
  99. const struct cbfs_header *file_cbfs_get_header(void);
  100. /**
  101. * file_cbfs_get_first() - Get a handle for the first file in CBFS.
  102. *
  103. * @return A handle for the first file in CBFS, NULL on error.
  104. */
  105. const struct cbfs_cachenode *file_cbfs_get_first(void);
  106. /**
  107. * file_cbfs_get_next() - Get a handle to the file after this one in CBFS.
  108. *
  109. * @file: A pointer to the handle to advance.
  110. */
  111. void file_cbfs_get_next(const struct cbfs_cachenode **file);
  112. /**
  113. * file_cbfs_find() - Find a file with a particular name in CBFS.
  114. *
  115. * @name: The name to search for.
  116. *
  117. * @return A handle to the file, or NULL on error.
  118. */
  119. const struct cbfs_cachenode *file_cbfs_find(const char *name);
  120. struct cbfs_priv;
  121. /**
  122. * cbfs_find_file() - Find a file in a given CBFS
  123. *
  124. * @cbfs: CBFS to look in (use cbfs_init_mem() to set it up)
  125. * @name: Filename to look for
  126. * @return pointer to CBFS node if found, else NULL
  127. */
  128. const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *cbfs,
  129. const char *name);
  130. /**
  131. * cbfs_init_mem() - Set up a new CBFS
  132. *
  133. * @base: Base address of CBFS
  134. * @cbfsp: Returns a pointer to CBFS on success
  135. * @return 0 if OK, -ve on error
  136. */
  137. int cbfs_init_mem(ulong base, struct cbfs_priv **privp);
  138. /***************************************************************************/
  139. /* All of the functions below can be used without first initializing CBFS. */
  140. /***************************************************************************/
  141. /**
  142. * file_cbfs_find_uncached() - Find a file in CBFS given the end of the ROM
  143. *
  144. * Note that @node should be declared by the caller. This design is to avoid
  145. * the need for allocation here.
  146. *
  147. * @end_of_rom: Points to the end of the ROM the CBFS should be read from
  148. * @name: The name to search for
  149. * @node: Returns the contents of the node if found (i.e. copied into *node)
  150. * @return 0 on success, -ENOENT if not found, -EFAULT on bad header
  151. */
  152. int file_cbfs_find_uncached(ulong end_of_rom, const char *name,
  153. struct cbfs_cachenode *node);
  154. /**
  155. * file_cbfs_find_uncached_base() - Find a file in CBFS given the base address
  156. *
  157. * Note that @node should be declared by the caller. This design is to avoid
  158. * the need for allocation here.
  159. *
  160. * @base: Points to the base of the CBFS
  161. * @name: The name to search for
  162. * @node: Returns the contents of the node if found (i.e. copied into *node)
  163. * @return 0 on success, -ENOENT if not found, -EFAULT on bad header
  164. */
  165. int file_cbfs_find_uncached_base(ulong base, const char *name,
  166. struct cbfs_cachenode *node);
  167. /**
  168. * file_cbfs_name() - Get the name of a file in CBFS.
  169. *
  170. * @file: The handle to the file.
  171. *
  172. * @return The name of the file, NULL on error.
  173. */
  174. const char *file_cbfs_name(const struct cbfs_cachenode *file);
  175. /**
  176. * file_cbfs_size() - Get the size of a file in CBFS.
  177. *
  178. * @file: The handle to the file.
  179. *
  180. * @return The size of the file, zero on error.
  181. */
  182. u32 file_cbfs_size(const struct cbfs_cachenode *file);
  183. /**
  184. * file_cbfs_type() - Get the type of a file in CBFS.
  185. *
  186. * @file: The handle to the file.
  187. *
  188. * @return The type of the file, zero on error.
  189. */
  190. u32 file_cbfs_type(const struct cbfs_cachenode *file);
  191. /**
  192. * file_cbfs_read() - Read a file from CBFS into RAM
  193. *
  194. * @file: A handle to the file to read.
  195. * @buffer: Where to read it into memory.
  196. * @maxsize: Maximum number of bytes to read
  197. *
  198. * @return If positive or zero, the number of characters read. If negative, an
  199. * error occurred.
  200. */
  201. long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
  202. unsigned long maxsize);
  203. #endif /* __CBFS_H */