forked from nrfconnect/sdk-mcuboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash_map_extended.c
138 lines (114 loc) · 3.13 KB
/
flash_map_extended.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
* Copyright (c) 2015 Runtime Inc
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <drivers/flash.h>
#include "target.h"
#include <flash_map_backend/flash_map_backend.h>
#include <sysflash/sysflash.h>
#include "bootutil/bootutil_log.h"
MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
#if (!defined(CONFIG_XTENSA) && defined(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL))
#define FLASH_DEVICE_ID SOC_FLASH_0_ID
#define FLASH_DEVICE_BASE CONFIG_FLASH_BASE_ADDRESS
#elif (defined(CONFIG_XTENSA) && defined(DT_JEDEC_SPI_NOR_0_LABEL))
#define FLASH_DEVICE_ID SPI_FLASH_0_ID
#define FLASH_DEVICE_BASE 0
#else
#error "FLASH_DEVICE_ID could not be determined"
#endif
static struct device *flash_dev;
struct device *flash_device_get_binding(char *dev_name)
{
if (!flash_dev) {
flash_dev = device_get_binding(dev_name);
}
return flash_dev;
}
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 sysflash.h.
* MCUBoot uses continuous numbering for the primary slot, the secondary slot,
* and the scratch while zephyr 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);
#if !defined(CONFIG_SINGLE_IMAGE_DFU)
case 1: return FLASH_AREA_IMAGE_SECONDARY(image_index);
#if !defined(CONFIG_BOOT_SWAP_USING_MOVE)
case 2: return FLASH_AREA_IMAGE_SCRATCH;
#endif
#endif
}
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 !defined(CONFIG_SINGLE_IMAGE_DFU)
if (area_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
return 1;
}
#endif
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);
}
int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
{
int rc;
struct flash_pages_info page;
rc = flash_get_page_info_by_offs(flash_dev, off, &page);
if (rc) {
return rc;
}
sector->fs_off = page.start_offset;
sector->fs_size = page.size;
return rc;
}
#define ERASED_VAL 0xff
uint8_t flash_area_erased_val(const struct flash_area *fap)
{
(void)fap;
return ERASED_VAL;
}
int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off,
void *dst, uint32_t len)
{
uint8_t i;
uint8_t *u8dst;
int rc;
rc = flash_area_read(fa, off, dst, len);
if (rc) {
return -1;
}
for (i = 0, u8dst = (uint8_t *)dst; i < len; i++) {
if (u8dst[i] != ERASED_VAL) {
return 0;
}
}
return 1;
}