blob: 55f80844db8fae0b5d7ae0971878b5c26bb4e29f [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
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070055static const struct typedesc_t types_cbfs_compression[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080056 {CBFS_COMPRESS_NONE, "none"},
57 {CBFS_COMPRESS_LZMA, "LZMA"},
Sol Boucher5bb90e62015-05-07 21:00:05 -070058 {0, NULL}
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080059};
60
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080061static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070062 const char *default_value)
63{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080064 int i;
65 for (i = 0; desc[i].name; i++)
66 if (desc[i].type == type)
67 return desc[i].name;
68 return default_value;
69}
70
Sol Boucherec424862015-05-07 21:00:05 -070071static int lookup_type_by_name(const struct typedesc_t *desc, const char *name)
72{
73 int i;
74 for (i = 0; desc[i].name && strcasecmp(name, desc[i].name); ++i);
75 return desc[i].name ? (int)desc[i].type : -1;
76}
77
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010078static const char *get_cbfs_entry_type_name(uint32_t type)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070079{
Patrick Georgidc37dab2015-09-09 16:46:00 +020080 return lookup_name_by_type(filetypes, type, "(unknown)");
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080081}
82
Sol Boucherec424862015-05-07 21:00:05 -070083int cbfs_parse_comp_algo(const char *name)
84{
85 return lookup_type_by_name(types_cbfs_compression, name);
86}
87
Hung-Te Linc03d9b02013-01-29 02:38:40 +080088/* CBFS image */
89
Patrick Georgi11ee08f2015-08-11 15:10:02 +020090size_t cbfs_calculate_file_header_size(const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070091{
Hung-Te Linc03d9b02013-01-29 02:38:40 +080092 return (sizeof(struct cbfs_file) +
93 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
94}
95
Sol Boucher67a0a862015-03-18 12:36:27 -070096/* Only call on legacy CBFSes possessing a master header. */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -060097static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070098{
Sol Boucher67a0a862015-03-18 12:36:27 -070099 assert(image);
100 assert(cbfs_is_legacy_cbfs(image));
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800101 // A bug in old cbfstool may produce extra few bytes (by alignment) and
102 // cause cbfstool to overwrite things after free space -- which is
103 // usually CBFS header on x86. We need to workaround that.
104
105 struct cbfs_file *entry, *first = NULL, *last = NULL;
106 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800107 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800108 entry = cbfs_find_next_entry(image, entry)) {
109 last = entry;
110 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600111 if ((char *)first < (char *)hdr_loc &&
112 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800113 WARN("CBFS image was created with old cbfstool with size bug. "
114 "Fixing size in last entry...\n");
Sol Boucher3e060ed2015-05-05 15:40:15 -0700115 last->len = htonl(ntohl(last->len) - image->header.align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800116 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
117 cbfs_get_entry_addr(image, entry),
118 cbfs_get_entry_addr(image,
119 cbfs_find_next_entry(image, last)));
120 }
121 return 0;
122}
123
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800124void cbfs_put_header(void *dest, const struct cbfs_header *header)
125{
126 struct buffer outheader;
127
128 outheader.data = dest;
129 outheader.size = 0;
130
131 xdr_be.put32(&outheader, header->magic);
132 xdr_be.put32(&outheader, header->version);
133 xdr_be.put32(&outheader, header->romsize);
134 xdr_be.put32(&outheader, header->bootblocksize);
135 xdr_be.put32(&outheader, header->align);
136 xdr_be.put32(&outheader, header->offset);
137 xdr_be.put32(&outheader, header->architecture);
138}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600139
Hung-Te Lin0780d672014-05-16 10:14:05 +0800140static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
141 struct cbfs_payload_segment *input)
142{
143 struct buffer seg = {
144 .data = (void *)input,
145 .size = sizeof(*input),
146 };
147 output->type = xdr_be.get32(&seg);
148 output->compression = xdr_be.get32(&seg);
149 output->offset = xdr_be.get32(&seg);
150 output->load_addr = xdr_be.get64(&seg);
151 output->len = xdr_be.get32(&seg);
152 output->mem_len = xdr_be.get32(&seg);
153 assert(seg.size == 0);
154}
155
Patrick Georgia71c83f2015-08-26 12:23:26 +0200156static int cbfs_file_get_compression_info(struct cbfs_file *entry,
157 uint32_t *decompressed_size)
158{
159 unsigned int compression = CBFS_COMPRESS_NONE;
160 *decompressed_size = ntohl(entry->len);
161 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
162 attr != NULL;
163 attr = cbfs_file_next_attr(entry, attr)) {
164 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) {
165 struct cbfs_file_attr_compression *ac =
166 (struct cbfs_file_attr_compression *)attr;
167 compression = ntohl(ac->compression);
168 if (decompressed_size)
169 *decompressed_size =
170 ntohl(ac->decompressed_size);
171 }
172 }
173 return compression;
174}
175
Sol Boucher0e539312015-03-05 15:38:03 -0800176void cbfs_get_header(struct cbfs_header *header, void *src)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600177{
178 struct buffer outheader;
179
Sol Boucher0e539312015-03-05 15:38:03 -0800180 outheader.data = src; /* We're not modifying the data */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600181 outheader.size = 0;
182
183 header->magic = xdr_be.get32(&outheader);
184 header->version = xdr_be.get32(&outheader);
185 header->romsize = xdr_be.get32(&outheader);
186 header->bootblocksize = xdr_be.get32(&outheader);
187 header->align = xdr_be.get32(&outheader);
188 header->offset = xdr_be.get32(&outheader);
189 header->architecture = xdr_be.get32(&outheader);
190}
191
Sol Boucher67a0a862015-03-18 12:36:27 -0700192int cbfs_image_create(struct cbfs_image *image, size_t entries_size)
193{
194 assert(image);
195 assert(image->buffer.data);
196
197 size_t empty_header_len = cbfs_calculate_file_header_size("");
198 uint32_t entries_offset = 0;
199 uint32_t align = CBFS_ENTRY_ALIGNMENT;
200 if (image->has_header) {
201 entries_offset = image->header.offset;
202
203 if (entries_offset > image->buffer.size) {
204 ERROR("CBFS file entries are located outside CBFS itself\n");
205 return -1;
206 }
207
208 align = image->header.align;
209 }
210
211 // This attribute must be given in order to prove that this module
212 // correctly preserves certain CBFS properties. See the block comment
213 // near the top of this file (and the associated commit message).
214 if (align < empty_header_len) {
215 ERROR("CBFS must be aligned to at least %zu bytes\n",
216 empty_header_len);
217 return -1;
218 }
219
220 if (entries_size > image->buffer.size - entries_offset) {
221 ERROR("CBFS doesn't have enough space to fit its file entries\n");
222 return -1;
223 }
224
225 if (empty_header_len > entries_size) {
226 ERROR("CBFS is too small to fit any header\n");
227 return -1;
228 }
229 struct cbfs_file *entry_header =
230 (struct cbfs_file *)(image->buffer.data + entries_offset);
231 // This alignment is necessary in order to prove that this module
232 // correctly preserves certain CBFS properties. See the block comment
233 // near the top of this file (and the associated commit message).
234 entries_size -= entries_size % align;
235
236 size_t capacity = entries_size - empty_header_len;
237 LOG("Created CBFS (capacity = %zu bytes)\n", capacity);
Patrick Georgiedf25d92015-08-12 09:12:06 +0200238 return cbfs_create_empty_entry(entry_header, CBFS_COMPONENT_NULL,
239 capacity, "");
Sol Boucher67a0a862015-03-18 12:36:27 -0700240}
241
242int cbfs_legacy_image_create(struct cbfs_image *image,
243 uint32_t architecture,
244 uint32_t align,
245 struct buffer *bootblock,
246 uint32_t bootblock_offset,
247 uint32_t header_offset,
248 uint32_t entries_offset)
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800249{
Sol Bouchere3260a02015-03-25 13:40:08 -0700250 assert(image);
251 assert(image->buffer.data);
252 assert(bootblock);
253
Julius Wernerefcee762014-11-10 13:14:24 -0800254 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800255 uint32_t cbfs_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600256 void *header_loc;
Sol Bouchere3260a02015-03-25 13:40:08 -0700257 size_t size = image->buffer.size;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800258
259 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
260 "header=0x%x+0x%zx, entries_offset=0x%x\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700261 bootblock_offset, bootblock->size, header_offset,
262 sizeof(image->header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800263
Sol Boucher67a0a862015-03-18 12:36:27 -0700264 // Adjust legacy top-aligned address to ROM offset.
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800265 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800266 entries_offset = size + (int32_t)entries_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800267 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800268 bootblock_offset = size + (int32_t)bootblock_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800269 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800270 header_offset = size + (int32_t)header_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800271
272 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
273 "header=0x%x, entries_offset=0x%x\n",
274 bootblock_offset, header_offset, entries_offset);
275
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800276 // Prepare bootblock
277 if (bootblock_offset + bootblock->size > size) {
278 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
279 bootblock_offset, bootblock->size, size);
280 return -1;
281 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800282 if (entries_offset > bootblock_offset &&
283 entries_offset < bootblock->size) {
284 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
285 bootblock_offset, bootblock->size, entries_offset);
286 return -1;
287 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800288 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
289 bootblock->size);
290
291 // Prepare header
Sol Boucher5bad3952015-05-05 20:35:26 -0700292 if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800293 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700294 header_offset, sizeof(image->header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800295 return -1;
296 }
Sol Boucher3e060ed2015-05-05 15:40:15 -0700297 image->header.magic = CBFS_HEADER_MAGIC;
298 image->header.version = CBFS_HEADER_VERSION;
299 image->header.romsize = size;
300 image->header.bootblocksize = bootblock->size;
301 image->header.align = align;
302 image->header.offset = entries_offset;
303 image->header.architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600304
305 header_loc = (image->buffer.data + header_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700306 cbfs_put_header(header_loc, &image->header);
Sol Boucher67a0a862015-03-18 12:36:27 -0700307 image->has_header = true;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800308
Julius Wernerefcee762014-11-10 13:14:24 -0800309 // The last 4 byte of the image contain the relative offset from the end
310 // of the image to the master header as a 32-bit signed integer. x86
311 // relies on this also being its (memory-mapped, top-aligned) absolute
312 // 32-bit address by virtue of how two's complement numbers work.
313 assert(size % sizeof(int32_t) == 0);
314 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
315 *rel_offset = header_offset - size;
316
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800317 // Prepare entries
318 if (align_up(entries_offset, align) != entries_offset) {
319 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
320 entries_offset, align);
321 return -1;
322 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800323 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800324 // e = min(bootblock, header, rel_offset) where e > entries_offset.
325 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800326 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
327 cbfs_len = bootblock_offset;
328 if (header_offset > entries_offset && header_offset < cbfs_len)
329 cbfs_len = header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700330
331 if (cbfs_image_create(image, cbfs_len - entries_offset))
332 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800333 return 0;
334}
335
Sol Bouchere3260a02015-03-25 13:40:08 -0700336int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
337 uint32_t offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700338{
Sol Bouchere3260a02015-03-25 13:40:08 -0700339 assert(out);
340 assert(in);
341 assert(in->data);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600342
Sol Bouchere3260a02015-03-25 13:40:08 -0700343 buffer_clone(&out->buffer, in);
Sol Boucher67a0a862015-03-18 12:36:27 -0700344 out->has_header = false;
345
Sol Bouchere3260a02015-03-25 13:40:08 -0700346 void *header_loc = cbfs_find_header(in->data, in->size, offset);
347 if (header_loc) {
348 cbfs_get_header(&out->header, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700349 out->has_header = true;
Sol Bouchere3260a02015-03-25 13:40:08 -0700350 cbfs_fix_legacy_size(out, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700351 } else if (offset != ~0u) {
352 ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n");
353 return 1;
354 } else if (!cbfs_is_valid_cbfs(out)) {
355 ERROR("Selected image region is not a valid CBFS.\n");
356 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800357 }
Sol Boucher67a0a862015-03-18 12:36:27 -0700358
359 return 0;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800360}
361
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800362int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
363 size_t copy_size)
364{
Sol Boucher67a0a862015-03-18 12:36:27 -0700365 assert(image);
366 if (!cbfs_is_legacy_cbfs(image))
367 return -1;
368
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800369 struct cbfs_file *src_entry, *dst_entry;
370 struct cbfs_header *copy_header;
371 size_t align, entry_offset;
372 ssize_t last_entry_size;
373
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800374 size_t cbfs_offset, cbfs_end;
375 size_t copy_end = copy_offset + copy_size;
376
Sol Boucher3e060ed2015-05-05 15:40:15 -0700377 align = image->header.align;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800378
Sol Boucher3e060ed2015-05-05 15:40:15 -0700379 cbfs_offset = image->header.offset;
380 cbfs_end = image->header.romsize;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800381
382 if (copy_end > image->buffer.size) {
383 ERROR("Copy offset out of range: [%zx:%zx)\n",
384 copy_offset, copy_end);
385 return 1;
386 }
387
Sol Boucher297c88c2015-05-05 15:35:18 -0700388 /* Range check requested copy region with source cbfs. */
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800389 if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) ||
390 (copy_end >= cbfs_offset && copy_end <= cbfs_end)) {
391 ERROR("New image would overlap old one.\n");
392 return 1;
393 }
394
395 /* This will work, let's create a copy. */
396 copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700397 cbfs_put_header(copy_header, &image->header);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800398
399 copy_header->bootblocksize = 0;
400 /* Romsize is a misnomer. It's the absolute limit of cbfs content.*/
401 copy_header->romsize = htonl(copy_end);
402 entry_offset = align_up(copy_offset + sizeof(*copy_header), align);
403 copy_header->offset = htonl(entry_offset);
404 dst_entry = (struct cbfs_file *)(image->buffer.data + entry_offset);
405
406 /* Copy non-empty files */
407 for (src_entry = cbfs_find_first_entry(image);
408 src_entry && cbfs_is_valid_entry(image, src_entry);
409 src_entry = cbfs_find_next_entry(image, src_entry)) {
410 size_t entry_size;
411
412 if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) ||
413 (src_entry->type == htonl(CBFS_COMPONENT_DELETED)))
414 continue;
415
416 entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
417 memcpy(dst_entry, src_entry, entry_size);
418 dst_entry = (struct cbfs_file *)(
419 (uintptr_t)dst_entry + align_up(entry_size, align));
420
Sol Boucher0e539312015-03-05 15:38:03 -0800421 if ((size_t)((char *)dst_entry - image->buffer.data) >=
422 copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800423 ERROR("Ran out of room in copy region.\n");
424 return 1;
425 }
426 }
427
428 /* Last entry size is all the room above it. */
429 last_entry_size = copy_end - ((char *)dst_entry - image->buffer.data)
430 - cbfs_calculate_file_header_size("");
431
432 if (last_entry_size < 0)
433 WARN("No room to create the last entry!\n")
434 else
Patrick Georgiedf25d92015-08-12 09:12:06 +0200435 cbfs_create_empty_entry(dst_entry, CBFS_COMPONENT_NULL,
436 last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800437
438 return 0;
439}
440
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700441int cbfs_image_delete(struct cbfs_image *image)
442{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100443 if (image == NULL)
444 return 0;
445
Hung-Te Lineab2c812013-01-29 01:56:17 +0800446 buffer_delete(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800447 return 0;
448}
449
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800450/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
451static int cbfs_add_entry_at(struct cbfs_image *image,
452 struct cbfs_file *entry,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800453 const void *data,
Patrick Georgi7fd14182015-08-11 15:55:16 +0200454 uint32_t content_offset,
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200455 const struct cbfs_file *header)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700456{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800457 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
458 uint32_t addr = cbfs_get_entry_addr(image, entry),
459 addr_next = cbfs_get_entry_addr(image, next);
Patrick Georgi7fd14182015-08-11 15:55:16 +0200460 uint32_t min_entry_size = cbfs_calculate_file_header_size("");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200461 uint32_t len, header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700462 uint32_t align = image->has_header ? image->header.align :
463 CBFS_ENTRY_ALIGNMENT;
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200464 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800465
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200466 header_offset = content_offset - header_size;
467 if (header_offset % align)
468 header_offset -= header_offset % align;
469 if (header_offset < addr) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800470 ERROR("No space to hold cbfs_file header.");
471 return -1;
472 }
473
474 // Process buffer BEFORE content_offset.
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200475 if (header_offset - addr > min_entry_size) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800476 DEBUG("|min|...|header|content|... <create new entry>\n");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200477 len = header_offset - addr - min_entry_size;
Patrick Georgiedf25d92015-08-12 09:12:06 +0200478 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800479 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
480 entry = cbfs_find_next_entry(image, entry);
481 addr = cbfs_get_entry_addr(image, entry);
482 }
483
Patrick Georgi7a33b532015-08-25 13:00:04 +0200484 len = content_offset - addr - header_size;
Patrick Georgia60e7b62015-08-25 22:26:02 +0200485 memcpy(entry, header, header_size);
Patrick Georgi7a33b532015-08-25 13:00:04 +0200486 if (len != 0) {
487 /* the header moved backwards a bit to accomodate cbfs_file
488 * alignment requirements, so patch up ->offset to still point
489 * to file data.
490 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800491 DEBUG("|..|header|content|... <use offset to create entry>\n");
Patrick Georgiae7efb92015-08-25 13:11:28 +0200492 DEBUG("before: offset=0x%x\n", ntohl(entry->offset));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800493 // TODO reset expanded name buffer to 0xFF.
Patrick Georgi7a33b532015-08-25 13:00:04 +0200494 entry->offset = htonl(ntohl(entry->offset) + len);
Patrick Georgiae7efb92015-08-25 13:11:28 +0200495 DEBUG("after: offset=0x%x\n", ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800496 }
497
498 // Ready to fill data into entry.
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800499 DEBUG("content_offset: 0x%x, entry location: %x\n",
500 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
501 image->buffer.data));
502 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
Patrick Georgicccc9d42015-04-28 13:09:36 +0200503 (ptrdiff_t)content_offset);
Patrick Georgi19c80b22015-08-25 13:16:04 +0200504 memcpy(CBFS_SUBHEADER(entry), data, ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800505 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
506
507 // Process buffer AFTER entry.
508 entry = cbfs_find_next_entry(image, entry);
509 addr = cbfs_get_entry_addr(image, entry);
Sol Boucher05725652015-04-02 20:58:26 -0700510 if (addr == addr_next)
511 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800512
Sol Boucher05725652015-04-02 20:58:26 -0700513 assert(addr < addr_next);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800514 if (addr_next - addr < min_entry_size) {
Sol Boucher636cc852015-04-03 09:13:04 -0700515 DEBUG("No need for new \"empty\" entry\n");
516 /* No need to increase the size of the just
517 * stored file to extend to next file. Alignment
518 * of next file takes care of this.
519 */
520 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800521 }
522
523 len = addr_next - addr - min_entry_size;
Patrick Georgiedf25d92015-08-12 09:12:06 +0200524 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800525 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
526 return 0;
527}
528
529int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Patrick Georgie5903582015-08-25 13:53:42 +0200530 uint32_t content_offset,
Patrick Georgif5252f32015-08-25 22:27:57 +0200531 struct cbfs_file *header)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700532{
Sol Boucher67d59982015-05-07 02:39:22 -0700533 assert(image);
534 assert(buffer);
535 assert(buffer->data);
Sol Boucher67d59982015-05-07 02:39:22 -0700536 assert(!IS_TOP_ALIGNED_ADDRESS(content_offset));
537
Patrick Georgia60e7b62015-08-25 22:26:02 +0200538 const char *name = header->filename;
Patrick Georgie5903582015-08-25 13:53:42 +0200539
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800540 uint32_t entry_type;
541 uint32_t addr, addr_next;
542 struct cbfs_file *entry, *next;
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200543 uint32_t need_size;
Patrick Georgif5252f32015-08-25 22:27:57 +0200544 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800545
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800546 need_size = header_size + buffer->size;
547 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
548 name, content_offset, header_size, buffer->size, need_size);
549
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800550 // Merge empty entries.
551 DEBUG("(trying to merge empty entries...)\n");
552 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
553
554 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800555 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800556 entry = cbfs_find_next_entry(image, entry)) {
557
558 entry_type = ntohl(entry->type);
559 if (entry_type != CBFS_COMPONENT_NULL)
560 continue;
561
562 addr = cbfs_get_entry_addr(image, entry);
563 next = cbfs_find_next_entry(image, entry);
564 addr_next = cbfs_get_entry_addr(image, next);
565
566 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
567 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600568
569 /* Will the file fit? Don't yet worry if we have space for a new
570 * "empty" entry. We take care of that later.
571 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800572 if (addr + need_size > addr_next)
573 continue;
574
Patrick Georgiaa44dbd2015-08-12 12:05:21 +0200575 // Test for complicated cases
576 if (content_offset > 0) {
577 if (addr_next < content_offset) {
578 DEBUG("Not for specified offset yet");
579 continue;
580 } else if (addr > content_offset) {
581 DEBUG("Exceed specified content_offset.");
582 break;
583 } else if (addr + header_size > content_offset) {
584 ERROR("Not enough space for header.\n");
585 break;
586 } else if (content_offset + buffer->size > addr_next) {
587 ERROR("Not enough space for content.\n");
588 break;
589 }
590 }
591
592 // TODO there are more few tricky cases that we may
593 // want to fit by altering offset.
594
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200595 if (content_offset == 0) {
596 // we tested every condition earlier under which
597 // placing the file there might fail
598 content_offset = addr + header_size;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800599 }
600
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800601 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
602 addr, addr_next - addr, content_offset);
603
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200604 if (cbfs_add_entry_at(image, entry, buffer->data,
605 content_offset, header) == 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800606 return 0;
607 }
608 break;
609 }
610
611 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
612 buffer->name, buffer->size, buffer->size / 1024, content_offset);
613 return -1;
614}
615
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700616struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
617{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800618 struct cbfs_file *entry;
619 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800620 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800621 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgic569b8b2015-07-15 16:42:38 +0200622 if (strcasecmp(entry->filename, name) == 0) {
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800623 DEBUG("cbfs_get_entry: found %s\n", name);
624 return entry;
625 }
626 }
627 return NULL;
628}
629
630int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700631 const char *filename)
632{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800633 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
634 struct buffer buffer;
635 if (!entry) {
636 ERROR("File not found: %s\n", entry_name);
637 return -1;
638 }
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200639
640 unsigned int decompressed_size = 0;
641 unsigned int compression = cbfs_file_get_compression_info(entry,
642 &decompressed_size);
643
644 decomp_func_ptr decompress = decompression_function(compression);
645 if (!decompress) {
646 ERROR("looking up decompression routine failed\n");
647 return -1;
648 }
649
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800650 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
651 entry_name, cbfs_get_entry_addr(image, entry),
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200652 get_cbfs_entry_type_name(ntohl(entry->type)), decompressed_size);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800653
Patrick Georgi011b0b32015-08-26 12:16:54 +0200654 if (ntohl(entry->type) == CBFS_COMPONENT_STAGE) {
655 WARN("Stages are extracted in SELF format.\n");
656 }
657
658 if (ntohl(entry->type) == CBFS_COMPONENT_PAYLOAD) {
659 WARN("Payloads are extracted in SELF format.\n");
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800660 }
661
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200662 buffer.data = malloc(decompressed_size);
663 buffer.size = decompressed_size;
664 if (decompress(CBFS_SUBHEADER(entry), ntohl(entry->len),
665 buffer.data, buffer.size)) {
666 ERROR("decompression failed for %s\n", entry_name);
667 return -1;
668 }
Sol Boucher0e539312015-03-05 15:38:03 -0800669 buffer.name = strdup("(cbfs_export_entry)");
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800670 if (buffer_write_file(&buffer, filename) != 0) {
671 ERROR("Failed to write %s into %s.\n",
672 entry_name, filename);
Sol Boucher0e539312015-03-05 15:38:03 -0800673 free(buffer.name);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800674 return -1;
675 }
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200676 free(buffer.data);
Sol Boucher0e539312015-03-05 15:38:03 -0800677 free(buffer.name);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800678 INFO("Successfully dumped the file to: %s\n", filename);
679 return 0;
680}
681
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700682int cbfs_remove_entry(struct cbfs_image *image, const char *name)
683{
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +0200684 struct cbfs_file *entry;
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800685 entry = cbfs_get_entry(image, name);
686 if (!entry) {
687 ERROR("CBFS file %s not found.\n", name);
688 return -1;
689 }
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800690 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
Patrick Georgic569b8b2015-07-15 16:42:38 +0200691 entry->filename, cbfs_get_entry_addr(image, entry));
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800692 entry->type = htonl(CBFS_COMPONENT_DELETED);
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +0200693 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800694 return 0;
695}
696
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700697int cbfs_print_header_info(struct cbfs_image *image)
698{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800699 char *name = strdup(image->buffer.name);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700700 assert(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800701 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800702 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800703 basename(name),
704 image->buffer.size / 1024,
Sol Boucher3e060ed2015-05-05 15:40:15 -0700705 image->header.bootblocksize,
706 image->header.romsize,
707 image->header.offset,
708 image->header.align,
709 arch_to_string(image->header.architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800710 free(name);
711 return 0;
712}
713
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700714static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
715{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800716 fprintf(fp,
717 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
718 "length: %d/%d\n",
719 lookup_name_by_type(types_cbfs_compression,
720 stage->compression, "(unknown)"),
721 stage->entry,
722 stage->load,
723 stage->len,
724 stage->memlen);
725 return 0;
726}
727
Hung-Te Lin0780d672014-05-16 10:14:05 +0800728static int cbfs_print_decoded_payload_segment_info(
729 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800730{
Hung-Te Lin0780d672014-05-16 10:14:05 +0800731 /* The input (seg) must be already decoded by
732 * cbfs_decode_payload_segment.
733 */
734 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800735 case PAYLOAD_SEGMENT_CODE:
736 case PAYLOAD_SEGMENT_DATA:
737 fprintf(fp, " %s (%s compression, offset: 0x%x, "
738 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800739 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800740 "code " : "data"),
741 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800742 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800743 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800744 seg->offset, seg->load_addr, seg->len,
745 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800746 break;
747
748 case PAYLOAD_SEGMENT_ENTRY:
749 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800750 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800751 break;
752
753 case PAYLOAD_SEGMENT_BSS:
754 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
755 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800756 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800757 break;
758
759 case PAYLOAD_SEGMENT_PARAMS:
760 fprintf(fp, " parameters\n");
761 break;
762
763 default:
764 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
765 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800766 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800767 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800768 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800769 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800770 seg->offset, seg->load_addr, seg->len,
771 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800772 break;
773 }
774 return 0;
775}
776
777int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700778 void *arg)
779{
Patrick Georgic569b8b2015-07-15 16:42:38 +0200780 const char *name = entry->filename;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800781 struct cbfs_payload_segment *payload;
782 FILE *fp = (FILE *)arg;
783
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800784 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800785 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
786 cbfs_get_entry_addr(image, entry));
787 return -1;
788 }
789 if (!fp)
790 fp = stdout;
791
Patrick Georgic82725c2015-08-26 12:13:03 +0200792 unsigned int decompressed_size = 0;
Patrick Georgia71c83f2015-08-26 12:23:26 +0200793 unsigned int compression = cbfs_file_get_compression_info(entry,
794 &decompressed_size);
Patrick Georgic82725c2015-08-26 12:13:03 +0200795
796 if (compression == CBFS_COMPRESS_NONE) {
797 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
798 *name ? name : "(empty)",
799 cbfs_get_entry_addr(image, entry),
800 get_cbfs_entry_type_name(ntohl(entry->type)),
801 ntohl(entry->len));
802 } else {
803 fprintf(fp, "%-30s 0x%-8x %-12s %d (%d after %s decompression)\n",
804 *name ? name : "(empty)",
805 cbfs_get_entry_addr(image, entry),
806 get_cbfs_entry_type_name(ntohl(entry->type)),
807 ntohl(entry->len),
808 decompressed_size,
809 lookup_name_by_type(types_cbfs_compression,
810 compression, "(unknown)")
811 );
812 }
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800813
814 if (!verbose)
815 return 0;
816
817 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
818 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
819 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
820 ntohl(entry->len));
821
822 /* note the components of the subheader may be in host order ... */
823 switch (ntohl(entry->type)) {
824 case CBFS_COMPONENT_STAGE:
825 cbfs_print_stage_info((struct cbfs_stage *)
826 CBFS_SUBHEADER(entry), fp);
827 break;
828
829 case CBFS_COMPONENT_PAYLOAD:
830 payload = (struct cbfs_payload_segment *)
831 CBFS_SUBHEADER(entry);
832 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +0800833 struct cbfs_payload_segment seg;
834 cbfs_decode_payload_segment(&seg, payload);
835 cbfs_print_decoded_payload_segment_info(
836 &seg, fp);
837 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800838 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +0800839 else
Aaron Durbinca630272014-08-05 10:48:20 -0500840 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800841 }
842 break;
843 default:
844 break;
845 }
846 return 0;
847}
848
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700849int cbfs_print_directory(struct cbfs_image *image)
850{
Sol Boucher67a0a862015-03-18 12:36:27 -0700851 if (cbfs_is_legacy_cbfs(image))
852 cbfs_print_header_info(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800853 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
854 cbfs_walk(image, cbfs_print_entry_info, NULL);
855 return 0;
856}
857
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800858int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -0800859 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700860{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800861 struct cbfs_file *next;
862 uint32_t type, addr, last_addr;
863
864 type = ntohl(entry->type);
865 if (type == CBFS_COMPONENT_DELETED) {
866 // Ready to be recycled.
867 type = CBFS_COMPONENT_NULL;
868 entry->type = htonl(type);
869 }
870 if (type != CBFS_COMPONENT_NULL)
871 return 0;
872
873 next = cbfs_find_next_entry(image, entry);
874
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800875 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800876 type = ntohl(next->type);
877 if (type == CBFS_COMPONENT_DELETED) {
878 type = CBFS_COMPONENT_NULL;
879 next->type = htonl(type);
880 }
881 if (type != CBFS_COMPONENT_NULL)
882 return 0;
883
884 addr = cbfs_get_entry_addr(image, entry);
885 last_addr = cbfs_get_entry_addr(
886 image, cbfs_find_next_entry(image, next));
887
888 // Now, we find two deleted/empty entries; try to merge now.
889 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
890 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
891 cbfs_get_entry_addr(image, next), ntohl(next->len));
Patrick Georgiedf25d92015-08-12 09:12:06 +0200892 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL,
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800893 (last_addr - addr -
894 cbfs_calculate_file_header_size("")),
895 "");
896 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
897 next = cbfs_find_next_entry(image, entry);
898 }
899 return 0;
900}
901
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800902int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700903 void *arg)
904{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800905 int count = 0;
906 struct cbfs_file *entry;
907 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800908 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800909 entry = cbfs_find_next_entry(image, entry)) {
910 count ++;
911 if (callback(image, entry, arg) != 0)
912 break;
913 }
914 return count;
915}
916
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800917static int cbfs_header_valid(struct cbfs_header *header, size_t size)
918{
919 if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
920 ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
921 (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
922 (ntohl(header->romsize) <= size) &&
923 (ntohl(header->offset) < ntohl(header->romsize)))
924 return 1;
925 return 0;
926}
927
928struct cbfs_header *cbfs_find_header(char *data, size_t size,
929 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700930{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800931 size_t offset;
932 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800933 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800934 struct cbfs_header *header, *result = NULL;
935
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800936 if (forced_offset < (size - sizeof(struct cbfs_header))) {
937 /* Check if the forced header is valid. */
938 header = (struct cbfs_header *)(data + forced_offset);
939 if (cbfs_header_valid(header, size))
940 return header;
941 return NULL;
942 }
943
Julius Wernerefcee762014-11-10 13:14:24 -0800944 // Try finding relative offset of master header at end of file first.
945 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
946 offset = size + rel_offset;
947 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
948 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800949
Hung-Te Lineab2c812013-01-29 01:56:17 +0800950 if (offset >= size - sizeof(*header) ||
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800951 !cbfs_header_valid((struct cbfs_header *)(data + offset), size)) {
Julius Wernerefcee762014-11-10 13:14:24 -0800952 // Some use cases append non-CBFS data to the end of the ROM.
953 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +0800954 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800955 }
Hung-Te Lineab2c812013-01-29 01:56:17 +0800956
957 for (; offset + sizeof(*header) < size; offset++) {
958 header = (struct cbfs_header *)(data + offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800959 if (!cbfs_header_valid(header, size))
Hung-Te Lineab2c812013-01-29 01:56:17 +0800960 continue;
Julius Wernerefcee762014-11-10 13:14:24 -0800961 if (!found++)
962 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800963 }
Julius Wernerefcee762014-11-10 13:14:24 -0800964 if (found > 1)
965 // Top-aligned images usually have a working relative offset
966 // field, so this is more likely to happen on bottom-aligned
967 // ones (where the first header is the "outermost" one)
968 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +0800969 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800970 return result;
971}
972
973
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700974struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
975{
Sol Boucher3e060ed2015-05-05 15:40:15 -0700976 assert(image);
Sol Boucher67a0a862015-03-18 12:36:27 -0700977 return image->has_header ? (struct cbfs_file *)(image->buffer.data +
978 image->header.offset) :
979 (struct cbfs_file *)image->buffer.data;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800980}
981
982struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700983 struct cbfs_file *entry)
984{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800985 uint32_t addr = cbfs_get_entry_addr(image, entry);
Sol Boucher67a0a862015-03-18 12:36:27 -0700986 int align = image->has_header ? image->header.align :
987 CBFS_ENTRY_ALIGNMENT;
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800988 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +0800989 addr += ntohl(entry->offset) + ntohl(entry->len);
990 addr = align_up(addr, align);
991 return (struct cbfs_file *)(image->buffer.data + addr);
992}
993
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700994uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
995{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800996 assert(image && image->buffer.data && entry);
997 return (int32_t)((char *)entry - image->buffer.data);
998}
999
Sol Boucher67a0a862015-03-18 12:36:27 -07001000int cbfs_is_valid_cbfs(struct cbfs_image *image)
1001{
1002 return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC,
1003 strlen(CBFS_FILE_MAGIC));
1004}
1005
1006int cbfs_is_legacy_cbfs(struct cbfs_image *image)
1007{
1008 return image->has_header;
1009}
1010
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001011int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
1012{
Sol Bouchere3260a02015-03-25 13:40:08 -07001013 uint32_t offset = cbfs_get_entry_addr(image, entry);
1014
1015 if (offset >= image->buffer.size)
1016 return 0;
1017
1018 struct buffer entry_data;
1019 buffer_clone(&entry_data, &image->buffer);
1020 buffer_seek(&entry_data, offset);
1021 return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
Sol Boucher67a0a862015-03-18 12:36:27 -07001022 strlen(CBFS_FILE_MAGIC));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001023}
1024
Patrick Georgi57edf162015-08-12 09:20:11 +02001025struct cbfs_file *cbfs_create_file_header(int type,
Vadim Bendebury45e59972014-12-23 15:59:57 -08001026 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001027{
Patrick Georgi2c615062015-07-15 20:49:00 +02001028 struct cbfs_file *entry = malloc(MAX_CBFS_FILE_HEADER_BUFFER);
1029 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, MAX_CBFS_FILE_HEADER_BUFFER);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001030 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
Patrick Georgiedf25d92015-08-12 09:12:06 +02001031 entry->type = htonl(type);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001032 entry->len = htonl(len);
Patrick Georgi0d618af2015-07-15 18:28:23 +02001033 entry->attributes_offset = 0;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001034 entry->offset = htonl(cbfs_calculate_file_header_size(name));
Patrick Georgic569b8b2015-07-15 16:42:38 +02001035 memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry));
1036 strcpy(entry->filename, name);
Patrick Georgi57edf162015-08-12 09:20:11 +02001037 return entry;
1038}
1039
1040int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
1041 size_t len, const char *name)
1042{
1043 struct cbfs_file *tmp = cbfs_create_file_header(type, len, name);
1044 memcpy(entry, tmp, ntohl(tmp->offset));
1045 free(tmp);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001046 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1047 return 0;
1048}
1049
Patrick Georgi2c615062015-07-15 20:49:00 +02001050struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file)
1051{
1052 /* attributes_offset should be 0 when there is no attribute, but all
1053 * values that point into the cbfs_file header are invalid, too. */
1054 if (ntohl(file->attributes_offset) <= sizeof(*file))
1055 return NULL;
1056
1057 /* There needs to be enough space for the file header and one
1058 * attribute header for this to make sense. */
1059 if (ntohl(file->offset) <=
1060 sizeof(*file) + sizeof(struct cbfs_file_attribute))
1061 return NULL;
1062
1063 return (struct cbfs_file_attribute *)
1064 (((uint8_t *)file) + ntohl(file->attributes_offset));
1065}
1066
1067struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
1068 struct cbfs_file_attribute *attr)
1069{
1070 /* ex falso sequitur quodlibet */
1071 if (attr == NULL)
1072 return NULL;
1073
1074 /* Is there enough space for another attribute? */
1075 if ((uint8_t *)attr + ntohl(attr->len) +
1076 sizeof(struct cbfs_file_attribute) >=
1077 (uint8_t *)file + ntohl(file->offset))
1078 return NULL;
1079
1080 struct cbfs_file_attribute *next = (struct cbfs_file_attribute *)
1081 (((uint8_t *)attr) + ntohl(attr->len));
1082 /* If any, "unused" attributes must come last. */
1083 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED)
1084 return NULL;
1085 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
1086 return NULL;
1087
1088 return next;
1089}
1090
1091struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
1092 uint32_t tag,
1093 uint32_t size)
1094{
1095 struct cbfs_file_attribute *attr, *next;
1096 next = cbfs_file_first_attr(header);
1097 do {
1098 attr = next;
1099 next = cbfs_file_next_attr(header, attr);
1100 } while (next != NULL);
1101 uint32_t header_size = ntohl(header->offset) + size;
1102 if (header_size > MAX_CBFS_FILE_HEADER_BUFFER) {
1103 DEBUG("exceeding allocated space for cbfs_file headers");
1104 return NULL;
1105 }
1106 /* attr points to the last valid attribute now.
1107 * If NULL, we have to create the first one. */
1108 if (attr == NULL) {
1109 /* New attributes start where the header ends.
1110 * header->offset is later set to accomodate the
1111 * additional structure.
1112 * No endianess translation necessary here, because both
1113 * fields are encoded the same way. */
1114 header->attributes_offset = header->offset;
1115 attr = (struct cbfs_file_attribute *)
1116 (((uint8_t *)header) +
1117 ntohl(header->attributes_offset));
1118 } else {
1119 attr = (struct cbfs_file_attribute *)
1120 (((uint8_t *)attr) +
1121 ntohl(attr->len));
1122 }
1123 header->offset = htonl(header_size);
1124 memset(attr, CBFS_CONTENT_DEFAULT_VALUE, size);
1125 attr->tag = htonl(tag);
1126 attr->len = htonl(size);
1127 return attr;
1128}
1129
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001130/* Finds a place to hold whole data in same memory page. */
1131static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1132{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001133 if (!page)
1134 return 1;
1135 return (start / page) == (start + size - 1) / page;
1136}
1137
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001138/* Tests if data can fit in a range by given offset:
Aaron Durbind7339412015-09-15 12:50:14 -05001139 * start ->| metadata_size | offset (+ size) |<- end
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001140 */
Aaron Durbind7339412015-09-15 12:50:14 -05001141static int is_in_range(size_t start, size_t end, size_t metadata_size,
1142 size_t offset, size_t size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001143{
Aaron Durbind7339412015-09-15 12:50:14 -05001144 return (offset >= start + metadata_size && offset + size <= end);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001145}
1146
Aaron Durbind7339412015-09-15 12:50:14 -05001147int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size,
1148 size_t page_size, size_t align, size_t metadata_size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001149{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001150 struct cbfs_file *entry;
1151 size_t need_len;
Aaron Durbind7339412015-09-15 12:50:14 -05001152 size_t addr, addr_next, addr2, addr3, offset;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001153
1154 /* Default values: allow fitting anywhere in ROM. */
1155 if (!page_size)
Sol Boucher67a0a862015-03-18 12:36:27 -07001156 page_size = image->has_header ? image->header.romsize :
1157 image->buffer.size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001158 if (!align)
1159 align = 1;
1160
1161 if (size > page_size)
Aaron Durbind7339412015-09-15 12:50:14 -05001162 ERROR("Input file size (%zd) greater than page size (%zd).\n",
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001163 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001164
Aaron Durbind7339412015-09-15 12:50:14 -05001165 size_t image_align = image->has_header ? image->header.align :
Sol Boucher67a0a862015-03-18 12:36:27 -07001166 CBFS_ENTRY_ALIGNMENT;
1167 if (page_size % image_align)
Aaron Durbind7339412015-09-15 12:50:14 -05001168 WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001169 __func__, page_size, image_align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001170
Aaron Durbind7339412015-09-15 12:50:14 -05001171 need_len = metadata_size + size;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001172
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001173 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001174 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
1175
1176 /* Three cases of content location on memory page:
1177 * case 1.
1178 * | PAGE 1 | PAGE 2 |
1179 * | <header><content>| Fit. Return start of content.
1180 *
1181 * case 2.
1182 * | PAGE 1 | PAGE 2 |
1183 * | <header><content> | Fits when we shift content to align
1184 * shift-> | <header>|<content> | at starting of PAGE 2.
1185 *
1186 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001187 * | PAGE 1 | PAGE 2 | PAGE 3 |
1188 * | <header>< content > | Can't fit. If we shift content to
1189 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1190 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001191 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001192 * The returned address can be then used as "base-address" (-b) in add-*
1193 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1194 * For stage targets, the address is also used to re-link stage before
1195 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001196 */
1197 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001198 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001199 entry = cbfs_find_next_entry(image, entry)) {
1200
1201 uint32_t type = ntohl(entry->type);
1202 if (type != CBFS_COMPONENT_NULL)
1203 continue;
1204
1205 addr = cbfs_get_entry_addr(image, entry);
1206 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
1207 image, entry));
1208 if (addr_next - addr < need_len)
1209 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001210
Aaron Durbind7339412015-09-15 12:50:14 -05001211 offset = align_up(addr + metadata_size, align);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001212 if (is_in_same_page(offset, size, page_size) &&
Aaron Durbind7339412015-09-15 12:50:14 -05001213 is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001214 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001215 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001216 }
1217
1218 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001219 offset = align_up(addr2, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001220 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001221 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001222 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001223 }
1224
Aaron Durbind7339412015-09-15 12:50:14 -05001225 /* Assume page_size >= metadata_size so adding one page will
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001226 * definitely provide the space for header. */
Aaron Durbind7339412015-09-15 12:50:14 -05001227 assert(page_size >= metadata_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001228 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001229 offset = align_up(addr3, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001230 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001231 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001232 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001233 }
1234 }
1235 return -1;
1236}