]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/PlatformDxe/PlatformConfig.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / OvmfPkg / PlatformDxe / PlatformConfig.c
1 /** @file
2
3 Utility functions for serializing (persistently storing) and deserializing
4 OVMF's platform configuration.
5
6 Copyright (C) 2014, Red Hat, Inc.
7
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
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>
18
19 #include "PlatformConfig.h"
20
21 //
22 // Name of the UEFI variable that we use for persistent storage.
23 //
24 CHAR16 mVariableName[] = L"PlatformConfig";
25 CHAR16 mHiiFormName[] = L"MainFormState";
26
27 /**
28 Serialize and persistently save platform configuration.
29
30 @param[in] PlatformConfig The platform configuration to serialize and save.
31
32 @return Status codes returned by gRT->SetVariable().
33 **/
34 EFI_STATUS
35 EFIAPI
36 PlatformConfigSave (
37 IN PLATFORM_CONFIG *PlatformConfig
38 )
39 {
40 EFI_STATUS Status;
41
42 //
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.
47 //
48 Status = gRT->SetVariable (
49 mVariableName,
50 &gOvmfPlatformConfigGuid,
51 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS |
52 EFI_VARIABLE_RUNTIME_ACCESS,
53 sizeof *PlatformConfig,
54 PlatformConfig
55 );
56 return Status;
57 }
58
59 /**
60 Load and deserialize platform configuration.
61
62 When the function fails, output parameters are indeterminate.
63
64 @param[out] PlatformConfig The platform configuration to receive the
65 loaded data.
66
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,
71 have been ignored.
72
73 @retval EFI_SUCCESS Loading & deserialization successful.
74 @return Error codes returned by GetVariable2().
75 **/
76 EFI_STATUS
77 EFIAPI
78 PlatformConfigLoad (
79 OUT PLATFORM_CONFIG *PlatformConfig,
80 OUT UINT64 *OptionalElements
81 )
82 {
83 VOID *Data;
84 UINTN DataSize;
85 EFI_STATUS Status;
86
87 //
88 // Any translation done in PlatformConfigSave() would have to be mirrored
89 // here. For now, just load the binary dump.
90 //
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.)
94 //
95 Status = GetVariable2 (
96 mVariableName,
97 &gOvmfPlatformConfigGuid,
98 &Data,
99 &DataSize
100 );
101 if (EFI_ERROR (Status)) {
102 return Status;
103 }
104
105 *OptionalElements = 0;
106 if (DataSize > sizeof *PlatformConfig) {
107 //
108 // Handle firmware downgrade -- keep only leading part.
109 //
110 CopyMem (PlatformConfig, Data, sizeof *PlatformConfig);
111 *OptionalElements |= PLATFORM_CONFIG_F_DOWNGRADE;
112 } else {
113 CopyMem (PlatformConfig, Data, DataSize);
114
115 //
116 // Handle firmware upgrade -- zero out missing fields.
117 //
118 ZeroMem (
119 (UINT8 *)PlatformConfig + DataSize,
120 sizeof *PlatformConfig - DataSize
121 );
122 }
123
124 //
125 // Based on DataSize, report the optional features that we recognize.
126 //
127 if (DataSize >= (OFFSET_OF (PLATFORM_CONFIG, VerticalResolution) +
128 sizeof PlatformConfig->VerticalResolution))
129 {
130 *OptionalElements |= PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION;
131 }
132
133 FreePool (Data);
134 return EFI_SUCCESS;
135 }