blob: 2e019bdc97a7889bfa8ec6801ef77d5a57515415 [file] [log] [blame]
David Vinczecea8b592019-10-29 16:09:51 +01001/*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 * Copyright (c) 2015 Runtime Inc
4 * Copyright (c) 2019 Arm Limited.
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 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"
18#include "flash_map/flash_map.h"
19#include "bootutil/bootutil_log.h"
20
21/* Flash device name must be specified by target */
22extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
23
24int flash_device_base(uint8_t fd_id, uintptr_t *ret)
25{
26 if (fd_id != FLASH_DEVICE_ID) {
27 BOOT_LOG_ERR("invalid flash ID %d; expected %d",
28 fd_id, FLASH_DEVICE_ID);
29 return -EINVAL;
30 }
31 *ret = FLASH_DEVICE_BASE;
32 return 0;
33}
34
35/*
36 * This depends on the mappings defined in flash_map.h.
37 * MCUBoot uses continuous numbering for the primary slot, the secondary slot,
38 * and the scratch while TF-M might number it differently.
39 */
40int flash_area_id_from_multi_image_slot(int image_index, int slot)
41{
42 switch (slot) {
43 case 0: return FLASH_AREA_IMAGE_PRIMARY(image_index);
44 case 1: return FLASH_AREA_IMAGE_SECONDARY(image_index);
45 case 2: return FLASH_AREA_IMAGE_SCRATCH;
46 }
47
48 return -EINVAL; /* flash_area_open will fail on that */
49}
50
51int flash_area_id_from_image_slot(int slot)
52{
53 return flash_area_id_from_multi_image_slot(0, slot);
54}
55
56int flash_area_id_to_multi_image_slot(int image_index, int area_id)
57{
58 if (area_id == FLASH_AREA_IMAGE_PRIMARY(image_index)) {
59 return 0;
60 }
61 if (area_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
62 return 1;
63 }
64
65 BOOT_LOG_ERR("invalid flash area ID");
66 return -1;
67}
68
69int flash_area_id_to_image_slot(int area_id)
70{
71 return flash_area_id_to_multi_image_slot(0, area_id);
72}
73
74uint8_t flash_area_erased_val(const struct flash_area *fap)
75{
76 (void)fap;
77
78 return FLASH_DEV_NAME.GetInfo()->erased_value;
79}
80
81int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off,
82 void *dst, uint32_t len)
83{
84 uint32_t i;
85 uint8_t *u8dst;
86 int rc;
87
88 BOOT_LOG_DBG("read_is_empty area=%d, off=%#x, len=%#x",
89 fa->fa_id, off, len);
90
91 rc = FLASH_DEV_NAME.ReadData(fa->fa_off + off, dst, len);
92 if (rc) {
93 return -1;
94 }
95
96 u8dst = (uint8_t*)dst;
97
98 for (i = 0; i < len; i++) {
99 if (u8dst[i] != flash_area_erased_val(fa)) {
100 return 0;
101 }
102 }
103
104 return 1;
105}