cbfs.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. enum cbfs_result file_cbfs_result;
  10. const char *file_cbfs_error(void)
  11. {
  12. switch (file_cbfs_result) {
  13. case CBFS_SUCCESS:
  14. return "Success";
  15. case CBFS_NOT_INITIALIZED:
  16. return "CBFS not initialized";
  17. case CBFS_BAD_HEADER:
  18. return "Bad CBFS header";
  19. case CBFS_BAD_FILE:
  20. return "Bad CBFS file";
  21. case CBFS_FILE_NOT_FOUND:
  22. return "File not found";
  23. default:
  24. return "Unknown";
  25. }
  26. }
  27. static const u32 good_magic = 0x4f524243;
  28. static const u8 good_file_magic[] = "LARCHIVE";
  29. static int initialized;
  30. static struct cbfs_header cbfs_header;
  31. static struct cbfs_cachenode *file_cache;
  32. /* Do endian conversion on the CBFS header structure. */
  33. static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
  34. {
  35. dest->magic = be32_to_cpu(src->magic);
  36. dest->version = be32_to_cpu(src->version);
  37. dest->rom_size = be32_to_cpu(src->rom_size);
  38. dest->boot_block_size = be32_to_cpu(src->boot_block_size);
  39. dest->align = be32_to_cpu(src->align);
  40. dest->offset = be32_to_cpu(src->offset);
  41. }
  42. /* Do endian conversion on a CBFS file header. */
  43. static void swap_file_header(struct cbfs_fileheader *dest,
  44. const struct cbfs_fileheader *src)
  45. {
  46. memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
  47. dest->len = be32_to_cpu(src->len);
  48. dest->type = be32_to_cpu(src->type);
  49. dest->checksum = be32_to_cpu(src->checksum);
  50. dest->offset = be32_to_cpu(src->offset);
  51. }
  52. /*
  53. * Given a starting position in memory, scan forward, bounded by a size, and
  54. * find the next valid CBFS file. No memory is allocated by this function. The
  55. * caller is responsible for allocating space for the new file structure.
  56. *
  57. * @param start The location in memory to start from.
  58. * @param size The size of the memory region to search.
  59. * @param align The alignment boundaries to check on.
  60. * @param newNode A pointer to the file structure to load.
  61. * @param used A pointer to the count of of bytes scanned through,
  62. * including the file if one is found.
  63. *
  64. * @return 1 if a file is found, 0 if one isn't.
  65. */
  66. static int file_cbfs_next_file(u8 *start, u32 size, u32 align,
  67. struct cbfs_cachenode *newNode, u32 *used)
  68. {
  69. struct cbfs_fileheader header;
  70. *used = 0;
  71. while (size >= align) {
  72. const struct cbfs_fileheader *fileHeader =
  73. (const struct cbfs_fileheader *)start;
  74. u32 name_len;
  75. u32 step;
  76. /* Check if there's a file here. */
  77. if (memcmp(good_file_magic, &(fileHeader->magic),
  78. sizeof(fileHeader->magic))) {
  79. *used += align;
  80. size -= align;
  81. start += align;
  82. continue;
  83. }
  84. swap_file_header(&header, fileHeader);
  85. if (header.offset < sizeof(struct cbfs_fileheader) ||
  86. header.offset > header.len) {
  87. file_cbfs_result = CBFS_BAD_FILE;
  88. return -1;
  89. }
  90. newNode->next = NULL;
  91. newNode->type = header.type;
  92. newNode->data = start + header.offset;
  93. newNode->data_length = header.len;
  94. name_len = header.offset - sizeof(struct cbfs_fileheader);
  95. newNode->name = (char *)fileHeader +
  96. sizeof(struct cbfs_fileheader);
  97. newNode->name_length = name_len;
  98. newNode->checksum = header.checksum;
  99. step = header.len;
  100. if (step % align)
  101. step = step + align - step % align;
  102. *used += step;
  103. return 1;
  104. }
  105. return 0;
  106. }
  107. /* Look through a CBFS instance and copy file metadata into regular memory. */
  108. static void file_cbfs_fill_cache(u8 *start, u32 size, u32 align)
  109. {
  110. struct cbfs_cachenode *cache_node;
  111. struct cbfs_cachenode *newNode;
  112. struct cbfs_cachenode **cache_tail = &file_cache;
  113. /* Clear out old information. */
  114. cache_node = file_cache;
  115. while (cache_node) {
  116. struct cbfs_cachenode *oldNode = cache_node;
  117. cache_node = cache_node->next;
  118. free(oldNode);
  119. }
  120. file_cache = NULL;
  121. while (size >= align) {
  122. int result;
  123. u32 used;
  124. newNode = (struct cbfs_cachenode *)
  125. malloc(sizeof(struct cbfs_cachenode));
  126. result = file_cbfs_next_file(start, size, align,
  127. newNode, &used);
  128. if (result < 0) {
  129. free(newNode);
  130. return;
  131. } else if (result == 0) {
  132. free(newNode);
  133. break;
  134. }
  135. *cache_tail = newNode;
  136. cache_tail = &newNode->next;
  137. size -= used;
  138. start += used;
  139. }
  140. file_cbfs_result = CBFS_SUCCESS;
  141. }
  142. /* Get the CBFS header out of the ROM and do endian conversion. */
  143. static int file_cbfs_load_header(uintptr_t end_of_rom,
  144. struct cbfs_header *header)
  145. {
  146. struct cbfs_header *header_in_rom;
  147. int32_t offset = *(u32 *)(end_of_rom - 3);
  148. header_in_rom = (struct cbfs_header *)(end_of_rom + offset + 1);
  149. swap_header(header, header_in_rom);
  150. if (header->magic != good_magic || header->offset >
  151. header->rom_size - header->boot_block_size) {
  152. file_cbfs_result = CBFS_BAD_HEADER;
  153. return 1;
  154. }
  155. return 0;
  156. }
  157. void file_cbfs_init(uintptr_t end_of_rom)
  158. {
  159. u8 *start_of_rom;
  160. initialized = 0;
  161. if (file_cbfs_load_header(end_of_rom, &cbfs_header))
  162. return;
  163. start_of_rom = (u8 *)(end_of_rom + 1 - cbfs_header.rom_size);
  164. file_cbfs_fill_cache(start_of_rom + cbfs_header.offset,
  165. cbfs_header.rom_size, cbfs_header.align);
  166. if (file_cbfs_result == CBFS_SUCCESS)
  167. initialized = 1;
  168. }
  169. const struct cbfs_header *file_cbfs_get_header(void)
  170. {
  171. if (initialized) {
  172. file_cbfs_result = CBFS_SUCCESS;
  173. return &cbfs_header;
  174. } else {
  175. file_cbfs_result = CBFS_NOT_INITIALIZED;
  176. return NULL;
  177. }
  178. }
  179. const struct cbfs_cachenode *file_cbfs_get_first(void)
  180. {
  181. if (!initialized) {
  182. file_cbfs_result = CBFS_NOT_INITIALIZED;
  183. return NULL;
  184. } else {
  185. file_cbfs_result = CBFS_SUCCESS;
  186. return file_cache;
  187. }
  188. }
  189. void file_cbfs_get_next(const struct cbfs_cachenode **file)
  190. {
  191. if (!initialized) {
  192. file_cbfs_result = CBFS_NOT_INITIALIZED;
  193. file = NULL;
  194. return;
  195. }
  196. if (*file)
  197. *file = (*file)->next;
  198. file_cbfs_result = CBFS_SUCCESS;
  199. }
  200. const struct cbfs_cachenode *file_cbfs_find(const char *name)
  201. {
  202. struct cbfs_cachenode *cache_node = file_cache;
  203. if (!initialized) {
  204. file_cbfs_result = CBFS_NOT_INITIALIZED;
  205. return NULL;
  206. }
  207. while (cache_node) {
  208. if (!strcmp(name, cache_node->name))
  209. break;
  210. cache_node = cache_node->next;
  211. }
  212. if (!cache_node)
  213. file_cbfs_result = CBFS_FILE_NOT_FOUND;
  214. else
  215. file_cbfs_result = CBFS_SUCCESS;
  216. return cache_node;
  217. }
  218. const struct cbfs_cachenode *file_cbfs_find_uncached(uintptr_t end_of_rom,
  219. const char *name)
  220. {
  221. u8 *start;
  222. u32 size;
  223. u32 align;
  224. static struct cbfs_cachenode node;
  225. if (file_cbfs_load_header(end_of_rom, &cbfs_header))
  226. return NULL;
  227. start = (u8 *)(end_of_rom + 1 - cbfs_header.rom_size);
  228. size = cbfs_header.rom_size;
  229. align = cbfs_header.align;
  230. while (size >= align) {
  231. int result;
  232. u32 used;
  233. result = file_cbfs_next_file(start, size, align, &node, &used);
  234. if (result < 0)
  235. return NULL;
  236. else if (result == 0)
  237. break;
  238. if (!strcmp(name, node.name))
  239. return &node;
  240. size -= used;
  241. start += used;
  242. }
  243. file_cbfs_result = CBFS_FILE_NOT_FOUND;
  244. return NULL;
  245. }
  246. const char *file_cbfs_name(const struct cbfs_cachenode *file)
  247. {
  248. file_cbfs_result = CBFS_SUCCESS;
  249. return file->name;
  250. }
  251. u32 file_cbfs_size(const struct cbfs_cachenode *file)
  252. {
  253. file_cbfs_result = CBFS_SUCCESS;
  254. return file->data_length;
  255. }
  256. u32 file_cbfs_type(const struct cbfs_cachenode *file)
  257. {
  258. file_cbfs_result = CBFS_SUCCESS;
  259. return file->type;
  260. }
  261. long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
  262. unsigned long maxsize)
  263. {
  264. u32 size;
  265. size = file->data_length;
  266. if (maxsize && size > maxsize)
  267. size = maxsize;
  268. memcpy(buffer, file->data, size);
  269. file_cbfs_result = CBFS_SUCCESS;
  270. return size;
  271. }