MdeModulePkg/DxeCapsuleLibFmp: Add DxeCapsuleLibFmp instance.
[mirror_edk2.git] / MdeModulePkg / Library / DxeCapsuleLibFmp / DxeCapsuleReportLib.c
1 /** @file
2 DXE capsule report related function.
3
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <PiDxe.h>
16 #include <Protocol/FirmwareManagement.h>
17 #include <Protocol/VariableLock.h>
18 #include <Guid/CapsuleReport.h>
19 #include <Guid/FmpCapsule.h>
20 #include <Guid/CapsuleVendor.h>
21
22 #include <Library/BaseLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/UefiRuntimeServicesTableLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/UefiLib.h>
29 #include <Library/PcdLib.h>
30 #include <Library/HobLib.h>
31 #include <Library/PrintLib.h>
32 #include <Library/ReportStatusCodeLib.h>
33 #include <Library/CapsuleLib.h>
34
35 #include <IndustryStandard/WindowsUxCapsule.h>
36
37 typedef struct {
38 EFI_CAPSULE_RESULT_VARIABLE_HEADER CapsuleResultHeader;
39 EFI_CAPSULE_RESULT_VARIABLE_FMP CapsuleResultFmp;
40 } CAPSULE_RESULT_VARIABLE_CACHE;
41
42 #define CAPSULE_RESULT_VARIABLE_CACHE_COUNT 0x10
43
44 CAPSULE_RESULT_VARIABLE_CACHE *mCapsuleResultVariableCache;
45 UINTN mCapsuleResultVariableCacheMaxCount;
46 UINTN mCapsuleResultVariableCacheCount;
47
48 /**
49 Get current capsule last variable index.
50
51 @return Current capsule last variable index.
52 @retval -1 No current capsule last variable.
53 **/
54 INTN
55 GetCurrentCapsuleLastIndex (
56 VOID
57 )
58 {
59 UINTN Size;
60 CHAR16 CapsuleLastStr[sizeof("Capsule####")];
61 EFI_STATUS Status;
62 UINT16 CurrentIndex;
63
64 Size = sizeof(L"Capsule####") - sizeof(CHAR16); // no zero terminator
65 Status = gRT->GetVariable(
66 L"CapsuleLast",
67 &gEfiCapsuleReportGuid,
68 NULL,
69 &Size,
70 CapsuleLastStr
71 );
72 if (EFI_ERROR(Status)) {
73 return -1;
74 }
75 CurrentIndex = (UINT16)StrHexToUintn(&CapsuleLastStr[sizeof("Capsule") - 1]);
76 return CurrentIndex;
77 }
78
79 /**
80 Check if this FMP capsule is processed.
81
82 @param[in] CapsuleHeader The capsule image header
83 @param[in] PayloadIndex FMP payload index
84 @param[in] ImageHeader FMP image header
85
86 @retval TRUE This FMP capsule is processed.
87 @retval FALSE This FMP capsule is not processed.
88 **/
89 BOOLEAN
90 IsFmpCapsuleProcessed (
91 IN EFI_CAPSULE_HEADER *CapsuleHeader,
92 IN UINTN PayloadIndex,
93 IN EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *ImageHeader
94 )
95 {
96 UINTN Index;
97 EFI_CAPSULE_RESULT_VARIABLE_HEADER *CapsuleResult;
98 EFI_CAPSULE_RESULT_VARIABLE_FMP *CapsuleResultFmp;
99
100 for (Index = 0; Index < mCapsuleResultVariableCacheCount; Index++) {
101 //
102 // Check
103 //
104 CapsuleResult = &mCapsuleResultVariableCache[Index].CapsuleResultHeader;
105 if (CapsuleResult->VariableTotalSize >= sizeof(EFI_CAPSULE_RESULT_VARIABLE_HEADER)) {
106 if (CompareGuid(&CapsuleResult->CapsuleGuid, &gEfiFmpCapsuleGuid)) {
107 if (CapsuleResult->VariableTotalSize >= sizeof(EFI_CAPSULE_RESULT_VARIABLE_HEADER) + sizeof(EFI_CAPSULE_RESULT_VARIABLE_FMP)) {
108 CapsuleResultFmp = (EFI_CAPSULE_RESULT_VARIABLE_FMP *)(CapsuleResult + 1);
109 if (CompareGuid(&CapsuleResultFmp->UpdateImageTypeId, &ImageHeader->UpdateImageTypeId) &&
110 (CapsuleResultFmp->UpdateImageIndex == ImageHeader->UpdateImageIndex) &&
111 (CapsuleResultFmp->PayloadIndex == PayloadIndex) ) {
112 return TRUE;
113 }
114 }
115 }
116 }
117 }
118
119 return FALSE;
120 }
121
122 /**
123 Write a new capsule status variable cache.
124
125 @param[in] CapsuleResult The capsule status variable
126 @param[in] CapsuleResultSize The size of the capsule stauts variable in bytes
127
128 @retval EFI_SUCCESS The capsule status variable is cached.
129 @retval EFI_OUT_OF_RESOURCES No resource to cache the capsule status variable.
130 **/
131 EFI_STATUS
132 WriteNewCapsuleResultVariableCache (
133 IN VOID *CapsuleResult,
134 IN UINTN CapsuleResultSize
135 )
136 {
137 if (CapsuleResultSize > sizeof(CAPSULE_RESULT_VARIABLE_CACHE)) {
138 CapsuleResultSize = sizeof(CAPSULE_RESULT_VARIABLE_CACHE);
139 }
140
141 if (mCapsuleResultVariableCacheCount == mCapsuleResultVariableCacheMaxCount) {
142 mCapsuleResultVariableCache = ReallocatePool(
143 mCapsuleResultVariableCacheMaxCount * sizeof(CAPSULE_RESULT_VARIABLE_CACHE),
144 (mCapsuleResultVariableCacheMaxCount + CAPSULE_RESULT_VARIABLE_CACHE_COUNT) * sizeof(CAPSULE_RESULT_VARIABLE_CACHE),
145 mCapsuleResultVariableCache
146 );
147 if (mCapsuleResultVariableCache == NULL) {
148 return EFI_OUT_OF_RESOURCES;
149 }
150 mCapsuleResultVariableCacheMaxCount += CAPSULE_RESULT_VARIABLE_CACHE_COUNT;
151 }
152
153 ASSERT(mCapsuleResultVariableCacheCount < mCapsuleResultVariableCacheMaxCount);
154 ASSERT(mCapsuleResultVariableCache != NULL);
155 CopyMem(
156 &mCapsuleResultVariableCache[mCapsuleResultVariableCacheCount],
157 CapsuleResult,
158 CapsuleResultSize
159 );
160 mCapsuleResultVariableCacheCount++;
161
162 return EFI_SUCCESS;
163 }
164
165 /**
166 Get a new capsule status variable index.
167
168 @return A new capsule status variable index.
169 @retval -1 No new capsule status variable index.
170 **/
171 INTN
172 GetNewCapsuleResultIndex (
173 VOID
174 )
175 {
176 INTN CurrentIndex;
177
178 CurrentIndex = GetCurrentCapsuleLastIndex();
179 if (CurrentIndex >= PcdGet16(PcdCapsuleMax)) {
180 return -1;
181 }
182
183 return CurrentIndex + 1;
184 }
185
186 /**
187 Write a new capsule status variable.
188
189 @param[in] CapsuleResult The capsule status variable
190 @param[in] CapsuleResultSize The size of the capsule stauts variable in bytes
191
192 @retval EFI_SUCCESS The capsule status variable is recorded.
193 @retval EFI_OUT_OF_RESOURCES No resource to record the capsule status variable.
194 **/
195 EFI_STATUS
196 WriteNewCapsuleResultVariable (
197 IN VOID *CapsuleResult,
198 IN UINTN CapsuleResultSize
199 )
200 {
201 INTN CapsuleResultIndex;
202 CHAR16 CapsuleResultStr[sizeof("Capsule####")];
203 UINTN Size;
204 EFI_STATUS Status;
205
206 CapsuleResultIndex = GetNewCapsuleResultIndex();
207 DEBUG((DEBUG_INFO, "New CapsuleResultIndex - 0x%x\n", CapsuleResultIndex));
208 if (CapsuleResultIndex == -1) {
209 return EFI_OUT_OF_RESOURCES;
210 }
211 UnicodeSPrint(
212 CapsuleResultStr,
213 sizeof(CapsuleResultStr),
214 L"Capsule%04x",
215 CapsuleResultIndex
216 );
217
218 Status = gRT->SetVariable(
219 CapsuleResultStr,
220 &gEfiCapsuleReportGuid,
221 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
222 CapsuleResultSize,
223 CapsuleResult
224 );
225 if (!EFI_ERROR(Status)) {
226 Size = sizeof(L"Capsule####") - sizeof(CHAR16); // no zero terminator
227 DEBUG((DEBUG_INFO, "Set CapsuleLast - %s\n", CapsuleResultStr));
228 Status = gRT->SetVariable(
229 L"CapsuleLast",
230 &gEfiCapsuleReportGuid,
231 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
232 Size,
233 CapsuleResultStr
234 );
235 }
236
237 return Status;
238 }
239
240 /**
241 Record capsule status variable and to local cache.
242
243 @param[in] CapsuleHeader The capsule image header
244 @param[in] CapsuleStatus The capsule process stauts
245
246 @retval EFI_SUCCESS The capsule status variable is recorded.
247 @retval EFI_OUT_OF_RESOURCES No resource to record the capsule status variable.
248 **/
249 EFI_STATUS
250 RecordCapsuleStatusVariable (
251 IN EFI_CAPSULE_HEADER *CapsuleHeader,
252 IN EFI_STATUS CapsuleStatus
253 )
254 {
255 EFI_CAPSULE_RESULT_VARIABLE_HEADER CapsuleResultVariable;
256 EFI_STATUS Status;
257
258 CapsuleResultVariable.VariableTotalSize = sizeof(CapsuleResultVariable);
259 CopyGuid (&CapsuleResultVariable.CapsuleGuid, &CapsuleHeader->CapsuleGuid);
260 ZeroMem(&CapsuleResultVariable.CapsuleProcessed, sizeof(CapsuleResultVariable.CapsuleProcessed));
261 gRT->GetTime(&CapsuleResultVariable.CapsuleProcessed, NULL);
262 CapsuleResultVariable.CapsuleStatus = CapsuleStatus;
263
264 //
265 // Save Local Cache
266 //
267 Status = WriteNewCapsuleResultVariableCache(&CapsuleResultVariable, sizeof(CapsuleResultVariable));
268
269 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {
270 Status = WriteNewCapsuleResultVariable(&CapsuleResultVariable, sizeof(CapsuleResultVariable));
271 }
272 return Status;
273 }
274
275 /**
276 Record FMP capsule status variable and to local cache.
277
278 @param[in] CapsuleHeader The capsule image header
279 @param[in] CapsuleStatus The capsule process stauts
280 @param[in] PayloadIndex FMP payload index
281 @param[in] ImageHeader FMP image header
282
283 @retval EFI_SUCCESS The capsule status variable is recorded.
284 @retval EFI_OUT_OF_RESOURCES No resource to record the capsule status variable.
285 **/
286 EFI_STATUS
287 RecordFmpCapsuleStatusVariable (
288 IN EFI_CAPSULE_HEADER *CapsuleHeader,
289 IN EFI_STATUS CapsuleStatus,
290 IN UINTN PayloadIndex,
291 IN EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *ImageHeader
292 )
293 {
294 UINT8 CapsuleResultVariable[sizeof(EFI_CAPSULE_RESULT_VARIABLE_HEADER) + sizeof(EFI_CAPSULE_RESULT_VARIABLE_FMP)];
295 EFI_CAPSULE_RESULT_VARIABLE_HEADER *CapsuleResultVariableHeader;
296 EFI_CAPSULE_RESULT_VARIABLE_FMP *CapsuleResultVariableFmp;
297 EFI_STATUS Status;
298
299 CapsuleResultVariableHeader = (VOID *)&CapsuleResultVariable[0];
300 CapsuleResultVariableHeader->VariableTotalSize = sizeof(CapsuleResultVariable);
301 CopyGuid(&CapsuleResultVariableHeader->CapsuleGuid, &CapsuleHeader->CapsuleGuid);
302 ZeroMem(&CapsuleResultVariableHeader->CapsuleProcessed, sizeof(CapsuleResultVariableHeader->CapsuleProcessed));
303 gRT->GetTime(&CapsuleResultVariableHeader->CapsuleProcessed, NULL);
304 CapsuleResultVariableHeader->CapsuleStatus = CapsuleStatus;
305
306 CapsuleResultVariableFmp = (VOID *)&CapsuleResultVariable[sizeof(EFI_CAPSULE_RESULT_VARIABLE_HEADER)];
307 CapsuleResultVariableFmp->Version = 0x1;
308 CapsuleResultVariableFmp->PayloadIndex = (UINT8)PayloadIndex;
309 CapsuleResultVariableFmp->UpdateImageIndex = ImageHeader->UpdateImageIndex;
310 CopyGuid (&CapsuleResultVariableFmp->UpdateImageTypeId, &ImageHeader->UpdateImageTypeId);
311
312 //
313 // Save Local Cache
314 //
315 Status = WriteNewCapsuleResultVariableCache(&CapsuleResultVariable, sizeof(CapsuleResultVariable));
316
317 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {
318 Status = WriteNewCapsuleResultVariable(&CapsuleResultVariable, sizeof(CapsuleResultVariable));
319 }
320 return Status;
321 }
322
323 /**
324 Initialize CapsuleMax variables.
325 **/
326 VOID
327 InitCapsuleMaxVariable (
328 VOID
329 )
330 {
331 EFI_STATUS Status;
332 UINTN Size;
333 CHAR16 CapsuleMaxStr[sizeof("Capsule####")];
334 EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock;
335
336 UnicodeSPrint(
337 CapsuleMaxStr,
338 sizeof(CapsuleMaxStr),
339 L"Capsule%04x",
340 PcdGet16(PcdCapsuleMax)
341 );
342
343 Size = sizeof(L"Capsule####") - sizeof(CHAR16); // no zero terminator
344 Status = gRT->SetVariable(
345 L"CapsuleMax",
346 &gEfiCapsuleReportGuid,
347 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
348 Size,
349 CapsuleMaxStr
350 );
351 if (!EFI_ERROR(Status)) {
352 // Lock it per UEFI spec.
353 Status = gBS->LocateProtocol(&gEdkiiVariableLockProtocolGuid, NULL, (VOID **)&VariableLock);
354 if (!EFI_ERROR(Status)) {
355 Status = VariableLock->RequestToLock(VariableLock, L"CapsuleMax", &gEfiCapsuleReportGuid);
356 ASSERT_EFI_ERROR(Status);
357 }
358 }
359 }
360
361 /**
362 Initialize CapsuleLast variables.
363 **/
364 VOID
365 InitCapsuleLastVariable (
366 VOID
367 )
368 {
369 EFI_STATUS Status;
370 EFI_BOOT_MODE BootMode;
371 EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock;
372 VOID *CapsuleResult;
373 UINTN Size;
374 CHAR16 CapsuleLastStr[sizeof("Capsule####")];
375
376 BootMode = GetBootModeHob();
377 if (BootMode == BOOT_ON_FLASH_UPDATE) {
378 Status = gRT->SetVariable(
379 L"CapsuleLast",
380 &gEfiCapsuleReportGuid,
381 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
382 0,
383 NULL
384 );
385 // Do not lock it because it will be updated later.
386 } else {
387 //
388 // Check if OS/APP cleared L"Capsule####"
389 //
390 ZeroMem(CapsuleLastStr, sizeof(CapsuleLastStr));
391 Size = sizeof(L"Capsule####") - sizeof(CHAR16); // no zero terminator
392 Status = gRT->GetVariable(
393 L"CapsuleLast",
394 &gEfiCapsuleReportGuid,
395 NULL,
396 &Size,
397 CapsuleLastStr
398 );
399 if (!EFI_ERROR(Status)) {
400 //
401 // L"CapsuleLast" is got, check if data is there.
402 //
403 Status = GetVariable2 (
404 CapsuleLastStr,
405 &gEfiCapsuleReportGuid,
406 (VOID **) &CapsuleResult,
407 NULL
408 );
409 if (EFI_ERROR(Status)) {
410 //
411 // If no data, delete L"CapsuleLast"
412 //
413 Status = gRT->SetVariable(
414 L"CapsuleLast",
415 &gEfiCapsuleReportGuid,
416 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
417 0,
418 NULL
419 );
420 }
421 }
422
423 // Lock it in normal boot path per UEFI spec.
424 Status = gBS->LocateProtocol(&gEdkiiVariableLockProtocolGuid, NULL, (VOID **)&VariableLock);
425 if (!EFI_ERROR(Status)) {
426 Status = VariableLock->RequestToLock(VariableLock, L"CapsuleLast", &gEfiCapsuleReportGuid);
427 ASSERT_EFI_ERROR(Status);
428 }
429 }
430 }
431
432 /**
433 Initialize capsule update variables.
434 **/
435 VOID
436 InitCapsuleUpdateVariable (
437 VOID
438 )
439 {
440 EFI_STATUS Status;
441 UINTN Index;
442 CHAR16 CapsuleVarName[30];
443 CHAR16 *TempVarName;
444
445 //
446 // Clear all the capsule variables CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...
447 // as early as possible which will avoid the next time boot after the capsule update
448 // will still into the capsule loop
449 //
450 StrCpyS (CapsuleVarName, sizeof(CapsuleVarName)/sizeof(CapsuleVarName[0]), EFI_CAPSULE_VARIABLE_NAME);
451 TempVarName = CapsuleVarName + StrLen (CapsuleVarName);
452 Index = 0;
453 while (TRUE) {
454 if (Index > 0) {
455 UnicodeValueToString (TempVarName, 0, Index, 0);
456 }
457 Status = gRT->SetVariable (
458 CapsuleVarName,
459 &gEfiCapsuleVendorGuid,
460 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,
461 0,
462 (VOID *)NULL
463 );
464 if (EFI_ERROR (Status)) {
465 //
466 // There is no capsule variables, quit
467 //
468 break;
469 }
470 Index++;
471 }
472 }
473
474 /**
475 Initialize capsule related variables.
476 **/
477 VOID
478 InitCapsuleVariable (
479 VOID
480 )
481 {
482 InitCapsuleUpdateVariable();
483 InitCapsuleMaxVariable();
484 InitCapsuleLastVariable();
485 //
486 // No need to clear L"Capsule####", because OS/APP should refer L"CapsuleLast"
487 // to check status and delete them.
488 //
489 }