blob: 12bc2fe74e49465b39dfe2ee5b098501c6e721e7 [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
156void cbfs_get_header(struct cbfs_header *header, const void *src)
157{
158 struct buffer outheader;
159
160 outheader.data = (void *)src; /* We're not modifying the data */
161 outheader.size = 0;
162
163 header->magic = xdr_be.get32(&outheader);
164 header->version = xdr_be.get32(&outheader);
165 header->romsize = xdr_be.get32(&outheader);
166 header->bootblocksize = xdr_be.get32(&outheader);
167 header->align = xdr_be.get32(&outheader);
168 header->offset = xdr_be.get32(&outheader);
169 header->architecture = xdr_be.get32(&outheader);
170}
171
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800172int cbfs_image_create(struct cbfs_image *image,
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800173 uint32_t myarch,
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800174 size_t size,
175 uint32_t align,
176 struct buffer *bootblock,
177 int32_t bootblock_offset,
178 int32_t header_offset,
179 int32_t entries_offset)
180{
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800181 struct cbfs_header header;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800182 struct cbfs_file *entry;
183 uint32_t cbfs_len;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800184 size_t entry_header_len;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600185 void *header_loc;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800186
187 DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, "
188 "header=0x%x+0x%zx, entries_offset=0x%x\n",
189 bootblock_offset, bootblock->size,
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800190 header_offset, sizeof(header), entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800191
192 if (buffer_create(&image->buffer, size, "(new)") != 0)
193 return -1;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600194 if ((image->header = malloc(sizeof(*image->header))) == NULL)
195 return -1;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800196 memset(image->buffer.data, CBFS_CONTENT_DEFAULT_VALUE, size);
197
198 // Adjust legcay top-aligned address to ROM offset.
199 if (IS_TOP_ALIGNED_ADDRESS(entries_offset))
200 entries_offset += (int32_t)size;
201 if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset))
202 bootblock_offset += (int32_t)size;
203 if (IS_TOP_ALIGNED_ADDRESS(header_offset))
204 header_offset += (int32_t) size;
205
206 DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, "
207 "header=0x%x, entries_offset=0x%x\n",
208 bootblock_offset, header_offset, entries_offset);
209
210 if (align == 0)
211 align = 64; // default align size.
212
213 // Prepare bootblock
214 if (bootblock_offset + bootblock->size > size) {
215 ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
216 bootblock_offset, bootblock->size, size);
217 return -1;
218 }
Hung-Te Linc5ff6482013-02-06 12:41:49 +0800219 if (entries_offset > bootblock_offset &&
220 entries_offset < bootblock->size) {
221 ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n",
222 bootblock_offset, bootblock->size, entries_offset);
223 return -1;
224 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800225 memcpy(image->buffer.data + bootblock_offset, bootblock->data,
226 bootblock->size);
227
228 // Prepare header
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800229 if (header_offset + sizeof(header) > size) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800230 ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n",
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -0800231 header_offset, sizeof(header), size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800232 return -1;
233 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600234 image->header->magic = CBFS_HEADER_MAGIC;
235 image->header->version = CBFS_HEADER_VERSION;
236 image->header->romsize = size;
237 image->header->bootblocksize = bootblock->size;
238 image->header->align = align;
239 image->header->offset = entries_offset;
240 image->header->architecture = myarch;
241
242 header_loc = (image->buffer.data + header_offset);
243 cbfs_put_header(header_loc, image->header);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800244
245 // Prepare entries
246 if (align_up(entries_offset, align) != entries_offset) {
247 ERROR("Offset (0x%x) must be aligned to 0x%x.\n",
248 entries_offset, align);
249 return -1;
250 }
Hung-Te Linb02c8732013-03-15 17:40:08 +0800251 entry_header_len = cbfs_calculate_file_header_size("");
252 if (entries_offset + entry_header_len > size) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800253 ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n",
Hung-Te Linb02c8732013-03-15 17:40:08 +0800254 entries_offset, entry_header_len, size);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800255 return -1;
256 }
257 entry = (struct cbfs_file *)(image->buffer.data + entries_offset);
258 // To calculate available length, find
259 // e = min(bootblock, header, size) where e > entries_offset.
260 cbfs_len = size;
261 if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len)
262 cbfs_len = bootblock_offset;
263 if (header_offset > entries_offset && header_offset < cbfs_len)
264 cbfs_len = header_offset;
Hung-Te Linb02c8732013-03-15 17:40:08 +0800265 cbfs_len -= entries_offset + align + entry_header_len;
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800266 cbfs_create_empty_entry(image, entry, cbfs_len, "");
267 LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len);
268 return 0;
269}
270
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700271int cbfs_image_from_file(struct cbfs_image *image, const char *filename)
272{
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600273 void *header_loc;
274
Hung-Te Lineab2c812013-01-29 01:56:17 +0800275 if (buffer_from_file(&image->buffer, filename) != 0)
276 return -1;
277 DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name,
278 image->buffer.size);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600279 header_loc = cbfs_find_header(image->buffer.data, image->buffer.size);
280 if (!header_loc) {
Hung-Te Lineab2c812013-01-29 01:56:17 +0800281 ERROR("%s does not have CBFS master header.\n", filename);
282 cbfs_image_delete(image);
283 return -1;
284 }
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600285
286 if ((image->header = malloc(sizeof(*image->header))) == NULL)
287 return -1;
288
289 cbfs_get_header(image->header, header_loc);
290 cbfs_fix_legacy_size(image, header_loc);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800291
292 return 0;
293}
294
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700295int cbfs_image_write_file(struct cbfs_image *image, const char *filename)
296{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800297 assert(image && image->buffer.data);
298 return buffer_write_file(&image->buffer, filename);
299}
300
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700301int cbfs_image_delete(struct cbfs_image *image)
302{
Edward O'Callaghana0f9ece2014-03-09 00:05:18 +1100303 if (image == NULL)
304 return 0;
305
Hung-Te Lineab2c812013-01-29 01:56:17 +0800306 buffer_delete(&image->buffer);
307 image->header = NULL;
308 return 0;
309}
310
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800311/* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */
312static int cbfs_add_entry_at(struct cbfs_image *image,
313 struct cbfs_file *entry,
314 uint32_t size,
315 const char *name,
316 uint32_t type,
317 const void *data,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700318 uint32_t content_offset)
319{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800320 struct cbfs_file *next = cbfs_find_next_entry(image, entry);
321 uint32_t addr = cbfs_get_entry_addr(image, entry),
322 addr_next = cbfs_get_entry_addr(image, next);
323 uint32_t header_size = cbfs_calculate_file_header_size(name),
324 min_entry_size = cbfs_calculate_file_header_size("");
325 uint32_t len, target;
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600326 uint32_t align = image->header->align;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800327
328 target = content_offset - header_size;
329 if (target % align)
330 target -= target % align;
331 if (target < addr) {
332 ERROR("No space to hold cbfs_file header.");
333 return -1;
334 }
335
336 // Process buffer BEFORE content_offset.
337 if (target - addr > min_entry_size) {
338 DEBUG("|min|...|header|content|... <create new entry>\n");
339 len = target - addr - min_entry_size;
340 cbfs_create_empty_entry(image, entry, len, "");
341 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
342 entry = cbfs_find_next_entry(image, entry);
343 addr = cbfs_get_entry_addr(image, entry);
344 }
345
346 len = size + (content_offset - addr - header_size);
347 cbfs_create_empty_entry(image, entry, len, name);
348 if (len != size) {
349 DEBUG("|..|header|content|... <use offset to create entry>\n");
350 DEBUG("before: offset=0x%x, len=0x%x\n",
351 ntohl(entry->offset), ntohl(entry->len));
352 // TODO reset expanded name buffer to 0xFF.
353 entry->offset = htonl(ntohl(entry->offset) + (len - size));
354 entry->len = htonl(size);
355 DEBUG("after: offset=0x%x, len=0x%x\n",
356 ntohl(entry->offset), ntohl(entry->len));
357 }
358
359 // Ready to fill data into entry.
360 assert(ntohl(entry->len) == size);
361 entry->type = htonl(type);
362 DEBUG("content_offset: 0x%x, entry location: %x\n",
363 content_offset, (int)((char*)CBFS_SUBHEADER(entry) -
364 image->buffer.data));
365 assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data ==
366 content_offset);
367 memcpy(CBFS_SUBHEADER(entry), data, size);
368 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
369
370 // Process buffer AFTER entry.
371 entry = cbfs_find_next_entry(image, entry);
372 addr = cbfs_get_entry_addr(image, entry);
373 assert(addr < addr_next);
374
375 if (addr_next - addr < min_entry_size) {
376 DEBUG("No space after content to keep CBFS structure.\n");
377 return -1;
378 }
379
380 len = addr_next - addr - min_entry_size;
381 cbfs_create_empty_entry(image, entry, len, "");
382 if (verbose > 1) cbfs_print_entry_info(image, entry, stderr);
383 return 0;
384}
385
386int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700387 const char *name, uint32_t type, uint32_t content_offset)
388{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800389 uint32_t entry_type;
390 uint32_t addr, addr_next;
391 struct cbfs_file *entry, *next;
392 uint32_t header_size, need_size, new_size;
393
394 header_size = cbfs_calculate_file_header_size(name);
395
396 need_size = header_size + buffer->size;
397 DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n",
398 name, content_offset, header_size, buffer->size, need_size);
399
400 if (IS_TOP_ALIGNED_ADDRESS(content_offset)) {
401 // legacy cbfstool takes top-aligned address.
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600402 uint32_t theromsize = image->header->romsize;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800403 INFO("Converting top-aligned address 0x%x to offset: 0x%x\n",
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800404 content_offset, content_offset + theromsize);
405 content_offset += theromsize;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800406 }
407
408 // Merge empty entries.
409 DEBUG("(trying to merge empty entries...)\n");
410 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
411
412 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800413 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800414 entry = cbfs_find_next_entry(image, entry)) {
415
416 entry_type = ntohl(entry->type);
417 if (entry_type != CBFS_COMPONENT_NULL)
418 continue;
419
420 addr = cbfs_get_entry_addr(image, entry);
421 next = cbfs_find_next_entry(image, entry);
422 addr_next = cbfs_get_entry_addr(image, next);
423
424 DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n",
425 addr, addr_next - addr, addr_next - addr);
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600426
427 /* Will the file fit? Don't yet worry if we have space for a new
428 * "empty" entry. We take care of that later.
429 */
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800430 if (addr + need_size > addr_next)
431 continue;
432
433 // Can we simply put object here?
434 if (!content_offset || content_offset == addr + header_size) {
435 DEBUG("Filling new entry data (%zd bytes).\n",
436 buffer->size);
437 cbfs_create_empty_entry(image, entry, buffer->size,
438 name);
439 entry->type = htonl(type);
440 memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size);
441 if (verbose)
442 cbfs_print_entry_info(image, entry, stderr);
443
444 // setup new entry
Paul Menzel4159a802013-07-14 00:24:31 +0200445 DEBUG("Setting new empty entry.\n");
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800446 entry = cbfs_find_next_entry(image, entry);
447 new_size = (cbfs_get_entry_addr(image, next) -
448 cbfs_get_entry_addr(image, entry));
Aaron Durbin1ebc7e92014-01-21 15:28:38 -0600449
450 /* Entry was added and no space for new "empty" entry */
451 if (new_size < cbfs_calculate_file_header_size("")) {
452 DEBUG("No need for new \"empty\" entry\n");
453 /* No need to increase the size of the just
454 * stored file to extend to next file. Alignment
455 * of next file takes care of this.
456 */
457 return 0;
458 }
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800459 new_size -= cbfs_calculate_file_header_size("");
460 DEBUG("new size: %d\n", new_size);
461 cbfs_create_empty_entry(image, entry, new_size, "");
462 if (verbose)
463 cbfs_print_entry_info(image, entry, stderr);
464 return 0;
465 }
466
467 // We need to put content here, and the case is really
468 // complicated...
469 assert(content_offset);
470 if (addr_next < content_offset) {
471 DEBUG("Not for specified offset yet");
472 continue;
473 } else if (addr > content_offset) {
474 DEBUG("Exceed specified content_offset.");
475 break;
476 } else if (addr + header_size > content_offset) {
477 ERROR("Not enough space for header.\n");
478 break;
479 } else if (content_offset + buffer->size > addr_next) {
480 ERROR("Not enough space for content.\n");
481 break;
482 }
483
484 // TODO there are more few tricky cases that we may
485 // want to fit by altering offset.
486 DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n",
487 addr, addr_next - addr, content_offset);
488
489 if (cbfs_add_entry_at(image, entry, buffer->size, name, type,
490 buffer->data, content_offset) == 0) {
491 return 0;
492 }
493 break;
494 }
495
496 ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n",
497 buffer->name, buffer->size, buffer->size / 1024, content_offset);
498 return -1;
499}
500
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700501struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name)
502{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800503 struct cbfs_file *entry;
504 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800505 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800506 entry = cbfs_find_next_entry(image, entry)) {
507 if (strcasecmp(CBFS_NAME(entry), name) == 0) {
508 DEBUG("cbfs_get_entry: found %s\n", name);
509 return entry;
510 }
511 }
512 return NULL;
513}
514
515int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700516 const char *filename)
517{
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800518 struct cbfs_file *entry = cbfs_get_entry(image, entry_name);
519 struct buffer buffer;
520 if (!entry) {
521 ERROR("File not found: %s\n", entry_name);
522 return -1;
523 }
524 LOG("Found file %.30s at 0x%x, type %.12s, size %d\n",
525 entry_name, cbfs_get_entry_addr(image, entry),
526 get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len));
527
528 if (ntohl(entry->type) != CBFS_COMPONENT_RAW) {
529 WARN("Only 'raw' files are safe to extract.\n");
530 }
531
532 buffer.data = CBFS_SUBHEADER(entry);
533 buffer.size = ntohl(entry->len);
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800534 buffer.name = (char *)"(cbfs_export_entry)";
Hung-Te Lin0f8af712013-01-29 02:29:49 +0800535 if (buffer_write_file(&buffer, filename) != 0) {
536 ERROR("Failed to write %s into %s.\n",
537 entry_name, filename);
538 return -1;
539 }
540 INFO("Successfully dumped the file to: %s\n", filename);
541 return 0;
542}
543
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700544int cbfs_remove_entry(struct cbfs_image *image, const char *name)
545{
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800546 struct cbfs_file *entry, *next;
547 size_t len;
548 entry = cbfs_get_entry(image, name);
549 if (!entry) {
550 ERROR("CBFS file %s not found.\n", name);
551 return -1;
552 }
553 next = cbfs_find_next_entry(image, entry);
554 assert(next);
555 DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n",
556 CBFS_NAME(entry), cbfs_get_entry_addr(image, entry));
557 entry->type = htonl(CBFS_COMPONENT_DELETED);
558 len = (cbfs_get_entry_addr(image, next) -
559 cbfs_get_entry_addr(image, entry));
560 entry->offset = htonl(cbfs_calculate_file_header_size(""));
561 entry->len = htonl(len - ntohl(entry->offset));
562 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
563 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE,
564 ntohl(entry->len));
565 return 0;
566}
567
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700568int cbfs_print_header_info(struct cbfs_image *image)
569{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800570 char *name = strdup(image->buffer.name);
571 assert(image && image->header);
572 printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n"
573 "alignment: %d bytes\n\n",
574 basename(name),
575 image->buffer.size / 1024,
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600576 image->header->bootblocksize,
577 image->header->romsize,
578 image->header->offset,
579 image->header->align);
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
598static int cbfs_print_payload_segment_info(struct cbfs_payload_segment *payload,
599 FILE *fp)
600{
601 switch(payload->type) {
602 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",
606 (payload->type == PAYLOAD_SEGMENT_CODE ?
607 "code " : "data"),
608 lookup_name_by_type(types_cbfs_compression,
609 ntohl(payload->compression),
610 "(unknown)"),
611 ntohl(payload->offset),
612 ntohll(payload->load_addr),
613 ntohl(payload->len), ntohl(payload->mem_len));
614 break;
615
616 case PAYLOAD_SEGMENT_ENTRY:
617 fprintf(fp, " entry (0x%" PRIx64 ")\n",
618 ntohll(payload->load_addr));
619 break;
620
621 case PAYLOAD_SEGMENT_BSS:
622 fprintf(fp, " BSS (address 0x%016" PRIx64 ", "
623 "length 0x%x)\n",
624 ntohll(payload->load_addr),
625 ntohl(payload->len));
626 break;
627
628 case PAYLOAD_SEGMENT_PARAMS:
629 fprintf(fp, " parameters\n");
630 break;
631
632 default:
633 fprintf(fp, " 0x%x (%s compression, offset: 0x%x, "
634 "load: 0x%" PRIx64 ", length: %d/%d\n",
635 payload->type,
636 lookup_name_by_type(types_cbfs_compression,
637 payload->compression,
638 "(unknown)"),
639 ntohl(payload->offset),
640 ntohll(payload->load_addr),
641 ntohl(payload->len),
642 ntohl(payload->mem_len));
643 break;
644 }
645 return 0;
646}
647
648int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700649 void *arg)
650{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800651 const char *name = CBFS_NAME(entry);
652 struct cbfs_payload_segment *payload;
653 FILE *fp = (FILE *)arg;
654
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800655 if (!cbfs_is_valid_entry(image, entry)) {
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800656 ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n",
657 cbfs_get_entry_addr(image, entry));
658 return -1;
659 }
660 if (!fp)
661 fp = stdout;
662
663 fprintf(fp, "%-30s 0x%-8x %-12s %d\n",
664 *name ? name : "(empty)",
665 cbfs_get_entry_addr(image, entry),
666 get_cbfs_entry_type_name(ntohl(entry->type)),
667 ntohl(entry->len));
668
669 if (!verbose)
670 return 0;
671
672 DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n",
673 cbfs_get_entry_addr(image, entry), ntohl(entry->offset),
674 cbfs_get_entry_addr(image, entry) + ntohl(entry->offset),
675 ntohl(entry->len));
676
677 /* note the components of the subheader may be in host order ... */
678 switch (ntohl(entry->type)) {
679 case CBFS_COMPONENT_STAGE:
680 cbfs_print_stage_info((struct cbfs_stage *)
681 CBFS_SUBHEADER(entry), fp);
682 break;
683
684 case CBFS_COMPONENT_PAYLOAD:
685 payload = (struct cbfs_payload_segment *)
686 CBFS_SUBHEADER(entry);
687 while (payload) {
688 cbfs_print_payload_segment_info(payload, fp);
689 if (payload->type == PAYLOAD_SEGMENT_ENTRY)
690 break;
691 else
692 payload ++;
693 }
694 break;
695 default:
696 break;
697 }
698 return 0;
699}
700
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700701int cbfs_print_directory(struct cbfs_image *image)
702{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800703 cbfs_print_header_info(image);
704 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
705 cbfs_walk(image, cbfs_print_entry_info, NULL);
706 return 0;
707}
708
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800709int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700710 void *arg)
711{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800712 struct cbfs_file *next;
713 uint32_t type, addr, last_addr;
714
715 type = ntohl(entry->type);
716 if (type == CBFS_COMPONENT_DELETED) {
717 // Ready to be recycled.
718 type = CBFS_COMPONENT_NULL;
719 entry->type = htonl(type);
720 }
721 if (type != CBFS_COMPONENT_NULL)
722 return 0;
723
724 next = cbfs_find_next_entry(image, entry);
725
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800726 while (next && cbfs_is_valid_entry(image, next)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800727 type = ntohl(next->type);
728 if (type == CBFS_COMPONENT_DELETED) {
729 type = CBFS_COMPONENT_NULL;
730 next->type = htonl(type);
731 }
732 if (type != CBFS_COMPONENT_NULL)
733 return 0;
734
735 addr = cbfs_get_entry_addr(image, entry);
736 last_addr = cbfs_get_entry_addr(
737 image, cbfs_find_next_entry(image, next));
738
739 // Now, we find two deleted/empty entries; try to merge now.
740 DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
741 cbfs_get_entry_addr(image, entry), ntohl(entry->len),
742 cbfs_get_entry_addr(image, next), ntohl(next->len));
743 cbfs_create_empty_entry(image, entry,
744 (last_addr - addr -
745 cbfs_calculate_file_header_size("")),
746 "");
747 DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
748 next = cbfs_find_next_entry(image, entry);
749 }
750 return 0;
751}
752
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800753int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700754 void *arg)
755{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800756 int count = 0;
757 struct cbfs_file *entry;
758 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800759 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800760 entry = cbfs_find_next_entry(image, entry)) {
761 count ++;
762 if (callback(image, entry, arg) != 0)
763 break;
764 }
765 return count;
766}
767
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700768struct cbfs_header *cbfs_find_header(char *data, size_t size)
769{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800770 size_t offset;
771 int found = 0;
772 uint32_t x86sig;
773 struct cbfs_header *header, *result = NULL;
774
775 // Try x86 style (check signature in bottom) header first.
776 x86sig = *(uint32_t *)(data + size - sizeof(uint32_t));
777 offset = (x86sig + (uint32_t)size);
778 DEBUG("x86sig: 0x%x, offset: 0x%zx\n", x86sig, offset);
779 if (offset >= size - sizeof(*header) ||
780 ntohl(((struct cbfs_header *)(data + offset))->magic) !=
781 CBFS_HEADER_MAGIC)
782 offset = 0;
783
784 for (; offset + sizeof(*header) < size; offset++) {
785 header = (struct cbfs_header *)(data + offset);
786 if (ntohl(header->magic) !=(CBFS_HEADER_MAGIC))
787 continue;
788 if (ntohl(header->version) != CBFS_HEADER_VERSION1 &&
789 ntohl(header->version) != CBFS_HEADER_VERSION2) {
790 // Probably not a real CBFS header?
791 continue;
792 }
793 found++;
794 result = header;
795 }
796 if (found > 1) {
797 ERROR("multiple (%d) CBFS headers found!\n",
798 found);
799 result = NULL;
800 }
801 return result;
802}
803
804
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700805struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
806{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800807 assert(image && image->header);
808 return (struct cbfs_file *)(image->buffer.data +
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600809 image->header->offset);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800810}
811
812struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700813 struct cbfs_file *entry)
814{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800815 uint32_t addr = cbfs_get_entry_addr(image, entry);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600816 int align = image->header->align;
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800817 assert(entry && cbfs_is_valid_entry(image, entry));
Hung-Te Lineab2c812013-01-29 01:56:17 +0800818 addr += ntohl(entry->offset) + ntohl(entry->len);
819 addr = align_up(addr, align);
820 return (struct cbfs_file *)(image->buffer.data + addr);
821}
822
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700823uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry)
824{
Hung-Te Lineab2c812013-01-29 01:56:17 +0800825 assert(image && image->buffer.data && entry);
826 return (int32_t)((char *)entry - image->buffer.data);
827}
828
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700829int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry)
830{
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800831 return (entry &&
832 (char *)entry >= image->buffer.data &&
833 (char *)entry + sizeof(entry->magic) <
834 image->buffer.data + image->buffer.size &&
835 memcmp(entry->magic, CBFS_FILE_MAGIC,
836 sizeof(entry->magic)) == 0);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800837}
838
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800839int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700840 size_t len, const char *name)
841{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800842 memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
843 memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
844 entry->type = htonl(CBFS_COMPONENT_NULL);
845 entry->len = htonl(len);
846 entry->checksum = 0; // TODO Build a checksum algorithm.
847 entry->offset = htonl(cbfs_calculate_file_header_size(name));
848 memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
849 strcpy(CBFS_NAME(entry), name);
850 memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
851 return 0;
852}
853
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700854/* Finds a place to hold whole data in same memory page. */
855static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page)
856{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800857 if (!page)
858 return 1;
859 return (start / page) == (start + size - 1) / page;
860}
861
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800862/* Tests if data can fit in a range by given offset:
863 * start ->| header_len | offset (+ size) |<- end
864 */
865static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700866 uint32_t offset, uint32_t size)
867{
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800868 return (offset >= start + header_len && offset + size <= end);
869}
870
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800871int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Stefan Reinauerdc7bc8e2013-03-26 12:51:36 -0700872 uint32_t size, uint32_t page_size, uint32_t align)
873{
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800874 struct cbfs_file *entry;
875 size_t need_len;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800876 uint32_t addr, addr_next, addr2, addr3, offset, header_len;
877
878 /* Default values: allow fitting anywhere in ROM. */
879 if (!page_size)
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600880 page_size = image->header->romsize;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800881 if (!align)
882 align = 1;
883
884 if (size > page_size)
885 ERROR("Input file size (%d) greater than page size (%d).\n",
886 size, page_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800887
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600888 if (page_size % image->header->align)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800889 WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -0600890 __func__, page_size, image->header->align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800891
892 /* TODO Old cbfstool always assume input is a stage file (and adding
893 * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800894 * (type) param in future. For right now, we assume cbfs_stage is the
895 * largest structure and add it into header size. */
896 assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800897 header_len = (cbfs_calculate_file_header_size(name) +
898 sizeof(struct cbfs_stage));
899 need_len = header_len + size;
900
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800901 // Merge empty entries to build get max available space.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800902 cbfs_walk(image, cbfs_merge_empty_entry, NULL);
903
904 /* Three cases of content location on memory page:
905 * case 1.
906 * | PAGE 1 | PAGE 2 |
907 * | <header><content>| Fit. Return start of content.
908 *
909 * case 2.
910 * | PAGE 1 | PAGE 2 |
911 * | <header><content> | Fits when we shift content to align
912 * shift-> | <header>|<content> | at starting of PAGE 2.
913 *
914 * case 3. (large content filling whole page)
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800915 * | PAGE 1 | PAGE 2 | PAGE 3 |
916 * | <header>< content > | Can't fit. If we shift content to
917 * |trial-> <header>< content > | PAGE 2, header can't fit in free
918 * | shift-> <header><content> space, so we must use PAGE 3.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800919 *
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800920 * The returned address can be then used as "base-address" (-b) in add-*
921 * commands (will be re-calculated and positioned by cbfs_add_entry_at).
922 * For stage targets, the address is also used to re-link stage before
923 * being added into CBFS.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800924 */
925 for (entry = cbfs_find_first_entry(image);
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800926 entry && cbfs_is_valid_entry(image, entry);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800927 entry = cbfs_find_next_entry(image, entry)) {
928
929 uint32_t type = ntohl(entry->type);
930 if (type != CBFS_COMPONENT_NULL)
931 continue;
932
933 addr = cbfs_get_entry_addr(image, entry);
934 addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
935 image, entry));
936 if (addr_next - addr < need_len)
937 continue;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800938
939 offset = align_up(addr + header_len, align);
940 if (is_in_same_page(offset, size, page_size) &&
941 is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800942 DEBUG("cbfs_locate_entry: FIT (PAGE1).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800943 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800944 }
945
946 addr2 = align_up(addr, page_size);
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800947 offset = align_up(addr2, align);
948 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800949 DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800950 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800951 }
952
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800953 /* Assume page_size >= header_len so adding one page will
954 * definitely provide the space for header. */
955 assert(page_size >= header_len);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800956 addr3 = addr2 + page_size;
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800957 offset = align_up(addr3, align);
958 if (is_in_range(addr, addr_next, header_len, offset, size)) {
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800959 DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800960 return offset;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800961 }
962 }
963 return -1;
964}