blob: b64f78e1403edb114c6a4f59a6c4a0c724b6bacd [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>
Hung-Te Lineab2c812013-01-29 01:56:17 +080022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Sol Boucher0e539312015-03-05 15:38:03 -080025#include <strings.h>
Hung-Te Lineab2c812013-01-29 01:56:17 +080026
27#include "common.h"
28#include "cbfs_image.h"
29
30/* The file name align is not defined in CBFS spec -- only a preference by
31 * (old) cbfstool. */
32#define CBFS_FILENAME_ALIGN (16)
33
34/* To make CBFS more friendly to ROM, fill -1 (0xFF) instead of zero. */
35#define CBFS_CONTENT_DEFAULT_VALUE (-1)
36
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080037/* Type and format */
38
39struct typedesc_t {
40 uint32_t type;
41 const char *name;
42};
43
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070044static const struct typedesc_t types_cbfs_entry[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080045 {CBFS_COMPONENT_STAGE, "stage"},
46 {CBFS_COMPONENT_PAYLOAD, "payload"},
47 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
48 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
49 {CBFS_COMPONENT_RAW, "raw"},
50 {CBFS_COMPONENT_VSA, "vsa"},
51 {CBFS_COMPONENT_MBI, "mbi"},
52 {CBFS_COMPONENT_MICROCODE, "microcode"},
Martin Rothdde307c2015-03-24 15:54:20 -060053 {CBFS_COMPONENT_FSP, "fsp"},
54 {CBFS_COMPONENT_MRC, "mrc"},
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080055 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos_default"},
56 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos_layout"},
Martin Rothdde307c2015-03-24 15:54:20 -060057 {CBFS_COMPONENT_SPD, "spd"},
58 {CBFS_COMPONENT_MRC_CACHE, "mrc_cache"},
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080059 {CBFS_COMPONENT_DELETED, "deleted"},
60 {CBFS_COMPONENT_NULL, "null"},
61 {0, NULL},
62};
63
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070064static const struct typedesc_t types_cbfs_compression[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080065 {CBFS_COMPRESS_NONE, "none"},
66 {CBFS_COMPRESS_LZMA, "LZMA"},
67 {0, NULL},
68};
69
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080070static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070071 const char *default_value)
72{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080073 int i;
74 for (i = 0; desc[i].name; i++)
75 if (desc[i].type == type)
76 return desc[i].name;
77 return default_value;
78}
79
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010080static const char *get_cbfs_entry_type_name(uint32_t type)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070081{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080082 return lookup_name_by_type(types_cbfs_entry, type, "(unknown)");
83}
84
Hung-Te Linc03d9b02013-01-29 02:38:40 +080085/* CBFS image */
86
Sol Boucher0e539312015-03-05 15:38:03 -080087static size_t cbfs_calculate_file_header_size(const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070088{
Hung-Te Linc03d9b02013-01-29 02:38:40 +080089 return (sizeof(struct cbfs_file) +
90 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
91}
92
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -060093static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070094{
Hung-Te Lin49fcd752013-01-29 03:16:20 +080095 // A bug in old cbfstool may produce extra few bytes (by alignment) and
96 // cause cbfstool to overwrite things after free space -- which is
97 // usually CBFS header on x86. We need to workaround that.
98
99 struct cbfs_file *entry, *first = NULL, *last = NULL;
100 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800101 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800102 entry = cbfs_find_next_entry(image, entry)) {
103 last = entry;
104 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600105 if ((char *)first < (char *)hdr_loc &&
106 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800107 WARN("CBFS image was created with old cbfstool with size bug. "
108 "Fixing size in last entry...\n");
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600109 last->len = htonl(ntohl(last->len) - image->header->align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800110 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
111 cbfs_get_entry_addr(image, entry),
112 cbfs_get_entry_addr(image,
113 cbfs_find_next_entry(image, last)));
114 }
115 return 0;
116}
117
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800118void cbfs_put_header(void *dest, const struct cbfs_header *header)
119{
120 struct buffer outheader;
121
122 outheader.data = dest;
123 outheader.size = 0;
124
125 xdr_be.put32(&outheader, header->magic);
126 xdr_be.put32(&outheader, header->version);
127 xdr_be.put32(&outheader, header->romsize);
128 xdr_be.put32(&outheader, header->bootblocksize);
129 xdr_be.put32(&outheader, header->align);
130 xdr_be.put32(&outheader, header->offset);
131 xdr_be.put32(&outheader, header->architecture);
132}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600133
Hung-Te Lin0780d672014-05-16 10:14:05 +0800134static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
135 struct cbfs_payload_segment *input)
136{
137 struct buffer seg = {
138 .data = (void *)input,
139 .size = sizeof(*input),
140 };
141 output->type = xdr_be.get32(&seg);
142 output->compression = xdr_be.get32(&seg);
143 output->offset = xdr_be.get32(&seg);
144 output->load_addr = xdr_be.get64(&seg);
145 output->len = xdr_be.get32(&seg);
146 output->mem_len = xdr_be.get32(&seg);
147 assert(seg.size == 0);
148}
149
Sol Boucher0e539312015-03-05 15:38:03 -0800150void cbfs_get_header(struct cbfs_header *header, void *src)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600151{
152 struct buffer outheader;
153
Sol Boucher0e539312015-03-05 15:38:03 -0800154 outheader.data = src; /* We're not modifying the data */
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600155 outheader.size = 0;
156
157 header->magic = xdr_be.get32(&outheader);
158 header->version = xdr_be.get32(&outheader);
159 header->romsize = xdr_be.get32(&outheader);
160 header->bootblocksize = xdr_be.get32(&outheader);
161 header->align = xdr_be.get32(&outheader);
162 header->offset = xdr_be.get32(&outheader);
163 header->architecture = xdr_be.get32(&outheader);
164}
165
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800166int cbfs_image_create(struct cbfs_image *image,
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100167 uint32_t architecture,
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800168 size_t size,
169 uint32_t align,
170 struct buffer *bootblock,
Sol Boucher0e539312015-03-05 15:38:03 -0800171 uint32_t bootblock_offset,
172 uint32_t header_offset,
173 uint32_t entries_offset)
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800174{
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800175 struct cbfs_header header;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800176 struct cbfs_file *entry;
Julius Wernerefcee762014-11-10 13:14:24 -0800177 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800178 uint32_t cbfs_len;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800179 size_t entry_header_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600180 void *header_loc;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800181
182 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
183 "header=0x%x+0x%zx, entries_offset=0x%x\n",
184 bootblock_offset, bootblock->size,
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800185 header_offset, sizeof(header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800186
187 if (buffer_create(&image->buffer, size, "(new)") != 0)
188 return -1;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600189 if ((image->header = malloc(sizeof(*image->header))) == NULL)
190 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800191 memset(image->buffer.data, CBFS_CONTENT_DEFAULT_VALUE, size);
192
193 // Adjust legcay top-aligned address to ROM offset.
194 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800195 entries_offset = size + (int32_t)entries_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800196 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800197 bootblock_offset = size + (int32_t)bootblock_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800198 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
Sol Boucher0e539312015-03-05 15:38:03 -0800199 header_offset = size + (int32_t)header_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800200
201 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
202 "header=0x%x, entries_offset=0x%x\n",
203 bootblock_offset, header_offset, entries_offset);
204
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800205 // Prepare bootblock
206 if (bootblock_offset + bootblock->size > size) {
207 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
208 bootblock_offset, bootblock->size, size);
209 return -1;
210 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800211 if (entries_offset > bootblock_offset &&
212 entries_offset < bootblock->size) {
213 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
214 bootblock_offset, bootblock->size, entries_offset);
215 return -1;
216 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800217 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
218 bootblock->size);
219
220 // Prepare header
Julius Wernerefcee762014-11-10 13:14:24 -0800221 if (header_offset + sizeof(header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800222 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800223 header_offset, sizeof(header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800224 return -1;
225 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600226 image->header->magic = CBFS_HEADER_MAGIC;
227 image->header->version = CBFS_HEADER_VERSION;
228 image->header->romsize = size;
229 image->header->bootblocksize = bootblock->size;
230 image->header->align = align;
231 image->header->offset = entries_offset;
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100232 image->header->architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600233
234 header_loc = (image->buffer.data + header_offset);
235 cbfs_put_header(header_loc, image->header);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800236
Julius Wernerefcee762014-11-10 13:14:24 -0800237 // The last 4 byte of the image contain the relative offset from the end
238 // of the image to the master header as a 32-bit signed integer. x86
239 // relies on this also being its (memory-mapped, top-aligned) absolute
240 // 32-bit address by virtue of how two's complement numbers work.
241 assert(size % sizeof(int32_t) == 0);
242 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
243 *rel_offset = header_offset - size;
244
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800245 // Prepare entries
246 if (align_up(entries_offset, align) != entries_offset) {
247 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
248 entries_offset, align);
249 return -1;
250 }
Hung-Te Linb02c8732013-03-15 17:40:08 +0800251 entry_header_len = cbfs_calculate_file_header_size("");
252 if (entries_offset + entry_header_len > size) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800253 ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n",
Hung-Te Linb02c8732013-03-15 17:40:08 +0800254 entries_offset, entry_header_len, size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800255 return -1;
256 }
257 entry = (struct cbfs_file *)(image->buffer.data + entries_offset);
258 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800259 // e = min(bootblock, header, rel_offset) where e > entries_offset.
260 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800261 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
262 cbfs_len = bootblock_offset;
263 if (header_offset > entries_offset && header_offset < cbfs_len)
264 cbfs_len = header_offset;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800265 cbfs_len -= entries_offset + align + entry_header_len;
Vadim Bendebury45e59972014-12-23 15:59:57 -0800266 cbfs_create_empty_entry(entry, cbfs_len, "");
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800267 LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len);
268 return 0;
269}
270
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800271int cbfs_image_from_file(struct cbfs_image *image,
272 const char *filename, uint32_t offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700273{
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600274 void *header_loc;
275
Hung-Te Lineab2c812013-01-29 01:56:17 +0800276 if (buffer_from_file(&image->buffer, filename) != 0)
277 return -1;
278 DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
279 image->buffer.size);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800280 header_loc = cbfs_find_header(image->buffer.data,
281 image->buffer.size,
282 offset);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600283 if (!header_loc) {
Hung-Te Lineab2c812013-01-29 01:56:17 +0800284 ERROR("%s does not have CBFS master header.\n", filename);
285 cbfs_image_delete(image);
286 return -1;
287 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600288
289 if ((image->header = malloc(sizeof(*image->header))) == NULL)
290 return -1;
291
292 cbfs_get_header(image->header, header_loc);
293 cbfs_fix_legacy_size(image, header_loc);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800294
295 return 0;
296}
297
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800298int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
299 size_t copy_size)
300{
301 struct cbfs_file *src_entry, *dst_entry;
302 struct cbfs_header *copy_header;
303 size_t align, entry_offset;
304 ssize_t last_entry_size;
305
306 size_t header_offset, header_end;
307 size_t cbfs_offset, cbfs_end;
308 size_t copy_end = copy_offset + copy_size;
309
310 align = htonl(image->header->align);
311
312 header_offset = (char *)image->header - image->buffer.data;
313 header_end = header_offset + sizeof(image->header);
314
315 cbfs_offset = htonl(image->header->offset);
316 cbfs_end = htonl(image->header->romsize);
317
318 if (copy_end > image->buffer.size) {
319 ERROR("Copy offset out of range: [%zx:%zx)\n",
320 copy_offset, copy_end);
321 return 1;
322 }
323
324 /* Range check requested copy region with header and source cbfs. */
325 if ((copy_offset >= header_offset && copy_offset < header_end) ||
326 (copy_end >= header_offset && copy_end <= header_end)) {
327 ERROR("New image would overlap old header.\n");
328 }
329
330 if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) ||
331 (copy_end >= cbfs_offset && copy_end <= cbfs_end)) {
332 ERROR("New image would overlap old one.\n");
333 return 1;
334 }
335
336 /* This will work, let's create a copy. */
337 copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset);
338 *copy_header = *image->header;
339
340 copy_header->bootblocksize = 0;
341 /* Romsize is a misnomer. It's the absolute limit of cbfs content.*/
342 copy_header->romsize = htonl(copy_end);
343 entry_offset = align_up(copy_offset + sizeof(*copy_header), align);
344 copy_header->offset = htonl(entry_offset);
345 dst_entry = (struct cbfs_file *)(image->buffer.data + entry_offset);
346
347 /* Copy non-empty files */
348 for (src_entry = cbfs_find_first_entry(image);
349 src_entry && cbfs_is_valid_entry(image, src_entry);
350 src_entry = cbfs_find_next_entry(image, src_entry)) {
351 size_t entry_size;
352
353 if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) ||
354 (src_entry->type == htonl(CBFS_COMPONENT_DELETED)))
355 continue;
356
357 entry_size = htonl(src_entry->len) + htonl(src_entry->offset);
358 memcpy(dst_entry, src_entry, entry_size);
359 dst_entry = (struct cbfs_file *)(
360 (uintptr_t)dst_entry + align_up(entry_size, align));
361
Sol Boucher0e539312015-03-05 15:38:03 -0800362 if ((size_t)((char *)dst_entry - image->buffer.data) >=
363 copy_end) {
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800364 ERROR("Ran out of room in copy region.\n");
365 return 1;
366 }
367 }
368
369 /* Last entry size is all the room above it. */
370 last_entry_size = copy_end - ((char *)dst_entry - image->buffer.data)
371 - cbfs_calculate_file_header_size("");
372
373 if (last_entry_size < 0)
374 WARN("No room to create the last entry!\n")
375 else
Vadim Bendebury45e59972014-12-23 15:59:57 -0800376 cbfs_create_empty_entry(dst_entry, last_entry_size, "");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800377
378 return 0;
379}
380
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700381int cbfs_image_write_file(struct cbfs_image *image, const char *filename)
382{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800383 assert(image && image->buffer.data);
384 return buffer_write_file(&image->buffer, filename);
385}
386
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700387int cbfs_image_delete(struct cbfs_image *image)
388{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100389 if (image == NULL)
390 return 0;
391
Hung-Te Lineab2c812013-01-29 01:56:17 +0800392 buffer_delete(&image->buffer);
393 image->header = NULL;
394 return 0;
395}
396
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800397/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
398static int cbfs_add_entry_at(struct cbfs_image *image,
399 struct cbfs_file *entry,
400 uint32_t size,
401 const char *name,
402 uint32_t type,
403 const void *data,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700404 uint32_t content_offset)
405{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800406 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
407 uint32_t addr = cbfs_get_entry_addr(image, entry),
408 addr_next = cbfs_get_entry_addr(image, next);
409 uint32_t header_size = cbfs_calculate_file_header_size(name),
410 min_entry_size = cbfs_calculate_file_header_size("");
411 uint32_t len, target;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600412 uint32_t align = image->header->align;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800413
414 target = content_offset - header_size;
415 if (target % align)
416 target -= target % align;
417 if (target < addr) {
418 ERROR("No space to hold cbfs_file header.");
419 return -1;
420 }
421
422 // Process buffer BEFORE content_offset.
423 if (target - addr > min_entry_size) {
424 DEBUG("|min|...|header|content|... <create new entry>\n");
425 len = target - addr - min_entry_size;
Vadim Bendebury45e59972014-12-23 15:59:57 -0800426 cbfs_create_empty_entry(entry, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800427 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
428 entry = cbfs_find_next_entry(image, entry);
429 addr = cbfs_get_entry_addr(image, entry);
430 }
431
432 len = size + (content_offset - addr - header_size);
Vadim Bendebury45e59972014-12-23 15:59:57 -0800433 cbfs_create_empty_entry(entry, len, name);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800434 if (len != size) {
435 DEBUG("|..|header|content|... <use offset to create entry>\n");
436 DEBUG("before: offset=0x%x, len=0x%x\n",
437 ntohl(entry->offset), ntohl(entry->len));
438 // TODO reset expanded name buffer to 0xFF.
439 entry->offset = htonl(ntohl(entry->offset) + (len - size));
440 entry->len = htonl(size);
441 DEBUG("after: offset=0x%x, len=0x%x\n",
442 ntohl(entry->offset), ntohl(entry->len));
443 }
444
445 // Ready to fill data into entry.
446 assert(ntohl(entry->len) == size);
447 entry->type = htonl(type);
448 DEBUG("content_offset: 0x%x, entry location: %x\n",
449 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
450 image->buffer.data));
451 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
452 content_offset);
453 memcpy(CBFS_SUBHEADER(entry), data, size);
454 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
455
456 // Process buffer AFTER entry.
457 entry = cbfs_find_next_entry(image, entry);
458 addr = cbfs_get_entry_addr(image, entry);
459 assert(addr < addr_next);
460
461 if (addr_next - addr < min_entry_size) {
462 DEBUG("No space after content to keep CBFS structure.\n");
463 return -1;
464 }
465
466 len = addr_next - addr - min_entry_size;
Vadim Bendebury45e59972014-12-23 15:59:57 -0800467 cbfs_create_empty_entry(entry, len, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800468 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
469 return 0;
470}
471
472int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700473 const char *name, uint32_t type, uint32_t content_offset)
474{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800475 uint32_t entry_type;
476 uint32_t addr, addr_next;
477 struct cbfs_file *entry, *next;
478 uint32_t header_size, need_size, new_size;
479
480 header_size = cbfs_calculate_file_header_size(name);
481
482 need_size = header_size + buffer->size;
483 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
484 name, content_offset, header_size, buffer->size, need_size);
485
486 if (IS_TOP_ALIGNED_ADDRESS(content_offset)) {
487 // legacy cbfstool takes top-aligned address.
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600488 uint32_t theromsize = image->header->romsize;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800489 INFO("Converting top-aligned address 0x%x to offset: 0x%x\n",
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800490 content_offset, content_offset + theromsize);
Sol Boucher0e539312015-03-05 15:38:03 -0800491 content_offset = theromsize + (int32_t)content_offset;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800492 }
493
494 // Merge empty entries.
495 DEBUG("(trying to merge empty entries...)\n");
496 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
497
498 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800499 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800500 entry = cbfs_find_next_entry(image, entry)) {
501
502 entry_type = ntohl(entry->type);
503 if (entry_type != CBFS_COMPONENT_NULL)
504 continue;
505
506 addr = cbfs_get_entry_addr(image, entry);
507 next = cbfs_find_next_entry(image, entry);
508 addr_next = cbfs_get_entry_addr(image, next);
509
510 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
511 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600512
513 /* Will the file fit? Don't yet worry if we have space for a new
514 * "empty" entry. We take care of that later.
515 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800516 if (addr + need_size > addr_next)
517 continue;
518
519 // Can we simply put object here?
520 if (!content_offset || content_offset == addr + header_size) {
521 DEBUG("Filling new entry data (%zd bytes).\n",
522 buffer->size);
Vadim Bendebury45e59972014-12-23 15:59:57 -0800523 cbfs_create_empty_entry(entry, buffer->size, name);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800524 entry->type = htonl(type);
525 memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
526 if (verbose)
527 cbfs_print_entry_info(image, entry, stderr);
528
529 // setup new entry
Paul Menzel4159a802013-07-14 00:24:31 +0200530 DEBUG("Setting new empty entry.\n");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800531 entry = cbfs_find_next_entry(image, entry);
532 new_size = (cbfs_get_entry_addr(image, next) -
533 cbfs_get_entry_addr(image, entry));
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600534
535 /* Entry was added and no space for new "empty" entry */
536 if (new_size < cbfs_calculate_file_header_size("")) {
537 DEBUG("No need for new \"empty\" entry\n");
538 /* No need to increase the size of the just
539 * stored file to extend to next file. Alignment
540 * of next file takes care of this.
541 */
542 return 0;
543 }
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800544 new_size -= cbfs_calculate_file_header_size("");
545 DEBUG("new size: %d\n", new_size);
Vadim Bendebury45e59972014-12-23 15:59:57 -0800546 cbfs_create_empty_entry(entry, new_size, "");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800547 if (verbose)
548 cbfs_print_entry_info(image, entry, stderr);
549 return 0;
550 }
551
552 // We need to put content here, and the case is really
553 // complicated...
554 assert(content_offset);
555 if (addr_next < content_offset) {
556 DEBUG("Not for specified offset yet");
557 continue;
558 } else if (addr > content_offset) {
559 DEBUG("Exceed specified content_offset.");
560 break;
561 } else if (addr + header_size > content_offset) {
562 ERROR("Not enough space for header.\n");
563 break;
564 } else if (content_offset + buffer->size > addr_next) {
565 ERROR("Not enough space for content.\n");
566 break;
567 }
568
569 // TODO there are more few tricky cases that we may
570 // want to fit by altering offset.
571 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
572 addr, addr_next - addr, content_offset);
573
574 if (cbfs_add_entry_at(image, entry, buffer->size, name, type,
575 buffer->data, content_offset) == 0) {
576 return 0;
577 }
578 break;
579 }
580
581 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
582 buffer->name, buffer->size, buffer->size / 1024, content_offset);
583 return -1;
584}
585
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700586struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
587{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800588 struct cbfs_file *entry;
589 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800590 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800591 entry = cbfs_find_next_entry(image, entry)) {
592 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
593 DEBUG("cbfs_get_entry: found %s\n", name);
594 return entry;
595 }
596 }
597 return NULL;
598}
599
600int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700601 const char *filename)
602{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800603 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
604 struct buffer buffer;
605 if (!entry) {
606 ERROR("File not found: %s\n", entry_name);
607 return -1;
608 }
609 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
610 entry_name, cbfs_get_entry_addr(image, entry),
611 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
612
613 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
614 WARN("Only 'raw' files are safe to extract.\n");
615 }
616
617 buffer.data = CBFS_SUBHEADER(entry);
618 buffer.size = ntohl(entry->len);
Sol Boucher0e539312015-03-05 15:38:03 -0800619 buffer.name = strdup("(cbfs_export_entry)");
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800620 if (buffer_write_file(&buffer, filename) != 0) {
621 ERROR("Failed to write %s into %s.\n",
622 entry_name, filename);
Sol Boucher0e539312015-03-05 15:38:03 -0800623 free(buffer.name);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800624 return -1;
625 }
Sol Boucher0e539312015-03-05 15:38:03 -0800626 free(buffer.name);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800627 INFO("Successfully dumped the file to: %s\n", filename);
628 return 0;
629}
630
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700631int cbfs_remove_entry(struct cbfs_image *image, const char *name)
632{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800633 struct cbfs_file *entry, *next;
634 size_t len;
635 entry = cbfs_get_entry(image, name);
636 if (!entry) {
637 ERROR("CBFS file %s not found.\n", name);
638 return -1;
639 }
640 next = cbfs_find_next_entry(image, entry);
641 assert(next);
642 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
643 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
644 entry->type = htonl(CBFS_COMPONENT_DELETED);
645 len = (cbfs_get_entry_addr(image, next) -
646 cbfs_get_entry_addr(image, entry));
647 entry->offset = htonl(cbfs_calculate_file_header_size(""));
648 entry->len = htonl(len - ntohl(entry->offset));
649 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
650 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
651 ntohl(entry->len));
652 return 0;
653}
654
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700655int cbfs_print_header_info(struct cbfs_image *image)
656{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800657 char *name = strdup(image->buffer.name);
658 assert(image && image->header);
659 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800660 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800661 basename(name),
662 image->buffer.size / 1024,
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600663 image->header->bootblocksize,
664 image->header->romsize,
665 image->header->offset,
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800666 image->header->align,
667 arch_to_string(image->header->architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800668 free(name);
669 return 0;
670}
671
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700672static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
673{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800674 fprintf(fp,
675 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
676 "length: %d/%d\n",
677 lookup_name_by_type(types_cbfs_compression,
678 stage->compression, "(unknown)"),
679 stage->entry,
680 stage->load,
681 stage->len,
682 stage->memlen);
683 return 0;
684}
685
Hung-Te Lin0780d672014-05-16 10:14:05 +0800686static int cbfs_print_decoded_payload_segment_info(
687 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800688{
Hung-Te Lin0780d672014-05-16 10:14:05 +0800689 /* The input (seg) must be already decoded by
690 * cbfs_decode_payload_segment.
691 */
692 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800693 case PAYLOAD_SEGMENT_CODE:
694 case PAYLOAD_SEGMENT_DATA:
695 fprintf(fp, " %s (%s compression, offset: 0x%x, "
696 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800697 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800698 "code " : "data"),
699 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800700 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800701 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800702 seg->offset, seg->load_addr, seg->len,
703 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800704 break;
705
706 case PAYLOAD_SEGMENT_ENTRY:
707 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800708 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800709 break;
710
711 case PAYLOAD_SEGMENT_BSS:
712 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
713 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800714 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800715 break;
716
717 case PAYLOAD_SEGMENT_PARAMS:
718 fprintf(fp, " parameters\n");
719 break;
720
721 default:
722 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
723 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800724 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800725 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800726 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800727 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800728 seg->offset, seg->load_addr, seg->len,
729 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800730 break;
731 }
732 return 0;
733}
734
735int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700736 void *arg)
737{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800738 const char *name = CBFS_NAME(entry);
739 struct cbfs_payload_segment *payload;
740 FILE *fp = (FILE *)arg;
741
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800742 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800743 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
744 cbfs_get_entry_addr(image, entry));
745 return -1;
746 }
747 if (!fp)
748 fp = stdout;
749
750 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
751 *name ? name : "(empty)",
752 cbfs_get_entry_addr(image, entry),
753 get_cbfs_entry_type_name(ntohl(entry->type)),
754 ntohl(entry->len));
755
756 if (!verbose)
757 return 0;
758
759 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
760 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
761 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
762 ntohl(entry->len));
763
764 /* note the components of the subheader may be in host order ... */
765 switch (ntohl(entry->type)) {
766 case CBFS_COMPONENT_STAGE:
767 cbfs_print_stage_info((struct cbfs_stage *)
768 CBFS_SUBHEADER(entry), fp);
769 break;
770
771 case CBFS_COMPONENT_PAYLOAD:
772 payload = (struct cbfs_payload_segment *)
773 CBFS_SUBHEADER(entry);
774 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +0800775 struct cbfs_payload_segment seg;
776 cbfs_decode_payload_segment(&seg, payload);
777 cbfs_print_decoded_payload_segment_info(
778 &seg, fp);
779 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800780 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +0800781 else
Aaron Durbinca630272014-08-05 10:48:20 -0500782 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800783 }
784 break;
785 default:
786 break;
787 }
788 return 0;
789}
790
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700791int cbfs_print_directory(struct cbfs_image *image)
792{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800793 cbfs_print_header_info(image);
794 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
795 cbfs_walk(image, cbfs_print_entry_info, NULL);
796 return 0;
797}
798
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800799int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Sol Boucher0e539312015-03-05 15:38:03 -0800800 unused void *arg)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700801{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800802 struct cbfs_file *next;
803 uint32_t type, addr, last_addr;
804
805 type = ntohl(entry->type);
806 if (type == CBFS_COMPONENT_DELETED) {
807 // Ready to be recycled.
808 type = CBFS_COMPONENT_NULL;
809 entry->type = htonl(type);
810 }
811 if (type != CBFS_COMPONENT_NULL)
812 return 0;
813
814 next = cbfs_find_next_entry(image, entry);
815
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800816 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800817 type = ntohl(next->type);
818 if (type == CBFS_COMPONENT_DELETED) {
819 type = CBFS_COMPONENT_NULL;
820 next->type = htonl(type);
821 }
822 if (type != CBFS_COMPONENT_NULL)
823 return 0;
824
825 addr = cbfs_get_entry_addr(image, entry);
826 last_addr = cbfs_get_entry_addr(
827 image, cbfs_find_next_entry(image, next));
828
829 // Now, we find two deleted/empty entries; try to merge now.
830 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
831 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
832 cbfs_get_entry_addr(image, next), ntohl(next->len));
Vadim Bendebury45e59972014-12-23 15:59:57 -0800833 cbfs_create_empty_entry(entry,
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800834 (last_addr - addr -
835 cbfs_calculate_file_header_size("")),
836 "");
837 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
838 next = cbfs_find_next_entry(image, entry);
839 }
840 return 0;
841}
842
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800843int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700844 void *arg)
845{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800846 int count = 0;
847 struct cbfs_file *entry;
848 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800849 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800850 entry = cbfs_find_next_entry(image, entry)) {
851 count ++;
852 if (callback(image, entry, arg) != 0)
853 break;
854 }
855 return count;
856}
857
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800858static int cbfs_header_valid(struct cbfs_header *header, size_t size)
859{
860 if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
861 ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
862 (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
863 (ntohl(header->romsize) <= size) &&
864 (ntohl(header->offset) < ntohl(header->romsize)))
865 return 1;
866 return 0;
867}
868
869struct cbfs_header *cbfs_find_header(char *data, size_t size,
870 uint32_t forced_offset)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700871{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800872 size_t offset;
873 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800874 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800875 struct cbfs_header *header, *result = NULL;
876
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800877 if (forced_offset < (size - sizeof(struct cbfs_header))) {
878 /* Check if the forced header is valid. */
879 header = (struct cbfs_header *)(data + forced_offset);
880 if (cbfs_header_valid(header, size))
881 return header;
882 return NULL;
883 }
884
Julius Wernerefcee762014-11-10 13:14:24 -0800885 // Try finding relative offset of master header at end of file first.
886 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
887 offset = size + rel_offset;
888 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
889 (size_t)rel_offset, (size_t)-rel_offset, offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800890
Hung-Te Lineab2c812013-01-29 01:56:17 +0800891 if (offset >= size - sizeof(*header) ||
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800892 !cbfs_header_valid((struct cbfs_header *)(data + offset), size)) {
Julius Wernerefcee762014-11-10 13:14:24 -0800893 // Some use cases append non-CBFS data to the end of the ROM.
894 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +0800895 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800896 }
Hung-Te Lineab2c812013-01-29 01:56:17 +0800897
898 for (; offset + sizeof(*header) < size; offset++) {
899 header = (struct cbfs_header *)(data + offset);
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800900 if (!cbfs_header_valid(header, size))
Hung-Te Lineab2c812013-01-29 01:56:17 +0800901 continue;
Julius Wernerefcee762014-11-10 13:14:24 -0800902 if (!found++)
903 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800904 }
Julius Wernerefcee762014-11-10 13:14:24 -0800905 if (found > 1)
906 // Top-aligned images usually have a working relative offset
907 // field, so this is more likely to happen on bottom-aligned
908 // ones (where the first header is the "outermost" one)
909 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +0800910 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800911 return result;
912}
913
914
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700915struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
916{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800917 assert(image && image->header);
918 return (struct cbfs_file *)(image->buffer.data +
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600919 image->header->offset);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800920}
921
922struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700923 struct cbfs_file *entry)
924{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800925 uint32_t addr = cbfs_get_entry_addr(image, entry);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600926 int align = image->header->align;
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800927 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +0800928 addr += ntohl(entry->offset) + ntohl(entry->len);
929 addr = align_up(addr, align);
930 return (struct cbfs_file *)(image->buffer.data + addr);
931}
932
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700933uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
934{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800935 assert(image && image->buffer.data && entry);
936 return (int32_t)((char *)entry - image->buffer.data);
937}
938
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700939int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
940{
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800941 return (entry &&
942 (char *)entry >= image->buffer.data &&
943 (char *)entry + sizeof(entry->magic) <
944 image->buffer.data + image->buffer.size &&
945 memcmp(entry->magic, CBFS_FILE_MAGIC,
946 sizeof(entry->magic)) == 0);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800947}
948
Vadim Bendebury45e59972014-12-23 15:59:57 -0800949int cbfs_create_empty_entry(struct cbfs_file *entry,
950 size_t len, const char *name)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700951{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800952 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
953 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
954 entry->type = htonl(CBFS_COMPONENT_NULL);
955 entry->len = htonl(len);
956 entry->checksum = 0; // TODO Build a checksum algorithm.
957 entry->offset = htonl(cbfs_calculate_file_header_size(name));
958 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
959 strcpy(CBFS_NAME(entry), name);
960 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
961 return 0;
962}
963
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700964/* Finds a place to hold whole data in same memory page. */
965static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
966{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800967 if (!page)
968 return 1;
969 return (start / page) == (start + size - 1) / page;
970}
971
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800972/* Tests if data can fit in a range by given offset:
973 * start ->| header_len | offset (+ size) |<- end
974 */
975static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700976 uint32_t offset, uint32_t size)
977{
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800978 return (offset >= start + header_len && offset + size <= end);
979}
980
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800981int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700982 uint32_t size, uint32_t page_size, uint32_t align)
983{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800984 struct cbfs_file *entry;
985 size_t need_len;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800986 uint32_t addr, addr_next, addr2, addr3, offset, header_len;
987
988 /* Default values: allow fitting anywhere in ROM. */
989 if (!page_size)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600990 page_size = image->header->romsize;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800991 if (!align)
992 align = 1;
993
994 if (size > page_size)
995 ERROR("Input file size (%d) greater than page size (%d).\n",
996 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800997
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600998 if (page_size % image->header->align)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800999 WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -06001000 __func__, page_size, image->header->align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001001
1002 /* TODO Old cbfstool always assume input is a stage file (and adding
1003 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001004 * (type) param in future. For right now, we assume cbfs_stage is the
1005 * largest structure and add it into header size. */
1006 assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001007 header_len = (cbfs_calculate_file_header_size(name) +
1008 sizeof(struct cbfs_stage));
1009 need_len = header_len + size;
1010
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001011 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001012 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
1013
1014 /* Three cases of content location on memory page:
1015 * case 1.
1016 * | PAGE 1 | PAGE 2 |
1017 * | <header><content>| Fit. Return start of content.
1018 *
1019 * case 2.
1020 * | PAGE 1 | PAGE 2 |
1021 * | <header><content> | Fits when we shift content to align
1022 * shift-> | <header>|<content> | at starting of PAGE 2.
1023 *
1024 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001025 * | PAGE 1 | PAGE 2 | PAGE 3 |
1026 * | <header>< content > | Can't fit. If we shift content to
1027 * |trial-> <header>< content > | PAGE 2, header can't fit in free
1028 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001029 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001030 * The returned address can be then used as "base-address" (-b) in add-*
1031 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
1032 * For stage targets, the address is also used to re-link stage before
1033 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001034 */
1035 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +08001036 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001037 entry = cbfs_find_next_entry(image, entry)) {
1038
1039 uint32_t type = ntohl(entry->type);
1040 if (type != CBFS_COMPONENT_NULL)
1041 continue;
1042
1043 addr = cbfs_get_entry_addr(image, entry);
1044 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
1045 image, entry));
1046 if (addr_next - addr < need_len)
1047 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001048
1049 offset = align_up(addr + header_len, align);
1050 if (is_in_same_page(offset, size, page_size) &&
1051 is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001052 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001053 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001054 }
1055
1056 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001057 offset = align_up(addr2, align);
1058 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001059 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001060 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001061 }
1062
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001063 /* Assume page_size >= header_len so adding one page will
1064 * definitely provide the space for header. */
1065 assert(page_size >= header_len);
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001066 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001067 offset = align_up(addr3, align);
1068 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001069 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +08001070 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001071 }
1072 }
1073 return -1;
1074}