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";
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 (mVariableName
, &gOvmfPlatformConfigGuid
,
49 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
|
50 EFI_VARIABLE_RUNTIME_ACCESS
,
51 sizeof *PlatformConfig
, PlatformConfig
);
57 Load and deserialize platform configuration.
59 When the function fails, output parameters are indeterminate.
61 @param[out] PlatformConfig The platform configuration to receive the
64 @param[out] OptionalElements This bitmap describes the presence of optional
65 configuration elements that have been loaded.
66 PLATFORM_CONFIG_F_DOWNGRADE means that some
67 unknown elements, present in the wire format,
70 @retval EFI_SUCCESS Loading & deserialization successful.
71 @return Error codes returned by GetVariable2().
76 OUT PLATFORM_CONFIG
*PlatformConfig
,
77 OUT UINT64
*OptionalElements
85 // Any translation done in PlatformConfigSave() would have to be mirrored
86 // here. For now, just load the binary dump.
88 // Versioning of the binary wire format is implemented based on size
89 // (only incremental changes, ie. new fields), and on GUID.
90 // (Incompatible changes require a GUID change.)
92 Status
= GetVariable2 (mVariableName
, &gOvmfPlatformConfigGuid
, &Data
,
94 if (EFI_ERROR (Status
)) {
98 *OptionalElements
= 0;
99 if (DataSize
> sizeof *PlatformConfig
) {
101 // Handle firmware downgrade -- keep only leading part.
103 CopyMem (PlatformConfig
, Data
, sizeof *PlatformConfig
);
104 *OptionalElements
|= PLATFORM_CONFIG_F_DOWNGRADE
;
106 CopyMem (PlatformConfig
, Data
, DataSize
);
109 // Handle firmware upgrade -- zero out missing fields.
111 ZeroMem ((UINT8
*)PlatformConfig
+ DataSize
,
112 sizeof *PlatformConfig
- DataSize
);
116 // Based on DataSize, report the optional features that we recognize.
118 if (DataSize
>= (OFFSET_OF (PLATFORM_CONFIG
, VerticalResolution
) +
119 sizeof PlatformConfig
->VerticalResolution
)) {
120 *OptionalElements
|= PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION
;