From f2273033a2178ff842e9f259394db97c8702c82e Mon Sep 17 00:00:00 2001 From: Abner Chang Date: Thu, 11 Jan 2024 16:53:34 +0800 Subject: [PATCH] RedfishClientPkg/FeatureDriver: ComputerSystem_1_13_0 driver Initial version of edk2 Redfish feature of Redfish schema ComputerSystem_1_13_0. This driver handles ComputerSystem Boot properties but not all. Signed-off-by: Abner Chang Cc: Nickle Wang Cc: Igor Kulchytskyy --- .../v1_13_0/Common/ComputerSystemCommon.c | 899 ++++++++++++++++++ .../v1_13_0/Common/ComputerSystemCommon.h | 29 + .../v1_13_0/Dxe/ComputerSystemDxe.c | 702 ++++++++++++++ .../v1_13_0/Dxe/ComputerSystemDxe.inf | 53 ++ 4 files changed, 1683 insertions(+) create mode 100644 RedfishClientPkg/Features/ComputerSystem/v1_13_0/Common/ComputerSystemCommon.c create mode 100644 RedfishClientPkg/Features/ComputerSystem/v1_13_0/Common/ComputerSystemCommon.h create mode 100644 RedfishClientPkg/Features/ComputerSystem/v1_13_0/Dxe/ComputerSystemDxe.c create mode 100644 RedfishClientPkg/Features/ComputerSystem/v1_13_0/Dxe/ComputerSystemDxe.inf diff --git a/RedfishClientPkg/Features/ComputerSystem/v1_13_0/Common/ComputerSystemCommon.c b/RedfishClientPkg/Features/ComputerSystem/v1_13_0/Common/ComputerSystemCommon.c new file mode 100644 index 000000000..bc24c4920 --- /dev/null +++ b/RedfishClientPkg/Features/ComputerSystem/v1_13_0/Common/ComputerSystemCommon.c @@ -0,0 +1,899 @@ +/** @file + Redfish feature driver implementation - common functions + + (C) Copyright 2020-2022 Hewlett Packard Enterprise Development LP
+ Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "ComputerSystemCommon.h" + +CHAR8 ComputerSystemEmptyJson[] = "{\"@odata.id\": \"\", \"@odata.type\": \"#ComputerSystem.v1_13_0.ComputerSystem\", \"Id\": \"\", \"Name\": \"\", \"Boot\":{}}"; + +REDFISH_RESOURCE_COMMON_PRIVATE *mRedfishResourcePrivate = NULL; +EFI_HANDLE mRedfishResourceConfigProtocolHandle = NULL; + +/** + Consume resource from given URI. + + @param[in] This Pointer to REDFISH_RESOURCE_COMMON_PRIVATE instance. + @param[in] Json The JSON to consume. + @param[in] HeaderEtag The Etag string returned in HTTP header. + + @retval EFI_SUCCESS Value is returned successfully. + @retval Others Some error happened. + +**/ +EFI_STATUS +RedfishConsumeResourceCommon ( + IN REDFISH_RESOURCE_COMMON_PRIVATE *Private, + IN CHAR8 *Json, + IN CHAR8 *HeaderEtag OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_REDFISH_COMPUTERSYSTEM_V1_13_0 *ComputerSystem; + EFI_REDFISH_COMPUTERSYSTEM_V1_13_0_CS *ComputerSystemCs; + EFI_STRING ConfigureLang; + + if ((Private == NULL) || IS_EMPTY_STRING (Json)) { + return EFI_INVALID_PARAMETER; + } + + ComputerSystem = NULL; + ComputerSystemCs = NULL; + ConfigureLang = NULL; + + Status = Private->JsonStructProtocol->ToStructure ( + Private->JsonStructProtocol, + NULL, + Json, + (EFI_REST_JSON_STRUCTURE_HEADER **)&ComputerSystem + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: ToStructure() failed: %r\n", __func__, Status)); + return Status; + } + + ComputerSystemCs = ComputerSystem->ComputerSystem; + + // + // Check ETAG to see if we need to consume it + // + if (CheckEtag (Private->Uri, HeaderEtag, NULL)) { + // + // No change + // + DEBUG ((DEBUG_MANAGEABILITY, "%a: ETAG: %s has no change, ignore consume action\n", __func__, Private->Uri)); + Status = EFI_SUCCESS; + goto ON_RELEASE; + } + + // + // Handle BOOT + // + if (ComputerSystemCs->Boot == NULL) { + ComputerSystemCs->Boot = AllocateZeroPool (sizeof (RedfishComputerSystem_V1_13_0_Boot_CS)); + ASSERT (ComputerSystemCs->Boot != NULL); + } + + // + // Handle BOOT->BOOTNEXT + // + if (ComputerSystemCs->Boot->BootNext != NULL) { + // + // Find corresponding configure language for collection resource. + // + ConfigureLang = GetConfigureLang (ComputerSystemCs->odata_id, "Boot/BootNext"); + if (ConfigureLang != NULL) { + Status = ApplyFeatureSettingsStringType (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, ConfigureLang, ComputerSystemCs->Boot->BootNext); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a, apply setting for %s failed: %r\n", __func__, ConfigureLang, Status)); + } + + FreePool (ConfigureLang); + } else { + DEBUG ((DEBUG_ERROR, "%a, can not get configure language for URI: %s\n", __func__, Private->Uri)); + } + } + + // + // Handle BOOT->BOOTORDER + // + if (ComputerSystemCs->Boot->BootOrder != NULL) { + // + // Find corresponding configure language for collection resource. + // + ConfigureLang = GetConfigureLang (ComputerSystemCs->odata_id, "Boot/BootOrder"); + if (ConfigureLang != NULL) { + Status = ApplyFeatureSettingsStringArrayType (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, ConfigureLang, ComputerSystemCs->Boot->BootOrder); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: apply setting for %s failed: %r\n", __func__, ConfigureLang, Status)); + } + + FreePool (ConfigureLang); + } else { + DEBUG ((DEBUG_ERROR, "%a: can not get configure language for URI: %s\n", __func__, Private->Uri)); + } + } + + // + // Handle BOOT->BOOTSOURCEOVERRIDEENABLED + // + if (ComputerSystemCs->Boot->BootSourceOverrideEnabled != NULL) { + // + // Find corresponding configure language for collection resource. + // + ConfigureLang = GetConfigureLang (ComputerSystemCs->odata_id, "Boot/BootSourceOverrideEnabled"); + if (ConfigureLang != NULL) { + Status = ApplyFeatureSettingsStringType (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, ConfigureLang, ComputerSystemCs->Boot->BootSourceOverrideEnabled); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: apply setting for %s failed: %r\n", __func__, ConfigureLang, Status)); + } + + FreePool (ConfigureLang); + } else { + DEBUG ((DEBUG_ERROR, "%a: can not get configure language for URI: %s\n", __func__, Private->Uri)); + } + } + + // + // Handle BOOT->BOOTSOURCEOVERRIDEMODE + // + if (ComputerSystemCs->Boot->BootSourceOverrideMode != NULL) { + // + // Find corresponding configure language for collection resource. + // + ConfigureLang = GetConfigureLang (ComputerSystemCs->odata_id, "Boot/BootSourceOverrideMode"); + if (ConfigureLang != NULL) { + Status = ApplyFeatureSettingsStringType (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, ConfigureLang, ComputerSystemCs->Boot->BootSourceOverrideMode); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: apply setting for %s failed: %r\n", __func__, ConfigureLang, Status)); + } + + FreePool (ConfigureLang); + } else { + DEBUG ((DEBUG_ERROR, "%a: can not get configure language for URI: %s\n", __func__, Private->Uri)); + } + } + + // + // Handle BOOT->BOOTSOURCEOVERRIDETARGET + // + if (ComputerSystemCs->Boot->BootSourceOverrideTarget != NULL) { + // + // Find corresponding configure language for collection resource. + // + ConfigureLang = GetConfigureLang (ComputerSystemCs->odata_id, "Boot/BootSourceOverrideTarget"); + if (ConfigureLang != NULL) { + Status = ApplyFeatureSettingsStringType (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, ConfigureLang, ComputerSystemCs->Boot->BootSourceOverrideTarget); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a, apply setting for %s failed: %r\n", __func__, ConfigureLang, Status)); + } + + FreePool (ConfigureLang); + } else { + DEBUG ((DEBUG_ERROR, "%a, can not get configure language for URI: %s\n", __func__, Private->Uri)); + } + } + + // + // Handle BOOT->UEFITARGETBOOTSOURCEOVERRIDE + // + if (ComputerSystemCs->Boot->UefiTargetBootSourceOverride != NULL) { + // + // Find corresponding configure language for collection resource. + // + ConfigureLang = GetConfigureLang (ComputerSystemCs->odata_id, "Boot/UefiTargetBootSourceOverride"); + if (ConfigureLang != NULL) { + Status = ApplyFeatureSettingsStringType (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, ConfigureLang, ComputerSystemCs->Boot->UefiTargetBootSourceOverride); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: apply setting for %s failed: %r\n", __func__, ConfigureLang, Status)); + } + + FreePool (ConfigureLang); + } else { + DEBUG ((DEBUG_ERROR, "%a: can not get configure language for URI: %s\n", __func__, Private->Uri)); + } + } + +ON_RELEASE: + + // + // Release resource. + // + Private->JsonStructProtocol->DestoryStructure ( + Private->JsonStructProtocol, + (EFI_REST_JSON_STRUCTURE_HEADER *)ComputerSystem + ); + + return EFI_SUCCESS; +} + +EFI_STATUS +ProvisioningComputerSystemProperties ( + IN EFI_REST_JSON_STRUCTURE_PROTOCOL *JsonStructProtocol, + IN CHAR8 *InputJson, + IN CHAR8 *ResourceId OPTIONAL, + IN EFI_STRING ConfigureLang, + IN BOOLEAN ProvisionMode, + OUT CHAR8 **ResultJson + ) +{ + EFI_REDFISH_COMPUTERSYSTEM_V1_13_0 *ComputerSystem; + EFI_REDFISH_COMPUTERSYSTEM_V1_13_0_CS *ComputerSystemCs; + EFI_REDFISH_COMPUTERSYSTEM_V1_13_0 *ComputerSystemEmpty; + EFI_REDFISH_COMPUTERSYSTEM_V1_13_0_CS *ComputerSystemCsEmpty; + EFI_STATUS Status; + BOOLEAN PropertyChanged; + CHAR8 *AsciiStringValue; + CHAR8 **AsciiStringArrayValue; + UINTN ArraySize; + + if ((JsonStructProtocol == NULL) || (ResultJson == NULL) || IS_EMPTY_STRING (InputJson) || IS_EMPTY_STRING (ConfigureLang)) { + return EFI_INVALID_PARAMETER; + } + + DEBUG ((REDFISH_DEBUG_TRACE, "%a provision for %s with: %s\n", __func__, ConfigureLang, (ProvisionMode ? L"Provision resource" : L"Update resource"))); + + *ResultJson = NULL; + PropertyChanged = FALSE; + + ComputerSystem = NULL; + Status = JsonStructProtocol->ToStructure ( + JsonStructProtocol, + NULL, + InputJson, + (EFI_REST_JSON_STRUCTURE_HEADER **)&ComputerSystem + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: ToStructure failure: %r\n", __func__, Status)); + return Status; + } + + ComputerSystemEmpty = NULL; + Status = JsonStructProtocol->ToStructure ( + JsonStructProtocol, + NULL, + ComputerSystemEmptyJson, + (EFI_REST_JSON_STRUCTURE_HEADER **)&ComputerSystemEmpty + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: ToStructure failure: %r\n", __func__, Status)); + return Status; + } + + ComputerSystemCs = ComputerSystem->ComputerSystem; + // + // Initial an empty ComputerSystemCS + // + ComputerSystemCsEmpty = ComputerSystemEmpty->ComputerSystem; + + // + // Handle BOOT + // + if (ComputerSystemCs->Boot != NULL) { + // + // Handle BOOT->BOOTNEXT + // + if (PropertyChecker (ComputerSystemCs->Boot->BootNext, ProvisionMode)) { + AsciiStringValue = GetPropertyStringValue (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, L"Boot/BootNext", ConfigureLang); + if (AsciiStringValue != NULL) { + if (ProvisionMode || (AsciiStrCmp (ComputerSystemCs->Boot->BootNext, AsciiStringValue) != 0)) { + ComputerSystemCsEmpty->Boot->BootNext = AsciiStringValue; + PropertyChanged = TRUE; + } + } + } + + // + // Handle BOOT->BOOTORDER + // + if (PropertyChecker (ComputerSystemCs->Boot->BootOrder, ProvisionMode)) { + AsciiStringArrayValue = GetPropertyStringArrayValue (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, L"Boot/BootOrder", ConfigureLang, &ArraySize); + if (AsciiStringArrayValue != NULL) { + if (ProvisionMode || !CompareRedfishStringArrayValues (ComputerSystemCs->Boot->BootOrder, AsciiStringArrayValue, ArraySize)) { + AddRedfishCharArray (&ComputerSystemCsEmpty->Boot->BootOrder, AsciiStringArrayValue, ArraySize); + PropertyChanged = TRUE; + } + } + } + + // + // Handle BOOT->BOOTSOURCEOVERRIDEENABLED + // + if (PropertyChecker (ComputerSystemCs->Boot->BootSourceOverrideEnabled, ProvisionMode)) { + AsciiStringValue = GetPropertyStringValue (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, L"Boot/BootSourceOverrideEnabled", ConfigureLang); + if (AsciiStringValue != NULL) { + if (ProvisionMode || (AsciiStrCmp (ComputerSystemCs->Boot->BootSourceOverrideEnabled, AsciiStringValue) != 0)) { + ComputerSystemCsEmpty->Boot->BootSourceOverrideEnabled = AsciiStringValue; + PropertyChanged = TRUE; + } + } + } + + // + // Handle BOOT->BOOTSOURCEOVERRIDEMODE + // + if (PropertyChecker (ComputerSystemCs->Boot->BootSourceOverrideMode, ProvisionMode)) { + AsciiStringValue = GetPropertyStringValue (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, L"Boot/BootSourceOverrideMode", ConfigureLang); + if (AsciiStringValue != NULL) { + if (ProvisionMode || (AsciiStrCmp (ComputerSystemCs->Boot->BootSourceOverrideMode, AsciiStringValue) != 0)) { + ComputerSystemCsEmpty->Boot->BootSourceOverrideMode = AsciiStringValue; + PropertyChanged = TRUE; + } + } + } + + // + // Handle BOOT->BOOTSOURCEOVERRIDETARGET + // + if (PropertyChecker (ComputerSystemCs->Boot->BootSourceOverrideTarget, ProvisionMode)) { + AsciiStringValue = GetPropertyStringValue (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, L"Boot/BootSourceOverrideTarget", ConfigureLang); + if (AsciiStringValue != NULL) { + if (ProvisionMode || (AsciiStrCmp (ComputerSystemCs->Boot->BootSourceOverrideTarget, AsciiStringValue) != 0)) { + ComputerSystemCsEmpty->Boot->BootSourceOverrideTarget = AsciiStringValue; + PropertyChanged = TRUE; + } + } + } + + // + // Handle BOOT->UEFITARGETBOOTSOURCEOVERRIDE + // + if (PropertyChecker (ComputerSystemCs->Boot->UefiTargetBootSourceOverride, ProvisionMode)) { + AsciiStringValue = GetPropertyStringValue (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, L"Boot/UefiTargetBootSourceOverride", ConfigureLang); + if (AsciiStringValue != NULL) { + if (ProvisionMode || (AsciiStrCmp (ComputerSystemCs->Boot->UefiTargetBootSourceOverride, AsciiStringValue) != 0)) { + ComputerSystemCsEmpty->Boot->UefiTargetBootSourceOverride = AsciiStringValue; + PropertyChanged = TRUE; + } + } + } + } + + // + // Convert C structure back to JSON text. + // + Status = JsonStructProtocol->ToJson ( + JsonStructProtocol, + (EFI_REST_JSON_STRUCTURE_HEADER *)ComputerSystemEmpty, + ResultJson + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: ToJson() failed: %r\n", __func__, Status)); + return Status; + } + + if (PropertyChanged) { + // Remove Redfish unchangeable properties. + Status = RedfishRemoveUnchangeableProperties (ResultJson); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: Fail to remove Redfish unchangeable properties from ResultJson.\n", __func__)); + *ResultJson = NULL; + return Status; + } + } + + // + // Release resource. + // + JsonStructProtocol->DestoryStructure ( + JsonStructProtocol, + (EFI_REST_JSON_STRUCTURE_HEADER *)ComputerSystem + ); + + // + // Free memory allocated for Computersystem empty CS + // + if (ComputerSystemCsEmpty->Boot->BootOrder != NULL) { + DestoryRedfishCharArray (ComputerSystemCsEmpty->Boot->BootOrder, ArraySize); + } + + JsonStructProtocol->DestoryStructure ( + JsonStructProtocol, + (EFI_REST_JSON_STRUCTURE_HEADER *)ComputerSystemEmpty + ); + return (PropertyChanged ? EFI_SUCCESS : EFI_NOT_FOUND); +} + +EFI_STATUS +ProvisioningComputerSystemResource ( + IN REDFISH_RESOURCE_COMMON_PRIVATE *Private, + IN UINTN Index, + IN EFI_STRING ConfigureLang + ) +{ + CHAR8 *Json; + CHAR8 *JsonWithAddendum; + EFI_STATUS Status; + EFI_STRING NewResourceLocation; + CHAR8 *EtagStr; + CHAR8 ResourceId[16]; + + if (IS_EMPTY_STRING (ConfigureLang) || (Private == NULL)) { + return EFI_INVALID_PARAMETER; + } + + EtagStr = NULL; + AsciiSPrint (ResourceId, sizeof (ResourceId), "%d", Index); + + Status = ProvisioningComputerSystemProperties ( + Private->JsonStructProtocol, + ComputerSystemEmptyJson, + ResourceId, + ConfigureLang, + TRUE, + &Json + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: provisioning resource for %s failed: %r\n", __func__, ConfigureLang, Status)); + return Status; + } + + // + // Check and see if platform has OEM data or not + // + Status = RedfishGetOemData ( + Private->Uri, + RESOURCE_SCHEMA, + RESOURCE_SCHEMA_VERSION, + Json, + &JsonWithAddendum + ); + if (!EFI_ERROR (Status) && (JsonWithAddendum != NULL)) { + FreePool (Json); + Json = JsonWithAddendum; + JsonWithAddendum = NULL; + } + + // + // Check and see if platform has addendum data or not + // + Status = RedfishGetAddendumData ( + Private->Uri, + RESOURCE_SCHEMA, + RESOURCE_SCHEMA_VERSION, + Json, + &JsonWithAddendum + ); + if (!EFI_ERROR (Status) && (JsonWithAddendum != NULL)) { + FreePool (Json); + Json = JsonWithAddendum; + JsonWithAddendum = NULL; + } + + Status = CreatePayloadToPostResource (Private->RedfishService, Private->Payload, Json, &NewResourceLocation, &EtagStr); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: post ComputerSystem resource for %s failed: %r\n", __func__, ConfigureLang, Status)); + goto RELEASE_RESOURCE; + } + + ASSERT (NewResourceLocation != NULL); + + // + // Keep location of new resource. + // + if (NewResourceLocation != NULL) { + RedfishSetRedfishUri (ConfigureLang, NewResourceLocation); + } + +RELEASE_RESOURCE: + + if (EtagStr != NULL) { + FreePool (EtagStr); + } + + if (NewResourceLocation != NULL) { + FreePool (NewResourceLocation); + } + + if (Json != NULL) { + FreePool (Json); + } + + return Status; +} + +EFI_STATUS +ProvisioningComputerSystemResources ( + IN REDFISH_RESOURCE_COMMON_PRIVATE *Private + ) +{ + UINTN Index; + EFI_STATUS Status; + REDFISH_FEATURE_ARRAY_TYPE_CONFIG_LANG_LIST UnifiedConfigureLangList; + + if (Private == NULL) { + return EFI_INVALID_PARAMETER; + } + + Status = RedfishFeatureGetUnifiedArrayTypeConfigureLang (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, REDPATH_ARRAY_PATTERN, &UnifiedConfigureLangList); + if (EFI_ERROR (Status) || (UnifiedConfigureLangList.Count == 0)) { + DEBUG ((DEBUG_ERROR, "%a: No HII question found with configure language: %s: %r\n", __func__, REDPATH_ARRAY_PATTERN, Status)); + return EFI_NOT_FOUND; + } + + // + // Set the configuration language in the RESOURCE_INFORMATION_EXCHANGE. + // This information is sent back to the parent resource (e.g. the collection driver). + // + EdkIIRedfishResourceSetConfigureLang (mRedfishResourceConfigProtocolHandle, &UnifiedConfigureLangList); + + for (Index = 0; Index < UnifiedConfigureLangList.Count; Index++) { + DEBUG ((DEBUG_MANAGEABILITY, "[%d] create ComputerSystem resource from: %s\n", UnifiedConfigureLangList.List[Index].Index, UnifiedConfigureLangList.List[Index].ConfigureLang)); + ProvisioningComputerSystemResource (Private, UnifiedConfigureLangList.List[Index].Index, UnifiedConfigureLangList.List[Index].ConfigureLang); + FreePool (UnifiedConfigureLangList.List[Index].ConfigureLang); + } + + return EFI_SUCCESS; +} + +EFI_STATUS +ProvisioningComputerSystemExistResource ( + IN REDFISH_RESOURCE_COMMON_PRIVATE *Private + ) +{ + EFI_STATUS Status; + EFI_STRING ConfigureLang; + CHAR8 *Json; + CHAR8 *JsonWithAddendum; + + if (Private == NULL) { + return EFI_INVALID_PARAMETER; + } + + Json = NULL; + ConfigureLang = NULL; + + ConfigureLang = RedfishGetConfigLanguage (Private->Uri); + if (ConfigureLang == NULL) { + return EFI_NOT_FOUND; + } + + Status = ProvisioningComputerSystemProperties ( + Private->JsonStructProtocol, + ComputerSystemEmptyJson, + NULL, + ConfigureLang, + TRUE, + &Json + ); + if (EFI_ERROR (Status)) { + if (Status == EFI_NOT_FOUND) { + DEBUG ((DEBUG_MANAGEABILITY, "%a: provisioning existing resource for %s ignored. Nothing changed\n", __func__, ConfigureLang)); + Status = EFI_SUCCESS; + } else { + DEBUG ((DEBUG_ERROR, "%a: provisioning existing resource for %s failed: %r\n", __func__, ConfigureLang, Status)); + } + + goto ON_RELEASE; + } + + // + // Check and see if platform has OEM data or not + // + Status = RedfishGetOemData ( + Private->Uri, + RESOURCE_SCHEMA, + RESOURCE_SCHEMA_VERSION, + Json, + &JsonWithAddendum + ); + if (!EFI_ERROR (Status) && (JsonWithAddendum != NULL)) { + FreePool (Json); + Json = JsonWithAddendum; + JsonWithAddendum = NULL; + } + + // + // Check and see if platform has addendum data or not + // + Status = RedfishGetAddendumData ( + Private->Uri, + RESOURCE_SCHEMA, + RESOURCE_SCHEMA_VERSION, + Json, + &JsonWithAddendum + ); + if (!EFI_ERROR (Status) && (JsonWithAddendum != NULL)) { + FreePool (Json); + Json = JsonWithAddendum; + JsonWithAddendum = NULL; + } + + DEBUG ((DEBUG_MANAGEABILITY, "%a: provisioning existing resource for %s\n", __func__, ConfigureLang)); + + // + // PUT back to instance + // + Status = CreatePayloadToPatchResource (Private->RedfishService, Private->Payload, Json, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: patch resource for %s failed: %r\n", __func__, ConfigureLang, Status)); + } + +ON_RELEASE: + + if (Json != NULL) { + FreePool (Json); + } + + if (ConfigureLang != NULL) { + FreePool (ConfigureLang); + } + + return Status; +} + +/** + Provisioning redfish resource by given URI. + + @param[in] This Pointer to EFI_HP_REDFISH_HII_PROTOCOL instance. + @param[in] ResourceExist TRUE if resource exists, PUT method will be used. + FALSE if resource does not exist POST method is used. + + @retval EFI_SUCCESS Value is returned successfully. + @retval Others Some error happened. + +**/ +EFI_STATUS +RedfishProvisioningResourceCommon ( + IN REDFISH_RESOURCE_COMMON_PRIVATE *Private, + IN BOOLEAN ResourceExist + ) +{ + if (Private == NULL) { + return EFI_INVALID_PARAMETER; + } + + return (ResourceExist ? ProvisioningComputerSystemExistResource (Private) : ProvisioningComputerSystemResources (Private)); +} + +/** + Check resource from given URI. + + @param[in] This Pointer to REDFISH_RESOURCE_COMMON_PRIVATE instance. + @param[in] Json The JSON to consume. + @param[in] HeaderEtag The Etag string returned in HTTP header. + + @retval EFI_SUCCESS Value is returned successfully. + @retval Others Some error happened. + +**/ +EFI_STATUS +RedfishCheckResourceCommon ( + IN REDFISH_RESOURCE_COMMON_PRIVATE *Private, + IN CHAR8 *Json, + IN CHAR8 *HeaderEtag OPTIONAL + ) +{ + UINTN Index; + EFI_STATUS Status; + EFI_STRING *ConfigureLangList; + UINTN Count; + EFI_STRING Property; + + if ((Private == NULL) || IS_EMPTY_STRING (Json)) { + return EFI_INVALID_PARAMETER; + } + + // + // Check ETAG to see if we need to check it + // + if (CheckEtag (Private->Uri, HeaderEtag, NULL)) { + // + // No change + // + DEBUG ((DEBUG_MANAGEABILITY, "%a: ETAG: %s has no change, ignore check action\n", __func__, Private->Uri)); + return EFI_SUCCESS; + } + + Status = RedfishPlatformConfigGetConfigureLang (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, REDPATH_ARRAY_PATTERN, &ConfigureLangList, &Count); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: failed: %r\n", __func__, Status)); + return Status; + } + + if (Count == 0) { + return EFI_UNSUPPORTED; + } + + Status = EFI_SUCCESS; + for (Index = 0; Index < Count; Index++) { + Property = GetPropertyFromConfigureLang (Private->Uri, ConfigureLangList[Index]); + if (Property == NULL) { + continue; + } + + DEBUG ((DEBUG_MANAGEABILITY, "%a: [%d] check attribute for: %s\n", __func__, Index, Property)); + if (!MatchPropertyWithJsonContext (Property, Json)) { + DEBUG ((DEBUG_MANAGEABILITY, "%a: property is missing: %s\n", __func__, Property)); + Status = EFI_NOT_FOUND; + } + } + + FreePool (ConfigureLangList); + + return Status; +} + +/** + Update resource to given URI. + + @param[in] This Pointer to REDFISH_RESOURCE_COMMON_PRIVATE instance. + @param[in] Json The JSON to consume. + + @retval EFI_SUCCESS Value is returned successfully. + @retval Others Some error happened. + +**/ +EFI_STATUS +RedfishUpdateResourceCommon ( + IN REDFISH_RESOURCE_COMMON_PRIVATE *Private, + IN CHAR8 *InputJson + ) +{ + EFI_STATUS Status; + CHAR8 *Json; + CHAR8 *JsonWithAddendum; + EFI_STRING ConfigureLang; + + if ((Private == NULL) || IS_EMPTY_STRING (InputJson)) { + return EFI_INVALID_PARAMETER; + } + + Json = NULL; + ConfigureLang = NULL; + + ConfigureLang = RedfishGetConfigLanguage (Private->Uri); + if (ConfigureLang == NULL) { + return EFI_NOT_FOUND; + } + + Status = ProvisioningComputerSystemProperties ( + Private->JsonStructProtocol, + InputJson, + NULL, + ConfigureLang, + FALSE, + &Json + ); + if (EFI_ERROR (Status)) { + if (Status == EFI_NOT_FOUND) { + DEBUG ((DEBUG_MANAGEABILITY, "%a: update resource for %s ignored. Nothing changed\n", __func__, ConfigureLang)); + Status = EFI_SUCCESS; + } else { + DEBUG ((DEBUG_ERROR, "%a: update resource for %s failed: %r\n", __func__, ConfigureLang, Status)); + } + + goto ON_RELEASE; + } + + // + // Check and see if platform has OEM data or not + // + Status = RedfishGetOemData ( + Private->Uri, + RESOURCE_SCHEMA, + RESOURCE_SCHEMA_VERSION, + Json, + &JsonWithAddendum + ); + if (!EFI_ERROR (Status) && (JsonWithAddendum != NULL)) { + FreePool (Json); + Json = JsonWithAddendum; + JsonWithAddendum = NULL; + } + + // + // Check and see if platform has addendum data or not + // + Status = RedfishGetAddendumData ( + Private->Uri, + RESOURCE_SCHEMA, + RESOURCE_SCHEMA_VERSION, + Json, + &JsonWithAddendum + ); + if (!EFI_ERROR (Status) && (JsonWithAddendum != NULL)) { + FreePool (Json); + Json = JsonWithAddendum; + JsonWithAddendum = NULL; + } + + DEBUG ((REDFISH_DEBUG_TRACE, "%a: update resource for %s\n", __func__, ConfigureLang)); + + // + // PUT back to instance + // + Status = CreatePayloadToPatchResource (Private->RedfishService, Private->Payload, Json, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: patch resource for %s failed: %r\n", __func__, ConfigureLang, Status)); + } + +ON_RELEASE: + + if (Json != NULL) { + FreePool (Json); + } + + if (ConfigureLang != NULL) { + FreePool (ConfigureLang); + } + + return Status; +} + +/** + Identify resource from given URI. + + @param[in] This Pointer to REDFISH_RESOURCE_COMMON_PRIVATE instance. + @param[in] Json The JSON to consume. + + @retval EFI_SUCCESS Value is returned successfully. + @retval Others Some error happened. + +**/ +EFI_STATUS +RedfishIdentifyResourceCommon ( + IN REDFISH_RESOURCE_COMMON_PRIVATE *Private, + IN CHAR8 *Json + ) +{ + BOOLEAN Supported; + EFI_STATUS Status; + EFI_STRING EndOfChar; + REDFISH_FEATURE_ARRAY_TYPE_CONFIG_LANG_LIST ConfigLangList; + + Supported = RedfishIdentifyResource (Private->Uri, Private->Json); + if (Supported) { + Status = RedfishFeatureGetUnifiedArrayTypeConfigureLang (RESOURCE_SCHEMA, RESOURCE_SCHEMA_VERSION, REDPATH_ARRAY_PATTERN, &ConfigLangList); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: RedfishFeatureGetUnifiedArrayTypeConfigureLang failed: %r\n", __func__, Status)); + return Status; + } + + if (ConfigLangList.Count == 0) { + DEBUG ((DEBUG_MANAGEABILITY, " No platform Redfish ConfigureLang found for %s\n", __func__, Private->Uri)); + return EFI_SUCCESS; + } + + EndOfChar = StrStr (ConfigLangList.List[0].ConfigureLang, L"}"); + if (EndOfChar == NULL) { + ASSERT (FALSE); + return EFI_DEVICE_ERROR; + } + + // EndOfChar = StrStr (ConfigLangList.List[0].ConfigureLang, L"}"); + Status = IsRedpathArray (ConfigLangList.List[0].ConfigureLang, NULL, &EndOfChar); + if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) { + ASSERT (FALSE); + return EFI_DEVICE_ERROR; + } + + if (Status != EFI_SUCCESS) { + // + // This is not the collection redpath. + // + GetRedpathNodeByIndex (ConfigLangList.List[0].ConfigureLang, 0, &EndOfChar); + } + + *(++EndOfChar) = '\0'; + // + // Keep URI and ConfigLang mapping + // + RedfishSetRedfishUri (ConfigLangList.List[0].ConfigureLang, Private->Uri); + // + // Set the configuration language in the RESOURCE_INFORMATION_EXCHANGE. + // This information is sent back to the parent resource (e.g. the collection driver). + // + EdkIIRedfishResourceSetConfigureLang (mRedfishResourceConfigProtocolHandle, &ConfigLangList); + DestroyConfiglanguageList (&ConfigLangList); + return EFI_SUCCESS; + } + + return EFI_UNSUPPORTED; +} diff --git a/RedfishClientPkg/Features/ComputerSystem/v1_13_0/Common/ComputerSystemCommon.h b/RedfishClientPkg/Features/ComputerSystem/v1_13_0/Common/ComputerSystemCommon.h new file mode 100644 index 000000000..c9c851288 --- /dev/null +++ b/RedfishClientPkg/Features/ComputerSystem/v1_13_0/Common/ComputerSystemCommon.h @@ -0,0 +1,29 @@ +/** @file + + Redfish feature driver implementation - internal header file + (C) Copyright 2020-2022 Hewlett Packard Enterprise Development LP
+ Copyright (c) 2022-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef EFI_REDFISH_COMPUTERSYSTEM_COMMON_H_ +#define EFI_REDFISH_COMPUTERSYSTEM_COMMON_H_ + +#include +#include + +// +// Schema information. +// +#define RESOURCE_SCHEMA "ComputerSystem" +#define RESOURCE_SCHEMA_MAJOR "1" +#define RESOURCE_SCHEMA_MINOR "13" +#define RESOURCE_SCHEMA_ERRATA "0" +#define RESOURCE_SCHEMA_VERSION "v1_13_0" +#define REDPATH_ARRAY_PATTERN L"/Systems/\\{.*\\}/" +#define REDPATH_ARRAY_PREFIX L"/Systems/" +#define RESOURCE_SCHEMA_FULL "x-uefi-redfish-ComputerSystem.v1_13_0" + +#endif diff --git a/RedfishClientPkg/Features/ComputerSystem/v1_13_0/Dxe/ComputerSystemDxe.c b/RedfishClientPkg/Features/ComputerSystem/v1_13_0/Dxe/ComputerSystemDxe.c new file mode 100644 index 000000000..30a0e64c6 --- /dev/null +++ b/RedfishClientPkg/Features/ComputerSystem/v1_13_0/Dxe/ComputerSystemDxe.c @@ -0,0 +1,702 @@ +/** @file + Redfish feature driver implementation - ComputerSystem + + (C) Copyright 2020-2022 Hewlett Packard Enterprise Development LP
+ Copyright (c) 2023-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "../Common/ComputerSystemCommon.h" + +extern REDFISH_RESOURCE_COMMON_PRIVATE *mRedfishResourcePrivate; +extern EFI_HANDLE mRedfishResourceConfigProtocolHandle; + +/** + Provision redfish resource by given URI. + + @param[in] This Pointer to EFI_HP_REDFISH_HII_PROTOCOL instance. + @param[in] Uri Target URI to create resource. + @param[in] PostMode TRUE if the resource does not exist, post method is used. + FALSE if the resource exist but property is missing, put method is used. + + @retval EFI_SUCCESS Value is returned successfully. + @retval Others Some error happened. + +**/ +EFI_STATUS +EFIAPI +RedfishResourceProvisioningResource ( + IN EDKII_REDFISH_RESOURCE_CONFIG_PROTOCOL *This, + IN EFI_STRING Uri, + IN BOOLEAN PostMode + ) +{ + REDFISH_RESOURCE_COMMON_PRIVATE *Private; + EFI_STATUS Status; + REDFISH_RESPONSE Response; + + if ((This == NULL) || IS_EMPTY_STRING (Uri)) { + return EFI_INVALID_PARAMETER; + } + + DEBUG ((DEBUG_MANAGEABILITY, "%a: provisioning in %s mode\n", __func__, (PostMode ? L"POST" : L"PATCH"))); + + Private = REDFISH_RESOURCE_COMMON_PRIVATE_DATA_FROM_RESOURCE_PROTOCOL (This); + + if (Private->RedfishService == NULL) { + return EFI_NOT_READY; + } + + ZeroMem (&Response, sizeof (Response)); + Status = RedfishHttpGetResource (Private->RedfishService, Uri, &Response, TRUE); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: get resource from: %s failed\n", __func__, Uri)); + return Status; + } + + Private->Uri = Uri; + Private->Payload = Response.Payload; + ASSERT (Private->Payload != NULL); + + Status = RedfishProvisioningResourceCommon (Private, !PostMode); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: failed to provision resource to: %s: %r\n", __func__, Uri, Status)); + } else { + // + // Get latest ETag on URI and keep it in variable. + // + SetEtagFromUri (Private->RedfishService, Private->Uri, TRUE); + } + + // + // Release resource + // + if (Private->Payload != NULL) { + RedfishFreeResponse ( + Response.StatusCode, + Response.HeaderCount, + Response.Headers, + Response.Payload + ); + Private->Payload = NULL; + } + + return Status; +} + +/** + Consume resource from given URI. + + @param[in] This Pointer to EFI_HP_REDFISH_HII_PROTOCOL instance. + @param[in] Uri The target URI to consume. + + @retval EFI_SUCCESS Value is returned successfully. + @retval Others Some error happened. + +**/ +EFI_STATUS +EFIAPI +RedfishResourceConsumeResource ( + IN EDKII_REDFISH_RESOURCE_CONFIG_PROTOCOL *This, + IN EFI_STRING Uri + ) +{ + REDFISH_RESOURCE_COMMON_PRIVATE *Private; + EFI_STATUS Status; + REDFISH_RESPONSE Response; + EFI_STRING PendingSettingUri; + REDFISH_RESPONSE PendingSettingResponse; + REDFISH_RESPONSE *ExpectedResponse; + CHAR8 *Etag; + + if ((This == NULL) || IS_EMPTY_STRING (Uri)) { + return EFI_INVALID_PARAMETER; + } + + Private = REDFISH_RESOURCE_COMMON_PRIVATE_DATA_FROM_RESOURCE_PROTOCOL (This); + + if (Private->RedfishService == NULL) { + return EFI_NOT_READY; + } + + ZeroMem (&Response, sizeof (Response)); + Status = RedfishHttpGetResource (Private->RedfishService, Uri, &Response, TRUE); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: get resource from: %s failed\n", __func__, Uri)); + return Status; + } + + // + // Check and see if "@Redfish.Settings" exist or not. + // + ZeroMem (&PendingSettingResponse, sizeof (REDFISH_RESPONSE)); + Status = GetPendingSettings ( + Private->RedfishService, + Response.Payload, + &PendingSettingResponse, + &PendingSettingUri + ); + if (!EFI_ERROR (Status)) { + DEBUG ((REDFISH_DEBUG_TRACE, "%a: @Redfish.Settings found: %s\n", __func__, PendingSettingUri)); + Private->Uri = PendingSettingUri; + ExpectedResponse = &PendingSettingResponse; + } else { + DEBUG ((REDFISH_DEBUG_TRACE, "%a: No @Redfish.Settings is found\n", __func__)); + Private->Uri = Uri; + ExpectedResponse = &Response; + } + + Private->Payload = ExpectedResponse->Payload; + ASSERT (Private->Payload != NULL); + + Private->Json = JsonDumpString (RedfishJsonInPayload (Private->Payload), EDKII_JSON_COMPACT); + ASSERT (Private->Json != NULL); + + // + // Find etag in HTTP response header + // + Etag = NULL; + GetHttpResponseEtag (ExpectedResponse, &Etag); + Status = RedfishConsumeResourceCommon (Private, Private->Json, Etag); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: failed to consume resource from: %s: %r\n", __func__, Private->Uri, Status)); + } + + // + // Release resource + // + if (Etag != NULL) { + FreePool (Etag); + } + + // + // Release resource + // + if (Private->Payload != NULL) { + if (Response.Payload != NULL) { + RedfishFreeResponse ( + Response.StatusCode, + Response.HeaderCount, + Response.Headers, + Response.Payload + ); + } + + if (PendingSettingResponse.Payload != NULL) { + RedfishFreeResponse ( + PendingSettingResponse.StatusCode, + PendingSettingResponse.HeaderCount, + PendingSettingResponse.Headers, + PendingSettingResponse.Payload + ); + } + + Private->Payload = NULL; + } + + if (Private->Json != NULL) { + FreePool (Private->Json); + Private->Json = NULL; + } + + return Status; +} + +/** + Get information about this protocol. + + @param[in] This Pointer to EFI_HP_REDFISH_HII_PROTOCOL instance. + @param[out] Schema Supported schema. + @param[out] Major Supported major number. + @param[out] Minor Supported minor number. + @param[out] Errata Supported errata number. + + @retval EFI_SUCCESS Value is returned successfully. + @retval Others Some error happened. + +**/ +EFI_STATUS +EFIAPI +RedfishResourceGetInfo ( + IN EDKII_REDFISH_RESOURCE_CONFIG_PROTOCOL *This, + OUT REDFISH_SCHEMA_INFO *Info + ) +{ + if ((This == NULL) || (Info == NULL)) { + return EFI_INVALID_PARAMETER; + } + + AsciiStrCpyS (Info->Schema, REDFISH_SCHEMA_STRING_SIZE, RESOURCE_SCHEMA); + AsciiStrCpyS (Info->Major, REDFISH_SCHEMA_VERSION_SIZE, RESOURCE_SCHEMA_MAJOR); + AsciiStrCpyS (Info->Minor, REDFISH_SCHEMA_VERSION_SIZE, RESOURCE_SCHEMA_MINOR); + AsciiStrCpyS (Info->Errata, REDFISH_SCHEMA_VERSION_SIZE, RESOURCE_SCHEMA_ERRATA); + + return EFI_SUCCESS; +} + +/** + Update resource to given URI. + + @param[in] This Pointer to EFI_HP_REDFISH_HII_PROTOCOL instance. + @param[in] Uri The target URI to consume. + + @retval EFI_SUCCESS Value is returned successfully. + @retval Others Some error happened. + +**/ +EFI_STATUS +EFIAPI +RedfishResourceUpdate ( + IN EDKII_REDFISH_RESOURCE_CONFIG_PROTOCOL *This, + IN EFI_STRING Uri + ) +{ + REDFISH_RESOURCE_COMMON_PRIVATE *Private; + EFI_STATUS Status; + REDFISH_RESPONSE Response; + + if ((This == NULL) || IS_EMPTY_STRING (Uri)) { + return EFI_INVALID_PARAMETER; + } + + Private = REDFISH_RESOURCE_COMMON_PRIVATE_DATA_FROM_RESOURCE_PROTOCOL (This); + + if (Private->RedfishService == NULL) { + return EFI_NOT_READY; + } + + ZeroMem (&Response, sizeof (Response)); + Status = RedfishHttpGetResource (Private->RedfishService, Uri, &Response, TRUE); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: get resource from: %s failed\n", __func__, Uri)); + return Status; + } + + Private->Uri = Uri; + Private->Payload = Response.Payload; + ASSERT (Private->Payload != NULL); + + Private->Json = JsonDumpString (RedfishJsonInPayload (Private->Payload), EDKII_JSON_COMPACT); + ASSERT (Private->Json != NULL); + + Status = RedfishUpdateResourceCommon (Private, Private->Json); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: failed to update resource to: %s: %r\n", __func__, Uri, Status)); + } else { + // + // Get latest ETag on URI and keep it in variable. + // + SetEtagFromUri (Private->RedfishService, Private->Uri, TRUE); + } + + // + // Release resource + // + if (Private->Payload != NULL) { + RedfishFreeResponse ( + Response.StatusCode, + Response.HeaderCount, + Response.Headers, + Response.Payload + ); + RedfishHttpResetResource (Uri); + Private->Payload = NULL; + } + + if (Private->Json != NULL) { + FreePool (Private->Json); + Private->Json = NULL; + } + + return Status; +} + +/** + Check resource on given URI. + + @param[in] This Pointer to EFI_HP_REDFISH_HII_PROTOCOL instance. + @param[in] Uri The target URI to consume. + + @retval EFI_SUCCESS Value is returned successfully. + @retval Others Some error happened. + +**/ +EFI_STATUS +EFIAPI +RedfishResourceCheck ( + IN EDKII_REDFISH_RESOURCE_CONFIG_PROTOCOL *This, + IN EFI_STRING Uri + ) +{ + REDFISH_RESOURCE_COMMON_PRIVATE *Private; + EFI_STATUS Status; + REDFISH_RESPONSE Response; + CHAR8 *Etag; + + if ((This == NULL) || IS_EMPTY_STRING (Uri)) { + return EFI_INVALID_PARAMETER; + } + + Private = REDFISH_RESOURCE_COMMON_PRIVATE_DATA_FROM_RESOURCE_PROTOCOL (This); + + if (Private->RedfishService == NULL) { + return EFI_NOT_READY; + } + + ZeroMem (&Response, sizeof (Response)); + Status = RedfishHttpGetResource (Private->RedfishService, Uri, &Response, TRUE); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: get resource from: %s failed\n", __func__, Uri)); + return Status; + } + + Private->Uri = Uri; + Private->Payload = Response.Payload; + ASSERT (Private->Payload != NULL); + + Private->Json = JsonDumpString (RedfishJsonInPayload (Private->Payload), EDKII_JSON_COMPACT); + ASSERT (Private->Json != NULL); + + // + // Find etag in HTTP response header + // + Etag = NULL; + GetHttpResponseEtag (&Response, &Etag); + Status = RedfishCheckResourceCommon (Private, Private->Json, Etag); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: failed to check resource from: %s: %r\n", __func__, Uri, Status)); + } + + // + // Release resource + // + if (Etag != NULL) { + FreePool (Etag); + } + + if (Private->Payload != NULL) { + RedfishFreeResponse ( + Response.StatusCode, + Response.HeaderCount, + Response.Headers, + Response.Payload + ); + Private->Payload = NULL; + } + + if (Private->Json != NULL) { + FreePool (Private->Json); + Private->Json = NULL; + } + + return Status; +} + +/** + Identify resource on given URI. + + @param[in] This Pointer to EDKII_REDFISH_RESOURCE_CONFIG_PROTOCOL instance. + @param[in] Uri The target URI to consume. + + @retval EFI_SUCCESS This is target resource which we want to handle. + @retval EFI_UNSUPPORTED This is not the target resource. + @retval Others Some error happened. + +**/ +EFI_STATUS +EFIAPI +RedfishResourceIdentify ( + IN EDKII_REDFISH_RESOURCE_CONFIG_PROTOCOL *This, + IN EFI_STRING Uri + ) +{ + REDFISH_RESOURCE_COMMON_PRIVATE *Private; + EFI_STATUS Status; + REDFISH_RESPONSE Response; + + if ((This == NULL) || IS_EMPTY_STRING (Uri)) { + return EFI_INVALID_PARAMETER; + } + + Private = REDFISH_RESOURCE_COMMON_PRIVATE_DATA_FROM_RESOURCE_PROTOCOL (This); + + if (Private->RedfishService == NULL) { + return EFI_NOT_READY; + } + + ZeroMem (&Response, sizeof (Response)); + Status = RedfishHttpGetResource (Private->RedfishService, Uri, &Response, TRUE); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: get resource from: %s failed\n", __func__, Uri)); + return Status; + } + + Private->Uri = Uri; + Private->Payload = Response.Payload; + ASSERT (Private->Payload != NULL); + + Private->Json = JsonDumpString (RedfishJsonInPayload (Private->Payload), EDKII_JSON_COMPACT); + ASSERT (Private->Json != NULL); + + Status = RedfishIdentifyResourceCommon (Private, Private->Json); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: identify %s failed: %r\n", __func__, Uri, Status)); + } + + // + // Release resource + // + if (Private->Payload != NULL) { + RedfishFreeResponse ( + Response.StatusCode, + Response.HeaderCount, + Response.Headers, + Response.Payload + ); + Private->Payload = NULL; + } + + if (Private->Json != NULL) { + FreePool (Private->Json); + Private->Json = NULL; + } + + return Status; +} + +EDKII_REDFISH_RESOURCE_CONFIG_PROTOCOL mRedfishResourceConfig = { + RedfishResourceProvisioningResource, + RedfishResourceConsumeResource, + RedfishResourceUpdate, + RedfishResourceCheck, + RedfishResourceIdentify, + RedfishResourceGetInfo +}; + +/** + Initialize a Redfish configure handler. + + This function will be called by the Redfish config driver to initialize each Redfish configure + handler. + + @param[in] This Pointer to EDKII_REDFISH_CONFIG_HANDLER_PROTOCOL instance. + @param[in] RedfishConfigServiceInfo Redfish service information. + + @retval EFI_SUCCESS The handler has been initialized successfully. + @retval EFI_DEVICE_ERROR Failed to create or configure the REST EX protocol instance. + @retval EFI_ALREADY_STARTED This handler has already been initialized. + @retval Other Error happens during the initialization. + +**/ +EFI_STATUS +EFIAPI +RedfishResourceInit ( + IN EDKII_REDFISH_CONFIG_HANDLER_PROTOCOL *This, + IN REDFISH_CONFIG_SERVICE_INFORMATION *RedfishConfigServiceInfo + ) +{ + REDFISH_RESOURCE_COMMON_PRIVATE *Private; + + Private = REDFISH_RESOURCE_COMMON_PRIVATE_DATA_FROM_CONFIG_PROTOCOL (This); + + Private->RedfishService = RedfishCreateService (RedfishConfigServiceInfo); + if (Private->RedfishService == NULL) { + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + +/** + Stop a Redfish configure handler. + + @param[in] This Pointer to EDKII_REDFISH_CONFIG_HANDLER_PROTOCOL instance. + + @retval EFI_SUCCESS This handler has been stoped successfully. + @retval Others Some error happened. + +**/ +EFI_STATUS +EFIAPI +RedfishResourceStop ( + IN EDKII_REDFISH_CONFIG_HANDLER_PROTOCOL *This + ) +{ + REDFISH_RESOURCE_COMMON_PRIVATE *Private; + + Private = REDFISH_RESOURCE_COMMON_PRIVATE_DATA_FROM_CONFIG_PROTOCOL (This); + + if (Private->Event != NULL) { + gBS->CloseEvent (Private->Event); + Private->Event = NULL; + } + + if (Private->RedfishService != NULL) { + RedfishCleanupService (Private->RedfishService); + Private->RedfishService = NULL; + } + + if (Private->Payload != NULL) { + RedfishCleanupPayload (Private->Payload); + Private->Payload = NULL; + } + + return EFI_SUCCESS; +} + +EDKII_REDFISH_CONFIG_HANDLER_PROTOCOL mRedfishConfigHandler = { + RedfishResourceInit, + RedfishResourceStop +}; + +/** + Callback function when gEfiRestJsonStructureProtocolGuid is installed. + + @param[in] Event Event whose notification function is being invoked. + @param[in] Context Pointer to the notification function's context. +**/ +VOID +EFIAPI +EfiRestJasonStructureProtocolIsReady ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EFI_STATUS Status; + + if (mRedfishResourcePrivate == NULL) { + return; + } + + if (mRedfishResourcePrivate->JsonStructProtocol != NULL) { + return; + } + + Status = gBS->LocateProtocol ( + &gEfiRestJsonStructureProtocolGuid, + NULL, + (VOID **)&mRedfishResourcePrivate->JsonStructProtocol + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: failed to locate gEfiRestJsonStructureProtocolGuid: %r\n", __func__, Status)); + } + + gBS->CloseEvent (Event); +} + +/** + Unloads an image. + + @param ImageHandle Handle that identifies the image to be unloaded. + + @retval EFI_SUCCESS The image has been unloaded. + @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle. + +**/ +EFI_STATUS +EFIAPI +RedfishResourceUnload ( + IN EFI_HANDLE ImageHandle + ) +{ + EFI_STATUS Status; + EDKII_REDFISH_CONFIG_HANDLER_PROTOCOL *ConfigHandler; + + if (mRedfishResourcePrivate == NULL) { + return EFI_NOT_READY; + } + + ConfigHandler = NULL; + + // + // Firstly, find ConfigHandler Protocol interface in this ImageHandle. + // + Status = gBS->OpenProtocol ( + ImageHandle, + &gEdkIIRedfishConfigHandlerProtocolGuid, + (VOID **)&ConfigHandler, + NULL, + NULL, + EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL + ); + if (EFI_ERROR (Status) || (ConfigHandler == NULL)) { + return Status; + } + + ConfigHandler->Stop (ConfigHandler); + + // + // Last, uninstall ConfigHandler Protocol and resource protocol. + // + Status = gBS->UninstallMultipleProtocolInterfaces ( + ImageHandle, + &gEdkIIRedfishConfigHandlerProtocolGuid, + ConfigHandler, + &gEdkIIRedfishResourceConfigProtocolGuid, + &mRedfishResourcePrivate->RedfishResourceConfig, + NULL + ); + + FreePool (mRedfishResourcePrivate); + mRedfishResourcePrivate = NULL; + + return Status; +} + +/** + This is the declaration of an EFI image entry point. This entry point is + the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including + both device drivers and bus drivers. It initialize the global variables and + publish the driver binding protocol. + + @param[in] ImageHandle The firmware allocated handle for the UEFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The operation completed successfully. + @retval Others Other errors as indicated. +**/ +EFI_STATUS +EFIAPI +RedfishResourceEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + VOID *Registration; + + if (mRedfishResourcePrivate != NULL) { + return EFI_ALREADY_STARTED; + } + + mRedfishResourceConfigProtocolHandle = ImageHandle; + + mRedfishResourcePrivate = AllocateZeroPool (sizeof (REDFISH_RESOURCE_COMMON_PRIVATE)); + CopyMem (&mRedfishResourcePrivate->ConfigHandler, &mRedfishConfigHandler, sizeof (EDKII_REDFISH_CONFIG_HANDLER_PROTOCOL)); + CopyMem (&mRedfishResourcePrivate->RedfishResourceConfig, &mRedfishResourceConfig, sizeof (EDKII_REDFISH_RESOURCE_CONFIG_PROTOCOL)); + + // + // Publish config handler protocol and resource protocol. + // + Status = gBS->InstallMultipleProtocolInterfaces ( + &ImageHandle, + &gEdkIIRedfishConfigHandlerProtocolGuid, + &mRedfishResourcePrivate->ConfigHandler, + &gEdkIIRedfishResourceConfigProtocolGuid, + &mRedfishResourcePrivate->RedfishResourceConfig, + NULL + ); + + EfiCreateProtocolNotifyEvent ( + &gEfiRestJsonStructureProtocolGuid, + TPL_CALLBACK, + EfiRestJasonStructureProtocolIsReady, + NULL, + &Registration + ); + + return Status; +} diff --git a/RedfishClientPkg/Features/ComputerSystem/v1_13_0/Dxe/ComputerSystemDxe.inf b/RedfishClientPkg/Features/ComputerSystem/v1_13_0/Dxe/ComputerSystemDxe.inf new file mode 100644 index 000000000..6bc5cb358 --- /dev/null +++ b/RedfishClientPkg/Features/ComputerSystem/v1_13_0/Dxe/ComputerSystemDxe.inf @@ -0,0 +1,53 @@ +## @file +# +# (C) Copyright 2020-2022 Hewlett Packard Enterprise Development LP
+# Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = ComputerSystemDxe + FILE_GUID = D94362A1-C358-4059-B014-F07D1AE04F16 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = RedfishResourceEntryPoint + UNLOAD_IMAGE = RedfishResourceUnload + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + RedfishPkg/RedfishPkg.dec + RedfishClientPkg/RedfishClientPkg.dec + +[Sources] + ../Common/ComputerSystemCommon.h + ../Common/ComputerSystemCommon.c + ComputerSystemDxe.c + +[LibraryClasses] + BaseMemoryLib + DebugLib + EdkIIRedfishResourceConfigLib + RedfishLib + RedfishFeatureUtilityLib + RedfishResourceIdentifyLib + UefiLib + UefiDriverEntryPoint + RedfishAddendumLib + RedfishHttpCacheLib + +[Protocols] + gEdkIIRedfishConfigHandlerProtocolGuid ## PRODUCED + gEfiRestJsonStructureProtocolGuid ## CONSUMED + gEdkIIRedfishResourceConfigProtocolGuid ## PRODUCED + +[Pcd] + gEfiRedfishClientPkgTokenSpaceGuid.PcdMaxRedfishSchemaStringSize + gEfiRedfishClientPkgTokenSpaceGuid.PcdMaxRedfishSchemaVersionSize + +[Depex] + TRUE