libpldm: Add encode API for UpdateComponent request

The update agent sends UpdateComponent command to request updating a
specific firmware component. This implementation works with
DSP0267_1.1.0, DSP0267_1.0.1 and DSP0267_1.0.0.

Tested: Unit tests passed

Signed-off-by: gokulsanker <gokul.sanker.v.g@intel.com>
Change-Id: I9f3328dfbbafd8dab03305e2f489ff28adf250a4
diff --git a/libpldm/firmware_update.c b/libpldm/firmware_update.c
index 898a7ef..8699bb5 100644
--- a/libpldm/firmware_update.c
+++ b/libpldm/firmware_update.c
@@ -840,3 +840,61 @@
 

 	return PLDM_SUCCESS;

 }

+

+int encode_update_component_req(

+    uint8_t instance_id, uint16_t comp_classification, uint16_t comp_identifier,

+    uint8_t comp_classification_index, uint32_t comp_comparison_stamp,

+    uint32_t comp_image_size, bitfield32_t update_option_flags,

+    uint8_t comp_ver_str_type, uint8_t comp_ver_str_len,

+    const struct variable_field *comp_ver_str, struct pldm_msg *msg,

+    size_t payload_length)

+{

+	if (comp_ver_str == NULL || comp_ver_str->ptr == NULL || msg == NULL) {

+		return PLDM_ERROR_INVALID_DATA;

+	}

+

+	if (payload_length !=

+	    sizeof(struct pldm_update_component_req) + comp_ver_str->length) {

+		return PLDM_ERROR_INVALID_LENGTH;

+	}

+

+	if (!comp_image_size) {

+		return PLDM_ERROR_INVALID_DATA;

+	}

+

+	if ((comp_ver_str_len == 0) ||

+	    (comp_ver_str_len != comp_ver_str->length)) {

+		return PLDM_ERROR_INVALID_DATA;

+	}

+

+	if (!is_string_type_valid(comp_ver_str_type)) {

+		return PLDM_ERROR_INVALID_DATA;

+	}

+

+	struct pldm_header_info header = {0};

+	header.instance = instance_id;

+	header.msg_type = PLDM_REQUEST;

+	header.pldm_type = PLDM_FWUP;

+	header.command = PLDM_UPDATE_COMPONENT;

+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));

+	if (rc) {

+		return rc;

+	}

+

+	struct pldm_update_component_req *request =

+	    (struct pldm_update_component_req *)msg->payload;

+

+	request->comp_classification = htole16(comp_classification);

+	request->comp_identifier = htole16(comp_identifier);

+	request->comp_classification_index = comp_classification_index;

+	request->comp_comparison_stamp = htole32(comp_comparison_stamp);

+	request->comp_image_size = htole32(comp_image_size);

+	request->update_option_flags.value = htole32(update_option_flags.value);

+	request->comp_ver_str_type = comp_ver_str_type;

+	request->comp_ver_str_len = comp_ver_str_len;

+

+	memcpy(msg->payload + sizeof(struct pldm_update_component_req),

+	       comp_ver_str->ptr, comp_ver_str->length);

+

+	return PLDM_SUCCESS;

+}
\ No newline at end of file