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 CHAR16 mVariableName
[] = L
"PlatformConfig";
25 CHAR16 mHiiFormName
[] = L
"MainFormState";
28 Serialize and persistently save platform configuration.
30 @param[in] PlatformConfig The platform configuration to serialize and save.
32 @return Status codes returned by gRT->SetVariable().
37 IN PLATFORM_CONFIG
*PlatformConfig
43 // We could implement any kind of translation here, as part of serialization.
44 // For example, we could expose the platform configuration in separate
45 // variables with human-readable contents, allowing other tools to access
46 // them more easily. For now, just save a binary dump.
48 Status
= gRT
->SetVariable (
50 &gOvmfPlatformConfigGuid
,
51 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
|
52 EFI_VARIABLE_RUNTIME_ACCESS
,
53 sizeof *PlatformConfig
,
60 Load and deserialize platform configuration.
62 When the function fails, output parameters are indeterminate.
64 @param[out] PlatformConfig The platform configuration to receive the
67 @param[out] OptionalElements This bitmap describes the presence of optional
68 configuration elements that have been loaded.
69 PLATFORM_CONFIG_F_DOWNGRADE means that some
70 unknown elements, present in the wire format,
73 @retval EFI_SUCCESS Loading & deserialization successful.
74 @return Error codes returned by GetVariable2().
79 OUT PLATFORM_CONFIG
*PlatformConfig
,
80 OUT UINT64
*OptionalElements
88 // Any translation done in PlatformConfigSave() would have to be mirrored
89 // here. For now, just load the binary dump.
91 // Versioning of the binary wire format is implemented based on size
92 // (only incremental changes, ie. new fields), and on GUID.
93 // (Incompatible changes require a GUID change.)
95 Status
= GetVariable2 (
97 &gOvmfPlatformConfigGuid
,
101 if (EFI_ERROR (Status
)) {
105 *OptionalElements
= 0;
106 if (DataSize
> sizeof *PlatformConfig
) {
108 // Handle firmware downgrade -- keep only leading part.
110 CopyMem (PlatformConfig
, Data
, sizeof *PlatformConfig
);
111 *OptionalElements
|= PLATFORM_CONFIG_F_DOWNGRADE
;
113 CopyMem (PlatformConfig
, Data
, DataSize
);
116 // Handle firmware upgrade -- zero out missing fields.
119 (UINT8
*)PlatformConfig
+ DataSize
,
120 sizeof *PlatformConfig
- DataSize
125 // Based on DataSize, report the optional features that we recognize.
127 if (DataSize
>= (OFFSET_OF (PLATFORM_CONFIG
, VerticalResolution
) +
128 sizeof PlatformConfig
->VerticalResolution
))
130 *OptionalElements
|= PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION
;