blob: dddd480cc2ee871d875c031063a37d563d52c4bc [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"
Ronald G. Minnicha8a133d2013-12-30 13:16:18 -080027#include "elf.h"
Hung-Te Lineab2c812013-01-29 01:56:17 +080028#include "cbfs_image.h"
29
30/* The file name align is not defined in CBFS spec -- only a preference by
31 * (old) cbfstool. */
32#define CBFS_FILENAME_ALIGN (16)
33
34/* To make CBFS more friendly to ROM, fill -1 (0xFF) instead of zero. */
35#define CBFS_CONTENT_DEFAULT_VALUE (-1)
36
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080037/* Type and format */
38
39struct typedesc_t {
40 uint32_t type;
41 const char *name;
42};
43
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070044static const struct typedesc_t types_cbfs_entry[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080045 {CBFS_COMPONENT_STAGE, "stage"},
46 {CBFS_COMPONENT_PAYLOAD, "payload"},
47 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
48 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
49 {CBFS_COMPONENT_RAW, "raw"},
50 {CBFS_COMPONENT_VSA, "vsa"},
51 {CBFS_COMPONENT_MBI, "mbi"},
52 {CBFS_COMPONENT_MICROCODE, "microcode"},
53 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos_default"},
54 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos_layout"},
55 {CBFS_COMPONENT_DELETED, "deleted"},
56 {CBFS_COMPONENT_NULL, "null"},
57 {0, NULL},
58};
59
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070060static const struct typedesc_t types_cbfs_compression[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080061 {CBFS_COMPRESS_NONE, "none"},
62 {CBFS_COMPRESS_LZMA, "LZMA"},
63 {0, NULL},
64};
65
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070066static uint32_t align_up(uint32_t value, uint32_t align)
67{
68 if (value % align)
69 value += align - (value % align);
70 return value;
71}
72
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080073static uint32_t lookup_type_by_name(const struct typedesc_t *desc, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070074 uint32_t default_value)
75{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080076 int i;
77 for (i = 0; desc[i].name; i++)
78 if (strcmp(desc[i].name, name) == 0)
79 return desc[i].type;
80 return default_value;
81}
82
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080083static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070084 const char *default_value)
85{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080086 int i;
87 for (i = 0; desc[i].name; i++)
88 if (desc[i].type == type)
89 return desc[i].name;
90 return default_value;
91}
92
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070093uint32_t get_cbfs_entry_type(const char *name, uint32_t default_value)
94{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080095 return lookup_type_by_name(types_cbfs_entry, name, default_value);
96}
97
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070098const char *get_cbfs_entry_type_name(uint32_t type)
99{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800100 return lookup_name_by_type(types_cbfs_entry, type, "(unknown)");
101}
102
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700103uint32_t get_cbfs_compression(const char *name, uint32_t unknown)
104{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800105 return lookup_type_by_name(types_cbfs_compression, name, unknown);
106}
107
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800108/* CBFS image */
109
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700110static int cbfs_calculate_file_header_size(const char *name)
111{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800112 return (sizeof(struct cbfs_file) +
113 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
114}
115
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700116static int cbfs_fix_legacy_size(struct cbfs_image *image)
117{
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800118 // A bug in old cbfstool may produce extra few bytes (by alignment) and
119 // cause cbfstool to overwrite things after free space -- which is
120 // usually CBFS header on x86. We need to workaround that.
121
122 struct cbfs_file *entry, *first = NULL, *last = NULL;
123 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800124 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800125 entry = cbfs_find_next_entry(image, entry)) {
126 last = entry;
127 }
128 if ((char *)first < (char *)image->header &&
129 (char *)entry > (char *)image->header) {
130 WARN("CBFS image was created with old cbfstool with size bug. "
131 "Fixing size in last entry...\n");
132 last->len = htonl(ntohl(last->len) -
133 ntohl(image->header->align));
134 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
135 cbfs_get_entry_addr(image, entry),
136 cbfs_get_entry_addr(image,
137 cbfs_find_next_entry(image, last)));
138 }
139 return 0;
140}
141
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800142void cbfs_put_header(void *dest, const struct cbfs_header *header)
143{
144 struct buffer outheader;
145
146 outheader.data = dest;
147 outheader.size = 0;
148
149 xdr_be.put32(&outheader, header->magic);
150 xdr_be.put32(&outheader, header->version);
151 xdr_be.put32(&outheader, header->romsize);
152 xdr_be.put32(&outheader, header->bootblocksize);
153 xdr_be.put32(&outheader, header->align);
154 xdr_be.put32(&outheader, header->offset);
155 xdr_be.put32(&outheader, header->architecture);
156}
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800157int cbfs_image_create(struct cbfs_image *image,
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800158 uint32_t myarch,
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800159 size_t size,
160 uint32_t align,
161 struct buffer *bootblock,
162 int32_t bootblock_offset,
163 int32_t header_offset,
164 int32_t entries_offset)
165{
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800166 struct cbfs_header header;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800167 struct cbfs_file *entry;
168 uint32_t cbfs_len;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800169 size_t entry_header_len;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800170
171 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
172 "header=0x%x+0x%zx, entries_offset=0x%x\n",
173 bootblock_offset, bootblock->size,
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800174 header_offset, sizeof(header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800175
176 if (buffer_create(&image->buffer, size, "(new)") != 0)
177 return -1;
178 image->header = NULL;
179 memset(image->buffer.data, CBFS_CONTENT_DEFAULT_VALUE, size);
180
181 // Adjust legcay top-aligned address to ROM offset.
182 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
183 entries_offset += (int32_t)size;
184 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
185 bootblock_offset += (int32_t)size;
186 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
187 header_offset += (int32_t) size;
188
189 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
190 "header=0x%x, entries_offset=0x%x\n",
191 bootblock_offset, header_offset, entries_offset);
192
193 if (align == 0)
194 align = 64; // default align size.
195
196 // Prepare bootblock
197 if (bootblock_offset + bootblock->size > size) {
198 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
199 bootblock_offset, bootblock->size, size);
200 return -1;
201 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800202 if (entries_offset > bootblock_offset &&
203 entries_offset < bootblock->size) {
204 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
205 bootblock_offset, bootblock->size, entries_offset);
206 return -1;
207 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800208 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
209 bootblock->size);
210
211 // Prepare header
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800212 if (header_offset + sizeof(header) > size) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800213 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800214 header_offset, sizeof(header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800215 return -1;
216 }
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800217 image->header = (struct cbfs_header *)(image->buffer.data + header_offset);
218 header.magic = CBFS_HEADER_MAGIC;
219 header.version = CBFS_HEADER_VERSION;
220 header.romsize = size;
221 header.bootblocksize = bootblock->size;
222 header.align = align;
223 header.offset = entries_offset;
224 header.architecture = myarch;
225 cbfs_put_header(image->header, &header);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800226
227 // Prepare entries
228 if (align_up(entries_offset, align) != entries_offset) {
229 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
230 entries_offset, align);
231 return -1;
232 }
Hung-Te Linb02c8732013-03-15 17:40:08 +0800233 entry_header_len = cbfs_calculate_file_header_size("");
234 if (entries_offset + entry_header_len > size) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800235 ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n",
Hung-Te Linb02c8732013-03-15 17:40:08 +0800236 entries_offset, entry_header_len, size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800237 return -1;
238 }
239 entry = (struct cbfs_file *)(image->buffer.data + entries_offset);
240 // To calculate available length, find
241 // e = min(bootblock, header, size) where e > entries_offset.
242 cbfs_len = size;
243 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
244 cbfs_len = bootblock_offset;
245 if (header_offset > entries_offset && header_offset < cbfs_len)
246 cbfs_len = header_offset;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800247 cbfs_len -= entries_offset + align + entry_header_len;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800248 cbfs_create_empty_entry(image, entry, cbfs_len, "");
249 LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len);
250 return 0;
251}
252
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700253int cbfs_image_from_file(struct cbfs_image *image, const char *filename)
254{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800255 if (buffer_from_file(&image->buffer, filename) != 0)
256 return -1;
257 DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
258 image->buffer.size);
259 image->header = cbfs_find_header(image->buffer.data,
260 image->buffer.size);
261 if (!image->header) {
262 ERROR("%s does not have CBFS master header.\n", filename);
263 cbfs_image_delete(image);
264 return -1;
265 }
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800266 cbfs_fix_legacy_size(image);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800267
268 return 0;
269}
270
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700271int cbfs_image_write_file(struct cbfs_image *image, const char *filename)
272{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800273 assert(image && image->buffer.data);
274 return buffer_write_file(&image->buffer, filename);
275}
276
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700277int cbfs_image_delete(struct cbfs_image *image)
278{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800279 buffer_delete(&image->buffer);
280 image->header = NULL;
281 return 0;
282}
283
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800284/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
285static int cbfs_add_entry_at(struct cbfs_image *image,
286 struct cbfs_file *entry,
287 uint32_t size,
288 const char *name,
289 uint32_t type,
290 const void *data,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700291 uint32_t content_offset)
292{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800293 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
294 uint32_t addr = cbfs_get_entry_addr(image, entry),
295 addr_next = cbfs_get_entry_addr(image, next);
296 uint32_t header_size = cbfs_calculate_file_header_size(name),
297 min_entry_size = cbfs_calculate_file_header_size("");
298 uint32_t len, target;
299 uint32_t align = ntohl(image->header->align);
300
301 target = content_offset - header_size;
302 if (target % align)
303 target -= target % align;
304 if (target < addr) {
305 ERROR("No space to hold cbfs_file header.");
306 return -1;
307 }
308
309 // Process buffer BEFORE content_offset.
310 if (target - addr > min_entry_size) {
311 DEBUG("|min|...|header|content|... <create new entry>\n");
312 len = target - addr - min_entry_size;
313 cbfs_create_empty_entry(image, entry, len, "");
314 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
315 entry = cbfs_find_next_entry(image, entry);
316 addr = cbfs_get_entry_addr(image, entry);
317 }
318
319 len = size + (content_offset - addr - header_size);
320 cbfs_create_empty_entry(image, entry, len, name);
321 if (len != size) {
322 DEBUG("|..|header|content|... <use offset to create entry>\n");
323 DEBUG("before: offset=0x%x, len=0x%x\n",
324 ntohl(entry->offset), ntohl(entry->len));
325 // TODO reset expanded name buffer to 0xFF.
326 entry->offset = htonl(ntohl(entry->offset) + (len - size));
327 entry->len = htonl(size);
328 DEBUG("after: offset=0x%x, len=0x%x\n",
329 ntohl(entry->offset), ntohl(entry->len));
330 }
331
332 // Ready to fill data into entry.
333 assert(ntohl(entry->len) == size);
334 entry->type = htonl(type);
335 DEBUG("content_offset: 0x%x, entry location: %x\n",
336 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
337 image->buffer.data));
338 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
339 content_offset);
340 memcpy(CBFS_SUBHEADER(entry), data, size);
341 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
342
343 // Process buffer AFTER entry.
344 entry = cbfs_find_next_entry(image, entry);
345 addr = cbfs_get_entry_addr(image, entry);
346 assert(addr < addr_next);
347
348 if (addr_next - addr < min_entry_size) {
349 DEBUG("No space after content to keep CBFS structure.\n");
350 return -1;
351 }
352
353 len = addr_next - addr - min_entry_size;
354 cbfs_create_empty_entry(image, entry, len, "");
355 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
356 return 0;
357}
358
359int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700360 const char *name, uint32_t type, uint32_t content_offset)
361{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800362 uint32_t entry_type;
363 uint32_t addr, addr_next;
364 struct cbfs_file *entry, *next;
365 uint32_t header_size, need_size, new_size;
366
367 header_size = cbfs_calculate_file_header_size(name);
368
369 need_size = header_size + buffer->size;
370 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
371 name, content_offset, header_size, buffer->size, need_size);
372
373 if (IS_TOP_ALIGNED_ADDRESS(content_offset)) {
374 // legacy cbfstool takes top-aligned address.
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800375 uint32_t theromsize = ntohl(image->header->romsize);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800376 INFO("Converting top-aligned address 0x%x to offset: 0x%x\n",
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800377 content_offset, content_offset + theromsize);
378 content_offset += theromsize;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800379 }
380
381 // Merge empty entries.
382 DEBUG("(trying to merge empty entries...)\n");
383 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
384
385 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800386 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800387 entry = cbfs_find_next_entry(image, entry)) {
388
389 entry_type = ntohl(entry->type);
390 if (entry_type != CBFS_COMPONENT_NULL)
391 continue;
392
393 addr = cbfs_get_entry_addr(image, entry);
394 next = cbfs_find_next_entry(image, entry);
395 addr_next = cbfs_get_entry_addr(image, next);
396
397 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
398 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600399
400 /* Will the file fit? Don't yet worry if we have space for a new
401 * "empty" entry. We take care of that later.
402 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800403 if (addr + need_size > addr_next)
404 continue;
405
406 // Can we simply put object here?
407 if (!content_offset || content_offset == addr + header_size) {
408 DEBUG("Filling new entry data (%zd bytes).\n",
409 buffer->size);
410 cbfs_create_empty_entry(image, entry, buffer->size,
411 name);
412 entry->type = htonl(type);
413 memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
414 if (verbose)
415 cbfs_print_entry_info(image, entry, stderr);
416
417 // setup new entry
Paul Menzel4159a802013-07-14 00:24:31 +0200418 DEBUG("Setting new empty entry.\n");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800419 entry = cbfs_find_next_entry(image, entry);
420 new_size = (cbfs_get_entry_addr(image, next) -
421 cbfs_get_entry_addr(image, entry));
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600422
423 /* Entry was added and no space for new "empty" entry */
424 if (new_size < cbfs_calculate_file_header_size("")) {
425 DEBUG("No need for new \"empty\" entry\n");
426 /* No need to increase the size of the just
427 * stored file to extend to next file. Alignment
428 * of next file takes care of this.
429 */
430 return 0;
431 }
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800432 new_size -= cbfs_calculate_file_header_size("");
433 DEBUG("new size: %d\n", new_size);
434 cbfs_create_empty_entry(image, entry, new_size, "");
435 if (verbose)
436 cbfs_print_entry_info(image, entry, stderr);
437 return 0;
438 }
439
440 // We need to put content here, and the case is really
441 // complicated...
442 assert(content_offset);
443 if (addr_next < content_offset) {
444 DEBUG("Not for specified offset yet");
445 continue;
446 } else if (addr > content_offset) {
447 DEBUG("Exceed specified content_offset.");
448 break;
449 } else if (addr + header_size > content_offset) {
450 ERROR("Not enough space for header.\n");
451 break;
452 } else if (content_offset + buffer->size > addr_next) {
453 ERROR("Not enough space for content.\n");
454 break;
455 }
456
457 // TODO there are more few tricky cases that we may
458 // want to fit by altering offset.
459 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
460 addr, addr_next - addr, content_offset);
461
462 if (cbfs_add_entry_at(image, entry, buffer->size, name, type,
463 buffer->data, content_offset) == 0) {
464 return 0;
465 }
466 break;
467 }
468
469 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
470 buffer->name, buffer->size, buffer->size / 1024, content_offset);
471 return -1;
472}
473
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700474struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
475{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800476 struct cbfs_file *entry;
477 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800478 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800479 entry = cbfs_find_next_entry(image, entry)) {
480 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
481 DEBUG("cbfs_get_entry: found %s\n", name);
482 return entry;
483 }
484 }
485 return NULL;
486}
487
488int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700489 const char *filename)
490{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800491 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
492 struct buffer buffer;
493 if (!entry) {
494 ERROR("File not found: %s\n", entry_name);
495 return -1;
496 }
497 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
498 entry_name, cbfs_get_entry_addr(image, entry),
499 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
500
501 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
502 WARN("Only 'raw' files are safe to extract.\n");
503 }
504
505 buffer.data = CBFS_SUBHEADER(entry);
506 buffer.size = ntohl(entry->len);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800507 buffer.name = (char *)"(cbfs_export_entry)";
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800508 if (buffer_write_file(&buffer, filename) != 0) {
509 ERROR("Failed to write %s into %s.\n",
510 entry_name, filename);
511 return -1;
512 }
513 INFO("Successfully dumped the file to: %s\n", filename);
514 return 0;
515}
516
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700517int cbfs_remove_entry(struct cbfs_image *image, const char *name)
518{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800519 struct cbfs_file *entry, *next;
520 size_t len;
521 entry = cbfs_get_entry(image, name);
522 if (!entry) {
523 ERROR("CBFS file %s not found.\n", name);
524 return -1;
525 }
526 next = cbfs_find_next_entry(image, entry);
527 assert(next);
528 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
529 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
530 entry->type = htonl(CBFS_COMPONENT_DELETED);
531 len = (cbfs_get_entry_addr(image, next) -
532 cbfs_get_entry_addr(image, entry));
533 entry->offset = htonl(cbfs_calculate_file_header_size(""));
534 entry->len = htonl(len - ntohl(entry->offset));
535 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
536 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
537 ntohl(entry->len));
538 return 0;
539}
540
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700541int cbfs_print_header_info(struct cbfs_image *image)
542{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800543 char *name = strdup(image->buffer.name);
544 assert(image && image->header);
545 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
546 "alignment: %d bytes\n\n",
547 basename(name),
548 image->buffer.size / 1024,
549 ntohl(image->header->bootblocksize),
550 ntohl(image->header->romsize),
551 ntohl(image->header->offset),
552 ntohl(image->header->align));
553 free(name);
554 return 0;
555}
556
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700557static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
558{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800559 fprintf(fp,
560 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
561 "length: %d/%d\n",
562 lookup_name_by_type(types_cbfs_compression,
563 stage->compression, "(unknown)"),
564 stage->entry,
565 stage->load,
566 stage->len,
567 stage->memlen);
568 return 0;
569}
570
571static int cbfs_print_payload_segment_info(struct cbfs_payload_segment *payload,
572 FILE *fp)
573{
574 switch(payload->type) {
575 case PAYLOAD_SEGMENT_CODE:
576 case PAYLOAD_SEGMENT_DATA:
577 fprintf(fp, " %s (%s compression, offset: 0x%x, "
578 "load: 0x%" PRIx64 ", length: %d/%d)\n",
579 (payload->type == PAYLOAD_SEGMENT_CODE ?
580 "code " : "data"),
581 lookup_name_by_type(types_cbfs_compression,
582 ntohl(payload->compression),
583 "(unknown)"),
584 ntohl(payload->offset),
585 ntohll(payload->load_addr),
586 ntohl(payload->len), ntohl(payload->mem_len));
587 break;
588
589 case PAYLOAD_SEGMENT_ENTRY:
590 fprintf(fp, " entry (0x%" PRIx64 ")\n",
591 ntohll(payload->load_addr));
592 break;
593
594 case PAYLOAD_SEGMENT_BSS:
595 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
596 "length 0x%x)\n",
597 ntohll(payload->load_addr),
598 ntohl(payload->len));
599 break;
600
601 case PAYLOAD_SEGMENT_PARAMS:
602 fprintf(fp, " parameters\n");
603 break;
604
605 default:
606 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
607 "load: 0x%" PRIx64 ", length: %d/%d\n",
608 payload->type,
609 lookup_name_by_type(types_cbfs_compression,
610 payload->compression,
611 "(unknown)"),
612 ntohl(payload->offset),
613 ntohll(payload->load_addr),
614 ntohl(payload->len),
615 ntohl(payload->mem_len));
616 break;
617 }
618 return 0;
619}
620
621int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700622 void *arg)
623{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800624 const char *name = CBFS_NAME(entry);
625 struct cbfs_payload_segment *payload;
626 FILE *fp = (FILE *)arg;
627
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800628 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800629 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
630 cbfs_get_entry_addr(image, entry));
631 return -1;
632 }
633 if (!fp)
634 fp = stdout;
635
636 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
637 *name ? name : "(empty)",
638 cbfs_get_entry_addr(image, entry),
639 get_cbfs_entry_type_name(ntohl(entry->type)),
640 ntohl(entry->len));
641
642 if (!verbose)
643 return 0;
644
645 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
646 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
647 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
648 ntohl(entry->len));
649
650 /* note the components of the subheader may be in host order ... */
651 switch (ntohl(entry->type)) {
652 case CBFS_COMPONENT_STAGE:
653 cbfs_print_stage_info((struct cbfs_stage *)
654 CBFS_SUBHEADER(entry), fp);
655 break;
656
657 case CBFS_COMPONENT_PAYLOAD:
658 payload = (struct cbfs_payload_segment *)
659 CBFS_SUBHEADER(entry);
660 while (payload) {
661 cbfs_print_payload_segment_info(payload, fp);
662 if (payload->type == PAYLOAD_SEGMENT_ENTRY)
663 break;
664 else
665 payload ++;
666 }
667 break;
668 default:
669 break;
670 }
671 return 0;
672}
673
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700674int cbfs_print_directory(struct cbfs_image *image)
675{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800676 cbfs_print_header_info(image);
677 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
678 cbfs_walk(image, cbfs_print_entry_info, NULL);
679 return 0;
680}
681
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800682int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700683 void *arg)
684{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800685 struct cbfs_file *next;
686 uint32_t type, addr, last_addr;
687
688 type = ntohl(entry->type);
689 if (type == CBFS_COMPONENT_DELETED) {
690 // Ready to be recycled.
691 type = CBFS_COMPONENT_NULL;
692 entry->type = htonl(type);
693 }
694 if (type != CBFS_COMPONENT_NULL)
695 return 0;
696
697 next = cbfs_find_next_entry(image, entry);
698
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800699 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800700 type = ntohl(next->type);
701 if (type == CBFS_COMPONENT_DELETED) {
702 type = CBFS_COMPONENT_NULL;
703 next->type = htonl(type);
704 }
705 if (type != CBFS_COMPONENT_NULL)
706 return 0;
707
708 addr = cbfs_get_entry_addr(image, entry);
709 last_addr = cbfs_get_entry_addr(
710 image, cbfs_find_next_entry(image, next));
711
712 // Now, we find two deleted/empty entries; try to merge now.
713 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
714 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
715 cbfs_get_entry_addr(image, next), ntohl(next->len));
716 cbfs_create_empty_entry(image, entry,
717 (last_addr - addr -
718 cbfs_calculate_file_header_size("")),
719 "");
720 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
721 next = cbfs_find_next_entry(image, entry);
722 }
723 return 0;
724}
725
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800726int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700727 void *arg)
728{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800729 int count = 0;
730 struct cbfs_file *entry;
731 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800732 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800733 entry = cbfs_find_next_entry(image, entry)) {
734 count ++;
735 if (callback(image, entry, arg) != 0)
736 break;
737 }
738 return count;
739}
740
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700741struct cbfs_header *cbfs_find_header(char *data, size_t size)
742{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800743 size_t offset;
744 int found = 0;
745 uint32_t x86sig;
746 struct cbfs_header *header, *result = NULL;
747
748 // Try x86 style (check signature in bottom) header first.
749 x86sig = *(uint32_t *)(data + size - sizeof(uint32_t));
750 offset = (x86sig + (uint32_t)size);
751 DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset);
752 if (offset >= size - sizeof(*header) ||
753 ntohl(((struct cbfs_header *)(data + offset))->magic) !=
754 CBFS_HEADER_MAGIC)
755 offset = 0;
756
757 for (; offset + sizeof(*header) < size; offset++) {
758 header = (struct cbfs_header *)(data + offset);
759 if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC))
760 continue;
761 if (ntohl(header->version) != CBFS_HEADER_VERSION1 &&
762 ntohl(header->version) != CBFS_HEADER_VERSION2) {
763 // Probably not a real CBFS header?
764 continue;
765 }
766 found++;
767 result = header;
768 }
769 if (found > 1) {
770 ERROR("multiple (%d) CBFS headers found!\n",
771 found);
772 result = NULL;
773 }
774 return result;
775}
776
777
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700778struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
779{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800780 assert(image && image->header);
781 return (struct cbfs_file *)(image->buffer.data +
782 ntohl(image->header->offset));
783}
784
785struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700786 struct cbfs_file *entry)
787{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800788 uint32_t addr = cbfs_get_entry_addr(image, entry);
789 int align = ntohl(image->header->align);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800790 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +0800791 addr += ntohl(entry->offset) + ntohl(entry->len);
792 addr = align_up(addr, align);
793 return (struct cbfs_file *)(image->buffer.data + addr);
794}
795
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700796uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
797{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800798 assert(image && image->buffer.data && entry);
799 return (int32_t)((char *)entry - image->buffer.data);
800}
801
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700802int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
803{
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800804 return (entry &&
805 (char *)entry >= image->buffer.data &&
806 (char *)entry + sizeof(entry->magic) <
807 image->buffer.data + image->buffer.size &&
808 memcmp(entry->magic, CBFS_FILE_MAGIC,
809 sizeof(entry->magic)) == 0);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800810}
811
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800812int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700813 size_t len, const char *name)
814{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800815 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
816 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
817 entry->type = htonl(CBFS_COMPONENT_NULL);
818 entry->len = htonl(len);
819 entry->checksum = 0; // TODO Build a checksum algorithm.
820 entry->offset = htonl(cbfs_calculate_file_header_size(name));
821 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
822 strcpy(CBFS_NAME(entry), name);
823 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
824 return 0;
825}
826
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700827/* Finds a place to hold whole data in same memory page. */
828static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
829{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800830 if (!page)
831 return 1;
832 return (start / page) == (start + size - 1) / page;
833}
834
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800835/* Tests if data can fit in a range by given offset:
836 * start ->| header_len | offset (+ size) |<- end
837 */
838static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700839 uint32_t offset, uint32_t size)
840{
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800841 return (offset >= start + header_len && offset + size <= end);
842}
843
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800844int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700845 uint32_t size, uint32_t page_size, uint32_t align)
846{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800847 struct cbfs_file *entry;
848 size_t need_len;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800849 uint32_t addr, addr_next, addr2, addr3, offset, header_len;
850
851 /* Default values: allow fitting anywhere in ROM. */
852 if (!page_size)
853 page_size = ntohl(image->header->romsize);
854 if (!align)
855 align = 1;
856
857 if (size > page_size)
858 ERROR("Input file size (%d) greater than page size (%d).\n",
859 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800860
861 if (page_size % ntohl(image->header->align))
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800862 WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
863 __func__, page_size, ntohl(image->header->align));
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800864
865 /* TODO Old cbfstool always assume input is a stage file (and adding
866 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800867 * (type) param in future. For right now, we assume cbfs_stage is the
868 * largest structure and add it into header size. */
869 assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800870 header_len = (cbfs_calculate_file_header_size(name) +
871 sizeof(struct cbfs_stage));
872 need_len = header_len + size;
873
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800874 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800875 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
876
877 /* Three cases of content location on memory page:
878 * case 1.
879 * | PAGE 1 | PAGE 2 |
880 * | <header><content>| Fit. Return start of content.
881 *
882 * case 2.
883 * | PAGE 1 | PAGE 2 |
884 * | <header><content> | Fits when we shift content to align
885 * shift-> | <header>|<content> | at starting of PAGE 2.
886 *
887 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800888 * | PAGE 1 | PAGE 2 | PAGE 3 |
889 * | <header>< content > | Can't fit. If we shift content to
890 * |trial-> <header>< content > | PAGE 2, header can't fit in free
891 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800892 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800893 * The returned address can be then used as "base-address" (-b) in add-*
894 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
895 * For stage targets, the address is also used to re-link stage before
896 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800897 */
898 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800899 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800900 entry = cbfs_find_next_entry(image, entry)) {
901
902 uint32_t type = ntohl(entry->type);
903 if (type != CBFS_COMPONENT_NULL)
904 continue;
905
906 addr = cbfs_get_entry_addr(image, entry);
907 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
908 image, entry));
909 if (addr_next - addr < need_len)
910 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800911
912 offset = align_up(addr + header_len, align);
913 if (is_in_same_page(offset, size, page_size) &&
914 is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800915 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800916 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800917 }
918
919 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800920 offset = align_up(addr2, align);
921 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800922 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800923 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800924 }
925
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800926 /* Assume page_size >= header_len so adding one page will
927 * definitely provide the space for header. */
928 assert(page_size >= header_len);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800929 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800930 offset = align_up(addr3, align);
931 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800932 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800933 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800934 }
935 }
936 return -1;
937}