blob: 016ec509406f3c638592f38fc9b7370adc81b4f9 [file] [log] [blame]
Julian Hallf31da8e2023-01-17 10:10:29 +00001// SPDX-License-Identifier: BSD-3-Clause
2/*
3 * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
4 */
5
6#include <stddef.h>
7#include <sp_api.h>
8#include <sp_discovery.h>
9#include <sp_messaging.h>
10#include <sp_rxtx.h>
11#include <trace.h>
12#include <rpc/ffarpc/endpoint/ffarpc_call_ep.h>
13#include <protocols/rpc/common/packed-c/status.h>
14#include <config/ramstore/config_ramstore.h>
15#include <config/loader/sp/sp_config_loader.h>
16#include <media/volume/factory/volume_factory.h>
17#include <service/fwu/config/fwu_configure.h>
18#include <service/discovery/provider/discovery_provider.h>
19#include <service/discovery/provider/serializer/packed-c/packedc_discovery_provider_serializer.h>
20#include <service/fwu/provider/fwu_provider.h>
21#include <service/fwu/provider/serializer/packed-c/packedc_fwu_provider_serializer.h>
22#include <service/fwu/fw_store/banked/bank_scheme.h>
23#include <service/fwu/fw_store/banked/metadata_serializer/v1/metadata_serializer_v1.h>
24#include <service/fwu/fw_store/banked/metadata_serializer/v2/metadata_serializer_v2.h>
25#include <service/fwu/inspector/direct/direct_fw_inspector.h>
26#include <service/fwu/agent/update_agent.h>
27#include <service/fwu/fw_store/banked/banked_fw_store.h>
28
29
30/* Set default limit on the number of storage devices to update */
31#ifndef FWU_SP_MAX_STORAGE_DEVICES
32#define FWU_SP_MAX_STORAGE_DEVICES (1)
33#endif
34
35/* Parameters that should be passed forward by the bootloader */
36#define HARD_CODED_BOOT_INDEX (0)
37#define HARD_CODED_METADATA_VER (2)
38
39
40static bool sp_init(uint16_t *own_sp_id);
41static bool configure_for_platform(void);
42const struct metadata_serializer *select_metadata_serializer(unsigned int version);
43
44void __noreturn sp_main(struct ffa_init_info *init_info)
45{
46 struct ffa_call_ep ffarpc_call_ep = { 0 };
47 struct fwu_provider service_provider = { 0 };
48 struct rpc_interface *service_iface = NULL;
49 struct update_agent update_agent = { 0 };
50 struct fw_store fw_store = { 0 };
51 struct sp_msg req_msg = { 0 };
52 struct sp_msg resp_msg = { 0 };
53 uint16_t own_id = 0;
54 sp_result result = SP_RESULT_INTERNAL_ERROR;
55
56 /* Boot phase */
57 if (!sp_init(&own_id)) {
58 EMSG("Failed to init SP");
59 goto fatal_error;
60 }
61
62 config_ramstore_init();
63
64 if (!sp_config_load(init_info)) {
65 EMSG("Failed to load SP config");
66 goto fatal_error;
67 }
68
69 /* Configuration - discovers required volumes and installers */
70 if (!configure_for_platform()) {
71 EMSG("Failed to configure for platform");
72 goto fatal_error;
73 }
74
75 /* Select FWU metadata serializer for compatibility with bootloader */
76 const struct metadata_serializer *serializer =
77 select_metadata_serializer(HARD_CODED_METADATA_VER);
78
79 if (!serializer) {
80 EMSG("Unsupported FWU metadata version");
81 goto fatal_error;
82 }
83
84 /* Initialise fw store */
85 if (banked_fw_store_init(&fw_store, serializer)) {
86 EMSG("Failed to init fw store");
87 goto fatal_error;
88 }
89
90 if (update_agent_init(&update_agent, HARD_CODED_BOOT_INDEX,
91 direct_fw_inspector_inspect, &fw_store)) {
92 EMSG("Failed to init update agent");
93 goto fatal_error;
94 }
95
96 /* Initialise the FWU service provider */
97 service_iface = fwu_provider_init(
98 &service_provider,
99 &update_agent);
100
101 if (!service_iface) {
102 EMSG("Failed to init service provider");
103 goto fatal_error;
104 }
105
106 fwu_provider_register_serializer(
107 &service_provider,
108 TS_RPC_ENCODING_PACKED_C,
109 packedc_fwu_provider_serializer_instance());
110
111 discovery_provider_register_serializer(
112 &service_provider.discovery_provider,
113 TS_RPC_ENCODING_PACKED_C,
114 packedc_discovery_provider_serializer_instance());
115
116 /* Associate service interface with FFA call endpoint */
117 ffa_call_ep_init(&ffarpc_call_ep, service_iface, own_id);
118
119 /* End of boot phase */
120 result = sp_msg_wait(&req_msg);
121 if (result != SP_RESULT_OK) {
122 EMSG("Failed to send message wait %d", result);
123 goto fatal_error;
124 }
125
126 while (1) {
127 ffa_call_ep_receive(&ffarpc_call_ep, &req_msg, &resp_msg);
128
129 result = sp_msg_send_direct_resp(&resp_msg, &req_msg);
130 if (result != SP_RESULT_OK) {
131 EMSG("Failed to send direct response %d", result);
132 result = sp_msg_wait(&req_msg);
133 if (result != SP_RESULT_OK) {
134 EMSG("Failed to send message wait %d", result);
135 goto fatal_error;
136 }
137 }
138 }
139
140fatal_error:
141 /* SP is not viable */
142 EMSG("FWU SP error");
143 while (1) {}
144}
145
146void sp_interrupt_handler(uint32_t interrupt_id)
147{
148 (void)interrupt_id;
149}
150
151static bool sp_init(uint16_t *own_id)
152{
153 sp_result sp_res = SP_RESULT_INTERNAL_ERROR;
154 static uint8_t tx_buffer[4096] __aligned(4096);
155 static uint8_t rx_buffer[4096] __aligned(4096);
156
157 sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
158 if (sp_res != SP_RESULT_OK) {
159 EMSG("Failed to map RXTX buffers: %d", sp_res);
160 return false;
161 }
162
163 sp_res = sp_discovery_own_id_get(own_id);
164 if (sp_res != SP_RESULT_OK) {
165 EMSG("Failed to query own ID: %d", sp_res);
166 return false;
167 }
168
169 return true;
170}
171
172static bool configure_for_platform(void)
173{
174 struct uuid_octets device_uuids[FWU_SP_MAX_STORAGE_DEVICES];
175 size_t num_storage_devices = 0;
176
177 int status = volume_factory_init(device_uuids,
178 FWU_SP_MAX_STORAGE_DEVICES, &num_storage_devices);
179
180 if (status) {
181
182 EMSG("Failed to init volume factory: %d", status);
183 return false;
184 }
185
186 status = fwu_configure(device_uuids, num_storage_devices);
187
188 if (status) {
189
190 EMSG("Failed to setup FWU configuration: %d", status);
191 return false;
192 }
193
194 return true;
195}
196
197const struct metadata_serializer *select_metadata_serializer(unsigned int version)
198{
199 if (version == 1)
200 return metadata_serializer_v1();
201
202 if (version == 2)
203 return metadata_serializer_v2();
204
205 return NULL;
206}