Boot: Synchronize MCUBoot code base
Perform a partial synchronization between the MCUBoot files in TF-M
and in the original repository. The hash of the source commit in the
original repository: 4f0ea747c314547daa6b6299ccbd77ae4dee6758.
Main changes:
- Remove current_image global variable and make it part
of the boot state struct
- Update routines to receive the boot state by parameter
- Refactor dependency check functions
- Reorganize the flash map and related files
- Fix swap status control
Change-Id: Ibe948792b306e96282fb82447bb3f05a0c6389ef
Signed-off-by: David Vincze <david.vincze@arm.com>
diff --git a/bl2/ext/mcuboot/flash_map_extended.c b/bl2/ext/mcuboot/flash_map_extended.c
new file mode 100644
index 0000000..d00686e
--- /dev/null
+++ b/bl2/ext/mcuboot/flash_map_extended.c
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2018 Nordic Semiconductor ASA
+ * Copyright (c) 2015 Runtime Inc
+ * Copyright (c) 2019 Arm Limited.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+/*
+ * Original code taken from mcuboot project at:
+ * https://github.com/JuulLabs-OSS/mcuboot
+ * Git SHA of the original version: 4f0ea747c314547daa6b6299ccbd77ae4dee6758
+ */
+
+#include <errno.h>
+#include "target.h"
+#include "Driver_Flash.h"
+#include "flash_map/flash_map.h"
+#include "bootutil/bootutil_log.h"
+
+/* Flash device name must be specified by target */
+extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
+
+int flash_device_base(uint8_t fd_id, uintptr_t *ret)
+{
+ if (fd_id != FLASH_DEVICE_ID) {
+ BOOT_LOG_ERR("invalid flash ID %d; expected %d",
+ fd_id, FLASH_DEVICE_ID);
+ return -EINVAL;
+ }
+ *ret = FLASH_DEVICE_BASE;
+ return 0;
+}
+
+/*
+ * This depends on the mappings defined in flash_map.h.
+ * MCUBoot uses continuous numbering for the primary slot, the secondary slot,
+ * and the scratch while TF-M might number it differently.
+ */
+int flash_area_id_from_multi_image_slot(int image_index, int slot)
+{
+ switch (slot) {
+ case 0: return FLASH_AREA_IMAGE_PRIMARY(image_index);
+ case 1: return FLASH_AREA_IMAGE_SECONDARY(image_index);
+ case 2: return FLASH_AREA_IMAGE_SCRATCH;
+ }
+
+ return -EINVAL; /* flash_area_open will fail on that */
+}
+
+int flash_area_id_from_image_slot(int slot)
+{
+ return flash_area_id_from_multi_image_slot(0, slot);
+}
+
+int flash_area_id_to_multi_image_slot(int image_index, int area_id)
+{
+ if (area_id == FLASH_AREA_IMAGE_PRIMARY(image_index)) {
+ return 0;
+ }
+ if (area_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
+ return 1;
+ }
+
+ BOOT_LOG_ERR("invalid flash area ID");
+ return -1;
+}
+
+int flash_area_id_to_image_slot(int area_id)
+{
+ return flash_area_id_to_multi_image_slot(0, area_id);
+}
+
+uint8_t flash_area_erased_val(const struct flash_area *fap)
+{
+ (void)fap;
+
+ return FLASH_DEV_NAME.GetInfo()->erased_value;
+}
+
+int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off,
+ void *dst, uint32_t len)
+{
+ uint32_t i;
+ uint8_t *u8dst;
+ int rc;
+
+ BOOT_LOG_DBG("read_is_empty area=%d, off=%#x, len=%#x",
+ fa->fa_id, off, len);
+
+ rc = FLASH_DEV_NAME.ReadData(fa->fa_off + off, dst, len);
+ if (rc) {
+ return -1;
+ }
+
+ u8dst = (uint8_t*)dst;
+
+ for (i = 0; i < len; i++) {
+ if (u8dst[i] != flash_area_erased_val(fa)) {
+ return 0;
+ }
+ }
+
+ return 1;
+}