-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmain.c
More file actions
272 lines (220 loc) · 8.04 KB
/
main.c
File metadata and controls
272 lines (220 loc) · 8.04 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright (c) 2025 - 2026 Nordic Semiconductor
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <zephyr/kernel.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/storage/flash_map.h>
#include <zephyr/logging/log.h>
#include <zephyr/logging/log_ctrl.h>
#include <zephyr/sys/crc.h>
#include <zephyr/sys/reboot.h>
#include <bm_installs.h>
LOG_MODULE_REGISTER(installer, CONFIG_INSTALLER_LOG_LEVEL);
#if defined(CONFIG_APP_BM_INSTALLER_SELF_DELAYS)
#define VERIFICATION_DELAY(x, msg) do { \
LOG_INF(msg); \
log_flush(); \
k_busy_wait((x) * USEC_PER_MSEC); \
} while (0)
#else
#define VERIFICATION_DELAY(x, msg)
#endif /* CONFIG_APP_BM_INSTALLER_SELF_DELAYS */
#define IMAGE_DATA_SIZE 64
#define IMAGE_DATA_ARRAY_SIZE 32
#define EXPECTED_HEADER { 0x92, 0x11, 0xf2, 0xe9 }
#define PROCESS_SECTOR_SIZE 4096
#define LOAD_OFFSET (PARTITION_ADDRESS(slot0_partition))
extern uintptr_t _flash_used;
static const uint8_t expected_header[] = EXPECTED_HEADER;
struct bm_installs_update_image {
off_t start_address; /* Metadata */
size_t image_size; /* Metadata - also need to erase the end of this section */
off_t data_offset_address; /* Position of data from end of image */
size_t data_image_size; /* Length of data */
uint32_t data_checksum; /* CRC32 of data */
uint8_t image_id; /* Metadata */
uint8_t padding[3];
} __packed;
struct bm_installs_update {
uint8_t header[sizeof(expected_header)];
struct bm_installs_update_image images[CONFIG_BM_INSTALL_IMAGES];
uint32_t checksum;
} __packed;
/* Data after end of installer image containing update images */
static const struct bm_installs_update *update_data;
int main(void)
{
int rc;
uint32_t checksum_calculated;
uint32_t checksum_expected;
uint32_t installer_data_size;
uint8_t i = 0;
bool upgrade_ok = true;
struct bm_installs replacement_metadata;
struct flash_area fa_installer = {
.fa_id = 1,
.fa_off = LOAD_OFFSET,
.fa_size = PROCESS_SECTOR_SIZE,
.fa_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller)),
};
#if defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
static uint8_t erase_buffer[PROCESS_SECTOR_SIZE];
const struct flash_parameters *fparams;
#else
static uint8_t write_buffer[CONFIG_ROM_START_OFFSET] = { 0x00 };
#endif
update_data = (struct bm_installs_update *)((int)&_flash_used + LOAD_OFFSET);
if (update_data == NULL) {
LOG_ERR("Installer data is NULL");
goto erase_header;
}
if (memcmp(update_data->header, expected_header, sizeof(expected_header)) != 0) {
LOG_ERR("Header mismatch, data not valid");
goto erase_header;
}
installer_data_size = sizeof(struct bm_installs_update) - sizeof(update_data->checksum);
checksum_calculated = crc32_ieee((void *)update_data, installer_data_size);
memcpy(&checksum_expected, &update_data->checksum, sizeof(checksum_expected));
LOG_DBG("Checksum - calculated %u, expected %u", checksum_calculated, checksum_expected);
if (checksum_calculated == checksum_expected) {
while (i < CONFIG_BM_INSTALL_IMAGES) {
checksum_calculated = crc32_ieee((void *)((uint32_t)update_data +
sizeof(struct bm_installs_update) +
update_data->images[i].data_offset_address),
update_data->images[i].data_image_size);
memcpy(&checksum_expected, &update_data->images[i].data_checksum,
sizeof(checksum_expected));
LOG_DBG("Image %d:", i);
LOG_DBG("\tStart address: 0x%lx:", update_data->images[i].start_address);
LOG_DBG("\tSize: %#x:", update_data->images[i].image_size);
LOG_DBG("\tData address: 0x%lx:",
update_data->images[i].data_offset_address);
LOG_DBG("\tData size: %#x:", update_data->images[i].data_image_size);
LOG_DBG("\tImage ID: %#x:", update_data->images[i].image_id);
LOG_DBG("\tImage checksum (calculated): %u", checksum_calculated);
LOG_DBG("\tImage checksum (expected): %u", checksum_expected);
if (checksum_calculated != checksum_expected) {
upgrade_ok = false;
}
++i;
}
} else {
upgrade_ok = false;
}
if (!upgrade_ok) {
/* Update data is bad, erase own header and reboot */
LOG_ERR("Upgrade data bad");
goto erase_header;
}
LOG_DBG("Upgrade data OK");
#if defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
fparams = flash_get_parameters(DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller)));
memset(erase_buffer, fparams->erase_value, sizeof(erase_buffer));
#endif
i = 0;
bm_installs_init();
if (bm_installs_is_valid()) {
rc = bm_installs_invalidate();
if (rc) {
LOG_ERR("Metadata invalidation failed: %d", rc);
} else {
LOG_DBG("Metadata invalidation OK");
}
}
/* Delay for reliability verification purpose */
VERIFICATION_DELAY(CONFIG_APP_BM_INSTALLER_POST_METADATA_INVALIDATION_DELAY_MS,
"V_DELAY_POST_METADATA_INVALIDATION");
memset(&replacement_metadata.padding, 0xff, BM_INSTALLS_PADDING_SIZE);
while (i < CONFIG_BM_INSTALL_IMAGES) {
uint32_t pos = 0;
uint32_t read_pos = (uint32_t)update_data + sizeof(struct bm_installs_update) +
update_data->images[i].data_offset_address;
uint32_t write_pos = update_data->images[i].start_address;
struct flash_area fa = {
.fa_id = 1,
.fa_off = write_pos,
.fa_size = update_data->images[i].image_size,
.fa_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller)),
};
LOG_DBG("Start update of image %d...", i);
replacement_metadata.images[i].start_address =
update_data->images[i].start_address;
replacement_metadata.images[i].image_size = update_data->images[i].image_size;
while (pos < update_data->images[i].data_image_size) {
uint32_t process_size = update_data->images[i].data_image_size - pos;
if (process_size > PROCESS_SECTOR_SIZE) {
process_size = PROCESS_SECTOR_SIZE;
}
LOG_DBG("Write to: %p, read from: %p, size: %d", (void *)write_pos,
(void *)read_pos, process_size);
if (memcmp((void *)write_pos, (void *)read_pos, process_size)) {
#if defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
if (memcmp((void *)write_pos, erase_buffer, process_size)) {
rc = flash_area_erase(&fa, pos, PROCESS_SECTOR_SIZE);
if (rc) {
LOG_ERR("Erase failed: %d, at: %#x, size: %d",
rc, pos, PROCESS_SECTOR_SIZE);
goto erase_header;
} else {
LOG_DBG("Erase OK at: %#x, size: %d", pos,
PROCESS_SECTOR_SIZE);
}
}
#endif
rc = flash_area_write(&fa, pos, (void *)read_pos, process_size);
if (rc) {
LOG_ERR("Write failed: %d, at: %#x, size: %d", rc, pos,
process_size);
goto erase_header;
} else {
LOG_DBG("Write OK at: %#x, size: %d", pos, process_size);
}
}
pos += process_size;
read_pos += process_size;
write_pos += process_size;
/* Delay for reliability verification purpose */
VERIFICATION_DELAY(CONFIG_APP_BM_INSTALLER_INTER_IMAGE_CHUNK_COPY_DELAY_MS,
"V_DELAY_IMAGE_CHUNK_COPY");
}
++i;
if (i < CONFIG_BM_INSTALL_IMAGES) {
VERIFICATION_DELAY(CONFIG_APP_BM_INSTALLER_NEXT_IMAGE_COPY_DELAY_MS,
"V_DELAY_NEXT_IMAGE_COPY");
}
}
/* Delay for reliability verification purpose */
VERIFICATION_DELAY(CONFIG_APP_BM_INSTALLER_PRIOR_METADATA_UPDATE_DELAY_MS,
"V_DELAY_METADATA_UPDATE");
/* Write new metadata, after updating checksum */
replacement_metadata.checksum = crc32_ieee((const uint8_t *)&replacement_metadata,
(sizeof(struct bm_installs) -
sizeof(replacement_metadata.checksum)));
rc = bm_installs_write(&replacement_metadata);
if (rc) {
LOG_ERR("Metadata update failed: %d", rc);
} else {
LOG_DBG("Metadata update OK");
}
erase_header:
/* Erase header of installer image to boot back into firmware loader image */
/* Delay for reliability verification purpose */
VERIFICATION_DELAY(CONFIG_APP_BM_INSTALLER_PRIOR_SELF_DESTRUCTION_DELAY_MS,
"V_DELAY_SELF_DESTRUCTION");
#if defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
memset(erase_buffer, 0, sizeof(erase_buffer));
rc = flash_area_write(&fa_installer, 0, erase_buffer, CONFIG_ROM_START_OFFSET);
#else
rc = flash_area_write(&fa_installer, 0, write_buffer, CONFIG_ROM_START_OFFSET);
#endif
if (rc) {
LOG_ERR("Clear installer header failed: %d", rc);
} else {
LOG_INF("Clear installer header OK");
}
log_flush();
sys_reboot(SYS_REBOOT_WARM);
return 0;
}