blob: 683a96cac7c9a56fca9890712e97f6343cbc536b [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
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800733 uint32_t entry_type;
734 uint32_t addr, addr_next;
735 struct cbfs_file *entry, *next;
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200736 uint32_t need_size;
Alex James02001a382021-12-19 16:41:59 -0600737 uint32_t header_size = be32toh(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800738
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800739 need_size = header_size + buffer->size;
740 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
741 name, content_offset, header_size, buffer->size, need_size);
742
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800743 // Merge empty entries.
744 DEBUG("(trying to merge empty entries...)\n");
Julius Werner7066a1e2020-04-02 15:49:34 -0700745 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800746
747 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800748 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800749 entry = cbfs_find_next_entry(image, entry)) {
750
Alex James02001a382021-12-19 16:41:59 -0600751 entry_type = be32toh(entry->type);
Julius Wernerd4775652020-03-13 16:43:34 -0700752 if (entry_type != CBFS_TYPE_NULL)
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800753 continue;
754
755 addr = cbfs_get_entry_addr(image, entry);
756 next = cbfs_find_next_entry(image, entry);
757 addr_next = cbfs_get_entry_addr(image, next);
758
759 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
760 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600761
762 /* Will the file fit? Don't yet worry if we have space for a new
763 * "empty" entry. We take care of that later.
764 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800765 if (addr + need_size > addr_next)
766 continue;
767
Patrick Georgiaa44dbd2015-08-12 12:05:21 +0200768 // Test for complicated cases
769 if (content_offset > 0) {
770 if (addr_next < content_offset) {
771 DEBUG("Not for specified offset yet");
772 continue;
773 } else if (addr > content_offset) {
774 DEBUG("Exceed specified content_offset.");
775 break;
776 } else if (addr + header_size > content_offset) {
777 ERROR("Not enough space for header.\n");
778 break;
779 } else if (content_offset + buffer->size > addr_next) {
780 ERROR("Not enough space for content.\n");
781 break;
782 }
783 }
784
785 // TODO there are more few tricky cases that we may
786 // want to fit by altering offset.
787
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200788 if (content_offset == 0) {
789 // we tested every condition earlier under which
790 // placing the file there might fail
791 content_offset = addr + header_size;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800792 }
793
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800794 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
795 addr, addr_next - addr, content_offset);
796
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200797 if (cbfs_add_entry_at(image, entry, buffer->data,
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100798 content_offset, header, len_align) == 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800799 return 0;
800 }
801 break;
802 }
803
804 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
805 buffer->name, buffer->size, buffer->size / 1024, content_offset);
806 return -1;
807}
808
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700809struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
810{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800811 struct cbfs_file *entry;
812 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800813 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800814 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgic569b8b2015-07-15 16:42:38 +0200815 if (strcasecmp(entry->filename, name) == 0) {
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800816 DEBUG("cbfs_get_entry: found %s\n", name);
817 return entry;
818 }
819 }
820 return NULL;
821}
822
Antonello Dettorifda691e2016-06-09 12:35:36 +0200823static int cbfs_payload_decompress(struct cbfs_payload_segment *segments,
824 struct buffer *buff, int num_seg)
825{
826 struct buffer new_buffer;
827 struct buffer seg_buffer;
828 size_t new_buff_sz;
829 char *in_ptr;
830 char *out_ptr;
831 size_t new_offset;
832 decomp_func_ptr decompress;
833
834 new_offset = num_seg * sizeof(*segments);
835 new_buff_sz = num_seg * sizeof(*segments);
836
837 /* Find out and allocate the amount of memory occupied
838 * by the binary data */
839 for (int i = 0; i < num_seg; i++)
840 new_buff_sz += segments[i].mem_len;
841
Furquan Shaikh58644a02016-08-05 08:27:18 -0700842 if (buffer_create(&new_buffer, new_buff_sz, "decompressed_buff"))
843 return -1;
Antonello Dettorifda691e2016-06-09 12:35:36 +0200844
845 in_ptr = buffer_get(buff) + new_offset;
846 out_ptr = buffer_get(&new_buffer) + new_offset;
847
848 for (int i = 0; i < num_seg; i++) {
849 struct buffer tbuff;
850 size_t decomp_size;
851
Antonello Dettorifda691e2016-06-09 12:35:36 +0200852 /* Segments BSS and ENTRY do not have binary data. */
853 if (segments[i].type == PAYLOAD_SEGMENT_BSS ||
854 segments[i].type == PAYLOAD_SEGMENT_ENTRY) {
855 continue;
856 } else if (segments[i].type == PAYLOAD_SEGMENT_PARAMS) {
857 memcpy(out_ptr, in_ptr, segments[i].len);
858 segments[i].offset = new_offset;
859 new_offset += segments[i].len;
860 in_ptr += segments[i].len;
861 out_ptr += segments[i].len;
862 segments[i].compression = CBFS_COMPRESS_NONE;
863 continue;
864 }
865
Joel Kitching72d77a92018-07-18 13:23:52 +0800866 /* The payload uses an unknown compression algorithm. */
867 decompress = decompression_function(segments[i].compression);
868 if (decompress == NULL) {
869 ERROR("Unknown decompression algorithm: %u\n",
870 segments[i].compression);
871 return -1;
872 }
873
Furquan Shaikh58644a02016-08-05 08:27:18 -0700874 if (buffer_create(&tbuff, segments[i].mem_len, "segment")) {
875 buffer_delete(&new_buffer);
876 return -1;
877 }
Antonello Dettorifda691e2016-06-09 12:35:36 +0200878
879 if (decompress(in_ptr, segments[i].len, buffer_get(&tbuff),
880 (int) buffer_size(&tbuff),
881 &decomp_size)) {
882 ERROR("Couldn't decompress payload segment %u\n", i);
883 buffer_delete(&new_buffer);
Furquan Shaikh9844d562016-08-05 08:32:23 -0700884 buffer_delete(&tbuff);
Antonello Dettorifda691e2016-06-09 12:35:36 +0200885 return -1;
886 }
887
888 memcpy(out_ptr, buffer_get(&tbuff), decomp_size);
889
890 in_ptr += segments[i].len;
891
892 /* Update the offset of the segment. */
893 segments[i].offset = new_offset;
894 /* True decompressed size is just the data size. No metadata */
895 segments[i].len = decomp_size;
896 /* Segment is not compressed. */
897 segments[i].compression = CBFS_COMPRESS_NONE;
898
899 /* Update the offset and output buffer pointer. */
900 new_offset += decomp_size;
901 out_ptr += decomp_size;
902
903 buffer_delete(&tbuff);
904 }
905
906 buffer_splice(&seg_buffer, &new_buffer, 0, 0);
907 xdr_segs(&seg_buffer, segments, num_seg);
908
909 buffer_delete(buff);
910 *buff = new_buffer;
911
912 return 0;
913}
914
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500915static int init_elf_from_arch(Elf64_Ehdr *ehdr, uint32_t cbfs_arch)
916{
917 int endian;
918 int nbits;
919 int machine;
920
921 switch (cbfs_arch) {
922 case CBFS_ARCHITECTURE_X86:
923 endian = ELFDATA2LSB;
924 nbits = ELFCLASS32;
925 machine = EM_386;
926 break;
927 case CBFS_ARCHITECTURE_ARM:
928 endian = ELFDATA2LSB;
929 nbits = ELFCLASS32;
930 machine = EM_ARM;
931 break;
932 case CBFS_ARCHITECTURE_AARCH64:
933 endian = ELFDATA2LSB;
934 nbits = ELFCLASS64;
935 machine = EM_AARCH64;
936 break;
937 case CBFS_ARCHITECTURE_MIPS:
938 endian = ELFDATA2LSB;
939 nbits = ELFCLASS32;
940 machine = EM_MIPS;
941 break;
942 case CBFS_ARCHITECTURE_RISCV:
943 endian = ELFDATA2LSB;
944 nbits = ELFCLASS32;
945 machine = EM_RISCV;
946 break;
947 default:
948 ERROR("Unsupported arch: %x\n", cbfs_arch);
949 return -1;
950 }
951
952 elf_init_eheader(ehdr, machine, nbits, endian);
953 return 0;
954}
955
Julius Werner81dc20e2020-10-15 17:37:57 -0700956static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch,
957 struct cbfs_file *entry)
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500958{
959 Elf64_Ehdr ehdr;
960 Elf64_Shdr shdr;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500961 struct elf_writer *ew;
962 struct buffer elf_out;
963 size_t empty_sz;
Aaron Durbin694fd132015-10-28 11:39:34 -0500964 int rmod_ret;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500965
Antonello Dettori0b806282016-06-26 00:24:25 +0200966 if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
967 ERROR("You need to specify -m ARCH.\n");
968 return -1;
969 }
970
Julius Werner81dc20e2020-10-15 17:37:57 -0700971 struct cbfs_file_attr_stageheader *stage = NULL;
972 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
973 attr != NULL; attr = cbfs_file_next_attr(entry, attr)) {
Alex James02001a382021-12-19 16:41:59 -0600974 if (be32toh(attr->tag) == CBFS_FILE_ATTR_TAG_STAGEHEADER) {
Julius Werner81dc20e2020-10-15 17:37:57 -0700975 stage = (struct cbfs_file_attr_stageheader *)attr;
976 break;
977 }
978 }
979
980 if (stage == NULL) {
981 ERROR("Stage header not found for %s\n", entry->filename);
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500982 return -1;
983 }
984
985 if (init_elf_from_arch(&ehdr, arch))
986 return -1;
987
Aaron Durbin694fd132015-10-28 11:39:34 -0500988 /* Attempt rmodule translation first. */
989 rmod_ret = rmodule_stage_to_elf(&ehdr, buff);
990
991 if (rmod_ret < 0) {
992 ERROR("rmodule parsing failed\n");
993 return -1;
994 } else if (rmod_ret == 0)
995 return 0;
996
997 /* Rmodule couldn't do anything with the data. Continue on with SELF. */
998
Alex James02001a382021-12-19 16:41:59 -0600999 ehdr.e_entry = be64toh(stage->loadaddr) + be32toh(stage->entry_offset);
Julius Werner81dc20e2020-10-15 17:37:57 -07001000
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001001 ew = elf_writer_init(&ehdr);
1002 if (ew == NULL) {
1003 ERROR("Unable to init ELF writer.\n");
1004 return -1;
1005 }
1006
1007 memset(&shdr, 0, sizeof(shdr));
1008 shdr.sh_type = SHT_PROGBITS;
1009 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
Alex James02001a382021-12-19 16:41:59 -06001010 shdr.sh_addr = be64toh(stage->loadaddr);
Julius Werner81dc20e2020-10-15 17:37:57 -07001011 shdr.sh_size = buffer_size(buff);
Alex James02001a382021-12-19 16:41:59 -06001012 empty_sz = be32toh(stage->memlen) - buffer_size(buff);
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001013
1014 if (elf_writer_add_section(ew, &shdr, buff, ".program")) {
1015 ERROR("Unable to add ELF section: .program\n");
1016 elf_writer_destroy(ew);
1017 return -1;
1018 }
1019
1020 if (empty_sz != 0) {
1021 struct buffer b;
1022
1023 buffer_init(&b, NULL, NULL, 0);
1024 memset(&shdr, 0, sizeof(shdr));
1025 shdr.sh_type = SHT_NOBITS;
1026 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
Alex Jamesb3398ba2022-01-08 01:29:29 -06001027 shdr.sh_addr = be64toh(stage->loadaddr) + buffer_size(buff);
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001028 shdr.sh_size = empty_sz;
1029 if (elf_writer_add_section(ew, &shdr, &b, ".empty")) {
1030 ERROR("Unable to add ELF section: .empty\n");
1031 elf_writer_destroy(ew);
1032 return -1;
1033 }
1034 }
1035
1036 if (elf_writer_serialize(ew, &elf_out)) {
1037 ERROR("Unable to create ELF file from stage.\n");
1038 elf_writer_destroy(ew);
1039 return -1;
1040 }
1041
1042 /* Flip buffer with the created ELF one. */
1043 buffer_delete(buff);
1044 *buff = elf_out;
1045
1046 elf_writer_destroy(ew);
1047
1048 return 0;
1049}
1050
Julius Werner81dc20e2020-10-15 17:37:57 -07001051static int cbfs_payload_make_elf(struct buffer *buff, uint32_t arch,
1052 unused struct cbfs_file *entry)
Antonello Dettorifda691e2016-06-09 12:35:36 +02001053{
1054 Elf64_Ehdr ehdr;
1055 Elf64_Shdr shdr;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001056 struct cbfs_payload_segment *segs = NULL;
Jonathan Neuschäfer5de54582016-08-09 15:01:58 +02001057 struct elf_writer *ew = NULL;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001058 struct buffer elf_out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001059 int segments = 0;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001060 int retval = -1;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001061
Antonello Dettori0b806282016-06-26 00:24:25 +02001062 if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
1063 ERROR("You need to specify -m ARCH.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001064 goto out;
Antonello Dettori0b806282016-06-26 00:24:25 +02001065 }
1066
Antonello Dettorifda691e2016-06-09 12:35:36 +02001067 /* Count the number of segments inside buffer */
1068 while (true) {
1069 uint32_t payload_type = 0;
1070
1071 struct cbfs_payload_segment *seg;
1072
1073 seg = buffer_get(buff);
1074 payload_type = read_be32(&seg[segments].type);
1075
1076 if (payload_type == PAYLOAD_SEGMENT_CODE) {
1077 segments++;
1078 } else if (payload_type == PAYLOAD_SEGMENT_DATA) {
1079 segments++;
1080 } else if (payload_type == PAYLOAD_SEGMENT_BSS) {
1081 segments++;
1082 } else if (payload_type == PAYLOAD_SEGMENT_PARAMS) {
1083 segments++;
1084 } else if (payload_type == PAYLOAD_SEGMENT_ENTRY) {
1085 /* The last segment in a payload is always ENTRY as
1086 * specified by the parse_elf_to_payload() function.
1087 * Therefore there is no need to continue looking for
1088 * segments.*/
1089 segments++;
1090 break;
1091 } else {
1092 ERROR("Unknown payload segment type: %x\n",
1093 payload_type);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001094 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001095 }
1096 }
1097
1098 segs = malloc(segments * sizeof(*segs));
1099
1100 /* Decode xdr segments */
1101 for (int i = 0; i < segments; i++) {
1102 struct cbfs_payload_segment *serialized_seg = buffer_get(buff);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001103 xdr_get_seg(&segs[i], &serialized_seg[i]);
1104 }
1105
1106 if (cbfs_payload_decompress(segs, buff, segments)) {
1107 ERROR("Failed to decompress payload.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001108 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001109 }
1110
1111 if (init_elf_from_arch(&ehdr, arch))
Furquan Shaikh7b405172016-08-05 08:20:37 -07001112 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001113
1114 ehdr.e_entry = segs[segments-1].load_addr;
1115
1116 ew = elf_writer_init(&ehdr);
1117 if (ew == NULL) {
1118 ERROR("Unable to init ELF writer.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001119 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001120 }
1121
1122 for (int i = 0; i < segments; i++) {
1123 struct buffer tbuff;
Furquan Shaikhf3bba442016-08-05 08:12:31 -07001124 size_t empty_sz = 0;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001125
1126 memset(&shdr, 0, sizeof(shdr));
1127 char *name = NULL;
1128
1129 if (segs[i].type == PAYLOAD_SEGMENT_CODE) {
1130 shdr.sh_type = SHT_PROGBITS;
1131 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
1132 shdr.sh_addr = segs[i].load_addr;
1133 shdr.sh_size = segs[i].len;
1134 empty_sz = segs[i].mem_len - segs[i].len;
1135 name = strdup(".text");
1136 buffer_splice(&tbuff, buff, segs[i].offset,
1137 segs[i].len);
1138 } else if (segs[i].type == PAYLOAD_SEGMENT_DATA) {
1139 shdr.sh_type = SHT_PROGBITS;
1140 shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
1141 shdr.sh_addr = segs[i].load_addr;
1142 shdr.sh_size = segs[i].len;
1143 empty_sz = segs[i].mem_len - segs[i].len;
1144 name = strdup(".data");
1145 buffer_splice(&tbuff, buff, segs[i].offset,
1146 segs[i].len);
1147 } else if (segs[i].type == PAYLOAD_SEGMENT_BSS) {
1148 shdr.sh_type = SHT_NOBITS;
1149 shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
1150 shdr.sh_addr = segs[i].load_addr;
1151 shdr.sh_size = segs[i].len;
1152 name = strdup(".bss");
1153 buffer_splice(&tbuff, buff, 0, 0);
1154 } else if (segs[i].type == PAYLOAD_SEGMENT_PARAMS) {
1155 shdr.sh_type = SHT_NOTE;
1156 shdr.sh_flags = 0;
1157 shdr.sh_size = segs[i].len;
1158 name = strdup(".note.pinfo");
1159 buffer_splice(&tbuff, buff, segs[i].offset,
1160 segs[i].len);
1161 } else if (segs[i].type == PAYLOAD_SEGMENT_ENTRY) {
1162 break;
Patrick Georgidce629b2017-01-13 13:30:54 +01001163 } else {
1164 ERROR("unknown ELF segment type\n");
1165 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001166 }
1167
Patrick Georgidce629b2017-01-13 13:30:54 +01001168 if (!name) {
1169 ERROR("out of memory\n");
1170 goto out;
1171 }
Antonello Dettorifda691e2016-06-09 12:35:36 +02001172
1173 if (elf_writer_add_section(ew, &shdr, &tbuff, name)) {
1174 ERROR("Unable to add ELF section: %s\n", name);
Patrick Georgidce629b2017-01-13 13:30:54 +01001175 free(name);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001176 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001177 }
Patrick Georgidce629b2017-01-13 13:30:54 +01001178 free(name);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001179
1180 if (empty_sz != 0) {
1181 struct buffer b;
1182
1183 buffer_init(&b, NULL, NULL, 0);
1184 memset(&shdr, 0, sizeof(shdr));
1185 shdr.sh_type = SHT_NOBITS;
1186 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
1187 shdr.sh_addr = segs[i].load_addr + segs[i].len;
1188 shdr.sh_size = empty_sz;
1189 name = strdup(".empty");
Patrick Georgidce629b2017-01-13 13:30:54 +01001190 if (!name) {
1191 ERROR("out of memory\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001192 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001193 }
Patrick Georgidce629b2017-01-13 13:30:54 +01001194 if (elf_writer_add_section(ew, &shdr, &b, name)) {
1195 ERROR("Unable to add ELF section: %s\n", name);
1196 free(name);
1197 goto out;
1198 }
1199 free(name);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001200 }
Antonello Dettorifda691e2016-06-09 12:35:36 +02001201 }
1202
1203 if (elf_writer_serialize(ew, &elf_out)) {
Julius Werner81dc20e2020-10-15 17:37:57 -07001204 ERROR("Unable to create ELF file from payload.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001205 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001206 }
1207
1208 /* Flip buffer with the created ELF one. */
1209 buffer_delete(buff);
1210 *buff = elf_out;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001211 retval = 0;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001212
Furquan Shaikh7b405172016-08-05 08:20:37 -07001213out:
1214 free(segs);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001215 elf_writer_destroy(ew);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001216 return retval;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001217}
1218
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001219int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Joel Kitching21fdd892018-08-09 17:49:52 +08001220 const char *filename, uint32_t arch, bool do_processing)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001221{
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001222 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
1223 struct buffer buffer;
1224 if (!entry) {
1225 ERROR("File not found: %s\n", entry_name);
1226 return -1;
1227 }
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001228
Alex James02001a382021-12-19 16:41:59 -06001229 unsigned int compressed_size = be32toh(entry->len);
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001230 unsigned int decompressed_size = 0;
1231 unsigned int compression = cbfs_file_get_compression_info(entry,
1232 &decompressed_size);
Werner Zehbbf1df72018-11-21 13:07:50 +01001233 unsigned int buffer_len;
Joel Kitching21fdd892018-08-09 17:49:52 +08001234 decomp_func_ptr decompress;
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001235
Joel Kitching21fdd892018-08-09 17:49:52 +08001236 if (do_processing) {
1237 decompress = decompression_function(compression);
1238 if (!decompress) {
1239 ERROR("looking up decompression routine failed\n");
1240 return -1;
1241 }
Werner Zehbbf1df72018-11-21 13:07:50 +01001242 buffer_len = decompressed_size;
Joel Kitching21fdd892018-08-09 17:49:52 +08001243 } else {
1244 /* Force nop decompression */
1245 decompress = decompression_function(CBFS_COMPRESS_NONE);
Werner Zehbbf1df72018-11-21 13:07:50 +01001246 buffer_len = compressed_size;
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001247 }
1248
Joel Kitching21fdd892018-08-09 17:49:52 +08001249 LOG("Found file %.30s at 0x%x, type %.12s, compressed %d, size %d\n",
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001250 entry_name, cbfs_get_entry_addr(image, entry),
Alex James02001a382021-12-19 16:41:59 -06001251 get_cbfs_entry_type_name(be32toh(entry->type)), compressed_size,
Joel Kitching21fdd892018-08-09 17:49:52 +08001252 decompressed_size);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001253
Aaron Durbin539aed02015-10-23 17:42:32 -05001254 buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0);
Werner Zehbbf1df72018-11-21 13:07:50 +01001255 buffer.data = malloc(buffer_len);
1256 buffer.size = buffer_len;
Aaron Durbin539aed02015-10-23 17:42:32 -05001257
Joel Kitching21fdd892018-08-09 17:49:52 +08001258 if (decompress(CBFS_SUBHEADER(entry), compressed_size,
1259 buffer.data, buffer.size, NULL)) {
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001260 ERROR("decompression failed for %s\n", entry_name);
Aaron Durbin539aed02015-10-23 17:42:32 -05001261 buffer_delete(&buffer);
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001262 return -1;
1263 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001264
1265 /*
Julius Werner81dc20e2020-10-15 17:37:57 -07001266 * We want to export stages and payloads as ELFs, not with coreboot's
1267 * custom stage/SELF binary formats, so we need to do extra processing
1268 * to turn them back into an ELF.
Aaron Durbin539aed02015-10-23 17:42:32 -05001269 */
Joel Kitching21fdd892018-08-09 17:49:52 +08001270 if (do_processing) {
Julius Werner81dc20e2020-10-15 17:37:57 -07001271 int (*make_elf)(struct buffer *, uint32_t,
1272 struct cbfs_file *) = NULL;
Alex James02001a382021-12-19 16:41:59 -06001273 switch (be32toh(entry->type)) {
Julius Wernerd4775652020-03-13 16:43:34 -07001274 case CBFS_TYPE_STAGE:
Joel Kitching21fdd892018-08-09 17:49:52 +08001275 make_elf = cbfs_stage_make_elf;
1276 break;
Julius Wernerd4775652020-03-13 16:43:34 -07001277 case CBFS_TYPE_SELF:
Joel Kitching21fdd892018-08-09 17:49:52 +08001278 make_elf = cbfs_payload_make_elf;
1279 break;
Aaron Durbin539aed02015-10-23 17:42:32 -05001280 }
Julius Werner81dc20e2020-10-15 17:37:57 -07001281 if (make_elf && make_elf(&buffer, arch, entry)) {
Joel Kitching21fdd892018-08-09 17:49:52 +08001282 ERROR("Failed to write %s into %s.\n",
1283 entry_name, filename);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001284 buffer_delete(&buffer);
1285 return -1;
1286 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001287 }
1288
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001289 if (buffer_write_file(&buffer, filename) != 0) {
1290 ERROR("Failed to write %s into %s.\n",
1291 entry_name, filename);
Aaron Durbin539aed02015-10-23 17:42:32 -05001292 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001293 return -1;
1294 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001295
1296 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001297 INFO("Successfully dumped the file to: %s\n", filename);
1298 return 0;
1299}
1300
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001301int cbfs_remove_entry(struct cbfs_image *image, const char *name)
1302{
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +02001303 struct cbfs_file *entry;
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001304 entry = cbfs_get_entry(image, name);
1305 if (!entry) {
1306 ERROR("CBFS file %s not found.\n", name);
1307 return -1;
1308 }
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001309 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
Patrick Georgic569b8b2015-07-15 16:42:38 +02001310 entry->filename, cbfs_get_entry_addr(image, entry));
Alex James02001a382021-12-19 16:41:59 -06001311 entry->type = htobe32(CBFS_TYPE_DELETED);
Julius Werner7066a1e2020-04-02 15:49:34 -07001312 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001313 return 0;
1314}
1315
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001316int cbfs_print_header_info(struct cbfs_image *image)
1317{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001318 char *name = strdup(image->buffer.name);
Sol Boucher3e060ed2015-05-05 15:40:15 -07001319 assert(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001320 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -08001321 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001322 basename(name),
1323 image->buffer.size / 1024,
Sol Boucher3e060ed2015-05-05 15:40:15 -07001324 image->header.bootblocksize,
1325 image->header.romsize,
1326 image->header.offset,
1327 image->header.align,
1328 arch_to_string(image->header.architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001329 free(name);
1330 return 0;
1331}
1332
Julius Werner81dc20e2020-10-15 17:37:57 -07001333static int cbfs_print_stage_info(struct cbfs_file *entry, FILE* fp)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001334{
Julius Werner81dc20e2020-10-15 17:37:57 -07001335
1336 struct cbfs_file_attr_stageheader *stage = NULL;
1337 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
1338 attr != NULL; attr = cbfs_file_next_attr(entry, attr)) {
Alex James02001a382021-12-19 16:41:59 -06001339 if (be32toh(attr->tag) == CBFS_FILE_ATTR_TAG_STAGEHEADER) {
Julius Werner81dc20e2020-10-15 17:37:57 -07001340 stage = (struct cbfs_file_attr_stageheader *)attr;
1341 break;
1342 }
1343 }
1344
1345 if (stage == NULL) {
1346 fprintf(fp, " ERROR: stage header not found!\n");
1347 return -1;
1348 }
1349
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001350 fprintf(fp,
Julius Werner81dc20e2020-10-15 17:37:57 -07001351 " entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
1352 "memlen: %d\n",
Alex James02001a382021-12-19 16:41:59 -06001353 be64toh(stage->loadaddr) + be32toh(stage->entry_offset),
1354 be64toh(stage->loadaddr),
1355 be32toh(stage->memlen));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001356 return 0;
1357}
1358
Hung-Te Lin0780d672014-05-16 10:14:05 +08001359static int cbfs_print_decoded_payload_segment_info(
1360 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001361{
Hung-Te Lin0780d672014-05-16 10:14:05 +08001362 /* The input (seg) must be already decoded by
1363 * cbfs_decode_payload_segment.
1364 */
1365 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001366 case PAYLOAD_SEGMENT_CODE:
1367 case PAYLOAD_SEGMENT_DATA:
1368 fprintf(fp, " %s (%s compression, offset: 0x%x, "
1369 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001370 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001371 "code " : "data"),
1372 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +08001373 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001374 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +08001375 seg->offset, seg->load_addr, seg->len,
1376 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001377 break;
1378
1379 case PAYLOAD_SEGMENT_ENTRY:
1380 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001381 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001382 break;
1383
1384 case PAYLOAD_SEGMENT_BSS:
1385 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
1386 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001387 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001388 break;
1389
1390 case PAYLOAD_SEGMENT_PARAMS:
1391 fprintf(fp, " parameters\n");
1392 break;
1393
1394 default:
1395 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
1396 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001397 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001398 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +08001399 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001400 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +08001401 seg->offset, seg->load_addr, seg->len,
1402 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001403 break;
1404 }
1405 return 0;
1406}
1407
1408int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001409 void *arg)
1410{
Patrick Georgic569b8b2015-07-15 16:42:38 +02001411 const char *name = entry->filename;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001412 struct cbfs_payload_segment *payload;
1413 FILE *fp = (FILE *)arg;
1414
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001415 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001416 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1417 cbfs_get_entry_addr(image, entry));
1418 return -1;
1419 }
1420 if (!fp)
1421 fp = stdout;
1422
Patrick Georgic82725c2015-08-26 12:13:03 +02001423 unsigned int decompressed_size = 0;
Patrick Georgia71c83f2015-08-26 12:23:26 +02001424 unsigned int compression = cbfs_file_get_compression_info(entry,
1425 &decompressed_size);
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001426 const char *compression_name = lookup_name_by_type(
1427 types_cbfs_compression, compression, "????");
Patrick Georgic82725c2015-08-26 12:13:03 +02001428
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001429 if (compression == CBFS_COMPRESS_NONE)
1430 fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s\n",
Patrick Georgic82725c2015-08-26 12:13:03 +02001431 *name ? name : "(empty)",
1432 cbfs_get_entry_addr(image, entry),
Alex James02001a382021-12-19 16:41:59 -06001433 get_cbfs_entry_type_name(be32toh(entry->type)),
1434 be32toh(entry->len),
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001435 compression_name
Patrick Georgic82725c2015-08-26 12:13:03 +02001436 );
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001437 else
1438 fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s (%d decompressed)\n",
1439 *name ? name : "(empty)",
1440 cbfs_get_entry_addr(image, entry),
Alex James02001a382021-12-19 16:41:59 -06001441 get_cbfs_entry_type_name(be32toh(entry->type)),
1442 be32toh(entry->len),
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001443 compression_name,
1444 decompressed_size
1445 );
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001446
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001447 if (!verbose)
1448 return 0;
1449
Julius Wernerd4775652020-03-13 16:43:34 -07001450 struct cbfs_file_attr_hash *attr = NULL;
1451 while ((attr = cbfs_file_get_next_hash(entry, attr)) != NULL) {
1452 size_t hash_len = vb2_digest_size(attr->hash.algo);
1453 if (!hash_len) {
1454 fprintf(fp, "invalid/unsupported hash algorithm: %d\n",
1455 attr->hash.algo);
Patrick Georgi89f20342015-10-01 15:54:04 +02001456 break;
1457 }
Julius Wernerd4775652020-03-13 16:43:34 -07001458 char *hash_str = bintohex(attr->hash.raw, hash_len);
Julius Wernerd96ca242022-08-08 18:08:35 -07001459 int valid = vb2_hash_verify(false, CBFS_SUBHEADER(entry),
Alex James02001a382021-12-19 16:41:59 -06001460 be32toh(entry->len), &attr->hash) == VB2_SUCCESS;
Patrick Georgi89f20342015-10-01 15:54:04 +02001461 const char *valid_str = valid ? "valid" : "invalid";
1462
1463 fprintf(fp, " hash %s:%s %s\n",
Julius Wernerd4775652020-03-13 16:43:34 -07001464 vb2_get_hash_algorithm_name(attr->hash.algo),
Patrick Georgi89f20342015-10-01 15:54:04 +02001465 hash_str, valid_str);
1466 free(hash_str);
1467 }
1468
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001469 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
Alex James02001a382021-12-19 16:41:59 -06001470 cbfs_get_entry_addr(image, entry), be32toh(entry->offset),
1471 cbfs_get_entry_addr(image, entry) + be32toh(entry->offset),
1472 be32toh(entry->len));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001473
1474 /* note the components of the subheader may be in host order ... */
Alex James02001a382021-12-19 16:41:59 -06001475 switch (be32toh(entry->type)) {
Julius Wernerd4775652020-03-13 16:43:34 -07001476 case CBFS_TYPE_STAGE:
Julius Werner81dc20e2020-10-15 17:37:57 -07001477 cbfs_print_stage_info(entry, fp);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001478 break;
1479
Julius Wernerd4775652020-03-13 16:43:34 -07001480 case CBFS_TYPE_SELF:
Paul Menzel831bbe82015-08-08 20:20:57 +02001481 payload = (struct cbfs_payload_segment *)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001482 CBFS_SUBHEADER(entry);
1483 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +08001484 struct cbfs_payload_segment seg;
1485 cbfs_decode_payload_segment(&seg, payload);
1486 cbfs_print_decoded_payload_segment_info(
1487 &seg, fp);
1488 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001489 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +08001490 else
Aaron Durbinca630272014-08-05 10:48:20 -05001491 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001492 }
1493 break;
1494 default:
1495 break;
1496 }
1497 return 0;
1498}
1499
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001500static int cbfs_print_parseable_entry_info(struct cbfs_image *image,
1501 struct cbfs_file *entry, void *arg)
1502{
1503 FILE *fp = (FILE *)arg;
1504 const char *name;
1505 const char *type;
1506 size_t offset;
1507 size_t metadata_size;
1508 size_t data_size;
1509 const char *sep = "\t";
1510
1511 if (!cbfs_is_valid_entry(image, entry)) {
1512 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1513 cbfs_get_entry_addr(image, entry));
1514 return -1;
1515 }
1516
1517 name = entry->filename;
1518 if (*name == '\0')
1519 name = "(empty)";
Alex James02001a382021-12-19 16:41:59 -06001520 type = get_cbfs_entry_type_name(be32toh(entry->type)),
1521 metadata_size = be32toh(entry->offset);
1522 data_size = be32toh(entry->len);
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001523 offset = cbfs_get_entry_addr(image, entry);
1524
1525 fprintf(fp, "%s%s", name, sep);
1526 fprintf(fp, "0x%zx%s", offset, sep);
1527 fprintf(fp, "%s%s", type, sep);
1528 fprintf(fp, "0x%zx%s", metadata_size, sep);
1529 fprintf(fp, "0x%zx%s", data_size, sep);
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001530 fprintf(fp, "0x%zx", metadata_size + data_size);
1531
1532 if (verbose) {
1533 unsigned int decompressed_size = 0;
1534 unsigned int compression = cbfs_file_get_compression_info(entry,
1535 &decompressed_size);
1536 if (compression != CBFS_COMPRESS_NONE)
1537 fprintf(fp, "%scomp:%s:0x%x", sep, lookup_name_by_type(
1538 types_cbfs_compression, compression, "????"),
1539 decompressed_size);
1540
1541 struct cbfs_file_attr_hash *attr = NULL;
1542 while ((attr = cbfs_file_get_next_hash(entry, attr)) != NULL) {
1543 size_t hash_len = vb2_digest_size(attr->hash.algo);
1544 if (!hash_len)
1545 continue;
1546 char *hash_str = bintohex(attr->hash.raw, hash_len);
Julius Wernerd96ca242022-08-08 18:08:35 -07001547 int valid = vb2_hash_verify(false, CBFS_SUBHEADER(entry),
Alex James02001a382021-12-19 16:41:59 -06001548 be32toh(entry->len), &attr->hash) == VB2_SUCCESS;
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001549 fprintf(fp, "%shash:%s:%s:%s", sep,
1550 vb2_get_hash_algorithm_name(attr->hash.algo),
1551 hash_str, valid ? "valid" : "invalid");
1552 free(hash_str);
1553 }
1554 }
1555 fprintf(fp, "\n");
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001556
1557 return 0;
1558}
1559
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001560void cbfs_print_directory(struct cbfs_image *image)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001561{
Sol Boucher67a0a862015-03-18 12:36:27 -07001562 if (cbfs_is_legacy_cbfs(image))
1563 cbfs_print_header_info(image);
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001564 printf("%-30s %-10s %-12s Size Comp\n", "Name", "Offset", "Type");
Julius Werner7066a1e2020-04-02 15:49:34 -07001565 cbfs_legacy_walk(image, cbfs_print_entry_info, NULL);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001566}
1567
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001568void cbfs_print_parseable_directory(struct cbfs_image *image)
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001569{
Furquan Shaikh161d2332016-05-26 14:41:02 -07001570 size_t i;
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001571 const char *header[] = {
1572 "Name",
1573 "Offset",
1574 "Type",
1575 "Metadata Size",
1576 "Data Size",
1577 "Total Size",
1578 };
1579 const char *sep = "\t";
1580
1581 for (i = 0; i < ARRAY_SIZE(header) - 1; i++)
1582 fprintf(stdout, "%s%s", header[i], sep);
1583 fprintf(stdout, "%s\n", header[i]);
Julius Werner7066a1e2020-04-02 15:49:34 -07001584 cbfs_legacy_walk(image, cbfs_print_parseable_entry_info, stdout);
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001585}
1586
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001587int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -08001588 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001589{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001590 struct cbfs_file *next;
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001591 uint32_t next_addr = 0;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001592
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001593 /* We don't return here even if this entry is already empty because we
1594 want to merge the empty entries following after it. */
1595
1596 /* Loop until non-empty entry is found, starting from the current entry.
1597 After the loop, next_addr points to the next non-empty entry. */
1598 next = entry;
Alex James02001a382021-12-19 16:41:59 -06001599 while (be32toh(next->type) == CBFS_TYPE_DELETED ||
1600 be32toh(next->type) == CBFS_TYPE_NULL) {
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001601 next = cbfs_find_next_entry(image, next);
1602 if (!next)
1603 break;
1604 next_addr = cbfs_get_entry_addr(image, next);
1605 if (!cbfs_is_valid_entry(image, next))
1606 /* 'next' could be the end of cbfs */
1607 break;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001608 }
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001609
1610 if (!next_addr)
1611 /* Nothing to empty */
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001612 return 0;
1613
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001614 /* We can return here if we find only a single empty entry.
1615 For simplicity, we just proceed (and make it empty again). */
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001616
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001617 /* We're creating one empty entry for combined empty spaces */
1618 uint32_t addr = cbfs_get_entry_addr(image, entry);
1619 size_t len = next_addr - addr - cbfs_calculate_file_header_size("");
1620 DEBUG("join_empty_entry: [0x%x, 0x%x) len=%zu\n", addr, next_addr, len);
Julius Wernerd4775652020-03-13 16:43:34 -07001621 cbfs_create_empty_entry(entry, CBFS_TYPE_NULL, len, "");
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001622
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001623 return 0;
1624}
1625
Julius Werner7066a1e2020-04-02 15:49:34 -07001626int cbfs_legacy_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001627 void *arg)
1628{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001629 int count = 0;
1630 struct cbfs_file *entry;
1631 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001632 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001633 entry = cbfs_find_next_entry(image, entry)) {
1634 count ++;
1635 if (callback(image, entry, arg) != 0)
1636 break;
1637 }
1638 return count;
1639}
1640
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001641static int cbfs_header_valid(struct cbfs_header *header)
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001642{
Alex James02001a382021-12-19 16:41:59 -06001643 if ((be32toh(header->magic) == CBFS_HEADER_MAGIC) &&
1644 ((be32toh(header->version) == CBFS_HEADER_VERSION1) ||
1645 (be32toh(header->version) == CBFS_HEADER_VERSION2)) &&
1646 (be32toh(header->offset) < be32toh(header->romsize)))
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001647 return 1;
1648 return 0;
1649}
1650
1651struct cbfs_header *cbfs_find_header(char *data, size_t size,
1652 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001653{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001654 size_t offset;
1655 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001656 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001657 struct cbfs_header *header, *result = NULL;
1658
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001659 if (forced_offset < (size - sizeof(struct cbfs_header))) {
1660 /* Check if the forced header is valid. */
1661 header = (struct cbfs_header *)(data + forced_offset);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001662 if (cbfs_header_valid(header))
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001663 return header;
1664 return NULL;
1665 }
1666
Julius Wernerefcee762014-11-10 13:14:24 -08001667 // Try finding relative offset of master header at end of file first.
1668 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
1669 offset = size + rel_offset;
1670 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
1671 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001672
Hung-Te Lineab2c812013-01-29 01:56:17 +08001673 if (offset >= size - sizeof(*header) ||
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001674 !cbfs_header_valid((struct cbfs_header *)(data + offset))) {
Julius Wernerefcee762014-11-10 13:14:24 -08001675 // Some use cases append non-CBFS data to the end of the ROM.
1676 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +08001677 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001678 }
Hung-Te Lineab2c812013-01-29 01:56:17 +08001679
1680 for (; offset + sizeof(*header) < size; offset++) {
1681 header = (struct cbfs_header *)(data + offset);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001682 if (!cbfs_header_valid(header))
Hung-Te Lineab2c812013-01-29 01:56:17 +08001683 continue;
Julius Wernerefcee762014-11-10 13:14:24 -08001684 if (!found++)
1685 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001686 }
Julius Wernerefcee762014-11-10 13:14:24 -08001687 if (found > 1)
1688 // Top-aligned images usually have a working relative offset
1689 // field, so this is more likely to happen on bottom-aligned
1690 // ones (where the first header is the "outermost" one)
1691 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +08001692 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001693 return result;
1694}
1695
1696
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001697struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
1698{
Sol Boucher3e060ed2015-05-05 15:40:15 -07001699 assert(image);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001700 if (image->has_header)
1701 /* header.offset is relative to start of flash, not
1702 * start of region, so use it with the full image.
1703 */
1704 return (struct cbfs_file *)
1705 (buffer_get_original_backing(&image->buffer) +
1706 image->header.offset);
1707 else
1708 return (struct cbfs_file *)buffer_get(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001709}
1710
1711struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001712 struct cbfs_file *entry)
1713{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001714 uint32_t addr = cbfs_get_entry_addr(image, entry);
Julius Wernerd4775652020-03-13 16:43:34 -07001715 int align = image->has_header ? image->header.align : CBFS_ALIGNMENT;
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001716 assert(entry && cbfs_is_valid_entry(image, entry));
Alex James02001a382021-12-19 16:41:59 -06001717 addr += be32toh(entry->offset) + be32toh(entry->len);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001718 addr = align_up(addr, align);
1719 return (struct cbfs_file *)(image->buffer.data + addr);
1720}
1721
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001722uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
1723{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001724 assert(image && image->buffer.data && entry);
1725 return (int32_t)((char *)entry - image->buffer.data);
1726}
1727
Sol Boucher67a0a862015-03-18 12:36:27 -07001728int cbfs_is_valid_cbfs(struct cbfs_image *image)
1729{
1730 return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC,
1731 strlen(CBFS_FILE_MAGIC));
1732}
1733
1734int cbfs_is_legacy_cbfs(struct cbfs_image *image)
1735{
1736 return image->has_header;
1737}
1738
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001739int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
1740{
Sol Bouchere3260a02015-03-25 13:40:08 -07001741 uint32_t offset = cbfs_get_entry_addr(image, entry);
1742
1743 if (offset >= image->buffer.size)
1744 return 0;
1745
1746 struct buffer entry_data;
1747 buffer_clone(&entry_data, &image->buffer);
1748 buffer_seek(&entry_data, offset);
1749 return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
Sol Boucher67a0a862015-03-18 12:36:27 -07001750 strlen(CBFS_FILE_MAGIC));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001751}
1752
Patrick Georgi57edf162015-08-12 09:20:11 +02001753struct cbfs_file *cbfs_create_file_header(int type,
Vadim Bendebury45e59972014-12-23 15:59:57 -08001754 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001755{
Julius Wernerd4775652020-03-13 16:43:34 -07001756 struct cbfs_file *entry = malloc(CBFS_METADATA_MAX_SIZE);
1757 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, CBFS_METADATA_MAX_SIZE);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001758 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
Alex James02001a382021-12-19 16:41:59 -06001759 entry->type = htobe32(type);
1760 entry->len = htobe32(len);
Patrick Georgi0d618af2015-07-15 18:28:23 +02001761 entry->attributes_offset = 0;
Alex James02001a382021-12-19 16:41:59 -06001762 entry->offset = htobe32(cbfs_calculate_file_header_size(name));
1763 memset(entry->filename, 0, be32toh(entry->offset) - sizeof(*entry));
Patrick Georgic569b8b2015-07-15 16:42:38 +02001764 strcpy(entry->filename, name);
Patrick Georgi57edf162015-08-12 09:20:11 +02001765 return entry;
1766}
1767
1768int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
1769 size_t len, const char *name)
1770{
1771 struct cbfs_file *tmp = cbfs_create_file_header(type, len, name);
Alex James02001a382021-12-19 16:41:59 -06001772 memcpy(entry, tmp, be32toh(tmp->offset));
Patrick Georgi57edf162015-08-12 09:20:11 +02001773 free(tmp);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001774 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1775 return 0;
1776}
1777
Patrick Georgi2c615062015-07-15 20:49:00 +02001778struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file)
1779{
1780 /* attributes_offset should be 0 when there is no attribute, but all
1781 * values that point into the cbfs_file header are invalid, too. */
Alex James02001a382021-12-19 16:41:59 -06001782 if (be32toh(file->attributes_offset) <= sizeof(*file))
Patrick Georgi2c615062015-07-15 20:49:00 +02001783 return NULL;
1784
1785 /* There needs to be enough space for the file header and one
1786 * attribute header for this to make sense. */
Alex James02001a382021-12-19 16:41:59 -06001787 if (be32toh(file->offset) <=
Patrick Georgi2c615062015-07-15 20:49:00 +02001788 sizeof(*file) + sizeof(struct cbfs_file_attribute))
1789 return NULL;
1790
1791 return (struct cbfs_file_attribute *)
Alex James02001a382021-12-19 16:41:59 -06001792 (((uint8_t *)file) + be32toh(file->attributes_offset));
Patrick Georgi2c615062015-07-15 20:49:00 +02001793}
1794
1795struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
1796 struct cbfs_file_attribute *attr)
1797{
1798 /* ex falso sequitur quodlibet */
1799 if (attr == NULL)
1800 return NULL;
1801
1802 /* Is there enough space for another attribute? */
Alex James02001a382021-12-19 16:41:59 -06001803 if ((uint8_t *)attr + be32toh(attr->len) +
Patrick Rudolphe28fa402019-02-14 11:19:07 +01001804 sizeof(struct cbfs_file_attribute) >
Alex James02001a382021-12-19 16:41:59 -06001805 (uint8_t *)file + be32toh(file->offset))
Patrick Georgi2c615062015-07-15 20:49:00 +02001806 return NULL;
1807
1808 struct cbfs_file_attribute *next = (struct cbfs_file_attribute *)
Alex James02001a382021-12-19 16:41:59 -06001809 (((uint8_t *)attr) + be32toh(attr->len));
Patrick Georgi2c615062015-07-15 20:49:00 +02001810 /* If any, "unused" attributes must come last. */
Alex James02001a382021-12-19 16:41:59 -06001811 if (be32toh(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED)
Patrick Georgi2c615062015-07-15 20:49:00 +02001812 return NULL;
Alex James02001a382021-12-19 16:41:59 -06001813 if (be32toh(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
Patrick Georgi2c615062015-07-15 20:49:00 +02001814 return NULL;
1815
1816 return next;
1817}
1818
1819struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
1820 uint32_t tag,
1821 uint32_t size)
1822{
Julius Werner5779ca72020-11-20 16:12:40 -08001823 assert(IS_ALIGNED(size, CBFS_ATTRIBUTE_ALIGN));
Patrick Georgi2c615062015-07-15 20:49:00 +02001824 struct cbfs_file_attribute *attr, *next;
1825 next = cbfs_file_first_attr(header);
1826 do {
1827 attr = next;
1828 next = cbfs_file_next_attr(header, attr);
1829 } while (next != NULL);
Alex James02001a382021-12-19 16:41:59 -06001830 uint32_t header_size = be32toh(header->offset) + size;
Julius Wernerd4775652020-03-13 16:43:34 -07001831 if (header_size > CBFS_METADATA_MAX_SIZE) {
Patrick Georgi2c615062015-07-15 20:49:00 +02001832 DEBUG("exceeding allocated space for cbfs_file headers");
1833 return NULL;
1834 }
1835 /* attr points to the last valid attribute now.
1836 * If NULL, we have to create the first one. */
1837 if (attr == NULL) {
1838 /* New attributes start where the header ends.
Elyes HAOUAS3db01982018-08-23 18:08:20 +02001839 * header->offset is later set to accommodate the
Patrick Georgi2c615062015-07-15 20:49:00 +02001840 * additional structure.
Elyes HAOUAS3db01982018-08-23 18:08:20 +02001841 * No endianness translation necessary here, because both
Patrick Georgi2c615062015-07-15 20:49:00 +02001842 * fields are encoded the same way. */
1843 header->attributes_offset = header->offset;
1844 attr = (struct cbfs_file_attribute *)
1845 (((uint8_t *)header) +
Alex James02001a382021-12-19 16:41:59 -06001846 be32toh(header->attributes_offset));
Patrick Georgi2c615062015-07-15 20:49:00 +02001847 } else {
1848 attr = (struct cbfs_file_attribute *)
1849 (((uint8_t *)attr) +
Alex James02001a382021-12-19 16:41:59 -06001850 be32toh(attr->len));
Patrick Georgi2c615062015-07-15 20:49:00 +02001851 }
Alex James02001a382021-12-19 16:41:59 -06001852 header->offset = htobe32(header_size);
Julius Wernerd4775652020-03-13 16:43:34 -07001853 /* Attributes are expected to be small (much smaller than a flash page)
1854 and not really meant to be overwritten in-place. To avoid surprising
1855 values in reserved fields of attribute structures, initialize them to
1856 0, not 0xff. */
1857 memset(attr, 0, size);
Alex James02001a382021-12-19 16:41:59 -06001858 attr->tag = htobe32(tag);
1859 attr->len = htobe32(size);
Patrick Georgi2c615062015-07-15 20:49:00 +02001860 return attr;
1861}
1862
Patrick Georgi89f20342015-10-01 15:54:04 +02001863int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer,
Julius Wernerd4775652020-03-13 16:43:34 -07001864 enum vb2_hash_algorithm alg)
Patrick Georgi89f20342015-10-01 15:54:04 +02001865{
Julius Wernerd4775652020-03-13 16:43:34 -07001866 if (!vb2_digest_size(alg))
Patrick Georgi89f20342015-10-01 15:54:04 +02001867 return -1;
1868
Julius Wernerd4775652020-03-13 16:43:34 -07001869 struct cbfs_file_attr_hash *attr =
Patrick Georgi89f20342015-10-01 15:54:04 +02001870 (struct cbfs_file_attr_hash *)cbfs_add_file_attr(header,
Julius Wernerd4775652020-03-13 16:43:34 -07001871 CBFS_FILE_ATTR_TAG_HASH, cbfs_file_attr_hash_size(alg));
Patrick Georgi89f20342015-10-01 15:54:04 +02001872
Julius Wernerd4775652020-03-13 16:43:34 -07001873 if (attr == NULL)
Patrick Georgi89f20342015-10-01 15:54:04 +02001874 return -1;
1875
Julius Wernerd96ca242022-08-08 18:08:35 -07001876 if (vb2_hash_calculate(false, buffer_get(buffer), buffer_size(buffer),
Julius Wernerd4775652020-03-13 16:43:34 -07001877 alg, &attr->hash) != VB2_SUCCESS)
Patrick Georgi89f20342015-10-01 15:54:04 +02001878 return -1;
1879
1880 return 0;
1881}
1882
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001883/* Finds a place to hold whole data in same memory page. */
1884static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1885{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001886 if (!page)
1887 return 1;
1888 return (start / page) == (start + size - 1) / page;
1889}
1890
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001891/* Tests if data can fit in a range by given offset:
Aaron Durbind7339412015-09-15 12:50:14 -05001892 * start ->| metadata_size | offset (+ size) |<- end
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001893 */
Aaron Durbind7339412015-09-15 12:50:14 -05001894static int is_in_range(size_t start, size_t end, size_t metadata_size,
1895 size_t offset, size_t size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001896{
Aaron Durbind7339412015-09-15 12:50:14 -05001897 return (offset >= start + metadata_size && offset + size <= end);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001898}
1899
Werner Zeh95bfcae2016-01-25 12:47:20 +01001900static size_t absolute_align(const struct cbfs_image *image, size_t val,
1901 size_t align)
1902{
1903 const size_t region_offset = buffer_offset(&image->buffer);
1904 /* To perform alignment on absolute address, take the region offset */
1905 /* of the image into account. */
1906 return align_up(val + region_offset, align) - region_offset;
1907
1908}
1909
Aaron Durbind7339412015-09-15 12:50:14 -05001910int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size,
1911 size_t page_size, size_t align, size_t metadata_size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001912{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001913 struct cbfs_file *entry;
1914 size_t need_len;
Aaron Durbind7339412015-09-15 12:50:14 -05001915 size_t addr, addr_next, addr2, addr3, offset;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001916
1917 /* Default values: allow fitting anywhere in ROM. */
1918 if (!page_size)
Sol Boucher67a0a862015-03-18 12:36:27 -07001919 page_size = image->has_header ? image->header.romsize :
1920 image->buffer.size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001921 if (!align)
1922 align = 1;
1923
1924 if (size > page_size)
Aaron Durbind7339412015-09-15 12:50:14 -05001925 ERROR("Input file size (%zd) greater than page size (%zd).\n",
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001926 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001927
Aaron Durbind7339412015-09-15 12:50:14 -05001928 size_t image_align = image->has_header ? image->header.align :
Julius Wernerd4775652020-03-13 16:43:34 -07001929 CBFS_ALIGNMENT;
Sol Boucher67a0a862015-03-18 12:36:27 -07001930 if (page_size % image_align)
Aaron Durbind7339412015-09-15 12:50:14 -05001931 WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001932 __func__, page_size, image_align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001933
Aaron Durbind7339412015-09-15 12:50:14 -05001934 need_len = metadata_size + size;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001935
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001936 // Merge empty entries to build get max available space.
Julius Werner7066a1e2020-04-02 15:49:34 -07001937 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001938
1939 /* Three cases of content location on memory page:
1940 * case 1.
1941 * | PAGE 1 | PAGE 2 |
1942 * | <header><content>| Fit. Return start of content.
1943 *
1944 * case 2.
1945 * | PAGE 1 | PAGE 2 |
1946 * | <header><content> | Fits when we shift content to align
1947 * shift-> | <header>|<content> | at starting of PAGE 2.
1948 *
1949 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001950 * | PAGE 1 | PAGE 2 | PAGE 3 |
1951 * | <header>< content > | Can't fit. If we shift content to
1952 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1953 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001954 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001955 * The returned address can be then used as "base-address" (-b) in add-*
1956 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1957 * For stage targets, the address is also used to re-link stage before
1958 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001959 */
1960 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001961 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001962 entry = cbfs_find_next_entry(image, entry)) {
1963
Alex James02001a382021-12-19 16:41:59 -06001964 uint32_t type = be32toh(entry->type);
Julius Wernerd4775652020-03-13 16:43:34 -07001965 if (type != CBFS_TYPE_NULL)
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001966 continue;
1967
1968 addr = cbfs_get_entry_addr(image, entry);
1969 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
1970 image, entry));
1971 if (addr_next - addr < need_len)
1972 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001973
Werner Zeh95bfcae2016-01-25 12:47:20 +01001974 offset = absolute_align(image, addr + metadata_size, align);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001975 if (is_in_same_page(offset, size, page_size) &&
Aaron Durbind7339412015-09-15 12:50:14 -05001976 is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001977 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001978 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001979 }
1980
1981 addr2 = align_up(addr, page_size);
Werner Zeh95bfcae2016-01-25 12:47:20 +01001982 offset = absolute_align(image, addr2, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001983 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001984 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001985 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001986 }
1987
Aaron Durbind7339412015-09-15 12:50:14 -05001988 /* Assume page_size >= metadata_size so adding one page will
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001989 * definitely provide the space for header. */
Aaron Durbind7339412015-09-15 12:50:14 -05001990 assert(page_size >= metadata_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001991 addr3 = addr2 + page_size;
Werner Zeh95bfcae2016-01-25 12:47:20 +01001992 offset = absolute_align(image, addr3, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001993 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001994 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001995 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001996 }
1997 }
1998 return -1;
1999}