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
|
/** @file
Recovery module.
Caution: This module requires additional review when modified.
This module will have external input - Capsule-on-Disk Temp Relocation image.
This external input must be validated carefully to avoid security issue like
buffer overflow, integer overflow.
RetrieveRelocatedCapsule() will receive untrusted input and do basic validation.
Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
//
// The package level header files this module uses
//
#include <Uefi.h>
#include <PiPei.h>
//
// The protocols, PPI and GUID defintions for this module
//
#include <Ppi/MasterBootMode.h>
#include <Ppi/FirmwareVolumeInfo.h>
#include <Ppi/ReadOnlyVariable2.h>
#include <Ppi/Capsule.h>
#include <Ppi/CapsuleOnDisk.h>
#include <Ppi/DeviceRecoveryModule.h>
#include <Guid/FirmwareFileSystem2.h>
//
// The Library classes this module consumes
//
#include <Library/DebugLib.h>
#include <Library/PeimEntryPoint.h>
#include <Library/PeiServicesLib.h>
#include <Library/HobLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/PcdLib.h>
#include <Library/CapsuleLib.h>
#include <Library/ReportStatusCodeLib.h>
/**
Loads a DXE capsule from some media into memory and updates the HOB table
with the DXE firmware volume information.
@param[in] PeiServices General-purpose services that are available to every PEIM.
@param[in] This Indicates the EFI_PEI_RECOVERY_MODULE_PPI instance.
@retval EFI_SUCCESS The capsule was loaded correctly.
@retval EFI_DEVICE_ERROR A device error occurred.
@retval EFI_NOT_FOUND A recovery DXE capsule cannot be found.
**/
EFI_STATUS
EFIAPI
LoadCapsuleOnDisk (
IN EFI_PEI_SERVICES **PeiServices,
IN EDKII_PEI_CAPSULE_ON_DISK_PPI *This
);
static EDKII_PEI_CAPSULE_ON_DISK_PPI mCapsuleOnDiskPpi = {
LoadCapsuleOnDisk
};
static EFI_PEI_PPI_DESCRIPTOR mCapsuleOnDiskPpiList = {
(EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
&gEdkiiPeiCapsuleOnDiskPpiGuid,
&mCapsuleOnDiskPpi
};
/**
Determine if capsule comes from memory by checking Capsule PPI.
@param[in] PeiServices General purpose services available to every PEIM.
@retval TRUE Capsule comes from memory.
@retval FALSE No capsule comes from memory.
**/
static
BOOLEAN
CheckCapsuleFromRam (
IN CONST EFI_PEI_SERVICES **PeiServices
)
{
EFI_STATUS Status;
PEI_CAPSULE_PPI *Capsule;
Status = PeiServicesLocatePpi (
&gEfiPeiCapsulePpiGuid,
0,
NULL,
(VOID **)&Capsule
);
if (!EFI_ERROR (Status)) {
Status = Capsule->CheckCapsuleUpdate ((EFI_PEI_SERVICES **)PeiServices);
if (!EFI_ERROR (Status)) {
return TRUE;
}
}
return FALSE;
}
/**
Determine if it is a Capsule On Disk mode.
@retval TRUE Capsule On Disk mode.
@retval FALSE Not capsule On Disk mode.
**/
static
BOOLEAN
IsCapsuleOnDiskMode (
VOID
)
{
EFI_STATUS Status;
UINTN Size;
EFI_PEI_READ_ONLY_VARIABLE2_PPI *PPIVariableServices;
BOOLEAN CodRelocInfo;
Status = PeiServicesLocatePpi (
&gEfiPeiReadOnlyVariable2PpiGuid,
0,
NULL,
(VOID **)&PPIVariableServices
);
ASSERT_EFI_ERROR (Status);
Size = sizeof (BOOLEAN);
Status = PPIVariableServices->GetVariable (
PPIVariableServices,
COD_RELOCATION_INFO_VAR_NAME,
&gEfiCapsuleVendorGuid,
NULL,
&Size,
&CodRelocInfo
);
if (EFI_ERROR (Status) || (Size != sizeof (BOOLEAN)) || !CodRelocInfo) {
DEBUG ((DEBUG_ERROR, "Error Get CodRelocationInfo variable %r!\n", Status));
return FALSE;
}
return TRUE;
}
/**
Gets capsule images from relocated capsule buffer.
Create Capsule hob for each Capsule.
Caution: This function may receive untrusted input.
Capsule-on-Disk Temp Relocation image is external input, so this function
will validate Capsule-on-Disk Temp Relocation image to make sure the content
is read within the buffer.
@param[in] RelocCapsuleBuf Buffer pointer to the relocated capsule.
@param[in] RelocCapsuleTotalSize Total size of the relocated capsule.
@retval EFI_SUCCESS Succeed to get capsules and create hob.
@retval Others Fail to get capsules and create hob.
**/
static
EFI_STATUS
RetrieveRelocatedCapsule (
IN UINT8 *RelocCapsuleBuf,
IN UINTN RelocCapsuleTotalSize
)
{
UINTN Index;
UINT8 *CapsuleDataBufEnd;
UINT8 *CapsulePtr;
UINT32 CapsuleSize;
UINT64 TotalImageSize;
UINTN CapsuleNum;
//
// Temp file contains at least 2 capsule (including 1 capsule name capsule) & 1 UINT64
//
if (RelocCapsuleTotalSize < sizeof (UINT64) + sizeof (EFI_CAPSULE_HEADER) * 2) {
return EFI_INVALID_PARAMETER;
}
CopyMem (&TotalImageSize, RelocCapsuleBuf, sizeof (UINT64));
DEBUG ((
DEBUG_INFO,
"ProcessRelocatedCapsule CapsuleBuf %x TotalCapSize %lx\n",
RelocCapsuleBuf,
TotalImageSize
));
RelocCapsuleBuf += sizeof (UINT64);
//
// TempCaspule file length check
//
if ((MAX_ADDRESS - TotalImageSize <= sizeof (UINT64)) ||
((UINT64)RelocCapsuleTotalSize != TotalImageSize + sizeof (UINT64)) ||
((UINTN)(MAX_ADDRESS - (PHYSICAL_ADDRESS)(UINTN)RelocCapsuleBuf) <= TotalImageSize))
{
return EFI_INVALID_PARAMETER;
}
CapsuleDataBufEnd = RelocCapsuleBuf + TotalImageSize;
//
// TempCapsule file integrity check over Capsule Header to ensure no data corruption in NV Var & Relocation storage
//
CapsulePtr = RelocCapsuleBuf;
CapsuleNum = 0;
while (CapsulePtr < CapsuleDataBufEnd) {
if (((CapsuleDataBufEnd - CapsulePtr) < sizeof (EFI_CAPSULE_HEADER)) ||
(((EFI_CAPSULE_HEADER *)CapsulePtr)->CapsuleImageSize < sizeof (EFI_CAPSULE_HEADER)) ||
((UINTN)(MAX_ADDRESS - (PHYSICAL_ADDRESS)(UINTN)CapsulePtr) < ((EFI_CAPSULE_HEADER *)CapsulePtr)->CapsuleImageSize)
)
{
break;
}
CapsulePtr += ((EFI_CAPSULE_HEADER *)CapsulePtr)->CapsuleImageSize;
CapsuleNum++;
}
if (CapsulePtr != CapsuleDataBufEnd) {
return EFI_INVALID_PARAMETER;
}
//
// Capsule count must be less than PcdCapsuleMax, avoid building too many CvHobs to occupy all the free space in HobList.
//
if (CapsuleNum > PcdGet16 (PcdCapsuleMax)) {
return EFI_INVALID_PARAMETER;
}
//
// Re-iterate the capsule buffer to create Capsule hob & Capsule Name Str Hob for each Capsule saved in relocated capsule file
//
CapsulePtr = RelocCapsuleBuf;
Index = 0;
while (CapsulePtr < CapsuleDataBufEnd) {
CapsuleSize = ((EFI_CAPSULE_HEADER *)CapsulePtr)->CapsuleImageSize;
BuildCvHob ((EFI_PHYSICAL_ADDRESS)(UINTN)CapsulePtr, CapsuleSize);
DEBUG ((DEBUG_INFO, "Capsule saved in address %x size %x\n", CapsulePtr, CapsuleSize));
CapsulePtr += CapsuleSize;
Index++;
}
return EFI_SUCCESS;
}
/**
Recovery module entrypoint
@param[in] FileHandle Handle of the file being invoked.
@param[in] PeiServices Describes the list of possible PEI Services.
@return EFI_SUCCESS Recovery module is initialized.
**/
EFI_STATUS
EFIAPI
InitializeCapsuleOnDiskLoad (
IN EFI_PEI_FILE_HANDLE FileHandle,
IN CONST EFI_PEI_SERVICES **PeiServices
)
{
EFI_STATUS Status;
UINTN BootMode;
UINTN FileNameSize;
BootMode = GetBootModeHob ();
ASSERT (BootMode == BOOT_ON_FLASH_UPDATE);
//
// If there are capsules provisioned in memory, quit.
// Only one capsule resource is accept, CapsuleOnRam's priority is higher than CapsuleOnDisk.
//
if (CheckCapsuleFromRam (PeiServices)) {
DEBUG ((DEBUG_ERROR, "Capsule On Memory Detected! Quit.\n"));
return EFI_ABORTED;
}
DEBUG_CODE_BEGIN ();
VOID *CapsuleOnDiskModePpi;
if (!IsCapsuleOnDiskMode ()) {
return EFI_NOT_FOUND;
}
//
// Check Capsule On Disk Relocation flag. If exists, load capsule & create Capsule Hob
//
Status = PeiServicesLocatePpi (
&gEdkiiPeiBootInCapsuleOnDiskModePpiGuid,
0,
NULL,
(VOID **)&CapsuleOnDiskModePpi
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Locate CapsuleOnDiskModePpi error %x\n", Status));
return Status;
}
DEBUG_CODE_END ();
Status = PeiServicesInstallPpi (&mCapsuleOnDiskPpiList);
ASSERT_EFI_ERROR (Status);
FileNameSize = PcdGetSize (PcdCoDRelocationFileName);
Status = PcdSetPtrS (PcdRecoveryFileName, &FileNameSize, (VOID *)PcdGetPtr (PcdCoDRelocationFileName));
ASSERT_EFI_ERROR (Status);
return Status;
}
/**
Loads a DXE capsule from some media into memory and updates the HOB table
with the DXE firmware volume information.
@param[in] PeiServices General-purpose services that are available to every PEIM.
@param[in] This Indicates the EFI_PEI_RECOVERY_MODULE_PPI instance.
@retval EFI_SUCCESS The capsule was loaded correctly.
@retval EFI_DEVICE_ERROR A device error occurred.
@retval EFI_NOT_FOUND A recovery DXE capsule cannot be found.
**/
EFI_STATUS
EFIAPI
LoadCapsuleOnDisk (
IN EFI_PEI_SERVICES **PeiServices,
IN EDKII_PEI_CAPSULE_ON_DISK_PPI *This
)
{
EFI_STATUS Status;
EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *DeviceRecoveryPpi;
UINTN NumberRecoveryCapsules;
UINTN Instance;
UINTN CapsuleInstance;
UINTN CapsuleSize;
EFI_GUID CapsuleType;
VOID *CapsuleBuffer;
DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Load Capsule On Disk Entry\n"));
for (Instance = 0; ; Instance++) {
Status = PeiServicesLocatePpi (
&gEfiPeiDeviceRecoveryModulePpiGuid,
Instance,
NULL,
(VOID **)&DeviceRecoveryPpi
);
DEBUG ((DEBUG_INFO, "LoadCapsuleOnDisk - LocateRecoveryPpi (%d) - %r\n", Instance, Status));
if (EFI_ERROR (Status)) {
if (Instance == 0) {
REPORT_STATUS_CODE (
EFI_ERROR_CODE | EFI_ERROR_MAJOR,
(EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_RECOVERY_PPI_NOT_FOUND)
);
}
break;
}
NumberRecoveryCapsules = 0;
Status = DeviceRecoveryPpi->GetNumberRecoveryCapsules (
(EFI_PEI_SERVICES **)PeiServices,
DeviceRecoveryPpi,
&NumberRecoveryCapsules
);
DEBUG ((DEBUG_INFO, "LoadCapsuleOnDisk - GetNumberRecoveryCapsules (%d) - %r\n", NumberRecoveryCapsules, Status));
if (EFI_ERROR (Status)) {
continue;
}
for (CapsuleInstance = 1; CapsuleInstance <= NumberRecoveryCapsules; CapsuleInstance++) {
CapsuleSize = 0;
Status = DeviceRecoveryPpi->GetRecoveryCapsuleInfo (
(EFI_PEI_SERVICES **)PeiServices,
DeviceRecoveryPpi,
CapsuleInstance,
&CapsuleSize,
&CapsuleType
);
DEBUG ((DEBUG_INFO, "LoadCapsuleOnDisk - GetRecoveryCapsuleInfo (%d - %x) - %r\n", CapsuleInstance, CapsuleSize, Status));
if (EFI_ERROR (Status)) {
break;
}
//
// Allocate the memory so that it gets preserved into DXE.
// Capsule is special because it may need to populate to system table
//
CapsuleBuffer = AllocateRuntimePages (EFI_SIZE_TO_PAGES (CapsuleSize));
if (CapsuleBuffer == NULL) {
DEBUG ((DEBUG_ERROR, "LoadCapsuleOnDisk - AllocateRuntimePages fail\n"));
continue;
}
Status = DeviceRecoveryPpi->LoadRecoveryCapsule (
(EFI_PEI_SERVICES **)PeiServices,
DeviceRecoveryPpi,
CapsuleInstance,
CapsuleBuffer
);
DEBUG ((DEBUG_INFO, "LoadCapsuleOnDisk - LoadRecoveryCapsule (%d) - %r\n", CapsuleInstance, Status));
if (EFI_ERROR (Status)) {
FreePages (CapsuleBuffer, EFI_SIZE_TO_PAGES (CapsuleSize));
break;
}
//
// Capsule Update Mode, Split relocated Capsule buffer into different capsule vehical hobs.
//
Status = RetrieveRelocatedCapsule (CapsuleBuffer, CapsuleSize);
break;
}
if (EFI_ERROR (Status)) {
REPORT_STATUS_CODE (
EFI_ERROR_CODE | EFI_ERROR_MAJOR,
(EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_NO_RECOVERY_CAPSULE)
);
}
return Status;
}
//
// Any attack against GPT, Relocation Info Variable or temp relocation file will result in no Capsule HOB and return EFI_NOT_FOUND.
// After flow to DXE phase. since no capsule hob is detected. Platform will clear Info flag and force restart.
// No volunerability will be exposed
//
return EFI_NOT_FOUND;
}
|