hc
2024-03-22 a0752693d998599af469473b8dc239ef973a012f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/** @file
  Platform variable initialization PEIM.
 
  This PEIM determines whether to load variable defaults. Ordinarily, the
  decision is based on the boot mode, but an OEM hook is provided to override
  that. The appropriate HOBs and PCDs are created to signal DXE code to update
  the variable default values.
 
  @copyright
  Copyright 2012 - 2021 Intel Corporation. <BR>
 
  SPDX-License-Identifier: BSD-2-Clause-Patent
**/
 
#include "PlatformVariableInitPei.h"
#include <Guid/PlatformVariableCommon.h>
#include <Uefi/UefiInternalFormRepresentation.h>
 
UINT16 BoardId = BOARD_ID_DEFAULT;
 
EFI_PEI_PPI_DESCRIPTOR  mPpiListPlatformVariableInit = {
  (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  &gPlatformVariableInitPpiGuid,
  NULL
};
 
/**
Apply platform variable defaults.
 
Create HOBs and set PCDs to prompt the (re-)loading of variable defaults.
Each step is attempted regardless of whether the previous steps succeeded.
If multiple errors occur, only the last error code is returned.
 
@param[in]  Events            Bitmap of events that occurred.
@param[in]  DefaultId         Default store ID, STANDARD or MANUFACTURING.
 
@retval EFI_SUCCESS           All steps completed successfully.
@retval EFI_OUT_OF_RESOURCES  One of the HOBs could not be created.
@retval EFI_NOT_FOUND         The default data could not be found in FFS.
**/
 
EFI_STATUS
ApplyPlatformVariableDefaults(
  IN UINT8  Events,
  IN UINT16 DefaultId
  )
{
  VOID        *Hob;
  EFI_STATUS  Status;
  EFI_STATUS  ReturnStatus;
 
  DEBUG((DEBUG_INFO, "Applying platform variable defaults:\n"));
  DEBUG((DEBUG_INFO, "  Events    = 0x%02x\n", Events));
  DEBUG((DEBUG_INFO, "  DefaultId = 0x%04x\n", DefaultId));
 
  //
  // Assume success up front. This will be overwritten if errors occur.
  //
  ReturnStatus = EFI_SUCCESS;
 
  //
  // Send the bitmap of events to the platform variable DXE driver.
  //
  Hob = BuildGuidDataHob(&gPlatformVariableHobGuid, &Events, sizeof(Events));
  if (Hob == NULL) {
    DEBUG((DEBUG_ERROR, "Create platform var event HOB: %r!\n", EFI_OUT_OF_RESOURCES));
    ReturnStatus = EFI_OUT_OF_RESOURCES;
  }
 
  //
  // Locate variable default data in FFS and send it to the core variable DXE
  // driver to write.
  //
  Status = CreateDefaultVariableHob(DefaultId, BoardId);
  if (EFI_ERROR(Status)) {
    DEBUG((DEBUG_ERROR, "create default var HOB: %r!\n", Status));
    ReturnStatus = Status;
  }
 
  //
  // Set the PCD SKU ID.
  //
  LibPcdSetSku(BoardId);
 
  //
  // Set the PCD default store ID.
  //
  Status = PcdSet16S(PcdSetNvStoreDefaultId, DefaultId);
  if (EFI_ERROR(Status)) {
    DEBUG((DEBUG_ERROR, "setNVstore default ID PCD: %r!\n", Status));
    ReturnStatus = Status;
  }
 
  return ReturnStatus;
}
 
/**
Perform the default variable initializations after variable service is ready.
 
@param[in]  PeiServices       General purpose services available to every PEIM.
@param[in]  NotifyDescriptor  Pointer to Notify PPI descriptor.
@param[in]  Interface         Pointer to PPI.
 
@retval EFI_SUCCESS   Default setting is initialized into variable.
@retval Other values  Can't find the matched default setting.
**/
EFI_STATUS
EFIAPI
PlatformVariablePeiInit(
  IN EFI_PEI_SERVICES           **PeiServices,
  IN EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDescriptor,
  IN VOID                       *Interface
)
{
  EFI_STATUS                      Status;
  UINT8                           *SystemConfiguration;
  EFI_GUID                        *SystemConfigurationGuid;
  UINTN                           DataSize;
  EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariableServices;
  UINT8                           Events;
  UINT16                          DefaultId;
  BOOLEAN                         ApplyDefaults;
 
  SystemConfigurationGuid = PcdGetPtr(PcdSetupVariableGuid);
  Events = 0;
  DefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
 
  if (PlatformVariableHookForHobGeneration(Interface, &Events, &DefaultId)) {
    //
    // Use events bitmap and default ID returned by PlatformVariableHook.
    //
    ApplyDefaults = TRUE;
  }
  else {
    //
    // If the setup variable does not exist (yet), defaults should be applied.
    //
    VariableServices = (EFI_PEI_READ_ONLY_VARIABLE2_PPI *)Interface;
    SystemConfiguration = NULL;
    DataSize = 0;
    Status = VariableServices->GetVariable(
      VariableServices,
      PLATFORM_SETUP_VARIABLE_NAME,
      SystemConfigurationGuid,
      NULL,
      &DataSize,
      SystemConfiguration
    );
    //
    // Setup variable is not found. So, set the default setting.
    //
    if (Status == EFI_NOT_FOUND) {
      Events = NULL_VARIABLE_EVENT;
      DefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
      ApplyDefaults = TRUE;
    }
    else {
      ApplyDefaults = FALSE;
    }
  }
 
 
  if (ApplyDefaults) {
    Status = ApplyPlatformVariableDefaults(Events, DefaultId);
  }
  else {
    //
    // Normal case boot flow
    //
    Events = 0; // no events occurred
    BuildGuidDataHob (&gPlatformVariableHobGuid, &Events, sizeof (UINT8));
 
    //
    // Patch RP variable value with PC variable in the begining of PEI
    //
    Status = CreateRPVariableHob (EFI_HII_DEFAULT_CLASS_STANDARD, BoardId);
  }
 
  PeiServicesInstallPpi (&mPpiListPlatformVariableInit);
  return Status;
}
 
 
/**
Variable Init BootMode CallBack
Prepare Knob values based on boot mode
Execute after discovering BootMode
 
@param[in]  PeiServices       General purpose services available to every PEIM.
@param[in]  NotifyDescriptor  Pointer to Notify PPI descriptor.
@param[in]  Interface         Pointer to PPI.
 
@retval EFI_SUCCESS   Knob Values.
@retval Other values
**/
EFI_STATUS
EFIAPI
VariableInitBootModeCallBack(
  IN EFI_PEI_SERVICES           **PeiServices,
  IN EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDescriptor,
  IN VOID                       *Interface
) {
  EFI_BOOT_MODE                   BootMode;
  BOOLEAN                         ApplyDefaults;
  UINT8                           Events;
  UINT16                          DefaultId;
  EFI_STATUS                      Status;
 
  Events = 0;
  DefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
  ApplyDefaults = FALSE;
 
  //
  // Certain boot modes require defaults to be (re-)applied.
  //
  Status = PeiServicesGetBootMode(&BootMode);
  ASSERT_EFI_ERROR(Status);
  if (EFI_ERROR(Status)) {
    BootMode = BOOT_WITH_DEFAULT_SETTINGS;
  }
  if (BootMode == BOOT_WITH_MFG_MODE_SETTINGS) {
    Events = MFG_MODE_EVENT;
    DefaultId = EFI_HII_DEFAULT_CLASS_MANUFACTURING;
    ApplyDefaults = TRUE;
  }
  else if (BootMode == BOOT_IN_RECOVERY_MODE) {
    Events = RECOVERY_MODE_EVENT;
    DefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
    ApplyDefaults = TRUE;
  }
  else if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {
    Events = CMOS_CLEAR_EVENT;
    DefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
    ApplyDefaults = TRUE;
  }
  if (ApplyDefaults) {
    Status = ApplyPlatformVariableDefaults(Events, DefaultId);
  }
  return Status;
}
 
EFI_PEI_NOTIFY_DESCRIPTOR mVariableNotifyList[] = {
  {
    (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK),
    &gEfiPeiReadOnlyVariable2PpiGuid,
    PlatformVariablePeiInit
  },
  {
    (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
    &gUpdateBootModePpiGuid,
    VariableInitBootModeCallBack
  }
};
 
EFI_STATUS
EFIAPI
PlatformVariableInitPeiEntry (
  IN EFI_PEI_FILE_HANDLE            FileHandle,
  IN CONST EFI_PEI_SERVICES          **PeiServices
  )
/*++
 
--*/
{
  EFI_STATUS                    Status;
 
  PlatformVariableHookForEntry();
 
  // Register notify to set default variable once variable service is ready.
  //
  Status = PeiServicesNotifyPpi(&mVariableNotifyList[0]);
 
  return Status;
}