cbfs.c 8.0 KB

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