VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c

Last change on this file was 108794, checked in by vboxsync, 4 weeks ago

Devices/EFI/FirmwareNew: Merge edk2-stable202502 from the vendor branch and make it build for the important platforms, bugref:4643

  • Property svn:eol-style set to native
File size: 14.9 KB
Line 
1/** @file
2 Main file for LoadPciRom shell Debug1 function.
3
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2005 - 2019, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8**/
9
10#include "UefiShellDebug1CommandsLib.h"
11#include <IndustryStandard/Pci22.h>
12#include <IndustryStandard/Pci23.h>
13#include <IndustryStandard/PeImage.h>
14#include <Protocol/Decompress.h>
15
16/**
17 Connects all available drives and controllers.
18
19 @retval EFI_SUCCESS The operation was successful.
20 @retval EFI_ABORTED The abort mechanism was received.
21**/
22EFI_STATUS
23LoadPciRomConnectAllDriversToAllControllers (
24 VOID
25 );
26
27/**
28 Command entry point.
29
30 @param[in] RomBar The Rom Base address.
31 @param[in] RomSize The Rom size.
32 @param[in] FileName The file name.
33
34 @retval EFI_SUCCESS The command completed successfully.
35 @retval EFI_INVALID_PARAMETER Command usage error.
36 @retval EFI_UNSUPPORTED Protocols unsupported.
37 @retval EFI_OUT_OF_RESOURCES Out of memory.
38 @retval Other value Unknown error.
39**/
40EFI_STATUS
41LoadEfiDriversFromRomImage (
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
52/**
53 Function for 'loadpcirom' command.
54
55 @param[in] ImageHandle Handle to the Image (NULL if Internal).
56 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
57**/
58SHELL_STATUS
59EFIAPI
60ShellCommandRunLoadPciRom (
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;
75 EFI_SHELL_FILE_INFO *Node;
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
139 if (FileHandleIsDirectory (Node->Handle) == EFI_SUCCESS) {
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 {
158 Status = LoadEfiDriversFromRomImage (
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) {
181 Status = LoadPciRomConnectAllDriversToAllControllers ();
182 }
183 }
184 }
185
186 return (ShellStatus);
187}
188
189/**
190 Command entry point.
191
192 @param[in] RomBar The Rom Base address.
193 @param[in] RomSize The Rom size.
194 @param[in] FileName The file name.
195
196 @retval EFI_SUCCESS The command completed successfully.
197 @retval EFI_INVALID_PARAMETER Command usage error.
198 @retval EFI_UNSUPPORTED Protocols unsupported.
199 @retval EFI_OUT_OF_RESOURCES Out of memory.
200 @retval Other value Unknown error.
201**/
202EFI_STATUS
203LoadEfiDriversFromRomImage (
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;
228 EFI_DECOMPRESS_PROTOCOL *Decompress;
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) &&
269 (EfiRomHeader->EfiSignature == EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE) &&
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 (
297 Decompress,
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 (
314 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,
349 gImageHandle,
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
392/**
393 Connects all available drives and controllers.
394
395 @retval EFI_SUCCESS The operation was successful.
396 @retval EFI_ABORTED The abort mechanism was received.
397**/
398EFI_STATUS
399LoadPciRomConnectAllDriversToAllControllers (
400 VOID
401 )
402{
403 EFI_STATUS Status;
404 UINTN HandleCount;
405 EFI_HANDLE *HandleBuffer;
406 UINTN Index;
407
408 Status = gBS->LocateHandleBuffer (
409 AllHandles,
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++) {
420 if (ShellGetExecutionBreakFlag ()) {
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}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette