[PATCH v3 1/2] efi_loader: bootbin: do not load an initrd if none is provided
Heinrich Schuchardt
xypron.glpk at gmx.de
Wed Apr 30 09:03:58 CEST 2025
On 4/29/25 15:27, Adriano Cordova wrote:
> Do not try to create an initrd device path nor try to register
> an initrd with the EFI_LOAD_FILE2_PROTOCOL if none is provided.
>
> Handle initrd installation in efi_binary_run_dp with
> efi_install_initrd, imitating what is done for the fdt.
>
> Fixes: 36835a9105c ("efi_loader: binary_run: register an initrd")
> Reported-by: Weizhao Ouyang <o451686892 at gmail.com>
> Signed-off-by: Adriano Cordova <adriano.cordova at canonical.com>
> Tested-by: Weizhao Ouyang <o451686892 at gmail.com>
I built and installed U-Boot for the StarFive VisionFive 2 based on
origin/master plus this series.
I created a FIT images using the its file listed below.
I loaded and tried to run the FIT image:
StarFive # load nvme 0:1 $kernel_addr_r initrd.itb
StarFive # bootm $kernel_addr_r
and saw the following failure:
## Transferring control to EFI (at address 00000000) ...
Card did not respond to voltage select! : -110
Booting /initrd.itb
Not a PE-COFF file
Loading image failed
We use the entry point instead of the load address when transferring
control to the EFI sub-system.
This bug was introduced by ecc7fdaa9ef1 ("bootm: Add a bootm command for
type IH_OS_EFI") and is not related to your patches but we still need to
fix it.
Afterwards I tried to boot into Ubuntu and got:
Loading Linux 6.14.0-13-generic ...
Loading initial ramdisk ...
error: failed to install protocols.
Loading device tree blob...
Press any key to continue...
It seems that U-Boot did not uninstall the LoadFile2 protocol when
discovering that the EFI image was invalid. We should have a look at the
error handling.
The initrd for my test case can be created with:
echo 'hello world' > hello
echo hello | cpio -o -H newc > initrd
Best regards
Heinrich
/dts-v1/;
/ {
description = "EFI FIT";
#address-cells = <1>;
images {
initrddump {
description = "initrddump";
data = /incbin/("lib/efi_loader/initrddump.efi");
type = "kernel";
arch = "riscv";
os = "efi";
compression = "none";
load = <0x58000000>;
entry = <0x0>;
};
initrd {
description = "initrd";
data = /incbin/("initrd");
type = "ramdisk";
compression = "none";
arch = "riscv";
os = "efi";
load = <0x60000000>;
};
fdt {
description = "vf2";
data =
/incbin/("dts/upstream/src/riscv/starfive/jh7110-starfive-visionfive-2-v1.3b.dtb");
type = "flat_dt";
arch = "riscv";
compression = "none";
load = <0x68000000>;
};
};
configurations {
default = "config-initrddump";
config-initrddump {
description = "initrddump";
kernel = "initrddump";
ramdisk = "initrd";
fdt = "fdt";
};
};
};
> ---
>
> Changes in v3:
> - Do not clear images->initrd_start in boot/bootm.c, as this is
> done in bootm_start().
>
> Changes in v2:
> - Free initrd_dp
>
> include/efi_loader.h | 2 ++
> lib/efi_loader/efi_bootbin.c | 7 +------
> lib/efi_loader/efi_helper.c | 29 +++++++++++++++++++++++++++++
> 3 files changed, 32 insertions(+), 6 deletions(-)
>
> diff --git a/include/efi_loader.h b/include/efi_loader.h
> index 144b749278a..84e8cfe320e 100644
> --- a/include/efi_loader.h
> +++ b/include/efi_loader.h
> @@ -597,6 +597,8 @@ efi_status_t efi_env_set_load_options(efi_handle_t handle, const char *env_var,
> void *efi_get_configuration_table(const efi_guid_t *guid);
> /* Install device tree */
> efi_status_t efi_install_fdt(void *fdt);
> +/* Install initrd */
> +efi_status_t efi_install_initrd(void *initrd, size_t initd_sz);
> /* Execute loaded UEFI image */
> efi_status_t do_bootefi_exec(efi_handle_t handle, void *load_options);
> /* Run loaded UEFI image with given fdt */
> diff --git a/lib/efi_loader/efi_bootbin.c b/lib/efi_loader/efi_bootbin.c
> index d0f7da309ce..6a189c31ffa 100644
> --- a/lib/efi_loader/efi_bootbin.c
> +++ b/lib/efi_loader/efi_bootbin.c
> @@ -220,7 +220,6 @@ static efi_status_t efi_binary_run_dp(void *image, size_t size, void *fdt,
> struct efi_device_path *dp_img)
> {
> efi_status_t ret;
> - struct efi_device_path *dp_initrd;
>
> /* Initialize EFI drivers */
> ret = efi_init_obj_list();
> @@ -234,11 +233,7 @@ static efi_status_t efi_binary_run_dp(void *image, size_t size, void *fdt,
> if (ret != EFI_SUCCESS)
> return ret;
>
> - dp_initrd = efi_dp_from_mem(EFI_LOADER_DATA, (uintptr_t)initrd, initd_sz);
> - if (!dp_initrd)
> - return EFI_OUT_OF_RESOURCES;
> -
> - ret = efi_initrd_register(dp_initrd);
> + ret = efi_install_initrd(initrd, initd_sz);
> if (ret != EFI_SUCCESS)
> return ret;
>
> diff --git a/lib/efi_loader/efi_helper.c b/lib/efi_loader/efi_helper.c
> index 3936139ca41..19fb5d03fec 100644
> --- a/lib/efi_loader/efi_helper.c
> +++ b/lib/efi_loader/efi_helper.c
> @@ -622,6 +622,35 @@ efi_status_t efi_install_fdt(void *fdt)
> return EFI_SUCCESS;
> }
>
> +/**
> + * efi_install_initrd() - install initrd
> + *
> + * Install the initrd located at @initrd using the EFI_LOAD_FILE2
> + * protocol.
> + *
> + * @initrd: address of initrd or NULL if none is provided
> + * @initrd_sz: size of initrd
> + * Return: status code
> + */
> +efi_status_t efi_install_initrd(void *initrd, size_t initd_sz)
> +{
> + efi_status_t ret;
> + struct efi_device_path *dp_initrd;
> +
> + if (!initrd)
> + return EFI_SUCCESS;
> +
> + dp_initrd = efi_dp_from_mem(EFI_LOADER_DATA, (uintptr_t)initrd, initd_sz);
> + if (!dp_initrd)
> + return EFI_OUT_OF_RESOURCES;
> +
> + ret = efi_initrd_register(dp_initrd);
> + if (ret != EFI_SUCCESS)
> + efi_free_pool(dp_initrd);
> +
> + return ret;
> +}
> +
> /**
> * do_bootefi_exec() - execute EFI binary
> *
More information about the U-Boot
mailing list