blob: 5f30877df22b0b6b58278279202552e0100a236e [file] [log] [blame]
Patrick Georgiea063cb2020-05-08 19:28:13 +02001/* CBFS Image Manipulation */
Patrick Georgi7333a112020-05-08 20:48:04 +02002/* SPDX-License-Identifier: GPL-2.0-only */
Hung-Te Lineab2c812013-01-29 01:56:17 +08003
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08004#include <inttypes.h>
5#include <libgen.h>
Patrick Georgicccc9d42015-04-28 13:09:36 +02006#include <stddef.h>
Hung-Te Lineab2c812013-01-29 01:56:17 +08007#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
Sol Boucher0e539312015-03-05 15:38:03 -080010#include <strings.h>
Antonello Dettorifda691e2016-06-09 12:35:36 +020011#include <commonlib/endian.h>
Joel Kitching2eb89c82019-04-25 17:45:12 +080012#include <vb2_sha.h>
Hung-Te Lineab2c812013-01-29 01:56:17 +080013
14#include "common.h"
15#include "cbfs_image.h"
Aaron Durbin5a1e85c2015-10-27 21:02:30 -050016#include "elfparsing.h"
Aaron Durbin694fd132015-10-28 11:39:34 -050017#include "rmodule.h"
Hung-Te Lineab2c812013-01-29 01:56:17 +080018
Sol Boucher636cc852015-04-03 09:13:04 -070019/* Even though the file-adding functions---cbfs_add_entry() and
20 * cbfs_add_entry_at()---perform their sizing checks against the beginning of
21 * the subsequent section rather than a stable recorded value such as an empty
22 * file header's len field, it's possible to prove two interesting properties
23 * about their behavior:
24 * - Placing a new file within an empty entry located below an existing file
25 * entry will never leave an aligned flash address containing neither the
26 * beginning of a file header nor part of a file.
27 * - Placing a new file in an empty entry at the very end of the image such
28 * that it fits, but leaves no room for a final header, is guaranteed not to
29 * change the total amount of space for entries, even if that new file is
30 * later removed from the CBFS.
31 * These properties are somewhat nonobvious from the implementation, so the
32 * reader is encouraged to blame this comment and examine the full proofs
33 * in the commit message before making significant changes that would risk
34 * removing said guarantees.
35 */
36
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080037static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070038 const char *default_value)
39{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080040 int i;
41 for (i = 0; desc[i].name; i++)
42 if (desc[i].type == type)
43 return desc[i].name;
44 return default_value;
45}
46
Sol Boucherec424862015-05-07 21:00:05 -070047static int lookup_type_by_name(const struct typedesc_t *desc, const char *name)
48{
49 int i;
50 for (i = 0; desc[i].name && strcasecmp(name, desc[i].name); ++i);
51 return desc[i].name ? (int)desc[i].type : -1;
52}
53
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010054static const char *get_cbfs_entry_type_name(uint32_t type)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070055{
Patrick Georgidc37dab2015-09-09 16:46:00 +020056 return lookup_name_by_type(filetypes, type, "(unknown)");
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080057}
58
Sol Boucherec424862015-05-07 21:00:05 -070059int cbfs_parse_comp_algo(const char *name)
60{
61 return lookup_type_by_name(types_cbfs_compression, name);
62}
63
Hung-Te Linc03d9b02013-01-29 02:38:40 +080064/* CBFS image */
65
Patrick Georgi11ee08f2015-08-11 15:10:02 +020066size_t cbfs_calculate_file_header_size(const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070067{
Hung-Te Linc03d9b02013-01-29 02:38:40 +080068 return (sizeof(struct cbfs_file) +
Julius Werner5779ca72020-11-20 16:12:40 -080069 align_up(strlen(name) + 1, CBFS_ATTRIBUTE_ALIGN));
Hung-Te Linc03d9b02013-01-29 02:38:40 +080070}
71
Sol Boucher67a0a862015-03-18 12:36:27 -070072/* Only call on legacy CBFSes possessing a master header. */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -060073static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070074{
Sol Boucher67a0a862015-03-18 12:36:27 -070075 assert(image);
76 assert(cbfs_is_legacy_cbfs(image));
Hung-Te Lin49fcd752013-01-29 03:16:20 +080077 // A bug in old cbfstool may produce extra few bytes (by alignment) and
78 // cause cbfstool to overwrite things after free space -- which is
79 // usually CBFS header on x86. We need to workaround that.
Patrick Georgi343ea082016-02-10 18:07:52 +010080 // Except when we run across a file that contains the actual header,
81 // in which case this image is a safe, new-style
82 // `cbfstool add-master-header` based image.
Hung-Te Lin49fcd752013-01-29 03:16:20 +080083
84 struct cbfs_file *entry, *first = NULL, *last = NULL;
85 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +080086 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +080087 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgi343ea082016-02-10 18:07:52 +010088 /* Is the header guarded by a CBFS file entry? Then exit */
Alex Jamesb3398ba2022-01-08 01:29:29 -060089 if (((char *)entry) + be32toh(entry->offset) == hdr_loc)
Patrick Georgi343ea082016-02-10 18:07:52 +010090 return 0;
Hung-Te Lin49fcd752013-01-29 03:16:20 +080091 last = entry;
92 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -060093 if ((char *)first < (char *)hdr_loc &&
94 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +080095 WARN("CBFS image was created with old cbfstool with size bug. "
96 "Fixing size in last entry...\n");
Alex James02001a382021-12-19 16:41:59 -060097 last->len = htobe32(be32toh(last->len) - image->header.align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +080098 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
99 cbfs_get_entry_addr(image, entry),
100 cbfs_get_entry_addr(image,
101 cbfs_find_next_entry(image, last)));
102 }
103 return 0;
104}
105
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800106void cbfs_put_header(void *dest, const struct cbfs_header *header)
107{
108 struct buffer outheader;
109
110 outheader.data = dest;
111 outheader.size = 0;
112
113 xdr_be.put32(&outheader, header->magic);
114 xdr_be.put32(&outheader, header->version);
115 xdr_be.put32(&outheader, header->romsize);
116 xdr_be.put32(&outheader, header->bootblocksize);
117 xdr_be.put32(&outheader, header->align);
118 xdr_be.put32(&outheader, header->offset);
119 xdr_be.put32(&outheader, header->architecture);
120}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600121
Hung-Te Lin0780d672014-05-16 10:14:05 +0800122static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
123 struct cbfs_payload_segment *input)
124{
125 struct buffer seg = {
126 .data = (void *)input,
127 .size = sizeof(*input),
128 };
129 output->type = xdr_be.get32(&seg);
130 output->compression = xdr_be.get32(&seg);
131 output->offset = xdr_be.get32(&seg);
132 output->load_addr = xdr_be.get64(&seg);
133 output->len = xdr_be.get32(&seg);
134 output->mem_len = xdr_be.get32(&seg);
135 assert(seg.size == 0);
136}
137
Patrick Georgia71c83f2015-08-26 12:23:26 +0200138static int cbfs_file_get_compression_info(struct cbfs_file *entry,
139 uint32_t *decompressed_size)
140{
141 unsigned int compression = CBFS_COMPRESS_NONE;
Patrick Georgia2ce7102016-12-14 16:08:52 +0100142 if (decompressed_size)
Alex James02001a382021-12-19 16:41:59 -0600143 *decompressed_size = be32toh(entry->len);
Patrick Georgia71c83f2015-08-26 12:23:26 +0200144 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
145 attr != NULL;
146 attr = cbfs_file_next_attr(entry, attr)) {
Alex James02001a382021-12-19 16:41:59 -0600147 if (be32toh(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) {
Patrick Georgia71c83f2015-08-26 12:23:26 +0200148 struct cbfs_file_attr_compression *ac =
149 (struct cbfs_file_attr_compression *)attr;
Alex James02001a382021-12-19 16:41:59 -0600150 compression = be32toh(ac->compression);
Patrick Georgia71c83f2015-08-26 12:23:26 +0200151 if (decompressed_size)
152 *decompressed_size =
Alex James02001a382021-12-19 16:41:59 -0600153 be32toh(ac->decompressed_size);
Patrick Georgia71c83f2015-08-26 12:23:26 +0200154 }
155 }
156 return compression;
157}
158
Patrick Georgi89f20342015-10-01 15:54:04 +0200159static struct cbfs_file_attr_hash *cbfs_file_get_next_hash(
160 struct cbfs_file *entry, struct cbfs_file_attr_hash *cur)
161{
162 struct cbfs_file_attribute *attr = (struct cbfs_file_attribute *)cur;
163 if (attr == NULL) {
164 attr = cbfs_file_first_attr(entry);
165 if (attr == NULL)
166 return NULL;
Alex James02001a382021-12-19 16:41:59 -0600167 if (be32toh(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
Patrick Georgi89f20342015-10-01 15:54:04 +0200168 return (struct cbfs_file_attr_hash *)attr;
169 }
170 while ((attr = cbfs_file_next_attr(entry, attr)) != NULL) {
Alex James02001a382021-12-19 16:41:59 -0600171 if (be32toh(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
Patrick Georgi89f20342015-10-01 15:54:04 +0200172 return (struct cbfs_file_attr_hash *)attr;
173 };
174 return NULL;
175}
176
Sol Boucher0e539312015-03-05 15:38:03 -0800177void cbfs_get_header(struct cbfs_header *header, void *src)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600178{
179 struct buffer outheader;
180
Sol Boucher0e539312015-03-05 15:38:03 -0800181 outheader.data = src; /* We're not modifying the data */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600182 outheader.size = 0;
183
184 header->magic = xdr_be.get32(&outheader);
185 header->version = xdr_be.get32(&outheader);
186 header->romsize = xdr_be.get32(&outheader);
187 header->bootblocksize = xdr_be.get32(&outheader);
188 header->align = xdr_be.get32(&outheader);
189 header->offset = xdr_be.get32(&outheader);
190 header->architecture = xdr_be.get32(&outheader);
191}
192
Sol Boucher67a0a862015-03-18 12:36:27 -0700193int cbfs_image_create(struct cbfs_image *image, size_t entries_size)
194{
195 assert(image);
196 assert(image->buffer.data);
197
198 size_t empty_header_len = cbfs_calculate_file_header_size("");
199 uint32_t entries_offset = 0;
Julius Wernerd4775652020-03-13 16:43:34 -0700200 uint32_t align = CBFS_ALIGNMENT;
Sol Boucher67a0a862015-03-18 12:36:27 -0700201 if (image->has_header) {
202 entries_offset = image->header.offset;
203
204 if (entries_offset > image->buffer.size) {
205 ERROR("CBFS file entries are located outside CBFS itself\n");
206 return -1;
207 }
208
209 align = image->header.align;
210 }
211
212 // This attribute must be given in order to prove that this module
213 // correctly preserves certain CBFS properties. See the block comment
214 // near the top of this file (and the associated commit message).
215 if (align < empty_header_len) {
216 ERROR("CBFS must be aligned to at least %zu bytes\n",
217 empty_header_len);
218 return -1;
219 }
220
221 if (entries_size > image->buffer.size - entries_offset) {
222 ERROR("CBFS doesn't have enough space to fit its file entries\n");
223 return -1;
224 }
225
226 if (empty_header_len > entries_size) {
227 ERROR("CBFS is too small to fit any header\n");
228 return -1;
229 }
230 struct cbfs_file *entry_header =
231 (struct cbfs_file *)(image->buffer.data + entries_offset);
232 // This alignment is necessary in order to prove that this module
233 // correctly preserves certain CBFS properties. See the block comment
234 // near the top of this file (and the associated commit message).
235 entries_size -= entries_size % align;
236
237 size_t capacity = entries_size - empty_header_len;
238 LOG("Created CBFS (capacity = %zu bytes)\n", capacity);
Julius Wernerd4775652020-03-13 16:43:34 -0700239 return cbfs_create_empty_entry(entry_header, CBFS_TYPE_NULL,
Patrick Georgiedf25d92015-08-12 09:12:06 +0200240 capacity, "");
Sol Boucher67a0a862015-03-18 12:36:27 -0700241}
242
243int cbfs_legacy_image_create(struct cbfs_image *image,
244 uint32_t architecture,
245 uint32_t align,
246 struct buffer *bootblock,
247 uint32_t bootblock_offset,
248 uint32_t header_offset,
249 uint32_t entries_offset)
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800250{
Sol Bouchere3260a02015-03-25 13:40:08 -0700251 assert(image);
252 assert(image->buffer.data);
253 assert(bootblock);
254
Julius Wernerefcee762014-11-10 13:14:24 -0800255 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800256 uint32_t cbfs_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600257 void *header_loc;
Sol Bouchere3260a02015-03-25 13:40:08 -0700258 size_t size = image->buffer.size;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800259
260 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
261 "header=0x%x+0x%zx, entries_offset=0x%x\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700262 bootblock_offset, bootblock->size, header_offset,
263 sizeof(image->header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800264
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800265 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
266 "header=0x%x, entries_offset=0x%x\n",
267 bootblock_offset, header_offset, entries_offset);
268
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800269 // Prepare bootblock
270 if (bootblock_offset + bootblock->size > size) {
271 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
272 bootblock_offset, bootblock->size, size);
273 return -1;
274 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800275 if (entries_offset > bootblock_offset &&
276 entries_offset < bootblock->size) {
277 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
278 bootblock_offset, bootblock->size, entries_offset);
279 return -1;
280 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800281 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
282 bootblock->size);
283
284 // Prepare header
Sol Boucher5bad3952015-05-05 20:35:26 -0700285 if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800286 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700287 header_offset, sizeof(image->header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800288 return -1;
289 }
Sol Boucher3e060ed2015-05-05 15:40:15 -0700290 image->header.magic = CBFS_HEADER_MAGIC;
291 image->header.version = CBFS_HEADER_VERSION;
292 image->header.romsize = size;
293 image->header.bootblocksize = bootblock->size;
294 image->header.align = align;
295 image->header.offset = entries_offset;
296 image->header.architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600297
298 header_loc = (image->buffer.data + header_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700299 cbfs_put_header(header_loc, &image->header);
Sol Boucher67a0a862015-03-18 12:36:27 -0700300 image->has_header = true;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800301
Julius Wernerefcee762014-11-10 13:14:24 -0800302 // The last 4 byte of the image contain the relative offset from the end
303 // of the image to the master header as a 32-bit signed integer. x86
304 // relies on this also being its (memory-mapped, top-aligned) absolute
305 // 32-bit address by virtue of how two's complement numbers work.
306 assert(size % sizeof(int32_t) == 0);
307 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
308 *rel_offset = header_offset - size;
309
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800310 // Prepare entries
311 if (align_up(entries_offset, align) != entries_offset) {
312 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
313 entries_offset, align);
314 return -1;
315 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800316 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800317 // e = min(bootblock, header, rel_offset) where e > entries_offset.
318 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800319 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
320 cbfs_len = bootblock_offset;
321 if (header_offset > entries_offset && header_offset < cbfs_len)
322 cbfs_len = header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700323
324 if (cbfs_image_create(image, cbfs_len - entries_offset))
325 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800326 return 0;
327}
328
Sol Bouchere3260a02015-03-25 13:40:08 -0700329int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
330 uint32_t offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700331{
Sol Bouchere3260a02015-03-25 13:40:08 -0700332 assert(out);
333 assert(in);
334 assert(in->data);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600335
Sol Bouchere3260a02015-03-25 13:40:08 -0700336 buffer_clone(&out->buffer, in);
Sol Boucher67a0a862015-03-18 12:36:27 -0700337 out->has_header = false;
338
Patrick Georgi2f953d32015-09-11 18:34:39 +0200339 if (cbfs_is_valid_cbfs(out)) {
340 return 0;
341 }
342
Sol Bouchere3260a02015-03-25 13:40:08 -0700343 void *header_loc = cbfs_find_header(in->data, in->size, offset);
344 if (header_loc) {
345 cbfs_get_header(&out->header, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700346 out->has_header = true;
Sol Bouchere3260a02015-03-25 13:40:08 -0700347 cbfs_fix_legacy_size(out, header_loc);
Patrick Georgi2f953d32015-09-11 18:34:39 +0200348 return 0;
Sol Boucher67a0a862015-03-18 12:36:27 -0700349 } else if (offset != ~0u) {
350 ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n");
351 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800352 }
Patrick Georgi2f953d32015-09-11 18:34:39 +0200353 ERROR("Selected image region is not a valid CBFS.\n");
354 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800355}
356
Patrick Georgi214e4af2015-11-20 19:22:50 +0100357int cbfs_copy_instance(struct cbfs_image *image, struct buffer *dst)
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800358{
Sol Boucher67a0a862015-03-18 12:36:27 -0700359 assert(image);
Sol Boucher67a0a862015-03-18 12:36:27 -0700360
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800361 struct cbfs_file *src_entry, *dst_entry;
Patrick Georgibd0bb232015-11-20 21:48:18 +0100362 size_t align;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800363 ssize_t last_entry_size;
364
Patrick Georgi214e4af2015-11-20 19:22:50 +0100365 size_t copy_end = buffer_size(dst);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800366
Julius Wernerd4775652020-03-13 16:43:34 -0700367 align = CBFS_ALIGNMENT;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800368
Patrick Georgibd0bb232015-11-20 21:48:18 +0100369 dst_entry = (struct cbfs_file *)buffer_get(dst);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800370
371 /* Copy non-empty files */
372 for (src_entry = cbfs_find_first_entry(image);
373 src_entry && cbfs_is_valid_entry(image, src_entry);
374 src_entry = cbfs_find_next_entry(image, src_entry)) {
375 size_t entry_size;
376
Alex James02001a382021-12-19 16:41:59 -0600377 if ((src_entry->type == htobe32(CBFS_TYPE_NULL)) ||
378 (src_entry->type == htobe32(CBFS_TYPE_CBFSHEADER)) ||
379 (src_entry->type == htobe32(CBFS_TYPE_DELETED)))
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800380 continue;
381
Alex James02001a382021-12-19 16:41:59 -0600382 entry_size = htobe32(src_entry->len) + htobe32(src_entry->offset);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800383 memcpy(dst_entry, src_entry, entry_size);
384 dst_entry = (struct cbfs_file *)(
385 (uintptr_t)dst_entry + align_up(entry_size, align));
386
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700387 if ((size_t)((uint8_t *)dst_entry - (uint8_t *)buffer_get(dst))
388 >= copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800389 ERROR("Ran out of room in copy region.\n");
390 return 1;
391 }
392 }
393
Patrick Georgibd0bb232015-11-20 21:48:18 +0100394 /* Last entry size is all the room above it, except for top 4 bytes
395 * which may be used by the master header pointer. This messes with
396 * the ability to stash something "top-aligned" into the region, but
397 * keeps things simpler. */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700398 last_entry_size = copy_end -
399 ((uint8_t *)dst_entry - (uint8_t *)buffer_get(dst)) -
400 cbfs_calculate_file_header_size("") - sizeof(int32_t);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800401
402 if (last_entry_size < 0)
403 WARN("No room to create the last entry!\n")
404 else
Julius Wernerd4775652020-03-13 16:43:34 -0700405 cbfs_create_empty_entry(dst_entry, CBFS_TYPE_NULL,
Patrick Georgiedf25d92015-08-12 09:12:06 +0200406 last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800407
408 return 0;
409}
410
Patrick Georgi5d982d72017-09-19 14:39:58 +0200411int cbfs_expand_to_region(struct buffer *region)
412{
413 if (buffer_get(region) == NULL)
414 return 1;
415
416 struct cbfs_image image;
417 memset(&image, 0, sizeof(image));
418 if (cbfs_image_from_buffer(&image, region, 0)) {
419 ERROR("reading CBFS failed!\n");
420 return 1;
421 }
422
423 uint32_t region_sz = buffer_size(region);
424
425 struct cbfs_file *entry;
426 for (entry = buffer_get(region);
427 cbfs_is_valid_entry(&image, entry);
428 entry = cbfs_find_next_entry(&image, entry)) {
429 /* just iterate through */
430 }
431
432 /* entry now points to the first aligned address after the last valid
433 * file header. That's either outside the image or exactly the place
434 * where we need to create a new file.
435 */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700436 int last_entry_size = region_sz -
437 ((uint8_t *)entry - (uint8_t *)buffer_get(region)) -
438 cbfs_calculate_file_header_size("") - sizeof(int32_t);
Patrick Georgi5d982d72017-09-19 14:39:58 +0200439
440 if (last_entry_size > 0) {
Julius Wernerd4775652020-03-13 16:43:34 -0700441 cbfs_create_empty_entry(entry, CBFS_TYPE_NULL,
Patrick Georgi5d982d72017-09-19 14:39:58 +0200442 last_entry_size, "");
443 /* If the last entry was an empty file, merge them. */
Julius Werner7066a1e2020-04-02 15:49:34 -0700444 cbfs_legacy_walk(&image, cbfs_merge_empty_entry, NULL);
Patrick Georgi5d982d72017-09-19 14:39:58 +0200445 }
446
447 return 0;
448}
449
Patrick Georgi12631a42017-09-20 11:59:18 +0200450int cbfs_truncate_space(struct buffer *region, uint32_t *size)
451{
452 if (buffer_get(region) == NULL)
453 return 1;
454
455 struct cbfs_image image;
456 memset(&image, 0, sizeof(image));
457 if (cbfs_image_from_buffer(&image, region, 0)) {
458 ERROR("reading CBFS failed!\n");
459 return 1;
460 }
461
462 struct cbfs_file *entry, *trailer;
463 for (trailer = entry = buffer_get(region);
464 cbfs_is_valid_entry(&image, entry);
465 trailer = entry,
466 entry = cbfs_find_next_entry(&image, entry)) {
467 /* just iterate through */
468 }
469
470 /* trailer now points to the last valid CBFS entry's header.
471 * If that file is empty, remove it and report its header's offset as
472 * maximum size.
473 */
474 if ((strlen(trailer->filename) != 0) &&
Alex James02001a382021-12-19 16:41:59 -0600475 (trailer->type != htobe32(CBFS_TYPE_NULL)) &&
476 (trailer->type != htobe32(CBFS_TYPE_DELETED))) {
Patrick Georgi12631a42017-09-20 11:59:18 +0200477 /* nothing to truncate. Return de-facto CBFS size in case it
478 * was already truncated. */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700479 *size = (uint8_t *)entry - (uint8_t *)buffer_get(region);
Patrick Georgi12631a42017-09-20 11:59:18 +0200480 return 0;
481 }
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700482 *size = (uint8_t *)trailer - (uint8_t *)buffer_get(region);
Patrick Georgi12631a42017-09-20 11:59:18 +0200483 memset(trailer, 0xff, buffer_size(region) - *size);
484
485 return 0;
486}
487
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600488static size_t cbfs_file_entry_metadata_size(const struct cbfs_file *f)
489{
Alex James02001a382021-12-19 16:41:59 -0600490 return be32toh(f->offset);
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600491}
492
493static size_t cbfs_file_entry_data_size(const struct cbfs_file *f)
494{
Alex James02001a382021-12-19 16:41:59 -0600495 return be32toh(f->len);
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600496}
497
498static size_t cbfs_file_entry_size(const struct cbfs_file *f)
499{
500 return cbfs_file_entry_metadata_size(f) + cbfs_file_entry_data_size(f);
501}
502
503int cbfs_compact_instance(struct cbfs_image *image)
504{
505 assert(image);
506
507 struct cbfs_file *prev;
508 struct cbfs_file *cur;
509
510 /* The prev entry will always be an empty entry. */
511 prev = NULL;
512
513 /*
514 * Note: this function does not honor alignment or fixed location files.
515 * It's behavior is akin to cbfs_copy_instance() in that it expects
516 * the caller to understand the ramifications of compacting a
517 * fragmented CBFS image.
518 */
519
520 for (cur = cbfs_find_first_entry(image);
521 cur && cbfs_is_valid_entry(image, cur);
522 cur = cbfs_find_next_entry(image, cur)) {
523 size_t prev_size;
524 size_t cur_size;
525 size_t empty_metadata_size;
526 size_t spill_size;
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600527
528 /* Current entry is empty. Kepp track of it. */
Alex Jamesb3398ba2022-01-08 01:29:29 -0600529 if (cur->type == CBFS_TYPE_NULL || cur->type == CBFS_TYPE_DELETED) {
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600530 prev = cur;
531 continue;
532 }
533
534 /* Need to ensure the previous entry is an empty one. */
535 if (prev == NULL)
536 continue;
537
538 /* At this point prev is an empty entry. Put the non-empty
Patrick Georgi01cfecc2020-01-29 13:31:16 +0100539 * file in prev's location. Then add a new empty entry. This
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600540 * essentialy bubbles empty entries towards the end. */
541
542 prev_size = cbfs_file_entry_size(prev);
543 cur_size = cbfs_file_entry_size(cur);
544
545 /*
546 * Adjust the empty file size by the actual space occupied
547 * bewtween the beginning of the empty file and the non-empty
548 * file.
549 */
550 prev_size += (cbfs_get_entry_addr(image, cur) -
551 cbfs_get_entry_addr(image, prev)) - prev_size;
552
553 /* Move the non-empty file over the empty file. */
554 memmove(prev, cur, cur_size);
555
556 /*
557 * Get location of the empty file. Note that since prev was
558 * overwritten with the non-empty file the previously moved
559 * file needs to be used to calculate the empty file's location.
560 */
561 cur = cbfs_find_next_entry(image, prev);
562
563 /*
564 * The total space to work with for swapping the 2 entries
565 * consists of the 2 files' sizes combined. However, the
566 * cbfs_file entries start on CBFS_ALIGNMENT boundaries.
567 * Because of this the empty file size may end up smaller
568 * because of the non-empty file's metadata and data length.
569 *
570 * Calculate the spill size which is the amount of data lost
571 * due to the alignment constraints after moving the non-empty
572 * file.
573 */
574 spill_size = (cbfs_get_entry_addr(image, cur) -
575 cbfs_get_entry_addr(image, prev)) - cur_size;
576
577 empty_metadata_size = cbfs_calculate_file_header_size("");
578
579 /* Check if new empty size can contain the metadata. */
580 if (empty_metadata_size + spill_size > prev_size) {
581 ERROR("Unable to swap '%s' with prev empty entry.\n",
582 prev->filename);
583 return 1;
584 }
585
586 /* Update the empty file's size. */
587 prev_size -= spill_size + empty_metadata_size;
588
589 /* Create new empty file. */
Julius Wernerd4775652020-03-13 16:43:34 -0700590 cbfs_create_empty_entry(cur, CBFS_TYPE_NULL,
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600591 prev_size, "");
592
593 /* Merge any potential empty entries together. */
Julius Werner7066a1e2020-04-02 15:49:34 -0700594 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600595
596 /*
597 * Since current switched to an empty file keep track of it.
598 * Even if any empty files were merged the empty entry still
599 * starts at previously calculated location.
600 */
601 prev = cur;
602 }
603
604 return 0;
605}
606
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700607int cbfs_image_delete(struct cbfs_image *image)
608{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100609 if (image == NULL)
610 return 0;
611
Hung-Te Lineab2c812013-01-29 01:56:17 +0800612 buffer_delete(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800613 return 0;
614}
615
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800616/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
617static int cbfs_add_entry_at(struct cbfs_image *image,
618 struct cbfs_file *entry,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800619 const void *data,
Patrick Georgi7fd14182015-08-11 15:55:16 +0200620 uint32_t content_offset,
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100621 const struct cbfs_file *header,
622 const size_t len_align)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700623{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800624 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
625 uint32_t addr = cbfs_get_entry_addr(image, entry),
626 addr_next = cbfs_get_entry_addr(image, next);
Patrick Georgi7fd14182015-08-11 15:55:16 +0200627 uint32_t min_entry_size = cbfs_calculate_file_header_size("");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200628 uint32_t len, header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700629 uint32_t align = image->has_header ? image->header.align :
Julius Wernerd4775652020-03-13 16:43:34 -0700630 CBFS_ALIGNMENT;
Alex James02001a382021-12-19 16:41:59 -0600631 uint32_t header_size = be32toh(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800632
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200633 header_offset = content_offset - header_size;
634 if (header_offset % align)
635 header_offset -= header_offset % align;
636 if (header_offset < addr) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800637 ERROR("No space to hold cbfs_file header.");
638 return -1;
639 }
640
641 // Process buffer BEFORE content_offset.
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200642 if (header_offset - addr > min_entry_size) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800643 DEBUG("|min|...|header|content|... <create new entry>\n");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200644 len = header_offset - addr - min_entry_size;
Julius Wernerd4775652020-03-13 16:43:34 -0700645 cbfs_create_empty_entry(entry, CBFS_TYPE_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800646 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
647 entry = cbfs_find_next_entry(image, entry);
648 addr = cbfs_get_entry_addr(image, entry);
649 }
650
Patrick Georgi7a33b532015-08-25 13:00:04 +0200651 len = content_offset - addr - header_size;
Patrick Georgia60e7b62015-08-25 22:26:02 +0200652 memcpy(entry, header, header_size);
Patrick Georgi7a33b532015-08-25 13:00:04 +0200653 if (len != 0) {
Julius Wernerf0cc7ad2020-11-18 18:31:22 -0800654 /*
655 * The header moved backwards a bit to accommodate cbfs_file
Patrick Georgi7a33b532015-08-25 13:00:04 +0200656 * alignment requirements, so patch up ->offset to still point
Julius Wernerf0cc7ad2020-11-18 18:31:22 -0800657 * to file data. Move attributes forward so the end of the
658 * attribute list still matches the end of the metadata.
Patrick Georgi7a33b532015-08-25 13:00:04 +0200659 */
Alex James02001a382021-12-19 16:41:59 -0600660 uint32_t offset = be32toh(entry->offset);
661 uint32_t attrs = be32toh(entry->attributes_offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800662 DEBUG("|..|header|content|... <use offset to create entry>\n");
Julius Wernerf0cc7ad2020-11-18 18:31:22 -0800663 DEBUG("before: attr_offset=0x%x, offset=0x%x\n", attrs, offset);
664 if (attrs == 0) {
665 memset((uint8_t *)entry + offset, 0, len);
666 } else {
667 uint8_t *p = (uint8_t *)entry + attrs;
668 memmove(p + len, p, offset - attrs);
669 memset(p, 0, len);
670 attrs += len;
Alex James02001a382021-12-19 16:41:59 -0600671 entry->attributes_offset = htobe32(attrs);
Julius Wernerf0cc7ad2020-11-18 18:31:22 -0800672 }
673 offset += len;
Alex James02001a382021-12-19 16:41:59 -0600674 entry->offset = htobe32(offset);
Julius Wernerf0cc7ad2020-11-18 18:31:22 -0800675 DEBUG("after: attr_offset=0x%x, offset=0x%x\n", attrs, offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800676 }
677
678 // Ready to fill data into entry.
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800679 DEBUG("content_offset: 0x%x, entry location: %x\n",
680 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
681 image->buffer.data));
682 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
Patrick Georgicccc9d42015-04-28 13:09:36 +0200683 (ptrdiff_t)content_offset);
Alex James02001a382021-12-19 16:41:59 -0600684 memcpy(CBFS_SUBHEADER(entry), data, be32toh(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800685 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
686
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100687 // Align the length to a multiple of len_align
688 if (len_align &&
Alex James02001a382021-12-19 16:41:59 -0600689 ((be32toh(entry->offset) + be32toh(entry->len)) % len_align)) {
690 size_t off = (be32toh(entry->offset) + be32toh(entry->len)) % len_align;
691 entry->len = htobe32(be32toh(entry->len) + len_align - off);
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100692 }
693
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800694 // Process buffer AFTER entry.
695 entry = cbfs_find_next_entry(image, entry);
696 addr = cbfs_get_entry_addr(image, entry);
Sol Boucher05725652015-04-02 20:58:26 -0700697 if (addr == addr_next)
698 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800699
Sol Boucher05725652015-04-02 20:58:26 -0700700 assert(addr < addr_next);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800701 if (addr_next - addr < min_entry_size) {
Sol Boucher636cc852015-04-03 09:13:04 -0700702 DEBUG("No need for new \"empty\" entry\n");
703 /* No need to increase the size of the just
704 * stored file to extend to next file. Alignment
705 * of next file takes care of this.
706 */
707 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800708 }
709
710 len = addr_next - addr - min_entry_size;
Patrick Georgi29a04d92015-11-20 23:23:44 +0100711 /* keep space for master header pointer */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700712 if ((uint8_t *)entry + min_entry_size + len >
713 (uint8_t *)buffer_get(&image->buffer) +
714 buffer_size(&image->buffer) - sizeof(int32_t)) {
Patrick Georgi29a04d92015-11-20 23:23:44 +0100715 len -= sizeof(int32_t);
716 }
Julius Wernerd4775652020-03-13 16:43:34 -0700717 cbfs_create_empty_entry(entry, CBFS_TYPE_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800718 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
719 return 0;
720}
721
722int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Patrick Georgie5903582015-08-25 13:53:42 +0200723 uint32_t content_offset,
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100724 struct cbfs_file *header,
725 const size_t len_align)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700726{
Sol Boucher67d59982015-05-07 02:39:22 -0700727 assert(image);
728 assert(buffer);
729 assert(buffer->data);
Furquan Shaikh19ba95f2020-11-20 22:50:26 -0800730 assert(!IS_HOST_SPACE_ADDRESS(content_offset));
Sol Boucher67d59982015-05-07 02:39:22 -0700731
Patrick Georgia60e7b62015-08-25 22:26:02 +0200732 const char *name = header->filename;
Patrick Georgie5903582015-08-25 13:53:42 +0200733
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800734 uint32_t entry_type;
735 uint32_t addr, addr_next;
736 struct cbfs_file *entry, *next;
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200737 uint32_t need_size;
Alex James02001a382021-12-19 16:41:59 -0600738 uint32_t header_size = be32toh(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800739
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800740 need_size = header_size + buffer->size;
741 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
742 name, content_offset, header_size, buffer->size, need_size);
743
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800744 // Merge empty entries.
745 DEBUG("(trying to merge empty entries...)\n");
Julius Werner7066a1e2020-04-02 15:49:34 -0700746 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800747
748 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800749 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800750 entry = cbfs_find_next_entry(image, entry)) {
751
Alex James02001a382021-12-19 16:41:59 -0600752 entry_type = be32toh(entry->type);
Julius Wernerd4775652020-03-13 16:43:34 -0700753 if (entry_type != CBFS_TYPE_NULL)
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800754 continue;
755
756 addr = cbfs_get_entry_addr(image, entry);
757 next = cbfs_find_next_entry(image, entry);
758 addr_next = cbfs_get_entry_addr(image, next);
759
760 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
761 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600762
763 /* Will the file fit? Don't yet worry if we have space for a new
764 * "empty" entry. We take care of that later.
765 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800766 if (addr + need_size > addr_next)
767 continue;
768
Patrick Georgiaa44dbd2015-08-12 12:05:21 +0200769 // Test for complicated cases
770 if (content_offset > 0) {
771 if (addr_next < content_offset) {
772 DEBUG("Not for specified offset yet");
773 continue;
774 } else if (addr > content_offset) {
775 DEBUG("Exceed specified content_offset.");
776 break;
777 } else if (addr + header_size > content_offset) {
778 ERROR("Not enough space for header.\n");
779 break;
780 } else if (content_offset + buffer->size > addr_next) {
781 ERROR("Not enough space for content.\n");
782 break;
783 }
784 }
785
786 // TODO there are more few tricky cases that we may
787 // want to fit by altering offset.
788
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200789 if (content_offset == 0) {
790 // we tested every condition earlier under which
791 // placing the file there might fail
792 content_offset = addr + header_size;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800793 }
794
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800795 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
796 addr, addr_next - addr, content_offset);
797
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200798 if (cbfs_add_entry_at(image, entry, buffer->data,
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100799 content_offset, header, len_align) == 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800800 return 0;
801 }
802 break;
803 }
804
805 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
806 buffer->name, buffer->size, buffer->size / 1024, content_offset);
807 return -1;
808}
809
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700810struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
811{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800812 struct cbfs_file *entry;
813 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800814 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800815 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgic569b8b2015-07-15 16:42:38 +0200816 if (strcasecmp(entry->filename, name) == 0) {
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800817 DEBUG("cbfs_get_entry: found %s\n", name);
818 return entry;
819 }
820 }
821 return NULL;
822}
823
Antonello Dettorifda691e2016-06-09 12:35:36 +0200824static int cbfs_payload_decompress(struct cbfs_payload_segment *segments,
825 struct buffer *buff, int num_seg)
826{
827 struct buffer new_buffer;
828 struct buffer seg_buffer;
829 size_t new_buff_sz;
830 char *in_ptr;
831 char *out_ptr;
832 size_t new_offset;
833 decomp_func_ptr decompress;
834
835 new_offset = num_seg * sizeof(*segments);
836 new_buff_sz = num_seg * sizeof(*segments);
837
838 /* Find out and allocate the amount of memory occupied
839 * by the binary data */
840 for (int i = 0; i < num_seg; i++)
841 new_buff_sz += segments[i].mem_len;
842
Furquan Shaikh58644a02016-08-05 08:27:18 -0700843 if (buffer_create(&new_buffer, new_buff_sz, "decompressed_buff"))
844 return -1;
Antonello Dettorifda691e2016-06-09 12:35:36 +0200845
846 in_ptr = buffer_get(buff) + new_offset;
847 out_ptr = buffer_get(&new_buffer) + new_offset;
848
849 for (int i = 0; i < num_seg; i++) {
850 struct buffer tbuff;
851 size_t decomp_size;
852
Antonello Dettorifda691e2016-06-09 12:35:36 +0200853 /* Segments BSS and ENTRY do not have binary data. */
854 if (segments[i].type == PAYLOAD_SEGMENT_BSS ||
855 segments[i].type == PAYLOAD_SEGMENT_ENTRY) {
856 continue;
857 } else if (segments[i].type == PAYLOAD_SEGMENT_PARAMS) {
858 memcpy(out_ptr, in_ptr, segments[i].len);
859 segments[i].offset = new_offset;
860 new_offset += segments[i].len;
861 in_ptr += segments[i].len;
862 out_ptr += segments[i].len;
863 segments[i].compression = CBFS_COMPRESS_NONE;
864 continue;
865 }
866
Joel Kitching72d77a92018-07-18 13:23:52 +0800867 /* The payload uses an unknown compression algorithm. */
868 decompress = decompression_function(segments[i].compression);
869 if (decompress == NULL) {
870 ERROR("Unknown decompression algorithm: %u\n",
871 segments[i].compression);
872 return -1;
873 }
874
Furquan Shaikh58644a02016-08-05 08:27:18 -0700875 if (buffer_create(&tbuff, segments[i].mem_len, "segment")) {
876 buffer_delete(&new_buffer);
877 return -1;
878 }
Antonello Dettorifda691e2016-06-09 12:35:36 +0200879
880 if (decompress(in_ptr, segments[i].len, buffer_get(&tbuff),
881 (int) buffer_size(&tbuff),
882 &decomp_size)) {
883 ERROR("Couldn't decompress payload segment %u\n", i);
884 buffer_delete(&new_buffer);
Furquan Shaikh9844d562016-08-05 08:32:23 -0700885 buffer_delete(&tbuff);
Antonello Dettorifda691e2016-06-09 12:35:36 +0200886 return -1;
887 }
888
889 memcpy(out_ptr, buffer_get(&tbuff), decomp_size);
890
891 in_ptr += segments[i].len;
892
893 /* Update the offset of the segment. */
894 segments[i].offset = new_offset;
895 /* True decompressed size is just the data size. No metadata */
896 segments[i].len = decomp_size;
897 /* Segment is not compressed. */
898 segments[i].compression = CBFS_COMPRESS_NONE;
899
900 /* Update the offset and output buffer pointer. */
901 new_offset += decomp_size;
902 out_ptr += decomp_size;
903
904 buffer_delete(&tbuff);
905 }
906
907 buffer_splice(&seg_buffer, &new_buffer, 0, 0);
908 xdr_segs(&seg_buffer, segments, num_seg);
909
910 buffer_delete(buff);
911 *buff = new_buffer;
912
913 return 0;
914}
915
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500916static int init_elf_from_arch(Elf64_Ehdr *ehdr, uint32_t cbfs_arch)
917{
918 int endian;
919 int nbits;
920 int machine;
921
922 switch (cbfs_arch) {
923 case CBFS_ARCHITECTURE_X86:
924 endian = ELFDATA2LSB;
925 nbits = ELFCLASS32;
926 machine = EM_386;
927 break;
928 case CBFS_ARCHITECTURE_ARM:
929 endian = ELFDATA2LSB;
930 nbits = ELFCLASS32;
931 machine = EM_ARM;
932 break;
933 case CBFS_ARCHITECTURE_AARCH64:
934 endian = ELFDATA2LSB;
935 nbits = ELFCLASS64;
936 machine = EM_AARCH64;
937 break;
938 case CBFS_ARCHITECTURE_MIPS:
939 endian = ELFDATA2LSB;
940 nbits = ELFCLASS32;
941 machine = EM_MIPS;
942 break;
943 case CBFS_ARCHITECTURE_RISCV:
944 endian = ELFDATA2LSB;
945 nbits = ELFCLASS32;
946 machine = EM_RISCV;
947 break;
948 default:
949 ERROR("Unsupported arch: %x\n", cbfs_arch);
950 return -1;
951 }
952
953 elf_init_eheader(ehdr, machine, nbits, endian);
954 return 0;
955}
956
Julius Werner81dc20e2020-10-15 17:37:57 -0700957static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch,
958 struct cbfs_file *entry)
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500959{
960 Elf64_Ehdr ehdr;
961 Elf64_Shdr shdr;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500962 struct elf_writer *ew;
963 struct buffer elf_out;
964 size_t empty_sz;
Aaron Durbin694fd132015-10-28 11:39:34 -0500965 int rmod_ret;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500966
Antonello Dettori0b806282016-06-26 00:24:25 +0200967 if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
968 ERROR("You need to specify -m ARCH.\n");
969 return -1;
970 }
971
Julius Werner81dc20e2020-10-15 17:37:57 -0700972 struct cbfs_file_attr_stageheader *stage = NULL;
973 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
974 attr != NULL; attr = cbfs_file_next_attr(entry, attr)) {
Alex James02001a382021-12-19 16:41:59 -0600975 if (be32toh(attr->tag) == CBFS_FILE_ATTR_TAG_STAGEHEADER) {
Julius Werner81dc20e2020-10-15 17:37:57 -0700976 stage = (struct cbfs_file_attr_stageheader *)attr;
977 break;
978 }
979 }
980
981 if (stage == NULL) {
982 ERROR("Stage header not found for %s\n", entry->filename);
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500983 return -1;
984 }
985
986 if (init_elf_from_arch(&ehdr, arch))
987 return -1;
988
Aaron Durbin694fd132015-10-28 11:39:34 -0500989 /* Attempt rmodule translation first. */
990 rmod_ret = rmodule_stage_to_elf(&ehdr, buff);
991
992 if (rmod_ret < 0) {
993 ERROR("rmodule parsing failed\n");
994 return -1;
995 } else if (rmod_ret == 0)
996 return 0;
997
998 /* Rmodule couldn't do anything with the data. Continue on with SELF. */
999
Alex James02001a382021-12-19 16:41:59 -06001000 ehdr.e_entry = be64toh(stage->loadaddr) + be32toh(stage->entry_offset);
Julius Werner81dc20e2020-10-15 17:37:57 -07001001
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001002 ew = elf_writer_init(&ehdr);
1003 if (ew == NULL) {
1004 ERROR("Unable to init ELF writer.\n");
1005 return -1;
1006 }
1007
1008 memset(&shdr, 0, sizeof(shdr));
1009 shdr.sh_type = SHT_PROGBITS;
1010 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
Alex James02001a382021-12-19 16:41:59 -06001011 shdr.sh_addr = be64toh(stage->loadaddr);
Julius Werner81dc20e2020-10-15 17:37:57 -07001012 shdr.sh_size = buffer_size(buff);
Alex James02001a382021-12-19 16:41:59 -06001013 empty_sz = be32toh(stage->memlen) - buffer_size(buff);
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001014
1015 if (elf_writer_add_section(ew, &shdr, buff, ".program")) {
1016 ERROR("Unable to add ELF section: .program\n");
1017 elf_writer_destroy(ew);
1018 return -1;
1019 }
1020
1021 if (empty_sz != 0) {
1022 struct buffer b;
1023
1024 buffer_init(&b, NULL, NULL, 0);
1025 memset(&shdr, 0, sizeof(shdr));
1026 shdr.sh_type = SHT_NOBITS;
1027 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
Alex Jamesb3398ba2022-01-08 01:29:29 -06001028 shdr.sh_addr = be64toh(stage->loadaddr) + buffer_size(buff);
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001029 shdr.sh_size = empty_sz;
1030 if (elf_writer_add_section(ew, &shdr, &b, ".empty")) {
1031 ERROR("Unable to add ELF section: .empty\n");
1032 elf_writer_destroy(ew);
1033 return -1;
1034 }
1035 }
1036
1037 if (elf_writer_serialize(ew, &elf_out)) {
1038 ERROR("Unable to create ELF file from stage.\n");
1039 elf_writer_destroy(ew);
1040 return -1;
1041 }
1042
1043 /* Flip buffer with the created ELF one. */
1044 buffer_delete(buff);
1045 *buff = elf_out;
1046
1047 elf_writer_destroy(ew);
1048
1049 return 0;
1050}
1051
Julius Werner81dc20e2020-10-15 17:37:57 -07001052static int cbfs_payload_make_elf(struct buffer *buff, uint32_t arch,
1053 unused struct cbfs_file *entry)
Antonello Dettorifda691e2016-06-09 12:35:36 +02001054{
1055 Elf64_Ehdr ehdr;
1056 Elf64_Shdr shdr;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001057 struct cbfs_payload_segment *segs = NULL;
Jonathan Neuschäfer5de54582016-08-09 15:01:58 +02001058 struct elf_writer *ew = NULL;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001059 struct buffer elf_out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001060 int segments = 0;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001061 int retval = -1;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001062
Antonello Dettori0b806282016-06-26 00:24:25 +02001063 if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
1064 ERROR("You need to specify -m ARCH.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001065 goto out;
Antonello Dettori0b806282016-06-26 00:24:25 +02001066 }
1067
Antonello Dettorifda691e2016-06-09 12:35:36 +02001068 /* Count the number of segments inside buffer */
1069 while (true) {
1070 uint32_t payload_type = 0;
1071
1072 struct cbfs_payload_segment *seg;
1073
1074 seg = buffer_get(buff);
1075 payload_type = read_be32(&seg[segments].type);
1076
1077 if (payload_type == PAYLOAD_SEGMENT_CODE) {
1078 segments++;
1079 } else if (payload_type == PAYLOAD_SEGMENT_DATA) {
1080 segments++;
1081 } else if (payload_type == PAYLOAD_SEGMENT_BSS) {
1082 segments++;
1083 } else if (payload_type == PAYLOAD_SEGMENT_PARAMS) {
1084 segments++;
1085 } else if (payload_type == PAYLOAD_SEGMENT_ENTRY) {
1086 /* The last segment in a payload is always ENTRY as
1087 * specified by the parse_elf_to_payload() function.
1088 * Therefore there is no need to continue looking for
1089 * segments.*/
1090 segments++;
1091 break;
1092 } else {
1093 ERROR("Unknown payload segment type: %x\n",
1094 payload_type);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001095 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001096 }
1097 }
1098
1099 segs = malloc(segments * sizeof(*segs));
1100
1101 /* Decode xdr segments */
1102 for (int i = 0; i < segments; i++) {
1103 struct cbfs_payload_segment *serialized_seg = buffer_get(buff);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001104 xdr_get_seg(&segs[i], &serialized_seg[i]);
1105 }
1106
1107 if (cbfs_payload_decompress(segs, buff, segments)) {
1108 ERROR("Failed to decompress payload.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001109 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001110 }
1111
1112 if (init_elf_from_arch(&ehdr, arch))
Furquan Shaikh7b405172016-08-05 08:20:37 -07001113 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001114
1115 ehdr.e_entry = segs[segments-1].load_addr;
1116
1117 ew = elf_writer_init(&ehdr);
1118 if (ew == NULL) {
1119 ERROR("Unable to init ELF writer.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001120 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001121 }
1122
1123 for (int i = 0; i < segments; i++) {
1124 struct buffer tbuff;
Furquan Shaikhf3bba442016-08-05 08:12:31 -07001125 size_t empty_sz = 0;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001126
1127 memset(&shdr, 0, sizeof(shdr));
1128 char *name = NULL;
1129
1130 if (segs[i].type == PAYLOAD_SEGMENT_CODE) {
1131 shdr.sh_type = SHT_PROGBITS;
1132 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
1133 shdr.sh_addr = segs[i].load_addr;
1134 shdr.sh_size = segs[i].len;
1135 empty_sz = segs[i].mem_len - segs[i].len;
1136 name = strdup(".text");
1137 buffer_splice(&tbuff, buff, segs[i].offset,
1138 segs[i].len);
1139 } else if (segs[i].type == PAYLOAD_SEGMENT_DATA) {
1140 shdr.sh_type = SHT_PROGBITS;
1141 shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
1142 shdr.sh_addr = segs[i].load_addr;
1143 shdr.sh_size = segs[i].len;
1144 empty_sz = segs[i].mem_len - segs[i].len;
1145 name = strdup(".data");
1146 buffer_splice(&tbuff, buff, segs[i].offset,
1147 segs[i].len);
1148 } else if (segs[i].type == PAYLOAD_SEGMENT_BSS) {
1149 shdr.sh_type = SHT_NOBITS;
1150 shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
1151 shdr.sh_addr = segs[i].load_addr;
1152 shdr.sh_size = segs[i].len;
1153 name = strdup(".bss");
1154 buffer_splice(&tbuff, buff, 0, 0);
1155 } else if (segs[i].type == PAYLOAD_SEGMENT_PARAMS) {
1156 shdr.sh_type = SHT_NOTE;
1157 shdr.sh_flags = 0;
1158 shdr.sh_size = segs[i].len;
1159 name = strdup(".note.pinfo");
1160 buffer_splice(&tbuff, buff, segs[i].offset,
1161 segs[i].len);
1162 } else if (segs[i].type == PAYLOAD_SEGMENT_ENTRY) {
1163 break;
Patrick Georgidce629b2017-01-13 13:30:54 +01001164 } else {
1165 ERROR("unknown ELF segment type\n");
1166 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001167 }
1168
Patrick Georgidce629b2017-01-13 13:30:54 +01001169 if (!name) {
1170 ERROR("out of memory\n");
1171 goto out;
1172 }
Antonello Dettorifda691e2016-06-09 12:35:36 +02001173
1174 if (elf_writer_add_section(ew, &shdr, &tbuff, name)) {
1175 ERROR("Unable to add ELF section: %s\n", name);
Patrick Georgidce629b2017-01-13 13:30:54 +01001176 free(name);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001177 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001178 }
Patrick Georgidce629b2017-01-13 13:30:54 +01001179 free(name);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001180
1181 if (empty_sz != 0) {
1182 struct buffer b;
1183
1184 buffer_init(&b, NULL, NULL, 0);
1185 memset(&shdr, 0, sizeof(shdr));
1186 shdr.sh_type = SHT_NOBITS;
1187 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
1188 shdr.sh_addr = segs[i].load_addr + segs[i].len;
1189 shdr.sh_size = empty_sz;
1190 name = strdup(".empty");
Patrick Georgidce629b2017-01-13 13:30:54 +01001191 if (!name) {
1192 ERROR("out of memory\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001193 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001194 }
Patrick Georgidce629b2017-01-13 13:30:54 +01001195 if (elf_writer_add_section(ew, &shdr, &b, name)) {
1196 ERROR("Unable to add ELF section: %s\n", name);
1197 free(name);
1198 goto out;
1199 }
1200 free(name);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001201 }
Antonello Dettorifda691e2016-06-09 12:35:36 +02001202 }
1203
1204 if (elf_writer_serialize(ew, &elf_out)) {
Julius Werner81dc20e2020-10-15 17:37:57 -07001205 ERROR("Unable to create ELF file from payload.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001206 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001207 }
1208
1209 /* Flip buffer with the created ELF one. */
1210 buffer_delete(buff);
1211 *buff = elf_out;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001212 retval = 0;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001213
Furquan Shaikh7b405172016-08-05 08:20:37 -07001214out:
1215 free(segs);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001216 elf_writer_destroy(ew);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001217 return retval;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001218}
1219
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001220int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Joel Kitching21fdd892018-08-09 17:49:52 +08001221 const char *filename, uint32_t arch, bool do_processing)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001222{
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001223 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
1224 struct buffer buffer;
1225 if (!entry) {
1226 ERROR("File not found: %s\n", entry_name);
1227 return -1;
1228 }
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001229
Alex James02001a382021-12-19 16:41:59 -06001230 unsigned int compressed_size = be32toh(entry->len);
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001231 unsigned int decompressed_size = 0;
1232 unsigned int compression = cbfs_file_get_compression_info(entry,
1233 &decompressed_size);
Werner Zehbbf1df72018-11-21 13:07:50 +01001234 unsigned int buffer_len;
Joel Kitching21fdd892018-08-09 17:49:52 +08001235 decomp_func_ptr decompress;
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001236
Joel Kitching21fdd892018-08-09 17:49:52 +08001237 if (do_processing) {
1238 decompress = decompression_function(compression);
1239 if (!decompress) {
1240 ERROR("looking up decompression routine failed\n");
1241 return -1;
1242 }
Werner Zehbbf1df72018-11-21 13:07:50 +01001243 buffer_len = decompressed_size;
Joel Kitching21fdd892018-08-09 17:49:52 +08001244 } else {
1245 /* Force nop decompression */
1246 decompress = decompression_function(CBFS_COMPRESS_NONE);
Werner Zehbbf1df72018-11-21 13:07:50 +01001247 buffer_len = compressed_size;
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001248 }
1249
Joel Kitching21fdd892018-08-09 17:49:52 +08001250 LOG("Found file %.30s at 0x%x, type %.12s, compressed %d, size %d\n",
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001251 entry_name, cbfs_get_entry_addr(image, entry),
Alex James02001a382021-12-19 16:41:59 -06001252 get_cbfs_entry_type_name(be32toh(entry->type)), compressed_size,
Joel Kitching21fdd892018-08-09 17:49:52 +08001253 decompressed_size);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001254
Aaron Durbin539aed02015-10-23 17:42:32 -05001255 buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0);
Werner Zehbbf1df72018-11-21 13:07:50 +01001256 buffer.data = malloc(buffer_len);
1257 buffer.size = buffer_len;
Aaron Durbin539aed02015-10-23 17:42:32 -05001258
Joel Kitching21fdd892018-08-09 17:49:52 +08001259 if (decompress(CBFS_SUBHEADER(entry), compressed_size,
1260 buffer.data, buffer.size, NULL)) {
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001261 ERROR("decompression failed for %s\n", entry_name);
Aaron Durbin539aed02015-10-23 17:42:32 -05001262 buffer_delete(&buffer);
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001263 return -1;
1264 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001265
1266 /*
Julius Werner81dc20e2020-10-15 17:37:57 -07001267 * We want to export stages and payloads as ELFs, not with coreboot's
1268 * custom stage/SELF binary formats, so we need to do extra processing
1269 * to turn them back into an ELF.
Aaron Durbin539aed02015-10-23 17:42:32 -05001270 */
Joel Kitching21fdd892018-08-09 17:49:52 +08001271 if (do_processing) {
Julius Werner81dc20e2020-10-15 17:37:57 -07001272 int (*make_elf)(struct buffer *, uint32_t,
1273 struct cbfs_file *) = NULL;
Alex James02001a382021-12-19 16:41:59 -06001274 switch (be32toh(entry->type)) {
Julius Wernerd4775652020-03-13 16:43:34 -07001275 case CBFS_TYPE_STAGE:
Joel Kitching21fdd892018-08-09 17:49:52 +08001276 make_elf = cbfs_stage_make_elf;
1277 break;
Julius Wernerd4775652020-03-13 16:43:34 -07001278 case CBFS_TYPE_SELF:
Joel Kitching21fdd892018-08-09 17:49:52 +08001279 make_elf = cbfs_payload_make_elf;
1280 break;
Aaron Durbin539aed02015-10-23 17:42:32 -05001281 }
Julius Werner81dc20e2020-10-15 17:37:57 -07001282 if (make_elf && make_elf(&buffer, arch, entry)) {
Joel Kitching21fdd892018-08-09 17:49:52 +08001283 ERROR("Failed to write %s into %s.\n",
1284 entry_name, filename);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001285 buffer_delete(&buffer);
1286 return -1;
1287 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001288 }
1289
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001290 if (buffer_write_file(&buffer, filename) != 0) {
1291 ERROR("Failed to write %s into %s.\n",
1292 entry_name, filename);
Aaron Durbin539aed02015-10-23 17:42:32 -05001293 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001294 return -1;
1295 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001296
1297 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001298 INFO("Successfully dumped the file to: %s\n", filename);
1299 return 0;
1300}
1301
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001302int cbfs_remove_entry(struct cbfs_image *image, const char *name)
1303{
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +02001304 struct cbfs_file *entry;
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001305 entry = cbfs_get_entry(image, name);
1306 if (!entry) {
1307 ERROR("CBFS file %s not found.\n", name);
1308 return -1;
1309 }
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001310 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
Patrick Georgic569b8b2015-07-15 16:42:38 +02001311 entry->filename, cbfs_get_entry_addr(image, entry));
Alex James02001a382021-12-19 16:41:59 -06001312 entry->type = htobe32(CBFS_TYPE_DELETED);
Julius Werner7066a1e2020-04-02 15:49:34 -07001313 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001314 return 0;
1315}
1316
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001317int cbfs_print_header_info(struct cbfs_image *image)
1318{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001319 char *name = strdup(image->buffer.name);
Sol Boucher3e060ed2015-05-05 15:40:15 -07001320 assert(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001321 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -08001322 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001323 basename(name),
1324 image->buffer.size / 1024,
Sol Boucher3e060ed2015-05-05 15:40:15 -07001325 image->header.bootblocksize,
1326 image->header.romsize,
1327 image->header.offset,
1328 image->header.align,
1329 arch_to_string(image->header.architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001330 free(name);
1331 return 0;
1332}
1333
Julius Werner81dc20e2020-10-15 17:37:57 -07001334static int cbfs_print_stage_info(struct cbfs_file *entry, FILE* fp)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001335{
Julius Werner81dc20e2020-10-15 17:37:57 -07001336
1337 struct cbfs_file_attr_stageheader *stage = NULL;
1338 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
1339 attr != NULL; attr = cbfs_file_next_attr(entry, attr)) {
Alex James02001a382021-12-19 16:41:59 -06001340 if (be32toh(attr->tag) == CBFS_FILE_ATTR_TAG_STAGEHEADER) {
Julius Werner81dc20e2020-10-15 17:37:57 -07001341 stage = (struct cbfs_file_attr_stageheader *)attr;
1342 break;
1343 }
1344 }
1345
1346 if (stage == NULL) {
1347 fprintf(fp, " ERROR: stage header not found!\n");
1348 return -1;
1349 }
1350
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001351 fprintf(fp,
Julius Werner81dc20e2020-10-15 17:37:57 -07001352 " entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
1353 "memlen: %d\n",
Alex James02001a382021-12-19 16:41:59 -06001354 be64toh(stage->loadaddr) + be32toh(stage->entry_offset),
1355 be64toh(stage->loadaddr),
1356 be32toh(stage->memlen));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001357 return 0;
1358}
1359
Hung-Te Lin0780d672014-05-16 10:14:05 +08001360static int cbfs_print_decoded_payload_segment_info(
1361 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001362{
Hung-Te Lin0780d672014-05-16 10:14:05 +08001363 /* The input (seg) must be already decoded by
1364 * cbfs_decode_payload_segment.
1365 */
1366 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001367 case PAYLOAD_SEGMENT_CODE:
1368 case PAYLOAD_SEGMENT_DATA:
1369 fprintf(fp, " %s (%s compression, offset: 0x%x, "
1370 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001371 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001372 "code " : "data"),
1373 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +08001374 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001375 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +08001376 seg->offset, seg->load_addr, seg->len,
1377 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001378 break;
1379
1380 case PAYLOAD_SEGMENT_ENTRY:
1381 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001382 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001383 break;
1384
1385 case PAYLOAD_SEGMENT_BSS:
1386 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
1387 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001388 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001389 break;
1390
1391 case PAYLOAD_SEGMENT_PARAMS:
1392 fprintf(fp, " parameters\n");
1393 break;
1394
1395 default:
1396 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
1397 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001398 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001399 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +08001400 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001401 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +08001402 seg->offset, seg->load_addr, seg->len,
1403 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001404 break;
1405 }
1406 return 0;
1407}
1408
1409int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001410 void *arg)
1411{
Patrick Georgic569b8b2015-07-15 16:42:38 +02001412 const char *name = entry->filename;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001413 struct cbfs_payload_segment *payload;
1414 FILE *fp = (FILE *)arg;
1415
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001416 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001417 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1418 cbfs_get_entry_addr(image, entry));
1419 return -1;
1420 }
1421 if (!fp)
1422 fp = stdout;
1423
Patrick Georgic82725c2015-08-26 12:13:03 +02001424 unsigned int decompressed_size = 0;
Patrick Georgia71c83f2015-08-26 12:23:26 +02001425 unsigned int compression = cbfs_file_get_compression_info(entry,
1426 &decompressed_size);
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001427 const char *compression_name = lookup_name_by_type(
1428 types_cbfs_compression, compression, "????");
Patrick Georgic82725c2015-08-26 12:13:03 +02001429
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001430 if (compression == CBFS_COMPRESS_NONE)
1431 fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s\n",
Patrick Georgic82725c2015-08-26 12:13:03 +02001432 *name ? name : "(empty)",
1433 cbfs_get_entry_addr(image, entry),
Alex James02001a382021-12-19 16:41:59 -06001434 get_cbfs_entry_type_name(be32toh(entry->type)),
1435 be32toh(entry->len),
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001436 compression_name
Patrick Georgic82725c2015-08-26 12:13:03 +02001437 );
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001438 else
1439 fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s (%d decompressed)\n",
1440 *name ? name : "(empty)",
1441 cbfs_get_entry_addr(image, entry),
Alex James02001a382021-12-19 16:41:59 -06001442 get_cbfs_entry_type_name(be32toh(entry->type)),
1443 be32toh(entry->len),
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001444 compression_name,
1445 decompressed_size
1446 );
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001447
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001448 if (!verbose)
1449 return 0;
1450
Julius Wernerd4775652020-03-13 16:43:34 -07001451 struct cbfs_file_attr_hash *attr = NULL;
1452 while ((attr = cbfs_file_get_next_hash(entry, attr)) != NULL) {
1453 size_t hash_len = vb2_digest_size(attr->hash.algo);
1454 if (!hash_len) {
1455 fprintf(fp, "invalid/unsupported hash algorithm: %d\n",
1456 attr->hash.algo);
Patrick Georgi89f20342015-10-01 15:54:04 +02001457 break;
1458 }
Julius Wernerd4775652020-03-13 16:43:34 -07001459 char *hash_str = bintohex(attr->hash.raw, hash_len);
1460 int valid = vb2_hash_verify(CBFS_SUBHEADER(entry),
Alex James02001a382021-12-19 16:41:59 -06001461 be32toh(entry->len), &attr->hash) == VB2_SUCCESS;
Patrick Georgi89f20342015-10-01 15:54:04 +02001462 const char *valid_str = valid ? "valid" : "invalid";
1463
1464 fprintf(fp, " hash %s:%s %s\n",
Julius Wernerd4775652020-03-13 16:43:34 -07001465 vb2_get_hash_algorithm_name(attr->hash.algo),
Patrick Georgi89f20342015-10-01 15:54:04 +02001466 hash_str, valid_str);
1467 free(hash_str);
1468 }
1469
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001470 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
Alex James02001a382021-12-19 16:41:59 -06001471 cbfs_get_entry_addr(image, entry), be32toh(entry->offset),
1472 cbfs_get_entry_addr(image, entry) + be32toh(entry->offset),
1473 be32toh(entry->len));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001474
1475 /* note the components of the subheader may be in host order ... */
Alex James02001a382021-12-19 16:41:59 -06001476 switch (be32toh(entry->type)) {
Julius Wernerd4775652020-03-13 16:43:34 -07001477 case CBFS_TYPE_STAGE:
Julius Werner81dc20e2020-10-15 17:37:57 -07001478 cbfs_print_stage_info(entry, fp);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001479 break;
1480
Julius Wernerd4775652020-03-13 16:43:34 -07001481 case CBFS_TYPE_SELF:
Paul Menzel831bbe82015-08-08 20:20:57 +02001482 payload = (struct cbfs_payload_segment *)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001483 CBFS_SUBHEADER(entry);
1484 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +08001485 struct cbfs_payload_segment seg;
1486 cbfs_decode_payload_segment(&seg, payload);
1487 cbfs_print_decoded_payload_segment_info(
1488 &seg, fp);
1489 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001490 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +08001491 else
Aaron Durbinca630272014-08-05 10:48:20 -05001492 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001493 }
1494 break;
1495 default:
1496 break;
1497 }
1498 return 0;
1499}
1500
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001501static int cbfs_print_parseable_entry_info(struct cbfs_image *image,
1502 struct cbfs_file *entry, void *arg)
1503{
1504 FILE *fp = (FILE *)arg;
1505 const char *name;
1506 const char *type;
1507 size_t offset;
1508 size_t metadata_size;
1509 size_t data_size;
1510 const char *sep = "\t";
1511
1512 if (!cbfs_is_valid_entry(image, entry)) {
1513 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1514 cbfs_get_entry_addr(image, entry));
1515 return -1;
1516 }
1517
1518 name = entry->filename;
1519 if (*name == '\0')
1520 name = "(empty)";
Alex James02001a382021-12-19 16:41:59 -06001521 type = get_cbfs_entry_type_name(be32toh(entry->type)),
1522 metadata_size = be32toh(entry->offset);
1523 data_size = be32toh(entry->len);
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001524 offset = cbfs_get_entry_addr(image, entry);
1525
1526 fprintf(fp, "%s%s", name, sep);
1527 fprintf(fp, "0x%zx%s", offset, sep);
1528 fprintf(fp, "%s%s", type, sep);
1529 fprintf(fp, "0x%zx%s", metadata_size, sep);
1530 fprintf(fp, "0x%zx%s", data_size, sep);
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001531 fprintf(fp, "0x%zx", metadata_size + data_size);
1532
1533 if (verbose) {
1534 unsigned int decompressed_size = 0;
1535 unsigned int compression = cbfs_file_get_compression_info(entry,
1536 &decompressed_size);
1537 if (compression != CBFS_COMPRESS_NONE)
1538 fprintf(fp, "%scomp:%s:0x%x", sep, lookup_name_by_type(
1539 types_cbfs_compression, compression, "????"),
1540 decompressed_size);
1541
1542 struct cbfs_file_attr_hash *attr = NULL;
1543 while ((attr = cbfs_file_get_next_hash(entry, attr)) != NULL) {
1544 size_t hash_len = vb2_digest_size(attr->hash.algo);
1545 if (!hash_len)
1546 continue;
1547 char *hash_str = bintohex(attr->hash.raw, hash_len);
1548 int valid = vb2_hash_verify(CBFS_SUBHEADER(entry),
Alex James02001a382021-12-19 16:41:59 -06001549 be32toh(entry->len), &attr->hash) == VB2_SUCCESS;
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001550 fprintf(fp, "%shash:%s:%s:%s", sep,
1551 vb2_get_hash_algorithm_name(attr->hash.algo),
1552 hash_str, valid ? "valid" : "invalid");
1553 free(hash_str);
1554 }
1555 }
1556 fprintf(fp, "\n");
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001557
1558 return 0;
1559}
1560
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001561void cbfs_print_directory(struct cbfs_image *image)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001562{
Sol Boucher67a0a862015-03-18 12:36:27 -07001563 if (cbfs_is_legacy_cbfs(image))
1564 cbfs_print_header_info(image);
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001565 printf("%-30s %-10s %-12s Size Comp\n", "Name", "Offset", "Type");
Julius Werner7066a1e2020-04-02 15:49:34 -07001566 cbfs_legacy_walk(image, cbfs_print_entry_info, NULL);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001567}
1568
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001569void cbfs_print_parseable_directory(struct cbfs_image *image)
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001570{
Furquan Shaikh161d2332016-05-26 14:41:02 -07001571 size_t i;
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001572 const char *header[] = {
1573 "Name",
1574 "Offset",
1575 "Type",
1576 "Metadata Size",
1577 "Data Size",
1578 "Total Size",
1579 };
1580 const char *sep = "\t";
1581
1582 for (i = 0; i < ARRAY_SIZE(header) - 1; i++)
1583 fprintf(stdout, "%s%s", header[i], sep);
1584 fprintf(stdout, "%s\n", header[i]);
Julius Werner7066a1e2020-04-02 15:49:34 -07001585 cbfs_legacy_walk(image, cbfs_print_parseable_entry_info, stdout);
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001586}
1587
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001588int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -08001589 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001590{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001591 struct cbfs_file *next;
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001592 uint32_t next_addr = 0;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001593
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001594 /* We don't return here even if this entry is already empty because we
1595 want to merge the empty entries following after it. */
1596
1597 /* Loop until non-empty entry is found, starting from the current entry.
1598 After the loop, next_addr points to the next non-empty entry. */
1599 next = entry;
Alex James02001a382021-12-19 16:41:59 -06001600 while (be32toh(next->type) == CBFS_TYPE_DELETED ||
1601 be32toh(next->type) == CBFS_TYPE_NULL) {
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001602 next = cbfs_find_next_entry(image, next);
1603 if (!next)
1604 break;
1605 next_addr = cbfs_get_entry_addr(image, next);
1606 if (!cbfs_is_valid_entry(image, next))
1607 /* 'next' could be the end of cbfs */
1608 break;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001609 }
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001610
1611 if (!next_addr)
1612 /* Nothing to empty */
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001613 return 0;
1614
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001615 /* We can return here if we find only a single empty entry.
1616 For simplicity, we just proceed (and make it empty again). */
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001617
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001618 /* We're creating one empty entry for combined empty spaces */
1619 uint32_t addr = cbfs_get_entry_addr(image, entry);
1620 size_t len = next_addr - addr - cbfs_calculate_file_header_size("");
1621 DEBUG("join_empty_entry: [0x%x, 0x%x) len=%zu\n", addr, next_addr, len);
Julius Wernerd4775652020-03-13 16:43:34 -07001622 cbfs_create_empty_entry(entry, CBFS_TYPE_NULL, len, "");
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001623
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001624 return 0;
1625}
1626
Julius Werner7066a1e2020-04-02 15:49:34 -07001627int cbfs_legacy_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001628 void *arg)
1629{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001630 int count = 0;
1631 struct cbfs_file *entry;
1632 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001633 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001634 entry = cbfs_find_next_entry(image, entry)) {
1635 count ++;
1636 if (callback(image, entry, arg) != 0)
1637 break;
1638 }
1639 return count;
1640}
1641
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001642static int cbfs_header_valid(struct cbfs_header *header)
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001643{
Alex James02001a382021-12-19 16:41:59 -06001644 if ((be32toh(header->magic) == CBFS_HEADER_MAGIC) &&
1645 ((be32toh(header->version) == CBFS_HEADER_VERSION1) ||
1646 (be32toh(header->version) == CBFS_HEADER_VERSION2)) &&
1647 (be32toh(header->offset) < be32toh(header->romsize)))
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001648 return 1;
1649 return 0;
1650}
1651
1652struct cbfs_header *cbfs_find_header(char *data, size_t size,
1653 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001654{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001655 size_t offset;
1656 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001657 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001658 struct cbfs_header *header, *result = NULL;
1659
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001660 if (forced_offset < (size - sizeof(struct cbfs_header))) {
1661 /* Check if the forced header is valid. */
1662 header = (struct cbfs_header *)(data + forced_offset);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001663 if (cbfs_header_valid(header))
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001664 return header;
1665 return NULL;
1666 }
1667
Julius Wernerefcee762014-11-10 13:14:24 -08001668 // Try finding relative offset of master header at end of file first.
1669 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
1670 offset = size + rel_offset;
1671 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
1672 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001673
Hung-Te Lineab2c812013-01-29 01:56:17 +08001674 if (offset >= size - sizeof(*header) ||
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001675 !cbfs_header_valid((struct cbfs_header *)(data + offset))) {
Julius Wernerefcee762014-11-10 13:14:24 -08001676 // Some use cases append non-CBFS data to the end of the ROM.
1677 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +08001678 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001679 }
Hung-Te Lineab2c812013-01-29 01:56:17 +08001680
1681 for (; offset + sizeof(*header) < size; offset++) {
1682 header = (struct cbfs_header *)(data + offset);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001683 if (!cbfs_header_valid(header))
Hung-Te Lineab2c812013-01-29 01:56:17 +08001684 continue;
Julius Wernerefcee762014-11-10 13:14:24 -08001685 if (!found++)
1686 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001687 }
Julius Wernerefcee762014-11-10 13:14:24 -08001688 if (found > 1)
1689 // Top-aligned images usually have a working relative offset
1690 // field, so this is more likely to happen on bottom-aligned
1691 // ones (where the first header is the "outermost" one)
1692 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +08001693 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001694 return result;
1695}
1696
1697
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001698struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
1699{
Sol Boucher3e060ed2015-05-05 15:40:15 -07001700 assert(image);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001701 if (image->has_header)
1702 /* header.offset is relative to start of flash, not
1703 * start of region, so use it with the full image.
1704 */
1705 return (struct cbfs_file *)
1706 (buffer_get_original_backing(&image->buffer) +
1707 image->header.offset);
1708 else
1709 return (struct cbfs_file *)buffer_get(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001710}
1711
1712struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001713 struct cbfs_file *entry)
1714{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001715 uint32_t addr = cbfs_get_entry_addr(image, entry);
Julius Wernerd4775652020-03-13 16:43:34 -07001716 int align = image->has_header ? image->header.align : CBFS_ALIGNMENT;
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001717 assert(entry && cbfs_is_valid_entry(image, entry));
Alex James02001a382021-12-19 16:41:59 -06001718 addr += be32toh(entry->offset) + be32toh(entry->len);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001719 addr = align_up(addr, align);
1720 return (struct cbfs_file *)(image->buffer.data + addr);
1721}
1722
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001723uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
1724{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001725 assert(image && image->buffer.data && entry);
1726 return (int32_t)((char *)entry - image->buffer.data);
1727}
1728
Sol Boucher67a0a862015-03-18 12:36:27 -07001729int cbfs_is_valid_cbfs(struct cbfs_image *image)
1730{
1731 return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC,
1732 strlen(CBFS_FILE_MAGIC));
1733}
1734
1735int cbfs_is_legacy_cbfs(struct cbfs_image *image)
1736{
1737 return image->has_header;
1738}
1739
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001740int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
1741{
Sol Bouchere3260a02015-03-25 13:40:08 -07001742 uint32_t offset = cbfs_get_entry_addr(image, entry);
1743
1744 if (offset >= image->buffer.size)
1745 return 0;
1746
1747 struct buffer entry_data;
1748 buffer_clone(&entry_data, &image->buffer);
1749 buffer_seek(&entry_data, offset);
1750 return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
Sol Boucher67a0a862015-03-18 12:36:27 -07001751 strlen(CBFS_FILE_MAGIC));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001752}
1753
Patrick Georgi57edf162015-08-12 09:20:11 +02001754struct cbfs_file *cbfs_create_file_header(int type,
Vadim Bendebury45e59972014-12-23 15:59:57 -08001755 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001756{
Julius Wernerd4775652020-03-13 16:43:34 -07001757 struct cbfs_file *entry = malloc(CBFS_METADATA_MAX_SIZE);
1758 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, CBFS_METADATA_MAX_SIZE);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001759 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
Alex James02001a382021-12-19 16:41:59 -06001760 entry->type = htobe32(type);
1761 entry->len = htobe32(len);
Patrick Georgi0d618af2015-07-15 18:28:23 +02001762 entry->attributes_offset = 0;
Alex James02001a382021-12-19 16:41:59 -06001763 entry->offset = htobe32(cbfs_calculate_file_header_size(name));
1764 memset(entry->filename, 0, be32toh(entry->offset) - sizeof(*entry));
Patrick Georgic569b8b2015-07-15 16:42:38 +02001765 strcpy(entry->filename, name);
Patrick Georgi57edf162015-08-12 09:20:11 +02001766 return entry;
1767}
1768
1769int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
1770 size_t len, const char *name)
1771{
1772 struct cbfs_file *tmp = cbfs_create_file_header(type, len, name);
Alex James02001a382021-12-19 16:41:59 -06001773 memcpy(entry, tmp, be32toh(tmp->offset));
Patrick Georgi57edf162015-08-12 09:20:11 +02001774 free(tmp);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001775 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1776 return 0;
1777}
1778
Patrick Georgi2c615062015-07-15 20:49:00 +02001779struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file)
1780{
1781 /* attributes_offset should be 0 when there is no attribute, but all
1782 * values that point into the cbfs_file header are invalid, too. */
Alex James02001a382021-12-19 16:41:59 -06001783 if (be32toh(file->attributes_offset) <= sizeof(*file))
Patrick Georgi2c615062015-07-15 20:49:00 +02001784 return NULL;
1785
1786 /* There needs to be enough space for the file header and one
1787 * attribute header for this to make sense. */
Alex James02001a382021-12-19 16:41:59 -06001788 if (be32toh(file->offset) <=
Patrick Georgi2c615062015-07-15 20:49:00 +02001789 sizeof(*file) + sizeof(struct cbfs_file_attribute))
1790 return NULL;
1791
1792 return (struct cbfs_file_attribute *)
Alex James02001a382021-12-19 16:41:59 -06001793 (((uint8_t *)file) + be32toh(file->attributes_offset));
Patrick Georgi2c615062015-07-15 20:49:00 +02001794}
1795
1796struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
1797 struct cbfs_file_attribute *attr)
1798{
1799 /* ex falso sequitur quodlibet */
1800 if (attr == NULL)
1801 return NULL;
1802
1803 /* Is there enough space for another attribute? */
Alex James02001a382021-12-19 16:41:59 -06001804 if ((uint8_t *)attr + be32toh(attr->len) +
Patrick Rudolphe28fa402019-02-14 11:19:07 +01001805 sizeof(struct cbfs_file_attribute) >
Alex James02001a382021-12-19 16:41:59 -06001806 (uint8_t *)file + be32toh(file->offset))
Patrick Georgi2c615062015-07-15 20:49:00 +02001807 return NULL;
1808
1809 struct cbfs_file_attribute *next = (struct cbfs_file_attribute *)
Alex James02001a382021-12-19 16:41:59 -06001810 (((uint8_t *)attr) + be32toh(attr->len));
Patrick Georgi2c615062015-07-15 20:49:00 +02001811 /* If any, "unused" attributes must come last. */
Alex James02001a382021-12-19 16:41:59 -06001812 if (be32toh(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED)
Patrick Georgi2c615062015-07-15 20:49:00 +02001813 return NULL;
Alex James02001a382021-12-19 16:41:59 -06001814 if (be32toh(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
Patrick Georgi2c615062015-07-15 20:49:00 +02001815 return NULL;
1816
1817 return next;
1818}
1819
1820struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
1821 uint32_t tag,
1822 uint32_t size)
1823{
Julius Werner5779ca72020-11-20 16:12:40 -08001824 assert(IS_ALIGNED(size, CBFS_ATTRIBUTE_ALIGN));
Patrick Georgi2c615062015-07-15 20:49:00 +02001825 struct cbfs_file_attribute *attr, *next;
1826 next = cbfs_file_first_attr(header);
1827 do {
1828 attr = next;
1829 next = cbfs_file_next_attr(header, attr);
1830 } while (next != NULL);
Alex James02001a382021-12-19 16:41:59 -06001831 uint32_t header_size = be32toh(header->offset) + size;
Julius Wernerd4775652020-03-13 16:43:34 -07001832 if (header_size > CBFS_METADATA_MAX_SIZE) {
Patrick Georgi2c615062015-07-15 20:49:00 +02001833 DEBUG("exceeding allocated space for cbfs_file headers");
1834 return NULL;
1835 }
1836 /* attr points to the last valid attribute now.
1837 * If NULL, we have to create the first one. */
1838 if (attr == NULL) {
1839 /* New attributes start where the header ends.
Elyes HAOUAS3db01982018-08-23 18:08:20 +02001840 * header->offset is later set to accommodate the
Patrick Georgi2c615062015-07-15 20:49:00 +02001841 * additional structure.
Elyes HAOUAS3db01982018-08-23 18:08:20 +02001842 * No endianness translation necessary here, because both
Patrick Georgi2c615062015-07-15 20:49:00 +02001843 * fields are encoded the same way. */
1844 header->attributes_offset = header->offset;
1845 attr = (struct cbfs_file_attribute *)
1846 (((uint8_t *)header) +
Alex James02001a382021-12-19 16:41:59 -06001847 be32toh(header->attributes_offset));
Patrick Georgi2c615062015-07-15 20:49:00 +02001848 } else {
1849 attr = (struct cbfs_file_attribute *)
1850 (((uint8_t *)attr) +
Alex James02001a382021-12-19 16:41:59 -06001851 be32toh(attr->len));
Patrick Georgi2c615062015-07-15 20:49:00 +02001852 }
Alex James02001a382021-12-19 16:41:59 -06001853 header->offset = htobe32(header_size);
Julius Wernerd4775652020-03-13 16:43:34 -07001854 /* Attributes are expected to be small (much smaller than a flash page)
1855 and not really meant to be overwritten in-place. To avoid surprising
1856 values in reserved fields of attribute structures, initialize them to
1857 0, not 0xff. */
1858 memset(attr, 0, size);
Alex James02001a382021-12-19 16:41:59 -06001859 attr->tag = htobe32(tag);
1860 attr->len = htobe32(size);
Patrick Georgi2c615062015-07-15 20:49:00 +02001861 return attr;
1862}
1863
Patrick Georgi89f20342015-10-01 15:54:04 +02001864int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer,
Julius Wernerd4775652020-03-13 16:43:34 -07001865 enum vb2_hash_algorithm alg)
Patrick Georgi89f20342015-10-01 15:54:04 +02001866{
Julius Wernerd4775652020-03-13 16:43:34 -07001867 if (!vb2_digest_size(alg))
Patrick Georgi89f20342015-10-01 15:54:04 +02001868 return -1;
1869
Julius Wernerd4775652020-03-13 16:43:34 -07001870 struct cbfs_file_attr_hash *attr =
Patrick Georgi89f20342015-10-01 15:54:04 +02001871 (struct cbfs_file_attr_hash *)cbfs_add_file_attr(header,
Julius Wernerd4775652020-03-13 16:43:34 -07001872 CBFS_FILE_ATTR_TAG_HASH, cbfs_file_attr_hash_size(alg));
Patrick Georgi89f20342015-10-01 15:54:04 +02001873
Julius Wernerd4775652020-03-13 16:43:34 -07001874 if (attr == NULL)
Patrick Georgi89f20342015-10-01 15:54:04 +02001875 return -1;
1876
Julius Wernerd4775652020-03-13 16:43:34 -07001877 if (vb2_hash_calculate(buffer_get(buffer), buffer_size(buffer),
1878 alg, &attr->hash) != VB2_SUCCESS)
Patrick Georgi89f20342015-10-01 15:54:04 +02001879 return -1;
1880
1881 return 0;
1882}
1883
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001884/* Finds a place to hold whole data in same memory page. */
1885static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1886{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001887 if (!page)
1888 return 1;
1889 return (start / page) == (start + size - 1) / page;
1890}
1891
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001892/* Tests if data can fit in a range by given offset:
Aaron Durbind7339412015-09-15 12:50:14 -05001893 * start ->| metadata_size | offset (+ size) |<- end
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001894 */
Aaron Durbind7339412015-09-15 12:50:14 -05001895static int is_in_range(size_t start, size_t end, size_t metadata_size,
1896 size_t offset, size_t size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001897{
Aaron Durbind7339412015-09-15 12:50:14 -05001898 return (offset >= start + metadata_size && offset + size <= end);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001899}
1900
Werner Zeh95bfcae2016-01-25 12:47:20 +01001901static size_t absolute_align(const struct cbfs_image *image, size_t val,
1902 size_t align)
1903{
1904 const size_t region_offset = buffer_offset(&image->buffer);
1905 /* To perform alignment on absolute address, take the region offset */
1906 /* of the image into account. */
1907 return align_up(val + region_offset, align) - region_offset;
1908
1909}
1910
Aaron Durbind7339412015-09-15 12:50:14 -05001911int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size,
1912 size_t page_size, size_t align, size_t metadata_size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001913{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001914 struct cbfs_file *entry;
1915 size_t need_len;
Aaron Durbind7339412015-09-15 12:50:14 -05001916 size_t addr, addr_next, addr2, addr3, offset;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001917
1918 /* Default values: allow fitting anywhere in ROM. */
1919 if (!page_size)
Sol Boucher67a0a862015-03-18 12:36:27 -07001920 page_size = image->has_header ? image->header.romsize :
1921 image->buffer.size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001922 if (!align)
1923 align = 1;
1924
1925 if (size > page_size)
Aaron Durbind7339412015-09-15 12:50:14 -05001926 ERROR("Input file size (%zd) greater than page size (%zd).\n",
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001927 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001928
Aaron Durbind7339412015-09-15 12:50:14 -05001929 size_t image_align = image->has_header ? image->header.align :
Julius Wernerd4775652020-03-13 16:43:34 -07001930 CBFS_ALIGNMENT;
Sol Boucher67a0a862015-03-18 12:36:27 -07001931 if (page_size % image_align)
Aaron Durbind7339412015-09-15 12:50:14 -05001932 WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001933 __func__, page_size, image_align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001934
Aaron Durbind7339412015-09-15 12:50:14 -05001935 need_len = metadata_size + size;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001936
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001937 // Merge empty entries to build get max available space.
Julius Werner7066a1e2020-04-02 15:49:34 -07001938 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001939
1940 /* Three cases of content location on memory page:
1941 * case 1.
1942 * | PAGE 1 | PAGE 2 |
1943 * | <header><content>| Fit. Return start of content.
1944 *
1945 * case 2.
1946 * | PAGE 1 | PAGE 2 |
1947 * | <header><content> | Fits when we shift content to align
1948 * shift-> | <header>|<content> | at starting of PAGE 2.
1949 *
1950 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001951 * | PAGE 1 | PAGE 2 | PAGE 3 |
1952 * | <header>< content > | Can't fit. If we shift content to
1953 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1954 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001955 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001956 * The returned address can be then used as "base-address" (-b) in add-*
1957 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1958 * For stage targets, the address is also used to re-link stage before
1959 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001960 */
1961 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001962 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001963 entry = cbfs_find_next_entry(image, entry)) {
1964
Alex James02001a382021-12-19 16:41:59 -06001965 uint32_t type = be32toh(entry->type);
Julius Wernerd4775652020-03-13 16:43:34 -07001966 if (type != CBFS_TYPE_NULL)
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001967 continue;
1968
1969 addr = cbfs_get_entry_addr(image, entry);
1970 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
1971 image, entry));
1972 if (addr_next - addr < need_len)
1973 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001974
Werner Zeh95bfcae2016-01-25 12:47:20 +01001975 offset = absolute_align(image, addr + metadata_size, align);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001976 if (is_in_same_page(offset, size, page_size) &&
Aaron Durbind7339412015-09-15 12:50:14 -05001977 is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001978 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001979 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001980 }
1981
1982 addr2 = align_up(addr, page_size);
Werner Zeh95bfcae2016-01-25 12:47:20 +01001983 offset = absolute_align(image, addr2, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001984 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001985 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001986 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001987 }
1988
Aaron Durbind7339412015-09-15 12:50:14 -05001989 /* Assume page_size >= metadata_size so adding one page will
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001990 * definitely provide the space for header. */
Aaron Durbind7339412015-09-15 12:50:14 -05001991 assert(page_size >= metadata_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001992 addr3 = addr2 + page_size;
Werner Zeh95bfcae2016-01-25 12:47:20 +01001993 offset = absolute_align(image, addr3, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001994 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001995 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001996 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001997 }
1998 }
1999 return -1;
2000}