blob: a042f0dac8cc9a62e9d735e45243f39adc46d85f [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
Hung-Te Lineab2c812013-01-29 01:56:17 +080037/* The file name align is not defined in CBFS spec -- only a preference by
38 * (old) cbfstool. */
39#define CBFS_FILENAME_ALIGN (16)
40
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080041static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070042 const char *default_value)
43{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080044 int i;
45 for (i = 0; desc[i].name; i++)
46 if (desc[i].type == type)
47 return desc[i].name;
48 return default_value;
49}
50
Sol Boucherec424862015-05-07 21:00:05 -070051static int lookup_type_by_name(const struct typedesc_t *desc, const char *name)
52{
53 int i;
54 for (i = 0; desc[i].name && strcasecmp(name, desc[i].name); ++i);
55 return desc[i].name ? (int)desc[i].type : -1;
56}
57
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010058static const char *get_cbfs_entry_type_name(uint32_t type)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070059{
Patrick Georgidc37dab2015-09-09 16:46:00 +020060 return lookup_name_by_type(filetypes, type, "(unknown)");
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080061}
62
Sol Boucherec424862015-05-07 21:00:05 -070063int cbfs_parse_comp_algo(const char *name)
64{
65 return lookup_type_by_name(types_cbfs_compression, name);
66}
67
Patrick Georgi89f20342015-10-01 15:54:04 +020068static const char *get_hash_attr_name(uint16_t hash_type)
69{
70 return lookup_name_by_type(types_cbfs_hash, hash_type, "(invalid)");
71}
72
73int cbfs_parse_hash_algo(const char *name)
74{
75 return lookup_type_by_name(types_cbfs_hash, name);
76}
77
Hung-Te Linc03d9b02013-01-29 02:38:40 +080078/* CBFS image */
79
Patrick Georgi11ee08f2015-08-11 15:10:02 +020080size_t cbfs_calculate_file_header_size(const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070081{
Hung-Te Linc03d9b02013-01-29 02:38:40 +080082 return (sizeof(struct cbfs_file) +
83 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
84}
85
Sol Boucher67a0a862015-03-18 12:36:27 -070086/* Only call on legacy CBFSes possessing a master header. */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -060087static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070088{
Sol Boucher67a0a862015-03-18 12:36:27 -070089 assert(image);
90 assert(cbfs_is_legacy_cbfs(image));
Hung-Te Lin49fcd752013-01-29 03:16:20 +080091 // A bug in old cbfstool may produce extra few bytes (by alignment) and
92 // cause cbfstool to overwrite things after free space -- which is
93 // usually CBFS header on x86. We need to workaround that.
Patrick Georgi343ea082016-02-10 18:07:52 +010094 // Except when we run across a file that contains the actual header,
95 // in which case this image is a safe, new-style
96 // `cbfstool add-master-header` based image.
Hung-Te Lin49fcd752013-01-29 03:16:20 +080097
98 struct cbfs_file *entry, *first = NULL, *last = NULL;
99 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800100 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800101 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgi343ea082016-02-10 18:07:52 +0100102 /* Is the header guarded by a CBFS file entry? Then exit */
103 if (((char *)entry) + ntohl(entry->offset) == hdr_loc) {
104 return 0;
105 }
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800106 last = entry;
107 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600108 if ((char *)first < (char *)hdr_loc &&
109 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800110 WARN("CBFS image was created with old cbfstool with size bug. "
111 "Fixing size in last entry...\n");
Sol Boucher3e060ed2015-05-05 15:40:15 -0700112 last->len = htonl(ntohl(last->len) - image->header.align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800113 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
114 cbfs_get_entry_addr(image, entry),
115 cbfs_get_entry_addr(image,
116 cbfs_find_next_entry(image, last)));
117 }
118 return 0;
119}
120
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800121void cbfs_put_header(void *dest, const struct cbfs_header *header)
122{
123 struct buffer outheader;
124
125 outheader.data = dest;
126 outheader.size = 0;
127
128 xdr_be.put32(&outheader, header->magic);
129 xdr_be.put32(&outheader, header->version);
130 xdr_be.put32(&outheader, header->romsize);
131 xdr_be.put32(&outheader, header->bootblocksize);
132 xdr_be.put32(&outheader, header->align);
133 xdr_be.put32(&outheader, header->offset);
134 xdr_be.put32(&outheader, header->architecture);
135}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600136
Hung-Te Lin0780d672014-05-16 10:14:05 +0800137static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
138 struct cbfs_payload_segment *input)
139{
140 struct buffer seg = {
141 .data = (void *)input,
142 .size = sizeof(*input),
143 };
144 output->type = xdr_be.get32(&seg);
145 output->compression = xdr_be.get32(&seg);
146 output->offset = xdr_be.get32(&seg);
147 output->load_addr = xdr_be.get64(&seg);
148 output->len = xdr_be.get32(&seg);
149 output->mem_len = xdr_be.get32(&seg);
150 assert(seg.size == 0);
151}
152
Patrick Georgia71c83f2015-08-26 12:23:26 +0200153static int cbfs_file_get_compression_info(struct cbfs_file *entry,
154 uint32_t *decompressed_size)
155{
156 unsigned int compression = CBFS_COMPRESS_NONE;
Patrick Georgia2ce7102016-12-14 16:08:52 +0100157 if (decompressed_size)
158 *decompressed_size = ntohl(entry->len);
Patrick Georgia71c83f2015-08-26 12:23:26 +0200159 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
160 attr != NULL;
161 attr = cbfs_file_next_attr(entry, attr)) {
162 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) {
163 struct cbfs_file_attr_compression *ac =
164 (struct cbfs_file_attr_compression *)attr;
165 compression = ntohl(ac->compression);
166 if (decompressed_size)
167 *decompressed_size =
168 ntohl(ac->decompressed_size);
169 }
170 }
171 return compression;
172}
173
Patrick Georgi89f20342015-10-01 15:54:04 +0200174static struct cbfs_file_attr_hash *cbfs_file_get_next_hash(
175 struct cbfs_file *entry, struct cbfs_file_attr_hash *cur)
176{
177 struct cbfs_file_attribute *attr = (struct cbfs_file_attribute *)cur;
178 if (attr == NULL) {
179 attr = cbfs_file_first_attr(entry);
180 if (attr == NULL)
181 return NULL;
182 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
183 return (struct cbfs_file_attr_hash *)attr;
184 }
185 while ((attr = cbfs_file_next_attr(entry, attr)) != NULL) {
186 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
187 return (struct cbfs_file_attr_hash *)attr;
188 };
189 return NULL;
190}
191
Sol Boucher0e539312015-03-05 15:38:03 -0800192void cbfs_get_header(struct cbfs_header *header, void *src)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600193{
194 struct buffer outheader;
195
Sol Boucher0e539312015-03-05 15:38:03 -0800196 outheader.data = src; /* We're not modifying the data */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600197 outheader.size = 0;
198
199 header->magic = xdr_be.get32(&outheader);
200 header->version = xdr_be.get32(&outheader);
201 header->romsize = xdr_be.get32(&outheader);
202 header->bootblocksize = xdr_be.get32(&outheader);
203 header->align = xdr_be.get32(&outheader);
204 header->offset = xdr_be.get32(&outheader);
205 header->architecture = xdr_be.get32(&outheader);
206}
207
Sol Boucher67a0a862015-03-18 12:36:27 -0700208int cbfs_image_create(struct cbfs_image *image, size_t entries_size)
209{
210 assert(image);
211 assert(image->buffer.data);
212
213 size_t empty_header_len = cbfs_calculate_file_header_size("");
214 uint32_t entries_offset = 0;
215 uint32_t align = CBFS_ENTRY_ALIGNMENT;
216 if (image->has_header) {
217 entries_offset = image->header.offset;
218
219 if (entries_offset > image->buffer.size) {
220 ERROR("CBFS file entries are located outside CBFS itself\n");
221 return -1;
222 }
223
224 align = image->header.align;
225 }
226
227 // This attribute must be given in order to prove that this module
228 // correctly preserves certain CBFS properties. See the block comment
229 // near the top of this file (and the associated commit message).
230 if (align < empty_header_len) {
231 ERROR("CBFS must be aligned to at least %zu bytes\n",
232 empty_header_len);
233 return -1;
234 }
235
236 if (entries_size > image->buffer.size - entries_offset) {
237 ERROR("CBFS doesn't have enough space to fit its file entries\n");
238 return -1;
239 }
240
241 if (empty_header_len > entries_size) {
242 ERROR("CBFS is too small to fit any header\n");
243 return -1;
244 }
245 struct cbfs_file *entry_header =
246 (struct cbfs_file *)(image->buffer.data + entries_offset);
247 // This alignment is necessary in order to prove that this module
248 // correctly preserves certain CBFS properties. See the block comment
249 // near the top of this file (and the associated commit message).
250 entries_size -= entries_size % align;
251
252 size_t capacity = entries_size - empty_header_len;
253 LOG("Created CBFS (capacity = %zu bytes)\n", capacity);
Patrick Georgiedf25d92015-08-12 09:12:06 +0200254 return cbfs_create_empty_entry(entry_header, CBFS_COMPONENT_NULL,
255 capacity, "");
Sol Boucher67a0a862015-03-18 12:36:27 -0700256}
257
258int cbfs_legacy_image_create(struct cbfs_image *image,
259 uint32_t architecture,
260 uint32_t align,
261 struct buffer *bootblock,
262 uint32_t bootblock_offset,
263 uint32_t header_offset,
264 uint32_t entries_offset)
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800265{
Sol Bouchere3260a02015-03-25 13:40:08 -0700266 assert(image);
267 assert(image->buffer.data);
268 assert(bootblock);
269
Julius Wernerefcee762014-11-10 13:14:24 -0800270 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800271 uint32_t cbfs_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600272 void *header_loc;
Sol Bouchere3260a02015-03-25 13:40:08 -0700273 size_t size = image->buffer.size;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800274
275 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
276 "header=0x%x+0x%zx, entries_offset=0x%x\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700277 bootblock_offset, bootblock->size, header_offset,
278 sizeof(image->header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800279
Sol Boucher67a0a862015-03-18 12:36:27 -0700280 // Adjust legacy top-aligned address to ROM offset.
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800281 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800282 entries_offset = size + (int32_t)entries_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800283 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800284 bootblock_offset = size + (int32_t)bootblock_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800285 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800286 header_offset = size + (int32_t)header_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800287
288 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
289 "header=0x%x, entries_offset=0x%x\n",
290 bootblock_offset, header_offset, entries_offset);
291
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800292 // Prepare bootblock
293 if (bootblock_offset + bootblock->size > size) {
294 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
295 bootblock_offset, bootblock->size, size);
296 return -1;
297 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800298 if (entries_offset > bootblock_offset &&
299 entries_offset < bootblock->size) {
300 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
301 bootblock_offset, bootblock->size, entries_offset);
302 return -1;
303 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800304 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
305 bootblock->size);
306
307 // Prepare header
Sol Boucher5bad3952015-05-05 20:35:26 -0700308 if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800309 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700310 header_offset, sizeof(image->header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800311 return -1;
312 }
Sol Boucher3e060ed2015-05-05 15:40:15 -0700313 image->header.magic = CBFS_HEADER_MAGIC;
314 image->header.version = CBFS_HEADER_VERSION;
315 image->header.romsize = size;
316 image->header.bootblocksize = bootblock->size;
317 image->header.align = align;
318 image->header.offset = entries_offset;
319 image->header.architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600320
321 header_loc = (image->buffer.data + header_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700322 cbfs_put_header(header_loc, &image->header);
Sol Boucher67a0a862015-03-18 12:36:27 -0700323 image->has_header = true;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800324
Julius Wernerefcee762014-11-10 13:14:24 -0800325 // The last 4 byte of the image contain the relative offset from the end
326 // of the image to the master header as a 32-bit signed integer. x86
327 // relies on this also being its (memory-mapped, top-aligned) absolute
328 // 32-bit address by virtue of how two's complement numbers work.
329 assert(size % sizeof(int32_t) == 0);
330 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
331 *rel_offset = header_offset - size;
332
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800333 // Prepare entries
334 if (align_up(entries_offset, align) != entries_offset) {
335 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
336 entries_offset, align);
337 return -1;
338 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800339 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800340 // e = min(bootblock, header, rel_offset) where e > entries_offset.
341 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800342 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
343 cbfs_len = bootblock_offset;
344 if (header_offset > entries_offset && header_offset < cbfs_len)
345 cbfs_len = header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700346
347 if (cbfs_image_create(image, cbfs_len - entries_offset))
348 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800349 return 0;
350}
351
Sol Bouchere3260a02015-03-25 13:40:08 -0700352int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
353 uint32_t offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700354{
Sol Bouchere3260a02015-03-25 13:40:08 -0700355 assert(out);
356 assert(in);
357 assert(in->data);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600358
Sol Bouchere3260a02015-03-25 13:40:08 -0700359 buffer_clone(&out->buffer, in);
Sol Boucher67a0a862015-03-18 12:36:27 -0700360 out->has_header = false;
361
Patrick Georgi2f953d32015-09-11 18:34:39 +0200362 if (cbfs_is_valid_cbfs(out)) {
363 return 0;
364 }
365
Sol Bouchere3260a02015-03-25 13:40:08 -0700366 void *header_loc = cbfs_find_header(in->data, in->size, offset);
367 if (header_loc) {
368 cbfs_get_header(&out->header, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700369 out->has_header = true;
Sol Bouchere3260a02015-03-25 13:40:08 -0700370 cbfs_fix_legacy_size(out, header_loc);
Patrick Georgi2f953d32015-09-11 18:34:39 +0200371 return 0;
Sol Boucher67a0a862015-03-18 12:36:27 -0700372 } else if (offset != ~0u) {
373 ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n");
374 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800375 }
Patrick Georgi2f953d32015-09-11 18:34:39 +0200376 ERROR("Selected image region is not a valid CBFS.\n");
377 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800378}
379
Patrick Georgi214e4af2015-11-20 19:22:50 +0100380int cbfs_copy_instance(struct cbfs_image *image, struct buffer *dst)
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800381{
Sol Boucher67a0a862015-03-18 12:36:27 -0700382 assert(image);
Sol Boucher67a0a862015-03-18 12:36:27 -0700383
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800384 struct cbfs_file *src_entry, *dst_entry;
Patrick Georgibd0bb232015-11-20 21:48:18 +0100385 size_t align;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800386 ssize_t last_entry_size;
387
Patrick Georgi214e4af2015-11-20 19:22:50 +0100388 size_t copy_end = buffer_size(dst);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800389
Patrick Georgibd0bb232015-11-20 21:48:18 +0100390 align = CBFS_ENTRY_ALIGNMENT;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800391
Patrick Georgibd0bb232015-11-20 21:48:18 +0100392 dst_entry = (struct cbfs_file *)buffer_get(dst);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800393
394 /* Copy non-empty files */
395 for (src_entry = cbfs_find_first_entry(image);
396 src_entry && cbfs_is_valid_entry(image, src_entry);
397 src_entry = cbfs_find_next_entry(image, src_entry)) {
398 size_t entry_size;
399
400 if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) ||
Patrick Georgibd0bb232015-11-20 21:48:18 +0100401 (src_entry->type == htonl(CBFS_COMPONENT_CBFSHEADER)) ||
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800402 (src_entry->type == htonl(CBFS_COMPONENT_DELETED)))
403 continue;
404
405 entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
406 memcpy(dst_entry, src_entry, entry_size);
407 dst_entry = (struct cbfs_file *)(
408 (uintptr_t)dst_entry + align_up(entry_size, align));
409
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700410 if ((size_t)((uint8_t *)dst_entry - (uint8_t *)buffer_get(dst))
411 >= copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800412 ERROR("Ran out of room in copy region.\n");
413 return 1;
414 }
415 }
416
Patrick Georgibd0bb232015-11-20 21:48:18 +0100417 /* Last entry size is all the room above it, except for top 4 bytes
418 * which may be used by the master header pointer. This messes with
419 * the ability to stash something "top-aligned" into the region, but
420 * keeps things simpler. */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700421 last_entry_size = copy_end -
422 ((uint8_t *)dst_entry - (uint8_t *)buffer_get(dst)) -
423 cbfs_calculate_file_header_size("") - sizeof(int32_t);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800424
425 if (last_entry_size < 0)
426 WARN("No room to create the last entry!\n")
427 else
Patrick Georgiedf25d92015-08-12 09:12:06 +0200428 cbfs_create_empty_entry(dst_entry, CBFS_COMPONENT_NULL,
429 last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800430
431 return 0;
432}
433
Patrick Georgi5d982d72017-09-19 14:39:58 +0200434int cbfs_expand_to_region(struct buffer *region)
435{
436 if (buffer_get(region) == NULL)
437 return 1;
438
439 struct cbfs_image image;
440 memset(&image, 0, sizeof(image));
441 if (cbfs_image_from_buffer(&image, region, 0)) {
442 ERROR("reading CBFS failed!\n");
443 return 1;
444 }
445
446 uint32_t region_sz = buffer_size(region);
447
448 struct cbfs_file *entry;
449 for (entry = buffer_get(region);
450 cbfs_is_valid_entry(&image, entry);
451 entry = cbfs_find_next_entry(&image, entry)) {
452 /* just iterate through */
453 }
454
455 /* entry now points to the first aligned address after the last valid
456 * file header. That's either outside the image or exactly the place
457 * where we need to create a new file.
458 */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700459 int last_entry_size = region_sz -
460 ((uint8_t *)entry - (uint8_t *)buffer_get(region)) -
461 cbfs_calculate_file_header_size("") - sizeof(int32_t);
Patrick Georgi5d982d72017-09-19 14:39:58 +0200462
463 if (last_entry_size > 0) {
464 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL,
465 last_entry_size, "");
466 /* If the last entry was an empty file, merge them. */
467 cbfs_walk(&image, cbfs_merge_empty_entry, NULL);
468 }
469
470 return 0;
471}
472
Patrick Georgi12631a42017-09-20 11:59:18 +0200473int cbfs_truncate_space(struct buffer *region, uint32_t *size)
474{
475 if (buffer_get(region) == NULL)
476 return 1;
477
478 struct cbfs_image image;
479 memset(&image, 0, sizeof(image));
480 if (cbfs_image_from_buffer(&image, region, 0)) {
481 ERROR("reading CBFS failed!\n");
482 return 1;
483 }
484
485 struct cbfs_file *entry, *trailer;
486 for (trailer = entry = buffer_get(region);
487 cbfs_is_valid_entry(&image, entry);
488 trailer = entry,
489 entry = cbfs_find_next_entry(&image, entry)) {
490 /* just iterate through */
491 }
492
493 /* trailer now points to the last valid CBFS entry's header.
494 * If that file is empty, remove it and report its header's offset as
495 * maximum size.
496 */
497 if ((strlen(trailer->filename) != 0) &&
498 (trailer->type != htonl(CBFS_COMPONENT_NULL)) &&
499 (trailer->type != htonl(CBFS_COMPONENT_DELETED))) {
500 /* nothing to truncate. Return de-facto CBFS size in case it
501 * was already truncated. */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700502 *size = (uint8_t *)entry - (uint8_t *)buffer_get(region);
Patrick Georgi12631a42017-09-20 11:59:18 +0200503 return 0;
504 }
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700505 *size = (uint8_t *)trailer - (uint8_t *)buffer_get(region);
Patrick Georgi12631a42017-09-20 11:59:18 +0200506 memset(trailer, 0xff, buffer_size(region) - *size);
507
508 return 0;
509}
510
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600511static size_t cbfs_file_entry_metadata_size(const struct cbfs_file *f)
512{
513 return ntohl(f->offset);
514}
515
516static size_t cbfs_file_entry_data_size(const struct cbfs_file *f)
517{
518 return ntohl(f->len);
519}
520
521static size_t cbfs_file_entry_size(const struct cbfs_file *f)
522{
523 return cbfs_file_entry_metadata_size(f) + cbfs_file_entry_data_size(f);
524}
525
526int cbfs_compact_instance(struct cbfs_image *image)
527{
528 assert(image);
529
530 struct cbfs_file *prev;
531 struct cbfs_file *cur;
532
533 /* The prev entry will always be an empty entry. */
534 prev = NULL;
535
536 /*
537 * Note: this function does not honor alignment or fixed location files.
538 * It's behavior is akin to cbfs_copy_instance() in that it expects
539 * the caller to understand the ramifications of compacting a
540 * fragmented CBFS image.
541 */
542
543 for (cur = cbfs_find_first_entry(image);
544 cur && cbfs_is_valid_entry(image, cur);
545 cur = cbfs_find_next_entry(image, cur)) {
546 size_t prev_size;
547 size_t cur_size;
548 size_t empty_metadata_size;
549 size_t spill_size;
550 uint32_t type = htonl(cur->type);
551
552 /* Current entry is empty. Kepp track of it. */
553 if ((type == htonl(CBFS_COMPONENT_NULL)) ||
554 (type == htonl(CBFS_COMPONENT_DELETED))) {
555 prev = cur;
556 continue;
557 }
558
559 /* Need to ensure the previous entry is an empty one. */
560 if (prev == NULL)
561 continue;
562
563 /* At this point prev is an empty entry. Put the non-empty
Patrick Georgi01cfecc2020-01-29 13:31:16 +0100564 * file in prev's location. Then add a new empty entry. This
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600565 * essentialy bubbles empty entries towards the end. */
566
567 prev_size = cbfs_file_entry_size(prev);
568 cur_size = cbfs_file_entry_size(cur);
569
570 /*
571 * Adjust the empty file size by the actual space occupied
572 * bewtween the beginning of the empty file and the non-empty
573 * file.
574 */
575 prev_size += (cbfs_get_entry_addr(image, cur) -
576 cbfs_get_entry_addr(image, prev)) - prev_size;
577
578 /* Move the non-empty file over the empty file. */
579 memmove(prev, cur, cur_size);
580
581 /*
582 * Get location of the empty file. Note that since prev was
583 * overwritten with the non-empty file the previously moved
584 * file needs to be used to calculate the empty file's location.
585 */
586 cur = cbfs_find_next_entry(image, prev);
587
588 /*
589 * The total space to work with for swapping the 2 entries
590 * consists of the 2 files' sizes combined. However, the
591 * cbfs_file entries start on CBFS_ALIGNMENT boundaries.
592 * Because of this the empty file size may end up smaller
593 * because of the non-empty file's metadata and data length.
594 *
595 * Calculate the spill size which is the amount of data lost
596 * due to the alignment constraints after moving the non-empty
597 * file.
598 */
599 spill_size = (cbfs_get_entry_addr(image, cur) -
600 cbfs_get_entry_addr(image, prev)) - cur_size;
601
602 empty_metadata_size = cbfs_calculate_file_header_size("");
603
604 /* Check if new empty size can contain the metadata. */
605 if (empty_metadata_size + spill_size > prev_size) {
606 ERROR("Unable to swap '%s' with prev empty entry.\n",
607 prev->filename);
608 return 1;
609 }
610
611 /* Update the empty file's size. */
612 prev_size -= spill_size + empty_metadata_size;
613
614 /* Create new empty file. */
615 cbfs_create_empty_entry(cur, CBFS_COMPONENT_NULL,
616 prev_size, "");
617
618 /* Merge any potential empty entries together. */
619 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
620
621 /*
622 * Since current switched to an empty file keep track of it.
623 * Even if any empty files were merged the empty entry still
624 * starts at previously calculated location.
625 */
626 prev = cur;
627 }
628
629 return 0;
630}
631
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700632int cbfs_image_delete(struct cbfs_image *image)
633{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100634 if (image == NULL)
635 return 0;
636
Hung-Te Lineab2c812013-01-29 01:56:17 +0800637 buffer_delete(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800638 return 0;
639}
640
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800641/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
642static int cbfs_add_entry_at(struct cbfs_image *image,
643 struct cbfs_file *entry,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800644 const void *data,
Patrick Georgi7fd14182015-08-11 15:55:16 +0200645 uint32_t content_offset,
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100646 const struct cbfs_file *header,
647 const size_t len_align)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700648{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800649 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
650 uint32_t addr = cbfs_get_entry_addr(image, entry),
651 addr_next = cbfs_get_entry_addr(image, next);
Patrick Georgi7fd14182015-08-11 15:55:16 +0200652 uint32_t min_entry_size = cbfs_calculate_file_header_size("");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200653 uint32_t len, header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700654 uint32_t align = image->has_header ? image->header.align :
655 CBFS_ENTRY_ALIGNMENT;
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200656 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800657
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200658 header_offset = content_offset - header_size;
659 if (header_offset % align)
660 header_offset -= header_offset % align;
661 if (header_offset < addr) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800662 ERROR("No space to hold cbfs_file header.");
663 return -1;
664 }
665
666 // Process buffer BEFORE content_offset.
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200667 if (header_offset - addr > min_entry_size) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800668 DEBUG("|min|...|header|content|... <create new entry>\n");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200669 len = header_offset - addr - min_entry_size;
Patrick Georgiedf25d92015-08-12 09:12:06 +0200670 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800671 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
672 entry = cbfs_find_next_entry(image, entry);
673 addr = cbfs_get_entry_addr(image, entry);
674 }
675
Patrick Georgi7a33b532015-08-25 13:00:04 +0200676 len = content_offset - addr - header_size;
Patrick Georgia60e7b62015-08-25 22:26:02 +0200677 memcpy(entry, header, header_size);
Patrick Georgi7a33b532015-08-25 13:00:04 +0200678 if (len != 0) {
Elyes HAOUAS3db01982018-08-23 18:08:20 +0200679 /* the header moved backwards a bit to accommodate cbfs_file
Patrick Georgi7a33b532015-08-25 13:00:04 +0200680 * alignment requirements, so patch up ->offset to still point
681 * to file data.
682 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800683 DEBUG("|..|header|content|... <use offset to create entry>\n");
Patrick Georgiae7efb92015-08-25 13:11:28 +0200684 DEBUG("before: offset=0x%x\n", ntohl(entry->offset));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800685 // TODO reset expanded name buffer to 0xFF.
Patrick Georgi7a33b532015-08-25 13:00:04 +0200686 entry->offset = htonl(ntohl(entry->offset) + len);
Patrick Georgiae7efb92015-08-25 13:11:28 +0200687 DEBUG("after: offset=0x%x\n", ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800688 }
689
690 // Ready to fill data into entry.
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800691 DEBUG("content_offset: 0x%x, entry location: %x\n",
692 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
693 image->buffer.data));
694 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
Patrick Georgicccc9d42015-04-28 13:09:36 +0200695 (ptrdiff_t)content_offset);
Patrick Georgi19c80b22015-08-25 13:16:04 +0200696 memcpy(CBFS_SUBHEADER(entry), data, ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800697 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
698
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100699 // Align the length to a multiple of len_align
700 if (len_align &&
701 ((ntohl(entry->offset) + ntohl(entry->len)) % len_align)) {
702 size_t off = (ntohl(entry->offset) + ntohl(entry->len)) % len_align;
703 entry->len = htonl(ntohl(entry->len) + len_align - off);
704 }
705
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800706 // Process buffer AFTER entry.
707 entry = cbfs_find_next_entry(image, entry);
708 addr = cbfs_get_entry_addr(image, entry);
Sol Boucher05725652015-04-02 20:58:26 -0700709 if (addr == addr_next)
710 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800711
Sol Boucher05725652015-04-02 20:58:26 -0700712 assert(addr < addr_next);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800713 if (addr_next - addr < min_entry_size) {
Sol Boucher636cc852015-04-03 09:13:04 -0700714 DEBUG("No need for new \"empty\" entry\n");
715 /* No need to increase the size of the just
716 * stored file to extend to next file. Alignment
717 * of next file takes care of this.
718 */
719 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800720 }
721
722 len = addr_next - addr - min_entry_size;
Patrick Georgi29a04d92015-11-20 23:23:44 +0100723 /* keep space for master header pointer */
Richard Spiegelb59c1f42018-11-02 11:14:38 -0700724 if ((uint8_t *)entry + min_entry_size + len >
725 (uint8_t *)buffer_get(&image->buffer) +
726 buffer_size(&image->buffer) - sizeof(int32_t)) {
Patrick Georgi29a04d92015-11-20 23:23:44 +0100727 len -= sizeof(int32_t);
728 }
Patrick Georgiedf25d92015-08-12 09:12:06 +0200729 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800730 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
731 return 0;
732}
733
734int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Patrick Georgie5903582015-08-25 13:53:42 +0200735 uint32_t content_offset,
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100736 struct cbfs_file *header,
737 const size_t len_align)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700738{
Sol Boucher67d59982015-05-07 02:39:22 -0700739 assert(image);
740 assert(buffer);
741 assert(buffer->data);
Sol Boucher67d59982015-05-07 02:39:22 -0700742 assert(!IS_TOP_ALIGNED_ADDRESS(content_offset));
743
Patrick Georgia60e7b62015-08-25 22:26:02 +0200744 const char *name = header->filename;
Patrick Georgie5903582015-08-25 13:53:42 +0200745
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800746 uint32_t entry_type;
747 uint32_t addr, addr_next;
748 struct cbfs_file *entry, *next;
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200749 uint32_t need_size;
Patrick Georgif5252f32015-08-25 22:27:57 +0200750 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800751
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800752 need_size = header_size + buffer->size;
753 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
754 name, content_offset, header_size, buffer->size, need_size);
755
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800756 // Merge empty entries.
757 DEBUG("(trying to merge empty entries...)\n");
758 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
759
760 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800761 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800762 entry = cbfs_find_next_entry(image, entry)) {
763
764 entry_type = ntohl(entry->type);
765 if (entry_type != CBFS_COMPONENT_NULL)
766 continue;
767
768 addr = cbfs_get_entry_addr(image, entry);
769 next = cbfs_find_next_entry(image, entry);
770 addr_next = cbfs_get_entry_addr(image, next);
771
772 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
773 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600774
775 /* Will the file fit? Don't yet worry if we have space for a new
776 * "empty" entry. We take care of that later.
777 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800778 if (addr + need_size > addr_next)
779 continue;
780
Patrick Georgiaa44dbd2015-08-12 12:05:21 +0200781 // Test for complicated cases
782 if (content_offset > 0) {
783 if (addr_next < content_offset) {
784 DEBUG("Not for specified offset yet");
785 continue;
786 } else if (addr > content_offset) {
787 DEBUG("Exceed specified content_offset.");
788 break;
789 } else if (addr + header_size > content_offset) {
790 ERROR("Not enough space for header.\n");
791 break;
792 } else if (content_offset + buffer->size > addr_next) {
793 ERROR("Not enough space for content.\n");
794 break;
795 }
796 }
797
798 // TODO there are more few tricky cases that we may
799 // want to fit by altering offset.
800
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200801 if (content_offset == 0) {
802 // we tested every condition earlier under which
803 // placing the file there might fail
804 content_offset = addr + header_size;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800805 }
806
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800807 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
808 addr, addr_next - addr, content_offset);
809
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200810 if (cbfs_add_entry_at(image, entry, buffer->data,
Philipp Deppenwiese7ba58712018-11-20 13:54:49 +0100811 content_offset, header, len_align) == 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800812 return 0;
813 }
814 break;
815 }
816
817 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
818 buffer->name, buffer->size, buffer->size / 1024, content_offset);
819 return -1;
820}
821
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700822struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
823{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800824 struct cbfs_file *entry;
825 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800826 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800827 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgic569b8b2015-07-15 16:42:38 +0200828 if (strcasecmp(entry->filename, name) == 0) {
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800829 DEBUG("cbfs_get_entry: found %s\n", name);
830 return entry;
831 }
832 }
833 return NULL;
834}
835
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500836static int cbfs_stage_decompress(struct cbfs_stage *stage, struct buffer *buff)
Aaron Durbin539aed02015-10-23 17:42:32 -0500837{
838 struct buffer reader;
Aaron Durbin539aed02015-10-23 17:42:32 -0500839 char *orig_buffer;
840 char *new_buffer;
841 size_t new_buff_sz;
842 decomp_func_ptr decompress;
843
844 buffer_clone(&reader, buff);
845
846 /* The stage metadata is in little endian. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500847 stage->compression = xdr_le.get32(&reader);
848 stage->entry = xdr_le.get64(&reader);
849 stage->load = xdr_le.get64(&reader);
850 stage->len = xdr_le.get32(&reader);
851 stage->memlen = xdr_le.get32(&reader);
Aaron Durbin539aed02015-10-23 17:42:32 -0500852
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500853 /* Create a buffer just with the uncompressed program now that the
854 * struct cbfs_stage has been peeled off. */
855 if (stage->compression == CBFS_COMPRESS_NONE) {
856 new_buff_sz = buffer_size(buff) - sizeof(struct cbfs_stage);
857
858 orig_buffer = buffer_get(buff);
859 new_buffer = calloc(1, new_buff_sz);
860 memcpy(new_buffer, orig_buffer + sizeof(struct cbfs_stage),
861 new_buff_sz);
862 buffer_init(buff, buff->name, new_buffer, new_buff_sz);
863 free(orig_buffer);
Aaron Durbin539aed02015-10-23 17:42:32 -0500864 return 0;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500865 }
Aaron Durbin539aed02015-10-23 17:42:32 -0500866
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500867 decompress = decompression_function(stage->compression);
Aaron Durbin539aed02015-10-23 17:42:32 -0500868 if (decompress == NULL)
869 return -1;
870
871 orig_buffer = buffer_get(buff);
872
873 /* This can be too big of a buffer needed, but there's no current
874 * field indicating decompressed size of data. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500875 new_buff_sz = stage->memlen;
Aaron Durbin539aed02015-10-23 17:42:32 -0500876 new_buffer = calloc(1, new_buff_sz);
877
878 if (decompress(orig_buffer + sizeof(struct cbfs_stage),
879 (int)(buffer_size(buff) - sizeof(struct cbfs_stage)),
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500880 new_buffer, (int)new_buff_sz, &new_buff_sz)) {
Aaron Durbin539aed02015-10-23 17:42:32 -0500881 ERROR("Couldn't decompress stage.\n");
882 free(new_buffer);
883 return -1;
884 }
885
886 /* Include correct size for full stage info. */
Aaron Durbin539aed02015-10-23 17:42:32 -0500887 buffer_init(buff, buff->name, new_buffer, new_buff_sz);
Aaron Durbin539aed02015-10-23 17:42:32 -0500888
889 /* True decompressed size is just the data size -- no metadata. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500890 stage->len = new_buff_sz;
Aaron Durbin539aed02015-10-23 17:42:32 -0500891 /* Stage is not compressed. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500892 stage->compression = CBFS_COMPRESS_NONE;
Aaron Durbin539aed02015-10-23 17:42:32 -0500893
894 free(orig_buffer);
895
896 return 0;
897}
898
Antonello Dettorifda691e2016-06-09 12:35:36 +0200899static int cbfs_payload_decompress(struct cbfs_payload_segment *segments,
900 struct buffer *buff, int num_seg)
901{
902 struct buffer new_buffer;
903 struct buffer seg_buffer;
904 size_t new_buff_sz;
905 char *in_ptr;
906 char *out_ptr;
907 size_t new_offset;
908 decomp_func_ptr decompress;
909
910 new_offset = num_seg * sizeof(*segments);
911 new_buff_sz = num_seg * sizeof(*segments);
912
913 /* Find out and allocate the amount of memory occupied
914 * by the binary data */
915 for (int i = 0; i < num_seg; i++)
916 new_buff_sz += segments[i].mem_len;
917
Furquan Shaikh58644a02016-08-05 08:27:18 -0700918 if (buffer_create(&new_buffer, new_buff_sz, "decompressed_buff"))
919 return -1;
Antonello Dettorifda691e2016-06-09 12:35:36 +0200920
921 in_ptr = buffer_get(buff) + new_offset;
922 out_ptr = buffer_get(&new_buffer) + new_offset;
923
924 for (int i = 0; i < num_seg; i++) {
925 struct buffer tbuff;
926 size_t decomp_size;
927
Antonello Dettorifda691e2016-06-09 12:35:36 +0200928 /* Segments BSS and ENTRY do not have binary data. */
929 if (segments[i].type == PAYLOAD_SEGMENT_BSS ||
930 segments[i].type == PAYLOAD_SEGMENT_ENTRY) {
931 continue;
932 } else if (segments[i].type == PAYLOAD_SEGMENT_PARAMS) {
933 memcpy(out_ptr, in_ptr, segments[i].len);
934 segments[i].offset = new_offset;
935 new_offset += segments[i].len;
936 in_ptr += segments[i].len;
937 out_ptr += segments[i].len;
938 segments[i].compression = CBFS_COMPRESS_NONE;
939 continue;
940 }
941
Joel Kitching72d77a92018-07-18 13:23:52 +0800942 /* The payload uses an unknown compression algorithm. */
943 decompress = decompression_function(segments[i].compression);
944 if (decompress == NULL) {
945 ERROR("Unknown decompression algorithm: %u\n",
946 segments[i].compression);
947 return -1;
948 }
949
Furquan Shaikh58644a02016-08-05 08:27:18 -0700950 if (buffer_create(&tbuff, segments[i].mem_len, "segment")) {
951 buffer_delete(&new_buffer);
952 return -1;
953 }
Antonello Dettorifda691e2016-06-09 12:35:36 +0200954
955 if (decompress(in_ptr, segments[i].len, buffer_get(&tbuff),
956 (int) buffer_size(&tbuff),
957 &decomp_size)) {
958 ERROR("Couldn't decompress payload segment %u\n", i);
959 buffer_delete(&new_buffer);
Furquan Shaikh9844d562016-08-05 08:32:23 -0700960 buffer_delete(&tbuff);
Antonello Dettorifda691e2016-06-09 12:35:36 +0200961 return -1;
962 }
963
964 memcpy(out_ptr, buffer_get(&tbuff), decomp_size);
965
966 in_ptr += segments[i].len;
967
968 /* Update the offset of the segment. */
969 segments[i].offset = new_offset;
970 /* True decompressed size is just the data size. No metadata */
971 segments[i].len = decomp_size;
972 /* Segment is not compressed. */
973 segments[i].compression = CBFS_COMPRESS_NONE;
974
975 /* Update the offset and output buffer pointer. */
976 new_offset += decomp_size;
977 out_ptr += decomp_size;
978
979 buffer_delete(&tbuff);
980 }
981
982 buffer_splice(&seg_buffer, &new_buffer, 0, 0);
983 xdr_segs(&seg_buffer, segments, num_seg);
984
985 buffer_delete(buff);
986 *buff = new_buffer;
987
988 return 0;
989}
990
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500991static int init_elf_from_arch(Elf64_Ehdr *ehdr, uint32_t cbfs_arch)
992{
993 int endian;
994 int nbits;
995 int machine;
996
997 switch (cbfs_arch) {
998 case CBFS_ARCHITECTURE_X86:
999 endian = ELFDATA2LSB;
1000 nbits = ELFCLASS32;
1001 machine = EM_386;
1002 break;
1003 case CBFS_ARCHITECTURE_ARM:
1004 endian = ELFDATA2LSB;
1005 nbits = ELFCLASS32;
1006 machine = EM_ARM;
1007 break;
1008 case CBFS_ARCHITECTURE_AARCH64:
1009 endian = ELFDATA2LSB;
1010 nbits = ELFCLASS64;
1011 machine = EM_AARCH64;
1012 break;
1013 case CBFS_ARCHITECTURE_MIPS:
1014 endian = ELFDATA2LSB;
1015 nbits = ELFCLASS32;
1016 machine = EM_MIPS;
1017 break;
1018 case CBFS_ARCHITECTURE_RISCV:
1019 endian = ELFDATA2LSB;
1020 nbits = ELFCLASS32;
1021 machine = EM_RISCV;
1022 break;
1023 default:
1024 ERROR("Unsupported arch: %x\n", cbfs_arch);
1025 return -1;
1026 }
1027
1028 elf_init_eheader(ehdr, machine, nbits, endian);
1029 return 0;
1030}
1031
1032static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch)
1033{
1034 Elf64_Ehdr ehdr;
1035 Elf64_Shdr shdr;
1036 struct cbfs_stage stage;
1037 struct elf_writer *ew;
1038 struct buffer elf_out;
1039 size_t empty_sz;
Aaron Durbin694fd132015-10-28 11:39:34 -05001040 int rmod_ret;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001041
Antonello Dettori0b806282016-06-26 00:24:25 +02001042 if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
1043 ERROR("You need to specify -m ARCH.\n");
1044 return -1;
1045 }
1046
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001047 if (cbfs_stage_decompress(&stage, buff)) {
1048 ERROR("Failed to decompress stage.\n");
1049 return -1;
1050 }
1051
1052 if (init_elf_from_arch(&ehdr, arch))
1053 return -1;
1054
1055 ehdr.e_entry = stage.entry;
1056
Aaron Durbin694fd132015-10-28 11:39:34 -05001057 /* Attempt rmodule translation first. */
1058 rmod_ret = rmodule_stage_to_elf(&ehdr, buff);
1059
1060 if (rmod_ret < 0) {
1061 ERROR("rmodule parsing failed\n");
1062 return -1;
1063 } else if (rmod_ret == 0)
1064 return 0;
1065
1066 /* Rmodule couldn't do anything with the data. Continue on with SELF. */
1067
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001068 ew = elf_writer_init(&ehdr);
1069 if (ew == NULL) {
1070 ERROR("Unable to init ELF writer.\n");
1071 return -1;
1072 }
1073
1074 memset(&shdr, 0, sizeof(shdr));
1075 shdr.sh_type = SHT_PROGBITS;
1076 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
1077 shdr.sh_addr = stage.load;
1078 shdr.sh_size = stage.len;
1079 empty_sz = stage.memlen - stage.len;
1080
1081 if (elf_writer_add_section(ew, &shdr, buff, ".program")) {
1082 ERROR("Unable to add ELF section: .program\n");
1083 elf_writer_destroy(ew);
1084 return -1;
1085 }
1086
1087 if (empty_sz != 0) {
1088 struct buffer b;
1089
1090 buffer_init(&b, NULL, NULL, 0);
1091 memset(&shdr, 0, sizeof(shdr));
1092 shdr.sh_type = SHT_NOBITS;
1093 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
1094 shdr.sh_addr = stage.load + stage.len;
1095 shdr.sh_size = empty_sz;
1096 if (elf_writer_add_section(ew, &shdr, &b, ".empty")) {
1097 ERROR("Unable to add ELF section: .empty\n");
1098 elf_writer_destroy(ew);
1099 return -1;
1100 }
1101 }
1102
1103 if (elf_writer_serialize(ew, &elf_out)) {
1104 ERROR("Unable to create ELF file from stage.\n");
1105 elf_writer_destroy(ew);
1106 return -1;
1107 }
1108
1109 /* Flip buffer with the created ELF one. */
1110 buffer_delete(buff);
1111 *buff = elf_out;
1112
1113 elf_writer_destroy(ew);
1114
1115 return 0;
1116}
1117
Antonello Dettorifda691e2016-06-09 12:35:36 +02001118static int cbfs_payload_make_elf(struct buffer *buff, uint32_t arch)
1119{
1120 Elf64_Ehdr ehdr;
1121 Elf64_Shdr shdr;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001122 struct cbfs_payload_segment *segs = NULL;
Jonathan Neuschäfer5de54582016-08-09 15:01:58 +02001123 struct elf_writer *ew = NULL;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001124 struct buffer elf_out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001125 int segments = 0;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001126 int retval = -1;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001127
Antonello Dettori0b806282016-06-26 00:24:25 +02001128 if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
1129 ERROR("You need to specify -m ARCH.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001130 goto out;
Antonello Dettori0b806282016-06-26 00:24:25 +02001131 }
1132
Antonello Dettorifda691e2016-06-09 12:35:36 +02001133 /* Count the number of segments inside buffer */
1134 while (true) {
1135 uint32_t payload_type = 0;
1136
1137 struct cbfs_payload_segment *seg;
1138
1139 seg = buffer_get(buff);
1140 payload_type = read_be32(&seg[segments].type);
1141
1142 if (payload_type == PAYLOAD_SEGMENT_CODE) {
1143 segments++;
1144 } else if (payload_type == PAYLOAD_SEGMENT_DATA) {
1145 segments++;
1146 } else if (payload_type == PAYLOAD_SEGMENT_BSS) {
1147 segments++;
1148 } else if (payload_type == PAYLOAD_SEGMENT_PARAMS) {
1149 segments++;
1150 } else if (payload_type == PAYLOAD_SEGMENT_ENTRY) {
1151 /* The last segment in a payload is always ENTRY as
1152 * specified by the parse_elf_to_payload() function.
1153 * Therefore there is no need to continue looking for
1154 * segments.*/
1155 segments++;
1156 break;
1157 } else {
1158 ERROR("Unknown payload segment type: %x\n",
1159 payload_type);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001160 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001161 }
1162 }
1163
1164 segs = malloc(segments * sizeof(*segs));
1165
1166 /* Decode xdr segments */
1167 for (int i = 0; i < segments; i++) {
1168 struct cbfs_payload_segment *serialized_seg = buffer_get(buff);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001169 xdr_get_seg(&segs[i], &serialized_seg[i]);
1170 }
1171
1172 if (cbfs_payload_decompress(segs, buff, segments)) {
1173 ERROR("Failed to decompress payload.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001174 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001175 }
1176
1177 if (init_elf_from_arch(&ehdr, arch))
Furquan Shaikh7b405172016-08-05 08:20:37 -07001178 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001179
1180 ehdr.e_entry = segs[segments-1].load_addr;
1181
1182 ew = elf_writer_init(&ehdr);
1183 if (ew == NULL) {
1184 ERROR("Unable to init ELF writer.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001185 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001186 }
1187
1188 for (int i = 0; i < segments; i++) {
1189 struct buffer tbuff;
Furquan Shaikhf3bba442016-08-05 08:12:31 -07001190 size_t empty_sz = 0;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001191
1192 memset(&shdr, 0, sizeof(shdr));
1193 char *name = NULL;
1194
1195 if (segs[i].type == PAYLOAD_SEGMENT_CODE) {
1196 shdr.sh_type = SHT_PROGBITS;
1197 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
1198 shdr.sh_addr = segs[i].load_addr;
1199 shdr.sh_size = segs[i].len;
1200 empty_sz = segs[i].mem_len - segs[i].len;
1201 name = strdup(".text");
1202 buffer_splice(&tbuff, buff, segs[i].offset,
1203 segs[i].len);
1204 } else if (segs[i].type == PAYLOAD_SEGMENT_DATA) {
1205 shdr.sh_type = SHT_PROGBITS;
1206 shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
1207 shdr.sh_addr = segs[i].load_addr;
1208 shdr.sh_size = segs[i].len;
1209 empty_sz = segs[i].mem_len - segs[i].len;
1210 name = strdup(".data");
1211 buffer_splice(&tbuff, buff, segs[i].offset,
1212 segs[i].len);
1213 } else if (segs[i].type == PAYLOAD_SEGMENT_BSS) {
1214 shdr.sh_type = SHT_NOBITS;
1215 shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
1216 shdr.sh_addr = segs[i].load_addr;
1217 shdr.sh_size = segs[i].len;
1218 name = strdup(".bss");
1219 buffer_splice(&tbuff, buff, 0, 0);
1220 } else if (segs[i].type == PAYLOAD_SEGMENT_PARAMS) {
1221 shdr.sh_type = SHT_NOTE;
1222 shdr.sh_flags = 0;
1223 shdr.sh_size = segs[i].len;
1224 name = strdup(".note.pinfo");
1225 buffer_splice(&tbuff, buff, segs[i].offset,
1226 segs[i].len);
1227 } else if (segs[i].type == PAYLOAD_SEGMENT_ENTRY) {
1228 break;
Patrick Georgidce629b2017-01-13 13:30:54 +01001229 } else {
1230 ERROR("unknown ELF segment type\n");
1231 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001232 }
1233
Patrick Georgidce629b2017-01-13 13:30:54 +01001234 if (!name) {
1235 ERROR("out of memory\n");
1236 goto out;
1237 }
Antonello Dettorifda691e2016-06-09 12:35:36 +02001238
1239 if (elf_writer_add_section(ew, &shdr, &tbuff, name)) {
1240 ERROR("Unable to add ELF section: %s\n", name);
Patrick Georgidce629b2017-01-13 13:30:54 +01001241 free(name);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001242 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001243 }
Patrick Georgidce629b2017-01-13 13:30:54 +01001244 free(name);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001245
1246 if (empty_sz != 0) {
1247 struct buffer b;
1248
1249 buffer_init(&b, NULL, NULL, 0);
1250 memset(&shdr, 0, sizeof(shdr));
1251 shdr.sh_type = SHT_NOBITS;
1252 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
1253 shdr.sh_addr = segs[i].load_addr + segs[i].len;
1254 shdr.sh_size = empty_sz;
1255 name = strdup(".empty");
Patrick Georgidce629b2017-01-13 13:30:54 +01001256 if (!name) {
1257 ERROR("out of memory\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001258 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001259 }
Patrick Georgidce629b2017-01-13 13:30:54 +01001260 if (elf_writer_add_section(ew, &shdr, &b, name)) {
1261 ERROR("Unable to add ELF section: %s\n", name);
1262 free(name);
1263 goto out;
1264 }
1265 free(name);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001266 }
Antonello Dettorifda691e2016-06-09 12:35:36 +02001267 }
1268
1269 if (elf_writer_serialize(ew, &elf_out)) {
1270 ERROR("Unable to create ELF file from stage.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001271 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001272 }
1273
1274 /* Flip buffer with the created ELF one. */
1275 buffer_delete(buff);
1276 *buff = elf_out;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001277 retval = 0;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001278
Furquan Shaikh7b405172016-08-05 08:20:37 -07001279out:
1280 free(segs);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001281 elf_writer_destroy(ew);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001282 return retval;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001283}
1284
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001285int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Joel Kitching21fdd892018-08-09 17:49:52 +08001286 const char *filename, uint32_t arch, bool do_processing)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001287{
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001288 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
1289 struct buffer buffer;
1290 if (!entry) {
1291 ERROR("File not found: %s\n", entry_name);
1292 return -1;
1293 }
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001294
Joel Kitching21fdd892018-08-09 17:49:52 +08001295 unsigned int compressed_size = ntohl(entry->len);
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001296 unsigned int decompressed_size = 0;
1297 unsigned int compression = cbfs_file_get_compression_info(entry,
1298 &decompressed_size);
Werner Zehbbf1df72018-11-21 13:07:50 +01001299 unsigned int buffer_len;
Joel Kitching21fdd892018-08-09 17:49:52 +08001300 decomp_func_ptr decompress;
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001301
Joel Kitching21fdd892018-08-09 17:49:52 +08001302 if (do_processing) {
1303 decompress = decompression_function(compression);
1304 if (!decompress) {
1305 ERROR("looking up decompression routine failed\n");
1306 return -1;
1307 }
Werner Zehbbf1df72018-11-21 13:07:50 +01001308 buffer_len = decompressed_size;
Joel Kitching21fdd892018-08-09 17:49:52 +08001309 } else {
1310 /* Force nop decompression */
1311 decompress = decompression_function(CBFS_COMPRESS_NONE);
Werner Zehbbf1df72018-11-21 13:07:50 +01001312 buffer_len = compressed_size;
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001313 }
1314
Joel Kitching21fdd892018-08-09 17:49:52 +08001315 LOG("Found file %.30s at 0x%x, type %.12s, compressed %d, size %d\n",
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001316 entry_name, cbfs_get_entry_addr(image, entry),
Joel Kitching21fdd892018-08-09 17:49:52 +08001317 get_cbfs_entry_type_name(ntohl(entry->type)), compressed_size,
1318 decompressed_size);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001319
Aaron Durbin539aed02015-10-23 17:42:32 -05001320 buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0);
Werner Zehbbf1df72018-11-21 13:07:50 +01001321 buffer.data = malloc(buffer_len);
1322 buffer.size = buffer_len;
Aaron Durbin539aed02015-10-23 17:42:32 -05001323
Joel Kitching21fdd892018-08-09 17:49:52 +08001324 if (decompress(CBFS_SUBHEADER(entry), compressed_size,
1325 buffer.data, buffer.size, NULL)) {
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001326 ERROR("decompression failed for %s\n", entry_name);
Aaron Durbin539aed02015-10-23 17:42:32 -05001327 buffer_delete(&buffer);
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001328 return -1;
1329 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001330
1331 /*
1332 * The stage metadata is never compressed proper for cbfs_stage
1333 * files. The contents of the stage data can be though. Therefore
1334 * one has to do a second pass for stages to potentially decompress
1335 * the stage data to make it more meaningful.
1336 */
Joel Kitching21fdd892018-08-09 17:49:52 +08001337 if (do_processing) {
1338 int (*make_elf)(struct buffer *, uint32_t) = NULL;
1339 switch (ntohl(entry->type)) {
1340 case CBFS_COMPONENT_STAGE:
1341 make_elf = cbfs_stage_make_elf;
1342 break;
1343 case CBFS_COMPONENT_SELF:
1344 make_elf = cbfs_payload_make_elf;
1345 break;
Aaron Durbin539aed02015-10-23 17:42:32 -05001346 }
Joel Kitching21fdd892018-08-09 17:49:52 +08001347 if (make_elf && make_elf(&buffer, arch)) {
1348 ERROR("Failed to write %s into %s.\n",
1349 entry_name, filename);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001350 buffer_delete(&buffer);
1351 return -1;
1352 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001353 }
1354
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001355 if (buffer_write_file(&buffer, filename) != 0) {
1356 ERROR("Failed to write %s into %s.\n",
1357 entry_name, filename);
Aaron Durbin539aed02015-10-23 17:42:32 -05001358 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001359 return -1;
1360 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001361
1362 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001363 INFO("Successfully dumped the file to: %s\n", filename);
1364 return 0;
1365}
1366
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001367int cbfs_remove_entry(struct cbfs_image *image, const char *name)
1368{
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +02001369 struct cbfs_file *entry;
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001370 entry = cbfs_get_entry(image, name);
1371 if (!entry) {
1372 ERROR("CBFS file %s not found.\n", name);
1373 return -1;
1374 }
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001375 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
Patrick Georgic569b8b2015-07-15 16:42:38 +02001376 entry->filename, cbfs_get_entry_addr(image, entry));
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001377 entry->type = htonl(CBFS_COMPONENT_DELETED);
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +02001378 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001379 return 0;
1380}
1381
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001382int cbfs_print_header_info(struct cbfs_image *image)
1383{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001384 char *name = strdup(image->buffer.name);
Sol Boucher3e060ed2015-05-05 15:40:15 -07001385 assert(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001386 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -08001387 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001388 basename(name),
1389 image->buffer.size / 1024,
Sol Boucher3e060ed2015-05-05 15:40:15 -07001390 image->header.bootblocksize,
1391 image->header.romsize,
1392 image->header.offset,
1393 image->header.align,
1394 arch_to_string(image->header.architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001395 free(name);
1396 return 0;
1397}
1398
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001399static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
1400{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001401 fprintf(fp,
1402 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
1403 "length: %d/%d\n",
1404 lookup_name_by_type(types_cbfs_compression,
1405 stage->compression, "(unknown)"),
1406 stage->entry,
1407 stage->load,
1408 stage->len,
1409 stage->memlen);
1410 return 0;
1411}
1412
Hung-Te Lin0780d672014-05-16 10:14:05 +08001413static int cbfs_print_decoded_payload_segment_info(
1414 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001415{
Hung-Te Lin0780d672014-05-16 10:14:05 +08001416 /* The input (seg) must be already decoded by
1417 * cbfs_decode_payload_segment.
1418 */
1419 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001420 case PAYLOAD_SEGMENT_CODE:
1421 case PAYLOAD_SEGMENT_DATA:
1422 fprintf(fp, " %s (%s compression, offset: 0x%x, "
1423 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001424 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001425 "code " : "data"),
1426 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +08001427 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001428 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +08001429 seg->offset, seg->load_addr, seg->len,
1430 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001431 break;
1432
1433 case PAYLOAD_SEGMENT_ENTRY:
1434 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001435 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001436 break;
1437
1438 case PAYLOAD_SEGMENT_BSS:
1439 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
1440 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001441 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001442 break;
1443
1444 case PAYLOAD_SEGMENT_PARAMS:
1445 fprintf(fp, " parameters\n");
1446 break;
1447
1448 default:
1449 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
1450 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001451 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001452 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +08001453 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001454 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +08001455 seg->offset, seg->load_addr, seg->len,
1456 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001457 break;
1458 }
1459 return 0;
1460}
1461
1462int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001463 void *arg)
1464{
Patrick Georgic569b8b2015-07-15 16:42:38 +02001465 const char *name = entry->filename;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001466 struct cbfs_payload_segment *payload;
1467 FILE *fp = (FILE *)arg;
1468
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001469 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001470 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1471 cbfs_get_entry_addr(image, entry));
1472 return -1;
1473 }
1474 if (!fp)
1475 fp = stdout;
1476
Patrick Georgic82725c2015-08-26 12:13:03 +02001477 unsigned int decompressed_size = 0;
Patrick Georgia71c83f2015-08-26 12:23:26 +02001478 unsigned int compression = cbfs_file_get_compression_info(entry,
1479 &decompressed_size);
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001480 const char *compression_name = lookup_name_by_type(
1481 types_cbfs_compression, compression, "????");
Patrick Georgic82725c2015-08-26 12:13:03 +02001482
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001483 if (compression == CBFS_COMPRESS_NONE)
1484 fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s\n",
Patrick Georgic82725c2015-08-26 12:13:03 +02001485 *name ? name : "(empty)",
1486 cbfs_get_entry_addr(image, entry),
1487 get_cbfs_entry_type_name(ntohl(entry->type)),
1488 ntohl(entry->len),
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001489 compression_name
Patrick Georgic82725c2015-08-26 12:13:03 +02001490 );
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001491 else
1492 fprintf(fp, "%-30s 0x%-8x %-12s %8d %-4s (%d decompressed)\n",
1493 *name ? name : "(empty)",
1494 cbfs_get_entry_addr(image, entry),
1495 get_cbfs_entry_type_name(ntohl(entry->type)),
1496 ntohl(entry->len),
1497 compression_name,
1498 decompressed_size
1499 );
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001500
Patrick Georgi89f20342015-10-01 15:54:04 +02001501 struct cbfs_file_attr_hash *hash = NULL;
1502 while ((hash = cbfs_file_get_next_hash(entry, hash)) != NULL) {
1503 unsigned int hash_type = ntohl(hash->hash_type);
Furquan Shaikh1d56eef2016-12-02 09:24:50 -08001504 if (hash_type >= CBFS_NUM_SUPPORTED_HASHES) {
Patrick Georgi89f20342015-10-01 15:54:04 +02001505 fprintf(fp, "invalid hash type %d\n", hash_type);
1506 break;
1507 }
1508 size_t hash_len = widths_cbfs_hash[hash_type];
1509 char *hash_str = bintohex(hash->hash_data, hash_len);
1510 uint8_t local_hash[hash_len];
1511 if (vb2_digest_buffer(CBFS_SUBHEADER(entry),
1512 ntohl(entry->len), hash_type, local_hash,
1513 hash_len) != VB2_SUCCESS) {
1514 fprintf(fp, "failed to hash '%s'\n", name);
Patrick Georgi862df922016-12-14 16:10:00 +01001515 free(hash_str);
Patrick Georgi89f20342015-10-01 15:54:04 +02001516 break;
1517 }
1518 int valid = memcmp(local_hash, hash->hash_data, hash_len) == 0;
1519 const char *valid_str = valid ? "valid" : "invalid";
1520
1521 fprintf(fp, " hash %s:%s %s\n",
1522 get_hash_attr_name(hash_type),
1523 hash_str, valid_str);
1524 free(hash_str);
1525 }
1526
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001527 if (!verbose)
1528 return 0;
1529
1530 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
1531 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
1532 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
1533 ntohl(entry->len));
1534
1535 /* note the components of the subheader may be in host order ... */
1536 switch (ntohl(entry->type)) {
1537 case CBFS_COMPONENT_STAGE:
1538 cbfs_print_stage_info((struct cbfs_stage *)
1539 CBFS_SUBHEADER(entry), fp);
1540 break;
1541
Patrick Rudolph4f5bed52018-05-02 09:44:08 +02001542 case CBFS_COMPONENT_SELF:
Paul Menzel831bbe82015-08-08 20:20:57 +02001543 payload = (struct cbfs_payload_segment *)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001544 CBFS_SUBHEADER(entry);
1545 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +08001546 struct cbfs_payload_segment seg;
1547 cbfs_decode_payload_segment(&seg, payload);
1548 cbfs_print_decoded_payload_segment_info(
1549 &seg, fp);
1550 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001551 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +08001552 else
Aaron Durbinca630272014-08-05 10:48:20 -05001553 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001554 }
1555 break;
1556 default:
1557 break;
1558 }
1559 return 0;
1560}
1561
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001562static int cbfs_print_parseable_entry_info(struct cbfs_image *image,
1563 struct cbfs_file *entry, void *arg)
1564{
1565 FILE *fp = (FILE *)arg;
1566 const char *name;
1567 const char *type;
1568 size_t offset;
1569 size_t metadata_size;
1570 size_t data_size;
1571 const char *sep = "\t";
1572
1573 if (!cbfs_is_valid_entry(image, entry)) {
1574 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1575 cbfs_get_entry_addr(image, entry));
1576 return -1;
1577 }
1578
1579 name = entry->filename;
1580 if (*name == '\0')
1581 name = "(empty)";
1582 type = get_cbfs_entry_type_name(ntohl(entry->type)),
1583 metadata_size = ntohl(entry->offset);
1584 data_size = ntohl(entry->len);
1585 offset = cbfs_get_entry_addr(image, entry);
1586
1587 fprintf(fp, "%s%s", name, sep);
1588 fprintf(fp, "0x%zx%s", offset, sep);
1589 fprintf(fp, "%s%s", type, sep);
1590 fprintf(fp, "0x%zx%s", metadata_size, sep);
1591 fprintf(fp, "0x%zx%s", data_size, sep);
1592 fprintf(fp, "0x%zx\n", metadata_size + data_size);
1593
1594 return 0;
1595}
1596
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001597int cbfs_print_directory(struct cbfs_image *image)
1598{
Sol Boucher67a0a862015-03-18 12:36:27 -07001599 if (cbfs_is_legacy_cbfs(image))
1600 cbfs_print_header_info(image);
Daisuke Nojiri0c2f0c12017-10-26 17:40:41 -07001601 printf("%-30s %-10s %-12s Size Comp\n", "Name", "Offset", "Type");
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001602 cbfs_walk(image, cbfs_print_entry_info, NULL);
1603 return 0;
1604}
1605
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001606int cbfs_print_parseable_directory(struct cbfs_image *image)
1607{
Furquan Shaikh161d2332016-05-26 14:41:02 -07001608 size_t i;
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001609 const char *header[] = {
1610 "Name",
1611 "Offset",
1612 "Type",
1613 "Metadata Size",
1614 "Data Size",
1615 "Total Size",
1616 };
1617 const char *sep = "\t";
1618
1619 for (i = 0; i < ARRAY_SIZE(header) - 1; i++)
1620 fprintf(stdout, "%s%s", header[i], sep);
1621 fprintf(stdout, "%s\n", header[i]);
1622 cbfs_walk(image, cbfs_print_parseable_entry_info, stdout);
1623 return 0;
1624}
1625
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001626int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -08001627 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001628{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001629 struct cbfs_file *next;
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001630 uint32_t next_addr = 0;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001631
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001632 /* We don't return here even if this entry is already empty because we
1633 want to merge the empty entries following after it. */
1634
1635 /* Loop until non-empty entry is found, starting from the current entry.
1636 After the loop, next_addr points to the next non-empty entry. */
1637 next = entry;
1638 while (ntohl(next->type) == CBFS_COMPONENT_DELETED ||
1639 ntohl(next->type) == CBFS_COMPONENT_NULL) {
1640 next = cbfs_find_next_entry(image, next);
1641 if (!next)
1642 break;
1643 next_addr = cbfs_get_entry_addr(image, next);
1644 if (!cbfs_is_valid_entry(image, next))
1645 /* 'next' could be the end of cbfs */
1646 break;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001647 }
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001648
1649 if (!next_addr)
1650 /* Nothing to empty */
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001651 return 0;
1652
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001653 /* We can return here if we find only a single empty entry.
1654 For simplicity, we just proceed (and make it empty again). */
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001655
Daisuke Nojiri2b59c612018-10-02 12:59:58 -07001656 /* We're creating one empty entry for combined empty spaces */
1657 uint32_t addr = cbfs_get_entry_addr(image, entry);
1658 size_t len = next_addr - addr - cbfs_calculate_file_header_size("");
1659 DEBUG("join_empty_entry: [0x%x, 0x%x) len=%zu\n", addr, next_addr, len);
1660 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001661
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001662 return 0;
1663}
1664
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001665int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001666 void *arg)
1667{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001668 int count = 0;
1669 struct cbfs_file *entry;
1670 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001671 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001672 entry = cbfs_find_next_entry(image, entry)) {
1673 count ++;
1674 if (callback(image, entry, arg) != 0)
1675 break;
1676 }
1677 return count;
1678}
1679
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001680static int cbfs_header_valid(struct cbfs_header *header)
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001681{
1682 if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
1683 ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
1684 (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001685 (ntohl(header->offset) < ntohl(header->romsize)))
1686 return 1;
1687 return 0;
1688}
1689
1690struct cbfs_header *cbfs_find_header(char *data, size_t size,
1691 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001692{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001693 size_t offset;
1694 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001695 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001696 struct cbfs_header *header, *result = NULL;
1697
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001698 if (forced_offset < (size - sizeof(struct cbfs_header))) {
1699 /* Check if the forced header is valid. */
1700 header = (struct cbfs_header *)(data + forced_offset);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001701 if (cbfs_header_valid(header))
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001702 return header;
1703 return NULL;
1704 }
1705
Julius Wernerefcee762014-11-10 13:14:24 -08001706 // Try finding relative offset of master header at end of file first.
1707 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
1708 offset = size + rel_offset;
1709 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
1710 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001711
Hung-Te Lineab2c812013-01-29 01:56:17 +08001712 if (offset >= size - sizeof(*header) ||
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001713 !cbfs_header_valid((struct cbfs_header *)(data + offset))) {
Julius Wernerefcee762014-11-10 13:14:24 -08001714 // Some use cases append non-CBFS data to the end of the ROM.
1715 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +08001716 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001717 }
Hung-Te Lineab2c812013-01-29 01:56:17 +08001718
1719 for (; offset + sizeof(*header) < size; offset++) {
1720 header = (struct cbfs_header *)(data + offset);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001721 if (!cbfs_header_valid(header))
Hung-Te Lineab2c812013-01-29 01:56:17 +08001722 continue;
Julius Wernerefcee762014-11-10 13:14:24 -08001723 if (!found++)
1724 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001725 }
Julius Wernerefcee762014-11-10 13:14:24 -08001726 if (found > 1)
1727 // Top-aligned images usually have a working relative offset
1728 // field, so this is more likely to happen on bottom-aligned
1729 // ones (where the first header is the "outermost" one)
1730 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +08001731 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001732 return result;
1733}
1734
1735
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001736struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
1737{
Sol Boucher3e060ed2015-05-05 15:40:15 -07001738 assert(image);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001739 if (image->has_header)
1740 /* header.offset is relative to start of flash, not
1741 * start of region, so use it with the full image.
1742 */
1743 return (struct cbfs_file *)
1744 (buffer_get_original_backing(&image->buffer) +
1745 image->header.offset);
1746 else
1747 return (struct cbfs_file *)buffer_get(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001748}
1749
1750struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001751 struct cbfs_file *entry)
1752{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001753 uint32_t addr = cbfs_get_entry_addr(image, entry);
Sol Boucher67a0a862015-03-18 12:36:27 -07001754 int align = image->has_header ? image->header.align :
1755 CBFS_ENTRY_ALIGNMENT;
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001756 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001757 addr += ntohl(entry->offset) + ntohl(entry->len);
1758 addr = align_up(addr, align);
1759 return (struct cbfs_file *)(image->buffer.data + addr);
1760}
1761
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001762uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
1763{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001764 assert(image && image->buffer.data && entry);
1765 return (int32_t)((char *)entry - image->buffer.data);
1766}
1767
Sol Boucher67a0a862015-03-18 12:36:27 -07001768int cbfs_is_valid_cbfs(struct cbfs_image *image)
1769{
1770 return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC,
1771 strlen(CBFS_FILE_MAGIC));
1772}
1773
1774int cbfs_is_legacy_cbfs(struct cbfs_image *image)
1775{
1776 return image->has_header;
1777}
1778
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001779int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
1780{
Sol Bouchere3260a02015-03-25 13:40:08 -07001781 uint32_t offset = cbfs_get_entry_addr(image, entry);
1782
1783 if (offset >= image->buffer.size)
1784 return 0;
1785
1786 struct buffer entry_data;
1787 buffer_clone(&entry_data, &image->buffer);
1788 buffer_seek(&entry_data, offset);
1789 return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
Sol Boucher67a0a862015-03-18 12:36:27 -07001790 strlen(CBFS_FILE_MAGIC));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001791}
1792
Patrick Georgi57edf162015-08-12 09:20:11 +02001793struct cbfs_file *cbfs_create_file_header(int type,
Vadim Bendebury45e59972014-12-23 15:59:57 -08001794 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001795{
Patrick Georgi2c615062015-07-15 20:49:00 +02001796 struct cbfs_file *entry = malloc(MAX_CBFS_FILE_HEADER_BUFFER);
1797 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, MAX_CBFS_FILE_HEADER_BUFFER);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001798 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
Patrick Georgiedf25d92015-08-12 09:12:06 +02001799 entry->type = htonl(type);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001800 entry->len = htonl(len);
Patrick Georgi0d618af2015-07-15 18:28:23 +02001801 entry->attributes_offset = 0;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001802 entry->offset = htonl(cbfs_calculate_file_header_size(name));
Patrick Georgic569b8b2015-07-15 16:42:38 +02001803 memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry));
1804 strcpy(entry->filename, name);
Patrick Georgi57edf162015-08-12 09:20:11 +02001805 return entry;
1806}
1807
1808int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
1809 size_t len, const char *name)
1810{
1811 struct cbfs_file *tmp = cbfs_create_file_header(type, len, name);
1812 memcpy(entry, tmp, ntohl(tmp->offset));
1813 free(tmp);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001814 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1815 return 0;
1816}
1817
Patrick Georgi2c615062015-07-15 20:49:00 +02001818struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file)
1819{
1820 /* attributes_offset should be 0 when there is no attribute, but all
1821 * values that point into the cbfs_file header are invalid, too. */
1822 if (ntohl(file->attributes_offset) <= sizeof(*file))
1823 return NULL;
1824
1825 /* There needs to be enough space for the file header and one
1826 * attribute header for this to make sense. */
1827 if (ntohl(file->offset) <=
1828 sizeof(*file) + sizeof(struct cbfs_file_attribute))
1829 return NULL;
1830
1831 return (struct cbfs_file_attribute *)
1832 (((uint8_t *)file) + ntohl(file->attributes_offset));
1833}
1834
1835struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
1836 struct cbfs_file_attribute *attr)
1837{
1838 /* ex falso sequitur quodlibet */
1839 if (attr == NULL)
1840 return NULL;
1841
1842 /* Is there enough space for another attribute? */
1843 if ((uint8_t *)attr + ntohl(attr->len) +
Patrick Rudolphe28fa402019-02-14 11:19:07 +01001844 sizeof(struct cbfs_file_attribute) >
Patrick Georgi2c615062015-07-15 20:49:00 +02001845 (uint8_t *)file + ntohl(file->offset))
1846 return NULL;
1847
1848 struct cbfs_file_attribute *next = (struct cbfs_file_attribute *)
1849 (((uint8_t *)attr) + ntohl(attr->len));
1850 /* If any, "unused" attributes must come last. */
1851 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED)
1852 return NULL;
1853 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
1854 return NULL;
1855
1856 return next;
1857}
1858
1859struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
1860 uint32_t tag,
1861 uint32_t size)
1862{
1863 struct cbfs_file_attribute *attr, *next;
1864 next = cbfs_file_first_attr(header);
1865 do {
1866 attr = next;
1867 next = cbfs_file_next_attr(header, attr);
1868 } while (next != NULL);
1869 uint32_t header_size = ntohl(header->offset) + size;
1870 if (header_size > MAX_CBFS_FILE_HEADER_BUFFER) {
1871 DEBUG("exceeding allocated space for cbfs_file headers");
1872 return NULL;
1873 }
1874 /* attr points to the last valid attribute now.
1875 * If NULL, we have to create the first one. */
1876 if (attr == NULL) {
1877 /* New attributes start where the header ends.
Elyes HAOUAS3db01982018-08-23 18:08:20 +02001878 * header->offset is later set to accommodate the
Patrick Georgi2c615062015-07-15 20:49:00 +02001879 * additional structure.
Elyes HAOUAS3db01982018-08-23 18:08:20 +02001880 * No endianness translation necessary here, because both
Patrick Georgi2c615062015-07-15 20:49:00 +02001881 * fields are encoded the same way. */
1882 header->attributes_offset = header->offset;
1883 attr = (struct cbfs_file_attribute *)
1884 (((uint8_t *)header) +
1885 ntohl(header->attributes_offset));
1886 } else {
1887 attr = (struct cbfs_file_attribute *)
1888 (((uint8_t *)attr) +
1889 ntohl(attr->len));
1890 }
1891 header->offset = htonl(header_size);
1892 memset(attr, CBFS_CONTENT_DEFAULT_VALUE, size);
1893 attr->tag = htonl(tag);
1894 attr->len = htonl(size);
1895 return attr;
1896}
1897
Patrick Georgi89f20342015-10-01 15:54:04 +02001898int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer,
1899 enum vb2_hash_algorithm hash_type)
1900{
zbao37450ff2015-11-05 14:35:57 +08001901 uint32_t hash_index = hash_type;
1902
1903 if (hash_index >= CBFS_NUM_SUPPORTED_HASHES)
Patrick Georgi89f20342015-10-01 15:54:04 +02001904 return -1;
1905
1906 unsigned hash_size = widths_cbfs_hash[hash_type];
1907 if (hash_size == 0)
1908 return -1;
1909
1910 struct cbfs_file_attr_hash *attrs =
1911 (struct cbfs_file_attr_hash *)cbfs_add_file_attr(header,
1912 CBFS_FILE_ATTR_TAG_HASH,
1913 sizeof(struct cbfs_file_attr_hash) + hash_size);
1914
1915 if (attrs == NULL)
1916 return -1;
1917
1918 attrs->hash_type = htonl(hash_type);
1919 if (vb2_digest_buffer(buffer_get(buffer), buffer_size(buffer),
1920 hash_type, attrs->hash_data, hash_size) != VB2_SUCCESS)
1921 return -1;
1922
1923 return 0;
1924}
1925
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001926/* Finds a place to hold whole data in same memory page. */
1927static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1928{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001929 if (!page)
1930 return 1;
1931 return (start / page) == (start + size - 1) / page;
1932}
1933
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001934/* Tests if data can fit in a range by given offset:
Aaron Durbind7339412015-09-15 12:50:14 -05001935 * start ->| metadata_size | offset (+ size) |<- end
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001936 */
Aaron Durbind7339412015-09-15 12:50:14 -05001937static int is_in_range(size_t start, size_t end, size_t metadata_size,
1938 size_t offset, size_t size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001939{
Aaron Durbind7339412015-09-15 12:50:14 -05001940 return (offset >= start + metadata_size && offset + size <= end);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001941}
1942
Werner Zeh95bfcae2016-01-25 12:47:20 +01001943static size_t absolute_align(const struct cbfs_image *image, size_t val,
1944 size_t align)
1945{
1946 const size_t region_offset = buffer_offset(&image->buffer);
1947 /* To perform alignment on absolute address, take the region offset */
1948 /* of the image into account. */
1949 return align_up(val + region_offset, align) - region_offset;
1950
1951}
1952
Aaron Durbind7339412015-09-15 12:50:14 -05001953int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size,
1954 size_t page_size, size_t align, size_t metadata_size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001955{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001956 struct cbfs_file *entry;
1957 size_t need_len;
Aaron Durbind7339412015-09-15 12:50:14 -05001958 size_t addr, addr_next, addr2, addr3, offset;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001959
1960 /* Default values: allow fitting anywhere in ROM. */
1961 if (!page_size)
Sol Boucher67a0a862015-03-18 12:36:27 -07001962 page_size = image->has_header ? image->header.romsize :
1963 image->buffer.size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001964 if (!align)
1965 align = 1;
1966
1967 if (size > page_size)
Aaron Durbind7339412015-09-15 12:50:14 -05001968 ERROR("Input file size (%zd) greater than page size (%zd).\n",
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001969 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001970
Aaron Durbind7339412015-09-15 12:50:14 -05001971 size_t image_align = image->has_header ? image->header.align :
Sol Boucher67a0a862015-03-18 12:36:27 -07001972 CBFS_ENTRY_ALIGNMENT;
1973 if (page_size % image_align)
Aaron Durbind7339412015-09-15 12:50:14 -05001974 WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001975 __func__, page_size, image_align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001976
Aaron Durbind7339412015-09-15 12:50:14 -05001977 need_len = metadata_size + size;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001978
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001979 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001980 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
1981
1982 /* Three cases of content location on memory page:
1983 * case 1.
1984 * | PAGE 1 | PAGE 2 |
1985 * | <header><content>| Fit. Return start of content.
1986 *
1987 * case 2.
1988 * | PAGE 1 | PAGE 2 |
1989 * | <header><content> | Fits when we shift content to align
1990 * shift-> | <header>|<content> | at starting of PAGE 2.
1991 *
1992 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001993 * | PAGE 1 | PAGE 2 | PAGE 3 |
1994 * | <header>< content > | Can't fit. If we shift content to
1995 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1996 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001997 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001998 * The returned address can be then used as "base-address" (-b) in add-*
1999 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
2000 * For stage targets, the address is also used to re-link stage before
2001 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002002 */
2003 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08002004 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002005 entry = cbfs_find_next_entry(image, entry)) {
2006
2007 uint32_t type = ntohl(entry->type);
2008 if (type != CBFS_COMPONENT_NULL)
2009 continue;
2010
2011 addr = cbfs_get_entry_addr(image, entry);
2012 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
2013 image, entry));
2014 if (addr_next - addr < need_len)
2015 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08002016
Werner Zeh95bfcae2016-01-25 12:47:20 +01002017 offset = absolute_align(image, addr + metadata_size, align);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08002018 if (is_in_same_page(offset, size, page_size) &&
Aaron Durbind7339412015-09-15 12:50:14 -05002019 is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002020 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08002021 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002022 }
2023
2024 addr2 = align_up(addr, page_size);
Werner Zeh95bfcae2016-01-25 12:47:20 +01002025 offset = absolute_align(image, addr2, align);
Aaron Durbind7339412015-09-15 12:50:14 -05002026 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002027 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08002028 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002029 }
2030
Aaron Durbind7339412015-09-15 12:50:14 -05002031 /* Assume page_size >= metadata_size so adding one page will
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08002032 * definitely provide the space for header. */
Aaron Durbind7339412015-09-15 12:50:14 -05002033 assert(page_size >= metadata_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002034 addr3 = addr2 + page_size;
Werner Zeh95bfcae2016-01-25 12:47:20 +01002035 offset = absolute_align(image, addr3, align);
Aaron Durbind7339412015-09-15 12:50:14 -05002036 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002037 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08002038 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08002039 }
2040 }
2041 return -1;
2042}