blob: f84d698603bbcc2bb352d655b2071df943a99a82 [file] [log] [blame]
David Vinczecea8b592019-10-29 16:09:51 +01001/*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 * Copyright (c) 2015 Runtime Inc
David Vincze225c58f2019-12-09 17:32:48 +01004 * Copyright (c) 2019-2020 Arm Limited.
David Vinczecea8b592019-10-29 16:09:51 +01005 *
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 Vincze2ddc1372019-10-25 11:10:08 +020012 * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
David Vinczecea8b592019-10-29 16:09:51 +010013 */
14
15#include <errno.h>
16#include "target.h"
17#include "Driver_Flash.h"
David Vincze225c58f2019-12-09 17:32:48 +010018#include "sysflash/sysflash.h"
David Vinczecea8b592019-10-29 16:09:51 +010019#include "flash_map/flash_map.h"
David Vincze225c58f2019-12-09 17:32:48 +010020#include "flash_map_backend/flash_map_backend.h"
David Vinczecea8b592019-10-29 16:09:51 +010021#include "bootutil/bootutil_log.h"
22
23/* Flash device name must be specified by target */
24extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
25
26int 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);
TTornblom83d96372019-11-19 12:53:16 +010031 return -1;
David Vinczecea8b592019-10-29 16:09:51 +010032 }
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 */
42int 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
TTornblom83d96372019-11-19 12:53:16 +010050 return -1; /* flash_area_open will fail on that */
David Vinczecea8b592019-10-29 16:09:51 +010051}
52
53int flash_area_id_from_image_slot(int slot)
54{
55 return flash_area_id_from_multi_image_slot(0, slot);
56}
57
58int 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
71int flash_area_id_to_image_slot(int area_id)
72{
73 return flash_area_id_to_multi_image_slot(0, area_id);
74}
75
76uint8_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
83int 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}