blob: 401654e99902880a3b340758661703d2ce066b8b [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);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600383
384 /* Will the file fit? Don't yet worry if we have space for a new
385 * "empty" entry. We take care of that later.
386 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800387 if (addr + need_size > addr_next)
388 continue;
389
390 // Can we simply put object here?
391 if (!content_offset || content_offset == addr + header_size) {
392 DEBUG("Filling new entry data (%zd bytes).\n",
393 buffer->size);
394 cbfs_create_empty_entry(image, entry, buffer->size,
395 name);
396 entry->type = htonl(type);
397 memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
398 if (verbose)
399 cbfs_print_entry_info(image, entry, stderr);
400
401 // setup new entry
Paul Menzel4159a802013-07-14 00:24:31 +0200402 DEBUG("Setting new empty entry.\n");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800403 entry = cbfs_find_next_entry(image, entry);
404 new_size = (cbfs_get_entry_addr(image, next) -
405 cbfs_get_entry_addr(image, entry));
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600406
407 /* Entry was added and no space for new "empty" entry */
408 if (new_size < cbfs_calculate_file_header_size("")) {
409 DEBUG("No need for new \"empty\" entry\n");
410 /* No need to increase the size of the just
411 * stored file to extend to next file. Alignment
412 * of next file takes care of this.
413 */
414 return 0;
415 }
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800416 new_size -= cbfs_calculate_file_header_size("");
417 DEBUG("new size: %d\n", new_size);
418 cbfs_create_empty_entry(image, entry, new_size, "");
419 if (verbose)
420 cbfs_print_entry_info(image, entry, stderr);
421 return 0;
422 }
423
424 // We need to put content here, and the case is really
425 // complicated...
426 assert(content_offset);
427 if (addr_next < content_offset) {
428 DEBUG("Not for specified offset yet");
429 continue;
430 } else if (addr > content_offset) {
431 DEBUG("Exceed specified content_offset.");
432 break;
433 } else if (addr + header_size > content_offset) {
434 ERROR("Not enough space for header.\n");
435 break;
436 } else if (content_offset + buffer->size > addr_next) {
437 ERROR("Not enough space for content.\n");
438 break;
439 }
440
441 // TODO there are more few tricky cases that we may
442 // want to fit by altering offset.
443 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
444 addr, addr_next - addr, content_offset);
445
446 if (cbfs_add_entry_at(image, entry, buffer->size, name, type,
447 buffer->data, content_offset) == 0) {
448 return 0;
449 }
450 break;
451 }
452
453 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
454 buffer->name, buffer->size, buffer->size / 1024, content_offset);
455 return -1;
456}
457
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700458struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
459{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800460 struct cbfs_file *entry;
461 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800462 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800463 entry = cbfs_find_next_entry(image, entry)) {
464 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
465 DEBUG("cbfs_get_entry: found %s\n", name);
466 return entry;
467 }
468 }
469 return NULL;
470}
471
472int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700473 const char *filename)
474{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800475 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
476 struct buffer buffer;
477 if (!entry) {
478 ERROR("File not found: %s\n", entry_name);
479 return -1;
480 }
481 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
482 entry_name, cbfs_get_entry_addr(image, entry),
483 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
484
485 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
486 WARN("Only 'raw' files are safe to extract.\n");
487 }
488
489 buffer.data = CBFS_SUBHEADER(entry);
490 buffer.size = ntohl(entry->len);
491 buffer.name = "(cbfs_export_entry)";
492 if (buffer_write_file(&buffer, filename) != 0) {
493 ERROR("Failed to write %s into %s.\n",
494 entry_name, filename);
495 return -1;
496 }
497 INFO("Successfully dumped the file to: %s\n", filename);
498 return 0;
499}
500
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700501int cbfs_remove_entry(struct cbfs_image *image, const char *name)
502{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800503 struct cbfs_file *entry, *next;
504 size_t len;
505 entry = cbfs_get_entry(image, name);
506 if (!entry) {
507 ERROR("CBFS file %s not found.\n", name);
508 return -1;
509 }
510 next = cbfs_find_next_entry(image, entry);
511 assert(next);
512 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
513 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
514 entry->type = htonl(CBFS_COMPONENT_DELETED);
515 len = (cbfs_get_entry_addr(image, next) -
516 cbfs_get_entry_addr(image, entry));
517 entry->offset = htonl(cbfs_calculate_file_header_size(""));
518 entry->len = htonl(len - ntohl(entry->offset));
519 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
520 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
521 ntohl(entry->len));
522 return 0;
523}
524
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700525int cbfs_print_header_info(struct cbfs_image *image)
526{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800527 char *name = strdup(image->buffer.name);
528 assert(image && image->header);
529 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
530 "alignment: %d bytes\n\n",
531 basename(name),
532 image->buffer.size / 1024,
533 ntohl(image->header->bootblocksize),
534 ntohl(image->header->romsize),
535 ntohl(image->header->offset),
536 ntohl(image->header->align));
537 free(name);
538 return 0;
539}
540
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700541static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
542{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800543 fprintf(fp,
544 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
545 "length: %d/%d\n",
546 lookup_name_by_type(types_cbfs_compression,
547 stage->compression, "(unknown)"),
548 stage->entry,
549 stage->load,
550 stage->len,
551 stage->memlen);
552 return 0;
553}
554
555static int cbfs_print_payload_segment_info(struct cbfs_payload_segment *payload,
556 FILE *fp)
557{
558 switch(payload->type) {
559 case PAYLOAD_SEGMENT_CODE:
560 case PAYLOAD_SEGMENT_DATA:
561 fprintf(fp, " %s (%s compression, offset: 0x%x, "
562 "load: 0x%" PRIx64 ", length: %d/%d)\n",
563 (payload->type == PAYLOAD_SEGMENT_CODE ?
564 "code " : "data"),
565 lookup_name_by_type(types_cbfs_compression,
566 ntohl(payload->compression),
567 "(unknown)"),
568 ntohl(payload->offset),
569 ntohll(payload->load_addr),
570 ntohl(payload->len), ntohl(payload->mem_len));
571 break;
572
573 case PAYLOAD_SEGMENT_ENTRY:
574 fprintf(fp, " entry (0x%" PRIx64 ")\n",
575 ntohll(payload->load_addr));
576 break;
577
578 case PAYLOAD_SEGMENT_BSS:
579 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
580 "length 0x%x)\n",
581 ntohll(payload->load_addr),
582 ntohl(payload->len));
583 break;
584
585 case PAYLOAD_SEGMENT_PARAMS:
586 fprintf(fp, " parameters\n");
587 break;
588
589 default:
590 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
591 "load: 0x%" PRIx64 ", length: %d/%d\n",
592 payload->type,
593 lookup_name_by_type(types_cbfs_compression,
594 payload->compression,
595 "(unknown)"),
596 ntohl(payload->offset),
597 ntohll(payload->load_addr),
598 ntohl(payload->len),
599 ntohl(payload->mem_len));
600 break;
601 }
602 return 0;
603}
604
605int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700606 void *arg)
607{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800608 const char *name = CBFS_NAME(entry);
609 struct cbfs_payload_segment *payload;
610 FILE *fp = (FILE *)arg;
611
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800612 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800613 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
614 cbfs_get_entry_addr(image, entry));
615 return -1;
616 }
617 if (!fp)
618 fp = stdout;
619
620 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
621 *name ? name : "(empty)",
622 cbfs_get_entry_addr(image, entry),
623 get_cbfs_entry_type_name(ntohl(entry->type)),
624 ntohl(entry->len));
625
626 if (!verbose)
627 return 0;
628
629 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
630 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
631 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
632 ntohl(entry->len));
633
634 /* note the components of the subheader may be in host order ... */
635 switch (ntohl(entry->type)) {
636 case CBFS_COMPONENT_STAGE:
637 cbfs_print_stage_info((struct cbfs_stage *)
638 CBFS_SUBHEADER(entry), fp);
639 break;
640
641 case CBFS_COMPONENT_PAYLOAD:
642 payload = (struct cbfs_payload_segment *)
643 CBFS_SUBHEADER(entry);
644 while (payload) {
645 cbfs_print_payload_segment_info(payload, fp);
646 if (payload->type == PAYLOAD_SEGMENT_ENTRY)
647 break;
648 else
649 payload ++;
650 }
651 break;
652 default:
653 break;
654 }
655 return 0;
656}
657
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700658int cbfs_print_directory(struct cbfs_image *image)
659{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800660 cbfs_print_header_info(image);
661 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
662 cbfs_walk(image, cbfs_print_entry_info, NULL);
663 return 0;
664}
665
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800666int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700667 void *arg)
668{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800669 struct cbfs_file *next;
670 uint32_t type, addr, last_addr;
671
672 type = ntohl(entry->type);
673 if (type == CBFS_COMPONENT_DELETED) {
674 // Ready to be recycled.
675 type = CBFS_COMPONENT_NULL;
676 entry->type = htonl(type);
677 }
678 if (type != CBFS_COMPONENT_NULL)
679 return 0;
680
681 next = cbfs_find_next_entry(image, entry);
682
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800683 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800684 type = ntohl(next->type);
685 if (type == CBFS_COMPONENT_DELETED) {
686 type = CBFS_COMPONENT_NULL;
687 next->type = htonl(type);
688 }
689 if (type != CBFS_COMPONENT_NULL)
690 return 0;
691
692 addr = cbfs_get_entry_addr(image, entry);
693 last_addr = cbfs_get_entry_addr(
694 image, cbfs_find_next_entry(image, next));
695
696 // Now, we find two deleted/empty entries; try to merge now.
697 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
698 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
699 cbfs_get_entry_addr(image, next), ntohl(next->len));
700 cbfs_create_empty_entry(image, entry,
701 (last_addr - addr -
702 cbfs_calculate_file_header_size("")),
703 "");
704 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
705 next = cbfs_find_next_entry(image, entry);
706 }
707 return 0;
708}
709
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800710int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700711 void *arg)
712{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800713 int count = 0;
714 struct cbfs_file *entry;
715 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800716 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800717 entry = cbfs_find_next_entry(image, entry)) {
718 count ++;
719 if (callback(image, entry, arg) != 0)
720 break;
721 }
722 return count;
723}
724
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700725struct cbfs_header *cbfs_find_header(char *data, size_t size)
726{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800727 size_t offset;
728 int found = 0;
729 uint32_t x86sig;
730 struct cbfs_header *header, *result = NULL;
731
732 // Try x86 style (check signature in bottom) header first.
733 x86sig = *(uint32_t *)(data + size - sizeof(uint32_t));
734 offset = (x86sig + (uint32_t)size);
735 DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset);
736 if (offset >= size - sizeof(*header) ||
737 ntohl(((struct cbfs_header *)(data + offset))->magic) !=
738 CBFS_HEADER_MAGIC)
739 offset = 0;
740
741 for (; offset + sizeof(*header) < size; offset++) {
742 header = (struct cbfs_header *)(data + offset);
743 if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC))
744 continue;
745 if (ntohl(header->version) != CBFS_HEADER_VERSION1 &&
746 ntohl(header->version) != CBFS_HEADER_VERSION2) {
747 // Probably not a real CBFS header?
748 continue;
749 }
750 found++;
751 result = header;
752 }
753 if (found > 1) {
754 ERROR("multiple (%d) CBFS headers found!\n",
755 found);
756 result = NULL;
757 }
758 return result;
759}
760
761
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700762struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
763{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800764 assert(image && image->header);
765 return (struct cbfs_file *)(image->buffer.data +
766 ntohl(image->header->offset));
767}
768
769struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700770 struct cbfs_file *entry)
771{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800772 uint32_t addr = cbfs_get_entry_addr(image, entry);
773 int align = ntohl(image->header->align);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800774 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +0800775 addr += ntohl(entry->offset) + ntohl(entry->len);
776 addr = align_up(addr, align);
777 return (struct cbfs_file *)(image->buffer.data + addr);
778}
779
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700780uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
781{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800782 assert(image && image->buffer.data && entry);
783 return (int32_t)((char *)entry - image->buffer.data);
784}
785
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700786int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
787{
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800788 return (entry &&
789 (char *)entry >= image->buffer.data &&
790 (char *)entry + sizeof(entry->magic) <
791 image->buffer.data + image->buffer.size &&
792 memcmp(entry->magic, CBFS_FILE_MAGIC,
793 sizeof(entry->magic)) == 0);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800794}
795
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800796int cbfs_init_entry(struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700797 struct buffer *buffer)
798{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800799 memset(entry, 0, sizeof(*entry));
800 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
801 entry->len = htonl(buffer->size);
802 entry->offset = htonl(sizeof(*entry) + strlen(buffer->name) + 1);
803 return 0;
804}
805
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800806int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700807 size_t len, const char *name)
808{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800809 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
810 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
811 entry->type = htonl(CBFS_COMPONENT_NULL);
812 entry->len = htonl(len);
813 entry->checksum = 0; // TODO Build a checksum algorithm.
814 entry->offset = htonl(cbfs_calculate_file_header_size(name));
815 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
816 strcpy(CBFS_NAME(entry), name);
817 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
818 return 0;
819}
820
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700821/* Finds a place to hold whole data in same memory page. */
822static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
823{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800824 if (!page)
825 return 1;
826 return (start / page) == (start + size - 1) / page;
827}
828
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800829/* Tests if data can fit in a range by given offset:
830 * start ->| header_len | offset (+ size) |<- end
831 */
832static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700833 uint32_t offset, uint32_t size)
834{
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800835 return (offset >= start + header_len && offset + size <= end);
836}
837
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800838int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700839 uint32_t size, uint32_t page_size, uint32_t align)
840{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800841 struct cbfs_file *entry;
842 size_t need_len;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800843 uint32_t addr, addr_next, addr2, addr3, offset, header_len;
844
845 /* Default values: allow fitting anywhere in ROM. */
846 if (!page_size)
847 page_size = ntohl(image->header->romsize);
848 if (!align)
849 align = 1;
850
851 if (size > page_size)
852 ERROR("Input file size (%d) greater than page size (%d).\n",
853 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800854
855 if (page_size % ntohl(image->header->align))
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800856 WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
857 __func__, page_size, ntohl(image->header->align));
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800858
859 /* TODO Old cbfstool always assume input is a stage file (and adding
860 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800861 * (type) param in future. For right now, we assume cbfs_stage is the
862 * largest structure and add it into header size. */
863 assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800864 header_len = (cbfs_calculate_file_header_size(name) +
865 sizeof(struct cbfs_stage));
866 need_len = header_len + size;
867
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800868 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800869 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
870
871 /* Three cases of content location on memory page:
872 * case 1.
873 * | PAGE 1 | PAGE 2 |
874 * | <header><content>| Fit. Return start of content.
875 *
876 * case 2.
877 * | PAGE 1 | PAGE 2 |
878 * | <header><content> | Fits when we shift content to align
879 * shift-> | <header>|<content> | at starting of PAGE 2.
880 *
881 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800882 * | PAGE 1 | PAGE 2 | PAGE 3 |
883 * | <header>< content > | Can't fit. If we shift content to
884 * |trial-> <header>< content > | PAGE 2, header can't fit in free
885 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800886 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800887 * The returned address can be then used as "base-address" (-b) in add-*
888 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
889 * For stage targets, the address is also used to re-link stage before
890 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800891 */
892 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800893 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800894 entry = cbfs_find_next_entry(image, entry)) {
895
896 uint32_t type = ntohl(entry->type);
897 if (type != CBFS_COMPONENT_NULL)
898 continue;
899
900 addr = cbfs_get_entry_addr(image, entry);
901 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
902 image, entry));
903 if (addr_next - addr < need_len)
904 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800905
906 offset = align_up(addr + header_len, align);
907 if (is_in_same_page(offset, size, page_size) &&
908 is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800909 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800910 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800911 }
912
913 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800914 offset = align_up(addr2, align);
915 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800916 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800917 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800918 }
919
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800920 /* Assume page_size >= header_len so adding one page will
921 * definitely provide the space for header. */
922 assert(page_size >= header_len);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800923 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800924 offset = align_up(addr3, align);
925 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800926 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800927 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800928 }
929 }
930 return -1;
931}