blob: 2030a6d10e23791a7e53c253c4f4299ebfb75003 [file] [log] [blame]
gokulsanker138ceba2021-04-05 13:25:25 +05301#include "firmware_update.h"
gokulsankereca3e192021-04-05 14:57:41 +05302#include <endian.h>
gokulsankere1fb7a82021-04-05 16:09:29 +05303#include <string.h>
gokulsanker138ceba2021-04-05 13:25:25 +05304
Tom Joseph763b51e2021-06-05 04:50:47 -07005/** @brief Check whether string type value is valid
6 *
7 * @return true if string type value is valid, false if not
8 */
9static bool is_string_type_valid(uint8_t string_type)
10{
11 switch (string_type) {
12 case PLDM_STR_TYPE_UNKNOWN:
13 case PLDM_STR_TYPE_ASCII:
14 case PLDM_STR_TYPE_UTF_8:
15 case PLDM_STR_TYPE_UTF_16:
16 case PLDM_STR_TYPE_UTF_16LE:
17 case PLDM_STR_TYPE_UTF_16BE:
18 return true;
19 default:
20 return false;
21 }
22}
23
24/** @brief Return the length of the descriptor type described in firmware update
25 * specification
26 *
27 * @return length of the descriptor type if descriptor type is valid else
28 * return 0
29 */
30static uint16_t get_descriptor_type_length(uint16_t descriptor_type)
31{
32 switch (descriptor_type) {
33
34 case PLDM_FWUP_PCI_VENDOR_ID:
35 return PLDM_FWUP_PCI_VENDOR_ID_LENGTH;
36 case PLDM_FWUP_IANA_ENTERPRISE_ID:
37 return PLDM_FWUP_IANA_ENTERPRISE_ID_LENGTH;
38 case PLDM_FWUP_UUID:
39 return PLDM_FWUP_UUID_LENGTH;
40 case PLDM_FWUP_PNP_VENDOR_ID:
41 return PLDM_FWUP_PNP_VENDOR_ID_LENGTH;
42 case PLDM_FWUP_ACPI_VENDOR_ID:
43 return PLDM_FWUP_ACPI_VENDOR_ID_LENGTH;
44 case PLDM_FWUP_IEEE_ASSIGNED_COMPANY_ID:
45 return PLDM_FWUP_IEEE_ASSIGNED_COMPANY_ID_LENGTH;
46 case PLDM_FWUP_SCSI_VENDOR_ID:
47 return PLDM_FWUP_SCSI_VENDOR_ID_LENGTH;
48 case PLDM_FWUP_PCI_DEVICE_ID:
49 return PLDM_FWUP_PCI_DEVICE_ID_LENGTH;
50 case PLDM_FWUP_PCI_SUBSYSTEM_VENDOR_ID:
51 return PLDM_FWUP_PCI_SUBSYSTEM_VENDOR_ID_LENGTH;
52 case PLDM_FWUP_PCI_SUBSYSTEM_ID:
53 return PLDM_FWUP_PCI_SUBSYSTEM_ID_LENGTH;
54 case PLDM_FWUP_PCI_REVISION_ID:
55 return PLDM_FWUP_PCI_REVISION_ID_LENGTH;
56 case PLDM_FWUP_PNP_PRODUCT_IDENTIFIER:
57 return PLDM_FWUP_PNP_PRODUCT_IDENTIFIER_LENGTH;
58 case PLDM_FWUP_ACPI_PRODUCT_IDENTIFIER:
59 return PLDM_FWUP_ACPI_PRODUCT_IDENTIFIER_LENGTH;
60 case PLDM_FWUP_ASCII_MODEL_NUMBER_LONG_STRING:
61 return PLDM_FWUP_ASCII_MODEL_NUMBER_LONG_STRING_LENGTH;
62 case PLDM_FWUP_ASCII_MODEL_NUMBER_SHORT_STRING:
63 return PLDM_FWUP_ASCII_MODEL_NUMBER_SHORT_STRING_LENGTH;
64 case PLDM_FWUP_SCSI_PRODUCT_ID:
65 return PLDM_FWUP_SCSI_PRODUCT_ID_LENGTH;
66 case PLDM_FWUP_UBM_CONTROLLER_DEVICE_CODE:
67 return PLDM_FWUP_UBM_CONTROLLER_DEVICE_CODE_LENGTH;
68 default:
69 return 0;
70 }
71}
72
73int decode_descriptor_type_length_value(const uint8_t *data, size_t length,
74 uint16_t *descriptor_type,
75 struct variable_field *descriptor_data)
76{
77 uint16_t descriptor_length = 0;
78
79 if (data == NULL || descriptor_type == NULL ||
80 descriptor_data == NULL) {
81 return PLDM_ERROR_INVALID_DATA;
82 }
83
84 if (length < PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN) {
85 return PLDM_ERROR_INVALID_LENGTH;
86 }
87
88 struct pldm_descriptor_tlv *entry =
89 (struct pldm_descriptor_tlv *)(data);
90
91 *descriptor_type = le16toh(entry->descriptor_type);
92 descriptor_length = le16toh(entry->descriptor_length);
93 if (*descriptor_type != PLDM_FWUP_VENDOR_DEFINED) {
94 if (descriptor_length !=
95 get_descriptor_type_length(*descriptor_type)) {
96 return PLDM_ERROR_INVALID_LENGTH;
97 }
98 }
99
100 if (length < (sizeof(*descriptor_type) + sizeof(descriptor_length) +
101 descriptor_length)) {
102 return PLDM_ERROR_INVALID_LENGTH;
103 }
104
105 descriptor_data->ptr = entry->descriptor_data;
106 descriptor_data->length = descriptor_length;
107
108 return PLDM_SUCCESS;
109}
110
111int decode_vendor_defined_descriptor_value(
112 const uint8_t *data, size_t length, uint8_t *descriptor_title_str_type,
113 struct variable_field *descriptor_title_str,
114 struct variable_field *descriptor_data)
115{
116 if (data == NULL || descriptor_title_str_type == NULL ||
117 descriptor_title_str == NULL || descriptor_data == NULL) {
118 return PLDM_ERROR_INVALID_DATA;
119 }
120
121 if (length < sizeof(struct pldm_vendor_defined_descriptor_title_data)) {
122 return PLDM_ERROR_INVALID_LENGTH;
123 }
124
125 struct pldm_vendor_defined_descriptor_title_data *entry =
126 (struct pldm_vendor_defined_descriptor_title_data *)(data);
127 if (!is_string_type_valid(
128 entry->vendor_defined_descriptor_title_str_type) ||
129 (entry->vendor_defined_descriptor_title_str_len == 0)) {
130 return PLDM_ERROR_INVALID_DATA;
131 }
132
133 // Assuming atleast 1 byte of VendorDefinedDescriptorData
134 if (length < (sizeof(struct pldm_vendor_defined_descriptor_title_data) +
135 entry->vendor_defined_descriptor_title_str_len)) {
136 return PLDM_ERROR_INVALID_LENGTH;
137 }
138
139 *descriptor_title_str_type =
140 entry->vendor_defined_descriptor_title_str_type;
141 descriptor_title_str->ptr = entry->vendor_defined_descriptor_title_str;
142 descriptor_title_str->length =
143 entry->vendor_defined_descriptor_title_str_len;
144
145 descriptor_data->ptr =
146 descriptor_title_str->ptr + descriptor_title_str->length;
147 descriptor_data->length =
148 length - sizeof(entry->vendor_defined_descriptor_title_str_type) -
149 sizeof(entry->vendor_defined_descriptor_title_str_len) -
150 descriptor_title_str->length;
151
152 return PLDM_SUCCESS;
153}
154
gokulsanker138ceba2021-04-05 13:25:25 +0530155int encode_query_device_identifiers_req(uint8_t instance_id,
156 size_t payload_length,
157 struct pldm_msg *msg)
158{
159 if (msg == NULL) {
160 return PLDM_ERROR_INVALID_DATA;
161 }
162
163 if (payload_length != PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES) {
164 return PLDM_ERROR_INVALID_LENGTH;
165 }
166
167 return encode_pldm_header_only(PLDM_REQUEST, instance_id, PLDM_FWUP,
168 PLDM_QUERY_DEVICE_IDENTIFIERS, msg);
169}
gokulsankereca3e192021-04-05 14:57:41 +0530170
171int decode_query_device_identifiers_resp(const struct pldm_msg *msg,
172 size_t payload_length,
173 uint8_t *completion_code,
174 uint32_t *device_identifiers_len,
175 uint8_t *descriptor_count,
176 uint8_t **descriptor_data)
177{
178 if (msg == NULL || completion_code == NULL ||
179 device_identifiers_len == NULL || descriptor_count == NULL ||
180 descriptor_data == NULL) {
181 return PLDM_ERROR_INVALID_DATA;
182 }
183
184 *completion_code = msg->payload[0];
185 if (PLDM_SUCCESS != *completion_code) {
186 return PLDM_SUCCESS;
187 }
188
189 if (payload_length <
190 sizeof(struct pldm_query_device_identifiers_resp)) {
191 return PLDM_ERROR_INVALID_LENGTH;
192 }
193
194 struct pldm_query_device_identifiers_resp *response =
195 (struct pldm_query_device_identifiers_resp *)msg->payload;
196 *device_identifiers_len = le32toh(response->device_identifiers_len);
197
198 if (*device_identifiers_len < PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN) {
199 return PLDM_ERROR_INVALID_LENGTH;
200 }
201
202 if (payload_length !=
203 sizeof(struct pldm_query_device_identifiers_resp) +
204 *device_identifiers_len) {
205 return PLDM_ERROR_INVALID_LENGTH;
206 }
207 *descriptor_count = response->descriptor_count;
208
209 if (*descriptor_count == 0) {
210 return PLDM_ERROR_INVALID_DATA;
211 }
gokulsankereca3e192021-04-05 14:57:41 +0530212 *descriptor_data =
213 (uint8_t *)(msg->payload +
214 sizeof(struct pldm_query_device_identifiers_resp));
215 return PLDM_SUCCESS;
216}
gokulsanker981fbfb2021-04-05 15:17:25 +0530217
218int encode_get_firmware_parameters_req(uint8_t instance_id,
219 size_t payload_length,
220 struct pldm_msg *msg)
221{
222 if (msg == NULL) {
223 return PLDM_ERROR_INVALID_DATA;
224 }
225
226 if (payload_length != PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES) {
227 return PLDM_ERROR_INVALID_LENGTH;
228 }
229
230 return encode_pldm_header_only(PLDM_REQUEST, instance_id, PLDM_FWUP,
231 PLDM_GET_FIRMWARE_PARAMETERS, msg);
232}
gokulsanker22fbb342021-04-05 15:55:06 +0530233
234int decode_get_firmware_parameters_resp_comp_set_info(
235 const struct pldm_msg *msg, size_t payload_length,
236 struct pldm_get_firmware_parameters_resp *resp_data,
237 struct variable_field *active_comp_image_set_ver_str,
238 struct variable_field *pending_comp_image_set_ver_str)
239{
240 if (msg == NULL || resp_data == NULL ||
241 active_comp_image_set_ver_str == NULL ||
242 pending_comp_image_set_ver_str == NULL) {
243
244 return PLDM_ERROR_INVALID_DATA;
245 }
246
247 if (payload_length < sizeof(struct pldm_get_firmware_parameters_resp)) {
248 return PLDM_ERROR_INVALID_LENGTH;
249 }
250
251 struct pldm_get_firmware_parameters_resp *response =
252 (struct pldm_get_firmware_parameters_resp *)msg->payload;
253
254 resp_data->completion_code = response->completion_code;
255
256 if (PLDM_SUCCESS != resp_data->completion_code) {
257 return PLDM_SUCCESS;
258 }
259
260 if (response->active_comp_image_set_ver_str_len == 0) {
261 return PLDM_ERROR_INVALID_DATA;
262 }
263
264 size_t resp_len = sizeof(struct pldm_get_firmware_parameters_resp) +
265 response->active_comp_image_set_ver_str_len +
266 response->pending_comp_image_set_ver_str_len;
267
268 if (payload_length != resp_len) {
269 return PLDM_ERROR_INVALID_LENGTH;
270 }
271
272 resp_data->capabilities_during_update.value =
273 le32toh(response->capabilities_during_update.value);
274
275 resp_data->comp_count = le16toh(response->comp_count);
276 if (resp_data->comp_count == 0) {
277 return PLDM_ERROR;
278 }
279
280 resp_data->active_comp_image_set_ver_str_type =
281 response->active_comp_image_set_ver_str_type;
282 resp_data->active_comp_image_set_ver_str_len =
283 response->active_comp_image_set_ver_str_len;
284 resp_data->pending_comp_image_set_ver_str_type =
285 response->pending_comp_image_set_ver_str_type;
286 resp_data->pending_comp_image_set_ver_str_len =
287 response->pending_comp_image_set_ver_str_len;
288
289 active_comp_image_set_ver_str->ptr =
290 msg->payload + sizeof(struct pldm_get_firmware_parameters_resp);
291 active_comp_image_set_ver_str->length =
292 resp_data->active_comp_image_set_ver_str_len;
293
294 if (resp_data->pending_comp_image_set_ver_str_len != 0) {
295 pending_comp_image_set_ver_str->ptr =
296 msg->payload +
297 sizeof(struct pldm_get_firmware_parameters_resp) +
298 resp_data->active_comp_image_set_ver_str_len;
299 pending_comp_image_set_ver_str->length =
300 resp_data->pending_comp_image_set_ver_str_len;
301 } else {
302 pending_comp_image_set_ver_str->ptr = NULL;
303 pending_comp_image_set_ver_str->length = 0;
304 }
305
306 return PLDM_SUCCESS;
307}
gokulsankere1fb7a82021-04-05 16:09:29 +0530308
309int decode_get_firmware_parameters_resp_comp_entry(
310 const uint8_t *data, size_t length,
311 struct pldm_component_parameter_entry *component_data,
312 struct variable_field *active_comp_ver_str,
313 struct variable_field *pending_comp_ver_str)
314{
315 if (data == NULL || component_data == NULL ||
316 active_comp_ver_str == NULL || pending_comp_ver_str == NULL) {
317 return PLDM_ERROR_INVALID_DATA;
318 }
319
320 if (length < sizeof(struct pldm_component_parameter_entry)) {
321 return PLDM_ERROR_INVALID_LENGTH;
322 }
323
324 struct pldm_component_parameter_entry *entry =
325 (struct pldm_component_parameter_entry *)(data);
326 if (entry->active_comp_ver_str_len == 0) {
327 return PLDM_ERROR_INVALID_LENGTH;
328 }
329
330 size_t entry_length = sizeof(struct pldm_component_parameter_entry) +
331 entry->active_comp_ver_str_len +
332 entry->pending_comp_ver_str_len;
333
334 if (length != entry_length) {
335 return PLDM_ERROR_INVALID_LENGTH;
336 }
337
338 component_data->comp_classification =
339 le16toh(entry->comp_classification);
340 component_data->comp_identifier = le16toh(entry->comp_identifier);
341 component_data->comp_classification_index =
342 entry->comp_classification_index;
343 component_data->active_comp_comparison_stamp =
344 le32toh(entry->active_comp_comparison_stamp);
345 component_data->active_comp_ver_str_type =
346 entry->active_comp_ver_str_type;
347 component_data->active_comp_ver_str_len =
348 entry->active_comp_ver_str_len;
349 memcpy(component_data->active_comp_release_date,
350 entry->active_comp_release_date,
351 sizeof(entry->active_comp_release_date));
352 component_data->pending_comp_comparison_stamp =
353 le32toh(entry->pending_comp_comparison_stamp);
354 component_data->pending_comp_ver_str_type =
355 entry->pending_comp_ver_str_type;
356 component_data->pending_comp_ver_str_len =
357 entry->pending_comp_ver_str_len;
358 memcpy(component_data->pending_comp_release_date,
359 entry->pending_comp_release_date,
360 sizeof(entry->pending_comp_release_date));
361 component_data->comp_activation_methods.value =
362 le16toh(entry->comp_activation_methods.value);
363 component_data->capabilities_during_update.value =
364 le32toh(entry->capabilities_during_update.value);
365
366 active_comp_ver_str->ptr =
367 data + sizeof(struct pldm_component_parameter_entry);
368 active_comp_ver_str->length = entry->active_comp_ver_str_len;
369
370 if (entry->pending_comp_ver_str_len != 0) {
371
372 pending_comp_ver_str->ptr =
373 data + sizeof(struct pldm_component_parameter_entry) +
374 entry->active_comp_ver_str_len;
375 pending_comp_ver_str->length = entry->pending_comp_ver_str_len;
376 } else {
377 pending_comp_ver_str->ptr = NULL;
378 pending_comp_ver_str->length = 0;
379 }
380 return PLDM_SUCCESS;
381}