blob: 9bae188cca23a5aae4424506da7efc27ddf712b5 [file] [log] [blame]
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +02001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2011 secunet Security Networks AG
Hung-Te Lind01d0362013-01-25 12:42:40 +08005 * Copyright (C) 2013 Google, Inc.
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +02006 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
Hung-Te Lind01d0362013-01-25 12:42:40 +080031/* The CBFS core requires a couple of #defines or functions to adapt it to the
32 * target environment:
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020033 *
34 * CBFS_CORE_WITH_LZMA (must be #define)
35 * if defined, ulzma() must exist for decompression of data streams
36 *
Hung-Te Lind01d0362013-01-25 12:42:40 +080037 * CBFS_HEADER_ROM_ADDRESS
38 * ROM address (offset) of CBFS header. Underlying CBFS media may interpret
39 * it in other way so we call this "address".
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020040 *
41 * ERROR(x...)
42 * print an error message x (in printf format)
43 *
44 * LOG(x...)
Hung-Te Lind01d0362013-01-25 12:42:40 +080045 * print a message x (in printf format)
46 *
47 * DEBUG(x...)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020048 * print a debug message x (in printf format)
49 *
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020050 */
51
Hung-Te Lind01d0362013-01-25 12:42:40 +080052#include <cbfs.h>
53#include <string.h>
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020054
Hung-Te Lind01d0362013-01-25 12:42:40 +080055/* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS
56 * on failure */
57const struct cbfs_header *cbfs_get_header(struct cbfs_media *media)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020058{
Hung-Te Lind01d0362013-01-25 12:42:40 +080059 const struct cbfs_header *header;
60 struct cbfs_media default_media;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020061
Hung-Te Lind01d0362013-01-25 12:42:40 +080062 if (media == CBFS_DEFAULT_MEDIA) {
63 media = &default_media;
64 if (init_default_cbfs_media(media) != 0) {
Patrick Georgi46cb96bb2013-02-19 17:59:21 +010065 ERROR("Failed to initialize default media.\n");
66 return CBFS_HEADER_INVALID_ADDRESS;
Hung-Te Lind01d0362013-01-25 12:42:40 +080067 }
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020068 }
Hung-Te Lind01d0362013-01-25 12:42:40 +080069
70 media->open(media);
71 DEBUG("CBFS_HEADER_ROM_ADDRESS: 0x%x/0x%x\n", CBFS_HEADER_ROM_ADDRESS,
72 CONFIG_ROM_SIZE);
73 header = media->map(media, CBFS_HEADER_ROM_ADDRESS, sizeof(*header));
74 media->close(media);
75
76 if (header == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
77 ERROR("Failed to load CBFS header from 0x%x\n",
78 CBFS_HEADER_ROM_ADDRESS);
79 return CBFS_HEADER_INVALID_ADDRESS;
80 }
81
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020082 if (CBFS_HEADER_MAGIC != ntohl(header->magic)) {
Hung-Te Lind01d0362013-01-25 12:42:40 +080083 ERROR("Could not find valid CBFS master header at %x: "
84 "%x vs %x.\n", CBFS_HEADER_ROM_ADDRESS, CBFS_HEADER_MAGIC,
85 ntohl(header->magic));
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020086 if (header->magic == 0xffffffff) {
87 ERROR("Maybe ROM is not mapped properly?\n");
88 }
Hung-Te Lind01d0362013-01-25 12:42:40 +080089 return CBFS_HEADER_INVALID_ADDRESS;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020090 }
91 return header;
92}
93
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020094/* public API starts here*/
Hung-Te Lind01d0362013-01-25 12:42:40 +080095struct cbfs_file *cbfs_get_file(struct cbfs_media *media, const char *name)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020096{
Hung-Te Lind01d0362013-01-25 12:42:40 +080097 const char *file_name;
98 uint32_t offset, align, romsize, name_len;
99 const struct cbfs_header *header;
100 struct cbfs_file file, *file_ptr;
101 struct cbfs_media default_media;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200102
Hung-Te Lind01d0362013-01-25 12:42:40 +0800103 if (media == CBFS_DEFAULT_MEDIA) {
104 media = &default_media;
105 if (init_default_cbfs_media(media) != 0) {
Patrick Georgi46cb96bb2013-02-19 17:59:21 +0100106 ERROR("Failed to initialize default media.\n");
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200107 return NULL;
108 }
109 }
Hung-Te Lind01d0362013-01-25 12:42:40 +0800110
111 if (CBFS_HEADER_INVALID_ADDRESS == (header = cbfs_get_header(media)))
112 return NULL;
113
114 // Logical offset (for source media) of first file.
115 offset = ntohl(header->offset);
116 align = ntohl(header->align);
117 romsize = ntohl(header->romsize);
118
119 // TODO header->romsize seems broken now on ARM. Remove this when it's
120 // fixed.
121#if defined(CONFIG_ARCH_ARMV7) && CONFIG_ARCH_ARMV7
122 romsize = CONFIG_ROM_SIZE;
123#endif
124 DEBUG("offset: 0x%x, align: %d, romsize: %d\n", offset, align, romsize);
125
126 LOG("Looking for '%s' starting from 0x%x.\n", name, offset);
127 media->open(media);
128 while (offset < romsize &&
129 media->read(media, &file, offset, sizeof(file)) == sizeof(file)) {
130 if (memcmp(CBFS_FILE_MAGIC, file.magic,
131 sizeof(file.magic)) != 0) {
132 uint32_t new_align = align;
133 if (offset % align)
134 new_align += align - (offset % align);
135 ERROR("ERROR: No file header found at 0x%xx - "
136 "try next aligned address: 0x%x.\n", offset,
137 offset + new_align);
138 offset += new_align;
139 continue;
140 }
141 name_len = ntohl(file.offset) - sizeof(file);
142 DEBUG(" - load entry 0x%x file name (%d bytes)...\n", offset,
143 name_len);
144
145 // load file name (arbitrary length).
146 file_name = (const char*)media->map(
147 media, offset + sizeof(file), name_len);
148 if (file_name == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
149 ERROR("ERROR: Failed to get filename: 0x%x.\n", offset);
150 } else if (strcmp(file_name, name) == 0) {
151 int file_offset = ntohl(file.offset),
152 file_len = ntohl(file.len);
153 LOG("Found file (offset=0x%x, len=%d).\n",
154 offset + file_offset, file_len);
155 media->unmap(media, file_name);
156 file_ptr = media->map(media, offset,
157 file_offset + file_len);
158 media->close(media);
159 return file_ptr;
160 } else {
161 LOG(" (unmatched file @0x%x: %s)\n", offset, file_name);
162 media->unmap(media, file_name);
163 }
164
165 // Move to next file.
166 offset += ntohl(file.len) + ntohl(file.offset);
167 if (offset % align)
168 offset += align - (offset % align);
169 }
170 media->close(media);
171 ERROR("ERROR: Not found.\n");
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200172 return NULL;
173}
174
Hung-Te Lind01d0362013-01-25 12:42:40 +0800175void *cbfs_get_file_content(struct cbfs_media *media, const char *name, int type)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200176{
Hung-Te Lind01d0362013-01-25 12:42:40 +0800177 struct cbfs_file *file = cbfs_get_file(media, name);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200178
179 if (file == NULL) {
180 ERROR("Could not find file '%s'.\n", name);
181 return NULL;
182 }
183
184 if (ntohl(file->type) != type) {
Hung-Te Lind01d0362013-01-25 12:42:40 +0800185 ERROR("File '%s' is of type %x, but we requested %x.\n", name,
186 ntohl(file->type), type);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200187 return NULL;
188 }
189
190 return (void*)CBFS_SUBHEADER(file);
191}
192
193int cbfs_decompress(int algo, void *src, void *dst, int len)
194{
195 switch (algo) {
196 case CBFS_COMPRESS_NONE:
197 memcpy(dst, src, len);
198 return 0;
199#ifdef CBFS_CORE_WITH_LZMA
200 case CBFS_COMPRESS_LZMA:
201 if (ulzma(src, dst) != 0) {
202 return 0;
203 }
204 return -1;
205#endif
206 default:
Hung-Te Lind01d0362013-01-25 12:42:40 +0800207 ERROR("tried to decompress %d bytes with algorithm #%x,"
208 "but that algorithm id is unsupported.\n", len,
209 algo);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200210 return -1;
211 }
212}
213