]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/PlatformDxe/PlatformConfig.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / OvmfPkg / PlatformDxe / PlatformConfig.c
CommitLineData
5267c89b
LE
1/** @file\r
2\r
3 Utility functions for serializing (persistently storing) and deserializing\r
4 OVMF's platform configuration.\r
5\r
6 Copyright (C) 2014, Red Hat, Inc.\r
7\r
b26f0cf9 8 SPDX-License-Identifier: BSD-2-Clause-Patent\r
5267c89b
LE
9\r
10**/\r
11\r
12#include <Library/BaseMemoryLib.h>\r
13#include <Library/DebugLib.h>\r
14#include <Library/MemoryAllocationLib.h>\r
15#include <Library/UefiLib.h>\r
16#include <Library/UefiRuntimeServicesTableLib.h>\r
17#include <Guid/OvmfPlatformConfig.h>\r
18\r
19#include "PlatformConfig.h"\r
20\r
21//\r
22// Name of the UEFI variable that we use for persistent storage.\r
23//\r
aefcc918 24CHAR16 mVariableName[] = L"PlatformConfig";\r
16acacf2 25CHAR16 mHiiFormName[] = L"MainFormState";\r
5267c89b
LE
26\r
27/**\r
28 Serialize and persistently save platform configuration.\r
29\r
30 @param[in] PlatformConfig The platform configuration to serialize and save.\r
31\r
32 @return Status codes returned by gRT->SetVariable().\r
33**/\r
34EFI_STATUS\r
35EFIAPI\r
36PlatformConfigSave (\r
ac0a286f 37 IN PLATFORM_CONFIG *PlatformConfig\r
5267c89b
LE
38 )\r
39{\r
ac0a286f 40 EFI_STATUS Status;\r
5267c89b
LE
41\r
42 //\r
43 // We could implement any kind of translation here, as part of serialization.\r
44 // For example, we could expose the platform configuration in separate\r
45 // variables with human-readable contents, allowing other tools to access\r
46 // them more easily. For now, just save a binary dump.\r
47 //\r
ac0a286f
MK
48 Status = gRT->SetVariable (\r
49 mVariableName,\r
50 &gOvmfPlatformConfigGuid,\r
5267c89b 51 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS |\r
ac0a286f
MK
52 EFI_VARIABLE_RUNTIME_ACCESS,\r
53 sizeof *PlatformConfig,\r
54 PlatformConfig\r
55 );\r
5267c89b
LE
56 return Status;\r
57}\r
58\r
5267c89b
LE
59/**\r
60 Load and deserialize platform configuration.\r
61\r
62 When the function fails, output parameters are indeterminate.\r
63\r
64 @param[out] PlatformConfig The platform configuration to receive the\r
65 loaded data.\r
66\r
67 @param[out] OptionalElements This bitmap describes the presence of optional\r
68 configuration elements that have been loaded.\r
69 PLATFORM_CONFIG_F_DOWNGRADE means that some\r
70 unknown elements, present in the wire format,\r
71 have been ignored.\r
72\r
73 @retval EFI_SUCCESS Loading & deserialization successful.\r
74 @return Error codes returned by GetVariable2().\r
75**/\r
76EFI_STATUS\r
77EFIAPI\r
78PlatformConfigLoad (\r
ac0a286f
MK
79 OUT PLATFORM_CONFIG *PlatformConfig,\r
80 OUT UINT64 *OptionalElements\r
5267c89b
LE
81 )\r
82{\r
ac0a286f
MK
83 VOID *Data;\r
84 UINTN DataSize;\r
85 EFI_STATUS Status;\r
5267c89b
LE
86\r
87 //\r
88 // Any translation done in PlatformConfigSave() would have to be mirrored\r
89 // here. For now, just load the binary dump.\r
90 //\r
91 // Versioning of the binary wire format is implemented based on size\r
92 // (only incremental changes, ie. new fields), and on GUID.\r
93 // (Incompatible changes require a GUID change.)\r
94 //\r
ac0a286f
MK
95 Status = GetVariable2 (\r
96 mVariableName,\r
97 &gOvmfPlatformConfigGuid,\r
98 &Data,\r
99 &DataSize\r
100 );\r
5267c89b
LE
101 if (EFI_ERROR (Status)) {\r
102 return Status;\r
103 }\r
104\r
105 *OptionalElements = 0;\r
106 if (DataSize > sizeof *PlatformConfig) {\r
107 //\r
108 // Handle firmware downgrade -- keep only leading part.\r
109 //\r
110 CopyMem (PlatformConfig, Data, sizeof *PlatformConfig);\r
111 *OptionalElements |= PLATFORM_CONFIG_F_DOWNGRADE;\r
112 } else {\r
113 CopyMem (PlatformConfig, Data, DataSize);\r
114\r
115 //\r
116 // Handle firmware upgrade -- zero out missing fields.\r
117 //\r
ac0a286f
MK
118 ZeroMem (\r
119 (UINT8 *)PlatformConfig + DataSize,\r
120 sizeof *PlatformConfig - DataSize\r
121 );\r
5267c89b
LE
122 }\r
123\r
124 //\r
125 // Based on DataSize, report the optional features that we recognize.\r
126 //\r
127 if (DataSize >= (OFFSET_OF (PLATFORM_CONFIG, VerticalResolution) +\r
ac0a286f
MK
128 sizeof PlatformConfig->VerticalResolution))\r
129 {\r
5267c89b
LE
130 *OptionalElements |= PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION;\r
131 }\r
132\r
133 FreePool (Data);\r
134 return EFI_SUCCESS;\r
135}\r