cbfstool locate: Implement alignment switch --align/-a

cbfstool usage change:
 "-a" for "cbfstool locate" can specify base address alignment.

To support putting a blob in aligned location (ex, microcode needs to be aligned
in 0x10), alignment (-a) is implemented into "locate" command.

Verified by manually testing a file (324 bytes) with alignment=0x10:
 cbfstool coreboot.rom locate -f test -n test -a 0x10
 # output: 0x71fdd0
 cbfstool coreboot.rom add -f test -n test -t raw -b 0x71fdd0
 cbfstool coreboot.rom print -v -v
 # output: test                           0x71fd80   raw          324
 # output:  cbfs_file=0x71fd80, offset=0x50, content_address=0x71fdd0+0x144

Also verified to be compatible with old behavior by building i386/axus/tc320
(with page limitation 0x40000):
 cbfstool coreboot.rom locate -f romstage_null.bin -n romstage -P 0x40000
 # output: 0x44
 cbfstool coreboot.rom locate -f x.bin -n romstage -P 0x40000 -a 0x30
 # output: 0x60

Change-Id: I78b549fe6097ce5cb6162b09f064853827069637
Signed-off-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-on: http://review.coreboot.org/2824
Reviewed-by: Paul Menzel <paulepanter@users.sourceforge.net>
Tested-by: build bot (Jenkins)
This commit is contained in:
Hung-Te Lin 2013-03-19 12:24:43 +08:00
parent b3b72f350e
commit e4ea2ca18d
3 changed files with 58 additions and 29 deletions

View File

@ -773,7 +773,7 @@ int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
return 0; return 0;
} }
/* Finds a place to hold whole stage data in same memory page. /* Finds a place to hold whole data in same memory page.
*/ */
static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page) { static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page) {
if (!page) if (!page)
@ -781,24 +781,44 @@ static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page) {
return (start / page) == (start + size - 1) / page; return (start / page) == (start + size - 1) / page;
} }
/* Tests if data can fit in a range by given offset:
* start ->| header_len | offset (+ size) |<- end
*/
static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len,
uint32_t offset, uint32_t size) {
return (offset >= start + header_len && offset + size <= end);
}
int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name, int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
uint32_t size, uint32_t page_size) { uint32_t size, uint32_t page_size, uint32_t align) {
struct cbfs_file *entry; struct cbfs_file *entry;
size_t need_len; size_t need_len;
uint32_t addr, addr_next, addr2, addr3, header_len; uint32_t addr, addr_next, addr2, addr3, offset, header_len;
assert(size < page_size);
/* Default values: allow fitting anywhere in ROM. */
if (!page_size)
page_size = ntohl(image->header->romsize);
if (!align)
align = 1;
if (size > page_size)
ERROR("Input file size (%d) greater than page size (%d).\n",
size, page_size);
if (page_size % ntohl(image->header->align)) if (page_size % ntohl(image->header->align))
WARN("locate_entry: page does not align with CBFS image.\n"); WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n",
__func__, page_size, ntohl(image->header->align));
/* TODO Old cbfstool always assume input is a stage file (and adding /* TODO Old cbfstool always assume input is a stage file (and adding
* sizeof(cbfs_stage) for header. We should fix that by adding "-t" * sizeof(cbfs_stage) for header. We should fix that by adding "-t"
* (type) param in future. For right now, follow old behavior. */ * (type) param in future. For right now, we assume cbfs_stage is the
* largest structure and add it into header size. */
assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload));
header_len = (cbfs_calculate_file_header_size(name) + header_len = (cbfs_calculate_file_header_size(name) +
sizeof(struct cbfs_stage)); sizeof(struct cbfs_stage));
need_len = header_len + size; need_len = header_len + size;
// Merge empty entries to build get max available pages. // Merge empty entries to build get max available space.
cbfs_walk(image, cbfs_merge_empty_entry, NULL); cbfs_walk(image, cbfs_merge_empty_entry, NULL);
/* Three cases of content location on memory page: /* Three cases of content location on memory page:
@ -812,14 +832,15 @@ int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
* shift-> | <header>|<content> | at starting of PAGE 2. * shift-> | <header>|<content> | at starting of PAGE 2.
* *
* case 3. (large content filling whole page) * case 3. (large content filling whole page)
* | PAGE 1 | PAGE 2 | PAGE 3| * | PAGE 1 | PAGE 2 | PAGE 3 |
* | <header>< content > | | Can't fit. If we shift content to * | <header>< content > | Can't fit. If we shift content to
* | { free space . } PAGE 2, header can't fit in free * |trial-> <header>< content > | PAGE 2, header can't fit in free
* | shift-> <header><content> space, so we must use PAGE 3. * | shift-> <header><content> space, so we must use PAGE 3.
* *
* The returned address will be used to re-link stage file, and then * The returned address can be then used as "base-address" (-b) in add-*
* assigned to add-stage command (-b), which will be then re-calculated * commands (will be re-calculated and positioned by cbfs_add_entry_at).
* by ELF loader and positioned by cbfs_add_entry. * For stage targets, the address is also used to re-link stage before
* being added into CBFS.
*/ */
for (entry = cbfs_find_first_entry(image); for (entry = cbfs_find_first_entry(image);
entry && cbfs_is_valid_entry(image, entry); entry && cbfs_is_valid_entry(image, entry);
@ -834,23 +855,29 @@ int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
image, entry)); image, entry));
if (addr_next - addr < need_len) if (addr_next - addr < need_len)
continue; continue;
if (is_in_same_page(addr + header_len, size, page_size)) {
offset = align_up(addr + header_len, align);
if (is_in_same_page(offset, size, page_size) &&
is_in_range(addr, addr_next, header_len, offset, size)) {
DEBUG("cbfs_locate_entry: FIT (PAGE1)."); DEBUG("cbfs_locate_entry: FIT (PAGE1).");
return addr + header_len; return offset;
} }
addr2 = align_up(addr, page_size); addr2 = align_up(addr, page_size);
if (addr2 < addr_next && addr_next - addr2 >= size && offset = align_up(addr2, align);
addr2 - addr >= header_len) { if (is_in_range(addr, addr_next, header_len, offset, size)) {
DEBUG("cbfs_locate_entry: OVERLAP (PAGE2)."); DEBUG("cbfs_locate_entry: OVERLAP (PAGE2).");
return addr2; return offset;
} }
/* Assume page_size >= header_len so adding one page will
* definitely provide the space for header. */
assert(page_size >= header_len);
addr3 = addr2 + page_size; addr3 = addr2 + page_size;
if (addr3 < addr_next && addr_next - addr3 >= size && offset = align_up(addr3, align);
addr3 - addr >= header_len) { if (is_in_range(addr, addr_next, header_len, offset, size)) {
DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3)."); DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3).");
return addr3; return offset;
} }
} }
return -1; return -1;

View File

@ -76,10 +76,12 @@ int cbfs_remove_entry(struct cbfs_image *image, const char *name);
int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry, int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
size_t len, const char *name); size_t len, const char *name);
/* Finds a location to put given content in same memory page. /* Finds a location to put given content by specified criteria:
* "page_size" limits the content to fit on same memory page, and
* "align" specifies starting address alignment.
* Returns a valid offset, or -1 on failure. */ * Returns a valid offset, or -1 on failure. */
int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name, int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
uint32_t size, uint32_t page_size); uint32_t size, uint32_t page_size, uint32_t align);
/* Callback function used by cbfs_walk. /* Callback function used by cbfs_walk.
* Returns 0 on success, or non-zero to stop further iteration. */ * Returns 0 on success, or non-zero to stop further iteration. */

View File

@ -356,12 +356,12 @@ static int cbfs_locate(void)
} }
address = cbfs_locate_entry(&image, param.name, buffer.size, address = cbfs_locate_entry(&image, param.name, buffer.size,
param.pagesize); param.pagesize, param.alignment);
buffer_delete(&buffer); buffer_delete(&buffer);
if (address == -1) { if (address == -1) {
ERROR("'%s' can't fit in CBFS for page-size %#x.\n", ERROR("'%s' can't fit in CBFS for page-size %#x, align %#x.\n",
param.name, param.pagesize); param.name, param.pagesize, param.alignment);
cbfs_image_delete(&image); cbfs_image_delete(&image);
return 1; return 1;
} }
@ -422,7 +422,7 @@ static const struct command commands[] = {
{"add-flat-binary", "f:n:l:e:c:b:vh?", cbfs_add_flat_binary}, {"add-flat-binary", "f:n:l:e:c:b:vh?", cbfs_add_flat_binary},
{"remove", "n:vh?", cbfs_remove}, {"remove", "n:vh?", cbfs_remove},
{"create", "s:B:b:H:a:o:m:vh?", cbfs_create}, {"create", "s:B:b:H:a:o:m:vh?", cbfs_create},
{"locate", "f:n:P:Tvh?", cbfs_locate}, {"locate", "f:n:P:a:Tvh?", cbfs_locate},
{"print", "vh?", cbfs_print}, {"print", "vh?", cbfs_print},
{"extract", "n:f:vh?", cbfs_extract}, {"extract", "n:f:vh?", cbfs_extract},
}; };
@ -470,7 +470,7 @@ static void usage(char *name)
"Remove a component\n" "Remove a component\n"
" create -s size -B bootblock -m ARCH [-a align] [-o offset] " " create -s size -B bootblock -m ARCH [-a align] [-o offset] "
"Create a ROM file\n" "Create a ROM file\n"
" locate -f FILE -n NAME [-P page-size] [-T] " " locate -f FILE -n NAME [-P page-size] [-a align] [-T] "
"Find a place for a file of that size\n" "Find a place for a file of that size\n"
" print " " print "
"Show the contents of the ROM\n" "Show the contents of the ROM\n"