3 Utility functions for serializing (persistently storing) and deserializing
4 OVMF's platform configuration.
6 Copyright (C) 2014, Red Hat, Inc.
8 This program and the accompanying materials are licensed and made available
9 under the terms and conditions of the BSD License which accompanies this
10 distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
14 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/UefiLib.h>
22 #include <Library/UefiRuntimeServicesTableLib.h>
23 #include <Guid/OvmfPlatformConfig.h>
25 #include "PlatformConfig.h"
28 // Name of the UEFI variable that we use for persistent storage.
30 STATIC CHAR16 mVariableName
[] = L
"PlatformConfig";
34 Serialize and persistently save platform configuration.
36 @param[in] PlatformConfig The platform configuration to serialize and save.
38 @return Status codes returned by gRT->SetVariable().
43 IN PLATFORM_CONFIG
*PlatformConfig
49 // We could implement any kind of translation here, as part of serialization.
50 // For example, we could expose the platform configuration in separate
51 // variables with human-readable contents, allowing other tools to access
52 // them more easily. For now, just save a binary dump.
54 Status
= gRT
->SetVariable (mVariableName
, &gOvmfPlatformConfigGuid
,
55 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
|
56 EFI_VARIABLE_RUNTIME_ACCESS
,
57 sizeof *PlatformConfig
, PlatformConfig
);
63 Load and deserialize platform configuration.
65 When the function fails, output parameters are indeterminate.
67 @param[out] PlatformConfig The platform configuration to receive the
70 @param[out] OptionalElements This bitmap describes the presence of optional
71 configuration elements that have been loaded.
72 PLATFORM_CONFIG_F_DOWNGRADE means that some
73 unknown elements, present in the wire format,
76 @retval EFI_SUCCESS Loading & deserialization successful.
77 @return Error codes returned by GetVariable2().
82 OUT PLATFORM_CONFIG
*PlatformConfig
,
83 OUT UINT64
*OptionalElements
91 // Any translation done in PlatformConfigSave() would have to be mirrored
92 // here. For now, just load the binary dump.
94 // Versioning of the binary wire format is implemented based on size
95 // (only incremental changes, ie. new fields), and on GUID.
96 // (Incompatible changes require a GUID change.)
98 Status
= GetVariable2 (mVariableName
, &gOvmfPlatformConfigGuid
, &Data
,
100 if (EFI_ERROR (Status
)) {
104 *OptionalElements
= 0;
105 if (DataSize
> sizeof *PlatformConfig
) {
107 // Handle firmware downgrade -- keep only leading part.
109 CopyMem (PlatformConfig
, Data
, sizeof *PlatformConfig
);
110 *OptionalElements
|= PLATFORM_CONFIG_F_DOWNGRADE
;
112 CopyMem (PlatformConfig
, Data
, DataSize
);
115 // Handle firmware upgrade -- zero out missing fields.
117 ZeroMem ((UINT8
*)PlatformConfig
+ DataSize
,
118 sizeof *PlatformConfig
- DataSize
);
122 // Based on DataSize, report the optional features that we recognize.
124 if (DataSize
>= (OFFSET_OF (PLATFORM_CONFIG
, VerticalResolution
) +
125 sizeof PlatformConfig
->VerticalResolution
)) {
126 *OptionalElements
|= PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION
;