cbfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  4. */
  5. #include <common.h>
  6. #include <cbfs.h>
  7. #include <malloc.h>
  8. #include <asm/byteorder.h>
  9. /* Offset of master header from the start of a coreboot ROM */
  10. #define MASTER_HDR_OFFSET 0x38
  11. static const u32 good_magic = 0x4f524243;
  12. static const u8 good_file_magic[] = "LARCHIVE";
  13. /**
  14. * struct cbfs_priv - Private data for this driver
  15. *
  16. * @initialised: true if this CBFS has been inited
  17. * @start: Start position of CBFS in memory, typically memory-mapped SPI flash
  18. * @header: Header read from the CBFS, byte-swapped so U-Boot can access it
  19. * @file_cache: List of file headers read from CBFS
  20. * @result: Success/error result
  21. */
  22. struct cbfs_priv {
  23. bool initialized;
  24. void *start;
  25. struct cbfs_header header;
  26. struct cbfs_cachenode *file_cache;
  27. enum cbfs_result result;
  28. };
  29. static struct cbfs_priv cbfs_s;
  30. const char *file_cbfs_error(void)
  31. {
  32. switch (cbfs_s.result) {
  33. case CBFS_SUCCESS:
  34. return "Success";
  35. case CBFS_NOT_INITIALIZED:
  36. return "CBFS not initialized";
  37. case CBFS_BAD_HEADER:
  38. return "Bad CBFS header";
  39. case CBFS_BAD_FILE:
  40. return "Bad CBFS file";
  41. case CBFS_FILE_NOT_FOUND:
  42. return "File not found";
  43. default:
  44. return "Unknown";
  45. }
  46. }
  47. enum cbfs_result cbfs_get_result(void)
  48. {
  49. return cbfs_s.result;
  50. }
  51. /* Do endian conversion on the CBFS header structure. */
  52. static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
  53. {
  54. dest->magic = be32_to_cpu(src->magic);
  55. dest->version = be32_to_cpu(src->version);
  56. dest->rom_size = be32_to_cpu(src->rom_size);
  57. dest->boot_block_size = be32_to_cpu(src->boot_block_size);
  58. dest->align = be32_to_cpu(src->align);
  59. dest->offset = be32_to_cpu(src->offset);
  60. }
  61. /* Do endian conversion on a CBFS file header. */
  62. static void swap_file_header(struct cbfs_fileheader *dest,
  63. const struct cbfs_fileheader *src)
  64. {
  65. memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
  66. dest->len = be32_to_cpu(src->len);
  67. dest->type = be32_to_cpu(src->type);
  68. dest->attributes_offset = be32_to_cpu(src->attributes_offset);
  69. dest->offset = be32_to_cpu(src->offset);
  70. }
  71. /*
  72. * Given a starting position in memory, scan forward, bounded by a size, and
  73. * find the next valid CBFS file. No memory is allocated by this function. The
  74. * caller is responsible for allocating space for the new file structure.
  75. *
  76. * @param start The location in memory to start from.
  77. * @param size The size of the memory region to search.
  78. * @param align The alignment boundaries to check on.
  79. * @param new_node A pointer to the file structure to load.
  80. * @param used A pointer to the count of of bytes scanned through,
  81. * including the file if one is found.
  82. *
  83. * @return 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header
  84. * is found.
  85. */
  86. static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size,
  87. int align, struct cbfs_cachenode *new_node,
  88. int *used)
  89. {
  90. struct cbfs_fileheader header;
  91. *used = 0;
  92. while (size >= align) {
  93. const struct cbfs_fileheader *file_header = start;
  94. u32 name_len;
  95. u32 step;
  96. /* Check if there's a file here. */
  97. if (memcmp(good_file_magic, &file_header->magic,
  98. sizeof(file_header->magic))) {
  99. *used += align;
  100. size -= align;
  101. start += align;
  102. continue;
  103. }
  104. swap_file_header(&header, file_header);
  105. if (header.offset < sizeof(struct cbfs_fileheader)) {
  106. priv->result = CBFS_BAD_FILE;
  107. return -EBADF;
  108. }
  109. new_node->next = NULL;
  110. new_node->type = header.type;
  111. new_node->data = start + header.offset;
  112. new_node->data_length = header.len;
  113. name_len = header.offset - sizeof(struct cbfs_fileheader);
  114. new_node->name = (char *)file_header +
  115. sizeof(struct cbfs_fileheader);
  116. new_node->name_length = name_len;
  117. new_node->attributes_offset = header.attributes_offset;
  118. step = header.len;
  119. if (step % align)
  120. step = step + align - step % align;
  121. *used += step;
  122. return 0;
  123. }
  124. return -ENOENT;
  125. }
  126. /* Look through a CBFS instance and copy file metadata into regular memory. */
  127. static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
  128. {
  129. struct cbfs_cachenode *cache_node;
  130. struct cbfs_cachenode *new_node;
  131. struct cbfs_cachenode **cache_tail = &priv->file_cache;
  132. void *start;
  133. /* Clear out old information. */
  134. cache_node = priv->file_cache;
  135. while (cache_node) {
  136. struct cbfs_cachenode *old_node = cache_node;
  137. cache_node = cache_node->next;
  138. free(old_node);
  139. }
  140. priv->file_cache = NULL;
  141. start = priv->start;
  142. while (size >= align) {
  143. int used;
  144. int ret;
  145. new_node = (struct cbfs_cachenode *)
  146. malloc(sizeof(struct cbfs_cachenode));
  147. if (!new_node)
  148. return -ENOMEM;
  149. ret = file_cbfs_next_file(priv, start, size, align, new_node,
  150. &used);
  151. if (ret < 0) {
  152. free(new_node);
  153. if (ret == -ENOENT)
  154. break;
  155. return ret;
  156. }
  157. *cache_tail = new_node;
  158. cache_tail = &new_node->next;
  159. size -= used;
  160. start += used;
  161. }
  162. priv->result = CBFS_SUCCESS;
  163. return 0;
  164. }
  165. /**
  166. * load_header() - Load the CBFS header
  167. *
  168. * Get the CBFS header out of the ROM and do endian conversion.
  169. *
  170. * @priv: Private data, which is inited by this function
  171. * @addr: Address of CBFS header in memory-mapped SPI flash
  172. * @return 0 if OK, -ENXIO if the header is bad
  173. */
  174. static int load_header(struct cbfs_priv *priv, ulong addr)
  175. {
  176. struct cbfs_header *header = &priv->header;
  177. struct cbfs_header *header_in_rom;
  178. memset(priv, '\0', sizeof(*priv));
  179. header_in_rom = (struct cbfs_header *)addr;
  180. swap_header(header, header_in_rom);
  181. if (header->magic != good_magic || header->offset >
  182. header->rom_size - header->boot_block_size) {
  183. priv->result = CBFS_BAD_HEADER;
  184. return -ENXIO;
  185. }
  186. return 0;
  187. }
  188. /**
  189. * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end
  190. *
  191. * @priv: Private data, which is inited by this function
  192. * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff)
  193. * @return 0 if OK, -ENXIO if the header is bad
  194. */
  195. static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
  196. {
  197. int offset = *(u32 *)(end_of_rom - 3);
  198. int ret;
  199. ret = load_header(priv, end_of_rom + offset + 1);
  200. if (ret)
  201. return ret;
  202. priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size);
  203. return 0;
  204. }
  205. /**
  206. * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base
  207. *
  208. * @priv: Private data, which is inited by this function
  209. * @base: Address of the first byte of the ROM (e.g. 0xff000000)
  210. * @return 0 if OK, -ENXIO if the header is bad
  211. */
  212. static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
  213. {
  214. int ret;
  215. ret = load_header(priv, base + MASTER_HDR_OFFSET);
  216. if (ret)
  217. return ret;
  218. priv->start = (void *)base;
  219. return 0;
  220. }
  221. static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
  222. {
  223. int ret;
  224. ret = file_cbfs_load_header(priv, end_of_rom);
  225. if (ret)
  226. return ret;
  227. ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
  228. priv->header.align);
  229. if (ret)
  230. return ret;
  231. priv->initialized = true;
  232. return 0;
  233. }
  234. int file_cbfs_init(ulong end_of_rom)
  235. {
  236. return cbfs_init(&cbfs_s, end_of_rom);
  237. }
  238. int cbfs_init_mem(ulong base, ulong size, struct cbfs_priv **privp)
  239. {
  240. struct cbfs_priv priv_s, *priv = &priv_s;
  241. int ret;
  242. /*
  243. * Use a local variable to start with until we know that the CBFS is
  244. * valid.
  245. */
  246. ret = cbfs_load_header_ptr(priv, base);
  247. if (ret)
  248. return ret;
  249. file_cbfs_fill_cache(priv, priv->header.rom_size, priv->header.align);
  250. if (priv->result != CBFS_SUCCESS)
  251. return -EINVAL;
  252. priv->initialized = true;
  253. priv = malloc(sizeof(priv_s));
  254. if (!priv)
  255. return -ENOMEM;
  256. memcpy(priv, &priv_s, sizeof(priv_s));
  257. *privp = priv;
  258. return 0;
  259. }
  260. const struct cbfs_header *file_cbfs_get_header(void)
  261. {
  262. struct cbfs_priv *priv = &cbfs_s;
  263. if (priv->initialized) {
  264. priv->result = CBFS_SUCCESS;
  265. return &priv->header;
  266. } else {
  267. priv->result = CBFS_NOT_INITIALIZED;
  268. return NULL;
  269. }
  270. }
  271. const struct cbfs_cachenode *file_cbfs_get_first(void)
  272. {
  273. struct cbfs_priv *priv = &cbfs_s;
  274. if (!priv->initialized) {
  275. priv->result = CBFS_NOT_INITIALIZED;
  276. return NULL;
  277. } else {
  278. priv->result = CBFS_SUCCESS;
  279. return priv->file_cache;
  280. }
  281. }
  282. void file_cbfs_get_next(const struct cbfs_cachenode **file)
  283. {
  284. struct cbfs_priv *priv = &cbfs_s;
  285. if (!priv->initialized) {
  286. priv->result = CBFS_NOT_INITIALIZED;
  287. *file = NULL;
  288. return;
  289. }
  290. if (*file)
  291. *file = (*file)->next;
  292. priv->result = CBFS_SUCCESS;
  293. }
  294. const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
  295. const char *name)
  296. {
  297. struct cbfs_cachenode *cache_node = priv->file_cache;
  298. if (!priv->initialized) {
  299. priv->result = CBFS_NOT_INITIALIZED;
  300. return NULL;
  301. }
  302. while (cache_node) {
  303. if (!strcmp(name, cache_node->name))
  304. break;
  305. cache_node = cache_node->next;
  306. }
  307. if (!cache_node)
  308. priv->result = CBFS_FILE_NOT_FOUND;
  309. else
  310. priv->result = CBFS_SUCCESS;
  311. return cache_node;
  312. }
  313. const struct cbfs_cachenode *file_cbfs_find(const char *name)
  314. {
  315. return cbfs_find_file(&cbfs_s, name);
  316. }
  317. const struct cbfs_cachenode *file_cbfs_find_uncached(ulong end_of_rom,
  318. const char *name)
  319. {
  320. struct cbfs_priv *priv = &cbfs_s;
  321. void *start;
  322. u32 size;
  323. u32 align;
  324. static struct cbfs_cachenode node;
  325. if (file_cbfs_load_header(priv, end_of_rom))
  326. return NULL;
  327. start = priv->start;
  328. size = priv->header.rom_size;
  329. align = priv->header.align;
  330. while (size >= align) {
  331. int ret;
  332. int used;
  333. ret = file_cbfs_next_file(priv, start, size, align, &node,
  334. &used);
  335. if (ret == -ENOENT)
  336. break;
  337. else if (ret)
  338. return NULL;
  339. if (!strcmp(name, node.name))
  340. return &node;
  341. size -= used;
  342. start += used;
  343. }
  344. cbfs_s.result = CBFS_FILE_NOT_FOUND;
  345. return NULL;
  346. }
  347. const char *file_cbfs_name(const struct cbfs_cachenode *file)
  348. {
  349. cbfs_s.result = CBFS_SUCCESS;
  350. return file->name;
  351. }
  352. u32 file_cbfs_size(const struct cbfs_cachenode *file)
  353. {
  354. cbfs_s.result = CBFS_SUCCESS;
  355. return file->data_length;
  356. }
  357. u32 file_cbfs_type(const struct cbfs_cachenode *file)
  358. {
  359. cbfs_s.result = CBFS_SUCCESS;
  360. return file->type;
  361. }
  362. long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
  363. unsigned long maxsize)
  364. {
  365. u32 size;
  366. size = file->data_length;
  367. if (maxsize && size > maxsize)
  368. size = maxsize;
  369. memcpy(buffer, file->data, size);
  370. cbfs_s.result = CBFS_SUCCESS;
  371. return size;
  372. }