blob: e854b552cd8dd761fb53e78a856a3f0cfefcab63 [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:
Tamas Banc5b2f2b2020-11-12 09:47:05 +000011 * https://github.com/mcu-tools/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"
Michel Jaouen26f6c022020-12-03 10:37:22 +010017#include "cmsis.h"
David Vinczecea8b592019-10-29 16:09:51 +010018#include "Driver_Flash.h"
David Vincze225c58f2019-12-09 17:32:48 +010019#include "sysflash/sysflash.h"
David Vinczecea8b592019-10-29 16:09:51 +010020#include "flash_map/flash_map.h"
David Vincze225c58f2019-12-09 17:32:48 +010021#include "flash_map_backend/flash_map_backend.h"
David Vinczecea8b592019-10-29 16:09:51 +010022#include "bootutil/bootutil_log.h"
23
Michel Jaouen26f6c022020-12-03 10:37:22 +010024__WEAK int flash_device_base(uint8_t fd_id, uintptr_t *ret)
David Vinczecea8b592019-10-29 16:09:51 +010025{
26 if (fd_id != FLASH_DEVICE_ID) {
27 BOOT_LOG_ERR("invalid flash ID %d; expected %d",
28 fd_id, FLASH_DEVICE_ID);
TTornblom83d96372019-11-19 12:53:16 +010029 return -1;
David Vinczecea8b592019-10-29 16:09:51 +010030 }
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
TTornblom83d96372019-11-19 12:53:16 +010048 return -1; /* flash_area_open will fail on that */
David Vinczecea8b592019-10-29 16:09:51 +010049}
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{
Michel Jaouen26f6c022020-12-03 10:37:22 +010076 return DRV_FLASH_AREA(fap)->GetInfo()->erased_value;
David Vinczecea8b592019-10-29 16:09:51 +010077}
78
79int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off,
80 void *dst, uint32_t len)
81{
82 uint32_t i;
83 uint8_t *u8dst;
84 int rc;
85
86 BOOT_LOG_DBG("read_is_empty area=%d, off=%#x, len=%#x",
87 fa->fa_id, off, len);
88
Michel Jaouen26f6c022020-12-03 10:37:22 +010089 rc = DRV_FLASH_AREA(fa)->ReadData(fa->fa_off + off, dst, len);
David Vinczecea8b592019-10-29 16:09:51 +010090 if (rc) {
91 return -1;
92 }
93
94 u8dst = (uint8_t*)dst;
95
96 for (i = 0; i < len; i++) {
97 if (u8dst[i] != flash_area_erased_val(fa)) {
98 return 0;
99 }
100 }
101
102 return 1;
103}