2 Main file for LoadPciRom shell Debug1 function.
4 Copyright (c) 2005 - 2010, 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>
23 LoadPciRomConnectAllDriversToAllControllers (
29 InitializeLoadPciRom (
30 IN EFI_HANDLE ImageHandle
,
31 IN EFI_SYSTEM_TABLE
*SystemTable
36 LoadEfiDriversFromRomImage (
39 CONST CHAR16
*FileName
42 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
49 ShellCommandRunLoadPciRom (
50 IN EFI_HANDLE ImageHandle
,
51 IN EFI_SYSTEM_TABLE
*SystemTable
54 EFI_SHELL_FILE_INFO
*FileList
;
60 SHELL_STATUS ShellStatus
;
64 EFI_SHELL_FILE_INFO
*Node
;
66 // Local variable initializations
69 ShellStatus
= SHELL_SUCCESS
;
74 // verify number of arguments
76 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
77 if (EFI_ERROR(Status
)) {
78 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
79 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, ProblemParam
);
80 FreePool(ProblemParam
);
81 ShellStatus
= SHELL_INVALID_PARAMETER
;
86 if (ShellCommandLineGetCount(Package
) < 1) {
87 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
88 ShellStatus
= SHELL_INVALID_PARAMETER
;
90 if (!ShellCommandLineGetFlag(Package
, L
"-nc")) {
97 // get a list with each file specified by parameters
98 // if parameter is a directory then add all the files below it to the list
100 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
102 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
104 Status
= ShellOpenFileMetaArg((CHAR16
*)Param
, EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
105 if (EFI_ERROR(Status
)) {
106 ShellStatus
= SHELL_ACCESS_DENIED
;
110 if (FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
111 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
);
112 } else if (ShellStatus
== SHELL_SUCCESS
) {
116 // loop through the list and make sure we are not aborting...
118 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
119 ; !IsNull(&FileList
->Link
, &Node
->Link
) && !ShellGetExecutionBreakFlag()
120 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
122 if (EFI_ERROR(Node
->Status
)){
123 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_OPEN_FAIL
), gShellDebug1HiiHandle
, Node
->FullName
);
124 ShellStatus
= SHELL_INVALID_PARAMETER
;
127 if (FileHandleIsDirectory(Node
->Handle
) == EFI_SUCCESS
) {
128 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_NOT_DIR
), gShellDebug1HiiHandle
, Node
->FullName
);
129 ShellStatus
= SHELL_INVALID_PARAMETER
;
132 SourceSize
= (UINTN
) Node
->Info
->FileSize
;
133 File1Buffer
= AllocatePool (SourceSize
);
134 ASSERT(File1Buffer
!= NULL
);
135 Status
= gEfiShellProtocol
->ReadFile(Node
->Handle
, &SourceSize
, File1Buffer
);
136 if (EFI_ERROR(Status
)) {
137 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_READ_FAIL
), gShellDebug1HiiHandle
, Node
->FullName
);
138 ShellStatus
= SHELL_INVALID_PARAMETER
;
140 Status
= LoadEfiDriversFromRomImage (
146 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_LOAD_PCI_ROM_RES
), gShellDebug1HiiHandle
, Node
->FullName
, Status
);
148 FreePool(File1Buffer
);
151 if (FileList
!= NULL
&& !IsListEmpty(&FileList
->Link
)) {
152 Status
= ShellCloseFileMetaArg(&FileList
);
157 Status
= LoadPciRomConnectAllDriversToAllControllers ();
162 return (ShellStatus
);
166 LoadEfiDriversFromRomImage (
169 CONST CHAR16
*FileName
180 FileName - The file name
183 EFI_SUCCESS - The command completed successfully
184 EFI_INVALID_PARAMETER - Command usage error
185 EFI_UNSUPPORTED - Protocols unsupported
186 EFI_OUT_OF_RESOURCES - Out of memory
187 Other value - Unknown error
191 EFI_PCI_EXPANSION_ROM_HEADER
*EfiRomHeader
;
192 PCI_DATA_STRUCTURE
*Pcir
;
197 EFI_HANDLE ImageHandle
;
199 EFI_STATUS retStatus
;
200 CHAR16 RomFileName
[280];
201 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
203 UINT32 DestinationSize
;
207 VOID
*DecompressedImageBuffer
;
209 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
212 retStatus
= EFI_NOT_FOUND
;
213 RomBarOffset
= (UINTN
) RomBar
;
217 EfiRomHeader
= (EFI_PCI_EXPANSION_ROM_HEADER
*) (UINTN
) RomBarOffset
;
219 if (EfiRomHeader
->Signature
!= 0xaa55) {
220 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_LOADPCIROM_CORRUPT
), gShellDebug1HiiHandle
, FileName
, ImageIndex
);
221 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_IMAGE_CORRUPT), HiiHandle, ImageIndex);
225 Pcir
= (PCI_DATA_STRUCTURE
*) (UINTN
) (RomBarOffset
+ EfiRomHeader
->PcirOffset
);
226 ImageSize
= Pcir
->ImageLength
* 512;
228 if ((Pcir
->CodeType
== PCI_CODE_TYPE_EFI_IMAGE
) &&
229 (EfiRomHeader
->EfiSignature
== EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
)
232 if ((EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
) ||
233 (EfiRomHeader
->EfiSubsystem
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
)
235 ImageOffset
= EfiRomHeader
->EfiImageHeaderOffset
;
236 ImageSize
= EfiRomHeader
->InitializationSize
* 512;
238 ImageBuffer
= (VOID
*) (UINTN
) (RomBarOffset
+ ImageOffset
);
239 ImageLength
= ImageSize
- ImageOffset
;
240 DecompressedImageBuffer
= NULL
;
243 // decompress here if needed
246 if (EfiRomHeader
->CompressionType
> EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
250 if (EfiRomHeader
->CompressionType
== EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
) {
251 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
252 ASSERT_EFI_ERROR(Status
);
253 if (EFI_ERROR (Status
)) {
257 Status
= Decompress
->GetInfo (
264 if (!EFI_ERROR (Status
)) {
265 DecompressedImageBuffer
= AllocatePool (DestinationSize
);
266 if (ImageBuffer
!= NULL
) {
267 Scratch
= AllocatePool (ScratchSize
);
268 if (Scratch
!= NULL
) {
269 Status
= Decompress
->Decompress (
273 DecompressedImageBuffer
,
278 if (!EFI_ERROR (Status
)) {
279 ImageBuffer
= DecompressedImageBuffer
;
280 ImageLength
= DestinationSize
;
293 // load image and start image
295 UnicodeSPrint (RomFileName
, sizeof (RomFileName
), L
"%s[%d]", FileName
, ImageIndex
);
296 FilePath
= FileDevicePath (NULL
, RomFileName
);
298 Status
= gBS
->LoadImage (
306 if (EFI_ERROR (Status
)) {
307 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_LOADPCIROM_LOAD_FAIL
), gShellDebug1HiiHandle
, FileName
, ImageIndex
, Status
);
308 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_LOAD_IMAGE_ERROR), HiiHandle, ImageIndex, Status);
310 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
311 if (EFI_ERROR (Status
)) {
312 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_LOADPCIROM_START_FAIL
), gShellDebug1HiiHandle
, FileName
, ImageIndex
, Status
);
313 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_START_IMAGE), HiiHandle, ImageIndex, Status);
320 if (DecompressedImageBuffer
!= NULL
) {
321 FreePool (DecompressedImageBuffer
);
327 RomBarOffset
= RomBarOffset
+ ImageSize
;
329 } while (((Pcir
->Indicator
& 0x80) == 0x00) && ((RomBarOffset
- (UINTN
) RomBar
) < RomSize
));
335 LoadPciRomConnectAllDriversToAllControllers (
341 UINTN AllHandleCount
;
342 EFI_HANDLE
*AllHandleBuffer
;
345 EFI_HANDLE
*HandleBuffer
;
351 Status
= gBS
->LocateHandleBuffer(
358 if (EFI_ERROR (Status
)) {
362 for (Index
= 0; Index
< AllHandleCount
; Index
++) {
363 if (ShellGetExecutionBreakFlag ()) {
364 Status
= EFI_ABORTED
;
368 // Scan the handle database
370 Status
= ParseHandleDatabaseByRelationshipWithType(
372 AllHandleBuffer
[Index
],
378 Status = LibScanHandleDatabase (
381 AllHandleBuffer[Index],
388 if (EFI_ERROR (Status
)) {
393 if ((HandleType
[Index
] & HR_DRIVER_BINDING_HANDLE
) != 0) {
397 if ((HandleType
[Index
] & HR_IMAGE_HANDLE
) != 0) {
403 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
404 if ((HandleType
[HandleIndex
] & HR_PARENT_HANDLE
) != 0) {
410 if ((HandleType
[Index
] & HR_DEVICE_HANDLE
) != 0) {
411 Status
= gBS
->ConnectController (
412 AllHandleBuffer
[Index
],
421 FreePool (HandleBuffer
);
422 FreePool (HandleType
);
426 FreePool (AllHandleBuffer
);