blob: 8e0e9a48ab305a4337964c12e364bab1b5bd94e2 [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>
Patrick Georgicccc9d42015-04-28 13:09:36 +020022#include <stddef.h>
Hung-Te Lineab2c812013-01-29 01:56:17 +080023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Sol Boucher0e539312015-03-05 15:38:03 -080026#include <strings.h>
Hung-Te Lineab2c812013-01-29 01:56:17 +080027
28#include "common.h"
29#include "cbfs_image.h"
30
Sol Boucher636cc852015-04-03 09:13:04 -070031/* Even though the file-adding functions---cbfs_add_entry() and
32 * cbfs_add_entry_at()---perform their sizing checks against the beginning of
33 * the subsequent section rather than a stable recorded value such as an empty
34 * file header's len field, it's possible to prove two interesting properties
35 * about their behavior:
36 * - Placing a new file within an empty entry located below an existing file
37 * entry will never leave an aligned flash address containing neither the
38 * beginning of a file header nor part of a file.
39 * - Placing a new file in an empty entry at the very end of the image such
40 * that it fits, but leaves no room for a final header, is guaranteed not to
41 * change the total amount of space for entries, even if that new file is
42 * later removed from the CBFS.
43 * These properties are somewhat nonobvious from the implementation, so the
44 * reader is encouraged to blame this comment and examine the full proofs
45 * in the commit message before making significant changes that would risk
46 * removing said guarantees.
47 */
48
Hung-Te Lineab2c812013-01-29 01:56:17 +080049/* The file name align is not defined in CBFS spec -- only a preference by
50 * (old) cbfstool. */
51#define CBFS_FILENAME_ALIGN (16)
52
53/* To make CBFS more friendly to ROM, fill -1 (0xFF) instead of zero. */
54#define CBFS_CONTENT_DEFAULT_VALUE (-1)
55
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080056/* Type and format */
57
58struct typedesc_t {
59 uint32_t type;
60 const char *name;
61};
62
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070063static const struct typedesc_t types_cbfs_entry[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080064 {CBFS_COMPONENT_STAGE, "stage"},
65 {CBFS_COMPONENT_PAYLOAD, "payload"},
66 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
67 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
68 {CBFS_COMPONENT_RAW, "raw"},
69 {CBFS_COMPONENT_VSA, "vsa"},
70 {CBFS_COMPONENT_MBI, "mbi"},
71 {CBFS_COMPONENT_MICROCODE, "microcode"},
Martin Rothdde307c2015-03-24 15:54:20 -060072 {CBFS_COMPONENT_FSP, "fsp"},
73 {CBFS_COMPONENT_MRC, "mrc"},
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080074 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos_default"},
75 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos_layout"},
Martin Rothdde307c2015-03-24 15:54:20 -060076 {CBFS_COMPONENT_SPD, "spd"},
77 {CBFS_COMPONENT_MRC_CACHE, "mrc_cache"},
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080078 {CBFS_COMPONENT_DELETED, "deleted"},
79 {CBFS_COMPONENT_NULL, "null"},
80 {0, NULL},
81};
82
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070083static const struct typedesc_t types_cbfs_compression[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080084 {CBFS_COMPRESS_NONE, "none"},
85 {CBFS_COMPRESS_LZMA, "LZMA"},
86 {0, NULL},
87};
88
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080089static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070090 const char *default_value)
91{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080092 int i;
93 for (i = 0; desc[i].name; i++)
94 if (desc[i].type == type)
95 return desc[i].name;
96 return default_value;
97}
98
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010099static const char *get_cbfs_entry_type_name(uint32_t type)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700100{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800101 return lookup_name_by_type(types_cbfs_entry, type, "(unknown)");
102}
103
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800104/* CBFS image */
105
Sol Boucher0e539312015-03-05 15:38:03 -0800106static size_t cbfs_calculate_file_header_size(const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700107{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800108 return (sizeof(struct cbfs_file) +
109 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
110}
111
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600112static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700113{
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800114 // A bug in old cbfstool may produce extra few bytes (by alignment) and
115 // cause cbfstool to overwrite things after free space -- which is
116 // usually CBFS header on x86. We need to workaround that.
117
118 struct cbfs_file *entry, *first = NULL, *last = NULL;
119 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800120 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800121 entry = cbfs_find_next_entry(image, entry)) {
122 last = entry;
123 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600124 if ((char *)first < (char *)hdr_loc &&
125 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800126 WARN("CBFS image was created with old cbfstool with size bug. "
127 "Fixing size in last entry...\n");
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600128 last->len = htonl(ntohl(last->len) - image->header->align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800129 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
130 cbfs_get_entry_addr(image, entry),
131 cbfs_get_entry_addr(image,
132 cbfs_find_next_entry(image, last)));
133 }
134 return 0;
135}
136
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800137void cbfs_put_header(void *dest, const struct cbfs_header *header)
138{
139 struct buffer outheader;
140
141 outheader.data = dest;
142 outheader.size = 0;
143
144 xdr_be.put32(&outheader, header->magic);
145 xdr_be.put32(&outheader, header->version);
146 xdr_be.put32(&outheader, header->romsize);
147 xdr_be.put32(&outheader, header->bootblocksize);
148 xdr_be.put32(&outheader, header->align);
149 xdr_be.put32(&outheader, header->offset);
150 xdr_be.put32(&outheader, header->architecture);
151}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600152
Hung-Te Lin0780d672014-05-16 10:14:05 +0800153static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
154 struct cbfs_payload_segment *input)
155{
156 struct buffer seg = {
157 .data = (void *)input,
158 .size = sizeof(*input),
159 };
160 output->type = xdr_be.get32(&seg);
161 output->compression = xdr_be.get32(&seg);
162 output->offset = xdr_be.get32(&seg);
163 output->load_addr = xdr_be.get64(&seg);
164 output->len = xdr_be.get32(&seg);
165 output->mem_len = xdr_be.get32(&seg);
166 assert(seg.size == 0);
167}
168
Sol Boucher0e539312015-03-05 15:38:03 -0800169void cbfs_get_header(struct cbfs_header *header, void *src)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600170{
171 struct buffer outheader;
172
Sol Boucher0e539312015-03-05 15:38:03 -0800173 outheader.data = src; /* We're not modifying the data */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600174 outheader.size = 0;
175
176 header->magic = xdr_be.get32(&outheader);
177 header->version = xdr_be.get32(&outheader);
178 header->romsize = xdr_be.get32(&outheader);
179 header->bootblocksize = xdr_be.get32(&outheader);
180 header->align = xdr_be.get32(&outheader);
181 header->offset = xdr_be.get32(&outheader);
182 header->architecture = xdr_be.get32(&outheader);
183}
184
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800185int cbfs_image_create(struct cbfs_image *image,
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100186 uint32_t architecture,
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800187 size_t size,
188 uint32_t align,
189 struct buffer *bootblock,
Sol Boucher0e539312015-03-05 15:38:03 -0800190 uint32_t bootblock_offset,
191 uint32_t header_offset,
192 uint32_t entries_offset)
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800193{
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800194 struct cbfs_header header;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800195 struct cbfs_file *entry;
Julius Wernerefcee762014-11-10 13:14:24 -0800196 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800197 uint32_t cbfs_len;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800198 size_t entry_header_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600199 void *header_loc;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800200
201 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
202 "header=0x%x+0x%zx, entries_offset=0x%x\n",
203 bootblock_offset, bootblock->size,
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800204 header_offset, sizeof(header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800205
Sol Boucher636cc852015-04-03 09:13:04 -0700206 // This attribute must be given in order to prove that this module
207 // correctly preserves certain CBFS properties. See the block comment
208 // near the top of this file (and the associated commit message).
209 size_t empty_header_len = cbfs_calculate_file_header_size("");
210 if (align < empty_header_len) {
211 ERROR("CBFS must be aligned to at least %zu bytes\n",
212 empty_header_len);
213 return -1;
214 }
215
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800216 if (buffer_create(&image->buffer, size, "(new)") != 0)
217 return -1;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600218 if ((image->header = malloc(sizeof(*image->header))) == NULL)
219 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800220 memset(image->buffer.data, CBFS_CONTENT_DEFAULT_VALUE, size);
221
222 // Adjust legcay top-aligned address to ROM offset.
223 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800224 entries_offset = size + (int32_t)entries_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800225 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800226 bootblock_offset = size + (int32_t)bootblock_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800227 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800228 header_offset = size + (int32_t)header_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800229
230 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
231 "header=0x%x, entries_offset=0x%x\n",
232 bootblock_offset, header_offset, entries_offset);
233
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800234 // Prepare bootblock
235 if (bootblock_offset + bootblock->size > size) {
236 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
237 bootblock_offset, bootblock->size, size);
238 return -1;
239 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800240 if (entries_offset > bootblock_offset &&
241 entries_offset < bootblock->size) {
242 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
243 bootblock_offset, bootblock->size, entries_offset);
244 return -1;
245 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800246 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
247 bootblock->size);
248
249 // Prepare header
Julius Wernerefcee762014-11-10 13:14:24 -0800250 if (header_offset + sizeof(header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800251 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800252 header_offset, sizeof(header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800253 return -1;
254 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600255 image->header->magic = CBFS_HEADER_MAGIC;
256 image->header->version = CBFS_HEADER_VERSION;
257 image->header->romsize = size;
258 image->header->bootblocksize = bootblock->size;
259 image->header->align = align;
260 image->header->offset = entries_offset;
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100261 image->header->architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600262
263 header_loc = (image->buffer.data + header_offset);
264 cbfs_put_header(header_loc, image->header);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800265
Julius Wernerefcee762014-11-10 13:14:24 -0800266 // The last 4 byte of the image contain the relative offset from the end
267 // of the image to the master header as a 32-bit signed integer. x86
268 // relies on this also being its (memory-mapped, top-aligned) absolute
269 // 32-bit address by virtue of how two's complement numbers work.
270 assert(size % sizeof(int32_t) == 0);
271 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
272 *rel_offset = header_offset - size;
273
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800274 // Prepare entries
275 if (align_up(entries_offset, align) != entries_offset) {
276 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
277 entries_offset, align);
278 return -1;
279 }
Hung-Te Linb02c8732013-03-15 17:40:08 +0800280 entry_header_len = cbfs_calculate_file_header_size("");
281 if (entries_offset + entry_header_len > size) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800282 ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n",
Hung-Te Linb02c8732013-03-15 17:40:08 +0800283 entries_offset, entry_header_len, size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800284 return -1;
285 }
286 entry = (struct cbfs_file *)(image->buffer.data + entries_offset);
287 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800288 // e = min(bootblock, header, rel_offset) where e > entries_offset.
289 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800290 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
291 cbfs_len = bootblock_offset;
292 if (header_offset > entries_offset && header_offset < cbfs_len)
293 cbfs_len = header_offset;
Sol Boucher636cc852015-04-03 09:13:04 -0700294 // This alignment is necessary in order to prove that this module
295 // correctly preserves certain CBFS properties. See the block comment
296 // near the top of this file (and the associated commit message).
297 cbfs_len -= cbfs_len % align;
298 cbfs_len -= entries_offset + entry_header_len;
Vadim Bendebury45e59972014-12-23 15:59:57 -0800299 cbfs_create_empty_entry(entry, cbfs_len, "");
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800300 LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len);
301 return 0;
302}
303
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800304int cbfs_image_from_file(struct cbfs_image *image,
305 const char *filename, uint32_t offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700306{
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600307 void *header_loc;
308
Hung-Te Lineab2c812013-01-29 01:56:17 +0800309 if (buffer_from_file(&image->buffer, filename) != 0)
310 return -1;
311 DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
312 image->buffer.size);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800313 header_loc = cbfs_find_header(image->buffer.data,
314 image->buffer.size,
315 offset);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600316 if (!header_loc) {
Hung-Te Lineab2c812013-01-29 01:56:17 +0800317 ERROR("%s does not have CBFS master header.\n", filename);
318 cbfs_image_delete(image);
319 return -1;
320 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600321
322 if ((image->header = malloc(sizeof(*image->header))) == NULL)
323 return -1;
324
325 cbfs_get_header(image->header, header_loc);
326 cbfs_fix_legacy_size(image, header_loc);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800327
328 return 0;
329}
330
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800331int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
332 size_t copy_size)
333{
334 struct cbfs_file *src_entry, *dst_entry;
335 struct cbfs_header *copy_header;
336 size_t align, entry_offset;
337 ssize_t last_entry_size;
338
339 size_t header_offset, header_end;
340 size_t cbfs_offset, cbfs_end;
341 size_t copy_end = copy_offset + copy_size;
342
343 align = htonl(image->header->align);
344
345 header_offset = (char *)image->header - image->buffer.data;
346 header_end = header_offset + sizeof(image->header);
347
348 cbfs_offset = htonl(image->header->offset);
349 cbfs_end = htonl(image->header->romsize);
350
351 if (copy_end > image->buffer.size) {
352 ERROR("Copy offset out of range: [%zx:%zx)\n",
353 copy_offset, copy_end);
354 return 1;
355 }
356
357 /* Range check requested copy region with header and source cbfs. */
358 if ((copy_offset >= header_offset && copy_offset < header_end) ||
359 (copy_end >= header_offset && copy_end <= header_end)) {
360 ERROR("New image would overlap old header.\n");
361 }
362
363 if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) ||
364 (copy_end >= cbfs_offset && copy_end <= cbfs_end)) {
365 ERROR("New image would overlap old one.\n");
366 return 1;
367 }
368
369 /* This will work, let's create a copy. */
370 copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset);
371 *copy_header = *image->header;
372
373 copy_header->bootblocksize = 0;
374 /* Romsize is a misnomer. It's the absolute limit of cbfs content.*/
375 copy_header->romsize = htonl(copy_end);
376 entry_offset = align_up(copy_offset + sizeof(*copy_header), align);
377 copy_header->offset = htonl(entry_offset);
378 dst_entry = (struct cbfs_file *)(image->buffer.data + entry_offset);
379
380 /* Copy non-empty files */
381 for (src_entry = cbfs_find_first_entry(image);
382 src_entry && cbfs_is_valid_entry(image, src_entry);
383 src_entry = cbfs_find_next_entry(image, src_entry)) {
384 size_t entry_size;
385
386 if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) ||
387 (src_entry->type == htonl(CBFS_COMPONENT_DELETED)))
388 continue;
389
390 entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
391 memcpy(dst_entry, src_entry, entry_size);
392 dst_entry = (struct cbfs_file *)(
393 (uintptr_t)dst_entry + align_up(entry_size, align));
394
Sol Boucher0e539312015-03-05 15:38:03 -0800395 if ((size_t)((char *)dst_entry - image->buffer.data) >=
396 copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800397 ERROR("Ran out of room in copy region.\n");
398 return 1;
399 }
400 }
401
402 /* Last entry size is all the room above it. */
403 last_entry_size = copy_end - ((char *)dst_entry - image->buffer.data)
404 - cbfs_calculate_file_header_size("");
405
406 if (last_entry_size < 0)
407 WARN("No room to create the last entry!\n")
408 else
Vadim Bendebury45e59972014-12-23 15:59:57 -0800409 cbfs_create_empty_entry(dst_entry, last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800410
411 return 0;
412}
413
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700414int cbfs_image_write_file(struct cbfs_image *image, const char *filename)
415{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800416 assert(image && image->buffer.data);
417 return buffer_write_file(&image->buffer, filename);
418}
419
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700420int cbfs_image_delete(struct cbfs_image *image)
421{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100422 if (image == NULL)
423 return 0;
424
Hung-Te Lineab2c812013-01-29 01:56:17 +0800425 buffer_delete(&image->buffer);
426 image->header = NULL;
427 return 0;
428}
429
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800430/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
431static int cbfs_add_entry_at(struct cbfs_image *image,
432 struct cbfs_file *entry,
433 uint32_t size,
434 const char *name,
435 uint32_t type,
436 const void *data,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700437 uint32_t content_offset)
438{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800439 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
440 uint32_t addr = cbfs_get_entry_addr(image, entry),
441 addr_next = cbfs_get_entry_addr(image, next);
442 uint32_t header_size = cbfs_calculate_file_header_size(name),
443 min_entry_size = cbfs_calculate_file_header_size("");
444 uint32_t len, target;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600445 uint32_t align = image->header->align;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800446
447 target = content_offset - header_size;
448 if (target % align)
449 target -= target % align;
450 if (target < addr) {
451 ERROR("No space to hold cbfs_file header.");
452 return -1;
453 }
454
455 // Process buffer BEFORE content_offset.
456 if (target - addr > min_entry_size) {
457 DEBUG("|min|...|header|content|... <create new entry>\n");
458 len = target - addr - min_entry_size;
Vadim Bendebury45e59972014-12-23 15:59:57 -0800459 cbfs_create_empty_entry(entry, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800460 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
461 entry = cbfs_find_next_entry(image, entry);
462 addr = cbfs_get_entry_addr(image, entry);
463 }
464
465 len = size + (content_offset - addr - header_size);
Vadim Bendebury45e59972014-12-23 15:59:57 -0800466 cbfs_create_empty_entry(entry, len, name);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800467 if (len != size) {
468 DEBUG("|..|header|content|... <use offset to create entry>\n");
469 DEBUG("before: offset=0x%x, len=0x%x\n",
470 ntohl(entry->offset), ntohl(entry->len));
471 // TODO reset expanded name buffer to 0xFF.
472 entry->offset = htonl(ntohl(entry->offset) + (len - size));
473 entry->len = htonl(size);
474 DEBUG("after: offset=0x%x, len=0x%x\n",
475 ntohl(entry->offset), ntohl(entry->len));
476 }
477
478 // Ready to fill data into entry.
479 assert(ntohl(entry->len) == size);
480 entry->type = htonl(type);
481 DEBUG("content_offset: 0x%x, entry location: %x\n",
482 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
483 image->buffer.data));
484 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
Patrick Georgicccc9d42015-04-28 13:09:36 +0200485 (ptrdiff_t)content_offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800486 memcpy(CBFS_SUBHEADER(entry), data, size);
487 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
488
489 // Process buffer AFTER entry.
490 entry = cbfs_find_next_entry(image, entry);
491 addr = cbfs_get_entry_addr(image, entry);
Sol Boucher05725652015-04-02 20:58:26 -0700492 if (addr == addr_next)
493 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800494
Sol Boucher05725652015-04-02 20:58:26 -0700495 assert(addr < addr_next);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800496 if (addr_next - addr < min_entry_size) {
Sol Boucher636cc852015-04-03 09:13:04 -0700497 DEBUG("No need for new \"empty\" entry\n");
498 /* No need to increase the size of the just
499 * stored file to extend to next file. Alignment
500 * of next file takes care of this.
501 */
502 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800503 }
504
505 len = addr_next - addr - min_entry_size;
Vadim Bendebury45e59972014-12-23 15:59:57 -0800506 cbfs_create_empty_entry(entry, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800507 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
508 return 0;
509}
510
511int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700512 const char *name, uint32_t type, uint32_t content_offset)
513{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800514 uint32_t entry_type;
515 uint32_t addr, addr_next;
516 struct cbfs_file *entry, *next;
517 uint32_t header_size, need_size, new_size;
518
519 header_size = cbfs_calculate_file_header_size(name);
520
521 need_size = header_size + buffer->size;
522 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
523 name, content_offset, header_size, buffer->size, need_size);
524
525 if (IS_TOP_ALIGNED_ADDRESS(content_offset)) {
526 // legacy cbfstool takes top-aligned address.
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600527 uint32_t theromsize = image->header->romsize;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800528 INFO("Converting top-aligned address 0x%x to offset: 0x%x\n",
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800529 content_offset, content_offset + theromsize);
Sol Boucher0e539312015-03-05 15:38:03 -0800530 content_offset = theromsize + (int32_t)content_offset;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800531 }
532
533 // Merge empty entries.
534 DEBUG("(trying to merge empty entries...)\n");
535 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
536
537 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800538 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800539 entry = cbfs_find_next_entry(image, entry)) {
540
541 entry_type = ntohl(entry->type);
542 if (entry_type != CBFS_COMPONENT_NULL)
543 continue;
544
545 addr = cbfs_get_entry_addr(image, entry);
546 next = cbfs_find_next_entry(image, entry);
547 addr_next = cbfs_get_entry_addr(image, next);
548
549 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
550 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600551
552 /* Will the file fit? Don't yet worry if we have space for a new
553 * "empty" entry. We take care of that later.
554 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800555 if (addr + need_size > addr_next)
556 continue;
557
558 // Can we simply put object here?
559 if (!content_offset || content_offset == addr + header_size) {
560 DEBUG("Filling new entry data (%zd bytes).\n",
561 buffer->size);
Vadim Bendebury45e59972014-12-23 15:59:57 -0800562 cbfs_create_empty_entry(entry, buffer->size, name);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800563 entry->type = htonl(type);
564 memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
565 if (verbose)
566 cbfs_print_entry_info(image, entry, stderr);
567
568 // setup new entry
Paul Menzel4159a802013-07-14 00:24:31 +0200569 DEBUG("Setting new empty entry.\n");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800570 entry = cbfs_find_next_entry(image, entry);
571 new_size = (cbfs_get_entry_addr(image, next) -
572 cbfs_get_entry_addr(image, entry));
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600573
574 /* Entry was added and no space for new "empty" entry */
575 if (new_size < cbfs_calculate_file_header_size("")) {
576 DEBUG("No need for new \"empty\" entry\n");
577 /* No need to increase the size of the just
578 * stored file to extend to next file. Alignment
579 * of next file takes care of this.
580 */
581 return 0;
582 }
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800583 new_size -= cbfs_calculate_file_header_size("");
584 DEBUG("new size: %d\n", new_size);
Vadim Bendebury45e59972014-12-23 15:59:57 -0800585 cbfs_create_empty_entry(entry, new_size, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800586 if (verbose)
587 cbfs_print_entry_info(image, entry, stderr);
588 return 0;
589 }
590
591 // We need to put content here, and the case is really
592 // complicated...
593 assert(content_offset);
594 if (addr_next < content_offset) {
595 DEBUG("Not for specified offset yet");
596 continue;
597 } else if (addr > content_offset) {
598 DEBUG("Exceed specified content_offset.");
599 break;
600 } else if (addr + header_size > content_offset) {
601 ERROR("Not enough space for header.\n");
602 break;
603 } else if (content_offset + buffer->size > addr_next) {
604 ERROR("Not enough space for content.\n");
605 break;
606 }
607
608 // TODO there are more few tricky cases that we may
609 // want to fit by altering offset.
610 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
611 addr, addr_next - addr, content_offset);
612
613 if (cbfs_add_entry_at(image, entry, buffer->size, name, type,
614 buffer->data, content_offset) == 0) {
615 return 0;
616 }
617 break;
618 }
619
620 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
621 buffer->name, buffer->size, buffer->size / 1024, content_offset);
622 return -1;
623}
624
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700625struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
626{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800627 struct cbfs_file *entry;
628 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800629 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800630 entry = cbfs_find_next_entry(image, entry)) {
631 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
632 DEBUG("cbfs_get_entry: found %s\n", name);
633 return entry;
634 }
635 }
636 return NULL;
637}
638
639int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700640 const char *filename)
641{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800642 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
643 struct buffer buffer;
644 if (!entry) {
645 ERROR("File not found: %s\n", entry_name);
646 return -1;
647 }
648 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
649 entry_name, cbfs_get_entry_addr(image, entry),
650 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
651
652 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
653 WARN("Only 'raw' files are safe to extract.\n");
654 }
655
656 buffer.data = CBFS_SUBHEADER(entry);
657 buffer.size = ntohl(entry->len);
Sol Boucher0e539312015-03-05 15:38:03 -0800658 buffer.name = strdup("(cbfs_export_entry)");
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800659 if (buffer_write_file(&buffer, filename) != 0) {
660 ERROR("Failed to write %s into %s.\n",
661 entry_name, filename);
Sol Boucher0e539312015-03-05 15:38:03 -0800662 free(buffer.name);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800663 return -1;
664 }
Sol Boucher0e539312015-03-05 15:38:03 -0800665 free(buffer.name);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800666 INFO("Successfully dumped the file to: %s\n", filename);
667 return 0;
668}
669
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700670int cbfs_remove_entry(struct cbfs_image *image, const char *name)
671{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800672 struct cbfs_file *entry, *next;
673 size_t len;
674 entry = cbfs_get_entry(image, name);
675 if (!entry) {
676 ERROR("CBFS file %s not found.\n", name);
677 return -1;
678 }
679 next = cbfs_find_next_entry(image, entry);
680 assert(next);
681 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
682 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
683 entry->type = htonl(CBFS_COMPONENT_DELETED);
684 len = (cbfs_get_entry_addr(image, next) -
685 cbfs_get_entry_addr(image, entry));
686 entry->offset = htonl(cbfs_calculate_file_header_size(""));
687 entry->len = htonl(len - ntohl(entry->offset));
688 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
689 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
690 ntohl(entry->len));
691 return 0;
692}
693
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700694int cbfs_print_header_info(struct cbfs_image *image)
695{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800696 char *name = strdup(image->buffer.name);
697 assert(image && image->header);
698 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800699 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800700 basename(name),
701 image->buffer.size / 1024,
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600702 image->header->bootblocksize,
703 image->header->romsize,
704 image->header->offset,
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800705 image->header->align,
706 arch_to_string(image->header->architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800707 free(name);
708 return 0;
709}
710
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700711static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
712{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800713 fprintf(fp,
714 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
715 "length: %d/%d\n",
716 lookup_name_by_type(types_cbfs_compression,
717 stage->compression, "(unknown)"),
718 stage->entry,
719 stage->load,
720 stage->len,
721 stage->memlen);
722 return 0;
723}
724
Hung-Te Lin0780d672014-05-16 10:14:05 +0800725static int cbfs_print_decoded_payload_segment_info(
726 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800727{
Hung-Te Lin0780d672014-05-16 10:14:05 +0800728 /* The input (seg) must be already decoded by
729 * cbfs_decode_payload_segment.
730 */
731 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800732 case PAYLOAD_SEGMENT_CODE:
733 case PAYLOAD_SEGMENT_DATA:
734 fprintf(fp, " %s (%s compression, offset: 0x%x, "
735 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800736 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800737 "code " : "data"),
738 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800739 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800740 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800741 seg->offset, seg->load_addr, seg->len,
742 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800743 break;
744
745 case PAYLOAD_SEGMENT_ENTRY:
746 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800747 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800748 break;
749
750 case PAYLOAD_SEGMENT_BSS:
751 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
752 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800753 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800754 break;
755
756 case PAYLOAD_SEGMENT_PARAMS:
757 fprintf(fp, " parameters\n");
758 break;
759
760 default:
761 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
762 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800763 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800764 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800765 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800766 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800767 seg->offset, seg->load_addr, seg->len,
768 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800769 break;
770 }
771 return 0;
772}
773
774int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700775 void *arg)
776{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800777 const char *name = CBFS_NAME(entry);
778 struct cbfs_payload_segment *payload;
779 FILE *fp = (FILE *)arg;
780
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800781 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800782 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
783 cbfs_get_entry_addr(image, entry));
784 return -1;
785 }
786 if (!fp)
787 fp = stdout;
788
789 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
790 *name ? name : "(empty)",
791 cbfs_get_entry_addr(image, entry),
792 get_cbfs_entry_type_name(ntohl(entry->type)),
793 ntohl(entry->len));
794
795 if (!verbose)
796 return 0;
797
798 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
799 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
800 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
801 ntohl(entry->len));
802
803 /* note the components of the subheader may be in host order ... */
804 switch (ntohl(entry->type)) {
805 case CBFS_COMPONENT_STAGE:
806 cbfs_print_stage_info((struct cbfs_stage *)
807 CBFS_SUBHEADER(entry), fp);
808 break;
809
810 case CBFS_COMPONENT_PAYLOAD:
811 payload = (struct cbfs_payload_segment *)
812 CBFS_SUBHEADER(entry);
813 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +0800814 struct cbfs_payload_segment seg;
815 cbfs_decode_payload_segment(&seg, payload);
816 cbfs_print_decoded_payload_segment_info(
817 &seg, fp);
818 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800819 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +0800820 else
Aaron Durbinca630272014-08-05 10:48:20 -0500821 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800822 }
823 break;
824 default:
825 break;
826 }
827 return 0;
828}
829
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700830int cbfs_print_directory(struct cbfs_image *image)
831{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800832 cbfs_print_header_info(image);
833 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
834 cbfs_walk(image, cbfs_print_entry_info, NULL);
835 return 0;
836}
837
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800838int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -0800839 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700840{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800841 struct cbfs_file *next;
842 uint32_t type, addr, last_addr;
843
844 type = ntohl(entry->type);
845 if (type == CBFS_COMPONENT_DELETED) {
846 // Ready to be recycled.
847 type = CBFS_COMPONENT_NULL;
848 entry->type = htonl(type);
849 }
850 if (type != CBFS_COMPONENT_NULL)
851 return 0;
852
853 next = cbfs_find_next_entry(image, entry);
854
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800855 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800856 type = ntohl(next->type);
857 if (type == CBFS_COMPONENT_DELETED) {
858 type = CBFS_COMPONENT_NULL;
859 next->type = htonl(type);
860 }
861 if (type != CBFS_COMPONENT_NULL)
862 return 0;
863
864 addr = cbfs_get_entry_addr(image, entry);
865 last_addr = cbfs_get_entry_addr(
866 image, cbfs_find_next_entry(image, next));
867
868 // Now, we find two deleted/empty entries; try to merge now.
869 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
870 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
871 cbfs_get_entry_addr(image, next), ntohl(next->len));
Vadim Bendebury45e59972014-12-23 15:59:57 -0800872 cbfs_create_empty_entry(entry,
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800873 (last_addr - addr -
874 cbfs_calculate_file_header_size("")),
875 "");
876 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
877 next = cbfs_find_next_entry(image, entry);
878 }
879 return 0;
880}
881
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800882int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700883 void *arg)
884{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800885 int count = 0;
886 struct cbfs_file *entry;
887 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800888 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800889 entry = cbfs_find_next_entry(image, entry)) {
890 count ++;
891 if (callback(image, entry, arg) != 0)
892 break;
893 }
894 return count;
895}
896
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800897static int cbfs_header_valid(struct cbfs_header *header, size_t size)
898{
899 if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
900 ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
901 (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
902 (ntohl(header->romsize) <= size) &&
903 (ntohl(header->offset) < ntohl(header->romsize)))
904 return 1;
905 return 0;
906}
907
908struct cbfs_header *cbfs_find_header(char *data, size_t size,
909 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700910{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800911 size_t offset;
912 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800913 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800914 struct cbfs_header *header, *result = NULL;
915
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800916 if (forced_offset < (size - sizeof(struct cbfs_header))) {
917 /* Check if the forced header is valid. */
918 header = (struct cbfs_header *)(data + forced_offset);
919 if (cbfs_header_valid(header, size))
920 return header;
921 return NULL;
922 }
923
Julius Wernerefcee762014-11-10 13:14:24 -0800924 // Try finding relative offset of master header at end of file first.
925 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
926 offset = size + rel_offset;
927 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
928 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800929
Hung-Te Lineab2c812013-01-29 01:56:17 +0800930 if (offset >= size - sizeof(*header) ||
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800931 !cbfs_header_valid((struct cbfs_header *)(data + offset), size)) {
Julius Wernerefcee762014-11-10 13:14:24 -0800932 // Some use cases append non-CBFS data to the end of the ROM.
933 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +0800934 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800935 }
Hung-Te Lineab2c812013-01-29 01:56:17 +0800936
937 for (; offset + sizeof(*header) < size; offset++) {
938 header = (struct cbfs_header *)(data + offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800939 if (!cbfs_header_valid(header, size))
Hung-Te Lineab2c812013-01-29 01:56:17 +0800940 continue;
Julius Wernerefcee762014-11-10 13:14:24 -0800941 if (!found++)
942 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800943 }
Julius Wernerefcee762014-11-10 13:14:24 -0800944 if (found > 1)
945 // Top-aligned images usually have a working relative offset
946 // field, so this is more likely to happen on bottom-aligned
947 // ones (where the first header is the "outermost" one)
948 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +0800949 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800950 return result;
951}
952
953
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700954struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
955{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800956 assert(image && image->header);
957 return (struct cbfs_file *)(image->buffer.data +
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600958 image->header->offset);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800959}
960
961struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700962 struct cbfs_file *entry)
963{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800964 uint32_t addr = cbfs_get_entry_addr(image, entry);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600965 int align = image->header->align;
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800966 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +0800967 addr += ntohl(entry->offset) + ntohl(entry->len);
968 addr = align_up(addr, align);
969 return (struct cbfs_file *)(image->buffer.data + addr);
970}
971
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700972uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
973{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800974 assert(image && image->buffer.data && entry);
975 return (int32_t)((char *)entry - image->buffer.data);
976}
977
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700978int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
979{
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800980 return (entry &&
981 (char *)entry >= image->buffer.data &&
982 (char *)entry + sizeof(entry->magic) <
983 image->buffer.data + image->buffer.size &&
984 memcmp(entry->magic, CBFS_FILE_MAGIC,
985 sizeof(entry->magic)) == 0);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800986}
987
Vadim Bendebury45e59972014-12-23 15:59:57 -0800988int cbfs_create_empty_entry(struct cbfs_file *entry,
989 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700990{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800991 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
992 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
993 entry->type = htonl(CBFS_COMPONENT_NULL);
994 entry->len = htonl(len);
995 entry->checksum = 0; // TODO Build a checksum algorithm.
996 entry->offset = htonl(cbfs_calculate_file_header_size(name));
997 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
998 strcpy(CBFS_NAME(entry), name);
999 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1000 return 0;
1001}
1002
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001003/* Finds a place to hold whole data in same memory page. */
1004static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1005{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001006 if (!page)
1007 return 1;
1008 return (start / page) == (start + size - 1) / page;
1009}
1010
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001011/* Tests if data can fit in a range by given offset:
1012 * start ->| header_len | offset (+ size) |<- end
1013 */
1014static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001015 uint32_t offset, uint32_t size)
1016{
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001017 return (offset >= start + header_len && offset + size <= end);
1018}
1019
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001020int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001021 uint32_t size, uint32_t page_size, uint32_t align)
1022{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001023 struct cbfs_file *entry;
1024 size_t need_len;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001025 uint32_t addr, addr_next, addr2, addr3, offset, header_len;
1026
1027 /* Default values: allow fitting anywhere in ROM. */
1028 if (!page_size)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -06001029 page_size = image->header->romsize;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001030 if (!align)
1031 align = 1;
1032
1033 if (size > page_size)
1034 ERROR("Input file size (%d) greater than page size (%d).\n",
1035 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001036
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -06001037 if (page_size % image->header->align)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001038 WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -06001039 __func__, page_size, image->header->align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001040
1041 /* TODO Old cbfstool always assume input is a stage file (and adding
1042 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001043 * (type) param in future. For right now, we assume cbfs_stage is the
1044 * largest structure and add it into header size. */
1045 assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001046 header_len = (cbfs_calculate_file_header_size(name) +
1047 sizeof(struct cbfs_stage));
1048 need_len = header_len + size;
1049
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001050 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001051 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
1052
1053 /* Three cases of content location on memory page:
1054 * case 1.
1055 * | PAGE 1 | PAGE 2 |
1056 * | <header><content>| Fit. Return start of content.
1057 *
1058 * case 2.
1059 * | PAGE 1 | PAGE 2 |
1060 * | <header><content> | Fits when we shift content to align
1061 * shift-> | <header>|<content> | at starting of PAGE 2.
1062 *
1063 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001064 * | PAGE 1 | PAGE 2 | PAGE 3 |
1065 * | <header>< content > | Can't fit. If we shift content to
1066 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1067 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001068 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001069 * The returned address can be then used as "base-address" (-b) in add-*
1070 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1071 * For stage targets, the address is also used to re-link stage before
1072 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001073 */
1074 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001075 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001076 entry = cbfs_find_next_entry(image, entry)) {
1077
1078 uint32_t type = ntohl(entry->type);
1079 if (type != CBFS_COMPONENT_NULL)
1080 continue;
1081
1082 addr = cbfs_get_entry_addr(image, entry);
1083 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
1084 image, entry));
1085 if (addr_next - addr < need_len)
1086 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001087
1088 offset = align_up(addr + header_len, align);
1089 if (is_in_same_page(offset, size, page_size) &&
1090 is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001091 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001092 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001093 }
1094
1095 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001096 offset = align_up(addr2, align);
1097 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001098 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001099 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001100 }
1101
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001102 /* Assume page_size >= header_len so adding one page will
1103 * definitely provide the space for header. */
1104 assert(page_size >= header_len);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001105 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001106 offset = align_up(addr3, align);
1107 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001108 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001109 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001110 }
1111 }
1112 return -1;
1113}