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 - 2012, 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
ShellCommandRunLoadPciRom(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)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 ASSERT(File1Buffer != NULL);
151 Status = gEfiShellProtocol->ReadFile(Node->Handle, &SourceSize, File1Buffer);
152 if (EFI_ERROR(Status)) {
153 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_READ_FAIL), gShellDebug1HiiHandle, L"loadpcirom", Node->FullName);
154 ShellStatus = SHELL_INVALID_PARAMETER;
155 } else {
156 Status = LoadEfiDriversFromRomImage (
157 File1Buffer,
158 SourceSize,
159 Node->FullName
160 );
161
162 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOAD_PCI_ROM_RES), gShellDebug1HiiHandle, Node->FullName, Status);
163 }
164 FreePool(File1Buffer);
165 }
166 } else if (ShellStatus == SHELL_SUCCESS) {
167 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_NOT_SPEC), gShellDebug1HiiHandle, "loadpcirom");
168 ShellStatus = SHELL_NOT_FOUND;
169 }
170 if (FileList != NULL && !IsListEmpty(&FileList->Link)) {
171 Status = ShellCloseFileMetaArg(&FileList);
172 }
173 FileList = NULL;
174
175 if (Connect) {
176 Status = LoadPciRomConnectAllDriversToAllControllers ();
177 }
178 }
179 }
180
181 return (ShellStatus);
182 }
183
184 /**
185 Command entry point.
186
187 @param[in] RomBar The Rom Base address.
188 @param[in] RomSize The Rom size.
189 @param[in] FileName The file name.
190
191 @retval EFI_SUCCESS The command completed successfully.
192 @retval EFI_INVALID_PARAMETER Command usage error.
193 @retval EFI_UNSUPPORTED Protocols unsupported.
194 @retval EFI_OUT_OF_RESOURCES Out of memory.
195 @retval Other value Unknown error.
196 **/
197 EFI_STATUS
198 EFIAPI
LoadEfiDriversFromRomImage(VOID * RomBar,UINTN RomSize,CONST CHAR16 * FileName)199 LoadEfiDriversFromRomImage (
200 VOID *RomBar,
201 UINTN RomSize,
202 CONST CHAR16 *FileName
203 )
204
205 {
206 EFI_PCI_EXPANSION_ROM_HEADER *EfiRomHeader;
207 PCI_DATA_STRUCTURE *Pcir;
208 UINTN ImageIndex;
209 UINTN RomBarOffset;
210 UINT32 ImageSize;
211 UINT16 ImageOffset;
212 EFI_HANDLE ImageHandle;
213 EFI_STATUS Status;
214 EFI_STATUS ReturnStatus;
215 CHAR16 RomFileName[280];
216 EFI_DEVICE_PATH_PROTOCOL *FilePath;
217 BOOLEAN SkipImage;
218 UINT32 DestinationSize;
219 UINT32 ScratchSize;
220 UINT8 *Scratch;
221 VOID *ImageBuffer;
222 VOID *DecompressedImageBuffer;
223 UINT32 ImageLength;
224 EFI_DECOMPRESS_PROTOCOL *Decompress;
225 UINT32 InitializationSize;
226
227 ImageIndex = 0;
228 ReturnStatus = EFI_NOT_FOUND;
229 RomBarOffset = (UINTN) RomBar;
230
231 do {
232
233 EfiRomHeader = (EFI_PCI_EXPANSION_ROM_HEADER *) (UINTN) RomBarOffset;
234
235 if (EfiRomHeader->Signature != PCI_EXPANSION_ROM_HEADER_SIGNATURE) {
236 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_CORRUPT), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex);
237 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_IMAGE_CORRUPT), HiiHandle, ImageIndex);
238 return ReturnStatus;
239 }
240
241 //
242 // If the pointer to the PCI Data Structure is invalid, no further images can be located.
243 // The PCI Data Structure must be DWORD aligned.
244 //
245 if (EfiRomHeader->PcirOffset == 0 ||
246 (EfiRomHeader->PcirOffset & 3) != 0 ||
247 RomBarOffset - (UINTN)RomBar + EfiRomHeader->PcirOffset + sizeof (PCI_DATA_STRUCTURE) > RomSize) {
248 break;
249 }
250
251 Pcir = (PCI_DATA_STRUCTURE *) (UINTN) (RomBarOffset + EfiRomHeader->PcirOffset);
252 //
253 // If a valid signature is not present in the PCI Data Structure, no further images can be located.
254 //
255 if (Pcir->Signature != PCI_DATA_STRUCTURE_SIGNATURE) {
256 break;
257 }
258 ImageSize = Pcir->ImageLength * 512;
259 if (RomBarOffset - (UINTN)RomBar + ImageSize > RomSize) {
260 break;
261 }
262
263 if ((Pcir->CodeType == PCI_CODE_TYPE_EFI_IMAGE) &&
264 (EfiRomHeader->EfiSignature == EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE) &&
265 ((EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER) ||
266 (EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER))) {
267
268 ImageOffset = EfiRomHeader->EfiImageHeaderOffset;
269 InitializationSize = EfiRomHeader->InitializationSize * 512;
270
271 if (InitializationSize <= ImageSize && ImageOffset < InitializationSize) {
272
273 ImageBuffer = (VOID *) (UINTN) (RomBarOffset + ImageOffset);
274 ImageLength = InitializationSize - ImageOffset;
275 DecompressedImageBuffer = NULL;
276
277 //
278 // decompress here if needed
279 //
280 SkipImage = FALSE;
281 if (EfiRomHeader->CompressionType > EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {
282 SkipImage = TRUE;
283 }
284
285 if (EfiRomHeader->CompressionType == EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {
286 Status = gBS->LocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID**)&Decompress);
287 ASSERT_EFI_ERROR(Status);
288 if (EFI_ERROR (Status)) {
289 SkipImage = TRUE;
290 } else {
291 SkipImage = TRUE;
292 Status = Decompress->GetInfo (
293 Decompress,
294 ImageBuffer,
295 ImageLength,
296 &DestinationSize,
297 &ScratchSize
298 );
299 if (!EFI_ERROR (Status)) {
300 DecompressedImageBuffer = AllocateZeroPool (DestinationSize);
301 if (ImageBuffer != NULL) {
302 Scratch = AllocateZeroPool (ScratchSize);
303 if (Scratch != NULL) {
304 Status = Decompress->Decompress (
305 Decompress,
306 ImageBuffer,
307 ImageLength,
308 DecompressedImageBuffer,
309 DestinationSize,
310 Scratch,
311 ScratchSize
312 );
313 if (!EFI_ERROR (Status)) {
314 ImageBuffer = DecompressedImageBuffer;
315 ImageLength = DestinationSize;
316 SkipImage = FALSE;
317 }
318
319 FreePool (Scratch);
320 }
321 }
322 }
323 }
324 }
325
326 if (!SkipImage) {
327 //
328 // load image and start image
329 //
330 UnicodeSPrint (RomFileName, sizeof (RomFileName), L"%s[%d]", FileName, ImageIndex);
331 FilePath = FileDevicePath (NULL, RomFileName);
332
333 Status = gBS->LoadImage (
334 TRUE,
335 gImageHandle,
336 FilePath,
337 ImageBuffer,
338 ImageLength,
339 &ImageHandle
340 );
341 if (EFI_ERROR (Status)) {
342 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_LOAD_FAIL), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex);
343 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_LOAD_IMAGE_ERROR), HiiHandle, ImageIndex, Status);
344 } else {
345 Status = gBS->StartImage (ImageHandle, NULL, NULL);
346 if (EFI_ERROR (Status)) {
347 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_START_FAIL), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex);
348 // PrintToken (STRING_TOKEN (STR_LOADPCIROM_START_IMAGE), HiiHandle, ImageIndex, Status);
349 } else {
350 ReturnStatus = Status;
351 }
352 }
353 }
354
355 if (DecompressedImageBuffer != NULL) {
356 FreePool (DecompressedImageBuffer);
357 }
358
359 }
360 }
361
362 RomBarOffset = RomBarOffset + ImageSize;
363 ImageIndex++;
364 } while (((Pcir->Indicator & 0x80) == 0x00) && ((RomBarOffset - (UINTN) RomBar) < RomSize));
365
366 return ReturnStatus;
367 }
368
369 /**
370 Connects all available drives and controllers.
371
372 @retval EFI_SUCCESS The operation was successful.
373 @retval EFI_ABORTED The abort mechanism was received.
374 **/
375 EFI_STATUS
376 EFIAPI
LoadPciRomConnectAllDriversToAllControllers(VOID)377 LoadPciRomConnectAllDriversToAllControllers (
378 VOID
379 )
380
381 {
382 EFI_STATUS Status;
383 UINTN AllHandleCount;
384 EFI_HANDLE *AllHandleBuffer;
385 UINTN Index;
386 UINTN HandleCount;
387 EFI_HANDLE *HandleBuffer;
388 UINTN *HandleType;
389 UINTN HandleIndex;
390 BOOLEAN Parent;
391 BOOLEAN Device;
392
393 Status = gBS->LocateHandleBuffer(
394 AllHandles,
395 NULL,
396 NULL,
397 &AllHandleCount,
398 &AllHandleBuffer
399 );
400 if (EFI_ERROR (Status)) {
401 return Status;
402 }
403
404 for (Index = 0; Index < AllHandleCount; Index++) {
405 if (ShellGetExecutionBreakFlag ()) {
406 Status = EFI_ABORTED;
407 goto Done;
408 }
409 //
410 // Scan the handle database
411 //
412 Status = ParseHandleDatabaseByRelationshipWithType(
413 NULL,
414 AllHandleBuffer[Index],
415 &HandleCount,
416 &HandleBuffer,
417 &HandleType
418 );
419 /*
420 Status = LibScanHandleDatabase (
421 NULL,
422 NULL,
423 AllHandleBuffer[Index],
424 NULL,
425 &HandleCount,
426 &HandleBuffer,
427 &HandleType
428 );
429 */
430 if (EFI_ERROR (Status)) {
431 goto Done;
432 }
433
434 Device = TRUE;
435 if ((HandleType[Index] & HR_DRIVER_BINDING_HANDLE) != 0) {
436 Device = FALSE;
437 }
438
439 if ((HandleType[Index] & HR_IMAGE_HANDLE) != 0) {
440 Device = FALSE;
441 }
442
443 if (Device) {
444 Parent = FALSE;
445 for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
446 if ((HandleType[HandleIndex] & HR_PARENT_HANDLE) != 0) {
447 Parent = TRUE;
448 }
449 }
450
451 if (!Parent) {
452 if ((HandleType[Index] & HR_DEVICE_HANDLE) != 0) {
453 Status = gBS->ConnectController (
454 AllHandleBuffer[Index],
455 NULL,
456 NULL,
457 TRUE
458 );
459 }
460 }
461 }
462
463 FreePool (HandleBuffer);
464 FreePool (HandleType);
465 }
466
467 Done:
468 FreePool (AllHandleBuffer);
469 return Status;
470 }
471