All what's done by bootm_has_initrd can be folded into bootm_load_initrd by having it return a special value (NULL) to indicate that no initrd is available. The advantage of doing it in one function is that it simplifies overriding the initrd in a follow-up commit Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx> --- arch/arm/lib32/bootm.c | 18 ++++++++---------- arch/kvx/lib/bootm.c | 23 +++++++++++------------ common/booti.c | 10 +++++----- common/bootm.c | 35 ++++++++++++++--------------------- include/bootm.h | 4 ++-- 5 files changed, 40 insertions(+), 50 deletions(-) diff --git a/arch/arm/lib32/bootm.c b/arch/arm/lib32/bootm.c index aeb873a3a723..d3a4d99a5828 100644 --- a/arch/arm/lib32/bootm.c +++ b/arch/arm/lib32/bootm.c @@ -241,6 +241,7 @@ static int __do_bootm_linux(struct image_data *data, unsigned long free_mem, { unsigned long kernel; unsigned long initrd_start = 0, initrd_size = 0, initrd_end = 0; + const struct resource *initrd_res; void *tee; enum arm_security_state state = bootm_arm_security_state(); void *fdt_load_address = NULL; @@ -259,16 +260,13 @@ static int __do_bootm_linux(struct image_data *data, unsigned long free_mem, } } - if (bootm_has_initrd(data)) { - ret = bootm_load_initrd(data, initrd_start); - if (ret) - return ret; - } - - if (data->initrd_res) { - initrd_start = data->initrd_res->start; - initrd_end = data->initrd_res->end; - initrd_size = resource_size(data->initrd_res); + initrd_res = bootm_load_initrd(data, initrd_start); + if (IS_ERR(initrd_res)) { + return PTR_ERR(initrd_res); + } else if (initrd_res) { + initrd_start = initrd_res->start; + initrd_end = initrd_res->end; + initrd_size = resource_size(initrd_res); free_mem = PAGE_ALIGN(initrd_end + 1); } diff --git a/arch/kvx/lib/bootm.c b/arch/kvx/lib/bootm.c index 4c77f676ec0b..6c35b68f3d5d 100644 --- a/arch/kvx/lib/bootm.c +++ b/arch/kvx/lib/bootm.c @@ -54,6 +54,7 @@ static int do_boot_entry(struct image_data *data, boot_func_entry entry, static int do_boot_elf(struct image_data *data, struct elf_image *elf) { + const struct resource *initrd_res; int ret; void *fdt; boot_func_entry entry; @@ -61,21 +62,19 @@ static int do_boot_elf(struct image_data *data, struct elf_image *elf) /* load initrd after the elf */ load_addr = PAGE_ALIGN((unsigned long) elf->high_addr); - if (bootm_has_initrd(data)) { - if (data->initrd_address != UIMAGE_INVALID_ADDRESS) - initrd_address = data->initrd_address; - else - initrd_address = load_addr; + if (data->initrd_address != UIMAGE_INVALID_ADDRESS) + initrd_address = data->initrd_address; + else + initrd_address = load_addr; + initrd_res = bootm_load_initrd(data, initrd_address); + if (IS_ERR(initrd_res)) { + printf("Failed to load initrd\n"); + return ret; + } else if (initrd_res) { printf("Loading initrd at 0x%lx\n", initrd_address); - ret = bootm_load_initrd(data, initrd_address); - if (ret) { - printf("Failed to load initrd\n"); - return ret; - } - if (data->initrd_address == UIMAGE_INVALID_ADDRESS) { - load_addr += resource_size(data->initrd_res); + load_addr += resource_size(initrd_res); load_addr = PAGE_ALIGN(load_addr); } } diff --git a/common/booti.c b/common/booti.c index e745ff696376..5efd4f97bbc2 100644 --- a/common/booti.c +++ b/common/booti.c @@ -58,12 +58,12 @@ void *booti_load_image(struct image_data *data, phys_addr_t *oftree) if (oftree) { unsigned long devicetree; + const struct resource *initrd_res; - if (bootm_has_initrd(data)) { - ret = bootm_load_initrd(data, image_end); - if (ret) - return ERR_PTR(ret); - + initrd_res = bootm_load_initrd(data, image_end); + if (IS_ERR(initrd_res)) { + return ERR_CAST(initrd_res); + } else if (initrd_res) { image_end += resource_size(data->initrd_res); image_end = PAGE_ALIGN(image_end); } diff --git a/common/bootm.c b/common/bootm.c index d24b18b41cb2..d2373f321c19 100644 --- a/common/bootm.c +++ b/common/bootm.c @@ -213,14 +213,6 @@ static bool fitconfig_has_ramdisk(struct image_data *data) return fit_has_image(data->os_fit, data->fit_config, "ramdisk"); } -bool bootm_has_initrd(struct image_data *data) -{ - if (!IS_ENABLED(CONFIG_BOOTM_INITRD)) - return false; - - return fitconfig_has_ramdisk(data) || data->initrd_file; -} - static int bootm_open_initrd_uimage(struct image_data *data) { int ret; @@ -262,19 +254,17 @@ static int bootm_open_initrd_uimage(struct image_data *data) * * Return: 0 on success, negative error code otherwise */ -int bootm_load_initrd(struct image_data *data, unsigned long load_address) +const struct resource * +bootm_load_initrd(struct image_data *data, unsigned long load_address) { enum filetype type; int ret; if (!IS_ENABLED(CONFIG_BOOTM_INITRD)) - return -ENOSYS; - - if (!bootm_has_initrd(data)) - return -EINVAL; + return NULL; if (data->initrd_res) - return 0; + return data->initrd_res; if (fitconfig_has_ramdisk(data)) { const void *initrd; @@ -285,7 +275,7 @@ int bootm_load_initrd(struct image_data *data, unsigned long load_address) if (ret) { pr_err("Cannot open ramdisk image in FIT image: %s\n", strerror(-ret)); - return ret; + return ERR_PTR(ret); } data->initrd_res = request_sdram_region("initrd", load_address, @@ -295,17 +285,20 @@ int bootm_load_initrd(struct image_data *data, unsigned long load_address) " 0x%08llx-0x%08llx\n", (unsigned long long)load_address, (unsigned long long)load_address + initrd_size - 1); - return -ENOMEM; + return ERR_PTR(-ENOMEM); } memcpy((void *)load_address, initrd, initrd_size); pr_info("Loaded initrd from FIT image\n"); goto done1; } + if (!data->initrd_file) + return NULL; + ret = file_name_detect_type(data->initrd_file, &type); if (ret) { pr_err("could not open initrd \"%s\": %s\n", data->initrd_file, strerror(-ret)); - return ret; + return ERR_PTR(ret); } if (type == filetype_uimage) { @@ -314,7 +307,7 @@ int bootm_load_initrd(struct image_data *data, unsigned long load_address) if (ret) { pr_err("loading initrd failed with %s\n", strerror(-ret)); - return ret; + return ERR_PTR(ret); } num = uimage_part_num(data->initrd_part); @@ -322,14 +315,14 @@ int bootm_load_initrd(struct image_data *data, unsigned long load_address) data->initrd_res = uimage_load_to_sdram(data->initrd, num, load_address); if (!data->initrd_res) - return -ENOMEM; + return ERR_PTR(-ENOMEM); goto done; } data->initrd_res = file_to_sdram(data->initrd_file, load_address); if (!data->initrd_res) - return -ENOMEM; + return ERR_PTR(-ENOMEM); done: @@ -343,7 +336,7 @@ int bootm_load_initrd(struct image_data *data, unsigned long load_address) &data->initrd_res->start, &data->initrd_res->end); - return 0; + return data->initrd_res; } static int bootm_open_oftree_uimage(struct image_data *data, size_t *size, diff --git a/include/bootm.h b/include/bootm.h index fdec137771f0..b86d06b0f55d 100644 --- a/include/bootm.h +++ b/include/bootm.h @@ -142,8 +142,8 @@ void bootm_data_restore_defaults(const struct bootm_data *data); int bootm_load_os(struct image_data *data, unsigned long load_address); -bool bootm_has_initrd(struct image_data *data); -int bootm_load_initrd(struct image_data *data, unsigned long load_address); +const struct resource * +bootm_load_initrd(struct image_data *data, unsigned long load_address); void *bootm_get_devicetree(struct image_data *data); int bootm_load_devicetree(struct image_data *data, void *fdt, -- 2.39.5