blob: d3008b1c9e5179bbd22061a3f40dec41f0b2db79 [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
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700234int decode_get_firmware_parameters_resp(
gokulsanker22fbb342021-04-05 15:55:06 +0530235 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,
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700238 struct variable_field *pending_comp_image_set_ver_str,
239 struct variable_field *comp_parameter_table)
gokulsanker22fbb342021-04-05 15:55:06 +0530240{
241 if (msg == NULL || resp_data == NULL ||
242 active_comp_image_set_ver_str == NULL ||
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700243 pending_comp_image_set_ver_str == NULL ||
244 comp_parameter_table == NULL) {
gokulsanker22fbb342021-04-05 15:55:06 +0530245 return PLDM_ERROR_INVALID_DATA;
246 }
247
248 if (payload_length < sizeof(struct pldm_get_firmware_parameters_resp)) {
249 return PLDM_ERROR_INVALID_LENGTH;
250 }
251
252 struct pldm_get_firmware_parameters_resp *response =
253 (struct pldm_get_firmware_parameters_resp *)msg->payload;
254
255 resp_data->completion_code = response->completion_code;
256
257 if (PLDM_SUCCESS != resp_data->completion_code) {
258 return PLDM_SUCCESS;
259 }
260
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700261 if (!is_string_type_valid(
262 response->active_comp_image_set_ver_str_type) ||
263 (response->active_comp_image_set_ver_str_len == 0)) {
gokulsanker22fbb342021-04-05 15:55:06 +0530264 return PLDM_ERROR_INVALID_DATA;
265 }
266
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700267 if (response->pending_comp_image_set_ver_str_len == 0) {
268 if (response->pending_comp_image_set_ver_str_type !=
269 PLDM_STR_TYPE_UNKNOWN) {
270 return PLDM_ERROR_INVALID_DATA;
271 }
272 } else {
273 if (!is_string_type_valid(
274 response->pending_comp_image_set_ver_str_type)) {
275 return PLDM_ERROR_INVALID_DATA;
276 }
277 }
gokulsanker22fbb342021-04-05 15:55:06 +0530278
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700279 size_t partial_response_length =
280 sizeof(struct pldm_get_firmware_parameters_resp) +
281 response->active_comp_image_set_ver_str_len +
282 response->pending_comp_image_set_ver_str_len;
283
284 if (payload_length < partial_response_length) {
gokulsanker22fbb342021-04-05 15:55:06 +0530285 return PLDM_ERROR_INVALID_LENGTH;
286 }
287
288 resp_data->capabilities_during_update.value =
289 le32toh(response->capabilities_during_update.value);
gokulsanker22fbb342021-04-05 15:55:06 +0530290 resp_data->comp_count = le16toh(response->comp_count);
gokulsanker22fbb342021-04-05 15:55:06 +0530291 resp_data->active_comp_image_set_ver_str_type =
292 response->active_comp_image_set_ver_str_type;
293 resp_data->active_comp_image_set_ver_str_len =
294 response->active_comp_image_set_ver_str_len;
295 resp_data->pending_comp_image_set_ver_str_type =
296 response->pending_comp_image_set_ver_str_type;
297 resp_data->pending_comp_image_set_ver_str_len =
298 response->pending_comp_image_set_ver_str_len;
299
300 active_comp_image_set_ver_str->ptr =
301 msg->payload + sizeof(struct pldm_get_firmware_parameters_resp);
302 active_comp_image_set_ver_str->length =
303 resp_data->active_comp_image_set_ver_str_len;
304
305 if (resp_data->pending_comp_image_set_ver_str_len != 0) {
306 pending_comp_image_set_ver_str->ptr =
307 msg->payload +
308 sizeof(struct pldm_get_firmware_parameters_resp) +
309 resp_data->active_comp_image_set_ver_str_len;
310 pending_comp_image_set_ver_str->length =
311 resp_data->pending_comp_image_set_ver_str_len;
312 } else {
313 pending_comp_image_set_ver_str->ptr = NULL;
314 pending_comp_image_set_ver_str->length = 0;
315 }
316
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700317 if (payload_length > partial_response_length && resp_data->comp_count) {
318 comp_parameter_table->ptr =
319 msg->payload +
320 sizeof(struct pldm_get_firmware_parameters_resp) +
321 resp_data->active_comp_image_set_ver_str_len +
322 resp_data->pending_comp_image_set_ver_str_len;
323 comp_parameter_table->length =
324 payload_length - partial_response_length;
325 } else {
326 comp_parameter_table->ptr = NULL;
327 comp_parameter_table->length = 0;
328 }
329
gokulsanker22fbb342021-04-05 15:55:06 +0530330 return PLDM_SUCCESS;
331}
gokulsankere1fb7a82021-04-05 16:09:29 +0530332
333int decode_get_firmware_parameters_resp_comp_entry(
334 const uint8_t *data, size_t length,
335 struct pldm_component_parameter_entry *component_data,
336 struct variable_field *active_comp_ver_str,
337 struct variable_field *pending_comp_ver_str)
338{
339 if (data == NULL || component_data == NULL ||
340 active_comp_ver_str == NULL || pending_comp_ver_str == NULL) {
341 return PLDM_ERROR_INVALID_DATA;
342 }
343
344 if (length < sizeof(struct pldm_component_parameter_entry)) {
345 return PLDM_ERROR_INVALID_LENGTH;
346 }
347
348 struct pldm_component_parameter_entry *entry =
349 (struct pldm_component_parameter_entry *)(data);
350 if (entry->active_comp_ver_str_len == 0) {
351 return PLDM_ERROR_INVALID_LENGTH;
352 }
353
354 size_t entry_length = sizeof(struct pldm_component_parameter_entry) +
355 entry->active_comp_ver_str_len +
356 entry->pending_comp_ver_str_len;
357
358 if (length != entry_length) {
359 return PLDM_ERROR_INVALID_LENGTH;
360 }
361
362 component_data->comp_classification =
363 le16toh(entry->comp_classification);
364 component_data->comp_identifier = le16toh(entry->comp_identifier);
365 component_data->comp_classification_index =
366 entry->comp_classification_index;
367 component_data->active_comp_comparison_stamp =
368 le32toh(entry->active_comp_comparison_stamp);
369 component_data->active_comp_ver_str_type =
370 entry->active_comp_ver_str_type;
371 component_data->active_comp_ver_str_len =
372 entry->active_comp_ver_str_len;
373 memcpy(component_data->active_comp_release_date,
374 entry->active_comp_release_date,
375 sizeof(entry->active_comp_release_date));
376 component_data->pending_comp_comparison_stamp =
377 le32toh(entry->pending_comp_comparison_stamp);
378 component_data->pending_comp_ver_str_type =
379 entry->pending_comp_ver_str_type;
380 component_data->pending_comp_ver_str_len =
381 entry->pending_comp_ver_str_len;
382 memcpy(component_data->pending_comp_release_date,
383 entry->pending_comp_release_date,
384 sizeof(entry->pending_comp_release_date));
385 component_data->comp_activation_methods.value =
386 le16toh(entry->comp_activation_methods.value);
387 component_data->capabilities_during_update.value =
388 le32toh(entry->capabilities_during_update.value);
389
390 active_comp_ver_str->ptr =
391 data + sizeof(struct pldm_component_parameter_entry);
392 active_comp_ver_str->length = entry->active_comp_ver_str_len;
393
394 if (entry->pending_comp_ver_str_len != 0) {
395
396 pending_comp_ver_str->ptr =
397 data + sizeof(struct pldm_component_parameter_entry) +
398 entry->active_comp_ver_str_len;
399 pending_comp_ver_str->length = entry->pending_comp_ver_str_len;
400 } else {
401 pending_comp_ver_str->ptr = NULL;
402 pending_comp_ver_str->length = 0;
403 }
404 return PLDM_SUCCESS;
405}