2 * This file is part of the libpayload project.
4 * Copyright (C) 2011 secunet Security Networks AG
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 /* The CBFS core requires a couple of #defines or functions to adapt it to the target environment:
32 * CBFS_CORE_WITH_LZMA (must be #define)
33 * if defined, ulzma() must exist for decompression of data streams
35 * phys_to_virt(x), virt_to_phys(x)
36 * translate physical addresses to virtual and vice versa
37 * can be idempotent if no mapping is necessary.
40 * print an error message x (in printf format)
43 * print a debug message x (in printf format)
46 * returns the start address of the ROM image, or 0xffffffff if ROM is
47 * top-aligned. This is a physical address.
50 * returns the highest address of the ROM image + 1, for use if
51 * romstart() == 0xffffffff. This is a physical address.
54 #include <cbfs_core.h>
57 /* returns pointer to master header or 0xffffffff if not found */
58 static struct cbfs_header *get_cbfs_header(void)
60 struct cbfs_header *header;
63 if (romstart() == 0xffffffff) {
64 header = (struct cbfs_header*)phys_to_virt(*(uint32_t*)phys_to_virt(romend() + CBFS_HEADPTR_ADDR));
66 // FIXME: where's the master header on ARM (our current bottom-aligned platform)?
69 if (CBFS_HEADER_MAGIC != ntohl(header->magic)) {
70 ERROR("Could not find valid CBFS master header at %p: %x vs %x.\n", header, CBFS_HEADER_MAGIC, ntohl(header->magic));
71 if (header->magic == 0xffffffff) {
72 ERROR("Maybe ROM is not mapped properly?\n");
74 return (void*)0xffffffff;
79 // by must be power-of-two
80 #define CBFS_ALIGN(val, by) (typeof(val))((uint32_t)(val + by - 1) & (uint32_t)~(by - 1))
81 #define CBFS_ALIGN_UP(val, by) CBFS_ALIGN(val + 1, by)
83 /* public API starts here*/
84 struct cbfs_file *cbfs_find(const char *name)
86 struct cbfs_header *header = get_cbfs_header();
87 if (header == (void*)0xffffffff) return NULL;
89 LOG("Searching for %s\n", name);
91 void *data, *dataend, *origdata;
92 /* find first entry */
93 if (romstart() == 0xffffffff) {
94 data = (void*)phys_to_virt(romend()) - ntohl(header->romsize) + ntohl(header->offset);
95 dataend = (void*)phys_to_virt(romend());
97 data = (void*)phys_to_virt(romstart()) + ntohl(header->offset);
98 dataend = (void*)phys_to_virt(romstart()) + ntohl(header->romsize);
101 int align = ntohl(header->align);
104 while ((data < (dataend - 1)) && (data >= origdata)) {
105 struct cbfs_file *file = data;
106 if (memcmp(CBFS_FILE_MAGIC, file->magic, strlen(CBFS_FILE_MAGIC)) != 0) {
107 // no file header found. corruption?
108 // proceed in aligned steps to resynchronize
109 LOG("No file header found at %p, searching for header\n", data);
110 data = phys_to_virt(CBFS_ALIGN_UP(virt_to_phys(data), align));
113 LOG("Check %s\n", CBFS_NAME(file));
114 if (strcmp(CBFS_NAME(file), name) == 0) {
117 void *olddata = data;
118 data = phys_to_virt(CBFS_ALIGN(virt_to_phys(data) + ntohl(file->len) + ntohl(file->offset), align));
119 if (olddata > data) {
120 LOG("Something is wrong here. File chain moved from %p to %p\n", olddata, data);
127 void *cbfs_get_file(const char *name)
129 struct cbfs_file *file = cbfs_find(name);
132 ERROR("Could not find file '%s'.\n", name);
136 return (void*)CBFS_SUBHEADER(file);
139 void *cbfs_find_file(const char *name, int type)
141 struct cbfs_file *file = cbfs_find(name);
144 ERROR("Could not find file '%s'.\n", name);
148 if (ntohl(file->type) != type) {
149 ERROR("File '%s' is of type %x, but we requested %x.\n", name, ntohl(file->type), type);
153 return (void*)CBFS_SUBHEADER(file);
156 int cbfs_decompress(int algo, void *src, void *dst, int len)
159 case CBFS_COMPRESS_NONE:
160 memcpy(dst, src, len);
162 #ifdef CBFS_CORE_WITH_LZMA
163 case CBFS_COMPRESS_LZMA:
164 if (ulzma(src, dst) != 0) {
170 ERROR("tried to decompress %d bytes with algorithm #%x, but that algorithm id is unsupported.\n", len, algo);