blob: ddc9cf5366631b00ff607bda5d712d810355d702 [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
Sol Boucher0e539312015-03-05 15:38:03 -0800181void cbfs_get_header(struct cbfs_header *header, void *src)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600182{
183 struct buffer outheader;
184
Sol Boucher0e539312015-03-05 15:38:03 -0800185 outheader.data = src; /* We're not modifying the data */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600186 outheader.size = 0;
187
188 header->magic = xdr_be.get32(&outheader);
189 header->version = xdr_be.get32(&outheader);
190 header->romsize = xdr_be.get32(&outheader);
191 header->bootblocksize = xdr_be.get32(&outheader);
192 header->align = xdr_be.get32(&outheader);
193 header->offset = xdr_be.get32(&outheader);
194 header->architecture = xdr_be.get32(&outheader);
195}
196
Sol Boucher67a0a862015-03-18 12:36:27 -0700197int cbfs_image_create(struct cbfs_image *image, size_t entries_size)
198{
199 assert(image);
200 assert(image->buffer.data);
201
202 size_t empty_header_len = cbfs_calculate_file_header_size("");
203 uint32_t entries_offset = 0;
204 uint32_t align = CBFS_ENTRY_ALIGNMENT;
205 if (image->has_header) {
206 entries_offset = image->header.offset;
207
208 if (entries_offset > image->buffer.size) {
209 ERROR("CBFS file entries are located outside CBFS itself\n");
210 return -1;
211 }
212
213 align = image->header.align;
214 }
215
216 // This attribute must be given in order to prove that this module
217 // correctly preserves certain CBFS properties. See the block comment
218 // near the top of this file (and the associated commit message).
219 if (align < empty_header_len) {
220 ERROR("CBFS must be aligned to at least %zu bytes\n",
221 empty_header_len);
222 return -1;
223 }
224
225 if (entries_size > image->buffer.size - entries_offset) {
226 ERROR("CBFS doesn't have enough space to fit its file entries\n");
227 return -1;
228 }
229
230 if (empty_header_len > entries_size) {
231 ERROR("CBFS is too small to fit any header\n");
232 return -1;
233 }
234 struct cbfs_file *entry_header =
235 (struct cbfs_file *)(image->buffer.data + entries_offset);
236 // This alignment is necessary 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 entries_size -= entries_size % align;
240
241 size_t capacity = entries_size - empty_header_len;
242 LOG("Created CBFS (capacity = %zu bytes)\n", capacity);
Patrick Georgiedf25d92015-08-12 09:12:06 +0200243 return cbfs_create_empty_entry(entry_header, CBFS_COMPONENT_NULL,
244 capacity, "");
Sol Boucher67a0a862015-03-18 12:36:27 -0700245}
246
247int cbfs_legacy_image_create(struct cbfs_image *image,
248 uint32_t architecture,
249 uint32_t align,
250 struct buffer *bootblock,
251 uint32_t bootblock_offset,
252 uint32_t header_offset,
253 uint32_t entries_offset)
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800254{
Sol Bouchere3260a02015-03-25 13:40:08 -0700255 assert(image);
256 assert(image->buffer.data);
257 assert(bootblock);
258
Julius Wernerefcee762014-11-10 13:14:24 -0800259 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800260 uint32_t cbfs_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600261 void *header_loc;
Sol Bouchere3260a02015-03-25 13:40:08 -0700262 size_t size = image->buffer.size;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800263
264 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
265 "header=0x%x+0x%zx, entries_offset=0x%x\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700266 bootblock_offset, bootblock->size, header_offset,
267 sizeof(image->header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800268
Sol Boucher67a0a862015-03-18 12:36:27 -0700269 // Adjust legacy top-aligned address to ROM offset.
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800270 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800271 entries_offset = size + (int32_t)entries_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800272 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800273 bootblock_offset = size + (int32_t)bootblock_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800274 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800275 header_offset = size + (int32_t)header_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800276
277 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
278 "header=0x%x, entries_offset=0x%x\n",
279 bootblock_offset, header_offset, entries_offset);
280
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800281 // Prepare bootblock
282 if (bootblock_offset + bootblock->size > size) {
283 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
284 bootblock_offset, bootblock->size, size);
285 return -1;
286 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800287 if (entries_offset > bootblock_offset &&
288 entries_offset < bootblock->size) {
289 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
290 bootblock_offset, bootblock->size, entries_offset);
291 return -1;
292 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800293 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
294 bootblock->size);
295
296 // Prepare header
Sol Boucher5bad3952015-05-05 20:35:26 -0700297 if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800298 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700299 header_offset, sizeof(image->header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800300 return -1;
301 }
Sol Boucher3e060ed2015-05-05 15:40:15 -0700302 image->header.magic = CBFS_HEADER_MAGIC;
303 image->header.version = CBFS_HEADER_VERSION;
304 image->header.romsize = size;
305 image->header.bootblocksize = bootblock->size;
306 image->header.align = align;
307 image->header.offset = entries_offset;
308 image->header.architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600309
310 header_loc = (image->buffer.data + header_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700311 cbfs_put_header(header_loc, &image->header);
Sol Boucher67a0a862015-03-18 12:36:27 -0700312 image->has_header = true;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800313
Julius Wernerefcee762014-11-10 13:14:24 -0800314 // The last 4 byte of the image contain the relative offset from the end
315 // of the image to the master header as a 32-bit signed integer. x86
316 // relies on this also being its (memory-mapped, top-aligned) absolute
317 // 32-bit address by virtue of how two's complement numbers work.
318 assert(size % sizeof(int32_t) == 0);
319 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
320 *rel_offset = header_offset - size;
321
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800322 // Prepare entries
323 if (align_up(entries_offset, align) != entries_offset) {
324 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
325 entries_offset, align);
326 return -1;
327 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800328 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800329 // e = min(bootblock, header, rel_offset) where e > entries_offset.
330 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800331 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
332 cbfs_len = bootblock_offset;
333 if (header_offset > entries_offset && header_offset < cbfs_len)
334 cbfs_len = header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700335
336 if (cbfs_image_create(image, cbfs_len - entries_offset))
337 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800338 return 0;
339}
340
Sol Bouchere3260a02015-03-25 13:40:08 -0700341int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
342 uint32_t offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700343{
Sol Bouchere3260a02015-03-25 13:40:08 -0700344 assert(out);
345 assert(in);
346 assert(in->data);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600347
Sol Bouchere3260a02015-03-25 13:40:08 -0700348 buffer_clone(&out->buffer, in);
Sol Boucher67a0a862015-03-18 12:36:27 -0700349 out->has_header = false;
350
Sol Bouchere3260a02015-03-25 13:40:08 -0700351 void *header_loc = cbfs_find_header(in->data, in->size, offset);
352 if (header_loc) {
353 cbfs_get_header(&out->header, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700354 out->has_header = true;
Sol Bouchere3260a02015-03-25 13:40:08 -0700355 cbfs_fix_legacy_size(out, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700356 } else if (offset != ~0u) {
357 ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n");
358 return 1;
359 } else if (!cbfs_is_valid_cbfs(out)) {
360 ERROR("Selected image region is not a valid CBFS.\n");
361 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800362 }
Sol Boucher67a0a862015-03-18 12:36:27 -0700363
364 return 0;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800365}
366
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800367int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
368 size_t copy_size)
369{
Sol Boucher67a0a862015-03-18 12:36:27 -0700370 assert(image);
371 if (!cbfs_is_legacy_cbfs(image))
372 return -1;
373
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800374 struct cbfs_file *src_entry, *dst_entry;
375 struct cbfs_header *copy_header;
376 size_t align, entry_offset;
377 ssize_t last_entry_size;
378
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800379 size_t cbfs_offset, cbfs_end;
380 size_t copy_end = copy_offset + copy_size;
381
Sol Boucher3e060ed2015-05-05 15:40:15 -0700382 align = image->header.align;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800383
Sol Boucher3e060ed2015-05-05 15:40:15 -0700384 cbfs_offset = image->header.offset;
385 cbfs_end = image->header.romsize;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800386
387 if (copy_end > image->buffer.size) {
388 ERROR("Copy offset out of range: [%zx:%zx)\n",
389 copy_offset, copy_end);
390 return 1;
391 }
392
Sol Boucher297c88c2015-05-05 15:35:18 -0700393 /* Range check requested copy region with source cbfs. */
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800394 if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) ||
395 (copy_end >= cbfs_offset && copy_end <= cbfs_end)) {
396 ERROR("New image would overlap old one.\n");
397 return 1;
398 }
399
400 /* This will work, let's create a copy. */
401 copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700402 cbfs_put_header(copy_header, &image->header);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800403
404 copy_header->bootblocksize = 0;
405 /* Romsize is a misnomer. It's the absolute limit of cbfs content.*/
406 copy_header->romsize = htonl(copy_end);
407 entry_offset = align_up(copy_offset + sizeof(*copy_header), align);
408 copy_header->offset = htonl(entry_offset);
409 dst_entry = (struct cbfs_file *)(image->buffer.data + entry_offset);
410
411 /* Copy non-empty files */
412 for (src_entry = cbfs_find_first_entry(image);
413 src_entry && cbfs_is_valid_entry(image, src_entry);
414 src_entry = cbfs_find_next_entry(image, src_entry)) {
415 size_t entry_size;
416
417 if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) ||
418 (src_entry->type == htonl(CBFS_COMPONENT_DELETED)))
419 continue;
420
421 entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
422 memcpy(dst_entry, src_entry, entry_size);
423 dst_entry = (struct cbfs_file *)(
424 (uintptr_t)dst_entry + align_up(entry_size, align));
425
Sol Boucher0e539312015-03-05 15:38:03 -0800426 if ((size_t)((char *)dst_entry - image->buffer.data) >=
427 copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800428 ERROR("Ran out of room in copy region.\n");
429 return 1;
430 }
431 }
432
433 /* Last entry size is all the room above it. */
434 last_entry_size = copy_end - ((char *)dst_entry - image->buffer.data)
435 - cbfs_calculate_file_header_size("");
436
437 if (last_entry_size < 0)
438 WARN("No room to create the last entry!\n")
439 else
Patrick Georgiedf25d92015-08-12 09:12:06 +0200440 cbfs_create_empty_entry(dst_entry, CBFS_COMPONENT_NULL,
441 last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800442
443 return 0;
444}
445
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700446int cbfs_image_delete(struct cbfs_image *image)
447{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100448 if (image == NULL)
449 return 0;
450
Hung-Te Lineab2c812013-01-29 01:56:17 +0800451 buffer_delete(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800452 return 0;
453}
454
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800455/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
456static int cbfs_add_entry_at(struct cbfs_image *image,
457 struct cbfs_file *entry,
458 uint32_t size,
459 const char *name,
460 uint32_t type,
461 const void *data,
Patrick Georgi7fd14182015-08-11 15:55:16 +0200462 uint32_t content_offset,
463 uint32_t header_size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700464{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800465 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
466 uint32_t addr = cbfs_get_entry_addr(image, entry),
467 addr_next = cbfs_get_entry_addr(image, next);
Patrick Georgi7fd14182015-08-11 15:55:16 +0200468 uint32_t min_entry_size = cbfs_calculate_file_header_size("");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800469 uint32_t len, target;
Sol Boucher67a0a862015-03-18 12:36:27 -0700470 uint32_t align = image->has_header ? image->header.align :
471 CBFS_ENTRY_ALIGNMENT;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800472
473 target = content_offset - header_size;
474 if (target % align)
475 target -= target % align;
476 if (target < addr) {
477 ERROR("No space to hold cbfs_file header.");
478 return -1;
479 }
480
481 // Process buffer BEFORE content_offset.
482 if (target - addr > min_entry_size) {
483 DEBUG("|min|...|header|content|... <create new entry>\n");
484 len = target - addr - min_entry_size;
Patrick Georgiedf25d92015-08-12 09:12:06 +0200485 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800486 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
487 entry = cbfs_find_next_entry(image, entry);
488 addr = cbfs_get_entry_addr(image, entry);
489 }
490
491 len = size + (content_offset - addr - header_size);
Patrick Georgiedf25d92015-08-12 09:12:06 +0200492 cbfs_create_empty_entry(entry, type, len, name);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800493 if (len != size) {
494 DEBUG("|..|header|content|... <use offset to create entry>\n");
495 DEBUG("before: offset=0x%x, len=0x%x\n",
496 ntohl(entry->offset), ntohl(entry->len));
497 // TODO reset expanded name buffer to 0xFF.
498 entry->offset = htonl(ntohl(entry->offset) + (len - size));
499 entry->len = htonl(size);
500 DEBUG("after: offset=0x%x, len=0x%x\n",
501 ntohl(entry->offset), ntohl(entry->len));
502 }
503
504 // Ready to fill data into entry.
505 assert(ntohl(entry->len) == size);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800506 DEBUG("content_offset: 0x%x, entry location: %x\n",
507 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
508 image->buffer.data));
509 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
Patrick Georgicccc9d42015-04-28 13:09:36 +0200510 (ptrdiff_t)content_offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800511 memcpy(CBFS_SUBHEADER(entry), data, size);
512 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
513
514 // Process buffer AFTER entry.
515 entry = cbfs_find_next_entry(image, entry);
516 addr = cbfs_get_entry_addr(image, entry);
Sol Boucher05725652015-04-02 20:58:26 -0700517 if (addr == addr_next)
518 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800519
Sol Boucher05725652015-04-02 20:58:26 -0700520 assert(addr < addr_next);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800521 if (addr_next - addr < min_entry_size) {
Sol Boucher636cc852015-04-03 09:13:04 -0700522 DEBUG("No need for new \"empty\" entry\n");
523 /* No need to increase the size of the just
524 * stored file to extend to next file. Alignment
525 * of next file takes care of this.
526 */
527 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800528 }
529
530 len = addr_next - addr - min_entry_size;
Patrick Georgiedf25d92015-08-12 09:12:06 +0200531 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800532 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
533 return 0;
534}
535
536int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Patrick Georgie60b55a2015-08-11 14:54:24 +0200537 const char *name, uint32_t type, uint32_t content_offset,
538 uint32_t header_size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700539{
Sol Boucher67d59982015-05-07 02:39:22 -0700540 assert(image);
541 assert(buffer);
542 assert(buffer->data);
543 assert(name);
544 assert(!IS_TOP_ALIGNED_ADDRESS(content_offset));
545
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800546 uint32_t entry_type;
547 uint32_t addr, addr_next;
548 struct cbfs_file *entry, *next;
Patrick Georgie60b55a2015-08-11 14:54:24 +0200549 uint32_t need_size, new_size;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800550
Patrick Georgie60b55a2015-08-11 14:54:24 +0200551 if (header_size == 0)
552 header_size = cbfs_calculate_file_header_size(name);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800553
554 need_size = header_size + buffer->size;
555 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
556 name, content_offset, header_size, buffer->size, need_size);
557
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800558 // Merge empty entries.
559 DEBUG("(trying to merge empty entries...)\n");
560 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
561
562 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800563 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800564 entry = cbfs_find_next_entry(image, entry)) {
565
566 entry_type = ntohl(entry->type);
567 if (entry_type != CBFS_COMPONENT_NULL)
568 continue;
569
570 addr = cbfs_get_entry_addr(image, entry);
571 next = cbfs_find_next_entry(image, entry);
572 addr_next = cbfs_get_entry_addr(image, next);
573
574 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
575 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600576
577 /* Will the file fit? Don't yet worry if we have space for a new
578 * "empty" entry. We take care of that later.
579 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800580 if (addr + need_size > addr_next)
581 continue;
582
583 // Can we simply put object here?
584 if (!content_offset || content_offset == addr + header_size) {
585 DEBUG("Filling new entry data (%zd bytes).\n",
586 buffer->size);
Patrick Georgiedf25d92015-08-12 09:12:06 +0200587 cbfs_create_empty_entry(entry, type, buffer->size, name);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800588 memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
589 if (verbose)
590 cbfs_print_entry_info(image, entry, stderr);
591
592 // setup new entry
Paul Menzel4159a802013-07-14 00:24:31 +0200593 DEBUG("Setting new empty entry.\n");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800594 entry = cbfs_find_next_entry(image, entry);
595 new_size = (cbfs_get_entry_addr(image, next) -
596 cbfs_get_entry_addr(image, entry));
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600597
598 /* Entry was added and no space for new "empty" entry */
599 if (new_size < cbfs_calculate_file_header_size("")) {
600 DEBUG("No need for new \"empty\" entry\n");
601 /* No need to increase the size of the just
602 * stored file to extend to next file. Alignment
603 * of next file takes care of this.
604 */
605 return 0;
606 }
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800607 new_size -= cbfs_calculate_file_header_size("");
608 DEBUG("new size: %d\n", new_size);
Patrick Georgiedf25d92015-08-12 09:12:06 +0200609 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL,
610 new_size, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800611 if (verbose)
612 cbfs_print_entry_info(image, entry, stderr);
613 return 0;
614 }
615
616 // We need to put content here, and the case is really
617 // complicated...
618 assert(content_offset);
619 if (addr_next < content_offset) {
620 DEBUG("Not for specified offset yet");
621 continue;
622 } else if (addr > content_offset) {
623 DEBUG("Exceed specified content_offset.");
624 break;
625 } else if (addr + header_size > content_offset) {
626 ERROR("Not enough space for header.\n");
627 break;
628 } else if (content_offset + buffer->size > addr_next) {
629 ERROR("Not enough space for content.\n");
630 break;
631 }
632
633 // TODO there are more few tricky cases that we may
634 // want to fit by altering offset.
635 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
636 addr, addr_next - addr, content_offset);
637
638 if (cbfs_add_entry_at(image, entry, buffer->size, name, type,
Patrick Georgi7fd14182015-08-11 15:55:16 +0200639 buffer->data, content_offset,
640 header_size) == 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800641 return 0;
642 }
643 break;
644 }
645
646 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
647 buffer->name, buffer->size, buffer->size / 1024, content_offset);
648 return -1;
649}
650
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700651struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
652{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800653 struct cbfs_file *entry;
654 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800655 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800656 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgic569b8b2015-07-15 16:42:38 +0200657 if (strcasecmp(entry->filename, name) == 0) {
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800658 DEBUG("cbfs_get_entry: found %s\n", name);
659 return entry;
660 }
661 }
662 return NULL;
663}
664
665int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700666 const char *filename)
667{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800668 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
669 struct buffer buffer;
670 if (!entry) {
671 ERROR("File not found: %s\n", entry_name);
672 return -1;
673 }
674 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
675 entry_name, cbfs_get_entry_addr(image, entry),
676 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
677
678 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
679 WARN("Only 'raw' files are safe to extract.\n");
680 }
681
682 buffer.data = CBFS_SUBHEADER(entry);
683 buffer.size = ntohl(entry->len);
Sol Boucher0e539312015-03-05 15:38:03 -0800684 buffer.name = strdup("(cbfs_export_entry)");
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800685 if (buffer_write_file(&buffer, filename) != 0) {
686 ERROR("Failed to write %s into %s.\n",
687 entry_name, filename);
Sol Boucher0e539312015-03-05 15:38:03 -0800688 free(buffer.name);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800689 return -1;
690 }
Sol Boucher0e539312015-03-05 15:38:03 -0800691 free(buffer.name);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800692 INFO("Successfully dumped the file to: %s\n", filename);
693 return 0;
694}
695
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700696int cbfs_remove_entry(struct cbfs_image *image, const char *name)
697{
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +0200698 struct cbfs_file *entry;
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800699 entry = cbfs_get_entry(image, name);
700 if (!entry) {
701 ERROR("CBFS file %s not found.\n", name);
702 return -1;
703 }
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800704 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
Patrick Georgic569b8b2015-07-15 16:42:38 +0200705 entry->filename, cbfs_get_entry_addr(image, entry));
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800706 entry->type = htonl(CBFS_COMPONENT_DELETED);
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +0200707 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800708 return 0;
709}
710
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700711int cbfs_print_header_info(struct cbfs_image *image)
712{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800713 char *name = strdup(image->buffer.name);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700714 assert(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800715 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800716 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800717 basename(name),
718 image->buffer.size / 1024,
Sol Boucher3e060ed2015-05-05 15:40:15 -0700719 image->header.bootblocksize,
720 image->header.romsize,
721 image->header.offset,
722 image->header.align,
723 arch_to_string(image->header.architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800724 free(name);
725 return 0;
726}
727
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700728static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
729{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800730 fprintf(fp,
731 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
732 "length: %d/%d\n",
733 lookup_name_by_type(types_cbfs_compression,
734 stage->compression, "(unknown)"),
735 stage->entry,
736 stage->load,
737 stage->len,
738 stage->memlen);
739 return 0;
740}
741
Hung-Te Lin0780d672014-05-16 10:14:05 +0800742static int cbfs_print_decoded_payload_segment_info(
743 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800744{
Hung-Te Lin0780d672014-05-16 10:14:05 +0800745 /* The input (seg) must be already decoded by
746 * cbfs_decode_payload_segment.
747 */
748 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800749 case PAYLOAD_SEGMENT_CODE:
750 case PAYLOAD_SEGMENT_DATA:
751 fprintf(fp, " %s (%s compression, offset: 0x%x, "
752 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800753 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800754 "code " : "data"),
755 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800756 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800757 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800758 seg->offset, seg->load_addr, seg->len,
759 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800760 break;
761
762 case PAYLOAD_SEGMENT_ENTRY:
763 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800764 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800765 break;
766
767 case PAYLOAD_SEGMENT_BSS:
768 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
769 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800770 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800771 break;
772
773 case PAYLOAD_SEGMENT_PARAMS:
774 fprintf(fp, " parameters\n");
775 break;
776
777 default:
778 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
779 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800780 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800781 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800782 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800783 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800784 seg->offset, seg->load_addr, seg->len,
785 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800786 break;
787 }
788 return 0;
789}
790
791int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700792 void *arg)
793{
Patrick Georgic569b8b2015-07-15 16:42:38 +0200794 const char *name = entry->filename;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800795 struct cbfs_payload_segment *payload;
796 FILE *fp = (FILE *)arg;
797
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800798 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800799 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
800 cbfs_get_entry_addr(image, entry));
801 return -1;
802 }
803 if (!fp)
804 fp = stdout;
805
806 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
807 *name ? name : "(empty)",
808 cbfs_get_entry_addr(image, entry),
809 get_cbfs_entry_type_name(ntohl(entry->type)),
810 ntohl(entry->len));
811
812 if (!verbose)
813 return 0;
814
815 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
816 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
817 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
818 ntohl(entry->len));
819
820 /* note the components of the subheader may be in host order ... */
821 switch (ntohl(entry->type)) {
822 case CBFS_COMPONENT_STAGE:
823 cbfs_print_stage_info((struct cbfs_stage *)
824 CBFS_SUBHEADER(entry), fp);
825 break;
826
827 case CBFS_COMPONENT_PAYLOAD:
828 payload = (struct cbfs_payload_segment *)
829 CBFS_SUBHEADER(entry);
830 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +0800831 struct cbfs_payload_segment seg;
832 cbfs_decode_payload_segment(&seg, payload);
833 cbfs_print_decoded_payload_segment_info(
834 &seg, fp);
835 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800836 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +0800837 else
Aaron Durbinca630272014-08-05 10:48:20 -0500838 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800839 }
840 break;
841 default:
842 break;
843 }
844 return 0;
845}
846
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700847int cbfs_print_directory(struct cbfs_image *image)
848{
Sol Boucher67a0a862015-03-18 12:36:27 -0700849 if (cbfs_is_legacy_cbfs(image))
850 cbfs_print_header_info(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800851 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
852 cbfs_walk(image, cbfs_print_entry_info, NULL);
853 return 0;
854}
855
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800856int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -0800857 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700858{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800859 struct cbfs_file *next;
860 uint32_t type, addr, last_addr;
861
862 type = ntohl(entry->type);
863 if (type == CBFS_COMPONENT_DELETED) {
864 // Ready to be recycled.
865 type = CBFS_COMPONENT_NULL;
866 entry->type = htonl(type);
867 }
868 if (type != CBFS_COMPONENT_NULL)
869 return 0;
870
871 next = cbfs_find_next_entry(image, entry);
872
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800873 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800874 type = ntohl(next->type);
875 if (type == CBFS_COMPONENT_DELETED) {
876 type = CBFS_COMPONENT_NULL;
877 next->type = htonl(type);
878 }
879 if (type != CBFS_COMPONENT_NULL)
880 return 0;
881
882 addr = cbfs_get_entry_addr(image, entry);
883 last_addr = cbfs_get_entry_addr(
884 image, cbfs_find_next_entry(image, next));
885
886 // Now, we find two deleted/empty entries; try to merge now.
887 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
888 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
889 cbfs_get_entry_addr(image, next), ntohl(next->len));
Patrick Georgiedf25d92015-08-12 09:12:06 +0200890 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL,
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800891 (last_addr - addr -
892 cbfs_calculate_file_header_size("")),
893 "");
894 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
895 next = cbfs_find_next_entry(image, entry);
896 }
897 return 0;
898}
899
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800900int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700901 void *arg)
902{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800903 int count = 0;
904 struct cbfs_file *entry;
905 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800906 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800907 entry = cbfs_find_next_entry(image, entry)) {
908 count ++;
909 if (callback(image, entry, arg) != 0)
910 break;
911 }
912 return count;
913}
914
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800915static int cbfs_header_valid(struct cbfs_header *header, size_t size)
916{
917 if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
918 ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
919 (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
920 (ntohl(header->romsize) <= size) &&
921 (ntohl(header->offset) < ntohl(header->romsize)))
922 return 1;
923 return 0;
924}
925
926struct cbfs_header *cbfs_find_header(char *data, size_t size,
927 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700928{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800929 size_t offset;
930 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800931 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800932 struct cbfs_header *header, *result = NULL;
933
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800934 if (forced_offset < (size - sizeof(struct cbfs_header))) {
935 /* Check if the forced header is valid. */
936 header = (struct cbfs_header *)(data + forced_offset);
937 if (cbfs_header_valid(header, size))
938 return header;
939 return NULL;
940 }
941
Julius Wernerefcee762014-11-10 13:14:24 -0800942 // Try finding relative offset of master header at end of file first.
943 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
944 offset = size + rel_offset;
945 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
946 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800947
Hung-Te Lineab2c812013-01-29 01:56:17 +0800948 if (offset >= size - sizeof(*header) ||
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800949 !cbfs_header_valid((struct cbfs_header *)(data + offset), size)) {
Julius Wernerefcee762014-11-10 13:14:24 -0800950 // Some use cases append non-CBFS data to the end of the ROM.
951 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +0800952 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800953 }
Hung-Te Lineab2c812013-01-29 01:56:17 +0800954
955 for (; offset + sizeof(*header) < size; offset++) {
956 header = (struct cbfs_header *)(data + offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800957 if (!cbfs_header_valid(header, size))
Hung-Te Lineab2c812013-01-29 01:56:17 +0800958 continue;
Julius Wernerefcee762014-11-10 13:14:24 -0800959 if (!found++)
960 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800961 }
Julius Wernerefcee762014-11-10 13:14:24 -0800962 if (found > 1)
963 // Top-aligned images usually have a working relative offset
964 // field, so this is more likely to happen on bottom-aligned
965 // ones (where the first header is the "outermost" one)
966 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +0800967 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800968 return result;
969}
970
971
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700972struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
973{
Sol Boucher3e060ed2015-05-05 15:40:15 -0700974 assert(image);
Sol Boucher67a0a862015-03-18 12:36:27 -0700975 return image->has_header ? (struct cbfs_file *)(image->buffer.data +
976 image->header.offset) :
977 (struct cbfs_file *)image->buffer.data;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800978}
979
980struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700981 struct cbfs_file *entry)
982{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800983 uint32_t addr = cbfs_get_entry_addr(image, entry);
Sol Boucher67a0a862015-03-18 12:36:27 -0700984 int align = image->has_header ? image->header.align :
985 CBFS_ENTRY_ALIGNMENT;
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800986 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +0800987 addr += ntohl(entry->offset) + ntohl(entry->len);
988 addr = align_up(addr, align);
989 return (struct cbfs_file *)(image->buffer.data + addr);
990}
991
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700992uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
993{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800994 assert(image && image->buffer.data && entry);
995 return (int32_t)((char *)entry - image->buffer.data);
996}
997
Sol Boucher67a0a862015-03-18 12:36:27 -0700998int cbfs_is_valid_cbfs(struct cbfs_image *image)
999{
1000 return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC,
1001 strlen(CBFS_FILE_MAGIC));
1002}
1003
1004int cbfs_is_legacy_cbfs(struct cbfs_image *image)
1005{
1006 return image->has_header;
1007}
1008
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001009int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
1010{
Sol Bouchere3260a02015-03-25 13:40:08 -07001011 uint32_t offset = cbfs_get_entry_addr(image, entry);
1012
1013 if (offset >= image->buffer.size)
1014 return 0;
1015
1016 struct buffer entry_data;
1017 buffer_clone(&entry_data, &image->buffer);
1018 buffer_seek(&entry_data, offset);
1019 return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
Sol Boucher67a0a862015-03-18 12:36:27 -07001020 strlen(CBFS_FILE_MAGIC));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001021}
1022
Patrick Georgi57edf162015-08-12 09:20:11 +02001023struct cbfs_file *cbfs_create_file_header(int type,
Vadim Bendebury45e59972014-12-23 15:59:57 -08001024 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001025{
Patrick Georgi57edf162015-08-12 09:20:11 +02001026 // assume that there won't be file names of ~1000 bytes
1027 const int bufsize = 1024;
1028
1029 struct cbfs_file *entry = malloc(bufsize);
1030 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, bufsize);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001031 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
Patrick Georgiedf25d92015-08-12 09:12:06 +02001032 entry->type = htonl(type);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001033 entry->len = htonl(len);
Patrick Georgi0d618af2015-07-15 18:28:23 +02001034 entry->attributes_offset = 0;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001035 entry->offset = htonl(cbfs_calculate_file_header_size(name));
Patrick Georgic569b8b2015-07-15 16:42:38 +02001036 memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry));
1037 strcpy(entry->filename, name);
Patrick Georgi57edf162015-08-12 09:20:11 +02001038 return entry;
1039}
1040
1041int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
1042 size_t len, const char *name)
1043{
1044 struct cbfs_file *tmp = cbfs_create_file_header(type, len, name);
1045 memcpy(entry, tmp, ntohl(tmp->offset));
1046 free(tmp);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001047 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1048 return 0;
1049}
1050
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001051/* Finds a place to hold whole data in same memory page. */
1052static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1053{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001054 if (!page)
1055 return 1;
1056 return (start / page) == (start + size - 1) / page;
1057}
1058
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001059/* Tests if data can fit in a range by given offset:
1060 * start ->| header_len | offset (+ size) |<- end
1061 */
1062static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001063 uint32_t offset, uint32_t size)
1064{
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001065 return (offset >= start + header_len && offset + size <= end);
1066}
1067
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001068int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001069 uint32_t size, uint32_t page_size, uint32_t align)
1070{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001071 struct cbfs_file *entry;
1072 size_t need_len;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001073 uint32_t addr, addr_next, addr2, addr3, offset, header_len;
1074
1075 /* Default values: allow fitting anywhere in ROM. */
1076 if (!page_size)
Sol Boucher67a0a862015-03-18 12:36:27 -07001077 page_size = image->has_header ? image->header.romsize :
1078 image->buffer.size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001079 if (!align)
1080 align = 1;
1081
1082 if (size > page_size)
1083 ERROR("Input file size (%d) greater than page size (%d).\n",
1084 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001085
Sol Boucher67a0a862015-03-18 12:36:27 -07001086 uint32_t image_align = image->has_header ? image->header.align :
1087 CBFS_ENTRY_ALIGNMENT;
1088 if (page_size % image_align)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001089 WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001090 __func__, page_size, image_align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001091
1092 /* TODO Old cbfstool always assume input is a stage file (and adding
1093 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001094 * (type) param in future. For right now, we assume cbfs_stage is the
1095 * largest structure and add it into header size. */
1096 assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001097 header_len = (cbfs_calculate_file_header_size(name) +
1098 sizeof(struct cbfs_stage));
1099 need_len = header_len + size;
1100
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001101 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001102 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
1103
1104 /* Three cases of content location on memory page:
1105 * case 1.
1106 * | PAGE 1 | PAGE 2 |
1107 * | <header><content>| Fit. Return start of content.
1108 *
1109 * case 2.
1110 * | PAGE 1 | PAGE 2 |
1111 * | <header><content> | Fits when we shift content to align
1112 * shift-> | <header>|<content> | at starting of PAGE 2.
1113 *
1114 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001115 * | PAGE 1 | PAGE 2 | PAGE 3 |
1116 * | <header>< content > | Can't fit. If we shift content to
1117 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1118 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001119 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001120 * The returned address can be then used as "base-address" (-b) in add-*
1121 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1122 * For stage targets, the address is also used to re-link stage before
1123 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001124 */
1125 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001126 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001127 entry = cbfs_find_next_entry(image, entry)) {
1128
1129 uint32_t type = ntohl(entry->type);
1130 if (type != CBFS_COMPONENT_NULL)
1131 continue;
1132
1133 addr = cbfs_get_entry_addr(image, entry);
1134 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
1135 image, entry));
1136 if (addr_next - addr < need_len)
1137 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001138
1139 offset = align_up(addr + header_len, align);
1140 if (is_in_same_page(offset, size, page_size) &&
1141 is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001142 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001143 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001144 }
1145
1146 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001147 offset = align_up(addr2, align);
1148 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001149 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001150 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001151 }
1152
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001153 /* Assume page_size >= header_len so adding one page will
1154 * definitely provide the space for header. */
1155 assert(page_size >= header_len);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001156 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001157 offset = align_up(addr3, align);
1158 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001159 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001160 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001161 }
1162 }
1163 return -1;
1164}