bootutil: Add missing images to get maximum image size details

Adds support for getting the maximum image size of multiple images
and adding this data to the shared data area

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
diff --git a/boot/bootutil/include/bootutil/boot_record.h b/boot/bootutil/include/bootutil/boot_record.h
index fd77e35..09bcc2a 100644
--- a/boot/bootutil/include/bootutil/boot_record.h
+++ b/boot/bootutil/include/bootutil/boot_record.h
@@ -72,14 +72,14 @@
  * @param[in]  hdr           Pointer to the image header stored in RAM.
  * @param[in]  fap           Pointer to the flash area where image is stored.
  * @param[in]  slot          The currently active slot being booted.
- * @param[in]  max_app_size  The maximum size of an image that can be loaded.
+ * @param[in]  max_app_sizes The maximum sizes of images that can be loaded.
  *
  * @return                    0 on success; nonzero on failure.
  */
 int boot_save_shared_data(const struct image_header *hdr,
                           const struct flash_area *fap,
                           const uint8_t active_slot,
-                          const int max_app_size);
+                          const struct image_max_size *max_app_sizes);
 
 #ifdef __cplusplus
 }
diff --git a/boot/bootutil/include/bootutil/boot_status.h b/boot/bootutil/include/bootutil/boot_status.h
index 8ec0619..ea3de5e 100644
--- a/boot/bootutil/include/bootutil/boot_status.h
+++ b/boot/bootutil/include/bootutil/boot_status.h
@@ -113,7 +113,12 @@
 #define BLINFO_RECOVERY             0x02
 #define BLINFO_RUNNING_SLOT         0x03
 #define BLINFO_BOOTLOADER_VERSION   0x04
-#define BLINFO_MAX_APPLICATION_SIZE 0x05
+#define BLINFO_MAX_APPLICATION_SIZE 0x05 /* Same as BLINFO_MAX_APPLICATION_SIZE_IMAGE_0, legacy name */
+#define BLINFO_MAX_APPLICATION_SIZE_IMAGE_0 BLINFO_MAX_APPLICATION_SIZE
+#define BLINFO_MAX_APPLICATION_SIZE_IMAGE_1 0x06
+#define BLINFO_MAX_APPLICATION_SIZE_IMAGE_2 0x07
+#define BLINFO_MAX_APPLICATION_SIZE_IMAGE_3 0x08
+#define BLINFO_MAX_APPLICATION_SIZE_IMAGE_4 0x09
 
 enum mcuboot_mode {
     MCUBOOT_MODE_SINGLE_SLOT,
diff --git a/boot/bootutil/include/bootutil/bootutil.h b/boot/bootutil/include/bootutil/bootutil.h
index 9ad962e..070adaf 100644
--- a/boot/bootutil/include/bootutil/bootutil.h
+++ b/boot/bootutil/include/bootutil/bootutil.h
@@ -77,6 +77,11 @@
     uint8_t magic[BOOT_MAGIC_SZ];
 };
 
+struct image_max_size {
+	bool calculated;
+	uint32_t max_size;
+};
+
 /* you must have pre-allocated all the entries within this structure */
 fih_ret boot_go(struct boot_rsp *rsp);
 fih_ret boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id);
@@ -84,6 +89,7 @@
 struct boot_loader_state;
 void boot_state_clear(struct boot_loader_state *state);
 fih_ret context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp);
+const struct image_max_size *boot_get_max_app_size(void);
 
 #define SPLIT_GO_OK                 (0)
 #define SPLIT_GO_NON_MATCHING       (-1)
diff --git a/boot/bootutil/src/boot_record.c b/boot/bootutil/src/boot_record.c
index 64a36d7..ccd9e6e 100644
--- a/boot/bootutil/src/boot_record.c
+++ b/boot/bootutil/src/boot_record.c
@@ -230,9 +230,10 @@
 
 #ifdef MCUBOOT_DATA_SHARING_BOOTINFO
 int boot_save_shared_data(const struct image_header *hdr, const struct flash_area *fap,
-                          const uint8_t slot, const int max_app_size)
+                          const uint8_t slot, const struct image_max_size *max_app_sizes)
 {
     int rc;
+    uint8_t image = 0;
 
 #if defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
     uint8_t mode = MCUBOOT_MODE_SINGLE_SLOT;
@@ -325,11 +326,16 @@
     }
 #endif
 
-    if (!rc) {
-        rc = boot_add_data_to_shared_area(TLV_MAJOR_BLINFO,
-                                          BLINFO_MAX_APPLICATION_SIZE,
-                                          sizeof(max_app_size),
-                                          (void *)&max_app_size);
+    while (image < BOOT_IMAGE_NUMBER && !rc) {
+        if (max_app_sizes[image].calculated == true) {
+            rc = boot_add_data_to_shared_area(TLV_MAJOR_BLINFO,
+                                              (BLINFO_MAX_APPLICATION_SIZE + image),
+                                              sizeof(max_app_sizes[image].max_size),
+                                              (void *)&max_app_sizes[image].max_size);
+
+        }
+
+        ++image;
     }
 
     return rc;
diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c
index 80be91f..4b0299f 100644
--- a/boot/bootutil/src/loader.c
+++ b/boot/bootutil/src/loader.c
@@ -63,6 +63,10 @@
 
 static struct boot_loader_state boot_data;
 
+#if defined(MCUBOOT_DATA_SHARING)
+static struct image_max_size image_max_sizes[BOOT_IMAGE_NUMBER] = {0};
+#endif
+
 #if (BOOT_IMAGE_NUMBER > 1)
 #define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
 #else
@@ -137,10 +141,6 @@
 #if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
     int rc;
 
-#ifdef MCUBOOT_DATA_SHARING
-    int max_app_size;
-#endif
-
 #ifdef MCUBOOT_MEASURED_BOOT
     rc = boot_save_boot_status(BOOT_CURR_IMG(state),
                                 boot_img_hdr(state, active_slot),
@@ -152,10 +152,9 @@
 #endif /* MCUBOOT_MEASURED_BOOT */
 
 #ifdef MCUBOOT_DATA_SHARING
-    max_app_size = app_max_size(state);
     rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
                                 BOOT_IMG_AREA(state, active_slot),
-                                active_slot, max_app_size);
+                                active_slot, image_max_sizes);
     if (rc != 0) {
         BOOT_LOG_ERR("Failed to add data to shared memory area.");
         return rc;
@@ -1841,6 +1840,10 @@
     int rc;
     FIH_DECLARE(fih_rc, FIH_FAILURE);
 
+#if defined(MCUBOOT_DATA_SHARING)
+    int max_size;
+#endif
+
     /* Determine the sector layout of the image slots and scratch area. */
     rc = boot_read_sectors(state);
     if (rc != 0) {
@@ -1867,6 +1870,16 @@
         return;
     }
 
+#if defined(MCUBOOT_DATA_SHARING)
+    /* Fetch information on maximum sizes for later usage, if needed */
+    max_size = app_max_size(state);
+
+    if (max_size > 0) {
+        image_max_sizes[BOOT_CURR_IMG(state)].calculated = true;
+        image_max_sizes[BOOT_CURR_IMG(state)].max_size = max_size;
+    }
+#endif
+
     /* If the current image's slots aren't compatible, no swap is possible.
      * Just boot into primary slot.
      */
@@ -3323,3 +3336,13 @@
         memset(&boot_data, 0, sizeof(struct boot_loader_state));
     }
 }
+
+#if defined(MCUBOOT_DATA_SHARING)
+/**
+* Fetches the maximum allowed size of the image
+*/
+const struct image_max_size *boot_get_max_app_size(void)
+{
+    return image_max_sizes;
+}
+#endif