Hung-Te Lin ([email protected]) just uploaded a new patch set to gerrit, 
which you can find at http://review.coreboot.org/2213

-gerrit

commit a6fe302e9b61de92d5f5d8ac85ef67014de38a99
Author: Hung-Te Lin <[email protected]>
Date:   Tue Jan 29 03:46:02 2013 +0800

    cbfstool: Use cbfs_image API for "locate-stage" command.
    
    "locate-stage" command now outputs platform independent offset.  To 
retrieve x86
    top-aligned virtual address, add "-T".
    
    To test:
        cbfstool coreboot.rom locate-stage -f stage -n stage -a 0x100000 -T
        # Example output: 0xffffdc10
    
    Change-Id: I474703c4197b36524b75407a91faab1194edc64d
    Signed-off-by: Hung-Te Lin <[email protected]>
---
 src/arch/x86/Makefile.inc  |   2 +-
 util/cbfstool/cbfs_image.c | 136 +++++++++++++++++++++++++++++++++++++++++++++
 util/cbfstool/cbfs_image.h |  15 +++++
 util/cbfstool/cbfstool.c   |  51 ++++++++++++++---
 4 files changed, 195 insertions(+), 9 deletions(-)

diff --git a/src/arch/x86/Makefile.inc b/src/arch/x86/Makefile.inc
index 1956ceb..cb446bb 100644
--- a/src/arch/x86/Makefile.inc
+++ b/src/arch/x86/Makefile.inc
@@ -375,7 +375,7 @@ $(objgenerated)/romstage_xip.ld: 
$(objgenerated)/romstage_null.ld $(objcbfs)/bas
 
 $(objcbfs)/base_xip.txt: $(obj)/coreboot.pre1 $(objcbfs)/romstage_null.bin
        rm -f $@
-       $(CBFSTOOL) $(obj)/coreboot.pre1 locate-stage -f 
$(objcbfs)/romstage_null.bin -n $(CONFIG_CBFS_PREFIX)/romstage -a 
$(CONFIG_XIP_ROM_SIZE) > [email protected] \
+       $(CBFSTOOL) $(obj)/coreboot.pre1 locate-stage -T -f 
$(objcbfs)/romstage_null.bin -n $(CONFIG_CBFS_PREFIX)/romstage -a 
$(CONFIG_XIP_ROM_SIZE) > [email protected] \
         || { echo "The romstage is larger than XIP size. Please expand the 
CONFIG_XIP_ROM_SIZE" ; exit 1; }
        mv [email protected] $@
 
diff --git a/util/cbfstool/cbfs_image.c b/util/cbfstool/cbfs_image.c
index e7e5053..fafbd7f 100644
--- a/util/cbfstool/cbfs_image.c
+++ b/util/cbfstool/cbfs_image.c
@@ -356,6 +356,49 @@ int cbfs_print_directory(struct cbfs_image *image) {
        return 0;
 }
 
+int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
+                          void *arg) {
+       struct cbfs_file *next;
+       uint32_t type, addr, last_addr;
+
+       type = ntohl(entry->type);
+       if (type == CBFS_COMPONENT_DELETED) {
+               // Ready to be recycled.
+               type = CBFS_COMPONENT_NULL;
+               entry->type = htonl(type);
+       }
+       if (type != CBFS_COMPONENT_NULL)
+               return 0;
+
+       next = cbfs_find_next_entry(image, entry);
+
+       while (next && cbfs_is_valid_entry(next)) {
+               type = ntohl(next->type);
+               if (type == CBFS_COMPONENT_DELETED) {
+                       type = CBFS_COMPONENT_NULL;
+                       next->type = htonl(type);
+               }
+               if (type != CBFS_COMPONENT_NULL)
+                       return 0;
+
+               addr = cbfs_get_entry_addr(image, entry);
+               last_addr = cbfs_get_entry_addr(
+                               image, cbfs_find_next_entry(image, next));
+
+               // Now, we find two deleted/empty entries; try to merge now.
+               DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n",
+                     cbfs_get_entry_addr(image, entry), ntohl(entry->len),
+                     cbfs_get_entry_addr(image, next), ntohl(next->len));
+               cbfs_create_empty_entry(image, entry,
+                                       (last_addr - addr -
+                                        cbfs_calculate_file_header_size("")),
+                                       "");
+               DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len));
+               next = cbfs_find_next_entry(image, entry);
+       }
+       return 0;
+}
+
 int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
              void *arg) {
        int count = 0;
@@ -432,3 +475,96 @@ int cbfs_is_valid_entry(struct cbfs_file *entry) {
                               sizeof(entry->magic)) == 0);
 }
 
+int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
+                     size_t len, const char *name) {
+       memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry));
+       memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic));
+       entry->type = htonl(CBFS_COMPONENT_NULL);
+       entry->len = htonl(len);
+       entry->checksum = 0;  // TODO Build a checksum algorithm.
+       entry->offset = htonl(cbfs_calculate_file_header_size(name));
+       memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry));
+       strcpy(CBFS_NAME(entry), name);
+       memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len);
+       return 0;
+}
+
+/* Finds a place to hold whole stage data in same memory page.
+ */
+static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page) {
+       if (!page)
+               return 1;
+       return (start / page) == (start + size - 1) / page;
+}
+
+int32_t cbfs_locate_stage(struct cbfs_image *image, const char *name,
+                         uint32_t size, uint32_t page_size) {
+       struct cbfs_file *entry;
+       size_t need_len;
+       uint32_t addr, addr_next, addr2, addr3, header_len;
+       assert(size < page_size);
+
+       if (page_size % ntohl(image->header->align))
+               WARN("locate_stage page does not align with CBFS image.\n");
+
+       header_len = (cbfs_calculate_file_header_size(name) +
+                     sizeof(struct cbfs_stage));
+       need_len = header_len + size;
+
+       // Merge empty entries to build get max available pages.
+       cbfs_walk(image, cbfs_merge_empty_entry, NULL);
+
+       /* Three cases of content location on memory page:
+        * case 1.
+        *          |  PAGE 1  |   PAGE 2  |
+        *          |     <header><content>| Fit. Return start of content.
+        *
+        * case 2.
+        *          |  PAGE 1  |   PAGE 2  |
+        *          | <header><content>    | Fits when we shift content to align
+        *  shift-> |  <header>|<content>  | at starting of PAGE 2.
+        *
+        * case 3. (large content filling whole page)
+        *  |  PAGE 1  |   PAGE 2  | PAGE 3|
+        *  | <header><  content > |       | Can't fit. If we shift content to
+        *  |       {   free space .       } PAGE 2, header can't fit in free
+        *  |  shift->     <header><content> space, so we must use PAGE 3.
+        *
+        * The returned address will be used to re-link stage file, and then
+        * assigned to add-stage command (-b), which will be then re-calculated
+        * by ELF loader and positioned by cbfs_add_entry.
+        */
+       for (entry = cbfs_find_first_entry(image);
+            entry && cbfs_is_valid_entry(entry);
+            entry = cbfs_find_next_entry(image, entry)) {
+
+               uint32_t type = ntohl(entry->type);
+               if (type != CBFS_COMPONENT_NULL)
+                       continue;
+
+               addr = cbfs_get_entry_addr(image, entry);
+               addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry(
+                               image, entry));
+               if (addr_next - addr < need_len)
+                       continue;
+               if (is_in_same_page(addr + header_len, size, page_size)) {
+                       DEBUG("cbfs_locate_stage: FIT (PAGE1).");
+                       return addr + header_len;
+               }
+
+               addr2 = align_up(addr, page_size);
+               if (addr2 < addr_next && addr_next - addr2 >= size &&
+                   addr2 - addr >= header_len) {
+                       DEBUG("cbfs_locate_stage: OVERLAP (PAGE2).");
+                       return addr2;
+               }
+
+               addr3 = addr2 + page_size;
+               if (addr3 < addr_next && addr_next - addr3 >= size &&
+                   addr3 - addr >= header_len) {
+                       DEBUG("cbfs_locate_stage: OVERLAP+ (PAGE3).");
+                       return addr3;
+               }
+       }
+       return -1;
+}
diff --git a/util/cbfstool/cbfs_image.h b/util/cbfstool/cbfs_image.h
index e9f64a4..53944b9 100644
--- a/util/cbfstool/cbfs_image.h
+++ b/util/cbfstool/cbfs_image.h
@@ -49,6 +49,16 @@ int cbfs_export_entry(struct cbfs_image *image, const char 
*entry_name,
 /* Removes an entry from CBFS image. Returns 0 on success, otherwise non-zero. 
*/
 int cbfs_remove_entry(struct cbfs_image *image, const char *name);
 
+/* Initializes a new empty (type = NULL) entry with size and name in CBFS 
image.
+ * Returns 0 on success, otherwise (ex, not found) non-zero. */
+int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
+                           size_t len, const char *name);
+
+/* Finds a location to put whole cbfs_stage content in same memory page.
+ * Returns a valid offset, or -1 on failure. */
+int32_t cbfs_locate_stage(struct cbfs_image *image, const char *name,
+                         uint32_t size, uint32_t page_size);
+
 /* Callback function used by cbfs_walk.
  * Returns 0 on success, or non-zero to stop further iteration. */
 typedef int (*cbfs_entry_callback)(struct cbfs_image *image,
@@ -90,4 +100,9 @@ int cbfs_print_header_info(struct cbfs_image *image);
 int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
                          void *arg);
 
+/* Merge empty entries starting from given entry.
+ * Returns 0 on success, otherwise non-zero. */
+int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
+                          void *arg);
+
 #endif
diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c
index 7dda508..148e80a 100644
--- a/util/cbfstool/cbfstool.c
+++ b/util/cbfstool/cbfstool.c
@@ -48,6 +48,7 @@ static struct param {
        uint32_t size;
        uint32_t alignment;
        uint32_t offset;
+       uint32_t top_aligned;
        comp_algo algo;
 } param = {
        /* All variables not listed are initialized as zero. */
@@ -363,9 +364,11 @@ static int cbfstool_create(void)
                                                param.alignment, param.offset);
 }
 
-static int cbfs_locate_stage(void)
+static int cbfstool_locate_stage(void)
 {
-       uint32_t filesize, location;
+       struct cbfs_image image;
+       struct buffer buffer;
+       int32_t address;
 
        if (!param.filename) {
                ERROR("You need to specify -f/--filename.\n");
@@ -377,13 +380,42 @@ static int cbfs_locate_stage(void)
                return 1;
        }
 
-       filesize = getfilesize(param.filename);
+       if (cbfs_image_from_file(&image, param.cbfs_name) != 0) {
+               ERROR("Failed to load %s.\n", param.cbfs_name);
+               return 1;
+       }
+
+       if (buffer_from_file(&buffer, param.filename) != 0) {
+               ERROR("Cannot load %s.\n", param.filename);
+               cbfs_image_delete(&image);
+               return 1;
+       }
+
+       if (cbfs_get_entry(&image, param.name)) {
+               ERROR("'%s' already in CBFS.\n", param.name);
+               buffer_delete(&buffer);
+               cbfs_image_delete(&image);
+               return 1;
+       }
+
+       address = cbfs_locate_stage(&image, param.name, buffer.size,
+                                   param.alignment);
+       if (address == -1) {
+               ERROR("'%s' can't fit in CBFS for align 0x%x.\n",
+                       param.name, param.alignment);
+               buffer_delete(&buffer);
+               cbfs_image_delete(&image);
+               return 1;
+       }
 
-       location = cbfs_find_stage_location(param.cbfs_name, filesize,
-                                           param.name, param.alignment);
+       if (param.top_aligned) {
+               address = address - ntohl(image.header->romsize);
+       }
 
-       printf("0x%x\n", location);
-       return location == 0 ? 1 : 0;
+       buffer_delete(&buffer);
+       cbfs_image_delete(&image);
+       printf("0x%x\n", address);
+       return 0;
 }
 
 static int cbfstool_print(void)
@@ -478,7 +510,7 @@ static void usage(char *name)
                        "Remove a component\n"
             " create -s size -B bootblock -m ARCH [-a align] [-o offset]  "
                        "Create a ROM file\n"
-            " locate-stage -f FILE -n NAME [-a align]                     "
+            " locate-stage -f FILE -n NAME [-a align] [-T]                "
                        "Find a place for a stage file of that size\n"
             " print                                                       "
                        "Show the contents of the ROM\n"
@@ -579,6 +611,9 @@ int main(int argc, char **argv)
                        case 'f':
                                param.filename = optarg;
                                break;
+                       case 'T':
+                               param.top_aligned = 1;
+                               break;
                        case 'v':
                                verbose++;
                                break;

-- 
coreboot mailing list: [email protected]
http://www.coreboot.org/mailman/listinfo/coreboot

Reply via email to