3 Utility functions for serializing (persistently storing) and deserializing
4 OVMF's platform configuration.
6 Copyright (C) 2014, Red Hat, Inc.
8 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/MemoryAllocationLib.h>
15 #include <Library/UefiLib.h>
16 #include <Library/UefiRuntimeServicesTableLib.h>
17 #include <Guid/OvmfPlatformConfig.h>
19 #include "PlatformConfig.h"
22 // Name of the UEFI variable that we use for persistent storage.
24 STATIC CHAR16 mVariableName
[] = L
"PlatformConfig";
27 Serialize and persistently save platform configuration.
29 @param[in] PlatformConfig The platform configuration to serialize and save.
31 @return Status codes returned by gRT->SetVariable().
36 IN PLATFORM_CONFIG
*PlatformConfig
42 // We could implement any kind of translation here, as part of serialization.
43 // For example, we could expose the platform configuration in separate
44 // variables with human-readable contents, allowing other tools to access
45 // them more easily. For now, just save a binary dump.
47 Status
= gRT
->SetVariable (
49 &gOvmfPlatformConfigGuid
,
50 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
|
51 EFI_VARIABLE_RUNTIME_ACCESS
,
52 sizeof *PlatformConfig
,
59 Load and deserialize platform configuration.
61 When the function fails, output parameters are indeterminate.
63 @param[out] PlatformConfig The platform configuration to receive the
66 @param[out] OptionalElements This bitmap describes the presence of optional
67 configuration elements that have been loaded.
68 PLATFORM_CONFIG_F_DOWNGRADE means that some
69 unknown elements, present in the wire format,
72 @retval EFI_SUCCESS Loading & deserialization successful.
73 @return Error codes returned by GetVariable2().
78 OUT PLATFORM_CONFIG
*PlatformConfig
,
79 OUT UINT64
*OptionalElements
87 // Any translation done in PlatformConfigSave() would have to be mirrored
88 // here. For now, just load the binary dump.
90 // Versioning of the binary wire format is implemented based on size
91 // (only incremental changes, ie. new fields), and on GUID.
92 // (Incompatible changes require a GUID change.)
94 Status
= GetVariable2 (
96 &gOvmfPlatformConfigGuid
,
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.
118 (UINT8
*)PlatformConfig
+ DataSize
,
119 sizeof *PlatformConfig
- DataSize
124 // Based on DataSize, report the optional features that we recognize.
126 if (DataSize
>= (OFFSET_OF (PLATFORM_CONFIG
, VerticalResolution
) +
127 sizeof PlatformConfig
->VerticalResolution
))
129 *OptionalElements
|= PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION
;