blob: 1cfe90d2736ad32689abcd64e09450878eea8969 [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.
Hung-Te Lineab2c812013-01-29 01:56:17 +080014 */
15
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080016#include <inttypes.h>
17#include <libgen.h>
Patrick Georgicccc9d42015-04-28 13:09:36 +020018#include <stddef.h>
Hung-Te Lineab2c812013-01-29 01:56:17 +080019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
Sol Boucher0e539312015-03-05 15:38:03 -080022#include <strings.h>
Hung-Te Lineab2c812013-01-29 01:56:17 +080023
24#include "common.h"
25#include "cbfs_image.h"
Aaron Durbin5a1e85c2015-10-27 21:02:30 -050026#include "elfparsing.h"
Aaron Durbin694fd132015-10-28 11:39:34 -050027#include "rmodule.h"
Hung-Te Lineab2c812013-01-29 01:56:17 +080028
Sol Boucher636cc852015-04-03 09:13:04 -070029/* Even though the file-adding functions---cbfs_add_entry() and
30 * cbfs_add_entry_at()---perform their sizing checks against the beginning of
31 * the subsequent section rather than a stable recorded value such as an empty
32 * file header's len field, it's possible to prove two interesting properties
33 * about their behavior:
34 * - Placing a new file within an empty entry located below an existing file
35 * entry will never leave an aligned flash address containing neither the
36 * beginning of a file header nor part of a file.
37 * - Placing a new file in an empty entry at the very end of the image such
38 * that it fits, but leaves no room for a final header, is guaranteed not to
39 * change the total amount of space for entries, even if that new file is
40 * later removed from the CBFS.
41 * These properties are somewhat nonobvious from the implementation, so the
42 * reader is encouraged to blame this comment and examine the full proofs
43 * in the commit message before making significant changes that would risk
44 * removing said guarantees.
45 */
46
Hung-Te Lineab2c812013-01-29 01:56:17 +080047/* The file name align is not defined in CBFS spec -- only a preference by
48 * (old) cbfstool. */
49#define CBFS_FILENAME_ALIGN (16)
50
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080051/* Type and format */
52
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070053static const struct typedesc_t types_cbfs_compression[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080054 {CBFS_COMPRESS_NONE, "none"},
55 {CBFS_COMPRESS_LZMA, "LZMA"},
Sol Boucher5bb90e62015-05-07 21:00:05 -070056 {0, NULL}
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080057};
58
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080059static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070060 const char *default_value)
61{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080062 int i;
63 for (i = 0; desc[i].name; i++)
64 if (desc[i].type == type)
65 return desc[i].name;
66 return default_value;
67}
68
Sol Boucherec424862015-05-07 21:00:05 -070069static int lookup_type_by_name(const struct typedesc_t *desc, const char *name)
70{
71 int i;
72 for (i = 0; desc[i].name && strcasecmp(name, desc[i].name); ++i);
73 return desc[i].name ? (int)desc[i].type : -1;
74}
75
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010076static const char *get_cbfs_entry_type_name(uint32_t type)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070077{
Patrick Georgidc37dab2015-09-09 16:46:00 +020078 return lookup_name_by_type(filetypes, type, "(unknown)");
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080079}
80
Sol Boucherec424862015-05-07 21:00:05 -070081int cbfs_parse_comp_algo(const char *name)
82{
83 return lookup_type_by_name(types_cbfs_compression, name);
84}
85
Patrick Georgi89f20342015-10-01 15:54:04 +020086static const char *get_hash_attr_name(uint16_t hash_type)
87{
88 return lookup_name_by_type(types_cbfs_hash, hash_type, "(invalid)");
89}
90
91int cbfs_parse_hash_algo(const char *name)
92{
93 return lookup_type_by_name(types_cbfs_hash, name);
94}
95
Hung-Te Linc03d9b02013-01-29 02:38:40 +080096/* CBFS image */
97
Patrick Georgi11ee08f2015-08-11 15:10:02 +020098size_t cbfs_calculate_file_header_size(const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070099{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800100 return (sizeof(struct cbfs_file) +
101 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
102}
103
Sol Boucher67a0a862015-03-18 12:36:27 -0700104/* Only call on legacy CBFSes possessing a master header. */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600105static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700106{
Sol Boucher67a0a862015-03-18 12:36:27 -0700107 assert(image);
108 assert(cbfs_is_legacy_cbfs(image));
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800109 // A bug in old cbfstool may produce extra few bytes (by alignment) and
110 // cause cbfstool to overwrite things after free space -- which is
111 // usually CBFS header on x86. We need to workaround that.
112
113 struct cbfs_file *entry, *first = NULL, *last = NULL;
114 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800115 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800116 entry = cbfs_find_next_entry(image, entry)) {
117 last = entry;
118 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600119 if ((char *)first < (char *)hdr_loc &&
120 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800121 WARN("CBFS image was created with old cbfstool with size bug. "
122 "Fixing size in last entry...\n");
Sol Boucher3e060ed2015-05-05 15:40:15 -0700123 last->len = htonl(ntohl(last->len) - image->header.align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800124 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
125 cbfs_get_entry_addr(image, entry),
126 cbfs_get_entry_addr(image,
127 cbfs_find_next_entry(image, last)));
128 }
129 return 0;
130}
131
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800132void cbfs_put_header(void *dest, const struct cbfs_header *header)
133{
134 struct buffer outheader;
135
136 outheader.data = dest;
137 outheader.size = 0;
138
139 xdr_be.put32(&outheader, header->magic);
140 xdr_be.put32(&outheader, header->version);
141 xdr_be.put32(&outheader, header->romsize);
142 xdr_be.put32(&outheader, header->bootblocksize);
143 xdr_be.put32(&outheader, header->align);
144 xdr_be.put32(&outheader, header->offset);
145 xdr_be.put32(&outheader, header->architecture);
146}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600147
Hung-Te Lin0780d672014-05-16 10:14:05 +0800148static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
149 struct cbfs_payload_segment *input)
150{
151 struct buffer seg = {
152 .data = (void *)input,
153 .size = sizeof(*input),
154 };
155 output->type = xdr_be.get32(&seg);
156 output->compression = xdr_be.get32(&seg);
157 output->offset = xdr_be.get32(&seg);
158 output->load_addr = xdr_be.get64(&seg);
159 output->len = xdr_be.get32(&seg);
160 output->mem_len = xdr_be.get32(&seg);
161 assert(seg.size == 0);
162}
163
Patrick Georgia71c83f2015-08-26 12:23:26 +0200164static int cbfs_file_get_compression_info(struct cbfs_file *entry,
165 uint32_t *decompressed_size)
166{
167 unsigned int compression = CBFS_COMPRESS_NONE;
168 *decompressed_size = ntohl(entry->len);
169 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
170 attr != NULL;
171 attr = cbfs_file_next_attr(entry, attr)) {
172 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) {
173 struct cbfs_file_attr_compression *ac =
174 (struct cbfs_file_attr_compression *)attr;
175 compression = ntohl(ac->compression);
176 if (decompressed_size)
177 *decompressed_size =
178 ntohl(ac->decompressed_size);
179 }
180 }
181 return compression;
182}
183
Patrick Georgi89f20342015-10-01 15:54:04 +0200184static struct cbfs_file_attr_hash *cbfs_file_get_next_hash(
185 struct cbfs_file *entry, struct cbfs_file_attr_hash *cur)
186{
187 struct cbfs_file_attribute *attr = (struct cbfs_file_attribute *)cur;
188 if (attr == NULL) {
189 attr = cbfs_file_first_attr(entry);
190 if (attr == NULL)
191 return NULL;
192 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
193 return (struct cbfs_file_attr_hash *)attr;
194 }
195 while ((attr = cbfs_file_next_attr(entry, attr)) != NULL) {
196 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
197 return (struct cbfs_file_attr_hash *)attr;
198 };
199 return NULL;
200}
201
Sol Boucher0e539312015-03-05 15:38:03 -0800202void cbfs_get_header(struct cbfs_header *header, void *src)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600203{
204 struct buffer outheader;
205
Sol Boucher0e539312015-03-05 15:38:03 -0800206 outheader.data = src; /* We're not modifying the data */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600207 outheader.size = 0;
208
209 header->magic = xdr_be.get32(&outheader);
210 header->version = xdr_be.get32(&outheader);
211 header->romsize = xdr_be.get32(&outheader);
212 header->bootblocksize = xdr_be.get32(&outheader);
213 header->align = xdr_be.get32(&outheader);
214 header->offset = xdr_be.get32(&outheader);
215 header->architecture = xdr_be.get32(&outheader);
216}
217
Sol Boucher67a0a862015-03-18 12:36:27 -0700218int cbfs_image_create(struct cbfs_image *image, size_t entries_size)
219{
220 assert(image);
221 assert(image->buffer.data);
222
223 size_t empty_header_len = cbfs_calculate_file_header_size("");
224 uint32_t entries_offset = 0;
225 uint32_t align = CBFS_ENTRY_ALIGNMENT;
226 if (image->has_header) {
227 entries_offset = image->header.offset;
228
229 if (entries_offset > image->buffer.size) {
230 ERROR("CBFS file entries are located outside CBFS itself\n");
231 return -1;
232 }
233
234 align = image->header.align;
235 }
236
237 // This attribute must be given in order to prove that this module
238 // correctly preserves certain CBFS properties. See the block comment
239 // near the top of this file (and the associated commit message).
240 if (align < empty_header_len) {
241 ERROR("CBFS must be aligned to at least %zu bytes\n",
242 empty_header_len);
243 return -1;
244 }
245
246 if (entries_size > image->buffer.size - entries_offset) {
247 ERROR("CBFS doesn't have enough space to fit its file entries\n");
248 return -1;
249 }
250
251 if (empty_header_len > entries_size) {
252 ERROR("CBFS is too small to fit any header\n");
253 return -1;
254 }
255 struct cbfs_file *entry_header =
256 (struct cbfs_file *)(image->buffer.data + entries_offset);
257 // This alignment is necessary in order to prove that this module
258 // correctly preserves certain CBFS properties. See the block comment
259 // near the top of this file (and the associated commit message).
260 entries_size -= entries_size % align;
261
262 size_t capacity = entries_size - empty_header_len;
263 LOG("Created CBFS (capacity = %zu bytes)\n", capacity);
Patrick Georgiedf25d92015-08-12 09:12:06 +0200264 return cbfs_create_empty_entry(entry_header, CBFS_COMPONENT_NULL,
265 capacity, "");
Sol Boucher67a0a862015-03-18 12:36:27 -0700266}
267
268int cbfs_legacy_image_create(struct cbfs_image *image,
269 uint32_t architecture,
270 uint32_t align,
271 struct buffer *bootblock,
272 uint32_t bootblock_offset,
273 uint32_t header_offset,
274 uint32_t entries_offset)
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800275{
Sol Bouchere3260a02015-03-25 13:40:08 -0700276 assert(image);
277 assert(image->buffer.data);
278 assert(bootblock);
279
Julius Wernerefcee762014-11-10 13:14:24 -0800280 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800281 uint32_t cbfs_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600282 void *header_loc;
Sol Bouchere3260a02015-03-25 13:40:08 -0700283 size_t size = image->buffer.size;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800284
285 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
286 "header=0x%x+0x%zx, entries_offset=0x%x\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700287 bootblock_offset, bootblock->size, header_offset,
288 sizeof(image->header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800289
Sol Boucher67a0a862015-03-18 12:36:27 -0700290 // Adjust legacy top-aligned address to ROM offset.
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800291 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800292 entries_offset = size + (int32_t)entries_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800293 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800294 bootblock_offset = size + (int32_t)bootblock_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800295 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800296 header_offset = size + (int32_t)header_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800297
298 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
299 "header=0x%x, entries_offset=0x%x\n",
300 bootblock_offset, header_offset, entries_offset);
301
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800302 // Prepare bootblock
303 if (bootblock_offset + bootblock->size > size) {
304 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
305 bootblock_offset, bootblock->size, size);
306 return -1;
307 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800308 if (entries_offset > bootblock_offset &&
309 entries_offset < bootblock->size) {
310 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
311 bootblock_offset, bootblock->size, entries_offset);
312 return -1;
313 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800314 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
315 bootblock->size);
316
317 // Prepare header
Sol Boucher5bad3952015-05-05 20:35:26 -0700318 if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800319 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700320 header_offset, sizeof(image->header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800321 return -1;
322 }
Sol Boucher3e060ed2015-05-05 15:40:15 -0700323 image->header.magic = CBFS_HEADER_MAGIC;
324 image->header.version = CBFS_HEADER_VERSION;
325 image->header.romsize = size;
326 image->header.bootblocksize = bootblock->size;
327 image->header.align = align;
328 image->header.offset = entries_offset;
329 image->header.architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600330
331 header_loc = (image->buffer.data + header_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700332 cbfs_put_header(header_loc, &image->header);
Sol Boucher67a0a862015-03-18 12:36:27 -0700333 image->has_header = true;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800334
Julius Wernerefcee762014-11-10 13:14:24 -0800335 // The last 4 byte of the image contain the relative offset from the end
336 // of the image to the master header as a 32-bit signed integer. x86
337 // relies on this also being its (memory-mapped, top-aligned) absolute
338 // 32-bit address by virtue of how two's complement numbers work.
339 assert(size % sizeof(int32_t) == 0);
340 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
341 *rel_offset = header_offset - size;
342
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800343 // Prepare entries
344 if (align_up(entries_offset, align) != entries_offset) {
345 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
346 entries_offset, align);
347 return -1;
348 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800349 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800350 // e = min(bootblock, header, rel_offset) where e > entries_offset.
351 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800352 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
353 cbfs_len = bootblock_offset;
354 if (header_offset > entries_offset && header_offset < cbfs_len)
355 cbfs_len = header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700356
357 if (cbfs_image_create(image, cbfs_len - entries_offset))
358 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800359 return 0;
360}
361
Sol Bouchere3260a02015-03-25 13:40:08 -0700362int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
363 uint32_t offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700364{
Sol Bouchere3260a02015-03-25 13:40:08 -0700365 assert(out);
366 assert(in);
367 assert(in->data);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600368
Sol Bouchere3260a02015-03-25 13:40:08 -0700369 buffer_clone(&out->buffer, in);
Sol Boucher67a0a862015-03-18 12:36:27 -0700370 out->has_header = false;
371
Patrick Georgi2f953d32015-09-11 18:34:39 +0200372 if (cbfs_is_valid_cbfs(out)) {
373 return 0;
374 }
375
Sol Bouchere3260a02015-03-25 13:40:08 -0700376 void *header_loc = cbfs_find_header(in->data, in->size, offset);
377 if (header_loc) {
378 cbfs_get_header(&out->header, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700379 out->has_header = true;
Sol Bouchere3260a02015-03-25 13:40:08 -0700380 cbfs_fix_legacy_size(out, header_loc);
Patrick Georgi2f953d32015-09-11 18:34:39 +0200381 return 0;
Sol Boucher67a0a862015-03-18 12:36:27 -0700382 } else if (offset != ~0u) {
383 ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n");
384 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800385 }
Patrick Georgi2f953d32015-09-11 18:34:39 +0200386 ERROR("Selected image region is not a valid CBFS.\n");
387 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800388}
389
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800390int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
391 size_t copy_size)
392{
Sol Boucher67a0a862015-03-18 12:36:27 -0700393 assert(image);
394 if (!cbfs_is_legacy_cbfs(image))
395 return -1;
396
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800397 struct cbfs_file *src_entry, *dst_entry;
398 struct cbfs_header *copy_header;
399 size_t align, entry_offset;
400 ssize_t last_entry_size;
401
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800402 size_t cbfs_offset, cbfs_end;
403 size_t copy_end = copy_offset + copy_size;
404
Sol Boucher3e060ed2015-05-05 15:40:15 -0700405 align = image->header.align;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800406
Sol Boucher3e060ed2015-05-05 15:40:15 -0700407 cbfs_offset = image->header.offset;
408 cbfs_end = image->header.romsize;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800409
410 if (copy_end > image->buffer.size) {
411 ERROR("Copy offset out of range: [%zx:%zx)\n",
412 copy_offset, copy_end);
413 return 1;
414 }
415
Sol Boucher297c88c2015-05-05 15:35:18 -0700416 /* Range check requested copy region with source cbfs. */
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800417 if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) ||
418 (copy_end >= cbfs_offset && copy_end <= cbfs_end)) {
419 ERROR("New image would overlap old one.\n");
420 return 1;
421 }
422
423 /* This will work, let's create a copy. */
424 copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700425 cbfs_put_header(copy_header, &image->header);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800426
427 copy_header->bootblocksize = 0;
428 /* Romsize is a misnomer. It's the absolute limit of cbfs content.*/
429 copy_header->romsize = htonl(copy_end);
430 entry_offset = align_up(copy_offset + sizeof(*copy_header), align);
431 copy_header->offset = htonl(entry_offset);
432 dst_entry = (struct cbfs_file *)(image->buffer.data + entry_offset);
433
434 /* Copy non-empty files */
435 for (src_entry = cbfs_find_first_entry(image);
436 src_entry && cbfs_is_valid_entry(image, src_entry);
437 src_entry = cbfs_find_next_entry(image, src_entry)) {
438 size_t entry_size;
439
440 if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) ||
441 (src_entry->type == htonl(CBFS_COMPONENT_DELETED)))
442 continue;
443
444 entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
445 memcpy(dst_entry, src_entry, entry_size);
446 dst_entry = (struct cbfs_file *)(
447 (uintptr_t)dst_entry + align_up(entry_size, align));
448
Sol Boucher0e539312015-03-05 15:38:03 -0800449 if ((size_t)((char *)dst_entry - image->buffer.data) >=
450 copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800451 ERROR("Ran out of room in copy region.\n");
452 return 1;
453 }
454 }
455
456 /* Last entry size is all the room above it. */
457 last_entry_size = copy_end - ((char *)dst_entry - image->buffer.data)
458 - cbfs_calculate_file_header_size("");
459
460 if (last_entry_size < 0)
461 WARN("No room to create the last entry!\n")
462 else
Patrick Georgiedf25d92015-08-12 09:12:06 +0200463 cbfs_create_empty_entry(dst_entry, CBFS_COMPONENT_NULL,
464 last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800465
466 return 0;
467}
468
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700469int cbfs_image_delete(struct cbfs_image *image)
470{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100471 if (image == NULL)
472 return 0;
473
Hung-Te Lineab2c812013-01-29 01:56:17 +0800474 buffer_delete(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800475 return 0;
476}
477
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800478/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
479static int cbfs_add_entry_at(struct cbfs_image *image,
480 struct cbfs_file *entry,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800481 const void *data,
Patrick Georgi7fd14182015-08-11 15:55:16 +0200482 uint32_t content_offset,
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200483 const struct cbfs_file *header)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700484{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800485 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
486 uint32_t addr = cbfs_get_entry_addr(image, entry),
487 addr_next = cbfs_get_entry_addr(image, next);
Patrick Georgi7fd14182015-08-11 15:55:16 +0200488 uint32_t min_entry_size = cbfs_calculate_file_header_size("");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200489 uint32_t len, header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700490 uint32_t align = image->has_header ? image->header.align :
491 CBFS_ENTRY_ALIGNMENT;
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200492 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800493
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200494 header_offset = content_offset - header_size;
495 if (header_offset % align)
496 header_offset -= header_offset % align;
497 if (header_offset < addr) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800498 ERROR("No space to hold cbfs_file header.");
499 return -1;
500 }
501
502 // Process buffer BEFORE content_offset.
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200503 if (header_offset - addr > min_entry_size) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800504 DEBUG("|min|...|header|content|... <create new entry>\n");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200505 len = header_offset - addr - min_entry_size;
Patrick Georgiedf25d92015-08-12 09:12:06 +0200506 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800507 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
508 entry = cbfs_find_next_entry(image, entry);
509 addr = cbfs_get_entry_addr(image, entry);
510 }
511
Patrick Georgi7a33b532015-08-25 13:00:04 +0200512 len = content_offset - addr - header_size;
Patrick Georgia60e7b62015-08-25 22:26:02 +0200513 memcpy(entry, header, header_size);
Patrick Georgi7a33b532015-08-25 13:00:04 +0200514 if (len != 0) {
515 /* the header moved backwards a bit to accomodate cbfs_file
516 * alignment requirements, so patch up ->offset to still point
517 * to file data.
518 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800519 DEBUG("|..|header|content|... <use offset to create entry>\n");
Patrick Georgiae7efb92015-08-25 13:11:28 +0200520 DEBUG("before: offset=0x%x\n", ntohl(entry->offset));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800521 // TODO reset expanded name buffer to 0xFF.
Patrick Georgi7a33b532015-08-25 13:00:04 +0200522 entry->offset = htonl(ntohl(entry->offset) + len);
Patrick Georgiae7efb92015-08-25 13:11:28 +0200523 DEBUG("after: offset=0x%x\n", ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800524 }
525
526 // Ready to fill data into entry.
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800527 DEBUG("content_offset: 0x%x, entry location: %x\n",
528 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
529 image->buffer.data));
530 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
Patrick Georgicccc9d42015-04-28 13:09:36 +0200531 (ptrdiff_t)content_offset);
Patrick Georgi19c80b22015-08-25 13:16:04 +0200532 memcpy(CBFS_SUBHEADER(entry), data, ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800533 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
534
535 // Process buffer AFTER entry.
536 entry = cbfs_find_next_entry(image, entry);
537 addr = cbfs_get_entry_addr(image, entry);
Sol Boucher05725652015-04-02 20:58:26 -0700538 if (addr == addr_next)
539 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800540
Sol Boucher05725652015-04-02 20:58:26 -0700541 assert(addr < addr_next);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800542 if (addr_next - addr < min_entry_size) {
Sol Boucher636cc852015-04-03 09:13:04 -0700543 DEBUG("No need for new \"empty\" entry\n");
544 /* No need to increase the size of the just
545 * stored file to extend to next file. Alignment
546 * of next file takes care of this.
547 */
548 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800549 }
550
551 len = addr_next - addr - min_entry_size;
Patrick Georgiedf25d92015-08-12 09:12:06 +0200552 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800553 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
554 return 0;
555}
556
557int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Patrick Georgie5903582015-08-25 13:53:42 +0200558 uint32_t content_offset,
Patrick Georgif5252f32015-08-25 22:27:57 +0200559 struct cbfs_file *header)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700560{
Sol Boucher67d59982015-05-07 02:39:22 -0700561 assert(image);
562 assert(buffer);
563 assert(buffer->data);
Sol Boucher67d59982015-05-07 02:39:22 -0700564 assert(!IS_TOP_ALIGNED_ADDRESS(content_offset));
565
Patrick Georgia60e7b62015-08-25 22:26:02 +0200566 const char *name = header->filename;
Patrick Georgie5903582015-08-25 13:53:42 +0200567
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800568 uint32_t entry_type;
569 uint32_t addr, addr_next;
570 struct cbfs_file *entry, *next;
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200571 uint32_t need_size;
Patrick Georgif5252f32015-08-25 22:27:57 +0200572 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800573
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800574 need_size = header_size + buffer->size;
575 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
576 name, content_offset, header_size, buffer->size, need_size);
577
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800578 // Merge empty entries.
579 DEBUG("(trying to merge empty entries...)\n");
580 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
581
582 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800583 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800584 entry = cbfs_find_next_entry(image, entry)) {
585
586 entry_type = ntohl(entry->type);
587 if (entry_type != CBFS_COMPONENT_NULL)
588 continue;
589
590 addr = cbfs_get_entry_addr(image, entry);
591 next = cbfs_find_next_entry(image, entry);
592 addr_next = cbfs_get_entry_addr(image, next);
593
594 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
595 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600596
597 /* Will the file fit? Don't yet worry if we have space for a new
598 * "empty" entry. We take care of that later.
599 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800600 if (addr + need_size > addr_next)
601 continue;
602
Patrick Georgiaa44dbd2015-08-12 12:05:21 +0200603 // Test for complicated cases
604 if (content_offset > 0) {
605 if (addr_next < content_offset) {
606 DEBUG("Not for specified offset yet");
607 continue;
608 } else if (addr > content_offset) {
609 DEBUG("Exceed specified content_offset.");
610 break;
611 } else if (addr + header_size > content_offset) {
612 ERROR("Not enough space for header.\n");
613 break;
614 } else if (content_offset + buffer->size > addr_next) {
615 ERROR("Not enough space for content.\n");
616 break;
617 }
618 }
619
620 // TODO there are more few tricky cases that we may
621 // want to fit by altering offset.
622
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200623 if (content_offset == 0) {
624 // we tested every condition earlier under which
625 // placing the file there might fail
626 content_offset = addr + header_size;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800627 }
628
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800629 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
630 addr, addr_next - addr, content_offset);
631
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200632 if (cbfs_add_entry_at(image, entry, buffer->data,
633 content_offset, header) == 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800634 return 0;
635 }
636 break;
637 }
638
639 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
640 buffer->name, buffer->size, buffer->size / 1024, content_offset);
641 return -1;
642}
643
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700644struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
645{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800646 struct cbfs_file *entry;
647 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800648 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800649 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgic569b8b2015-07-15 16:42:38 +0200650 if (strcasecmp(entry->filename, name) == 0) {
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800651 DEBUG("cbfs_get_entry: found %s\n", name);
652 return entry;
653 }
654 }
655 return NULL;
656}
657
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500658static int cbfs_stage_decompress(struct cbfs_stage *stage, struct buffer *buff)
Aaron Durbin539aed02015-10-23 17:42:32 -0500659{
660 struct buffer reader;
Aaron Durbin539aed02015-10-23 17:42:32 -0500661 char *orig_buffer;
662 char *new_buffer;
663 size_t new_buff_sz;
664 decomp_func_ptr decompress;
665
666 buffer_clone(&reader, buff);
667
668 /* The stage metadata is in little endian. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500669 stage->compression = xdr_le.get32(&reader);
670 stage->entry = xdr_le.get64(&reader);
671 stage->load = xdr_le.get64(&reader);
672 stage->len = xdr_le.get32(&reader);
673 stage->memlen = xdr_le.get32(&reader);
Aaron Durbin539aed02015-10-23 17:42:32 -0500674
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500675 /* Create a buffer just with the uncompressed program now that the
676 * struct cbfs_stage has been peeled off. */
677 if (stage->compression == CBFS_COMPRESS_NONE) {
678 new_buff_sz = buffer_size(buff) - sizeof(struct cbfs_stage);
679
680 orig_buffer = buffer_get(buff);
681 new_buffer = calloc(1, new_buff_sz);
682 memcpy(new_buffer, orig_buffer + sizeof(struct cbfs_stage),
683 new_buff_sz);
684 buffer_init(buff, buff->name, new_buffer, new_buff_sz);
685 free(orig_buffer);
Aaron Durbin539aed02015-10-23 17:42:32 -0500686 return 0;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500687 }
Aaron Durbin539aed02015-10-23 17:42:32 -0500688
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500689 decompress = decompression_function(stage->compression);
Aaron Durbin539aed02015-10-23 17:42:32 -0500690 if (decompress == NULL)
691 return -1;
692
693 orig_buffer = buffer_get(buff);
694
695 /* This can be too big of a buffer needed, but there's no current
696 * field indicating decompressed size of data. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500697 new_buff_sz = stage->memlen;
Aaron Durbin539aed02015-10-23 17:42:32 -0500698 new_buffer = calloc(1, new_buff_sz);
699
700 if (decompress(orig_buffer + sizeof(struct cbfs_stage),
701 (int)(buffer_size(buff) - sizeof(struct cbfs_stage)),
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500702 new_buffer, (int)new_buff_sz, &new_buff_sz)) {
Aaron Durbin539aed02015-10-23 17:42:32 -0500703 ERROR("Couldn't decompress stage.\n");
704 free(new_buffer);
705 return -1;
706 }
707
708 /* Include correct size for full stage info. */
Aaron Durbin539aed02015-10-23 17:42:32 -0500709 buffer_init(buff, buff->name, new_buffer, new_buff_sz);
Aaron Durbin539aed02015-10-23 17:42:32 -0500710
711 /* True decompressed size is just the data size -- no metadata. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500712 stage->len = new_buff_sz;
Aaron Durbin539aed02015-10-23 17:42:32 -0500713 /* Stage is not compressed. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500714 stage->compression = CBFS_COMPRESS_NONE;
Aaron Durbin539aed02015-10-23 17:42:32 -0500715
716 free(orig_buffer);
717
718 return 0;
719}
720
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500721static int init_elf_from_arch(Elf64_Ehdr *ehdr, uint32_t cbfs_arch)
722{
723 int endian;
724 int nbits;
725 int machine;
726
727 switch (cbfs_arch) {
728 case CBFS_ARCHITECTURE_X86:
729 endian = ELFDATA2LSB;
730 nbits = ELFCLASS32;
731 machine = EM_386;
732 break;
733 case CBFS_ARCHITECTURE_ARM:
734 endian = ELFDATA2LSB;
735 nbits = ELFCLASS32;
736 machine = EM_ARM;
737 break;
738 case CBFS_ARCHITECTURE_AARCH64:
739 endian = ELFDATA2LSB;
740 nbits = ELFCLASS64;
741 machine = EM_AARCH64;
742 break;
743 case CBFS_ARCHITECTURE_MIPS:
744 endian = ELFDATA2LSB;
745 nbits = ELFCLASS32;
746 machine = EM_MIPS;
747 break;
748 case CBFS_ARCHITECTURE_RISCV:
749 endian = ELFDATA2LSB;
750 nbits = ELFCLASS32;
751 machine = EM_RISCV;
752 break;
753 default:
754 ERROR("Unsupported arch: %x\n", cbfs_arch);
755 return -1;
756 }
757
758 elf_init_eheader(ehdr, machine, nbits, endian);
759 return 0;
760}
761
762static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch)
763{
764 Elf64_Ehdr ehdr;
765 Elf64_Shdr shdr;
766 struct cbfs_stage stage;
767 struct elf_writer *ew;
768 struct buffer elf_out;
769 size_t empty_sz;
Aaron Durbin694fd132015-10-28 11:39:34 -0500770 int rmod_ret;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500771
772 if (cbfs_stage_decompress(&stage, buff)) {
773 ERROR("Failed to decompress stage.\n");
774 return -1;
775 }
776
777 if (init_elf_from_arch(&ehdr, arch))
778 return -1;
779
780 ehdr.e_entry = stage.entry;
781
Aaron Durbin694fd132015-10-28 11:39:34 -0500782 /* Attempt rmodule translation first. */
783 rmod_ret = rmodule_stage_to_elf(&ehdr, buff);
784
785 if (rmod_ret < 0) {
786 ERROR("rmodule parsing failed\n");
787 return -1;
788 } else if (rmod_ret == 0)
789 return 0;
790
791 /* Rmodule couldn't do anything with the data. Continue on with SELF. */
792
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500793 ew = elf_writer_init(&ehdr);
794 if (ew == NULL) {
795 ERROR("Unable to init ELF writer.\n");
796 return -1;
797 }
798
799 memset(&shdr, 0, sizeof(shdr));
800 shdr.sh_type = SHT_PROGBITS;
801 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
802 shdr.sh_addr = stage.load;
803 shdr.sh_size = stage.len;
804 empty_sz = stage.memlen - stage.len;
805
806 if (elf_writer_add_section(ew, &shdr, buff, ".program")) {
807 ERROR("Unable to add ELF section: .program\n");
808 elf_writer_destroy(ew);
809 return -1;
810 }
811
812 if (empty_sz != 0) {
813 struct buffer b;
814
815 buffer_init(&b, NULL, NULL, 0);
816 memset(&shdr, 0, sizeof(shdr));
817 shdr.sh_type = SHT_NOBITS;
818 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
819 shdr.sh_addr = stage.load + stage.len;
820 shdr.sh_size = empty_sz;
821 if (elf_writer_add_section(ew, &shdr, &b, ".empty")) {
822 ERROR("Unable to add ELF section: .empty\n");
823 elf_writer_destroy(ew);
824 return -1;
825 }
826 }
827
828 if (elf_writer_serialize(ew, &elf_out)) {
829 ERROR("Unable to create ELF file from stage.\n");
830 elf_writer_destroy(ew);
831 return -1;
832 }
833
834 /* Flip buffer with the created ELF one. */
835 buffer_delete(buff);
836 *buff = elf_out;
837
838 elf_writer_destroy(ew);
839
840 return 0;
841}
842
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800843int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500844 const char *filename, uint32_t arch)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700845{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800846 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
847 struct buffer buffer;
848 if (!entry) {
849 ERROR("File not found: %s\n", entry_name);
850 return -1;
851 }
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200852
853 unsigned int decompressed_size = 0;
854 unsigned int compression = cbfs_file_get_compression_info(entry,
855 &decompressed_size);
856
857 decomp_func_ptr decompress = decompression_function(compression);
858 if (!decompress) {
859 ERROR("looking up decompression routine failed\n");
860 return -1;
861 }
862
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800863 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
864 entry_name, cbfs_get_entry_addr(image, entry),
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200865 get_cbfs_entry_type_name(ntohl(entry->type)), decompressed_size);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800866
Patrick Georgi011b0b32015-08-26 12:16:54 +0200867 if (ntohl(entry->type) == CBFS_COMPONENT_PAYLOAD) {
868 WARN("Payloads are extracted in SELF format.\n");
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800869 }
870
Aaron Durbin539aed02015-10-23 17:42:32 -0500871 buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0);
872
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200873 buffer.data = malloc(decompressed_size);
874 buffer.size = decompressed_size;
875 if (decompress(CBFS_SUBHEADER(entry), ntohl(entry->len),
Aaron Durbin5213c532015-10-23 17:38:40 -0500876 buffer.data, buffer.size, NULL)) {
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200877 ERROR("decompression failed for %s\n", entry_name);
Aaron Durbin539aed02015-10-23 17:42:32 -0500878 buffer_delete(&buffer);
Patrick Georgi23aeaff2015-08-26 13:01:10 +0200879 return -1;
880 }
Aaron Durbin539aed02015-10-23 17:42:32 -0500881
882 /*
883 * The stage metadata is never compressed proper for cbfs_stage
884 * files. The contents of the stage data can be though. Therefore
885 * one has to do a second pass for stages to potentially decompress
886 * the stage data to make it more meaningful.
887 */
888 if (ntohl(entry->type) == CBFS_COMPONENT_STAGE) {
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500889 if (cbfs_stage_make_elf(&buffer, arch)) {
Aaron Durbin539aed02015-10-23 17:42:32 -0500890 buffer_delete(&buffer);
891 return -1;
892 }
893 }
894
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800895 if (buffer_write_file(&buffer, filename) != 0) {
896 ERROR("Failed to write %s into %s.\n",
897 entry_name, filename);
Aaron Durbin539aed02015-10-23 17:42:32 -0500898 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800899 return -1;
900 }
Aaron Durbin539aed02015-10-23 17:42:32 -0500901
902 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800903 INFO("Successfully dumped the file to: %s\n", filename);
904 return 0;
905}
906
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700907int cbfs_remove_entry(struct cbfs_image *image, const char *name)
908{
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +0200909 struct cbfs_file *entry;
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800910 entry = cbfs_get_entry(image, name);
911 if (!entry) {
912 ERROR("CBFS file %s not found.\n", name);
913 return -1;
914 }
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800915 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
Patrick Georgic569b8b2015-07-15 16:42:38 +0200916 entry->filename, cbfs_get_entry_addr(image, entry));
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800917 entry->type = htonl(CBFS_COMPONENT_DELETED);
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +0200918 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800919 return 0;
920}
921
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700922int cbfs_print_header_info(struct cbfs_image *image)
923{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800924 char *name = strdup(image->buffer.name);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700925 assert(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800926 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800927 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800928 basename(name),
929 image->buffer.size / 1024,
Sol Boucher3e060ed2015-05-05 15:40:15 -0700930 image->header.bootblocksize,
931 image->header.romsize,
932 image->header.offset,
933 image->header.align,
934 arch_to_string(image->header.architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800935 free(name);
936 return 0;
937}
938
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700939static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
940{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800941 fprintf(fp,
942 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
943 "length: %d/%d\n",
944 lookup_name_by_type(types_cbfs_compression,
945 stage->compression, "(unknown)"),
946 stage->entry,
947 stage->load,
948 stage->len,
949 stage->memlen);
950 return 0;
951}
952
Hung-Te Lin0780d672014-05-16 10:14:05 +0800953static int cbfs_print_decoded_payload_segment_info(
954 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800955{
Hung-Te Lin0780d672014-05-16 10:14:05 +0800956 /* The input (seg) must be already decoded by
957 * cbfs_decode_payload_segment.
958 */
959 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800960 case PAYLOAD_SEGMENT_CODE:
961 case PAYLOAD_SEGMENT_DATA:
962 fprintf(fp, " %s (%s compression, offset: 0x%x, "
963 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800964 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800965 "code " : "data"),
966 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800967 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800968 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800969 seg->offset, seg->load_addr, seg->len,
970 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800971 break;
972
973 case PAYLOAD_SEGMENT_ENTRY:
974 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800975 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800976 break;
977
978 case PAYLOAD_SEGMENT_BSS:
979 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
980 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800981 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800982 break;
983
984 case PAYLOAD_SEGMENT_PARAMS:
985 fprintf(fp, " parameters\n");
986 break;
987
988 default:
989 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
990 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800991 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800992 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800993 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800994 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800995 seg->offset, seg->load_addr, seg->len,
996 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800997 break;
998 }
999 return 0;
1000}
1001
1002int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001003 void *arg)
1004{
Patrick Georgic569b8b2015-07-15 16:42:38 +02001005 const char *name = entry->filename;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001006 struct cbfs_payload_segment *payload;
1007 FILE *fp = (FILE *)arg;
1008
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001009 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001010 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1011 cbfs_get_entry_addr(image, entry));
1012 return -1;
1013 }
1014 if (!fp)
1015 fp = stdout;
1016
Patrick Georgic82725c2015-08-26 12:13:03 +02001017 unsigned int decompressed_size = 0;
Patrick Georgia71c83f2015-08-26 12:23:26 +02001018 unsigned int compression = cbfs_file_get_compression_info(entry,
1019 &decompressed_size);
Patrick Georgic82725c2015-08-26 12:13:03 +02001020
1021 if (compression == CBFS_COMPRESS_NONE) {
1022 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
1023 *name ? name : "(empty)",
1024 cbfs_get_entry_addr(image, entry),
1025 get_cbfs_entry_type_name(ntohl(entry->type)),
1026 ntohl(entry->len));
1027 } else {
1028 fprintf(fp, "%-30s 0x%-8x %-12s %d (%d after %s decompression)\n",
1029 *name ? name : "(empty)",
1030 cbfs_get_entry_addr(image, entry),
1031 get_cbfs_entry_type_name(ntohl(entry->type)),
1032 ntohl(entry->len),
1033 decompressed_size,
1034 lookup_name_by_type(types_cbfs_compression,
1035 compression, "(unknown)")
1036 );
1037 }
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001038
Patrick Georgi89f20342015-10-01 15:54:04 +02001039 struct cbfs_file_attr_hash *hash = NULL;
1040 while ((hash = cbfs_file_get_next_hash(entry, hash)) != NULL) {
1041 unsigned int hash_type = ntohl(hash->hash_type);
1042 if (hash_type > CBFS_NUM_SUPPORTED_HASHES) {
1043 fprintf(fp, "invalid hash type %d\n", hash_type);
1044 break;
1045 }
1046 size_t hash_len = widths_cbfs_hash[hash_type];
1047 char *hash_str = bintohex(hash->hash_data, hash_len);
1048 uint8_t local_hash[hash_len];
1049 if (vb2_digest_buffer(CBFS_SUBHEADER(entry),
1050 ntohl(entry->len), hash_type, local_hash,
1051 hash_len) != VB2_SUCCESS) {
1052 fprintf(fp, "failed to hash '%s'\n", name);
1053 break;
1054 }
1055 int valid = memcmp(local_hash, hash->hash_data, hash_len) == 0;
1056 const char *valid_str = valid ? "valid" : "invalid";
1057
1058 fprintf(fp, " hash %s:%s %s\n",
1059 get_hash_attr_name(hash_type),
1060 hash_str, valid_str);
1061 free(hash_str);
1062 }
1063
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001064 if (!verbose)
1065 return 0;
1066
1067 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
1068 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
1069 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
1070 ntohl(entry->len));
1071
1072 /* note the components of the subheader may be in host order ... */
1073 switch (ntohl(entry->type)) {
1074 case CBFS_COMPONENT_STAGE:
1075 cbfs_print_stage_info((struct cbfs_stage *)
1076 CBFS_SUBHEADER(entry), fp);
1077 break;
1078
1079 case CBFS_COMPONENT_PAYLOAD:
Paul Menzel831bbe82015-08-08 20:20:57 +02001080 payload = (struct cbfs_payload_segment *)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001081 CBFS_SUBHEADER(entry);
1082 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +08001083 struct cbfs_payload_segment seg;
1084 cbfs_decode_payload_segment(&seg, payload);
1085 cbfs_print_decoded_payload_segment_info(
1086 &seg, fp);
1087 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001088 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +08001089 else
Aaron Durbinca630272014-08-05 10:48:20 -05001090 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001091 }
1092 break;
1093 default:
1094 break;
1095 }
1096 return 0;
1097}
1098
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001099int cbfs_print_directory(struct cbfs_image *image)
1100{
Sol Boucher67a0a862015-03-18 12:36:27 -07001101 if (cbfs_is_legacy_cbfs(image))
1102 cbfs_print_header_info(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001103 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
1104 cbfs_walk(image, cbfs_print_entry_info, NULL);
1105 return 0;
1106}
1107
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001108int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -08001109 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001110{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001111 struct cbfs_file *next;
Aaron Durbin285111f2015-08-08 20:25:17 +02001112 uint8_t *name;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001113 uint32_t type, addr, last_addr;
1114
1115 type = ntohl(entry->type);
1116 if (type == CBFS_COMPONENT_DELETED) {
1117 // Ready to be recycled.
1118 type = CBFS_COMPONENT_NULL;
1119 entry->type = htonl(type);
Aaron Durbin285111f2015-08-08 20:25:17 +02001120 // Place NUL byte as first byte of name to be viewed as "empty".
1121 name = (void *)&entry[1];
1122 *name = '\0';
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001123 }
1124 if (type != CBFS_COMPONENT_NULL)
1125 return 0;
1126
1127 next = cbfs_find_next_entry(image, entry);
1128
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001129 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001130 type = ntohl(next->type);
1131 if (type == CBFS_COMPONENT_DELETED) {
1132 type = CBFS_COMPONENT_NULL;
1133 next->type = htonl(type);
1134 }
1135 if (type != CBFS_COMPONENT_NULL)
1136 return 0;
1137
1138 addr = cbfs_get_entry_addr(image, entry);
1139 last_addr = cbfs_get_entry_addr(
1140 image, cbfs_find_next_entry(image, next));
1141
1142 // Now, we find two deleted/empty entries; try to merge now.
1143 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
1144 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
1145 cbfs_get_entry_addr(image, next), ntohl(next->len));
Patrick Georgiedf25d92015-08-12 09:12:06 +02001146 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL,
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001147 (last_addr - addr -
1148 cbfs_calculate_file_header_size("")),
1149 "");
1150 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
1151 next = cbfs_find_next_entry(image, entry);
1152 }
1153 return 0;
1154}
1155
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001156int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001157 void *arg)
1158{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001159 int count = 0;
1160 struct cbfs_file *entry;
1161 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001162 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001163 entry = cbfs_find_next_entry(image, entry)) {
1164 count ++;
1165 if (callback(image, entry, arg) != 0)
1166 break;
1167 }
1168 return count;
1169}
1170
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001171static int cbfs_header_valid(struct cbfs_header *header)
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001172{
1173 if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
1174 ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
1175 (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001176 (ntohl(header->offset) < ntohl(header->romsize)))
1177 return 1;
1178 return 0;
1179}
1180
1181struct cbfs_header *cbfs_find_header(char *data, size_t size,
1182 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001183{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001184 size_t offset;
1185 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001186 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001187 struct cbfs_header *header, *result = NULL;
1188
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001189 if (forced_offset < (size - sizeof(struct cbfs_header))) {
1190 /* Check if the forced header is valid. */
1191 header = (struct cbfs_header *)(data + forced_offset);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001192 if (cbfs_header_valid(header))
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001193 return header;
1194 return NULL;
1195 }
1196
Julius Wernerefcee762014-11-10 13:14:24 -08001197 // Try finding relative offset of master header at end of file first.
1198 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
1199 offset = size + rel_offset;
1200 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
1201 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001202
Hung-Te Lineab2c812013-01-29 01:56:17 +08001203 if (offset >= size - sizeof(*header) ||
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001204 !cbfs_header_valid((struct cbfs_header *)(data + offset))) {
Julius Wernerefcee762014-11-10 13:14:24 -08001205 // Some use cases append non-CBFS data to the end of the ROM.
1206 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +08001207 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001208 }
Hung-Te Lineab2c812013-01-29 01:56:17 +08001209
1210 for (; offset + sizeof(*header) < size; offset++) {
1211 header = (struct cbfs_header *)(data + offset);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001212 if (!cbfs_header_valid(header))
Hung-Te Lineab2c812013-01-29 01:56:17 +08001213 continue;
Julius Wernerefcee762014-11-10 13:14:24 -08001214 if (!found++)
1215 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001216 }
Julius Wernerefcee762014-11-10 13:14:24 -08001217 if (found > 1)
1218 // Top-aligned images usually have a working relative offset
1219 // field, so this is more likely to happen on bottom-aligned
1220 // ones (where the first header is the "outermost" one)
1221 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +08001222 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001223 return result;
1224}
1225
1226
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001227struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
1228{
Sol Boucher3e060ed2015-05-05 15:40:15 -07001229 assert(image);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001230 if (image->has_header)
1231 /* header.offset is relative to start of flash, not
1232 * start of region, so use it with the full image.
1233 */
1234 return (struct cbfs_file *)
1235 (buffer_get_original_backing(&image->buffer) +
1236 image->header.offset);
1237 else
1238 return (struct cbfs_file *)buffer_get(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001239}
1240
1241struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001242 struct cbfs_file *entry)
1243{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001244 uint32_t addr = cbfs_get_entry_addr(image, entry);
Sol Boucher67a0a862015-03-18 12:36:27 -07001245 int align = image->has_header ? image->header.align :
1246 CBFS_ENTRY_ALIGNMENT;
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001247 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001248 addr += ntohl(entry->offset) + ntohl(entry->len);
1249 addr = align_up(addr, align);
1250 return (struct cbfs_file *)(image->buffer.data + addr);
1251}
1252
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001253uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
1254{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001255 assert(image && image->buffer.data && entry);
1256 return (int32_t)((char *)entry - image->buffer.data);
1257}
1258
Sol Boucher67a0a862015-03-18 12:36:27 -07001259int cbfs_is_valid_cbfs(struct cbfs_image *image)
1260{
1261 return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC,
1262 strlen(CBFS_FILE_MAGIC));
1263}
1264
1265int cbfs_is_legacy_cbfs(struct cbfs_image *image)
1266{
1267 return image->has_header;
1268}
1269
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001270int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
1271{
Sol Bouchere3260a02015-03-25 13:40:08 -07001272 uint32_t offset = cbfs_get_entry_addr(image, entry);
1273
1274 if (offset >= image->buffer.size)
1275 return 0;
1276
1277 struct buffer entry_data;
1278 buffer_clone(&entry_data, &image->buffer);
1279 buffer_seek(&entry_data, offset);
1280 return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
Sol Boucher67a0a862015-03-18 12:36:27 -07001281 strlen(CBFS_FILE_MAGIC));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001282}
1283
Patrick Georgi57edf162015-08-12 09:20:11 +02001284struct cbfs_file *cbfs_create_file_header(int type,
Vadim Bendebury45e59972014-12-23 15:59:57 -08001285 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001286{
Patrick Georgi2c615062015-07-15 20:49:00 +02001287 struct cbfs_file *entry = malloc(MAX_CBFS_FILE_HEADER_BUFFER);
1288 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, MAX_CBFS_FILE_HEADER_BUFFER);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001289 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
Patrick Georgiedf25d92015-08-12 09:12:06 +02001290 entry->type = htonl(type);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001291 entry->len = htonl(len);
Patrick Georgi0d618af2015-07-15 18:28:23 +02001292 entry->attributes_offset = 0;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001293 entry->offset = htonl(cbfs_calculate_file_header_size(name));
Patrick Georgic569b8b2015-07-15 16:42:38 +02001294 memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry));
1295 strcpy(entry->filename, name);
Patrick Georgi57edf162015-08-12 09:20:11 +02001296 return entry;
1297}
1298
1299int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
1300 size_t len, const char *name)
1301{
1302 struct cbfs_file *tmp = cbfs_create_file_header(type, len, name);
1303 memcpy(entry, tmp, ntohl(tmp->offset));
1304 free(tmp);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001305 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1306 return 0;
1307}
1308
Patrick Georgi2c615062015-07-15 20:49:00 +02001309struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file)
1310{
1311 /* attributes_offset should be 0 when there is no attribute, but all
1312 * values that point into the cbfs_file header are invalid, too. */
1313 if (ntohl(file->attributes_offset) <= sizeof(*file))
1314 return NULL;
1315
1316 /* There needs to be enough space for the file header and one
1317 * attribute header for this to make sense. */
1318 if (ntohl(file->offset) <=
1319 sizeof(*file) + sizeof(struct cbfs_file_attribute))
1320 return NULL;
1321
1322 return (struct cbfs_file_attribute *)
1323 (((uint8_t *)file) + ntohl(file->attributes_offset));
1324}
1325
1326struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
1327 struct cbfs_file_attribute *attr)
1328{
1329 /* ex falso sequitur quodlibet */
1330 if (attr == NULL)
1331 return NULL;
1332
1333 /* Is there enough space for another attribute? */
1334 if ((uint8_t *)attr + ntohl(attr->len) +
1335 sizeof(struct cbfs_file_attribute) >=
1336 (uint8_t *)file + ntohl(file->offset))
1337 return NULL;
1338
1339 struct cbfs_file_attribute *next = (struct cbfs_file_attribute *)
1340 (((uint8_t *)attr) + ntohl(attr->len));
1341 /* If any, "unused" attributes must come last. */
1342 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED)
1343 return NULL;
1344 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
1345 return NULL;
1346
1347 return next;
1348}
1349
1350struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
1351 uint32_t tag,
1352 uint32_t size)
1353{
1354 struct cbfs_file_attribute *attr, *next;
1355 next = cbfs_file_first_attr(header);
1356 do {
1357 attr = next;
1358 next = cbfs_file_next_attr(header, attr);
1359 } while (next != NULL);
1360 uint32_t header_size = ntohl(header->offset) + size;
1361 if (header_size > MAX_CBFS_FILE_HEADER_BUFFER) {
1362 DEBUG("exceeding allocated space for cbfs_file headers");
1363 return NULL;
1364 }
1365 /* attr points to the last valid attribute now.
1366 * If NULL, we have to create the first one. */
1367 if (attr == NULL) {
1368 /* New attributes start where the header ends.
1369 * header->offset is later set to accomodate the
1370 * additional structure.
1371 * No endianess translation necessary here, because both
1372 * fields are encoded the same way. */
1373 header->attributes_offset = header->offset;
1374 attr = (struct cbfs_file_attribute *)
1375 (((uint8_t *)header) +
1376 ntohl(header->attributes_offset));
1377 } else {
1378 attr = (struct cbfs_file_attribute *)
1379 (((uint8_t *)attr) +
1380 ntohl(attr->len));
1381 }
1382 header->offset = htonl(header_size);
1383 memset(attr, CBFS_CONTENT_DEFAULT_VALUE, size);
1384 attr->tag = htonl(tag);
1385 attr->len = htonl(size);
1386 return attr;
1387}
1388
Patrick Georgi89f20342015-10-01 15:54:04 +02001389int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer,
1390 enum vb2_hash_algorithm hash_type)
1391{
zbao37450ff2015-11-05 14:35:57 +08001392 uint32_t hash_index = hash_type;
1393
1394 if (hash_index >= CBFS_NUM_SUPPORTED_HASHES)
Patrick Georgi89f20342015-10-01 15:54:04 +02001395 return -1;
1396
1397 unsigned hash_size = widths_cbfs_hash[hash_type];
1398 if (hash_size == 0)
1399 return -1;
1400
1401 struct cbfs_file_attr_hash *attrs =
1402 (struct cbfs_file_attr_hash *)cbfs_add_file_attr(header,
1403 CBFS_FILE_ATTR_TAG_HASH,
1404 sizeof(struct cbfs_file_attr_hash) + hash_size);
1405
1406 if (attrs == NULL)
1407 return -1;
1408
1409 attrs->hash_type = htonl(hash_type);
1410 if (vb2_digest_buffer(buffer_get(buffer), buffer_size(buffer),
1411 hash_type, attrs->hash_data, hash_size) != VB2_SUCCESS)
1412 return -1;
1413
1414 return 0;
1415}
1416
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001417/* Finds a place to hold whole data in same memory page. */
1418static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1419{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001420 if (!page)
1421 return 1;
1422 return (start / page) == (start + size - 1) / page;
1423}
1424
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001425/* Tests if data can fit in a range by given offset:
Aaron Durbind7339412015-09-15 12:50:14 -05001426 * start ->| metadata_size | offset (+ size) |<- end
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001427 */
Aaron Durbind7339412015-09-15 12:50:14 -05001428static int is_in_range(size_t start, size_t end, size_t metadata_size,
1429 size_t offset, size_t size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001430{
Aaron Durbind7339412015-09-15 12:50:14 -05001431 return (offset >= start + metadata_size && offset + size <= end);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001432}
1433
Aaron Durbind7339412015-09-15 12:50:14 -05001434int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size,
1435 size_t page_size, size_t align, size_t metadata_size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001436{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001437 struct cbfs_file *entry;
1438 size_t need_len;
Aaron Durbind7339412015-09-15 12:50:14 -05001439 size_t addr, addr_next, addr2, addr3, offset;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001440
1441 /* Default values: allow fitting anywhere in ROM. */
1442 if (!page_size)
Sol Boucher67a0a862015-03-18 12:36:27 -07001443 page_size = image->has_header ? image->header.romsize :
1444 image->buffer.size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001445 if (!align)
1446 align = 1;
1447
1448 if (size > page_size)
Aaron Durbind7339412015-09-15 12:50:14 -05001449 ERROR("Input file size (%zd) greater than page size (%zd).\n",
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001450 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001451
Aaron Durbind7339412015-09-15 12:50:14 -05001452 size_t image_align = image->has_header ? image->header.align :
Sol Boucher67a0a862015-03-18 12:36:27 -07001453 CBFS_ENTRY_ALIGNMENT;
1454 if (page_size % image_align)
Aaron Durbind7339412015-09-15 12:50:14 -05001455 WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001456 __func__, page_size, image_align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001457
Aaron Durbind7339412015-09-15 12:50:14 -05001458 need_len = metadata_size + size;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001459
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001460 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001461 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
1462
1463 /* Three cases of content location on memory page:
1464 * case 1.
1465 * | PAGE 1 | PAGE 2 |
1466 * | <header><content>| Fit. Return start of content.
1467 *
1468 * case 2.
1469 * | PAGE 1 | PAGE 2 |
1470 * | <header><content> | Fits when we shift content to align
1471 * shift-> | <header>|<content> | at starting of PAGE 2.
1472 *
1473 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001474 * | PAGE 1 | PAGE 2 | PAGE 3 |
1475 * | <header>< content > | Can't fit. If we shift content to
1476 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1477 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001478 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001479 * The returned address can be then used as "base-address" (-b) in add-*
1480 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1481 * For stage targets, the address is also used to re-link stage before
1482 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001483 */
1484 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001485 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001486 entry = cbfs_find_next_entry(image, entry)) {
1487
1488 uint32_t type = ntohl(entry->type);
1489 if (type != CBFS_COMPONENT_NULL)
1490 continue;
1491
1492 addr = cbfs_get_entry_addr(image, entry);
1493 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
1494 image, entry));
1495 if (addr_next - addr < need_len)
1496 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001497
Aaron Durbind7339412015-09-15 12:50:14 -05001498 offset = align_up(addr + metadata_size, align);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001499 if (is_in_same_page(offset, size, page_size) &&
Aaron Durbind7339412015-09-15 12:50:14 -05001500 is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001501 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001502 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001503 }
1504
1505 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001506 offset = align_up(addr2, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001507 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001508 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001509 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001510 }
1511
Aaron Durbind7339412015-09-15 12:50:14 -05001512 /* Assume page_size >= metadata_size so adding one page will
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001513 * definitely provide the space for header. */
Aaron Durbind7339412015-09-15 12:50:14 -05001514 assert(page_size >= metadata_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001515 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001516 offset = align_up(addr3, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001517 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001518 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001519 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001520 }
1521 }
1522 return -1;
1523}