TianoCore EDK2 master
Loading...
Searching...
No Matches
LoadPciRom.c
Go to the documentation of this file.
1
14#include <Protocol/Decompress.h>
15
24 VOID
25 );
26
42 VOID *RomBar,
43 UINTN RomSize,
44 CONST CHAR16 *FileName
45 );
46
47STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
48 { L"-nc", TypeFlag },
49 { NULL, TypeMax }
50};
51
59EFIAPI
61 IN EFI_HANDLE ImageHandle,
62 IN EFI_SYSTEM_TABLE *SystemTable
63 )
64{
65 EFI_SHELL_FILE_INFO *FileList;
66 UINTN SourceSize;
67 UINT8 *File1Buffer;
68 EFI_STATUS Status;
69 LIST_ENTRY *Package;
70 CHAR16 *ProblemParam;
71 SHELL_STATUS ShellStatus;
72 BOOLEAN Connect;
73 CONST CHAR16 *Param;
74 UINTN ParamCount;
76
77 //
78 // Local variable initializations
79 //
80 File1Buffer = NULL;
81 ShellStatus = SHELL_SUCCESS;
82 FileList = NULL;
83
84 //
85 // verify number of arguments
86 //
87 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
88 if (EFI_ERROR (Status)) {
89 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
90 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"loadpcirom", ProblemParam);
91 FreePool (ProblemParam);
92 ShellStatus = SHELL_INVALID_PARAMETER;
93 } else {
94 ASSERT (FALSE);
95 }
96 } else {
97 if (ShellCommandLineGetCount (Package) < 2) {
98 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"loadpcirom");
99 ShellStatus = SHELL_INVALID_PARAMETER;
100 } else {
101 if (ShellCommandLineGetFlag (Package, L"-nc")) {
102 Connect = FALSE;
103 } else {
104 Connect = TRUE;
105 }
106
107 //
108 // get a list with each file specified by parameters
109 // if parameter is a directory then add all the files below it to the list
110 //
111 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue (Package, ParamCount)
112 ; Param != NULL
113 ; ParamCount++, Param = ShellCommandLineGetRawValue (Package, ParamCount)
114 )
115 {
116 Status = ShellOpenFileMetaArg ((CHAR16 *)Param, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);
117 if (EFI_ERROR (Status)) {
118 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"loadpcirom", Param);
119 ShellStatus = SHELL_ACCESS_DENIED;
120 break;
121 }
122 }
123
124 if ((ShellStatus == SHELL_SUCCESS) && (FileList != NULL)) {
125 //
126 // loop through the list and make sure we are not aborting...
127 //
128 for ( Node = (EFI_SHELL_FILE_INFO *)GetFirstNode (&FileList->Link)
129 ; !IsNull (&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag ()
130 ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode (&FileList->Link, &Node->Link)
131 )
132 {
133 if (EFI_ERROR (Node->Status)) {
134 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"loadpcirom", Node->FullName);
135 ShellStatus = SHELL_INVALID_PARAMETER;
136 continue;
137 }
138
140 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_NOT_DIR), gShellDebug1HiiHandle, L"loadpcirom", Node->FullName);
141 ShellStatus = SHELL_INVALID_PARAMETER;
142 continue;
143 }
144
145 SourceSize = (UINTN)Node->Info->FileSize;
146 File1Buffer = AllocateZeroPool (SourceSize);
147 if (File1Buffer == NULL) {
148 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"loadpcirom");
149 ShellStatus = SHELL_OUT_OF_RESOURCES;
150 continue;
151 }
152
153 Status = gEfiShellProtocol->ReadFile (Node->Handle, &SourceSize, File1Buffer);
154 if (EFI_ERROR (Status)) {
155 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_READ_FAIL), gShellDebug1HiiHandle, L"loadpcirom", Node->FullName);
156 ShellStatus = SHELL_INVALID_PARAMETER;
157 } else {
159 File1Buffer,
160 SourceSize,
161 Node->FullName
162 );
163
164 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_LOAD_PCI_ROM_RES), gShellDebug1HiiHandle, Node->FullName, Status);
165 }
166
167 FreePool (File1Buffer);
168 }
169 } else if (ShellStatus == SHELL_SUCCESS) {
170 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_NOT_SPEC), gShellDebug1HiiHandle, "loadpcirom");
171 ShellStatus = SHELL_NOT_FOUND;
172 }
173
174 if ((FileList != NULL) && !IsListEmpty (&FileList->Link)) {
175 Status = ShellCloseFileMetaArg (&FileList);
176 }
177
178 FileList = NULL;
179
180 if (Connect) {
182 }
183 }
184 }
185
186 return (ShellStatus);
187}
188
204 VOID *RomBar,
205 UINTN RomSize,
206 CONST CHAR16 *FileName
207 )
208
209{
210 EFI_PCI_EXPANSION_ROM_HEADER *EfiRomHeader;
211 PCI_DATA_STRUCTURE *Pcir;
212 UINTN ImageIndex;
213 UINTN RomBarOffset;
214 UINT32 ImageSize;
215 UINT16 ImageOffset;
216 EFI_HANDLE ImageHandle;
217 EFI_STATUS Status;
218 EFI_STATUS ReturnStatus;
219 CHAR16 RomFileName[280];
220 EFI_DEVICE_PATH_PROTOCOL *FilePath;
221 BOOLEAN SkipImage;
222 UINT32 DestinationSize;
223 UINT32 ScratchSize;
224 UINT8 *Scratch;
225 VOID *ImageBuffer;
226 VOID *DecompressedImageBuffer;
227 UINT32 ImageLength;
229 UINT32 InitializationSize;
230
231 ImageIndex = 0;
232 ReturnStatus = EFI_NOT_FOUND;
233 RomBarOffset = (UINTN)RomBar;
234
235 do {
236 EfiRomHeader = (EFI_PCI_EXPANSION_ROM_HEADER *)(UINTN)RomBarOffset;
237
238 if (EfiRomHeader->Signature != PCI_EXPANSION_ROM_HEADER_SIGNATURE) {
239 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_CORRUPT), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex);
240 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_IMAGE_CORRUPT), HiiHandle, ImageIndex);
241 return ReturnStatus;
242 }
243
244 //
245 // If the pointer to the PCI Data Structure is invalid, no further images can be located.
246 // The PCI Data Structure must be DWORD aligned.
247 //
248 if ((EfiRomHeader->PcirOffset == 0) ||
249 ((EfiRomHeader->PcirOffset & 3) != 0) ||
250 (RomBarOffset - (UINTN)RomBar + EfiRomHeader->PcirOffset + sizeof (PCI_DATA_STRUCTURE) > RomSize))
251 {
252 break;
253 }
254
255 Pcir = (PCI_DATA_STRUCTURE *)(UINTN)(RomBarOffset + EfiRomHeader->PcirOffset);
256 //
257 // If a valid signature is not present in the PCI Data Structure, no further images can be located.
258 //
259 if (Pcir->Signature != PCI_DATA_STRUCTURE_SIGNATURE) {
260 break;
261 }
262
263 ImageSize = Pcir->ImageLength * 512;
264 if (RomBarOffset - (UINTN)RomBar + ImageSize > RomSize) {
265 break;
266 }
267
268 if ((Pcir->CodeType == PCI_CODE_TYPE_EFI_IMAGE) &&
270 ((EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER) ||
271 (EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER)))
272 {
273 ImageOffset = EfiRomHeader->EfiImageHeaderOffset;
274 InitializationSize = EfiRomHeader->InitializationSize * 512;
275
276 if ((InitializationSize <= ImageSize) && (ImageOffset < InitializationSize)) {
277 ImageBuffer = (VOID *)(UINTN)(RomBarOffset + ImageOffset);
278 ImageLength = InitializationSize - ImageOffset;
279 DecompressedImageBuffer = NULL;
280
281 //
282 // decompress here if needed
283 //
284 SkipImage = FALSE;
285 if (EfiRomHeader->CompressionType > EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {
286 SkipImage = TRUE;
287 }
288
289 if (EfiRomHeader->CompressionType == EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {
290 Status = gBS->LocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID **)&Decompress);
291 ASSERT_EFI_ERROR (Status);
292 if (EFI_ERROR (Status)) {
293 SkipImage = TRUE;
294 } else {
295 SkipImage = TRUE;
296 Status = Decompress->GetInfo (
298 ImageBuffer,
299 ImageLength,
300 &DestinationSize,
301 &ScratchSize
302 );
303 if (!EFI_ERROR (Status)) {
304 DecompressedImageBuffer = AllocateZeroPool (DestinationSize);
305 if (DecompressedImageBuffer == NULL) {
306 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"loadpcirom");
307 return EFI_OUT_OF_RESOURCES;
308 }
309
310 if (ImageBuffer != NULL) {
311 Scratch = AllocateZeroPool (ScratchSize);
312 if (Scratch != NULL) {
313 Status = Decompress->Decompress (
315 ImageBuffer,
316 ImageLength,
317 DecompressedImageBuffer,
318 DestinationSize,
319 Scratch,
320 ScratchSize
321 );
322 if (!EFI_ERROR (Status)) {
323 ImageBuffer = DecompressedImageBuffer;
324 ImageLength = DestinationSize;
325 SkipImage = FALSE;
326 }
327
328 FreePool (Scratch);
329 }
330 }
331 }
332 }
333 }
334
335 if (!SkipImage) {
336 //
337 // load image and start image
338 //
339 UnicodeSPrint (RomFileName, sizeof (RomFileName), L"%s[%d]", FileName, ImageIndex);
340 FilePath = FileDevicePath (NULL, RomFileName);
341 if (FilePath == NULL) {
342 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_LOAD_FAIL), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex);
343 SHELL_FREE_NON_NULL (DecompressedImageBuffer);
344 return EFI_OUT_OF_RESOURCES;
345 }
346
347 Status = gBS->LoadImage (
348 TRUE,
350 FilePath,
351 ImageBuffer,
352 ImageLength,
353 &ImageHandle
354 );
355 if (EFI_ERROR (Status)) {
356 //
357 // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
358 // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
359 // If the caller doesn't have the option to defer the execution of an image, we should
360 // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
361 //
362 if (Status == EFI_SECURITY_VIOLATION) {
363 gBS->UnloadImage (ImageHandle);
364 }
365
366 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_LOAD_FAIL), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex);
367 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_LOAD_IMAGE_ERROR), HiiHandle, ImageIndex, Status);
368 } else {
369 Status = gBS->StartImage (ImageHandle, NULL, NULL);
370 if (EFI_ERROR (Status)) {
371 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_START_FAIL), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex);
372 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_START_IMAGE), HiiHandle, ImageIndex, Status);
373 } else {
374 ReturnStatus = Status;
375 }
376 }
377 }
378
379 if (DecompressedImageBuffer != NULL) {
380 FreePool (DecompressedImageBuffer);
381 }
382 }
383 }
384
385 RomBarOffset = RomBarOffset + ImageSize;
386 ImageIndex++;
387 } while (((Pcir->Indicator & 0x80) == 0x00) && ((RomBarOffset - (UINTN)RomBar) < RomSize));
388
389 return ReturnStatus;
390}
391
400 VOID
401 )
402{
403 EFI_STATUS Status;
404 UINTN HandleCount;
405 EFI_HANDLE *HandleBuffer;
406 UINTN Index;
407
408 Status = gBS->LocateHandleBuffer (
410 NULL,
411 NULL,
412 &HandleCount,
413 &HandleBuffer
414 );
415 if (EFI_ERROR (Status)) {
416 return Status;
417 }
418
419 for (Index = 0; Index < HandleCount; Index++) {
421 Status = EFI_ABORTED;
422 break;
423 }
424
425 gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);
426 }
427
428 if (HandleBuffer != NULL) {
429 FreePool (HandleBuffer);
430 }
431
432 return Status;
433}
UINT64 UINTN
STATIC VOID EFIAPI Connect(IN EFI_HANDLE Handle, IN CONST CHAR16 *ReportText)
Definition: PlatformBm.c:317
BOOLEAN EFIAPI IsNull(IN CONST LIST_ENTRY *List, IN CONST LIST_ENTRY *Node)
Definition: LinkedList.c:443
BOOLEAN EFIAPI IsListEmpty(IN CONST LIST_ENTRY *ListHead)
Definition: LinkedList.c:403
LIST_ENTRY *EFIAPI GetNextNode(IN CONST LIST_ENTRY *List, IN CONST LIST_ENTRY *Node)
Definition: LinkedList.c:333
LIST_ENTRY *EFIAPI GetFirstNode(IN CONST LIST_ENTRY *List)
Definition: LinkedList.c:298
EFI_DEVICE_PATH_PROTOCOL *EFIAPI FileDevicePath(IN EFI_HANDLE Device OPTIONAL, IN CONST CHAR16 *FileName)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
EFI_STATUS EFIAPI FileHandleIsDirectory(IN EFI_FILE_HANDLE DirHandle)
EFI_STATUS LoadEfiDriversFromRomImage(VOID *RomBar, UINTN RomSize, CONST CHAR16 *FileName)
Definition: LoadPciRom.c:203
SHELL_STATUS EFIAPI ShellCommandRunLoadPciRom(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: LoadPciRom.c:60
EFI_STATUS LoadPciRomConnectAllDriversToAllControllers(VOID)
Definition: LoadPciRom.c:399
EFI_STATUS EFIAPI Decompress(IN CONST EFI_PEI_DECOMPRESS_PPI *This, IN CONST EFI_COMPRESSION_SECTION *CompressionSection, OUT VOID **OutputBuffer, OUT UINTN *OutputSize)
Definition: DxeLoad.c:674
UINTN EFIAPI UnicodeSPrint(OUT CHAR16 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR16 *FormatString,...)
Definition: PrintLib.c:408
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
SHELL_STATUS
Definition: Shell.h:21
@ SHELL_OUT_OF_RESOURCES
Definition: Shell.h:73
@ SHELL_ACCESS_DENIED
Definition: Shell.h:106
@ SHELL_SUCCESS
Definition: Shell.h:25
@ SHELL_NOT_FOUND
Definition: Shell.h:101
@ SHELL_INVALID_PARAMETER
Definition: Shell.h:35
#define EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE
defined in UEFI Spec.
Definition: Pci22.h:805
#define EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED
defined in UEFI spec.
Definition: Pci22.h:810
BOOLEAN EFIAPI ShellGetExecutionBreakFlag(VOID)
EFI_STATUS EFIAPI ShellCloseFileMetaArg(IN OUT EFI_SHELL_FILE_INFO **ListHead)
#define ShellCommandLineParse(CheckList, CheckPackage, ProblemParam, AutoPageBreak)
Make it easy to upgrade from older versions of the shell library.
Definition: ShellLib.h:755
EFI_STATUS EFIAPI ShellPrintHiiEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR8 *Language OPTIONAL, IN CONST EFI_STRING_ID HiiFormatStringId, IN CONST EFI_HII_HANDLE HiiFormatHandle,...)
BOOLEAN EFIAPI ShellCommandLineGetFlag(IN CONST LIST_ENTRY *CONST CheckPackage, IN CONST CHAR16 *CONST KeyString)
@ TypeFlag
A flag that is present or not present only (IE "-a").
Definition: ShellLib.h:699
EFI_STATUS EFIAPI ShellOpenFileMetaArg(IN CHAR16 *Arg, IN UINT64 OpenMode, IN OUT EFI_SHELL_FILE_INFO **ListHead)
CONST CHAR16 *EFIAPI ShellCommandLineGetRawValue(IN CONST LIST_ENTRY *CONST CheckPackage, IN UINTN Position)
UINTN EFIAPI ShellCommandLineGetCount(IN CONST LIST_ENTRY *CheckPackage)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_HANDLE gImageHandle
EFI_BOOT_SERVICES * gBS
#define STRING_TOKEN(t)
@ AllHandles
Definition: UefiSpec.h:1509
UINT64 FileSize
Definition: FileInfo.h:27
UINT16 Signature
0xaa55
Definition: Pci22.h:858
UINT32 EfiSignature
0x0EF1
Definition: Pci22.h:860
LIST_ENTRY Link
Linked list members.
Definition: Shell.h:153
SHELL_FILE_HANDLE Handle
Handle for interacting with the opened file or NULL if closed.
Definition: Shell.h:157
EFI_FILE_INFO * Info
Pointer to the FileInfo struct for this file or NULL.
Definition: Shell.h:158
EFI_STATUS Status
Status of opening the file. Valid only if Handle != NULL.
Definition: Shell.h:154
CONST CHAR16 * FullName
Fully qualified filename.
Definition: Shell.h:155
UINT32 Signature
"PCIR"
Definition: Pci22.h:839