]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/PlatformDxe/PlatformConfig.c
OvmfPkg: PlatformDxe: utility functions for saving / loading configuration
[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
8 This program and the accompanying materials are licensed and made available\r
9 under the terms and conditions of the BSD License which accompanies this\r
10 distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
14 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include <Library/BaseMemoryLib.h>\r
19#include <Library/DebugLib.h>\r
20#include <Library/MemoryAllocationLib.h>\r
21#include <Library/UefiLib.h>\r
22#include <Library/UefiRuntimeServicesTableLib.h>\r
23#include <Guid/OvmfPlatformConfig.h>\r
24\r
25#include "PlatformConfig.h"\r
26\r
27//\r
28// Name of the UEFI variable that we use for persistent storage.\r
29//\r
30STATIC CHAR16 mVariableName[] = L"PlatformConfig";\r
31\r
32\r
33/**\r
34 Serialize and persistently save platform configuration.\r
35\r
36 @param[in] PlatformConfig The platform configuration to serialize and save.\r
37\r
38 @return Status codes returned by gRT->SetVariable().\r
39**/\r
40EFI_STATUS\r
41EFIAPI\r
42PlatformConfigSave (\r
43 IN PLATFORM_CONFIG *PlatformConfig\r
44 )\r
45{\r
46 EFI_STATUS Status;\r
47\r
48 //\r
49 // We could implement any kind of translation here, as part of serialization.\r
50 // For example, we could expose the platform configuration in separate\r
51 // variables with human-readable contents, allowing other tools to access\r
52 // them more easily. For now, just save a binary dump.\r
53 //\r
54 Status = gRT->SetVariable (mVariableName, &gOvmfPlatformConfigGuid,\r
55 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS |\r
56 EFI_VARIABLE_RUNTIME_ACCESS,\r
57 sizeof *PlatformConfig, PlatformConfig);\r
58 return Status;\r
59}\r
60\r
61\r
62/**\r
63 Load and deserialize platform configuration.\r
64\r
65 When the function fails, output parameters are indeterminate.\r
66\r
67 @param[out] PlatformConfig The platform configuration to receive the\r
68 loaded data.\r
69\r
70 @param[out] OptionalElements This bitmap describes the presence of optional\r
71 configuration elements that have been loaded.\r
72 PLATFORM_CONFIG_F_DOWNGRADE means that some\r
73 unknown elements, present in the wire format,\r
74 have been ignored.\r
75\r
76 @retval EFI_SUCCESS Loading & deserialization successful.\r
77 @return Error codes returned by GetVariable2().\r
78**/\r
79EFI_STATUS\r
80EFIAPI\r
81PlatformConfigLoad (\r
82 OUT PLATFORM_CONFIG *PlatformConfig,\r
83 OUT UINT64 *OptionalElements\r
84 )\r
85{\r
86 VOID *Data;\r
87 UINTN DataSize;\r
88 EFI_STATUS Status;\r
89\r
90 //\r
91 // Any translation done in PlatformConfigSave() would have to be mirrored\r
92 // here. For now, just load the binary dump.\r
93 //\r
94 // Versioning of the binary wire format is implemented based on size\r
95 // (only incremental changes, ie. new fields), and on GUID.\r
96 // (Incompatible changes require a GUID change.)\r
97 //\r
98 Status = GetVariable2 (mVariableName, &gOvmfPlatformConfigGuid, &Data,\r
99 &DataSize);\r
100 if (EFI_ERROR (Status)) {\r
101 return Status;\r
102 }\r
103\r
104 *OptionalElements = 0;\r
105 if (DataSize > sizeof *PlatformConfig) {\r
106 //\r
107 // Handle firmware downgrade -- keep only leading part.\r
108 //\r
109 CopyMem (PlatformConfig, Data, sizeof *PlatformConfig);\r
110 *OptionalElements |= PLATFORM_CONFIG_F_DOWNGRADE;\r
111 } else {\r
112 CopyMem (PlatformConfig, Data, DataSize);\r
113\r
114 //\r
115 // Handle firmware upgrade -- zero out missing fields.\r
116 //\r
117 ZeroMem ((UINT8 *)PlatformConfig + DataSize,\r
118 sizeof *PlatformConfig - DataSize);\r
119 }\r
120\r
121 //\r
122 // Based on DataSize, report the optional features that we recognize.\r
123 //\r
124 if (DataSize >= (OFFSET_OF (PLATFORM_CONFIG, VerticalResolution) +\r
125 sizeof PlatformConfig->VerticalResolution)) {\r
126 *OptionalElements |= PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION;\r
127 }\r
128\r
129 FreePool (Data);\r
130 return EFI_SUCCESS;\r
131}\r