Fix the return value of pack_pldm_header and unpack_pldm_header

- The intent behind this commit is to fix the return value of the
  pack_pldm_header and the unpack_pldm_header methods.

- According to PLDM spec, their return value should be `uint8_t`, not
  `int`, so 0 is PLDM_SUCCESS and non-0 is failure.

- Also, when we call the pack_pldm_header and unpack_pldm_header
  methods, we need to verify the return value of the method.

Tested: Built pldm successfully and Unit Test passes.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I0bd6838c4fb3b90f821c10324e4536ed352ffcfa
diff --git a/libpldm/bios.c b/libpldm/bios.c
index 6dbc0b0..8d93a6a 100644
--- a/libpldm/bios.c
+++ b/libpldm/bios.c
@@ -6,12 +6,11 @@
 
 int encode_get_date_time_req(uint8_t instance_id, struct pldm_msg *msg)
 {
-	struct pldm_header_info header = {0};
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_REQUEST;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_BIOS;
@@ -24,25 +23,23 @@
 			      uint8_t day, uint8_t month, uint16_t year,
 			      struct pldm_msg *msg)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_RESPONSE;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_BIOS;
 	header.command = PLDM_GET_DATE_TIME;
 
-	struct pldm_get_date_time_resp *response =
-	    (struct pldm_get_date_time_resp *)msg->payload;
-
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
 
+	struct pldm_get_date_time_resp *response =
+	    (struct pldm_get_date_time_resp *)msg->payload;
 	response->completion_code = completion_code;
 	if (response->completion_code == PLDM_SUCCESS) {
 		response->completion_code = completion_code;
@@ -94,8 +91,6 @@
 			     uint8_t month, uint16_t year, struct pldm_msg *msg,
 			     size_t payload_length)
 {
-	struct pldm_header_info header = {0};
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
@@ -106,11 +101,17 @@
 	if (!is_time_legal(seconds, minutes, hours, day, month, year)) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
+
+	struct pldm_header_info header = {0};
 	header.instance = instance_id;
 	header.msg_type = PLDM_REQUEST;
 	header.pldm_type = PLDM_BIOS;
 	header.command = PLDM_SET_DATE_TIME;
-	pack_pldm_header(&header, &msg->hdr);
+
+	uint8_t rc = pack_pldm_header(&header, &msg->hdr);
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
 
 	struct pldm_set_date_time_req *request =
 	    (struct pldm_set_date_time_req *)msg->payload;
@@ -156,8 +157,6 @@
 int encode_set_date_time_resp(uint8_t instance_id, uint8_t completion_code,
 			      struct pldm_msg *msg, size_t payload_length)
 {
-	struct pldm_header_info header = {0};
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
@@ -165,11 +164,13 @@
 		return PLDM_ERROR_INVALID_LENGTH;
 	}
 
+	struct pldm_header_info header = {0};
 	header.instance = instance_id;
 	header.msg_type = PLDM_RESPONSE;
 	header.pldm_type = PLDM_BIOS;
 	header.command = PLDM_SET_DATE_TIME;
-	int rc = pack_pldm_header(&header, &msg->hdr);
+
+	uint8_t rc = pack_pldm_header(&header, &msg->hdr);
 	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
@@ -205,25 +206,24 @@
 			       uint8_t transfer_flag, uint8_t *table_data,
 			       size_t payload_length, struct pldm_msg *msg)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
-	struct pldm_get_bios_table_resp *response =
-	    (struct pldm_get_bios_table_resp *)msg->payload;
-
-	response->completion_code = completion_code;
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_RESPONSE;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_BIOS;
 	header.command = PLDM_GET_BIOS_TABLE;
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
 
+	struct pldm_get_bios_table_resp *response =
+	    (struct pldm_get_bios_table_resp *)msg->payload;
+	response->completion_code = completion_code;
 	if (response->completion_code == PLDM_SUCCESS) {
 
 		response->next_transfer_handle = htole32(next_transfer_handle);
@@ -244,17 +244,20 @@
 			      uint8_t transfer_op_flag, uint8_t table_type,
 			      struct pldm_msg *msg)
 {
-	struct pldm_header_info header = {0};
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_REQUEST;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_BIOS;
 	header.command = PLDM_GET_BIOS_TABLE;
-	pack_pldm_header(&header, &(msg->hdr));
+
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
 
 	struct pldm_get_bios_table_req *request =
 	    (struct pldm_get_bios_table_req *)msg->payload;
@@ -325,17 +328,20 @@
     uint8_t instance_id, uint32_t transfer_handle, uint8_t transfer_op_flag,
     uint16_t attribute_handle, struct pldm_msg *msg)
 {
-	struct pldm_header_info header = {0};
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_REQUEST;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_BIOS;
 	header.command = PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE;
-	pack_pldm_header(&header, &(msg->hdr));
+
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
 
 	struct pldm_get_bios_attribute_current_value_by_handle_req *request =
 	    (struct pldm_get_bios_attribute_current_value_by_handle_req *)
@@ -410,25 +416,25 @@
     uint8_t transfer_flag, const uint8_t *attribute_data,
     size_t attribute_length, struct pldm_msg *msg)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_SUCCESS;
-
 	if (msg == NULL || attribute_data == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
+	struct pldm_header_info header = {0};
+	header.msg_type = PLDM_RESPONSE;
+	header.instance = instance_id;
+	header.pldm_type = PLDM_BIOS;
+	header.command = PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE;
+
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
+
 	struct pldm_get_bios_attribute_current_value_by_handle_resp *response =
 	    (struct pldm_get_bios_attribute_current_value_by_handle_resp *)
 		msg->payload;
-
 	response->completion_code = completion_code;
-	header.msg_type = PLDM_RESPONSE;
-	header.instance = instance_id;
-	header.pldm_type = PLDM_BIOS;
-	header.command = PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE;
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
-		return rc;
-	}
 	if (response->completion_code == PLDM_SUCCESS) {
 
 		response->next_transfer_handle = htole32(next_transfer_handle);
@@ -457,7 +463,11 @@
 	header.msg_type = PLDM_REQUEST;
 	header.pldm_type = PLDM_BIOS;
 	header.command = PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE;
-	pack_pldm_header(&header, &msg->hdr);
+
+	uint8_t rc = pack_pldm_header(&header, &msg->hdr);
+	if (rc != PLDM_SUCCESS) {
+		return rc;
+	}
 
 	struct pldm_set_bios_attribute_current_value_req *request =
 	    (struct pldm_set_bios_attribute_current_value_req *)msg->payload;
@@ -532,7 +542,7 @@
 	header.pldm_type = PLDM_BIOS;
 	header.command = PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE;
 
-	int rc = pack_pldm_header(&header, &msg->hdr);
+	uint8_t rc = pack_pldm_header(&header, &msg->hdr);
 	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
@@ -550,8 +560,6 @@
 			      const uint8_t *table_data, size_t table_length,
 			      struct pldm_msg *msg, size_t payload_length)
 {
-	int rc = PLDM_SUCCESS;
-
 	if (msg == NULL || table_data == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
@@ -567,7 +575,8 @@
 	header.pldm_type = PLDM_BIOS;
 	header.command = PLDM_SET_BIOS_TABLE;
 
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
 
@@ -611,8 +620,6 @@
 			       uint32_t next_transfer_handle,
 			       struct pldm_msg *msg)
 {
-	int rc = PLDM_SUCCESS;
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
@@ -623,7 +630,8 @@
 	header.pldm_type = PLDM_BIOS;
 	header.command = PLDM_SET_BIOS_TABLE;
 
-	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
+	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}