2 Main file for LoadPciRom shell Debug1 function.
4 Copyright (c) 2005 - 2011, 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
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 "UefiShellDebug1CommandsLib.h"
16 #include <IndustryStandard/Pci22.h>
17 #include <IndustryStandard/Pci23.h>
18 #include <IndustryStandard/PeImage.h>
19 #include <Protocol/Decompress.h>
22 Connects all available drives and controllers.
24 @retval EFI_SUCCESS The operation was successful.
25 @retval EFI_ABORTED The abort mechanism was received.
29 LoadPciRomConnectAllDriversToAllControllers (
36 @param[in] RomBar The Rom Base address.
37 @param[in] RomSize The Rom size.
38 @param[in] FileName The file name.
40 @retval EFI_SUCCESS The command completed successfully.
41 @retval EFI_INVALID_PARAMETER Command usage error.
42 @retval EFI_UNSUPPORTED Protocols unsupported.
43 @retval EFI_OUT_OF_RESOURCES Out of memory.
44 @retval Other value Unknown error.
48 LoadEfiDriversFromRomImage (
51 CONST CHAR16
*FileName
54 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
60 Function for 'loadpcirom' command.
62 @param[in] ImageHandle Handle to the Image (NULL if Internal).
63 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
67 ShellCommandRunLoadPciRom (
68 IN EFI_HANDLE ImageHandle
,
69 IN EFI_SYSTEM_TABLE
*SystemTable
72 EFI_SHELL_FILE_INFO
*FileList
;
78 SHELL_STATUS ShellStatus
;
82 EFI_SHELL_FILE_INFO
*Node
;
84 // Local variable initializations
87 ShellStatus
= SHELL_SUCCESS
;
92 // verify number of arguments
94 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
95 if (EFI_ERROR(Status
)) {
96 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
97 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, ProblemParam
);
98 FreePool(ProblemParam
);
99 ShellStatus
= SHELL_INVALID_PARAMETER
;
104 if (ShellCommandLineGetCount(Package
) < 2) {
105 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
106 ShellStatus
= SHELL_INVALID_PARAMETER
;
108 if (ShellCommandLineGetFlag(Package
, L
"-nc")) {
115 // get a list with each file specified by parameters
116 // if parameter is a directory then add all the files below it to the list
118 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
120 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
122 Status
= ShellOpenFileMetaArg((CHAR16
*)Param
, EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
123 if (EFI_ERROR(Status
)) {
124 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_OPEN_FAIL
), gShellDebug1HiiHandle
, Param
, Status
);
125 ShellStatus
= SHELL_ACCESS_DENIED
;
129 if (ShellStatus
== SHELL_SUCCESS
&& FileList
!= NULL
) {
131 // loop through the list and make sure we are not aborting...
133 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
134 ; !IsNull(&FileList
->Link
, &Node
->Link
) && !ShellGetExecutionBreakFlag()
135 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
137 if (EFI_ERROR(Node
->Status
)){
138 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_OPEN_FAIL
), gShellDebug1HiiHandle
, Node
->FullName
);
139 ShellStatus
= SHELL_INVALID_PARAMETER
;
142 if (FileHandleIsDirectory(Node
->Handle
) == EFI_SUCCESS
) {
143 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_NOT_DIR
), gShellDebug1HiiHandle
, Node
->FullName
);
144 ShellStatus
= SHELL_INVALID_PARAMETER
;
147 SourceSize
= (UINTN
) Node
->Info
->FileSize
;
148 File1Buffer
= AllocateZeroPool (SourceSize
);
149 ASSERT(File1Buffer
!= NULL
);
150 Status
= gEfiShellProtocol
->ReadFile(Node
->Handle
, &SourceSize
, File1Buffer
);
151 if (EFI_ERROR(Status
)) {
152 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_READ_FAIL
), gShellDebug1HiiHandle
, Node
->FullName
);
153 ShellStatus
= SHELL_INVALID_PARAMETER
;
155 Status
= LoadEfiDriversFromRomImage (
161 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_LOAD_PCI_ROM_RES
), gShellDebug1HiiHandle
, Node
->FullName
, Status
);
163 FreePool(File1Buffer
);
165 } else if (ShellStatus
== SHELL_SUCCESS
) {
166 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_NOT_SPEC
), gShellDebug1HiiHandle
);
167 ShellStatus
= SHELL_NOT_FOUND
;
169 if (FileList
!= NULL
&& !IsListEmpty(&FileList
->Link
)) {
170 Status
= ShellCloseFileMetaArg(&FileList
);
175 Status
= LoadPciRomConnectAllDriversToAllControllers ();
180 return (ShellStatus
);
186 @param[in] RomBar The Rom Base address.
187 @param[in] RomSize The Rom size.
188 @param[in] FileName The file name.
190 @retval EFI_SUCCESS The command completed successfully.
191 @retval EFI_INVALID_PARAMETER Command usage error.
192 @retval EFI_UNSUPPORTED Protocols unsupported.
193 @retval EFI_OUT_OF_RESOURCES Out of memory.
194 @retval Other value Unknown error.
198 LoadEfiDriversFromRomImage (
201 CONST CHAR16
*FileName
205 EFI_PCI_EXPANSION_ROM_HEADER
*EfiRomHeader
;
206 PCI_DATA_STRUCTURE
*Pcir
;
211 EFI_HANDLE ImageHandle
;
213 EFI_STATUS ReturnStatus
;
214 CHAR16 RomFileName
[280];
215 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
217 UINT32 DestinationSize
;
221 VOID
*DecompressedImageBuffer
;
223 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
226 ReturnStatus
= EFI_NOT_FOUND
;
227 RomBarOffset
= (UINTN
) RomBar
;
231 EfiRomHeader
= (EFI_PCI_EXPANSION_ROM_HEADER
*) (UINTN
) RomBarOffset
;
233 if (EfiRomHeader
->Signature
!= 0xaa55) {
234 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_LOADPCIROM_CORRUPT
), gShellDebug1HiiHandle
, FileName
, ImageIndex
);
235 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_IMAGE_CORRUPT), HiiHandle, ImageIndex);
239 Pcir
= (PCI_DATA_STRUCTURE
*) (UINTN
) (RomBarOffset
+ EfiRomHeader
->PcirOffset
);
240 ImageSize
= Pcir
->ImageLength
* 512;
242 if ((Pcir
->CodeType
== PCI_CODE_TYPE_EFI_IMAGE
) &&
243 (EfiRomHeader
->EfiSignature
== EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
)
246 if ((EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
) ||
247 (EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
)
249 ImageOffset
= EfiRomHeader
->EfiImageHeaderOffset
;
250 ImageSize
= EfiRomHeader
->InitializationSize
* 512;
252 ImageBuffer
= (VOID
*) (UINTN
) (RomBarOffset
+ ImageOffset
);
253 ImageLength
= ImageSize
- ImageOffset
;
254 DecompressedImageBuffer
= NULL
;
257 // decompress here if needed
260 if (EfiRomHeader
->CompressionType
> EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
264 if (EfiRomHeader
->CompressionType
== EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
265 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
266 ASSERT_EFI_ERROR(Status
);
267 if (EFI_ERROR (Status
)) {
271 Status
= Decompress
->GetInfo (
278 if (!EFI_ERROR (Status
)) {
279 DecompressedImageBuffer
= AllocateZeroPool (DestinationSize
);
280 if (ImageBuffer
!= NULL
) {
281 Scratch
= AllocateZeroPool (ScratchSize
);
282 if (Scratch
!= NULL
) {
283 Status
= Decompress
->Decompress (
287 DecompressedImageBuffer
,
292 if (!EFI_ERROR (Status
)) {
293 ImageBuffer
= DecompressedImageBuffer
;
294 ImageLength
= DestinationSize
;
307 // load image and start image
309 UnicodeSPrint (RomFileName
, sizeof (RomFileName
), L
"%s[%d]", FileName
, ImageIndex
);
310 FilePath
= FileDevicePath (NULL
, RomFileName
);
312 Status
= gBS
->LoadImage (
320 if (EFI_ERROR (Status
)) {
321 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_LOADPCIROM_LOAD_FAIL
), gShellDebug1HiiHandle
, FileName
, ImageIndex
, Status
);
322 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_LOAD_IMAGE_ERROR), HiiHandle, ImageIndex, Status);
324 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
325 if (EFI_ERROR (Status
)) {
326 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_LOADPCIROM_START_FAIL
), gShellDebug1HiiHandle
, FileName
, ImageIndex
, Status
);
327 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_START_IMAGE), HiiHandle, ImageIndex, Status);
329 ReturnStatus
= Status
;
334 if (DecompressedImageBuffer
!= NULL
) {
335 FreePool (DecompressedImageBuffer
);
341 RomBarOffset
= RomBarOffset
+ ImageSize
;
343 } while (((Pcir
->Indicator
& 0x80) == 0x00) && ((RomBarOffset
- (UINTN
) RomBar
) < RomSize
));
349 Connects all available drives and controllers.
351 @retval EFI_SUCCESS The operation was successful.
352 @retval EFI_ABORTED The abort mechanism was received.
356 LoadPciRomConnectAllDriversToAllControllers (
362 UINTN AllHandleCount
;
363 EFI_HANDLE
*AllHandleBuffer
;
366 EFI_HANDLE
*HandleBuffer
;
372 Status
= gBS
->LocateHandleBuffer(
379 if (EFI_ERROR (Status
)) {
383 for (Index
= 0; Index
< AllHandleCount
; Index
++) {
384 if (ShellGetExecutionBreakFlag ()) {
385 Status
= EFI_ABORTED
;
389 // Scan the handle database
391 Status
= ParseHandleDatabaseByRelationshipWithType(
393 AllHandleBuffer
[Index
],
399 Status = LibScanHandleDatabase (
402 AllHandleBuffer[Index],
409 if (EFI_ERROR (Status
)) {
414 if ((HandleType
[Index
] & HR_DRIVER_BINDING_HANDLE
) != 0) {
418 if ((HandleType
[Index
] & HR_IMAGE_HANDLE
) != 0) {
424 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
425 if ((HandleType
[HandleIndex
] & HR_PARENT_HANDLE
) != 0) {
431 if ((HandleType
[Index
] & HR_DEVICE_HANDLE
) != 0) {
432 Status
= gBS
->ConnectController (
433 AllHandleBuffer
[Index
],
442 FreePool (HandleBuffer
);
443 FreePool (HandleType
);
447 FreePool (AllHandleBuffer
);