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/fru.c b/libpldm/fru.c
index 3e0887d..b726139 100644
--- a/libpldm/fru.c
+++ b/libpldm/fru.c
@@ -22,12 +22,8 @@
 	header.msg_type = PLDM_REQUEST;
 	header.pldm_type = PLDM_FRU;
 	header.command = PLDM_GET_FRU_RECORD_TABLE_METADATA;
-	int rc = pack_pldm_header(&header, &(msg->hdr));
-	if (PLDM_SUCCESS != rc) {
-		return rc;
-	}
 
-	return PLDM_SUCCESS;
+	return pack_pldm_header(&header, &(msg->hdr));
 }
 
 int decode_get_fru_record_table_metadata_resp(
@@ -76,7 +72,6 @@
     uint16_t total_record_set_identifiers, uint16_t total_table_records,
     uint32_t checksum, struct pldm_msg *msg)
 {
-
 	if (msg == NULL) {
 		return PLDM_ERROR_INVALID_DATA;
 	}
@@ -86,7 +81,8 @@
 	header.instance = instance_id;
 	header.pldm_type = PLDM_FRU;
 	header.command = PLDM_GET_FRU_RECORD_TABLE_METADATA;
-	int rc = pack_pldm_header(&header, &(msg->hdr));
+
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
 	if (PLDM_SUCCESS != rc) {
 		return rc;
 	}
@@ -138,19 +134,18 @@
 				     uint8_t transfer_flag,
 				     struct pldm_msg *msg)
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_ERROR_INVALID_DATA;
+	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_FRU;
 	header.command = PLDM_GET_FRU_RECORD_TABLE;
 
-	if (msg == NULL) {
-		return rc;
-	}
-
-	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;
 	}
 
@@ -288,7 +283,7 @@
 	header.msg_type = PLDM_REQUEST;
 	header.pldm_type = PLDM_FRU;
 	header.command = PLDM_GET_FRU_RECORD_BY_OPTION;
-	int rc = pack_pldm_header(&header, &(msg->hdr));
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
 	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
@@ -348,7 +343,8 @@
 		return PLDM_ERROR_INVALID_DATA;
 	}
 
-	if (payload_length < PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES) {
+	if (payload_length !=
+	    PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES + data_size) {
 		return PLDM_ERROR_INVALID_LENGTH;
 	}
 
@@ -357,7 +353,7 @@
 	header.msg_type = PLDM_RESPONSE;
 	header.pldm_type = PLDM_FRU;
 	header.command = PLDM_GET_FRU_RECORD_BY_OPTION;
-	int rc = pack_pldm_header(&header, &(msg->hdr));
+	uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
 	if (rc != PLDM_SUCCESS) {
 		return rc;
 	}
@@ -369,17 +365,10 @@
 	resp->next_data_transfer_handle = htole32(next_data_transfer_handle);
 	resp->transfer_flag = transfer_flag;
 
-	if (completion_code != PLDM_SUCCESS) {
-		return PLDM_SUCCESS;
+	if (completion_code == PLDM_SUCCESS) {
+		memcpy(resp->fru_structure_data, fru_structure_data, data_size);
 	}
 
-	if (payload_length !=
-	    PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES + data_size) {
-		return PLDM_ERROR_INVALID_LENGTH;
-	}
-
-	memcpy(resp->fru_structure_data, fru_structure_data, data_size);
-
 	return PLDM_SUCCESS;
 }
 
@@ -421,21 +410,21 @@
 				    struct pldm_msg *msg, size_t payload_length)
 
 {
-	struct pldm_header_info header = {0};
-	int rc = PLDM_ERROR_INVALID_DATA;
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+	if (payload_length != sizeof(struct pldm_get_fru_record_table_req)) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
 
+	struct pldm_header_info header = {0};
 	header.msg_type = PLDM_REQUEST;
 	header.instance = instance_id;
 	header.pldm_type = PLDM_FRU;
 	header.command = PLDM_GET_FRU_RECORD_TABLE;
 
-	if (msg == NULL) {
-		return rc;
-	}
-	if (payload_length != sizeof(struct pldm_get_fru_record_table_req)) {
-		return PLDM_ERROR_INVALID_LENGTH;
-	}
-	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;
 	}