Re: [v2 PATCH] RISC-V: Add a PE/COFF compliant Image header.

From: Atish Patra
Date: Mon May 13 2019 - 20:36:05 EST


On 5/13/19 5:09 PM, Paul Walmsley wrote:
On Mon, 13 May 2019, Atish Patra wrote:

On 5/13/19 3:31 PM, Paul Walmsley wrote:
On Wed, 1 May 2019, Atish Patra wrote:

Currently, last stage boot loaders such as U-Boot can accept only
uImage which is an unnecessary additional step in automating boot flows.

Add a PE/COFF compliant image header that boot loaders can parse and
directly load kernel flat Image. The existing booting methods will
continue
to work as it is.

Another goal of this header is to support EFI stub for RISC-V in future.
EFI specification needs PE/COFF image header in the beginning of the
kernel
image in order to load it as an EFI application. In order to support
EFI stub, code0 should be replaced with "MZ" magic string and res5(at
offset 0x3c) should point to the rest of the PE/COFF header (which will
be added during EFI support).

Tested on both QEMU and HiFive Unleashed using OpenSBI + U-Boot + Linux.

Signed-off-by: Atish Patra <atish.patra@xxxxxxx>

Seems like we're stuck with this basic format for EFI, etc. Even though
we may be stuck with it, I think we should take the opportunity to add the
possibility to extending this header format by adding fields after the
basic PE/COFF header ends.

In particular, at the very least, I'd suggest adding a u32 after the
PE/COFF header ends, to represent a "RISC-V header format version number".
Then if we add more fields that follow the PE/COFF header -- for example,
to represent the RISC-V instruction set extensions that are required to
run this binary -- we can just bump that RISC-V-specific version number
field to indicate to bootloaders that there's more there.

That would be inventing our RISC-V specific header format. However, we
can always use the one of the reserved fields in proposed header (res4)
for this purpose.

What are the semantics of those reserved fields?


+struct riscv_image_header {
+ u32 code0;
+ u32 code1;
+ u64 text_offset;
+ u64 image_size;
+ u64 res1;
+ u64 res2;
+ u64 res3;
+ u64 magic;
+ u32 res4; ---> We can use this for versioning when required
+ u32 res5; ---> This is reserved for PE/COFF header
+};

Do we need to add it now or add it later when we actually need a version
number. My preference is to add it later based on requirement.

If it isn't added now, how would bootloaders know whether it was there or
not?


Here is the corresponding U-Boot Patch
https://patchwork.ozlabs.org/patch/1096087/

Currently, boot loader doesn't care about versioning. Since we are updating a reserved field, offsets will not change. If a boot loader want to use the versioning, it should be patched along with the kernel patch.

Any other boot loader that doesn't care about the version, it can continue to do so without any change.

My idea is to enable the minimum required fields in this patch and keep everything else as reserved so that it can be amended in future as required.

Regards,
Atish

- Paul