blob: 6775b069dde354133a37d89064b35412380a9307 [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>
25
26#include "common.h"
27#include "cbfs_image.h"
28
29/* The file name align is not defined in CBFS spec -- only a preference by
30 * (old) cbfstool. */
31#define CBFS_FILENAME_ALIGN (16)
32
33/* To make CBFS more friendly to ROM, fill -1 (0xFF) instead of zero. */
34#define CBFS_CONTENT_DEFAULT_VALUE (-1)
35
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080036/* Type and format */
37
38struct typedesc_t {
39 uint32_t type;
40 const char *name;
41};
42
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070043static const struct typedesc_t types_cbfs_entry[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080044 {CBFS_COMPONENT_STAGE, "stage"},
45 {CBFS_COMPONENT_PAYLOAD, "payload"},
46 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
47 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
48 {CBFS_COMPONENT_RAW, "raw"},
49 {CBFS_COMPONENT_VSA, "vsa"},
50 {CBFS_COMPONENT_MBI, "mbi"},
51 {CBFS_COMPONENT_MICROCODE, "microcode"},
52 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos_default"},
53 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos_layout"},
54 {CBFS_COMPONENT_DELETED, "deleted"},
55 {CBFS_COMPONENT_NULL, "null"},
56 {0, NULL},
57};
58
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070059static const struct typedesc_t types_cbfs_compression[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080060 {CBFS_COMPRESS_NONE, "none"},
61 {CBFS_COMPRESS_LZMA, "LZMA"},
62 {0, NULL},
63};
64
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070065static uint32_t align_up(uint32_t value, uint32_t align)
66{
67 if (value % align)
68 value += align - (value % align);
69 return value;
70}
71
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080072static uint32_t lookup_type_by_name(const struct typedesc_t *desc, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070073 uint32_t default_value)
74{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080075 int i;
76 for (i = 0; desc[i].name; i++)
77 if (strcmp(desc[i].name, name) == 0)
78 return desc[i].type;
79 return default_value;
80}
81
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080082static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070083 const char *default_value)
84{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080085 int i;
86 for (i = 0; desc[i].name; i++)
87 if (desc[i].type == type)
88 return desc[i].name;
89 return default_value;
90}
91
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070092uint32_t get_cbfs_entry_type(const char *name, uint32_t default_value)
93{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080094 return lookup_type_by_name(types_cbfs_entry, name, default_value);
95}
96
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070097const char *get_cbfs_entry_type_name(uint32_t type)
98{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080099 return lookup_name_by_type(types_cbfs_entry, type, "(unknown)");
100}
101
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700102uint32_t get_cbfs_compression(const char *name, uint32_t unknown)
103{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800104 return lookup_type_by_name(types_cbfs_compression, name, unknown);
105}
106
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800107/* CBFS image */
108
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700109static int cbfs_calculate_file_header_size(const char *name)
110{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800111 return (sizeof(struct cbfs_file) +
112 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
113}
114
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600115static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700116{
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800117 // A bug in old cbfstool may produce extra few bytes (by alignment) and
118 // cause cbfstool to overwrite things after free space -- which is
119 // usually CBFS header on x86. We need to workaround that.
120
121 struct cbfs_file *entry, *first = NULL, *last = NULL;
122 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800123 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800124 entry = cbfs_find_next_entry(image, entry)) {
125 last = entry;
126 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600127 if ((char *)first < (char *)hdr_loc &&
128 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800129 WARN("CBFS image was created with old cbfstool with size bug. "
130 "Fixing size in last entry...\n");
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600131 last->len = htonl(ntohl(last->len) - image->header->align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800132 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
133 cbfs_get_entry_addr(image, entry),
134 cbfs_get_entry_addr(image,
135 cbfs_find_next_entry(image, last)));
136 }
137 return 0;
138}
139
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800140void cbfs_put_header(void *dest, const struct cbfs_header *header)
141{
142 struct buffer outheader;
143
144 outheader.data = dest;
145 outheader.size = 0;
146
147 xdr_be.put32(&outheader, header->magic);
148 xdr_be.put32(&outheader, header->version);
149 xdr_be.put32(&outheader, header->romsize);
150 xdr_be.put32(&outheader, header->bootblocksize);
151 xdr_be.put32(&outheader, header->align);
152 xdr_be.put32(&outheader, header->offset);
153 xdr_be.put32(&outheader, header->architecture);
154}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600155
Hung-Te Lin0780d672014-05-16 10:14:05 +0800156static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
157 struct cbfs_payload_segment *input)
158{
159 struct buffer seg = {
160 .data = (void *)input,
161 .size = sizeof(*input),
162 };
163 output->type = xdr_be.get32(&seg);
164 output->compression = xdr_be.get32(&seg);
165 output->offset = xdr_be.get32(&seg);
166 output->load_addr = xdr_be.get64(&seg);
167 output->len = xdr_be.get32(&seg);
168 output->mem_len = xdr_be.get32(&seg);
169 assert(seg.size == 0);
170}
171
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600172void cbfs_get_header(struct cbfs_header *header, const void *src)
173{
174 struct buffer outheader;
175
176 outheader.data = (void *)src; /* We're not modifying the data */
177 outheader.size = 0;
178
179 header->magic = xdr_be.get32(&outheader);
180 header->version = xdr_be.get32(&outheader);
181 header->romsize = xdr_be.get32(&outheader);
182 header->bootblocksize = xdr_be.get32(&outheader);
183 header->align = xdr_be.get32(&outheader);
184 header->offset = xdr_be.get32(&outheader);
185 header->architecture = xdr_be.get32(&outheader);
186}
187
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800188int cbfs_image_create(struct cbfs_image *image,
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800189 uint32_t myarch,
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800190 size_t size,
191 uint32_t align,
192 struct buffer *bootblock,
193 int32_t bootblock_offset,
194 int32_t header_offset,
195 int32_t entries_offset)
196{
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800197 struct cbfs_header header;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800198 struct cbfs_file *entry;
199 uint32_t cbfs_len;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800200 size_t entry_header_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600201 void *header_loc;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800202
203 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
204 "header=0x%x+0x%zx, entries_offset=0x%x\n",
205 bootblock_offset, bootblock->size,
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800206 header_offset, sizeof(header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800207
208 if (buffer_create(&image->buffer, size, "(new)") != 0)
209 return -1;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600210 if ((image->header = malloc(sizeof(*image->header))) == NULL)
211 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800212 memset(image->buffer.data, CBFS_CONTENT_DEFAULT_VALUE, size);
213
214 // Adjust legcay top-aligned address to ROM offset.
215 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
216 entries_offset += (int32_t)size;
217 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
218 bootblock_offset += (int32_t)size;
219 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
220 header_offset += (int32_t) size;
221
222 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
223 "header=0x%x, entries_offset=0x%x\n",
224 bootblock_offset, header_offset, entries_offset);
225
226 if (align == 0)
227 align = 64; // default align size.
228
229 // Prepare bootblock
230 if (bootblock_offset + bootblock->size > size) {
231 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
232 bootblock_offset, bootblock->size, size);
233 return -1;
234 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800235 if (entries_offset > bootblock_offset &&
236 entries_offset < bootblock->size) {
237 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
238 bootblock_offset, bootblock->size, entries_offset);
239 return -1;
240 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800241 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
242 bootblock->size);
243
244 // Prepare header
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800245 if (header_offset + sizeof(header) > size) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800246 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800247 header_offset, sizeof(header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800248 return -1;
249 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600250 image->header->magic = CBFS_HEADER_MAGIC;
251 image->header->version = CBFS_HEADER_VERSION;
252 image->header->romsize = size;
253 image->header->bootblocksize = bootblock->size;
254 image->header->align = align;
255 image->header->offset = entries_offset;
256 image->header->architecture = myarch;
257
258 header_loc = (image->buffer.data + header_offset);
259 cbfs_put_header(header_loc, image->header);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800260
261 // Prepare entries
262 if (align_up(entries_offset, align) != entries_offset) {
263 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
264 entries_offset, align);
265 return -1;
266 }
Hung-Te Linb02c8732013-03-15 17:40:08 +0800267 entry_header_len = cbfs_calculate_file_header_size("");
268 if (entries_offset + entry_header_len > size) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800269 ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n",
Hung-Te Linb02c8732013-03-15 17:40:08 +0800270 entries_offset, entry_header_len, size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800271 return -1;
272 }
273 entry = (struct cbfs_file *)(image->buffer.data + entries_offset);
274 // To calculate available length, find
275 // e = min(bootblock, header, size) where e > entries_offset.
276 cbfs_len = size;
277 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
278 cbfs_len = bootblock_offset;
279 if (header_offset > entries_offset && header_offset < cbfs_len)
280 cbfs_len = header_offset;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800281 cbfs_len -= entries_offset + align + entry_header_len;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800282 cbfs_create_empty_entry(image, entry, cbfs_len, "");
283 LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len);
284 return 0;
285}
286
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700287int cbfs_image_from_file(struct cbfs_image *image, const char *filename)
288{
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600289 void *header_loc;
290
Hung-Te Lineab2c812013-01-29 01:56:17 +0800291 if (buffer_from_file(&image->buffer, filename) != 0)
292 return -1;
293 DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
294 image->buffer.size);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600295 header_loc = cbfs_find_header(image->buffer.data, image->buffer.size);
296 if (!header_loc) {
Hung-Te Lineab2c812013-01-29 01:56:17 +0800297 ERROR("%s does not have CBFS master header.\n", filename);
298 cbfs_image_delete(image);
299 return -1;
300 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600301
302 if ((image->header = malloc(sizeof(*image->header))) == NULL)
303 return -1;
304
305 cbfs_get_header(image->header, header_loc);
306 cbfs_fix_legacy_size(image, header_loc);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800307
308 return 0;
309}
310
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700311int cbfs_image_write_file(struct cbfs_image *image, const char *filename)
312{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800313 assert(image && image->buffer.data);
314 return buffer_write_file(&image->buffer, filename);
315}
316
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700317int cbfs_image_delete(struct cbfs_image *image)
318{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100319 if (image == NULL)
320 return 0;
321
Hung-Te Lineab2c812013-01-29 01:56:17 +0800322 buffer_delete(&image->buffer);
323 image->header = NULL;
324 return 0;
325}
326
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800327/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
328static int cbfs_add_entry_at(struct cbfs_image *image,
329 struct cbfs_file *entry,
330 uint32_t size,
331 const char *name,
332 uint32_t type,
333 const void *data,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700334 uint32_t content_offset)
335{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800336 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
337 uint32_t addr = cbfs_get_entry_addr(image, entry),
338 addr_next = cbfs_get_entry_addr(image, next);
339 uint32_t header_size = cbfs_calculate_file_header_size(name),
340 min_entry_size = cbfs_calculate_file_header_size("");
341 uint32_t len, target;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600342 uint32_t align = image->header->align;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800343
344 target = content_offset - header_size;
345 if (target % align)
346 target -= target % align;
347 if (target < addr) {
348 ERROR("No space to hold cbfs_file header.");
349 return -1;
350 }
351
352 // Process buffer BEFORE content_offset.
353 if (target - addr > min_entry_size) {
354 DEBUG("|min|...|header|content|... <create new entry>\n");
355 len = target - addr - min_entry_size;
356 cbfs_create_empty_entry(image, entry, len, "");
357 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
358 entry = cbfs_find_next_entry(image, entry);
359 addr = cbfs_get_entry_addr(image, entry);
360 }
361
362 len = size + (content_offset - addr - header_size);
363 cbfs_create_empty_entry(image, entry, len, name);
364 if (len != size) {
365 DEBUG("|..|header|content|... <use offset to create entry>\n");
366 DEBUG("before: offset=0x%x, len=0x%x\n",
367 ntohl(entry->offset), ntohl(entry->len));
368 // TODO reset expanded name buffer to 0xFF.
369 entry->offset = htonl(ntohl(entry->offset) + (len - size));
370 entry->len = htonl(size);
371 DEBUG("after: offset=0x%x, len=0x%x\n",
372 ntohl(entry->offset), ntohl(entry->len));
373 }
374
375 // Ready to fill data into entry.
376 assert(ntohl(entry->len) == size);
377 entry->type = htonl(type);
378 DEBUG("content_offset: 0x%x, entry location: %x\n",
379 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
380 image->buffer.data));
381 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
382 content_offset);
383 memcpy(CBFS_SUBHEADER(entry), data, size);
384 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
385
386 // Process buffer AFTER entry.
387 entry = cbfs_find_next_entry(image, entry);
388 addr = cbfs_get_entry_addr(image, entry);
389 assert(addr < addr_next);
390
391 if (addr_next - addr < min_entry_size) {
392 DEBUG("No space after content to keep CBFS structure.\n");
393 return -1;
394 }
395
396 len = addr_next - addr - min_entry_size;
397 cbfs_create_empty_entry(image, entry, len, "");
398 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
399 return 0;
400}
401
402int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700403 const char *name, uint32_t type, uint32_t content_offset)
404{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800405 uint32_t entry_type;
406 uint32_t addr, addr_next;
407 struct cbfs_file *entry, *next;
408 uint32_t header_size, need_size, new_size;
409
410 header_size = cbfs_calculate_file_header_size(name);
411
412 need_size = header_size + buffer->size;
413 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
414 name, content_offset, header_size, buffer->size, need_size);
415
416 if (IS_TOP_ALIGNED_ADDRESS(content_offset)) {
417 // legacy cbfstool takes top-aligned address.
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600418 uint32_t theromsize = image->header->romsize;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800419 INFO("Converting top-aligned address 0x%x to offset: 0x%x\n",
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800420 content_offset, content_offset + theromsize);
421 content_offset += theromsize;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800422 }
423
424 // Merge empty entries.
425 DEBUG("(trying to merge empty entries...)\n");
426 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
427
428 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800429 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800430 entry = cbfs_find_next_entry(image, entry)) {
431
432 entry_type = ntohl(entry->type);
433 if (entry_type != CBFS_COMPONENT_NULL)
434 continue;
435
436 addr = cbfs_get_entry_addr(image, entry);
437 next = cbfs_find_next_entry(image, entry);
438 addr_next = cbfs_get_entry_addr(image, next);
439
440 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
441 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600442
443 /* Will the file fit? Don't yet worry if we have space for a new
444 * "empty" entry. We take care of that later.
445 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800446 if (addr + need_size > addr_next)
447 continue;
448
449 // Can we simply put object here?
450 if (!content_offset || content_offset == addr + header_size) {
451 DEBUG("Filling new entry data (%zd bytes).\n",
452 buffer->size);
453 cbfs_create_empty_entry(image, entry, buffer->size,
454 name);
455 entry->type = htonl(type);
456 memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
457 if (verbose)
458 cbfs_print_entry_info(image, entry, stderr);
459
460 // setup new entry
Paul Menzel4159a802013-07-14 00:24:31 +0200461 DEBUG("Setting new empty entry.\n");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800462 entry = cbfs_find_next_entry(image, entry);
463 new_size = (cbfs_get_entry_addr(image, next) -
464 cbfs_get_entry_addr(image, entry));
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600465
466 /* Entry was added and no space for new "empty" entry */
467 if (new_size < cbfs_calculate_file_header_size("")) {
468 DEBUG("No need for new \"empty\" entry\n");
469 /* No need to increase the size of the just
470 * stored file to extend to next file. Alignment
471 * of next file takes care of this.
472 */
473 return 0;
474 }
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800475 new_size -= cbfs_calculate_file_header_size("");
476 DEBUG("new size: %d\n", new_size);
477 cbfs_create_empty_entry(image, entry, new_size, "");
478 if (verbose)
479 cbfs_print_entry_info(image, entry, stderr);
480 return 0;
481 }
482
483 // We need to put content here, and the case is really
484 // complicated...
485 assert(content_offset);
486 if (addr_next < content_offset) {
487 DEBUG("Not for specified offset yet");
488 continue;
489 } else if (addr > content_offset) {
490 DEBUG("Exceed specified content_offset.");
491 break;
492 } else if (addr + header_size > content_offset) {
493 ERROR("Not enough space for header.\n");
494 break;
495 } else if (content_offset + buffer->size > addr_next) {
496 ERROR("Not enough space for content.\n");
497 break;
498 }
499
500 // TODO there are more few tricky cases that we may
501 // want to fit by altering offset.
502 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
503 addr, addr_next - addr, content_offset);
504
505 if (cbfs_add_entry_at(image, entry, buffer->size, name, type,
506 buffer->data, content_offset) == 0) {
507 return 0;
508 }
509 break;
510 }
511
512 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
513 buffer->name, buffer->size, buffer->size / 1024, content_offset);
514 return -1;
515}
516
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700517struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
518{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800519 struct cbfs_file *entry;
520 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800521 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800522 entry = cbfs_find_next_entry(image, entry)) {
523 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
524 DEBUG("cbfs_get_entry: found %s\n", name);
525 return entry;
526 }
527 }
528 return NULL;
529}
530
531int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700532 const char *filename)
533{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800534 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
535 struct buffer buffer;
536 if (!entry) {
537 ERROR("File not found: %s\n", entry_name);
538 return -1;
539 }
540 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
541 entry_name, cbfs_get_entry_addr(image, entry),
542 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
543
544 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
545 WARN("Only 'raw' files are safe to extract.\n");
546 }
547
548 buffer.data = CBFS_SUBHEADER(entry);
549 buffer.size = ntohl(entry->len);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800550 buffer.name = (char *)"(cbfs_export_entry)";
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800551 if (buffer_write_file(&buffer, filename) != 0) {
552 ERROR("Failed to write %s into %s.\n",
553 entry_name, filename);
554 return -1;
555 }
556 INFO("Successfully dumped the file to: %s\n", filename);
557 return 0;
558}
559
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700560int cbfs_remove_entry(struct cbfs_image *image, const char *name)
561{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800562 struct cbfs_file *entry, *next;
563 size_t len;
564 entry = cbfs_get_entry(image, name);
565 if (!entry) {
566 ERROR("CBFS file %s not found.\n", name);
567 return -1;
568 }
569 next = cbfs_find_next_entry(image, entry);
570 assert(next);
571 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
572 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
573 entry->type = htonl(CBFS_COMPONENT_DELETED);
574 len = (cbfs_get_entry_addr(image, next) -
575 cbfs_get_entry_addr(image, entry));
576 entry->offset = htonl(cbfs_calculate_file_header_size(""));
577 entry->len = htonl(len - ntohl(entry->offset));
578 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
579 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
580 ntohl(entry->len));
581 return 0;
582}
583
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700584int cbfs_print_header_info(struct cbfs_image *image)
585{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800586 char *name = strdup(image->buffer.name);
587 assert(image && image->header);
588 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800589 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800590 basename(name),
591 image->buffer.size / 1024,
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600592 image->header->bootblocksize,
593 image->header->romsize,
594 image->header->offset,
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800595 image->header->align,
596 arch_to_string(image->header->architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800597 free(name);
598 return 0;
599}
600
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700601static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
602{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800603 fprintf(fp,
604 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
605 "length: %d/%d\n",
606 lookup_name_by_type(types_cbfs_compression,
607 stage->compression, "(unknown)"),
608 stage->entry,
609 stage->load,
610 stage->len,
611 stage->memlen);
612 return 0;
613}
614
Hung-Te Lin0780d672014-05-16 10:14:05 +0800615static int cbfs_print_decoded_payload_segment_info(
616 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800617{
Hung-Te Lin0780d672014-05-16 10:14:05 +0800618 /* The input (seg) must be already decoded by
619 * cbfs_decode_payload_segment.
620 */
621 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800622 case PAYLOAD_SEGMENT_CODE:
623 case PAYLOAD_SEGMENT_DATA:
624 fprintf(fp, " %s (%s compression, offset: 0x%x, "
625 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800626 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800627 "code " : "data"),
628 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800629 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800630 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800631 seg->offset, seg->load_addr, seg->len,
632 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800633 break;
634
635 case PAYLOAD_SEGMENT_ENTRY:
636 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800637 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800638 break;
639
640 case PAYLOAD_SEGMENT_BSS:
641 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
642 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800643 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800644 break;
645
646 case PAYLOAD_SEGMENT_PARAMS:
647 fprintf(fp, " parameters\n");
648 break;
649
650 default:
651 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
652 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800653 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800654 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800655 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800656 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800657 seg->offset, seg->load_addr, seg->len,
658 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800659 break;
660 }
661 return 0;
662}
663
664int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700665 void *arg)
666{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800667 const char *name = CBFS_NAME(entry);
668 struct cbfs_payload_segment *payload;
669 FILE *fp = (FILE *)arg;
670
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800671 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800672 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
673 cbfs_get_entry_addr(image, entry));
674 return -1;
675 }
676 if (!fp)
677 fp = stdout;
678
679 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
680 *name ? name : "(empty)",
681 cbfs_get_entry_addr(image, entry),
682 get_cbfs_entry_type_name(ntohl(entry->type)),
683 ntohl(entry->len));
684
685 if (!verbose)
686 return 0;
687
688 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
689 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
690 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
691 ntohl(entry->len));
692
693 /* note the components of the subheader may be in host order ... */
694 switch (ntohl(entry->type)) {
695 case CBFS_COMPONENT_STAGE:
696 cbfs_print_stage_info((struct cbfs_stage *)
697 CBFS_SUBHEADER(entry), fp);
698 break;
699
700 case CBFS_COMPONENT_PAYLOAD:
701 payload = (struct cbfs_payload_segment *)
702 CBFS_SUBHEADER(entry);
703 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +0800704 struct cbfs_payload_segment seg;
705 cbfs_decode_payload_segment(&seg, payload);
706 cbfs_print_decoded_payload_segment_info(
707 &seg, fp);
708 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800709 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +0800710 else
Aaron Durbinca630272014-08-05 10:48:20 -0500711 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800712 }
713 break;
714 default:
715 break;
716 }
717 return 0;
718}
719
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700720int cbfs_print_directory(struct cbfs_image *image)
721{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800722 cbfs_print_header_info(image);
723 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
724 cbfs_walk(image, cbfs_print_entry_info, NULL);
725 return 0;
726}
727
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800728int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700729 void *arg)
730{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800731 struct cbfs_file *next;
732 uint32_t type, addr, last_addr;
733
734 type = ntohl(entry->type);
735 if (type == CBFS_COMPONENT_DELETED) {
736 // Ready to be recycled.
737 type = CBFS_COMPONENT_NULL;
738 entry->type = htonl(type);
739 }
740 if (type != CBFS_COMPONENT_NULL)
741 return 0;
742
743 next = cbfs_find_next_entry(image, entry);
744
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800745 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800746 type = ntohl(next->type);
747 if (type == CBFS_COMPONENT_DELETED) {
748 type = CBFS_COMPONENT_NULL;
749 next->type = htonl(type);
750 }
751 if (type != CBFS_COMPONENT_NULL)
752 return 0;
753
754 addr = cbfs_get_entry_addr(image, entry);
755 last_addr = cbfs_get_entry_addr(
756 image, cbfs_find_next_entry(image, next));
757
758 // Now, we find two deleted/empty entries; try to merge now.
759 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
760 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
761 cbfs_get_entry_addr(image, next), ntohl(next->len));
762 cbfs_create_empty_entry(image, entry,
763 (last_addr - addr -
764 cbfs_calculate_file_header_size("")),
765 "");
766 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
767 next = cbfs_find_next_entry(image, entry);
768 }
769 return 0;
770}
771
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800772int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700773 void *arg)
774{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800775 int count = 0;
776 struct cbfs_file *entry;
777 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800778 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800779 entry = cbfs_find_next_entry(image, entry)) {
780 count ++;
781 if (callback(image, entry, arg) != 0)
782 break;
783 }
784 return count;
785}
786
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700787struct cbfs_header *cbfs_find_header(char *data, size_t size)
788{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800789 size_t offset;
790 int found = 0;
791 uint32_t x86sig;
792 struct cbfs_header *header, *result = NULL;
793
794 // Try x86 style (check signature in bottom) header first.
795 x86sig = *(uint32_t *)(data + size - sizeof(uint32_t));
796 offset = (x86sig + (uint32_t)size);
797 DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset);
798 if (offset >= size - sizeof(*header) ||
799 ntohl(((struct cbfs_header *)(data + offset))->magic) !=
800 CBFS_HEADER_MAGIC)
801 offset = 0;
802
803 for (; offset + sizeof(*header) < size; offset++) {
804 header = (struct cbfs_header *)(data + offset);
805 if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC))
806 continue;
807 if (ntohl(header->version) != CBFS_HEADER_VERSION1 &&
808 ntohl(header->version) != CBFS_HEADER_VERSION2) {
809 // Probably not a real CBFS header?
810 continue;
811 }
812 found++;
813 result = header;
814 }
815 if (found > 1) {
816 ERROR("multiple (%d) CBFS headers found!\n",
817 found);
818 result = NULL;
819 }
820 return result;
821}
822
823
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700824struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
825{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800826 assert(image && image->header);
827 return (struct cbfs_file *)(image->buffer.data +
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600828 image->header->offset);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800829}
830
831struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700832 struct cbfs_file *entry)
833{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800834 uint32_t addr = cbfs_get_entry_addr(image, entry);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600835 int align = image->header->align;
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800836 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +0800837 addr += ntohl(entry->offset) + ntohl(entry->len);
838 addr = align_up(addr, align);
839 return (struct cbfs_file *)(image->buffer.data + addr);
840}
841
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700842uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
843{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800844 assert(image && image->buffer.data && entry);
845 return (int32_t)((char *)entry - image->buffer.data);
846}
847
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700848int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
849{
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800850 return (entry &&
851 (char *)entry >= image->buffer.data &&
852 (char *)entry + sizeof(entry->magic) <
853 image->buffer.data + image->buffer.size &&
854 memcmp(entry->magic, CBFS_FILE_MAGIC,
855 sizeof(entry->magic)) == 0);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800856}
857
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800858int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700859 size_t len, const char *name)
860{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800861 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
862 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
863 entry->type = htonl(CBFS_COMPONENT_NULL);
864 entry->len = htonl(len);
865 entry->checksum = 0; // TODO Build a checksum algorithm.
866 entry->offset = htonl(cbfs_calculate_file_header_size(name));
867 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
868 strcpy(CBFS_NAME(entry), name);
869 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
870 return 0;
871}
872
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700873/* Finds a place to hold whole data in same memory page. */
874static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
875{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800876 if (!page)
877 return 1;
878 return (start / page) == (start + size - 1) / page;
879}
880
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800881/* Tests if data can fit in a range by given offset:
882 * start ->| header_len | offset (+ size) |<- end
883 */
884static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700885 uint32_t offset, uint32_t size)
886{
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800887 return (offset >= start + header_len && offset + size <= end);
888}
889
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800890int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700891 uint32_t size, uint32_t page_size, uint32_t align)
892{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800893 struct cbfs_file *entry;
894 size_t need_len;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800895 uint32_t addr, addr_next, addr2, addr3, offset, header_len;
896
897 /* Default values: allow fitting anywhere in ROM. */
898 if (!page_size)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600899 page_size = image->header->romsize;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800900 if (!align)
901 align = 1;
902
903 if (size > page_size)
904 ERROR("Input file size (%d) greater than page size (%d).\n",
905 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800906
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600907 if (page_size % image->header->align)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800908 WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600909 __func__, page_size, image->header->align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800910
911 /* TODO Old cbfstool always assume input is a stage file (and adding
912 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800913 * (type) param in future. For right now, we assume cbfs_stage is the
914 * largest structure and add it into header size. */
915 assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800916 header_len = (cbfs_calculate_file_header_size(name) +
917 sizeof(struct cbfs_stage));
918 need_len = header_len + size;
919
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800920 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800921 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
922
923 /* Three cases of content location on memory page:
924 * case 1.
925 * | PAGE 1 | PAGE 2 |
926 * | <header><content>| Fit. Return start of content.
927 *
928 * case 2.
929 * | PAGE 1 | PAGE 2 |
930 * | <header><content> | Fits when we shift content to align
931 * shift-> | <header>|<content> | at starting of PAGE 2.
932 *
933 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800934 * | PAGE 1 | PAGE 2 | PAGE 3 |
935 * | <header>< content > | Can't fit. If we shift content to
936 * |trial-> <header>< content > | PAGE 2, header can't fit in free
937 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800938 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800939 * The returned address can be then used as "base-address" (-b) in add-*
940 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
941 * For stage targets, the address is also used to re-link stage before
942 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800943 */
944 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800945 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800946 entry = cbfs_find_next_entry(image, entry)) {
947
948 uint32_t type = ntohl(entry->type);
949 if (type != CBFS_COMPONENT_NULL)
950 continue;
951
952 addr = cbfs_get_entry_addr(image, entry);
953 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
954 image, entry));
955 if (addr_next - addr < need_len)
956 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800957
958 offset = align_up(addr + header_len, align);
959 if (is_in_same_page(offset, size, page_size) &&
960 is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800961 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800962 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800963 }
964
965 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800966 offset = align_up(addr2, align);
967 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800968 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800969 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800970 }
971
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800972 /* Assume page_size >= header_len so adding one page will
973 * definitely provide the space for header. */
974 assert(page_size >= header_len);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800975 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800976 offset = align_up(addr3, align);
977 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800978 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800979 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800980 }
981 }
982 return -1;
983}