]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c
ShellPkg/LoadPciRom: Handle memory allocation failure
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / LoadPciRom.c
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 - 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "UefiShellDebug1CommandsLib.h"
17 #include <IndustryStandard/Pci22.h>
18 #include <IndustryStandard/Pci23.h>
19 #include <IndustryStandard/PeImage.h>
20 #include <Protocol/Decompress.h>
21
22 /**
23 Connects all available drives and controllers.
24
25 @retval EFI_SUCCESS The operation was successful.
26 @retval EFI_ABORTED The abort mechanism was received.
27 **/
28 EFI_STATUS
29 EFIAPI
30 LoadPciRomConnectAllDriversToAllControllers (
31 VOID
32 );
33
34 /**
35 Command entry point.
36
37 @param[in] RomBar The Rom Base address.
38 @param[in] RomSize The Rom size.
39 @param[in] FileName The file name.
40
41 @retval EFI_SUCCESS The command completed successfully.
42 @retval EFI_INVALID_PARAMETER Command usage error.
43 @retval EFI_UNSUPPORTED Protocols unsupported.
44 @retval EFI_OUT_OF_RESOURCES Out of memory.
45 @retval Other value Unknown error.
46 **/
47 EFI_STATUS
48 EFIAPI
49 LoadEfiDriversFromRomImage (
50 VOID *RomBar,
51 UINTN RomSize,
52 CONST CHAR16 *FileName
53 );
54
55 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
56 {L"-nc", TypeFlag},
57 {NULL, TypeMax}
58 };
59
60 /**
61 Function for 'loadpcirom' command.
62
63 @param[in] ImageHandle Handle to the Image (NULL if Internal).
64 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
65 **/
66 SHELL_STATUS
67 EFIAPI
68 ShellCommandRunLoadPciRom (
69 IN EFI_HANDLE ImageHandle,
70 IN EFI_SYSTEM_TABLE *SystemTable
71 )
72 {
73 EFI_SHELL_FILE_INFO *FileList;
74 UINTN SourceSize;
75 UINT8 *File1Buffer;
76 EFI_STATUS Status;
77 LIST_ENTRY *Package;
78 CHAR16 *ProblemParam;
79 SHELL_STATUS ShellStatus;
80 BOOLEAN Connect;
81 CONST CHAR16 *Param;
82 UINTN ParamCount;
83 EFI_SHELL_FILE_INFO *Node;
84 //
85 // Local variable initializations
86 //
87 File1Buffer = NULL;
88 ShellStatus = SHELL_SUCCESS;
89 FileList = NULL;
90
91
92 //
93 // verify number of arguments
94 //
95 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
96 if (EFI_ERROR(Status)) {
97 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
98 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"loadpcirom", ProblemParam);
99 FreePool(ProblemParam);
100 ShellStatus = SHELL_INVALID_PARAMETER;
101 } else {
102 ASSERT(FALSE);
103 }
104 } else {
105 if (ShellCommandLineGetCount(Package) < 2) {
106 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"loadpcirom");
107 ShellStatus = SHELL_INVALID_PARAMETER;
108 } else {
109 if (ShellCommandLineGetFlag(Package, L"-nc")) {
110 Connect = FALSE;
111 } else {
112 Connect = TRUE;
113 }
114
115 //
116 // get a list with each file specified by parameters
117 // if parameter is a directory then add all the files below it to the list
118 //
119 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)
120 ; Param != NULL
121 ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)
122 ){
123 Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);
124 if (EFI_ERROR(Status)) {
125 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"loadpcirom", Param);
126 ShellStatus = SHELL_ACCESS_DENIED;
127 break;
128 }
129 }
130 if (ShellStatus == SHELL_SUCCESS && FileList != NULL) {
131 //
132 // loop through the list and make sure we are not aborting...
133 //
134 for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)
135 ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()
136 ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)
137 ){
138 if (EFI_ERROR(Node->Status)){
139 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"loadpcirom", Node->FullName);
140 ShellStatus = SHELL_INVALID_PARAMETER;
141 continue;
142 }
143 if (FileHandleIsDirectory(Node->Handle) == EFI_SUCCESS) {
144 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_NOT_DIR), gShellDebug1HiiHandle, L"loadpcirom", Node->FullName);
145 ShellStatus = SHELL_INVALID_PARAMETER;
146 continue;
147 }
148 SourceSize = (UINTN) Node->Info->FileSize;
149 File1Buffer = AllocateZeroPool (SourceSize);
150 if (File1Buffer == NULL) {
151 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"loadpcirom");
152 ShellStatus = SHELL_OUT_OF_RESOURCES;
153 continue;
154 }
155 Status = gEfiShellProtocol->ReadFile(Node->Handle, &SourceSize, File1Buffer);
156 if (EFI_ERROR(Status)) {
157 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_READ_FAIL), gShellDebug1HiiHandle, L"loadpcirom", Node->FullName);
158 ShellStatus = SHELL_INVALID_PARAMETER;
159 } else {
160 Status = LoadEfiDriversFromRomImage (
161 File1Buffer,
162 SourceSize,
163 Node->FullName
164 );
165
166 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOAD_PCI_ROM_RES), gShellDebug1HiiHandle, Node->FullName, Status);
167 }
168 FreePool(File1Buffer);
169 }
170 } else if (ShellStatus == SHELL_SUCCESS) {
171 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_NOT_SPEC), gShellDebug1HiiHandle, "loadpcirom");
172 ShellStatus = SHELL_NOT_FOUND;
173 }
174 if (FileList != NULL && !IsListEmpty(&FileList->Link)) {
175 Status = ShellCloseFileMetaArg(&FileList);
176 }
177 FileList = NULL;
178
179 if (Connect) {
180 Status = LoadPciRomConnectAllDriversToAllControllers ();
181 }
182 }
183 }
184
185 return (ShellStatus);
186 }
187
188 /**
189 Command entry point.
190
191 @param[in] RomBar The Rom Base address.
192 @param[in] RomSize The Rom size.
193 @param[in] FileName The file name.
194
195 @retval EFI_SUCCESS The command completed successfully.
196 @retval EFI_INVALID_PARAMETER Command usage error.
197 @retval EFI_UNSUPPORTED Protocols unsupported.
198 @retval EFI_OUT_OF_RESOURCES Out of memory.
199 @retval Other value Unknown error.
200 **/
201 EFI_STATUS
202 EFIAPI
203 LoadEfiDriversFromRomImage (
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
237 EfiRomHeader = (EFI_PCI_EXPANSION_ROM_HEADER *) (UINTN) RomBarOffset;
238
239 if (EfiRomHeader->Signature != PCI_EXPANSION_ROM_HEADER_SIGNATURE) {
240 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_CORRUPT), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex);
241 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_IMAGE_CORRUPT), HiiHandle, ImageIndex);
242 return ReturnStatus;
243 }
244
245 //
246 // If the pointer to the PCI Data Structure is invalid, no further images can be located.
247 // The PCI Data Structure must be DWORD aligned.
248 //
249 if (EfiRomHeader->PcirOffset == 0 ||
250 (EfiRomHeader->PcirOffset & 3) != 0 ||
251 RomBarOffset - (UINTN)RomBar + EfiRomHeader->PcirOffset + sizeof (PCI_DATA_STRUCTURE) > RomSize) {
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 ImageSize = Pcir->ImageLength * 512;
263 if (RomBarOffset - (UINTN)RomBar + ImageSize > RomSize) {
264 break;
265 }
266
267 if ((Pcir->CodeType == PCI_CODE_TYPE_EFI_IMAGE) &&
268 (EfiRomHeader->EfiSignature == EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE) &&
269 ((EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER) ||
270 (EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER))) {
271
272 ImageOffset = EfiRomHeader->EfiImageHeaderOffset;
273 InitializationSize = EfiRomHeader->InitializationSize * 512;
274
275 if (InitializationSize <= ImageSize && ImageOffset < InitializationSize) {
276
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 (ImageBuffer != NULL) {
306 Scratch = AllocateZeroPool (ScratchSize);
307 if (Scratch != NULL) {
308 Status = Decompress->Decompress (
309 Decompress,
310 ImageBuffer,
311 ImageLength,
312 DecompressedImageBuffer,
313 DestinationSize,
314 Scratch,
315 ScratchSize
316 );
317 if (!EFI_ERROR (Status)) {
318 ImageBuffer = DecompressedImageBuffer;
319 ImageLength = DestinationSize;
320 SkipImage = FALSE;
321 }
322
323 FreePool (Scratch);
324 }
325 }
326 }
327 }
328 }
329
330 if (!SkipImage) {
331 //
332 // load image and start image
333 //
334 UnicodeSPrint (RomFileName, sizeof (RomFileName), L"%s[%d]", FileName, ImageIndex);
335 FilePath = FileDevicePath (NULL, RomFileName);
336
337 Status = gBS->LoadImage (
338 TRUE,
339 gImageHandle,
340 FilePath,
341 ImageBuffer,
342 ImageLength,
343 &ImageHandle
344 );
345 if (EFI_ERROR (Status)) {
346 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_LOAD_FAIL), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex);
347 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_LOAD_IMAGE_ERROR), HiiHandle, ImageIndex, Status);
348 } else {
349 Status = gBS->StartImage (ImageHandle, NULL, NULL);
350 if (EFI_ERROR (Status)) {
351 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_START_FAIL), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex);
352 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_START_IMAGE), HiiHandle, ImageIndex, Status);
353 } else {
354 ReturnStatus = Status;
355 }
356 }
357 }
358
359 if (DecompressedImageBuffer != NULL) {
360 FreePool (DecompressedImageBuffer);
361 }
362
363 }
364 }
365
366 RomBarOffset = RomBarOffset + ImageSize;
367 ImageIndex++;
368 } while (((Pcir->Indicator & 0x80) == 0x00) && ((RomBarOffset - (UINTN) RomBar) < RomSize));
369
370 return ReturnStatus;
371 }
372
373 /**
374 Connects all available drives and controllers.
375
376 @retval EFI_SUCCESS The operation was successful.
377 @retval EFI_ABORTED The abort mechanism was received.
378 **/
379 EFI_STATUS
380 EFIAPI
381 LoadPciRomConnectAllDriversToAllControllers (
382 VOID
383 )
384
385 {
386 EFI_STATUS Status;
387 UINTN AllHandleCount;
388 EFI_HANDLE *AllHandleBuffer;
389 UINTN Index;
390 UINTN HandleCount;
391 EFI_HANDLE *HandleBuffer;
392 UINTN *HandleType;
393 UINTN HandleIndex;
394 BOOLEAN Parent;
395 BOOLEAN Device;
396
397 Status = gBS->LocateHandleBuffer(
398 AllHandles,
399 NULL,
400 NULL,
401 &AllHandleCount,
402 &AllHandleBuffer
403 );
404 if (EFI_ERROR (Status)) {
405 return Status;
406 }
407
408 for (Index = 0; Index < AllHandleCount; Index++) {
409 if (ShellGetExecutionBreakFlag ()) {
410 Status = EFI_ABORTED;
411 goto Done;
412 }
413 //
414 // Scan the handle database
415 //
416 Status = ParseHandleDatabaseByRelationshipWithType(
417 NULL,
418 AllHandleBuffer[Index],
419 &HandleCount,
420 &HandleBuffer,
421 &HandleType
422 );
423 /*
424 Status = LibScanHandleDatabase (
425 NULL,
426 NULL,
427 AllHandleBuffer[Index],
428 NULL,
429 &HandleCount,
430 &HandleBuffer,
431 &HandleType
432 );
433 */
434 if (EFI_ERROR (Status)) {
435 goto Done;
436 }
437
438 Device = TRUE;
439 if ((HandleType[Index] & HR_DRIVER_BINDING_HANDLE) != 0) {
440 Device = FALSE;
441 }
442
443 if ((HandleType[Index] & HR_IMAGE_HANDLE) != 0) {
444 Device = FALSE;
445 }
446
447 if (Device) {
448 Parent = FALSE;
449 for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
450 if ((HandleType[HandleIndex] & HR_PARENT_HANDLE) != 0) {
451 Parent = TRUE;
452 }
453 }
454
455 if (!Parent) {
456 if ((HandleType[Index] & HR_DEVICE_HANDLE) != 0) {
457 Status = gBS->ConnectController (
458 AllHandleBuffer[Index],
459 NULL,
460 NULL,
461 TRUE
462 );
463 }
464 }
465 }
466
467 FreePool (HandleBuffer);
468 FreePool (HandleType);
469 }
470
471 Done:
472 FreePool (AllHandleBuffer);
473 return Status;
474 }