/
nrf53_hooks.c
128 lines (108 loc) · 3.01 KB
/
nrf53_hooks.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
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <assert.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/flash/flash_simulator.h>
#include "bootutil/image.h"
#include "bootutil/bootutil.h"
#include "bootutil/fault_injection_hardening.h"
#include "flash_map_backend/flash_map_backend.h"
#define NET_CORE_SECONDARY_SLOT 1
#define NET_CORE_VIRTUAL_PRIMARY_SLOT 3
#include <dfu/pcd.h>
int boot_read_image_header_hook(int img_index, int slot,
struct image_header *img_head)
{
if (img_index == 1 && slot == 0) {
img_head->ih_magic = IMAGE_MAGIC;
img_head->ih_hdr_size = PM_MCUBOOT_PAD_SIZE;
img_head->ih_load_addr = PM_MCUBOOT_PRIMARY_1_ADDRESS;
img_head->ih_img_size = PM_CPUNET_APP_SIZE;
img_head->ih_flags = 0;
img_head->ih_ver.iv_major = 0;
img_head->ih_ver.iv_minor = 0;
img_head->ih_ver.iv_revision = 0;
img_head->ih_ver.iv_build_num = 0;
img_head->_pad1 = 0;
return 0;
}
return BOOT_HOOK_REGULAR;
}
fih_int boot_image_check_hook(int img_index, int slot)
{
if (img_index == 1 && slot == 0) {
FIH_RET(FIH_SUCCESS);
}
FIH_RET(fih_int_encode(BOOT_HOOK_REGULAR));
}
int boot_perform_update_hook(int img_index, struct image_header *img_head,
const struct flash_area *area)
{
return BOOT_HOOK_REGULAR;
}
int boot_read_swap_state_primary_slot_hook(int image_index,
struct boot_swap_state *state)
{
if (image_index == 1) {
/* Populate with fake data */
state->magic = BOOT_MAGIC_UNSET;
state->swap_type = BOOT_SWAP_TYPE_NONE;
state->image_num = image_index;
state->copy_done = BOOT_FLAG_UNSET;
state->image_ok = BOOT_FLAG_UNSET;
/*
* Skip more handling of the primary slot for Image 1 as the slot
* exsists in RAM and is empty.
*/
return 0;
}
return BOOT_HOOK_REGULAR;
}
int network_core_update(bool wait)
{
struct image_header *hdr;
static const struct device *mock_flash_dev;
void *mock_flash;
size_t mock_size;
mock_flash_dev = device_get_binding(PM_MCUBOOT_PRIMARY_1_DEV_NAME);
if (mock_flash_dev == NULL) {
return -ENODEV;
}
mock_flash = flash_simulator_get_memory(NULL, &mock_size);
hdr = (struct image_header *) mock_flash;
if (hdr->ih_magic == IMAGE_MAGIC) {
uint32_t fw_size = hdr->ih_img_size;
uint32_t vtable_addr = (uint32_t)hdr + hdr->ih_hdr_size;
uint32_t *vtable = (uint32_t *)(vtable_addr);
uint32_t reset_addr = vtable[1];
if (reset_addr > PM_CPUNET_B0N_ADDRESS) {
if (wait) {
return pcd_network_core_update(vtable, fw_size);
} else {
return pcd_network_core_update_initiate(vtable, fw_size);
}
}
}
/* No IMAGE_MAGIC no valid image */
return -ENODATA;
}
int boot_copy_region_post_hook(int img_index, const struct flash_area *area,
size_t size)
{
if (img_index == NET_CORE_SECONDARY_SLOT) {
return network_core_update(true);
}
return 0;
}
int boot_serial_uploaded_hook(int img_index, const struct flash_area *area,
size_t size)
{
if (img_index == NET_CORE_VIRTUAL_PRIMARY_SLOT) {
return network_core_update(false);
}
return 0;
}