blob: e530224fac4d932b735d6c76b4511e3d25d36197 [file] [log] [blame]
Hung-Te Lineab2c812013-01-29 01:56:17 +08001/*
2 * CBFS Image Manipulation
3 *
4 * Copyright (C) 2013 The Chromium OS Authors. All rights reserved.
Werner Zeh95bfcae2016-01-25 12:47:20 +01005 * Copyright (C) 2016 Siemens AG. All rights reserved.
Hung-Te Lineab2c812013-01-29 01:56:17 +08006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Hung-Te Lineab2c812013-01-29 01:56:17 +080015 */
16
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080017#include <inttypes.h>
18#include <libgen.h>
Patrick Georgicccc9d42015-04-28 13:09:36 +020019#include <stddef.h>
Hung-Te Lineab2c812013-01-29 01:56:17 +080020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Sol Boucher0e539312015-03-05 15:38:03 -080023#include <strings.h>
Antonello Dettorifda691e2016-06-09 12:35:36 +020024#include <commonlib/endian.h>
Hung-Te Lineab2c812013-01-29 01:56:17 +080025
26#include "common.h"
27#include "cbfs_image.h"
Aaron Durbin5a1e85c2015-10-27 21:02:30 -050028#include "elfparsing.h"
Aaron Durbin694fd132015-10-28 11:39:34 -050029#include "rmodule.h"
Hung-Te Lineab2c812013-01-29 01:56:17 +080030
Sol Boucher636cc852015-04-03 09:13:04 -070031/* Even though the file-adding functions---cbfs_add_entry() and
32 * cbfs_add_entry_at()---perform their sizing checks against the beginning of
33 * the subsequent section rather than a stable recorded value such as an empty
34 * file header's len field, it's possible to prove two interesting properties
35 * about their behavior:
36 * - Placing a new file within an empty entry located below an existing file
37 * entry will never leave an aligned flash address containing neither the
38 * beginning of a file header nor part of a file.
39 * - Placing a new file in an empty entry at the very end of the image such
40 * that it fits, but leaves no room for a final header, is guaranteed not to
41 * change the total amount of space for entries, even if that new file is
42 * later removed from the CBFS.
43 * These properties are somewhat nonobvious from the implementation, so the
44 * reader is encouraged to blame this comment and examine the full proofs
45 * in the commit message before making significant changes that would risk
46 * removing said guarantees.
47 */
48
Hung-Te Lineab2c812013-01-29 01:56:17 +080049/* The file name align is not defined in CBFS spec -- only a preference by
50 * (old) cbfstool. */
51#define CBFS_FILENAME_ALIGN (16)
52
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080053static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070054 const char *default_value)
55{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080056 int i;
57 for (i = 0; desc[i].name; i++)
58 if (desc[i].type == type)
59 return desc[i].name;
60 return default_value;
61}
62
Sol Boucherec424862015-05-07 21:00:05 -070063static int lookup_type_by_name(const struct typedesc_t *desc, const char *name)
64{
65 int i;
66 for (i = 0; desc[i].name && strcasecmp(name, desc[i].name); ++i);
67 return desc[i].name ? (int)desc[i].type : -1;
68}
69
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010070static const char *get_cbfs_entry_type_name(uint32_t type)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070071{
Patrick Georgidc37dab2015-09-09 16:46:00 +020072 return lookup_name_by_type(filetypes, type, "(unknown)");
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080073}
74
Sol Boucherec424862015-05-07 21:00:05 -070075int cbfs_parse_comp_algo(const char *name)
76{
77 return lookup_type_by_name(types_cbfs_compression, name);
78}
79
Patrick Georgi89f20342015-10-01 15:54:04 +020080static const char *get_hash_attr_name(uint16_t hash_type)
81{
82 return lookup_name_by_type(types_cbfs_hash, hash_type, "(invalid)");
83}
84
85int cbfs_parse_hash_algo(const char *name)
86{
87 return lookup_type_by_name(types_cbfs_hash, name);
88}
89
Hung-Te Linc03d9b02013-01-29 02:38:40 +080090/* CBFS image */
91
Patrick Georgi11ee08f2015-08-11 15:10:02 +020092size_t cbfs_calculate_file_header_size(const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070093{
Hung-Te Linc03d9b02013-01-29 02:38:40 +080094 return (sizeof(struct cbfs_file) +
95 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
96}
97
Sol Boucher67a0a862015-03-18 12:36:27 -070098/* Only call on legacy CBFSes possessing a master header. */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -060099static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700100{
Sol Boucher67a0a862015-03-18 12:36:27 -0700101 assert(image);
102 assert(cbfs_is_legacy_cbfs(image));
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800103 // A bug in old cbfstool may produce extra few bytes (by alignment) and
104 // cause cbfstool to overwrite things after free space -- which is
105 // usually CBFS header on x86. We need to workaround that.
Patrick Georgi343ea082016-02-10 18:07:52 +0100106 // Except when we run across a file that contains the actual header,
107 // in which case this image is a safe, new-style
108 // `cbfstool add-master-header` based image.
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800109
110 struct cbfs_file *entry, *first = NULL, *last = NULL;
111 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800112 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800113 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgi343ea082016-02-10 18:07:52 +0100114 /* Is the header guarded by a CBFS file entry? Then exit */
115 if (((char *)entry) + ntohl(entry->offset) == hdr_loc) {
116 return 0;
117 }
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800118 last = entry;
119 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600120 if ((char *)first < (char *)hdr_loc &&
121 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800122 WARN("CBFS image was created with old cbfstool with size bug. "
123 "Fixing size in last entry...\n");
Sol Boucher3e060ed2015-05-05 15:40:15 -0700124 last->len = htonl(ntohl(last->len) - image->header.align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800125 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
126 cbfs_get_entry_addr(image, entry),
127 cbfs_get_entry_addr(image,
128 cbfs_find_next_entry(image, last)));
129 }
130 return 0;
131}
132
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800133void cbfs_put_header(void *dest, const struct cbfs_header *header)
134{
135 struct buffer outheader;
136
137 outheader.data = dest;
138 outheader.size = 0;
139
140 xdr_be.put32(&outheader, header->magic);
141 xdr_be.put32(&outheader, header->version);
142 xdr_be.put32(&outheader, header->romsize);
143 xdr_be.put32(&outheader, header->bootblocksize);
144 xdr_be.put32(&outheader, header->align);
145 xdr_be.put32(&outheader, header->offset);
146 xdr_be.put32(&outheader, header->architecture);
147}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600148
Hung-Te Lin0780d672014-05-16 10:14:05 +0800149static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
150 struct cbfs_payload_segment *input)
151{
152 struct buffer seg = {
153 .data = (void *)input,
154 .size = sizeof(*input),
155 };
156 output->type = xdr_be.get32(&seg);
157 output->compression = xdr_be.get32(&seg);
158 output->offset = xdr_be.get32(&seg);
159 output->load_addr = xdr_be.get64(&seg);
160 output->len = xdr_be.get32(&seg);
161 output->mem_len = xdr_be.get32(&seg);
162 assert(seg.size == 0);
163}
164
Patrick Georgia71c83f2015-08-26 12:23:26 +0200165static int cbfs_file_get_compression_info(struct cbfs_file *entry,
166 uint32_t *decompressed_size)
167{
168 unsigned int compression = CBFS_COMPRESS_NONE;
Patrick Georgia2ce7102016-12-14 16:08:52 +0100169 if (decompressed_size)
170 *decompressed_size = ntohl(entry->len);
Patrick Georgia71c83f2015-08-26 12:23:26 +0200171 for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry);
172 attr != NULL;
173 attr = cbfs_file_next_attr(entry, attr)) {
174 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) {
175 struct cbfs_file_attr_compression *ac =
176 (struct cbfs_file_attr_compression *)attr;
177 compression = ntohl(ac->compression);
178 if (decompressed_size)
179 *decompressed_size =
180 ntohl(ac->decompressed_size);
181 }
182 }
183 return compression;
184}
185
Patrick Georgi89f20342015-10-01 15:54:04 +0200186static struct cbfs_file_attr_hash *cbfs_file_get_next_hash(
187 struct cbfs_file *entry, struct cbfs_file_attr_hash *cur)
188{
189 struct cbfs_file_attribute *attr = (struct cbfs_file_attribute *)cur;
190 if (attr == NULL) {
191 attr = cbfs_file_first_attr(entry);
192 if (attr == NULL)
193 return NULL;
194 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
195 return (struct cbfs_file_attr_hash *)attr;
196 }
197 while ((attr = cbfs_file_next_attr(entry, attr)) != NULL) {
198 if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH)
199 return (struct cbfs_file_attr_hash *)attr;
200 };
201 return NULL;
202}
203
Sol Boucher0e539312015-03-05 15:38:03 -0800204void cbfs_get_header(struct cbfs_header *header, void *src)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600205{
206 struct buffer outheader;
207
Sol Boucher0e539312015-03-05 15:38:03 -0800208 outheader.data = src; /* We're not modifying the data */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600209 outheader.size = 0;
210
211 header->magic = xdr_be.get32(&outheader);
212 header->version = xdr_be.get32(&outheader);
213 header->romsize = xdr_be.get32(&outheader);
214 header->bootblocksize = xdr_be.get32(&outheader);
215 header->align = xdr_be.get32(&outheader);
216 header->offset = xdr_be.get32(&outheader);
217 header->architecture = xdr_be.get32(&outheader);
218}
219
Sol Boucher67a0a862015-03-18 12:36:27 -0700220int cbfs_image_create(struct cbfs_image *image, size_t entries_size)
221{
222 assert(image);
223 assert(image->buffer.data);
224
225 size_t empty_header_len = cbfs_calculate_file_header_size("");
226 uint32_t entries_offset = 0;
227 uint32_t align = CBFS_ENTRY_ALIGNMENT;
228 if (image->has_header) {
229 entries_offset = image->header.offset;
230
231 if (entries_offset > image->buffer.size) {
232 ERROR("CBFS file entries are located outside CBFS itself\n");
233 return -1;
234 }
235
236 align = image->header.align;
237 }
238
239 // This attribute must be given in order to prove that this module
240 // correctly preserves certain CBFS properties. See the block comment
241 // near the top of this file (and the associated commit message).
242 if (align < empty_header_len) {
243 ERROR("CBFS must be aligned to at least %zu bytes\n",
244 empty_header_len);
245 return -1;
246 }
247
248 if (entries_size > image->buffer.size - entries_offset) {
249 ERROR("CBFS doesn't have enough space to fit its file entries\n");
250 return -1;
251 }
252
253 if (empty_header_len > entries_size) {
254 ERROR("CBFS is too small to fit any header\n");
255 return -1;
256 }
257 struct cbfs_file *entry_header =
258 (struct cbfs_file *)(image->buffer.data + entries_offset);
259 // This alignment is necessary in order to prove that this module
260 // correctly preserves certain CBFS properties. See the block comment
261 // near the top of this file (and the associated commit message).
262 entries_size -= entries_size % align;
263
264 size_t capacity = entries_size - empty_header_len;
265 LOG("Created CBFS (capacity = %zu bytes)\n", capacity);
Patrick Georgiedf25d92015-08-12 09:12:06 +0200266 return cbfs_create_empty_entry(entry_header, CBFS_COMPONENT_NULL,
267 capacity, "");
Sol Boucher67a0a862015-03-18 12:36:27 -0700268}
269
270int cbfs_legacy_image_create(struct cbfs_image *image,
271 uint32_t architecture,
272 uint32_t align,
273 struct buffer *bootblock,
274 uint32_t bootblock_offset,
275 uint32_t header_offset,
276 uint32_t entries_offset)
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800277{
Sol Bouchere3260a02015-03-25 13:40:08 -0700278 assert(image);
279 assert(image->buffer.data);
280 assert(bootblock);
281
Julius Wernerefcee762014-11-10 13:14:24 -0800282 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800283 uint32_t cbfs_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600284 void *header_loc;
Sol Bouchere3260a02015-03-25 13:40:08 -0700285 size_t size = image->buffer.size;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800286
287 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
288 "header=0x%x+0x%zx, entries_offset=0x%x\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700289 bootblock_offset, bootblock->size, header_offset,
290 sizeof(image->header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800291
Sol Boucher67a0a862015-03-18 12:36:27 -0700292 // Adjust legacy top-aligned address to ROM offset.
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800293 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800294 entries_offset = size + (int32_t)entries_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800295 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800296 bootblock_offset = size + (int32_t)bootblock_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800297 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800298 header_offset = size + (int32_t)header_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800299
300 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
301 "header=0x%x, entries_offset=0x%x\n",
302 bootblock_offset, header_offset, entries_offset);
303
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800304 // Prepare bootblock
305 if (bootblock_offset + bootblock->size > size) {
306 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
307 bootblock_offset, bootblock->size, size);
308 return -1;
309 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800310 if (entries_offset > bootblock_offset &&
311 entries_offset < bootblock->size) {
312 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
313 bootblock_offset, bootblock->size, entries_offset);
314 return -1;
315 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800316 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
317 bootblock->size);
318
319 // Prepare header
Sol Boucher5bad3952015-05-05 20:35:26 -0700320 if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800321 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700322 header_offset, sizeof(image->header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800323 return -1;
324 }
Sol Boucher3e060ed2015-05-05 15:40:15 -0700325 image->header.magic = CBFS_HEADER_MAGIC;
326 image->header.version = CBFS_HEADER_VERSION;
327 image->header.romsize = size;
328 image->header.bootblocksize = bootblock->size;
329 image->header.align = align;
330 image->header.offset = entries_offset;
331 image->header.architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600332
333 header_loc = (image->buffer.data + header_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700334 cbfs_put_header(header_loc, &image->header);
Sol Boucher67a0a862015-03-18 12:36:27 -0700335 image->has_header = true;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800336
Julius Wernerefcee762014-11-10 13:14:24 -0800337 // The last 4 byte of the image contain the relative offset from the end
338 // of the image to the master header as a 32-bit signed integer. x86
339 // relies on this also being its (memory-mapped, top-aligned) absolute
340 // 32-bit address by virtue of how two's complement numbers work.
341 assert(size % sizeof(int32_t) == 0);
342 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
343 *rel_offset = header_offset - size;
344
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800345 // Prepare entries
346 if (align_up(entries_offset, align) != entries_offset) {
347 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
348 entries_offset, align);
349 return -1;
350 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800351 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800352 // e = min(bootblock, header, rel_offset) where e > entries_offset.
353 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800354 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
355 cbfs_len = bootblock_offset;
356 if (header_offset > entries_offset && header_offset < cbfs_len)
357 cbfs_len = header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700358
359 if (cbfs_image_create(image, cbfs_len - entries_offset))
360 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800361 return 0;
362}
363
Sol Bouchere3260a02015-03-25 13:40:08 -0700364int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
365 uint32_t offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700366{
Sol Bouchere3260a02015-03-25 13:40:08 -0700367 assert(out);
368 assert(in);
369 assert(in->data);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600370
Sol Bouchere3260a02015-03-25 13:40:08 -0700371 buffer_clone(&out->buffer, in);
Sol Boucher67a0a862015-03-18 12:36:27 -0700372 out->has_header = false;
373
Patrick Georgi2f953d32015-09-11 18:34:39 +0200374 if (cbfs_is_valid_cbfs(out)) {
375 return 0;
376 }
377
Sol Bouchere3260a02015-03-25 13:40:08 -0700378 void *header_loc = cbfs_find_header(in->data, in->size, offset);
379 if (header_loc) {
380 cbfs_get_header(&out->header, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700381 out->has_header = true;
Sol Bouchere3260a02015-03-25 13:40:08 -0700382 cbfs_fix_legacy_size(out, header_loc);
Patrick Georgi2f953d32015-09-11 18:34:39 +0200383 return 0;
Sol Boucher67a0a862015-03-18 12:36:27 -0700384 } else if (offset != ~0u) {
385 ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n");
386 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800387 }
Patrick Georgi2f953d32015-09-11 18:34:39 +0200388 ERROR("Selected image region is not a valid CBFS.\n");
389 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800390}
391
Patrick Georgi214e4af2015-11-20 19:22:50 +0100392int cbfs_copy_instance(struct cbfs_image *image, struct buffer *dst)
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800393{
Sol Boucher67a0a862015-03-18 12:36:27 -0700394 assert(image);
Sol Boucher67a0a862015-03-18 12:36:27 -0700395
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800396 struct cbfs_file *src_entry, *dst_entry;
Patrick Georgibd0bb232015-11-20 21:48:18 +0100397 size_t align;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800398 ssize_t last_entry_size;
399
Patrick Georgi214e4af2015-11-20 19:22:50 +0100400 size_t copy_end = buffer_size(dst);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800401
Patrick Georgibd0bb232015-11-20 21:48:18 +0100402 align = CBFS_ENTRY_ALIGNMENT;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800403
Patrick Georgibd0bb232015-11-20 21:48:18 +0100404 dst_entry = (struct cbfs_file *)buffer_get(dst);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800405
406 /* Copy non-empty files */
407 for (src_entry = cbfs_find_first_entry(image);
408 src_entry && cbfs_is_valid_entry(image, src_entry);
409 src_entry = cbfs_find_next_entry(image, src_entry)) {
410 size_t entry_size;
411
412 if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) ||
Patrick Georgibd0bb232015-11-20 21:48:18 +0100413 (src_entry->type == htonl(CBFS_COMPONENT_CBFSHEADER)) ||
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800414 (src_entry->type == htonl(CBFS_COMPONENT_DELETED)))
415 continue;
416
417 entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
418 memcpy(dst_entry, src_entry, entry_size);
419 dst_entry = (struct cbfs_file *)(
420 (uintptr_t)dst_entry + align_up(entry_size, align));
421
Patrick Georgi214e4af2015-11-20 19:22:50 +0100422 if ((size_t)((void *)dst_entry - buffer_get(dst)) >=
Sol Boucher0e539312015-03-05 15:38:03 -0800423 copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800424 ERROR("Ran out of room in copy region.\n");
425 return 1;
426 }
427 }
428
Patrick Georgibd0bb232015-11-20 21:48:18 +0100429 /* Last entry size is all the room above it, except for top 4 bytes
430 * which may be used by the master header pointer. This messes with
431 * the ability to stash something "top-aligned" into the region, but
432 * keeps things simpler. */
Patrick Georgi214e4af2015-11-20 19:22:50 +0100433 last_entry_size = copy_end - ((void *)dst_entry - buffer_get(dst))
Patrick Georgibd0bb232015-11-20 21:48:18 +0100434 - cbfs_calculate_file_header_size("") - sizeof(int32_t);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800435
436 if (last_entry_size < 0)
437 WARN("No room to create the last entry!\n")
438 else
Patrick Georgiedf25d92015-08-12 09:12:06 +0200439 cbfs_create_empty_entry(dst_entry, CBFS_COMPONENT_NULL,
440 last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800441
442 return 0;
443}
444
Aaron Durbin71c60ca2016-01-26 17:08:56 -0600445static size_t cbfs_file_entry_metadata_size(const struct cbfs_file *f)
446{
447 return ntohl(f->offset);
448}
449
450static size_t cbfs_file_entry_data_size(const struct cbfs_file *f)
451{
452 return ntohl(f->len);
453}
454
455static size_t cbfs_file_entry_size(const struct cbfs_file *f)
456{
457 return cbfs_file_entry_metadata_size(f) + cbfs_file_entry_data_size(f);
458}
459
460int cbfs_compact_instance(struct cbfs_image *image)
461{
462 assert(image);
463
464 struct cbfs_file *prev;
465 struct cbfs_file *cur;
466
467 /* The prev entry will always be an empty entry. */
468 prev = NULL;
469
470 /*
471 * Note: this function does not honor alignment or fixed location files.
472 * It's behavior is akin to cbfs_copy_instance() in that it expects
473 * the caller to understand the ramifications of compacting a
474 * fragmented CBFS image.
475 */
476
477 for (cur = cbfs_find_first_entry(image);
478 cur && cbfs_is_valid_entry(image, cur);
479 cur = cbfs_find_next_entry(image, cur)) {
480 size_t prev_size;
481 size_t cur_size;
482 size_t empty_metadata_size;
483 size_t spill_size;
484 uint32_t type = htonl(cur->type);
485
486 /* Current entry is empty. Kepp track of it. */
487 if ((type == htonl(CBFS_COMPONENT_NULL)) ||
488 (type == htonl(CBFS_COMPONENT_DELETED))) {
489 prev = cur;
490 continue;
491 }
492
493 /* Need to ensure the previous entry is an empty one. */
494 if (prev == NULL)
495 continue;
496
497 /* At this point prev is an empty entry. Put the non-empty
498 * file in prev's location. Then add a new emptry entry. This
499 * essentialy bubbles empty entries towards the end. */
500
501 prev_size = cbfs_file_entry_size(prev);
502 cur_size = cbfs_file_entry_size(cur);
503
504 /*
505 * Adjust the empty file size by the actual space occupied
506 * bewtween the beginning of the empty file and the non-empty
507 * file.
508 */
509 prev_size += (cbfs_get_entry_addr(image, cur) -
510 cbfs_get_entry_addr(image, prev)) - prev_size;
511
512 /* Move the non-empty file over the empty file. */
513 memmove(prev, cur, cur_size);
514
515 /*
516 * Get location of the empty file. Note that since prev was
517 * overwritten with the non-empty file the previously moved
518 * file needs to be used to calculate the empty file's location.
519 */
520 cur = cbfs_find_next_entry(image, prev);
521
522 /*
523 * The total space to work with for swapping the 2 entries
524 * consists of the 2 files' sizes combined. However, the
525 * cbfs_file entries start on CBFS_ALIGNMENT boundaries.
526 * Because of this the empty file size may end up smaller
527 * because of the non-empty file's metadata and data length.
528 *
529 * Calculate the spill size which is the amount of data lost
530 * due to the alignment constraints after moving the non-empty
531 * file.
532 */
533 spill_size = (cbfs_get_entry_addr(image, cur) -
534 cbfs_get_entry_addr(image, prev)) - cur_size;
535
536 empty_metadata_size = cbfs_calculate_file_header_size("");
537
538 /* Check if new empty size can contain the metadata. */
539 if (empty_metadata_size + spill_size > prev_size) {
540 ERROR("Unable to swap '%s' with prev empty entry.\n",
541 prev->filename);
542 return 1;
543 }
544
545 /* Update the empty file's size. */
546 prev_size -= spill_size + empty_metadata_size;
547
548 /* Create new empty file. */
549 cbfs_create_empty_entry(cur, CBFS_COMPONENT_NULL,
550 prev_size, "");
551
552 /* Merge any potential empty entries together. */
553 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
554
555 /*
556 * Since current switched to an empty file keep track of it.
557 * Even if any empty files were merged the empty entry still
558 * starts at previously calculated location.
559 */
560 prev = cur;
561 }
562
563 return 0;
564}
565
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700566int cbfs_image_delete(struct cbfs_image *image)
567{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100568 if (image == NULL)
569 return 0;
570
Hung-Te Lineab2c812013-01-29 01:56:17 +0800571 buffer_delete(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800572 return 0;
573}
574
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800575/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
576static int cbfs_add_entry_at(struct cbfs_image *image,
577 struct cbfs_file *entry,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800578 const void *data,
Patrick Georgi7fd14182015-08-11 15:55:16 +0200579 uint32_t content_offset,
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200580 const struct cbfs_file *header)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700581{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800582 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
583 uint32_t addr = cbfs_get_entry_addr(image, entry),
584 addr_next = cbfs_get_entry_addr(image, next);
Patrick Georgi7fd14182015-08-11 15:55:16 +0200585 uint32_t min_entry_size = cbfs_calculate_file_header_size("");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200586 uint32_t len, header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700587 uint32_t align = image->has_header ? image->header.align :
588 CBFS_ENTRY_ALIGNMENT;
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200589 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800590
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200591 header_offset = content_offset - header_size;
592 if (header_offset % align)
593 header_offset -= header_offset % align;
594 if (header_offset < addr) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800595 ERROR("No space to hold cbfs_file header.");
596 return -1;
597 }
598
599 // Process buffer BEFORE content_offset.
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200600 if (header_offset - addr > min_entry_size) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800601 DEBUG("|min|...|header|content|... <create new entry>\n");
Patrick Georgi4eb8abe2015-08-25 12:24:49 +0200602 len = header_offset - addr - min_entry_size;
Patrick Georgiedf25d92015-08-12 09:12:06 +0200603 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800604 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
605 entry = cbfs_find_next_entry(image, entry);
606 addr = cbfs_get_entry_addr(image, entry);
607 }
608
Patrick Georgi7a33b532015-08-25 13:00:04 +0200609 len = content_offset - addr - header_size;
Patrick Georgia60e7b62015-08-25 22:26:02 +0200610 memcpy(entry, header, header_size);
Patrick Georgi7a33b532015-08-25 13:00:04 +0200611 if (len != 0) {
612 /* the header moved backwards a bit to accomodate cbfs_file
613 * alignment requirements, so patch up ->offset to still point
614 * to file data.
615 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800616 DEBUG("|..|header|content|... <use offset to create entry>\n");
Patrick Georgiae7efb92015-08-25 13:11:28 +0200617 DEBUG("before: offset=0x%x\n", ntohl(entry->offset));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800618 // TODO reset expanded name buffer to 0xFF.
Patrick Georgi7a33b532015-08-25 13:00:04 +0200619 entry->offset = htonl(ntohl(entry->offset) + len);
Patrick Georgiae7efb92015-08-25 13:11:28 +0200620 DEBUG("after: offset=0x%x\n", ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800621 }
622
623 // Ready to fill data into entry.
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800624 DEBUG("content_offset: 0x%x, entry location: %x\n",
625 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
626 image->buffer.data));
627 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
Patrick Georgicccc9d42015-04-28 13:09:36 +0200628 (ptrdiff_t)content_offset);
Patrick Georgi19c80b22015-08-25 13:16:04 +0200629 memcpy(CBFS_SUBHEADER(entry), data, ntohl(entry->len));
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800630 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
631
632 // Process buffer AFTER entry.
633 entry = cbfs_find_next_entry(image, entry);
634 addr = cbfs_get_entry_addr(image, entry);
Sol Boucher05725652015-04-02 20:58:26 -0700635 if (addr == addr_next)
636 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800637
Sol Boucher05725652015-04-02 20:58:26 -0700638 assert(addr < addr_next);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800639 if (addr_next - addr < min_entry_size) {
Sol Boucher636cc852015-04-03 09:13:04 -0700640 DEBUG("No need for new \"empty\" entry\n");
641 /* No need to increase the size of the just
642 * stored file to extend to next file. Alignment
643 * of next file takes care of this.
644 */
645 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800646 }
647
648 len = addr_next - addr - min_entry_size;
Patrick Georgi29a04d92015-11-20 23:23:44 +0100649 /* keep space for master header pointer */
650 if ((void *)entry + min_entry_size + len > buffer_get(&image->buffer) +
651 buffer_size(&image->buffer) - sizeof(int32_t)) {
652 len -= sizeof(int32_t);
653 }
Patrick Georgiedf25d92015-08-12 09:12:06 +0200654 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800655 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
656 return 0;
657}
658
659int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Patrick Georgie5903582015-08-25 13:53:42 +0200660 uint32_t content_offset,
Patrick Georgif5252f32015-08-25 22:27:57 +0200661 struct cbfs_file *header)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700662{
Sol Boucher67d59982015-05-07 02:39:22 -0700663 assert(image);
664 assert(buffer);
665 assert(buffer->data);
Sol Boucher67d59982015-05-07 02:39:22 -0700666 assert(!IS_TOP_ALIGNED_ADDRESS(content_offset));
667
Patrick Georgia60e7b62015-08-25 22:26:02 +0200668 const char *name = header->filename;
Patrick Georgie5903582015-08-25 13:53:42 +0200669
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800670 uint32_t entry_type;
671 uint32_t addr, addr_next;
672 struct cbfs_file *entry, *next;
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200673 uint32_t need_size;
Patrick Georgif5252f32015-08-25 22:27:57 +0200674 uint32_t header_size = ntohl(header->offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800675
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800676 need_size = header_size + buffer->size;
677 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
678 name, content_offset, header_size, buffer->size, need_size);
679
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800680 // Merge empty entries.
681 DEBUG("(trying to merge empty entries...)\n");
682 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
683
684 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800685 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800686 entry = cbfs_find_next_entry(image, entry)) {
687
688 entry_type = ntohl(entry->type);
689 if (entry_type != CBFS_COMPONENT_NULL)
690 continue;
691
692 addr = cbfs_get_entry_addr(image, entry);
693 next = cbfs_find_next_entry(image, entry);
694 addr_next = cbfs_get_entry_addr(image, next);
695
696 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
697 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600698
699 /* Will the file fit? Don't yet worry if we have space for a new
700 * "empty" entry. We take care of that later.
701 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800702 if (addr + need_size > addr_next)
703 continue;
704
Patrick Georgiaa44dbd2015-08-12 12:05:21 +0200705 // Test for complicated cases
706 if (content_offset > 0) {
707 if (addr_next < content_offset) {
708 DEBUG("Not for specified offset yet");
709 continue;
710 } else if (addr > content_offset) {
711 DEBUG("Exceed specified content_offset.");
712 break;
713 } else if (addr + header_size > content_offset) {
714 ERROR("Not enough space for header.\n");
715 break;
716 } else if (content_offset + buffer->size > addr_next) {
717 ERROR("Not enough space for content.\n");
718 break;
719 }
720 }
721
722 // TODO there are more few tricky cases that we may
723 // want to fit by altering offset.
724
Patrick Georgidd2d3f92015-08-12 12:29:20 +0200725 if (content_offset == 0) {
726 // we tested every condition earlier under which
727 // placing the file there might fail
728 content_offset = addr + header_size;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800729 }
730
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800731 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
732 addr, addr_next - addr, content_offset);
733
Patrick Georgid5a4afa2015-08-25 22:27:57 +0200734 if (cbfs_add_entry_at(image, entry, buffer->data,
735 content_offset, header) == 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800736 return 0;
737 }
738 break;
739 }
740
741 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
742 buffer->name, buffer->size, buffer->size / 1024, content_offset);
743 return -1;
744}
745
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700746struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
747{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800748 struct cbfs_file *entry;
749 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800750 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800751 entry = cbfs_find_next_entry(image, entry)) {
Patrick Georgic569b8b2015-07-15 16:42:38 +0200752 if (strcasecmp(entry->filename, name) == 0) {
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800753 DEBUG("cbfs_get_entry: found %s\n", name);
754 return entry;
755 }
756 }
757 return NULL;
758}
759
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500760static int cbfs_stage_decompress(struct cbfs_stage *stage, struct buffer *buff)
Aaron Durbin539aed02015-10-23 17:42:32 -0500761{
762 struct buffer reader;
Aaron Durbin539aed02015-10-23 17:42:32 -0500763 char *orig_buffer;
764 char *new_buffer;
765 size_t new_buff_sz;
766 decomp_func_ptr decompress;
767
768 buffer_clone(&reader, buff);
769
770 /* The stage metadata is in little endian. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500771 stage->compression = xdr_le.get32(&reader);
772 stage->entry = xdr_le.get64(&reader);
773 stage->load = xdr_le.get64(&reader);
774 stage->len = xdr_le.get32(&reader);
775 stage->memlen = xdr_le.get32(&reader);
Aaron Durbin539aed02015-10-23 17:42:32 -0500776
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500777 /* Create a buffer just with the uncompressed program now that the
778 * struct cbfs_stage has been peeled off. */
779 if (stage->compression == CBFS_COMPRESS_NONE) {
780 new_buff_sz = buffer_size(buff) - sizeof(struct cbfs_stage);
781
782 orig_buffer = buffer_get(buff);
783 new_buffer = calloc(1, new_buff_sz);
784 memcpy(new_buffer, orig_buffer + sizeof(struct cbfs_stage),
785 new_buff_sz);
786 buffer_init(buff, buff->name, new_buffer, new_buff_sz);
787 free(orig_buffer);
Aaron Durbin539aed02015-10-23 17:42:32 -0500788 return 0;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500789 }
Aaron Durbin539aed02015-10-23 17:42:32 -0500790
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500791 decompress = decompression_function(stage->compression);
Aaron Durbin539aed02015-10-23 17:42:32 -0500792 if (decompress == NULL)
793 return -1;
794
795 orig_buffer = buffer_get(buff);
796
797 /* This can be too big of a buffer needed, but there's no current
798 * field indicating decompressed size of data. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500799 new_buff_sz = stage->memlen;
Aaron Durbin539aed02015-10-23 17:42:32 -0500800 new_buffer = calloc(1, new_buff_sz);
801
802 if (decompress(orig_buffer + sizeof(struct cbfs_stage),
803 (int)(buffer_size(buff) - sizeof(struct cbfs_stage)),
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500804 new_buffer, (int)new_buff_sz, &new_buff_sz)) {
Aaron Durbin539aed02015-10-23 17:42:32 -0500805 ERROR("Couldn't decompress stage.\n");
806 free(new_buffer);
807 return -1;
808 }
809
810 /* Include correct size for full stage info. */
Aaron Durbin539aed02015-10-23 17:42:32 -0500811 buffer_init(buff, buff->name, new_buffer, new_buff_sz);
Aaron Durbin539aed02015-10-23 17:42:32 -0500812
813 /* True decompressed size is just the data size -- no metadata. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500814 stage->len = new_buff_sz;
Aaron Durbin539aed02015-10-23 17:42:32 -0500815 /* Stage is not compressed. */
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500816 stage->compression = CBFS_COMPRESS_NONE;
Aaron Durbin539aed02015-10-23 17:42:32 -0500817
818 free(orig_buffer);
819
820 return 0;
821}
822
Antonello Dettorifda691e2016-06-09 12:35:36 +0200823static int cbfs_payload_decompress(struct cbfs_payload_segment *segments,
824 struct buffer *buff, int num_seg)
825{
826 struct buffer new_buffer;
827 struct buffer seg_buffer;
828 size_t new_buff_sz;
829 char *in_ptr;
830 char *out_ptr;
831 size_t new_offset;
832 decomp_func_ptr decompress;
833
834 new_offset = num_seg * sizeof(*segments);
835 new_buff_sz = num_seg * sizeof(*segments);
836
837 /* Find out and allocate the amount of memory occupied
838 * by the binary data */
839 for (int i = 0; i < num_seg; i++)
840 new_buff_sz += segments[i].mem_len;
841
Furquan Shaikh58644a02016-08-05 08:27:18 -0700842 if (buffer_create(&new_buffer, new_buff_sz, "decompressed_buff"))
843 return -1;
Antonello Dettorifda691e2016-06-09 12:35:36 +0200844
845 in_ptr = buffer_get(buff) + new_offset;
846 out_ptr = buffer_get(&new_buffer) + new_offset;
847
848 for (int i = 0; i < num_seg; i++) {
849 struct buffer tbuff;
850 size_t decomp_size;
851
852 /* The payload uses an unknown compression algorithm. */
853 decompress = decompression_function(segments[i].compression);
854 if (decompress == NULL) {
855 ERROR("Unknown decompression algorithm: %u",
856 segments[i].compression);
857 return -1;
858 }
859
860 /* Segments BSS and ENTRY do not have binary data. */
861 if (segments[i].type == PAYLOAD_SEGMENT_BSS ||
862 segments[i].type == PAYLOAD_SEGMENT_ENTRY) {
863 continue;
864 } else if (segments[i].type == PAYLOAD_SEGMENT_PARAMS) {
865 memcpy(out_ptr, in_ptr, segments[i].len);
866 segments[i].offset = new_offset;
867 new_offset += segments[i].len;
868 in_ptr += segments[i].len;
869 out_ptr += segments[i].len;
870 segments[i].compression = CBFS_COMPRESS_NONE;
871 continue;
872 }
873
Furquan Shaikh58644a02016-08-05 08:27:18 -0700874 if (buffer_create(&tbuff, segments[i].mem_len, "segment")) {
875 buffer_delete(&new_buffer);
876 return -1;
877 }
Antonello Dettorifda691e2016-06-09 12:35:36 +0200878
879 if (decompress(in_ptr, segments[i].len, buffer_get(&tbuff),
880 (int) buffer_size(&tbuff),
881 &decomp_size)) {
882 ERROR("Couldn't decompress payload segment %u\n", i);
883 buffer_delete(&new_buffer);
Furquan Shaikh9844d562016-08-05 08:32:23 -0700884 buffer_delete(&tbuff);
Antonello Dettorifda691e2016-06-09 12:35:36 +0200885 return -1;
886 }
887
888 memcpy(out_ptr, buffer_get(&tbuff), decomp_size);
889
890 in_ptr += segments[i].len;
891
892 /* Update the offset of the segment. */
893 segments[i].offset = new_offset;
894 /* True decompressed size is just the data size. No metadata */
895 segments[i].len = decomp_size;
896 /* Segment is not compressed. */
897 segments[i].compression = CBFS_COMPRESS_NONE;
898
899 /* Update the offset and output buffer pointer. */
900 new_offset += decomp_size;
901 out_ptr += decomp_size;
902
903 buffer_delete(&tbuff);
904 }
905
906 buffer_splice(&seg_buffer, &new_buffer, 0, 0);
907 xdr_segs(&seg_buffer, segments, num_seg);
908
909 buffer_delete(buff);
910 *buff = new_buffer;
911
912 return 0;
913}
914
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500915static int init_elf_from_arch(Elf64_Ehdr *ehdr, uint32_t cbfs_arch)
916{
917 int endian;
918 int nbits;
919 int machine;
920
921 switch (cbfs_arch) {
922 case CBFS_ARCHITECTURE_X86:
923 endian = ELFDATA2LSB;
924 nbits = ELFCLASS32;
925 machine = EM_386;
926 break;
927 case CBFS_ARCHITECTURE_ARM:
928 endian = ELFDATA2LSB;
929 nbits = ELFCLASS32;
930 machine = EM_ARM;
931 break;
932 case CBFS_ARCHITECTURE_AARCH64:
933 endian = ELFDATA2LSB;
934 nbits = ELFCLASS64;
935 machine = EM_AARCH64;
936 break;
937 case CBFS_ARCHITECTURE_MIPS:
938 endian = ELFDATA2LSB;
939 nbits = ELFCLASS32;
940 machine = EM_MIPS;
941 break;
942 case CBFS_ARCHITECTURE_RISCV:
943 endian = ELFDATA2LSB;
944 nbits = ELFCLASS32;
945 machine = EM_RISCV;
946 break;
947 default:
948 ERROR("Unsupported arch: %x\n", cbfs_arch);
949 return -1;
950 }
951
952 elf_init_eheader(ehdr, machine, nbits, endian);
953 return 0;
954}
955
956static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch)
957{
958 Elf64_Ehdr ehdr;
959 Elf64_Shdr shdr;
960 struct cbfs_stage stage;
961 struct elf_writer *ew;
962 struct buffer elf_out;
963 size_t empty_sz;
Aaron Durbin694fd132015-10-28 11:39:34 -0500964 int rmod_ret;
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500965
Antonello Dettori0b806282016-06-26 00:24:25 +0200966 if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
967 ERROR("You need to specify -m ARCH.\n");
968 return -1;
969 }
970
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500971 if (cbfs_stage_decompress(&stage, buff)) {
972 ERROR("Failed to decompress stage.\n");
973 return -1;
974 }
975
976 if (init_elf_from_arch(&ehdr, arch))
977 return -1;
978
979 ehdr.e_entry = stage.entry;
980
Aaron Durbin694fd132015-10-28 11:39:34 -0500981 /* Attempt rmodule translation first. */
982 rmod_ret = rmodule_stage_to_elf(&ehdr, buff);
983
984 if (rmod_ret < 0) {
985 ERROR("rmodule parsing failed\n");
986 return -1;
987 } else if (rmod_ret == 0)
988 return 0;
989
990 /* Rmodule couldn't do anything with the data. Continue on with SELF. */
991
Aaron Durbin5a1e85c2015-10-27 21:02:30 -0500992 ew = elf_writer_init(&ehdr);
993 if (ew == NULL) {
994 ERROR("Unable to init ELF writer.\n");
995 return -1;
996 }
997
998 memset(&shdr, 0, sizeof(shdr));
999 shdr.sh_type = SHT_PROGBITS;
1000 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
1001 shdr.sh_addr = stage.load;
1002 shdr.sh_size = stage.len;
1003 empty_sz = stage.memlen - stage.len;
1004
1005 if (elf_writer_add_section(ew, &shdr, buff, ".program")) {
1006 ERROR("Unable to add ELF section: .program\n");
1007 elf_writer_destroy(ew);
1008 return -1;
1009 }
1010
1011 if (empty_sz != 0) {
1012 struct buffer b;
1013
1014 buffer_init(&b, NULL, NULL, 0);
1015 memset(&shdr, 0, sizeof(shdr));
1016 shdr.sh_type = SHT_NOBITS;
1017 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
1018 shdr.sh_addr = stage.load + stage.len;
1019 shdr.sh_size = empty_sz;
1020 if (elf_writer_add_section(ew, &shdr, &b, ".empty")) {
1021 ERROR("Unable to add ELF section: .empty\n");
1022 elf_writer_destroy(ew);
1023 return -1;
1024 }
1025 }
1026
1027 if (elf_writer_serialize(ew, &elf_out)) {
1028 ERROR("Unable to create ELF file from stage.\n");
1029 elf_writer_destroy(ew);
1030 return -1;
1031 }
1032
1033 /* Flip buffer with the created ELF one. */
1034 buffer_delete(buff);
1035 *buff = elf_out;
1036
1037 elf_writer_destroy(ew);
1038
1039 return 0;
1040}
1041
Antonello Dettorifda691e2016-06-09 12:35:36 +02001042static int cbfs_payload_make_elf(struct buffer *buff, uint32_t arch)
1043{
1044 Elf64_Ehdr ehdr;
1045 Elf64_Shdr shdr;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001046 struct cbfs_payload_segment *segs = NULL;
Jonathan Neuschäfer5de54582016-08-09 15:01:58 +02001047 struct elf_writer *ew = NULL;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001048 struct buffer elf_out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001049 int segments = 0;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001050 int retval = -1;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001051
Antonello Dettori0b806282016-06-26 00:24:25 +02001052 if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
1053 ERROR("You need to specify -m ARCH.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001054 goto out;
Antonello Dettori0b806282016-06-26 00:24:25 +02001055 }
1056
Antonello Dettorifda691e2016-06-09 12:35:36 +02001057 /* Count the number of segments inside buffer */
1058 while (true) {
1059 uint32_t payload_type = 0;
1060
1061 struct cbfs_payload_segment *seg;
1062
1063 seg = buffer_get(buff);
1064 payload_type = read_be32(&seg[segments].type);
1065
1066 if (payload_type == PAYLOAD_SEGMENT_CODE) {
1067 segments++;
1068 } else if (payload_type == PAYLOAD_SEGMENT_DATA) {
1069 segments++;
1070 } else if (payload_type == PAYLOAD_SEGMENT_BSS) {
1071 segments++;
1072 } else if (payload_type == PAYLOAD_SEGMENT_PARAMS) {
1073 segments++;
1074 } else if (payload_type == PAYLOAD_SEGMENT_ENTRY) {
1075 /* The last segment in a payload is always ENTRY as
1076 * specified by the parse_elf_to_payload() function.
1077 * Therefore there is no need to continue looking for
1078 * segments.*/
1079 segments++;
1080 break;
1081 } else {
1082 ERROR("Unknown payload segment type: %x\n",
1083 payload_type);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001084 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001085 }
1086 }
1087
1088 segs = malloc(segments * sizeof(*segs));
1089
1090 /* Decode xdr segments */
1091 for (int i = 0; i < segments; i++) {
1092 struct cbfs_payload_segment *serialized_seg = buffer_get(buff);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001093 xdr_get_seg(&segs[i], &serialized_seg[i]);
1094 }
1095
1096 if (cbfs_payload_decompress(segs, buff, segments)) {
1097 ERROR("Failed to decompress payload.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001098 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001099 }
1100
1101 if (init_elf_from_arch(&ehdr, arch))
Furquan Shaikh7b405172016-08-05 08:20:37 -07001102 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001103
1104 ehdr.e_entry = segs[segments-1].load_addr;
1105
1106 ew = elf_writer_init(&ehdr);
1107 if (ew == NULL) {
1108 ERROR("Unable to init ELF writer.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001109 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001110 }
1111
1112 for (int i = 0; i < segments; i++) {
1113 struct buffer tbuff;
Furquan Shaikhf3bba442016-08-05 08:12:31 -07001114 size_t empty_sz = 0;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001115
1116 memset(&shdr, 0, sizeof(shdr));
1117 char *name = NULL;
1118
1119 if (segs[i].type == PAYLOAD_SEGMENT_CODE) {
1120 shdr.sh_type = SHT_PROGBITS;
1121 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
1122 shdr.sh_addr = segs[i].load_addr;
1123 shdr.sh_size = segs[i].len;
1124 empty_sz = segs[i].mem_len - segs[i].len;
1125 name = strdup(".text");
1126 buffer_splice(&tbuff, buff, segs[i].offset,
1127 segs[i].len);
1128 } else if (segs[i].type == PAYLOAD_SEGMENT_DATA) {
1129 shdr.sh_type = SHT_PROGBITS;
1130 shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
1131 shdr.sh_addr = segs[i].load_addr;
1132 shdr.sh_size = segs[i].len;
1133 empty_sz = segs[i].mem_len - segs[i].len;
1134 name = strdup(".data");
1135 buffer_splice(&tbuff, buff, segs[i].offset,
1136 segs[i].len);
1137 } else if (segs[i].type == PAYLOAD_SEGMENT_BSS) {
1138 shdr.sh_type = SHT_NOBITS;
1139 shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
1140 shdr.sh_addr = segs[i].load_addr;
1141 shdr.sh_size = segs[i].len;
1142 name = strdup(".bss");
1143 buffer_splice(&tbuff, buff, 0, 0);
1144 } else if (segs[i].type == PAYLOAD_SEGMENT_PARAMS) {
1145 shdr.sh_type = SHT_NOTE;
1146 shdr.sh_flags = 0;
1147 shdr.sh_size = segs[i].len;
1148 name = strdup(".note.pinfo");
1149 buffer_splice(&tbuff, buff, segs[i].offset,
1150 segs[i].len);
1151 } else if (segs[i].type == PAYLOAD_SEGMENT_ENTRY) {
1152 break;
1153 }
1154
1155
1156 if (elf_writer_add_section(ew, &shdr, &tbuff, name)) {
1157 ERROR("Unable to add ELF section: %s\n", name);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001158 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001159 }
1160
1161 if (empty_sz != 0) {
1162 struct buffer b;
1163
1164 buffer_init(&b, NULL, NULL, 0);
1165 memset(&shdr, 0, sizeof(shdr));
1166 shdr.sh_type = SHT_NOBITS;
1167 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
1168 shdr.sh_addr = segs[i].load_addr + segs[i].len;
1169 shdr.sh_size = empty_sz;
1170 name = strdup(".empty");
1171 if (elf_writer_add_section(ew, &shdr, &b, name)) {
1172 ERROR("Unable to add ELF section: %s\n", name);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001173 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001174 }
1175 }
Antonello Dettorifda691e2016-06-09 12:35:36 +02001176 }
1177
1178 if (elf_writer_serialize(ew, &elf_out)) {
1179 ERROR("Unable to create ELF file from stage.\n");
Furquan Shaikh7b405172016-08-05 08:20:37 -07001180 goto out;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001181 }
1182
1183 /* Flip buffer with the created ELF one. */
1184 buffer_delete(buff);
1185 *buff = elf_out;
Furquan Shaikh7b405172016-08-05 08:20:37 -07001186 retval = 0;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001187
Furquan Shaikh7b405172016-08-05 08:20:37 -07001188out:
1189 free(segs);
Antonello Dettorifda691e2016-06-09 12:35:36 +02001190 elf_writer_destroy(ew);
Furquan Shaikh7b405172016-08-05 08:20:37 -07001191 return retval;
Antonello Dettorifda691e2016-06-09 12:35:36 +02001192}
1193
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001194int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001195 const char *filename, uint32_t arch)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001196{
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001197 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
1198 struct buffer buffer;
1199 if (!entry) {
1200 ERROR("File not found: %s\n", entry_name);
1201 return -1;
1202 }
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001203
1204 unsigned int decompressed_size = 0;
1205 unsigned int compression = cbfs_file_get_compression_info(entry,
1206 &decompressed_size);
1207
1208 decomp_func_ptr decompress = decompression_function(compression);
1209 if (!decompress) {
1210 ERROR("looking up decompression routine failed\n");
1211 return -1;
1212 }
1213
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001214 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
1215 entry_name, cbfs_get_entry_addr(image, entry),
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001216 get_cbfs_entry_type_name(ntohl(entry->type)), decompressed_size);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001217
Aaron Durbin539aed02015-10-23 17:42:32 -05001218 buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0);
1219
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001220 buffer.data = malloc(decompressed_size);
1221 buffer.size = decompressed_size;
1222 if (decompress(CBFS_SUBHEADER(entry), ntohl(entry->len),
Aaron Durbin5213c532015-10-23 17:38:40 -05001223 buffer.data, buffer.size, NULL)) {
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001224 ERROR("decompression failed for %s\n", entry_name);
Aaron Durbin539aed02015-10-23 17:42:32 -05001225 buffer_delete(&buffer);
Patrick Georgi23aeaff2015-08-26 13:01:10 +02001226 return -1;
1227 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001228
1229 /*
1230 * The stage metadata is never compressed proper for cbfs_stage
1231 * files. The contents of the stage data can be though. Therefore
1232 * one has to do a second pass for stages to potentially decompress
1233 * the stage data to make it more meaningful.
1234 */
1235 if (ntohl(entry->type) == CBFS_COMPONENT_STAGE) {
Aaron Durbin5a1e85c2015-10-27 21:02:30 -05001236 if (cbfs_stage_make_elf(&buffer, arch)) {
Aaron Durbin539aed02015-10-23 17:42:32 -05001237 buffer_delete(&buffer);
1238 return -1;
1239 }
Antonello Dettorifda691e2016-06-09 12:35:36 +02001240 } else if (ntohl(entry->type) == CBFS_COMPONENT_PAYLOAD) {
1241 if (cbfs_payload_make_elf(&buffer, arch)) {
1242 buffer_delete(&buffer);
1243 return -1;
1244 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001245 }
1246
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001247 if (buffer_write_file(&buffer, filename) != 0) {
1248 ERROR("Failed to write %s into %s.\n",
1249 entry_name, filename);
Aaron Durbin539aed02015-10-23 17:42:32 -05001250 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001251 return -1;
1252 }
Aaron Durbin539aed02015-10-23 17:42:32 -05001253
1254 buffer_delete(&buffer);
Hung-Te Lin0f8af712013-01-29 02:29:49 +08001255 INFO("Successfully dumped the file to: %s\n", filename);
1256 return 0;
1257}
1258
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001259int cbfs_remove_entry(struct cbfs_image *image, const char *name)
1260{
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +02001261 struct cbfs_file *entry;
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001262 entry = cbfs_get_entry(image, name);
1263 if (!entry) {
1264 ERROR("CBFS file %s not found.\n", name);
1265 return -1;
1266 }
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001267 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
Patrick Georgic569b8b2015-07-15 16:42:38 +02001268 entry->filename, cbfs_get_entry_addr(image, entry));
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001269 entry->type = htonl(CBFS_COMPONENT_DELETED);
Patrick Georgi4d1c5aa2015-07-17 22:07:26 +02001270 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
Hung-Te Linc03d9b02013-01-29 02:38:40 +08001271 return 0;
1272}
1273
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001274int cbfs_print_header_info(struct cbfs_image *image)
1275{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001276 char *name = strdup(image->buffer.name);
Sol Boucher3e060ed2015-05-05 15:40:15 -07001277 assert(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001278 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -08001279 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001280 basename(name),
1281 image->buffer.size / 1024,
Sol Boucher3e060ed2015-05-05 15:40:15 -07001282 image->header.bootblocksize,
1283 image->header.romsize,
1284 image->header.offset,
1285 image->header.align,
1286 arch_to_string(image->header.architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001287 free(name);
1288 return 0;
1289}
1290
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001291static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
1292{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001293 fprintf(fp,
1294 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
1295 "length: %d/%d\n",
1296 lookup_name_by_type(types_cbfs_compression,
1297 stage->compression, "(unknown)"),
1298 stage->entry,
1299 stage->load,
1300 stage->len,
1301 stage->memlen);
1302 return 0;
1303}
1304
Hung-Te Lin0780d672014-05-16 10:14:05 +08001305static int cbfs_print_decoded_payload_segment_info(
1306 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001307{
Hung-Te Lin0780d672014-05-16 10:14:05 +08001308 /* The input (seg) must be already decoded by
1309 * cbfs_decode_payload_segment.
1310 */
1311 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001312 case PAYLOAD_SEGMENT_CODE:
1313 case PAYLOAD_SEGMENT_DATA:
1314 fprintf(fp, " %s (%s compression, offset: 0x%x, "
1315 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001316 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001317 "code " : "data"),
1318 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +08001319 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001320 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +08001321 seg->offset, seg->load_addr, seg->len,
1322 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001323 break;
1324
1325 case PAYLOAD_SEGMENT_ENTRY:
1326 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001327 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001328 break;
1329
1330 case PAYLOAD_SEGMENT_BSS:
1331 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
1332 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001333 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001334 break;
1335
1336 case PAYLOAD_SEGMENT_PARAMS:
1337 fprintf(fp, " parameters\n");
1338 break;
1339
1340 default:
1341 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
1342 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +08001343 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001344 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +08001345 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001346 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +08001347 seg->offset, seg->load_addr, seg->len,
1348 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001349 break;
1350 }
1351 return 0;
1352}
1353
1354int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001355 void *arg)
1356{
Patrick Georgic569b8b2015-07-15 16:42:38 +02001357 const char *name = entry->filename;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001358 struct cbfs_payload_segment *payload;
1359 FILE *fp = (FILE *)arg;
1360
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001361 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001362 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1363 cbfs_get_entry_addr(image, entry));
1364 return -1;
1365 }
1366 if (!fp)
1367 fp = stdout;
1368
Patrick Georgic82725c2015-08-26 12:13:03 +02001369 unsigned int decompressed_size = 0;
Patrick Georgia71c83f2015-08-26 12:23:26 +02001370 unsigned int compression = cbfs_file_get_compression_info(entry,
1371 &decompressed_size);
Patrick Georgic82725c2015-08-26 12:13:03 +02001372
1373 if (compression == CBFS_COMPRESS_NONE) {
1374 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
1375 *name ? name : "(empty)",
1376 cbfs_get_entry_addr(image, entry),
1377 get_cbfs_entry_type_name(ntohl(entry->type)),
1378 ntohl(entry->len));
1379 } else {
1380 fprintf(fp, "%-30s 0x%-8x %-12s %d (%d after %s decompression)\n",
1381 *name ? name : "(empty)",
1382 cbfs_get_entry_addr(image, entry),
1383 get_cbfs_entry_type_name(ntohl(entry->type)),
1384 ntohl(entry->len),
1385 decompressed_size,
1386 lookup_name_by_type(types_cbfs_compression,
1387 compression, "(unknown)")
1388 );
1389 }
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001390
Patrick Georgi89f20342015-10-01 15:54:04 +02001391 struct cbfs_file_attr_hash *hash = NULL;
1392 while ((hash = cbfs_file_get_next_hash(entry, hash)) != NULL) {
1393 unsigned int hash_type = ntohl(hash->hash_type);
Furquan Shaikh1d56eef2016-12-02 09:24:50 -08001394 if (hash_type >= CBFS_NUM_SUPPORTED_HASHES) {
Patrick Georgi89f20342015-10-01 15:54:04 +02001395 fprintf(fp, "invalid hash type %d\n", hash_type);
1396 break;
1397 }
1398 size_t hash_len = widths_cbfs_hash[hash_type];
1399 char *hash_str = bintohex(hash->hash_data, hash_len);
1400 uint8_t local_hash[hash_len];
1401 if (vb2_digest_buffer(CBFS_SUBHEADER(entry),
1402 ntohl(entry->len), hash_type, local_hash,
1403 hash_len) != VB2_SUCCESS) {
1404 fprintf(fp, "failed to hash '%s'\n", name);
Patrick Georgi862df922016-12-14 16:10:00 +01001405 free(hash_str);
Patrick Georgi89f20342015-10-01 15:54:04 +02001406 break;
1407 }
1408 int valid = memcmp(local_hash, hash->hash_data, hash_len) == 0;
1409 const char *valid_str = valid ? "valid" : "invalid";
1410
1411 fprintf(fp, " hash %s:%s %s\n",
1412 get_hash_attr_name(hash_type),
1413 hash_str, valid_str);
1414 free(hash_str);
1415 }
1416
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001417 if (!verbose)
1418 return 0;
1419
1420 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
1421 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
1422 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
1423 ntohl(entry->len));
1424
1425 /* note the components of the subheader may be in host order ... */
1426 switch (ntohl(entry->type)) {
1427 case CBFS_COMPONENT_STAGE:
1428 cbfs_print_stage_info((struct cbfs_stage *)
1429 CBFS_SUBHEADER(entry), fp);
1430 break;
1431
1432 case CBFS_COMPONENT_PAYLOAD:
Paul Menzel831bbe82015-08-08 20:20:57 +02001433 payload = (struct cbfs_payload_segment *)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001434 CBFS_SUBHEADER(entry);
1435 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +08001436 struct cbfs_payload_segment seg;
1437 cbfs_decode_payload_segment(&seg, payload);
1438 cbfs_print_decoded_payload_segment_info(
1439 &seg, fp);
1440 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001441 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +08001442 else
Aaron Durbinca630272014-08-05 10:48:20 -05001443 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001444 }
1445 break;
1446 default:
1447 break;
1448 }
1449 return 0;
1450}
1451
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001452static int cbfs_print_parseable_entry_info(struct cbfs_image *image,
1453 struct cbfs_file *entry, void *arg)
1454{
1455 FILE *fp = (FILE *)arg;
1456 const char *name;
1457 const char *type;
1458 size_t offset;
1459 size_t metadata_size;
1460 size_t data_size;
1461 const char *sep = "\t";
1462
1463 if (!cbfs_is_valid_entry(image, entry)) {
1464 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
1465 cbfs_get_entry_addr(image, entry));
1466 return -1;
1467 }
1468
1469 name = entry->filename;
1470 if (*name == '\0')
1471 name = "(empty)";
1472 type = get_cbfs_entry_type_name(ntohl(entry->type)),
1473 metadata_size = ntohl(entry->offset);
1474 data_size = ntohl(entry->len);
1475 offset = cbfs_get_entry_addr(image, entry);
1476
1477 fprintf(fp, "%s%s", name, sep);
1478 fprintf(fp, "0x%zx%s", offset, sep);
1479 fprintf(fp, "%s%s", type, sep);
1480 fprintf(fp, "0x%zx%s", metadata_size, sep);
1481 fprintf(fp, "0x%zx%s", data_size, sep);
1482 fprintf(fp, "0x%zx\n", metadata_size + data_size);
1483
1484 return 0;
1485}
1486
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001487int cbfs_print_directory(struct cbfs_image *image)
1488{
Sol Boucher67a0a862015-03-18 12:36:27 -07001489 if (cbfs_is_legacy_cbfs(image))
1490 cbfs_print_header_info(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001491 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
1492 cbfs_walk(image, cbfs_print_entry_info, NULL);
1493 return 0;
1494}
1495
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001496int cbfs_print_parseable_directory(struct cbfs_image *image)
1497{
Furquan Shaikh161d2332016-05-26 14:41:02 -07001498 size_t i;
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001499 const char *header[] = {
1500 "Name",
1501 "Offset",
1502 "Type",
1503 "Metadata Size",
1504 "Data Size",
1505 "Total Size",
1506 };
1507 const char *sep = "\t";
1508
1509 for (i = 0; i < ARRAY_SIZE(header) - 1; i++)
1510 fprintf(stdout, "%s%s", header[i], sep);
1511 fprintf(stdout, "%s\n", header[i]);
1512 cbfs_walk(image, cbfs_print_parseable_entry_info, stdout);
1513 return 0;
1514}
1515
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001516int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -08001517 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001518{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001519 struct cbfs_file *next;
Aaron Durbin285111f2015-08-08 20:25:17 +02001520 uint8_t *name;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001521 uint32_t type, addr, last_addr;
1522
1523 type = ntohl(entry->type);
1524 if (type == CBFS_COMPONENT_DELETED) {
1525 // Ready to be recycled.
1526 type = CBFS_COMPONENT_NULL;
1527 entry->type = htonl(type);
Aaron Durbin285111f2015-08-08 20:25:17 +02001528 // Place NUL byte as first byte of name to be viewed as "empty".
1529 name = (void *)&entry[1];
1530 *name = '\0';
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001531 }
1532 if (type != CBFS_COMPONENT_NULL)
1533 return 0;
1534
1535 next = cbfs_find_next_entry(image, entry);
1536
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001537 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001538 type = ntohl(next->type);
1539 if (type == CBFS_COMPONENT_DELETED) {
1540 type = CBFS_COMPONENT_NULL;
1541 next->type = htonl(type);
1542 }
1543 if (type != CBFS_COMPONENT_NULL)
1544 return 0;
1545
1546 addr = cbfs_get_entry_addr(image, entry);
1547 last_addr = cbfs_get_entry_addr(
1548 image, cbfs_find_next_entry(image, next));
1549
1550 // Now, we find two deleted/empty entries; try to merge now.
1551 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
1552 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
1553 cbfs_get_entry_addr(image, next), ntohl(next->len));
Patrick Georgiedf25d92015-08-12 09:12:06 +02001554 cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL,
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001555 (last_addr - addr -
1556 cbfs_calculate_file_header_size("")),
1557 "");
1558 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
1559 next = cbfs_find_next_entry(image, entry);
1560 }
1561 return 0;
1562}
1563
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001564int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001565 void *arg)
1566{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001567 int count = 0;
1568 struct cbfs_file *entry;
1569 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001570 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +08001571 entry = cbfs_find_next_entry(image, entry)) {
1572 count ++;
1573 if (callback(image, entry, arg) != 0)
1574 break;
1575 }
1576 return count;
1577}
1578
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001579static int cbfs_header_valid(struct cbfs_header *header)
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001580{
1581 if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
1582 ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
1583 (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001584 (ntohl(header->offset) < ntohl(header->romsize)))
1585 return 1;
1586 return 0;
1587}
1588
1589struct cbfs_header *cbfs_find_header(char *data, size_t size,
1590 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001591{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001592 size_t offset;
1593 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001594 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001595 struct cbfs_header *header, *result = NULL;
1596
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001597 if (forced_offset < (size - sizeof(struct cbfs_header))) {
1598 /* Check if the forced header is valid. */
1599 header = (struct cbfs_header *)(data + forced_offset);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001600 if (cbfs_header_valid(header))
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001601 return header;
1602 return NULL;
1603 }
1604
Julius Wernerefcee762014-11-10 13:14:24 -08001605 // Try finding relative offset of master header at end of file first.
1606 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
1607 offset = size + rel_offset;
1608 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
1609 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -08001610
Hung-Te Lineab2c812013-01-29 01:56:17 +08001611 if (offset >= size - sizeof(*header) ||
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001612 !cbfs_header_valid((struct cbfs_header *)(data + offset))) {
Julius Wernerefcee762014-11-10 13:14:24 -08001613 // Some use cases append non-CBFS data to the end of the ROM.
1614 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +08001615 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -08001616 }
Hung-Te Lineab2c812013-01-29 01:56:17 +08001617
1618 for (; offset + sizeof(*header) < size; offset++) {
1619 header = (struct cbfs_header *)(data + offset);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001620 if (!cbfs_header_valid(header))
Hung-Te Lineab2c812013-01-29 01:56:17 +08001621 continue;
Julius Wernerefcee762014-11-10 13:14:24 -08001622 if (!found++)
1623 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +08001624 }
Julius Wernerefcee762014-11-10 13:14:24 -08001625 if (found > 1)
1626 // Top-aligned images usually have a working relative offset
1627 // field, so this is more likely to happen on bottom-aligned
1628 // ones (where the first header is the "outermost" one)
1629 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +08001630 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001631 return result;
1632}
1633
1634
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001635struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
1636{
Sol Boucher3e060ed2015-05-05 15:40:15 -07001637 assert(image);
Patrick Georgi7db2b6c2015-11-11 15:35:24 +01001638 if (image->has_header)
1639 /* header.offset is relative to start of flash, not
1640 * start of region, so use it with the full image.
1641 */
1642 return (struct cbfs_file *)
1643 (buffer_get_original_backing(&image->buffer) +
1644 image->header.offset);
1645 else
1646 return (struct cbfs_file *)buffer_get(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +08001647}
1648
1649struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001650 struct cbfs_file *entry)
1651{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001652 uint32_t addr = cbfs_get_entry_addr(image, entry);
Sol Boucher67a0a862015-03-18 12:36:27 -07001653 int align = image->has_header ? image->header.align :
1654 CBFS_ENTRY_ALIGNMENT;
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001655 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001656 addr += ntohl(entry->offset) + ntohl(entry->len);
1657 addr = align_up(addr, align);
1658 return (struct cbfs_file *)(image->buffer.data + addr);
1659}
1660
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001661uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
1662{
Hung-Te Lineab2c812013-01-29 01:56:17 +08001663 assert(image && image->buffer.data && entry);
1664 return (int32_t)((char *)entry - image->buffer.data);
1665}
1666
Sol Boucher67a0a862015-03-18 12:36:27 -07001667int cbfs_is_valid_cbfs(struct cbfs_image *image)
1668{
1669 return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC,
1670 strlen(CBFS_FILE_MAGIC));
1671}
1672
1673int cbfs_is_legacy_cbfs(struct cbfs_image *image)
1674{
1675 return image->has_header;
1676}
1677
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001678int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
1679{
Sol Bouchere3260a02015-03-25 13:40:08 -07001680 uint32_t offset = cbfs_get_entry_addr(image, entry);
1681
1682 if (offset >= image->buffer.size)
1683 return 0;
1684
1685 struct buffer entry_data;
1686 buffer_clone(&entry_data, &image->buffer);
1687 buffer_seek(&entry_data, offset);
1688 return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
Sol Boucher67a0a862015-03-18 12:36:27 -07001689 strlen(CBFS_FILE_MAGIC));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001690}
1691
Patrick Georgi57edf162015-08-12 09:20:11 +02001692struct cbfs_file *cbfs_create_file_header(int type,
Vadim Bendebury45e59972014-12-23 15:59:57 -08001693 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001694{
Patrick Georgi2c615062015-07-15 20:49:00 +02001695 struct cbfs_file *entry = malloc(MAX_CBFS_FILE_HEADER_BUFFER);
1696 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, MAX_CBFS_FILE_HEADER_BUFFER);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001697 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
Patrick Georgiedf25d92015-08-12 09:12:06 +02001698 entry->type = htonl(type);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001699 entry->len = htonl(len);
Patrick Georgi0d618af2015-07-15 18:28:23 +02001700 entry->attributes_offset = 0;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001701 entry->offset = htonl(cbfs_calculate_file_header_size(name));
Patrick Georgic569b8b2015-07-15 16:42:38 +02001702 memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry));
1703 strcpy(entry->filename, name);
Patrick Georgi57edf162015-08-12 09:20:11 +02001704 return entry;
1705}
1706
1707int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
1708 size_t len, const char *name)
1709{
1710 struct cbfs_file *tmp = cbfs_create_file_header(type, len, name);
1711 memcpy(entry, tmp, ntohl(tmp->offset));
1712 free(tmp);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001713 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1714 return 0;
1715}
1716
Patrick Georgi2c615062015-07-15 20:49:00 +02001717struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file)
1718{
1719 /* attributes_offset should be 0 when there is no attribute, but all
1720 * values that point into the cbfs_file header are invalid, too. */
1721 if (ntohl(file->attributes_offset) <= sizeof(*file))
1722 return NULL;
1723
1724 /* There needs to be enough space for the file header and one
1725 * attribute header for this to make sense. */
1726 if (ntohl(file->offset) <=
1727 sizeof(*file) + sizeof(struct cbfs_file_attribute))
1728 return NULL;
1729
1730 return (struct cbfs_file_attribute *)
1731 (((uint8_t *)file) + ntohl(file->attributes_offset));
1732}
1733
1734struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
1735 struct cbfs_file_attribute *attr)
1736{
1737 /* ex falso sequitur quodlibet */
1738 if (attr == NULL)
1739 return NULL;
1740
1741 /* Is there enough space for another attribute? */
1742 if ((uint8_t *)attr + ntohl(attr->len) +
1743 sizeof(struct cbfs_file_attribute) >=
1744 (uint8_t *)file + ntohl(file->offset))
1745 return NULL;
1746
1747 struct cbfs_file_attribute *next = (struct cbfs_file_attribute *)
1748 (((uint8_t *)attr) + ntohl(attr->len));
1749 /* If any, "unused" attributes must come last. */
1750 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED)
1751 return NULL;
1752 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
1753 return NULL;
1754
1755 return next;
1756}
1757
1758struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
1759 uint32_t tag,
1760 uint32_t size)
1761{
1762 struct cbfs_file_attribute *attr, *next;
1763 next = cbfs_file_first_attr(header);
1764 do {
1765 attr = next;
1766 next = cbfs_file_next_attr(header, attr);
1767 } while (next != NULL);
1768 uint32_t header_size = ntohl(header->offset) + size;
1769 if (header_size > MAX_CBFS_FILE_HEADER_BUFFER) {
1770 DEBUG("exceeding allocated space for cbfs_file headers");
1771 return NULL;
1772 }
1773 /* attr points to the last valid attribute now.
1774 * If NULL, we have to create the first one. */
1775 if (attr == NULL) {
1776 /* New attributes start where the header ends.
1777 * header->offset is later set to accomodate the
1778 * additional structure.
1779 * No endianess translation necessary here, because both
1780 * fields are encoded the same way. */
1781 header->attributes_offset = header->offset;
1782 attr = (struct cbfs_file_attribute *)
1783 (((uint8_t *)header) +
1784 ntohl(header->attributes_offset));
1785 } else {
1786 attr = (struct cbfs_file_attribute *)
1787 (((uint8_t *)attr) +
1788 ntohl(attr->len));
1789 }
1790 header->offset = htonl(header_size);
1791 memset(attr, CBFS_CONTENT_DEFAULT_VALUE, size);
1792 attr->tag = htonl(tag);
1793 attr->len = htonl(size);
1794 return attr;
1795}
1796
Patrick Georgi89f20342015-10-01 15:54:04 +02001797int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer,
1798 enum vb2_hash_algorithm hash_type)
1799{
zbao37450ff2015-11-05 14:35:57 +08001800 uint32_t hash_index = hash_type;
1801
1802 if (hash_index >= CBFS_NUM_SUPPORTED_HASHES)
Patrick Georgi89f20342015-10-01 15:54:04 +02001803 return -1;
1804
1805 unsigned hash_size = widths_cbfs_hash[hash_type];
1806 if (hash_size == 0)
1807 return -1;
1808
1809 struct cbfs_file_attr_hash *attrs =
1810 (struct cbfs_file_attr_hash *)cbfs_add_file_attr(header,
1811 CBFS_FILE_ATTR_TAG_HASH,
1812 sizeof(struct cbfs_file_attr_hash) + hash_size);
1813
1814 if (attrs == NULL)
1815 return -1;
1816
1817 attrs->hash_type = htonl(hash_type);
1818 if (vb2_digest_buffer(buffer_get(buffer), buffer_size(buffer),
1819 hash_type, attrs->hash_data, hash_size) != VB2_SUCCESS)
1820 return -1;
1821
1822 return 0;
1823}
1824
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001825/* Finds a place to hold whole data in same memory page. */
1826static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1827{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001828 if (!page)
1829 return 1;
1830 return (start / page) == (start + size - 1) / page;
1831}
1832
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001833/* Tests if data can fit in a range by given offset:
Aaron Durbind7339412015-09-15 12:50:14 -05001834 * start ->| metadata_size | offset (+ size) |<- end
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001835 */
Aaron Durbind7339412015-09-15 12:50:14 -05001836static int is_in_range(size_t start, size_t end, size_t metadata_size,
1837 size_t offset, size_t size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001838{
Aaron Durbind7339412015-09-15 12:50:14 -05001839 return (offset >= start + metadata_size && offset + size <= end);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001840}
1841
Werner Zeh95bfcae2016-01-25 12:47:20 +01001842static size_t absolute_align(const struct cbfs_image *image, size_t val,
1843 size_t align)
1844{
1845 const size_t region_offset = buffer_offset(&image->buffer);
1846 /* To perform alignment on absolute address, take the region offset */
1847 /* of the image into account. */
1848 return align_up(val + region_offset, align) - region_offset;
1849
1850}
1851
Aaron Durbind7339412015-09-15 12:50:14 -05001852int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size,
1853 size_t page_size, size_t align, size_t metadata_size)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001854{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001855 struct cbfs_file *entry;
1856 size_t need_len;
Aaron Durbind7339412015-09-15 12:50:14 -05001857 size_t addr, addr_next, addr2, addr3, offset;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001858
1859 /* Default values: allow fitting anywhere in ROM. */
1860 if (!page_size)
Sol Boucher67a0a862015-03-18 12:36:27 -07001861 page_size = image->has_header ? image->header.romsize :
1862 image->buffer.size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001863 if (!align)
1864 align = 1;
1865
1866 if (size > page_size)
Aaron Durbind7339412015-09-15 12:50:14 -05001867 ERROR("Input file size (%zd) greater than page size (%zd).\n",
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001868 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001869
Aaron Durbind7339412015-09-15 12:50:14 -05001870 size_t image_align = image->has_header ? image->header.align :
Sol Boucher67a0a862015-03-18 12:36:27 -07001871 CBFS_ENTRY_ALIGNMENT;
1872 if (page_size % image_align)
Aaron Durbind7339412015-09-15 12:50:14 -05001873 WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001874 __func__, page_size, image_align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001875
Aaron Durbind7339412015-09-15 12:50:14 -05001876 need_len = metadata_size + size;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001877
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001878 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001879 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
1880
1881 /* Three cases of content location on memory page:
1882 * case 1.
1883 * | PAGE 1 | PAGE 2 |
1884 * | <header><content>| Fit. Return start of content.
1885 *
1886 * case 2.
1887 * | PAGE 1 | PAGE 2 |
1888 * | <header><content> | Fits when we shift content to align
1889 * shift-> | <header>|<content> | at starting of PAGE 2.
1890 *
1891 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001892 * | PAGE 1 | PAGE 2 | PAGE 3 |
1893 * | <header>< content > | Can't fit. If we shift content to
1894 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1895 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001896 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001897 * The returned address can be then used as "base-address" (-b) in add-*
1898 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1899 * For stage targets, the address is also used to re-link stage before
1900 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001901 */
1902 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001903 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001904 entry = cbfs_find_next_entry(image, entry)) {
1905
1906 uint32_t type = ntohl(entry->type);
1907 if (type != CBFS_COMPONENT_NULL)
1908 continue;
1909
1910 addr = cbfs_get_entry_addr(image, entry);
1911 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
1912 image, entry));
1913 if (addr_next - addr < need_len)
1914 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001915
Werner Zeh95bfcae2016-01-25 12:47:20 +01001916 offset = absolute_align(image, addr + metadata_size, align);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001917 if (is_in_same_page(offset, size, page_size) &&
Aaron Durbind7339412015-09-15 12:50:14 -05001918 is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001919 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001920 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001921 }
1922
1923 addr2 = align_up(addr, page_size);
Werner Zeh95bfcae2016-01-25 12:47:20 +01001924 offset = absolute_align(image, addr2, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001925 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001926 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001927 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001928 }
1929
Aaron Durbind7339412015-09-15 12:50:14 -05001930 /* Assume page_size >= metadata_size so adding one page will
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001931 * definitely provide the space for header. */
Aaron Durbind7339412015-09-15 12:50:14 -05001932 assert(page_size >= metadata_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001933 addr3 = addr2 + page_size;
Werner Zeh95bfcae2016-01-25 12:47:20 +01001934 offset = absolute_align(image, addr3, align);
Aaron Durbind7339412015-09-15 12:50:14 -05001935 if (is_in_range(addr, addr_next, metadata_size, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001936 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001937 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001938 }
1939 }
1940 return -1;
1941}