-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathactive_application.cpp
executable file
·362 lines (293 loc) · 13.1 KB
/
active_application.cpp
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// ----------------------------------------------------------------------------
// Copyright 2016-2017 ARM Ltd.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include "active_application.h"
#include "bootloader_common.h"
#include "update-client-metadata-header/arm_uc_metadata_header_v2.h"
#include "update-client-paal/arm_uc_paal_update_api.h"
#include "update-client-pal-flashiap/arm_uc_pal_flashiap_platform.h"
#include "mbedtls/sha256.h"
#include "mbed.h"
#include <inttypes.h>
bool activeStorageInit(void)
{
int rc = arm_uc_flashiap_init();
return (rc == ARM_UC_FLASHIAP_SUCCESS);
}
void activeStorageDeinit(void)
{
arm_uc_flashiap_free();
}
/**
* Read the metadata header of the active image from internal flash
* @param headerP
* Caller-allocated header structure.
* @return true if the read succeeds.
*/
bool readActiveFirmwareHeader(arm_uc_firmware_details_t *details)
{
bool result = false;
if (details) {
/* get active firmware details using UCP */
int32_t status = MBED_CLOUD_CLIENT_UPDATE_STORAGE.GetActiveFirmwareDetails(details);
if (status == ERR_NONE) {
result = true;
}
}
return result;
}
/**
* Verify the integrity of the Active application
* @detail Read the firmware in the ACTIVE app region and compute its hash.
* Compare the computed hash with the one given in the header
* to verify the ACTIVE firmware integrity
* @param headerP
* Caller-allocated header structure containing the hash and size
* of the firmware.
* @return SUCCESS if the validation succeeds
* EMPTY if no active application is present
* ERROR if the validation fails
*/
int checkActiveApplication(arm_uc_firmware_details_t *details)
{
int result = RESULT_ERROR;
if (details) {
/* Read header and verify that it is valid */
bool headerValid = readActiveFirmwareHeader(details);
/* calculate hash if header is valid and slot is not empty */
if ((headerValid) && (details->size > 0)) {
uint32_t appStart = MBED_CONF_MBED_BOOTLOADER_APPLICATION_START_ADDRESS;
/* initialize hashing facility */
mbedtls_sha256_context mbedtls_ctx;
mbedtls_sha256_init(&mbedtls_ctx);
mbedtls_sha256_starts(&mbedtls_ctx, 0);
uint8_t SHA[SIZEOF_SHA256] = { 0 };
uint32_t remaining = details->size;
int32_t status = ARM_UC_FLASHIAP_SUCCESS;
/* read full image */
while ((remaining > 0) && (status == ARM_UC_FLASHIAP_SUCCESS)) {
/* read full buffer or what is remaining */
uint32_t readSize = (remaining > BUFFER_SIZE) ?
BUFFER_SIZE : remaining;
/* read buffer using FlashIAP API for portability */
status = arm_uc_flashiap_read(buffer_array,
appStart + (details->size - remaining),
readSize);
/* update hash */
mbedtls_sha256_update(&mbedtls_ctx, buffer_array, readSize);
/* update remaining bytes */
remaining -= readSize;
}
/* finalize hash */
mbedtls_sha256_finish(&mbedtls_ctx, SHA);
mbedtls_sha256_free(&mbedtls_ctx);
/* compare calculated hash with hash from header */
int diff = memcmp(details->hash, SHA, SIZEOF_SHA256);
if (diff == 0) {
result = RESULT_SUCCESS;
}
} else if ((headerValid) && (details->size == 0)) {
/* header is valid but application size is 0 */
result = RESULT_EMPTY;
}
}
return result;
}
uint32_t getSectorAlignedSize(uint32_t addr, uint32_t size)
{
/* Find the exact end sector boundary. Some platforms have different sector
sizes from sector to sector. Hence we count the sizes 1 sector at a time here */
uint32_t erase_address = addr;
while (erase_address < (addr + size)) {
erase_address += arm_uc_flashiap_get_sector_size(erase_address);
}
return erase_address - addr;
}
/**
* Wipe the ACTIVE firmware region in the flash
*/
bool eraseActiveFirmware(uint32_t firmwareSize)
{
uint32_t fw_metadata_hdr_size = getSectorAlignedSize(FIRMWARE_METADATA_HEADER_ADDRESS,
ARM_UC_INTERNAL_HEADER_SIZE_V2);
uint32_t size_needed = 0;
uint32_t erase_start_addr = 0;
int result = ARM_UC_FLASHIAP_SUCCESS;
if (((FIRMWARE_METADATA_HEADER_ADDRESS + fw_metadata_hdr_size) < \
(MBED_CONF_MBED_BOOTLOADER_APPLICATION_START_ADDRESS)) || \
(FIRMWARE_METADATA_HEADER_ADDRESS > MBED_CONF_MBED_BOOTLOADER_APPLICATION_START_ADDRESS)) {
/* header separate from app */
boot_debug("[DBG ] Erasing header separately from active application\r\n");
/* erase header section first */
result = arm_uc_flashiap_erase(FIRMWARE_METADATA_HEADER_ADDRESS, fw_metadata_hdr_size);
/* setup erase of the application region */
size_needed = firmwareSize;
erase_start_addr = MBED_CONF_MBED_BOOTLOADER_APPLICATION_START_ADDRESS;
} else { /* header contiguous with app */
/* setup erase of the header + application region */
size_needed = (MBED_CONF_MBED_BOOTLOADER_APPLICATION_START_ADDRESS - FIRMWARE_METADATA_HEADER_ADDRESS) + firmwareSize;
erase_start_addr = FIRMWARE_METADATA_HEADER_ADDRESS;
}
if (result == ARM_UC_FLASHIAP_SUCCESS) {
result = ARM_UC_FLASHIAP_FAIL;
uint32_t erase_end_addr = erase_start_addr + \
getSectorAlignedSize(erase_start_addr,
size_needed);
uint32_t max_end_addr = MBED_CONF_MBED_BOOTLOADER_MAX_APPLICATION_SIZE + \
MBED_CONF_MBED_BOOTLOADER_APPLICATION_START_ADDRESS;
/* check that the erase will not exceed MBED_CONF_MBED_BOOTLOADER_MAX_APPLICATION_SIZE */
if (erase_end_addr <= max_end_addr) {
result = arm_uc_flashiap_erase(erase_start_addr, size_needed);
} else {
boot_debug("[DBG ] Firmware size rounded up to the nearest sector boundary is larger than the maximum application size\r\n");
}
}
return (result == ARM_UC_FLASHIAP_SUCCESS);
}
bool writeActiveFirmwareHeader(arm_uc_firmware_details_t *details)
{
int result = ARM_UC_FLASHIAP_FAIL;
if (details) {
/* round up program size to nearest page size */
const uint32_t pageSize = arm_uc_flashiap_get_page_size();
const uint32_t programSize = (ARM_UC_INTERNAL_HEADER_SIZE_V2 + pageSize - 1)
/ pageSize * pageSize;
const uint32_t fw_metadata_hdr_size = \
getSectorAlignedSize(FIRMWARE_METADATA_HEADER_ADDRESS,
ARM_UC_INTERNAL_HEADER_SIZE_V2);
/* coverity[no_escape] */
MBED_BOOTLOADER_ASSERT((programSize <= BUFFER_SIZE),
"Header program size %" PRIu32 " bigger than buffer %d\r\n",
programSize, BUFFER_SIZE);
/* coverity[no_escape] */
MBED_BOOTLOADER_ASSERT((programSize <= fw_metadata_hdr_size),
"Header program size %" PRIu32 " bigger than expected header %" PRIu32 "\r\n",
programSize, fw_metadata_hdr_size);
/* pad buffer to 0xFF */
memset(buffer_array, 0xFF, programSize);
/* create internal header in temporary buffer */
arm_uc_buffer_t output_buffer = {
.size_max = BUFFER_SIZE,
.size = 0,
.ptr = buffer_array
};
int32_t status = arm_uc_create_internal_header_v2(details,
&output_buffer);
if ((status == ERR_NONE) &&
(output_buffer.size == ARM_UC_INTERNAL_HEADER_SIZE_V2)) {
/* write header using FlashIAP API */
result = arm_uc_flashiap_program(buffer_array,
FIRMWARE_METADATA_HEADER_ADDRESS,
programSize);
}
}
return (result == ARM_UC_FLASHIAP_SUCCESS);
}
bool writeActiveFirmware(uint32_t index, arm_uc_firmware_details_t *details)
{
bool result = false;
if (details) {
const uint32_t pageSize = arm_uc_flashiap_get_page_size();
/* we require app_start_addr fall on a page size boundary */
uint32_t app_start_addr = MBED_CONF_MBED_BOOTLOADER_APPLICATION_START_ADDRESS;
/* coverity[no_escape] */
MBED_BOOTLOADER_ASSERT((app_start_addr % pageSize) == 0,
"Application (0x%" PRIX32 ") does not start on a "
"page size (0x%" PRIX32 ") aligned address\r\n",
app_start_addr,
pageSize);
/* round down the read size to a multiple of the page size
that still fits inside the main buffer.
*/
uint32_t readSize = (BUFFER_SIZE / pageSize) * pageSize;
arm_uc_buffer_t buffer = {
.size_max = readSize,
.size = 0,
.ptr = buffer_array
};
result = true;
uint32_t offset = 0;
/* write firmware */
while ((offset < details->size) &&
(result == true)) {
/* set the number of bytes expected */
buffer.size = (details->size - offset) > buffer.size_max ?
buffer.size_max : (details->size - offset);
/* fill buffer using UCP */
int32_t ucp_status = MBED_CLOUD_CLIENT_UPDATE_STORAGE.Read(index, offset, &buffer);
/* check status and actual read size */
if ((ucp_status == ERR_NONE) &&
(buffer.size > 0)) {
/* the last page, in the last buffer might not be completely
filled, round up the program size to include the last page
*/
uint32_t programSize = (buffer.size + pageSize - 1)
/ pageSize * pageSize;
int retval = arm_uc_flashiap_program(buffer.ptr,
app_start_addr + offset,
programSize);
result = (retval == ARM_UC_FLASHIAP_SUCCESS);
offset += programSize;
} else {
boot_debug("[DBG ] ARM_UCP_Read returned 0 bytes\r\n");
/* set error and break out of loop */
result = false;
break;
}
}
}
return result;
}
/*
* Copy loop to update the application
*/
bool copyStoredApplication(uint32_t index,
arm_uc_firmware_details_t *details)
{
bool result = false;
/*************************************************************************/
/* Step 1. Erase active application */
/*************************************************************************/
boot_debug("[DBG ] Erase active application\r\n");
result = eraseActiveFirmware(details->size);
/*************************************************************************/
/* Step 2. Write header */
/*************************************************************************/
if (result) {
boot_debug("[DBG ] Write header\r\n");
result = writeActiveFirmwareHeader(details);
}
/*************************************************************************/
/* Step 3. Copy application */
/*************************************************************************/
if (result) {
boot_debug("[DBG ] Copy application\r\n");
result = writeActiveFirmware(index, details);
}
/*************************************************************************/
/* Step 4. Verify application */
/*************************************************************************/
if (result) {
boot_debug("[DBG ] Verify application\r\n");
int recheck = checkActiveApplication(details);
result = (recheck == RESULT_SUCCESS);
}
return result;
}