Having this function callable on its own will come in handy once we add
boot.image override support as that would need to recompute the file
type.

Signed-off-by: Ahmad Fatoum <[email protected]>
---
 common/bootm.c | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/common/bootm.c b/common/bootm.c
index 7fe0afe24ce8..9e302da36ca6 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -543,6 +543,25 @@ static int bootm_image_name_and_part(const char *name, 
char **filename, char **p
        return 0;
 }
 
+static int file_read_and_detect_boot_image_type(const char *os_file, void 
**os_header)
+{
+       size_t size;
+       int ret;
+
+       ret = read_file_2(os_file, &size, os_header, PAGE_SIZE);
+       if (ret < 0 && ret != -EFBIG) {
+               pr_err("could not open %s: %pe\n", os_file, ERR_PTR(ret));
+               return ret;
+       }
+       if (size < PAGE_SIZE) {
+               free(*os_header);
+               *os_header = NULL;
+               return -ENODATA;
+       }
+
+       return file_detect_boot_image_type(*os_header, PAGE_SIZE);
+}
+
 /*
  * bootm_boot - Boot an application image described by bootm_data
  */
@@ -552,7 +571,6 @@ int bootm_boot(struct bootm_data *bootm_data)
        struct image_data *data;
        struct image_handler *handler;
        int ret;
-       size_t size;
        const char *image_type_str;
 
        if (!bootm_data->os_file) {
@@ -577,16 +595,10 @@ int bootm_boot(struct bootm_data *bootm_data)
        data->efi_boot = bootm_data->efi_boot;
 
        os_file = xstrdup(data->os_file);
-
-       ret = read_file_2(os_file, &size, &data->os_header, PAGE_SIZE);
-       if (ret < 0 && ret != -EFBIG) {
-               pr_err("could not open %s: %pe\n", os_file, ERR_PTR(ret));
+       ret = file_read_and_detect_boot_image_type(os_file, &data->os_header);
+       if (ret < 0)
                goto err_out;
-       }
-       if (size < PAGE_SIZE)
-               goto err_out;
-
-       data->image_type = file_detect_boot_image_type(data->os_header, 
PAGE_SIZE);
+       data->image_type = ret;
 
        if (!data->force && data->image_type == filetype_unknown) {
                pr_err("Unknown OS filetype (try -f)\n");
-- 
2.47.3


Reply via email to