3 * Copyright (c) 2011-2013, ARM Limited. All rights reserved.
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
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.
15 #include "BdsInternal.h"
17 #include <Protocol/UsbIo.h>
18 #include <Protocol/DiskIo.h>
19 #include <Protocol/LoadedImage.h>
20 #include <Protocol/SimpleNetwork.h>
22 #define IS_DEVICE_PATH_NODE(node,type,subtype) (((node)->Type == (type)) && ((node)->SubType == (subtype)))
24 // Extract the FilePath from the Device Path
26 BdsExtractFilePathFromDevicePath (
27 IN CONST CHAR16
*StrDevicePath
,
28 IN UINTN NumberDevicePathNode
34 Str
= (CHAR16
*)StrDevicePath
;
36 while ((Str
!= NULL
) && (*Str
!= L
'\0') && (Node
< NumberDevicePathNode
)) {
37 if ((*Str
== L
'/') || (*Str
== L
'\\')) {
52 IN EFI_DEVICE_PATH
* DevicePath
55 return ((DevicePathType (DevicePath
) == MESSAGING_DEVICE_PATH
) &&
56 ((DevicePathSubType (DevicePath
) == MSG_USB_CLASS_DP
) ||
57 (DevicePathSubType (DevicePath
) == MSG_USB_WWID_DP
)));
62 IN EFI_DEVICE_PATH
* RemovableDevicePath
,
63 OUT EFI_HANDLE
* DeviceHandle
,
64 OUT EFI_DEVICE_PATH
** NewDevicePath
69 UINTN UsbIoHandleCount
;
70 EFI_HANDLE
*UsbIoBuffer
;
71 EFI_DEVICE_PATH
* UsbIoDevicePath
;
72 EFI_DEVICE_PATH
* TmpDevicePath
;
73 USB_WWID_DEVICE_PATH
* WwidDevicePath1
;
74 USB_WWID_DEVICE_PATH
* WwidDevicePath2
;
75 USB_CLASS_DEVICE_PATH
* UsbClassDevicePath1
;
76 USB_CLASS_DEVICE_PATH
* UsbClassDevicePath2
;
78 // Get all the UsbIo handles
80 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiUsbIoProtocolGuid
, NULL
, &UsbIoHandleCount
, &UsbIoBuffer
);
81 if (EFI_ERROR (Status
) || (UsbIoHandleCount
== 0)) {
85 // Check if one of the handles matches the USB description
86 for (Index
= 0; Index
< UsbIoHandleCount
; Index
++) {
87 Status
= gBS
->HandleProtocol (UsbIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
**) &UsbIoDevicePath
);
88 if (!EFI_ERROR (Status
)) {
89 TmpDevicePath
= UsbIoDevicePath
;
90 while (!IsDevicePathEnd (TmpDevicePath
)) {
91 // Check if the Device Path node is a USB Removable device Path node
92 if (BdsIsRemovableUsb (TmpDevicePath
)) {
93 if (TmpDevicePath
->SubType
== MSG_USB_WWID_DP
) {
94 WwidDevicePath1
= (USB_WWID_DEVICE_PATH
*)RemovableDevicePath
;
95 WwidDevicePath2
= (USB_WWID_DEVICE_PATH
*)TmpDevicePath
;
96 if ((WwidDevicePath1
->VendorId
== WwidDevicePath2
->VendorId
) &&
97 (WwidDevicePath1
->ProductId
== WwidDevicePath2
->ProductId
) &&
98 (CompareMem (WwidDevicePath1
+1, WwidDevicePath2
+1, DevicePathNodeLength(WwidDevicePath1
)-sizeof (USB_WWID_DEVICE_PATH
)) == 0))
100 *DeviceHandle
= UsbIoBuffer
[Index
];
101 // Add the additional original Device Path Nodes (eg: FilePath Device Path Node) to the new Device Path
102 *NewDevicePath
= AppendDevicePath (UsbIoDevicePath
, NextDevicePathNode (RemovableDevicePath
));
106 UsbClassDevicePath1
= (USB_CLASS_DEVICE_PATH
*)RemovableDevicePath
;
107 UsbClassDevicePath2
= (USB_CLASS_DEVICE_PATH
*)TmpDevicePath
;
108 if ((UsbClassDevicePath1
->VendorId
!= 0xFFFF) && (UsbClassDevicePath1
->VendorId
== UsbClassDevicePath2
->VendorId
) &&
109 (UsbClassDevicePath1
->ProductId
!= 0xFFFF) && (UsbClassDevicePath1
->ProductId
== UsbClassDevicePath2
->ProductId
) &&
110 (UsbClassDevicePath1
->DeviceClass
!= 0xFF) && (UsbClassDevicePath1
->DeviceClass
== UsbClassDevicePath2
->DeviceClass
) &&
111 (UsbClassDevicePath1
->DeviceSubClass
!= 0xFF) && (UsbClassDevicePath1
->DeviceSubClass
== UsbClassDevicePath2
->DeviceSubClass
) &&
112 (UsbClassDevicePath1
->DeviceProtocol
!= 0xFF) && (UsbClassDevicePath1
->DeviceProtocol
== UsbClassDevicePath2
->DeviceProtocol
))
114 *DeviceHandle
= UsbIoBuffer
[Index
];
115 // Add the additional original Device Path Nodes (eg: FilePath Device Path Node) to the new Device Path
116 *NewDevicePath
= AppendDevicePath (UsbIoDevicePath
, NextDevicePathNode (RemovableDevicePath
));
121 TmpDevicePath
= NextDevicePathNode (TmpDevicePath
);
127 return EFI_NOT_FOUND
;
132 IN EFI_DEVICE_PATH
* DevicePath
135 return IS_DEVICE_PATH_NODE (DevicePath
, MEDIA_DEVICE_PATH
, MEDIA_HARDDRIVE_DP
);
140 IN EFI_DEVICE_PATH
* RemovableDevicePath
,
141 OUT EFI_HANDLE
* DeviceHandle
,
142 OUT EFI_DEVICE_PATH
** NewDevicePath
147 UINTN PartitionHandleCount
;
148 EFI_HANDLE
*PartitionBuffer
;
149 EFI_DEVICE_PATH
* PartitionDevicePath
;
150 EFI_DEVICE_PATH
* TmpDevicePath
;
151 HARDDRIVE_DEVICE_PATH
* HardDriveDevicePath1
;
152 HARDDRIVE_DEVICE_PATH
* HardDriveDevicePath2
;
154 // Get all the DiskIo handles
155 PartitionHandleCount
= 0;
156 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiDiskIoProtocolGuid
, NULL
, &PartitionHandleCount
, &PartitionBuffer
);
157 if (EFI_ERROR (Status
) || (PartitionHandleCount
== 0)) {
161 // Check if one of the handles matches the Hard Disk Description
162 for (Index
= 0; Index
< PartitionHandleCount
; Index
++) {
163 Status
= gBS
->HandleProtocol (PartitionBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
**) &PartitionDevicePath
);
164 if (!EFI_ERROR (Status
)) {
165 TmpDevicePath
= PartitionDevicePath
;
166 while (!IsDevicePathEnd (TmpDevicePath
)) {
167 // Check if the Device Path node is a HD Removable device Path node
168 if (BdsIsRemovableHd (TmpDevicePath
)) {
169 HardDriveDevicePath1
= (HARDDRIVE_DEVICE_PATH
*)RemovableDevicePath
;
170 HardDriveDevicePath2
= (HARDDRIVE_DEVICE_PATH
*)TmpDevicePath
;
171 if ((HardDriveDevicePath1
->SignatureType
== HardDriveDevicePath2
->SignatureType
) &&
172 (CompareGuid ((EFI_GUID
*)HardDriveDevicePath1
->Signature
, (EFI_GUID
*)HardDriveDevicePath2
->Signature
) == TRUE
) &&
173 (HardDriveDevicePath1
->PartitionNumber
== HardDriveDevicePath2
->PartitionNumber
))
175 *DeviceHandle
= PartitionBuffer
[Index
];
176 // Add the additional original Device Path Nodes (eg: FilePath Device Path Node) to the new Device Path
177 *NewDevicePath
= AppendDevicePath (PartitionDevicePath
, NextDevicePathNode (RemovableDevicePath
));
181 TmpDevicePath
= NextDevicePathNode (TmpDevicePath
);
187 return EFI_NOT_FOUND
;
191 BdsIsRemovableCdrom (
192 IN EFI_DEVICE_PATH* DevicePath
195 return IS_DEVICE_PATH_NODE (DevicePath, MEDIA_DEVICE_PATH, MEDIA_CDROM_DP);
200 IN EFI_DEVICE_PATH* RemovableDevicePath,
201 OUT EFI_HANDLE* DeviceHandle,
202 OUT EFI_DEVICE_PATH** DevicePath
206 return EFI_UNSUPPORTED;
210 (*BDS_IS_REMOVABLE
) (
211 IN EFI_DEVICE_PATH
* DevicePath
216 IN EFI_DEVICE_PATH
* RemovableDevicePath
,
217 OUT EFI_HANDLE
* DeviceHandle
,
218 OUT EFI_DEVICE_PATH
** DevicePath
222 BDS_IS_REMOVABLE IsRemovable
;
223 BDS_GET_DEVICE GetDevice
;
224 } BDS_REMOVABLE_DEVICE_SUPPORT
;
226 BDS_REMOVABLE_DEVICE_SUPPORT RemovableDeviceSupport
[] = {
227 { BdsIsRemovableUsb
, BdsGetDeviceUsb
},
228 { BdsIsRemovableHd
, BdsGetDeviceHd
},
229 //{ BdsIsRemovableCdrom, BdsGetDeviceCdrom }
235 IN EFI_DEVICE_PATH
* DevicePath
239 EFI_DEVICE_PATH
* TmpDevicePath
;
241 TmpDevicePath
= DevicePath
;
242 while (!IsDevicePathEnd (TmpDevicePath
)) {
243 for (Index
= 0; Index
< sizeof (RemovableDeviceSupport
) / sizeof (BDS_REMOVABLE_DEVICE_SUPPORT
); Index
++) {
244 if (RemovableDeviceSupport
[Index
].IsRemovable (TmpDevicePath
)) {
248 TmpDevicePath
= NextDevicePathNode (TmpDevicePath
);
257 IN EFI_DEVICE_PATH
* DevicePath
,
258 OUT EFI_HANDLE
* DeviceHandle
,
259 OUT EFI_DEVICE_PATH
** NewDevicePath
264 EFI_DEVICE_PATH
* TmpDevicePath
;
265 BDS_REMOVABLE_DEVICE_SUPPORT
* RemovableDevice
;
266 EFI_DEVICE_PATH
* RemovableDevicePath
;
267 BOOLEAN RemovableFound
;
269 RemovableDevice
= NULL
;
270 RemovableDevicePath
= NULL
;
271 RemovableFound
= FALSE
;
272 TmpDevicePath
= DevicePath
;
274 while (!IsDevicePathEnd (TmpDevicePath
) && !RemovableFound
) {
275 for (Index
= 0; Index
< sizeof (RemovableDeviceSupport
) / sizeof (BDS_REMOVABLE_DEVICE_SUPPORT
); Index
++) {
276 RemovableDevice
= &RemovableDeviceSupport
[Index
];
277 if (RemovableDevice
->IsRemovable (TmpDevicePath
)) {
278 RemovableDevicePath
= TmpDevicePath
;
279 RemovableFound
= TRUE
;
283 TmpDevicePath
= NextDevicePathNode (TmpDevicePath
);
286 if (!RemovableFound
) {
287 return EFI_NOT_FOUND
;
290 // Search into the current started drivers
291 Status
= RemovableDevice
->GetDevice (RemovableDevicePath
, DeviceHandle
, NewDevicePath
);
292 if (Status
== EFI_NOT_FOUND
) {
293 // Connect all the drivers
294 BdsConnectAllDrivers ();
296 // Search again into all the drivers
297 Status
= RemovableDevice
->GetDevice (RemovableDevicePath
, DeviceHandle
, NewDevicePath
);
305 BdsConnectAndUpdateDevicePath (
306 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
307 OUT EFI_HANDLE
*Handle
,
308 OUT EFI_DEVICE_PATH_PROTOCOL
**RemainingDevicePath
311 EFI_DEVICE_PATH
* Remaining
;
312 EFI_DEVICE_PATH
* NewDevicePath
;
315 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
) || (Handle
== NULL
)) {
316 return EFI_INVALID_PARAMETER
;
320 Remaining
= *DevicePath
;
321 // The LocateDevicePath() function locates all devices on DevicePath that support Protocol and returns
322 // the handle to the device that is closest to DevicePath. On output, the device path pointer is modified
323 // to point to the remaining part of the device path
324 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &Remaining
, Handle
);
325 if (!EFI_ERROR (Status
)) {
326 // Recursive = FALSE: We do not want to start all the device tree
327 Status
= gBS
->ConnectController (*Handle
, NULL
, Remaining
, FALSE
);
330 /*// We need to check if RemainingDevicePath does not point on the last node. Otherwise, calling
331 // NextDevicePathNode () will return an undetermined Device Path Node
332 if (!IsDevicePathEnd (RemainingDevicePath)) {
333 RemainingDevicePath = NextDevicePathNode (RemainingDevicePath);
335 } while (!EFI_ERROR (Status
) && !IsDevicePathEnd (Remaining
));
337 if (!EFI_ERROR (Status
)) {
338 // Now, we have got the whole Device Path connected, call again ConnectController to ensure all the supported Driver
339 // Binding Protocol are connected (such as DiskIo and SimpleFileSystem)
340 Remaining
= *DevicePath
;
341 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &Remaining
, Handle
);
342 if (!EFI_ERROR (Status
)) {
343 Status
= gBS
->ConnectController (*Handle
, NULL
, Remaining
, FALSE
);
344 if (EFI_ERROR (Status
)) {
345 // If the last node is a Memory Map Device Path just return EFI_SUCCESS.
346 if ((Remaining
->Type
== HARDWARE_DEVICE_PATH
) && (Remaining
->SubType
== HW_MEMMAP_DP
)) {
347 Status
= EFI_SUCCESS
;
351 } else if (!IsDevicePathEnd (Remaining
) && !IsRemovableDevice (Remaining
)) {
353 /*// If the remaining Device Path is a FilePath or MemoryMap then we consider the Device Path has been loaded correctly
354 if ((Remaining->Type == MEDIA_DEVICE_PATH) && (Remaining->SubType == MEDIA_FILEPATH_DP)) {
355 Status = EFI_SUCCESS;
356 } else if ((Remaining->Type == HARDWARE_DEVICE_PATH) && (Remaining->SubType == HW_MEMMAP_DP)) {
357 Status = EFI_SUCCESS;
360 //TODO: Should we just return success and leave the caller decide if it is the expected RemainingPath
361 Status
= EFI_SUCCESS
;
363 Status
= TryRemovableDevice (*DevicePath
, Handle
, &NewDevicePath
);
364 if (!EFI_ERROR (Status
)) {
365 Status
= BdsConnectAndUpdateDevicePath (&NewDevicePath
, Handle
, RemainingDevicePath
);
366 *DevicePath
= NewDevicePath
;
371 if (RemainingDevicePath
) {
372 *RemainingDevicePath
= Remaining
;
379 Connect a Device Path and return the handle of the driver that support this DevicePath
381 @param DevicePath Device Path of the File to connect
382 @param Handle Handle of the driver that support this DevicePath
383 @param RemainingDevicePath Remaining DevicePath nodes that do not match the driver DevicePath
385 @retval EFI_SUCCESS A driver that matches the Device Path has been found
386 @retval EFI_NOT_FOUND No handles match the search.
387 @retval EFI_INVALID_PARAMETER DevicePath or Handle is NULL
391 BdsConnectDevicePath (
392 IN EFI_DEVICE_PATH_PROTOCOL
* DevicePath
,
393 OUT EFI_HANDLE
*Handle
,
394 OUT EFI_DEVICE_PATH_PROTOCOL
**RemainingDevicePath
397 return BdsConnectAndUpdateDevicePath (&DevicePath
, Handle
, RemainingDevicePath
);
401 BdsFileSystemSupport (
402 IN EFI_DEVICE_PATH
*DevicePath
,
403 IN EFI_HANDLE Handle
,
404 IN EFI_DEVICE_PATH
*RemainingDevicePath
408 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*FsProtocol
;
410 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&FsProtocol
);
412 return (!EFI_ERROR (Status
) && IS_DEVICE_PATH_NODE (RemainingDevicePath
, MEDIA_DEVICE_PATH
, MEDIA_FILEPATH_DP
));
416 BdsFileSystemLoadImage (
417 IN EFI_DEVICE_PATH
*DevicePath
,
418 IN EFI_HANDLE Handle
,
419 IN EFI_DEVICE_PATH
*RemainingDevicePath
,
420 IN EFI_ALLOCATE_TYPE Type
,
421 IN OUT EFI_PHYSICAL_ADDRESS
* Image
,
425 FILEPATH_DEVICE_PATH
* FilePathDevicePath
;
426 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*FsProtocol
;
427 EFI_FILE_PROTOCOL
*Fs
;
429 EFI_FILE_INFO
*FileInfo
;
430 EFI_FILE_PROTOCOL
*File
;
433 ASSERT (IS_DEVICE_PATH_NODE (RemainingDevicePath
, MEDIA_DEVICE_PATH
, MEDIA_FILEPATH_DP
));
435 FilePathDevicePath
= (FILEPATH_DEVICE_PATH
*)RemainingDevicePath
;
437 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&FsProtocol
);
438 if (EFI_ERROR (Status
)) {
442 // Try to Open the volume and get root directory
443 Status
= FsProtocol
->OpenVolume (FsProtocol
, &Fs
);
444 if (EFI_ERROR (Status
)) {
449 Status
= Fs
->Open (Fs
, &File
, FilePathDevicePath
->PathName
, EFI_FILE_MODE_READ
, 0);
450 if (EFI_ERROR (Status
)) {
455 File
->GetInfo (File
, &gEfiFileInfoGuid
, &Size
, NULL
);
456 FileInfo
= AllocatePool (Size
);
457 Status
= File
->GetInfo (File
, &gEfiFileInfoGuid
, &Size
, FileInfo
);
458 if (EFI_ERROR (Status
)) {
463 Size
= FileInfo
->FileSize
;
469 Status
= gBS
->AllocatePages (Type
, EfiBootServicesCode
, EFI_SIZE_TO_PAGES(Size
), Image
);
470 // Try to allocate in any pages if failed to allocate memory at the defined location
471 if ((Status
== EFI_OUT_OF_RESOURCES
) && (Type
!= AllocateAnyPages
)) {
472 Status
= gBS
->AllocatePages (AllocateAnyPages
, EfiBootServicesCode
, EFI_SIZE_TO_PAGES(Size
), Image
);
474 if (!EFI_ERROR (Status
)) {
475 Status
= File
->Read (File
, &Size
, (VOID
*)(UINTN
)(*Image
));
482 BdsMemoryMapSupport (
483 IN EFI_DEVICE_PATH
*DevicePath
,
484 IN EFI_HANDLE Handle
,
485 IN EFI_DEVICE_PATH
*RemainingDevicePath
488 return IS_DEVICE_PATH_NODE (DevicePath
, HARDWARE_DEVICE_PATH
, HW_MEMMAP_DP
) ||
489 IS_DEVICE_PATH_NODE (RemainingDevicePath
, HARDWARE_DEVICE_PATH
, HW_MEMMAP_DP
);
493 BdsMemoryMapLoadImage (
494 IN EFI_DEVICE_PATH
*DevicePath
,
495 IN EFI_HANDLE Handle
,
496 IN EFI_DEVICE_PATH
*RemainingDevicePath
,
497 IN EFI_ALLOCATE_TYPE Type
,
498 IN OUT EFI_PHYSICAL_ADDRESS
* Image
,
503 MEMMAP_DEVICE_PATH
* MemMapPathDevicePath
;
506 if (IS_DEVICE_PATH_NODE (RemainingDevicePath
, HARDWARE_DEVICE_PATH
, HW_MEMMAP_DP
)) {
507 MemMapPathDevicePath
= (MEMMAP_DEVICE_PATH
*)RemainingDevicePath
;
509 ASSERT (IS_DEVICE_PATH_NODE (DevicePath
, HARDWARE_DEVICE_PATH
, HW_MEMMAP_DP
));
510 MemMapPathDevicePath
= (MEMMAP_DEVICE_PATH
*)DevicePath
;
513 Size
= MemMapPathDevicePath
->EndingAddress
- MemMapPathDevicePath
->StartingAddress
;
515 return EFI_INVALID_PARAMETER
;
518 Status
= gBS
->AllocatePages (Type
, EfiBootServicesCode
, EFI_SIZE_TO_PAGES(Size
), Image
);
519 // Try to allocate in any pages if failed to allocate memory at the defined location
520 if ((Status
== EFI_OUT_OF_RESOURCES
) && (Type
!= AllocateAnyPages
)) {
521 Status
= gBS
->AllocatePages (AllocateAnyPages
, EfiBootServicesCode
, EFI_SIZE_TO_PAGES(Size
), Image
);
523 if (!EFI_ERROR (Status
)) {
524 CopyMem ((VOID
*)(UINTN
)(*Image
), (CONST VOID
*)(UINTN
)MemMapPathDevicePath
->StartingAddress
, Size
);
526 if (ImageSize
!= NULL
) {
535 BdsFirmwareVolumeSupport (
536 IN EFI_DEVICE_PATH
*DevicePath
,
537 IN EFI_HANDLE Handle
,
538 IN EFI_DEVICE_PATH
*RemainingDevicePath
541 return IS_DEVICE_PATH_NODE (RemainingDevicePath
, MEDIA_DEVICE_PATH
, MEDIA_PIWG_FW_FILE_DP
);
545 BdsFirmwareVolumeLoadImage (
546 IN EFI_DEVICE_PATH
*DevicePath
,
547 IN EFI_HANDLE Handle
,
548 IN EFI_DEVICE_PATH
*RemainingDevicePath
,
549 IN EFI_ALLOCATE_TYPE Type
,
550 IN OUT EFI_PHYSICAL_ADDRESS
* Image
,
555 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FwVol
;
556 EFI_GUID
*FvNameGuid
;
557 EFI_SECTION_TYPE SectionType
;
558 EFI_FV_FILETYPE FvType
;
559 EFI_FV_FILE_ATTRIBUTES Attrib
;
560 UINT32 AuthenticationStatus
;
563 ASSERT (IS_DEVICE_PATH_NODE (RemainingDevicePath
, MEDIA_DEVICE_PATH
, MEDIA_PIWG_FW_FILE_DP
));
565 Status
= gBS
->HandleProtocol (Handle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&FwVol
);
566 if (EFI_ERROR (Status
)) {
570 FvNameGuid
= EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*)RemainingDevicePath
);
571 if (FvNameGuid
== NULL
) {
572 Status
= EFI_INVALID_PARAMETER
;
575 SectionType
= EFI_SECTION_PE32
;
576 AuthenticationStatus
= 0;
577 //Note: ReadSection at the opposite of ReadFile does not allow to pass ImageBuffer == NULL to get the size of the file.
579 Status
= FwVol
->ReadSection (
586 &AuthenticationStatus
588 if (!EFI_ERROR (Status
)) {
590 // In case the buffer has some address requirements, we must copy the buffer to a buffer following the requirements
591 if (Type
!= AllocateAnyPages
) {
592 Status
= gBS
->AllocatePages (Type
, EfiBootServicesCode
, EFI_SIZE_TO_PAGES(*ImageSize
),Image
);
593 if (!EFI_ERROR (Status
)) {
594 CopyMem ((VOID
*)(UINTN
)(*Image
), ImageBuffer
, *ImageSize
);
595 FreePool (ImageBuffer
);
599 // We must copy the buffer into a page allocations. Otherwise, the caller could call gBS->FreePages() on the pool allocation
600 Status
= gBS
->AllocatePages (Type
, EfiBootServicesCode
, EFI_SIZE_TO_PAGES(*ImageSize
), Image
);
601 // Try to allocate in any pages if failed to allocate memory at the defined location
602 if ((Status
== EFI_OUT_OF_RESOURCES
) && (Type
!= AllocateAnyPages
)) {
603 Status
= gBS
->AllocatePages (AllocateAnyPages
, EfiBootServicesCode
, EFI_SIZE_TO_PAGES(*ImageSize
), Image
);
605 if (!EFI_ERROR (Status
)) {
606 CopyMem ((VOID
*)(UINTN
)(*Image
), ImageBuffer
, *ImageSize
);
607 FreePool (ImageBuffer
);
611 // Try a raw file, since a PE32 SECTION does not exist
612 Status
= FwVol
->ReadFile (
619 &AuthenticationStatus
621 if (!EFI_ERROR (Status
)) {
622 Status
= gBS
->AllocatePages (Type
, EfiBootServicesCode
, EFI_SIZE_TO_PAGES(*ImageSize
), Image
);
623 // Try to allocate in any pages if failed to allocate memory at the defined location
624 if ((Status
== EFI_OUT_OF_RESOURCES
) && (Type
!= AllocateAnyPages
)) {
625 Status
= gBS
->AllocatePages (AllocateAnyPages
, EfiBootServicesCode
, EFI_SIZE_TO_PAGES(*ImageSize
), Image
);
627 if (!EFI_ERROR (Status
)) {
628 Status
= FwVol
->ReadFile (
631 (VOID
*)(UINTN
)(*Image
),
635 &AuthenticationStatus
645 IN EFI_DEVICE_PATH
* DevicePath
,
646 IN EFI_HANDLE Handle
,
647 IN EFI_DEVICE_PATH
* RemainingDevicePath
651 EFI_PXE_BASE_CODE_PROTOCOL
* PxeBcProtocol
;
653 if (!IsDevicePathEnd (RemainingDevicePath
)) {
657 Status
= gBS
->HandleProtocol (Handle
, &gEfiPxeBaseCodeProtocolGuid
, (VOID
**)&PxeBcProtocol
);
658 if (EFI_ERROR (Status
)) {
667 IN EFI_DEVICE_PATH
* DevicePath
,
668 IN EFI_HANDLE Handle
,
669 IN EFI_DEVICE_PATH
* RemainingDevicePath
,
670 IN EFI_ALLOCATE_TYPE Type
,
671 IN OUT EFI_PHYSICAL_ADDRESS
*Image
,
676 EFI_LOAD_FILE_PROTOCOL
*LoadFileProtocol
;
678 EFI_PXE_BASE_CODE_PROTOCOL
*Pxe
;
680 // Get Load File Protocol attached to the PXE protocol
681 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFileProtocolGuid
, (VOID
**)&LoadFileProtocol
);
682 if (EFI_ERROR (Status
)) {
686 Status
= LoadFileProtocol
->LoadFile (LoadFileProtocol
, DevicePath
, TRUE
, &BufferSize
, NULL
);
687 if (Status
== EFI_BUFFER_TOO_SMALL
) {
688 Status
= gBS
->AllocatePages (Type
, EfiBootServicesCode
, EFI_SIZE_TO_PAGES(BufferSize
), Image
);
689 if (EFI_ERROR (Status
)) {
693 Status
= LoadFileProtocol
->LoadFile (LoadFileProtocol
, DevicePath
, TRUE
, &BufferSize
, (VOID
*)(UINTN
)(*Image
));
694 if (!EFI_ERROR (Status
) && (ImageSize
!= NULL
)) {
695 *ImageSize
= BufferSize
;
699 if (Status
== EFI_ALREADY_STARTED
) {
700 Status
= gBS
->LocateProtocol (&gEfiPxeBaseCodeProtocolGuid
, NULL
, (VOID
**)&Pxe
);
701 if (!EFI_ERROR(Status
)) {
702 // If PXE is already started, we stop it
705 return BdsPxeLoadImage (DevicePath
, Handle
, RemainingDevicePath
, Type
, Image
, ImageSize
);
713 IN EFI_DEVICE_PATH
* DevicePath
,
714 IN EFI_HANDLE Handle
,
715 IN EFI_DEVICE_PATH
* RemainingDevicePath
719 EFI_DEVICE_PATH
*NextDevicePath
;
720 EFI_PXE_BASE_CODE_PROTOCOL
*PxeBcProtocol
;
722 // Validate the Remaining Device Path
723 if (IsDevicePathEnd (RemainingDevicePath
)) {
726 if (!IS_DEVICE_PATH_NODE (RemainingDevicePath
, MESSAGING_DEVICE_PATH
, MSG_IPv4_DP
) &&
727 !IS_DEVICE_PATH_NODE (RemainingDevicePath
, MESSAGING_DEVICE_PATH
, MSG_IPv6_DP
)) {
730 NextDevicePath
= NextDevicePathNode (RemainingDevicePath
);
731 if (IsDevicePathEnd (NextDevicePath
)) {
734 if (!IS_DEVICE_PATH_NODE (NextDevicePath
, MEDIA_DEVICE_PATH
, MEDIA_FILEPATH_DP
)) {
738 Status
= gBS
->HandleProtocol (Handle
, &gEfiPxeBaseCodeProtocolGuid
, (VOID
**)&PxeBcProtocol
);
739 if (EFI_ERROR (Status
)) {
748 IN EFI_DEVICE_PATH
* DevicePath
,
749 IN EFI_HANDLE Handle
,
750 IN EFI_DEVICE_PATH
* RemainingDevicePath
,
751 IN EFI_ALLOCATE_TYPE Type
,
752 IN OUT EFI_PHYSICAL_ADDRESS
*Image
,
757 EFI_PXE_BASE_CODE_PROTOCOL
*Pxe
;
758 UINT64 TftpBufferSize
;
760 EFI_IP_ADDRESS ServerIp
;
761 IPv4_DEVICE_PATH
* IPv4DevicePathNode
;
762 FILEPATH_DEVICE_PATH
* FilePathDevicePath
;
763 EFI_IP_ADDRESS LocalIp
;
764 CHAR8
* AsciiPathName
;
765 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
767 ASSERT(IS_DEVICE_PATH_NODE (RemainingDevicePath
, MESSAGING_DEVICE_PATH
, MSG_IPv4_DP
));
769 IPv4DevicePathNode
= (IPv4_DEVICE_PATH
*)RemainingDevicePath
;
770 FilePathDevicePath
= (FILEPATH_DEVICE_PATH
*)(IPv4DevicePathNode
+ 1);
772 Status
= gBS
->LocateProtocol (&gEfiPxeBaseCodeProtocolGuid
, NULL
, (VOID
**)&Pxe
);
773 if (EFI_ERROR (Status
)) {
777 Status
= Pxe
->Start (Pxe
, FALSE
);
778 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
783 if (!IPv4DevicePathNode
->StaticIpAddress
) {
784 Status
= Pxe
->Dhcp (Pxe
, TRUE
);
786 CopyMem (&LocalIp
.v4
, &IPv4DevicePathNode
->LocalIpAddress
, sizeof (EFI_IPv4_ADDRESS
));
787 Status
= Pxe
->SetStationIp (Pxe
, &LocalIp
, NULL
);
790 // If an IP Address has already been set and a different static IP address is requested then restart
791 // the Network service.
792 if (Status
== EFI_ALREADY_STARTED
) {
793 Status
= gBS
->LocateProtocol (&gEfiSimpleNetworkProtocolGuid
, NULL
, (VOID
**)&Snp
);
794 if (!EFI_ERROR (Status
) && IPv4DevicePathNode
->StaticIpAddress
&&
795 (CompareMem (&Snp
->Mode
->CurrentAddress
, &IPv4DevicePathNode
->LocalIpAddress
, sizeof(EFI_MAC_ADDRESS
)) != 0))
798 Status
= Pxe
->Start (Pxe
, FALSE
);
799 if (EFI_ERROR(Status
)) {
802 // After restarting the PXE protocol, we want to try again with our new IP Address
803 Status
= EFI_ALREADY_STARTED
;
806 } while (Status
== EFI_ALREADY_STARTED
);
808 if (EFI_ERROR(Status
)) {
812 CopyMem (&ServerIp
.v4
, &IPv4DevicePathNode
->RemoteIpAddress
, sizeof (EFI_IPv4_ADDRESS
));
814 // Convert the Unicode PathName to Ascii
815 AsciiPathName
= AllocatePool ((StrLen (FilePathDevicePath
->PathName
) + 1) * sizeof (CHAR8
));
816 if (AsciiPathName
== NULL
) {
817 return EFI_OUT_OF_RESOURCES
;
819 UnicodeStrToAsciiStr (FilePathDevicePath
->PathName
, AsciiPathName
);
821 Status
= Pxe
->Mtftp (
823 EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE
,
829 (UINT8
*)AsciiPathName
,
833 if (EFI_ERROR(Status
)) {
834 if (Status
== EFI_TFTP_ERROR
) {
835 DEBUG((EFI_D_ERROR
, "TFTP Error: Fail to get the size of the file\n"));
840 // Allocate a buffer to hold the whole file.
841 TftpBuffer
= AllocatePool (TftpBufferSize
);
842 if (TftpBuffer
== NULL
) {
843 Status
= EFI_OUT_OF_RESOURCES
;
847 Status
= Pxe
->Mtftp (
849 EFI_PXE_BASE_CODE_TFTP_READ_FILE
,
855 (UINT8
*)AsciiPathName
,
859 if (EFI_ERROR (Status
)) {
860 FreePool (TftpBuffer
);
861 } else if (ImageSize
!= NULL
) {
862 *Image
= (UINTN
)TftpBuffer
;
863 *ImageSize
= (UINTN
)TftpBufferSize
;
867 FreePool (AsciiPathName
);
871 BDS_FILE_LOADER FileLoaders
[] = {
872 { BdsFileSystemSupport
, BdsFileSystemLoadImage
},
873 { BdsFirmwareVolumeSupport
, BdsFirmwareVolumeLoadImage
},
874 //{ BdsLoadFileSupport, BdsLoadFileLoadImage },
875 { BdsMemoryMapSupport
, BdsMemoryMapLoadImage
},
876 { BdsPxeSupport
, BdsPxeLoadImage
},
877 { BdsTftpSupport
, BdsTftpLoadImage
},
882 BdsLoadImageAndUpdateDevicePath (
883 IN OUT EFI_DEVICE_PATH
**DevicePath
,
884 IN EFI_ALLOCATE_TYPE Type
,
885 IN OUT EFI_PHYSICAL_ADDRESS
* Image
,
891 EFI_DEVICE_PATH
*RemainingDevicePath
;
892 BDS_FILE_LOADER
* FileLoader
;
894 Status
= BdsConnectAndUpdateDevicePath (DevicePath
, &Handle
, &RemainingDevicePath
);
895 if (EFI_ERROR (Status
)) {
899 FileLoader
= FileLoaders
;
900 while (FileLoader
->Support
!= NULL
) {
901 if (FileLoader
->Support (*DevicePath
, Handle
, RemainingDevicePath
)) {
902 return FileLoader
->LoadImage (*DevicePath
, Handle
, RemainingDevicePath
, Type
, Image
, FileSize
);
907 return EFI_UNSUPPORTED
;
912 IN EFI_DEVICE_PATH
*DevicePath
,
913 IN EFI_ALLOCATE_TYPE Type
,
914 IN OUT EFI_PHYSICAL_ADDRESS
* Image
,
918 return BdsLoadImageAndUpdateDevicePath (&DevicePath
, Type
, Image
, FileSize
);
922 Start an EFI Application from a Device Path
924 @param ParentImageHandle Handle of the calling image
925 @param DevicePath Location of the EFI Application
927 @retval EFI_SUCCESS All drivers have been connected
928 @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found
929 @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results.
933 BdsStartEfiApplication (
934 IN EFI_HANDLE ParentImageHandle
,
935 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
936 IN UINTN LoadOptionsSize
,
941 EFI_HANDLE ImageHandle
;
942 EFI_PHYSICAL_ADDRESS BinaryBuffer
;
944 EFI_LOADED_IMAGE_PROTOCOL
* LoadedImage
;
946 // Find the nearest supported file loader
947 Status
= BdsLoadImageAndUpdateDevicePath (&DevicePath
, AllocateAnyPages
, &BinaryBuffer
, &BinarySize
);
948 if (EFI_ERROR (Status
)) {
952 // Load the image from the Buffer with Boot Services function
953 Status
= gBS
->LoadImage (TRUE
, ParentImageHandle
, DevicePath
, (VOID
*)(UINTN
)BinaryBuffer
, BinarySize
, &ImageHandle
);
954 if (EFI_ERROR (Status
)) {
958 // Passed LoadOptions to the EFI Application
959 if (LoadOptionsSize
!= 0) {
960 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &LoadedImage
);
961 if (EFI_ERROR (Status
)) {
965 LoadedImage
->LoadOptionsSize
= LoadOptionsSize
;
966 LoadedImage
->LoadOptions
= LoadOptions
;
969 // Before calling the image, enable the Watchdog Timer for the 5 Minute period
970 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
972 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
973 // Clear the Watchdog Timer after the image returns
974 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);