blob: c40bd66411376366799304b7e337345dce367242 [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
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Hung-Te Lineab2c812013-01-29 01:56:17 +080018 */
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
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080053/* Type and format */
54
55struct typedesc_t {
56 uint32_t type;
57 const char *name;
58};
59
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070060static const struct typedesc_t types_cbfs_entry[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080061 {CBFS_COMPONENT_STAGE, "stage"},
62 {CBFS_COMPONENT_PAYLOAD, "payload"},
63 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
64 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
65 {CBFS_COMPONENT_RAW, "raw"},
66 {CBFS_COMPONENT_VSA, "vsa"},
67 {CBFS_COMPONENT_MBI, "mbi"},
68 {CBFS_COMPONENT_MICROCODE, "microcode"},
Martin Rothdde307c2015-03-24 15:54:20 -060069 {CBFS_COMPONENT_FSP, "fsp"},
70 {CBFS_COMPONENT_MRC, "mrc"},
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080071 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos_default"},
72 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos_layout"},
Martin Rothdde307c2015-03-24 15:54:20 -060073 {CBFS_COMPONENT_SPD, "spd"},
74 {CBFS_COMPONENT_MRC_CACHE, "mrc_cache"},
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080075 {CBFS_COMPONENT_DELETED, "deleted"},
76 {CBFS_COMPONENT_NULL, "null"},
Sol Boucher5bb90e62015-05-07 21:00:05 -070077 {0, NULL}
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080078};
79
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070080static const struct typedesc_t types_cbfs_compression[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080081 {CBFS_COMPRESS_NONE, "none"},
82 {CBFS_COMPRESS_LZMA, "LZMA"},
Sol Boucher5bb90e62015-05-07 21:00:05 -070083 {0, NULL}
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080084};
85
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080086static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070087 const char *default_value)
88{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080089 int i;
90 for (i = 0; desc[i].name; i++)
91 if (desc[i].type == type)
92 return desc[i].name;
93 return default_value;
94}
95
Sol Boucherec424862015-05-07 21:00:05 -070096static int lookup_type_by_name(const struct typedesc_t *desc, const char *name)
97{
98 int i;
99 for (i = 0; desc[i].name && strcasecmp(name, desc[i].name); ++i);
100 return desc[i].name ? (int)desc[i].type : -1;
101}
102
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100103static const char *get_cbfs_entry_type_name(uint32_t type)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700104{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800105 return lookup_name_by_type(types_cbfs_entry, type, "(unknown)");
106}
107
Sol Boucherec424862015-05-07 21:00:05 -0700108int cbfs_parse_comp_algo(const char *name)
109{
110 return lookup_type_by_name(types_cbfs_compression, name);
111}
112
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800113/* CBFS image */
114
Patrick Georgi11ee08f2015-08-11 15:10:02 +0200115size_t cbfs_calculate_file_header_size(const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700116{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800117 return (sizeof(struct cbfs_file) +
118 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
119}
120
Sol Boucher67a0a862015-03-18 12:36:27 -0700121/* Only call on legacy CBFSes possessing a master header. */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600122static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700123{
Sol Boucher67a0a862015-03-18 12:36:27 -0700124 assert(image);
125 assert(cbfs_is_legacy_cbfs(image));
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800126 // A bug in old cbfstool may produce extra few bytes (by alignment) and
127 // cause cbfstool to overwrite things after free space -- which is
128 // usually CBFS header on x86. We need to workaround that.
129
130 struct cbfs_file *entry, *first = NULL, *last = NULL;
131 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800132 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800133 entry = cbfs_find_next_entry(image, entry)) {
134 last = entry;
135 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600136 if ((char *)first < (char *)hdr_loc &&
137 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800138 WARN("CBFS image was created with old cbfstool with size bug. "
139 "Fixing size in last entry...\n");
Sol Boucher3e060ed2015-05-05 15:40:15 -0700140 last->len = htonl(ntohl(last->len) - image->header.align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800141 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
142 cbfs_get_entry_addr(image, entry),
143 cbfs_get_entry_addr(image,
144 cbfs_find_next_entry(image, last)));
145 }
146 return 0;
147}
148
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800149void cbfs_put_header(void *dest, const struct cbfs_header *header)
150{
151 struct buffer outheader;
152
153 outheader.data = dest;
154 outheader.size = 0;
155
156 xdr_be.put32(&outheader, header->magic);
157 xdr_be.put32(&outheader, header->version);
158 xdr_be.put32(&outheader, header->romsize);
159 xdr_be.put32(&outheader, header->bootblocksize);
160 xdr_be.put32(&outheader, header->align);
161 xdr_be.put32(&outheader, header->offset);
162 xdr_be.put32(&outheader, header->architecture);
163}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600164
Hung-Te Lin0780d672014-05-16 10:14:05 +0800165static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
166 struct cbfs_payload_segment *input)
167{
168 struct buffer seg = {
169 .data = (void *)input,
170 .size = sizeof(*input),
171 };
172 output->type = xdr_be.get32(&seg);
173 output->compression = xdr_be.get32(&seg);
174 output->offset = xdr_be.get32(&seg);
175 output->load_addr = xdr_be.get64(&seg);
176 output->len = xdr_be.get32(&seg);
177 output->mem_len = xdr_be.get32(&seg);
178 assert(seg.size == 0);
179}
180
Patrick Georgia71c83f2015-08-26 12:23:26 +0200181static int cbfs_file_get_compression_info(struct cbfs_file *entry,
182 uint32_t *decompressed_size)
183{
184 unsigned int compression = CBFS_COMPRESS_NONE;
185 *decompressed_size = ntohl(entry->len);
186 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
187 attr != NULL;
188 attr = cbfs_file_next_attr(entry, attr)) {
189 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) {
190 struct cbfs_file_attr_compression *ac =
191 (struct cbfs_file_attr_compression *)attr;
192 compression = ntohl(ac->compression);
193 if (decompressed_size)
194 *decompressed_size =
195 ntohl(ac->decompressed_size);
196 }
197 }
198 return compression;
199}
200
Sol Boucher0e539312015-03-05 15:38:03 -0800201void cbfs_get_header(struct cbfs_header *header, void *src)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600202{
203 struct buffer outheader;
204
Sol Boucher0e539312015-03-05 15:38:03 -0800205 outheader.data = src; /* We're not modifying the data */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600206 outheader.size = 0;
207
208 header->magic = xdr_be.get32(&outheader);
209 header->version = xdr_be.get32(&outheader);
210 header->romsize = xdr_be.get32(&outheader);
211 header->bootblocksize = xdr_be.get32(&outheader);
212 header->align = xdr_be.get32(&outheader);
213 header->offset = xdr_be.get32(&outheader);
214 header->architecture = xdr_be.get32(&outheader);
215}
216
Sol Boucher67a0a862015-03-18 12:36:27 -0700217int cbfs_image_create(struct cbfs_image *image, size_t entries_size)
218{
219 assert(image);
220 assert(image->buffer.data);
221
222 size_t empty_header_len = cbfs_calculate_file_header_size("");
223 uint32_t entries_offset = 0;
224 uint32_t align = CBFS_ENTRY_ALIGNMENT;
225 if (image->has_header) {
226 entries_offset = image->header.offset;
227
228 if (entries_offset > image->buffer.size) {
229 ERROR("CBFS file entries are located outside CBFS itself\n");
230 return -1;
231 }
232
233 align = image->header.align;
234 }
235
236 // This attribute must be given in order to prove that this module
237 // correctly preserves certain CBFS properties. See the block comment
238 // near the top of this file (and the associated commit message).
239 if (align < empty_header_len) {
240 ERROR("CBFS must be aligned to at least %zu bytes\n",
241 empty_header_len);
242 return -1;
243 }
244
245 if (entries_size > image->buffer.size - entries_offset) {
246 ERROR("CBFS doesn't have enough space to fit its file entries\n");
247 return -1;
248 }
249
250 if (empty_header_len > entries_size) {
251 ERROR("CBFS is too small to fit any header\n");
252 return -1;
253 }
254 struct cbfs_file *entry_header =
255 (struct cbfs_file *)(image->buffer.data + entries_offset);
256 // This alignment is necessary in order to prove that this module
257 // correctly preserves certain CBFS properties. See the block comment
258 // near the top of this file (and the associated commit message).
259 entries_size -= entries_size % align;
260
261 size_t capacity = entries_size - empty_header_len;
262 LOG("Created CBFS (capacity = %zu bytes)\n", capacity);
Patrick Georgiedf25d92015-08-12 09:12:06 +0200263 return cbfs_create_empty_entry(entry_header, CBFS_COMPONENT_NULL,
264 capacity, "");
Sol Boucher67a0a862015-03-18 12:36:27 -0700265}
266
267int cbfs_legacy_image_create(struct cbfs_image *image,
268 uint32_t architecture,
269 uint32_t align,
270 struct buffer *bootblock,
271 uint32_t bootblock_offset,
272 uint32_t header_offset,
273 uint32_t entries_offset)
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800274{
Sol Bouchere3260a02015-03-25 13:40:08 -0700275 assert(image);
276 assert(image->buffer.data);
277 assert(bootblock);
278
Julius Wernerefcee762014-11-10 13:14:24 -0800279 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800280 uint32_t cbfs_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600281 void *header_loc;
Sol Bouchere3260a02015-03-25 13:40:08 -0700282 size_t size = image->buffer.size;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800283
284 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
285 "header=0x%x+0x%zx, entries_offset=0x%x\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700286 bootblock_offset, bootblock->size, header_offset,
287 sizeof(image->header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800288
Sol Boucher67a0a862015-03-18 12:36:27 -0700289 // Adjust legacy top-aligned address to ROM offset.
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800290 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800291 entries_offset = size + (int32_t)entries_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800292 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800293 bootblock_offset = size + (int32_t)bootblock_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800294 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800295 header_offset = size + (int32_t)header_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800296
297 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
298 "header=0x%x, entries_offset=0x%x\n",
299 bootblock_offset, header_offset, entries_offset);
300
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800301 // Prepare bootblock
302 if (bootblock_offset + bootblock->size > size) {
303 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
304 bootblock_offset, bootblock->size, size);
305 return -1;
306 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800307 if (entries_offset > bootblock_offset &&
308 entries_offset < bootblock->size) {
309 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
310 bootblock_offset, bootblock->size, entries_offset);
311 return -1;
312 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800313 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
314 bootblock->size);
315
316 // Prepare header
Sol Boucher5bad3952015-05-05 20:35:26 -0700317 if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800318 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700319 header_offset, sizeof(image->header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800320 return -1;
321 }
Sol Boucher3e060ed2015-05-05 15:40:15 -0700322 image->header.magic = CBFS_HEADER_MAGIC;
323 image->header.version = CBFS_HEADER_VERSION;
324 image->header.romsize = size;
325 image->header.bootblocksize = bootblock->size;
326 image->header.align = align;
327 image->header.offset = entries_offset;
328 image->header.architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600329
330 header_loc = (image->buffer.data + header_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700331 cbfs_put_header(header_loc, &image->header);
Sol Boucher67a0a862015-03-18 12:36:27 -0700332 image->has_header = true;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800333
Julius Wernerefcee762014-11-10 13:14:24 -0800334 // The last 4 byte of the image contain the relative offset from the end
335 // of the image to the master header as a 32-bit signed integer. x86
336 // relies on this also being its (memory-mapped, top-aligned) absolute
337 // 32-bit address by virtue of how two's complement numbers work.
338 assert(size % sizeof(int32_t) == 0);
339 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
340 *rel_offset = header_offset - size;
341
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800342 // Prepare entries
343 if (align_up(entries_offset, align) != entries_offset) {
344 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
345 entries_offset, align);
346 return -1;
347 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800348 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800349 // e = min(bootblock, header, rel_offset) where e > entries_offset.
350 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800351 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
352 cbfs_len = bootblock_offset;
353 if (header_offset > entries_offset && header_offset < cbfs_len)
354 cbfs_len = header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700355
356 if (cbfs_image_create(image, cbfs_len - entries_offset))
357 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800358 return 0;
359}
360
Sol Bouchere3260a02015-03-25 13:40:08 -0700361int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
362 uint32_t offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700363{
Sol Bouchere3260a02015-03-25 13:40:08 -0700364 assert(out);
365 assert(in);
366 assert(in->data);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600367
Sol Bouchere3260a02015-03-25 13:40:08 -0700368 buffer_clone(&out->buffer, in);
Sol Boucher67a0a862015-03-18 12:36:27 -0700369 out->has_header = false;
370
Sol Bouchere3260a02015-03-25 13:40:08 -0700371 void *header_loc = cbfs_find_header(in->data, in->size, offset);
372 if (header_loc) {
373 cbfs_get_header(&out->header, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700374 out->has_header = true;
Sol Bouchere3260a02015-03-25 13:40:08 -0700375 cbfs_fix_legacy_size(out, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700376 } else if (offset != ~0u) {
377 ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n");
378 return 1;
379 } else if (!cbfs_is_valid_cbfs(out)) {
380 ERROR("Selected image region is not a valid CBFS.\n");
381 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800382 }
Sol Boucher67a0a862015-03-18 12:36:27 -0700383
384 return 0;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800385}
386
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800387int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
388 size_t copy_size)
389{
Sol Boucher67a0a862015-03-18 12:36:27 -0700390 assert(image);
391 if (!cbfs_is_legacy_cbfs(image))
392 return -1;
393
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800394 struct cbfs_file *src_entry, *dst_entry;
395 struct cbfs_header *copy_header;
396 size_t align, entry_offset;
397 ssize_t last_entry_size;
398
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800399 size_t cbfs_offset, cbfs_end;
400 size_t copy_end = copy_offset + copy_size;
401
Sol Boucher3e060ed2015-05-05 15:40:15 -0700402 align = image->header.align;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800403
Sol Boucher3e060ed2015-05-05 15:40:15 -0700404 cbfs_offset = image->header.offset;
405 cbfs_end = image->header.romsize;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800406
407 if (copy_end > image->buffer.size) {
408 ERROR("Copy offset out of range: [%zx:%zx)\n",
409 copy_offset, copy_end);
410 return 1;
411 }
412
Sol Boucher297c88c2015-05-05 15:35:18 -0700413 /* Range check requested copy region with source cbfs. */
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800414 if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) ||
415 (copy_end >= cbfs_offset && copy_end <= cbfs_end)) {
416 ERROR("New image would overlap old one.\n");
417 return 1;
418 }
419
420 /* This will work, let's create a copy. */
421 copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700422 cbfs_put_header(copy_header, &image->header);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800423
424 copy_header->bootblocksize = 0;
425 /* Romsize is a misnomer. It's the absolute limit of cbfs content.*/
426 copy_header->romsize = htonl(copy_end);
427 entry_offset = align_up(copy_offset + sizeof(*copy_header), align);
428 copy_header->offset = htonl(entry_offset);
429 dst_entry = (struct cbfs_file *)(image->buffer.data + entry_offset);
430
431 /* Copy non-empty files */
432 for (src_entry = cbfs_find_first_entry(image);
433 src_entry && cbfs_is_valid_entry(image, src_entry);
434 src_entry = cbfs_find_next_entry(image, src_entry)) {
435 size_t entry_size;
436
437 if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) ||
438 (src_entry->type == htonl(CBFS_COMPONENT_DELETED)))
439 continue;
440
441 entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
442 memcpy(dst_entry, src_entry, entry_size);
443 dst_entry = (struct cbfs_file *)(
444 (uintptr_t)dst_entry + align_up(entry_size, align));
445
Sol Boucher0e539312015-03-05 15:38:03 -0800446 if ((size_t)((char *)dst_entry - image->buffer.data) >=
447 copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800448 ERROR("Ran out of room in copy region.\n");
449 return 1;
450 }
451 }
452
453 /* Last entry size is all the room above it. */
454 last_entry_size = copy_end - ((char *)dst_entry - image->buffer.data)
455 - cbfs_calculate_file_header_size("");
456
457 if (last_entry_size < 0)
458 WARN("No room to create the last entry!\n")
459 else
Patrick Georgiedf25d92015-08-12 09:12:06 +0200460 cbfs_create_empty_entry(dst_entry, CBFS_COMPONENT_NULL,
461 last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800462
463 return 0;
464}
465
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700466int cbfs_image_delete(struct cbfs_image *image)
467{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100468 if (image == NULL)
469 return 0;
470
Hung-Te Lineab2c812013-01-29 01:56:17 +0800471 buffer_delete(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800472 return 0;
473}
474
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800475/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
476static int cbfs_add_entry_at(struct cbfs_image *image,
477 struct cbfs_file *entry,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800478 const void *data,
Patrick Georgi7fd14182015-08-11 15:55:16 +0200479 uint32_t content_offset,
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200480 const struct cbfs_file *header)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700481{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800482 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
483 uint32_t addr = cbfs_get_entry_addr(image, entry),
484 addr_next = cbfs_get_entry_addr(image, next);
Patrick Georgi7fd14182015-08-11 15:55:16 +0200485 uint32_t min_entry_size = cbfs_calculate_file_header_size("");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200486 uint32_t len, header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700487 uint32_t align = image->has_header ? image->header.align :
488 CBFS_ENTRY_ALIGNMENT;
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200489 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800490
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200491 header_offset = content_offset - header_size;
492 if (header_offset % align)
493 header_offset -= header_offset % align;
494 if (header_offset < addr) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800495 ERROR("No space to hold cbfs_file header.");
496 return -1;
497 }
498
499 // Process buffer BEFORE content_offset.
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200500 if (header_offset - addr > min_entry_size) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800501 DEBUG("|min|...|header|content|... <create new entry>\n");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200502 len = header_offset - addr - min_entry_size;
Patrick Georgiedf25d92015-08-12 09:12:06 +0200503 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800504 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
505 entry = cbfs_find_next_entry(image, entry);
506 addr = cbfs_get_entry_addr(image, entry);
507 }
508
Patrick Georgi7a33b532015-08-25 13:00:04 +0200509 len = content_offset - addr - header_size;
Patrick Georgia60e7b62015-08-25 22:26:02 +0200510 memcpy(entry, header, header_size);
Patrick Georgi7a33b532015-08-25 13:00:04 +0200511 if (len != 0) {
512 /* the header moved backwards a bit to accomodate cbfs_file
513 * alignment requirements, so patch up ->offset to still point
514 * to file data.
515 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800516 DEBUG("|..|header|content|... <use offset to create entry>\n");
Patrick Georgiae7efb92015-08-25 13:11:28 +0200517 DEBUG("before: offset=0x%x\n", ntohl(entry->offset));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800518 // TODO reset expanded name buffer to 0xFF.
Patrick Georgi7a33b532015-08-25 13:00:04 +0200519 entry->offset = htonl(ntohl(entry->offset) + len);
Patrick Georgiae7efb92015-08-25 13:11:28 +0200520 DEBUG("after: offset=0x%x\n", ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800521 }
522
523 // Ready to fill data into entry.
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800524 DEBUG("content_offset: 0x%x, entry location: %x\n",
525 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
526 image->buffer.data));
527 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
Patrick Georgicccc9d42015-04-28 13:09:36 +0200528 (ptrdiff_t)content_offset);
Patrick Georgi19c80b22015-08-25 13:16:04 +0200529 memcpy(CBFS_SUBHEADER(entry), data, ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800530 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
531
532 // Process buffer AFTER entry.
533 entry = cbfs_find_next_entry(image, entry);
534 addr = cbfs_get_entry_addr(image, entry);
Sol Boucher05725652015-04-02 20:58:26 -0700535 if (addr == addr_next)
536 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800537
Sol Boucher05725652015-04-02 20:58:26 -0700538 assert(addr < addr_next);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800539 if (addr_next - addr < min_entry_size) {
Sol Boucher636cc852015-04-03 09:13:04 -0700540 DEBUG("No need for new \"empty\" entry\n");
541 /* No need to increase the size of the just
542 * stored file to extend to next file. Alignment
543 * of next file takes care of this.
544 */
545 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800546 }
547
548 len = addr_next - addr - min_entry_size;
Patrick Georgiedf25d92015-08-12 09:12:06 +0200549 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800550 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
551 return 0;
552}
553
554int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Patrick Georgie5903582015-08-25 13:53:42 +0200555 uint32_t content_offset,
Patrick Georgif5252f32015-08-25 22:27:57 +0200556 struct cbfs_file *header)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700557{
Sol Boucher67d59982015-05-07 02:39:22 -0700558 assert(image);
559 assert(buffer);
560 assert(buffer->data);
Sol Boucher67d59982015-05-07 02:39:22 -0700561 assert(!IS_TOP_ALIGNED_ADDRESS(content_offset));
562
Patrick Georgia60e7b62015-08-25 22:26:02 +0200563 const char *name = header->filename;
Patrick Georgie5903582015-08-25 13:53:42 +0200564
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800565 uint32_t entry_type;
566 uint32_t addr, addr_next;
567 struct cbfs_file *entry, *next;
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200568 uint32_t need_size;
Patrick Georgif5252f32015-08-25 22:27:57 +0200569 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800570
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800571 need_size = header_size + buffer->size;
572 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
573 name, content_offset, header_size, buffer->size, need_size);
574
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800575 // Merge empty entries.
576 DEBUG("(trying to merge empty entries...)\n");
577 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
578
579 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800580 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800581 entry = cbfs_find_next_entry(image, entry)) {
582
583 entry_type = ntohl(entry->type);
584 if (entry_type != CBFS_COMPONENT_NULL)
585 continue;
586
587 addr = cbfs_get_entry_addr(image, entry);
588 next = cbfs_find_next_entry(image, entry);
589 addr_next = cbfs_get_entry_addr(image, next);
590
591 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
592 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600593
594 /* Will the file fit? Don't yet worry if we have space for a new
595 * "empty" entry. We take care of that later.
596 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800597 if (addr + need_size > addr_next)
598 continue;
599
Patrick Georgiaa44dbd2015-08-12 12:05:21 +0200600 // Test for complicated cases
601 if (content_offset > 0) {
602 if (addr_next < content_offset) {
603 DEBUG("Not for specified offset yet");
604 continue;
605 } else if (addr > content_offset) {
606 DEBUG("Exceed specified content_offset.");
607 break;
608 } else if (addr + header_size > content_offset) {
609 ERROR("Not enough space for header.\n");
610 break;
611 } else if (content_offset + buffer->size > addr_next) {
612 ERROR("Not enough space for content.\n");
613 break;
614 }
615 }
616
617 // TODO there are more few tricky cases that we may
618 // want to fit by altering offset.
619
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200620 if (content_offset == 0) {
621 // we tested every condition earlier under which
622 // placing the file there might fail
623 content_offset = addr + header_size;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800624 }
625
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800626 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
627 addr, addr_next - addr, content_offset);
628
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200629 if (cbfs_add_entry_at(image, entry, buffer->data,
630 content_offset, header) == 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800631 return 0;
632 }
633 break;
634 }
635
636 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
637 buffer->name, buffer->size, buffer->size / 1024, content_offset);
638 return -1;
639}
640
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700641struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
642{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800643 struct cbfs_file *entry;
644 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800645 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800646 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgic569b8b2015-07-15 16:42:38 +0200647 if (strcasecmp(entry->filename, name) == 0) {
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800648 DEBUG("cbfs_get_entry: found %s\n", name);
649 return entry;
650 }
651 }
652 return NULL;
653}
654
655int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700656 const char *filename)
657{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800658 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
659 struct buffer buffer;
660 if (!entry) {
661 ERROR("File not found: %s\n", entry_name);
662 return -1;
663 }
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200664
665 unsigned int decompressed_size = 0;
666 unsigned int compression = cbfs_file_get_compression_info(entry,
667 &decompressed_size);
668
669 decomp_func_ptr decompress = decompression_function(compression);
670 if (!decompress) {
671 ERROR("looking up decompression routine failed\n");
672 return -1;
673 }
674
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800675 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
676 entry_name, cbfs_get_entry_addr(image, entry),
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200677 get_cbfs_entry_type_name(ntohl(entry->type)), decompressed_size);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800678
Patrick Georgi011b0b32015-08-26 12:16:54 +0200679 if (ntohl(entry->type) == CBFS_COMPONENT_STAGE) {
680 WARN("Stages are extracted in SELF format.\n");
681 }
682
683 if (ntohl(entry->type) == CBFS_COMPONENT_PAYLOAD) {
684 WARN("Payloads are extracted in SELF format.\n");
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800685 }
686
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200687 buffer.data = malloc(decompressed_size);
688 buffer.size = decompressed_size;
689 if (decompress(CBFS_SUBHEADER(entry), ntohl(entry->len),
690 buffer.data, buffer.size)) {
691 ERROR("decompression failed for %s\n", entry_name);
692 return -1;
693 }
Sol Boucher0e539312015-03-05 15:38:03 -0800694 buffer.name = strdup("(cbfs_export_entry)");
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800695 if (buffer_write_file(&buffer, filename) != 0) {
696 ERROR("Failed to write %s into %s.\n",
697 entry_name, filename);
Sol Boucher0e539312015-03-05 15:38:03 -0800698 free(buffer.name);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800699 return -1;
700 }
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200701 free(buffer.data);
Sol Boucher0e539312015-03-05 15:38:03 -0800702 free(buffer.name);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800703 INFO("Successfully dumped the file to: %s\n", filename);
704 return 0;
705}
706
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700707int cbfs_remove_entry(struct cbfs_image *image, const char *name)
708{
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +0200709 struct cbfs_file *entry;
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800710 entry = cbfs_get_entry(image, name);
711 if (!entry) {
712 ERROR("CBFS file %s not found.\n", name);
713 return -1;
714 }
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800715 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
Patrick Georgic569b8b2015-07-15 16:42:38 +0200716 entry->filename, cbfs_get_entry_addr(image, entry));
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800717 entry->type = htonl(CBFS_COMPONENT_DELETED);
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +0200718 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800719 return 0;
720}
721
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700722int cbfs_print_header_info(struct cbfs_image *image)
723{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800724 char *name = strdup(image->buffer.name);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700725 assert(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800726 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800727 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800728 basename(name),
729 image->buffer.size / 1024,
Sol Boucher3e060ed2015-05-05 15:40:15 -0700730 image->header.bootblocksize,
731 image->header.romsize,
732 image->header.offset,
733 image->header.align,
734 arch_to_string(image->header.architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800735 free(name);
736 return 0;
737}
738
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700739static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
740{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800741 fprintf(fp,
742 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
743 "length: %d/%d\n",
744 lookup_name_by_type(types_cbfs_compression,
745 stage->compression, "(unknown)"),
746 stage->entry,
747 stage->load,
748 stage->len,
749 stage->memlen);
750 return 0;
751}
752
Hung-Te Lin0780d672014-05-16 10:14:05 +0800753static int cbfs_print_decoded_payload_segment_info(
754 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800755{
Hung-Te Lin0780d672014-05-16 10:14:05 +0800756 /* The input (seg) must be already decoded by
757 * cbfs_decode_payload_segment.
758 */
759 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800760 case PAYLOAD_SEGMENT_CODE:
761 case PAYLOAD_SEGMENT_DATA:
762 fprintf(fp, " %s (%s compression, offset: 0x%x, "
763 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800764 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800765 "code " : "data"),
766 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800767 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800768 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800769 seg->offset, seg->load_addr, seg->len,
770 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800771 break;
772
773 case PAYLOAD_SEGMENT_ENTRY:
774 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800775 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800776 break;
777
778 case PAYLOAD_SEGMENT_BSS:
779 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
780 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800781 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800782 break;
783
784 case PAYLOAD_SEGMENT_PARAMS:
785 fprintf(fp, " parameters\n");
786 break;
787
788 default:
789 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
790 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800791 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800792 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800793 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800794 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800795 seg->offset, seg->load_addr, seg->len,
796 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800797 break;
798 }
799 return 0;
800}
801
802int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700803 void *arg)
804{
Patrick Georgic569b8b2015-07-15 16:42:38 +0200805 const char *name = entry->filename;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800806 struct cbfs_payload_segment *payload;
807 FILE *fp = (FILE *)arg;
808
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800809 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800810 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
811 cbfs_get_entry_addr(image, entry));
812 return -1;
813 }
814 if (!fp)
815 fp = stdout;
816
Patrick Georgic82725c2015-08-26 12:13:03 +0200817 unsigned int decompressed_size = 0;
Patrick Georgia71c83f2015-08-26 12:23:26 +0200818 unsigned int compression = cbfs_file_get_compression_info(entry,
819 &decompressed_size);
Patrick Georgic82725c2015-08-26 12:13:03 +0200820
821 if (compression == CBFS_COMPRESS_NONE) {
822 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
823 *name ? name : "(empty)",
824 cbfs_get_entry_addr(image, entry),
825 get_cbfs_entry_type_name(ntohl(entry->type)),
826 ntohl(entry->len));
827 } else {
828 fprintf(fp, "%-30s 0x%-8x %-12s %d (%d after %s decompression)\n",
829 *name ? name : "(empty)",
830 cbfs_get_entry_addr(image, entry),
831 get_cbfs_entry_type_name(ntohl(entry->type)),
832 ntohl(entry->len),
833 decompressed_size,
834 lookup_name_by_type(types_cbfs_compression,
835 compression, "(unknown)")
836 );
837 }
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800838
839 if (!verbose)
840 return 0;
841
842 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
843 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
844 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
845 ntohl(entry->len));
846
847 /* note the components of the subheader may be in host order ... */
848 switch (ntohl(entry->type)) {
849 case CBFS_COMPONENT_STAGE:
850 cbfs_print_stage_info((struct cbfs_stage *)
851 CBFS_SUBHEADER(entry), fp);
852 break;
853
854 case CBFS_COMPONENT_PAYLOAD:
855 payload = (struct cbfs_payload_segment *)
856 CBFS_SUBHEADER(entry);
857 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +0800858 struct cbfs_payload_segment seg;
859 cbfs_decode_payload_segment(&seg, payload);
860 cbfs_print_decoded_payload_segment_info(
861 &seg, fp);
862 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800863 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +0800864 else
Aaron Durbinca630272014-08-05 10:48:20 -0500865 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800866 }
867 break;
868 default:
869 break;
870 }
871 return 0;
872}
873
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700874int cbfs_print_directory(struct cbfs_image *image)
875{
Sol Boucher67a0a862015-03-18 12:36:27 -0700876 if (cbfs_is_legacy_cbfs(image))
877 cbfs_print_header_info(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800878 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
879 cbfs_walk(image, cbfs_print_entry_info, NULL);
880 return 0;
881}
882
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800883int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -0800884 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700885{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800886 struct cbfs_file *next;
887 uint32_t type, addr, last_addr;
888
889 type = ntohl(entry->type);
890 if (type == CBFS_COMPONENT_DELETED) {
891 // Ready to be recycled.
892 type = CBFS_COMPONENT_NULL;
893 entry->type = htonl(type);
894 }
895 if (type != CBFS_COMPONENT_NULL)
896 return 0;
897
898 next = cbfs_find_next_entry(image, entry);
899
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800900 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800901 type = ntohl(next->type);
902 if (type == CBFS_COMPONENT_DELETED) {
903 type = CBFS_COMPONENT_NULL;
904 next->type = htonl(type);
905 }
906 if (type != CBFS_COMPONENT_NULL)
907 return 0;
908
909 addr = cbfs_get_entry_addr(image, entry);
910 last_addr = cbfs_get_entry_addr(
911 image, cbfs_find_next_entry(image, next));
912
913 // Now, we find two deleted/empty entries; try to merge now.
914 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
915 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
916 cbfs_get_entry_addr(image, next), ntohl(next->len));
Patrick Georgiedf25d92015-08-12 09:12:06 +0200917 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL,
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800918 (last_addr - addr -
919 cbfs_calculate_file_header_size("")),
920 "");
921 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
922 next = cbfs_find_next_entry(image, entry);
923 }
924 return 0;
925}
926
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800927int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700928 void *arg)
929{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800930 int count = 0;
931 struct cbfs_file *entry;
932 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800933 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800934 entry = cbfs_find_next_entry(image, entry)) {
935 count ++;
936 if (callback(image, entry, arg) != 0)
937 break;
938 }
939 return count;
940}
941
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800942static int cbfs_header_valid(struct cbfs_header *header, size_t size)
943{
944 if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
945 ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
946 (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
947 (ntohl(header->romsize) <= size) &&
948 (ntohl(header->offset) < ntohl(header->romsize)))
949 return 1;
950 return 0;
951}
952
953struct cbfs_header *cbfs_find_header(char *data, size_t size,
954 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700955{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800956 size_t offset;
957 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800958 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800959 struct cbfs_header *header, *result = NULL;
960
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800961 if (forced_offset < (size - sizeof(struct cbfs_header))) {
962 /* Check if the forced header is valid. */
963 header = (struct cbfs_header *)(data + forced_offset);
964 if (cbfs_header_valid(header, size))
965 return header;
966 return NULL;
967 }
968
Julius Wernerefcee762014-11-10 13:14:24 -0800969 // Try finding relative offset of master header at end of file first.
970 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
971 offset = size + rel_offset;
972 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
973 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800974
Hung-Te Lineab2c812013-01-29 01:56:17 +0800975 if (offset >= size - sizeof(*header) ||
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800976 !cbfs_header_valid((struct cbfs_header *)(data + offset), size)) {
Julius Wernerefcee762014-11-10 13:14:24 -0800977 // Some use cases append non-CBFS data to the end of the ROM.
978 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +0800979 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800980 }
Hung-Te Lineab2c812013-01-29 01:56:17 +0800981
982 for (; offset + sizeof(*header) < size; offset++) {
983 header = (struct cbfs_header *)(data + offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800984 if (!cbfs_header_valid(header, size))
Hung-Te Lineab2c812013-01-29 01:56:17 +0800985 continue;
Julius Wernerefcee762014-11-10 13:14:24 -0800986 if (!found++)
987 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800988 }
Julius Wernerefcee762014-11-10 13:14:24 -0800989 if (found > 1)
990 // Top-aligned images usually have a working relative offset
991 // field, so this is more likely to happen on bottom-aligned
992 // ones (where the first header is the "outermost" one)
993 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +0800994 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800995 return result;
996}
997
998
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700999struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
1000{
Sol Boucher3e060ed2015-05-05 15:40:15 -07001001 assert(image);
Sol Boucher67a0a862015-03-18 12:36:27 -07001002 return image->has_header ? (struct cbfs_file *)(image->buffer.data +
1003 image->header.offset) :
1004 (struct cbfs_file *)image->buffer.data;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001005}
1006
1007struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001008 struct cbfs_file *entry)
1009{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001010 uint32_t addr = cbfs_get_entry_addr(image, entry);
Sol Boucher67a0a862015-03-18 12:36:27 -07001011 int align = image->has_header ? image->header.align :
1012 CBFS_ENTRY_ALIGNMENT;
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001013 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001014 addr += ntohl(entry->offset) + ntohl(entry->len);
1015 addr = align_up(addr, align);
1016 return (struct cbfs_file *)(image->buffer.data + addr);
1017}
1018
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001019uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
1020{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001021 assert(image && image->buffer.data && entry);
1022 return (int32_t)((char *)entry - image->buffer.data);
1023}
1024
Sol Boucher67a0a862015-03-18 12:36:27 -07001025int cbfs_is_valid_cbfs(struct cbfs_image *image)
1026{
1027 return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC,
1028 strlen(CBFS_FILE_MAGIC));
1029}
1030
1031int cbfs_is_legacy_cbfs(struct cbfs_image *image)
1032{
1033 return image->has_header;
1034}
1035
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001036int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
1037{
Sol Bouchere3260a02015-03-25 13:40:08 -07001038 uint32_t offset = cbfs_get_entry_addr(image, entry);
1039
1040 if (offset >= image->buffer.size)
1041 return 0;
1042
1043 struct buffer entry_data;
1044 buffer_clone(&entry_data, &image->buffer);
1045 buffer_seek(&entry_data, offset);
1046 return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
Sol Boucher67a0a862015-03-18 12:36:27 -07001047 strlen(CBFS_FILE_MAGIC));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001048}
1049
Patrick Georgi57edf162015-08-12 09:20:11 +02001050struct cbfs_file *cbfs_create_file_header(int type,
Vadim Bendebury45e59972014-12-23 15:59:57 -08001051 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001052{
Patrick Georgi2c615062015-07-15 20:49:00 +02001053 struct cbfs_file *entry = malloc(MAX_CBFS_FILE_HEADER_BUFFER);
1054 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, MAX_CBFS_FILE_HEADER_BUFFER);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001055 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
Patrick Georgiedf25d92015-08-12 09:12:06 +02001056 entry->type = htonl(type);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001057 entry->len = htonl(len);
Patrick Georgi0d618af2015-07-15 18:28:23 +02001058 entry->attributes_offset = 0;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001059 entry->offset = htonl(cbfs_calculate_file_header_size(name));
Patrick Georgic569b8b2015-07-15 16:42:38 +02001060 memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry));
1061 strcpy(entry->filename, name);
Patrick Georgi57edf162015-08-12 09:20:11 +02001062 return entry;
1063}
1064
1065int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
1066 size_t len, const char *name)
1067{
1068 struct cbfs_file *tmp = cbfs_create_file_header(type, len, name);
1069 memcpy(entry, tmp, ntohl(tmp->offset));
1070 free(tmp);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001071 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1072 return 0;
1073}
1074
Patrick Georgi2c615062015-07-15 20:49:00 +02001075struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file)
1076{
1077 /* attributes_offset should be 0 when there is no attribute, but all
1078 * values that point into the cbfs_file header are invalid, too. */
1079 if (ntohl(file->attributes_offset) <= sizeof(*file))
1080 return NULL;
1081
1082 /* There needs to be enough space for the file header and one
1083 * attribute header for this to make sense. */
1084 if (ntohl(file->offset) <=
1085 sizeof(*file) + sizeof(struct cbfs_file_attribute))
1086 return NULL;
1087
1088 return (struct cbfs_file_attribute *)
1089 (((uint8_t *)file) + ntohl(file->attributes_offset));
1090}
1091
1092struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
1093 struct cbfs_file_attribute *attr)
1094{
1095 /* ex falso sequitur quodlibet */
1096 if (attr == NULL)
1097 return NULL;
1098
1099 /* Is there enough space for another attribute? */
1100 if ((uint8_t *)attr + ntohl(attr->len) +
1101 sizeof(struct cbfs_file_attribute) >=
1102 (uint8_t *)file + ntohl(file->offset))
1103 return NULL;
1104
1105 struct cbfs_file_attribute *next = (struct cbfs_file_attribute *)
1106 (((uint8_t *)attr) + ntohl(attr->len));
1107 /* If any, "unused" attributes must come last. */
1108 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED)
1109 return NULL;
1110 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
1111 return NULL;
1112
1113 return next;
1114}
1115
1116struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
1117 uint32_t tag,
1118 uint32_t size)
1119{
1120 struct cbfs_file_attribute *attr, *next;
1121 next = cbfs_file_first_attr(header);
1122 do {
1123 attr = next;
1124 next = cbfs_file_next_attr(header, attr);
1125 } while (next != NULL);
1126 uint32_t header_size = ntohl(header->offset) + size;
1127 if (header_size > MAX_CBFS_FILE_HEADER_BUFFER) {
1128 DEBUG("exceeding allocated space for cbfs_file headers");
1129 return NULL;
1130 }
1131 /* attr points to the last valid attribute now.
1132 * If NULL, we have to create the first one. */
1133 if (attr == NULL) {
1134 /* New attributes start where the header ends.
1135 * header->offset is later set to accomodate the
1136 * additional structure.
1137 * No endianess translation necessary here, because both
1138 * fields are encoded the same way. */
1139 header->attributes_offset = header->offset;
1140 attr = (struct cbfs_file_attribute *)
1141 (((uint8_t *)header) +
1142 ntohl(header->attributes_offset));
1143 } else {
1144 attr = (struct cbfs_file_attribute *)
1145 (((uint8_t *)attr) +
1146 ntohl(attr->len));
1147 }
1148 header->offset = htonl(header_size);
1149 memset(attr, CBFS_CONTENT_DEFAULT_VALUE, size);
1150 attr->tag = htonl(tag);
1151 attr->len = htonl(size);
1152 return attr;
1153}
1154
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001155/* Finds a place to hold whole data in same memory page. */
1156static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1157{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001158 if (!page)
1159 return 1;
1160 return (start / page) == (start + size - 1) / page;
1161}
1162
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001163/* Tests if data can fit in a range by given offset:
1164 * start ->| header_len | offset (+ size) |<- end
1165 */
1166static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001167 uint32_t offset, uint32_t size)
1168{
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001169 return (offset >= start + header_len && offset + size <= end);
1170}
1171
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001172int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001173 uint32_t size, uint32_t page_size, uint32_t align)
1174{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001175 struct cbfs_file *entry;
1176 size_t need_len;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001177 uint32_t addr, addr_next, addr2, addr3, offset, header_len;
1178
1179 /* Default values: allow fitting anywhere in ROM. */
1180 if (!page_size)
Sol Boucher67a0a862015-03-18 12:36:27 -07001181 page_size = image->has_header ? image->header.romsize :
1182 image->buffer.size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001183 if (!align)
1184 align = 1;
1185
1186 if (size > page_size)
1187 ERROR("Input file size (%d) greater than page size (%d).\n",
1188 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001189
Sol Boucher67a0a862015-03-18 12:36:27 -07001190 uint32_t image_align = image->has_header ? image->header.align :
1191 CBFS_ENTRY_ALIGNMENT;
1192 if (page_size % image_align)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001193 WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001194 __func__, page_size, image_align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001195
1196 /* TODO Old cbfstool always assume input is a stage file (and adding
1197 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001198 * (type) param in future. For right now, we assume cbfs_stage is the
1199 * largest structure and add it into header size. */
1200 assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001201 header_len = (cbfs_calculate_file_header_size(name) +
1202 sizeof(struct cbfs_stage));
1203 need_len = header_len + size;
1204
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001205 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001206 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
1207
1208 /* Three cases of content location on memory page:
1209 * case 1.
1210 * | PAGE 1 | PAGE 2 |
1211 * | <header><content>| Fit. Return start of content.
1212 *
1213 * case 2.
1214 * | PAGE 1 | PAGE 2 |
1215 * | <header><content> | Fits when we shift content to align
1216 * shift-> | <header>|<content> | at starting of PAGE 2.
1217 *
1218 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001219 * | PAGE 1 | PAGE 2 | PAGE 3 |
1220 * | <header>< content > | Can't fit. If we shift content to
1221 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1222 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001223 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001224 * The returned address can be then used as "base-address" (-b) in add-*
1225 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1226 * For stage targets, the address is also used to re-link stage before
1227 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001228 */
1229 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001230 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001231 entry = cbfs_find_next_entry(image, entry)) {
1232
1233 uint32_t type = ntohl(entry->type);
1234 if (type != CBFS_COMPONENT_NULL)
1235 continue;
1236
1237 addr = cbfs_get_entry_addr(image, entry);
1238 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
1239 image, entry));
1240 if (addr_next - addr < need_len)
1241 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001242
1243 offset = align_up(addr + header_len, align);
1244 if (is_in_same_page(offset, size, page_size) &&
1245 is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001246 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001247 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001248 }
1249
1250 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001251 offset = align_up(addr2, align);
1252 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001253 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001254 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001255 }
1256
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001257 /* Assume page_size >= header_len so adding one page will
1258 * definitely provide the space for header. */
1259 assert(page_size >= header_len);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001260 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001261 offset = align_up(addr3, align);
1262 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001263 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001264 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001265 }
1266 }
1267 return -1;
1268}