blob: 2581bef3f4aa91a994a6e29b6664164c2f0a9401 [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
31/* The file name align is not defined in CBFS spec -- only a preference by
32 * (old) cbfstool. */
33#define CBFS_FILENAME_ALIGN (16)
34
35/* To make CBFS more friendly to ROM, fill -1 (0xFF) instead of zero. */
36#define CBFS_CONTENT_DEFAULT_VALUE (-1)
37
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080038/* Type and format */
39
40struct typedesc_t {
41 uint32_t type;
42 const char *name;
43};
44
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070045static const struct typedesc_t types_cbfs_entry[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080046 {CBFS_COMPONENT_STAGE, "stage"},
47 {CBFS_COMPONENT_PAYLOAD, "payload"},
48 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
49 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
50 {CBFS_COMPONENT_RAW, "raw"},
51 {CBFS_COMPONENT_VSA, "vsa"},
52 {CBFS_COMPONENT_MBI, "mbi"},
53 {CBFS_COMPONENT_MICROCODE, "microcode"},
Martin Rothdde307c2015-03-24 15:54:20 -060054 {CBFS_COMPONENT_FSP, "fsp"},
55 {CBFS_COMPONENT_MRC, "mrc"},
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080056 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos_default"},
57 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos_layout"},
Martin Rothdde307c2015-03-24 15:54:20 -060058 {CBFS_COMPONENT_SPD, "spd"},
59 {CBFS_COMPONENT_MRC_CACHE, "mrc_cache"},
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080060 {CBFS_COMPONENT_DELETED, "deleted"},
61 {CBFS_COMPONENT_NULL, "null"},
62 {0, NULL},
63};
64
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070065static const struct typedesc_t types_cbfs_compression[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080066 {CBFS_COMPRESS_NONE, "none"},
67 {CBFS_COMPRESS_LZMA, "LZMA"},
68 {0, NULL},
69};
70
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080071static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070072 const char *default_value)
73{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080074 int i;
75 for (i = 0; desc[i].name; i++)
76 if (desc[i].type == type)
77 return desc[i].name;
78 return default_value;
79}
80
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010081static const char *get_cbfs_entry_type_name(uint32_t type)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070082{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080083 return lookup_name_by_type(types_cbfs_entry, type, "(unknown)");
84}
85
Hung-Te Linc03d9b02013-01-29 02:38:40 +080086/* CBFS image */
87
Sol Boucher0e539312015-03-05 15:38:03 -080088static size_t cbfs_calculate_file_header_size(const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070089{
Hung-Te Linc03d9b02013-01-29 02:38:40 +080090 return (sizeof(struct cbfs_file) +
91 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
92}
93
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -060094static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070095{
Hung-Te Lin49fcd752013-01-29 03:16:20 +080096 // A bug in old cbfstool may produce extra few bytes (by alignment) and
97 // cause cbfstool to overwrite things after free space -- which is
98 // usually CBFS header on x86. We need to workaround that.
99
100 struct cbfs_file *entry, *first = NULL, *last = NULL;
101 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800102 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800103 entry = cbfs_find_next_entry(image, entry)) {
104 last = entry;
105 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600106 if ((char *)first < (char *)hdr_loc &&
107 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800108 WARN("CBFS image was created with old cbfstool with size bug. "
109 "Fixing size in last entry...\n");
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600110 last->len = htonl(ntohl(last->len) - image->header->align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800111 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
112 cbfs_get_entry_addr(image, entry),
113 cbfs_get_entry_addr(image,
114 cbfs_find_next_entry(image, last)));
115 }
116 return 0;
117}
118
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800119void cbfs_put_header(void *dest, const struct cbfs_header *header)
120{
121 struct buffer outheader;
122
123 outheader.data = dest;
124 outheader.size = 0;
125
126 xdr_be.put32(&outheader, header->magic);
127 xdr_be.put32(&outheader, header->version);
128 xdr_be.put32(&outheader, header->romsize);
129 xdr_be.put32(&outheader, header->bootblocksize);
130 xdr_be.put32(&outheader, header->align);
131 xdr_be.put32(&outheader, header->offset);
132 xdr_be.put32(&outheader, header->architecture);
133}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600134
Hung-Te Lin0780d672014-05-16 10:14:05 +0800135static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
136 struct cbfs_payload_segment *input)
137{
138 struct buffer seg = {
139 .data = (void *)input,
140 .size = sizeof(*input),
141 };
142 output->type = xdr_be.get32(&seg);
143 output->compression = xdr_be.get32(&seg);
144 output->offset = xdr_be.get32(&seg);
145 output->load_addr = xdr_be.get64(&seg);
146 output->len = xdr_be.get32(&seg);
147 output->mem_len = xdr_be.get32(&seg);
148 assert(seg.size == 0);
149}
150
Sol Boucher0e539312015-03-05 15:38:03 -0800151void cbfs_get_header(struct cbfs_header *header, void *src)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600152{
153 struct buffer outheader;
154
Sol Boucher0e539312015-03-05 15:38:03 -0800155 outheader.data = src; /* We're not modifying the data */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600156 outheader.size = 0;
157
158 header->magic = xdr_be.get32(&outheader);
159 header->version = xdr_be.get32(&outheader);
160 header->romsize = xdr_be.get32(&outheader);
161 header->bootblocksize = xdr_be.get32(&outheader);
162 header->align = xdr_be.get32(&outheader);
163 header->offset = xdr_be.get32(&outheader);
164 header->architecture = xdr_be.get32(&outheader);
165}
166
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800167int cbfs_image_create(struct cbfs_image *image,
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100168 uint32_t architecture,
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800169 size_t size,
170 uint32_t align,
171 struct buffer *bootblock,
Sol Boucher0e539312015-03-05 15:38:03 -0800172 uint32_t bootblock_offset,
173 uint32_t header_offset,
174 uint32_t entries_offset)
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800175{
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800176 struct cbfs_header header;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800177 struct cbfs_file *entry;
Julius Wernerefcee762014-11-10 13:14:24 -0800178 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800179 uint32_t cbfs_len;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800180 size_t entry_header_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600181 void *header_loc;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800182
183 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
184 "header=0x%x+0x%zx, entries_offset=0x%x\n",
185 bootblock_offset, bootblock->size,
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800186 header_offset, sizeof(header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800187
188 if (buffer_create(&image->buffer, size, "(new)") != 0)
189 return -1;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600190 if ((image->header = malloc(sizeof(*image->header))) == NULL)
191 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800192 memset(image->buffer.data, CBFS_CONTENT_DEFAULT_VALUE, size);
193
194 // Adjust legcay top-aligned address to ROM offset.
195 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800196 entries_offset = size + (int32_t)entries_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800197 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800198 bootblock_offset = size + (int32_t)bootblock_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800199 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800200 header_offset = size + (int32_t)header_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800201
202 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
203 "header=0x%x, entries_offset=0x%x\n",
204 bootblock_offset, header_offset, entries_offset);
205
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800206 // Prepare bootblock
207 if (bootblock_offset + bootblock->size > size) {
208 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
209 bootblock_offset, bootblock->size, size);
210 return -1;
211 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800212 if (entries_offset > bootblock_offset &&
213 entries_offset < bootblock->size) {
214 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
215 bootblock_offset, bootblock->size, entries_offset);
216 return -1;
217 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800218 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
219 bootblock->size);
220
221 // Prepare header
Julius Wernerefcee762014-11-10 13:14:24 -0800222 if (header_offset + sizeof(header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800223 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800224 header_offset, sizeof(header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800225 return -1;
226 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600227 image->header->magic = CBFS_HEADER_MAGIC;
228 image->header->version = CBFS_HEADER_VERSION;
229 image->header->romsize = size;
230 image->header->bootblocksize = bootblock->size;
231 image->header->align = align;
232 image->header->offset = entries_offset;
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100233 image->header->architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600234
235 header_loc = (image->buffer.data + header_offset);
236 cbfs_put_header(header_loc, image->header);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800237
Julius Wernerefcee762014-11-10 13:14:24 -0800238 // The last 4 byte of the image contain the relative offset from the end
239 // of the image to the master header as a 32-bit signed integer. x86
240 // relies on this also being its (memory-mapped, top-aligned) absolute
241 // 32-bit address by virtue of how two's complement numbers work.
242 assert(size % sizeof(int32_t) == 0);
243 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
244 *rel_offset = header_offset - size;
245
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800246 // Prepare entries
247 if (align_up(entries_offset, align) != entries_offset) {
248 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
249 entries_offset, align);
250 return -1;
251 }
Hung-Te Linb02c8732013-03-15 17:40:08 +0800252 entry_header_len = cbfs_calculate_file_header_size("");
253 if (entries_offset + entry_header_len > size) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800254 ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n",
Hung-Te Linb02c8732013-03-15 17:40:08 +0800255 entries_offset, entry_header_len, size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800256 return -1;
257 }
258 entry = (struct cbfs_file *)(image->buffer.data + entries_offset);
259 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800260 // e = min(bootblock, header, rel_offset) where e > entries_offset.
261 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800262 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
263 cbfs_len = bootblock_offset;
264 if (header_offset > entries_offset && header_offset < cbfs_len)
265 cbfs_len = header_offset;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800266 cbfs_len -= entries_offset + align + entry_header_len;
Vadim Bendebury45e59972014-12-23 15:59:57 -0800267 cbfs_create_empty_entry(entry, cbfs_len, "");
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800268 LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len);
269 return 0;
270}
271
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800272int cbfs_image_from_file(struct cbfs_image *image,
273 const char *filename, uint32_t offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700274{
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600275 void *header_loc;
276
Hung-Te Lineab2c812013-01-29 01:56:17 +0800277 if (buffer_from_file(&image->buffer, filename) != 0)
278 return -1;
279 DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
280 image->buffer.size);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800281 header_loc = cbfs_find_header(image->buffer.data,
282 image->buffer.size,
283 offset);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600284 if (!header_loc) {
Hung-Te Lineab2c812013-01-29 01:56:17 +0800285 ERROR("%s does not have CBFS master header.\n", filename);
286 cbfs_image_delete(image);
287 return -1;
288 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600289
290 if ((image->header = malloc(sizeof(*image->header))) == NULL)
291 return -1;
292
293 cbfs_get_header(image->header, header_loc);
294 cbfs_fix_legacy_size(image, header_loc);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800295
296 return 0;
297}
298
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800299int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
300 size_t copy_size)
301{
302 struct cbfs_file *src_entry, *dst_entry;
303 struct cbfs_header *copy_header;
304 size_t align, entry_offset;
305 ssize_t last_entry_size;
306
307 size_t header_offset, header_end;
308 size_t cbfs_offset, cbfs_end;
309 size_t copy_end = copy_offset + copy_size;
310
311 align = htonl(image->header->align);
312
313 header_offset = (char *)image->header - image->buffer.data;
314 header_end = header_offset + sizeof(image->header);
315
316 cbfs_offset = htonl(image->header->offset);
317 cbfs_end = htonl(image->header->romsize);
318
319 if (copy_end > image->buffer.size) {
320 ERROR("Copy offset out of range: [%zx:%zx)\n",
321 copy_offset, copy_end);
322 return 1;
323 }
324
325 /* Range check requested copy region with header and source cbfs. */
326 if ((copy_offset >= header_offset && copy_offset < header_end) ||
327 (copy_end >= header_offset && copy_end <= header_end)) {
328 ERROR("New image would overlap old header.\n");
329 }
330
331 if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) ||
332 (copy_end >= cbfs_offset && copy_end <= cbfs_end)) {
333 ERROR("New image would overlap old one.\n");
334 return 1;
335 }
336
337 /* This will work, let's create a copy. */
338 copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset);
339 *copy_header = *image->header;
340
341 copy_header->bootblocksize = 0;
342 /* Romsize is a misnomer. It's the absolute limit of cbfs content.*/
343 copy_header->romsize = htonl(copy_end);
344 entry_offset = align_up(copy_offset + sizeof(*copy_header), align);
345 copy_header->offset = htonl(entry_offset);
346 dst_entry = (struct cbfs_file *)(image->buffer.data + entry_offset);
347
348 /* Copy non-empty files */
349 for (src_entry = cbfs_find_first_entry(image);
350 src_entry && cbfs_is_valid_entry(image, src_entry);
351 src_entry = cbfs_find_next_entry(image, src_entry)) {
352 size_t entry_size;
353
354 if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) ||
355 (src_entry->type == htonl(CBFS_COMPONENT_DELETED)))
356 continue;
357
358 entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
359 memcpy(dst_entry, src_entry, entry_size);
360 dst_entry = (struct cbfs_file *)(
361 (uintptr_t)dst_entry + align_up(entry_size, align));
362
Sol Boucher0e539312015-03-05 15:38:03 -0800363 if ((size_t)((char *)dst_entry - image->buffer.data) >=
364 copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800365 ERROR("Ran out of room in copy region.\n");
366 return 1;
367 }
368 }
369
370 /* Last entry size is all the room above it. */
371 last_entry_size = copy_end - ((char *)dst_entry - image->buffer.data)
372 - cbfs_calculate_file_header_size("");
373
374 if (last_entry_size < 0)
375 WARN("No room to create the last entry!\n")
376 else
Vadim Bendebury45e59972014-12-23 15:59:57 -0800377 cbfs_create_empty_entry(dst_entry, last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800378
379 return 0;
380}
381
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700382int cbfs_image_write_file(struct cbfs_image *image, const char *filename)
383{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800384 assert(image && image->buffer.data);
385 return buffer_write_file(&image->buffer, filename);
386}
387
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700388int cbfs_image_delete(struct cbfs_image *image)
389{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100390 if (image == NULL)
391 return 0;
392
Hung-Te Lineab2c812013-01-29 01:56:17 +0800393 buffer_delete(&image->buffer);
394 image->header = NULL;
395 return 0;
396}
397
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800398/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
399static int cbfs_add_entry_at(struct cbfs_image *image,
400 struct cbfs_file *entry,
401 uint32_t size,
402 const char *name,
403 uint32_t type,
404 const void *data,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700405 uint32_t content_offset)
406{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800407 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
408 uint32_t addr = cbfs_get_entry_addr(image, entry),
409 addr_next = cbfs_get_entry_addr(image, next);
410 uint32_t header_size = cbfs_calculate_file_header_size(name),
411 min_entry_size = cbfs_calculate_file_header_size("");
412 uint32_t len, target;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600413 uint32_t align = image->header->align;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800414
415 target = content_offset - header_size;
416 if (target % align)
417 target -= target % align;
418 if (target < addr) {
419 ERROR("No space to hold cbfs_file header.");
420 return -1;
421 }
422
423 // Process buffer BEFORE content_offset.
424 if (target - addr > min_entry_size) {
425 DEBUG("|min|...|header|content|... <create new entry>\n");
426 len = target - addr - min_entry_size;
Vadim Bendebury45e59972014-12-23 15:59:57 -0800427 cbfs_create_empty_entry(entry, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800428 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
429 entry = cbfs_find_next_entry(image, entry);
430 addr = cbfs_get_entry_addr(image, entry);
431 }
432
433 len = size + (content_offset - addr - header_size);
Vadim Bendebury45e59972014-12-23 15:59:57 -0800434 cbfs_create_empty_entry(entry, len, name);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800435 if (len != size) {
436 DEBUG("|..|header|content|... <use offset to create entry>\n");
437 DEBUG("before: offset=0x%x, len=0x%x\n",
438 ntohl(entry->offset), ntohl(entry->len));
439 // TODO reset expanded name buffer to 0xFF.
440 entry->offset = htonl(ntohl(entry->offset) + (len - size));
441 entry->len = htonl(size);
442 DEBUG("after: offset=0x%x, len=0x%x\n",
443 ntohl(entry->offset), ntohl(entry->len));
444 }
445
446 // Ready to fill data into entry.
447 assert(ntohl(entry->len) == size);
448 entry->type = htonl(type);
449 DEBUG("content_offset: 0x%x, entry location: %x\n",
450 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
451 image->buffer.data));
452 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
Patrick Georgicccc9d42015-04-28 13:09:36 +0200453 (ptrdiff_t)content_offset);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800454 memcpy(CBFS_SUBHEADER(entry), data, size);
455 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
456
457 // Process buffer AFTER entry.
458 entry = cbfs_find_next_entry(image, entry);
459 addr = cbfs_get_entry_addr(image, entry);
Sol Boucher05725652015-04-02 20:58:26 -0700460 if (addr == addr_next)
461 return 0;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800462
Sol Boucher05725652015-04-02 20:58:26 -0700463 assert(addr < addr_next);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800464 if (addr_next - addr < min_entry_size) {
465 DEBUG("No space after content to keep CBFS structure.\n");
466 return -1;
467 }
468
469 len = addr_next - addr - min_entry_size;
Vadim Bendebury45e59972014-12-23 15:59:57 -0800470 cbfs_create_empty_entry(entry, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800471 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
472 return 0;
473}
474
475int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700476 const char *name, uint32_t type, uint32_t content_offset)
477{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800478 uint32_t entry_type;
479 uint32_t addr, addr_next;
480 struct cbfs_file *entry, *next;
481 uint32_t header_size, need_size, new_size;
482
483 header_size = cbfs_calculate_file_header_size(name);
484
485 need_size = header_size + buffer->size;
486 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
487 name, content_offset, header_size, buffer->size, need_size);
488
489 if (IS_TOP_ALIGNED_ADDRESS(content_offset)) {
490 // legacy cbfstool takes top-aligned address.
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600491 uint32_t theromsize = image->header->romsize;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800492 INFO("Converting top-aligned address 0x%x to offset: 0x%x\n",
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800493 content_offset, content_offset + theromsize);
Sol Boucher0e539312015-03-05 15:38:03 -0800494 content_offset = theromsize + (int32_t)content_offset;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800495 }
496
497 // Merge empty entries.
498 DEBUG("(trying to merge empty entries...)\n");
499 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
500
501 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800502 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800503 entry = cbfs_find_next_entry(image, entry)) {
504
505 entry_type = ntohl(entry->type);
506 if (entry_type != CBFS_COMPONENT_NULL)
507 continue;
508
509 addr = cbfs_get_entry_addr(image, entry);
510 next = cbfs_find_next_entry(image, entry);
511 addr_next = cbfs_get_entry_addr(image, next);
512
513 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
514 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600515
516 /* Will the file fit? Don't yet worry if we have space for a new
517 * "empty" entry. We take care of that later.
518 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800519 if (addr + need_size > addr_next)
520 continue;
521
522 // Can we simply put object here?
523 if (!content_offset || content_offset == addr + header_size) {
524 DEBUG("Filling new entry data (%zd bytes).\n",
525 buffer->size);
Vadim Bendebury45e59972014-12-23 15:59:57 -0800526 cbfs_create_empty_entry(entry, buffer->size, name);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800527 entry->type = htonl(type);
528 memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
529 if (verbose)
530 cbfs_print_entry_info(image, entry, stderr);
531
532 // setup new entry
Paul Menzel4159a802013-07-14 00:24:31 +0200533 DEBUG("Setting new empty entry.\n");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800534 entry = cbfs_find_next_entry(image, entry);
535 new_size = (cbfs_get_entry_addr(image, next) -
536 cbfs_get_entry_addr(image, entry));
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600537
538 /* Entry was added and no space for new "empty" entry */
539 if (new_size < cbfs_calculate_file_header_size("")) {
540 DEBUG("No need for new \"empty\" entry\n");
541 /* No need to increase the size of the just
542 * stored file to extend to next file. Alignment
543 * of next file takes care of this.
544 */
545 return 0;
546 }
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800547 new_size -= cbfs_calculate_file_header_size("");
548 DEBUG("new size: %d\n", new_size);
Vadim Bendebury45e59972014-12-23 15:59:57 -0800549 cbfs_create_empty_entry(entry, new_size, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800550 if (verbose)
551 cbfs_print_entry_info(image, entry, stderr);
552 return 0;
553 }
554
555 // We need to put content here, and the case is really
556 // complicated...
557 assert(content_offset);
558 if (addr_next < content_offset) {
559 DEBUG("Not for specified offset yet");
560 continue;
561 } else if (addr > content_offset) {
562 DEBUG("Exceed specified content_offset.");
563 break;
564 } else if (addr + header_size > content_offset) {
565 ERROR("Not enough space for header.\n");
566 break;
567 } else if (content_offset + buffer->size > addr_next) {
568 ERROR("Not enough space for content.\n");
569 break;
570 }
571
572 // TODO there are more few tricky cases that we may
573 // want to fit by altering offset.
574 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
575 addr, addr_next - addr, content_offset);
576
577 if (cbfs_add_entry_at(image, entry, buffer->size, name, type,
578 buffer->data, content_offset) == 0) {
579 return 0;
580 }
581 break;
582 }
583
584 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
585 buffer->name, buffer->size, buffer->size / 1024, content_offset);
586 return -1;
587}
588
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700589struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
590{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800591 struct cbfs_file *entry;
592 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800593 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800594 entry = cbfs_find_next_entry(image, entry)) {
595 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
596 DEBUG("cbfs_get_entry: found %s\n", name);
597 return entry;
598 }
599 }
600 return NULL;
601}
602
603int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700604 const char *filename)
605{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800606 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
607 struct buffer buffer;
608 if (!entry) {
609 ERROR("File not found: %s\n", entry_name);
610 return -1;
611 }
612 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
613 entry_name, cbfs_get_entry_addr(image, entry),
614 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
615
616 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
617 WARN("Only 'raw' files are safe to extract.\n");
618 }
619
620 buffer.data = CBFS_SUBHEADER(entry);
621 buffer.size = ntohl(entry->len);
Sol Boucher0e539312015-03-05 15:38:03 -0800622 buffer.name = strdup("(cbfs_export_entry)");
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800623 if (buffer_write_file(&buffer, filename) != 0) {
624 ERROR("Failed to write %s into %s.\n",
625 entry_name, filename);
Sol Boucher0e539312015-03-05 15:38:03 -0800626 free(buffer.name);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800627 return -1;
628 }
Sol Boucher0e539312015-03-05 15:38:03 -0800629 free(buffer.name);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800630 INFO("Successfully dumped the file to: %s\n", filename);
631 return 0;
632}
633
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700634int cbfs_remove_entry(struct cbfs_image *image, const char *name)
635{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800636 struct cbfs_file *entry, *next;
637 size_t len;
638 entry = cbfs_get_entry(image, name);
639 if (!entry) {
640 ERROR("CBFS file %s not found.\n", name);
641 return -1;
642 }
643 next = cbfs_find_next_entry(image, entry);
644 assert(next);
645 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
646 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
647 entry->type = htonl(CBFS_COMPONENT_DELETED);
648 len = (cbfs_get_entry_addr(image, next) -
649 cbfs_get_entry_addr(image, entry));
650 entry->offset = htonl(cbfs_calculate_file_header_size(""));
651 entry->len = htonl(len - ntohl(entry->offset));
652 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
653 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
654 ntohl(entry->len));
655 return 0;
656}
657
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700658int cbfs_print_header_info(struct cbfs_image *image)
659{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800660 char *name = strdup(image->buffer.name);
661 assert(image && image->header);
662 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800663 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800664 basename(name),
665 image->buffer.size / 1024,
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600666 image->header->bootblocksize,
667 image->header->romsize,
668 image->header->offset,
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800669 image->header->align,
670 arch_to_string(image->header->architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800671 free(name);
672 return 0;
673}
674
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700675static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
676{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800677 fprintf(fp,
678 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
679 "length: %d/%d\n",
680 lookup_name_by_type(types_cbfs_compression,
681 stage->compression, "(unknown)"),
682 stage->entry,
683 stage->load,
684 stage->len,
685 stage->memlen);
686 return 0;
687}
688
Hung-Te Lin0780d672014-05-16 10:14:05 +0800689static int cbfs_print_decoded_payload_segment_info(
690 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800691{
Hung-Te Lin0780d672014-05-16 10:14:05 +0800692 /* The input (seg) must be already decoded by
693 * cbfs_decode_payload_segment.
694 */
695 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800696 case PAYLOAD_SEGMENT_CODE:
697 case PAYLOAD_SEGMENT_DATA:
698 fprintf(fp, " %s (%s compression, offset: 0x%x, "
699 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800700 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800701 "code " : "data"),
702 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800703 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800704 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800705 seg->offset, seg->load_addr, seg->len,
706 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800707 break;
708
709 case PAYLOAD_SEGMENT_ENTRY:
710 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800711 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800712 break;
713
714 case PAYLOAD_SEGMENT_BSS:
715 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
716 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800717 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800718 break;
719
720 case PAYLOAD_SEGMENT_PARAMS:
721 fprintf(fp, " parameters\n");
722 break;
723
724 default:
725 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
726 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800727 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800728 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800729 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800730 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800731 seg->offset, seg->load_addr, seg->len,
732 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800733 break;
734 }
735 return 0;
736}
737
738int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700739 void *arg)
740{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800741 const char *name = CBFS_NAME(entry);
742 struct cbfs_payload_segment *payload;
743 FILE *fp = (FILE *)arg;
744
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800745 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800746 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
747 cbfs_get_entry_addr(image, entry));
748 return -1;
749 }
750 if (!fp)
751 fp = stdout;
752
753 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
754 *name ? name : "(empty)",
755 cbfs_get_entry_addr(image, entry),
756 get_cbfs_entry_type_name(ntohl(entry->type)),
757 ntohl(entry->len));
758
759 if (!verbose)
760 return 0;
761
762 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
763 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
764 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
765 ntohl(entry->len));
766
767 /* note the components of the subheader may be in host order ... */
768 switch (ntohl(entry->type)) {
769 case CBFS_COMPONENT_STAGE:
770 cbfs_print_stage_info((struct cbfs_stage *)
771 CBFS_SUBHEADER(entry), fp);
772 break;
773
774 case CBFS_COMPONENT_PAYLOAD:
775 payload = (struct cbfs_payload_segment *)
776 CBFS_SUBHEADER(entry);
777 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +0800778 struct cbfs_payload_segment seg;
779 cbfs_decode_payload_segment(&seg, payload);
780 cbfs_print_decoded_payload_segment_info(
781 &seg, fp);
782 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800783 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +0800784 else
Aaron Durbinca630272014-08-05 10:48:20 -0500785 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800786 }
787 break;
788 default:
789 break;
790 }
791 return 0;
792}
793
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700794int cbfs_print_directory(struct cbfs_image *image)
795{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800796 cbfs_print_header_info(image);
797 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
798 cbfs_walk(image, cbfs_print_entry_info, NULL);
799 return 0;
800}
801
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800802int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -0800803 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700804{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800805 struct cbfs_file *next;
806 uint32_t type, addr, last_addr;
807
808 type = ntohl(entry->type);
809 if (type == CBFS_COMPONENT_DELETED) {
810 // Ready to be recycled.
811 type = CBFS_COMPONENT_NULL;
812 entry->type = htonl(type);
813 }
814 if (type != CBFS_COMPONENT_NULL)
815 return 0;
816
817 next = cbfs_find_next_entry(image, entry);
818
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800819 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800820 type = ntohl(next->type);
821 if (type == CBFS_COMPONENT_DELETED) {
822 type = CBFS_COMPONENT_NULL;
823 next->type = htonl(type);
824 }
825 if (type != CBFS_COMPONENT_NULL)
826 return 0;
827
828 addr = cbfs_get_entry_addr(image, entry);
829 last_addr = cbfs_get_entry_addr(
830 image, cbfs_find_next_entry(image, next));
831
832 // Now, we find two deleted/empty entries; try to merge now.
833 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
834 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
835 cbfs_get_entry_addr(image, next), ntohl(next->len));
Vadim Bendebury45e59972014-12-23 15:59:57 -0800836 cbfs_create_empty_entry(entry,
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800837 (last_addr - addr -
838 cbfs_calculate_file_header_size("")),
839 "");
840 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
841 next = cbfs_find_next_entry(image, entry);
842 }
843 return 0;
844}
845
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800846int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700847 void *arg)
848{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800849 int count = 0;
850 struct cbfs_file *entry;
851 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800852 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800853 entry = cbfs_find_next_entry(image, entry)) {
854 count ++;
855 if (callback(image, entry, arg) != 0)
856 break;
857 }
858 return count;
859}
860
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800861static int cbfs_header_valid(struct cbfs_header *header, size_t size)
862{
863 if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
864 ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
865 (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
866 (ntohl(header->romsize) <= size) &&
867 (ntohl(header->offset) < ntohl(header->romsize)))
868 return 1;
869 return 0;
870}
871
872struct cbfs_header *cbfs_find_header(char *data, size_t size,
873 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700874{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800875 size_t offset;
876 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800877 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800878 struct cbfs_header *header, *result = NULL;
879
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800880 if (forced_offset < (size - sizeof(struct cbfs_header))) {
881 /* Check if the forced header is valid. */
882 header = (struct cbfs_header *)(data + forced_offset);
883 if (cbfs_header_valid(header, size))
884 return header;
885 return NULL;
886 }
887
Julius Wernerefcee762014-11-10 13:14:24 -0800888 // Try finding relative offset of master header at end of file first.
889 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
890 offset = size + rel_offset;
891 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
892 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800893
Hung-Te Lineab2c812013-01-29 01:56:17 +0800894 if (offset >= size - sizeof(*header) ||
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800895 !cbfs_header_valid((struct cbfs_header *)(data + offset), size)) {
Julius Wernerefcee762014-11-10 13:14:24 -0800896 // Some use cases append non-CBFS data to the end of the ROM.
897 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +0800898 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800899 }
Hung-Te Lineab2c812013-01-29 01:56:17 +0800900
901 for (; offset + sizeof(*header) < size; offset++) {
902 header = (struct cbfs_header *)(data + offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800903 if (!cbfs_header_valid(header, size))
Hung-Te Lineab2c812013-01-29 01:56:17 +0800904 continue;
Julius Wernerefcee762014-11-10 13:14:24 -0800905 if (!found++)
906 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800907 }
Julius Wernerefcee762014-11-10 13:14:24 -0800908 if (found > 1)
909 // Top-aligned images usually have a working relative offset
910 // field, so this is more likely to happen on bottom-aligned
911 // ones (where the first header is the "outermost" one)
912 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +0800913 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800914 return result;
915}
916
917
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700918struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
919{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800920 assert(image && image->header);
921 return (struct cbfs_file *)(image->buffer.data +
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600922 image->header->offset);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800923}
924
925struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700926 struct cbfs_file *entry)
927{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800928 uint32_t addr = cbfs_get_entry_addr(image, entry);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600929 int align = image->header->align;
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800930 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +0800931 addr += ntohl(entry->offset) + ntohl(entry->len);
932 addr = align_up(addr, align);
933 return (struct cbfs_file *)(image->buffer.data + addr);
934}
935
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700936uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
937{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800938 assert(image && image->buffer.data && entry);
939 return (int32_t)((char *)entry - image->buffer.data);
940}
941
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700942int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
943{
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800944 return (entry &&
945 (char *)entry >= image->buffer.data &&
946 (char *)entry + sizeof(entry->magic) <
947 image->buffer.data + image->buffer.size &&
948 memcmp(entry->magic, CBFS_FILE_MAGIC,
949 sizeof(entry->magic)) == 0);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800950}
951
Vadim Bendebury45e59972014-12-23 15:59:57 -0800952int cbfs_create_empty_entry(struct cbfs_file *entry,
953 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700954{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800955 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
956 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
957 entry->type = htonl(CBFS_COMPONENT_NULL);
958 entry->len = htonl(len);
959 entry->checksum = 0; // TODO Build a checksum algorithm.
960 entry->offset = htonl(cbfs_calculate_file_header_size(name));
961 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
962 strcpy(CBFS_NAME(entry), name);
963 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
964 return 0;
965}
966
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700967/* Finds a place to hold whole data in same memory page. */
968static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
969{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800970 if (!page)
971 return 1;
972 return (start / page) == (start + size - 1) / page;
973}
974
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800975/* Tests if data can fit in a range by given offset:
976 * start ->| header_len | offset (+ size) |<- end
977 */
978static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700979 uint32_t offset, uint32_t size)
980{
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800981 return (offset >= start + header_len && offset + size <= end);
982}
983
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800984int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700985 uint32_t size, uint32_t page_size, uint32_t align)
986{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800987 struct cbfs_file *entry;
988 size_t need_len;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800989 uint32_t addr, addr_next, addr2, addr3, offset, header_len;
990
991 /* Default values: allow fitting anywhere in ROM. */
992 if (!page_size)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600993 page_size = image->header->romsize;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800994 if (!align)
995 align = 1;
996
997 if (size > page_size)
998 ERROR("Input file size (%d) greater than page size (%d).\n",
999 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001000
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -06001001 if (page_size % image->header->align)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001002 WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -06001003 __func__, page_size, image->header->align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001004
1005 /* TODO Old cbfstool always assume input is a stage file (and adding
1006 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001007 * (type) param in future. For right now, we assume cbfs_stage is the
1008 * largest structure and add it into header size. */
1009 assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001010 header_len = (cbfs_calculate_file_header_size(name) +
1011 sizeof(struct cbfs_stage));
1012 need_len = header_len + size;
1013
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001014 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001015 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
1016
1017 /* Three cases of content location on memory page:
1018 * case 1.
1019 * | PAGE 1 | PAGE 2 |
1020 * | <header><content>| Fit. Return start of content.
1021 *
1022 * case 2.
1023 * | PAGE 1 | PAGE 2 |
1024 * | <header><content> | Fits when we shift content to align
1025 * shift-> | <header>|<content> | at starting of PAGE 2.
1026 *
1027 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001028 * | PAGE 1 | PAGE 2 | PAGE 3 |
1029 * | <header>< content > | Can't fit. If we shift content to
1030 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1031 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001032 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001033 * The returned address can be then used as "base-address" (-b) in add-*
1034 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1035 * For stage targets, the address is also used to re-link stage before
1036 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001037 */
1038 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001039 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001040 entry = cbfs_find_next_entry(image, entry)) {
1041
1042 uint32_t type = ntohl(entry->type);
1043 if (type != CBFS_COMPONENT_NULL)
1044 continue;
1045
1046 addr = cbfs_get_entry_addr(image, entry);
1047 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
1048 image, entry));
1049 if (addr_next - addr < need_len)
1050 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001051
1052 offset = align_up(addr + header_len, align);
1053 if (is_in_same_page(offset, size, page_size) &&
1054 is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001055 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001056 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001057 }
1058
1059 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001060 offset = align_up(addr2, align);
1061 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001062 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001063 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001064 }
1065
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001066 /* Assume page_size >= header_len so adding one page will
1067 * definitely provide the space for header. */
1068 assert(page_size >= header_len);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001069 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001070 offset = align_up(addr3, align);
1071 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001072 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001073 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001074 }
1075 }
1076 return -1;
1077}