David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Nordic Semiconductor ASA |
| 3 | * Copyright (c) 2015 Runtime Inc |
David Vincze | 225c58f | 2019-12-09 17:32:48 +0100 | [diff] [blame] | 4 | * Copyright (c) 2019-2020 Arm Limited. |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 5 | * |
| 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Original code taken from mcuboot project at: |
| 11 | * https://github.com/JuulLabs-OSS/mcuboot |
David Vincze | 2ddc137 | 2019-10-25 11:10:08 +0200 | [diff] [blame] | 12 | * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <errno.h> |
| 16 | #include "target.h" |
| 17 | #include "Driver_Flash.h" |
David Vincze | 225c58f | 2019-12-09 17:32:48 +0100 | [diff] [blame] | 18 | #include "sysflash/sysflash.h" |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 19 | #include "flash_map/flash_map.h" |
David Vincze | 225c58f | 2019-12-09 17:32:48 +0100 | [diff] [blame] | 20 | #include "flash_map_backend/flash_map_backend.h" |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 21 | #include "bootutil/bootutil_log.h" |
| 22 | |
| 23 | /* Flash device name must be specified by target */ |
| 24 | extern ARM_DRIVER_FLASH FLASH_DEV_NAME; |
| 25 | |
| 26 | int flash_device_base(uint8_t fd_id, uintptr_t *ret) |
| 27 | { |
| 28 | if (fd_id != FLASH_DEVICE_ID) { |
| 29 | BOOT_LOG_ERR("invalid flash ID %d; expected %d", |
| 30 | fd_id, FLASH_DEVICE_ID); |
TTornblom | 83d9637 | 2019-11-19 12:53:16 +0100 | [diff] [blame^] | 31 | return -1; |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 32 | } |
| 33 | *ret = FLASH_DEVICE_BASE; |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * This depends on the mappings defined in flash_map.h. |
| 39 | * MCUBoot uses continuous numbering for the primary slot, the secondary slot, |
| 40 | * and the scratch while TF-M might number it differently. |
| 41 | */ |
| 42 | int flash_area_id_from_multi_image_slot(int image_index, int slot) |
| 43 | { |
| 44 | switch (slot) { |
| 45 | case 0: return FLASH_AREA_IMAGE_PRIMARY(image_index); |
| 46 | case 1: return FLASH_AREA_IMAGE_SECONDARY(image_index); |
| 47 | case 2: return FLASH_AREA_IMAGE_SCRATCH; |
| 48 | } |
| 49 | |
TTornblom | 83d9637 | 2019-11-19 12:53:16 +0100 | [diff] [blame^] | 50 | return -1; /* flash_area_open will fail on that */ |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | int flash_area_id_from_image_slot(int slot) |
| 54 | { |
| 55 | return flash_area_id_from_multi_image_slot(0, slot); |
| 56 | } |
| 57 | |
| 58 | int flash_area_id_to_multi_image_slot(int image_index, int area_id) |
| 59 | { |
| 60 | if (area_id == FLASH_AREA_IMAGE_PRIMARY(image_index)) { |
| 61 | return 0; |
| 62 | } |
| 63 | if (area_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) { |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | BOOT_LOG_ERR("invalid flash area ID"); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | int flash_area_id_to_image_slot(int area_id) |
| 72 | { |
| 73 | return flash_area_id_to_multi_image_slot(0, area_id); |
| 74 | } |
| 75 | |
| 76 | uint8_t flash_area_erased_val(const struct flash_area *fap) |
| 77 | { |
| 78 | (void)fap; |
| 79 | |
| 80 | return FLASH_DEV_NAME.GetInfo()->erased_value; |
| 81 | } |
| 82 | |
| 83 | int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off, |
| 84 | void *dst, uint32_t len) |
| 85 | { |
| 86 | uint32_t i; |
| 87 | uint8_t *u8dst; |
| 88 | int rc; |
| 89 | |
| 90 | BOOT_LOG_DBG("read_is_empty area=%d, off=%#x, len=%#x", |
| 91 | fa->fa_id, off, len); |
| 92 | |
| 93 | rc = FLASH_DEV_NAME.ReadData(fa->fa_off + off, dst, len); |
| 94 | if (rc) { |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | u8dst = (uint8_t*)dst; |
| 99 | |
| 100 | for (i = 0; i < len; i++) { |
| 101 | if (u8dst[i] != flash_area_erased_val(fa)) { |
| 102 | return 0; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return 1; |
| 107 | } |