]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/PlatformDxe/PlatformConfig.c
OvmfPkg: Apply uncrustify changes
[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
ac0a286f 24STATIC CHAR16 mVariableName[] = L"PlatformConfig";\r
5267c89b
LE
25\r
26/**\r
27 Serialize and persistently save platform configuration.\r
28\r
29 @param[in] PlatformConfig The platform configuration to serialize and save.\r
30\r
31 @return Status codes returned by gRT->SetVariable().\r
32**/\r
33EFI_STATUS\r
34EFIAPI\r
35PlatformConfigSave (\r
ac0a286f 36 IN PLATFORM_CONFIG *PlatformConfig\r
5267c89b
LE
37 )\r
38{\r
ac0a286f 39 EFI_STATUS Status;\r
5267c89b
LE
40\r
41 //\r
42 // We could implement any kind of translation here, as part of serialization.\r
43 // For example, we could expose the platform configuration in separate\r
44 // variables with human-readable contents, allowing other tools to access\r
45 // them more easily. For now, just save a binary dump.\r
46 //\r
ac0a286f
MK
47 Status = gRT->SetVariable (\r
48 mVariableName,\r
49 &gOvmfPlatformConfigGuid,\r
5267c89b 50 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS |\r
ac0a286f
MK
51 EFI_VARIABLE_RUNTIME_ACCESS,\r
52 sizeof *PlatformConfig,\r
53 PlatformConfig\r
54 );\r
5267c89b
LE
55 return Status;\r
56}\r
57\r
5267c89b
LE
58/**\r
59 Load and deserialize platform configuration.\r
60\r
61 When the function fails, output parameters are indeterminate.\r
62\r
63 @param[out] PlatformConfig The platform configuration to receive the\r
64 loaded data.\r
65\r
66 @param[out] OptionalElements This bitmap describes the presence of optional\r
67 configuration elements that have been loaded.\r
68 PLATFORM_CONFIG_F_DOWNGRADE means that some\r
69 unknown elements, present in the wire format,\r
70 have been ignored.\r
71\r
72 @retval EFI_SUCCESS Loading & deserialization successful.\r
73 @return Error codes returned by GetVariable2().\r
74**/\r
75EFI_STATUS\r
76EFIAPI\r
77PlatformConfigLoad (\r
ac0a286f
MK
78 OUT PLATFORM_CONFIG *PlatformConfig,\r
79 OUT UINT64 *OptionalElements\r
5267c89b
LE
80 )\r
81{\r
ac0a286f
MK
82 VOID *Data;\r
83 UINTN DataSize;\r
84 EFI_STATUS Status;\r
5267c89b
LE
85\r
86 //\r
87 // Any translation done in PlatformConfigSave() would have to be mirrored\r
88 // here. For now, just load the binary dump.\r
89 //\r
90 // Versioning of the binary wire format is implemented based on size\r
91 // (only incremental changes, ie. new fields), and on GUID.\r
92 // (Incompatible changes require a GUID change.)\r
93 //\r
ac0a286f
MK
94 Status = GetVariable2 (\r
95 mVariableName,\r
96 &gOvmfPlatformConfigGuid,\r
97 &Data,\r
98 &DataSize\r
99 );\r
5267c89b
LE
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
ac0a286f
MK
117 ZeroMem (\r
118 (UINT8 *)PlatformConfig + DataSize,\r
119 sizeof *PlatformConfig - DataSize\r
120 );\r
5267c89b
LE
121 }\r
122\r
123 //\r
124 // Based on DataSize, report the optional features that we recognize.\r
125 //\r
126 if (DataSize >= (OFFSET_OF (PLATFORM_CONFIG, VerticalResolution) +\r
ac0a286f
MK
127 sizeof PlatformConfig->VerticalResolution))\r
128 {\r
5267c89b
LE
129 *OptionalElements |= PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION;\r
130 }\r
131\r
132 FreePool (Data);\r
133 return EFI_SUCCESS;\r
134}\r