]> 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 STATIC CHAR16 mVariableName[] = L"PlatformConfig";
25
26 /**
27 Serialize and persistently save platform configuration.
28
29 @param[in] PlatformConfig The platform configuration to serialize and save.
30
31 @return Status codes returned by gRT->SetVariable().
32 **/
33 EFI_STATUS
34 EFIAPI
35 PlatformConfigSave (
36 IN PLATFORM_CONFIG *PlatformConfig
37 )
38 {
39 EFI_STATUS Status;
40
41 //
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.
46 //
47 Status = gRT->SetVariable (
48 mVariableName,
49 &gOvmfPlatformConfigGuid,
50 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS |
51 EFI_VARIABLE_RUNTIME_ACCESS,
52 sizeof *PlatformConfig,
53 PlatformConfig
54 );
55 return Status;
56 }
57
58 /**
59 Load and deserialize platform configuration.
60
61 When the function fails, output parameters are indeterminate.
62
63 @param[out] PlatformConfig The platform configuration to receive the
64 loaded data.
65
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,
70 have been ignored.
71
72 @retval EFI_SUCCESS Loading & deserialization successful.
73 @return Error codes returned by GetVariable2().
74 **/
75 EFI_STATUS
76 EFIAPI
77 PlatformConfigLoad (
78 OUT PLATFORM_CONFIG *PlatformConfig,
79 OUT UINT64 *OptionalElements
80 )
81 {
82 VOID *Data;
83 UINTN DataSize;
84 EFI_STATUS Status;
85
86 //
87 // Any translation done in PlatformConfigSave() would have to be mirrored
88 // here. For now, just load the binary dump.
89 //
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.)
93 //
94 Status = GetVariable2 (
95 mVariableName,
96 &gOvmfPlatformConfigGuid,
97 &Data,
98 &DataSize
99 );
100 if (EFI_ERROR (Status)) {
101 return Status;
102 }
103
104 *OptionalElements = 0;
105 if (DataSize > sizeof *PlatformConfig) {
106 //
107 // Handle firmware downgrade -- keep only leading part.
108 //
109 CopyMem (PlatformConfig, Data, sizeof *PlatformConfig);
110 *OptionalElements |= PLATFORM_CONFIG_F_DOWNGRADE;
111 } else {
112 CopyMem (PlatformConfig, Data, DataSize);
113
114 //
115 // Handle firmware upgrade -- zero out missing fields.
116 //
117 ZeroMem (
118 (UINT8 *)PlatformConfig + DataSize,
119 sizeof *PlatformConfig - DataSize
120 );
121 }
122
123 //
124 // Based on DataSize, report the optional features that we recognize.
125 //
126 if (DataSize >= (OFFSET_OF (PLATFORM_CONFIG, VerticalResolution) +
127 sizeof PlatformConfig->VerticalResolution))
128 {
129 *OptionalElements |= PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION;
130 }
131
132 FreePool (Data);
133 return EFI_SUCCESS;
134 }