blob: ea214ee00a6b05b726c6fc89602aa12cba52574e [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>
Gyorgy Szing3c446242023-03-31 01:53:15 +020013
14#include "common/uuid/uuid.h"
Julian Halle35efa52022-10-31 16:34:58 +000015#include "fw_directory.h"
Gyorgy Szing3c446242023-03-31 01:53:15 +020016#include "service/fwu/inspector/fw_inspector.h"
Julian Halle35efa52022-10-31 16:34:58 +000017#include "stream_manager.h"
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23/**
24 * Interface dependencies
25 */
26struct fw_store;
27
28/**
29 * \brief Update process states
30 *
31 * The update_agent is responsible for ensuring that only a valid update flow
32 * is followed by a client. To enforce the flow, public operations can only be
33 * used in a valid state that reflects the FWU-A behavioral model.
34 */
35enum fwu_state {
36 FWU_STATE_DEINITIALZED,
37 FWU_STATE_INITIALIZING,
38 FWU_STATE_REGULAR,
39 FWU_STATE_STAGING,
40 FWU_STATE_TRIAL_PENDING,
41 FWU_STATE_TRIAL
42};
43
44/**
45 * \brief update_agent structure definition
46 *
47 * An update_agent instance is responsible for coordinating firmware updates applied
48 * to a fw_store. An update_agent performs a security role by enforcing that a
49 * valid flow is performed to update the fw store.
50 */
51struct update_agent {
52 enum fwu_state state;
53 fw_inspector_inspect fw_inspect_method;
54 struct fw_store *fw_store;
55 struct fw_directory fw_directory;
56 struct stream_manager stream_manager;
57 uint8_t *image_dir_buf;
58 size_t image_dir_buf_size;
59};
60
61/**
62 * \brief Initialise the update_agent
63 *
64 * \param[in] update_agent The subject update_agent
65 * \param[in] boot_index The boot_index used by the bootloader
66 * \param[in] fw_inspect_method fw_inspector inspect method
67 * \param[in] fw_store The fw_store to manage
68 *
69 * \return Status (0 for success). Uses fwu protocol status codes.
70 */
Gyorgy Szing3c446242023-03-31 01:53:15 +020071int update_agent_init(struct update_agent *update_agent, unsigned int boot_index,
72 fw_inspector_inspect fw_inspect_method, struct fw_store *fw_store);
Julian Halle35efa52022-10-31 16:34:58 +000073
74/**
75 * \brief De-initialise the update_agent
76 *
77 * \param[in] update_agent The subject update_agent
78 */
Gyorgy Szing3c446242023-03-31 01:53:15 +020079void update_agent_deinit(struct update_agent *update_agent);
Julian Halle35efa52022-10-31 16:34:58 +000080
81/**
82 * \brief Begin staging
83 *
84 * \param[in] update_agent The subject update_agent
85 *
86 * \return 0 on successfully transitioning to the STAGING state
87 */
Gyorgy Szing3c446242023-03-31 01:53:15 +020088int update_agent_begin_staging(struct update_agent *update_agent);
Julian Halle35efa52022-10-31 16:34:58 +000089
90/**
91 * \brief End staging
92 *
93 * \param[in] update_agent The subject update_agent
94 *
95 * \return 0 on successfully transitioning to the TRIAL state
96 */
Gyorgy Szing3c446242023-03-31 01:53:15 +020097int update_agent_end_staging(struct update_agent *update_agent);
Julian Halle35efa52022-10-31 16:34:58 +000098
99/**
100 * \brief Cancel staging
101 *
102 * \param[in] update_agent The subject update_agent
103 *
104 * \return 0 on successfully transitioning to the REGULAR state
105 */
Gyorgy Szing3c446242023-03-31 01:53:15 +0200106int update_agent_cancel_staging(struct update_agent *update_agent);
Julian Halle35efa52022-10-31 16:34:58 +0000107
108/**
109 * \brief Accept an updated image
110 *
111 * \param[in] update_agent The subject update_agent
112 * \param[in] image_type_uuid Identifies the image to accept
113 *
114 * \return Status (0 on success)
115 */
Gyorgy Szing3c446242023-03-31 01:53:15 +0200116int update_agent_accept(struct update_agent *update_agent,
117 const struct uuid_octets *image_type_uuid);
Julian Halle35efa52022-10-31 16:34:58 +0000118
119/**
120 * \brief Select previous version
121 *
122 * Revert to a previous good version (if possible).
123 *
124 * \param[in] update_agent The subject update_agent
125 *
126 * \return Status (0 on success)
127 */
Gyorgy Szing3c446242023-03-31 01:53:15 +0200128int update_agent_select_previous(struct update_agent *update_agent);
Julian Halle35efa52022-10-31 16:34:58 +0000129
130/**
131 * \brief Open a stream for accessing an fwu stream
132 *
133 * Used for reading or writing data for accessing images or other fwu
134 * related objects.
135 *
136 * \param[in] update_agent The subject update_agent
137 * \param[in] uuid Identifies the object to access
138 * \param[out] handle For subsequent read/write operations
139 *
140 * \return Status (0 on success)
141 */
Gyorgy Szing3c446242023-03-31 01:53:15 +0200142int update_agent_open(struct update_agent *update_agent, const struct uuid_octets *uuid,
143 uint32_t *handle);
Julian Halle35efa52022-10-31 16:34:58 +0000144
145/**
146 * \brief Close a stream and commit any writes to the stream
147 *
148 * \param[in] update_agent The subject update_agent
149 * \param[in] handle The handle returned by open
150 * \param[in] accepted Initial accepted state of an image
151 *
152 * \return Status (0 on success)
153 */
Gyorgy Szing3c446242023-03-31 01:53:15 +0200154int update_agent_commit(struct update_agent *update_agent, uint32_t handle, bool accepted);
Julian Halle35efa52022-10-31 16:34:58 +0000155
156/**
157 * \brief Write to a previously opened stream
158 *
159 * \param[in] update_agent The subject update_agent
160 * \param[in] handle The handle returned by open
161 * \param[in] data Pointer to data
162 * \param[in] data_len The data length
163 *
164 * \return Status (0 on success)
165 */
Gyorgy Szing3c446242023-03-31 01:53:15 +0200166int update_agent_write_stream(struct update_agent *update_agent, uint32_t handle,
167 const uint8_t *data, size_t data_len);
Julian Halle35efa52022-10-31 16:34:58 +0000168
169/**
170 * \brief Read from 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] buf Pointer to buffer to copy to
175 * \param[in] buf_size The size of the buffer
176 * \param[out] read_len The length of data read
177 * \param[out] total_len The total length of the object to read
178 *
179 * \return Status (0 on success)
180 */
Gyorgy Szing3c446242023-03-31 01:53:15 +0200181int update_agent_read_stream(struct update_agent *update_agent, uint32_t handle, uint8_t *buf,
182 size_t buf_size, size_t *read_len, size_t *total_len);
Julian Halle35efa52022-10-31 16:34:58 +0000183
184#ifdef __cplusplus
185}
186#endif
187
188#endif /* FW_UPDATE_AGENT_H */