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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
/** @file
Implementation of helper routines for DXE environment.
Copyright (c) 2013 - 2016 Intel Corporation.
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <PiDxe.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/S3BootScriptLib.h>
#include <Library/DxeServicesLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Protocol/SmmBase2.h>
#include <Protocol/Spi.h>
#include <Protocol/VariableLock.h>
#include <Guid/MemoryConfigData.h>
#include <Guid/QuarkVariableLock.h>
#include "CommonHeader.h"
#define FLASH_BLOCK_SIZE SIZE_4KB
//
// Global variables.
//
EFI_SPI_PROTOCOL *mPlatHelpSpiProtocolRef = NULL;
//
// Routines defined in other source modules of this component.
//
//
// Routines local to this component.
//
//
// Routines shared with other souce modules in this component.
//
EFI_SPI_PROTOCOL *
LocateSpiProtocol (
IN EFI_SMM_SYSTEM_TABLE2 *Smst
)
{
if (mPlatHelpSpiProtocolRef == NULL) {
if (Smst != NULL) {
Smst->SmmLocateProtocol (
&gEfiSmmSpiProtocolGuid,
NULL,
(VOID **) &mPlatHelpSpiProtocolRef
);
} else {
gBS->LocateProtocol (
&gEfiSpiProtocolGuid,
NULL,
(VOID **) &mPlatHelpSpiProtocolRef
);
}
ASSERT (mPlatHelpSpiProtocolRef != NULL);
}
return mPlatHelpSpiProtocolRef;
}
//
// Routines exported by this source module.
//
/**
Find pointer to RAW data in Firmware volume file.
@param FvNameGuid Firmware volume to search. If == NULL search all.
@param FileNameGuid Firmware volume file to search for.
@param SectionData Pointer to RAW data section of found file.
@param SectionDataSize Pointer to UNITN to get size of RAW data.
@retval EFI_SUCCESS Raw Data found.
@retval EFI_INVALID_PARAMETER FileNameGuid == NULL.
@retval EFI_NOT_FOUND Firmware volume file not found.
@retval EFI_UNSUPPORTED Unsupported in current enviroment (PEI or DXE).
**/
EFI_STATUS
EFIAPI
PlatformFindFvFileRawDataSection (
IN CONST EFI_GUID *FvNameGuid OPTIONAL,
IN CONST EFI_GUID *FileNameGuid,
OUT VOID **SectionData,
OUT UINTN *SectionDataSize
)
{
if (FileNameGuid == NULL || SectionData == NULL || SectionDataSize == NULL) {
return EFI_INVALID_PARAMETER;
}
if (FvNameGuid != NULL) {
return EFI_UNSUPPORTED; // Searching in specific FV unsupported in DXE.
}
return GetSectionFromAnyFv (FileNameGuid, EFI_SECTION_RAW, 0, SectionData, SectionDataSize);
}
/**
Find free spi protect register and write to it to protect a flash region.
@param DirectValue Value to directly write to register.
if DirectValue == 0 the use Base & Length below.
@param BaseAddress Base address of region in Flash Memory Map.
@param Length Length of region to protect.
@retval EFI_SUCCESS Free spi protect register found & written.
@retval EFI_NOT_FOUND Free Spi protect register not found.
@retval EFI_DEVICE_ERROR Unable to write to spi protect register.
**/
EFI_STATUS
EFIAPI
PlatformWriteFirstFreeSpiProtect (
IN CONST UINT32 DirectValue,
IN CONST UINT32 BaseAddress,
IN CONST UINT32 Length
)
{
UINT32 FreeOffset;
UINT32 PchRootComplexBar;
EFI_STATUS Status;
PchRootComplexBar = QNC_RCRB_BASE;
Status = WriteFirstFreeSpiProtect (
PchRootComplexBar,
DirectValue,
BaseAddress,
Length,
&FreeOffset
);
if (!EFI_ERROR (Status)) {
S3BootScriptSaveMemWrite (
S3BootScriptWidthUint32,
(UINTN) (PchRootComplexBar + FreeOffset),
1,
(VOID *) (UINTN) (PchRootComplexBar + FreeOffset)
);
}
return Status;
}
/**
Lock legacy SPI static configuration information.
Function will assert if unable to lock config.
**/
VOID
EFIAPI
PlatformFlashLockConfig (
VOID
)
{
EFI_STATUS Status;
EFI_SPI_PROTOCOL *SpiProtocol;
//
// Enable lock of legacy SPI static configuration information.
//
SpiProtocol = LocateSpiProtocol (NULL); // This routine will not be called in SMM.
ASSERT_EFI_ERROR (SpiProtocol != NULL);
if (SpiProtocol != NULL) {
Status = SpiProtocol->Lock (SpiProtocol);
if (!EFI_ERROR (Status)) {
DEBUG ((EFI_D_INFO, "Platform: Spi Config Locked Down\n"));
} else if (Status == EFI_ACCESS_DENIED) {
DEBUG ((EFI_D_INFO, "Platform: Spi Config already locked down\n"));
} else {
ASSERT_EFI_ERROR (Status);
}
}
}
/**
Platform Variable Lock.
@retval EFI_SUCCESS Platform Variable Lock successful.
@retval EFI_NOT_FOUND No protocol instances were found that match Protocol and
Registration.
**/
VOID
EFIAPI
PlatformVariableLock (
)
{
EFI_STATUS Status;
EDKII_VARIABLE_LOCK_PROTOCOL *VariableLockProtocol;
Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **)&VariableLockProtocol);
ASSERT_EFI_ERROR (Status);
Status = VariableLockProtocol->RequestToLock (
VariableLockProtocol,
QUARK_VARIABLE_LOCK_NAME,
&gQuarkVariableLockGuid
);
ASSERT_EFI_ERROR (Status);
// Memory Config Data shouldn't be writable when Quark Variable Lock is enabled.
Status = VariableLockProtocol->RequestToLock (
VariableLockProtocol,
EFI_MEMORY_CONFIG_DATA_NAME,
&gEfiMemoryConfigDataGuid
);
ASSERT_EFI_ERROR (Status);
}
/**
Lock regions and config of SPI flash given the policy for this platform.
Function will assert if unable to lock regions or config.
@param PreBootPolicy If TRUE do Pre Boot Flash Lock Policy.
**/
VOID
EFIAPI
PlatformFlashLockPolicy (
IN CONST BOOLEAN PreBootPolicy
)
{
EFI_STATUS Status;
UINT64 CpuAddressNvStorage;
UINT64 CpuAddressFlashDevice;
UINT64 SpiAddress;
EFI_BOOT_MODE BootMode;
UINTN SpiFlashDeviceSize;
BootMode = GetBootModeHob ();
SpiFlashDeviceSize = (UINTN) PcdGet32 (PcdSpiFlashDeviceSize);
CpuAddressFlashDevice = SIZE_4GB - SpiFlashDeviceSize;
DEBUG (
(EFI_D_INFO,
"Platform:FlashDeviceSize = 0x%08x Bytes\n",
SpiFlashDeviceSize)
);
//
// If not in update or recovery mode, lock stuff down
//
if ((BootMode != BOOT_IN_RECOVERY_MODE) && (BootMode != BOOT_ON_FLASH_UPDATE)) {
//
// Lock regions
//
CpuAddressNvStorage = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);
//
// Lock from start of flash device up to Smi writable flash storage areas.
//
SpiAddress = 0;
if (!PlatformIsSpiRangeProtected ((UINT32) SpiAddress, (UINT32) (CpuAddressNvStorage - CpuAddressFlashDevice))) {
DEBUG (
(EFI_D_INFO,
"Platform: Protect Region Base:Len 0x%08x:0x%08x\n",
(UINTN) SpiAddress, (UINTN)(CpuAddressNvStorage - CpuAddressFlashDevice))
);
Status = PlatformWriteFirstFreeSpiProtect (
0,
(UINT32) SpiAddress,
(UINT32) (CpuAddressNvStorage - CpuAddressFlashDevice)
);
ASSERT_EFI_ERROR (Status);
}
//
// Move Spi Address to after Smi writable flash storage areas.
//
SpiAddress = CpuAddressNvStorage - CpuAddressFlashDevice;
SpiAddress += ((UINT64) PcdGet32 (PcdFlashNvStorageVariableSize));
//
// Lock from end of OEM area to end of flash part.
//
if (!PlatformIsSpiRangeProtected ((UINT32) SpiAddress, SpiFlashDeviceSize - ((UINT32) SpiAddress))) {
DEBUG (
(EFI_D_INFO,
"Platform: Protect Region Base:Len 0x%08x:0x%08x\n",
(UINTN) SpiAddress,
(UINTN) (SpiFlashDeviceSize - ((UINT32) SpiAddress)))
);
ASSERT (SpiAddress < ((UINT64) SpiFlashDeviceSize));
Status = PlatformWriteFirstFreeSpiProtect (
0,
(UINT32) SpiAddress,
SpiFlashDeviceSize - ((UINT32) SpiAddress)
);
ASSERT_EFI_ERROR (Status);
}
}
//
// Always Lock flash config registers if about to boot a boot option
// else lock depending on boot mode.
//
if (PreBootPolicy || (BootMode != BOOT_ON_FLASH_UPDATE)) {
PlatformFlashLockConfig ();
}
//
// Enable Quark Variable lock if PreBootPolicy.
//
if (PreBootPolicy) {
PlatformVariableLock ();
}
}
/**
Erase and Write to platform flash.
Routine accesses one flash block at a time, each access consists
of an erase followed by a write of FLASH_BLOCK_SIZE. One or both
of DoErase & DoWrite params must be TRUE.
Limitations:-
CpuWriteAddress must be aligned to FLASH_BLOCK_SIZE.
DataSize must be a multiple of FLASH_BLOCK_SIZE.
@param Smst If != NULL then InSmm and use to locate
SpiProtocol.
@param CpuWriteAddress Address in CPU memory map of flash region.
@param Data The buffer containing the data to be written.
@param DataSize Amount of data to write.
@param DoErase Earse each block.
@param DoWrite Write to each block.
@retval EFI_SUCCESS Operation successful.
@retval EFI_NOT_READY Required resources not setup.
@retval EFI_INVALID_PARAMETER Invalid parameter.
@retval Others Unexpected error happened.
**/
EFI_STATUS
EFIAPI
PlatformFlashEraseWrite (
IN VOID *Smst,
IN UINTN CpuWriteAddress,
IN UINT8 *Data,
IN UINTN DataSize,
IN BOOLEAN DoErase,
IN BOOLEAN DoWrite
)
{
EFI_STATUS Status;
UINT64 CpuBaseAddress;
SPI_INIT_INFO *SpiInfo;
UINT8 *WriteBuf;
UINTN Index;
UINTN SpiWriteAddress;
EFI_SPI_PROTOCOL *SpiProtocol;
if (!DoErase && !DoWrite) {
return EFI_INVALID_PARAMETER;
}
if (DoWrite && Data == NULL) {
return EFI_INVALID_PARAMETER;
}
if ((CpuWriteAddress % FLASH_BLOCK_SIZE) != 0) {
return EFI_INVALID_PARAMETER;
}
if ((DataSize % FLASH_BLOCK_SIZE) != 0) {
return EFI_INVALID_PARAMETER;
}
SpiProtocol = LocateSpiProtocol ((EFI_SMM_SYSTEM_TABLE2 *)Smst);
if (SpiProtocol == NULL) {
return EFI_NOT_READY;
}
//
// Find info to allow usage of SpiProtocol->Execute.
//
Status = SpiProtocol->Info (
SpiProtocol,
&SpiInfo
);
if (EFI_ERROR(Status)) {
return Status;
}
ASSERT (SpiInfo->InitTable != NULL);
ASSERT (SpiInfo->EraseOpcodeIndex < SPI_NUM_OPCODE);
ASSERT (SpiInfo->ProgramOpcodeIndex < SPI_NUM_OPCODE);
CpuBaseAddress = PcdGet32 (PcdFlashAreaBaseAddress) - (UINT32)SpiInfo->InitTable->BiosStartOffset;
ASSERT(CpuBaseAddress >= (SIZE_4GB - SIZE_8MB));
if (CpuWriteAddress < CpuBaseAddress) {
return (EFI_INVALID_PARAMETER);
}
SpiWriteAddress = CpuWriteAddress - ((UINTN) CpuBaseAddress);
WriteBuf = Data;
DEBUG (
(EFI_D_INFO, "PlatformFlashWrite:SpiWriteAddress=%08x EraseIndex=%d WriteIndex=%d\n",
SpiWriteAddress,
(UINTN) SpiInfo->EraseOpcodeIndex,
(UINTN) SpiInfo->ProgramOpcodeIndex
));
for (Index =0; Index < DataSize / FLASH_BLOCK_SIZE; Index++) {
if (DoErase) {
DEBUG (
(EFI_D_INFO, "PlatformFlashWrite:Erase[%04x] SpiWriteAddress=%08x\n",
Index,
SpiWriteAddress
));
Status = SpiProtocol->Execute (
SpiProtocol,
SpiInfo->EraseOpcodeIndex,// OpcodeIndex
0, // PrefixOpcodeIndex
FALSE, // DataCycle
TRUE, // Atomic
FALSE, // ShiftOut
SpiWriteAddress, // Address
0, // Data Number
NULL,
EnumSpiRegionAll // SPI_REGION_TYPE
);
if (EFI_ERROR(Status)) {
return Status;
}
}
if (DoWrite) {
DEBUG (
(EFI_D_INFO, "PlatformFlashWrite:Write[%04x] SpiWriteAddress=%08x\n",
Index,
SpiWriteAddress
));
Status = SpiProtocol->Execute (
SpiProtocol,
SpiInfo->ProgramOpcodeIndex, // OpcodeIndex
0, // PrefixOpcodeIndex
TRUE, // DataCycle
TRUE, // Atomic
TRUE, // ShiftOut
SpiWriteAddress, // Address
FLASH_BLOCK_SIZE, // Data Number
WriteBuf,
EnumSpiRegionAll
);
if (EFI_ERROR(Status)) {
return Status;
}
WriteBuf+=FLASH_BLOCK_SIZE;
}
SpiWriteAddress+=FLASH_BLOCK_SIZE;
}
return EFI_SUCCESS;
}
/** Check if System booted with recovery Boot Stage1 image.
@retval TRUE If system booted with recovery Boot Stage1 image.
@retval FALSE If system booted with normal stage1 image.
**/
BOOLEAN
EFIAPI
PlatformIsBootWithRecoveryStage1 (
VOID
)
{
ASSERT_EFI_ERROR (EFI_UNSUPPORTED);
return FALSE;
}
|