Furquan Shaikh has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
util/cbfstool: Introduce concept of mmap_window
This change adds the concept of mmap_window to describe how the SPI flash address space is mapped to host address space. It gets rid of the assumption that the SPI flash address space is mapped only below the 4G boundary in host space. This is required in follow up changes to be able to add more decode windows for the SPI flash into the host address space.
Currently, a single mmap window is added i.e. the default x86 decode window of maximum 16MiB size living just below the 4G boundary.
Change-Id: I8dd3d1c922cc834c1e67f279ffce8fa438d8209c Signed-off-by: Furquan Shaikh furquan@google.com --- M util/cbfstool/cbfstool.c 1 file changed, 125 insertions(+), 15 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/31/47831/1
diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c index 061ff82..08d3cd0 100644 --- a/util/cbfstool/cbfstool.c +++ b/util/cbfstool/cbfstool.c @@ -17,6 +17,7 @@ #include <commonlib/fsp.h> #include <commonlib/endian.h> #include <commonlib/helpers.h> +#include <commonlib/region.h>
#define SECTION_WITH_FIT_TABLE "BOOTBLOCK"
@@ -107,18 +108,130 @@ CBFS_FILE_MAGIC, strlen(CBFS_FILE_MAGIC)); }
-/* - * Converts between offsets from the start of the specified image region and - * "top-aligned" offsets from the top of the entire boot media. - */ -static unsigned convert_to_from_absolute_top_aligned( - const struct buffer *region, unsigned offset) +/* This describes a window from the SPI flash address space into the host address space. */ +struct mmap_window { + struct region flash_space; + struct region host_space; +}; + +enum mmap_window_type { + X86_DEFAULT_DECODE_WINDOW, /* Decode window just below 4G boundary */ + MMAP_MAX_WINDOWS, +}; + +/* Table of all the decode windows supported by the platform. */ +static struct mmap_window mmap_window_table[MMAP_MAX_WINDOWS]; + +static void add_mmap_window(enum mmap_window_type idx, size_t flash_offset, size_t host_offset, + size_t window_size) +{ + if (idx >= MMAP_MAX_WINDOWS) { + ERROR("Incorrect mmap window index(%d)\n", idx); + return; + } + + mmap_window_table[idx].flash_space.offset = flash_offset; + mmap_window_table[idx].host_space.offset = host_offset; + mmap_window_table[idx].flash_space.size = window_size; + mmap_window_table[idx].host_space.size = window_size; +} + +static void create_mmap_windows(void) +{ + static bool done; + + if (done) + return; + + const size_t image_size = partitioned_file_total_size(param.image_file); + const size_t window_size = MIN(16 * MiB, image_size); + + /* + * Default decode window lives just below 4G boundary in host space and maps upto a + * maximum of 16MiB. + */ + add_mmap_window(X86_DEFAULT_DECODE_WINDOW, image_size - window_size, + 4ULL * GiB - window_size, window_size); + + done = true; +} + +static unsigned convert_address(const struct region *to, const struct region *from, + unsigned addr) +{ + /* + * Calculate the offset in the "from" region and use that offset to calculate + * corresponding address in the "to" region. + */ + size_t offset = addr - region_offset(from); + return region_offset(to) + offset; +} + +static int find_mmap_window(size_t struct_offset, unsigned addr) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(mmap_window_table); i++) { + const struct region *reg; + reg = (struct region *)((char *)&mmap_window_table[i] + struct_offset); + if (region_offset(reg) <= addr && region_end(reg) >= addr) + return i; + } + + return -1; +} + +static unsigned convert_host_to_flash(const struct buffer *region, unsigned addr) +{ + int idx; + const struct region *to, *from; + + idx = find_mmap_window(offsetof(struct mmap_window, host_space), addr); + if (idx == -1) { + ERROR("Host address(%x) not in any mmap window!\n", addr); + return 0; + } + + to = &mmap_window_table[idx].flash_space; + from = &mmap_window_table[idx].host_space; + + /* region->offset is subtracted because caller expects offset in the given region. */ + return convert_address(to, from, addr) - region->offset; +} + +static unsigned convert_flash_to_host(const struct buffer *region, unsigned addr) +{ + int idx; + const struct region *to, *from; + + /* + * region->offset is added because caller provides offset in the given region. This is + * converted to an absolute address in the SPI flash space. + */ + addr += region->offset; + idx = find_mmap_window(offsetof(struct mmap_window, flash_space), addr); + + if (idx == -1) { + ERROR("SPI flash address(%x) not in any mmap window!\n", addr); + return 0; + } + + to = &mmap_window_table[idx].host_space; + from = &mmap_window_table[idx].flash_space; + + return convert_address(to, from, addr); +} + +static unsigned convert_addr_space(const struct buffer *region, unsigned addr) { assert(region);
- size_t image_size = partitioned_file_total_size(param.image_file); + create_mmap_windows();
- return image_size - region->offset - offset; + if (IS_HOST_SPACE_ADDRESS(addr)) + return convert_host_to_flash(region, addr); + else + return convert_flash_to_host(region, addr); }
/* @@ -564,8 +677,8 @@ }
if (IS_HOST_SPACE_ADDRESS(offset)) - offset = convert_to_from_absolute_top_aligned(param.image_region, - -offset); + offset = convert_addr_space(param.image_region, offset); + if (cbfs_add_entry(&image, &buffer, offset, header, len_align) != 0) { ERROR("Failed to add '%s' into ROM image.\n", filename); free(header); @@ -647,8 +760,7 @@ */ if (param.stage_xip) { if (!IS_HOST_SPACE_ADDRESS(address)) - address = -convert_to_from_absolute_top_aligned( - param.image_region, address); + address = convert_addr_space(param.image_region, address); } else { if (param.baseaddress_assigned == 0) { INFO("Honoring pre-linked FSP module.\n"); @@ -721,9 +833,7 @@ * x86 semantics about the boot media being directly mapped * below 4GiB in the CPU address space. **/ - address = -convert_to_from_absolute_top_aligned( - param.image_region, address); - *offset = address; + *offset = convert_addr_space(param.image_region, address);
ret = parse_elf_to_xip_stage(buffer, &output, offset, param.ignore_section);
Furquan Shaikh has uploaded a new patch set (#2). ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
util/cbfstool: Introduce concept of mmap_window
This change adds the concept of mmap_window to describe how the SPI flash address space is mapped to host address space. It gets rid of the assumption that the SPI flash address space is mapped only below the 4G boundary in host space. This is required in follow up changes to be able to add more decode windows for the SPI flash into the host address space.
Currently, a single mmap window is added i.e. the default x86 decode window of maximum 16MiB size living just below the 4G boundary.
TEST=Verified using abuild with timeless option for all coreboot boards that there is no change in the resultant coreboot.rom file.
Change-Id: I8dd3d1c922cc834c1e67f279ffce8fa438d8209c Signed-off-by: Furquan Shaikh furquan@google.com --- M util/cbfstool/cbfstool.c 1 file changed, 125 insertions(+), 15 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/31/47831/2
build bot (Jenkins) has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 1:
(10 comments)
https://review.coreboot.org/c/coreboot/+/47831/1/util/cbfstool/cbfstool.c File util/cbfstool/cbfstool.c:
https://review.coreboot.org/c/coreboot/+/47831/1/util/cbfstool/cbfstool.c@15... PS1, Line 150: * Default decode window lives just below 4G boundary in host space and maps upto a 'upto' may be misspelled - perhaps 'up to'?
https://review.coreboot.org/c/coreboot/+/47831/1/util/cbfstool/cbfstool.c@15... PS1, Line 159: static unsigned convert_address(const struct region *to, const struct region *from, Prefer 'unsigned int' to bare use of 'unsigned'
https://review.coreboot.org/c/coreboot/+/47831/1/util/cbfstool/cbfstool.c@16... PS1, Line 160: unsigned addr) Prefer 'unsigned int' to bare use of 'unsigned'
https://review.coreboot.org/c/coreboot/+/47831/1/util/cbfstool/cbfstool.c@17... PS1, Line 170: static int find_mmap_window(size_t struct_offset, unsigned addr) Prefer 'unsigned int' to bare use of 'unsigned'
https://review.coreboot.org/c/coreboot/+/47831/1/util/cbfstool/cbfstool.c@18... PS1, Line 184: static unsigned convert_host_to_flash(const struct buffer *region, unsigned addr) Prefer 'unsigned int' to bare use of 'unsigned'
https://review.coreboot.org/c/coreboot/+/47831/1/util/cbfstool/cbfstool.c@18... PS1, Line 184: static unsigned convert_host_to_flash(const struct buffer *region, unsigned addr) Prefer 'unsigned int' to bare use of 'unsigned'
https://review.coreboot.org/c/coreboot/+/47831/1/util/cbfstool/cbfstool.c@20... PS1, Line 202: static unsigned convert_flash_to_host(const struct buffer *region, unsigned addr) Prefer 'unsigned int' to bare use of 'unsigned'
https://review.coreboot.org/c/coreboot/+/47831/1/util/cbfstool/cbfstool.c@20... PS1, Line 202: static unsigned convert_flash_to_host(const struct buffer *region, unsigned addr) Prefer 'unsigned int' to bare use of 'unsigned'
https://review.coreboot.org/c/coreboot/+/47831/1/util/cbfstool/cbfstool.c@22... PS1, Line 225: static unsigned convert_addr_space(const struct buffer *region, unsigned addr) Prefer 'unsigned int' to bare use of 'unsigned'
https://review.coreboot.org/c/coreboot/+/47831/1/util/cbfstool/cbfstool.c@22... PS1, Line 225: static unsigned convert_addr_space(const struct buffer *region, unsigned addr) Prefer 'unsigned int' to bare use of 'unsigned'
Hello build bot (Jenkins),
I'd like you to reexamine a change. Please visit
https://review.coreboot.org/c/coreboot/+/47831
to look at the new patch set (#3).
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
util/cbfstool: Introduce concept of mmap_window
This change adds the concept of mmap_window to describe how the SPI flash address space is mapped to host address space on x86 platforms. It gets rid of the assumption that the SPI flash address space is mapped only below the 4G boundary in host space. This is required in follow up changes to be able to add more decode windows for the SPI flash into the host address space.
Currently, a single mmap window is added i.e. the default x86 decode window of maximum 16MiB size living just below the 4G boundary. If the window is smaller than 16MiB, then it is mapped at the top of the host window.
TEST=Verified using abuild with timeless option for all coreboot boards that there is no change in the resultant coreboot.rom file.
Change-Id: I8dd3d1c922cc834c1e67f279ffce8fa438d8209c Signed-off-by: Furquan Shaikh furquan@google.com --- M util/cbfstool/cbfstool.c 1 file changed, 126 insertions(+), 13 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/31/47831/3
Hello build bot (Jenkins),
I'd like you to reexamine a change. Please visit
https://review.coreboot.org/c/coreboot/+/47831
to look at the new patch set (#4).
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
util/cbfstool: Introduce concept of mmap_window
This change adds the concept of mmap_window to describe how the SPI flash address space is mapped to host address space on x86 platforms. It gets rid of the assumption that the SPI flash address space is mapped only below the 4G boundary in host space. This is required in follow up changes to be able to add more decode windows for the SPI flash into the host address space.
Currently, a single mmap window is added i.e. the default x86 decode window of maximum 16MiB size living just below the 4G boundary. If the window is smaller than 16MiB, then it is mapped at the top of the host window.
BUG=b:171534504 TEST=Verified using abuild with timeless option for all coreboot boards that there is no change in the resultant coreboot.rom file.
Change-Id: I8dd3d1c922cc834c1e67f279ffce8fa438d8209c Signed-off-by: Furquan Shaikh furquan@google.com --- M util/cbfstool/cbfstool.c 1 file changed, 126 insertions(+), 13 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/31/47831/4
Srinidhi N Kaushik has uploaded a new patch set (#8) to the change originally created by Furquan Shaikh. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
util/cbfstool: Introduce concept of mmap_window
This change adds the concept of mmap_window to describe how the SPI flash address space is mapped to host address space on x86 platforms. It gets rid of the assumption that the SPI flash address space is mapped only below the 4G boundary in host space. This is required in follow up changes to be able to add more decode windows for the SPI flash into the host address space.
Currently, a single mmap window is added i.e. the default x86 decode window of maximum 16MiB size living just below the 4G boundary. If the window is smaller than 16MiB, then it is mapped at the top of the host window.
BUG=b:171534504 TEST=Verified using abuild with timeless option for all coreboot boards that there is no change in the resultant coreboot.rom file.
Change-Id: I8dd3d1c922cc834c1e67f279ffce8fa438d8209c Signed-off-by: Furquan Shaikh furquan@google.com --- M util/cbfstool/cbfstool.c 1 file changed, 126 insertions(+), 13 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/31/47831/8
Hello build bot (Jenkins), Duncan Laurie, Tim Wawrzynczak, Srinidhi N Kaushik,
I'd like you to reexamine a change. Please visit
https://review.coreboot.org/c/coreboot/+/47831
to look at the new patch set (#11).
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
util/cbfstool: Introduce concept of mmap_window
This change adds the concept of mmap_window to describe how the SPI flash address space is mapped to host address space on x86 platforms. It gets rid of the assumption that the SPI flash address space is mapped only below the 4G boundary in host space. This is required in follow up changes to be able to add more decode windows for the SPI flash into the host address space.
Currently, a single mmap window is added i.e. the default x86 decode window of maximum 16MiB size living just below the 4G boundary. If the window is smaller than 16MiB, then it is mapped at the top of the host window.
BUG=b:171534504 TEST=Verified using abuild with timeless option for all coreboot boards that there is no change in the resultant coreboot.rom file.
Change-Id: I8dd3d1c922cc834c1e67f279ffce8fa438d8209c Signed-off-by: Furquan Shaikh furquan@google.com --- M util/cbfstool/cbfstool.c 1 file changed, 126 insertions(+), 13 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/31/47831/11
Duncan Laurie has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 11:
(3 comments)
https://review.coreboot.org/c/coreboot/+/47831/11/util/cbfstool/cbfstool.c File util/cbfstool/cbfstool.c:
https://review.coreboot.org/c/coreboot/+/47831/11/util/cbfstool/cbfstool.c@1... PS11, Line 131: /* : * pointless nit about comment formatting
https://review.coreboot.org/c/coreboot/+/47831/11/util/cbfstool/cbfstool.c@1... PS11, Line 187: reg = (struct region *)((char *)&mmap_window_table[i] + struct_offset); this is a pretty messy way to get at the structure, any reason you didn't add something like an enum for HOST_SPACE/FLASH_SPACE to pass around instead of using offsetof? I'm just complaining because it takes more work to understand what it is doing when reading the code..
https://review.coreboot.org/c/coreboot/+/47831/11/util/cbfstool/cbfstool.c@2... PS11, Line 219: added might be good to note why this is done before the convert instead of after (like the host_to_flash case). (it makes sense once you think about it but for those attempting to read the code for the first time...)
Hello build bot (Jenkins), Duncan Laurie, Tim Wawrzynczak, Srinidhi N Kaushik,
I'd like you to reexamine a change. Please visit
https://review.coreboot.org/c/coreboot/+/47831
to look at the new patch set (#12).
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
util/cbfstool: Introduce concept of mmap_window
This change adds the concept of mmap_window to describe how the SPI flash address space is mapped to host address space on x86 platforms. It gets rid of the assumption that the SPI flash address space is mapped only below the 4G boundary in host space. This is required in follow up changes to be able to add more decode windows for the SPI flash into the host address space.
Currently, a single mmap window is added i.e. the default x86 decode window of maximum 16MiB size living just below the 4G boundary. If the window is smaller than 16MiB, then it is mapped at the top of the host window.
BUG=b:171534504 TEST=Verified using abuild with timeless option for all coreboot boards that there is no change in the resultant coreboot.rom file.
Change-Id: I8dd3d1c922cc834c1e67f279ffce8fa438d8209c Signed-off-by: Furquan Shaikh furquan@google.com --- M util/cbfstool/cbfstool.c 1 file changed, 139 insertions(+), 14 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/31/47831/12
Furquan Shaikh has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 11:
(3 comments)
https://review.coreboot.org/c/coreboot/+/47831/11/util/cbfstool/cbfstool.c File util/cbfstool/cbfstool.c:
https://review.coreboot.org/c/coreboot/+/47831/11/util/cbfstool/cbfstool.c@1... PS11, Line 131: /* : *
pointless nit about comment formatting
Done
https://review.coreboot.org/c/coreboot/+/47831/11/util/cbfstool/cbfstool.c@1... PS11, Line 187: reg = (struct region *)((char *)&mmap_window_table[i] + struct_offset);
this is a pretty messy way to get at the structure, any reason you didn't add something like an enum […]
Done
https://review.coreboot.org/c/coreboot/+/47831/11/util/cbfstool/cbfstool.c@2... PS11, Line 219: added
might be good to note why this is done before the convert instead of after (like the host_to_flash c […]
Done
Tim Wawrzynczak has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 13: Code-Review+1
(1 comment)
https://review.coreboot.org/c/coreboot/+/47831/13/util/cbfstool/cbfstool.c File util/cbfstool/cbfstool.c:
https://review.coreboot.org/c/coreboot/+/47831/13/util/cbfstool/cbfstool.c@2... PS13, Line 231: with within?
Hello build bot (Jenkins), Tim Wawrzynczak, Duncan Laurie, Srinidhi N Kaushik,
I'd like you to reexamine a change. Please visit
https://review.coreboot.org/c/coreboot/+/47831
to look at the new patch set (#14).
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
util/cbfstool: Introduce concept of mmap_window
This change adds the concept of mmap_window to describe how the SPI flash address space is mapped to host address space on x86 platforms. It gets rid of the assumption that the SPI flash address space is mapped only below the 4G boundary in host space. This is required in follow up changes to be able to add more decode windows for the SPI flash into the host address space.
Currently, a single mmap window is added i.e. the default x86 decode window of maximum 16MiB size living just below the 4G boundary. If the window is smaller than 16MiB, then it is mapped at the top of the host window.
BUG=b:171534504 TEST=Verified using abuild with timeless option for all coreboot boards that there is no change in the resultant coreboot.rom file.
Change-Id: I8dd3d1c922cc834c1e67f279ffce8fa438d8209c Signed-off-by: Furquan Shaikh furquan@google.com --- M util/cbfstool/cbfstool.c 1 file changed, 140 insertions(+), 14 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/31/47831/14
Furquan Shaikh has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 13:
(1 comment)
https://review.coreboot.org/c/coreboot/+/47831/13/util/cbfstool/cbfstool.c File util/cbfstool/cbfstool.c:
https://review.coreboot.org/c/coreboot/+/47831/13/util/cbfstool/cbfstool.c@2... PS13, Line 231: with
within?
Done
Tim Wawrzynczak has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 14: Code-Review+1
Duncan Laurie has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 14: Code-Review+2
Furquan Shaikh has submitted this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
util/cbfstool: Introduce concept of mmap_window
This change adds the concept of mmap_window to describe how the SPI flash address space is mapped to host address space on x86 platforms. It gets rid of the assumption that the SPI flash address space is mapped only below the 4G boundary in host space. This is required in follow up changes to be able to add more decode windows for the SPI flash into the host address space.
Currently, a single mmap window is added i.e. the default x86 decode window of maximum 16MiB size living just below the 4G boundary. If the window is smaller than 16MiB, then it is mapped at the top of the host window.
BUG=b:171534504 TEST=Verified using abuild with timeless option for all coreboot boards that there is no change in the resultant coreboot.rom file.
Change-Id: I8dd3d1c922cc834c1e67f279ffce8fa438d8209c Signed-off-by: Furquan Shaikh furquan@google.com Reviewed-on: https://review.coreboot.org/c/coreboot/+/47831 Tested-by: build bot (Jenkins) no-reply@coreboot.org Reviewed-by: Tim Wawrzynczak twawrzynczak@chromium.org Reviewed-by: Duncan Laurie dlaurie@chromium.org --- M util/cbfstool/cbfstool.c 1 file changed, 140 insertions(+), 14 deletions(-)
Approvals: build bot (Jenkins): Verified Duncan Laurie: Looks good to me, approved Tim Wawrzynczak: Looks good to me, but someone else must approve
diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c index f9f4e53..eca9bcc 100644 --- a/util/cbfstool/cbfstool.c +++ b/util/cbfstool/cbfstool.c @@ -17,6 +17,7 @@ #include <commonlib/fsp.h> #include <commonlib/endian.h> #include <commonlib/helpers.h> +#include <commonlib/region.h> #include <vboot_host.h>
#define SECTION_WITH_FIT_TABLE "BOOTBLOCK" @@ -116,18 +117,145 @@ CBFS_FILE_MAGIC, strlen(CBFS_FILE_MAGIC)); }
-/* - * Converts between offsets from the start of the specified image region and - * "top-aligned" offsets from the top of the entire boot media. - */ -static unsigned convert_to_from_absolute_top_aligned( - const struct buffer *region, unsigned offset) +/* This describes a window from the SPI flash address space into the host address space. */ +struct mmap_window { + struct region flash_space; + struct region host_space; +}; + +enum mmap_window_type { + X86_DEFAULT_DECODE_WINDOW, /* Decode window just below 4G boundary */ + MMAP_MAX_WINDOWS, +}; + +/* Table of all the decode windows supported by the platform. */ +static struct mmap_window mmap_window_table[MMAP_MAX_WINDOWS]; + +static void add_mmap_window(enum mmap_window_type idx, size_t flash_offset, size_t host_offset, + size_t window_size) +{ + if (idx >= MMAP_MAX_WINDOWS) { + ERROR("Incorrect mmap window index(%d)\n", idx); + return; + } + + mmap_window_table[idx].flash_space.offset = flash_offset; + mmap_window_table[idx].host_space.offset = host_offset; + mmap_window_table[idx].flash_space.size = window_size; + mmap_window_table[idx].host_space.size = window_size; +} + +static void create_mmap_windows(void) +{ + static bool done; + + if (done) + return; + + const size_t image_size = partitioned_file_total_size(param.image_file); + const size_t window_size = MIN(16 * MiB, image_size); + + /* + * Default decode window lives just below 4G boundary in host space and maps up to a + * maximum of 16MiB. If the window is smaller than 16MiB, the SPI flash window is mapped + * at the top of the host window just below 4G. + */ + add_mmap_window(X86_DEFAULT_DECODE_WINDOW, image_size - window_size, + 4ULL * GiB - window_size, window_size); + + done = true; +} + +static unsigned int convert_address(const struct region *to, const struct region *from, + unsigned int addr) +{ + /* + * Calculate the offset in the "from" region and use that offset to calculate + * corresponding address in the "to" region. + */ + size_t offset = addr - region_offset(from); + return region_offset(to) + offset; +} + +enum mmap_addr_type { + HOST_SPACE_ADDR, + FLASH_SPACE_ADDR, +}; + +static int find_mmap_window(enum mmap_addr_type addr_type, unsigned int addr) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(mmap_window_table); i++) { + const struct region *reg; + + if (addr_type == HOST_SPACE_ADDR) + reg = &mmap_window_table[i].host_space; + else + reg = &mmap_window_table[i].flash_space; + + if (region_offset(reg) <= addr && region_end(reg) >= addr) + return i; + } + + return -1; +} + +static unsigned int convert_host_to_flash(const struct buffer *region, unsigned int addr) +{ + int idx; + const struct region *to, *from; + + idx = find_mmap_window(HOST_SPACE_ADDR, addr); + if (idx == -1) { + ERROR("Host address(%x) not in any mmap window!\n", addr); + return 0; + } + + to = &mmap_window_table[idx].flash_space; + from = &mmap_window_table[idx].host_space; + + /* region->offset is subtracted because caller expects offset in the given region. */ + return convert_address(to, from, addr) - region->offset; +} + +static unsigned int convert_flash_to_host(const struct buffer *region, unsigned int addr) +{ + int idx; + const struct region *to, *from; + + /* + * region->offset is added because caller provides offset in the given region. This is + * converted to an absolute address in the SPI flash space. This is done before the + * conversion as opposed to after in convert_host_to_flash() above because the address + * is actually an offset within the region. So, it needs to be converted into an + * absolute address in the SPI flash space before converting into an address in host + * space. + */ + addr += region->offset; + idx = find_mmap_window(FLASH_SPACE_ADDR, addr); + + if (idx == -1) { + ERROR("SPI flash address(%x) not in any mmap window!\n", addr); + return 0; + } + + to = &mmap_window_table[idx].host_space; + from = &mmap_window_table[idx].flash_space; + + return convert_address(to, from, addr); +} + +static unsigned int convert_addr_space(const struct buffer *region, unsigned int addr) { assert(region);
- size_t image_size = partitioned_file_total_size(param.image_file); + create_mmap_windows();
- return image_size - region->offset - offset; + if (IS_HOST_SPACE_ADDRESS(addr)) + return convert_host_to_flash(region, addr); + else + return convert_flash_to_host(region, addr); }
/* @@ -572,7 +700,8 @@ }
if (IS_HOST_SPACE_ADDRESS(offset)) - offset = convert_to_from_absolute_top_aligned(param.image_region, -offset); + offset = convert_addr_space(param.image_region, offset); + if (cbfs_add_entry(&image, &buffer, offset, header, len_align) != 0) { ERROR("Failed to add '%s' into ROM image.\n", filename); free(header); @@ -658,8 +787,7 @@ */ if (param.stage_xip) { if (!IS_HOST_SPACE_ADDRESS(address)) - address = -convert_to_from_absolute_top_aligned( - param.image_region, address); + address = convert_addr_space(param.image_region, address); } else { if (param.baseaddress_assigned == 0) { INFO("Honoring pre-linked FSP module.\n"); @@ -732,9 +860,7 @@ * x86 semantics about the boot media being directly mapped * below 4GiB in the CPU address space. **/ - address = -convert_to_from_absolute_top_aligned( - param.image_region, address); - *offset = address; + *offset = convert_addr_space(param.image_region, address);
ret = parse_elf_to_xip_stage(buffer, &output, offset, param.ignore_section);
Paul Menzel has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 16:
(1 comment)
Patchset:
PS16: This change broke booting on the ASRock E350M1. With this commit (and building with `make -j2`), the layout is changed, and serial console output (and boot) stops after bootblock.
``` $ more defconfig CONFIG_ANY_TOOLCHAIN=y CONFIG_VENDOR_ASROCK=y CONFIG_NO_POST=y CONFIG_VGA_BIOS=y CONFIG_VGA_BIOS_FILE="pci1002,9802.rom" CONFIG_BOARD_ASROCK_E350M1=y CONFIG_SEABIOS_MASTER=y ```
### runs
``` $ build/cbfstool build/coreboot.rom print FMAP REGION: COREBOOT Name Offset Type Size Comp cbfs master header 0x0 cbfs header 32 none fallback/romstage 0x80 stage 187284 none fallback/ramstage 0x2dc80 stage 119179 none config 0x4ae80 raw 234 none revision 0x4afc0 raw 675 none build_info 0x4b2c0 raw 95 none fallback/dsdt.aml 0x4b380 raw 9598 none cmos_layout.bin 0x4d940 cmos_layout 1228 none pci1002,9802.rom 0x4de40 optionrom 65536 none fallback/postcar 0x5de80 stage 17952 none fallback/payload 0x62500 simple elf 70483 none payload_config 0x738c0 raw 1621 none payload_revision 0x73f40 raw 233 none (empty) 0x74080 null 3652888 none s3nv 0x3efdc0 raw 8192 none (empty) 0x3f1e00 null 44312 none bootblock 0x3fcb40 bootblock 12912 none ```
### fails
``` $ /dev/shm/coreboot/build/cbfstool /dev/shm/coreboot.rom print FMAP REGION: COREBOOT Name Offset Type Size Comp cbfs master header 0x0 cbfs header 32 none s3nv 0x80 raw 8192 none fallback/romstage 0x20c0 stage 187284 none fallback/ramstage 0x2fcc0 stage 119179 none config 0x4cec0 raw 234 none revision 0x4d000 raw 675 none build_info 0x4d300 raw 95 none fallback/dsdt.aml 0x4d3c0 raw 9598 none cmos_layout.bin 0x4f980 cmos_layout 1228 none pci1002,9802.rom 0x4fe80 optionrom 65536 none fallback/postcar 0x5fec0 stage 17952 none fallback/payload 0x64540 simple elf 70495 none payload_config 0x75900 raw 1621 none payload_revision 0x75f80 raw 233 none (empty) 0x760c0 null 3697240 none bootblock 0x3fcb40 bootblock 12912 none ```
``` coreboot-4.13-1928-gad581984a6 Thu Feb 11 10:25:44 UTC 2021 bootblock starting (log level: 7)... FMAP: Found "FLASH" version 1.1 at 0x0. FMAP: base = 0xffc00000 size = 0x400000 #areas = 3 FMAP: area COREBOOT found @ 200 (4193792 bytes) CBFS: mcache @0x00034e00 built for 15 files, used 0x304 of 0x2000 bytes CBFS: Found 'fallback/romstage' @0x20c0 size 0x2db94 in mcache @0x00034e60 BS: bootblock times (exec / console): total (unknown) / 35 ms ```
Attention is currently required from: Furquan Shaikh. Nico Huber has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 16:
(1 comment)
File util/cbfstool/cbfstool.c:
https://review.coreboot.org/c/coreboot/+/47831/comment/9d9bc1d4_2b2dded7 PS16, Line 197: >= region_end() is non-inclusive (unrelated to Paul's issue)
Attention is currently required from: Furquan Shaikh. Paul Menzel has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 16:
(1 comment)
Patchset:
PS16: 1. Working (4.13-375-g19ba95f799-dirty): https://dpaste.org/hJ0A (`make V=1 -j1`) 2. Non-working (4.13-376-g0dcc0662f3-dirty): https://dpaste.org/osnA (`make V=1 -j1`)
Attention is currently required from: Furquan Shaikh. Paul Menzel has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 16:
(1 comment)
Patchset:
PS16: Kyösti found the error:
``` build/util/cbfstool/cbfstool build/coreboot.pre.tmp add-master-header printf " CBFS fallback/romstage\n" CBFS fallback/romstage build/util/cbfstool/cbfstool build/coreboot.pre.tmp add-stage -f build/cbfs/fallback/romstage.elf -n fallback/romstage -c none -r COREBOOT -a 64 -S ".car.data" --xip E: Host address(ffc002e4) not in any mmap window! printf " CBFS fallback/ramstage\n" CBFS fallback/ramstage ```
This is a 32-bit user space system. No idea, if it matters.
Attention is currently required from: Furquan Shaikh. Kyösti Mälkki has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 16:
(1 comment)
File util/cbfstool/cbfstool.c:
https://review.coreboot.org/c/coreboot/+/47831/comment/fee0ff57_ae0c611e PS16, Line 197: >=
region_end() is non-inclusive […]
static inline size_t region_end(const struct region *r) { return region_offset(r) + region_sz(r); }
Looks a lot like 4 GiB, or 0 for 32-bit user space.
Furquan Shaikh has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 16:
(2 comments)
Patchset:
PS16:
Kyösti found the error: […]
Sorry about the breakage, Paul. Does this help fix the problem:
```
diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c index 57c9e5117f..3329719a26 100644 --- a/util/cbfstool/cbfstool.c +++ b/util/cbfstool/cbfstool.c @@ -255,7 +255,8 @@ static int find_mmap_window(enum mmap_addr_type addr_type, unsigned int addr) else reg = &mmap_window_table[i].flash_space;
- if (region_offset(reg) <= addr && region_end(reg) >= addr) + if (region_offset(reg) <= addr && + ((uint64_t)region_offset(reg) + (uint64_t)region_sz(reg) - 1) >= addr) return i; } ```
File util/cbfstool/cbfstool.c:
https://review.coreboot.org/c/coreboot/+/47831/comment/98a8537f_4a7ad09e PS16, Line 197: >=
static inline size_t region_end(const struct region *r) […]
Yeah, both the points you pointed out are problems and need to be fixed:
* region_end() is non-inclusive * check doesn't work for 32-bit user space
Attention is currently required from: Furquan Shaikh. Paul Menzel has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 16:
(1 comment)
Patchset:
PS16:
Sorry about the breakage, Paul. Does this help fix the problem: […]
No worries, and thank you for the quick fix. Applying the diff, the payload is reached on the ASRock E350M1.
Attention is currently required from: Furquan Shaikh. Branden Waldner has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 16:
(1 comment)
Patchset:
PS16: I'm having a problem with this commit on my asus/p2b as well. Failed boot and no serial output - on default logging settings. I tried the patch in the comments, but I couldn't get it to work - maybe I copied it wrong or something? It looked correct to me though. Is there anything else I can do to help figure it out?
Attention is currently required from: Furquan Shaikh. Angel Pons has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 16:
(1 comment)
Patchset:
PS16:
I'm having a problem with this commit on my asus/p2b as well. […]
Are you sure it's this commit what breaks booting for you? i.e. if you revert it or checkout its parent, does the board start booting fine again?
IIRC bootblock console is disabled for these old boards. Maybe try to enable it, if space constraints allow it.
Attention is currently required from: Furquan Shaikh. Branden Waldner has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 16:
(1 comment)
Patchset:
PS16:
Are you sure it's this commit what breaks booting for you? i.e. […]
I ran a git bisect from November until now and this is what it ended up on. I also had the same error message during build: E: Host address(ffc002e4) not in any mmap window! This is a 32-bit system if that's relevant. I could probably make some space if I limited or removed the microcode updates. I'd have to dump the rom after hot swapping from clean build though, which I would prefer to avoid doing more then necessary. I'd like to get a zif adapter or rig a dual bios switch, but haven't worked on it yet.
Attention is currently required from: Furquan Shaikh. Kyösti Mälkki has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 16:
(1 comment)
Patchset:
PS16:
I ran a git bisect from November until now and this is what it ended up on. […]
Branden, try CB:50618 if you had trouble patching from the comment. Does the "Host address" line disappear still?
Attention is currently required from: Furquan Shaikh. Branden Waldner has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/47831 )
Change subject: util/cbfstool: Introduce concept of mmap_window ......................................................................
Patch Set 16:
(1 comment)
Patchset:
PS16:
Branden, try CB:50618 if you had trouble patching from the comment. […]
That seems to fix it and it boots correctly.