]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/PlatformDxe/PlatformConfig.c
OvmfPkg: Replace BSD License with BSD+Patent License
[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 /**
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 (mVariableName, &gOvmfPlatformConfigGuid,
49 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS |
50 EFI_VARIABLE_RUNTIME_ACCESS,
51 sizeof *PlatformConfig, PlatformConfig);
52 return Status;
53 }
54
55
56 /**
57 Load and deserialize platform configuration.
58
59 When the function fails, output parameters are indeterminate.
60
61 @param[out] PlatformConfig The platform configuration to receive the
62 loaded data.
63
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,
68 have been ignored.
69
70 @retval EFI_SUCCESS Loading & deserialization successful.
71 @return Error codes returned by GetVariable2().
72 **/
73 EFI_STATUS
74 EFIAPI
75 PlatformConfigLoad (
76 OUT PLATFORM_CONFIG *PlatformConfig,
77 OUT UINT64 *OptionalElements
78 )
79 {
80 VOID *Data;
81 UINTN DataSize;
82 EFI_STATUS Status;
83
84 //
85 // Any translation done in PlatformConfigSave() would have to be mirrored
86 // here. For now, just load the binary dump.
87 //
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.)
91 //
92 Status = GetVariable2 (mVariableName, &gOvmfPlatformConfigGuid, &Data,
93 &DataSize);
94 if (EFI_ERROR (Status)) {
95 return Status;
96 }
97
98 *OptionalElements = 0;
99 if (DataSize > sizeof *PlatformConfig) {
100 //
101 // Handle firmware downgrade -- keep only leading part.
102 //
103 CopyMem (PlatformConfig, Data, sizeof *PlatformConfig);
104 *OptionalElements |= PLATFORM_CONFIG_F_DOWNGRADE;
105 } else {
106 CopyMem (PlatformConfig, Data, DataSize);
107
108 //
109 // Handle firmware upgrade -- zero out missing fields.
110 //
111 ZeroMem ((UINT8 *)PlatformConfig + DataSize,
112 sizeof *PlatformConfig - DataSize);
113 }
114
115 //
116 // Based on DataSize, report the optional features that we recognize.
117 //
118 if (DataSize >= (OFFSET_OF (PLATFORM_CONFIG, VerticalResolution) +
119 sizeof PlatformConfig->VerticalResolution)) {
120 *OptionalElements |= PLATFORM_CONFIG_F_GRAPHICS_RESOLUTION;
121 }
122
123 FreePool (Data);
124 return EFI_SUCCESS;
125 }