Boot: Synchronize MCUBoot with v1.4.0

Perform a partial synchronization of the MCUBoot code base in TF-M
with mainstream MCUBoot version 1.4.0. Hash of the source commit in the
original repository: ac55554059147fff718015be9f4bd3108123f50a.

Main changes:
- Add overflow-safe 32 and 16-bit add functions

Change-Id: I36f04bcc9c7fb9c7609bb45b621ece518da91ad0
Signed-off-by: David Vincze <david.vincze@arm.com>
diff --git a/bl2/ext/mcuboot/bootutil/include/bootutil/bootutil.h b/bl2/ext/mcuboot/bootutil/include/bootutil/bootutil.h
index 5d48124..92a9efc 100644
--- a/bl2/ext/mcuboot/bootutil/include/bootutil/bootutil.h
+++ b/bl2/ext/mcuboot/bootutil/include/bootutil/bootutil.h
@@ -50,8 +50,7 @@
 /** Swapping encountered an unrecoverable error */
 #define BOOT_SWAP_TYPE_PANIC    0xff
 
-#define MAX_FLASH_ALIGN         8
-extern const uint32_t BOOT_MAX_ALIGN;
+#define BOOT_MAX_ALIGN          8
 
 struct image_header;
 /**
@@ -75,11 +74,11 @@
  */
 struct image_trailer {
     uint8_t swap_type;
-    uint8_t pad1[MAX_FLASH_ALIGN - 1];
+    uint8_t pad1[BOOT_MAX_ALIGN - 1];
     uint8_t copy_done;
-    uint8_t pad2[MAX_FLASH_ALIGN - 1];
+    uint8_t pad2[BOOT_MAX_ALIGN - 1];
     uint8_t image_ok;
-    uint8_t pad3[MAX_FLASH_ALIGN - 1];
+    uint8_t pad3[BOOT_MAX_ALIGN - 1];
     uint8_t magic[16];
 };
 
diff --git a/bl2/ext/mcuboot/bootutil/include/bootutil/image.h b/bl2/ext/mcuboot/bootutil/include/bootutil/image.h
index 2b3163b..a3ebf91 100644
--- a/bl2/ext/mcuboot/bootutil/include/bootutil/image.h
+++ b/bl2/ext/mcuboot/bootutil/include/bootutil/image.h
@@ -20,7 +20,7 @@
 /*
  * Original code taken from mcuboot project at:
  * https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
+ * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
  * Modifications are Copyright (c) 2018-2019 Arm Limited.
  */
 
diff --git a/bl2/ext/mcuboot/bootutil/include/bootutil/sha256.h b/bl2/ext/mcuboot/bootutil/include/bootutil/sha256.h
index 763097d..545621e 100644
--- a/bl2/ext/mcuboot/bootutil/include/bootutil/sha256.h
+++ b/bl2/ext/mcuboot/bootutil/include/bootutil/sha256.h
@@ -27,7 +27,7 @@
 /*
  * Original code taken from mcuboot project at:
  * https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
+ * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
  * Modifications are Copyright (c) 2019 Arm Limited.
  */
 
diff --git a/bl2/ext/mcuboot/bootutil/src/bootutil_misc.c b/bl2/ext/mcuboot/bootutil/src/bootutil_misc.c
index e295fa8..4cddec3 100644
--- a/bl2/ext/mcuboot/bootutil/src/bootutil_misc.c
+++ b/bl2/ext/mcuboot/bootutil/src/bootutil_misc.c
@@ -20,7 +20,7 @@
 /*
  * Original code taken from mcuboot project at:
  * https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
+ * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
  * Modifications are Copyright (c) 2019 Arm Limited.
  */
 
@@ -49,9 +49,6 @@
 #define BOOT_MAGIC_ARR_SZ \
     (sizeof boot_img_magic / sizeof boot_img_magic[0])
 
-const uint32_t BOOT_MAGIC_SZ = sizeof(boot_img_magic);
-const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
-
 struct boot_swap_table {
     uint8_t magic_primary_slot;
     uint8_t magic_secondary_slot;
diff --git a/bl2/ext/mcuboot/bootutil/src/bootutil_priv.h b/bl2/ext/mcuboot/bootutil/src/bootutil_priv.h
index 5d5e19d..f2965e5 100644
--- a/bl2/ext/mcuboot/bootutil/src/bootutil_priv.h
+++ b/bl2/ext/mcuboot/bootutil/src/bootutil_priv.h
@@ -20,7 +20,7 @@
 /*
  * Original code taken from mcuboot project at:
  * https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
+ * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
  * Modifications are Copyright (c) 2018-2019 Arm Limited.
  */
 
@@ -174,7 +174,7 @@
 #define BOOT_STATUS_SOURCE_SCRATCH      1
 #define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2
 
-extern const uint32_t BOOT_MAGIC_SZ;
+#define BOOT_MAGIC_SZ (sizeof boot_img_magic)
 
 /**
  * Compatibility shim for flash sector type.
@@ -236,6 +236,41 @@
 bool boot_add_uint32_overflow_check(uint32_t a, uint32_t b);
 bool boot_add_uint16_overflow_check(uint16_t a, uint16_t b);
 
+/**
+ * Safe (non-overflowing) uint32_t addition.  Returns true, and stores
+ * the result in *dest if it can be done without overflow.  Otherwise,
+ * returns false.
+ */
+static inline bool boot_u32_safe_add(uint32_t *dest, uint32_t a, uint32_t b)
+{
+    /*
+     * "a + b <= UINT32_MAX", subtract 'b' from both sides to avoid
+     * the overflow.
+     */
+    if (a > UINT32_MAX - b) {
+        return false;
+    } else {
+        *dest = a + b;
+        return true;
+    }
+}
+
+/**
+ * Safe (non-overflowing) uint16_t addition.  Returns true, and stores
+ * the result in *dest if it can be done without overflow.  Otherwise,
+ * returns false.
+ */
+static inline bool boot_u16_safe_add(uint16_t *dest, uint16_t a, uint16_t b)
+{
+    uint32_t tmp = a + b;
+    if (tmp > UINT16_MAX) {
+        return false;
+    } else {
+        *dest = tmp;
+        return true;
+    }
+}
+
 /*
  * Accessors for the contents of struct boot_loader_state.
  */
diff --git a/bl2/ext/mcuboot/bootutil/src/image_rsa.c b/bl2/ext/mcuboot/bootutil/src/image_rsa.c
index 9aaf403..ea7c78f 100644
--- a/bl2/ext/mcuboot/bootutil/src/image_rsa.c
+++ b/bl2/ext/mcuboot/bootutil/src/image_rsa.c
@@ -20,7 +20,7 @@
 /*
  * Original code taken from mcuboot project at:
  * https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
+ * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
  * Modifications are Copyright (c) 2018-2019 Arm Limited.
  */
 
diff --git a/bl2/ext/mcuboot/bootutil/src/image_validate.c b/bl2/ext/mcuboot/bootutil/src/image_validate.c
index 3b18010..18dd3e1 100644
--- a/bl2/ext/mcuboot/bootutil/src/image_validate.c
+++ b/bl2/ext/mcuboot/bootutil/src/image_validate.c
@@ -20,7 +20,7 @@
 /*
  * Original code taken from mcuboot project at:
  * https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
+ * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
  * Modifications are Copyright (c) 2018-2019 Arm Limited.
  */
 
diff --git a/bl2/ext/mcuboot/bootutil/src/loader.c b/bl2/ext/mcuboot/bootutil/src/loader.c
index ab25e43..9387174 100644
--- a/bl2/ext/mcuboot/bootutil/src/loader.c
+++ b/bl2/ext/mcuboot/bootutil/src/loader.c
@@ -20,7 +20,7 @@
 /*
  * Original code taken from mcuboot project at:
  * https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
+ * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
  * Modifications are Copyright (c) 2018-2019 Arm Limited.
  */
 
diff --git a/bl2/ext/mcuboot/flash_map_extended.c b/bl2/ext/mcuboot/flash_map_extended.c
index 2f0c8ad..2e019bd 100644
--- a/bl2/ext/mcuboot/flash_map_extended.c
+++ b/bl2/ext/mcuboot/flash_map_extended.c
@@ -9,7 +9,7 @@
 /*
  * Original code taken from mcuboot project at:
  * https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
+ * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
  */
 
 #include <errno.h>
diff --git a/bl2/ext/mcuboot/flash_map_legacy.c b/bl2/ext/mcuboot/flash_map_legacy.c
index bdcf44b..6ea65fe 100644
--- a/bl2/ext/mcuboot/flash_map_legacy.c
+++ b/bl2/ext/mcuboot/flash_map_legacy.c
@@ -20,7 +20,7 @@
 /*
  * Original code taken from mcuboot project at:
  * https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
+ * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
  * Modifications are Copyright (c) 2019 Arm Limited.
  */
 
diff --git a/bl2/ext/mcuboot/include/config-rsa.h b/bl2/ext/mcuboot/include/config-rsa.h
index 7082268..b721953 100644
--- a/bl2/ext/mcuboot/include/config-rsa.h
+++ b/bl2/ext/mcuboot/include/config-rsa.h
@@ -24,7 +24,7 @@
 /*
  * Original code taken from mcuboot project at:
  * https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
+ * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
  */
 
 /*
diff --git a/bl2/ext/mcuboot/include/flash_map/flash_map.h b/bl2/ext/mcuboot/include/flash_map/flash_map.h
index 44dc1f9..1bc30f1 100644
--- a/bl2/ext/mcuboot/include/flash_map/flash_map.h
+++ b/bl2/ext/mcuboot/include/flash_map/flash_map.h
@@ -20,7 +20,7 @@
 /*
  * Original code taken from mcuboot project at:
  * https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
+ * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
  * Modifications are Copyright (c) 2018-2019 Arm Limited.
  */
 
diff --git a/bl2/ext/mcuboot/include/target.h b/bl2/ext/mcuboot/include/target.h
index 0e1a211..68f1190 100644
--- a/bl2/ext/mcuboot/include/target.h
+++ b/bl2/ext/mcuboot/include/target.h
@@ -8,7 +8,7 @@
 /*
  * Original code taken from mcuboot project at:
  * https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
+ * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
  */
 
 #ifndef H_TARGETS_TARGET_
diff --git a/bl2/ext/mcuboot/keys.c b/bl2/ext/mcuboot/keys.c
index 0640cdc..c8f960b 100644
--- a/bl2/ext/mcuboot/keys.c
+++ b/bl2/ext/mcuboot/keys.c
@@ -20,7 +20,7 @@
 /*
  * Original code taken from mcuboot project at:
  * https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
+ * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
  * Modifications are Copyright (c) 2019 Arm Limited.
  */