blob: 1fb19bacd65ad34f0834f9b6c8e5e1a2776c28dd [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 */
89 if (((char *)entry) + ntohl(entry->offset) == hdr_loc) {
90 return 0;
91 }
Hung-Te Lin49fcd752013-01-29 03:16:20 +080092 last = entry;
93 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -060094 if ((char *)first < (char *)hdr_loc &&
95 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +080096 WARN("CBFS image was created with old cbfstool with size bug. "
97 "Fixing size in last entry...\n");
Sol Boucher3e060ed2015-05-05 15:40:15 -070098 last->len = htonl(ntohl(last->len) - image->header.align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +080099 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
100 cbfs_get_entry_addr(image, entry),
101 cbfs_get_entry_addr(image,
102 cbfs_find_next_entry(image, last)));
103 }
104 return 0;
105}
106
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800107void cbfs_put_header(void *dest, const struct cbfs_header *header)
108{
109 struct buffer outheader;
110
111 outheader.data = dest;
112 outheader.size = 0;
113
114 xdr_be.put32(&outheader, header->magic);
115 xdr_be.put32(&outheader, header->version);
116 xdr_be.put32(&outheader, header->romsize);
117 xdr_be.put32(&outheader, header->bootblocksize);
118 xdr_be.put32(&outheader, header->align);
119 xdr_be.put32(&outheader, header->offset);
120 xdr_be.put32(&outheader, header->architecture);
121}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600122
Hung-Te Lin0780d672014-05-16 10:14:05 +0800123static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
124 struct cbfs_payload_segment *input)
125{
126 struct buffer seg = {
127 .data = (void *)input,
128 .size = sizeof(*input),
129 };
130 output->type = xdr_be.get32(&seg);
131 output->compression = xdr_be.get32(&seg);
132 output->offset = xdr_be.get32(&seg);
133 output->load_addr = xdr_be.get64(&seg);
134 output->len = xdr_be.get32(&seg);
135 output->mem_len = xdr_be.get32(&seg);
136 assert(seg.size == 0);
137}
138
Patrick Georgia71c83f2015-08-26 12:23:26 +0200139static int cbfs_file_get_compression_info(struct cbfs_file *entry,
140 uint32_t *decompressed_size)
141{
142 unsigned int compression = CBFS_COMPRESS_NONE;
Patrick Georgia2ce7102016-12-14 16:08:52 +0100143 if (decompressed_size)
144 *decompressed_size = ntohl(entry->len);
Patrick Georgia71c83f2015-08-26 12:23:26 +0200145 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
146 attr != NULL;
147 attr = cbfs_file_next_attr(entry, attr)) {
148 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) {
149 struct cbfs_file_attr_compression *ac =
150 (struct cbfs_file_attr_compression *)attr;
151 compression = ntohl(ac->compression);
152 if (decompressed_size)
153 *decompressed_size =
154 ntohl(ac->decompressed_size);
155 }
156 }
157 return compression;
158}
159
Patrick Georgi89f20342015-10-01 15:54:04 +0200160static struct cbfs_file_attr_hash *cbfs_file_get_next_hash(
161 struct cbfs_file *entry, struct cbfs_file_attr_hash *cur)
162{
163 struct cbfs_file_attribute *attr = (struct cbfs_file_attribute *)cur;
164 if (attr == NULL) {
165 attr = cbfs_file_first_attr(entry);
166 if (attr == NULL)
167 return NULL;
168 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
169 return (struct cbfs_file_attr_hash *)attr;
170 }
171 while ((attr = cbfs_file_next_attr(entry, attr)) != NULL) {
172 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
173 return (struct cbfs_file_attr_hash *)attr;
174 };
175 return NULL;
176}
177
Sol Boucher0e539312015-03-05 15:38:03 -0800178void cbfs_get_header(struct cbfs_header *header, void *src)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600179{
180 struct buffer outheader;
181
Sol Boucher0e539312015-03-05 15:38:03 -0800182 outheader.data = src; /* We're not modifying the data */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600183 outheader.size = 0;
184
185 header->magic = xdr_be.get32(&outheader);
186 header->version = xdr_be.get32(&outheader);
187 header->romsize = xdr_be.get32(&outheader);
188 header->bootblocksize = xdr_be.get32(&outheader);
189 header->align = xdr_be.get32(&outheader);
190 header->offset = xdr_be.get32(&outheader);
191 header->architecture = xdr_be.get32(&outheader);
192}
193
Sol Boucher67a0a862015-03-18 12:36:27 -0700194int cbfs_image_create(struct cbfs_image *image, size_t entries_size)
195{
196 assert(image);
197 assert(image->buffer.data);
198
199 size_t empty_header_len = cbfs_calculate_file_header_size("");
200 uint32_t entries_offset = 0;
Julius Wernerd4775652020-03-13 16:43:34 -0700201 uint32_t align = CBFS_ALIGNMENT;
Sol Boucher67a0a862015-03-18 12:36:27 -0700202 if (image->has_header) {
203 entries_offset = image->header.offset;
204
205 if (entries_offset > image->buffer.size) {
206 ERROR("CBFS file entries are located outside CBFS itself\n");
207 return -1;
208 }
209
210 align = image->header.align;
211 }
212
213 // This attribute must be given in order to prove that this module
214 // correctly preserves certain CBFS properties. See the block comment
215 // near the top of this file (and the associated commit message).
216 if (align < empty_header_len) {
217 ERROR("CBFS must be aligned to at least %zu bytes\n",
218 empty_header_len);
219 return -1;
220 }
221
222 if (entries_size > image->buffer.size - entries_offset) {
223 ERROR("CBFS doesn't have enough space to fit its file entries\n");
224 return -1;
225 }
226
227 if (empty_header_len > entries_size) {
228 ERROR("CBFS is too small to fit any header\n");
229 return -1;
230 }
231 struct cbfs_file *entry_header =
232 (struct cbfs_file *)(image->buffer.data + entries_offset);
233 // This alignment is necessary in order to prove that this module
234 // correctly preserves certain CBFS properties. See the block comment
235 // near the top of this file (and the associated commit message).
236 entries_size -= entries_size % align;
237
238 size_t capacity = entries_size - empty_header_len;
239 LOG("Created CBFS (capacity = %zu bytes)\n", capacity);
Julius Wernerd4775652020-03-13 16:43:34 -0700240 return cbfs_create_empty_entry(entry_header, CBFS_TYPE_NULL,
Patrick Georgiedf25d92015-08-12 09:12:06 +0200241 capacity, "");
Sol Boucher67a0a862015-03-18 12:36:27 -0700242}
243
244int cbfs_legacy_image_create(struct cbfs_image *image,
245 uint32_t architecture,
246 uint32_t align,
247 struct buffer *bootblock,
248 uint32_t bootblock_offset,
249 uint32_t header_offset,
250 uint32_t entries_offset)
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800251{
Sol Bouchere3260a02015-03-25 13:40:08 -0700252 assert(image);
253 assert(image->buffer.data);
254 assert(bootblock);
255
Julius Wernerefcee762014-11-10 13:14:24 -0800256 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800257 uint32_t cbfs_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600258 void *header_loc;
Sol Bouchere3260a02015-03-25 13:40:08 -0700259 size_t size = image->buffer.size;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800260
261 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
262 "header=0x%x+0x%zx, entries_offset=0x%x\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700263 bootblock_offset, bootblock->size, header_offset,
264 sizeof(image->header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800265
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800266 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
267 "header=0x%x, entries_offset=0x%x\n",
268 bootblock_offset, header_offset, entries_offset);
269
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800270 // Prepare bootblock
271 if (bootblock_offset + bootblock->size > size) {
272 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
273 bootblock_offset, bootblock->size, size);
274 return -1;
275 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800276 if (entries_offset > bootblock_offset &&
277 entries_offset < bootblock->size) {
278 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
279 bootblock_offset, bootblock->size, entries_offset);
280 return -1;
281 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800282 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
283 bootblock->size);
284
285 // Prepare header
Sol Boucher5bad3952015-05-05 20:35:26 -0700286 if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800287 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700288 header_offset, sizeof(image->header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800289 return -1;
290 }
Sol Boucher3e060ed2015-05-05 15:40:15 -0700291 image->header.magic = CBFS_HEADER_MAGIC;
292 image->header.version = CBFS_HEADER_VERSION;
293 image->header.romsize = size;
294 image->header.bootblocksize = bootblock->size;
295 image->header.align = align;
296 image->header.offset = entries_offset;
297 image->header.architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600298
299 header_loc = (image->buffer.data + header_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700300 cbfs_put_header(header_loc, &image->header);
Sol Boucher67a0a862015-03-18 12:36:27 -0700301 image->has_header = true;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800302
Julius Wernerefcee762014-11-10 13:14:24 -0800303 // The last 4 byte of the image contain the relative offset from the end
304 // of the image to the master header as a 32-bit signed integer. x86
305 // relies on this also being its (memory-mapped, top-aligned) absolute
306 // 32-bit address by virtue of how two's complement numbers work.
307 assert(size % sizeof(int32_t) == 0);
308 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
309 *rel_offset = header_offset - size;
310
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800311 // Prepare entries
312 if (align_up(entries_offset, align) != entries_offset) {
313 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
314 entries_offset, align);
315 return -1;
316 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800317 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800318 // e = min(bootblock, header, rel_offset) where e > entries_offset.
319 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800320 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
321 cbfs_len = bootblock_offset;
322 if (header_offset > entries_offset && header_offset < cbfs_len)
323 cbfs_len = header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700324
325 if (cbfs_image_create(image, cbfs_len - entries_offset))
326 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800327 return 0;
328}
329
Sol Bouchere3260a02015-03-25 13:40:08 -0700330int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
331 uint32_t offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700332{
Sol Bouchere3260a02015-03-25 13:40:08 -0700333 assert(out);
334 assert(in);
335 assert(in->data);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600336
Sol Bouchere3260a02015-03-25 13:40:08 -0700337 buffer_clone(&out->buffer, in);
Sol Boucher67a0a862015-03-18 12:36:27 -0700338 out->has_header = false;
339
Patrick Georgi2f953d32015-09-11 18:34:39 +0200340 if (cbfs_is_valid_cbfs(out)) {
341 return 0;
342 }
343
Sol Bouchere3260a02015-03-25 13:40:08 -0700344 void *header_loc = cbfs_find_header(in->data, in->size, offset);
345 if (header_loc) {
346 cbfs_get_header(&out->header, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700347 out->has_header = true;
Sol Bouchere3260a02015-03-25 13:40:08 -0700348 cbfs_fix_legacy_size(out, header_loc);
Patrick Georgi2f953d32015-09-11 18:34:39 +0200349 return 0;
Sol Boucher67a0a862015-03-18 12:36:27 -0700350 } else if (offset != ~0u) {
351 ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n");
352 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800353 }
Patrick Georgi2f953d32015-09-11 18:34:39 +0200354 ERROR("Selected image region is not a valid CBFS.\n");
355 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800356}
357
Patrick Georgi214e4af2015-11-20 19:22:50 +0100358int cbfs_copy_instance(struct cbfs_image *image, struct buffer *dst)
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800359{
Sol Boucher67a0a862015-03-18 12:36:27 -0700360 assert(image);
Sol Boucher67a0a862015-03-18 12:36:27 -0700361
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800362 struct cbfs_file *src_entry, *dst_entry;
Patrick Georgibd0bb232015-11-20 21:48:18 +0100363 size_t align;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800364 ssize_t last_entry_size;
365
Patrick Georgi214e4af2015-11-20 19:22:50 +0100366 size_t copy_end = buffer_size(dst);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800367
Julius Wernerd4775652020-03-13 16:43:34 -0700368 align = CBFS_ALIGNMENT;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800369
Patrick Georgibd0bb232015-11-20 21:48:18 +0100370 dst_entry = (struct cbfs_file *)buffer_get(dst);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800371
372 /* Copy non-empty files */
373 for (src_entry = cbfs_find_first_entry(image);
374 src_entry && cbfs_is_valid_entry(image, src_entry);
375 src_entry = cbfs_find_next_entry(image, src_entry)) {
376 size_t entry_size;
377
Julius Wernerd4775652020-03-13 16:43:34 -0700378 if ((src_entry->type == htonl(CBFS_TYPE_NULL)) ||
379 (src_entry->type == htonl(CBFS_TYPE_CBFSHEADER)) ||
380 (src_entry->type == htonl(CBFS_TYPE_DELETED)))
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800381 continue;
382
383 entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
384 memcpy(dst_entry, src_entry, entry_size);
385 dst_entry = (struct cbfs_file *)(
386 (uintptr_t)dst_entry + align_up(entry_size, align));
387
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700388 if ((size_t)((uint8_t *)dst_entry - (uint8_t *)buffer_get(dst))
389 >= copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800390 ERROR("Ran out of room in copy region.\n");
391 return 1;
392 }
393 }
394
Patrick Georgibd0bb232015-11-20 21:48:18 +0100395 /* Last entry size is all the room above it, except for top 4 bytes
396 * which may be used by the master header pointer. This messes with
397 * the ability to stash something "top-aligned" into the region, but
398 * keeps things simpler. */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700399 last_entry_size = copy_end -
400 ((uint8_t *)dst_entry - (uint8_t *)buffer_get(dst)) -
401 cbfs_calculate_file_header_size("") - sizeof(int32_t);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800402
403 if (last_entry_size < 0)
404 WARN("No room to create the last entry!\n")
405 else
Julius Wernerd4775652020-03-13 16:43:34 -0700406 cbfs_create_empty_entry(dst_entry, CBFS_TYPE_NULL,
Patrick Georgiedf25d92015-08-12 09:12:06 +0200407 last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800408
409 return 0;
410}
411
Patrick Georgi5d982d72017-09-19 14:39:58 +0200412int cbfs_expand_to_region(struct buffer *region)
413{
414 if (buffer_get(region) == NULL)
415 return 1;
416
417 struct cbfs_image image;
418 memset(&image, 0, sizeof(image));
419 if (cbfs_image_from_buffer(&image, region, 0)) {
420 ERROR("reading CBFS failed!\n");
421 return 1;
422 }
423
424 uint32_t region_sz = buffer_size(region);
425
426 struct cbfs_file *entry;
427 for (entry = buffer_get(region);
428 cbfs_is_valid_entry(&image, entry);
429 entry = cbfs_find_next_entry(&image, entry)) {
430 /* just iterate through */
431 }
432
433 /* entry now points to the first aligned address after the last valid
434 * file header. That's either outside the image or exactly the place
435 * where we need to create a new file.
436 */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700437 int last_entry_size = region_sz -
438 ((uint8_t *)entry - (uint8_t *)buffer_get(region)) -
439 cbfs_calculate_file_header_size("") - sizeof(int32_t);
Patrick Georgi5d982d72017-09-19 14:39:58 +0200440
441 if (last_entry_size > 0) {
Julius Wernerd4775652020-03-13 16:43:34 -0700442 cbfs_create_empty_entry(entry, CBFS_TYPE_NULL,
Patrick Georgi5d982d72017-09-19 14:39:58 +0200443 last_entry_size, "");
444 /* If the last entry was an empty file, merge them. */
Julius Werner7066a1e2020-04-02 15:49:34 -0700445 cbfs_legacy_walk(&image, cbfs_merge_empty_entry, NULL);
Patrick Georgi5d982d72017-09-19 14:39:58 +0200446 }
447
448 return 0;
449}
450
Patrick Georgi12631a42017-09-20 11:59:18 +0200451int cbfs_truncate_space(struct buffer *region, uint32_t *size)
452{
453 if (buffer_get(region) == NULL)
454 return 1;
455
456 struct cbfs_image image;
457 memset(&image, 0, sizeof(image));
458 if (cbfs_image_from_buffer(&image, region, 0)) {
459 ERROR("reading CBFS failed!\n");
460 return 1;
461 }
462
463 struct cbfs_file *entry, *trailer;
464 for (trailer = entry = buffer_get(region);
465 cbfs_is_valid_entry(&image, entry);
466 trailer = entry,
467 entry = cbfs_find_next_entry(&image, entry)) {
468 /* just iterate through */
469 }
470
471 /* trailer now points to the last valid CBFS entry's header.
472 * If that file is empty, remove it and report its header's offset as
473 * maximum size.
474 */
475 if ((strlen(trailer->filename) != 0) &&
Julius Wernerd4775652020-03-13 16:43:34 -0700476 (trailer->type != htonl(CBFS_TYPE_NULL)) &&
477 (trailer->type != htonl(CBFS_TYPE_DELETED))) {
Patrick Georgi12631a42017-09-20 11:59:18 +0200478 /* nothing to truncate. Return de-facto CBFS size in case it
479 * was already truncated. */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700480 *size = (uint8_t *)entry - (uint8_t *)buffer_get(region);
Patrick Georgi12631a42017-09-20 11:59:18 +0200481 return 0;
482 }
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700483 *size = (uint8_t *)trailer - (uint8_t *)buffer_get(region);
Patrick Georgi12631a42017-09-20 11:59:18 +0200484 memset(trailer, 0xff, buffer_size(region) - *size);
485
486 return 0;
487}
488
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600489static size_t cbfs_file_entry_metadata_size(const struct cbfs_file *f)
490{
491 return ntohl(f->offset);
492}
493
494static size_t cbfs_file_entry_data_size(const struct cbfs_file *f)
495{
496 return ntohl(f->len);
497}
498
499static size_t cbfs_file_entry_size(const struct cbfs_file *f)
500{
501 return cbfs_file_entry_metadata_size(f) + cbfs_file_entry_data_size(f);
502}
503
504int cbfs_compact_instance(struct cbfs_image *image)
505{
506 assert(image);
507
508 struct cbfs_file *prev;
509 struct cbfs_file *cur;
510
511 /* The prev entry will always be an empty entry. */
512 prev = NULL;
513
514 /*
515 * Note: this function does not honor alignment or fixed location files.
516 * It's behavior is akin to cbfs_copy_instance() in that it expects
517 * the caller to understand the ramifications of compacting a
518 * fragmented CBFS image.
519 */
520
521 for (cur = cbfs_find_first_entry(image);
522 cur && cbfs_is_valid_entry(image, cur);
523 cur = cbfs_find_next_entry(image, cur)) {
524 size_t prev_size;
525 size_t cur_size;
526 size_t empty_metadata_size;
527 size_t spill_size;
528 uint32_t type = htonl(cur->type);
529
530 /* Current entry is empty. Kepp track of it. */
Julius Wernerd4775652020-03-13 16:43:34 -0700531 if ((type == htonl(CBFS_TYPE_NULL)) ||
532 (type == htonl(CBFS_TYPE_DELETED))) {
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600533 prev = cur;
534 continue;
535 }
536
537 /* Need to ensure the previous entry is an empty one. */
538 if (prev == NULL)
539 continue;
540
541 /* At this point prev is an empty entry. Put the non-empty
Patrick Georgi01cfecc2020-01-29 13:31:16 +0100542 * file in prev's location. Then add a new empty entry. This
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600543 * essentialy bubbles empty entries towards the end. */
544
545 prev_size = cbfs_file_entry_size(prev);
546 cur_size = cbfs_file_entry_size(cur);
547
548 /*
549 * Adjust the empty file size by the actual space occupied
550 * bewtween the beginning of the empty file and the non-empty
551 * file.
552 */
553 prev_size += (cbfs_get_entry_addr(image, cur) -
554 cbfs_get_entry_addr(image, prev)) - prev_size;
555
556 /* Move the non-empty file over the empty file. */
557 memmove(prev, cur, cur_size);
558
559 /*
560 * Get location of the empty file. Note that since prev was
561 * overwritten with the non-empty file the previously moved
562 * file needs to be used to calculate the empty file's location.
563 */
564 cur = cbfs_find_next_entry(image, prev);
565
566 /*
567 * The total space to work with for swapping the 2 entries
568 * consists of the 2 files' sizes combined. However, the
569 * cbfs_file entries start on CBFS_ALIGNMENT boundaries.
570 * Because of this the empty file size may end up smaller
571 * because of the non-empty file's metadata and data length.
572 *
573 * Calculate the spill size which is the amount of data lost
574 * due to the alignment constraints after moving the non-empty
575 * file.
576 */
577 spill_size = (cbfs_get_entry_addr(image, cur) -
578 cbfs_get_entry_addr(image, prev)) - cur_size;
579
580 empty_metadata_size = cbfs_calculate_file_header_size("");
581
582 /* Check if new empty size can contain the metadata. */
583 if (empty_metadata_size + spill_size > prev_size) {
584 ERROR("Unable to swap '%s' with prev empty entry.\n",
585 prev->filename);
586 return 1;
587 }
588
589 /* Update the empty file's size. */
590 prev_size -= spill_size + empty_metadata_size;
591
592 /* Create new empty file. */
Julius Wernerd4775652020-03-13 16:43:34 -0700593 cbfs_create_empty_entry(cur, CBFS_TYPE_NULL,
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600594 prev_size, "");
595
596 /* Merge any potential empty entries together. */
Julius Werner7066a1e2020-04-02 15:49:34 -0700597 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600598
599 /*
600 * Since current switched to an empty file keep track of it.
601 * Even if any empty files were merged the empty entry still
602 * starts at previously calculated location.
603 */
604 prev = cur;
605 }
606
607 return 0;
608}
609
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700610int cbfs_image_delete(struct cbfs_image *image)
611{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100612 if (image == NULL)
613 return 0;
614
Hung-Te Lineab2c812013-01-29 01:56:17 +0800615 buffer_delete(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800616 return 0;
617}
618
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800619/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
620static int cbfs_add_entry_at(struct cbfs_image *image,
621 struct cbfs_file *entry,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800622 const void *data,
Patrick Georgi7fd14182015-08-11 15:55:16 +0200623 uint32_t content_offset,
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100624 const struct cbfs_file *header,
625 const size_t len_align)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700626{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800627 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
628 uint32_t addr = cbfs_get_entry_addr(image, entry),
629 addr_next = cbfs_get_entry_addr(image, next);
Patrick Georgi7fd14182015-08-11 15:55:16 +0200630 uint32_t min_entry_size = cbfs_calculate_file_header_size("");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200631 uint32_t len, header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700632 uint32_t align = image->has_header ? image->header.align :
Julius Wernerd4775652020-03-13 16:43:34 -0700633 CBFS_ALIGNMENT;
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200634 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800635
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200636 header_offset = content_offset - header_size;
637 if (header_offset % align)
638 header_offset -= header_offset % align;
639 if (header_offset < addr) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800640 ERROR("No space to hold cbfs_file header.");
641 return -1;
642 }
643
644 // Process buffer BEFORE content_offset.
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200645 if (header_offset - addr > min_entry_size) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800646 DEBUG("|min|...|header|content|... <create new entry>\n");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200647 len = header_offset - addr - min_entry_size;
Julius Wernerd4775652020-03-13 16:43:34 -0700648 cbfs_create_empty_entry(entry, CBFS_TYPE_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800649 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
650 entry = cbfs_find_next_entry(image, entry);
651 addr = cbfs_get_entry_addr(image, entry);
652 }
653
Patrick Georgi7a33b532015-08-25 13:00:04 +0200654 len = content_offset - addr - header_size;
Patrick Georgia60e7b62015-08-25 22:26:02 +0200655 memcpy(entry, header, header_size);
Patrick Georgi7a33b532015-08-25 13:00:04 +0200656 if (len != 0) {
Julius Wernerf0cc7ad2020-11-18 18:31:22 -0800657 /*
658 * The header moved backwards a bit to accommodate cbfs_file
Patrick Georgi7a33b532015-08-25 13:00:04 +0200659 * alignment requirements, so patch up ->offset to still point
Julius Wernerf0cc7ad2020-11-18 18:31:22 -0800660 * to file data. Move attributes forward so the end of the
661 * attribute list still matches the end of the metadata.
Patrick Georgi7a33b532015-08-25 13:00:04 +0200662 */
Julius Wernerf0cc7ad2020-11-18 18:31:22 -0800663 uint32_t offset = ntohl(entry->offset);
664 uint32_t attrs = ntohl(entry->attributes_offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800665 DEBUG("|..|header|content|... <use offset to create entry>\n");
Julius Wernerf0cc7ad2020-11-18 18:31:22 -0800666 DEBUG("before: attr_offset=0x%x, offset=0x%x\n", attrs, offset);
667 if (attrs == 0) {
668 memset((uint8_t *)entry + offset, 0, len);
669 } else {
670 uint8_t *p = (uint8_t *)entry + attrs;
671 memmove(p + len, p, offset - attrs);
672 memset(p, 0, len);
673 attrs += len;
674 entry->attributes_offset = htonl(attrs);
675 }
676 offset += len;
677 entry->offset = htonl(offset);
678 DEBUG("after: attr_offset=0x%x, offset=0x%x\n", attrs, offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800679 }
680
681 // Ready to fill data into entry.
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800682 DEBUG("content_offset: 0x%x, entry location: %x\n",
683 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
684 image->buffer.data));
685 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
Patrick Georgicccc9d42015-04-28 13:09:36 +0200686 (ptrdiff_t)content_offset);
Patrick Georgi19c80b22015-08-25 13:16:04 +0200687 memcpy(CBFS_SUBHEADER(entry), data, ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800688 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
689
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100690 // Align the length to a multiple of len_align
691 if (len_align &&
692 ((ntohl(entry->offset) + ntohl(entry->len)) % len_align)) {
693 size_t off = (ntohl(entry->offset) + ntohl(entry->len)) % len_align;
694 entry->len = htonl(ntohl(entry->len) + len_align - off);
695 }
696
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800697 // Process buffer AFTER entry.
698 entry = cbfs_find_next_entry(image, entry);
699 addr = cbfs_get_entry_addr(image, entry);
Sol Boucher05725652015-04-02 20:58:26 -0700700 if (addr == addr_next)
701 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800702
Sol Boucher05725652015-04-02 20:58:26 -0700703 assert(addr < addr_next);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800704 if (addr_next - addr < min_entry_size) {
Sol Boucher636cc852015-04-03 09:13:04 -0700705 DEBUG("No need for new \"empty\" entry\n");
706 /* No need to increase the size of the just
707 * stored file to extend to next file. Alignment
708 * of next file takes care of this.
709 */
710 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800711 }
712
713 len = addr_next - addr - min_entry_size;
Patrick Georgi29a04d92015-11-20 23:23:44 +0100714 /* keep space for master header pointer */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700715 if ((uint8_t *)entry + min_entry_size + len >
716 (uint8_t *)buffer_get(&image->buffer) +
717 buffer_size(&image->buffer) - sizeof(int32_t)) {
Patrick Georgi29a04d92015-11-20 23:23:44 +0100718 len -= sizeof(int32_t);
719 }
Julius Wernerd4775652020-03-13 16:43:34 -0700720 cbfs_create_empty_entry(entry, CBFS_TYPE_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800721 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
722 return 0;
723}
724
725int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Patrick Georgie5903582015-08-25 13:53:42 +0200726 uint32_t content_offset,
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100727 struct cbfs_file *header,
728 const size_t len_align)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700729{
Sol Boucher67d59982015-05-07 02:39:22 -0700730 assert(image);
731 assert(buffer);
732 assert(buffer->data);
Furquan Shaikh19ba95f2020-11-20 22:50:26 -0800733 assert(!IS_HOST_SPACE_ADDRESS(content_offset));
Sol Boucher67d59982015-05-07 02:39:22 -0700734
Patrick Georgia60e7b62015-08-25 22:26:02 +0200735 const char *name = header->filename;
Patrick Georgie5903582015-08-25 13:53:42 +0200736
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800737 uint32_t entry_type;
738 uint32_t addr, addr_next;
739 struct cbfs_file *entry, *next;
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200740 uint32_t need_size;
Patrick Georgif5252f32015-08-25 22:27:57 +0200741 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800742
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800743 need_size = header_size + buffer->size;
744 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
745 name, content_offset, header_size, buffer->size, need_size);
746
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800747 // Merge empty entries.
748 DEBUG("(trying to merge empty entries...)\n");
Julius Werner7066a1e2020-04-02 15:49:34 -0700749 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800750
751 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800752 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800753 entry = cbfs_find_next_entry(image, entry)) {
754
755 entry_type = ntohl(entry->type);
Julius Wernerd4775652020-03-13 16:43:34 -0700756 if (entry_type != CBFS_TYPE_NULL)
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800757 continue;
758
759 addr = cbfs_get_entry_addr(image, entry);
760 next = cbfs_find_next_entry(image, entry);
761 addr_next = cbfs_get_entry_addr(image, next);
762
763 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
764 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600765
766 /* Will the file fit? Don't yet worry if we have space for a new
767 * "empty" entry. We take care of that later.
768 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800769 if (addr + need_size > addr_next)
770 continue;
771
Patrick Georgiaa44dbd2015-08-12 12:05:21 +0200772 // Test for complicated cases
773 if (content_offset > 0) {
774 if (addr_next < content_offset) {
775 DEBUG("Not for specified offset yet");
776 continue;
777 } else if (addr > content_offset) {
778 DEBUG("Exceed specified content_offset.");
779 break;
780 } else if (addr + header_size > content_offset) {
781 ERROR("Not enough space for header.\n");
782 break;
783 } else if (content_offset + buffer->size > addr_next) {
784 ERROR("Not enough space for content.\n");
785 break;
786 }
787 }
788
789 // TODO there are more few tricky cases that we may
790 // want to fit by altering offset.
791
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200792 if (content_offset == 0) {
793 // we tested every condition earlier under which
794 // placing the file there might fail
795 content_offset = addr + header_size;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800796 }
797
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800798 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
799 addr, addr_next - addr, content_offset);
800
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200801 if (cbfs_add_entry_at(image, entry, buffer->data,
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100802 content_offset, header, len_align) == 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800803 return 0;
804 }
805 break;
806 }
807
808 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
809 buffer->name, buffer->size, buffer->size / 1024, content_offset);
810 return -1;
811}
812
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700813struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
814{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800815 struct cbfs_file *entry;
816 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800817 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800818 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgic569b8b2015-07-15 16:42:38 +0200819 if (strcasecmp(entry->filename, name) == 0) {
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800820 DEBUG("cbfs_get_entry: found %s\n", name);
821 return entry;
822 }
823 }
824 return NULL;
825}
826
Antonello Dettorifda691e2016-06-09 12:35:36 +0200827static int cbfs_payload_decompress(struct cbfs_payload_segment *segments,
828 struct buffer *buff, int num_seg)
829{
830 struct buffer new_buffer;
831 struct buffer seg_buffer;
832 size_t new_buff_sz;
833 char *in_ptr;
834 char *out_ptr;
835 size_t new_offset;
836 decomp_func_ptr decompress;
837
838 new_offset = num_seg * sizeof(*segments);
839 new_buff_sz = num_seg * sizeof(*segments);
840
841 /* Find out and allocate the amount of memory occupied
842 * by the binary data */
843 for (int i = 0; i < num_seg; i++)
844 new_buff_sz += segments[i].mem_len;
845
Furquan Shaikh58644a02016-08-05 08:27:18 -0700846 if (buffer_create(&new_buffer, new_buff_sz, "decompressed_buff"))
847 return -1;
Antonello Dettorifda691e2016-06-09 12:35:36 +0200848
849 in_ptr = buffer_get(buff) + new_offset;
850 out_ptr = buffer_get(&new_buffer) + new_offset;
851
852 for (int i = 0; i < num_seg; i++) {
853 struct buffer tbuff;
854 size_t decomp_size;
855
Antonello Dettorifda691e2016-06-09 12:35:36 +0200856 /* Segments BSS and ENTRY do not have binary data. */
857 if (segments[i].type == PAYLOAD_SEGMENT_BSS ||
858 segments[i].type == PAYLOAD_SEGMENT_ENTRY) {
859 continue;
860 } else if (segments[i].type == PAYLOAD_SEGMENT_PARAMS) {
861 memcpy(out_ptr, in_ptr, segments[i].len);
862 segments[i].offset = new_offset;
863 new_offset += segments[i].len;
864 in_ptr += segments[i].len;
865 out_ptr += segments[i].len;
866 segments[i].compression = CBFS_COMPRESS_NONE;
867 continue;
868 }
869
Joel Kitching72d77a92018-07-18 13:23:52 +0800870 /* The payload uses an unknown compression algorithm. */
871 decompress = decompression_function(segments[i].compression);
872 if (decompress == NULL) {
873 ERROR("Unknown decompression algorithm: %u\n",
874 segments[i].compression);
875 return -1;
876 }
877
Furquan Shaikh58644a02016-08-05 08:27:18 -0700878 if (buffer_create(&tbuff, segments[i].mem_len, "segment")) {
879 buffer_delete(&new_buffer);
880 return -1;
881 }
Antonello Dettorifda691e2016-06-09 12:35:36 +0200882
883 if (decompress(in_ptr, segments[i].len, buffer_get(&tbuff),
884 (int) buffer_size(&tbuff),
885 &decomp_size)) {
886 ERROR("Couldn't decompress payload segment %u\n", i);
887 buffer_delete(&new_buffer);
Furquan Shaikh9844d562016-08-05 08:32:23 -0700888 buffer_delete(&tbuff);
Antonello Dettorifda691e2016-06-09 12:35:36 +0200889 return -1;
890 }
891
892 memcpy(out_ptr, buffer_get(&tbuff), decomp_size);
893
894 in_ptr += segments[i].len;
895
896 /* Update the offset of the segment. */
897 segments[i].offset = new_offset;
898 /* True decompressed size is just the data size. No metadata */
899 segments[i].len = decomp_size;
900 /* Segment is not compressed. */
901 segments[i].compression = CBFS_COMPRESS_NONE;
902
903 /* Update the offset and output buffer pointer. */
904 new_offset += decomp_size;
905 out_ptr += decomp_size;
906
907 buffer_delete(&tbuff);
908 }
909
910 buffer_splice(&seg_buffer, &new_buffer, 0, 0);
911 xdr_segs(&seg_buffer, segments, num_seg);
912
913 buffer_delete(buff);
914 *buff = new_buffer;
915
916 return 0;
917}
918
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500919static int init_elf_from_arch(Elf64_Ehdr *ehdr, uint32_t cbfs_arch)
920{
921 int endian;
922 int nbits;
923 int machine;
924
925 switch (cbfs_arch) {
926 case CBFS_ARCHITECTURE_X86:
927 endian = ELFDATA2LSB;
928 nbits = ELFCLASS32;
929 machine = EM_386;
930 break;
931 case CBFS_ARCHITECTURE_ARM:
932 endian = ELFDATA2LSB;
933 nbits = ELFCLASS32;
934 machine = EM_ARM;
935 break;
936 case CBFS_ARCHITECTURE_AARCH64:
937 endian = ELFDATA2LSB;
938 nbits = ELFCLASS64;
939 machine = EM_AARCH64;
940 break;
941 case CBFS_ARCHITECTURE_MIPS:
942 endian = ELFDATA2LSB;
943 nbits = ELFCLASS32;
944 machine = EM_MIPS;
945 break;
946 case CBFS_ARCHITECTURE_RISCV:
947 endian = ELFDATA2LSB;
948 nbits = ELFCLASS32;
949 machine = EM_RISCV;
950 break;
951 default:
952 ERROR("Unsupported arch: %x\n", cbfs_arch);
953 return -1;
954 }
955
956 elf_init_eheader(ehdr, machine, nbits, endian);
957 return 0;
958}
959
Julius Werner81dc20e2020-10-15 17:37:57 -0700960static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch,
961 struct cbfs_file *entry)
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500962{
963 Elf64_Ehdr ehdr;
964 Elf64_Shdr shdr;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500965 struct elf_writer *ew;
966 struct buffer elf_out;
967 size_t empty_sz;
Aaron Durbin694fd132015-10-28 11:39:34 -0500968 int rmod_ret;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500969
Antonello Dettori0b806282016-06-26 00:24:25 +0200970 if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
971 ERROR("You need to specify -m ARCH.\n");
972 return -1;
973 }
974
Julius Werner81dc20e2020-10-15 17:37:57 -0700975 struct cbfs_file_attr_stageheader *stage = NULL;
976 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
977 attr != NULL; attr = cbfs_file_next_attr(entry, attr)) {
978 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_STAGEHEADER) {
979 stage = (struct cbfs_file_attr_stageheader *)attr;
980 break;
981 }
982 }
983
984 if (stage == NULL) {
985 ERROR("Stage header not found for %s\n", entry->filename);
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500986 return -1;
987 }
988
989 if (init_elf_from_arch(&ehdr, arch))
990 return -1;
991
Aaron Durbin694fd132015-10-28 11:39:34 -0500992 /* Attempt rmodule translation first. */
993 rmod_ret = rmodule_stage_to_elf(&ehdr, buff);
994
995 if (rmod_ret < 0) {
996 ERROR("rmodule parsing failed\n");
997 return -1;
998 } else if (rmod_ret == 0)
999 return 0;
1000
1001 /* Rmodule couldn't do anything with the data. Continue on with SELF. */
1002
Julius Werner81dc20e2020-10-15 17:37:57 -07001003 ehdr.e_entry = ntohll(stage->loadaddr) + ntohl(stage->entry_offset);
1004
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001005 ew = elf_writer_init(&ehdr);
1006 if (ew == NULL) {
1007 ERROR("Unable to init ELF writer.\n");
1008 return -1;
1009 }
1010
1011 memset(&shdr, 0, sizeof(shdr));
1012 shdr.sh_type = SHT_PROGBITS;
1013 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
Julius Werner81dc20e2020-10-15 17:37:57 -07001014 shdr.sh_addr = ntohll(stage->loadaddr);
1015 shdr.sh_size = buffer_size(buff);
1016 empty_sz = ntohl(stage->memlen) - buffer_size(buff);
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001017
1018 if (elf_writer_add_section(ew, &shdr, buff, ".program")) {
1019 ERROR("Unable to add ELF section: .program\n");
1020 elf_writer_destroy(ew);
1021 return -1;
1022 }
1023
1024 if (empty_sz != 0) {
1025 struct buffer b;
1026
1027 buffer_init(&b, NULL, NULL, 0);
1028 memset(&shdr, 0, sizeof(shdr));
1029 shdr.sh_type = SHT_NOBITS;
1030 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
Julius Werner81dc20e2020-10-15 17:37:57 -07001031 shdr.sh_addr = ntohl(stage->loadaddr) + buffer_size(buff);
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001032 shdr.sh_size = empty_sz;
1033 if (elf_writer_add_section(ew, &shdr, &b, ".empty")) {
1034 ERROR("Unable to add ELF section: .empty\n");
1035 elf_writer_destroy(ew);
1036 return -1;
1037 }
1038 }
1039
1040 if (elf_writer_serialize(ew, &elf_out)) {
1041 ERROR("Unable to create ELF file from stage.\n");
1042 elf_writer_destroy(ew);
1043 return -1;
1044 }
1045
1046 /* Flip buffer with the created ELF one. */
1047 buffer_delete(buff);
1048 *buff = elf_out;
1049
1050 elf_writer_destroy(ew);
1051
1052 return 0;
1053}
1054
Julius Werner81dc20e2020-10-15 17:37:57 -07001055static int cbfs_payload_make_elf(struct buffer *buff, uint32_t arch,
1056 unused struct cbfs_file *entry)
Antonello Dettorifda691e2016-06-09 12:35:36 +02001057{
1058 Elf64_Ehdr ehdr;
1059 Elf64_Shdr shdr;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001060 struct cbfs_payload_segment *segs = NULL;
Jonathan Neuschäfer5de54582016-08-09 15:01:58 +02001061 struct elf_writer *ew = NULL;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001062 struct buffer elf_out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001063 int segments = 0;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001064 int retval = -1;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001065
Antonello Dettori0b806282016-06-26 00:24:25 +02001066 if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
1067 ERROR("You need to specify -m ARCH.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001068 goto out;
Antonello Dettori0b806282016-06-26 00:24:25 +02001069 }
1070
Antonello Dettorifda691e2016-06-09 12:35:36 +02001071 /* Count the number of segments inside buffer */
1072 while (true) {
1073 uint32_t payload_type = 0;
1074
1075 struct cbfs_payload_segment *seg;
1076
1077 seg = buffer_get(buff);
1078 payload_type = read_be32(&seg[segments].type);
1079
1080 if (payload_type == PAYLOAD_SEGMENT_CODE) {
1081 segments++;
1082 } else if (payload_type == PAYLOAD_SEGMENT_DATA) {
1083 segments++;
1084 } else if (payload_type == PAYLOAD_SEGMENT_BSS) {
1085 segments++;
1086 } else if (payload_type == PAYLOAD_SEGMENT_PARAMS) {
1087 segments++;
1088 } else if (payload_type == PAYLOAD_SEGMENT_ENTRY) {
1089 /* The last segment in a payload is always ENTRY as
1090 * specified by the parse_elf_to_payload() function.
1091 * Therefore there is no need to continue looking for
1092 * segments.*/
1093 segments++;
1094 break;
1095 } else {
1096 ERROR("Unknown payload segment type: %x\n",
1097 payload_type);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001098 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001099 }
1100 }
1101
1102 segs = malloc(segments * sizeof(*segs));
1103
1104 /* Decode xdr segments */
1105 for (int i = 0; i < segments; i++) {
1106 struct cbfs_payload_segment *serialized_seg = buffer_get(buff);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001107 xdr_get_seg(&segs[i], &serialized_seg[i]);
1108 }
1109
1110 if (cbfs_payload_decompress(segs, buff, segments)) {
1111 ERROR("Failed to decompress payload.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001112 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001113 }
1114
1115 if (init_elf_from_arch(&ehdr, arch))
Furquan Shaikh7b405172016-08-05 08:20:37 -07001116 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001117
1118 ehdr.e_entry = segs[segments-1].load_addr;
1119
1120 ew = elf_writer_init(&ehdr);
1121 if (ew == NULL) {
1122 ERROR("Unable to init ELF writer.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001123 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001124 }
1125
1126 for (int i = 0; i < segments; i++) {
1127 struct buffer tbuff;
Furquan Shaikhf3bba442016-08-05 08:12:31 -07001128 size_t empty_sz = 0;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001129
1130 memset(&shdr, 0, sizeof(shdr));
1131 char *name = NULL;
1132
1133 if (segs[i].type == PAYLOAD_SEGMENT_CODE) {
1134 shdr.sh_type = SHT_PROGBITS;
1135 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
1136 shdr.sh_addr = segs[i].load_addr;
1137 shdr.sh_size = segs[i].len;
1138 empty_sz = segs[i].mem_len - segs[i].len;
1139 name = strdup(".text");
1140 buffer_splice(&tbuff, buff, segs[i].offset,
1141 segs[i].len);
1142 } else if (segs[i].type == PAYLOAD_SEGMENT_DATA) {
1143 shdr.sh_type = SHT_PROGBITS;
1144 shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
1145 shdr.sh_addr = segs[i].load_addr;
1146 shdr.sh_size = segs[i].len;
1147 empty_sz = segs[i].mem_len - segs[i].len;
1148 name = strdup(".data");
1149 buffer_splice(&tbuff, buff, segs[i].offset,
1150 segs[i].len);
1151 } else if (segs[i].type == PAYLOAD_SEGMENT_BSS) {
1152 shdr.sh_type = SHT_NOBITS;
1153 shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
1154 shdr.sh_addr = segs[i].load_addr;
1155 shdr.sh_size = segs[i].len;
1156 name = strdup(".bss");
1157 buffer_splice(&tbuff, buff, 0, 0);
1158 } else if (segs[i].type == PAYLOAD_SEGMENT_PARAMS) {
1159 shdr.sh_type = SHT_NOTE;
1160 shdr.sh_flags = 0;
1161 shdr.sh_size = segs[i].len;
1162 name = strdup(".note.pinfo");
1163 buffer_splice(&tbuff, buff, segs[i].offset,
1164 segs[i].len);
1165 } else if (segs[i].type == PAYLOAD_SEGMENT_ENTRY) {
1166 break;
Patrick Georgidce629b2017-01-13 13:30:54 +01001167 } else {
1168 ERROR("unknown ELF segment type\n");
1169 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001170 }
1171
Patrick Georgidce629b2017-01-13 13:30:54 +01001172 if (!name) {
1173 ERROR("out of memory\n");
1174 goto out;
1175 }
Antonello Dettorifda691e2016-06-09 12:35:36 +02001176
1177 if (elf_writer_add_section(ew, &shdr, &tbuff, name)) {
1178 ERROR("Unable to add ELF section: %s\n", name);
Patrick Georgidce629b2017-01-13 13:30:54 +01001179 free(name);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001180 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001181 }
Patrick Georgidce629b2017-01-13 13:30:54 +01001182 free(name);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001183
1184 if (empty_sz != 0) {
1185 struct buffer b;
1186
1187 buffer_init(&b, NULL, NULL, 0);
1188 memset(&shdr, 0, sizeof(shdr));
1189 shdr.sh_type = SHT_NOBITS;
1190 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
1191 shdr.sh_addr = segs[i].load_addr + segs[i].len;
1192 shdr.sh_size = empty_sz;
1193 name = strdup(".empty");
Patrick Georgidce629b2017-01-13 13:30:54 +01001194 if (!name) {
1195 ERROR("out of memory\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001196 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001197 }
Patrick Georgidce629b2017-01-13 13:30:54 +01001198 if (elf_writer_add_section(ew, &shdr, &b, name)) {
1199 ERROR("Unable to add ELF section: %s\n", name);
1200 free(name);
1201 goto out;
1202 }
1203 free(name);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001204 }
Antonello Dettorifda691e2016-06-09 12:35:36 +02001205 }
1206
1207 if (elf_writer_serialize(ew, &elf_out)) {
Julius Werner81dc20e2020-10-15 17:37:57 -07001208 ERROR("Unable to create ELF file from payload.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001209 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001210 }
1211
1212 /* Flip buffer with the created ELF one. */
1213 buffer_delete(buff);
1214 *buff = elf_out;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001215 retval = 0;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001216
Furquan Shaikh7b405172016-08-05 08:20:37 -07001217out:
1218 free(segs);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001219 elf_writer_destroy(ew);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001220 return retval;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001221}
1222
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001223int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Joel Kitching21fdd892018-08-09 17:49:52 +08001224 const char *filename, uint32_t arch, bool do_processing)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001225{
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001226 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
1227 struct buffer buffer;
1228 if (!entry) {
1229 ERROR("File not found: %s\n", entry_name);
1230 return -1;
1231 }
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001232
Joel Kitching21fdd892018-08-09 17:49:52 +08001233 unsigned int compressed_size = ntohl(entry->len);
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001234 unsigned int decompressed_size = 0;
1235 unsigned int compression = cbfs_file_get_compression_info(entry,
1236 &decompressed_size);
Werner Zehbbf1df72018-11-21 13:07:50 +01001237 unsigned int buffer_len;
Joel Kitching21fdd892018-08-09 17:49:52 +08001238 decomp_func_ptr decompress;
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001239
Joel Kitching21fdd892018-08-09 17:49:52 +08001240 if (do_processing) {
1241 decompress = decompression_function(compression);
1242 if (!decompress) {
1243 ERROR("looking up decompression routine failed\n");
1244 return -1;
1245 }
Werner Zehbbf1df72018-11-21 13:07:50 +01001246 buffer_len = decompressed_size;
Joel Kitching21fdd892018-08-09 17:49:52 +08001247 } else {
1248 /* Force nop decompression */
1249 decompress = decompression_function(CBFS_COMPRESS_NONE);
Werner Zehbbf1df72018-11-21 13:07:50 +01001250 buffer_len = compressed_size;
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001251 }
1252
Joel Kitching21fdd892018-08-09 17:49:52 +08001253 LOG("Found file %.30s at 0x%x, type %.12s, compressed %d, size %d\n",
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001254 entry_name, cbfs_get_entry_addr(image, entry),
Joel Kitching21fdd892018-08-09 17:49:52 +08001255 get_cbfs_entry_type_name(ntohl(entry->type)), compressed_size,
1256 decompressed_size);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001257
Aaron Durbin539aed02015-10-23 17:42:32 -05001258 buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0);
Werner Zehbbf1df72018-11-21 13:07:50 +01001259 buffer.data = malloc(buffer_len);
1260 buffer.size = buffer_len;
Aaron Durbin539aed02015-10-23 17:42:32 -05001261
Joel Kitching21fdd892018-08-09 17:49:52 +08001262 if (decompress(CBFS_SUBHEADER(entry), compressed_size,
1263 buffer.data, buffer.size, NULL)) {
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001264 ERROR("decompression failed for %s\n", entry_name);
Aaron Durbin539aed02015-10-23 17:42:32 -05001265 buffer_delete(&buffer);
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001266 return -1;
1267 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001268
1269 /*
Julius Werner81dc20e2020-10-15 17:37:57 -07001270 * We want to export stages and payloads as ELFs, not with coreboot's
1271 * custom stage/SELF binary formats, so we need to do extra processing
1272 * to turn them back into an ELF.
Aaron Durbin539aed02015-10-23 17:42:32 -05001273 */
Joel Kitching21fdd892018-08-09 17:49:52 +08001274 if (do_processing) {
Julius Werner81dc20e2020-10-15 17:37:57 -07001275 int (*make_elf)(struct buffer *, uint32_t,
1276 struct cbfs_file *) = NULL;
Joel Kitching21fdd892018-08-09 17:49:52 +08001277 switch (ntohl(entry->type)) {
Julius Wernerd4775652020-03-13 16:43:34 -07001278 case CBFS_TYPE_STAGE:
Joel Kitching21fdd892018-08-09 17:49:52 +08001279 make_elf = cbfs_stage_make_elf;
1280 break;
Julius Wernerd4775652020-03-13 16:43:34 -07001281 case CBFS_TYPE_SELF:
Joel Kitching21fdd892018-08-09 17:49:52 +08001282 make_elf = cbfs_payload_make_elf;
1283 break;
Aaron Durbin539aed02015-10-23 17:42:32 -05001284 }
Julius Werner81dc20e2020-10-15 17:37:57 -07001285 if (make_elf && make_elf(&buffer, arch, entry)) {
Joel Kitching21fdd892018-08-09 17:49:52 +08001286 ERROR("Failed to write %s into %s.\n",
1287 entry_name, filename);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001288 buffer_delete(&buffer);
1289 return -1;
1290 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001291 }
1292
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001293 if (buffer_write_file(&buffer, filename) != 0) {
1294 ERROR("Failed to write %s into %s.\n",
1295 entry_name, filename);
Aaron Durbin539aed02015-10-23 17:42:32 -05001296 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001297 return -1;
1298 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001299
1300 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001301 INFO("Successfully dumped the file to: %s\n", filename);
1302 return 0;
1303}
1304
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001305int cbfs_remove_entry(struct cbfs_image *image, const char *name)
1306{
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +02001307 struct cbfs_file *entry;
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001308 entry = cbfs_get_entry(image, name);
1309 if (!entry) {
1310 ERROR("CBFS file %s not found.\n", name);
1311 return -1;
1312 }
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001313 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
Patrick Georgic569b8b2015-07-15 16:42:38 +02001314 entry->filename, cbfs_get_entry_addr(image, entry));
Julius Wernerd4775652020-03-13 16:43:34 -07001315 entry->type = htonl(CBFS_TYPE_DELETED);
Julius Werner7066a1e2020-04-02 15:49:34 -07001316 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001317 return 0;
1318}
1319
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001320int cbfs_print_header_info(struct cbfs_image *image)
1321{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001322 char *name = strdup(image->buffer.name);
Sol Boucher3e060ed2015-05-05 15:40:15 -07001323 assert(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001324 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -08001325 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001326 basename(name),
1327 image->buffer.size / 1024,
Sol Boucher3e060ed2015-05-05 15:40:15 -07001328 image->header.bootblocksize,
1329 image->header.romsize,
1330 image->header.offset,
1331 image->header.align,
1332 arch_to_string(image->header.architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001333 free(name);
1334 return 0;
1335}
1336
Julius Werner81dc20e2020-10-15 17:37:57 -07001337static int cbfs_print_stage_info(struct cbfs_file *entry, FILE* fp)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001338{
Julius Werner81dc20e2020-10-15 17:37:57 -07001339
1340 struct cbfs_file_attr_stageheader *stage = NULL;
1341 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
1342 attr != NULL; attr = cbfs_file_next_attr(entry, attr)) {
1343 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_STAGEHEADER) {
1344 stage = (struct cbfs_file_attr_stageheader *)attr;
1345 break;
1346 }
1347 }
1348
1349 if (stage == NULL) {
1350 fprintf(fp, " ERROR: stage header not found!\n");
1351 return -1;
1352 }
1353
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001354 fprintf(fp,
Julius Werner81dc20e2020-10-15 17:37:57 -07001355 " entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
1356 "memlen: %d\n",
1357 ntohll(stage->loadaddr) + ntohl(stage->entry_offset),
1358 ntohll(stage->loadaddr),
1359 ntohl(stage->memlen));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001360 return 0;
1361}
1362
Hung-Te Lin0780d672014-05-16 10:14:05 +08001363static int cbfs_print_decoded_payload_segment_info(
1364 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001365{
Hung-Te Lin0780d672014-05-16 10:14:05 +08001366 /* The input (seg) must be already decoded by
1367 * cbfs_decode_payload_segment.
1368 */
1369 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001370 case PAYLOAD_SEGMENT_CODE:
1371 case PAYLOAD_SEGMENT_DATA:
1372 fprintf(fp, " %s (%s compression, offset: 0x%x, "
1373 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001374 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001375 "code " : "data"),
1376 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +08001377 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001378 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +08001379 seg->offset, seg->load_addr, seg->len,
1380 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001381 break;
1382
1383 case PAYLOAD_SEGMENT_ENTRY:
1384 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001385 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001386 break;
1387
1388 case PAYLOAD_SEGMENT_BSS:
1389 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
1390 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001391 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001392 break;
1393
1394 case PAYLOAD_SEGMENT_PARAMS:
1395 fprintf(fp, " parameters\n");
1396 break;
1397
1398 default:
1399 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
1400 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001401 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001402 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +08001403 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001404 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +08001405 seg->offset, seg->load_addr, seg->len,
1406 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001407 break;
1408 }
1409 return 0;
1410}
1411
1412int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001413 void *arg)
1414{
Patrick Georgic569b8b2015-07-15 16:42:38 +02001415 const char *name = entry->filename;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001416 struct cbfs_payload_segment *payload;
1417 FILE *fp = (FILE *)arg;
1418
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001419 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001420 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1421 cbfs_get_entry_addr(image, entry));
1422 return -1;
1423 }
1424 if (!fp)
1425 fp = stdout;
1426
Patrick Georgic82725c2015-08-26 12:13:03 +02001427 unsigned int decompressed_size = 0;
Patrick Georgia71c83f2015-08-26 12:23:26 +02001428 unsigned int compression = cbfs_file_get_compression_info(entry,
1429 &decompressed_size);
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001430 const char *compression_name = lookup_name_by_type(
1431 types_cbfs_compression, compression, "????");
Patrick Georgic82725c2015-08-26 12:13:03 +02001432
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001433 if (compression == CBFS_COMPRESS_NONE)
1434 fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s\n",
Patrick Georgic82725c2015-08-26 12:13:03 +02001435 *name ? name : "(empty)",
1436 cbfs_get_entry_addr(image, entry),
1437 get_cbfs_entry_type_name(ntohl(entry->type)),
1438 ntohl(entry->len),
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001439 compression_name
Patrick Georgic82725c2015-08-26 12:13:03 +02001440 );
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001441 else
1442 fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s (%d decompressed)\n",
1443 *name ? name : "(empty)",
1444 cbfs_get_entry_addr(image, entry),
1445 get_cbfs_entry_type_name(ntohl(entry->type)),
1446 ntohl(entry->len),
1447 compression_name,
1448 decompressed_size
1449 );
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001450
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001451 if (!verbose)
1452 return 0;
1453
Julius Wernerd4775652020-03-13 16:43:34 -07001454 struct cbfs_file_attr_hash *attr = NULL;
1455 while ((attr = cbfs_file_get_next_hash(entry, attr)) != NULL) {
1456 size_t hash_len = vb2_digest_size(attr->hash.algo);
1457 if (!hash_len) {
1458 fprintf(fp, "invalid/unsupported hash algorithm: %d\n",
1459 attr->hash.algo);
Patrick Georgi89f20342015-10-01 15:54:04 +02001460 break;
1461 }
Julius Wernerd4775652020-03-13 16:43:34 -07001462 char *hash_str = bintohex(attr->hash.raw, hash_len);
1463 int valid = vb2_hash_verify(CBFS_SUBHEADER(entry),
1464 ntohl(entry->len), &attr->hash) == VB2_SUCCESS;
Patrick Georgi89f20342015-10-01 15:54:04 +02001465 const char *valid_str = valid ? "valid" : "invalid";
1466
1467 fprintf(fp, " hash %s:%s %s\n",
Julius Wernerd4775652020-03-13 16:43:34 -07001468 vb2_get_hash_algorithm_name(attr->hash.algo),
Patrick Georgi89f20342015-10-01 15:54:04 +02001469 hash_str, valid_str);
1470 free(hash_str);
1471 }
1472
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001473 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
1474 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
1475 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
1476 ntohl(entry->len));
1477
1478 /* note the components of the subheader may be in host order ... */
1479 switch (ntohl(entry->type)) {
Julius Wernerd4775652020-03-13 16:43:34 -07001480 case CBFS_TYPE_STAGE:
Julius Werner81dc20e2020-10-15 17:37:57 -07001481 cbfs_print_stage_info(entry, fp);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001482 break;
1483
Julius Wernerd4775652020-03-13 16:43:34 -07001484 case CBFS_TYPE_SELF:
Paul Menzel831bbe82015-08-08 20:20:57 +02001485 payload = (struct cbfs_payload_segment *)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001486 CBFS_SUBHEADER(entry);
1487 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +08001488 struct cbfs_payload_segment seg;
1489 cbfs_decode_payload_segment(&seg, payload);
1490 cbfs_print_decoded_payload_segment_info(
1491 &seg, fp);
1492 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001493 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +08001494 else
Aaron Durbinca630272014-08-05 10:48:20 -05001495 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001496 }
1497 break;
1498 default:
1499 break;
1500 }
1501 return 0;
1502}
1503
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001504static int cbfs_print_parseable_entry_info(struct cbfs_image *image,
1505 struct cbfs_file *entry, void *arg)
1506{
1507 FILE *fp = (FILE *)arg;
1508 const char *name;
1509 const char *type;
1510 size_t offset;
1511 size_t metadata_size;
1512 size_t data_size;
1513 const char *sep = "\t";
1514
1515 if (!cbfs_is_valid_entry(image, entry)) {
1516 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1517 cbfs_get_entry_addr(image, entry));
1518 return -1;
1519 }
1520
1521 name = entry->filename;
1522 if (*name == '\0')
1523 name = "(empty)";
1524 type = get_cbfs_entry_type_name(ntohl(entry->type)),
1525 metadata_size = ntohl(entry->offset);
1526 data_size = ntohl(entry->len);
1527 offset = cbfs_get_entry_addr(image, entry);
1528
1529 fprintf(fp, "%s%s", name, sep);
1530 fprintf(fp, "0x%zx%s", offset, sep);
1531 fprintf(fp, "%s%s", type, sep);
1532 fprintf(fp, "0x%zx%s", metadata_size, sep);
1533 fprintf(fp, "0x%zx%s", data_size, sep);
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001534 fprintf(fp, "0x%zx", metadata_size + data_size);
1535
1536 if (verbose) {
1537 unsigned int decompressed_size = 0;
1538 unsigned int compression = cbfs_file_get_compression_info(entry,
1539 &decompressed_size);
1540 if (compression != CBFS_COMPRESS_NONE)
1541 fprintf(fp, "%scomp:%s:0x%x", sep, lookup_name_by_type(
1542 types_cbfs_compression, compression, "????"),
1543 decompressed_size);
1544
1545 struct cbfs_file_attr_hash *attr = NULL;
1546 while ((attr = cbfs_file_get_next_hash(entry, attr)) != NULL) {
1547 size_t hash_len = vb2_digest_size(attr->hash.algo);
1548 if (!hash_len)
1549 continue;
1550 char *hash_str = bintohex(attr->hash.raw, hash_len);
1551 int valid = vb2_hash_verify(CBFS_SUBHEADER(entry),
1552 ntohl(entry->len), &attr->hash) == VB2_SUCCESS;
1553 fprintf(fp, "%shash:%s:%s:%s", sep,
1554 vb2_get_hash_algorithm_name(attr->hash.algo),
1555 hash_str, valid ? "valid" : "invalid");
1556 free(hash_str);
1557 }
1558 }
1559 fprintf(fp, "\n");
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001560
1561 return 0;
1562}
1563
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001564void cbfs_print_directory(struct cbfs_image *image)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001565{
Sol Boucher67a0a862015-03-18 12:36:27 -07001566 if (cbfs_is_legacy_cbfs(image))
1567 cbfs_print_header_info(image);
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001568 printf("%-30s %-10s %-12s Size Comp\n", "Name", "Offset", "Type");
Julius Werner7066a1e2020-04-02 15:49:34 -07001569 cbfs_legacy_walk(image, cbfs_print_entry_info, NULL);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001570}
1571
Julius Wernerc4ee28c2020-04-27 19:31:03 -07001572void cbfs_print_parseable_directory(struct cbfs_image *image)
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001573{
Furquan Shaikh161d2332016-05-26 14:41:02 -07001574 size_t i;
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001575 const char *header[] = {
1576 "Name",
1577 "Offset",
1578 "Type",
1579 "Metadata Size",
1580 "Data Size",
1581 "Total Size",
1582 };
1583 const char *sep = "\t";
1584
1585 for (i = 0; i < ARRAY_SIZE(header) - 1; i++)
1586 fprintf(stdout, "%s%s", header[i], sep);
1587 fprintf(stdout, "%s\n", header[i]);
Julius Werner7066a1e2020-04-02 15:49:34 -07001588 cbfs_legacy_walk(image, cbfs_print_parseable_entry_info, stdout);
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001589}
1590
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001591int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -08001592 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001593{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001594 struct cbfs_file *next;
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001595 uint32_t next_addr = 0;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001596
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001597 /* We don't return here even if this entry is already empty because we
1598 want to merge the empty entries following after it. */
1599
1600 /* Loop until non-empty entry is found, starting from the current entry.
1601 After the loop, next_addr points to the next non-empty entry. */
1602 next = entry;
Julius Wernerd4775652020-03-13 16:43:34 -07001603 while (ntohl(next->type) == CBFS_TYPE_DELETED ||
1604 ntohl(next->type) == CBFS_TYPE_NULL) {
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001605 next = cbfs_find_next_entry(image, next);
1606 if (!next)
1607 break;
1608 next_addr = cbfs_get_entry_addr(image, next);
1609 if (!cbfs_is_valid_entry(image, next))
1610 /* 'next' could be the end of cbfs */
1611 break;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001612 }
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001613
1614 if (!next_addr)
1615 /* Nothing to empty */
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001616 return 0;
1617
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001618 /* We can return here if we find only a single empty entry.
1619 For simplicity, we just proceed (and make it empty again). */
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001620
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001621 /* We're creating one empty entry for combined empty spaces */
1622 uint32_t addr = cbfs_get_entry_addr(image, entry);
1623 size_t len = next_addr - addr - cbfs_calculate_file_header_size("");
1624 DEBUG("join_empty_entry: [0x%x, 0x%x) len=%zu\n", addr, next_addr, len);
Julius Wernerd4775652020-03-13 16:43:34 -07001625 cbfs_create_empty_entry(entry, CBFS_TYPE_NULL, len, "");
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001626
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001627 return 0;
1628}
1629
Julius Werner7066a1e2020-04-02 15:49:34 -07001630int cbfs_legacy_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001631 void *arg)
1632{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001633 int count = 0;
1634 struct cbfs_file *entry;
1635 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001636 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001637 entry = cbfs_find_next_entry(image, entry)) {
1638 count ++;
1639 if (callback(image, entry, arg) != 0)
1640 break;
1641 }
1642 return count;
1643}
1644
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001645static int cbfs_header_valid(struct cbfs_header *header)
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001646{
1647 if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
1648 ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
1649 (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001650 (ntohl(header->offset) < ntohl(header->romsize)))
1651 return 1;
1652 return 0;
1653}
1654
1655struct cbfs_header *cbfs_find_header(char *data, size_t size,
1656 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001657{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001658 size_t offset;
1659 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001660 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001661 struct cbfs_header *header, *result = NULL;
1662
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001663 if (forced_offset < (size - sizeof(struct cbfs_header))) {
1664 /* Check if the forced header is valid. */
1665 header = (struct cbfs_header *)(data + forced_offset);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001666 if (cbfs_header_valid(header))
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001667 return header;
1668 return NULL;
1669 }
1670
Julius Wernerefcee762014-11-10 13:14:24 -08001671 // Try finding relative offset of master header at end of file first.
1672 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
1673 offset = size + rel_offset;
1674 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
1675 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001676
Hung-Te Lineab2c812013-01-29 01:56:17 +08001677 if (offset >= size - sizeof(*header) ||
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001678 !cbfs_header_valid((struct cbfs_header *)(data + offset))) {
Julius Wernerefcee762014-11-10 13:14:24 -08001679 // Some use cases append non-CBFS data to the end of the ROM.
1680 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +08001681 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001682 }
Hung-Te Lineab2c812013-01-29 01:56:17 +08001683
1684 for (; offset + sizeof(*header) < size; offset++) {
1685 header = (struct cbfs_header *)(data + offset);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001686 if (!cbfs_header_valid(header))
Hung-Te Lineab2c812013-01-29 01:56:17 +08001687 continue;
Julius Wernerefcee762014-11-10 13:14:24 -08001688 if (!found++)
1689 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001690 }
Julius Wernerefcee762014-11-10 13:14:24 -08001691 if (found > 1)
1692 // Top-aligned images usually have a working relative offset
1693 // field, so this is more likely to happen on bottom-aligned
1694 // ones (where the first header is the "outermost" one)
1695 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +08001696 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001697 return result;
1698}
1699
1700
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001701struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
1702{
Sol Boucher3e060ed2015-05-05 15:40:15 -07001703 assert(image);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001704 if (image->has_header)
1705 /* header.offset is relative to start of flash, not
1706 * start of region, so use it with the full image.
1707 */
1708 return (struct cbfs_file *)
1709 (buffer_get_original_backing(&image->buffer) +
1710 image->header.offset);
1711 else
1712 return (struct cbfs_file *)buffer_get(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001713}
1714
1715struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001716 struct cbfs_file *entry)
1717{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001718 uint32_t addr = cbfs_get_entry_addr(image, entry);
Julius Wernerd4775652020-03-13 16:43:34 -07001719 int align = image->has_header ? image->header.align : CBFS_ALIGNMENT;
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001720 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001721 addr += ntohl(entry->offset) + ntohl(entry->len);
1722 addr = align_up(addr, align);
1723 return (struct cbfs_file *)(image->buffer.data + addr);
1724}
1725
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001726uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
1727{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001728 assert(image && image->buffer.data && entry);
1729 return (int32_t)((char *)entry - image->buffer.data);
1730}
1731
Sol Boucher67a0a862015-03-18 12:36:27 -07001732int cbfs_is_valid_cbfs(struct cbfs_image *image)
1733{
1734 return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC,
1735 strlen(CBFS_FILE_MAGIC));
1736}
1737
1738int cbfs_is_legacy_cbfs(struct cbfs_image *image)
1739{
1740 return image->has_header;
1741}
1742
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001743int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
1744{
Sol Bouchere3260a02015-03-25 13:40:08 -07001745 uint32_t offset = cbfs_get_entry_addr(image, entry);
1746
1747 if (offset >= image->buffer.size)
1748 return 0;
1749
1750 struct buffer entry_data;
1751 buffer_clone(&entry_data, &image->buffer);
1752 buffer_seek(&entry_data, offset);
1753 return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
Sol Boucher67a0a862015-03-18 12:36:27 -07001754 strlen(CBFS_FILE_MAGIC));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001755}
1756
Patrick Georgi57edf162015-08-12 09:20:11 +02001757struct cbfs_file *cbfs_create_file_header(int type,
Vadim Bendebury45e59972014-12-23 15:59:57 -08001758 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001759{
Julius Wernerd4775652020-03-13 16:43:34 -07001760 struct cbfs_file *entry = malloc(CBFS_METADATA_MAX_SIZE);
1761 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, CBFS_METADATA_MAX_SIZE);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001762 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
Patrick Georgiedf25d92015-08-12 09:12:06 +02001763 entry->type = htonl(type);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001764 entry->len = htonl(len);
Patrick Georgi0d618af2015-07-15 18:28:23 +02001765 entry->attributes_offset = 0;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001766 entry->offset = htonl(cbfs_calculate_file_header_size(name));
Patrick Georgic569b8b2015-07-15 16:42:38 +02001767 memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry));
1768 strcpy(entry->filename, name);
Patrick Georgi57edf162015-08-12 09:20:11 +02001769 return entry;
1770}
1771
1772int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
1773 size_t len, const char *name)
1774{
1775 struct cbfs_file *tmp = cbfs_create_file_header(type, len, name);
1776 memcpy(entry, tmp, ntohl(tmp->offset));
1777 free(tmp);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001778 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1779 return 0;
1780}
1781
Patrick Georgi2c615062015-07-15 20:49:00 +02001782struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file)
1783{
1784 /* attributes_offset should be 0 when there is no attribute, but all
1785 * values that point into the cbfs_file header are invalid, too. */
1786 if (ntohl(file->attributes_offset) <= sizeof(*file))
1787 return NULL;
1788
1789 /* There needs to be enough space for the file header and one
1790 * attribute header for this to make sense. */
1791 if (ntohl(file->offset) <=
1792 sizeof(*file) + sizeof(struct cbfs_file_attribute))
1793 return NULL;
1794
1795 return (struct cbfs_file_attribute *)
1796 (((uint8_t *)file) + ntohl(file->attributes_offset));
1797}
1798
1799struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
1800 struct cbfs_file_attribute *attr)
1801{
1802 /* ex falso sequitur quodlibet */
1803 if (attr == NULL)
1804 return NULL;
1805
1806 /* Is there enough space for another attribute? */
1807 if ((uint8_t *)attr + ntohl(attr->len) +
Patrick Rudolphe28fa402019-02-14 11:19:07 +01001808 sizeof(struct cbfs_file_attribute) >
Patrick Georgi2c615062015-07-15 20:49:00 +02001809 (uint8_t *)file + ntohl(file->offset))
1810 return NULL;
1811
1812 struct cbfs_file_attribute *next = (struct cbfs_file_attribute *)
1813 (((uint8_t *)attr) + ntohl(attr->len));
1814 /* If any, "unused" attributes must come last. */
1815 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED)
1816 return NULL;
1817 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
1818 return NULL;
1819
1820 return next;
1821}
1822
1823struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
1824 uint32_t tag,
1825 uint32_t size)
1826{
Julius Werner5779ca72020-11-20 16:12:40 -08001827 assert(IS_ALIGNED(size, CBFS_ATTRIBUTE_ALIGN));
Patrick Georgi2c615062015-07-15 20:49:00 +02001828 struct cbfs_file_attribute *attr, *next;
1829 next = cbfs_file_first_attr(header);
1830 do {
1831 attr = next;
1832 next = cbfs_file_next_attr(header, attr);
1833 } while (next != NULL);
1834 uint32_t header_size = ntohl(header->offset) + size;
Julius Wernerd4775652020-03-13 16:43:34 -07001835 if (header_size > CBFS_METADATA_MAX_SIZE) {
Patrick Georgi2c615062015-07-15 20:49:00 +02001836 DEBUG("exceeding allocated space for cbfs_file headers");
1837 return NULL;
1838 }
1839 /* attr points to the last valid attribute now.
1840 * If NULL, we have to create the first one. */
1841 if (attr == NULL) {
1842 /* New attributes start where the header ends.
Elyes HAOUAS3db01982018-08-23 18:08:20 +02001843 * header->offset is later set to accommodate the
Patrick Georgi2c615062015-07-15 20:49:00 +02001844 * additional structure.
Elyes HAOUAS3db01982018-08-23 18:08:20 +02001845 * No endianness translation necessary here, because both
Patrick Georgi2c615062015-07-15 20:49:00 +02001846 * fields are encoded the same way. */
1847 header->attributes_offset = header->offset;
1848 attr = (struct cbfs_file_attribute *)
1849 (((uint8_t *)header) +
1850 ntohl(header->attributes_offset));
1851 } else {
1852 attr = (struct cbfs_file_attribute *)
1853 (((uint8_t *)attr) +
1854 ntohl(attr->len));
1855 }
1856 header->offset = htonl(header_size);
Julius Wernerd4775652020-03-13 16:43:34 -07001857 /* Attributes are expected to be small (much smaller than a flash page)
1858 and not really meant to be overwritten in-place. To avoid surprising
1859 values in reserved fields of attribute structures, initialize them to
1860 0, not 0xff. */
1861 memset(attr, 0, size);
Patrick Georgi2c615062015-07-15 20:49:00 +02001862 attr->tag = htonl(tag);
1863 attr->len = htonl(size);
1864 return attr;
1865}
1866
Patrick Georgi89f20342015-10-01 15:54:04 +02001867int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer,
Julius Wernerd4775652020-03-13 16:43:34 -07001868 enum vb2_hash_algorithm alg)
Patrick Georgi89f20342015-10-01 15:54:04 +02001869{
Julius Wernerd4775652020-03-13 16:43:34 -07001870 if (!vb2_digest_size(alg))
Patrick Georgi89f20342015-10-01 15:54:04 +02001871 return -1;
1872
Julius Wernerd4775652020-03-13 16:43:34 -07001873 struct cbfs_file_attr_hash *attr =
Patrick Georgi89f20342015-10-01 15:54:04 +02001874 (struct cbfs_file_attr_hash *)cbfs_add_file_attr(header,
Julius Wernerd4775652020-03-13 16:43:34 -07001875 CBFS_FILE_ATTR_TAG_HASH, cbfs_file_attr_hash_size(alg));
Patrick Georgi89f20342015-10-01 15:54:04 +02001876
Julius Wernerd4775652020-03-13 16:43:34 -07001877 if (attr == NULL)
Patrick Georgi89f20342015-10-01 15:54:04 +02001878 return -1;
1879
Julius Wernerd4775652020-03-13 16:43:34 -07001880 if (vb2_hash_calculate(buffer_get(buffer), buffer_size(buffer),
1881 alg, &attr->hash) != VB2_SUCCESS)
Patrick Georgi89f20342015-10-01 15:54:04 +02001882 return -1;
1883
1884 return 0;
1885}
1886
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001887/* Finds a place to hold whole data in same memory page. */
1888static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1889{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001890 if (!page)
1891 return 1;
1892 return (start / page) == (start + size - 1) / page;
1893}
1894
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001895/* Tests if data can fit in a range by given offset:
Aaron Durbind7339412015-09-15 12:50:14 -05001896 * start ->| metadata_size | offset (+ size) |<- end
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001897 */
Aaron Durbind7339412015-09-15 12:50:14 -05001898static int is_in_range(size_t start, size_t end, size_t metadata_size,
1899 size_t offset, size_t size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001900{
Aaron Durbind7339412015-09-15 12:50:14 -05001901 return (offset >= start + metadata_size && offset + size <= end);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001902}
1903
Werner Zeh95bfcae2016-01-25 12:47:20 +01001904static size_t absolute_align(const struct cbfs_image *image, size_t val,
1905 size_t align)
1906{
1907 const size_t region_offset = buffer_offset(&image->buffer);
1908 /* To perform alignment on absolute address, take the region offset */
1909 /* of the image into account. */
1910 return align_up(val + region_offset, align) - region_offset;
1911
1912}
1913
Aaron Durbind7339412015-09-15 12:50:14 -05001914int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size,
1915 size_t page_size, size_t align, size_t metadata_size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001916{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001917 struct cbfs_file *entry;
1918 size_t need_len;
Aaron Durbind7339412015-09-15 12:50:14 -05001919 size_t addr, addr_next, addr2, addr3, offset;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001920
1921 /* Default values: allow fitting anywhere in ROM. */
1922 if (!page_size)
Sol Boucher67a0a862015-03-18 12:36:27 -07001923 page_size = image->has_header ? image->header.romsize :
1924 image->buffer.size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001925 if (!align)
1926 align = 1;
1927
1928 if (size > page_size)
Aaron Durbind7339412015-09-15 12:50:14 -05001929 ERROR("Input file size (%zd) greater than page size (%zd).\n",
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001930 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001931
Aaron Durbind7339412015-09-15 12:50:14 -05001932 size_t image_align = image->has_header ? image->header.align :
Julius Wernerd4775652020-03-13 16:43:34 -07001933 CBFS_ALIGNMENT;
Sol Boucher67a0a862015-03-18 12:36:27 -07001934 if (page_size % image_align)
Aaron Durbind7339412015-09-15 12:50:14 -05001935 WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001936 __func__, page_size, image_align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001937
Aaron Durbind7339412015-09-15 12:50:14 -05001938 need_len = metadata_size + size;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001939
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001940 // Merge empty entries to build get max available space.
Julius Werner7066a1e2020-04-02 15:49:34 -07001941 cbfs_legacy_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001942
1943 /* Three cases of content location on memory page:
1944 * case 1.
1945 * | PAGE 1 | PAGE 2 |
1946 * | <header><content>| Fit. Return start of content.
1947 *
1948 * case 2.
1949 * | PAGE 1 | PAGE 2 |
1950 * | <header><content> | Fits when we shift content to align
1951 * shift-> | <header>|<content> | at starting of PAGE 2.
1952 *
1953 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001954 * | PAGE 1 | PAGE 2 | PAGE 3 |
1955 * | <header>< content > | Can't fit. If we shift content to
1956 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1957 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001958 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001959 * The returned address can be then used as "base-address" (-b) in add-*
1960 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1961 * For stage targets, the address is also used to re-link stage before
1962 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001963 */
1964 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001965 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001966 entry = cbfs_find_next_entry(image, entry)) {
1967
1968 uint32_t type = ntohl(entry->type);
Julius Wernerd4775652020-03-13 16:43:34 -07001969 if (type != CBFS_TYPE_NULL)
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001970 continue;
1971
1972 addr = cbfs_get_entry_addr(image, entry);
1973 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
1974 image, entry));
1975 if (addr_next - addr < need_len)
1976 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001977
Werner Zeh95bfcae2016-01-25 12:47:20 +01001978 offset = absolute_align(image, addr + metadata_size, align);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001979 if (is_in_same_page(offset, size, page_size) &&
Aaron Durbind7339412015-09-15 12:50:14 -05001980 is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001981 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001982 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001983 }
1984
1985 addr2 = align_up(addr, page_size);
Werner Zeh95bfcae2016-01-25 12:47:20 +01001986 offset = absolute_align(image, addr2, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001987 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001988 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001989 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001990 }
1991
Aaron Durbind7339412015-09-15 12:50:14 -05001992 /* Assume page_size >= metadata_size so adding one page will
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001993 * definitely provide the space for header. */
Aaron Durbind7339412015-09-15 12:50:14 -05001994 assert(page_size >= metadata_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001995 addr3 = addr2 + page_size;
Werner Zeh95bfcae2016-01-25 12:47:20 +01001996 offset = absolute_align(image, addr3, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001997 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001998 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001999 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002000 }
2001 }
2002 return -1;
2003}