cbfs.c 7.4 KB

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