cbfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. * fill_node() - Fill a node struct with information from the CBFS
  74. *
  75. * @node: Node to fill
  76. * @start: Pointer to the start of the CBFS file in memory
  77. * @header: Pointer to the header information (in our enddianess)
  78. * @return 0 if OK, -EBADF if the header is too small
  79. */
  80. static int fill_node(struct cbfs_cachenode *node, void *start,
  81. struct cbfs_fileheader *header)
  82. {
  83. uint name_len;
  84. uint offset;
  85. /* Check the header is large enough */
  86. if (header->offset < sizeof(struct cbfs_fileheader))
  87. return -EBADF;
  88. node->next = NULL;
  89. node->type = header->type;
  90. node->data = start + header->offset;
  91. node->data_length = header->len;
  92. name_len = header->offset - sizeof(struct cbfs_fileheader);
  93. node->name = start + sizeof(struct cbfs_fileheader);
  94. node->name_length = name_len;
  95. node->attr_offset = header->attributes_offset;
  96. node->comp_algo = CBFS_COMPRESS_NONE;
  97. node->decomp_size = 0;
  98. for (offset = node->attr_offset; offset < header->offset;) {
  99. const struct cbfs_file_attribute *attr;
  100. uint tag, len;
  101. attr = start + offset;
  102. tag = be32_to_cpu(attr->tag);
  103. len = be32_to_cpu(attr->len);
  104. if (tag == CBFS_FILE_ATTR_TAG_COMPRESSION) {
  105. struct cbfs_file_attr_compression *comp;
  106. comp = start + offset;
  107. node->comp_algo = be32_to_cpu(comp->compression);
  108. node->decomp_size =
  109. be32_to_cpu(comp->decompressed_size);
  110. }
  111. offset += len;
  112. }
  113. return 0;
  114. }
  115. /*
  116. * Given a starting position in memory, scan forward, bounded by a size, and
  117. * find the next valid CBFS file. No memory is allocated by this function. The
  118. * caller is responsible for allocating space for the new file structure.
  119. *
  120. * @param start The location in memory to start from.
  121. * @param size The size of the memory region to search.
  122. * @param align The alignment boundaries to check on.
  123. * @param node A pointer to the file structure to load.
  124. * @param used A pointer to the count of of bytes scanned through,
  125. * including the file if one is found.
  126. *
  127. * @return 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header
  128. * is found.
  129. */
  130. static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size,
  131. int align, struct cbfs_cachenode *node,
  132. int *used)
  133. {
  134. struct cbfs_fileheader header;
  135. *used = 0;
  136. while (size >= align) {
  137. const struct cbfs_fileheader *file_header = start;
  138. int ret;
  139. /* Check if there's a file here. */
  140. if (memcmp(good_file_magic, &file_header->magic,
  141. sizeof(file_header->magic))) {
  142. *used += align;
  143. size -= align;
  144. start += align;
  145. continue;
  146. }
  147. swap_file_header(&header, file_header);
  148. if (header.offset >= size)
  149. return log_msg_ret("range", -E2BIG);
  150. ret = fill_node(node, start, &header);
  151. if (ret) {
  152. priv->result = CBFS_BAD_FILE;
  153. return log_msg_ret("fill", ret);
  154. }
  155. *used += ALIGN(header.len, align);
  156. return 0;
  157. }
  158. return -ENOENT;
  159. }
  160. /* Look through a CBFS instance and copy file metadata into regular memory. */
  161. static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
  162. {
  163. struct cbfs_cachenode *cache_node;
  164. struct cbfs_cachenode *node;
  165. struct cbfs_cachenode **cache_tail = &priv->file_cache;
  166. void *start;
  167. /* Clear out old information. */
  168. cache_node = priv->file_cache;
  169. while (cache_node) {
  170. struct cbfs_cachenode *old_node = cache_node;
  171. cache_node = cache_node->next;
  172. free(old_node);
  173. }
  174. priv->file_cache = NULL;
  175. start = priv->start;
  176. while (size >= align) {
  177. int used;
  178. int ret;
  179. node = malloc(sizeof(struct cbfs_cachenode));
  180. if (!node)
  181. return -ENOMEM;
  182. ret = file_cbfs_next_file(priv, start, size, align, node,
  183. &used);
  184. if (ret < 0) {
  185. free(node);
  186. if (ret == -ENOENT)
  187. break;
  188. return ret;
  189. }
  190. *cache_tail = node;
  191. cache_tail = &node->next;
  192. size -= used;
  193. start += used;
  194. }
  195. priv->result = CBFS_SUCCESS;
  196. return 0;
  197. }
  198. /**
  199. * load_header() - Load the CBFS header
  200. *
  201. * Get the CBFS header out of the ROM and do endian conversion.
  202. *
  203. * @priv: Private data, which is inited by this function
  204. * @addr: Address of CBFS header in memory-mapped SPI flash
  205. * @return 0 if OK, -ENXIO if the header is bad
  206. */
  207. static int load_header(struct cbfs_priv *priv, ulong addr)
  208. {
  209. struct cbfs_header *header = &priv->header;
  210. struct cbfs_header *header_in_rom;
  211. memset(priv, '\0', sizeof(*priv));
  212. header_in_rom = (struct cbfs_header *)addr;
  213. swap_header(header, header_in_rom);
  214. if (header->magic != good_magic || header->offset >
  215. header->rom_size - header->boot_block_size) {
  216. priv->result = CBFS_BAD_HEADER;
  217. return -ENXIO;
  218. }
  219. return 0;
  220. }
  221. /**
  222. * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end
  223. *
  224. * @priv: Private data, which is inited by this function
  225. * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff)
  226. * @return 0 if OK, -ENXIO if the header is bad
  227. */
  228. static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
  229. {
  230. int offset = *(u32 *)(end_of_rom - 3);
  231. int ret;
  232. ret = load_header(priv, end_of_rom + offset + 1);
  233. if (ret)
  234. return ret;
  235. priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size);
  236. return 0;
  237. }
  238. /**
  239. * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base
  240. *
  241. * @priv: Private data, which is inited by this function
  242. * @base: Address of the first byte of the ROM (e.g. 0xff000000)
  243. * @return 0 if OK, -ENXIO if the header is bad
  244. */
  245. static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
  246. {
  247. int ret;
  248. ret = load_header(priv, base + MASTER_HDR_OFFSET);
  249. if (ret)
  250. return ret;
  251. priv->start = (void *)base;
  252. return 0;
  253. }
  254. static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
  255. {
  256. int ret;
  257. ret = file_cbfs_load_header(priv, end_of_rom);
  258. if (ret)
  259. return ret;
  260. ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
  261. priv->header.align);
  262. if (ret)
  263. return ret;
  264. priv->initialized = true;
  265. return 0;
  266. }
  267. int file_cbfs_init(ulong end_of_rom)
  268. {
  269. return cbfs_init(&cbfs_s, end_of_rom);
  270. }
  271. int cbfs_init_mem(ulong base, ulong size, bool require_hdr,
  272. struct cbfs_priv **privp)
  273. {
  274. struct cbfs_priv priv_s, *priv = &priv_s;
  275. int ret;
  276. /*
  277. * Use a local variable to start with until we know that the * CBFS is
  278. * valid. Note that size is detected from the header, if present,
  279. * meaning the parameter is ignored.
  280. */
  281. ret = cbfs_load_header_ptr(priv, base);
  282. if (ret) {
  283. if (require_hdr || size == CBFS_SIZE_UNKNOWN)
  284. return ret;
  285. memset(priv, '\0', sizeof(struct cbfs_priv));
  286. priv->header.rom_size = size;
  287. priv->header.align = CBFS_ALIGN_SIZE;
  288. priv->start = (void *)base;
  289. }
  290. ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
  291. priv->header.align);
  292. if (ret)
  293. return log_msg_ret("fill", ret);
  294. priv->initialized = true;
  295. priv = malloc(sizeof(priv_s));
  296. if (!priv)
  297. return -ENOMEM;
  298. memcpy(priv, &priv_s, sizeof(priv_s));
  299. *privp = priv;
  300. return 0;
  301. }
  302. const struct cbfs_header *file_cbfs_get_header(void)
  303. {
  304. struct cbfs_priv *priv = &cbfs_s;
  305. if (priv->initialized) {
  306. priv->result = CBFS_SUCCESS;
  307. return &priv->header;
  308. } else {
  309. priv->result = CBFS_NOT_INITIALIZED;
  310. return NULL;
  311. }
  312. }
  313. const struct cbfs_cachenode *cbfs_get_first(const struct cbfs_priv *priv)
  314. {
  315. return priv->file_cache;
  316. }
  317. void cbfs_get_next(const struct cbfs_cachenode **filep)
  318. {
  319. if (*filep)
  320. *filep = (*filep)->next;
  321. }
  322. const struct cbfs_cachenode *file_cbfs_get_first(void)
  323. {
  324. struct cbfs_priv *priv = &cbfs_s;
  325. if (!priv->initialized) {
  326. priv->result = CBFS_NOT_INITIALIZED;
  327. return NULL;
  328. } else {
  329. priv->result = CBFS_SUCCESS;
  330. return priv->file_cache;
  331. }
  332. }
  333. void file_cbfs_get_next(const struct cbfs_cachenode **file)
  334. {
  335. struct cbfs_priv *priv = &cbfs_s;
  336. if (!priv->initialized) {
  337. priv->result = CBFS_NOT_INITIALIZED;
  338. *file = NULL;
  339. return;
  340. }
  341. if (*file)
  342. *file = (*file)->next;
  343. priv->result = CBFS_SUCCESS;
  344. }
  345. const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
  346. const char *name)
  347. {
  348. struct cbfs_cachenode *cache_node = priv->file_cache;
  349. if (!priv->initialized) {
  350. priv->result = CBFS_NOT_INITIALIZED;
  351. return NULL;
  352. }
  353. while (cache_node) {
  354. if (!strcmp(name, cache_node->name))
  355. break;
  356. cache_node = cache_node->next;
  357. }
  358. if (!cache_node)
  359. priv->result = CBFS_FILE_NOT_FOUND;
  360. else
  361. priv->result = CBFS_SUCCESS;
  362. return cache_node;
  363. }
  364. const struct cbfs_cachenode *file_cbfs_find(const char *name)
  365. {
  366. return cbfs_find_file(&cbfs_s, name);
  367. }
  368. static int find_uncached(struct cbfs_priv *priv, const char *name, void *start,
  369. struct cbfs_cachenode *node)
  370. {
  371. int size = priv->header.rom_size;
  372. int align = priv->header.align;
  373. while (size >= align) {
  374. int used;
  375. int ret;
  376. ret = file_cbfs_next_file(priv, start, size, align, node,
  377. &used);
  378. if (ret == -ENOENT)
  379. break;
  380. else if (ret)
  381. return ret;
  382. if (!strcmp(name, node->name))
  383. return 0;
  384. size -= used;
  385. start += used;
  386. }
  387. priv->result = CBFS_FILE_NOT_FOUND;
  388. return -ENOENT;
  389. }
  390. int file_cbfs_find_uncached(ulong end_of_rom, const char *name,
  391. struct cbfs_cachenode *node)
  392. {
  393. struct cbfs_priv priv;
  394. void *start;
  395. int ret;
  396. ret = file_cbfs_load_header(&priv, end_of_rom);
  397. if (ret)
  398. return ret;
  399. start = priv.start;
  400. return find_uncached(&priv, name, start, node);
  401. }
  402. int file_cbfs_find_uncached_base(ulong base, const char *name,
  403. struct cbfs_cachenode *node)
  404. {
  405. struct cbfs_priv priv;
  406. int ret;
  407. ret = cbfs_load_header_ptr(&priv, base);
  408. if (ret)
  409. return ret;
  410. return find_uncached(&priv, name, (void *)base, node);
  411. }
  412. const char *file_cbfs_name(const struct cbfs_cachenode *file)
  413. {
  414. cbfs_s.result = CBFS_SUCCESS;
  415. return file->name;
  416. }
  417. u32 file_cbfs_size(const struct cbfs_cachenode *file)
  418. {
  419. cbfs_s.result = CBFS_SUCCESS;
  420. return file->data_length;
  421. }
  422. u32 file_cbfs_type(const struct cbfs_cachenode *file)
  423. {
  424. cbfs_s.result = CBFS_SUCCESS;
  425. return file->type;
  426. }
  427. long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
  428. unsigned long maxsize)
  429. {
  430. u32 size;
  431. size = file->data_length;
  432. if (maxsize && size > maxsize)
  433. size = maxsize;
  434. memcpy(buffer, file->data, size);
  435. cbfs_s.result = CBFS_SUCCESS;
  436. return size;
  437. }