diff mbox series

[V5,21/21] core/pldm: Get file handle and file length

Message ID 20220429094030.71902-22-clombard@linux.vnet.ibm.com
State Superseded
Headers show
Series Implement MCTP and PLDM features | expand

Commit Message

Christophe Lombard April 29, 2022, 9:40 a.m. UTC
Retrieve the file handle and file length based on attribute name.

Reviewed-by: Abhishek Singh Tomar <abhishek at linux.ibm.com>
Signed-off-by: Christophe Lombard <clombard@linux.vnet.ibm.com>
---
 core/pldm/pldm-file-io-requests.c | 76 +++++++++++++++++++++++++++++++
 core/pldm/pldm.h                  |  3 ++
 2 files changed, 79 insertions(+)
diff mbox series

Patch

diff --git a/core/pldm/pldm-file-io-requests.c b/core/pldm/pldm-file-io-requests.c
index 06edfe23..94828fcd 100644
--- a/core/pldm/pldm-file-io-requests.c
+++ b/core/pldm/pldm-file-io-requests.c
@@ -34,6 +34,82 @@  static void file_io_init_complete(bool success)
 	file_io_ready = true;
 }
 
+#define CHKSUM_PADDING 8
+
+/*
+ * Retrieve the file handle and file length from the file attribute
+ * table.
+ */
+static int find_file_handle_by_lid_id(const char *lid_id,
+				      uint32_t *file_handle,
+				      uint32_t *file_length)
+{
+	const struct pldm_file_attr_table_entry *file_entry;
+	char *startptr, *endptr;
+	uint16_t file_name_length;
+
+	if ((file_attr_table == NULL) || (file_attr_length == 0))
+		return OPAL_PARAMETER;
+
+	startptr = (char *)file_attr_table;
+	endptr = startptr + file_attr_length - CHKSUM_PADDING;
+	*file_handle = 0;
+	*file_length = 0;
+
+	while (startptr < endptr) {
+		/* file entry:
+		 *   4 Bytes: file handle
+		 *   2 Bytes: file name length
+		 *   <file name length> Bytes: file name
+		 *   4 Bytes: file length
+		 */
+		file_entry = (struct pldm_file_attr_table_entry *)startptr;
+
+		*file_handle = le32_to_cpu(file_entry->file_handle);
+		startptr += sizeof(uint32_t);
+
+		file_name_length = le16_to_cpu(file_entry->file_name_length);
+		startptr += sizeof(file_name_length);
+
+		if (!strncmp(startptr, lid_id, strlen(lid_id))) {
+			startptr += file_name_length;
+			*file_length = le32_to_cpu(*(uint32_t *)startptr);
+			break;
+		}
+		startptr += file_name_length;
+		startptr += sizeof(uint32_t);
+		startptr += sizeof(bitfield32_t);
+	}
+
+	if (*file_length == 0) {
+		prlog(PR_ERR, "%s - No file handle found, lid_id: %s\n",
+			      __func__, lid_id);
+		*file_handle = 0xff;
+		*file_length = 0;
+		return OPAL_PARAMETER;
+	}
+
+	prlog(PR_DEBUG, "%s - lid_id: %s, file_handle: %d, file_length: %d\n",
+			__func__, lid_id, *file_handle, *file_length);
+
+	return OPAL_SUCCESS;
+}
+
+/*
+ * Retrieve the file handle and file length based on lid id.
+ */
+int pldm_find_file_handle_by_lid_id(const char *lid_id,
+				    uint32_t *file_handle,
+				    uint32_t *file_length)
+{
+	if (!file_io_ready)
+		return OPAL_PARAMETER;
+
+	return find_file_handle_by_lid_id(lid_id,
+					  file_handle,
+					  file_length);
+}
+
 /* maximum currently transfer size for PLDM */
 #define KILOBYTE 1024ul
 #define MAX_TRANSFER_SIZE_BYTES (127 * KILOBYTE + 1)
diff --git a/core/pldm/pldm.h b/core/pldm/pldm.h
index 3ec5126b..7054ba54 100644
--- a/core/pldm/pldm.h
+++ b/core/pldm/pldm.h
@@ -56,6 +56,9 @@  int pldm_rx_handle_request(struct pldm_rx_data *rx);
 int pldm_mctp_responder_init(void);
 
 /* Requester support */
+int pldm_find_file_handle_by_lid_id(const char *lid_id,
+				    uint32_t *file_handle,
+				    uint32_t *file_length);
 int pldm_file_io_read_file(uint32_t file_handle, uint32_t file_length,
 			   void *buf, uint32_t offset, uint64_t size);
 int pldm_file_io_write_file(uint32_t file_handle, const void *buf,