blob: 0230d8032e8a1144e53ab322fe12ddfe033fe87c [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"},
Martin Rothdde307c2015-03-24 15:54:20 -060052 {CBFS_COMPONENT_FSP, "fsp"},
53 {CBFS_COMPONENT_MRC, "mrc"},
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080054 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos_default"},
55 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos_layout"},
Martin Rothdde307c2015-03-24 15:54:20 -060056 {CBFS_COMPONENT_SPD, "spd"},
57 {CBFS_COMPONENT_MRC_CACHE, "mrc_cache"},
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080058 {CBFS_COMPONENT_DELETED, "deleted"},
59 {CBFS_COMPONENT_NULL, "null"},
60 {0, NULL},
61};
62
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070063static const struct typedesc_t types_cbfs_compression[] = {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080064 {CBFS_COMPRESS_NONE, "none"},
65 {CBFS_COMPRESS_LZMA, "LZMA"},
66 {0, NULL},
67};
68
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -080069static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070070 const char *default_value)
71{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080072 int i;
73 for (i = 0; desc[i].name; i++)
74 if (desc[i].type == type)
75 return desc[i].name;
76 return default_value;
77}
78
Stefan Reinauer2dd161f2015-03-04 00:55:03 +010079static const char *get_cbfs_entry_type_name(uint32_t type)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070080{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080081 return lookup_name_by_type(types_cbfs_entry, type, "(unknown)");
82}
83
Hung-Te Linc03d9b02013-01-29 02:38:40 +080084/* CBFS image */
85
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070086static int cbfs_calculate_file_header_size(const char *name)
87{
Hung-Te Linc03d9b02013-01-29 02:38:40 +080088 return (sizeof(struct cbfs_file) +
89 align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN));
90}
91
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -060092static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc)
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -070093{
Hung-Te Lin49fcd752013-01-29 03:16:20 +080094 // A bug in old cbfstool may produce extra few bytes (by alignment) and
95 // cause cbfstool to overwrite things after free space -- which is
96 // usually CBFS header on x86. We need to workaround that.
97
98 struct cbfs_file *entry, *first = NULL, *last = NULL;
99 for (first = entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800100 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800101 entry = cbfs_find_next_entry(image, entry)) {
102 last = entry;
103 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600104 if ((char *)first < (char *)hdr_loc &&
105 (char *)entry > (char *)hdr_loc) {
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800106 WARN("CBFS image was created with old cbfstool with size bug. "
107 "Fixing size in last entry...\n");
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600108 last->len = htonl(ntohl(last->len) - image->header->align);
Hung-Te Lin49fcd752013-01-29 03:16:20 +0800109 DEBUG("Last entry has been changed from 0x%x to 0x%x.\n",
110 cbfs_get_entry_addr(image, entry),
111 cbfs_get_entry_addr(image,
112 cbfs_find_next_entry(image, last)));
113 }
114 return 0;
115}
116
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800117void cbfs_put_header(void *dest, const struct cbfs_header *header)
118{
119 struct buffer outheader;
120
121 outheader.data = dest;
122 outheader.size = 0;
123
124 xdr_be.put32(&outheader, header->magic);
125 xdr_be.put32(&outheader, header->version);
126 xdr_be.put32(&outheader, header->romsize);
127 xdr_be.put32(&outheader, header->bootblocksize);
128 xdr_be.put32(&outheader, header->align);
129 xdr_be.put32(&outheader, header->offset);
130 xdr_be.put32(&outheader, header->architecture);
131}
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600132
Hung-Te Lin0780d672014-05-16 10:14:05 +0800133static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output,
134 struct cbfs_payload_segment *input)
135{
136 struct buffer seg = {
137 .data = (void *)input,
138 .size = sizeof(*input),
139 };
140 output->type = xdr_be.get32(&seg);
141 output->compression = xdr_be.get32(&seg);
142 output->offset = xdr_be.get32(&seg);
143 output->load_addr = xdr_be.get64(&seg);
144 output->len = xdr_be.get32(&seg);
145 output->mem_len = xdr_be.get32(&seg);
146 assert(seg.size == 0);
147}
148
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600149void cbfs_get_header(struct cbfs_header *header, const void *src)
150{
151 struct buffer outheader;
152
153 outheader.data = (void *)src; /* We're not modifying the data */
154 outheader.size = 0;
155
156 header->magic = xdr_be.get32(&outheader);
157 header->version = xdr_be.get32(&outheader);
158 header->romsize = xdr_be.get32(&outheader);
159 header->bootblocksize = xdr_be.get32(&outheader);
160 header->align = xdr_be.get32(&outheader);
161 header->offset = xdr_be.get32(&outheader);
162 header->architecture = xdr_be.get32(&outheader);
163}
164
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800165int cbfs_image_create(struct cbfs_image *image,
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100166 uint32_t architecture,
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800167 size_t size,
168 uint32_t align,
169 struct buffer *bootblock,
170 int32_t bootblock_offset,
171 int32_t header_offset,
172 int32_t entries_offset)
173{
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800174 struct cbfs_header header;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800175 struct cbfs_file *entry;
Julius Wernerefcee762014-11-10 13:14:24 -0800176 int32_t *rel_offset;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800177 uint32_t cbfs_len;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800178 size_t entry_header_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600179 void *header_loc;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800180
181 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
182 "header=0x%x+0x%zx, entries_offset=0x%x\n",
183 bootblock_offset, bootblock->size,
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800184 header_offset, sizeof(header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800185
186 if (buffer_create(&image->buffer, size, "(new)") != 0)
187 return -1;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600188 if ((image->header = malloc(sizeof(*image->header))) == NULL)
189 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800190 memset(image->buffer.data, CBFS_CONTENT_DEFAULT_VALUE, size);
191
192 // Adjust legcay top-aligned address to ROM offset.
193 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
194 entries_offset += (int32_t)size;
195 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
196 bootblock_offset += (int32_t)size;
197 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
198 header_offset += (int32_t) size;
199
200 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
201 "header=0x%x, entries_offset=0x%x\n",
202 bootblock_offset, header_offset, entries_offset);
203
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800204 // Prepare bootblock
205 if (bootblock_offset + bootblock->size > size) {
206 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
207 bootblock_offset, bootblock->size, size);
208 return -1;
209 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800210 if (entries_offset > bootblock_offset &&
211 entries_offset < bootblock->size) {
212 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
213 bootblock_offset, bootblock->size, entries_offset);
214 return -1;
215 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800216 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
217 bootblock->size);
218
219 // Prepare header
Julius Wernerefcee762014-11-10 13:14:24 -0800220 if (header_offset + sizeof(header) > size - sizeof(int32_t)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800221 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800222 header_offset, sizeof(header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800223 return -1;
224 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600225 image->header->magic = CBFS_HEADER_MAGIC;
226 image->header->version = CBFS_HEADER_VERSION;
227 image->header->romsize = size;
228 image->header->bootblocksize = bootblock->size;
229 image->header->align = align;
230 image->header->offset = entries_offset;
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100231 image->header->architecture = architecture;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600232
233 header_loc = (image->buffer.data + header_offset);
234 cbfs_put_header(header_loc, image->header);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800235
Julius Wernerefcee762014-11-10 13:14:24 -0800236 // The last 4 byte of the image contain the relative offset from the end
237 // of the image to the master header as a 32-bit signed integer. x86
238 // relies on this also being its (memory-mapped, top-aligned) absolute
239 // 32-bit address by virtue of how two's complement numbers work.
240 assert(size % sizeof(int32_t) == 0);
241 rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t));
242 *rel_offset = header_offset - size;
243
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800244 // Prepare entries
245 if (align_up(entries_offset, align) != entries_offset) {
246 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
247 entries_offset, align);
248 return -1;
249 }
Hung-Te Linb02c8732013-03-15 17:40:08 +0800250 entry_header_len = cbfs_calculate_file_header_size("");
251 if (entries_offset + entry_header_len > size) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800252 ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n",
Hung-Te Linb02c8732013-03-15 17:40:08 +0800253 entries_offset, entry_header_len, size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800254 return -1;
255 }
256 entry = (struct cbfs_file *)(image->buffer.data + entries_offset);
257 // To calculate available length, find
Julius Wernerefcee762014-11-10 13:14:24 -0800258 // e = min(bootblock, header, rel_offset) where e > entries_offset.
259 cbfs_len = size - sizeof(int32_t);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800260 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
261 cbfs_len = bootblock_offset;
262 if (header_offset > entries_offset && header_offset < cbfs_len)
263 cbfs_len = header_offset;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800264 cbfs_len -= entries_offset + align + entry_header_len;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800265 cbfs_create_empty_entry(image, entry, cbfs_len, "");
266 LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len);
267 return 0;
268}
269
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700270int cbfs_image_from_file(struct cbfs_image *image, const char *filename)
271{
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600272 void *header_loc;
273
Hung-Te Lineab2c812013-01-29 01:56:17 +0800274 if (buffer_from_file(&image->buffer, filename) != 0)
275 return -1;
276 DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
277 image->buffer.size);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600278 header_loc = cbfs_find_header(image->buffer.data, image->buffer.size);
279 if (!header_loc) {
Hung-Te Lineab2c812013-01-29 01:56:17 +0800280 ERROR("%s does not have CBFS master header.\n", filename);
281 cbfs_image_delete(image);
282 return -1;
283 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600284
285 if ((image->header = malloc(sizeof(*image->header))) == NULL)
286 return -1;
287
288 cbfs_get_header(image->header, header_loc);
289 cbfs_fix_legacy_size(image, header_loc);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800290
291 return 0;
292}
293
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700294int cbfs_image_write_file(struct cbfs_image *image, const char *filename)
295{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800296 assert(image && image->buffer.data);
297 return buffer_write_file(&image->buffer, filename);
298}
299
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700300int cbfs_image_delete(struct cbfs_image *image)
301{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100302 if (image == NULL)
303 return 0;
304
Hung-Te Lineab2c812013-01-29 01:56:17 +0800305 buffer_delete(&image->buffer);
306 image->header = NULL;
307 return 0;
308}
309
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800310/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
311static int cbfs_add_entry_at(struct cbfs_image *image,
312 struct cbfs_file *entry,
313 uint32_t size,
314 const char *name,
315 uint32_t type,
316 const void *data,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700317 uint32_t content_offset)
318{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800319 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
320 uint32_t addr = cbfs_get_entry_addr(image, entry),
321 addr_next = cbfs_get_entry_addr(image, next);
322 uint32_t header_size = cbfs_calculate_file_header_size(name),
323 min_entry_size = cbfs_calculate_file_header_size("");
324 uint32_t len, target;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600325 uint32_t align = image->header->align;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800326
327 target = content_offset - header_size;
328 if (target % align)
329 target -= target % align;
330 if (target < addr) {
331 ERROR("No space to hold cbfs_file header.");
332 return -1;
333 }
334
335 // Process buffer BEFORE content_offset.
336 if (target - addr > min_entry_size) {
337 DEBUG("|min|...|header|content|... <create new entry>\n");
338 len = target - addr - min_entry_size;
339 cbfs_create_empty_entry(image, entry, len, "");
340 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
341 entry = cbfs_find_next_entry(image, entry);
342 addr = cbfs_get_entry_addr(image, entry);
343 }
344
345 len = size + (content_offset - addr - header_size);
346 cbfs_create_empty_entry(image, entry, len, name);
347 if (len != size) {
348 DEBUG("|..|header|content|... <use offset to create entry>\n");
349 DEBUG("before: offset=0x%x, len=0x%x\n",
350 ntohl(entry->offset), ntohl(entry->len));
351 // TODO reset expanded name buffer to 0xFF.
352 entry->offset = htonl(ntohl(entry->offset) + (len - size));
353 entry->len = htonl(size);
354 DEBUG("after: offset=0x%x, len=0x%x\n",
355 ntohl(entry->offset), ntohl(entry->len));
356 }
357
358 // Ready to fill data into entry.
359 assert(ntohl(entry->len) == size);
360 entry->type = htonl(type);
361 DEBUG("content_offset: 0x%x, entry location: %x\n",
362 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
363 image->buffer.data));
364 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
365 content_offset);
366 memcpy(CBFS_SUBHEADER(entry), data, size);
367 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
368
369 // Process buffer AFTER entry.
370 entry = cbfs_find_next_entry(image, entry);
371 addr = cbfs_get_entry_addr(image, entry);
372 assert(addr < addr_next);
373
374 if (addr_next - addr < min_entry_size) {
375 DEBUG("No space after content to keep CBFS structure.\n");
376 return -1;
377 }
378
379 len = addr_next - addr - min_entry_size;
380 cbfs_create_empty_entry(image, entry, len, "");
381 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
382 return 0;
383}
384
385int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700386 const char *name, uint32_t type, uint32_t content_offset)
387{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800388 uint32_t entry_type;
389 uint32_t addr, addr_next;
390 struct cbfs_file *entry, *next;
391 uint32_t header_size, need_size, new_size;
392
393 header_size = cbfs_calculate_file_header_size(name);
394
395 need_size = header_size + buffer->size;
396 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
397 name, content_offset, header_size, buffer->size, need_size);
398
399 if (IS_TOP_ALIGNED_ADDRESS(content_offset)) {
400 // legacy cbfstool takes top-aligned address.
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600401 uint32_t theromsize = image->header->romsize;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800402 INFO("Converting top-aligned address 0x%x to offset: 0x%x\n",
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800403 content_offset, content_offset + theromsize);
404 content_offset += theromsize;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800405 }
406
407 // Merge empty entries.
408 DEBUG("(trying to merge empty entries...)\n");
409 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
410
411 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800412 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800413 entry = cbfs_find_next_entry(image, entry)) {
414
415 entry_type = ntohl(entry->type);
416 if (entry_type != CBFS_COMPONENT_NULL)
417 continue;
418
419 addr = cbfs_get_entry_addr(image, entry);
420 next = cbfs_find_next_entry(image, entry);
421 addr_next = cbfs_get_entry_addr(image, next);
422
423 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
424 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600425
426 /* Will the file fit? Don't yet worry if we have space for a new
427 * "empty" entry. We take care of that later.
428 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800429 if (addr + need_size > addr_next)
430 continue;
431
432 // Can we simply put object here?
433 if (!content_offset || content_offset == addr + header_size) {
434 DEBUG("Filling new entry data (%zd bytes).\n",
435 buffer->size);
436 cbfs_create_empty_entry(image, entry, buffer->size,
437 name);
438 entry->type = htonl(type);
439 memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
440 if (verbose)
441 cbfs_print_entry_info(image, entry, stderr);
442
443 // setup new entry
Paul Menzel4159a802013-07-14 00:24:31 +0200444 DEBUG("Setting new empty entry.\n");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800445 entry = cbfs_find_next_entry(image, entry);
446 new_size = (cbfs_get_entry_addr(image, next) -
447 cbfs_get_entry_addr(image, entry));
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600448
449 /* Entry was added and no space for new "empty" entry */
450 if (new_size < cbfs_calculate_file_header_size("")) {
451 DEBUG("No need for new \"empty\" entry\n");
452 /* No need to increase the size of the just
453 * stored file to extend to next file. Alignment
454 * of next file takes care of this.
455 */
456 return 0;
457 }
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800458 new_size -= cbfs_calculate_file_header_size("");
459 DEBUG("new size: %d\n", new_size);
460 cbfs_create_empty_entry(image, entry, new_size, "");
461 if (verbose)
462 cbfs_print_entry_info(image, entry, stderr);
463 return 0;
464 }
465
466 // We need to put content here, and the case is really
467 // complicated...
468 assert(content_offset);
469 if (addr_next < content_offset) {
470 DEBUG("Not for specified offset yet");
471 continue;
472 } else if (addr > content_offset) {
473 DEBUG("Exceed specified content_offset.");
474 break;
475 } else if (addr + header_size > content_offset) {
476 ERROR("Not enough space for header.\n");
477 break;
478 } else if (content_offset + buffer->size > addr_next) {
479 ERROR("Not enough space for content.\n");
480 break;
481 }
482
483 // TODO there are more few tricky cases that we may
484 // want to fit by altering offset.
485 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
486 addr, addr_next - addr, content_offset);
487
488 if (cbfs_add_entry_at(image, entry, buffer->size, name, type,
489 buffer->data, content_offset) == 0) {
490 return 0;
491 }
492 break;
493 }
494
495 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
496 buffer->name, buffer->size, buffer->size / 1024, content_offset);
497 return -1;
498}
499
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700500struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
501{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800502 struct cbfs_file *entry;
503 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800504 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800505 entry = cbfs_find_next_entry(image, entry)) {
506 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
507 DEBUG("cbfs_get_entry: found %s\n", name);
508 return entry;
509 }
510 }
511 return NULL;
512}
513
514int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700515 const char *filename)
516{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800517 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
518 struct buffer buffer;
519 if (!entry) {
520 ERROR("File not found: %s\n", entry_name);
521 return -1;
522 }
523 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
524 entry_name, cbfs_get_entry_addr(image, entry),
525 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
526
527 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
528 WARN("Only 'raw' files are safe to extract.\n");
529 }
530
531 buffer.data = CBFS_SUBHEADER(entry);
532 buffer.size = ntohl(entry->len);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800533 buffer.name = (char *)"(cbfs_export_entry)";
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800534 if (buffer_write_file(&buffer, filename) != 0) {
535 ERROR("Failed to write %s into %s.\n",
536 entry_name, filename);
537 return -1;
538 }
539 INFO("Successfully dumped the file to: %s\n", filename);
540 return 0;
541}
542
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700543int cbfs_remove_entry(struct cbfs_image *image, const char *name)
544{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800545 struct cbfs_file *entry, *next;
546 size_t len;
547 entry = cbfs_get_entry(image, name);
548 if (!entry) {
549 ERROR("CBFS file %s not found.\n", name);
550 return -1;
551 }
552 next = cbfs_find_next_entry(image, entry);
553 assert(next);
554 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
555 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
556 entry->type = htonl(CBFS_COMPONENT_DELETED);
557 len = (cbfs_get_entry_addr(image, next) -
558 cbfs_get_entry_addr(image, entry));
559 entry->offset = htonl(cbfs_calculate_file_header_size(""));
560 entry->len = htonl(len - ntohl(entry->offset));
561 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
562 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
563 ntohl(entry->len));
564 return 0;
565}
566
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700567int cbfs_print_header_info(struct cbfs_image *image)
568{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800569 char *name = strdup(image->buffer.name);
570 assert(image && image->header);
571 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800572 "alignment: %d bytes, architecture: %s\n\n",
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800573 basename(name),
574 image->buffer.size / 1024,
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600575 image->header->bootblocksize,
576 image->header->romsize,
577 image->header->offset,
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800578 image->header->align,
579 arch_to_string(image->header->architecture));
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800580 free(name);
581 return 0;
582}
583
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700584static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp)
585{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800586 fprintf(fp,
587 " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", "
588 "length: %d/%d\n",
589 lookup_name_by_type(types_cbfs_compression,
590 stage->compression, "(unknown)"),
591 stage->entry,
592 stage->load,
593 stage->len,
594 stage->memlen);
595 return 0;
596}
597
Hung-Te Lin0780d672014-05-16 10:14:05 +0800598static int cbfs_print_decoded_payload_segment_info(
599 struct cbfs_payload_segment *seg, FILE *fp)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800600{
Hung-Te Lin0780d672014-05-16 10:14:05 +0800601 /* The input (seg) must be already decoded by
602 * cbfs_decode_payload_segment.
603 */
604 switch (seg->type) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800605 case PAYLOAD_SEGMENT_CODE:
606 case PAYLOAD_SEGMENT_DATA:
607 fprintf(fp, " %s (%s compression, offset: 0x%x, "
608 "load: 0x%" PRIx64 ", length: %d/%d)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800609 (seg->type == PAYLOAD_SEGMENT_CODE ?
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800610 "code " : "data"),
611 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800612 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800613 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800614 seg->offset, seg->load_addr, seg->len,
615 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800616 break;
617
618 case PAYLOAD_SEGMENT_ENTRY:
619 fprintf(fp, " entry (0x%" PRIx64 ")\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800620 seg->load_addr);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800621 break;
622
623 case PAYLOAD_SEGMENT_BSS:
624 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
625 "length 0x%x)\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800626 seg->load_addr, seg->len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800627 break;
628
629 case PAYLOAD_SEGMENT_PARAMS:
630 fprintf(fp, " parameters\n");
631 break;
632
633 default:
634 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
635 "load: 0x%" PRIx64 ", length: %d/%d\n",
Hung-Te Lin0780d672014-05-16 10:14:05 +0800636 seg->type,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800637 lookup_name_by_type(types_cbfs_compression,
Hung-Te Lin0780d672014-05-16 10:14:05 +0800638 seg->compression,
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800639 "(unknown)"),
Hung-Te Lin0780d672014-05-16 10:14:05 +0800640 seg->offset, seg->load_addr, seg->len,
641 seg->mem_len);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800642 break;
643 }
644 return 0;
645}
646
647int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700648 void *arg)
649{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800650 const char *name = CBFS_NAME(entry);
651 struct cbfs_payload_segment *payload;
652 FILE *fp = (FILE *)arg;
653
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800654 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800655 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
656 cbfs_get_entry_addr(image, entry));
657 return -1;
658 }
659 if (!fp)
660 fp = stdout;
661
662 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
663 *name ? name : "(empty)",
664 cbfs_get_entry_addr(image, entry),
665 get_cbfs_entry_type_name(ntohl(entry->type)),
666 ntohl(entry->len));
667
668 if (!verbose)
669 return 0;
670
671 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
672 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
673 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
674 ntohl(entry->len));
675
676 /* note the components of the subheader may be in host order ... */
677 switch (ntohl(entry->type)) {
678 case CBFS_COMPONENT_STAGE:
679 cbfs_print_stage_info((struct cbfs_stage *)
680 CBFS_SUBHEADER(entry), fp);
681 break;
682
683 case CBFS_COMPONENT_PAYLOAD:
684 payload = (struct cbfs_payload_segment *)
685 CBFS_SUBHEADER(entry);
686 while (payload) {
Hung-Te Lin0780d672014-05-16 10:14:05 +0800687 struct cbfs_payload_segment seg;
688 cbfs_decode_payload_segment(&seg, payload);
689 cbfs_print_decoded_payload_segment_info(
690 &seg, fp);
691 if (seg.type == PAYLOAD_SEGMENT_ENTRY)
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800692 break;
Hung-Te Lin0780d672014-05-16 10:14:05 +0800693 else
Aaron Durbinca630272014-08-05 10:48:20 -0500694 payload ++;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800695 }
696 break;
697 default:
698 break;
699 }
700 return 0;
701}
702
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700703int cbfs_print_directory(struct cbfs_image *image)
704{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800705 cbfs_print_header_info(image);
706 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
707 cbfs_walk(image, cbfs_print_entry_info, NULL);
708 return 0;
709}
710
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800711int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700712 void *arg)
713{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800714 struct cbfs_file *next;
715 uint32_t type, addr, last_addr;
716
717 type = ntohl(entry->type);
718 if (type == CBFS_COMPONENT_DELETED) {
719 // Ready to be recycled.
720 type = CBFS_COMPONENT_NULL;
721 entry->type = htonl(type);
722 }
723 if (type != CBFS_COMPONENT_NULL)
724 return 0;
725
726 next = cbfs_find_next_entry(image, entry);
727
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800728 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800729 type = ntohl(next->type);
730 if (type == CBFS_COMPONENT_DELETED) {
731 type = CBFS_COMPONENT_NULL;
732 next->type = htonl(type);
733 }
734 if (type != CBFS_COMPONENT_NULL)
735 return 0;
736
737 addr = cbfs_get_entry_addr(image, entry);
738 last_addr = cbfs_get_entry_addr(
739 image, cbfs_find_next_entry(image, next));
740
741 // Now, we find two deleted/empty entries; try to merge now.
742 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
743 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
744 cbfs_get_entry_addr(image, next), ntohl(next->len));
745 cbfs_create_empty_entry(image, entry,
746 (last_addr - addr -
747 cbfs_calculate_file_header_size("")),
748 "");
749 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
750 next = cbfs_find_next_entry(image, entry);
751 }
752 return 0;
753}
754
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800755int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700756 void *arg)
757{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800758 int count = 0;
759 struct cbfs_file *entry;
760 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800761 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800762 entry = cbfs_find_next_entry(image, entry)) {
763 count ++;
764 if (callback(image, entry, arg) != 0)
765 break;
766 }
767 return count;
768}
769
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700770struct cbfs_header *cbfs_find_header(char *data, size_t size)
771{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800772 size_t offset;
773 int found = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800774 int32_t rel_offset;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800775 struct cbfs_header *header, *result = NULL;
776
Julius Wernerefcee762014-11-10 13:14:24 -0800777 // Try finding relative offset of master header at end of file first.
778 rel_offset = *(int32_t *)(data + size - sizeof(int32_t));
779 offset = size + rel_offset;
780 DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n",
781 (size_t)rel_offset, (size_t)-rel_offset, offset);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800782 if (offset >= size - sizeof(*header) ||
783 ntohl(((struct cbfs_header *)(data + offset))->magic) !=
Julius Wernerefcee762014-11-10 13:14:24 -0800784 CBFS_HEADER_MAGIC) {
785 // Some use cases append non-CBFS data to the end of the ROM.
786 DEBUG("relative offset seems wrong, scanning whole image...\n");
Hung-Te Lineab2c812013-01-29 01:56:17 +0800787 offset = 0;
Julius Wernerefcee762014-11-10 13:14:24 -0800788 }
Hung-Te Lineab2c812013-01-29 01:56:17 +0800789
790 for (; offset + sizeof(*header) < size; offset++) {
791 header = (struct cbfs_header *)(data + offset);
792 if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC))
793 continue;
794 if (ntohl(header->version) != CBFS_HEADER_VERSION1 &&
795 ntohl(header->version) != CBFS_HEADER_VERSION2) {
796 // Probably not a real CBFS header?
797 continue;
798 }
Julius Wernerefcee762014-11-10 13:14:24 -0800799 if (!found++)
800 result = header;
Hung-Te Lineab2c812013-01-29 01:56:17 +0800801 }
Julius Wernerefcee762014-11-10 13:14:24 -0800802 if (found > 1)
803 // Top-aligned images usually have a working relative offset
804 // field, so this is more likely to happen on bottom-aligned
805 // ones (where the first header is the "outermost" one)
806 WARN("Multiple (%d) CBFS headers found, using the first one.\n",
Hung-Te Lineab2c812013-01-29 01:56:17 +0800807 found);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800808 return result;
809}
810
811
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700812struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
813{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800814 assert(image && image->header);
815 return (struct cbfs_file *)(image->buffer.data +
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600816 image->header->offset);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800817}
818
819struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700820 struct cbfs_file *entry)
821{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800822 uint32_t addr = cbfs_get_entry_addr(image, entry);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600823 int align = image->header->align;
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800824 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +0800825 addr += ntohl(entry->offset) + ntohl(entry->len);
826 addr = align_up(addr, align);
827 return (struct cbfs_file *)(image->buffer.data + addr);
828}
829
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700830uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
831{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800832 assert(image && image->buffer.data && entry);
833 return (int32_t)((char *)entry - image->buffer.data);
834}
835
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700836int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
837{
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800838 return (entry &&
839 (char *)entry >= image->buffer.data &&
840 (char *)entry + sizeof(entry->magic) <
841 image->buffer.data + image->buffer.size &&
842 memcmp(entry->magic, CBFS_FILE_MAGIC,
843 sizeof(entry->magic)) == 0);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800844}
845
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800846int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700847 size_t len, const char *name)
848{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800849 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
850 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
851 entry->type = htonl(CBFS_COMPONENT_NULL);
852 entry->len = htonl(len);
853 entry->checksum = 0; // TODO Build a checksum algorithm.
854 entry->offset = htonl(cbfs_calculate_file_header_size(name));
855 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
856 strcpy(CBFS_NAME(entry), name);
857 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
858 return 0;
859}
860
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700861/* Finds a place to hold whole data in same memory page. */
862static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
863{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800864 if (!page)
865 return 1;
866 return (start / page) == (start + size - 1) / page;
867}
868
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800869/* Tests if data can fit in a range by given offset:
870 * start ->| header_len | offset (+ size) |<- end
871 */
872static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700873 uint32_t offset, uint32_t size)
874{
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800875 return (offset >= start + header_len && offset + size <= end);
876}
877
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800878int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700879 uint32_t size, uint32_t page_size, uint32_t align)
880{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800881 struct cbfs_file *entry;
882 size_t need_len;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800883 uint32_t addr, addr_next, addr2, addr3, offset, header_len;
884
885 /* Default values: allow fitting anywhere in ROM. */
886 if (!page_size)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600887 page_size = image->header->romsize;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800888 if (!align)
889 align = 1;
890
891 if (size > page_size)
892 ERROR("Input file size (%d) greater than page size (%d).\n",
893 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800894
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600895 if (page_size % image->header->align)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800896 WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600897 __func__, page_size, image->header->align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800898
899 /* TODO Old cbfstool always assume input is a stage file (and adding
900 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800901 * (type) param in future. For right now, we assume cbfs_stage is the
902 * largest structure and add it into header size. */
903 assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800904 header_len = (cbfs_calculate_file_header_size(name) +
905 sizeof(struct cbfs_stage));
906 need_len = header_len + size;
907
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800908 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800909 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
910
911 /* Three cases of content location on memory page:
912 * case 1.
913 * | PAGE 1 | PAGE 2 |
914 * | <header><content>| Fit. Return start of content.
915 *
916 * case 2.
917 * | PAGE 1 | PAGE 2 |
918 * | <header><content> | Fits when we shift content to align
919 * shift-> | <header>|<content> | at starting of PAGE 2.
920 *
921 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800922 * | PAGE 1 | PAGE 2 | PAGE 3 |
923 * | <header>< content > | Can't fit. If we shift content to
924 * |trial-> <header>< content > | PAGE 2, header can't fit in free
925 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800926 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800927 * The returned address can be then used as "base-address" (-b) in add-*
928 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
929 * For stage targets, the address is also used to re-link stage before
930 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800931 */
932 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800933 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800934 entry = cbfs_find_next_entry(image, entry)) {
935
936 uint32_t type = ntohl(entry->type);
937 if (type != CBFS_COMPONENT_NULL)
938 continue;
939
940 addr = cbfs_get_entry_addr(image, entry);
941 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
942 image, entry));
943 if (addr_next - addr < need_len)
944 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800945
946 offset = align_up(addr + header_len, align);
947 if (is_in_same_page(offset, size, page_size) &&
948 is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800949 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800950 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800951 }
952
953 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800954 offset = align_up(addr2, align);
955 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800956 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800957 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800958 }
959
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800960 /* Assume page_size >= header_len so adding one page will
961 * definitely provide the space for header. */
962 assert(page_size >= header_len);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800963 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800964 offset = align_up(addr3, align);
965 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800966 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800967 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800968 }
969 }
970 return -1;
971}