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 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
|
/** @file
Process Capsule On Disk.
Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "CapsuleApp.h"
EFI_GUID mCapsuleOnDiskBootOptionGuid = {
0x4CC29BB7, 0x2413, 0x40A2, { 0xB0, 0x6D, 0x25, 0x3E, 0x37, 0x10, 0xF5, 0x32 }
};
/**
Get file name from file path.
@param FilePath File path.
@return Pointer to file name.
**/
CHAR16 *
GetFileNameFromPath (
CHAR16 *FilePath
)
{
EFI_STATUS Status;
EFI_SHELL_PROTOCOL *ShellProtocol;
SHELL_FILE_HANDLE Handle;
EFI_FILE_INFO *FileInfo;
ShellProtocol = GetShellProtocol ();
if (ShellProtocol == NULL) {
return NULL;
}
//
// Open file by FileName.
//
Status = ShellProtocol->OpenFileByName (
FilePath,
&Handle,
EFI_FILE_MODE_READ
);
if (EFI_ERROR (Status)) {
return NULL;
}
//
// Get file name from EFI_FILE_INFO.
//
FileInfo = ShellProtocol->GetFileInfo (Handle);
ShellProtocol->CloseFile (Handle);
if (FileInfo == NULL) {
return NULL;
}
return FileInfo->FileName;
}
/**
Check if the device path is EFI system Partition.
@param DevicePath The ESP device path.
@retval TRUE DevicePath is a device path for ESP.
@retval FALSE DevicePath is not a device path for ESP.
**/
BOOLEAN
IsEfiSysPartitionDevicePath (
EFI_DEVICE_PATH_PROTOCOL *DevicePath
)
{
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
HARDDRIVE_DEVICE_PATH *Hd;
EFI_HANDLE Handle;
//
// Check if the device path contains GPT node
//
TempDevicePath = DevicePath;
while (!IsDevicePathEnd (TempDevicePath)) {
if ((DevicePathType (TempDevicePath) == MEDIA_DEVICE_PATH) &&
(DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP))
{
Hd = (HARDDRIVE_DEVICE_PATH *)TempDevicePath;
if (Hd->MBRType == MBR_TYPE_EFI_PARTITION_TABLE_HEADER) {
break;
}
}
TempDevicePath = NextDevicePathNode (TempDevicePath);
}
if (!IsDevicePathEnd (TempDevicePath)) {
//
// Search for EFI system partition protocol on full device path in Boot Option
//
Status = gBS->LocateDevicePath (&gEfiPartTypeSystemPartGuid, &DevicePath, &Handle);
return EFI_ERROR (Status) ? FALSE : TRUE;
} else {
return FALSE;
}
}
/**
Dump all EFI System Partition.
**/
VOID
DumpAllEfiSysPartition (
VOID
)
{
EFI_HANDLE *SimpleFileSystemHandles;
UINTN NumberSimpleFileSystemHandles;
UINTN Index;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
UINTN NumberEfiSystemPartitions;
EFI_SHELL_PROTOCOL *ShellProtocol;
NumberEfiSystemPartitions = 0;
ShellProtocol = GetShellProtocol ();
if (ShellProtocol == NULL) {
Print (L"Get Shell Protocol Fail\n");
return;
}
Print (L"EFI System Partition list:\n");
gBS->LocateHandleBuffer (
ByProtocol,
&gEfiSimpleFileSystemProtocolGuid,
NULL,
&NumberSimpleFileSystemHandles,
&SimpleFileSystemHandles
);
for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {
DevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);
if (IsEfiSysPartitionDevicePath (DevicePath)) {
NumberEfiSystemPartitions++;
Print (L" %s\n %s\n", ShellProtocol->GetMapFromDevicePath (&DevicePath), ConvertDevicePathToText (DevicePath, TRUE, TRUE));
}
}
if (NumberEfiSystemPartitions == 0) {
Print (L" No ESP found.\n");
}
}
/**
Check if capsule is provisioned.
@retval TRUE Capsule is provisioned previously.
@retval FALSE No capsule is provisioned.
**/
BOOLEAN
IsCapsuleProvisioned (
VOID
)
{
EFI_STATUS Status;
UINT64 OsIndication;
UINTN DataSize;
OsIndication = 0;
DataSize = sizeof (UINT64);
Status = gRT->GetVariable (
L"OsIndications",
&gEfiGlobalVariableGuid,
NULL,
&DataSize,
&OsIndication
);
if (!EFI_ERROR (Status) &&
((OsIndication & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) != 0))
{
return TRUE;
}
return FALSE;
}
/**
Get one active Efi System Partition.
@param[out] FsDevicePath The device path of Fs
@param[out] Fs The file system within EfiSysPartition
@retval EFI_SUCCESS Get file system successfully
@retval EFI_NOT_FOUND No valid file system found
**/
EFI_STATUS
GetEfiSysPartition (
OUT EFI_DEVICE_PATH_PROTOCOL **FsDevicePath,
OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL **Fs
)
{
EFI_HANDLE *SimpleFileSystemHandles;
UINTN NumberSimpleFileSystemHandles;
UINTN Index;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
EFI_STATUS Status;
Status = gBS->LocateHandleBuffer (
ByProtocol,
&gEfiSimpleFileSystemProtocolGuid,
NULL,
&NumberSimpleFileSystemHandles,
&SimpleFileSystemHandles
);
if (EFI_ERROR (Status)) {
return EFI_NOT_FOUND;
}
for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {
DevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);
if (IsEfiSysPartitionDevicePath (DevicePath)) {
Status = gBS->HandleProtocol (SimpleFileSystemHandles[Index], &gEfiSimpleFileSystemProtocolGuid, (VOID **)Fs);
if (!EFI_ERROR (Status)) {
*FsDevicePath = DevicePath;
return EFI_SUCCESS;
}
}
}
return EFI_NOT_FOUND;
}
/**
Check if Active Efi System Partition within GPT is in the device path.
@param[in] DevicePath The device path
@param[out] FsDevicePath The device path of Fs
@param[out] Fs The file system within EfiSysPartition
@retval EFI_SUCCESS Get file system successfully
@retval EFI_NOT_FOUND No valid file system found
@retval others Get file system failed
**/
EFI_STATUS
GetEfiSysPartitionFromDevPath (
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
OUT EFI_DEVICE_PATH_PROTOCOL **FsDevicePath,
OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL **Fs
)
{
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
HARDDRIVE_DEVICE_PATH *Hd;
EFI_HANDLE Handle;
//
// Check if the device path contains GPT node
//
TempDevicePath = DevicePath;
while (!IsDevicePathEnd (TempDevicePath)) {
if ((DevicePathType (TempDevicePath) == MEDIA_DEVICE_PATH) &&
(DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP))
{
Hd = (HARDDRIVE_DEVICE_PATH *)TempDevicePath;
if (Hd->MBRType == MBR_TYPE_EFI_PARTITION_TABLE_HEADER) {
break;
}
}
TempDevicePath = NextDevicePathNode (TempDevicePath);
}
if (!IsDevicePathEnd (TempDevicePath)) {
//
// Search for EFI system partition protocol on full device path in Boot Option
//
Status = gBS->LocateDevicePath (&gEfiPartTypeSystemPartGuid, &DevicePath, &Handle);
//
// Search for simple file system on this handler
//
if (!EFI_ERROR (Status)) {
Status = gBS->HandleProtocol (Handle, &gEfiSimpleFileSystemProtocolGuid, (VOID **)Fs);
if (!EFI_ERROR (Status)) {
*FsDevicePath = DevicePathFromHandle (Handle);
return EFI_SUCCESS;
}
}
}
return EFI_NOT_FOUND;
}
/**
Get SimpleFileSystem from boot option file path.
@param[in] DevicePath The file path of boot option
@param[out] FullPath The full device path of boot device
@param[out] Fs The file system within EfiSysPartition
@retval EFI_SUCCESS Get file system successfully
@retval EFI_NOT_FOUND No valid file system found
@retval others Get file system failed
**/
EFI_STATUS
GetEfiSysPartitionFromBootOptionFilePath (
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
OUT EFI_DEVICE_PATH_PROTOCOL **FullPath,
OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL **Fs
)
{
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *CurFullPath;
EFI_DEVICE_PATH_PROTOCOL *PreFullPath;
EFI_DEVICE_PATH_PROTOCOL *FsFullPath;
CurFullPath = NULL;
FsFullPath = NULL;
//
// Try every full device Path generated from bootoption
//
do {
PreFullPath = CurFullPath;
CurFullPath = EfiBootManagerGetNextLoadOptionDevicePath (DevicePath, CurFullPath);
if (PreFullPath != NULL) {
FreePool (PreFullPath);
}
if (CurFullPath == NULL) {
//
// No Active EFI system partition is found in BootOption device path
//
Status = EFI_NOT_FOUND;
break;
}
DEBUG_CODE_BEGIN ();
CHAR16 *DevicePathStr;
DevicePathStr = ConvertDevicePathToText (CurFullPath, TRUE, TRUE);
if (DevicePathStr != NULL) {
DEBUG ((DEBUG_INFO, "Full device path %s\n", DevicePathStr));
FreePool (DevicePathStr);
}
DEBUG_CODE_END ();
Status = GetEfiSysPartitionFromDevPath (CurFullPath, &FsFullPath, Fs);
} while (EFI_ERROR (Status));
if (*Fs != NULL) {
*FullPath = FsFullPath;
return EFI_SUCCESS;
} else {
return EFI_NOT_FOUND;
}
}
/**
Get a valid SimpleFileSystem within EFI system partition.
@param[in] Map The FS mapping capsule write to
@param[out] BootNext The value of BootNext Variable
@param[out] Fs The file system within EfiSysPartition
@param[out] UpdateBootNext The flag to indicate whether update BootNext Variable
@retval EFI_SUCCESS Get FS successfully
@retval EFI_NOT_FOUND No valid FS found
@retval others Get FS failed
**/
EFI_STATUS
GetUpdateFileSystem (
IN CHAR16 *Map,
OUT UINT16 *BootNext,
OUT EFI_SIMPLE_FILE_SYSTEM_PROTOCOL **Fs,
OUT BOOLEAN *UpdateBootNext
)
{
EFI_STATUS Status;
CHAR16 BootOptionName[20];
UINTN Index;
CONST EFI_DEVICE_PATH_PROTOCOL *MappedDevicePath;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
EFI_DEVICE_PATH_PROTOCOL *FullPath;
UINT16 *BootNextData;
EFI_BOOT_MANAGER_LOAD_OPTION BootNextOption;
EFI_BOOT_MANAGER_LOAD_OPTION *BootOptionBuffer;
UINTN BootOptionCount;
EFI_SHELL_PROTOCOL *ShellProtocol;
EFI_BOOT_MANAGER_LOAD_OPTION NewOption;
MappedDevicePath = NULL;
BootOptionBuffer = NULL;
ShellProtocol = GetShellProtocol ();
if (ShellProtocol == NULL) {
Print (L"Get Shell Protocol Fail\n");
return EFI_NOT_FOUND;
}
//
// 1. If Fs is not assigned and there are capsule provisioned before,
// Get EFI system partition from BootNext.
//
if (IsCapsuleProvisioned () && (Map == NULL)) {
Status = GetVariable2 (
L"BootNext",
&gEfiGlobalVariableGuid,
(VOID **)&BootNextData,
NULL
);
if (EFI_ERROR (Status) || (BootNextData == NULL)) {
Print (L"Get Boot Next Data Fail. Status = %r\n", Status);
return EFI_NOT_FOUND;
} else {
UnicodeSPrint (BootOptionName, sizeof (BootOptionName), L"Boot%04x", *BootNextData);
Status = EfiBootManagerVariableToLoadOption (BootOptionName, &BootNextOption);
if (!EFI_ERROR (Status)) {
DevicePath = BootNextOption.FilePath;
Status = GetEfiSysPartitionFromBootOptionFilePath (DevicePath, &FullPath, Fs);
if (!EFI_ERROR (Status)) {
*UpdateBootNext = FALSE;
Print (L"Get EFI system partition from BootNext : %s\n", BootNextOption.Description);
Print (L"%s %s\n", ShellProtocol->GetMapFromDevicePath (&FullPath), ConvertDevicePathToText (FullPath, TRUE, TRUE));
return EFI_SUCCESS;
}
}
}
}
//
// Check if Map is valid.
//
if (Map != NULL) {
MappedDevicePath = ShellProtocol->GetDevicePathFromMap (Map);
if (MappedDevicePath == NULL) {
Print (L"'%s' is not a valid mapping.\n", Map);
return EFI_INVALID_PARAMETER;
} else if (!IsEfiSysPartitionDevicePath (DuplicateDevicePath (MappedDevicePath))) {
Print (L"'%s' is not a EFI System Partition.\n", Map);
return EFI_INVALID_PARAMETER;
}
}
//
// 2. Get EFI system partition form boot options.
//
BootOptionBuffer = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);
if ((BootOptionBuffer == NULL) ||
((BootOptionCount == 0) && (Map == NULL))
)
{
return EFI_NOT_FOUND;
}
for (Index = 0; Index < BootOptionCount; Index++) {
//
// Get the boot option from the link list
//
DevicePath = BootOptionBuffer[Index].FilePath;
//
// Skip inactive or legacy boot options
//
if (((BootOptionBuffer[Index].Attributes & LOAD_OPTION_ACTIVE) == 0) ||
(DevicePathType (DevicePath) == BBS_DEVICE_PATH))
{
continue;
}
DEBUG_CODE_BEGIN ();
CHAR16 *DevicePathStr;
DevicePathStr = ConvertDevicePathToText (DevicePath, TRUE, TRUE);
if (DevicePathStr != NULL) {
DEBUG ((DEBUG_INFO, "Try BootOption %s\n", DevicePathStr));
FreePool (DevicePathStr);
} else {
DEBUG ((DEBUG_INFO, "DevicePathToStr failed\n"));
}
DEBUG_CODE_END ();
Status = GetEfiSysPartitionFromBootOptionFilePath (DevicePath, &FullPath, Fs);
if (!EFI_ERROR (Status)) {
if (Map == NULL) {
*BootNext = (UINT16)BootOptionBuffer[Index].OptionNumber;
*UpdateBootNext = TRUE;
Print (L"Found EFI system partition on Boot%04x: %s\n", *BootNext, BootOptionBuffer[Index].Description);
Print (L"%s %s\n", ShellProtocol->GetMapFromDevicePath (&FullPath), ConvertDevicePathToText (FullPath, TRUE, TRUE));
return EFI_SUCCESS;
}
if (StrnCmp (Map, ShellProtocol->GetMapFromDevicePath (&FullPath), StrLen (Map)) == 0) {
*BootNext = (UINT16)BootOptionBuffer[Index].OptionNumber;
*UpdateBootNext = TRUE;
Print (L"Found Boot Option on %s : %s\n", Map, BootOptionBuffer[Index].Description);
return EFI_SUCCESS;
}
}
}
//
// 3. If no ESP is found on boot option, try to find a ESP and create boot option for it.
//
if (Map != NULL) {
//
// If map is assigned, try to get ESP from mapped Fs.
//
DevicePath = DuplicateDevicePath (MappedDevicePath);
Status = GetEfiSysPartitionFromDevPath (DevicePath, &FullPath, Fs);
if (EFI_ERROR (Status)) {
Print (L"Error: Cannot get EFI system partition from '%s' - %r\n", Map, Status);
return EFI_NOT_FOUND;
}
Print (L"Warning: Cannot find Boot Option on '%s'!\n", Map);
} else {
Status = GetEfiSysPartition (&DevicePath, Fs);
if (EFI_ERROR (Status)) {
Print (L"Error: Cannot find a EFI system partition!\n");
return EFI_NOT_FOUND;
}
}
Print (L"Create Boot option for capsule on disk:\n");
Status = EfiBootManagerInitializeLoadOption (
&NewOption,
LoadOptionNumberUnassigned,
LoadOptionTypeBoot,
LOAD_OPTION_ACTIVE,
L"UEFI Capsule On Disk",
DevicePath,
(UINT8 *)&mCapsuleOnDiskBootOptionGuid,
sizeof (EFI_GUID)
);
if (!EFI_ERROR (Status)) {
Status = EfiBootManagerAddLoadOptionVariable (&NewOption, (UINTN)-1);
{
if (!EFI_ERROR (Status)) {
*UpdateBootNext = TRUE;
*BootNext = (UINT16)NewOption.OptionNumber;
Print (L" Boot%04x: %s\n", *BootNext, ConvertDevicePathToText (DevicePath, TRUE, TRUE));
return EFI_SUCCESS;
}
}
}
Print (L"ERROR: Cannot create boot option! - %r\n", Status);
return EFI_NOT_FOUND;
}
/**
Write files to a given SimpleFileSystem.
@param[in] Buffer The buffer array
@param[in] BufferSize The buffer size array
@param[in] FileName The file name array
@param[in] BufferNum The buffer number
@param[in] Fs The SimpleFileSystem handle to be written
@retval EFI_SUCCESS Write file successfully
@retval EFI_NOT_FOUND SFS protocol not found
@retval others Write file failed
**/
EFI_STATUS
WriteUpdateFile (
IN VOID **Buffer,
IN UINTN *BufferSize,
IN CHAR16 **FileName,
IN UINTN BufferNum,
IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs
)
{
EFI_STATUS Status;
EFI_FILE *Root;
EFI_FILE *FileHandle;
EFI_FILE_PROTOCOL *DirHandle;
UINT64 FileInfo;
VOID *Filebuffer;
UINTN FileSize;
UINTN Index;
DirHandle = NULL;
FileHandle = NULL;
Index = 0;
//
// Open Root from SFS
//
Status = Fs->OpenVolume (Fs, &Root);
if (EFI_ERROR (Status)) {
Print (L"Cannot open volume. Status = %r\n", Status);
return EFI_NOT_FOUND;
}
//
// Ensure that efi and updatecapsule directories exist
//
Status = Root->Open (Root, &DirHandle, L"\\EFI", EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE, 0);
if (EFI_ERROR (Status)) {
Status = Root->Open (Root, &DirHandle, L"\\EFI", EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, EFI_FILE_DIRECTORY);
if (EFI_ERROR (Status)) {
Print (L"Unable to create %s directory\n", L"\\EFI");
return EFI_NOT_FOUND;
}
}
Status = Root->Open (Root, &DirHandle, EFI_CAPSULE_FILE_DIRECTORY, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE, 0);
if (EFI_ERROR (Status)) {
Status = Root->Open (Root, &DirHandle, EFI_CAPSULE_FILE_DIRECTORY, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, EFI_FILE_DIRECTORY);
if (EFI_ERROR (Status)) {
Print (L"Unable to create %s directory\n", EFI_CAPSULE_FILE_DIRECTORY);
return EFI_NOT_FOUND;
}
}
for (Index = 0; Index < BufferNum; Index++) {
FileHandle = NULL;
//
// Open UpdateCapsule file
//
Status = DirHandle->Open (DirHandle, &FileHandle, FileName[Index], EFI_FILE_MODE_CREATE | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ, 0);
if (EFI_ERROR (Status)) {
Print (L"Unable to create %s file\n", FileName[Index]);
return EFI_NOT_FOUND;
}
//
// Empty the file contents
//
Status = FileHandleGetSize (FileHandle, &FileInfo);
if (EFI_ERROR (Status)) {
FileHandleClose (FileHandle);
Print (L"Error Reading %s\n", FileName[Index]);
return EFI_DEVICE_ERROR;
}
//
// If the file size is already 0, then it has been empty.
//
if (FileInfo != 0) {
//
// Set the file size to 0.
//
FileInfo = 0;
Status = FileHandleSetSize (FileHandle, FileInfo);
if (EFI_ERROR (Status)) {
Print (L"Error Deleting %s\n", FileName[Index]);
FileHandleClose (FileHandle);
return Status;
}
}
//
// Write Filebuffer to file
//
Filebuffer = Buffer[Index];
FileSize = BufferSize[Index];
Status = FileHandleWrite (FileHandle, &FileSize, Filebuffer);
if (EFI_ERROR (Status)) {
Print (L"Unable to write Capsule Update to %s, Status = %r\n", FileName[Index], Status);
return EFI_NOT_FOUND;
}
Print (L"Succeed to write %s\n", FileName[Index]);
FileHandleClose (FileHandle);
}
return EFI_SUCCESS;
}
/**
Set capsule status variable.
@param[in] SetCap Set or clear the capsule flag.
@retval EFI_SUCCESS Succeed to set SetCap variable.
@retval others Fail to set the variable.
**/
EFI_STATUS
SetCapsuleStatusVariable (
BOOLEAN SetCap
)
{
EFI_STATUS Status;
UINT64 OsIndication;
UINTN DataSize;
OsIndication = 0;
DataSize = sizeof (UINT64);
Status = gRT->GetVariable (
L"OsIndications",
&gEfiGlobalVariableGuid,
NULL,
&DataSize,
&OsIndication
);
if (EFI_ERROR (Status)) {
OsIndication = 0;
}
if (SetCap) {
OsIndication |= ((UINT64)EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED);
} else {
OsIndication &= ~((UINT64)EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED);
}
Status = gRT->SetVariable (
L"OsIndications",
&gEfiGlobalVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
sizeof (UINT64),
&OsIndication
);
return Status;
}
/**
Check if Capsule On Disk is supported.
@retval TRUE Capsule On Disk is supported.
@retval FALSE Capsule On Disk is not supported.
**/
BOOLEAN
IsCapsuleOnDiskSupported (
VOID
)
{
EFI_STATUS Status;
UINT64 OsIndicationsSupported;
UINTN DataSize;
DataSize = sizeof (UINT64);
Status = gRT->GetVariable (
L"OsIndicationsSupported",
&gEfiGlobalVariableGuid,
NULL,
&DataSize,
&OsIndicationsSupported
);
if (EFI_ERROR (Status)) {
return FALSE;
}
if ((OsIndicationsSupported & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) != 0) {
return TRUE;
}
return FALSE;
}
/**
Process Capsule On Disk.
@param[in] CapsuleBuffer An array of pointer to capsule images
@param[in] CapsuleBufferSize An array of UINTN to capsule images size
@param[in] FilePath An array of capsule images file path
@param[in] Map File system mapping string
@param[in] CapsuleNum The count of capsule images
@retval EFI_SUCCESS Capsule on disk success.
@retval others Capsule on disk fail.
**/
EFI_STATUS
ProcessCapsuleOnDisk (
IN VOID **CapsuleBuffer,
IN UINTN *CapsuleBufferSize,
IN CHAR16 **FilePath,
IN CHAR16 *Map,
IN UINTN CapsuleNum
)
{
EFI_STATUS Status;
UINT16 BootNext;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs;
BOOLEAN UpdateBootNext;
CHAR16 *FileName[MAX_CAPSULE_NUM];
UINTN Index;
//
// Check if Capsule On Disk is supported
//
if (!IsCapsuleOnDiskSupported ()) {
Print (L"CapsuleApp: Capsule On Disk is not supported.\n");
return EFI_UNSUPPORTED;
}
//
// Get a valid file system from boot path
//
Fs = NULL;
Status = GetUpdateFileSystem (Map, &BootNext, &Fs, &UpdateBootNext);
if (EFI_ERROR (Status)) {
Print (L"CapsuleApp: cannot find a valid file system on boot devices. Status = %r\n", Status);
return Status;
}
//
// Get file name from file path
//
for (Index = 0; Index < CapsuleNum; Index++) {
FileName[Index] = GetFileNameFromPath (FilePath[Index]);
}
//
// Copy capsule image to '\efi\UpdateCapsule\'
//
Status = WriteUpdateFile (CapsuleBuffer, CapsuleBufferSize, FileName, CapsuleNum, Fs);
if (EFI_ERROR (Status)) {
Print (L"CapsuleApp: capsule image could not be copied for update.\n");
return Status;
}
//
// Set variable then reset
//
Status = SetCapsuleStatusVariable (TRUE);
if (EFI_ERROR (Status)) {
Print (L"CapsuleApp: unable to set OSIndication variable.\n");
return Status;
}
if (UpdateBootNext) {
Status = gRT->SetVariable (
L"BootNext",
&gEfiGlobalVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
sizeof (UINT16),
&BootNext
);
if (EFI_ERROR (Status)) {
Print (L"CapsuleApp: unable to set BootNext variable.\n");
return Status;
}
}
return EFI_SUCCESS;
}
|