From 9f51d0742e6feee8ce064d63c79174598d459267 Mon Sep 17 00:00:00 2001 From: Alistair Francis Date: Wed, 14 Jun 2023 14:49:36 +1000 Subject: [PATCH] library/pci_doe_responder_lib: Use libspdm DOE encode/decode helpers Instead of encoding and decoding DOE discovery packets manually inside spdm-emu let's instead use the generic helper functions in libspdm. This way we can test functions that others can also utilise to when using libspdm. Signed-off-by: Alistair Francis --- library/pci_doe_responder_lib/CMakeLists.txt | 1 - .../pci_doe_responder_lib/pci_doe_dispatch.c | 47 +++++----- .../pci_doe_rsp_discovery.c | 92 ------------------- 3 files changed, 26 insertions(+), 114 deletions(-) delete mode 100644 library/pci_doe_responder_lib/pci_doe_rsp_discovery.c diff --git a/library/pci_doe_responder_lib/CMakeLists.txt b/library/pci_doe_responder_lib/CMakeLists.txt index 9f06218..175d619 100644 --- a/library/pci_doe_responder_lib/CMakeLists.txt +++ b/library/pci_doe_responder_lib/CMakeLists.txt @@ -6,7 +6,6 @@ INCLUDE_DIRECTORIES(${LIBSPDM_DIR}/include SET(src_pci_doe_responder_lib pci_doe_dispatch.c - pci_doe_rsp_discovery.c pci_doe_spdm_vendor_dispatch.c ) diff --git a/library/pci_doe_responder_lib/pci_doe_dispatch.c b/library/pci_doe_responder_lib/pci_doe_dispatch.c index a5fe249..a844b16 100644 --- a/library/pci_doe_responder_lib/pci_doe_dispatch.c +++ b/library/pci_doe_responder_lib/pci_doe_dispatch.c @@ -10,14 +10,10 @@ #include "library/spdm_transport_pcidoe_lib.h" #include "library/pci_doe_responder_lib.h" -typedef struct { - pci_doe_data_object_protocol_t protocol; - pci_doe_get_response_func_t func; -} pci_doe_dispatch_struct_t; - -pci_doe_dispatch_struct_t m_pci_doe_dispatch[] = { - {{PCI_DOE_VENDOR_ID_PCISIG, PCI_DOE_DATA_OBJECT_TYPE_DOE_DISCOVERY}, - pci_doe_get_response_discovery}, +pci_doe_data_object_protocol_t m_data_object_protocol[] = { + {PCI_DOE_VENDOR_ID_PCISIG, PCI_DOE_DATA_OBJECT_TYPE_DOE_DISCOVERY}, + {PCI_DOE_VENDOR_ID_PCISIG, PCI_DOE_DATA_OBJECT_TYPE_SPDM}, + {PCI_DOE_VENDOR_ID_PCISIG, PCI_DOE_DATA_OBJECT_TYPE_SECURED_SPDM}, }; /** @@ -35,22 +31,31 @@ libspdm_return_t pci_doe_get_response_doe_request(const void *pci_doe_context, const void *request, size_t request_size, void *response, size_t *response_size) { - pci_doe_data_object_header_t *doe_request; - size_t index; + uint8_t index = 0; + uint32_t *reply = response; + libspdm_return_t ret; + + ret = libspdm_pci_doe_decode_discovery_request(request_size, request, &index); - doe_request = (void *)request; - if (request_size < sizeof(doe_request)) { - return LIBSPDM_STATUS_INVALID_MSG_SIZE; + if (ret != LIBSPDM_STATUS_SUCCESS) { + return ret; } - for (index = 0; index < LIBSPDM_ARRAY_SIZE(m_pci_doe_dispatch); index++) { - if ((doe_request->vendor_id == m_pci_doe_dispatch[index].protocol.vendor_id) && - (doe_request->data_object_type == - m_pci_doe_dispatch[index].protocol.data_object_type)) { - return m_pci_doe_dispatch[index].func (pci_doe_context, request, request_size, response, - response_size); - } + if (index >= LIBSPDM_ARRAY_SIZE(m_data_object_protocol)) { + return LIBSPDM_STATUS_INVALID_MSG_FIELD; + } else if (index == LIBSPDM_ARRAY_SIZE(m_data_object_protocol) - 1) { + reply[2] = m_data_object_protocol[index].data_object_type << 16 | + m_data_object_protocol[index].vendor_id; + } else { + reply[2] = (index + 1) << 24 | + m_data_object_protocol[index].data_object_type << 16 | + m_data_object_protocol[index].vendor_id; } - return LIBSPDM_STATUS_UNSUPPORTED_CAP; + return libspdm_pci_doe_encode_discovery( + sizeof(uint32_t), + &reply[2], + response_size, + &response + ); } diff --git a/library/pci_doe_responder_lib/pci_doe_rsp_discovery.c b/library/pci_doe_responder_lib/pci_doe_rsp_discovery.c deleted file mode 100644 index a9cbda1..0000000 --- a/library/pci_doe_responder_lib/pci_doe_rsp_discovery.c +++ /dev/null @@ -1,92 +0,0 @@ -/** - * Copyright Notice: - * Copyright 2021-2022 DMTF. All rights reserved. - * License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md - **/ - -#include "hal/base.h" -#include "hal/library/memlib.h" -#include "library/spdm_requester_lib.h" -#include "library/spdm_transport_pcidoe_lib.h" -#include "library/pci_doe_responder_lib.h" - -/* Vendor message*/ - -#pragma pack(1) - -/* DOE Discovery request*/ - -typedef struct { - pci_doe_data_object_header_t doe_header; - pci_doe_discovery_request_t doe_discovery_request; -} doe_discovery_request_mine_t; - - -/* DOE Discovery response*/ - -typedef struct { - pci_doe_data_object_header_t doe_header; - pci_doe_discovery_response_t doe_discovery_response; -} doe_discovery_response_mine_t; - -#pragma pack() - -pci_doe_data_object_protocol_t m_data_object_protocol[] = { - {PCI_DOE_VENDOR_ID_PCISIG, PCI_DOE_DATA_OBJECT_TYPE_DOE_DISCOVERY}, - {PCI_DOE_VENDOR_ID_PCISIG, PCI_DOE_DATA_OBJECT_TYPE_SPDM}, - {PCI_DOE_VENDOR_ID_PCISIG, PCI_DOE_DATA_OBJECT_TYPE_SECURED_SPDM}, -}; - -/** - * Process the DOE request and return the response. - * - * @param request the PCI_DOE request message, start from pci_doe_data_object_header_t. - * @param request_size size in bytes of request. - * @param response the PCI_DOE response message, start from pci_doe_data_object_header_t. - * @param response_size size in bytes of response. - * - * @retval LIBSPDM_STATUS_SUCCESS The request is processed and the response is returned. - * @return ERROR The request is not processed. - **/ -libspdm_return_t pci_doe_get_response_discovery (const void *pci_doe_context, - const void *request, size_t request_size, - void *response, size_t *response_size) -{ - doe_discovery_request_mine_t *doe_request; - doe_discovery_response_mine_t doe_response; - uint8_t index; - - doe_request = (void *)request; - if (request_size != sizeof(doe_discovery_request_mine_t)) { - return LIBSPDM_STATUS_INVALID_MSG_SIZE; - } - if (doe_request->doe_header.length != sizeof(*doe_request) / sizeof(uint32_t)) { - return LIBSPDM_STATUS_INVALID_MSG_FIELD; - } - index = doe_request->doe_discovery_request.index; - if (index >= LIBSPDM_ARRAY_SIZE(m_data_object_protocol)) { - return LIBSPDM_STATUS_INVALID_MSG_FIELD; - } - - libspdm_zero_mem (&doe_response, sizeof(doe_response)); - doe_response.doe_header.vendor_id = PCI_DOE_VENDOR_ID_PCISIG; - doe_response.doe_header.data_object_type = PCI_DOE_DATA_OBJECT_TYPE_DOE_DISCOVERY; - doe_response.doe_header.length = sizeof(doe_discovery_response_mine_t) / sizeof(uint32_t); - doe_response.doe_discovery_response.vendor_id = m_data_object_protocol[index].vendor_id; - doe_response.doe_discovery_response.data_object_type = - m_data_object_protocol[index].data_object_type; - if (index + 1 == LIBSPDM_ARRAY_SIZE(m_data_object_protocol)) { - doe_response.doe_discovery_response.next_index = 0; - } else { - doe_response.doe_discovery_response.next_index = index + 1; - } - - if (*response_size < sizeof(doe_response)) { - *response_size = sizeof(doe_response); - return LIBSPDM_STATUS_BUFFER_TOO_SMALL; - } - *response_size = sizeof(doe_response); - libspdm_copy_mem (response, *response_size, (uint8_t *)&doe_response, sizeof(doe_response)); - - return LIBSPDM_STATUS_SUCCESS; -}