blob: 4ecb461f5e6e8c7921644decf20513c8266e5c23 [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.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
19
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080020#include <inttypes.h>
21#include <libgen.h>
Patrick Georgicccc9d42015-04-28 13:09:36 +020022#include <stddef.h>
Hung-Te Lineab2c812013-01-29 01:56:17 +080023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Sol Boucher0e539312015-03-05 15:38:03 -080026#include <strings.h>
Hung-Te Lineab2c812013-01-29 01:56:17 +080027
28#include "common.h"
29#include "cbfs_image.h"
30
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
53/* To make CBFS more friendly to ROM, fill -1 (0xFF) instead of zero. */
54#define CBFS_CONTENT_DEFAULT_VALUE (-1)
55
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080056/* Type and format */
57
58struct typedesc_t {
59 uint32_t type;
60 const char *name;
61};
62
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070063static const struct typedesc_t types_cbfs_entry[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080064 {CBFS_COMPONENT_STAGE, "stage"},
65 {CBFS_COMPONENT_PAYLOAD, "payload"},
66 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
67 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
68 {CBFS_COMPONENT_RAW, "raw"},
69 {CBFS_COMPONENT_VSA, "vsa"},
70 {CBFS_COMPONENT_MBI, "mbi"},
71 {CBFS_COMPONENT_MICROCODE, "microcode"},
Martin Rothdde307c2015-03-24 15:54:20 -060072 {CBFS_COMPONENT_FSP, "fsp"},
73 {CBFS_COMPONENT_MRC, "mrc"},
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080074 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos_default"},
75 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos_layout"},
Martin Rothdde307c2015-03-24 15:54:20 -060076 {CBFS_COMPONENT_SPD, "spd"},
77 {CBFS_COMPONENT_MRC_CACHE, "mrc_cache"},
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080078 {CBFS_COMPONENT_DELETED, "deleted"},
79 {CBFS_COMPONENT_NULL, "null"},
80 {0, NULL},
81};
82
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070083static const struct typedesc_t types_cbfs_compression[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080084 {CBFS_COMPRESS_NONE, "none"},
85 {CBFS_COMPRESS_LZMA, "LZMA"},
86 {0, NULL},
87};
88
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080089static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070090 const char *default_value)
91{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080092 int i;
93 for (i = 0; desc[i].name; i++)
94 if (desc[i].type == type)
95 return desc[i].name;
96 return default_value;
97}
98
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010099static const char *get_cbfs_entry_type_name(uint32_t type)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700100{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800101 return lookup_name_by_type(types_cbfs_entry, type, "(unknown)");
102}
103
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800104/* CBFS image */
105
Sol Boucher0e539312015-03-05 15:38:03 -0800106static size_t cbfs_calculate_file_header_size(const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700107{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800108 return (sizeof(struct cbfs_file) +
109 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
110}
111
Sol Boucher67a0a862015-03-18 12:36:27 -0700112/* Only call on legacy CBFSes possessing a master header. */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600113static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700114{
Sol Boucher67a0a862015-03-18 12:36:27 -0700115 assert(image);
116 assert(cbfs_is_legacy_cbfs(image));
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800117 // A bug in old cbfstool may produce extra few bytes (by alignment) and
118 // cause cbfstool to overwrite things after free space -- which is
119 // usually CBFS header on x86. We need to workaround that.
120
121 struct cbfs_file *entry, *first = NULL, *last = NULL;
122 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800123 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800124 entry = cbfs_find_next_entry(image, entry)) {
125 last = entry;
126 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600127 if ((char *)first < (char *)hdr_loc &&
128 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800129 WARN("CBFS image was created with old cbfstool with size bug. "
130 "Fixing size in last entry...\n");
Sol Boucher3e060ed2015-05-05 15:40:15 -0700131 last->len = htonl(ntohl(last->len) - image->header.align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800132 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
133 cbfs_get_entry_addr(image, entry),
134 cbfs_get_entry_addr(image,
135 cbfs_find_next_entry(image, last)));
136 }
137 return 0;
138}
139
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800140void cbfs_put_header(void *dest, const struct cbfs_header *header)
141{
142 struct buffer outheader;
143
144 outheader.data = dest;
145 outheader.size = 0;
146
147 xdr_be.put32(&outheader, header->magic);
148 xdr_be.put32(&outheader, header->version);
149 xdr_be.put32(&outheader, header->romsize);
150 xdr_be.put32(&outheader, header->bootblocksize);
151 xdr_be.put32(&outheader, header->align);
152 xdr_be.put32(&outheader, header->offset);
153 xdr_be.put32(&outheader, header->architecture);
154}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600155
Hung-Te Lin0780d672014-05-16 10:14:05 +0800156static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
157 struct cbfs_payload_segment *input)
158{
159 struct buffer seg = {
160 .data = (void *)input,
161 .size = sizeof(*input),
162 };
163 output->type = xdr_be.get32(&seg);
164 output->compression = xdr_be.get32(&seg);
165 output->offset = xdr_be.get32(&seg);
166 output->load_addr = xdr_be.get64(&seg);
167 output->len = xdr_be.get32(&seg);
168 output->mem_len = xdr_be.get32(&seg);
169 assert(seg.size == 0);
170}
171
Sol Boucher0e539312015-03-05 15:38:03 -0800172void cbfs_get_header(struct cbfs_header *header, void *src)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600173{
174 struct buffer outheader;
175
Sol Boucher0e539312015-03-05 15:38:03 -0800176 outheader.data = src; /* We're not modifying the data */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600177 outheader.size = 0;
178
179 header->magic = xdr_be.get32(&outheader);
180 header->version = xdr_be.get32(&outheader);
181 header->romsize = xdr_be.get32(&outheader);
182 header->bootblocksize = xdr_be.get32(&outheader);
183 header->align = xdr_be.get32(&outheader);
184 header->offset = xdr_be.get32(&outheader);
185 header->architecture = xdr_be.get32(&outheader);
186}
187
Sol Boucher67a0a862015-03-18 12:36:27 -0700188int cbfs_image_create(struct cbfs_image *image, size_t entries_size)
189{
190 assert(image);
191 assert(image->buffer.data);
192
193 size_t empty_header_len = cbfs_calculate_file_header_size("");
194 uint32_t entries_offset = 0;
195 uint32_t align = CBFS_ENTRY_ALIGNMENT;
196 if (image->has_header) {
197 entries_offset = image->header.offset;
198
199 if (entries_offset > image->buffer.size) {
200 ERROR("CBFS file entries are located outside CBFS itself\n");
201 return -1;
202 }
203
204 align = image->header.align;
205 }
206
207 // This attribute must be given in order to prove that this module
208 // correctly preserves certain CBFS properties. See the block comment
209 // near the top of this file (and the associated commit message).
210 if (align < empty_header_len) {
211 ERROR("CBFS must be aligned to at least %zu bytes\n",
212 empty_header_len);
213 return -1;
214 }
215
216 if (entries_size > image->buffer.size - entries_offset) {
217 ERROR("CBFS doesn't have enough space to fit its file entries\n");
218 return -1;
219 }
220
221 if (empty_header_len > entries_size) {
222 ERROR("CBFS is too small to fit any header\n");
223 return -1;
224 }
225 struct cbfs_file *entry_header =
226 (struct cbfs_file *)(image->buffer.data + entries_offset);
227 // This alignment is necessary 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 entries_size -= entries_size % align;
231
232 size_t capacity = entries_size - empty_header_len;
233 LOG("Created CBFS (capacity = %zu bytes)\n", capacity);
234 return cbfs_create_empty_entry(entry_header, capacity, "");
235}
236
237int cbfs_legacy_image_create(struct cbfs_image *image,
238 uint32_t architecture,
239 uint32_t align,
240 struct buffer *bootblock,
241 uint32_t bootblock_offset,
242 uint32_t header_offset,
243 uint32_t entries_offset)
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800244{
Sol Bouchere3260a02015-03-25 13:40:08 -0700245 assert(image);
246 assert(image->buffer.data);
247 assert(bootblock);
248
Julius Wernerefcee762014-11-10 13:14:24 -0800249 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800250 uint32_t cbfs_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600251 void *header_loc;
Sol Bouchere3260a02015-03-25 13:40:08 -0700252 size_t size = image->buffer.size;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800253
254 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
255 "header=0x%x+0x%zx, entries_offset=0x%x\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700256 bootblock_offset, bootblock->size, header_offset,
257 sizeof(image->header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800258
Sol Boucher67a0a862015-03-18 12:36:27 -0700259 // Adjust legacy top-aligned address to ROM offset.
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800260 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800261 entries_offset = size + (int32_t)entries_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800262 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800263 bootblock_offset = size + (int32_t)bootblock_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800264 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800265 header_offset = size + (int32_t)header_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800266
267 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
268 "header=0x%x, entries_offset=0x%x\n",
269 bootblock_offset, header_offset, entries_offset);
270
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800271 // Prepare bootblock
272 if (bootblock_offset + bootblock->size > size) {
273 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
274 bootblock_offset, bootblock->size, size);
275 return -1;
276 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800277 if (entries_offset > bootblock_offset &&
278 entries_offset < bootblock->size) {
279 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
280 bootblock_offset, bootblock->size, entries_offset);
281 return -1;
282 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800283 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
284 bootblock->size);
285
286 // Prepare header
Sol Boucher5bad3952015-05-05 20:35:26 -0700287 if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800288 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Sol Boucher5bad3952015-05-05 20:35:26 -0700289 header_offset, sizeof(image->header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800290 return -1;
291 }
Sol Boucher3e060ed2015-05-05 15:40:15 -0700292 image->header.magic = CBFS_HEADER_MAGIC;
293 image->header.version = CBFS_HEADER_VERSION;
294 image->header.romsize = size;
295 image->header.bootblocksize = bootblock->size;
296 image->header.align = align;
297 image->header.offset = entries_offset;
298 image->header.architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600299
300 header_loc = (image->buffer.data + header_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700301 cbfs_put_header(header_loc, &image->header);
Sol Boucher67a0a862015-03-18 12:36:27 -0700302 image->has_header = true;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800303
Julius Wernerefcee762014-11-10 13:14:24 -0800304 // The last 4 byte of the image contain the relative offset from the end
305 // of the image to the master header as a 32-bit signed integer. x86
306 // relies on this also being its (memory-mapped, top-aligned) absolute
307 // 32-bit address by virtue of how two's complement numbers work.
308 assert(size % sizeof(int32_t) == 0);
309 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
310 *rel_offset = header_offset - size;
311
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800312 // Prepare entries
313 if (align_up(entries_offset, align) != entries_offset) {
314 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
315 entries_offset, align);
316 return -1;
317 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800318 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800319 // e = min(bootblock, header, rel_offset) where e > entries_offset.
320 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800321 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
322 cbfs_len = bootblock_offset;
323 if (header_offset > entries_offset && header_offset < cbfs_len)
324 cbfs_len = header_offset;
Sol Boucher67a0a862015-03-18 12:36:27 -0700325
326 if (cbfs_image_create(image, cbfs_len - entries_offset))
327 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800328 return 0;
329}
330
Sol Bouchere3260a02015-03-25 13:40:08 -0700331int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
332 uint32_t offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700333{
Sol Bouchere3260a02015-03-25 13:40:08 -0700334 assert(out);
335 assert(in);
336 assert(in->data);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600337
Sol Bouchere3260a02015-03-25 13:40:08 -0700338 buffer_clone(&out->buffer, in);
Sol Boucher67a0a862015-03-18 12:36:27 -0700339 out->has_header = false;
340
Sol Bouchere3260a02015-03-25 13:40:08 -0700341 void *header_loc = cbfs_find_header(in->data, in->size, offset);
342 if (header_loc) {
343 cbfs_get_header(&out->header, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700344 out->has_header = true;
Sol Bouchere3260a02015-03-25 13:40:08 -0700345 cbfs_fix_legacy_size(out, header_loc);
Sol Boucher67a0a862015-03-18 12:36:27 -0700346 } else if (offset != ~0u) {
347 ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n");
348 return 1;
349 } else if (!cbfs_is_valid_cbfs(out)) {
350 ERROR("Selected image region is not a valid CBFS.\n");
351 return 1;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800352 }
Sol Boucher67a0a862015-03-18 12:36:27 -0700353
354 return 0;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800355}
356
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800357int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
358 size_t copy_size)
359{
Sol Boucher67a0a862015-03-18 12:36:27 -0700360 assert(image);
361 if (!cbfs_is_legacy_cbfs(image))
362 return -1;
363
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800364 struct cbfs_file *src_entry, *dst_entry;
365 struct cbfs_header *copy_header;
366 size_t align, entry_offset;
367 ssize_t last_entry_size;
368
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800369 size_t cbfs_offset, cbfs_end;
370 size_t copy_end = copy_offset + copy_size;
371
Sol Boucher3e060ed2015-05-05 15:40:15 -0700372 align = image->header.align;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800373
Sol Boucher3e060ed2015-05-05 15:40:15 -0700374 cbfs_offset = image->header.offset;
375 cbfs_end = image->header.romsize;
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800376
377 if (copy_end > image->buffer.size) {
378 ERROR("Copy offset out of range: [%zx:%zx)\n",
379 copy_offset, copy_end);
380 return 1;
381 }
382
Sol Boucher297c88c2015-05-05 15:35:18 -0700383 /* Range check requested copy region with source cbfs. */
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800384 if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) ||
385 (copy_end >= cbfs_offset && copy_end <= cbfs_end)) {
386 ERROR("New image would overlap old one.\n");
387 return 1;
388 }
389
390 /* This will work, let's create a copy. */
391 copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700392 cbfs_put_header(copy_header, &image->header);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800393
394 copy_header->bootblocksize = 0;
395 /* Romsize is a misnomer. It's the absolute limit of cbfs content.*/
396 copy_header->romsize = htonl(copy_end);
397 entry_offset = align_up(copy_offset + sizeof(*copy_header), align);
398 copy_header->offset = htonl(entry_offset);
399 dst_entry = (struct cbfs_file *)(image->buffer.data + entry_offset);
400
401 /* Copy non-empty files */
402 for (src_entry = cbfs_find_first_entry(image);
403 src_entry && cbfs_is_valid_entry(image, src_entry);
404 src_entry = cbfs_find_next_entry(image, src_entry)) {
405 size_t entry_size;
406
407 if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) ||
408 (src_entry->type == htonl(CBFS_COMPONENT_DELETED)))
409 continue;
410
411 entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
412 memcpy(dst_entry, src_entry, entry_size);
413 dst_entry = (struct cbfs_file *)(
414 (uintptr_t)dst_entry + align_up(entry_size, align));
415
Sol Boucher0e539312015-03-05 15:38:03 -0800416 if ((size_t)((char *)dst_entry - image->buffer.data) >=
417 copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800418 ERROR("Ran out of room in copy region.\n");
419 return 1;
420 }
421 }
422
423 /* Last entry size is all the room above it. */
424 last_entry_size = copy_end - ((char *)dst_entry - image->buffer.data)
425 - cbfs_calculate_file_header_size("");
426
427 if (last_entry_size < 0)
428 WARN("No room to create the last entry!\n")
429 else
Vadim Bendebury45e59972014-12-23 15:59:57 -0800430 cbfs_create_empty_entry(dst_entry, last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800431
432 return 0;
433}
434
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700435int cbfs_image_delete(struct cbfs_image *image)
436{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100437 if (image == NULL)
438 return 0;
439
Hung-Te Lineab2c812013-01-29 01:56:17 +0800440 buffer_delete(&image->buffer);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800441 return 0;
442}
443
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800444/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
445static int cbfs_add_entry_at(struct cbfs_image *image,
446 struct cbfs_file *entry,
447 uint32_t size,
448 const char *name,
449 uint32_t type,
450 const void *data,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700451 uint32_t content_offset)
452{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800453 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
454 uint32_t addr = cbfs_get_entry_addr(image, entry),
455 addr_next = cbfs_get_entry_addr(image, next);
456 uint32_t header_size = cbfs_calculate_file_header_size(name),
457 min_entry_size = cbfs_calculate_file_header_size("");
458 uint32_t len, target;
Sol Boucher67a0a862015-03-18 12:36:27 -0700459 uint32_t align = image->has_header ? image->header.align :
460 CBFS_ENTRY_ALIGNMENT;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800461
462 target = content_offset - header_size;
463 if (target % align)
464 target -= target % align;
465 if (target < addr) {
466 ERROR("No space to hold cbfs_file header.");
467 return -1;
468 }
469
470 // Process buffer BEFORE content_offset.
471 if (target - addr > min_entry_size) {
472 DEBUG("|min|...|header|content|... <create new entry>\n");
473 len = target - addr - min_entry_size;
Vadim Bendebury45e59972014-12-23 15:59:57 -0800474 cbfs_create_empty_entry(entry, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800475 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
476 entry = cbfs_find_next_entry(image, entry);
477 addr = cbfs_get_entry_addr(image, entry);
478 }
479
480 len = size + (content_offset - addr - header_size);
Vadim Bendebury45e59972014-12-23 15:59:57 -0800481 cbfs_create_empty_entry(entry, len, name);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800482 if (len != size) {
483 DEBUG("|..|header|content|... <use offset to create entry>\n");
484 DEBUG("before: offset=0x%x, len=0x%x\n",
485 ntohl(entry->offset), ntohl(entry->len));
486 // TODO reset expanded name buffer to 0xFF.
487 entry->offset = htonl(ntohl(entry->offset) + (len - size));
488 entry->len = htonl(size);
489 DEBUG("after: offset=0x%x, len=0x%x\n",
490 ntohl(entry->offset), ntohl(entry->len));
491 }
492
493 // Ready to fill data into entry.
494 assert(ntohl(entry->len) == size);
495 entry->type = htonl(type);
496 DEBUG("content_offset: 0x%x, entry location: %x\n",
497 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
498 image->buffer.data));
499 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
Patrick Georgicccc9d42015-04-28 13:09:36 +0200500 (ptrdiff_t)content_offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800501 memcpy(CBFS_SUBHEADER(entry), data, size);
502 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
503
504 // Process buffer AFTER entry.
505 entry = cbfs_find_next_entry(image, entry);
506 addr = cbfs_get_entry_addr(image, entry);
Sol Boucher05725652015-04-02 20:58:26 -0700507 if (addr == addr_next)
508 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800509
Sol Boucher05725652015-04-02 20:58:26 -0700510 assert(addr < addr_next);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800511 if (addr_next - addr < min_entry_size) {
Sol Boucher636cc852015-04-03 09:13:04 -0700512 DEBUG("No need for new \"empty\" entry\n");
513 /* No need to increase the size of the just
514 * stored file to extend to next file. Alignment
515 * of next file takes care of this.
516 */
517 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800518 }
519
520 len = addr_next - addr - min_entry_size;
Vadim Bendebury45e59972014-12-23 15:59:57 -0800521 cbfs_create_empty_entry(entry, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800522 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
523 return 0;
524}
525
526int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700527 const char *name, uint32_t type, uint32_t content_offset)
528{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800529 uint32_t entry_type;
530 uint32_t addr, addr_next;
531 struct cbfs_file *entry, *next;
532 uint32_t header_size, need_size, new_size;
533
534 header_size = cbfs_calculate_file_header_size(name);
535
536 need_size = header_size + buffer->size;
537 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
538 name, content_offset, header_size, buffer->size, need_size);
539
540 if (IS_TOP_ALIGNED_ADDRESS(content_offset)) {
Sol Boucher67a0a862015-03-18 12:36:27 -0700541 if (!cbfs_is_legacy_cbfs(image)) {
542 ERROR("Top-aligned offsets are only supported for legacy CBFSes (with master headers)\n");
543 return -1;
544 }
545
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800546 // legacy cbfstool takes top-aligned address.
Sol Boucher3e060ed2015-05-05 15:40:15 -0700547 uint32_t theromsize = image->header.romsize;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800548 INFO("Converting top-aligned address 0x%x to offset: 0x%x\n",
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800549 content_offset, content_offset + theromsize);
Sol Boucher0e539312015-03-05 15:38:03 -0800550 content_offset = theromsize + (int32_t)content_offset;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800551 }
552
553 // Merge empty entries.
554 DEBUG("(trying to merge empty entries...)\n");
555 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
556
557 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800558 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800559 entry = cbfs_find_next_entry(image, entry)) {
560
561 entry_type = ntohl(entry->type);
562 if (entry_type != CBFS_COMPONENT_NULL)
563 continue;
564
565 addr = cbfs_get_entry_addr(image, entry);
566 next = cbfs_find_next_entry(image, entry);
567 addr_next = cbfs_get_entry_addr(image, next);
568
569 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
570 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600571
572 /* Will the file fit? Don't yet worry if we have space for a new
573 * "empty" entry. We take care of that later.
574 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800575 if (addr + need_size > addr_next)
576 continue;
577
578 // Can we simply put object here?
579 if (!content_offset || content_offset == addr + header_size) {
580 DEBUG("Filling new entry data (%zd bytes).\n",
581 buffer->size);
Vadim Bendebury45e59972014-12-23 15:59:57 -0800582 cbfs_create_empty_entry(entry, buffer->size, name);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800583 entry->type = htonl(type);
584 memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
585 if (verbose)
586 cbfs_print_entry_info(image, entry, stderr);
587
588 // setup new entry
Paul Menzel4159a802013-07-14 00:24:31 +0200589 DEBUG("Setting new empty entry.\n");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800590 entry = cbfs_find_next_entry(image, entry);
591 new_size = (cbfs_get_entry_addr(image, next) -
592 cbfs_get_entry_addr(image, entry));
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600593
594 /* Entry was added and no space for new "empty" entry */
595 if (new_size < cbfs_calculate_file_header_size("")) {
596 DEBUG("No need for new \"empty\" entry\n");
597 /* No need to increase the size of the just
598 * stored file to extend to next file. Alignment
599 * of next file takes care of this.
600 */
601 return 0;
602 }
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800603 new_size -= cbfs_calculate_file_header_size("");
604 DEBUG("new size: %d\n", new_size);
Vadim Bendebury45e59972014-12-23 15:59:57 -0800605 cbfs_create_empty_entry(entry, new_size, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800606 if (verbose)
607 cbfs_print_entry_info(image, entry, stderr);
608 return 0;
609 }
610
611 // We need to put content here, and the case is really
612 // complicated...
613 assert(content_offset);
614 if (addr_next < content_offset) {
615 DEBUG("Not for specified offset yet");
616 continue;
617 } else if (addr > content_offset) {
618 DEBUG("Exceed specified content_offset.");
619 break;
620 } else if (addr + header_size > content_offset) {
621 ERROR("Not enough space for header.\n");
622 break;
623 } else if (content_offset + buffer->size > addr_next) {
624 ERROR("Not enough space for content.\n");
625 break;
626 }
627
628 // TODO there are more few tricky cases that we may
629 // want to fit by altering offset.
630 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
631 addr, addr_next - addr, content_offset);
632
633 if (cbfs_add_entry_at(image, entry, buffer->size, name, type,
634 buffer->data, content_offset) == 0) {
635 return 0;
636 }
637 break;
638 }
639
640 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
641 buffer->name, buffer->size, buffer->size / 1024, content_offset);
642 return -1;
643}
644
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700645struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
646{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800647 struct cbfs_file *entry;
648 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800649 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800650 entry = cbfs_find_next_entry(image, entry)) {
651 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
652 DEBUG("cbfs_get_entry: found %s\n", name);
653 return entry;
654 }
655 }
656 return NULL;
657}
658
659int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700660 const char *filename)
661{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800662 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
663 struct buffer buffer;
664 if (!entry) {
665 ERROR("File not found: %s\n", entry_name);
666 return -1;
667 }
668 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
669 entry_name, cbfs_get_entry_addr(image, entry),
670 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
671
672 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
673 WARN("Only 'raw' files are safe to extract.\n");
674 }
675
676 buffer.data = CBFS_SUBHEADER(entry);
677 buffer.size = ntohl(entry->len);
Sol Boucher0e539312015-03-05 15:38:03 -0800678 buffer.name = strdup("(cbfs_export_entry)");
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800679 if (buffer_write_file(&buffer, filename) != 0) {
680 ERROR("Failed to write %s into %s.\n",
681 entry_name, filename);
Sol Boucher0e539312015-03-05 15:38:03 -0800682 free(buffer.name);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800683 return -1;
684 }
Sol Boucher0e539312015-03-05 15:38:03 -0800685 free(buffer.name);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800686 INFO("Successfully dumped the file to: %s\n", filename);
687 return 0;
688}
689
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700690int cbfs_remove_entry(struct cbfs_image *image, const char *name)
691{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800692 struct cbfs_file *entry, *next;
693 size_t len;
694 entry = cbfs_get_entry(image, name);
695 if (!entry) {
696 ERROR("CBFS file %s not found.\n", name);
697 return -1;
698 }
699 next = cbfs_find_next_entry(image, entry);
700 assert(next);
701 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
702 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
703 entry->type = htonl(CBFS_COMPONENT_DELETED);
704 len = (cbfs_get_entry_addr(image, next) -
705 cbfs_get_entry_addr(image, entry));
706 entry->offset = htonl(cbfs_calculate_file_header_size(""));
707 entry->len = htonl(len - ntohl(entry->offset));
708 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
709 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
710 ntohl(entry->len));
711 return 0;
712}
713
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700714int cbfs_print_header_info(struct cbfs_image *image)
715{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800716 char *name = strdup(image->buffer.name);
Sol Boucher3e060ed2015-05-05 15:40:15 -0700717 assert(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800718 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800719 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800720 basename(name),
721 image->buffer.size / 1024,
Sol Boucher3e060ed2015-05-05 15:40:15 -0700722 image->header.bootblocksize,
723 image->header.romsize,
724 image->header.offset,
725 image->header.align,
726 arch_to_string(image->header.architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800727 free(name);
728 return 0;
729}
730
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700731static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
732{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800733 fprintf(fp,
734 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
735 "length: %d/%d\n",
736 lookup_name_by_type(types_cbfs_compression,
737 stage->compression, "(unknown)"),
738 stage->entry,
739 stage->load,
740 stage->len,
741 stage->memlen);
742 return 0;
743}
744
Hung-Te Lin0780d672014-05-16 10:14:05 +0800745static int cbfs_print_decoded_payload_segment_info(
746 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800747{
Hung-Te Lin0780d672014-05-16 10:14:05 +0800748 /* The input (seg) must be already decoded by
749 * cbfs_decode_payload_segment.
750 */
751 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800752 case PAYLOAD_SEGMENT_CODE:
753 case PAYLOAD_SEGMENT_DATA:
754 fprintf(fp, " %s (%s compression, offset: 0x%x, "
755 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800756 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800757 "code " : "data"),
758 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800759 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800760 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800761 seg->offset, seg->load_addr, seg->len,
762 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800763 break;
764
765 case PAYLOAD_SEGMENT_ENTRY:
766 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800767 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800768 break;
769
770 case PAYLOAD_SEGMENT_BSS:
771 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
772 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800773 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800774 break;
775
776 case PAYLOAD_SEGMENT_PARAMS:
777 fprintf(fp, " parameters\n");
778 break;
779
780 default:
781 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
782 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800783 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800784 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800785 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800786 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800787 seg->offset, seg->load_addr, seg->len,
788 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800789 break;
790 }
791 return 0;
792}
793
794int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700795 void *arg)
796{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800797 const char *name = CBFS_NAME(entry);
798 struct cbfs_payload_segment *payload;
799 FILE *fp = (FILE *)arg;
800
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800801 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800802 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
803 cbfs_get_entry_addr(image, entry));
804 return -1;
805 }
806 if (!fp)
807 fp = stdout;
808
809 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
810 *name ? name : "(empty)",
811 cbfs_get_entry_addr(image, entry),
812 get_cbfs_entry_type_name(ntohl(entry->type)),
813 ntohl(entry->len));
814
815 if (!verbose)
816 return 0;
817
818 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
819 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
820 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
821 ntohl(entry->len));
822
823 /* note the components of the subheader may be in host order ... */
824 switch (ntohl(entry->type)) {
825 case CBFS_COMPONENT_STAGE:
826 cbfs_print_stage_info((struct cbfs_stage *)
827 CBFS_SUBHEADER(entry), fp);
828 break;
829
830 case CBFS_COMPONENT_PAYLOAD:
831 payload = (struct cbfs_payload_segment *)
832 CBFS_SUBHEADER(entry);
833 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +0800834 struct cbfs_payload_segment seg;
835 cbfs_decode_payload_segment(&seg, payload);
836 cbfs_print_decoded_payload_segment_info(
837 &seg, fp);
838 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800839 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +0800840 else
Aaron Durbinca630272014-08-05 10:48:20 -0500841 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800842 }
843 break;
844 default:
845 break;
846 }
847 return 0;
848}
849
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700850int cbfs_print_directory(struct cbfs_image *image)
851{
Sol Boucher67a0a862015-03-18 12:36:27 -0700852 if (cbfs_is_legacy_cbfs(image))
853 cbfs_print_header_info(image);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800854 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
855 cbfs_walk(image, cbfs_print_entry_info, NULL);
856 return 0;
857}
858
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800859int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -0800860 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700861{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800862 struct cbfs_file *next;
863 uint32_t type, addr, last_addr;
864
865 type = ntohl(entry->type);
866 if (type == CBFS_COMPONENT_DELETED) {
867 // Ready to be recycled.
868 type = CBFS_COMPONENT_NULL;
869 entry->type = htonl(type);
870 }
871 if (type != CBFS_COMPONENT_NULL)
872 return 0;
873
874 next = cbfs_find_next_entry(image, entry);
875
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800876 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800877 type = ntohl(next->type);
878 if (type == CBFS_COMPONENT_DELETED) {
879 type = CBFS_COMPONENT_NULL;
880 next->type = htonl(type);
881 }
882 if (type != CBFS_COMPONENT_NULL)
883 return 0;
884
885 addr = cbfs_get_entry_addr(image, entry);
886 last_addr = cbfs_get_entry_addr(
887 image, cbfs_find_next_entry(image, next));
888
889 // Now, we find two deleted/empty entries; try to merge now.
890 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
891 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
892 cbfs_get_entry_addr(image, next), ntohl(next->len));
Vadim Bendebury45e59972014-12-23 15:59:57 -0800893 cbfs_create_empty_entry(entry,
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800894 (last_addr - addr -
895 cbfs_calculate_file_header_size("")),
896 "");
897 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
898 next = cbfs_find_next_entry(image, entry);
899 }
900 return 0;
901}
902
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800903int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700904 void *arg)
905{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800906 int count = 0;
907 struct cbfs_file *entry;
908 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800909 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800910 entry = cbfs_find_next_entry(image, entry)) {
911 count ++;
912 if (callback(image, entry, arg) != 0)
913 break;
914 }
915 return count;
916}
917
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800918static int cbfs_header_valid(struct cbfs_header *header, size_t size)
919{
920 if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
921 ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
922 (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
923 (ntohl(header->romsize) <= size) &&
924 (ntohl(header->offset) < ntohl(header->romsize)))
925 return 1;
926 return 0;
927}
928
929struct cbfs_header *cbfs_find_header(char *data, size_t size,
930 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700931{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800932 size_t offset;
933 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800934 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800935 struct cbfs_header *header, *result = NULL;
936
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800937 if (forced_offset < (size - sizeof(struct cbfs_header))) {
938 /* Check if the forced header is valid. */
939 header = (struct cbfs_header *)(data + forced_offset);
940 if (cbfs_header_valid(header, size))
941 return header;
942 return NULL;
943 }
944
Julius Wernerefcee762014-11-10 13:14:24 -0800945 // Try finding relative offset of master header at end of file first.
946 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
947 offset = size + rel_offset;
948 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
949 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800950
Hung-Te Lineab2c812013-01-29 01:56:17 +0800951 if (offset >= size - sizeof(*header) ||
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800952 !cbfs_header_valid((struct cbfs_header *)(data + offset), size)) {
Julius Wernerefcee762014-11-10 13:14:24 -0800953 // Some use cases append non-CBFS data to the end of the ROM.
954 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +0800955 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800956 }
Hung-Te Lineab2c812013-01-29 01:56:17 +0800957
958 for (; offset + sizeof(*header) < size; offset++) {
959 header = (struct cbfs_header *)(data + offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800960 if (!cbfs_header_valid(header, size))
Hung-Te Lineab2c812013-01-29 01:56:17 +0800961 continue;
Julius Wernerefcee762014-11-10 13:14:24 -0800962 if (!found++)
963 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800964 }
Julius Wernerefcee762014-11-10 13:14:24 -0800965 if (found > 1)
966 // Top-aligned images usually have a working relative offset
967 // field, so this is more likely to happen on bottom-aligned
968 // ones (where the first header is the "outermost" one)
969 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +0800970 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800971 return result;
972}
973
974
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700975struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
976{
Sol Boucher3e060ed2015-05-05 15:40:15 -0700977 assert(image);
Sol Boucher67a0a862015-03-18 12:36:27 -0700978 return image->has_header ? (struct cbfs_file *)(image->buffer.data +
979 image->header.offset) :
980 (struct cbfs_file *)image->buffer.data;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800981}
982
983struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700984 struct cbfs_file *entry)
985{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800986 uint32_t addr = cbfs_get_entry_addr(image, entry);
Sol Boucher67a0a862015-03-18 12:36:27 -0700987 int align = image->has_header ? image->header.align :
988 CBFS_ENTRY_ALIGNMENT;
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800989 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +0800990 addr += ntohl(entry->offset) + ntohl(entry->len);
991 addr = align_up(addr, align);
992 return (struct cbfs_file *)(image->buffer.data + addr);
993}
994
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700995uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
996{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800997 assert(image && image->buffer.data && entry);
998 return (int32_t)((char *)entry - image->buffer.data);
999}
1000
Sol Boucher67a0a862015-03-18 12:36:27 -07001001int cbfs_is_valid_cbfs(struct cbfs_image *image)
1002{
1003 return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC,
1004 strlen(CBFS_FILE_MAGIC));
1005}
1006
1007int cbfs_is_legacy_cbfs(struct cbfs_image *image)
1008{
1009 return image->has_header;
1010}
1011
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001012int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
1013{
Sol Bouchere3260a02015-03-25 13:40:08 -07001014 uint32_t offset = cbfs_get_entry_addr(image, entry);
1015
1016 if (offset >= image->buffer.size)
1017 return 0;
1018
1019 struct buffer entry_data;
1020 buffer_clone(&entry_data, &image->buffer);
1021 buffer_seek(&entry_data, offset);
1022 return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC,
Sol Boucher67a0a862015-03-18 12:36:27 -07001023 strlen(CBFS_FILE_MAGIC));
Hung-Te Lineab2c812013-01-29 01:56:17 +08001024}
1025
Vadim Bendebury45e59972014-12-23 15:59:57 -08001026int cbfs_create_empty_entry(struct cbfs_file *entry,
1027 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001028{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001029 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
1030 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
1031 entry->type = htonl(CBFS_COMPONENT_NULL);
1032 entry->len = htonl(len);
1033 entry->checksum = 0; // TODO Build a checksum algorithm.
1034 entry->offset = htonl(cbfs_calculate_file_header_size(name));
1035 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
1036 strcpy(CBFS_NAME(entry), name);
1037 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
1038 return 0;
1039}
1040
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001041/* Finds a place to hold whole data in same memory page. */
1042static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
1043{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001044 if (!page)
1045 return 1;
1046 return (start / page) == (start + size - 1) / page;
1047}
1048
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001049/* Tests if data can fit in a range by given offset:
1050 * start ->| header_len | offset (+ size) |<- end
1051 */
1052static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001053 uint32_t offset, uint32_t size)
1054{
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001055 return (offset >= start + header_len && offset + size <= end);
1056}
1057
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001058int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -07001059 uint32_t size, uint32_t page_size, uint32_t align)
1060{
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001061 struct cbfs_file *entry;
1062 size_t need_len;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001063 uint32_t addr, addr_next, addr2, addr3, offset, header_len;
1064
1065 /* Default values: allow fitting anywhere in ROM. */
1066 if (!page_size)
Sol Boucher67a0a862015-03-18 12:36:27 -07001067 page_size = image->has_header ? image->header.romsize :
1068 image->buffer.size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001069 if (!align)
1070 align = 1;
1071
1072 if (size > page_size)
1073 ERROR("Input file size (%d) greater than page size (%d).\n",
1074 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001075
Sol Boucher67a0a862015-03-18 12:36:27 -07001076 uint32_t image_align = image->has_header ? image->header.align :
1077 CBFS_ENTRY_ALIGNMENT;
1078 if (page_size % image_align)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001079 WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001080 __func__, page_size, image_align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001081
1082 /* TODO Old cbfstool always assume input is a stage file (and adding
1083 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001084 * (type) param in future. For right now, we assume cbfs_stage is the
1085 * largest structure and add it into header size. */
1086 assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001087 header_len = (cbfs_calculate_file_header_size(name) +
1088 sizeof(struct cbfs_stage));
1089 need_len = header_len + size;
1090
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001091 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001092 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
1093
1094 /* Three cases of content location on memory page:
1095 * case 1.
1096 * | PAGE 1 | PAGE 2 |
1097 * | <header><content>| Fit. Return start of content.
1098 *
1099 * case 2.
1100 * | PAGE 1 | PAGE 2 |
1101 * | <header><content> | Fits when we shift content to align
1102 * shift-> | <header>|<content> | at starting of PAGE 2.
1103 *
1104 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001105 * | PAGE 1 | PAGE 2 | PAGE 3 |
1106 * | <header>< content > | Can't fit. If we shift content to
1107 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1108 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001109 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001110 * The returned address can be then used as "base-address" (-b) in add-*
1111 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1112 * For stage targets, the address is also used to re-link stage before
1113 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001114 */
1115 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001116 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001117 entry = cbfs_find_next_entry(image, entry)) {
1118
1119 uint32_t type = ntohl(entry->type);
1120 if (type != CBFS_COMPONENT_NULL)
1121 continue;
1122
1123 addr = cbfs_get_entry_addr(image, entry);
1124 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
1125 image, entry));
1126 if (addr_next - addr < need_len)
1127 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001128
1129 offset = align_up(addr + header_len, align);
1130 if (is_in_same_page(offset, size, page_size) &&
1131 is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001132 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001133 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001134 }
1135
1136 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001137 offset = align_up(addr2, align);
1138 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001139 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001140 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001141 }
1142
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001143 /* Assume page_size >= header_len so adding one page will
1144 * definitely provide the space for header. */
1145 assert(page_size >= header_len);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001146 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001147 offset = align_up(addr3, align);
1148 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001149 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001150 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001151 }
1152 }
1153 return -1;
1154}