cbfs.c 10 KB

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