blob: bd432d43de7df3f2390a14da45717d6e44327405 [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"
Aaron Durbin5a1e85c2015-10-27 21:02:30 -050030#include "elfparsing.h"
Hung-Te Lineab2c812013-01-29 01:56:17 +080031
Sol Boucher636cc852015-04-03 09:13:04 -070032/* Even though the file-adding functions---cbfs_add_entry() and
33 * cbfs_add_entry_at()---perform their sizing checks against the beginning of
34 * the subsequent section rather than a stable recorded value such as an empty
35 * file header's len field, it's possible to prove two interesting properties
36 * about their behavior:
37 * - Placing a new file within an empty entry located below an existing file
38 * entry will never leave an aligned flash address containing neither the
39 * beginning of a file header nor part of a file.
40 * - Placing a new file in an empty entry at the very end of the image such
41 * that it fits, but leaves no room for a final header, is guaranteed not to
42 * change the total amount of space for entries, even if that new file is
43 * later removed from the CBFS.
44 * These properties are somewhat nonobvious from the implementation, so the
45 * reader is encouraged to blame this comment and examine the full proofs
46 * in the commit message before making significant changes that would risk
47 * removing said guarantees.
48 */
49
Hung-Te Lineab2c812013-01-29 01:56:17 +080050/* The file name align is not defined in CBFS spec -- only a preference by
51 * (old) cbfstool. */
52#define CBFS_FILENAME_ALIGN (16)
53
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080054/* Type and format */
55
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070056static const struct typedesc_t types_cbfs_compression[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080057 {CBFS_COMPRESS_NONE, "none"},
58 {CBFS_COMPRESS_LZMA, "LZMA"},
Sol Boucher5bb90e62015-05-07 21:00:05 -070059 {0, NULL}
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080060};
61
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080062static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070063 const char *default_value)
64{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080065 int i;
66 for (i = 0; desc[i].name; i++)
67 if (desc[i].type == type)
68 return desc[i].name;
69 return default_value;
70}
71
Sol Boucherec424862015-05-07 21:00:05 -070072static int lookup_type_by_name(const struct typedesc_t *desc, const char *name)
73{
74 int i;
75 for (i = 0; desc[i].name && strcasecmp(name, desc[i].name); ++i);
76 return desc[i].name ? (int)desc[i].type : -1;
77}
78
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010079static const char *get_cbfs_entry_type_name(uint32_t type)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070080{
Patrick Georgidc37dab2015-09-09 16:46:00 +020081 return lookup_name_by_type(filetypes, type, "(unknown)");
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080082}
83
Sol Boucherec424862015-05-07 21:00:05 -070084int cbfs_parse_comp_algo(const char *name)
85{
86 return lookup_type_by_name(types_cbfs_compression, name);
87}
88
Patrick Georgi89f20342015-10-01 15:54:04 +020089static const char *get_hash_attr_name(uint16_t hash_type)
90{
91 return lookup_name_by_type(types_cbfs_hash, hash_type, "(invalid)");
92}
93
94int cbfs_parse_hash_algo(const char *name)
95{
96 return lookup_type_by_name(types_cbfs_hash, name);
97}
98
Hung-Te Linc03d9b02013-01-29 02:38:40 +080099/* CBFS image */
100
Patrick Georgi11ee08f2015-08-11 15:10:02 +0200101size_t cbfs_calculate_file_header_size(const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700102{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800103 return (sizeof(struct cbfs_file) +
104 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
105}
106
Sol Boucher67a0a862015-03-18 12:36:27 -0700107/* Only call on legacy CBFSes possessing a master header. */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600108static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700109{
Sol Boucher67a0a862015-03-18 12:36:27 -0700110 assert(image);
111 assert(cbfs_is_legacy_cbfs(image));
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800112 // A bug in old cbfstool may produce extra few bytes (by alignment) and
113 // cause cbfstool to overwrite things after free space -- which is
114 // usually CBFS header on x86. We need to workaround that.
115
116 struct cbfs_file *entry, *first = NULL, *last = NULL;
117 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800118 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800119 entry = cbfs_find_next_entry(image, entry)) {
120 last = entry;
121 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600122 if ((char *)first < (char *)hdr_loc &&
123 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800124 WARN("CBFS image was created with old cbfstool with size bug. "
125 "Fixing size in last entry...\n");
Sol Boucher3e060ed2015-05-05 15:40:15 -0700126 last->len = htonl(ntohl(last->len) - image->header.align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800127 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
128 cbfs_get_entry_addr(image, entry),
129 cbfs_get_entry_addr(image,
130 cbfs_find_next_entry(image, last)));
131 }
132 return 0;
133}
134
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800135void cbfs_put_header(void *dest, const struct cbfs_header *header)
136{
137 struct buffer outheader;
138
139 outheader.data = dest;
140 outheader.size = 0;
141
142 xdr_be.put32(&outheader, header->magic);
143 xdr_be.put32(&outheader, header->version);
144 xdr_be.put32(&outheader, header->romsize);
145 xdr_be.put32(&outheader, header->bootblocksize);
146 xdr_be.put32(&outheader, header->align);
147 xdr_be.put32(&outheader, header->offset);
148 xdr_be.put32(&outheader, header->architecture);
149}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600150
Hung-Te Lin0780d672014-05-16 10:14:05 +0800151static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
152 struct cbfs_payload_segment *input)
153{
154 struct buffer seg = {
155 .data = (void *)input,
156 .size = sizeof(*input),
157 };
158 output->type = xdr_be.get32(&seg);
159 output->compression = xdr_be.get32(&seg);
160 output->offset = xdr_be.get32(&seg);
161 output->load_addr = xdr_be.get64(&seg);
162 output->len = xdr_be.get32(&seg);
163 output->mem_len = xdr_be.get32(&seg);
164 assert(seg.size == 0);
165}
166
Patrick Georgia71c83f2015-08-26 12:23:26 +0200167static int cbfs_file_get_compression_info(struct cbfs_file *entry,
168 uint32_t *decompressed_size)
169{
170 unsigned int compression = CBFS_COMPRESS_NONE;
171 *decompressed_size = ntohl(entry->len);
172 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
173 attr != NULL;
174 attr = cbfs_file_next_attr(entry, attr)) {
175 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) {
176 struct cbfs_file_attr_compression *ac =
177 (struct cbfs_file_attr_compression *)attr;
178 compression = ntohl(ac->compression);
179 if (decompressed_size)
180 *decompressed_size =
181 ntohl(ac->decompressed_size);
182 }
183 }
184 return compression;
185}
186
Patrick Georgi89f20342015-10-01 15:54:04 +0200187static struct cbfs_file_attr_hash *cbfs_file_get_next_hash(
188 struct cbfs_file *entry, struct cbfs_file_attr_hash *cur)
189{
190 struct cbfs_file_attribute *attr = (struct cbfs_file_attribute *)cur;
191 if (attr == NULL) {
192 attr = cbfs_file_first_attr(entry);
193 if (attr == NULL)
194 return NULL;
195 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
196 return (struct cbfs_file_attr_hash *)attr;
197 }
198 while ((attr = cbfs_file_next_attr(entry, attr)) != NULL) {
199 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
200 return (struct cbfs_file_attr_hash *)attr;
201 };
202 return NULL;
203}
204
Sol Boucher0e539312015-03-05 15:38:03 -0800205void cbfs_get_header(struct cbfs_header *header, void *src)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600206{
207 struct buffer outheader;
208
Sol Boucher0e539312015-03-05 15:38:03 -0800209 outheader.data = src; /* We're not modifying the data */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600210 outheader.size = 0;
211
212 header->magic = xdr_be.get32(&outheader);
213 header->version = xdr_be.get32(&outheader);
214 header->romsize = xdr_be.get32(&outheader);
215 header->bootblocksize = xdr_be.get32(&outheader);
216 header->align = xdr_be.get32(&outheader);
217 header->offset = xdr_be.get32(&outheader);
218 header->architecture = xdr_be.get32(&outheader);
219}
220
Sol Boucher67a0a862015-03-18 12:36:27 -0700221int cbfs_image_create(struct cbfs_image *image, size_t entries_size)
222{
223 assert(image);
224 assert(image->buffer.data);
225
226 size_t empty_header_len = cbfs_calculate_file_header_size("");
227 uint32_t entries_offset = 0;
228 uint32_t align = CBFS_ENTRY_ALIGNMENT;
229 if (image->has_header) {
230 entries_offset = image->header.offset;
231
232 if (entries_offset > image->buffer.size) {
233 ERROR("CBFS file entries are located outside CBFS itself\n");
234 return -1;
235 }
236
237 align = image->header.align;
238 }
239
240 // This attribute must be given in order to prove that this module
241 // correctly preserves certain CBFS properties. See the block comment
242 // near the top of this file (and the associated commit message).
243 if (align < empty_header_len) {
244 ERROR("CBFS must be aligned to at least %zu bytes\n",
245 empty_header_len);
246 return -1;
247 }
248
249 if (entries_size > image->buffer.size - entries_offset) {
250 ERROR("CBFS doesn't have enough space to fit its file entries\n");
251 return -1;
252 }
253
254 if (empty_header_len > entries_size) {
255 ERROR("CBFS is too small to fit any header\n");
256 return -1;
257 }
258 struct cbfs_file *entry_header =
259 (struct cbfs_file *)(image->buffer.data + entries_offset);
260 // This alignment is necessary in order to prove that this module
261 // correctly preserves certain CBFS properties. See the block comment
262 // near the top of this file (and the associated commit message).
263 entries_size -= entries_size % align;
264
265 size_t capacity = entries_size - empty_header_len;
266 LOG("Created CBFS (capacity = %zu bytes)\n", capacity);
Patrick Georgiedf25d92015-08-12 09:12:06 +0200267 return cbfs_create_empty_entry(entry_header, CBFS_COMPONENT_NULL,
268 capacity, "");
Sol Boucher67a0a862015-03-18 12:36:27 -0700269}
270
271int cbfs_legacy_image_create(struct cbfs_image *image,
272 uint32_t architecture,
273 uint32_t align,
274 struct buffer *bootblock,
275 uint32_t bootblock_offset,
276 uint32_t header_offset,
277 uint32_t entries_offset)
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800278{
Sol Bouchere3260a02015-03-25 13:40:08 -0700279 assert(image);
280 assert(image->buffer.data);
281 assert(bootblock);
282
Julius Wernerefcee762014-11-10 13:14:24 -0800283 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800284 uint32_t cbfs_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600285 void *header_loc;
Sol Bouchere3260a02015-03-25 13:40:08 -0700286 size_t size = image->buffer.size;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800287
288 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
289 "header=0x%x+0x%zx, entries_offset=0x%x\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700290 bootblock_offset, bootblock->size, header_offset,
291 sizeof(image->header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800292
Sol Boucher67a0a862015-03-18 12:36:27 -0700293 // Adjust legacy top-aligned address to ROM offset.
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800294 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800295 entries_offset = size + (int32_t)entries_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800296 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800297 bootblock_offset = size + (int32_t)bootblock_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800298 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800299 header_offset = size + (int32_t)header_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800300
301 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
302 "header=0x%x, entries_offset=0x%x\n",
303 bootblock_offset, header_offset, entries_offset);
304
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800305 // Prepare bootblock
306 if (bootblock_offset + bootblock->size > size) {
307 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
308 bootblock_offset, bootblock->size, size);
309 return -1;
310 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800311 if (entries_offset > bootblock_offset &&
312 entries_offset < bootblock->size) {
313 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
314 bootblock_offset, bootblock->size, entries_offset);
315 return -1;
316 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800317 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
318 bootblock->size);
319
320 // Prepare header
Sol Boucher5bad3952015-05-05 20:35:26 -0700321 if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800322 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700323 header_offset, sizeof(image->header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800324 return -1;
325 }
Sol Boucher3e060ed2015-05-05 15:40:15 -0700326 image->header.magic = CBFS_HEADER_MAGIC;
327 image->header.version = CBFS_HEADER_VERSION;
328 image->header.romsize = size;
329 image->header.bootblocksize = bootblock->size;
330 image->header.align = align;
331 image->header.offset = entries_offset;
332 image->header.architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600333
334 header_loc = (image->buffer.data + header_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700335 cbfs_put_header(header_loc, &image->header);
Sol Boucher67a0a862015-03-18 12:36:27 -0700336 image->has_header = true;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800337
Julius Wernerefcee762014-11-10 13:14:24 -0800338 // The last 4 byte of the image contain the relative offset from the end
339 // of the image to the master header as a 32-bit signed integer. x86
340 // relies on this also being its (memory-mapped, top-aligned) absolute
341 // 32-bit address by virtue of how two's complement numbers work.
342 assert(size % sizeof(int32_t) == 0);
343 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
344 *rel_offset = header_offset - size;
345
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800346 // Prepare entries
347 if (align_up(entries_offset, align) != entries_offset) {
348 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
349 entries_offset, align);
350 return -1;
351 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800352 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800353 // e = min(bootblock, header, rel_offset) where e > entries_offset.
354 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800355 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
356 cbfs_len = bootblock_offset;
357 if (header_offset > entries_offset && header_offset < cbfs_len)
358 cbfs_len = header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700359
360 if (cbfs_image_create(image, cbfs_len - entries_offset))
361 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800362 return 0;
363}
364
Sol Bouchere3260a02015-03-25 13:40:08 -0700365int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
366 uint32_t offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700367{
Sol Bouchere3260a02015-03-25 13:40:08 -0700368 assert(out);
369 assert(in);
370 assert(in->data);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600371
Sol Bouchere3260a02015-03-25 13:40:08 -0700372 buffer_clone(&out->buffer, in);
Sol Boucher67a0a862015-03-18 12:36:27 -0700373 out->has_header = false;
374
Patrick Georgi2f953d32015-09-11 18:34:39 +0200375 if (cbfs_is_valid_cbfs(out)) {
376 return 0;
377 }
378
Sol Bouchere3260a02015-03-25 13:40:08 -0700379 void *header_loc = cbfs_find_header(in->data, in->size, offset);
380 if (header_loc) {
381 cbfs_get_header(&out->header, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700382 out->has_header = true;
Sol Bouchere3260a02015-03-25 13:40:08 -0700383 cbfs_fix_legacy_size(out, header_loc);
Patrick Georgi2f953d32015-09-11 18:34:39 +0200384 return 0;
Sol Boucher67a0a862015-03-18 12:36:27 -0700385 } else if (offset != ~0u) {
386 ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n");
387 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800388 }
Patrick Georgi2f953d32015-09-11 18:34:39 +0200389 ERROR("Selected image region is not a valid CBFS.\n");
390 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800391}
392
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800393int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
394 size_t copy_size)
395{
Sol Boucher67a0a862015-03-18 12:36:27 -0700396 assert(image);
397 if (!cbfs_is_legacy_cbfs(image))
398 return -1;
399
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800400 struct cbfs_file *src_entry, *dst_entry;
401 struct cbfs_header *copy_header;
402 size_t align, entry_offset;
403 ssize_t last_entry_size;
404
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800405 size_t cbfs_offset, cbfs_end;
406 size_t copy_end = copy_offset + copy_size;
407
Sol Boucher3e060ed2015-05-05 15:40:15 -0700408 align = image->header.align;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800409
Sol Boucher3e060ed2015-05-05 15:40:15 -0700410 cbfs_offset = image->header.offset;
411 cbfs_end = image->header.romsize;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800412
413 if (copy_end > image->buffer.size) {
414 ERROR("Copy offset out of range: [%zx:%zx)\n",
415 copy_offset, copy_end);
416 return 1;
417 }
418
Sol Boucher297c88c2015-05-05 15:35:18 -0700419 /* Range check requested copy region with source cbfs. */
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800420 if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) ||
421 (copy_end >= cbfs_offset && copy_end <= cbfs_end)) {
422 ERROR("New image would overlap old one.\n");
423 return 1;
424 }
425
426 /* This will work, let's create a copy. */
427 copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700428 cbfs_put_header(copy_header, &image->header);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800429
430 copy_header->bootblocksize = 0;
431 /* Romsize is a misnomer. It's the absolute limit of cbfs content.*/
432 copy_header->romsize = htonl(copy_end);
433 entry_offset = align_up(copy_offset + sizeof(*copy_header), align);
434 copy_header->offset = htonl(entry_offset);
435 dst_entry = (struct cbfs_file *)(image->buffer.data + entry_offset);
436
437 /* Copy non-empty files */
438 for (src_entry = cbfs_find_first_entry(image);
439 src_entry && cbfs_is_valid_entry(image, src_entry);
440 src_entry = cbfs_find_next_entry(image, src_entry)) {
441 size_t entry_size;
442
443 if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) ||
444 (src_entry->type == htonl(CBFS_COMPONENT_DELETED)))
445 continue;
446
447 entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
448 memcpy(dst_entry, src_entry, entry_size);
449 dst_entry = (struct cbfs_file *)(
450 (uintptr_t)dst_entry + align_up(entry_size, align));
451
Sol Boucher0e539312015-03-05 15:38:03 -0800452 if ((size_t)((char *)dst_entry - image->buffer.data) >=
453 copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800454 ERROR("Ran out of room in copy region.\n");
455 return 1;
456 }
457 }
458
459 /* Last entry size is all the room above it. */
460 last_entry_size = copy_end - ((char *)dst_entry - image->buffer.data)
461 - cbfs_calculate_file_header_size("");
462
463 if (last_entry_size < 0)
464 WARN("No room to create the last entry!\n")
465 else
Patrick Georgiedf25d92015-08-12 09:12:06 +0200466 cbfs_create_empty_entry(dst_entry, CBFS_COMPONENT_NULL,
467 last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800468
469 return 0;
470}
471
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700472int cbfs_image_delete(struct cbfs_image *image)
473{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100474 if (image == NULL)
475 return 0;
476
Hung-Te Lineab2c812013-01-29 01:56:17 +0800477 buffer_delete(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800478 return 0;
479}
480
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800481/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
482static int cbfs_add_entry_at(struct cbfs_image *image,
483 struct cbfs_file *entry,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800484 const void *data,
Patrick Georgi7fd14182015-08-11 15:55:16 +0200485 uint32_t content_offset,
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200486 const struct cbfs_file *header)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700487{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800488 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
489 uint32_t addr = cbfs_get_entry_addr(image, entry),
490 addr_next = cbfs_get_entry_addr(image, next);
Patrick Georgi7fd14182015-08-11 15:55:16 +0200491 uint32_t min_entry_size = cbfs_calculate_file_header_size("");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200492 uint32_t len, header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700493 uint32_t align = image->has_header ? image->header.align :
494 CBFS_ENTRY_ALIGNMENT;
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200495 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800496
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200497 header_offset = content_offset - header_size;
498 if (header_offset % align)
499 header_offset -= header_offset % align;
500 if (header_offset < addr) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800501 ERROR("No space to hold cbfs_file header.");
502 return -1;
503 }
504
505 // Process buffer BEFORE content_offset.
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200506 if (header_offset - addr > min_entry_size) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800507 DEBUG("|min|...|header|content|... <create new entry>\n");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200508 len = header_offset - addr - min_entry_size;
Patrick Georgiedf25d92015-08-12 09:12:06 +0200509 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800510 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
511 entry = cbfs_find_next_entry(image, entry);
512 addr = cbfs_get_entry_addr(image, entry);
513 }
514
Patrick Georgi7a33b532015-08-25 13:00:04 +0200515 len = content_offset - addr - header_size;
Patrick Georgia60e7b62015-08-25 22:26:02 +0200516 memcpy(entry, header, header_size);
Patrick Georgi7a33b532015-08-25 13:00:04 +0200517 if (len != 0) {
518 /* the header moved backwards a bit to accomodate cbfs_file
519 * alignment requirements, so patch up ->offset to still point
520 * to file data.
521 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800522 DEBUG("|..|header|content|... <use offset to create entry>\n");
Patrick Georgiae7efb92015-08-25 13:11:28 +0200523 DEBUG("before: offset=0x%x\n", ntohl(entry->offset));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800524 // TODO reset expanded name buffer to 0xFF.
Patrick Georgi7a33b532015-08-25 13:00:04 +0200525 entry->offset = htonl(ntohl(entry->offset) + len);
Patrick Georgiae7efb92015-08-25 13:11:28 +0200526 DEBUG("after: offset=0x%x\n", ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800527 }
528
529 // Ready to fill data into entry.
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800530 DEBUG("content_offset: 0x%x, entry location: %x\n",
531 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
532 image->buffer.data));
533 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
Patrick Georgicccc9d42015-04-28 13:09:36 +0200534 (ptrdiff_t)content_offset);
Patrick Georgi19c80b22015-08-25 13:16:04 +0200535 memcpy(CBFS_SUBHEADER(entry), data, ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800536 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
537
538 // Process buffer AFTER entry.
539 entry = cbfs_find_next_entry(image, entry);
540 addr = cbfs_get_entry_addr(image, entry);
Sol Boucher05725652015-04-02 20:58:26 -0700541 if (addr == addr_next)
542 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800543
Sol Boucher05725652015-04-02 20:58:26 -0700544 assert(addr < addr_next);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800545 if (addr_next - addr < min_entry_size) {
Sol Boucher636cc852015-04-03 09:13:04 -0700546 DEBUG("No need for new \"empty\" entry\n");
547 /* No need to increase the size of the just
548 * stored file to extend to next file. Alignment
549 * of next file takes care of this.
550 */
551 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800552 }
553
554 len = addr_next - addr - min_entry_size;
Patrick Georgiedf25d92015-08-12 09:12:06 +0200555 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800556 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
557 return 0;
558}
559
560int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Patrick Georgie5903582015-08-25 13:53:42 +0200561 uint32_t content_offset,
Patrick Georgif5252f32015-08-25 22:27:57 +0200562 struct cbfs_file *header)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700563{
Sol Boucher67d59982015-05-07 02:39:22 -0700564 assert(image);
565 assert(buffer);
566 assert(buffer->data);
Sol Boucher67d59982015-05-07 02:39:22 -0700567 assert(!IS_TOP_ALIGNED_ADDRESS(content_offset));
568
Patrick Georgia60e7b62015-08-25 22:26:02 +0200569 const char *name = header->filename;
Patrick Georgie5903582015-08-25 13:53:42 +0200570
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800571 uint32_t entry_type;
572 uint32_t addr, addr_next;
573 struct cbfs_file *entry, *next;
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200574 uint32_t need_size;
Patrick Georgif5252f32015-08-25 22:27:57 +0200575 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800576
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800577 need_size = header_size + buffer->size;
578 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
579 name, content_offset, header_size, buffer->size, need_size);
580
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800581 // Merge empty entries.
582 DEBUG("(trying to merge empty entries...)\n");
583 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
584
585 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800586 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800587 entry = cbfs_find_next_entry(image, entry)) {
588
589 entry_type = ntohl(entry->type);
590 if (entry_type != CBFS_COMPONENT_NULL)
591 continue;
592
593 addr = cbfs_get_entry_addr(image, entry);
594 next = cbfs_find_next_entry(image, entry);
595 addr_next = cbfs_get_entry_addr(image, next);
596
597 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
598 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600599
600 /* Will the file fit? Don't yet worry if we have space for a new
601 * "empty" entry. We take care of that later.
602 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800603 if (addr + need_size > addr_next)
604 continue;
605
Patrick Georgiaa44dbd2015-08-12 12:05:21 +0200606 // Test for complicated cases
607 if (content_offset > 0) {
608 if (addr_next < content_offset) {
609 DEBUG("Not for specified offset yet");
610 continue;
611 } else if (addr > content_offset) {
612 DEBUG("Exceed specified content_offset.");
613 break;
614 } else if (addr + header_size > content_offset) {
615 ERROR("Not enough space for header.\n");
616 break;
617 } else if (content_offset + buffer->size > addr_next) {
618 ERROR("Not enough space for content.\n");
619 break;
620 }
621 }
622
623 // TODO there are more few tricky cases that we may
624 // want to fit by altering offset.
625
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200626 if (content_offset == 0) {
627 // we tested every condition earlier under which
628 // placing the file there might fail
629 content_offset = addr + header_size;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800630 }
631
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800632 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
633 addr, addr_next - addr, content_offset);
634
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200635 if (cbfs_add_entry_at(image, entry, buffer->data,
636 content_offset, header) == 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800637 return 0;
638 }
639 break;
640 }
641
642 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
643 buffer->name, buffer->size, buffer->size / 1024, content_offset);
644 return -1;
645}
646
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700647struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
648{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800649 struct cbfs_file *entry;
650 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800651 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800652 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgic569b8b2015-07-15 16:42:38 +0200653 if (strcasecmp(entry->filename, name) == 0) {
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800654 DEBUG("cbfs_get_entry: found %s\n", name);
655 return entry;
656 }
657 }
658 return NULL;
659}
660
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500661static int cbfs_stage_decompress(struct cbfs_stage *stage, struct buffer *buff)
Aaron Durbin539aed02015-10-23 17:42:32 -0500662{
663 struct buffer reader;
Aaron Durbin539aed02015-10-23 17:42:32 -0500664 char *orig_buffer;
665 char *new_buffer;
666 size_t new_buff_sz;
667 decomp_func_ptr decompress;
668
669 buffer_clone(&reader, buff);
670
671 /* The stage metadata is in little endian. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500672 stage->compression = xdr_le.get32(&reader);
673 stage->entry = xdr_le.get64(&reader);
674 stage->load = xdr_le.get64(&reader);
675 stage->len = xdr_le.get32(&reader);
676 stage->memlen = xdr_le.get32(&reader);
Aaron Durbin539aed02015-10-23 17:42:32 -0500677
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500678 /* Create a buffer just with the uncompressed program now that the
679 * struct cbfs_stage has been peeled off. */
680 if (stage->compression == CBFS_COMPRESS_NONE) {
681 new_buff_sz = buffer_size(buff) - sizeof(struct cbfs_stage);
682
683 orig_buffer = buffer_get(buff);
684 new_buffer = calloc(1, new_buff_sz);
685 memcpy(new_buffer, orig_buffer + sizeof(struct cbfs_stage),
686 new_buff_sz);
687 buffer_init(buff, buff->name, new_buffer, new_buff_sz);
688 free(orig_buffer);
Aaron Durbin539aed02015-10-23 17:42:32 -0500689 return 0;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500690 }
Aaron Durbin539aed02015-10-23 17:42:32 -0500691
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500692 decompress = decompression_function(stage->compression);
Aaron Durbin539aed02015-10-23 17:42:32 -0500693 if (decompress == NULL)
694 return -1;
695
696 orig_buffer = buffer_get(buff);
697
698 /* This can be too big of a buffer needed, but there's no current
699 * field indicating decompressed size of data. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500700 new_buff_sz = stage->memlen;
Aaron Durbin539aed02015-10-23 17:42:32 -0500701 new_buffer = calloc(1, new_buff_sz);
702
703 if (decompress(orig_buffer + sizeof(struct cbfs_stage),
704 (int)(buffer_size(buff) - sizeof(struct cbfs_stage)),
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500705 new_buffer, (int)new_buff_sz, &new_buff_sz)) {
Aaron Durbin539aed02015-10-23 17:42:32 -0500706 ERROR("Couldn't decompress stage.\n");
707 free(new_buffer);
708 return -1;
709 }
710
711 /* Include correct size for full stage info. */
Aaron Durbin539aed02015-10-23 17:42:32 -0500712 buffer_init(buff, buff->name, new_buffer, new_buff_sz);
Aaron Durbin539aed02015-10-23 17:42:32 -0500713
714 /* True decompressed size is just the data size -- no metadata. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500715 stage->len = new_buff_sz;
Aaron Durbin539aed02015-10-23 17:42:32 -0500716 /* Stage is not compressed. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500717 stage->compression = CBFS_COMPRESS_NONE;
Aaron Durbin539aed02015-10-23 17:42:32 -0500718
719 free(orig_buffer);
720
721 return 0;
722}
723
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500724static int init_elf_from_arch(Elf64_Ehdr *ehdr, uint32_t cbfs_arch)
725{
726 int endian;
727 int nbits;
728 int machine;
729
730 switch (cbfs_arch) {
731 case CBFS_ARCHITECTURE_X86:
732 endian = ELFDATA2LSB;
733 nbits = ELFCLASS32;
734 machine = EM_386;
735 break;
736 case CBFS_ARCHITECTURE_ARM:
737 endian = ELFDATA2LSB;
738 nbits = ELFCLASS32;
739 machine = EM_ARM;
740 break;
741 case CBFS_ARCHITECTURE_AARCH64:
742 endian = ELFDATA2LSB;
743 nbits = ELFCLASS64;
744 machine = EM_AARCH64;
745 break;
746 case CBFS_ARCHITECTURE_MIPS:
747 endian = ELFDATA2LSB;
748 nbits = ELFCLASS32;
749 machine = EM_MIPS;
750 break;
751 case CBFS_ARCHITECTURE_RISCV:
752 endian = ELFDATA2LSB;
753 nbits = ELFCLASS32;
754 machine = EM_RISCV;
755 break;
756 default:
757 ERROR("Unsupported arch: %x\n", cbfs_arch);
758 return -1;
759 }
760
761 elf_init_eheader(ehdr, machine, nbits, endian);
762 return 0;
763}
764
765static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch)
766{
767 Elf64_Ehdr ehdr;
768 Elf64_Shdr shdr;
769 struct cbfs_stage stage;
770 struct elf_writer *ew;
771 struct buffer elf_out;
772 size_t empty_sz;
773
774 if (cbfs_stage_decompress(&stage, buff)) {
775 ERROR("Failed to decompress stage.\n");
776 return -1;
777 }
778
779 if (init_elf_from_arch(&ehdr, arch))
780 return -1;
781
782 ehdr.e_entry = stage.entry;
783
784 ew = elf_writer_init(&ehdr);
785 if (ew == NULL) {
786 ERROR("Unable to init ELF writer.\n");
787 return -1;
788 }
789
790 memset(&shdr, 0, sizeof(shdr));
791 shdr.sh_type = SHT_PROGBITS;
792 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
793 shdr.sh_addr = stage.load;
794 shdr.sh_size = stage.len;
795 empty_sz = stage.memlen - stage.len;
796
797 if (elf_writer_add_section(ew, &shdr, buff, ".program")) {
798 ERROR("Unable to add ELF section: .program\n");
799 elf_writer_destroy(ew);
800 return -1;
801 }
802
803 if (empty_sz != 0) {
804 struct buffer b;
805
806 buffer_init(&b, NULL, NULL, 0);
807 memset(&shdr, 0, sizeof(shdr));
808 shdr.sh_type = SHT_NOBITS;
809 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
810 shdr.sh_addr = stage.load + stage.len;
811 shdr.sh_size = empty_sz;
812 if (elf_writer_add_section(ew, &shdr, &b, ".empty")) {
813 ERROR("Unable to add ELF section: .empty\n");
814 elf_writer_destroy(ew);
815 return -1;
816 }
817 }
818
819 if (elf_writer_serialize(ew, &elf_out)) {
820 ERROR("Unable to create ELF file from stage.\n");
821 elf_writer_destroy(ew);
822 return -1;
823 }
824
825 /* Flip buffer with the created ELF one. */
826 buffer_delete(buff);
827 *buff = elf_out;
828
829 elf_writer_destroy(ew);
830
831 return 0;
832}
833
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800834int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500835 const char *filename, uint32_t arch)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700836{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800837 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
838 struct buffer buffer;
839 if (!entry) {
840 ERROR("File not found: %s\n", entry_name);
841 return -1;
842 }
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200843
844 unsigned int decompressed_size = 0;
845 unsigned int compression = cbfs_file_get_compression_info(entry,
846 &decompressed_size);
847
848 decomp_func_ptr decompress = decompression_function(compression);
849 if (!decompress) {
850 ERROR("looking up decompression routine failed\n");
851 return -1;
852 }
853
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800854 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
855 entry_name, cbfs_get_entry_addr(image, entry),
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200856 get_cbfs_entry_type_name(ntohl(entry->type)), decompressed_size);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800857
Patrick Georgi011b0b32015-08-26 12:16:54 +0200858 if (ntohl(entry->type) == CBFS_COMPONENT_PAYLOAD) {
859 WARN("Payloads are extracted in SELF format.\n");
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800860 }
861
Aaron Durbin539aed02015-10-23 17:42:32 -0500862 buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0);
863
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200864 buffer.data = malloc(decompressed_size);
865 buffer.size = decompressed_size;
866 if (decompress(CBFS_SUBHEADER(entry), ntohl(entry->len),
Aaron Durbin5213c532015-10-23 17:38:40 -0500867 buffer.data, buffer.size, NULL)) {
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200868 ERROR("decompression failed for %s\n", entry_name);
Aaron Durbin539aed02015-10-23 17:42:32 -0500869 buffer_delete(&buffer);
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200870 return -1;
871 }
Aaron Durbin539aed02015-10-23 17:42:32 -0500872
873 /*
874 * The stage metadata is never compressed proper for cbfs_stage
875 * files. The contents of the stage data can be though. Therefore
876 * one has to do a second pass for stages to potentially decompress
877 * the stage data to make it more meaningful.
878 */
879 if (ntohl(entry->type) == CBFS_COMPONENT_STAGE) {
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500880 if (cbfs_stage_make_elf(&buffer, arch)) {
Aaron Durbin539aed02015-10-23 17:42:32 -0500881 buffer_delete(&buffer);
882 return -1;
883 }
884 }
885
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800886 if (buffer_write_file(&buffer, filename) != 0) {
887 ERROR("Failed to write %s into %s.\n",
888 entry_name, filename);
Aaron Durbin539aed02015-10-23 17:42:32 -0500889 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800890 return -1;
891 }
Aaron Durbin539aed02015-10-23 17:42:32 -0500892
893 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800894 INFO("Successfully dumped the file to: %s\n", filename);
895 return 0;
896}
897
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700898int cbfs_remove_entry(struct cbfs_image *image, const char *name)
899{
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +0200900 struct cbfs_file *entry;
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800901 entry = cbfs_get_entry(image, name);
902 if (!entry) {
903 ERROR("CBFS file %s not found.\n", name);
904 return -1;
905 }
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800906 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
Patrick Georgic569b8b2015-07-15 16:42:38 +0200907 entry->filename, cbfs_get_entry_addr(image, entry));
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800908 entry->type = htonl(CBFS_COMPONENT_DELETED);
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +0200909 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800910 return 0;
911}
912
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700913int cbfs_print_header_info(struct cbfs_image *image)
914{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800915 char *name = strdup(image->buffer.name);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700916 assert(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800917 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800918 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800919 basename(name),
920 image->buffer.size / 1024,
Sol Boucher3e060ed2015-05-05 15:40:15 -0700921 image->header.bootblocksize,
922 image->header.romsize,
923 image->header.offset,
924 image->header.align,
925 arch_to_string(image->header.architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800926 free(name);
927 return 0;
928}
929
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700930static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
931{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800932 fprintf(fp,
933 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
934 "length: %d/%d\n",
935 lookup_name_by_type(types_cbfs_compression,
936 stage->compression, "(unknown)"),
937 stage->entry,
938 stage->load,
939 stage->len,
940 stage->memlen);
941 return 0;
942}
943
Hung-Te Lin0780d672014-05-16 10:14:05 +0800944static int cbfs_print_decoded_payload_segment_info(
945 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800946{
Hung-Te Lin0780d672014-05-16 10:14:05 +0800947 /* The input (seg) must be already decoded by
948 * cbfs_decode_payload_segment.
949 */
950 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800951 case PAYLOAD_SEGMENT_CODE:
952 case PAYLOAD_SEGMENT_DATA:
953 fprintf(fp, " %s (%s compression, offset: 0x%x, "
954 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800955 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800956 "code " : "data"),
957 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800958 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800959 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800960 seg->offset, seg->load_addr, seg->len,
961 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800962 break;
963
964 case PAYLOAD_SEGMENT_ENTRY:
965 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800966 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800967 break;
968
969 case PAYLOAD_SEGMENT_BSS:
970 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
971 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800972 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800973 break;
974
975 case PAYLOAD_SEGMENT_PARAMS:
976 fprintf(fp, " parameters\n");
977 break;
978
979 default:
980 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
981 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800982 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800983 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800984 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800985 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800986 seg->offset, seg->load_addr, seg->len,
987 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800988 break;
989 }
990 return 0;
991}
992
993int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700994 void *arg)
995{
Patrick Georgic569b8b2015-07-15 16:42:38 +0200996 const char *name = entry->filename;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800997 struct cbfs_payload_segment *payload;
998 FILE *fp = (FILE *)arg;
999
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001000 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001001 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1002 cbfs_get_entry_addr(image, entry));
1003 return -1;
1004 }
1005 if (!fp)
1006 fp = stdout;
1007
Patrick Georgic82725c2015-08-26 12:13:03 +02001008 unsigned int decompressed_size = 0;
Patrick Georgia71c83f2015-08-26 12:23:26 +02001009 unsigned int compression = cbfs_file_get_compression_info(entry,
1010 &decompressed_size);
Patrick Georgic82725c2015-08-26 12:13:03 +02001011
1012 if (compression == CBFS_COMPRESS_NONE) {
1013 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
1014 *name ? name : "(empty)",
1015 cbfs_get_entry_addr(image, entry),
1016 get_cbfs_entry_type_name(ntohl(entry->type)),
1017 ntohl(entry->len));
1018 } else {
1019 fprintf(fp, "%-30s 0x%-8x %-12s %d (%d after %s decompression)\n",
1020 *name ? name : "(empty)",
1021 cbfs_get_entry_addr(image, entry),
1022 get_cbfs_entry_type_name(ntohl(entry->type)),
1023 ntohl(entry->len),
1024 decompressed_size,
1025 lookup_name_by_type(types_cbfs_compression,
1026 compression, "(unknown)")
1027 );
1028 }
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001029
Patrick Georgi89f20342015-10-01 15:54:04 +02001030 struct cbfs_file_attr_hash *hash = NULL;
1031 while ((hash = cbfs_file_get_next_hash(entry, hash)) != NULL) {
1032 unsigned int hash_type = ntohl(hash->hash_type);
1033 if (hash_type > CBFS_NUM_SUPPORTED_HASHES) {
1034 fprintf(fp, "invalid hash type %d\n", hash_type);
1035 break;
1036 }
1037 size_t hash_len = widths_cbfs_hash[hash_type];
1038 char *hash_str = bintohex(hash->hash_data, hash_len);
1039 uint8_t local_hash[hash_len];
1040 if (vb2_digest_buffer(CBFS_SUBHEADER(entry),
1041 ntohl(entry->len), hash_type, local_hash,
1042 hash_len) != VB2_SUCCESS) {
1043 fprintf(fp, "failed to hash '%s'\n", name);
1044 break;
1045 }
1046 int valid = memcmp(local_hash, hash->hash_data, hash_len) == 0;
1047 const char *valid_str = valid ? "valid" : "invalid";
1048
1049 fprintf(fp, " hash %s:%s %s\n",
1050 get_hash_attr_name(hash_type),
1051 hash_str, valid_str);
1052 free(hash_str);
1053 }
1054
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001055 if (!verbose)
1056 return 0;
1057
1058 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
1059 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
1060 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
1061 ntohl(entry->len));
1062
1063 /* note the components of the subheader may be in host order ... */
1064 switch (ntohl(entry->type)) {
1065 case CBFS_COMPONENT_STAGE:
1066 cbfs_print_stage_info((struct cbfs_stage *)
1067 CBFS_SUBHEADER(entry), fp);
1068 break;
1069
1070 case CBFS_COMPONENT_PAYLOAD:
Paul Menzel831bbe82015-08-08 20:20:57 +02001071 payload = (struct cbfs_payload_segment *)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001072 CBFS_SUBHEADER(entry);
1073 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +08001074 struct cbfs_payload_segment seg;
1075 cbfs_decode_payload_segment(&seg, payload);
1076 cbfs_print_decoded_payload_segment_info(
1077 &seg, fp);
1078 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001079 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +08001080 else
Aaron Durbinca630272014-08-05 10:48:20 -05001081 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001082 }
1083 break;
1084 default:
1085 break;
1086 }
1087 return 0;
1088}
1089
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001090int cbfs_print_directory(struct cbfs_image *image)
1091{
Sol Boucher67a0a862015-03-18 12:36:27 -07001092 if (cbfs_is_legacy_cbfs(image))
1093 cbfs_print_header_info(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001094 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
1095 cbfs_walk(image, cbfs_print_entry_info, NULL);
1096 return 0;
1097}
1098
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001099int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -08001100 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001101{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001102 struct cbfs_file *next;
Aaron Durbin285111f2015-08-08 20:25:17 +02001103 uint8_t *name;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001104 uint32_t type, addr, last_addr;
1105
1106 type = ntohl(entry->type);
1107 if (type == CBFS_COMPONENT_DELETED) {
1108 // Ready to be recycled.
1109 type = CBFS_COMPONENT_NULL;
1110 entry->type = htonl(type);
Aaron Durbin285111f2015-08-08 20:25:17 +02001111 // Place NUL byte as first byte of name to be viewed as "empty".
1112 name = (void *)&entry[1];
1113 *name = '\0';
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001114 }
1115 if (type != CBFS_COMPONENT_NULL)
1116 return 0;
1117
1118 next = cbfs_find_next_entry(image, entry);
1119
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001120 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001121 type = ntohl(next->type);
1122 if (type == CBFS_COMPONENT_DELETED) {
1123 type = CBFS_COMPONENT_NULL;
1124 next->type = htonl(type);
1125 }
1126 if (type != CBFS_COMPONENT_NULL)
1127 return 0;
1128
1129 addr = cbfs_get_entry_addr(image, entry);
1130 last_addr = cbfs_get_entry_addr(
1131 image, cbfs_find_next_entry(image, next));
1132
1133 // Now, we find two deleted/empty entries; try to merge now.
1134 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
1135 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
1136 cbfs_get_entry_addr(image, next), ntohl(next->len));
Patrick Georgiedf25d92015-08-12 09:12:06 +02001137 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL,
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001138 (last_addr - addr -
1139 cbfs_calculate_file_header_size("")),
1140 "");
1141 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
1142 next = cbfs_find_next_entry(image, entry);
1143 }
1144 return 0;
1145}
1146
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001147int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001148 void *arg)
1149{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001150 int count = 0;
1151 struct cbfs_file *entry;
1152 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001153 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001154 entry = cbfs_find_next_entry(image, entry)) {
1155 count ++;
1156 if (callback(image, entry, arg) != 0)
1157 break;
1158 }
1159 return count;
1160}
1161
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001162static int cbfs_header_valid(struct cbfs_header *header, size_t size)
1163{
1164 if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
1165 ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
1166 (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
1167 (ntohl(header->romsize) <= size) &&
1168 (ntohl(header->offset) < ntohl(header->romsize)))
1169 return 1;
1170 return 0;
1171}
1172
1173struct cbfs_header *cbfs_find_header(char *data, size_t size,
1174 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001175{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001176 size_t offset;
1177 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001178 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001179 struct cbfs_header *header, *result = NULL;
1180
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001181 if (forced_offset < (size - sizeof(struct cbfs_header))) {
1182 /* Check if the forced header is valid. */
1183 header = (struct cbfs_header *)(data + forced_offset);
1184 if (cbfs_header_valid(header, size))
1185 return header;
1186 return NULL;
1187 }
1188
Julius Wernerefcee762014-11-10 13:14:24 -08001189 // Try finding relative offset of master header at end of file first.
1190 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
1191 offset = size + rel_offset;
1192 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
1193 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001194
Hung-Te Lineab2c812013-01-29 01:56:17 +08001195 if (offset >= size - sizeof(*header) ||
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001196 !cbfs_header_valid((struct cbfs_header *)(data + offset), size)) {
Julius Wernerefcee762014-11-10 13:14:24 -08001197 // Some use cases append non-CBFS data to the end of the ROM.
1198 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +08001199 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001200 }
Hung-Te Lineab2c812013-01-29 01:56:17 +08001201
1202 for (; offset + sizeof(*header) < size; offset++) {
1203 header = (struct cbfs_header *)(data + offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001204 if (!cbfs_header_valid(header, size))
Hung-Te Lineab2c812013-01-29 01:56:17 +08001205 continue;
Julius Wernerefcee762014-11-10 13:14:24 -08001206 if (!found++)
1207 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001208 }
Julius Wernerefcee762014-11-10 13:14:24 -08001209 if (found > 1)
1210 // Top-aligned images usually have a working relative offset
1211 // field, so this is more likely to happen on bottom-aligned
1212 // ones (where the first header is the "outermost" one)
1213 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +08001214 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001215 return result;
1216}
1217
1218
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001219struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
1220{
Sol Boucher3e060ed2015-05-05 15:40:15 -07001221 assert(image);
Sol Boucher67a0a862015-03-18 12:36:27 -07001222 return image->has_header ? (struct cbfs_file *)(image->buffer.data +
1223 image->header.offset) :
1224 (struct cbfs_file *)image->buffer.data;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001225}
1226
1227struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001228 struct cbfs_file *entry)
1229{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001230 uint32_t addr = cbfs_get_entry_addr(image, entry);
Sol Boucher67a0a862015-03-18 12:36:27 -07001231 int align = image->has_header ? image->header.align :
1232 CBFS_ENTRY_ALIGNMENT;
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001233 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001234 addr += ntohl(entry->offset) + ntohl(entry->len);
1235 addr = align_up(addr, align);
1236 return (struct cbfs_file *)(image->buffer.data + addr);
1237}
1238
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001239uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
1240{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001241 assert(image && image->buffer.data && entry);
1242 return (int32_t)((char *)entry - image->buffer.data);
1243}
1244
Sol Boucher67a0a862015-03-18 12:36:27 -07001245int cbfs_is_valid_cbfs(struct cbfs_image *image)
1246{
1247 return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC,
1248 strlen(CBFS_FILE_MAGIC));
1249}
1250
1251int cbfs_is_legacy_cbfs(struct cbfs_image *image)
1252{
1253 return image->has_header;
1254}
1255
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001256int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
1257{
Sol Bouchere3260a02015-03-25 13:40:08 -07001258 uint32_t offset = cbfs_get_entry_addr(image, entry);
1259
1260 if (offset >= image->buffer.size)
1261 return 0;
1262
1263 struct buffer entry_data;
1264 buffer_clone(&entry_data, &image->buffer);
1265 buffer_seek(&entry_data, offset);
1266 return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
Sol Boucher67a0a862015-03-18 12:36:27 -07001267 strlen(CBFS_FILE_MAGIC));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001268}
1269
Patrick Georgi57edf162015-08-12 09:20:11 +02001270struct cbfs_file *cbfs_create_file_header(int type,
Vadim Bendebury45e59972014-12-23 15:59:57 -08001271 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001272{
Patrick Georgi2c615062015-07-15 20:49:00 +02001273 struct cbfs_file *entry = malloc(MAX_CBFS_FILE_HEADER_BUFFER);
1274 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, MAX_CBFS_FILE_HEADER_BUFFER);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001275 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
Patrick Georgiedf25d92015-08-12 09:12:06 +02001276 entry->type = htonl(type);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001277 entry->len = htonl(len);
Patrick Georgi0d618af2015-07-15 18:28:23 +02001278 entry->attributes_offset = 0;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001279 entry->offset = htonl(cbfs_calculate_file_header_size(name));
Patrick Georgic569b8b2015-07-15 16:42:38 +02001280 memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry));
1281 strcpy(entry->filename, name);
Patrick Georgi57edf162015-08-12 09:20:11 +02001282 return entry;
1283}
1284
1285int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
1286 size_t len, const char *name)
1287{
1288 struct cbfs_file *tmp = cbfs_create_file_header(type, len, name);
1289 memcpy(entry, tmp, ntohl(tmp->offset));
1290 free(tmp);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001291 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1292 return 0;
1293}
1294
Patrick Georgi2c615062015-07-15 20:49:00 +02001295struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file)
1296{
1297 /* attributes_offset should be 0 when there is no attribute, but all
1298 * values that point into the cbfs_file header are invalid, too. */
1299 if (ntohl(file->attributes_offset) <= sizeof(*file))
1300 return NULL;
1301
1302 /* There needs to be enough space for the file header and one
1303 * attribute header for this to make sense. */
1304 if (ntohl(file->offset) <=
1305 sizeof(*file) + sizeof(struct cbfs_file_attribute))
1306 return NULL;
1307
1308 return (struct cbfs_file_attribute *)
1309 (((uint8_t *)file) + ntohl(file->attributes_offset));
1310}
1311
1312struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
1313 struct cbfs_file_attribute *attr)
1314{
1315 /* ex falso sequitur quodlibet */
1316 if (attr == NULL)
1317 return NULL;
1318
1319 /* Is there enough space for another attribute? */
1320 if ((uint8_t *)attr + ntohl(attr->len) +
1321 sizeof(struct cbfs_file_attribute) >=
1322 (uint8_t *)file + ntohl(file->offset))
1323 return NULL;
1324
1325 struct cbfs_file_attribute *next = (struct cbfs_file_attribute *)
1326 (((uint8_t *)attr) + ntohl(attr->len));
1327 /* If any, "unused" attributes must come last. */
1328 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED)
1329 return NULL;
1330 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
1331 return NULL;
1332
1333 return next;
1334}
1335
1336struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
1337 uint32_t tag,
1338 uint32_t size)
1339{
1340 struct cbfs_file_attribute *attr, *next;
1341 next = cbfs_file_first_attr(header);
1342 do {
1343 attr = next;
1344 next = cbfs_file_next_attr(header, attr);
1345 } while (next != NULL);
1346 uint32_t header_size = ntohl(header->offset) + size;
1347 if (header_size > MAX_CBFS_FILE_HEADER_BUFFER) {
1348 DEBUG("exceeding allocated space for cbfs_file headers");
1349 return NULL;
1350 }
1351 /* attr points to the last valid attribute now.
1352 * If NULL, we have to create the first one. */
1353 if (attr == NULL) {
1354 /* New attributes start where the header ends.
1355 * header->offset is later set to accomodate the
1356 * additional structure.
1357 * No endianess translation necessary here, because both
1358 * fields are encoded the same way. */
1359 header->attributes_offset = header->offset;
1360 attr = (struct cbfs_file_attribute *)
1361 (((uint8_t *)header) +
1362 ntohl(header->attributes_offset));
1363 } else {
1364 attr = (struct cbfs_file_attribute *)
1365 (((uint8_t *)attr) +
1366 ntohl(attr->len));
1367 }
1368 header->offset = htonl(header_size);
1369 memset(attr, CBFS_CONTENT_DEFAULT_VALUE, size);
1370 attr->tag = htonl(tag);
1371 attr->len = htonl(size);
1372 return attr;
1373}
1374
Patrick Georgi89f20342015-10-01 15:54:04 +02001375int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer,
1376 enum vb2_hash_algorithm hash_type)
1377{
1378 if (hash_type >= CBFS_NUM_SUPPORTED_HASHES)
1379 return -1;
1380
1381 unsigned hash_size = widths_cbfs_hash[hash_type];
1382 if (hash_size == 0)
1383 return -1;
1384
1385 struct cbfs_file_attr_hash *attrs =
1386 (struct cbfs_file_attr_hash *)cbfs_add_file_attr(header,
1387 CBFS_FILE_ATTR_TAG_HASH,
1388 sizeof(struct cbfs_file_attr_hash) + hash_size);
1389
1390 if (attrs == NULL)
1391 return -1;
1392
1393 attrs->hash_type = htonl(hash_type);
1394 if (vb2_digest_buffer(buffer_get(buffer), buffer_size(buffer),
1395 hash_type, attrs->hash_data, hash_size) != VB2_SUCCESS)
1396 return -1;
1397
1398 return 0;
1399}
1400
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001401/* Finds a place to hold whole data in same memory page. */
1402static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1403{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001404 if (!page)
1405 return 1;
1406 return (start / page) == (start + size - 1) / page;
1407}
1408
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001409/* Tests if data can fit in a range by given offset:
Aaron Durbind7339412015-09-15 12:50:14 -05001410 * start ->| metadata_size | offset (+ size) |<- end
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001411 */
Aaron Durbind7339412015-09-15 12:50:14 -05001412static int is_in_range(size_t start, size_t end, size_t metadata_size,
1413 size_t offset, size_t size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001414{
Aaron Durbind7339412015-09-15 12:50:14 -05001415 return (offset >= start + metadata_size && offset + size <= end);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001416}
1417
Aaron Durbind7339412015-09-15 12:50:14 -05001418int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size,
1419 size_t page_size, size_t align, size_t metadata_size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001420{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001421 struct cbfs_file *entry;
1422 size_t need_len;
Aaron Durbind7339412015-09-15 12:50:14 -05001423 size_t addr, addr_next, addr2, addr3, offset;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001424
1425 /* Default values: allow fitting anywhere in ROM. */
1426 if (!page_size)
Sol Boucher67a0a862015-03-18 12:36:27 -07001427 page_size = image->has_header ? image->header.romsize :
1428 image->buffer.size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001429 if (!align)
1430 align = 1;
1431
1432 if (size > page_size)
Aaron Durbind7339412015-09-15 12:50:14 -05001433 ERROR("Input file size (%zd) greater than page size (%zd).\n",
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001434 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001435
Aaron Durbind7339412015-09-15 12:50:14 -05001436 size_t image_align = image->has_header ? image->header.align :
Sol Boucher67a0a862015-03-18 12:36:27 -07001437 CBFS_ENTRY_ALIGNMENT;
1438 if (page_size % image_align)
Aaron Durbind7339412015-09-15 12:50:14 -05001439 WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001440 __func__, page_size, image_align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001441
Aaron Durbind7339412015-09-15 12:50:14 -05001442 need_len = metadata_size + size;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001443
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001444 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001445 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
1446
1447 /* Three cases of content location on memory page:
1448 * case 1.
1449 * | PAGE 1 | PAGE 2 |
1450 * | <header><content>| Fit. Return start of content.
1451 *
1452 * case 2.
1453 * | PAGE 1 | PAGE 2 |
1454 * | <header><content> | Fits when we shift content to align
1455 * shift-> | <header>|<content> | at starting of PAGE 2.
1456 *
1457 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001458 * | PAGE 1 | PAGE 2 | PAGE 3 |
1459 * | <header>< content > | Can't fit. If we shift content to
1460 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1461 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001462 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001463 * The returned address can be then used as "base-address" (-b) in add-*
1464 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1465 * For stage targets, the address is also used to re-link stage before
1466 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001467 */
1468 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001469 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001470 entry = cbfs_find_next_entry(image, entry)) {
1471
1472 uint32_t type = ntohl(entry->type);
1473 if (type != CBFS_COMPONENT_NULL)
1474 continue;
1475
1476 addr = cbfs_get_entry_addr(image, entry);
1477 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
1478 image, entry));
1479 if (addr_next - addr < need_len)
1480 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001481
Aaron Durbind7339412015-09-15 12:50:14 -05001482 offset = align_up(addr + metadata_size, align);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001483 if (is_in_same_page(offset, size, page_size) &&
Aaron Durbind7339412015-09-15 12:50:14 -05001484 is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001485 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001486 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001487 }
1488
1489 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001490 offset = align_up(addr2, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001491 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001492 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001493 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001494 }
1495
Aaron Durbind7339412015-09-15 12:50:14 -05001496 /* Assume page_size >= metadata_size so adding one page will
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001497 * definitely provide the space for header. */
Aaron Durbind7339412015-09-15 12:50:14 -05001498 assert(page_size >= metadata_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001499 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001500 offset = align_up(addr3, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001501 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001502 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001503 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001504 }
1505 }
1506 return -1;
1507}