blob: 274e71afe3582fe3df963deb4174eaf47f89b170 [file] [log] [blame]
Julian Halle35efa52022-10-31 16:34:58 +00001/*
2 * Copyright (c) 2022, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef FW_UPDATE_AGENT_H
9#define FW_UPDATE_AGENT_H
10
11#include <stdbool.h>
12#include <stdint.h>
13#include <common/uuid/uuid.h>
14#include <service/fwu/inspector/fw_inspector.h>
15#include "fw_directory.h"
16#include "stream_manager.h"
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22/**
23 * Interface dependencies
24 */
25struct fw_store;
26
27/**
28 * \brief Update process states
29 *
30 * The update_agent is responsible for ensuring that only a valid update flow
31 * is followed by a client. To enforce the flow, public operations can only be
32 * used in a valid state that reflects the FWU-A behavioral model.
33 */
34enum fwu_state {
35 FWU_STATE_DEINITIALZED,
36 FWU_STATE_INITIALIZING,
37 FWU_STATE_REGULAR,
38 FWU_STATE_STAGING,
39 FWU_STATE_TRIAL_PENDING,
40 FWU_STATE_TRIAL
41};
42
43/**
44 * \brief update_agent structure definition
45 *
46 * An update_agent instance is responsible for coordinating firmware updates applied
47 * to a fw_store. An update_agent performs a security role by enforcing that a
48 * valid flow is performed to update the fw store.
49 */
50struct update_agent {
51 enum fwu_state state;
52 fw_inspector_inspect fw_inspect_method;
53 struct fw_store *fw_store;
54 struct fw_directory fw_directory;
55 struct stream_manager stream_manager;
56 uint8_t *image_dir_buf;
57 size_t image_dir_buf_size;
58};
59
60/**
61 * \brief Initialise the update_agent
62 *
63 * \param[in] update_agent The subject update_agent
64 * \param[in] boot_index The boot_index used by the bootloader
65 * \param[in] fw_inspect_method fw_inspector inspect method
66 * \param[in] fw_store The fw_store to manage
67 *
68 * \return Status (0 for success). Uses fwu protocol status codes.
69 */
70int update_agent_init(
71 struct update_agent *update_agent,
72 unsigned int boot_index,
73 fw_inspector_inspect fw_inspect_method,
74 struct fw_store *fw_store);
75
76/**
77 * \brief De-initialise the update_agent
78 *
79 * \param[in] update_agent The subject update_agent
80 */
81void update_agent_deinit(
82 struct update_agent *update_agent);
83
84/**
85 * \brief Begin staging
86 *
87 * \param[in] update_agent The subject update_agent
88 *
89 * \return 0 on successfully transitioning to the STAGING state
90 */
91int update_agent_begin_staging(
92 struct update_agent *update_agent);
93
94/**
95 * \brief End staging
96 *
97 * \param[in] update_agent The subject update_agent
98 *
99 * \return 0 on successfully transitioning to the TRIAL state
100 */
101int update_agent_end_staging(
102 struct update_agent *update_agent);
103
104/**
105 * \brief Cancel staging
106 *
107 * \param[in] update_agent The subject update_agent
108 *
109 * \return 0 on successfully transitioning to the REGULAR state
110 */
111int update_agent_cancel_staging(
112 struct update_agent *update_agent);
113
114/**
115 * \brief Accept an updated image
116 *
117 * \param[in] update_agent The subject update_agent
118 * \param[in] image_type_uuid Identifies the image to accept
119 *
120 * \return Status (0 on success)
121 */
122int update_agent_accept(
123 struct update_agent *update_agent,
124 const struct uuid_octets *image_type_uuid);
125
126/**
127 * \brief Select previous version
128 *
129 * Revert to a previous good version (if possible).
130 *
131 * \param[in] update_agent The subject update_agent
132 *
133 * \return Status (0 on success)
134 */
135int update_agent_select_previous(
136 struct update_agent *update_agent);
137
138/**
139 * \brief Open a stream for accessing an fwu stream
140 *
141 * Used for reading or writing data for accessing images or other fwu
142 * related objects.
143 *
144 * \param[in] update_agent The subject update_agent
145 * \param[in] uuid Identifies the object to access
146 * \param[out] handle For subsequent read/write operations
147 *
148 * \return Status (0 on success)
149 */
150int update_agent_open(
151 struct update_agent *update_agent,
152 const struct uuid_octets *uuid,
153 uint32_t *handle);
154
155/**
156 * \brief Close a stream and commit any writes to the stream
157 *
158 * \param[in] update_agent The subject update_agent
159 * \param[in] handle The handle returned by open
160 * \param[in] accepted Initial accepted state of an image
161 *
162 * \return Status (0 on success)
163 */
164int update_agent_commit(
165 struct update_agent *update_agent,
166 uint32_t handle,
167 bool accepted);
168
169/**
170 * \brief Write to a previously opened stream
171 *
172 * \param[in] update_agent The subject update_agent
173 * \param[in] handle The handle returned by open
174 * \param[in] data Pointer to data
175 * \param[in] data_len The data length
176 *
177 * \return Status (0 on success)
178 */
179int update_agent_write_stream(
180 struct update_agent *update_agent,
181 uint32_t handle,
182 const uint8_t *data,
183 size_t data_len);
184
185/**
186 * \brief Read from a previously opened stream
187 *
188 * \param[in] update_agent The subject update_agent
189 * \param[in] handle The handle returned by open
190 * \param[in] buf Pointer to buffer to copy to
191 * \param[in] buf_size The size of the buffer
192 * \param[out] read_len The length of data read
193 * \param[out] total_len The total length of the object to read
194 *
195 * \return Status (0 on success)
196 */
197int update_agent_read_stream(
198 struct update_agent *update_agent,
199 uint32_t handle,
200 uint8_t *buf,
201 size_t buf_size,
202 size_t *read_len,
203 size_t *total_len);
204
205
206#ifdef __cplusplus
207}
208#endif
209
210#endif /* FW_UPDATE_AGENT_H */