blob: 11570a1a713ae9df830159fe30d9f9dcc972ba65 [file] [log] [blame]
Tom Josephef90b0d2021-08-17 07:12:49 -07001#pragma once
2
3#include "common/types.hpp"
4#include "requester/handler.hpp"
5#include "requester/request.hpp"
6
7#include <sdeventplus/event.hpp>
8#include <sdeventplus/source/event.hpp>
9
10#include <fstream>
11
12namespace pldm
13{
14
15namespace fw_update
16{
17
P Arun Kumar Reddyd1c955b2025-04-15 16:06:06 +053018/** @brief Type alias for component update status tracking
19 * Maps component index to its update completion status (true indicates
20 * successful completion, false indicates cancellation)
21 */
22using ComponentUpdateStatusMap = std::map<size_t, bool>;
23
Tom Josephef90b0d2021-08-17 07:12:49 -070024class UpdateManager;
25
26/** @class DeviceUpdater
27 *
28 * DeviceUpdater orchestrates the firmware update of the firmware device and
29 * updates the UpdateManager about the status once it is complete.
30 */
31class DeviceUpdater
32{
33 public:
34 DeviceUpdater() = delete;
35 DeviceUpdater(const DeviceUpdater&) = delete;
36 DeviceUpdater(DeviceUpdater&&) = default;
37 DeviceUpdater& operator=(const DeviceUpdater&) = delete;
Pavithra Barithayaa7dbca52023-07-07 04:19:37 -050038 DeviceUpdater& operator=(DeviceUpdater&&) = delete;
Tom Josephef90b0d2021-08-17 07:12:49 -070039 ~DeviceUpdater() = default;
40
41 /** @brief Constructor
42 *
43 * @param[in] eid - Endpoint ID of the firmware device
Tom Josephef90b0d2021-08-17 07:12:49 -070044 * @param[in] package - File stream for firmware update package
45 * @param[in] fwDeviceIDRecord - FirmwareDeviceIDRecord in the fw update
46 * package that matches this firmware device
47 * @param[in] compImageInfos - Component image information for all the
48 * components in the fw update package
49 * @param[in] compInfo - Component info for the components in this FD
50 * derived from GetFirmwareParameters response
51 * @param[in] maxTransferSize - Maximum size in bytes of the variable
52 * payload allowed to be requested by the FD
53 * @param[in] updateManager - To update the status of fw update of the
54 * device
55 */
Tom Josephb7e083e2021-10-26 15:10:03 +053056 explicit DeviceUpdater(mctp_eid_t eid, std::ifstream& package,
57 const FirmwareDeviceIDRecord& fwDeviceIDRecord,
58 const ComponentImageInfos& compImageInfos,
59 const ComponentInfo& compInfo,
60 uint32_t maxTransferSize,
61 UpdateManager* updateManager) :
Patrick Williams16c2a0a2024-08-16 15:20:59 -040062 eid(eid), package(package), fwDeviceIDRecord(fwDeviceIDRecord),
Tom Josephb7e083e2021-10-26 15:10:03 +053063 compImageInfos(compImageInfos), compInfo(compInfo),
64 maxTransferSize(maxTransferSize), updateManager(updateManager)
Tom Josephef90b0d2021-08-17 07:12:49 -070065 {}
66
67 /** @brief Start the firmware update flow for the FD
68 *
69 * To start the update flow RequestUpdate command is sent to the FD.
70 *
71 */
72 void startFwUpdateFlow();
73
74 /** @brief Handler for RequestUpdate command response
75 *
76 * The response of the RequestUpdate is processed and if the response
77 * is success, send PassComponentTable request to FD.
78 *
79 * @param[in] eid - Remote MCTP endpoint
80 * @param[in] response - PLDM response message
81 * @param[in] respMsgLen - Response message length
82 */
83 void requestUpdate(mctp_eid_t eid, const pldm_msg* response,
84 size_t respMsgLen);
85
86 /** @brief Handler for PassComponentTable command response
87 *
88 * The response of the PassComponentTable is processed. If the response
89 * indicates component can be updated, continue with either a) or b).
90 *
91 * a. Send PassComponentTable request for the next component if
92 * applicable
93 * b. UpdateComponent command to request updating a specific
94 * firmware component
95 *
96 * If the response indicates component may be updateable, continue
97 * based on the policy in DeviceUpdateOptionFlags.
98 *
99 * @param[in] eid - Remote MCTP endpoint
100 * @param[in] response - PLDM response message
101 * @param[in] respMsgLen - Response message length
102 */
103 void passCompTable(mctp_eid_t eid, const pldm_msg* response,
104 size_t respMsgLen);
105
106 /** @brief Handler for UpdateComponent command response
107 *
108 * The response of the UpdateComponent is processed and will wait for
109 * FD to request the firmware data.
110 *
111 * @param[in] eid - Remote MCTP endpoint
112 * @param[in] response - PLDM response message
113 * @param[in] respMsgLen - Response message length
114 */
115 void updateComponent(mctp_eid_t eid, const pldm_msg* response,
116 size_t respMsgLen);
117
118 /** @brief Handler for RequestFirmwareData request
119 *
120 * @param[in] request - Request message
121 * @param[in] payload_length - Request message payload length
122 * @return Response - PLDM Response message
123 */
124 Response requestFwData(const pldm_msg* request, size_t payloadLength);
125
126 /** @brief Handler for TransferComplete request
127 *
128 * @param[in] request - Request message
129 * @param[in] payload_length - Request message payload length
130 * @return Response - PLDM Response message
131 */
132 Response transferComplete(const pldm_msg* request, size_t payloadLength);
133
134 /** @brief Handler for VerifyComplete request
135 *
136 * @param[in] request - Request message
137 * @param[in] payload_length - Request message payload length
138 * @return Response - PLDM Response message
139 */
140 Response verifyComplete(const pldm_msg* request, size_t payloadLength);
141
142 /** @brief Handler for ApplyComplete request
143 *
144 * @param[in] request - Request message
145 * @param[in] payload_length - Request message payload length
146 * @return Response - PLDM Response message
147 */
148 Response applyComplete(const pldm_msg* request, size_t payloadLength);
149
150 /** @brief Handler for ActivateFirmware command response
151 *
152 * The response of the ActivateFirmware is processed and will update the
153 * UpdateManager with the completion of the firmware update.
154 *
155 * @param[in] eid - Remote MCTP endpoint
156 * @param[in] response - PLDM response message
157 * @param[in] respMsgLen - Response message length
158 */
159 void activateFirmware(mctp_eid_t eid, const pldm_msg* response,
160 size_t respMsgLen);
P Arun Kumar Reddyd1c955b2025-04-15 16:06:06 +0530161 /**
162 * @brief Handler for CancelUpdateComponent command response
163 *
164 * @param[in] eid - Remote MCTP endpoint
165 * @param[in] response - PLDM Response message
166 * @param[in] respMsgLen - Response message length
167 */
168 void cancelUpdateComponent(mctp_eid_t eid, const pldm_msg* response,
169 size_t respMsgLen);
Tom Josephef90b0d2021-08-17 07:12:49 -0700170
171 private:
172 /** @brief Send PassComponentTable command request
173 *
174 * @param[in] compOffset - component offset in compImageInfos
175 */
176 void sendPassCompTableRequest(size_t offset);
177
178 /** @brief Send UpdateComponent command request
179 *
180 * @param[in] compOffset - component offset in compImageInfos
181 */
182 void sendUpdateComponentRequest(size_t offset);
183
184 /** @brief Send ActivateFirmware command request */
185 void sendActivateFirmwareRequest();
186
P Arun Kumar Reddyd1c955b2025-04-15 16:06:06 +0530187 /**
188 * @brief Send cancel update component request
189 */
190 void sendCancelUpdateComponentRequest();
191
Tom Josephef90b0d2021-08-17 07:12:49 -0700192 /** @brief Endpoint ID of the firmware device */
193 mctp_eid_t eid;
194
Tom Josephef90b0d2021-08-17 07:12:49 -0700195 /** @brief File stream for firmware update package */
196 std::ifstream& package;
197
198 /** @brief FirmwareDeviceIDRecord in the fw update package that matches this
199 * firmware device
200 */
201 const FirmwareDeviceIDRecord& fwDeviceIDRecord;
202
203 /** @brief Component image information for all the components in the fw
204 * update package
205 */
206 const ComponentImageInfos& compImageInfos;
207
208 /** @brief Component info for the components in this FD derived from
209 * GetFirmwareParameters response
210 */
211 const ComponentInfo& compInfo;
212
213 /** @brief Maximum size in bytes of the variable payload to be requested by
214 * the FD via RequestFirmwareData command
215 */
216 uint32_t maxTransferSize;
217
218 /** @brief To update the status of fw update of the FD */
219 UpdateManager* updateManager;
220
221 /** @brief Component index is used to track the current component being
222 * updated if multiple components are applicable for the FD.
223 * It is also used to keep track of the next component in
224 * PassComponentTable
225 */
226 size_t componentIndex = 0;
227
228 /** @brief To send a PLDM request after the current command handling */
229 std::unique_ptr<sdeventplus::source::Defer> pldmRequest;
P Arun Kumar Reddyd1c955b2025-04-15 16:06:06 +0530230
231 /**
232 * @brief Map to hold component update status. True - success, False -
233 * cancelled
234 */
235 ComponentUpdateStatusMap componentUpdateStatus;
Tom Josephef90b0d2021-08-17 07:12:49 -0700236};
237
238} // namespace fw_update
239
Patrick Williams6da4f912023-05-10 07:50:53 -0500240} // namespace pldm