]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
bootm: Add a function to check overlap
authorSimon Glass <sjg@chromium.org>
Sat, 18 Nov 2023 21:05:17 +0000 (14:05 -0700)
committerTom Rini <trini@konsulko.com>
Wed, 13 Dec 2023 16:51:24 +0000 (11:51 -0500)
Move this code into a function to reduce code size and make it easier
to understand. Drop the unnecessary 0x to help a little with code size.

Use this in bootm_find_images()

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>
boot/bootm.c

index fe6e84601f58f00698493f27b9b5a10d73503981..371f404a02a5d2a9b02ae88936dcd54c8302a8a9 100644 (file)
@@ -468,6 +468,37 @@ static int bootm_find_os(const char *cmd_name, const char *addr_fit)
        return 0;
 }
 
+/**
+ * check_overlap() - Check if an image overlaps the OS
+ *
+ * @name: Name of image to check (used to print error)
+ * @base: Base address of image
+ * @end: End address of image (+1)
+ * @os_start: Start of OS
+ * @os_size: Size of OS in bytes
+ * Return: 0 if OK, -EXDEV if the image overlaps the OS
+ */
+static int check_overlap(const char *name, ulong base, ulong end,
+                        ulong os_start, ulong os_size)
+{
+       ulong os_end;
+
+       if (!base)
+               return 0;
+       os_end = os_start + os_size;
+
+       if ((base >= os_start && base < os_end) ||
+           (end > os_start && end <= os_end) ||
+           (base < os_start && end >= os_end)) {
+               printf("ERROR: %s image overlaps OS image (OS=%lx..%lx)\n",
+                      name, os_start, os_end);
+
+               return -EXDEV;
+       }
+
+       return 0;
+}
+
 int bootm_find_images(ulong img_addr, const char *conf_ramdisk,
                      const char *conf_fdt, ulong start, ulong size)
 {
@@ -497,16 +528,8 @@ int bootm_find_images(ulong img_addr, const char *conf_ramdisk,
        }
 
        /* check if ramdisk overlaps OS image */
-       if (images.rd_start && (((ulong)images.rd_start >= start &&
-                                (ulong)images.rd_start < start + size) ||
-                               ((ulong)images.rd_end > start &&
-                                (ulong)images.rd_end <= start + size) ||
-                               ((ulong)images.rd_start < start &&
-                                (ulong)images.rd_end >= start + size))) {
-               printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
-                      start, start + size);
+       if (check_overlap("RD", images.rd_start, images.rd_end, start, size))
                return 1;
-       }
 
        if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
                buf = map_sysmem(img_addr, 0);
@@ -520,15 +543,9 @@ int bootm_find_images(ulong img_addr, const char *conf_ramdisk,
                }
 
                /* check if FDT overlaps OS image */
-               if (images.ft_addr &&
-                   (((ulong)images.ft_addr >= start &&
-                     (ulong)images.ft_addr < start + size) ||
-                    ((ulong)images.ft_addr + images.ft_len >= start &&
-                     (ulong)images.ft_addr + images.ft_len < start + size))) {
-                       printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
-                              start, start + size);
+               if (check_overlap("FDT", map_to_sysmem(images.ft_addr),
+                                 images.ft_len, start, size))
                        return 1;
-               }
 
                if (IS_ENABLED(CONFIG_CMD_FDT))
                        set_working_fdt_addr(map_to_sysmem(images.ft_addr));