blob: 7a34bfe58f04ba70d537004eaf5f831ecfe1454a [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;
Jakub Czapigaaa415632022-08-01 16:01:28 +0200349 } else if (offset != HEADER_OFFSET_UNKNOWN) {
Sol Boucher67a0a862015-03-18 12:36:27 -0700350 ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +0800351 }
Patrick Georgi2f953d32015-09-11 18:34:39 +0200352 ERROR("Selected image region is not a valid CBFS.\n");
353 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800354}
355
Patrick Georgi214e4af2015-11-20 19:22:50 +0100356int cbfs_copy_instance(struct cbfs_image *image, struct buffer *dst)
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800357{
Sol Boucher67a0a862015-03-18 12:36:27 -0700358 assert(image);
Sol Boucher67a0a862015-03-18 12:36:27 -0700359
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800360 struct cbfs_file *src_entry, *dst_entry;
Patrick Georgibd0bb232015-11-20 21:48:18 +0100361 size_t align;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800362 ssize_t last_entry_size;
363
Patrick Georgi214e4af2015-11-20 19:22:50 +0100364 size_t copy_end = buffer_size(dst);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800365
Julius Wernerd4775652020-03-13 16:43:34 -0700366 align = CBFS_ALIGNMENT;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800367
Patrick Georgibd0bb232015-11-20 21:48:18 +0100368 dst_entry = (struct cbfs_file *)buffer_get(dst);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800369
370 /* Copy non-empty files */
371 for (src_entry = cbfs_find_first_entry(image);
372 src_entry && cbfs_is_valid_entry(image, src_entry);
373 src_entry = cbfs_find_next_entry(image, src_entry)) {
374 size_t entry_size;
375
Alex James02001a382021-12-19 16:41:59 -0600376 if ((src_entry->type == htobe32(CBFS_TYPE_NULL)) ||
377 (src_entry->type == htobe32(CBFS_TYPE_CBFSHEADER)) ||
378 (src_entry->type == htobe32(CBFS_TYPE_DELETED)))
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800379 continue;
380
Alex James02001a382021-12-19 16:41:59 -0600381 entry_size = htobe32(src_entry->len) + htobe32(src_entry->offset);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800382 memcpy(dst_entry, src_entry, entry_size);
383 dst_entry = (struct cbfs_file *)(
384 (uintptr_t)dst_entry + align_up(entry_size, align));
385
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700386 if ((size_t)((uint8_t *)dst_entry - (uint8_t *)buffer_get(dst))
387 >= copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800388 ERROR("Ran out of room in copy region.\n");
389 return 1;
390 }
391 }
392
Patrick Georgibd0bb232015-11-20 21:48:18 +0100393 /* Last entry size is all the room above it, except for top 4 bytes
394 * which may be used by the master header pointer. This messes with
395 * the ability to stash something "top-aligned" into the region, but
396 * keeps things simpler. */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700397 last_entry_size = copy_end -
398 ((uint8_t *)dst_entry - (uint8_t *)buffer_get(dst)) -
399 cbfs_calculate_file_header_size("") - sizeof(int32_t);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800400
401 if (last_entry_size < 0)
Fred Reitberger9049dfd2022-10-12 10:47:39 -0400402 WARN("No room to create the last entry!\n");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800403 else
Julius Wernerd4775652020-03-13 16:43:34 -0700404 cbfs_create_empty_entry(dst_entry, CBFS_TYPE_NULL,
Patrick Georgiedf25d92015-08-12 09:12:06 +0200405 last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800406
407 return 0;
408}
409
Patrick Georgi5d982d72017-09-19 14:39:58 +0200410int cbfs_expand_to_region(struct buffer *region)
411{
412 if (buffer_get(region) == NULL)
413 return 1;
414
415 struct cbfs_image image;
416 memset(&image, 0, sizeof(image));
Jakub Czapigaaa415632022-08-01 16:01:28 +0200417 if (cbfs_image_from_buffer(&image, region, HEADER_OFFSET_UNKNOWN)) {
Patrick Georgi5d982d72017-09-19 14:39:58 +0200418 ERROR("reading CBFS failed!\n");
419 return 1;
420 }
421
422 uint32_t region_sz = buffer_size(region);
423
424 struct cbfs_file *entry;
425 for (entry = buffer_get(region);
426 cbfs_is_valid_entry(&image, entry);
427 entry = cbfs_find_next_entry(&image, entry)) {
428 /* just iterate through */
429 }
430
431 /* entry now points to the first aligned address after the last valid
432 * file header. That's either outside the image or exactly the place
433 * where we need to create a new file.
434 */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700435 int last_entry_size = region_sz -
436 ((uint8_t *)entry - (uint8_t *)buffer_get(region)) -
437 cbfs_calculate_file_header_size("") - sizeof(int32_t);
Patrick Georgi5d982d72017-09-19 14:39:58 +0200438
439 if (last_entry_size > 0) {
Julius Wernerd4775652020-03-13 16:43:34 -0700440 cbfs_create_empty_entry(entry, CBFS_TYPE_NULL,
Patrick Georgi5d982d72017-09-19 14:39:58 +0200441 last_entry_size, "");
442 /* If the last entry was an empty file, merge them. */
Julius Werner7066a1e2020-04-02 15:49:34 -0700443 cbfs_legacy_walk(&image, cbfs_merge_empty_entry, NULL);
Patrick Georgi5d982d72017-09-19 14:39:58 +0200444 }
445
446 return 0;
447}
448
Patrick Georgi12631a42017-09-20 11:59:18 +0200449int cbfs_truncate_space(struct buffer *region, uint32_t *size)
450{
451 if (buffer_get(region) == NULL)
452 return 1;
453
454 struct cbfs_image image;
455 memset(&image, 0, sizeof(image));
Jakub Czapigaaa415632022-08-01 16:01:28 +0200456 if (cbfs_image_from_buffer(&image, region, HEADER_OFFSET_UNKNOWN)) {
Patrick Georgi12631a42017-09-20 11:59:18 +0200457 ERROR("reading CBFS failed!\n");
458 return 1;
459 }
460
461 struct cbfs_file *entry, *trailer;
462 for (trailer = entry = buffer_get(region);
463 cbfs_is_valid_entry(&image, entry);
464 trailer = entry,
465 entry = cbfs_find_next_entry(&image, entry)) {
466 /* just iterate through */
467 }
468
469 /* trailer now points to the last valid CBFS entry's header.
470 * If that file is empty, remove it and report its header's offset as
471 * maximum size.
472 */
473 if ((strlen(trailer->filename) != 0) &&
Alex James02001a382021-12-19 16:41:59 -0600474 (trailer->type != htobe32(CBFS_TYPE_NULL)) &&
475 (trailer->type != htobe32(CBFS_TYPE_DELETED))) {
Patrick Georgi12631a42017-09-20 11:59:18 +0200476 /* nothing to truncate. Return de-facto CBFS size in case it
477 * was already truncated. */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700478 *size = (uint8_t *)entry - (uint8_t *)buffer_get(region);
Patrick Georgi12631a42017-09-20 11:59:18 +0200479 return 0;
480 }
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700481 *size = (uint8_t *)trailer - (uint8_t *)buffer_get(region);
Patrick Georgi12631a42017-09-20 11:59:18 +0200482 memset(trailer, 0xff, buffer_size(region) - *size);
483
484 return 0;
485}
486
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600487static size_t cbfs_file_entry_metadata_size(const struct cbfs_file *f)
488{
Alex James02001a382021-12-19 16:41:59 -0600489 return be32toh(f->offset);
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600490}
491
492static size_t cbfs_file_entry_data_size(const struct cbfs_file *f)
493{
Alex James02001a382021-12-19 16:41:59 -0600494 return be32toh(f->len);
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600495}
496
497static size_t cbfs_file_entry_size(const struct cbfs_file *f)
498{
499 return cbfs_file_entry_metadata_size(f) + cbfs_file_entry_data_size(f);
500}
501
502int cbfs_compact_instance(struct cbfs_image *image)
503{
504 assert(image);
505
506 struct cbfs_file *prev;
507 struct cbfs_file *cur;
508
509 /* The prev entry will always be an empty entry. */
510 prev = NULL;
511
512 /*
513 * Note: this function does not honor alignment or fixed location files.
514 * It's behavior is akin to cbfs_copy_instance() in that it expects
515 * the caller to understand the ramifications of compacting a
516 * fragmented CBFS image.
517 */
518
519 for (cur = cbfs_find_first_entry(image);
520 cur && cbfs_is_valid_entry(image, cur);
521 cur = cbfs_find_next_entry(image, cur)) {
522 size_t prev_size;
523 size_t cur_size;
524 size_t empty_metadata_size;
525 size_t spill_size;
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600526
527 /* Current entry is empty. Kepp track of it. */
Alex Jamesb3398ba2022-01-08 01:29:29 -0600528 if (cur->type == CBFS_TYPE_NULL || cur->type == CBFS_TYPE_DELETED) {
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600529 prev = cur;
530 continue;
531 }
532
533 /* Need to ensure the previous entry is an empty one. */
534 if (prev == NULL)
535 continue;
536
537 /* At this point prev is an empty entry. Put the non-empty
Patrick Georgi01cfecc2020-01-29 13:31:16 +0100538 * file in prev's location. Then add a new empty entry. This
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600539 * essentialy bubbles empty entries towards the end. */
540
541 prev_size = cbfs_file_entry_size(prev);
542 cur_size = cbfs_file_entry_size(cur);
543
544 /*
545 * Adjust the empty file size by the actual space occupied
546 * bewtween the beginning of the empty file and the non-empty
547 * file.
548 */
549 prev_size += (cbfs_get_entry_addr(image, cur) -
550 cbfs_get_entry_addr(image, prev)) - prev_size;
551
552 /* Move the non-empty file over the empty file. */
553 memmove(prev, cur, cur_size);
554
555 /*
556 * Get location of the empty file. Note that since prev was
557 * overwritten with the non-empty file the previously moved
558 * file needs to be used to calculate the empty file's location.
559 */
560 cur = cbfs_find_next_entry(image, prev);
561
562 /*
563 * The total space to work with for swapping the 2 entries
564 * consists of the 2 files' sizes combined. However, the
565 * cbfs_file entries start on CBFS_ALIGNMENT boundaries.
566 * Because of this the empty file size may end up smaller
567 * because of the non-empty file's metadata and data length.
568 *
569 * Calculate the spill size which is the amount of data lost
570 * due to the alignment constraints after moving the non-empty
571 * file.
572 */
573 spill_size = (cbfs_get_entry_addr(image, cur) -
574 cbfs_get_entry_addr(image, prev)) - cur_size;
575
576 empty_metadata_size = cbfs_calculate_file_header_size("");
577
578 /* Check if new empty size can contain the metadata. */
579 if (empty_metadata_size + spill_size > prev_size) {
580 ERROR("Unable to swap '%s' with prev empty entry.\n",
581 prev->filename);
582 return 1;
583 }
584
585 /* Update the empty file's size. */
586 prev_size -= spill_size + empty_metadata_size;
587
588 /* Create new empty file. */
Julius Wernerd4775652020-03-13 16:43:34 -0700589 cbfs_create_empty_entry(cur, CBFS_TYPE_NULL,
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600590 prev_size, "");
591
592 /* Merge any potential empty entries together. */
Julius Werner7066a1e2020-04-02 15:49:34 -0700593 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600594
595 /*
596 * Since current switched to an empty file keep track of it.
597 * Even if any empty files were merged the empty entry still
598 * starts at previously calculated location.
599 */
600 prev = cur;
601 }
602
603 return 0;
604}
605
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700606int cbfs_image_delete(struct cbfs_image *image)
607{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100608 if (image == NULL)
609 return 0;
610
Hung-Te Lineab2c812013-01-29 01:56:17 +0800611 buffer_delete(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800612 return 0;
613}
614
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800615/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
616static int cbfs_add_entry_at(struct cbfs_image *image,
617 struct cbfs_file *entry,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800618 const void *data,
Patrick Georgi7fd14182015-08-11 15:55:16 +0200619 uint32_t content_offset,
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100620 const struct cbfs_file *header,
621 const size_t len_align)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700622{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800623 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
624 uint32_t addr = cbfs_get_entry_addr(image, entry),
625 addr_next = cbfs_get_entry_addr(image, next);
Patrick Georgi7fd14182015-08-11 15:55:16 +0200626 uint32_t min_entry_size = cbfs_calculate_file_header_size("");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200627 uint32_t len, header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700628 uint32_t align = image->has_header ? image->header.align :
Julius Wernerd4775652020-03-13 16:43:34 -0700629 CBFS_ALIGNMENT;
Alex James02001a382021-12-19 16:41:59 -0600630 uint32_t header_size = be32toh(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800631
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200632 header_offset = content_offset - header_size;
633 if (header_offset % align)
634 header_offset -= header_offset % align;
635 if (header_offset < addr) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800636 ERROR("No space to hold cbfs_file header.");
637 return -1;
638 }
639
640 // Process buffer BEFORE content_offset.
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200641 if (header_offset - addr > min_entry_size) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800642 DEBUG("|min|...|header|content|... <create new entry>\n");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200643 len = header_offset - addr - min_entry_size;
Julius Wernerd4775652020-03-13 16:43:34 -0700644 cbfs_create_empty_entry(entry, CBFS_TYPE_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800645 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
646 entry = cbfs_find_next_entry(image, entry);
647 addr = cbfs_get_entry_addr(image, entry);
648 }
649
Patrick Georgi7a33b532015-08-25 13:00:04 +0200650 len = content_offset - addr - header_size;
Patrick Georgia60e7b62015-08-25 22:26:02 +0200651 memcpy(entry, header, header_size);
Patrick Georgi7a33b532015-08-25 13:00:04 +0200652 if (len != 0) {
Julius Wernerf0cc7ad2020-11-18 18:31:22 -0800653 /*
654 * The header moved backwards a bit to accommodate cbfs_file
Patrick Georgi7a33b532015-08-25 13:00:04 +0200655 * alignment requirements, so patch up ->offset to still point
Julius Wernerf0cc7ad2020-11-18 18:31:22 -0800656 * to file data. Move attributes forward so the end of the
657 * attribute list still matches the end of the metadata.
Patrick Georgi7a33b532015-08-25 13:00:04 +0200658 */
Alex James02001a382021-12-19 16:41:59 -0600659 uint32_t offset = be32toh(entry->offset);
660 uint32_t attrs = be32toh(entry->attributes_offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800661 DEBUG("|..|header|content|... <use offset to create entry>\n");
Julius Wernerf0cc7ad2020-11-18 18:31:22 -0800662 DEBUG("before: attr_offset=0x%x, offset=0x%x\n", attrs, offset);
663 if (attrs == 0) {
664 memset((uint8_t *)entry + offset, 0, len);
665 } else {
666 uint8_t *p = (uint8_t *)entry + attrs;
667 memmove(p + len, p, offset - attrs);
668 memset(p, 0, len);
669 attrs += len;
Alex James02001a382021-12-19 16:41:59 -0600670 entry->attributes_offset = htobe32(attrs);
Julius Wernerf0cc7ad2020-11-18 18:31:22 -0800671 }
672 offset += len;
Alex James02001a382021-12-19 16:41:59 -0600673 entry->offset = htobe32(offset);
Julius Wernerf0cc7ad2020-11-18 18:31:22 -0800674 DEBUG("after: attr_offset=0x%x, offset=0x%x\n", attrs, offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800675 }
676
677 // Ready to fill data into entry.
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800678 DEBUG("content_offset: 0x%x, entry location: %x\n",
679 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
680 image->buffer.data));
681 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
Patrick Georgicccc9d42015-04-28 13:09:36 +0200682 (ptrdiff_t)content_offset);
Alex James02001a382021-12-19 16:41:59 -0600683 memcpy(CBFS_SUBHEADER(entry), data, be32toh(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800684 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
685
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100686 // Align the length to a multiple of len_align
687 if (len_align &&
Alex James02001a382021-12-19 16:41:59 -0600688 ((be32toh(entry->offset) + be32toh(entry->len)) % len_align)) {
689 size_t off = (be32toh(entry->offset) + be32toh(entry->len)) % len_align;
690 entry->len = htobe32(be32toh(entry->len) + len_align - off);
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100691 }
692
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800693 // Process buffer AFTER entry.
694 entry = cbfs_find_next_entry(image, entry);
695 addr = cbfs_get_entry_addr(image, entry);
Sol Boucher05725652015-04-02 20:58:26 -0700696 if (addr == addr_next)
697 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800698
Sol Boucher05725652015-04-02 20:58:26 -0700699 assert(addr < addr_next);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800700 if (addr_next - addr < min_entry_size) {
Sol Boucher636cc852015-04-03 09:13:04 -0700701 DEBUG("No need for new \"empty\" entry\n");
702 /* No need to increase the size of the just
703 * stored file to extend to next file. Alignment
704 * of next file takes care of this.
705 */
706 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800707 }
708
709 len = addr_next - addr - min_entry_size;
Patrick Georgi29a04d92015-11-20 23:23:44 +0100710 /* keep space for master header pointer */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700711 if ((uint8_t *)entry + min_entry_size + len >
712 (uint8_t *)buffer_get(&image->buffer) +
713 buffer_size(&image->buffer) - sizeof(int32_t)) {
Patrick Georgi29a04d92015-11-20 23:23:44 +0100714 len -= sizeof(int32_t);
715 }
Julius Wernerd4775652020-03-13 16:43:34 -0700716 cbfs_create_empty_entry(entry, CBFS_TYPE_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800717 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
718 return 0;
719}
720
721int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Patrick Georgie5903582015-08-25 13:53:42 +0200722 uint32_t content_offset,
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100723 struct cbfs_file *header,
724 const size_t len_align)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700725{
Sol Boucher67d59982015-05-07 02:39:22 -0700726 assert(image);
727 assert(buffer);
728 assert(buffer->data);
Furquan Shaikh19ba95f2020-11-20 22:50:26 -0800729 assert(!IS_HOST_SPACE_ADDRESS(content_offset));
Sol Boucher67d59982015-05-07 02:39:22 -0700730
Patrick Georgia60e7b62015-08-25 22:26:02 +0200731 const char *name = header->filename;
Patrick Georgie5903582015-08-25 13:53:42 +0200732
Julius Wernerea4d4c92023-04-11 17:01:00 -0700733 /* This is so special rows in cbfstool print -k -v output stay unambiguous. */
734 if (name[0] == '[') {
735 ERROR("CBFS file name `%s` must not start with `[`\n", name);
736 return -1;
737 }
738
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800739 uint32_t entry_type;
740 uint32_t addr, addr_next;
741 struct cbfs_file *entry, *next;
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200742 uint32_t need_size;
Alex James02001a382021-12-19 16:41:59 -0600743 uint32_t header_size = be32toh(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800744
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800745 need_size = header_size + buffer->size;
746 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
747 name, content_offset, header_size, buffer->size, need_size);
748
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800749 // Merge empty entries.
750 DEBUG("(trying to merge empty entries...)\n");
Julius Werner7066a1e2020-04-02 15:49:34 -0700751 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800752
753 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800754 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800755 entry = cbfs_find_next_entry(image, entry)) {
756
Alex James02001a382021-12-19 16:41:59 -0600757 entry_type = be32toh(entry->type);
Julius Wernerd4775652020-03-13 16:43:34 -0700758 if (entry_type != CBFS_TYPE_NULL)
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800759 continue;
760
761 addr = cbfs_get_entry_addr(image, entry);
762 next = cbfs_find_next_entry(image, entry);
763 addr_next = cbfs_get_entry_addr(image, next);
764
765 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
766 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600767
768 /* Will the file fit? Don't yet worry if we have space for a new
769 * "empty" entry. We take care of that later.
770 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800771 if (addr + need_size > addr_next)
772 continue;
773
Patrick Georgiaa44dbd2015-08-12 12:05:21 +0200774 // Test for complicated cases
775 if (content_offset > 0) {
776 if (addr_next < content_offset) {
777 DEBUG("Not for specified offset yet");
778 continue;
779 } else if (addr > content_offset) {
780 DEBUG("Exceed specified content_offset.");
781 break;
782 } else if (addr + header_size > content_offset) {
783 ERROR("Not enough space for header.\n");
784 break;
785 } else if (content_offset + buffer->size > addr_next) {
786 ERROR("Not enough space for content.\n");
787 break;
788 }
789 }
790
791 // TODO there are more few tricky cases that we may
792 // want to fit by altering offset.
793
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200794 if (content_offset == 0) {
795 // we tested every condition earlier under which
796 // placing the file there might fail
797 content_offset = addr + header_size;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800798 }
799
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800800 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
801 addr, addr_next - addr, content_offset);
802
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200803 if (cbfs_add_entry_at(image, entry, buffer->data,
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100804 content_offset, header, len_align) == 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800805 return 0;
806 }
807 break;
808 }
809
810 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
811 buffer->name, buffer->size, buffer->size / 1024, content_offset);
812 return -1;
813}
814
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700815struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
816{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800817 struct cbfs_file *entry;
818 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800819 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800820 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgic569b8b2015-07-15 16:42:38 +0200821 if (strcasecmp(entry->filename, name) == 0) {
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800822 DEBUG("cbfs_get_entry: found %s\n", name);
823 return entry;
824 }
825 }
826 return NULL;
827}
828
Antonello Dettorifda691e2016-06-09 12:35:36 +0200829static int cbfs_payload_decompress(struct cbfs_payload_segment *segments,
830 struct buffer *buff, int num_seg)
831{
832 struct buffer new_buffer;
833 struct buffer seg_buffer;
834 size_t new_buff_sz;
835 char *in_ptr;
836 char *out_ptr;
837 size_t new_offset;
838 decomp_func_ptr decompress;
839
840 new_offset = num_seg * sizeof(*segments);
841 new_buff_sz = num_seg * sizeof(*segments);
842
843 /* Find out and allocate the amount of memory occupied
844 * by the binary data */
845 for (int i = 0; i < num_seg; i++)
846 new_buff_sz += segments[i].mem_len;
847
Furquan Shaikh58644a02016-08-05 08:27:18 -0700848 if (buffer_create(&new_buffer, new_buff_sz, "decompressed_buff"))
849 return -1;
Antonello Dettorifda691e2016-06-09 12:35:36 +0200850
851 in_ptr = buffer_get(buff) + new_offset;
852 out_ptr = buffer_get(&new_buffer) + new_offset;
853
854 for (int i = 0; i < num_seg; i++) {
855 struct buffer tbuff;
856 size_t decomp_size;
857
Antonello Dettorifda691e2016-06-09 12:35:36 +0200858 /* Segments BSS and ENTRY do not have binary data. */
859 if (segments[i].type == PAYLOAD_SEGMENT_BSS ||
860 segments[i].type == PAYLOAD_SEGMENT_ENTRY) {
861 continue;
862 } else if (segments[i].type == PAYLOAD_SEGMENT_PARAMS) {
863 memcpy(out_ptr, in_ptr, segments[i].len);
864 segments[i].offset = new_offset;
865 new_offset += segments[i].len;
866 in_ptr += segments[i].len;
867 out_ptr += segments[i].len;
868 segments[i].compression = CBFS_COMPRESS_NONE;
869 continue;
870 }
871
Joel Kitching72d77a92018-07-18 13:23:52 +0800872 /* The payload uses an unknown compression algorithm. */
873 decompress = decompression_function(segments[i].compression);
874 if (decompress == NULL) {
875 ERROR("Unknown decompression algorithm: %u\n",
876 segments[i].compression);
877 return -1;
878 }
879
Furquan Shaikh58644a02016-08-05 08:27:18 -0700880 if (buffer_create(&tbuff, segments[i].mem_len, "segment")) {
881 buffer_delete(&new_buffer);
882 return -1;
883 }
Antonello Dettorifda691e2016-06-09 12:35:36 +0200884
885 if (decompress(in_ptr, segments[i].len, buffer_get(&tbuff),
886 (int) buffer_size(&tbuff),
887 &decomp_size)) {
888 ERROR("Couldn't decompress payload segment %u\n", i);
889 buffer_delete(&new_buffer);
Furquan Shaikh9844d562016-08-05 08:32:23 -0700890 buffer_delete(&tbuff);
Antonello Dettorifda691e2016-06-09 12:35:36 +0200891 return -1;
892 }
893
894 memcpy(out_ptr, buffer_get(&tbuff), decomp_size);
895
896 in_ptr += segments[i].len;
897
898 /* Update the offset of the segment. */
899 segments[i].offset = new_offset;
900 /* True decompressed size is just the data size. No metadata */
901 segments[i].len = decomp_size;
902 /* Segment is not compressed. */
903 segments[i].compression = CBFS_COMPRESS_NONE;
904
905 /* Update the offset and output buffer pointer. */
906 new_offset += decomp_size;
907 out_ptr += decomp_size;
908
909 buffer_delete(&tbuff);
910 }
911
912 buffer_splice(&seg_buffer, &new_buffer, 0, 0);
913 xdr_segs(&seg_buffer, segments, num_seg);
914
915 buffer_delete(buff);
916 *buff = new_buffer;
917
918 return 0;
919}
920
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500921static int init_elf_from_arch(Elf64_Ehdr *ehdr, uint32_t cbfs_arch)
922{
923 int endian;
924 int nbits;
925 int machine;
926
927 switch (cbfs_arch) {
928 case CBFS_ARCHITECTURE_X86:
929 endian = ELFDATA2LSB;
930 nbits = ELFCLASS32;
931 machine = EM_386;
932 break;
933 case CBFS_ARCHITECTURE_ARM:
934 endian = ELFDATA2LSB;
935 nbits = ELFCLASS32;
936 machine = EM_ARM;
937 break;
938 case CBFS_ARCHITECTURE_AARCH64:
939 endian = ELFDATA2LSB;
940 nbits = ELFCLASS64;
941 machine = EM_AARCH64;
942 break;
943 case CBFS_ARCHITECTURE_MIPS:
944 endian = ELFDATA2LSB;
945 nbits = ELFCLASS32;
946 machine = EM_MIPS;
947 break;
948 case CBFS_ARCHITECTURE_RISCV:
949 endian = ELFDATA2LSB;
950 nbits = ELFCLASS32;
951 machine = EM_RISCV;
952 break;
953 default:
954 ERROR("Unsupported arch: %x\n", cbfs_arch);
955 return -1;
956 }
957
958 elf_init_eheader(ehdr, machine, nbits, endian);
959 return 0;
960}
961
Julius Werner81dc20e2020-10-15 17:37:57 -0700962static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch,
963 struct cbfs_file *entry)
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500964{
965 Elf64_Ehdr ehdr;
966 Elf64_Shdr shdr;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500967 struct elf_writer *ew;
968 struct buffer elf_out;
969 size_t empty_sz;
Aaron Durbin694fd132015-10-28 11:39:34 -0500970 int rmod_ret;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500971
Antonello Dettori0b806282016-06-26 00:24:25 +0200972 if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
973 ERROR("You need to specify -m ARCH.\n");
974 return -1;
975 }
976
Julius Werner81dc20e2020-10-15 17:37:57 -0700977 struct cbfs_file_attr_stageheader *stage = NULL;
978 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
979 attr != NULL; attr = cbfs_file_next_attr(entry, attr)) {
Alex James02001a382021-12-19 16:41:59 -0600980 if (be32toh(attr->tag) == CBFS_FILE_ATTR_TAG_STAGEHEADER) {
Julius Werner81dc20e2020-10-15 17:37:57 -0700981 stage = (struct cbfs_file_attr_stageheader *)attr;
982 break;
983 }
984 }
985
986 if (stage == NULL) {
987 ERROR("Stage header not found for %s\n", entry->filename);
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500988 return -1;
989 }
990
991 if (init_elf_from_arch(&ehdr, arch))
992 return -1;
993
Aaron Durbin694fd132015-10-28 11:39:34 -0500994 /* Attempt rmodule translation first. */
995 rmod_ret = rmodule_stage_to_elf(&ehdr, buff);
996
997 if (rmod_ret < 0) {
998 ERROR("rmodule parsing failed\n");
999 return -1;
1000 } else if (rmod_ret == 0)
1001 return 0;
1002
1003 /* Rmodule couldn't do anything with the data. Continue on with SELF. */
1004
Alex James02001a382021-12-19 16:41:59 -06001005 ehdr.e_entry = be64toh(stage->loadaddr) + be32toh(stage->entry_offset);
Julius Werner81dc20e2020-10-15 17:37:57 -07001006
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001007 ew = elf_writer_init(&ehdr);
1008 if (ew == NULL) {
1009 ERROR("Unable to init ELF writer.\n");
1010 return -1;
1011 }
1012
1013 memset(&shdr, 0, sizeof(shdr));
1014 shdr.sh_type = SHT_PROGBITS;
1015 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
Alex James02001a382021-12-19 16:41:59 -06001016 shdr.sh_addr = be64toh(stage->loadaddr);
Julius Werner81dc20e2020-10-15 17:37:57 -07001017 shdr.sh_size = buffer_size(buff);
Alex James02001a382021-12-19 16:41:59 -06001018 empty_sz = be32toh(stage->memlen) - buffer_size(buff);
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001019
1020 if (elf_writer_add_section(ew, &shdr, buff, ".program")) {
1021 ERROR("Unable to add ELF section: .program\n");
1022 elf_writer_destroy(ew);
1023 return -1;
1024 }
1025
1026 if (empty_sz != 0) {
1027 struct buffer b;
1028
1029 buffer_init(&b, NULL, NULL, 0);
1030 memset(&shdr, 0, sizeof(shdr));
1031 shdr.sh_type = SHT_NOBITS;
1032 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
Alex Jamesb3398ba2022-01-08 01:29:29 -06001033 shdr.sh_addr = be64toh(stage->loadaddr) + buffer_size(buff);
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001034 shdr.sh_size = empty_sz;
1035 if (elf_writer_add_section(ew, &shdr, &b, ".empty")) {
1036 ERROR("Unable to add ELF section: .empty\n");
1037 elf_writer_destroy(ew);
1038 return -1;
1039 }
1040 }
1041
1042 if (elf_writer_serialize(ew, &elf_out)) {
1043 ERROR("Unable to create ELF file from stage.\n");
1044 elf_writer_destroy(ew);
1045 return -1;
1046 }
1047
1048 /* Flip buffer with the created ELF one. */
1049 buffer_delete(buff);
1050 *buff = elf_out;
1051
1052 elf_writer_destroy(ew);
1053
1054 return 0;
1055}
1056
Julius Werner81dc20e2020-10-15 17:37:57 -07001057static int cbfs_payload_make_elf(struct buffer *buff, uint32_t arch,
1058 unused struct cbfs_file *entry)
Antonello Dettorifda691e2016-06-09 12:35:36 +02001059{
1060 Elf64_Ehdr ehdr;
1061 Elf64_Shdr shdr;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001062 struct cbfs_payload_segment *segs = NULL;
Jonathan Neuschäfer5de54582016-08-09 15:01:58 +02001063 struct elf_writer *ew = NULL;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001064 struct buffer elf_out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001065 int segments = 0;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001066 int retval = -1;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001067
Antonello Dettori0b806282016-06-26 00:24:25 +02001068 if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
1069 ERROR("You need to specify -m ARCH.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001070 goto out;
Antonello Dettori0b806282016-06-26 00:24:25 +02001071 }
1072
Antonello Dettorifda691e2016-06-09 12:35:36 +02001073 /* Count the number of segments inside buffer */
1074 while (true) {
1075 uint32_t payload_type = 0;
1076
1077 struct cbfs_payload_segment *seg;
1078
1079 seg = buffer_get(buff);
1080 payload_type = read_be32(&seg[segments].type);
1081
1082 if (payload_type == PAYLOAD_SEGMENT_CODE) {
1083 segments++;
1084 } else if (payload_type == PAYLOAD_SEGMENT_DATA) {
1085 segments++;
1086 } else if (payload_type == PAYLOAD_SEGMENT_BSS) {
1087 segments++;
1088 } else if (payload_type == PAYLOAD_SEGMENT_PARAMS) {
1089 segments++;
1090 } else if (payload_type == PAYLOAD_SEGMENT_ENTRY) {
1091 /* The last segment in a payload is always ENTRY as
1092 * specified by the parse_elf_to_payload() function.
1093 * Therefore there is no need to continue looking for
1094 * segments.*/
1095 segments++;
1096 break;
1097 } else {
1098 ERROR("Unknown payload segment type: %x\n",
1099 payload_type);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001100 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001101 }
1102 }
1103
1104 segs = malloc(segments * sizeof(*segs));
1105
1106 /* Decode xdr segments */
1107 for (int i = 0; i < segments; i++) {
1108 struct cbfs_payload_segment *serialized_seg = buffer_get(buff);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001109 xdr_get_seg(&segs[i], &serialized_seg[i]);
1110 }
1111
1112 if (cbfs_payload_decompress(segs, buff, segments)) {
1113 ERROR("Failed to decompress payload.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001114 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001115 }
1116
1117 if (init_elf_from_arch(&ehdr, arch))
Furquan Shaikh7b405172016-08-05 08:20:37 -07001118 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001119
1120 ehdr.e_entry = segs[segments-1].load_addr;
1121
1122 ew = elf_writer_init(&ehdr);
1123 if (ew == NULL) {
1124 ERROR("Unable to init ELF writer.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001125 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001126 }
1127
1128 for (int i = 0; i < segments; i++) {
1129 struct buffer tbuff;
Furquan Shaikhf3bba442016-08-05 08:12:31 -07001130 size_t empty_sz = 0;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001131
1132 memset(&shdr, 0, sizeof(shdr));
1133 char *name = NULL;
1134
1135 if (segs[i].type == PAYLOAD_SEGMENT_CODE) {
1136 shdr.sh_type = SHT_PROGBITS;
1137 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
1138 shdr.sh_addr = segs[i].load_addr;
1139 shdr.sh_size = segs[i].len;
1140 empty_sz = segs[i].mem_len - segs[i].len;
1141 name = strdup(".text");
1142 buffer_splice(&tbuff, buff, segs[i].offset,
1143 segs[i].len);
1144 } else if (segs[i].type == PAYLOAD_SEGMENT_DATA) {
1145 shdr.sh_type = SHT_PROGBITS;
1146 shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
1147 shdr.sh_addr = segs[i].load_addr;
1148 shdr.sh_size = segs[i].len;
1149 empty_sz = segs[i].mem_len - segs[i].len;
1150 name = strdup(".data");
1151 buffer_splice(&tbuff, buff, segs[i].offset,
1152 segs[i].len);
1153 } else if (segs[i].type == PAYLOAD_SEGMENT_BSS) {
1154 shdr.sh_type = SHT_NOBITS;
1155 shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
1156 shdr.sh_addr = segs[i].load_addr;
1157 shdr.sh_size = segs[i].len;
1158 name = strdup(".bss");
1159 buffer_splice(&tbuff, buff, 0, 0);
1160 } else if (segs[i].type == PAYLOAD_SEGMENT_PARAMS) {
1161 shdr.sh_type = SHT_NOTE;
1162 shdr.sh_flags = 0;
1163 shdr.sh_size = segs[i].len;
1164 name = strdup(".note.pinfo");
1165 buffer_splice(&tbuff, buff, segs[i].offset,
1166 segs[i].len);
1167 } else if (segs[i].type == PAYLOAD_SEGMENT_ENTRY) {
1168 break;
Patrick Georgidce629b2017-01-13 13:30:54 +01001169 } else {
1170 ERROR("unknown ELF segment type\n");
1171 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001172 }
1173
Patrick Georgidce629b2017-01-13 13:30:54 +01001174 if (!name) {
1175 ERROR("out of memory\n");
1176 goto out;
1177 }
Antonello Dettorifda691e2016-06-09 12:35:36 +02001178
1179 if (elf_writer_add_section(ew, &shdr, &tbuff, name)) {
1180 ERROR("Unable to add ELF section: %s\n", name);
Patrick Georgidce629b2017-01-13 13:30:54 +01001181 free(name);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001182 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001183 }
Patrick Georgidce629b2017-01-13 13:30:54 +01001184 free(name);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001185
1186 if (empty_sz != 0) {
1187 struct buffer b;
1188
1189 buffer_init(&b, NULL, NULL, 0);
1190 memset(&shdr, 0, sizeof(shdr));
1191 shdr.sh_type = SHT_NOBITS;
1192 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
1193 shdr.sh_addr = segs[i].load_addr + segs[i].len;
1194 shdr.sh_size = empty_sz;
1195 name = strdup(".empty");
Patrick Georgidce629b2017-01-13 13:30:54 +01001196 if (!name) {
1197 ERROR("out of memory\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001198 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001199 }
Patrick Georgidce629b2017-01-13 13:30:54 +01001200 if (elf_writer_add_section(ew, &shdr, &b, name)) {
1201 ERROR("Unable to add ELF section: %s\n", name);
1202 free(name);
1203 goto out;
1204 }
1205 free(name);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001206 }
Antonello Dettorifda691e2016-06-09 12:35:36 +02001207 }
1208
1209 if (elf_writer_serialize(ew, &elf_out)) {
Julius Werner81dc20e2020-10-15 17:37:57 -07001210 ERROR("Unable to create ELF file from payload.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001211 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001212 }
1213
1214 /* Flip buffer with the created ELF one. */
1215 buffer_delete(buff);
1216 *buff = elf_out;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001217 retval = 0;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001218
Furquan Shaikh7b405172016-08-05 08:20:37 -07001219out:
1220 free(segs);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001221 elf_writer_destroy(ew);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001222 return retval;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001223}
1224
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001225int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Joel Kitching21fdd892018-08-09 17:49:52 +08001226 const char *filename, uint32_t arch, bool do_processing)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001227{
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001228 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
1229 struct buffer buffer;
1230 if (!entry) {
1231 ERROR("File not found: %s\n", entry_name);
1232 return -1;
1233 }
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001234
Alex James02001a382021-12-19 16:41:59 -06001235 unsigned int compressed_size = be32toh(entry->len);
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001236 unsigned int decompressed_size = 0;
1237 unsigned int compression = cbfs_file_get_compression_info(entry,
1238 &decompressed_size);
Werner Zehbbf1df72018-11-21 13:07:50 +01001239 unsigned int buffer_len;
Joel Kitching21fdd892018-08-09 17:49:52 +08001240 decomp_func_ptr decompress;
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001241
Joel Kitching21fdd892018-08-09 17:49:52 +08001242 if (do_processing) {
1243 decompress = decompression_function(compression);
1244 if (!decompress) {
1245 ERROR("looking up decompression routine failed\n");
1246 return -1;
1247 }
Werner Zehbbf1df72018-11-21 13:07:50 +01001248 buffer_len = decompressed_size;
Joel Kitching21fdd892018-08-09 17:49:52 +08001249 } else {
1250 /* Force nop decompression */
1251 decompress = decompression_function(CBFS_COMPRESS_NONE);
Werner Zehbbf1df72018-11-21 13:07:50 +01001252 buffer_len = compressed_size;
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001253 }
1254
Joel Kitching21fdd892018-08-09 17:49:52 +08001255 LOG("Found file %.30s at 0x%x, type %.12s, compressed %d, size %d\n",
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001256 entry_name, cbfs_get_entry_addr(image, entry),
Alex James02001a382021-12-19 16:41:59 -06001257 get_cbfs_entry_type_name(be32toh(entry->type)), compressed_size,
Joel Kitching21fdd892018-08-09 17:49:52 +08001258 decompressed_size);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001259
Aaron Durbin539aed02015-10-23 17:42:32 -05001260 buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0);
Werner Zehbbf1df72018-11-21 13:07:50 +01001261 buffer.data = malloc(buffer_len);
1262 buffer.size = buffer_len;
Aaron Durbin539aed02015-10-23 17:42:32 -05001263
Joel Kitching21fdd892018-08-09 17:49:52 +08001264 if (decompress(CBFS_SUBHEADER(entry), compressed_size,
1265 buffer.data, buffer.size, NULL)) {
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001266 ERROR("decompression failed for %s\n", entry_name);
Aaron Durbin539aed02015-10-23 17:42:32 -05001267 buffer_delete(&buffer);
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001268 return -1;
1269 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001270
1271 /*
Julius Werner81dc20e2020-10-15 17:37:57 -07001272 * We want to export stages and payloads as ELFs, not with coreboot's
1273 * custom stage/SELF binary formats, so we need to do extra processing
1274 * to turn them back into an ELF.
Aaron Durbin539aed02015-10-23 17:42:32 -05001275 */
Joel Kitching21fdd892018-08-09 17:49:52 +08001276 if (do_processing) {
Julius Werner81dc20e2020-10-15 17:37:57 -07001277 int (*make_elf)(struct buffer *, uint32_t,
1278 struct cbfs_file *) = NULL;
Alex James02001a382021-12-19 16:41:59 -06001279 switch (be32toh(entry->type)) {
Julius Wernerd4775652020-03-13 16:43:34 -07001280 case CBFS_TYPE_STAGE:
Joel Kitching21fdd892018-08-09 17:49:52 +08001281 make_elf = cbfs_stage_make_elf;
1282 break;
Julius Wernerd4775652020-03-13 16:43:34 -07001283 case CBFS_TYPE_SELF:
Joel Kitching21fdd892018-08-09 17:49:52 +08001284 make_elf = cbfs_payload_make_elf;
1285 break;
Aaron Durbin539aed02015-10-23 17:42:32 -05001286 }
Julius Werner81dc20e2020-10-15 17:37:57 -07001287 if (make_elf && make_elf(&buffer, arch, entry)) {
Joel Kitching21fdd892018-08-09 17:49:52 +08001288 ERROR("Failed to write %s into %s.\n",
1289 entry_name, filename);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001290 buffer_delete(&buffer);
1291 return -1;
1292 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001293 }
1294
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001295 if (buffer_write_file(&buffer, filename) != 0) {
1296 ERROR("Failed to write %s into %s.\n",
1297 entry_name, filename);
Aaron Durbin539aed02015-10-23 17:42:32 -05001298 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001299 return -1;
1300 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001301
1302 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001303 INFO("Successfully dumped the file to: %s\n", filename);
1304 return 0;
1305}
1306
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001307int cbfs_remove_entry(struct cbfs_image *image, const char *name)
1308{
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +02001309 struct cbfs_file *entry;
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001310 entry = cbfs_get_entry(image, name);
1311 if (!entry) {
1312 ERROR("CBFS file %s not found.\n", name);
1313 return -1;
1314 }
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001315 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
Patrick Georgic569b8b2015-07-15 16:42:38 +02001316 entry->filename, cbfs_get_entry_addr(image, entry));
Alex James02001a382021-12-19 16:41:59 -06001317 entry->type = htobe32(CBFS_TYPE_DELETED);
Julius Werner7066a1e2020-04-02 15:49:34 -07001318 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001319 return 0;
1320}
1321
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001322int cbfs_print_header_info(struct cbfs_image *image)
1323{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001324 char *name = strdup(image->buffer.name);
Sol Boucher3e060ed2015-05-05 15:40:15 -07001325 assert(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001326 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -08001327 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001328 basename(name),
1329 image->buffer.size / 1024,
Sol Boucher3e060ed2015-05-05 15:40:15 -07001330 image->header.bootblocksize,
1331 image->header.romsize,
1332 image->header.offset,
1333 image->header.align,
1334 arch_to_string(image->header.architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001335 free(name);
1336 return 0;
1337}
1338
Julius Werner81dc20e2020-10-15 17:37:57 -07001339static int cbfs_print_stage_info(struct cbfs_file *entry, FILE* fp)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001340{
Julius Werner81dc20e2020-10-15 17:37:57 -07001341
1342 struct cbfs_file_attr_stageheader *stage = NULL;
1343 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
1344 attr != NULL; attr = cbfs_file_next_attr(entry, attr)) {
Alex James02001a382021-12-19 16:41:59 -06001345 if (be32toh(attr->tag) == CBFS_FILE_ATTR_TAG_STAGEHEADER) {
Julius Werner81dc20e2020-10-15 17:37:57 -07001346 stage = (struct cbfs_file_attr_stageheader *)attr;
1347 break;
1348 }
1349 }
1350
1351 if (stage == NULL) {
1352 fprintf(fp, " ERROR: stage header not found!\n");
1353 return -1;
1354 }
1355
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001356 fprintf(fp,
Julius Werner81dc20e2020-10-15 17:37:57 -07001357 " entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
1358 "memlen: %d\n",
Alex James02001a382021-12-19 16:41:59 -06001359 be64toh(stage->loadaddr) + be32toh(stage->entry_offset),
1360 be64toh(stage->loadaddr),
1361 be32toh(stage->memlen));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001362 return 0;
1363}
1364
Hung-Te Lin0780d672014-05-16 10:14:05 +08001365static int cbfs_print_decoded_payload_segment_info(
1366 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001367{
Hung-Te Lin0780d672014-05-16 10:14:05 +08001368 /* The input (seg) must be already decoded by
1369 * cbfs_decode_payload_segment.
1370 */
1371 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001372 case PAYLOAD_SEGMENT_CODE:
1373 case PAYLOAD_SEGMENT_DATA:
1374 fprintf(fp, " %s (%s compression, offset: 0x%x, "
1375 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001376 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001377 "code " : "data"),
1378 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +08001379 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001380 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +08001381 seg->offset, seg->load_addr, seg->len,
1382 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001383 break;
1384
1385 case PAYLOAD_SEGMENT_ENTRY:
1386 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001387 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001388 break;
1389
1390 case PAYLOAD_SEGMENT_BSS:
1391 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
1392 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001393 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001394 break;
1395
1396 case PAYLOAD_SEGMENT_PARAMS:
1397 fprintf(fp, " parameters\n");
1398 break;
1399
1400 default:
1401 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
1402 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001403 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001404 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +08001405 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001406 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +08001407 seg->offset, seg->load_addr, seg->len,
1408 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001409 break;
1410 }
1411 return 0;
1412}
1413
1414int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001415 void *arg)
1416{
Patrick Georgic569b8b2015-07-15 16:42:38 +02001417 const char *name = entry->filename;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001418 struct cbfs_payload_segment *payload;
1419 FILE *fp = (FILE *)arg;
1420
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001421 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001422 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1423 cbfs_get_entry_addr(image, entry));
1424 return -1;
1425 }
1426 if (!fp)
1427 fp = stdout;
1428
Patrick Georgic82725c2015-08-26 12:13:03 +02001429 unsigned int decompressed_size = 0;
Patrick Georgia71c83f2015-08-26 12:23:26 +02001430 unsigned int compression = cbfs_file_get_compression_info(entry,
1431 &decompressed_size);
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001432 const char *compression_name = lookup_name_by_type(
1433 types_cbfs_compression, compression, "????");
Patrick Georgic82725c2015-08-26 12:13:03 +02001434
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001435 if (compression == CBFS_COMPRESS_NONE)
1436 fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s\n",
Patrick Georgic82725c2015-08-26 12:13:03 +02001437 *name ? name : "(empty)",
1438 cbfs_get_entry_addr(image, entry),
Alex James02001a382021-12-19 16:41:59 -06001439 get_cbfs_entry_type_name(be32toh(entry->type)),
1440 be32toh(entry->len),
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001441 compression_name
Patrick Georgic82725c2015-08-26 12:13:03 +02001442 );
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001443 else
1444 fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s (%d decompressed)\n",
1445 *name ? name : "(empty)",
1446 cbfs_get_entry_addr(image, entry),
Alex James02001a382021-12-19 16:41:59 -06001447 get_cbfs_entry_type_name(be32toh(entry->type)),
1448 be32toh(entry->len),
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001449 compression_name,
1450 decompressed_size
1451 );
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001452
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001453 if (!verbose)
1454 return 0;
1455
Julius Wernerd4775652020-03-13 16:43:34 -07001456 struct cbfs_file_attr_hash *attr = NULL;
1457 while ((attr = cbfs_file_get_next_hash(entry, attr)) != NULL) {
1458 size_t hash_len = vb2_digest_size(attr->hash.algo);
1459 if (!hash_len) {
1460 fprintf(fp, "invalid/unsupported hash algorithm: %d\n",
1461 attr->hash.algo);
Patrick Georgi89f20342015-10-01 15:54:04 +02001462 break;
1463 }
Julius Wernerd4775652020-03-13 16:43:34 -07001464 char *hash_str = bintohex(attr->hash.raw, hash_len);
Julius Wernerd96ca242022-08-08 18:08:35 -07001465 int valid = vb2_hash_verify(false, CBFS_SUBHEADER(entry),
Alex James02001a382021-12-19 16:41:59 -06001466 be32toh(entry->len), &attr->hash) == VB2_SUCCESS;
Patrick Georgi89f20342015-10-01 15:54:04 +02001467 const char *valid_str = valid ? "valid" : "invalid";
1468
1469 fprintf(fp, " hash %s:%s %s\n",
Julius Wernerd4775652020-03-13 16:43:34 -07001470 vb2_get_hash_algorithm_name(attr->hash.algo),
Patrick Georgi89f20342015-10-01 15:54:04 +02001471 hash_str, valid_str);
1472 free(hash_str);
1473 }
1474
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001475 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
Alex James02001a382021-12-19 16:41:59 -06001476 cbfs_get_entry_addr(image, entry), be32toh(entry->offset),
1477 cbfs_get_entry_addr(image, entry) + be32toh(entry->offset),
1478 be32toh(entry->len));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001479
1480 /* note the components of the subheader may be in host order ... */
Alex James02001a382021-12-19 16:41:59 -06001481 switch (be32toh(entry->type)) {
Julius Wernerd4775652020-03-13 16:43:34 -07001482 case CBFS_TYPE_STAGE:
Julius Werner81dc20e2020-10-15 17:37:57 -07001483 cbfs_print_stage_info(entry, fp);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001484 break;
1485
Julius Wernerd4775652020-03-13 16:43:34 -07001486 case CBFS_TYPE_SELF:
Paul Menzel831bbe82015-08-08 20:20:57 +02001487 payload = (struct cbfs_payload_segment *)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001488 CBFS_SUBHEADER(entry);
1489 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +08001490 struct cbfs_payload_segment seg;
1491 cbfs_decode_payload_segment(&seg, payload);
1492 cbfs_print_decoded_payload_segment_info(
1493 &seg, fp);
1494 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001495 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +08001496 else
Aaron Durbinca630272014-08-05 10:48:20 -05001497 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001498 }
1499 break;
1500 default:
1501 break;
1502 }
1503 return 0;
1504}
1505
Julius Wernerea4d4c92023-04-11 17:01:00 -07001506/*
1507 * The format of this output has been stable for many years. Since it is meant
1508 * to be parsed by scripts, we should probably not lightly make changes to it as
1509 * that could break older scripts expecting a different format.
1510 *
1511 * Until CB:41119, the `-v` flag made no difference when `-k` was selected, so
1512 * presumably no scripts were using that combination. That's why that patch left
1513 * the output for `-k` by itself alone to avoid breaking legacy scripts, and
1514 * expanded `-k -v` to allow an arbitrary number of `<key>:<value>` tokens at
1515 * the end of each row behind the legacy column output. So the new output format
1516 * stability rules should be that `-k` will stay as it is, and `-k -v` may be
1517 * expanded to add more `<key>:<value>` tokens to the end of a row. Scripts that
1518 * want to parse `-k -v` output should be written to gracefully ignore any extra
1519 * such tokens where they don't recognize the key.
1520 *
1521 * The `-k -v` output may also include extra rows that start with a `[`. These
1522 * do not represent a CBFS file and can instead be used to display data that is
1523 * associated with the CBFS as a whole and not any single file. Currently
1524 * defined are `[FMAP REGION]\t<region name>` and
1525 * `[METADATA HASH]\t<hash>:<algo>`. More may be defined in the future and
1526 * scripts parsing `-k -v` output should be written to gracefully ignore any
1527 * rows starting with `[` that they don't recognize.
1528 *
1529 * The format for existing `<key:value>` tokens or `[` rows should never be
1530 * changed once they are added.
1531 */
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001532static int cbfs_print_parseable_entry_info(struct cbfs_image *image,
1533 struct cbfs_file *entry, void *arg)
1534{
1535 FILE *fp = (FILE *)arg;
1536 const char *name;
1537 const char *type;
1538 size_t offset;
1539 size_t metadata_size;
1540 size_t data_size;
1541 const char *sep = "\t";
1542
1543 if (!cbfs_is_valid_entry(image, entry)) {
1544 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1545 cbfs_get_entry_addr(image, entry));
1546 return -1;
1547 }
1548
1549 name = entry->filename;
1550 if (*name == '\0')
1551 name = "(empty)";
Alex James02001a382021-12-19 16:41:59 -06001552 type = get_cbfs_entry_type_name(be32toh(entry->type)),
1553 metadata_size = be32toh(entry->offset);
1554 data_size = be32toh(entry->len);
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001555 offset = cbfs_get_entry_addr(image, entry);
1556
1557 fprintf(fp, "%s%s", name, sep);
1558 fprintf(fp, "0x%zx%s", offset, sep);
1559 fprintf(fp, "%s%s", type, sep);
1560 fprintf(fp, "0x%zx%s", metadata_size, sep);
1561 fprintf(fp, "0x%zx%s", data_size, sep);
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001562 fprintf(fp, "0x%zx", metadata_size + data_size);
1563
1564 if (verbose) {
1565 unsigned int decompressed_size = 0;
1566 unsigned int compression = cbfs_file_get_compression_info(entry,
1567 &decompressed_size);
1568 if (compression != CBFS_COMPRESS_NONE)
1569 fprintf(fp, "%scomp:%s:0x%x", sep, lookup_name_by_type(
1570 types_cbfs_compression, compression, "????"),
1571 decompressed_size);
1572
1573 struct cbfs_file_attr_hash *attr = NULL;
1574 while ((attr = cbfs_file_get_next_hash(entry, attr)) != NULL) {
1575 size_t hash_len = vb2_digest_size(attr->hash.algo);
1576 if (!hash_len)
1577 continue;
1578 char *hash_str = bintohex(attr->hash.raw, hash_len);
Julius Wernerd96ca242022-08-08 18:08:35 -07001579 int valid = vb2_hash_verify(false, CBFS_SUBHEADER(entry),
Alex James02001a382021-12-19 16:41:59 -06001580 be32toh(entry->len), &attr->hash) == VB2_SUCCESS;
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001581 fprintf(fp, "%shash:%s:%s:%s", sep,
1582 vb2_get_hash_algorithm_name(attr->hash.algo),
1583 hash_str, valid ? "valid" : "invalid");
1584 free(hash_str);
1585 }
1586 }
1587 fprintf(fp, "\n");
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001588
1589 return 0;
1590}
1591
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001592void cbfs_print_directory(struct cbfs_image *image)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001593{
Sol Boucher67a0a862015-03-18 12:36:27 -07001594 if (cbfs_is_legacy_cbfs(image))
1595 cbfs_print_header_info(image);
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001596 printf("%-30s %-10s %-12s Size Comp\n", "Name", "Offset", "Type");
Julius Werner7066a1e2020-04-02 15:49:34 -07001597 cbfs_legacy_walk(image, cbfs_print_entry_info, NULL);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001598}
1599
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001600void cbfs_print_parseable_directory(struct cbfs_image *image)
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001601{
Furquan Shaikh161d2332016-05-26 14:41:02 -07001602 size_t i;
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001603 const char *header[] = {
1604 "Name",
1605 "Offset",
1606 "Type",
1607 "Metadata Size",
1608 "Data Size",
1609 "Total Size",
1610 };
1611 const char *sep = "\t";
1612
1613 for (i = 0; i < ARRAY_SIZE(header) - 1; i++)
1614 fprintf(stdout, "%s%s", header[i], sep);
1615 fprintf(stdout, "%s\n", header[i]);
Julius Werner7066a1e2020-04-02 15:49:34 -07001616 cbfs_legacy_walk(image, cbfs_print_parseable_entry_info, stdout);
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001617}
1618
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001619int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -08001620 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001621{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001622 struct cbfs_file *next;
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001623 uint32_t next_addr = 0;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001624
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001625 /* We don't return here even if this entry is already empty because we
1626 want to merge the empty entries following after it. */
1627
1628 /* Loop until non-empty entry is found, starting from the current entry.
1629 After the loop, next_addr points to the next non-empty entry. */
1630 next = entry;
Alex James02001a382021-12-19 16:41:59 -06001631 while (be32toh(next->type) == CBFS_TYPE_DELETED ||
1632 be32toh(next->type) == CBFS_TYPE_NULL) {
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001633 next = cbfs_find_next_entry(image, next);
1634 if (!next)
1635 break;
1636 next_addr = cbfs_get_entry_addr(image, next);
1637 if (!cbfs_is_valid_entry(image, next))
1638 /* 'next' could be the end of cbfs */
1639 break;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001640 }
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001641
1642 if (!next_addr)
1643 /* Nothing to empty */
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001644 return 0;
1645
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001646 /* We can return here if we find only a single empty entry.
1647 For simplicity, we just proceed (and make it empty again). */
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001648
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001649 /* We're creating one empty entry for combined empty spaces */
1650 uint32_t addr = cbfs_get_entry_addr(image, entry);
1651 size_t len = next_addr - addr - cbfs_calculate_file_header_size("");
1652 DEBUG("join_empty_entry: [0x%x, 0x%x) len=%zu\n", addr, next_addr, len);
Julius Wernerd4775652020-03-13 16:43:34 -07001653 cbfs_create_empty_entry(entry, CBFS_TYPE_NULL, len, "");
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001654
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001655 return 0;
1656}
1657
Julius Werner7066a1e2020-04-02 15:49:34 -07001658int cbfs_legacy_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001659 void *arg)
1660{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001661 int count = 0;
1662 struct cbfs_file *entry;
1663 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001664 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001665 entry = cbfs_find_next_entry(image, entry)) {
1666 count ++;
1667 if (callback(image, entry, arg) != 0)
1668 break;
1669 }
1670 return count;
1671}
1672
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001673static int cbfs_header_valid(struct cbfs_header *header)
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001674{
Alex James02001a382021-12-19 16:41:59 -06001675 if ((be32toh(header->magic) == CBFS_HEADER_MAGIC) &&
1676 ((be32toh(header->version) == CBFS_HEADER_VERSION1) ||
1677 (be32toh(header->version) == CBFS_HEADER_VERSION2)) &&
1678 (be32toh(header->offset) < be32toh(header->romsize)))
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001679 return 1;
1680 return 0;
1681}
1682
1683struct cbfs_header *cbfs_find_header(char *data, size_t size,
1684 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001685{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001686 size_t offset;
1687 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001688 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001689 struct cbfs_header *header, *result = NULL;
1690
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001691 if (forced_offset < (size - sizeof(struct cbfs_header))) {
1692 /* Check if the forced header is valid. */
1693 header = (struct cbfs_header *)(data + forced_offset);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001694 if (cbfs_header_valid(header))
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001695 return header;
1696 return NULL;
1697 }
1698
Julius Wernerefcee762014-11-10 13:14:24 -08001699 // Try finding relative offset of master header at end of file first.
1700 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
1701 offset = size + rel_offset;
1702 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
1703 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001704
Hung-Te Lineab2c812013-01-29 01:56:17 +08001705 if (offset >= size - sizeof(*header) ||
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001706 !cbfs_header_valid((struct cbfs_header *)(data + offset))) {
Julius Wernerefcee762014-11-10 13:14:24 -08001707 // Some use cases append non-CBFS data to the end of the ROM.
1708 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +08001709 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001710 }
Hung-Te Lineab2c812013-01-29 01:56:17 +08001711
1712 for (; offset + sizeof(*header) < size; offset++) {
1713 header = (struct cbfs_header *)(data + offset);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001714 if (!cbfs_header_valid(header))
Hung-Te Lineab2c812013-01-29 01:56:17 +08001715 continue;
Julius Wernerefcee762014-11-10 13:14:24 -08001716 if (!found++)
1717 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001718 }
Julius Wernerefcee762014-11-10 13:14:24 -08001719 if (found > 1)
1720 // Top-aligned images usually have a working relative offset
1721 // field, so this is more likely to happen on bottom-aligned
1722 // ones (where the first header is the "outermost" one)
1723 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +08001724 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001725 return result;
1726}
1727
1728
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001729struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
1730{
Sol Boucher3e060ed2015-05-05 15:40:15 -07001731 assert(image);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001732 if (image->has_header)
1733 /* header.offset is relative to start of flash, not
1734 * start of region, so use it with the full image.
1735 */
1736 return (struct cbfs_file *)
1737 (buffer_get_original_backing(&image->buffer) +
1738 image->header.offset);
1739 else
1740 return (struct cbfs_file *)buffer_get(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001741}
1742
1743struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001744 struct cbfs_file *entry)
1745{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001746 uint32_t addr = cbfs_get_entry_addr(image, entry);
Julius Wernerd4775652020-03-13 16:43:34 -07001747 int align = image->has_header ? image->header.align : CBFS_ALIGNMENT;
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001748 assert(entry && cbfs_is_valid_entry(image, entry));
Alex James02001a382021-12-19 16:41:59 -06001749 addr += be32toh(entry->offset) + be32toh(entry->len);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001750 addr = align_up(addr, align);
1751 return (struct cbfs_file *)(image->buffer.data + addr);
1752}
1753
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001754uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
1755{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001756 assert(image && image->buffer.data && entry);
1757 return (int32_t)((char *)entry - image->buffer.data);
1758}
1759
Sol Boucher67a0a862015-03-18 12:36:27 -07001760int cbfs_is_valid_cbfs(struct cbfs_image *image)
1761{
1762 return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC,
1763 strlen(CBFS_FILE_MAGIC));
1764}
1765
1766int cbfs_is_legacy_cbfs(struct cbfs_image *image)
1767{
1768 return image->has_header;
1769}
1770
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001771int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
1772{
Sol Bouchere3260a02015-03-25 13:40:08 -07001773 uint32_t offset = cbfs_get_entry_addr(image, entry);
1774
1775 if (offset >= image->buffer.size)
1776 return 0;
1777
1778 struct buffer entry_data;
1779 buffer_clone(&entry_data, &image->buffer);
1780 buffer_seek(&entry_data, offset);
1781 return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
Sol Boucher67a0a862015-03-18 12:36:27 -07001782 strlen(CBFS_FILE_MAGIC));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001783}
1784
Patrick Georgi57edf162015-08-12 09:20:11 +02001785struct cbfs_file *cbfs_create_file_header(int type,
Vadim Bendebury45e59972014-12-23 15:59:57 -08001786 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001787{
Julius Wernerd4775652020-03-13 16:43:34 -07001788 struct cbfs_file *entry = malloc(CBFS_METADATA_MAX_SIZE);
1789 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, CBFS_METADATA_MAX_SIZE);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001790 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
Alex James02001a382021-12-19 16:41:59 -06001791 entry->type = htobe32(type);
1792 entry->len = htobe32(len);
Patrick Georgi0d618af2015-07-15 18:28:23 +02001793 entry->attributes_offset = 0;
Alex James02001a382021-12-19 16:41:59 -06001794 entry->offset = htobe32(cbfs_calculate_file_header_size(name));
1795 memset(entry->filename, 0, be32toh(entry->offset) - sizeof(*entry));
Patrick Georgic569b8b2015-07-15 16:42:38 +02001796 strcpy(entry->filename, name);
Patrick Georgi57edf162015-08-12 09:20:11 +02001797 return entry;
1798}
1799
1800int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
1801 size_t len, const char *name)
1802{
1803 struct cbfs_file *tmp = cbfs_create_file_header(type, len, name);
Alex James02001a382021-12-19 16:41:59 -06001804 memcpy(entry, tmp, be32toh(tmp->offset));
Patrick Georgi57edf162015-08-12 09:20:11 +02001805 free(tmp);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001806 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1807 return 0;
1808}
1809
Patrick Georgi2c615062015-07-15 20:49:00 +02001810struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file)
1811{
1812 /* attributes_offset should be 0 when there is no attribute, but all
1813 * values that point into the cbfs_file header are invalid, too. */
Alex James02001a382021-12-19 16:41:59 -06001814 if (be32toh(file->attributes_offset) <= sizeof(*file))
Patrick Georgi2c615062015-07-15 20:49:00 +02001815 return NULL;
1816
1817 /* There needs to be enough space for the file header and one
1818 * attribute header for this to make sense. */
Alex James02001a382021-12-19 16:41:59 -06001819 if (be32toh(file->offset) <=
Patrick Georgi2c615062015-07-15 20:49:00 +02001820 sizeof(*file) + sizeof(struct cbfs_file_attribute))
1821 return NULL;
1822
1823 return (struct cbfs_file_attribute *)
Alex James02001a382021-12-19 16:41:59 -06001824 (((uint8_t *)file) + be32toh(file->attributes_offset));
Patrick Georgi2c615062015-07-15 20:49:00 +02001825}
1826
1827struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
1828 struct cbfs_file_attribute *attr)
1829{
1830 /* ex falso sequitur quodlibet */
1831 if (attr == NULL)
1832 return NULL;
1833
1834 /* Is there enough space for another attribute? */
Alex James02001a382021-12-19 16:41:59 -06001835 if ((uint8_t *)attr + be32toh(attr->len) +
Patrick Rudolphe28fa402019-02-14 11:19:07 +01001836 sizeof(struct cbfs_file_attribute) >
Alex James02001a382021-12-19 16:41:59 -06001837 (uint8_t *)file + be32toh(file->offset))
Patrick Georgi2c615062015-07-15 20:49:00 +02001838 return NULL;
1839
1840 struct cbfs_file_attribute *next = (struct cbfs_file_attribute *)
Alex James02001a382021-12-19 16:41:59 -06001841 (((uint8_t *)attr) + be32toh(attr->len));
Patrick Georgi2c615062015-07-15 20:49:00 +02001842 /* If any, "unused" attributes must come last. */
Alex James02001a382021-12-19 16:41:59 -06001843 if (be32toh(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED)
Patrick Georgi2c615062015-07-15 20:49:00 +02001844 return NULL;
Alex James02001a382021-12-19 16:41:59 -06001845 if (be32toh(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
Patrick Georgi2c615062015-07-15 20:49:00 +02001846 return NULL;
1847
1848 return next;
1849}
1850
1851struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
1852 uint32_t tag,
1853 uint32_t size)
1854{
Julius Werner5779ca72020-11-20 16:12:40 -08001855 assert(IS_ALIGNED(size, CBFS_ATTRIBUTE_ALIGN));
Patrick Georgi2c615062015-07-15 20:49:00 +02001856 struct cbfs_file_attribute *attr, *next;
1857 next = cbfs_file_first_attr(header);
1858 do {
1859 attr = next;
1860 next = cbfs_file_next_attr(header, attr);
1861 } while (next != NULL);
Alex James02001a382021-12-19 16:41:59 -06001862 uint32_t header_size = be32toh(header->offset) + size;
Julius Wernerd4775652020-03-13 16:43:34 -07001863 if (header_size > CBFS_METADATA_MAX_SIZE) {
Patrick Georgi2c615062015-07-15 20:49:00 +02001864 DEBUG("exceeding allocated space for cbfs_file headers");
1865 return NULL;
1866 }
1867 /* attr points to the last valid attribute now.
1868 * If NULL, we have to create the first one. */
1869 if (attr == NULL) {
1870 /* New attributes start where the header ends.
Elyes HAOUAS3db01982018-08-23 18:08:20 +02001871 * header->offset is later set to accommodate the
Patrick Georgi2c615062015-07-15 20:49:00 +02001872 * additional structure.
Elyes HAOUAS3db01982018-08-23 18:08:20 +02001873 * No endianness translation necessary here, because both
Patrick Georgi2c615062015-07-15 20:49:00 +02001874 * fields are encoded the same way. */
1875 header->attributes_offset = header->offset;
1876 attr = (struct cbfs_file_attribute *)
1877 (((uint8_t *)header) +
Alex James02001a382021-12-19 16:41:59 -06001878 be32toh(header->attributes_offset));
Patrick Georgi2c615062015-07-15 20:49:00 +02001879 } else {
1880 attr = (struct cbfs_file_attribute *)
1881 (((uint8_t *)attr) +
Alex James02001a382021-12-19 16:41:59 -06001882 be32toh(attr->len));
Patrick Georgi2c615062015-07-15 20:49:00 +02001883 }
Alex James02001a382021-12-19 16:41:59 -06001884 header->offset = htobe32(header_size);
Julius Wernerd4775652020-03-13 16:43:34 -07001885 /* Attributes are expected to be small (much smaller than a flash page)
1886 and not really meant to be overwritten in-place. To avoid surprising
1887 values in reserved fields of attribute structures, initialize them to
1888 0, not 0xff. */
1889 memset(attr, 0, size);
Alex James02001a382021-12-19 16:41:59 -06001890 attr->tag = htobe32(tag);
1891 attr->len = htobe32(size);
Patrick Georgi2c615062015-07-15 20:49:00 +02001892 return attr;
1893}
1894
Patrick Georgi89f20342015-10-01 15:54:04 +02001895int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer,
Julius Wernerd4775652020-03-13 16:43:34 -07001896 enum vb2_hash_algorithm alg)
Patrick Georgi89f20342015-10-01 15:54:04 +02001897{
Julius Wernerd4775652020-03-13 16:43:34 -07001898 if (!vb2_digest_size(alg))
Patrick Georgi89f20342015-10-01 15:54:04 +02001899 return -1;
1900
Julius Wernerd4775652020-03-13 16:43:34 -07001901 struct cbfs_file_attr_hash *attr =
Patrick Georgi89f20342015-10-01 15:54:04 +02001902 (struct cbfs_file_attr_hash *)cbfs_add_file_attr(header,
Julius Wernerd4775652020-03-13 16:43:34 -07001903 CBFS_FILE_ATTR_TAG_HASH, cbfs_file_attr_hash_size(alg));
Patrick Georgi89f20342015-10-01 15:54:04 +02001904
Julius Wernerd4775652020-03-13 16:43:34 -07001905 if (attr == NULL)
Patrick Georgi89f20342015-10-01 15:54:04 +02001906 return -1;
1907
Julius Wernerd96ca242022-08-08 18:08:35 -07001908 if (vb2_hash_calculate(false, buffer_get(buffer), buffer_size(buffer),
Julius Wernerd4775652020-03-13 16:43:34 -07001909 alg, &attr->hash) != VB2_SUCCESS)
Patrick Georgi89f20342015-10-01 15:54:04 +02001910 return -1;
1911
1912 return 0;
1913}
1914
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001915/* Finds a place to hold whole data in same memory page. */
1916static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1917{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001918 if (!page)
1919 return 1;
1920 return (start / page) == (start + size - 1) / page;
1921}
1922
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001923/* Tests if data can fit in a range by given offset:
Aaron Durbind7339412015-09-15 12:50:14 -05001924 * start ->| metadata_size | offset (+ size) |<- end
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001925 */
Aaron Durbind7339412015-09-15 12:50:14 -05001926static int is_in_range(size_t start, size_t end, size_t metadata_size,
1927 size_t offset, size_t size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001928{
Aaron Durbind7339412015-09-15 12:50:14 -05001929 return (offset >= start + metadata_size && offset + size <= end);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001930}
1931
Werner Zeh95bfcae2016-01-25 12:47:20 +01001932static size_t absolute_align(const struct cbfs_image *image, size_t val,
1933 size_t align)
1934{
1935 const size_t region_offset = buffer_offset(&image->buffer);
1936 /* To perform alignment on absolute address, take the region offset */
1937 /* of the image into account. */
1938 return align_up(val + region_offset, align) - region_offset;
1939
1940}
1941
Aaron Durbind7339412015-09-15 12:50:14 -05001942int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size,
1943 size_t page_size, size_t align, size_t metadata_size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001944{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001945 struct cbfs_file *entry;
1946 size_t need_len;
Aaron Durbind7339412015-09-15 12:50:14 -05001947 size_t addr, addr_next, addr2, addr3, offset;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001948
1949 /* Default values: allow fitting anywhere in ROM. */
1950 if (!page_size)
Sol Boucher67a0a862015-03-18 12:36:27 -07001951 page_size = image->has_header ? image->header.romsize :
1952 image->buffer.size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001953 if (!align)
1954 align = 1;
1955
1956 if (size > page_size)
Aaron Durbind7339412015-09-15 12:50:14 -05001957 ERROR("Input file size (%zd) greater than page size (%zd).\n",
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001958 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001959
Aaron Durbind7339412015-09-15 12:50:14 -05001960 size_t image_align = image->has_header ? image->header.align :
Julius Wernerd4775652020-03-13 16:43:34 -07001961 CBFS_ALIGNMENT;
Sol Boucher67a0a862015-03-18 12:36:27 -07001962 if (page_size % image_align)
Aaron Durbind7339412015-09-15 12:50:14 -05001963 WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001964 __func__, page_size, image_align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001965
Aaron Durbind7339412015-09-15 12:50:14 -05001966 need_len = metadata_size + size;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001967
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001968 // Merge empty entries to build get max available space.
Julius Werner7066a1e2020-04-02 15:49:34 -07001969 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001970
1971 /* Three cases of content location on memory page:
1972 * case 1.
1973 * | PAGE 1 | PAGE 2 |
1974 * | <header><content>| Fit. Return start of content.
1975 *
1976 * case 2.
1977 * | PAGE 1 | PAGE 2 |
1978 * | <header><content> | Fits when we shift content to align
1979 * shift-> | <header>|<content> | at starting of PAGE 2.
1980 *
1981 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001982 * | PAGE 1 | PAGE 2 | PAGE 3 |
1983 * | <header>< content > | Can't fit. If we shift content to
1984 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1985 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001986 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001987 * The returned address can be then used as "base-address" (-b) in add-*
1988 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1989 * For stage targets, the address is also used to re-link stage before
1990 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001991 */
1992 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001993 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001994 entry = cbfs_find_next_entry(image, entry)) {
1995
Alex James02001a382021-12-19 16:41:59 -06001996 uint32_t type = be32toh(entry->type);
Julius Wernerd4775652020-03-13 16:43:34 -07001997 if (type != CBFS_TYPE_NULL)
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001998 continue;
1999
2000 addr = cbfs_get_entry_addr(image, entry);
2001 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
2002 image, entry));
2003 if (addr_next - addr < need_len)
2004 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08002005
Werner Zeh95bfcae2016-01-25 12:47:20 +01002006 offset = absolute_align(image, addr + metadata_size, align);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08002007 if (is_in_same_page(offset, size, page_size) &&
Aaron Durbind7339412015-09-15 12:50:14 -05002008 is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002009 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08002010 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002011 }
2012
2013 addr2 = align_up(addr, page_size);
Werner Zeh95bfcae2016-01-25 12:47:20 +01002014 offset = absolute_align(image, addr2, align);
Aaron Durbind7339412015-09-15 12:50:14 -05002015 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002016 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08002017 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002018 }
2019
Aaron Durbind7339412015-09-15 12:50:14 -05002020 /* Assume page_size >= metadata_size so adding one page will
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08002021 * definitely provide the space for header. */
Aaron Durbind7339412015-09-15 12:50:14 -05002022 assert(page_size >= metadata_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002023 addr3 = addr2 + page_size;
Werner Zeh95bfcae2016-01-25 12:47:20 +01002024 offset = absolute_align(image, addr3, align);
Aaron Durbind7339412015-09-15 12:50:14 -05002025 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002026 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08002027 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002028 }
2029 }
2030 return -1;
2031}