blob: 44a9fe8c48121eecf7e5b606361af071d35fa12f [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
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080036/* Type and format */
37
38struct typedesc_t {
39 uint32_t type;
40 const char *name;
41};
42
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070043static const struct typedesc_t types_cbfs_entry[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080044 {CBFS_COMPONENT_STAGE, "stage"},
45 {CBFS_COMPONENT_PAYLOAD, "payload"},
46 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
47 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
48 {CBFS_COMPONENT_RAW, "raw"},
49 {CBFS_COMPONENT_VSA, "vsa"},
50 {CBFS_COMPONENT_MBI, "mbi"},
51 {CBFS_COMPONENT_MICROCODE, "microcode"},
52 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos_default"},
53 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos_layout"},
54 {CBFS_COMPONENT_DELETED, "deleted"},
55 {CBFS_COMPONENT_NULL, "null"},
56 {0, NULL},
57};
58
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070059static const struct typedesc_t types_cbfs_compression[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080060 {CBFS_COMPRESS_NONE, "none"},
61 {CBFS_COMPRESS_LZMA, "LZMA"},
62 {0, NULL},
63};
64
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070065static uint32_t align_up(uint32_t value, uint32_t align)
66{
67 if (value % align)
68 value += align - (value % align);
69 return value;
70}
71
72uint32_t lookup_type_by_name(const struct typedesc_t *desc, const char *name,
73 uint32_t default_value)
74{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080075 int i;
76 for (i = 0; desc[i].name; i++)
77 if (strcmp(desc[i].name, name) == 0)
78 return desc[i].type;
79 return default_value;
80}
81
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070082const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
83 const char *default_value)
84{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080085 int i;
86 for (i = 0; desc[i].name; i++)
87 if (desc[i].type == type)
88 return desc[i].name;
89 return default_value;
90}
91
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070092uint32_t get_cbfs_entry_type(const char *name, uint32_t default_value)
93{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080094 return lookup_type_by_name(types_cbfs_entry, name, default_value);
95}
96
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070097const char *get_cbfs_entry_type_name(uint32_t type)
98{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080099 return lookup_name_by_type(types_cbfs_entry, type, "(unknown)");
100}
101
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700102uint32_t get_cbfs_compression(const char *name, uint32_t unknown)
103{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800104 return lookup_type_by_name(types_cbfs_compression, name, unknown);
105}
106
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800107/* CBFS image */
108
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700109static int cbfs_calculate_file_header_size(const char *name)
110{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800111 return (sizeof(struct cbfs_file) +
112 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
113}
114
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700115static int cbfs_fix_legacy_size(struct cbfs_image *image)
116{
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800117 // A bug in old cbfstool may produce extra few bytes (by alignment) and
118 // cause cbfstool to overwrite things after free space -- which is
119 // usually CBFS header on x86. We need to workaround that.
120
121 struct cbfs_file *entry, *first = NULL, *last = NULL;
122 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800123 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800124 entry = cbfs_find_next_entry(image, entry)) {
125 last = entry;
126 }
127 if ((char *)first < (char *)image->header &&
128 (char *)entry > (char *)image->header) {
129 WARN("CBFS image was created with old cbfstool with size bug. "
130 "Fixing size in last entry...\n");
131 last->len = htonl(ntohl(last->len) -
132 ntohl(image->header->align));
133 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
134 cbfs_get_entry_addr(image, entry),
135 cbfs_get_entry_addr(image,
136 cbfs_find_next_entry(image, last)));
137 }
138 return 0;
139}
140
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800141int cbfs_image_create(struct cbfs_image *image,
142 uint32_t arch,
143 size_t size,
144 uint32_t align,
145 struct buffer *bootblock,
146 int32_t bootblock_offset,
147 int32_t header_offset,
148 int32_t entries_offset)
149{
150 struct cbfs_header *header;
151 struct cbfs_file *entry;
152 uint32_t cbfs_len;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800153 size_t entry_header_len;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800154
155 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
156 "header=0x%x+0x%zx, entries_offset=0x%x\n",
157 bootblock_offset, bootblock->size,
158 header_offset, sizeof(*header), entries_offset);
159
160 if (buffer_create(&image->buffer, size, "(new)") != 0)
161 return -1;
162 image->header = NULL;
163 memset(image->buffer.data, CBFS_CONTENT_DEFAULT_VALUE, size);
164
165 // Adjust legcay top-aligned address to ROM offset.
166 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
167 entries_offset += (int32_t)size;
168 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
169 bootblock_offset += (int32_t)size;
170 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
171 header_offset += (int32_t) size;
172
173 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
174 "header=0x%x, entries_offset=0x%x\n",
175 bootblock_offset, header_offset, entries_offset);
176
177 if (align == 0)
178 align = 64; // default align size.
179
180 // Prepare bootblock
181 if (bootblock_offset + bootblock->size > size) {
182 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
183 bootblock_offset, bootblock->size, size);
184 return -1;
185 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800186 if (entries_offset > bootblock_offset &&
187 entries_offset < bootblock->size) {
188 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
189 bootblock_offset, bootblock->size, entries_offset);
190 return -1;
191 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800192 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
193 bootblock->size);
194
195 // Prepare header
196 if (header_offset + sizeof(*header) > size) {
197 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
198 header_offset, sizeof(*header), size);
199 return -1;
200 }
201 header = (struct cbfs_header *)(image->buffer.data + header_offset);
202 image->header = header;
203 header->magic = htonl(CBFS_HEADER_MAGIC);
204 header->version = htonl(CBFS_HEADER_VERSION);
205 header->romsize = htonl(size);
206 header->bootblocksize = htonl(bootblock->size);
207 header->align = htonl(align);
208 header->offset = htonl(entries_offset);
209 header->architecture = htonl(arch);
210
211 // Prepare entries
212 if (align_up(entries_offset, align) != entries_offset) {
213 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
214 entries_offset, align);
215 return -1;
216 }
Hung-Te Linb02c8732013-03-15 17:40:08 +0800217 entry_header_len = cbfs_calculate_file_header_size("");
218 if (entries_offset + entry_header_len > size) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800219 ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n",
Hung-Te Linb02c8732013-03-15 17:40:08 +0800220 entries_offset, entry_header_len, size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800221 return -1;
222 }
223 entry = (struct cbfs_file *)(image->buffer.data + entries_offset);
224 // To calculate available length, find
225 // e = min(bootblock, header, size) where e > entries_offset.
226 cbfs_len = size;
227 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
228 cbfs_len = bootblock_offset;
229 if (header_offset > entries_offset && header_offset < cbfs_len)
230 cbfs_len = header_offset;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800231 cbfs_len -= entries_offset + align + entry_header_len;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800232 cbfs_create_empty_entry(image, entry, cbfs_len, "");
233 LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len);
234 return 0;
235}
236
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700237int cbfs_image_from_file(struct cbfs_image *image, const char *filename)
238{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800239 if (buffer_from_file(&image->buffer, filename) != 0)
240 return -1;
241 DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
242 image->buffer.size);
243 image->header = cbfs_find_header(image->buffer.data,
244 image->buffer.size);
245 if (!image->header) {
246 ERROR("%s does not have CBFS master header.\n", filename);
247 cbfs_image_delete(image);
248 return -1;
249 }
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800250 cbfs_fix_legacy_size(image);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800251
252 return 0;
253}
254
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700255int cbfs_image_write_file(struct cbfs_image *image, const char *filename)
256{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800257 assert(image && image->buffer.data);
258 return buffer_write_file(&image->buffer, filename);
259}
260
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700261int cbfs_image_delete(struct cbfs_image *image)
262{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800263 buffer_delete(&image->buffer);
264 image->header = NULL;
265 return 0;
266}
267
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800268/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
269static int cbfs_add_entry_at(struct cbfs_image *image,
270 struct cbfs_file *entry,
271 uint32_t size,
272 const char *name,
273 uint32_t type,
274 const void *data,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700275 uint32_t content_offset)
276{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800277 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
278 uint32_t addr = cbfs_get_entry_addr(image, entry),
279 addr_next = cbfs_get_entry_addr(image, next);
280 uint32_t header_size = cbfs_calculate_file_header_size(name),
281 min_entry_size = cbfs_calculate_file_header_size("");
282 uint32_t len, target;
283 uint32_t align = ntohl(image->header->align);
284
285 target = content_offset - header_size;
286 if (target % align)
287 target -= target % align;
288 if (target < addr) {
289 ERROR("No space to hold cbfs_file header.");
290 return -1;
291 }
292
293 // Process buffer BEFORE content_offset.
294 if (target - addr > min_entry_size) {
295 DEBUG("|min|...|header|content|... <create new entry>\n");
296 len = target - addr - min_entry_size;
297 cbfs_create_empty_entry(image, entry, len, "");
298 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
299 entry = cbfs_find_next_entry(image, entry);
300 addr = cbfs_get_entry_addr(image, entry);
301 }
302
303 len = size + (content_offset - addr - header_size);
304 cbfs_create_empty_entry(image, entry, len, name);
305 if (len != size) {
306 DEBUG("|..|header|content|... <use offset to create entry>\n");
307 DEBUG("before: offset=0x%x, len=0x%x\n",
308 ntohl(entry->offset), ntohl(entry->len));
309 // TODO reset expanded name buffer to 0xFF.
310 entry->offset = htonl(ntohl(entry->offset) + (len - size));
311 entry->len = htonl(size);
312 DEBUG("after: offset=0x%x, len=0x%x\n",
313 ntohl(entry->offset), ntohl(entry->len));
314 }
315
316 // Ready to fill data into entry.
317 assert(ntohl(entry->len) == size);
318 entry->type = htonl(type);
319 DEBUG("content_offset: 0x%x, entry location: %x\n",
320 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
321 image->buffer.data));
322 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
323 content_offset);
324 memcpy(CBFS_SUBHEADER(entry), data, size);
325 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
326
327 // Process buffer AFTER entry.
328 entry = cbfs_find_next_entry(image, entry);
329 addr = cbfs_get_entry_addr(image, entry);
330 assert(addr < addr_next);
331
332 if (addr_next - addr < min_entry_size) {
333 DEBUG("No space after content to keep CBFS structure.\n");
334 return -1;
335 }
336
337 len = addr_next - addr - min_entry_size;
338 cbfs_create_empty_entry(image, entry, len, "");
339 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
340 return 0;
341}
342
343int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700344 const char *name, uint32_t type, uint32_t content_offset)
345{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800346 uint32_t entry_type;
347 uint32_t addr, addr_next;
348 struct cbfs_file *entry, *next;
349 uint32_t header_size, need_size, new_size;
350
351 header_size = cbfs_calculate_file_header_size(name);
352
353 need_size = header_size + buffer->size;
354 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
355 name, content_offset, header_size, buffer->size, need_size);
356
357 if (IS_TOP_ALIGNED_ADDRESS(content_offset)) {
358 // legacy cbfstool takes top-aligned address.
359 uint32_t romsize = ntohl(image->header->romsize);
360 INFO("Converting top-aligned address 0x%x to offset: 0x%x\n",
361 content_offset, content_offset + romsize);
362 content_offset += romsize;
363 }
364
365 // Merge empty entries.
366 DEBUG("(trying to merge empty entries...)\n");
367 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
368
369 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800370 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800371 entry = cbfs_find_next_entry(image, entry)) {
372
373 entry_type = ntohl(entry->type);
374 if (entry_type != CBFS_COMPONENT_NULL)
375 continue;
376
377 addr = cbfs_get_entry_addr(image, entry);
378 next = cbfs_find_next_entry(image, entry);
379 addr_next = cbfs_get_entry_addr(image, next);
380
381 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
382 addr, addr_next - addr, addr_next - addr);
383 if (addr + need_size > addr_next)
384 continue;
385
386 // Can we simply put object here?
387 if (!content_offset || content_offset == addr + header_size) {
388 DEBUG("Filling new entry data (%zd bytes).\n",
389 buffer->size);
390 cbfs_create_empty_entry(image, entry, buffer->size,
391 name);
392 entry->type = htonl(type);
393 memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
394 if (verbose)
395 cbfs_print_entry_info(image, entry, stderr);
396
397 // setup new entry
398 DEBUG("Seting new empty entry.\n");
399 entry = cbfs_find_next_entry(image, entry);
400 new_size = (cbfs_get_entry_addr(image, next) -
401 cbfs_get_entry_addr(image, entry));
402 new_size -= cbfs_calculate_file_header_size("");
403 DEBUG("new size: %d\n", new_size);
404 cbfs_create_empty_entry(image, entry, new_size, "");
405 if (verbose)
406 cbfs_print_entry_info(image, entry, stderr);
407 return 0;
408 }
409
410 // We need to put content here, and the case is really
411 // complicated...
412 assert(content_offset);
413 if (addr_next < content_offset) {
414 DEBUG("Not for specified offset yet");
415 continue;
416 } else if (addr > content_offset) {
417 DEBUG("Exceed specified content_offset.");
418 break;
419 } else if (addr + header_size > content_offset) {
420 ERROR("Not enough space for header.\n");
421 break;
422 } else if (content_offset + buffer->size > addr_next) {
423 ERROR("Not enough space for content.\n");
424 break;
425 }
426
427 // TODO there are more few tricky cases that we may
428 // want to fit by altering offset.
429 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
430 addr, addr_next - addr, content_offset);
431
432 if (cbfs_add_entry_at(image, entry, buffer->size, name, type,
433 buffer->data, content_offset) == 0) {
434 return 0;
435 }
436 break;
437 }
438
439 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
440 buffer->name, buffer->size, buffer->size / 1024, content_offset);
441 return -1;
442}
443
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700444struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
445{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800446 struct cbfs_file *entry;
447 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800448 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800449 entry = cbfs_find_next_entry(image, entry)) {
450 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
451 DEBUG("cbfs_get_entry: found %s\n", name);
452 return entry;
453 }
454 }
455 return NULL;
456}
457
458int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700459 const char *filename)
460{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800461 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
462 struct buffer buffer;
463 if (!entry) {
464 ERROR("File not found: %s\n", entry_name);
465 return -1;
466 }
467 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
468 entry_name, cbfs_get_entry_addr(image, entry),
469 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
470
471 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
472 WARN("Only 'raw' files are safe to extract.\n");
473 }
474
475 buffer.data = CBFS_SUBHEADER(entry);
476 buffer.size = ntohl(entry->len);
477 buffer.name = "(cbfs_export_entry)";
478 if (buffer_write_file(&buffer, filename) != 0) {
479 ERROR("Failed to write %s into %s.\n",
480 entry_name, filename);
481 return -1;
482 }
483 INFO("Successfully dumped the file to: %s\n", filename);
484 return 0;
485}
486
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700487int cbfs_remove_entry(struct cbfs_image *image, const char *name)
488{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800489 struct cbfs_file *entry, *next;
490 size_t len;
491 entry = cbfs_get_entry(image, name);
492 if (!entry) {
493 ERROR("CBFS file %s not found.\n", name);
494 return -1;
495 }
496 next = cbfs_find_next_entry(image, entry);
497 assert(next);
498 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
499 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
500 entry->type = htonl(CBFS_COMPONENT_DELETED);
501 len = (cbfs_get_entry_addr(image, next) -
502 cbfs_get_entry_addr(image, entry));
503 entry->offset = htonl(cbfs_calculate_file_header_size(""));
504 entry->len = htonl(len - ntohl(entry->offset));
505 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
506 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
507 ntohl(entry->len));
508 return 0;
509}
510
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700511int cbfs_print_header_info(struct cbfs_image *image)
512{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800513 char *name = strdup(image->buffer.name);
514 assert(image && image->header);
515 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
516 "alignment: %d bytes\n\n",
517 basename(name),
518 image->buffer.size / 1024,
519 ntohl(image->header->bootblocksize),
520 ntohl(image->header->romsize),
521 ntohl(image->header->offset),
522 ntohl(image->header->align));
523 free(name);
524 return 0;
525}
526
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700527static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
528{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800529 fprintf(fp,
530 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
531 "length: %d/%d\n",
532 lookup_name_by_type(types_cbfs_compression,
533 stage->compression, "(unknown)"),
534 stage->entry,
535 stage->load,
536 stage->len,
537 stage->memlen);
538 return 0;
539}
540
541static int cbfs_print_payload_segment_info(struct cbfs_payload_segment *payload,
542 FILE *fp)
543{
544 switch(payload->type) {
545 case PAYLOAD_SEGMENT_CODE:
546 case PAYLOAD_SEGMENT_DATA:
547 fprintf(fp, " %s (%s compression, offset: 0x%x, "
548 "load: 0x%" PRIx64 ", length: %d/%d)\n",
549 (payload->type == PAYLOAD_SEGMENT_CODE ?
550 "code " : "data"),
551 lookup_name_by_type(types_cbfs_compression,
552 ntohl(payload->compression),
553 "(unknown)"),
554 ntohl(payload->offset),
555 ntohll(payload->load_addr),
556 ntohl(payload->len), ntohl(payload->mem_len));
557 break;
558
559 case PAYLOAD_SEGMENT_ENTRY:
560 fprintf(fp, " entry (0x%" PRIx64 ")\n",
561 ntohll(payload->load_addr));
562 break;
563
564 case PAYLOAD_SEGMENT_BSS:
565 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
566 "length 0x%x)\n",
567 ntohll(payload->load_addr),
568 ntohl(payload->len));
569 break;
570
571 case PAYLOAD_SEGMENT_PARAMS:
572 fprintf(fp, " parameters\n");
573 break;
574
575 default:
576 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
577 "load: 0x%" PRIx64 ", length: %d/%d\n",
578 payload->type,
579 lookup_name_by_type(types_cbfs_compression,
580 payload->compression,
581 "(unknown)"),
582 ntohl(payload->offset),
583 ntohll(payload->load_addr),
584 ntohl(payload->len),
585 ntohl(payload->mem_len));
586 break;
587 }
588 return 0;
589}
590
591int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700592 void *arg)
593{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800594 const char *name = CBFS_NAME(entry);
595 struct cbfs_payload_segment *payload;
596 FILE *fp = (FILE *)arg;
597
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800598 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800599 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
600 cbfs_get_entry_addr(image, entry));
601 return -1;
602 }
603 if (!fp)
604 fp = stdout;
605
606 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
607 *name ? name : "(empty)",
608 cbfs_get_entry_addr(image, entry),
609 get_cbfs_entry_type_name(ntohl(entry->type)),
610 ntohl(entry->len));
611
612 if (!verbose)
613 return 0;
614
615 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
616 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
617 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
618 ntohl(entry->len));
619
620 /* note the components of the subheader may be in host order ... */
621 switch (ntohl(entry->type)) {
622 case CBFS_COMPONENT_STAGE:
623 cbfs_print_stage_info((struct cbfs_stage *)
624 CBFS_SUBHEADER(entry), fp);
625 break;
626
627 case CBFS_COMPONENT_PAYLOAD:
628 payload = (struct cbfs_payload_segment *)
629 CBFS_SUBHEADER(entry);
630 while (payload) {
631 cbfs_print_payload_segment_info(payload, fp);
632 if (payload->type == PAYLOAD_SEGMENT_ENTRY)
633 break;
634 else
635 payload ++;
636 }
637 break;
638 default:
639 break;
640 }
641 return 0;
642}
643
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700644int cbfs_print_directory(struct cbfs_image *image)
645{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800646 cbfs_print_header_info(image);
647 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
648 cbfs_walk(image, cbfs_print_entry_info, NULL);
649 return 0;
650}
651
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800652int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700653 void *arg)
654{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800655 struct cbfs_file *next;
656 uint32_t type, addr, last_addr;
657
658 type = ntohl(entry->type);
659 if (type == CBFS_COMPONENT_DELETED) {
660 // Ready to be recycled.
661 type = CBFS_COMPONENT_NULL;
662 entry->type = htonl(type);
663 }
664 if (type != CBFS_COMPONENT_NULL)
665 return 0;
666
667 next = cbfs_find_next_entry(image, entry);
668
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800669 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800670 type = ntohl(next->type);
671 if (type == CBFS_COMPONENT_DELETED) {
672 type = CBFS_COMPONENT_NULL;
673 next->type = htonl(type);
674 }
675 if (type != CBFS_COMPONENT_NULL)
676 return 0;
677
678 addr = cbfs_get_entry_addr(image, entry);
679 last_addr = cbfs_get_entry_addr(
680 image, cbfs_find_next_entry(image, next));
681
682 // Now, we find two deleted/empty entries; try to merge now.
683 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
684 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
685 cbfs_get_entry_addr(image, next), ntohl(next->len));
686 cbfs_create_empty_entry(image, entry,
687 (last_addr - addr -
688 cbfs_calculate_file_header_size("")),
689 "");
690 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
691 next = cbfs_find_next_entry(image, entry);
692 }
693 return 0;
694}
695
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800696int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700697 void *arg)
698{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800699 int count = 0;
700 struct cbfs_file *entry;
701 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800702 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800703 entry = cbfs_find_next_entry(image, entry)) {
704 count ++;
705 if (callback(image, entry, arg) != 0)
706 break;
707 }
708 return count;
709}
710
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700711struct cbfs_header *cbfs_find_header(char *data, size_t size)
712{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800713 size_t offset;
714 int found = 0;
715 uint32_t x86sig;
716 struct cbfs_header *header, *result = NULL;
717
718 // Try x86 style (check signature in bottom) header first.
719 x86sig = *(uint32_t *)(data + size - sizeof(uint32_t));
720 offset = (x86sig + (uint32_t)size);
721 DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset);
722 if (offset >= size - sizeof(*header) ||
723 ntohl(((struct cbfs_header *)(data + offset))->magic) !=
724 CBFS_HEADER_MAGIC)
725 offset = 0;
726
727 for (; offset + sizeof(*header) < size; offset++) {
728 header = (struct cbfs_header *)(data + offset);
729 if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC))
730 continue;
731 if (ntohl(header->version) != CBFS_HEADER_VERSION1 &&
732 ntohl(header->version) != CBFS_HEADER_VERSION2) {
733 // Probably not a real CBFS header?
734 continue;
735 }
736 found++;
737 result = header;
738 }
739 if (found > 1) {
740 ERROR("multiple (%d) CBFS headers found!\n",
741 found);
742 result = NULL;
743 }
744 return result;
745}
746
747
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700748struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
749{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800750 assert(image && image->header);
751 return (struct cbfs_file *)(image->buffer.data +
752 ntohl(image->header->offset));
753}
754
755struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700756 struct cbfs_file *entry)
757{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800758 uint32_t addr = cbfs_get_entry_addr(image, entry);
759 int align = ntohl(image->header->align);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800760 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +0800761 addr += ntohl(entry->offset) + ntohl(entry->len);
762 addr = align_up(addr, align);
763 return (struct cbfs_file *)(image->buffer.data + addr);
764}
765
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700766uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
767{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800768 assert(image && image->buffer.data && entry);
769 return (int32_t)((char *)entry - image->buffer.data);
770}
771
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700772int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
773{
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800774 return (entry &&
775 (char *)entry >= image->buffer.data &&
776 (char *)entry + sizeof(entry->magic) <
777 image->buffer.data + image->buffer.size &&
778 memcmp(entry->magic, CBFS_FILE_MAGIC,
779 sizeof(entry->magic)) == 0);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800780}
781
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800782int cbfs_init_entry(struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700783 struct buffer *buffer)
784{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800785 memset(entry, 0, sizeof(*entry));
786 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
787 entry->len = htonl(buffer->size);
788 entry->offset = htonl(sizeof(*entry) + strlen(buffer->name) + 1);
789 return 0;
790}
791
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800792int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700793 size_t len, const char *name)
794{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800795 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
796 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
797 entry->type = htonl(CBFS_COMPONENT_NULL);
798 entry->len = htonl(len);
799 entry->checksum = 0; // TODO Build a checksum algorithm.
800 entry->offset = htonl(cbfs_calculate_file_header_size(name));
801 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
802 strcpy(CBFS_NAME(entry), name);
803 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
804 return 0;
805}
806
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700807/* Finds a place to hold whole data in same memory page. */
808static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
809{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800810 if (!page)
811 return 1;
812 return (start / page) == (start + size - 1) / page;
813}
814
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800815/* Tests if data can fit in a range by given offset:
816 * start ->| header_len | offset (+ size) |<- end
817 */
818static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700819 uint32_t offset, uint32_t size)
820{
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800821 return (offset >= start + header_len && offset + size <= end);
822}
823
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800824int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700825 uint32_t size, uint32_t page_size, uint32_t align)
826{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800827 struct cbfs_file *entry;
828 size_t need_len;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800829 uint32_t addr, addr_next, addr2, addr3, offset, header_len;
830
831 /* Default values: allow fitting anywhere in ROM. */
832 if (!page_size)
833 page_size = ntohl(image->header->romsize);
834 if (!align)
835 align = 1;
836
837 if (size > page_size)
838 ERROR("Input file size (%d) greater than page size (%d).\n",
839 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800840
841 if (page_size % ntohl(image->header->align))
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800842 WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
843 __func__, page_size, ntohl(image->header->align));
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800844
845 /* TODO Old cbfstool always assume input is a stage file (and adding
846 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800847 * (type) param in future. For right now, we assume cbfs_stage is the
848 * largest structure and add it into header size. */
849 assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800850 header_len = (cbfs_calculate_file_header_size(name) +
851 sizeof(struct cbfs_stage));
852 need_len = header_len + size;
853
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800854 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800855 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
856
857 /* Three cases of content location on memory page:
858 * case 1.
859 * | PAGE 1 | PAGE 2 |
860 * | <header><content>| Fit. Return start of content.
861 *
862 * case 2.
863 * | PAGE 1 | PAGE 2 |
864 * | <header><content> | Fits when we shift content to align
865 * shift-> | <header>|<content> | at starting of PAGE 2.
866 *
867 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800868 * | PAGE 1 | PAGE 2 | PAGE 3 |
869 * | <header>< content > | Can't fit. If we shift content to
870 * |trial-> <header>< content > | PAGE 2, header can't fit in free
871 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800872 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800873 * The returned address can be then used as "base-address" (-b) in add-*
874 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
875 * For stage targets, the address is also used to re-link stage before
876 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800877 */
878 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800879 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800880 entry = cbfs_find_next_entry(image, entry)) {
881
882 uint32_t type = ntohl(entry->type);
883 if (type != CBFS_COMPONENT_NULL)
884 continue;
885
886 addr = cbfs_get_entry_addr(image, entry);
887 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
888 image, entry));
889 if (addr_next - addr < need_len)
890 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800891
892 offset = align_up(addr + header_len, align);
893 if (is_in_same_page(offset, size, page_size) &&
894 is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800895 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800896 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800897 }
898
899 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800900 offset = align_up(addr2, align);
901 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800902 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800903 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800904 }
905
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800906 /* Assume page_size >= header_len so adding one page will
907 * definitely provide the space for header. */
908 assert(page_size >= header_len);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800909 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800910 offset = align_up(addr3, align);
911 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800912 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800913 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800914 }
915 }
916 return -1;
917}