[PATCH 04/13] boot: Move type and OS checking into a new function
Simon Glass
sjg at chromium.org
Wed Mar 25 17:54:13 CET 2026
Move the code which checks whether the image can be loaded into a
separate check_allowed() function.
Signed-off-by: Simon Glass <sjg at chromium.org>
---
boot/image-fit.c | 98 ++++++++++++++++++++++++++++++------------------
include/image.h | 4 +-
2 files changed, 63 insertions(+), 39 deletions(-)
diff --git a/boot/image-fit.c b/boot/image-fit.c
index 5680cc8b496..3a6120e9151 100644
--- a/boot/image-fit.c
+++ b/boot/image-fit.c
@@ -2177,9 +2177,64 @@ static int select_image(const void *fit, struct bootm_headers *images,
return noffset;
}
+/**
+ * check_allowed() - Check if an image is allowed to be loaded
+ *
+ * @fit: FIT to check
+ * @noffset: Node offset of the image being loaded
+ * @image_type: Type of the image
+ * @arch: Expected architecture for the image
+ * @bootstage_id: ID of starting bootstage to use for progress updates
+ * Return: 0 if OK, -EIO if not
+ */
+static int check_allowed(const void *fit, int noffset,
+ enum image_type_t image_type, enum image_arch_t arch,
+ int bootstage_id)
+{
+ bool type_ok, os_ok;
+ uint8_t os;
+
+ bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
+ type_ok = fit_image_check_type(fit, noffset, image_type) ||
+ fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
+ fit_image_check_type(fit, noffset, IH_TYPE_TEE) ||
+ fit_image_check_type(fit, noffset, IH_TYPE_TFA_BL31) ||
+ (image_type == IH_TYPE_KERNEL &&
+ fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
+
+ os_ok = image_type == IH_TYPE_FLATDT ||
+ image_type == IH_TYPE_FPGA ||
+ fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
+ fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
+ fit_image_check_os(fit, noffset, IH_OS_TEE) ||
+ fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
+ fit_image_check_os(fit, noffset, IH_OS_EFI) ||
+ fit_image_check_os(fit, noffset, IH_OS_VXWORKS) ||
+ fit_image_check_os(fit, noffset, IH_OS_ELF);
+
+ /*
+ * If either of the checks fail, we should report an error, but
+ * if the image type is coming from the "loadables" field, we
+ * don't care what it is
+ */
+ if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
+ fit_image_get_os(fit, noffset, &os);
+ printf("No %s %s %s Image\n",
+ genimg_get_os_name(os),
+ genimg_get_arch_name(arch),
+ genimg_get_type_name(image_type));
+ bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
+ return -EIO;
+ }
+
+ bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
+
+ return 0;
+}
+
int fit_image_load(struct bootm_headers *images, ulong addr,
const char **fit_unamep, const char **fit_uname_configp,
- int arch, int ph_type, int bootstage_id,
+ enum image_arch_t arch, int ph_type, int bootstage_id,
enum fit_load_op load_op, ulong *datap, ulong *lenp)
{
int image_type = image_ph_type(ph_type);
@@ -2191,10 +2246,10 @@ int fit_image_load(struct bootm_headers *images, ulong addr,
void *buf;
void *loadbuf;
size_t size;
- int type_ok, os_ok;
ulong load, load_end, data, len;
- uint8_t os, comp, os_arch;
+ uint8_t comp, os_arch;
const char *prop_name;
+ int ret;
fit = map_sysmem(addr, 0);
prop_name = fit_get_image_type_property(ph_type);
@@ -2221,40 +2276,9 @@ int fit_image_load(struct bootm_headers *images, ulong addr,
fit_image_get_arch(fit, noffset, &os_arch);
images_set_arch(images, os_arch);
- bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
- type_ok = fit_image_check_type(fit, noffset, image_type) ||
- fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
- fit_image_check_type(fit, noffset, IH_TYPE_TEE) ||
- fit_image_check_type(fit, noffset, IH_TYPE_TFA_BL31) ||
- (image_type == IH_TYPE_KERNEL &&
- fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
-
- os_ok = image_type == IH_TYPE_FLATDT ||
- image_type == IH_TYPE_FPGA ||
- fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
- fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
- fit_image_check_os(fit, noffset, IH_OS_TEE) ||
- fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
- fit_image_check_os(fit, noffset, IH_OS_EFI) ||
- fit_image_check_os(fit, noffset, IH_OS_VXWORKS) ||
- fit_image_check_os(fit, noffset, IH_OS_ELF);
-
- /*
- * If either of the checks fail, we should report an error, but
- * if the image type is coming from the "loadables" field, we
- * don't care what it is
- */
- if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
- fit_image_get_os(fit, noffset, &os);
- printf("No %s %s %s Image\n",
- genimg_get_os_name(os),
- genimg_get_arch_name(arch),
- genimg_get_type_name(image_type));
- bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
- return -EIO;
- }
-
- bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
+ ret = check_allowed(fit, noffset, image_type, arch, bootstage_id);
+ if (ret)
+ return ret;
/* get image data address and length */
if (fit_image_get_data(fit, noffset, (const void **)&buf, &size)) {
diff --git a/include/image.h b/include/image.h
index 5e11dc92559..726d82deb22 100644
--- a/include/image.h
+++ b/include/image.h
@@ -111,7 +111,7 @@ enum {
* New IDs *MUST* be appended at the end of the list and *NEVER*
* inserted for backward compatibility.
*/
-enum {
+enum image_arch_t {
IH_ARCH_INVALID = 0, /* Invalid CPU */
IH_ARCH_ALPHA, /* Alpha */
IH_ARCH_ARM, /* ARM */
@@ -760,7 +760,7 @@ int boot_get_fdt_fit(struct bootm_headers *images, ulong addr,
*/
int fit_image_load(struct bootm_headers *images, ulong addr,
const char **fit_unamep, const char **fit_uname_configp,
- int arch, int image_ph_type, int bootstage_id,
+ enum image_arch_t arch, int image_ph_type, int bootstage_id,
enum fit_load_op load_op, ulong *datap, ulong *lenp);
/**
--
2.43.0
More information about the U-Boot
mailing list