blob: cc4460c66b504885b9207e98987e114a7699a08f [file] [log] [blame]
Hung-Te Lineab2c812013-01-29 01:56:17 +08001/*
2 * CBFS Image Manipulation
3 *
4 * Copyright (C) 2013 The Chromium OS Authors. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
19
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080020#include <inttypes.h>
21#include <libgen.h>
Hung-Te Lineab2c812013-01-29 01:56:17 +080022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include "common.h"
27#include "cbfs_image.h"
28
29/* The file name align is not defined in CBFS spec -- only a preference by
30 * (old) cbfstool. */
31#define CBFS_FILENAME_ALIGN (16)
32
33/* To make CBFS more friendly to ROM, fill -1 (0xFF) instead of zero. */
34#define CBFS_CONTENT_DEFAULT_VALUE (-1)
35
36static uint32_t align_up(uint32_t value, uint32_t align) {
37 if (value % align)
38 value += align - (value % align);
39 return value;
40}
41
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080042/* Type and format */
43
44struct typedesc_t {
45 uint32_t type;
46 const char *name;
47};
48
49static struct typedesc_t types_cbfs_entry[] = {
50 {CBFS_COMPONENT_STAGE, "stage"},
51 {CBFS_COMPONENT_PAYLOAD, "payload"},
52 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
53 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
54 {CBFS_COMPONENT_RAW, "raw"},
55 {CBFS_COMPONENT_VSA, "vsa"},
56 {CBFS_COMPONENT_MBI, "mbi"},
57 {CBFS_COMPONENT_MICROCODE, "microcode"},
58 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos_default"},
59 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos_layout"},
60 {CBFS_COMPONENT_DELETED, "deleted"},
61 {CBFS_COMPONENT_NULL, "null"},
62 {0, NULL},
63};
64
65static struct typedesc_t types_cbfs_compression[] = {
66 {CBFS_COMPRESS_NONE, "none"},
67 {CBFS_COMPRESS_LZMA, "LZMA"},
68 {0, NULL},
69};
70
71uint32_t lookup_type_by_name(struct typedesc_t *desc, const char *name,
72 uint32_t default_value) {
73 int i;
74 for (i = 0; desc[i].name; i++)
75 if (strcmp(desc[i].name, name) == 0)
76 return desc[i].type;
77 return default_value;
78}
79
80const char *lookup_name_by_type(struct typedesc_t *desc, uint32_t type,
81 const char *default_value) {
82 int i;
83 for (i = 0; desc[i].name; i++)
84 if (desc[i].type == type)
85 return desc[i].name;
86 return default_value;
87}
88
89uint32_t get_cbfs_entry_type(const char *name, uint32_t default_value) {
90 return lookup_type_by_name(types_cbfs_entry, name, default_value);
91}
92
93const char *get_cbfs_entry_type_name(uint32_t type) {
94 return lookup_name_by_type(types_cbfs_entry, type, "(unknown)");
95}
96
97uint32_t get_cbfs_compression(const char *name, uint32_t unknown) {
98 return lookup_type_by_name(types_cbfs_compression, name, unknown);
99}
100
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800101/* CBFS image */
102
103static int cbfs_calculate_file_header_size(const char *name) {
104 return (sizeof(struct cbfs_file) +
105 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
106}
107
Hung-Te Lineab2c812013-01-29 01:56:17 +0800108int cbfs_image_from_file(struct cbfs_image *image, const char *filename) {
109 if (buffer_from_file(&image->buffer, filename) != 0)
110 return -1;
111 DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
112 image->buffer.size);
113 image->header = cbfs_find_header(image->buffer.data,
114 image->buffer.size);
115 if (!image->header) {
116 ERROR("%s does not have CBFS master header.\n", filename);
117 cbfs_image_delete(image);
118 return -1;
119 }
120
121 return 0;
122}
123
124int cbfs_image_write_file(struct cbfs_image *image, const char *filename) {
125 assert(image && image->buffer.data);
126 return buffer_write_file(&image->buffer, filename);
127}
128
129int cbfs_image_delete(struct cbfs_image *image) {
130 buffer_delete(&image->buffer);
131 image->header = NULL;
132 return 0;
133}
134
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800135struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name) {
136 struct cbfs_file *entry;
137 for (entry = cbfs_find_first_entry(image);
138 entry && cbfs_is_valid_entry(entry);
139 entry = cbfs_find_next_entry(image, entry)) {
140 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
141 DEBUG("cbfs_get_entry: found %s\n", name);
142 return entry;
143 }
144 }
145 return NULL;
146}
147
148int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
149 const char *filename) {
150 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
151 struct buffer buffer;
152 if (!entry) {
153 ERROR("File not found: %s\n", entry_name);
154 return -1;
155 }
156 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
157 entry_name, cbfs_get_entry_addr(image, entry),
158 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
159
160 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
161 WARN("Only 'raw' files are safe to extract.\n");
162 }
163
164 buffer.data = CBFS_SUBHEADER(entry);
165 buffer.size = ntohl(entry->len);
166 buffer.name = "(cbfs_export_entry)";
167 if (buffer_write_file(&buffer, filename) != 0) {
168 ERROR("Failed to write %s into %s.\n",
169 entry_name, filename);
170 return -1;
171 }
172 INFO("Successfully dumped the file to: %s\n", filename);
173 return 0;
174}
175
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800176int cbfs_remove_entry(struct cbfs_image *image, const char *name) {
177 struct cbfs_file *entry, *next;
178 size_t len;
179 entry = cbfs_get_entry(image, name);
180 if (!entry) {
181 ERROR("CBFS file %s not found.\n", name);
182 return -1;
183 }
184 next = cbfs_find_next_entry(image, entry);
185 assert(next);
186 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
187 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
188 entry->type = htonl(CBFS_COMPONENT_DELETED);
189 len = (cbfs_get_entry_addr(image, next) -
190 cbfs_get_entry_addr(image, entry));
191 entry->offset = htonl(cbfs_calculate_file_header_size(""));
192 entry->len = htonl(len - ntohl(entry->offset));
193 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
194 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
195 ntohl(entry->len));
196 return 0;
197}
198
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800199int cbfs_print_header_info(struct cbfs_image *image) {
200 char *name = strdup(image->buffer.name);
201 assert(image && image->header);
202 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
203 "alignment: %d bytes\n\n",
204 basename(name),
205 image->buffer.size / 1024,
206 ntohl(image->header->bootblocksize),
207 ntohl(image->header->romsize),
208 ntohl(image->header->offset),
209 ntohl(image->header->align));
210 free(name);
211 return 0;
212}
213
214static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp) {
215 fprintf(fp,
216 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
217 "length: %d/%d\n",
218 lookup_name_by_type(types_cbfs_compression,
219 stage->compression, "(unknown)"),
220 stage->entry,
221 stage->load,
222 stage->len,
223 stage->memlen);
224 return 0;
225}
226
227static int cbfs_print_payload_segment_info(struct cbfs_payload_segment *payload,
228 FILE *fp)
229{
230 switch(payload->type) {
231 case PAYLOAD_SEGMENT_CODE:
232 case PAYLOAD_SEGMENT_DATA:
233 fprintf(fp, " %s (%s compression, offset: 0x%x, "
234 "load: 0x%" PRIx64 ", length: %d/%d)\n",
235 (payload->type == PAYLOAD_SEGMENT_CODE ?
236 "code " : "data"),
237 lookup_name_by_type(types_cbfs_compression,
238 ntohl(payload->compression),
239 "(unknown)"),
240 ntohl(payload->offset),
241 ntohll(payload->load_addr),
242 ntohl(payload->len), ntohl(payload->mem_len));
243 break;
244
245 case PAYLOAD_SEGMENT_ENTRY:
246 fprintf(fp, " entry (0x%" PRIx64 ")\n",
247 ntohll(payload->load_addr));
248 break;
249
250 case PAYLOAD_SEGMENT_BSS:
251 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
252 "length 0x%x)\n",
253 ntohll(payload->load_addr),
254 ntohl(payload->len));
255 break;
256
257 case PAYLOAD_SEGMENT_PARAMS:
258 fprintf(fp, " parameters\n");
259 break;
260
261 default:
262 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
263 "load: 0x%" PRIx64 ", length: %d/%d\n",
264 payload->type,
265 lookup_name_by_type(types_cbfs_compression,
266 payload->compression,
267 "(unknown)"),
268 ntohl(payload->offset),
269 ntohll(payload->load_addr),
270 ntohl(payload->len),
271 ntohl(payload->mem_len));
272 break;
273 }
274 return 0;
275}
276
277int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
278 void *arg) {
279 const char *name = CBFS_NAME(entry);
280 struct cbfs_payload_segment *payload;
281 FILE *fp = (FILE *)arg;
282
283 if (!cbfs_is_valid_entry(entry)) {
284 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
285 cbfs_get_entry_addr(image, entry));
286 return -1;
287 }
288 if (!fp)
289 fp = stdout;
290
291 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
292 *name ? name : "(empty)",
293 cbfs_get_entry_addr(image, entry),
294 get_cbfs_entry_type_name(ntohl(entry->type)),
295 ntohl(entry->len));
296
297 if (!verbose)
298 return 0;
299
300 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
301 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
302 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
303 ntohl(entry->len));
304
305 /* note the components of the subheader may be in host order ... */
306 switch (ntohl(entry->type)) {
307 case CBFS_COMPONENT_STAGE:
308 cbfs_print_stage_info((struct cbfs_stage *)
309 CBFS_SUBHEADER(entry), fp);
310 break;
311
312 case CBFS_COMPONENT_PAYLOAD:
313 payload = (struct cbfs_payload_segment *)
314 CBFS_SUBHEADER(entry);
315 while (payload) {
316 cbfs_print_payload_segment_info(payload, fp);
317 if (payload->type == PAYLOAD_SEGMENT_ENTRY)
318 break;
319 else
320 payload ++;
321 }
322 break;
323 default:
324 break;
325 }
326 return 0;
327}
328
329int cbfs_print_directory(struct cbfs_image *image) {
330 cbfs_print_header_info(image);
331 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
332 cbfs_walk(image, cbfs_print_entry_info, NULL);
333 return 0;
334}
335
336int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
337 void *arg) {
338 int count = 0;
339 struct cbfs_file *entry;
340 for (entry = cbfs_find_first_entry(image);
341 entry && cbfs_is_valid_entry(entry);
342 entry = cbfs_find_next_entry(image, entry)) {
343 count ++;
344 if (callback(image, entry, arg) != 0)
345 break;
346 }
347 return count;
348}
349
Hung-Te Lineab2c812013-01-29 01:56:17 +0800350struct cbfs_header *cbfs_find_header(char *data, size_t size) {
351 size_t offset;
352 int found = 0;
353 uint32_t x86sig;
354 struct cbfs_header *header, *result = NULL;
355
356 // Try x86 style (check signature in bottom) header first.
357 x86sig = *(uint32_t *)(data + size - sizeof(uint32_t));
358 offset = (x86sig + (uint32_t)size);
359 DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset);
360 if (offset >= size - sizeof(*header) ||
361 ntohl(((struct cbfs_header *)(data + offset))->magic) !=
362 CBFS_HEADER_MAGIC)
363 offset = 0;
364
365 for (; offset + sizeof(*header) < size; offset++) {
366 header = (struct cbfs_header *)(data + offset);
367 if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC))
368 continue;
369 if (ntohl(header->version) != CBFS_HEADER_VERSION1 &&
370 ntohl(header->version) != CBFS_HEADER_VERSION2) {
371 // Probably not a real CBFS header?
372 continue;
373 }
374 found++;
375 result = header;
376 }
377 if (found > 1) {
378 ERROR("multiple (%d) CBFS headers found!\n",
379 found);
380 result = NULL;
381 }
382 return result;
383}
384
385
386struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) {
387 assert(image && image->header);
388 return (struct cbfs_file *)(image->buffer.data +
389 ntohl(image->header->offset));
390}
391
392struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
393 struct cbfs_file *entry) {
394 uint32_t addr = cbfs_get_entry_addr(image, entry);
395 int align = ntohl(image->header->align);
396 assert(entry && cbfs_is_valid_entry(entry));
397 addr += ntohl(entry->offset) + ntohl(entry->len);
398 addr = align_up(addr, align);
399 return (struct cbfs_file *)(image->buffer.data + addr);
400}
401
402uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry) {
403 assert(image && image->buffer.data && entry);
404 return (int32_t)((char *)entry - image->buffer.data);
405}
406
407int cbfs_is_valid_entry(struct cbfs_file *entry) {
408 return (entry &&memcmp(entry->magic, CBFS_FILE_MAGIC,
409 sizeof(entry->magic)) == 0);
410}
411