]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - OvmfPkg/PlatformDxe/PlatformConfig.c
OvmfPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / OvmfPkg / PlatformDxe / PlatformConfig.c
... / ...
CommitLineData
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 SPDX-License-Identifier: BSD-2-Clause-Patent\r
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
24STATIC CHAR16 mVariableName[] = L"PlatformConfig";\r
25\r
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
37 IN PLATFORM_CONFIG *PlatformConfig\r
38 )\r
39{\r
40 EFI_STATUS Status;\r
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
48 Status = gRT->SetVariable (mVariableName, &gOvmfPlatformConfigGuid,\r
49 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS |\r
50 EFI_VARIABLE_RUNTIME_ACCESS,\r
51 sizeof *PlatformConfig, PlatformConfig);\r
52 return Status;\r
53}\r
54\r
55\r
56/**\r
57 Load and deserialize platform configuration.\r
58\r
59 When the function fails, output parameters are indeterminate.\r
60\r
61 @param[out] PlatformConfig The platform configuration to receive the\r
62 loaded data.\r
63\r
64 @param[out] OptionalElements This bitmap describes the presence of optional\r
65 configuration elements that have been loaded.\r
66 PLATFORM_CONFIG_F_DOWNGRADE means that some\r
67 unknown elements, present in the wire format,\r
68 have been ignored.\r
69\r
70 @retval EFI_SUCCESS Loading & deserialization successful.\r
71 @return Error codes returned by GetVariable2().\r
72**/\r
73EFI_STATUS\r
74EFIAPI\r
75PlatformConfigLoad (\r
76 OUT PLATFORM_CONFIG *PlatformConfig,\r
77 OUT UINT64 *OptionalElements\r
78 )\r
79{\r
80 VOID *Data;\r
81 UINTN DataSize;\r
82 EFI_STATUS Status;\r
83\r
84 //\r
85 // Any translation done in PlatformConfigSave() would have to be mirrored\r
86 // here. For now, just load the binary dump.\r
87 //\r
88 // Versioning of the binary wire format is implemented based on size\r
89 // (only incremental changes, ie. new fields), and on GUID.\r
90 // (Incompatible changes require a GUID change.)\r
91 //\r
92 Status = GetVariable2 (mVariableName, &gOvmfPlatformConfigGuid, &Data,\r
93 &DataSize);\r
94 if (EFI_ERROR (Status)) {\r
95 return Status;\r
96 }\r
97\r
98 *OptionalElements = 0;\r
99 if (DataSize > sizeof *PlatformConfig) {\r
100 //\r
101 // Handle firmware downgrade -- keep only leading part.\r
102 //\r
103 CopyMem (PlatformConfig, Data, sizeof *PlatformConfig);\r
104 *OptionalElements |= PLATFORM_CONFIG_F_DOWNGRADE;\r
105 } else {\r
106 CopyMem (PlatformConfig, Data, DataSize);\r
107\r
108 //\r
109 // Handle firmware upgrade -- zero out missing fields.\r
110 //\r
111 ZeroMem ((UINT8 *)PlatformConfig + DataSize,\r
112 sizeof *PlatformConfig - DataSize);\r
113 }\r
114\r
115 //\r
116 // Based on DataSize, report the optional features that we recognize.\r
117 //\r
118 if (DataSize >= (OFFSET_OF (PLATFORM_CONFIG, VerticalResolution) +\r
119 sizeof PlatformConfig->VerticalResolution)) {\r
120 *OptionalElements |= PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION;\r
121 }\r
122\r
123 FreePool (Data);\r
124 return EFI_SUCCESS;\r
125}\r