blob: 6b0ed59efc80a28b1ec139643256d3bca6954983 [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 const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070073 const char *default_value)
74{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080075 int i;
76 for (i = 0; desc[i].name; i++)
77 if (desc[i].type == type)
78 return desc[i].name;
79 return default_value;
80}
81
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010082static const char *get_cbfs_entry_type_name(uint32_t type)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070083{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080084 return lookup_name_by_type(types_cbfs_entry, type, "(unknown)");
85}
86
Hung-Te Linc03d9b02013-01-29 02:38:40 +080087/* CBFS image */
88
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070089static int cbfs_calculate_file_header_size(const char *name)
90{
Hung-Te Linc03d9b02013-01-29 02:38:40 +080091 return (sizeof(struct cbfs_file) +
92 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
93}
94
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -060095static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070096{
Hung-Te Lin49fcd752013-01-29 03:16:20 +080097 // A bug in old cbfstool may produce extra few bytes (by alignment) and
98 // cause cbfstool to overwrite things after free space -- which is
99 // usually CBFS header on x86. We need to workaround that.
100
101 struct cbfs_file *entry, *first = NULL, *last = NULL;
102 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800103 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800104 entry = cbfs_find_next_entry(image, entry)) {
105 last = entry;
106 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600107 if ((char *)first < (char *)hdr_loc &&
108 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800109 WARN("CBFS image was created with old cbfstool with size bug. "
110 "Fixing size in last entry...\n");
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600111 last->len = htonl(ntohl(last->len) - image->header->align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800112 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
113 cbfs_get_entry_addr(image, entry),
114 cbfs_get_entry_addr(image,
115 cbfs_find_next_entry(image, last)));
116 }
117 return 0;
118}
119
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800120void cbfs_put_header(void *dest, const struct cbfs_header *header)
121{
122 struct buffer outheader;
123
124 outheader.data = dest;
125 outheader.size = 0;
126
127 xdr_be.put32(&outheader, header->magic);
128 xdr_be.put32(&outheader, header->version);
129 xdr_be.put32(&outheader, header->romsize);
130 xdr_be.put32(&outheader, header->bootblocksize);
131 xdr_be.put32(&outheader, header->align);
132 xdr_be.put32(&outheader, header->offset);
133 xdr_be.put32(&outheader, header->architecture);
134}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600135
Hung-Te Lin0780d672014-05-16 10:14:05 +0800136static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
137 struct cbfs_payload_segment *input)
138{
139 struct buffer seg = {
140 .data = (void *)input,
141 .size = sizeof(*input),
142 };
143 output->type = xdr_be.get32(&seg);
144 output->compression = xdr_be.get32(&seg);
145 output->offset = xdr_be.get32(&seg);
146 output->load_addr = xdr_be.get64(&seg);
147 output->len = xdr_be.get32(&seg);
148 output->mem_len = xdr_be.get32(&seg);
149 assert(seg.size == 0);
150}
151
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600152void cbfs_get_header(struct cbfs_header *header, const void *src)
153{
154 struct buffer outheader;
155
156 outheader.data = (void *)src; /* We're not modifying the data */
157 outheader.size = 0;
158
159 header->magic = xdr_be.get32(&outheader);
160 header->version = xdr_be.get32(&outheader);
161 header->romsize = xdr_be.get32(&outheader);
162 header->bootblocksize = xdr_be.get32(&outheader);
163 header->align = xdr_be.get32(&outheader);
164 header->offset = xdr_be.get32(&outheader);
165 header->architecture = xdr_be.get32(&outheader);
166}
167
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800168int cbfs_image_create(struct cbfs_image *image,
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100169 uint32_t architecture,
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800170 size_t size,
171 uint32_t align,
172 struct buffer *bootblock,
173 int32_t bootblock_offset,
174 int32_t header_offset,
175 int32_t entries_offset)
176{
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800177 struct cbfs_header header;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800178 struct cbfs_file *entry;
179 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))
196 entries_offset += (int32_t)size;
197 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
198 bootblock_offset += (int32_t)size;
199 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
200 header_offset += (int32_t) size;
201
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
206 if (align == 0)
207 align = 64; // default align size.
208
209 // Prepare bootblock
210 if (bootblock_offset + bootblock->size > size) {
211 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
212 bootblock_offset, bootblock->size, size);
213 return -1;
214 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800215 if (entries_offset > bootblock_offset &&
216 entries_offset < bootblock->size) {
217 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
218 bootblock_offset, bootblock->size, entries_offset);
219 return -1;
220 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800221 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
222 bootblock->size);
223
224 // Prepare header
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800225 if (header_offset + sizeof(header) > size) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800226 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800227 header_offset, sizeof(header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800228 return -1;
229 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600230 image->header->magic = CBFS_HEADER_MAGIC;
231 image->header->version = CBFS_HEADER_VERSION;
232 image->header->romsize = size;
233 image->header->bootblocksize = bootblock->size;
234 image->header->align = align;
235 image->header->offset = entries_offset;
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100236 image->header->architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600237
238 header_loc = (image->buffer.data + header_offset);
239 cbfs_put_header(header_loc, image->header);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800240
241 // Prepare entries
242 if (align_up(entries_offset, align) != entries_offset) {
243 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
244 entries_offset, align);
245 return -1;
246 }
Hung-Te Linb02c8732013-03-15 17:40:08 +0800247 entry_header_len = cbfs_calculate_file_header_size("");
248 if (entries_offset + entry_header_len > size) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800249 ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n",
Hung-Te Linb02c8732013-03-15 17:40:08 +0800250 entries_offset, entry_header_len, size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800251 return -1;
252 }
253 entry = (struct cbfs_file *)(image->buffer.data + entries_offset);
254 // To calculate available length, find
255 // e = min(bootblock, header, size) where e > entries_offset.
256 cbfs_len = size;
257 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
258 cbfs_len = bootblock_offset;
259 if (header_offset > entries_offset && header_offset < cbfs_len)
260 cbfs_len = header_offset;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800261 cbfs_len -= entries_offset + align + entry_header_len;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800262 cbfs_create_empty_entry(image, entry, cbfs_len, "");
263 LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len);
264 return 0;
265}
266
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700267int cbfs_image_from_file(struct cbfs_image *image, const char *filename)
268{
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600269 void *header_loc;
270
Hung-Te Lineab2c812013-01-29 01:56:17 +0800271 if (buffer_from_file(&image->buffer, filename) != 0)
272 return -1;
273 DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
274 image->buffer.size);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600275 header_loc = cbfs_find_header(image->buffer.data, image->buffer.size);
276 if (!header_loc) {
Hung-Te Lineab2c812013-01-29 01:56:17 +0800277 ERROR("%s does not have CBFS master header.\n", filename);
278 cbfs_image_delete(image);
279 return -1;
280 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600281
282 if ((image->header = malloc(sizeof(*image->header))) == NULL)
283 return -1;
284
285 cbfs_get_header(image->header, header_loc);
286 cbfs_fix_legacy_size(image, header_loc);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800287
288 return 0;
289}
290
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700291int cbfs_image_write_file(struct cbfs_image *image, const char *filename)
292{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800293 assert(image && image->buffer.data);
294 return buffer_write_file(&image->buffer, filename);
295}
296
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700297int cbfs_image_delete(struct cbfs_image *image)
298{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100299 if (image == NULL)
300 return 0;
301
Hung-Te Lineab2c812013-01-29 01:56:17 +0800302 buffer_delete(&image->buffer);
303 image->header = NULL;
304 return 0;
305}
306
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800307/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
308static int cbfs_add_entry_at(struct cbfs_image *image,
309 struct cbfs_file *entry,
310 uint32_t size,
311 const char *name,
312 uint32_t type,
313 const void *data,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700314 uint32_t content_offset)
315{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800316 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
317 uint32_t addr = cbfs_get_entry_addr(image, entry),
318 addr_next = cbfs_get_entry_addr(image, next);
319 uint32_t header_size = cbfs_calculate_file_header_size(name),
320 min_entry_size = cbfs_calculate_file_header_size("");
321 uint32_t len, target;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600322 uint32_t align = image->header->align;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800323
324 target = content_offset - header_size;
325 if (target % align)
326 target -= target % align;
327 if (target < addr) {
328 ERROR("No space to hold cbfs_file header.");
329 return -1;
330 }
331
332 // Process buffer BEFORE content_offset.
333 if (target - addr > min_entry_size) {
334 DEBUG("|min|...|header|content|... <create new entry>\n");
335 len = target - addr - min_entry_size;
336 cbfs_create_empty_entry(image, entry, len, "");
337 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
338 entry = cbfs_find_next_entry(image, entry);
339 addr = cbfs_get_entry_addr(image, entry);
340 }
341
342 len = size + (content_offset - addr - header_size);
343 cbfs_create_empty_entry(image, entry, len, name);
344 if (len != size) {
345 DEBUG("|..|header|content|... <use offset to create entry>\n");
346 DEBUG("before: offset=0x%x, len=0x%x\n",
347 ntohl(entry->offset), ntohl(entry->len));
348 // TODO reset expanded name buffer to 0xFF.
349 entry->offset = htonl(ntohl(entry->offset) + (len - size));
350 entry->len = htonl(size);
351 DEBUG("after: offset=0x%x, len=0x%x\n",
352 ntohl(entry->offset), ntohl(entry->len));
353 }
354
355 // Ready to fill data into entry.
356 assert(ntohl(entry->len) == size);
357 entry->type = htonl(type);
358 DEBUG("content_offset: 0x%x, entry location: %x\n",
359 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
360 image->buffer.data));
361 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
362 content_offset);
363 memcpy(CBFS_SUBHEADER(entry), data, size);
364 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
365
366 // Process buffer AFTER entry.
367 entry = cbfs_find_next_entry(image, entry);
368 addr = cbfs_get_entry_addr(image, entry);
369 assert(addr < addr_next);
370
371 if (addr_next - addr < min_entry_size) {
372 DEBUG("No space after content to keep CBFS structure.\n");
373 return -1;
374 }
375
376 len = addr_next - addr - min_entry_size;
377 cbfs_create_empty_entry(image, entry, len, "");
378 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
379 return 0;
380}
381
382int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700383 const char *name, uint32_t type, uint32_t content_offset)
384{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800385 uint32_t entry_type;
386 uint32_t addr, addr_next;
387 struct cbfs_file *entry, *next;
388 uint32_t header_size, need_size, new_size;
389
390 header_size = cbfs_calculate_file_header_size(name);
391
392 need_size = header_size + buffer->size;
393 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
394 name, content_offset, header_size, buffer->size, need_size);
395
396 if (IS_TOP_ALIGNED_ADDRESS(content_offset)) {
397 // legacy cbfstool takes top-aligned address.
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600398 uint32_t theromsize = image->header->romsize;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800399 INFO("Converting top-aligned address 0x%x to offset: 0x%x\n",
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800400 content_offset, content_offset + theromsize);
401 content_offset += theromsize;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800402 }
403
404 // Merge empty entries.
405 DEBUG("(trying to merge empty entries...)\n");
406 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
407
408 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800409 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800410 entry = cbfs_find_next_entry(image, entry)) {
411
412 entry_type = ntohl(entry->type);
413 if (entry_type != CBFS_COMPONENT_NULL)
414 continue;
415
416 addr = cbfs_get_entry_addr(image, entry);
417 next = cbfs_find_next_entry(image, entry);
418 addr_next = cbfs_get_entry_addr(image, next);
419
420 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
421 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600422
423 /* Will the file fit? Don't yet worry if we have space for a new
424 * "empty" entry. We take care of that later.
425 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800426 if (addr + need_size > addr_next)
427 continue;
428
429 // Can we simply put object here?
430 if (!content_offset || content_offset == addr + header_size) {
431 DEBUG("Filling new entry data (%zd bytes).\n",
432 buffer->size);
433 cbfs_create_empty_entry(image, entry, buffer->size,
434 name);
435 entry->type = htonl(type);
436 memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
437 if (verbose)
438 cbfs_print_entry_info(image, entry, stderr);
439
440 // setup new entry
Paul Menzel4159a802013-07-14 00:24:31 +0200441 DEBUG("Setting new empty entry.\n");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800442 entry = cbfs_find_next_entry(image, entry);
443 new_size = (cbfs_get_entry_addr(image, next) -
444 cbfs_get_entry_addr(image, entry));
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600445
446 /* Entry was added and no space for new "empty" entry */
447 if (new_size < cbfs_calculate_file_header_size("")) {
448 DEBUG("No need for new \"empty\" entry\n");
449 /* No need to increase the size of the just
450 * stored file to extend to next file. Alignment
451 * of next file takes care of this.
452 */
453 return 0;
454 }
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800455 new_size -= cbfs_calculate_file_header_size("");
456 DEBUG("new size: %d\n", new_size);
457 cbfs_create_empty_entry(image, entry, new_size, "");
458 if (verbose)
459 cbfs_print_entry_info(image, entry, stderr);
460 return 0;
461 }
462
463 // We need to put content here, and the case is really
464 // complicated...
465 assert(content_offset);
466 if (addr_next < content_offset) {
467 DEBUG("Not for specified offset yet");
468 continue;
469 } else if (addr > content_offset) {
470 DEBUG("Exceed specified content_offset.");
471 break;
472 } else if (addr + header_size > content_offset) {
473 ERROR("Not enough space for header.\n");
474 break;
475 } else if (content_offset + buffer->size > addr_next) {
476 ERROR("Not enough space for content.\n");
477 break;
478 }
479
480 // TODO there are more few tricky cases that we may
481 // want to fit by altering offset.
482 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
483 addr, addr_next - addr, content_offset);
484
485 if (cbfs_add_entry_at(image, entry, buffer->size, name, type,
486 buffer->data, content_offset) == 0) {
487 return 0;
488 }
489 break;
490 }
491
492 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
493 buffer->name, buffer->size, buffer->size / 1024, content_offset);
494 return -1;
495}
496
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700497struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
498{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800499 struct cbfs_file *entry;
500 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800501 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800502 entry = cbfs_find_next_entry(image, entry)) {
503 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
504 DEBUG("cbfs_get_entry: found %s\n", name);
505 return entry;
506 }
507 }
508 return NULL;
509}
510
511int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700512 const char *filename)
513{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800514 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
515 struct buffer buffer;
516 if (!entry) {
517 ERROR("File not found: %s\n", entry_name);
518 return -1;
519 }
520 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
521 entry_name, cbfs_get_entry_addr(image, entry),
522 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
523
524 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
525 WARN("Only 'raw' files are safe to extract.\n");
526 }
527
528 buffer.data = CBFS_SUBHEADER(entry);
529 buffer.size = ntohl(entry->len);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800530 buffer.name = (char *)"(cbfs_export_entry)";
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800531 if (buffer_write_file(&buffer, filename) != 0) {
532 ERROR("Failed to write %s into %s.\n",
533 entry_name, filename);
534 return -1;
535 }
536 INFO("Successfully dumped the file to: %s\n", filename);
537 return 0;
538}
539
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700540int cbfs_remove_entry(struct cbfs_image *image, const char *name)
541{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800542 struct cbfs_file *entry, *next;
543 size_t len;
544 entry = cbfs_get_entry(image, name);
545 if (!entry) {
546 ERROR("CBFS file %s not found.\n", name);
547 return -1;
548 }
549 next = cbfs_find_next_entry(image, entry);
550 assert(next);
551 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
552 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
553 entry->type = htonl(CBFS_COMPONENT_DELETED);
554 len = (cbfs_get_entry_addr(image, next) -
555 cbfs_get_entry_addr(image, entry));
556 entry->offset = htonl(cbfs_calculate_file_header_size(""));
557 entry->len = htonl(len - ntohl(entry->offset));
558 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
559 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
560 ntohl(entry->len));
561 return 0;
562}
563
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700564int cbfs_print_header_info(struct cbfs_image *image)
565{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800566 char *name = strdup(image->buffer.name);
567 assert(image && image->header);
568 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800569 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800570 basename(name),
571 image->buffer.size / 1024,
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600572 image->header->bootblocksize,
573 image->header->romsize,
574 image->header->offset,
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800575 image->header->align,
576 arch_to_string(image->header->architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800577 free(name);
578 return 0;
579}
580
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700581static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
582{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800583 fprintf(fp,
584 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
585 "length: %d/%d\n",
586 lookup_name_by_type(types_cbfs_compression,
587 stage->compression, "(unknown)"),
588 stage->entry,
589 stage->load,
590 stage->len,
591 stage->memlen);
592 return 0;
593}
594
Hung-Te Lin0780d672014-05-16 10:14:05 +0800595static int cbfs_print_decoded_payload_segment_info(
596 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800597{
Hung-Te Lin0780d672014-05-16 10:14:05 +0800598 /* The input (seg) must be already decoded by
599 * cbfs_decode_payload_segment.
600 */
601 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800602 case PAYLOAD_SEGMENT_CODE:
603 case PAYLOAD_SEGMENT_DATA:
604 fprintf(fp, " %s (%s compression, offset: 0x%x, "
605 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800606 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800607 "code " : "data"),
608 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800609 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800610 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800611 seg->offset, seg->load_addr, seg->len,
612 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800613 break;
614
615 case PAYLOAD_SEGMENT_ENTRY:
616 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800617 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800618 break;
619
620 case PAYLOAD_SEGMENT_BSS:
621 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
622 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800623 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800624 break;
625
626 case PAYLOAD_SEGMENT_PARAMS:
627 fprintf(fp, " parameters\n");
628 break;
629
630 default:
631 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
632 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800633 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800634 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800635 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800636 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800637 seg->offset, seg->load_addr, seg->len,
638 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800639 break;
640 }
641 return 0;
642}
643
644int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700645 void *arg)
646{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800647 const char *name = CBFS_NAME(entry);
648 struct cbfs_payload_segment *payload;
649 FILE *fp = (FILE *)arg;
650
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800651 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800652 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
653 cbfs_get_entry_addr(image, entry));
654 return -1;
655 }
656 if (!fp)
657 fp = stdout;
658
659 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
660 *name ? name : "(empty)",
661 cbfs_get_entry_addr(image, entry),
662 get_cbfs_entry_type_name(ntohl(entry->type)),
663 ntohl(entry->len));
664
665 if (!verbose)
666 return 0;
667
668 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
669 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
670 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
671 ntohl(entry->len));
672
673 /* note the components of the subheader may be in host order ... */
674 switch (ntohl(entry->type)) {
675 case CBFS_COMPONENT_STAGE:
676 cbfs_print_stage_info((struct cbfs_stage *)
677 CBFS_SUBHEADER(entry), fp);
678 break;
679
680 case CBFS_COMPONENT_PAYLOAD:
681 payload = (struct cbfs_payload_segment *)
682 CBFS_SUBHEADER(entry);
683 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +0800684 struct cbfs_payload_segment seg;
685 cbfs_decode_payload_segment(&seg, payload);
686 cbfs_print_decoded_payload_segment_info(
687 &seg, fp);
688 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800689 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +0800690 else
Aaron Durbinca630272014-08-05 10:48:20 -0500691 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800692 }
693 break;
694 default:
695 break;
696 }
697 return 0;
698}
699
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700700int cbfs_print_directory(struct cbfs_image *image)
701{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800702 cbfs_print_header_info(image);
703 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
704 cbfs_walk(image, cbfs_print_entry_info, NULL);
705 return 0;
706}
707
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800708int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700709 void *arg)
710{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800711 struct cbfs_file *next;
712 uint32_t type, addr, last_addr;
713
714 type = ntohl(entry->type);
715 if (type == CBFS_COMPONENT_DELETED) {
716 // Ready to be recycled.
717 type = CBFS_COMPONENT_NULL;
718 entry->type = htonl(type);
719 }
720 if (type != CBFS_COMPONENT_NULL)
721 return 0;
722
723 next = cbfs_find_next_entry(image, entry);
724
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800725 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800726 type = ntohl(next->type);
727 if (type == CBFS_COMPONENT_DELETED) {
728 type = CBFS_COMPONENT_NULL;
729 next->type = htonl(type);
730 }
731 if (type != CBFS_COMPONENT_NULL)
732 return 0;
733
734 addr = cbfs_get_entry_addr(image, entry);
735 last_addr = cbfs_get_entry_addr(
736 image, cbfs_find_next_entry(image, next));
737
738 // Now, we find two deleted/empty entries; try to merge now.
739 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
740 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
741 cbfs_get_entry_addr(image, next), ntohl(next->len));
742 cbfs_create_empty_entry(image, entry,
743 (last_addr - addr -
744 cbfs_calculate_file_header_size("")),
745 "");
746 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
747 next = cbfs_find_next_entry(image, entry);
748 }
749 return 0;
750}
751
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800752int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700753 void *arg)
754{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800755 int count = 0;
756 struct cbfs_file *entry;
757 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800758 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800759 entry = cbfs_find_next_entry(image, entry)) {
760 count ++;
761 if (callback(image, entry, arg) != 0)
762 break;
763 }
764 return count;
765}
766
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700767struct cbfs_header *cbfs_find_header(char *data, size_t size)
768{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800769 size_t offset;
770 int found = 0;
771 uint32_t x86sig;
772 struct cbfs_header *header, *result = NULL;
773
774 // Try x86 style (check signature in bottom) header first.
775 x86sig = *(uint32_t *)(data + size - sizeof(uint32_t));
776 offset = (x86sig + (uint32_t)size);
777 DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset);
778 if (offset >= size - sizeof(*header) ||
779 ntohl(((struct cbfs_header *)(data + offset))->magic) !=
780 CBFS_HEADER_MAGIC)
781 offset = 0;
782
783 for (; offset + sizeof(*header) < size; offset++) {
784 header = (struct cbfs_header *)(data + offset);
785 if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC))
786 continue;
787 if (ntohl(header->version) != CBFS_HEADER_VERSION1 &&
788 ntohl(header->version) != CBFS_HEADER_VERSION2) {
789 // Probably not a real CBFS header?
790 continue;
791 }
792 found++;
793 result = header;
794 }
795 if (found > 1) {
796 ERROR("multiple (%d) CBFS headers found!\n",
797 found);
798 result = NULL;
799 }
800 return result;
801}
802
803
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700804struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
805{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800806 assert(image && image->header);
807 return (struct cbfs_file *)(image->buffer.data +
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600808 image->header->offset);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800809}
810
811struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700812 struct cbfs_file *entry)
813{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800814 uint32_t addr = cbfs_get_entry_addr(image, entry);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600815 int align = image->header->align;
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800816 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +0800817 addr += ntohl(entry->offset) + ntohl(entry->len);
818 addr = align_up(addr, align);
819 return (struct cbfs_file *)(image->buffer.data + addr);
820}
821
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700822uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
823{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800824 assert(image && image->buffer.data && entry);
825 return (int32_t)((char *)entry - image->buffer.data);
826}
827
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700828int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
829{
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800830 return (entry &&
831 (char *)entry >= image->buffer.data &&
832 (char *)entry + sizeof(entry->magic) <
833 image->buffer.data + image->buffer.size &&
834 memcmp(entry->magic, CBFS_FILE_MAGIC,
835 sizeof(entry->magic)) == 0);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800836}
837
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800838int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700839 size_t len, const char *name)
840{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800841 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
842 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
843 entry->type = htonl(CBFS_COMPONENT_NULL);
844 entry->len = htonl(len);
845 entry->checksum = 0; // TODO Build a checksum algorithm.
846 entry->offset = htonl(cbfs_calculate_file_header_size(name));
847 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
848 strcpy(CBFS_NAME(entry), name);
849 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
850 return 0;
851}
852
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700853/* Finds a place to hold whole data in same memory page. */
854static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
855{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800856 if (!page)
857 return 1;
858 return (start / page) == (start + size - 1) / page;
859}
860
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800861/* Tests if data can fit in a range by given offset:
862 * start ->| header_len | offset (+ size) |<- end
863 */
864static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700865 uint32_t offset, uint32_t size)
866{
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800867 return (offset >= start + header_len && offset + size <= end);
868}
869
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800870int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700871 uint32_t size, uint32_t page_size, uint32_t align)
872{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800873 struct cbfs_file *entry;
874 size_t need_len;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800875 uint32_t addr, addr_next, addr2, addr3, offset, header_len;
876
877 /* Default values: allow fitting anywhere in ROM. */
878 if (!page_size)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600879 page_size = image->header->romsize;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800880 if (!align)
881 align = 1;
882
883 if (size > page_size)
884 ERROR("Input file size (%d) greater than page size (%d).\n",
885 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800886
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600887 if (page_size % image->header->align)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800888 WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600889 __func__, page_size, image->header->align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800890
891 /* TODO Old cbfstool always assume input is a stage file (and adding
892 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800893 * (type) param in future. For right now, we assume cbfs_stage is the
894 * largest structure and add it into header size. */
895 assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800896 header_len = (cbfs_calculate_file_header_size(name) +
897 sizeof(struct cbfs_stage));
898 need_len = header_len + size;
899
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800900 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800901 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
902
903 /* Three cases of content location on memory page:
904 * case 1.
905 * | PAGE 1 | PAGE 2 |
906 * | <header><content>| Fit. Return start of content.
907 *
908 * case 2.
909 * | PAGE 1 | PAGE 2 |
910 * | <header><content> | Fits when we shift content to align
911 * shift-> | <header>|<content> | at starting of PAGE 2.
912 *
913 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800914 * | PAGE 1 | PAGE 2 | PAGE 3 |
915 * | <header>< content > | Can't fit. If we shift content to
916 * |trial-> <header>< content > | PAGE 2, header can't fit in free
917 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800918 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800919 * The returned address can be then used as "base-address" (-b) in add-*
920 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
921 * For stage targets, the address is also used to re-link stage before
922 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800923 */
924 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800925 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800926 entry = cbfs_find_next_entry(image, entry)) {
927
928 uint32_t type = ntohl(entry->type);
929 if (type != CBFS_COMPONENT_NULL)
930 continue;
931
932 addr = cbfs_get_entry_addr(image, entry);
933 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
934 image, entry));
935 if (addr_next - addr < need_len)
936 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800937
938 offset = align_up(addr + header_len, align);
939 if (is_in_same_page(offset, size, page_size) &&
940 is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800941 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800942 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800943 }
944
945 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800946 offset = align_up(addr2, align);
947 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800948 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800949 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800950 }
951
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800952 /* Assume page_size >= header_len so adding one page will
953 * definitely provide the space for header. */
954 assert(page_size >= header_len);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800955 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800956 offset = align_up(addr3, align);
957 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800958 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800959 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800960 }
961 }
962 return -1;
963}