Julian Hall | e35efa5 | 2022-10-31 16:34:58 +0000 | [diff] [blame] | 1 | /* |
| 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 Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame^] | 13 | |
| 14 | #include "common/uuid/uuid.h" |
Julian Hall | e35efa5 | 2022-10-31 16:34:58 +0000 | [diff] [blame] | 15 | #include "fw_directory.h" |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame^] | 16 | #include "service/fwu/inspector/fw_inspector.h" |
Julian Hall | e35efa5 | 2022-10-31 16:34:58 +0000 | [diff] [blame] | 17 | #include "stream_manager.h" |
| 18 | |
| 19 | #ifdef __cplusplus |
| 20 | extern "C" { |
| 21 | #endif |
| 22 | |
| 23 | /** |
| 24 | * Interface dependencies |
| 25 | */ |
| 26 | struct 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 | */ |
| 35 | enum 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 | */ |
| 51 | struct 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 Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame^] | 71 | int 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 Hall | e35efa5 | 2022-10-31 16:34:58 +0000 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * \brief De-initialise the update_agent |
| 76 | * |
| 77 | * \param[in] update_agent The subject update_agent |
| 78 | */ |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame^] | 79 | void update_agent_deinit(struct update_agent *update_agent); |
Julian Hall | e35efa5 | 2022-10-31 16:34:58 +0000 | [diff] [blame] | 80 | |
| 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 Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame^] | 88 | int update_agent_begin_staging(struct update_agent *update_agent); |
Julian Hall | e35efa5 | 2022-10-31 16:34:58 +0000 | [diff] [blame] | 89 | |
| 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 Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame^] | 97 | int update_agent_end_staging(struct update_agent *update_agent); |
Julian Hall | e35efa5 | 2022-10-31 16:34:58 +0000 | [diff] [blame] | 98 | |
| 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 Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame^] | 106 | int update_agent_cancel_staging(struct update_agent *update_agent); |
Julian Hall | e35efa5 | 2022-10-31 16:34:58 +0000 | [diff] [blame] | 107 | |
| 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 Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame^] | 116 | int update_agent_accept(struct update_agent *update_agent, |
| 117 | const struct uuid_octets *image_type_uuid); |
Julian Hall | e35efa5 | 2022-10-31 16:34:58 +0000 | [diff] [blame] | 118 | |
| 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 Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame^] | 128 | int update_agent_select_previous(struct update_agent *update_agent); |
Julian Hall | e35efa5 | 2022-10-31 16:34:58 +0000 | [diff] [blame] | 129 | |
| 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 Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame^] | 142 | int update_agent_open(struct update_agent *update_agent, const struct uuid_octets *uuid, |
| 143 | uint32_t *handle); |
Julian Hall | e35efa5 | 2022-10-31 16:34:58 +0000 | [diff] [blame] | 144 | |
| 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 Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame^] | 154 | int update_agent_commit(struct update_agent *update_agent, uint32_t handle, bool accepted); |
Julian Hall | e35efa5 | 2022-10-31 16:34:58 +0000 | [diff] [blame] | 155 | |
| 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 Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame^] | 166 | int update_agent_write_stream(struct update_agent *update_agent, uint32_t handle, |
| 167 | const uint8_t *data, size_t data_len); |
Julian Hall | e35efa5 | 2022-10-31 16:34:58 +0000 | [diff] [blame] | 168 | |
| 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 Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame^] | 181 | int 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 Hall | e35efa5 | 2022-10-31 16:34:58 +0000 | [diff] [blame] | 183 | |
| 184 | #ifdef __cplusplus |
| 185 | } |
| 186 | #endif |
| 187 | |
| 188 | #endif /* FW_UPDATE_AGENT_H */ |