blob: 245230a7cb6da686908231e622275ddc2b7f8a99 [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
Patrick Georgi89f20342015-10-01 15:54:04 +020088static const char *get_hash_attr_name(uint16_t hash_type)
89{
90 return lookup_name_by_type(types_cbfs_hash, hash_type, "(invalid)");
91}
92
93int cbfs_parse_hash_algo(const char *name)
94{
95 return lookup_type_by_name(types_cbfs_hash, name);
96}
97
Hung-Te Linc03d9b02013-01-29 02:38:40 +080098/* CBFS image */
99
Patrick Georgi11ee08f2015-08-11 15:10:02 +0200100size_t cbfs_calculate_file_header_size(const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700101{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800102 return (sizeof(struct cbfs_file) +
103 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
104}
105
Sol Boucher67a0a862015-03-18 12:36:27 -0700106/* Only call on legacy CBFSes possessing a master header. */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600107static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700108{
Sol Boucher67a0a862015-03-18 12:36:27 -0700109 assert(image);
110 assert(cbfs_is_legacy_cbfs(image));
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800111 // A bug in old cbfstool may produce extra few bytes (by alignment) and
112 // cause cbfstool to overwrite things after free space -- which is
113 // usually CBFS header on x86. We need to workaround that.
114
115 struct cbfs_file *entry, *first = NULL, *last = NULL;
116 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800117 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800118 entry = cbfs_find_next_entry(image, entry)) {
119 last = entry;
120 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600121 if ((char *)first < (char *)hdr_loc &&
122 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800123 WARN("CBFS image was created with old cbfstool with size bug. "
124 "Fixing size in last entry...\n");
Sol Boucher3e060ed2015-05-05 15:40:15 -0700125 last->len = htonl(ntohl(last->len) - image->header.align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800126 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
127 cbfs_get_entry_addr(image, entry),
128 cbfs_get_entry_addr(image,
129 cbfs_find_next_entry(image, last)));
130 }
131 return 0;
132}
133
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800134void cbfs_put_header(void *dest, const struct cbfs_header *header)
135{
136 struct buffer outheader;
137
138 outheader.data = dest;
139 outheader.size = 0;
140
141 xdr_be.put32(&outheader, header->magic);
142 xdr_be.put32(&outheader, header->version);
143 xdr_be.put32(&outheader, header->romsize);
144 xdr_be.put32(&outheader, header->bootblocksize);
145 xdr_be.put32(&outheader, header->align);
146 xdr_be.put32(&outheader, header->offset);
147 xdr_be.put32(&outheader, header->architecture);
148}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600149
Hung-Te Lin0780d672014-05-16 10:14:05 +0800150static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
151 struct cbfs_payload_segment *input)
152{
153 struct buffer seg = {
154 .data = (void *)input,
155 .size = sizeof(*input),
156 };
157 output->type = xdr_be.get32(&seg);
158 output->compression = xdr_be.get32(&seg);
159 output->offset = xdr_be.get32(&seg);
160 output->load_addr = xdr_be.get64(&seg);
161 output->len = xdr_be.get32(&seg);
162 output->mem_len = xdr_be.get32(&seg);
163 assert(seg.size == 0);
164}
165
Patrick Georgia71c83f2015-08-26 12:23:26 +0200166static int cbfs_file_get_compression_info(struct cbfs_file *entry,
167 uint32_t *decompressed_size)
168{
169 unsigned int compression = CBFS_COMPRESS_NONE;
170 *decompressed_size = ntohl(entry->len);
171 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
172 attr != NULL;
173 attr = cbfs_file_next_attr(entry, attr)) {
174 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) {
175 struct cbfs_file_attr_compression *ac =
176 (struct cbfs_file_attr_compression *)attr;
177 compression = ntohl(ac->compression);
178 if (decompressed_size)
179 *decompressed_size =
180 ntohl(ac->decompressed_size);
181 }
182 }
183 return compression;
184}
185
Patrick Georgi89f20342015-10-01 15:54:04 +0200186static struct cbfs_file_attr_hash *cbfs_file_get_next_hash(
187 struct cbfs_file *entry, struct cbfs_file_attr_hash *cur)
188{
189 struct cbfs_file_attribute *attr = (struct cbfs_file_attribute *)cur;
190 if (attr == NULL) {
191 attr = cbfs_file_first_attr(entry);
192 if (attr == NULL)
193 return NULL;
194 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
195 return (struct cbfs_file_attr_hash *)attr;
196 }
197 while ((attr = cbfs_file_next_attr(entry, attr)) != NULL) {
198 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
199 return (struct cbfs_file_attr_hash *)attr;
200 };
201 return NULL;
202}
203
Sol Boucher0e539312015-03-05 15:38:03 -0800204void cbfs_get_header(struct cbfs_header *header, void *src)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600205{
206 struct buffer outheader;
207
Sol Boucher0e539312015-03-05 15:38:03 -0800208 outheader.data = src; /* We're not modifying the data */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600209 outheader.size = 0;
210
211 header->magic = xdr_be.get32(&outheader);
212 header->version = xdr_be.get32(&outheader);
213 header->romsize = xdr_be.get32(&outheader);
214 header->bootblocksize = xdr_be.get32(&outheader);
215 header->align = xdr_be.get32(&outheader);
216 header->offset = xdr_be.get32(&outheader);
217 header->architecture = xdr_be.get32(&outheader);
218}
219
Sol Boucher67a0a862015-03-18 12:36:27 -0700220int cbfs_image_create(struct cbfs_image *image, size_t entries_size)
221{
222 assert(image);
223 assert(image->buffer.data);
224
225 size_t empty_header_len = cbfs_calculate_file_header_size("");
226 uint32_t entries_offset = 0;
227 uint32_t align = CBFS_ENTRY_ALIGNMENT;
228 if (image->has_header) {
229 entries_offset = image->header.offset;
230
231 if (entries_offset > image->buffer.size) {
232 ERROR("CBFS file entries are located outside CBFS itself\n");
233 return -1;
234 }
235
236 align = image->header.align;
237 }
238
239 // This attribute must be given in order to prove that this module
240 // correctly preserves certain CBFS properties. See the block comment
241 // near the top of this file (and the associated commit message).
242 if (align < empty_header_len) {
243 ERROR("CBFS must be aligned to at least %zu bytes\n",
244 empty_header_len);
245 return -1;
246 }
247
248 if (entries_size > image->buffer.size - entries_offset) {
249 ERROR("CBFS doesn't have enough space to fit its file entries\n");
250 return -1;
251 }
252
253 if (empty_header_len > entries_size) {
254 ERROR("CBFS is too small to fit any header\n");
255 return -1;
256 }
257 struct cbfs_file *entry_header =
258 (struct cbfs_file *)(image->buffer.data + entries_offset);
259 // This alignment is necessary in order to prove that this module
260 // correctly preserves certain CBFS properties. See the block comment
261 // near the top of this file (and the associated commit message).
262 entries_size -= entries_size % align;
263
264 size_t capacity = entries_size - empty_header_len;
265 LOG("Created CBFS (capacity = %zu bytes)\n", capacity);
Patrick Georgiedf25d92015-08-12 09:12:06 +0200266 return cbfs_create_empty_entry(entry_header, CBFS_COMPONENT_NULL,
267 capacity, "");
Sol Boucher67a0a862015-03-18 12:36:27 -0700268}
269
270int cbfs_legacy_image_create(struct cbfs_image *image,
271 uint32_t architecture,
272 uint32_t align,
273 struct buffer *bootblock,
274 uint32_t bootblock_offset,
275 uint32_t header_offset,
276 uint32_t entries_offset)
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800277{
Sol Bouchere3260a02015-03-25 13:40:08 -0700278 assert(image);
279 assert(image->buffer.data);
280 assert(bootblock);
281
Julius Wernerefcee762014-11-10 13:14:24 -0800282 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800283 uint32_t cbfs_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600284 void *header_loc;
Sol Bouchere3260a02015-03-25 13:40:08 -0700285 size_t size = image->buffer.size;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800286
287 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
288 "header=0x%x+0x%zx, entries_offset=0x%x\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700289 bootblock_offset, bootblock->size, header_offset,
290 sizeof(image->header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800291
Sol Boucher67a0a862015-03-18 12:36:27 -0700292 // Adjust legacy top-aligned address to ROM offset.
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800293 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800294 entries_offset = size + (int32_t)entries_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800295 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800296 bootblock_offset = size + (int32_t)bootblock_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800297 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800298 header_offset = size + (int32_t)header_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800299
300 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
301 "header=0x%x, entries_offset=0x%x\n",
302 bootblock_offset, header_offset, entries_offset);
303
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800304 // Prepare bootblock
305 if (bootblock_offset + bootblock->size > size) {
306 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
307 bootblock_offset, bootblock->size, size);
308 return -1;
309 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800310 if (entries_offset > bootblock_offset &&
311 entries_offset < bootblock->size) {
312 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
313 bootblock_offset, bootblock->size, entries_offset);
314 return -1;
315 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800316 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
317 bootblock->size);
318
319 // Prepare header
Sol Boucher5bad3952015-05-05 20:35:26 -0700320 if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800321 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700322 header_offset, sizeof(image->header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800323 return -1;
324 }
Sol Boucher3e060ed2015-05-05 15:40:15 -0700325 image->header.magic = CBFS_HEADER_MAGIC;
326 image->header.version = CBFS_HEADER_VERSION;
327 image->header.romsize = size;
328 image->header.bootblocksize = bootblock->size;
329 image->header.align = align;
330 image->header.offset = entries_offset;
331 image->header.architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600332
333 header_loc = (image->buffer.data + header_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700334 cbfs_put_header(header_loc, &image->header);
Sol Boucher67a0a862015-03-18 12:36:27 -0700335 image->has_header = true;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800336
Julius Wernerefcee762014-11-10 13:14:24 -0800337 // The last 4 byte of the image contain the relative offset from the end
338 // of the image to the master header as a 32-bit signed integer. x86
339 // relies on this also being its (memory-mapped, top-aligned) absolute
340 // 32-bit address by virtue of how two's complement numbers work.
341 assert(size % sizeof(int32_t) == 0);
342 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
343 *rel_offset = header_offset - size;
344
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800345 // Prepare entries
346 if (align_up(entries_offset, align) != entries_offset) {
347 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
348 entries_offset, align);
349 return -1;
350 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800351 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800352 // e = min(bootblock, header, rel_offset) where e > entries_offset.
353 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800354 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
355 cbfs_len = bootblock_offset;
356 if (header_offset > entries_offset && header_offset < cbfs_len)
357 cbfs_len = header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700358
359 if (cbfs_image_create(image, cbfs_len - entries_offset))
360 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800361 return 0;
362}
363
Sol Bouchere3260a02015-03-25 13:40:08 -0700364int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
365 uint32_t offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700366{
Sol Bouchere3260a02015-03-25 13:40:08 -0700367 assert(out);
368 assert(in);
369 assert(in->data);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600370
Sol Bouchere3260a02015-03-25 13:40:08 -0700371 buffer_clone(&out->buffer, in);
Sol Boucher67a0a862015-03-18 12:36:27 -0700372 out->has_header = false;
373
Patrick Georgi2f953d32015-09-11 18:34:39 +0200374 if (cbfs_is_valid_cbfs(out)) {
375 return 0;
376 }
377
Sol Bouchere3260a02015-03-25 13:40:08 -0700378 void *header_loc = cbfs_find_header(in->data, in->size, offset);
379 if (header_loc) {
380 cbfs_get_header(&out->header, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700381 out->has_header = true;
Sol Bouchere3260a02015-03-25 13:40:08 -0700382 cbfs_fix_legacy_size(out, header_loc);
Patrick Georgi2f953d32015-09-11 18:34:39 +0200383 return 0;
Sol Boucher67a0a862015-03-18 12:36:27 -0700384 } else if (offset != ~0u) {
385 ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n");
386 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800387 }
Patrick Georgi2f953d32015-09-11 18:34:39 +0200388 ERROR("Selected image region is not a valid CBFS.\n");
389 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800390}
391
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800392int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
393 size_t copy_size)
394{
Sol Boucher67a0a862015-03-18 12:36:27 -0700395 assert(image);
396 if (!cbfs_is_legacy_cbfs(image))
397 return -1;
398
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800399 struct cbfs_file *src_entry, *dst_entry;
400 struct cbfs_header *copy_header;
401 size_t align, entry_offset;
402 ssize_t last_entry_size;
403
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800404 size_t cbfs_offset, cbfs_end;
405 size_t copy_end = copy_offset + copy_size;
406
Sol Boucher3e060ed2015-05-05 15:40:15 -0700407 align = image->header.align;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800408
Sol Boucher3e060ed2015-05-05 15:40:15 -0700409 cbfs_offset = image->header.offset;
410 cbfs_end = image->header.romsize;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800411
412 if (copy_end > image->buffer.size) {
413 ERROR("Copy offset out of range: [%zx:%zx)\n",
414 copy_offset, copy_end);
415 return 1;
416 }
417
Sol Boucher297c88c2015-05-05 15:35:18 -0700418 /* Range check requested copy region with source cbfs. */
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800419 if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) ||
420 (copy_end >= cbfs_offset && copy_end <= cbfs_end)) {
421 ERROR("New image would overlap old one.\n");
422 return 1;
423 }
424
425 /* This will work, let's create a copy. */
426 copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700427 cbfs_put_header(copy_header, &image->header);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800428
429 copy_header->bootblocksize = 0;
430 /* Romsize is a misnomer. It's the absolute limit of cbfs content.*/
431 copy_header->romsize = htonl(copy_end);
432 entry_offset = align_up(copy_offset + sizeof(*copy_header), align);
433 copy_header->offset = htonl(entry_offset);
434 dst_entry = (struct cbfs_file *)(image->buffer.data + entry_offset);
435
436 /* Copy non-empty files */
437 for (src_entry = cbfs_find_first_entry(image);
438 src_entry && cbfs_is_valid_entry(image, src_entry);
439 src_entry = cbfs_find_next_entry(image, src_entry)) {
440 size_t entry_size;
441
442 if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) ||
443 (src_entry->type == htonl(CBFS_COMPONENT_DELETED)))
444 continue;
445
446 entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
447 memcpy(dst_entry, src_entry, entry_size);
448 dst_entry = (struct cbfs_file *)(
449 (uintptr_t)dst_entry + align_up(entry_size, align));
450
Sol Boucher0e539312015-03-05 15:38:03 -0800451 if ((size_t)((char *)dst_entry - image->buffer.data) >=
452 copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800453 ERROR("Ran out of room in copy region.\n");
454 return 1;
455 }
456 }
457
458 /* Last entry size is all the room above it. */
459 last_entry_size = copy_end - ((char *)dst_entry - image->buffer.data)
460 - cbfs_calculate_file_header_size("");
461
462 if (last_entry_size < 0)
463 WARN("No room to create the last entry!\n")
464 else
Patrick Georgiedf25d92015-08-12 09:12:06 +0200465 cbfs_create_empty_entry(dst_entry, CBFS_COMPONENT_NULL,
466 last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800467
468 return 0;
469}
470
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700471int cbfs_image_delete(struct cbfs_image *image)
472{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100473 if (image == NULL)
474 return 0;
475
Hung-Te Lineab2c812013-01-29 01:56:17 +0800476 buffer_delete(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800477 return 0;
478}
479
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800480/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
481static int cbfs_add_entry_at(struct cbfs_image *image,
482 struct cbfs_file *entry,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800483 const void *data,
Patrick Georgi7fd14182015-08-11 15:55:16 +0200484 uint32_t content_offset,
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200485 const struct cbfs_file *header)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700486{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800487 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
488 uint32_t addr = cbfs_get_entry_addr(image, entry),
489 addr_next = cbfs_get_entry_addr(image, next);
Patrick Georgi7fd14182015-08-11 15:55:16 +0200490 uint32_t min_entry_size = cbfs_calculate_file_header_size("");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200491 uint32_t len, header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700492 uint32_t align = image->has_header ? image->header.align :
493 CBFS_ENTRY_ALIGNMENT;
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200494 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800495
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200496 header_offset = content_offset - header_size;
497 if (header_offset % align)
498 header_offset -= header_offset % align;
499 if (header_offset < addr) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800500 ERROR("No space to hold cbfs_file header.");
501 return -1;
502 }
503
504 // Process buffer BEFORE content_offset.
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200505 if (header_offset - addr > min_entry_size) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800506 DEBUG("|min|...|header|content|... <create new entry>\n");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200507 len = header_offset - addr - min_entry_size;
Patrick Georgiedf25d92015-08-12 09:12:06 +0200508 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800509 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
510 entry = cbfs_find_next_entry(image, entry);
511 addr = cbfs_get_entry_addr(image, entry);
512 }
513
Patrick Georgi7a33b532015-08-25 13:00:04 +0200514 len = content_offset - addr - header_size;
Patrick Georgia60e7b62015-08-25 22:26:02 +0200515 memcpy(entry, header, header_size);
Patrick Georgi7a33b532015-08-25 13:00:04 +0200516 if (len != 0) {
517 /* the header moved backwards a bit to accomodate cbfs_file
518 * alignment requirements, so patch up ->offset to still point
519 * to file data.
520 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800521 DEBUG("|..|header|content|... <use offset to create entry>\n");
Patrick Georgiae7efb92015-08-25 13:11:28 +0200522 DEBUG("before: offset=0x%x\n", ntohl(entry->offset));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800523 // TODO reset expanded name buffer to 0xFF.
Patrick Georgi7a33b532015-08-25 13:00:04 +0200524 entry->offset = htonl(ntohl(entry->offset) + len);
Patrick Georgiae7efb92015-08-25 13:11:28 +0200525 DEBUG("after: offset=0x%x\n", ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800526 }
527
528 // Ready to fill data into entry.
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800529 DEBUG("content_offset: 0x%x, entry location: %x\n",
530 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
531 image->buffer.data));
532 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
Patrick Georgicccc9d42015-04-28 13:09:36 +0200533 (ptrdiff_t)content_offset);
Patrick Georgi19c80b22015-08-25 13:16:04 +0200534 memcpy(CBFS_SUBHEADER(entry), data, ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800535 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
536
537 // Process buffer AFTER entry.
538 entry = cbfs_find_next_entry(image, entry);
539 addr = cbfs_get_entry_addr(image, entry);
Sol Boucher05725652015-04-02 20:58:26 -0700540 if (addr == addr_next)
541 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800542
Sol Boucher05725652015-04-02 20:58:26 -0700543 assert(addr < addr_next);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800544 if (addr_next - addr < min_entry_size) {
Sol Boucher636cc852015-04-03 09:13:04 -0700545 DEBUG("No need for new \"empty\" entry\n");
546 /* No need to increase the size of the just
547 * stored file to extend to next file. Alignment
548 * of next file takes care of this.
549 */
550 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800551 }
552
553 len = addr_next - addr - min_entry_size;
Patrick Georgiedf25d92015-08-12 09:12:06 +0200554 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800555 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
556 return 0;
557}
558
559int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Patrick Georgie5903582015-08-25 13:53:42 +0200560 uint32_t content_offset,
Patrick Georgif5252f32015-08-25 22:27:57 +0200561 struct cbfs_file *header)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700562{
Sol Boucher67d59982015-05-07 02:39:22 -0700563 assert(image);
564 assert(buffer);
565 assert(buffer->data);
Sol Boucher67d59982015-05-07 02:39:22 -0700566 assert(!IS_TOP_ALIGNED_ADDRESS(content_offset));
567
Patrick Georgia60e7b62015-08-25 22:26:02 +0200568 const char *name = header->filename;
Patrick Georgie5903582015-08-25 13:53:42 +0200569
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800570 uint32_t entry_type;
571 uint32_t addr, addr_next;
572 struct cbfs_file *entry, *next;
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200573 uint32_t need_size;
Patrick Georgif5252f32015-08-25 22:27:57 +0200574 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800575
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800576 need_size = header_size + buffer->size;
577 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
578 name, content_offset, header_size, buffer->size, need_size);
579
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800580 // Merge empty entries.
581 DEBUG("(trying to merge empty entries...)\n");
582 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
583
584 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800585 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800586 entry = cbfs_find_next_entry(image, entry)) {
587
588 entry_type = ntohl(entry->type);
589 if (entry_type != CBFS_COMPONENT_NULL)
590 continue;
591
592 addr = cbfs_get_entry_addr(image, entry);
593 next = cbfs_find_next_entry(image, entry);
594 addr_next = cbfs_get_entry_addr(image, next);
595
596 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
597 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600598
599 /* Will the file fit? Don't yet worry if we have space for a new
600 * "empty" entry. We take care of that later.
601 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800602 if (addr + need_size > addr_next)
603 continue;
604
Patrick Georgiaa44dbd2015-08-12 12:05:21 +0200605 // Test for complicated cases
606 if (content_offset > 0) {
607 if (addr_next < content_offset) {
608 DEBUG("Not for specified offset yet");
609 continue;
610 } else if (addr > content_offset) {
611 DEBUG("Exceed specified content_offset.");
612 break;
613 } else if (addr + header_size > content_offset) {
614 ERROR("Not enough space for header.\n");
615 break;
616 } else if (content_offset + buffer->size > addr_next) {
617 ERROR("Not enough space for content.\n");
618 break;
619 }
620 }
621
622 // TODO there are more few tricky cases that we may
623 // want to fit by altering offset.
624
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200625 if (content_offset == 0) {
626 // we tested every condition earlier under which
627 // placing the file there might fail
628 content_offset = addr + header_size;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800629 }
630
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800631 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
632 addr, addr_next - addr, content_offset);
633
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200634 if (cbfs_add_entry_at(image, entry, buffer->data,
635 content_offset, header) == 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800636 return 0;
637 }
638 break;
639 }
640
641 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
642 buffer->name, buffer->size, buffer->size / 1024, content_offset);
643 return -1;
644}
645
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700646struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
647{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800648 struct cbfs_file *entry;
649 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800650 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800651 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgic569b8b2015-07-15 16:42:38 +0200652 if (strcasecmp(entry->filename, name) == 0) {
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800653 DEBUG("cbfs_get_entry: found %s\n", name);
654 return entry;
655 }
656 }
657 return NULL;
658}
659
Aaron Durbin539aed02015-10-23 17:42:32 -0500660static int cbfs_stage_decompress(struct buffer *buff)
661{
662 struct buffer reader;
663 struct buffer writer;
664 struct cbfs_stage metadata;
665 char *orig_buffer;
666 char *new_buffer;
667 size_t new_buff_sz;
668 decomp_func_ptr decompress;
669
670 buffer_clone(&reader, buff);
671
672 /* The stage metadata is in little endian. */
673 metadata.compression = xdr_le.get32(&reader);
674 metadata.entry = xdr_le.get64(&reader);
675 metadata.load = xdr_le.get64(&reader);
676 metadata.len = xdr_le.get32(&reader);
677 metadata.memlen = xdr_le.get32(&reader);
678
679 if (metadata.compression == CBFS_COMPRESS_NONE)
680 return 0;
681
682 decompress = decompression_function(metadata.compression);
683 if (decompress == NULL)
684 return -1;
685
686 orig_buffer = buffer_get(buff);
687
688 /* This can be too big of a buffer needed, but there's no current
689 * field indicating decompressed size of data. */
690 new_buff_sz = metadata.memlen + sizeof(struct cbfs_stage);
691 new_buffer = calloc(1, new_buff_sz);
692
693 if (decompress(orig_buffer + sizeof(struct cbfs_stage),
694 (int)(buffer_size(buff) - sizeof(struct cbfs_stage)),
695 new_buffer + sizeof(struct cbfs_stage),
696 (int)(new_buff_sz - sizeof(struct cbfs_stage)),
697 &new_buff_sz)) {
698 ERROR("Couldn't decompress stage.\n");
699 free(new_buffer);
700 return -1;
701 }
702
703 /* Include correct size for full stage info. */
704 new_buff_sz += sizeof(struct cbfs_stage);
705 buffer_init(buff, buff->name, new_buffer, new_buff_sz);
706 buffer_clone(&writer, buff);
707 /* Set size to 0 to please xdr. */
708 buffer_set_size(&writer, 0);
709
710 /* True decompressed size is just the data size -- no metadata. */
711 metadata.len = new_buff_sz - sizeof(struct cbfs_stage);
712 /* Stage is not compressed. */
713 metadata.compression = CBFS_COMPRESS_NONE;
714
715 /* Write back out the stage metadata. */
716 xdr_le.put32(&writer, metadata.compression);
717 xdr_le.put32(&writer, metadata.entry);
718 xdr_le.put32(&writer, metadata.load);
719 xdr_le.put32(&writer, metadata.len);
720 xdr_le.put32(&writer, metadata.memlen);
721
722 free(orig_buffer);
723
724 return 0;
725}
726
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800727int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Aaron Durbin17625022015-10-27 13:17:52 -0500728 const char *filename, unused uint32_t arch)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700729{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800730 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
731 struct buffer buffer;
732 if (!entry) {
733 ERROR("File not found: %s\n", entry_name);
734 return -1;
735 }
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200736
737 unsigned int decompressed_size = 0;
738 unsigned int compression = cbfs_file_get_compression_info(entry,
739 &decompressed_size);
740
741 decomp_func_ptr decompress = decompression_function(compression);
742 if (!decompress) {
743 ERROR("looking up decompression routine failed\n");
744 return -1;
745 }
746
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800747 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
748 entry_name, cbfs_get_entry_addr(image, entry),
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200749 get_cbfs_entry_type_name(ntohl(entry->type)), decompressed_size);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800750
Patrick Georgi011b0b32015-08-26 12:16:54 +0200751 if (ntohl(entry->type) == CBFS_COMPONENT_STAGE) {
752 WARN("Stages are extracted in SELF format.\n");
753 }
754
755 if (ntohl(entry->type) == CBFS_COMPONENT_PAYLOAD) {
756 WARN("Payloads are extracted in SELF format.\n");
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800757 }
758
Aaron Durbin539aed02015-10-23 17:42:32 -0500759 buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0);
760
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200761 buffer.data = malloc(decompressed_size);
762 buffer.size = decompressed_size;
763 if (decompress(CBFS_SUBHEADER(entry), ntohl(entry->len),
Aaron Durbin5213c532015-10-23 17:38:40 -0500764 buffer.data, buffer.size, NULL)) {
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200765 ERROR("decompression failed for %s\n", entry_name);
Aaron Durbin539aed02015-10-23 17:42:32 -0500766 buffer_delete(&buffer);
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200767 return -1;
768 }
Aaron Durbin539aed02015-10-23 17:42:32 -0500769
770 /*
771 * The stage metadata is never compressed proper for cbfs_stage
772 * files. The contents of the stage data can be though. Therefore
773 * one has to do a second pass for stages to potentially decompress
774 * the stage data to make it more meaningful.
775 */
776 if (ntohl(entry->type) == CBFS_COMPONENT_STAGE) {
777 if (cbfs_stage_decompress(&buffer)) {
778 ERROR("Failed to write %s into %s.\n",
779 entry_name, filename);
780 buffer_delete(&buffer);
781 return -1;
782 }
783 }
784
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800785 if (buffer_write_file(&buffer, filename) != 0) {
786 ERROR("Failed to write %s into %s.\n",
787 entry_name, filename);
Aaron Durbin539aed02015-10-23 17:42:32 -0500788 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800789 return -1;
790 }
Aaron Durbin539aed02015-10-23 17:42:32 -0500791
792 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800793 INFO("Successfully dumped the file to: %s\n", filename);
794 return 0;
795}
796
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700797int cbfs_remove_entry(struct cbfs_image *image, const char *name)
798{
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +0200799 struct cbfs_file *entry;
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800800 entry = cbfs_get_entry(image, name);
801 if (!entry) {
802 ERROR("CBFS file %s not found.\n", name);
803 return -1;
804 }
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800805 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
Patrick Georgic569b8b2015-07-15 16:42:38 +0200806 entry->filename, cbfs_get_entry_addr(image, entry));
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800807 entry->type = htonl(CBFS_COMPONENT_DELETED);
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +0200808 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800809 return 0;
810}
811
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700812int cbfs_print_header_info(struct cbfs_image *image)
813{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800814 char *name = strdup(image->buffer.name);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700815 assert(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800816 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800817 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800818 basename(name),
819 image->buffer.size / 1024,
Sol Boucher3e060ed2015-05-05 15:40:15 -0700820 image->header.bootblocksize,
821 image->header.romsize,
822 image->header.offset,
823 image->header.align,
824 arch_to_string(image->header.architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800825 free(name);
826 return 0;
827}
828
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700829static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
830{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800831 fprintf(fp,
832 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
833 "length: %d/%d\n",
834 lookup_name_by_type(types_cbfs_compression,
835 stage->compression, "(unknown)"),
836 stage->entry,
837 stage->load,
838 stage->len,
839 stage->memlen);
840 return 0;
841}
842
Hung-Te Lin0780d672014-05-16 10:14:05 +0800843static int cbfs_print_decoded_payload_segment_info(
844 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800845{
Hung-Te Lin0780d672014-05-16 10:14:05 +0800846 /* The input (seg) must be already decoded by
847 * cbfs_decode_payload_segment.
848 */
849 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800850 case PAYLOAD_SEGMENT_CODE:
851 case PAYLOAD_SEGMENT_DATA:
852 fprintf(fp, " %s (%s compression, offset: 0x%x, "
853 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800854 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800855 "code " : "data"),
856 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800857 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800858 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800859 seg->offset, seg->load_addr, seg->len,
860 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800861 break;
862
863 case PAYLOAD_SEGMENT_ENTRY:
864 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800865 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800866 break;
867
868 case PAYLOAD_SEGMENT_BSS:
869 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
870 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800871 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800872 break;
873
874 case PAYLOAD_SEGMENT_PARAMS:
875 fprintf(fp, " parameters\n");
876 break;
877
878 default:
879 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
880 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800881 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800882 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800883 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800884 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800885 seg->offset, seg->load_addr, seg->len,
886 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800887 break;
888 }
889 return 0;
890}
891
892int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700893 void *arg)
894{
Patrick Georgic569b8b2015-07-15 16:42:38 +0200895 const char *name = entry->filename;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800896 struct cbfs_payload_segment *payload;
897 FILE *fp = (FILE *)arg;
898
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800899 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800900 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
901 cbfs_get_entry_addr(image, entry));
902 return -1;
903 }
904 if (!fp)
905 fp = stdout;
906
Patrick Georgic82725c2015-08-26 12:13:03 +0200907 unsigned int decompressed_size = 0;
Patrick Georgia71c83f2015-08-26 12:23:26 +0200908 unsigned int compression = cbfs_file_get_compression_info(entry,
909 &decompressed_size);
Patrick Georgic82725c2015-08-26 12:13:03 +0200910
911 if (compression == CBFS_COMPRESS_NONE) {
912 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
913 *name ? name : "(empty)",
914 cbfs_get_entry_addr(image, entry),
915 get_cbfs_entry_type_name(ntohl(entry->type)),
916 ntohl(entry->len));
917 } else {
918 fprintf(fp, "%-30s 0x%-8x %-12s %d (%d after %s decompression)\n",
919 *name ? name : "(empty)",
920 cbfs_get_entry_addr(image, entry),
921 get_cbfs_entry_type_name(ntohl(entry->type)),
922 ntohl(entry->len),
923 decompressed_size,
924 lookup_name_by_type(types_cbfs_compression,
925 compression, "(unknown)")
926 );
927 }
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800928
Patrick Georgi89f20342015-10-01 15:54:04 +0200929 struct cbfs_file_attr_hash *hash = NULL;
930 while ((hash = cbfs_file_get_next_hash(entry, hash)) != NULL) {
931 unsigned int hash_type = ntohl(hash->hash_type);
932 if (hash_type > CBFS_NUM_SUPPORTED_HASHES) {
933 fprintf(fp, "invalid hash type %d\n", hash_type);
934 break;
935 }
936 size_t hash_len = widths_cbfs_hash[hash_type];
937 char *hash_str = bintohex(hash->hash_data, hash_len);
938 uint8_t local_hash[hash_len];
939 if (vb2_digest_buffer(CBFS_SUBHEADER(entry),
940 ntohl(entry->len), hash_type, local_hash,
941 hash_len) != VB2_SUCCESS) {
942 fprintf(fp, "failed to hash '%s'\n", name);
943 break;
944 }
945 int valid = memcmp(local_hash, hash->hash_data, hash_len) == 0;
946 const char *valid_str = valid ? "valid" : "invalid";
947
948 fprintf(fp, " hash %s:%s %s\n",
949 get_hash_attr_name(hash_type),
950 hash_str, valid_str);
951 free(hash_str);
952 }
953
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800954 if (!verbose)
955 return 0;
956
957 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
958 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
959 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
960 ntohl(entry->len));
961
962 /* note the components of the subheader may be in host order ... */
963 switch (ntohl(entry->type)) {
964 case CBFS_COMPONENT_STAGE:
965 cbfs_print_stage_info((struct cbfs_stage *)
966 CBFS_SUBHEADER(entry), fp);
967 break;
968
969 case CBFS_COMPONENT_PAYLOAD:
Paul Menzel831bbe82015-08-08 20:20:57 +0200970 payload = (struct cbfs_payload_segment *)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800971 CBFS_SUBHEADER(entry);
972 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +0800973 struct cbfs_payload_segment seg;
974 cbfs_decode_payload_segment(&seg, payload);
975 cbfs_print_decoded_payload_segment_info(
976 &seg, fp);
977 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800978 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +0800979 else
Aaron Durbinca630272014-08-05 10:48:20 -0500980 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800981 }
982 break;
983 default:
984 break;
985 }
986 return 0;
987}
988
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700989int cbfs_print_directory(struct cbfs_image *image)
990{
Sol Boucher67a0a862015-03-18 12:36:27 -0700991 if (cbfs_is_legacy_cbfs(image))
992 cbfs_print_header_info(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800993 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
994 cbfs_walk(image, cbfs_print_entry_info, NULL);
995 return 0;
996}
997
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800998int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -0800999 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001000{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001001 struct cbfs_file *next;
Aaron Durbin285111f2015-08-08 20:25:17 +02001002 uint8_t *name;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001003 uint32_t type, addr, last_addr;
1004
1005 type = ntohl(entry->type);
1006 if (type == CBFS_COMPONENT_DELETED) {
1007 // Ready to be recycled.
1008 type = CBFS_COMPONENT_NULL;
1009 entry->type = htonl(type);
Aaron Durbin285111f2015-08-08 20:25:17 +02001010 // Place NUL byte as first byte of name to be viewed as "empty".
1011 name = (void *)&entry[1];
1012 *name = '\0';
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001013 }
1014 if (type != CBFS_COMPONENT_NULL)
1015 return 0;
1016
1017 next = cbfs_find_next_entry(image, entry);
1018
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001019 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001020 type = ntohl(next->type);
1021 if (type == CBFS_COMPONENT_DELETED) {
1022 type = CBFS_COMPONENT_NULL;
1023 next->type = htonl(type);
1024 }
1025 if (type != CBFS_COMPONENT_NULL)
1026 return 0;
1027
1028 addr = cbfs_get_entry_addr(image, entry);
1029 last_addr = cbfs_get_entry_addr(
1030 image, cbfs_find_next_entry(image, next));
1031
1032 // Now, we find two deleted/empty entries; try to merge now.
1033 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
1034 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
1035 cbfs_get_entry_addr(image, next), ntohl(next->len));
Patrick Georgiedf25d92015-08-12 09:12:06 +02001036 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL,
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001037 (last_addr - addr -
1038 cbfs_calculate_file_header_size("")),
1039 "");
1040 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
1041 next = cbfs_find_next_entry(image, entry);
1042 }
1043 return 0;
1044}
1045
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001046int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001047 void *arg)
1048{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001049 int count = 0;
1050 struct cbfs_file *entry;
1051 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001052 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001053 entry = cbfs_find_next_entry(image, entry)) {
1054 count ++;
1055 if (callback(image, entry, arg) != 0)
1056 break;
1057 }
1058 return count;
1059}
1060
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001061static int cbfs_header_valid(struct cbfs_header *header, size_t size)
1062{
1063 if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
1064 ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
1065 (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
1066 (ntohl(header->romsize) <= size) &&
1067 (ntohl(header->offset) < ntohl(header->romsize)))
1068 return 1;
1069 return 0;
1070}
1071
1072struct cbfs_header *cbfs_find_header(char *data, size_t size,
1073 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001074{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001075 size_t offset;
1076 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001077 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001078 struct cbfs_header *header, *result = NULL;
1079
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001080 if (forced_offset < (size - sizeof(struct cbfs_header))) {
1081 /* Check if the forced header is valid. */
1082 header = (struct cbfs_header *)(data + forced_offset);
1083 if (cbfs_header_valid(header, size))
1084 return header;
1085 return NULL;
1086 }
1087
Julius Wernerefcee762014-11-10 13:14:24 -08001088 // Try finding relative offset of master header at end of file first.
1089 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
1090 offset = size + rel_offset;
1091 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
1092 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001093
Hung-Te Lineab2c812013-01-29 01:56:17 +08001094 if (offset >= size - sizeof(*header) ||
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001095 !cbfs_header_valid((struct cbfs_header *)(data + offset), size)) {
Julius Wernerefcee762014-11-10 13:14:24 -08001096 // Some use cases append non-CBFS data to the end of the ROM.
1097 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +08001098 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001099 }
Hung-Te Lineab2c812013-01-29 01:56:17 +08001100
1101 for (; offset + sizeof(*header) < size; offset++) {
1102 header = (struct cbfs_header *)(data + offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001103 if (!cbfs_header_valid(header, size))
Hung-Te Lineab2c812013-01-29 01:56:17 +08001104 continue;
Julius Wernerefcee762014-11-10 13:14:24 -08001105 if (!found++)
1106 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001107 }
Julius Wernerefcee762014-11-10 13:14:24 -08001108 if (found > 1)
1109 // Top-aligned images usually have a working relative offset
1110 // field, so this is more likely to happen on bottom-aligned
1111 // ones (where the first header is the "outermost" one)
1112 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +08001113 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001114 return result;
1115}
1116
1117
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001118struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
1119{
Sol Boucher3e060ed2015-05-05 15:40:15 -07001120 assert(image);
Sol Boucher67a0a862015-03-18 12:36:27 -07001121 return image->has_header ? (struct cbfs_file *)(image->buffer.data +
1122 image->header.offset) :
1123 (struct cbfs_file *)image->buffer.data;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001124}
1125
1126struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001127 struct cbfs_file *entry)
1128{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001129 uint32_t addr = cbfs_get_entry_addr(image, entry);
Sol Boucher67a0a862015-03-18 12:36:27 -07001130 int align = image->has_header ? image->header.align :
1131 CBFS_ENTRY_ALIGNMENT;
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001132 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001133 addr += ntohl(entry->offset) + ntohl(entry->len);
1134 addr = align_up(addr, align);
1135 return (struct cbfs_file *)(image->buffer.data + addr);
1136}
1137
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001138uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
1139{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001140 assert(image && image->buffer.data && entry);
1141 return (int32_t)((char *)entry - image->buffer.data);
1142}
1143
Sol Boucher67a0a862015-03-18 12:36:27 -07001144int cbfs_is_valid_cbfs(struct cbfs_image *image)
1145{
1146 return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC,
1147 strlen(CBFS_FILE_MAGIC));
1148}
1149
1150int cbfs_is_legacy_cbfs(struct cbfs_image *image)
1151{
1152 return image->has_header;
1153}
1154
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001155int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
1156{
Sol Bouchere3260a02015-03-25 13:40:08 -07001157 uint32_t offset = cbfs_get_entry_addr(image, entry);
1158
1159 if (offset >= image->buffer.size)
1160 return 0;
1161
1162 struct buffer entry_data;
1163 buffer_clone(&entry_data, &image->buffer);
1164 buffer_seek(&entry_data, offset);
1165 return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
Sol Boucher67a0a862015-03-18 12:36:27 -07001166 strlen(CBFS_FILE_MAGIC));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001167}
1168
Patrick Georgi57edf162015-08-12 09:20:11 +02001169struct cbfs_file *cbfs_create_file_header(int type,
Vadim Bendebury45e59972014-12-23 15:59:57 -08001170 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001171{
Patrick Georgi2c615062015-07-15 20:49:00 +02001172 struct cbfs_file *entry = malloc(MAX_CBFS_FILE_HEADER_BUFFER);
1173 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, MAX_CBFS_FILE_HEADER_BUFFER);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001174 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
Patrick Georgiedf25d92015-08-12 09:12:06 +02001175 entry->type = htonl(type);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001176 entry->len = htonl(len);
Patrick Georgi0d618af2015-07-15 18:28:23 +02001177 entry->attributes_offset = 0;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001178 entry->offset = htonl(cbfs_calculate_file_header_size(name));
Patrick Georgic569b8b2015-07-15 16:42:38 +02001179 memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry));
1180 strcpy(entry->filename, name);
Patrick Georgi57edf162015-08-12 09:20:11 +02001181 return entry;
1182}
1183
1184int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
1185 size_t len, const char *name)
1186{
1187 struct cbfs_file *tmp = cbfs_create_file_header(type, len, name);
1188 memcpy(entry, tmp, ntohl(tmp->offset));
1189 free(tmp);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001190 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1191 return 0;
1192}
1193
Patrick Georgi2c615062015-07-15 20:49:00 +02001194struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file)
1195{
1196 /* attributes_offset should be 0 when there is no attribute, but all
1197 * values that point into the cbfs_file header are invalid, too. */
1198 if (ntohl(file->attributes_offset) <= sizeof(*file))
1199 return NULL;
1200
1201 /* There needs to be enough space for the file header and one
1202 * attribute header for this to make sense. */
1203 if (ntohl(file->offset) <=
1204 sizeof(*file) + sizeof(struct cbfs_file_attribute))
1205 return NULL;
1206
1207 return (struct cbfs_file_attribute *)
1208 (((uint8_t *)file) + ntohl(file->attributes_offset));
1209}
1210
1211struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
1212 struct cbfs_file_attribute *attr)
1213{
1214 /* ex falso sequitur quodlibet */
1215 if (attr == NULL)
1216 return NULL;
1217
1218 /* Is there enough space for another attribute? */
1219 if ((uint8_t *)attr + ntohl(attr->len) +
1220 sizeof(struct cbfs_file_attribute) >=
1221 (uint8_t *)file + ntohl(file->offset))
1222 return NULL;
1223
1224 struct cbfs_file_attribute *next = (struct cbfs_file_attribute *)
1225 (((uint8_t *)attr) + ntohl(attr->len));
1226 /* If any, "unused" attributes must come last. */
1227 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED)
1228 return NULL;
1229 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
1230 return NULL;
1231
1232 return next;
1233}
1234
1235struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
1236 uint32_t tag,
1237 uint32_t size)
1238{
1239 struct cbfs_file_attribute *attr, *next;
1240 next = cbfs_file_first_attr(header);
1241 do {
1242 attr = next;
1243 next = cbfs_file_next_attr(header, attr);
1244 } while (next != NULL);
1245 uint32_t header_size = ntohl(header->offset) + size;
1246 if (header_size > MAX_CBFS_FILE_HEADER_BUFFER) {
1247 DEBUG("exceeding allocated space for cbfs_file headers");
1248 return NULL;
1249 }
1250 /* attr points to the last valid attribute now.
1251 * If NULL, we have to create the first one. */
1252 if (attr == NULL) {
1253 /* New attributes start where the header ends.
1254 * header->offset is later set to accomodate the
1255 * additional structure.
1256 * No endianess translation necessary here, because both
1257 * fields are encoded the same way. */
1258 header->attributes_offset = header->offset;
1259 attr = (struct cbfs_file_attribute *)
1260 (((uint8_t *)header) +
1261 ntohl(header->attributes_offset));
1262 } else {
1263 attr = (struct cbfs_file_attribute *)
1264 (((uint8_t *)attr) +
1265 ntohl(attr->len));
1266 }
1267 header->offset = htonl(header_size);
1268 memset(attr, CBFS_CONTENT_DEFAULT_VALUE, size);
1269 attr->tag = htonl(tag);
1270 attr->len = htonl(size);
1271 return attr;
1272}
1273
Patrick Georgi89f20342015-10-01 15:54:04 +02001274int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer,
1275 enum vb2_hash_algorithm hash_type)
1276{
1277 if (hash_type >= CBFS_NUM_SUPPORTED_HASHES)
1278 return -1;
1279
1280 unsigned hash_size = widths_cbfs_hash[hash_type];
1281 if (hash_size == 0)
1282 return -1;
1283
1284 struct cbfs_file_attr_hash *attrs =
1285 (struct cbfs_file_attr_hash *)cbfs_add_file_attr(header,
1286 CBFS_FILE_ATTR_TAG_HASH,
1287 sizeof(struct cbfs_file_attr_hash) + hash_size);
1288
1289 if (attrs == NULL)
1290 return -1;
1291
1292 attrs->hash_type = htonl(hash_type);
1293 if (vb2_digest_buffer(buffer_get(buffer), buffer_size(buffer),
1294 hash_type, attrs->hash_data, hash_size) != VB2_SUCCESS)
1295 return -1;
1296
1297 return 0;
1298}
1299
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001300/* Finds a place to hold whole data in same memory page. */
1301static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1302{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001303 if (!page)
1304 return 1;
1305 return (start / page) == (start + size - 1) / page;
1306}
1307
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001308/* Tests if data can fit in a range by given offset:
Aaron Durbind7339412015-09-15 12:50:14 -05001309 * start ->| metadata_size | offset (+ size) |<- end
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001310 */
Aaron Durbind7339412015-09-15 12:50:14 -05001311static int is_in_range(size_t start, size_t end, size_t metadata_size,
1312 size_t offset, size_t size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001313{
Aaron Durbind7339412015-09-15 12:50:14 -05001314 return (offset >= start + metadata_size && offset + size <= end);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001315}
1316
Aaron Durbind7339412015-09-15 12:50:14 -05001317int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size,
1318 size_t page_size, size_t align, size_t metadata_size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001319{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001320 struct cbfs_file *entry;
1321 size_t need_len;
Aaron Durbind7339412015-09-15 12:50:14 -05001322 size_t addr, addr_next, addr2, addr3, offset;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001323
1324 /* Default values: allow fitting anywhere in ROM. */
1325 if (!page_size)
Sol Boucher67a0a862015-03-18 12:36:27 -07001326 page_size = image->has_header ? image->header.romsize :
1327 image->buffer.size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001328 if (!align)
1329 align = 1;
1330
1331 if (size > page_size)
Aaron Durbind7339412015-09-15 12:50:14 -05001332 ERROR("Input file size (%zd) greater than page size (%zd).\n",
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001333 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001334
Aaron Durbind7339412015-09-15 12:50:14 -05001335 size_t image_align = image->has_header ? image->header.align :
Sol Boucher67a0a862015-03-18 12:36:27 -07001336 CBFS_ENTRY_ALIGNMENT;
1337 if (page_size % image_align)
Aaron Durbind7339412015-09-15 12:50:14 -05001338 WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001339 __func__, page_size, image_align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001340
Aaron Durbind7339412015-09-15 12:50:14 -05001341 need_len = metadata_size + size;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001342
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001343 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001344 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
1345
1346 /* Three cases of content location on memory page:
1347 * case 1.
1348 * | PAGE 1 | PAGE 2 |
1349 * | <header><content>| Fit. Return start of content.
1350 *
1351 * case 2.
1352 * | PAGE 1 | PAGE 2 |
1353 * | <header><content> | Fits when we shift content to align
1354 * shift-> | <header>|<content> | at starting of PAGE 2.
1355 *
1356 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001357 * | PAGE 1 | PAGE 2 | PAGE 3 |
1358 * | <header>< content > | Can't fit. If we shift content to
1359 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1360 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001361 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001362 * The returned address can be then used as "base-address" (-b) in add-*
1363 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1364 * For stage targets, the address is also used to re-link stage before
1365 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001366 */
1367 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001368 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001369 entry = cbfs_find_next_entry(image, entry)) {
1370
1371 uint32_t type = ntohl(entry->type);
1372 if (type != CBFS_COMPONENT_NULL)
1373 continue;
1374
1375 addr = cbfs_get_entry_addr(image, entry);
1376 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
1377 image, entry));
1378 if (addr_next - addr < need_len)
1379 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001380
Aaron Durbind7339412015-09-15 12:50:14 -05001381 offset = align_up(addr + metadata_size, align);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001382 if (is_in_same_page(offset, size, page_size) &&
Aaron Durbind7339412015-09-15 12:50:14 -05001383 is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001384 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001385 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001386 }
1387
1388 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001389 offset = align_up(addr2, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001390 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001391 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001392 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001393 }
1394
Aaron Durbind7339412015-09-15 12:50:14 -05001395 /* Assume page_size >= metadata_size so adding one page will
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001396 * definitely provide the space for header. */
Aaron Durbind7339412015-09-15 12:50:14 -05001397 assert(page_size >= metadata_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001398 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001399 offset = align_up(addr3, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001400 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001401 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001402 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001403 }
1404 }
1405 return -1;
1406}