]>
Commit | Line | Data |
---|---|---|
5d73d92f | 1 | /** @file\r |
2 | Main file for LoadPciRom shell Debug1 function.\r | |
3 | \r | |
c011b6c9 | 4 | (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r |
20cfed16 | 5 | Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.<BR>\r |
5d73d92f | 6 | This program and the accompanying materials\r |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | #include "UefiShellDebug1CommandsLib.h"\r | |
17 | #include <IndustryStandard/Pci22.h>\r | |
18 | #include <IndustryStandard/Pci23.h>\r | |
19 | #include <IndustryStandard/PeImage.h>\r | |
20 | #include <Protocol/Decompress.h>\r | |
21 | \r | |
3737ac2b | 22 | /**\r |
23 | Connects all available drives and controllers.\r | |
24 | \r | |
25 | @retval EFI_SUCCESS The operation was successful.\r | |
26 | @retval EFI_ABORTED The abort mechanism was received.\r | |
27 | **/\r | |
5d73d92f | 28 | EFI_STATUS\r |
5d73d92f | 29 | LoadPciRomConnectAllDriversToAllControllers (\r |
30 | VOID\r | |
31 | );\r | |
32 | \r | |
3737ac2b | 33 | /**\r |
34 | Command entry point.\r | |
5d73d92f | 35 | \r |
3737ac2b | 36 | @param[in] RomBar The Rom Base address.\r |
37 | @param[in] RomSize The Rom size.\r | |
38 | @param[in] FileName The file name.\r | |
39 | \r | |
40 | @retval EFI_SUCCESS The command completed successfully.\r | |
41 | @retval EFI_INVALID_PARAMETER Command usage error.\r | |
42 | @retval EFI_UNSUPPORTED Protocols unsupported.\r | |
43 | @retval EFI_OUT_OF_RESOURCES Out of memory.\r | |
44 | @retval Other value Unknown error.\r | |
45 | **/\r | |
5d73d92f | 46 | EFI_STATUS\r |
47 | EFIAPI\r | |
48 | LoadEfiDriversFromRomImage (\r | |
49 | VOID *RomBar,\r | |
50 | UINTN RomSize,\r | |
51 | CONST CHAR16 *FileName\r | |
52 | );\r | |
53 | \r | |
54 | STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r | |
55 | {L"-nc", TypeFlag},\r | |
56 | {NULL, TypeMax}\r | |
57 | };\r | |
58 | \r | |
3737ac2b | 59 | /**\r |
60 | Function for 'loadpcirom' command.\r | |
61 | \r | |
62 | @param[in] ImageHandle Handle to the Image (NULL if Internal).\r | |
63 | @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r | |
64 | **/\r | |
5d73d92f | 65 | SHELL_STATUS\r |
66 | EFIAPI\r | |
67 | ShellCommandRunLoadPciRom (\r | |
68 | IN EFI_HANDLE ImageHandle,\r | |
69 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
70 | )\r | |
71 | {\r | |
72 | EFI_SHELL_FILE_INFO *FileList;\r | |
73 | UINTN SourceSize;\r | |
74 | UINT8 *File1Buffer;\r | |
75 | EFI_STATUS Status;\r | |
76 | LIST_ENTRY *Package;\r | |
77 | CHAR16 *ProblemParam;\r | |
78 | SHELL_STATUS ShellStatus;\r | |
79 | BOOLEAN Connect;\r | |
80 | CONST CHAR16 *Param;\r | |
81 | UINTN ParamCount;\r | |
82 | EFI_SHELL_FILE_INFO *Node;\r | |
83 | //\r | |
84 | // Local variable initializations\r | |
85 | //\r | |
86 | File1Buffer = NULL;\r | |
87 | ShellStatus = SHELL_SUCCESS;\r | |
88 | FileList = NULL;\r | |
89 | \r | |
90 | \r | |
91 | //\r | |
92 | // verify number of arguments\r | |
93 | //\r | |
94 | Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);\r | |
95 | if (EFI_ERROR(Status)) {\r | |
96 | if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r | |
4092a8f6 | 97 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"loadpcirom", ProblemParam); \r |
5d73d92f | 98 | FreePool(ProblemParam);\r |
99 | ShellStatus = SHELL_INVALID_PARAMETER;\r | |
100 | } else {\r | |
101 | ASSERT(FALSE);\r | |
102 | }\r | |
103 | } else {\r | |
3737ac2b | 104 | if (ShellCommandLineGetCount(Package) < 2) {\r |
4092a8f6 | 105 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"loadpcirom"); \r |
5d73d92f | 106 | ShellStatus = SHELL_INVALID_PARAMETER;\r |
107 | } else {\r | |
3737ac2b | 108 | if (ShellCommandLineGetFlag(Package, L"-nc")) {\r |
5d73d92f | 109 | Connect = FALSE;\r |
110 | } else {\r | |
111 | Connect = TRUE;\r | |
112 | }\r | |
113 | \r | |
114 | //\r | |
115 | // get a list with each file specified by parameters\r | |
116 | // if parameter is a directory then add all the files below it to the list\r | |
117 | //\r | |
118 | for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r | |
119 | ; Param != NULL\r | |
120 | ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r | |
121 | ){\r | |
122 | Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r | |
123 | if (EFI_ERROR(Status)) {\r | |
4092a8f6 | 124 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"loadpcirom", Param); \r |
5d73d92f | 125 | ShellStatus = SHELL_ACCESS_DENIED;\r |
126 | break;\r | |
127 | }\r | |
128 | }\r | |
3737ac2b | 129 | if (ShellStatus == SHELL_SUCCESS && FileList != NULL) {\r |
5d73d92f | 130 | //\r |
131 | // loop through the list and make sure we are not aborting...\r | |
132 | //\r | |
133 | for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)\r | |
134 | ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()\r | |
135 | ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)\r | |
136 | ){\r | |
137 | if (EFI_ERROR(Node->Status)){\r | |
4092a8f6 | 138 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"loadpcirom", Node->FullName); \r |
5d73d92f | 139 | ShellStatus = SHELL_INVALID_PARAMETER;\r |
140 | continue;\r | |
141 | }\r | |
142 | if (FileHandleIsDirectory(Node->Handle) == EFI_SUCCESS) {\r | |
4092a8f6 | 143 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_NOT_DIR), gShellDebug1HiiHandle, L"loadpcirom", Node->FullName); \r |
5d73d92f | 144 | ShellStatus = SHELL_INVALID_PARAMETER;\r |
145 | continue;\r | |
146 | }\r | |
147 | SourceSize = (UINTN) Node->Info->FileSize;\r | |
3737ac2b | 148 | File1Buffer = AllocateZeroPool (SourceSize);\r |
20cfed16 RN |
149 | if (File1Buffer == NULL) {\r |
150 | ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"loadpcirom");\r | |
151 | ShellStatus = SHELL_OUT_OF_RESOURCES;\r | |
152 | continue;\r | |
153 | }\r | |
5d73d92f | 154 | Status = gEfiShellProtocol->ReadFile(Node->Handle, &SourceSize, File1Buffer);\r |
155 | if (EFI_ERROR(Status)) {\r | |
4092a8f6 | 156 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_READ_FAIL), gShellDebug1HiiHandle, L"loadpcirom", Node->FullName); \r |
5d73d92f | 157 | ShellStatus = SHELL_INVALID_PARAMETER;\r |
158 | } else {\r | |
159 | Status = LoadEfiDriversFromRomImage (\r | |
160 | File1Buffer,\r | |
161 | SourceSize,\r | |
162 | Node->FullName\r | |
163 | );\r | |
164 | \r | |
165 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOAD_PCI_ROM_RES), gShellDebug1HiiHandle, Node->FullName, Status);\r | |
166 | }\r | |
167 | FreePool(File1Buffer);\r | |
168 | }\r | |
3737ac2b | 169 | } else if (ShellStatus == SHELL_SUCCESS) {\r |
4092a8f6 | 170 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_NOT_SPEC), gShellDebug1HiiHandle, "loadpcirom"); \r |
3737ac2b | 171 | ShellStatus = SHELL_NOT_FOUND;\r |
5d73d92f | 172 | }\r |
173 | if (FileList != NULL && !IsListEmpty(&FileList->Link)) {\r | |
174 | Status = ShellCloseFileMetaArg(&FileList);\r | |
175 | }\r | |
176 | FileList = NULL;\r | |
177 | \r | |
178 | if (Connect) {\r | |
179 | Status = LoadPciRomConnectAllDriversToAllControllers ();\r | |
180 | }\r | |
181 | }\r | |
182 | }\r | |
183 | \r | |
184 | return (ShellStatus);\r | |
185 | }\r | |
186 | \r | |
3737ac2b | 187 | /**\r |
188 | Command entry point.\r | |
189 | \r | |
190 | @param[in] RomBar The Rom Base address.\r | |
191 | @param[in] RomSize The Rom size.\r | |
192 | @param[in] FileName The file name.\r | |
193 | \r | |
194 | @retval EFI_SUCCESS The command completed successfully.\r | |
195 | @retval EFI_INVALID_PARAMETER Command usage error.\r | |
196 | @retval EFI_UNSUPPORTED Protocols unsupported.\r | |
197 | @retval EFI_OUT_OF_RESOURCES Out of memory.\r | |
198 | @retval Other value Unknown error.\r | |
199 | **/\r | |
5d73d92f | 200 | EFI_STATUS\r |
3737ac2b | 201 | EFIAPI\r |
5d73d92f | 202 | LoadEfiDriversFromRomImage (\r |
203 | VOID *RomBar,\r | |
204 | UINTN RomSize,\r | |
205 | CONST CHAR16 *FileName\r | |
206 | )\r | |
5d73d92f | 207 | \r |
5d73d92f | 208 | {\r |
209 | EFI_PCI_EXPANSION_ROM_HEADER *EfiRomHeader;\r | |
210 | PCI_DATA_STRUCTURE *Pcir;\r | |
211 | UINTN ImageIndex;\r | |
212 | UINTN RomBarOffset;\r | |
213 | UINT32 ImageSize;\r | |
214 | UINT16 ImageOffset;\r | |
215 | EFI_HANDLE ImageHandle;\r | |
216 | EFI_STATUS Status;\r | |
3737ac2b | 217 | EFI_STATUS ReturnStatus;\r |
5d73d92f | 218 | CHAR16 RomFileName[280];\r |
219 | EFI_DEVICE_PATH_PROTOCOL *FilePath;\r | |
220 | BOOLEAN SkipImage;\r | |
221 | UINT32 DestinationSize;\r | |
222 | UINT32 ScratchSize;\r | |
223 | UINT8 *Scratch;\r | |
224 | VOID *ImageBuffer;\r | |
225 | VOID *DecompressedImageBuffer;\r | |
226 | UINT32 ImageLength;\r | |
227 | EFI_DECOMPRESS_PROTOCOL *Decompress;\r | |
94020bb4 | 228 | UINT32 InitializationSize;\r |
5d73d92f | 229 | \r |
230 | ImageIndex = 0;\r | |
3737ac2b | 231 | ReturnStatus = EFI_NOT_FOUND;\r |
5d73d92f | 232 | RomBarOffset = (UINTN) RomBar;\r |
233 | \r | |
234 | do {\r | |
235 | \r | |
236 | EfiRomHeader = (EFI_PCI_EXPANSION_ROM_HEADER *) (UINTN) RomBarOffset;\r | |
237 | \r | |
94020bb4 | 238 | if (EfiRomHeader->Signature != PCI_EXPANSION_ROM_HEADER_SIGNATURE) {\r |
4092a8f6 | 239 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_CORRUPT), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex); \r |
5d73d92f | 240 | // PrintToken (STRING_TOKEN (STR_LOADPCIROM_IMAGE_CORRUPT), HiiHandle, ImageIndex);\r |
3737ac2b | 241 | return ReturnStatus;\r |
5d73d92f | 242 | }\r |
243 | \r | |
94020bb4 | 244 | //\r |
245 | // If the pointer to the PCI Data Structure is invalid, no further images can be located. \r | |
246 | // The PCI Data Structure must be DWORD aligned. \r | |
247 | //\r | |
248 | if (EfiRomHeader->PcirOffset == 0 ||\r | |
249 | (EfiRomHeader->PcirOffset & 3) != 0 ||\r | |
250 | RomBarOffset - (UINTN)RomBar + EfiRomHeader->PcirOffset + sizeof (PCI_DATA_STRUCTURE) > RomSize) {\r | |
251 | break;\r | |
252 | }\r | |
253 | \r | |
5d73d92f | 254 | Pcir = (PCI_DATA_STRUCTURE *) (UINTN) (RomBarOffset + EfiRomHeader->PcirOffset);\r |
94020bb4 | 255 | //\r |
256 | // If a valid signature is not present in the PCI Data Structure, no further images can be located.\r | |
257 | //\r | |
258 | if (Pcir->Signature != PCI_DATA_STRUCTURE_SIGNATURE) {\r | |
259 | break;\r | |
260 | }\r | |
5d73d92f | 261 | ImageSize = Pcir->ImageLength * 512;\r |
94020bb4 | 262 | if (RomBarOffset - (UINTN)RomBar + ImageSize > RomSize) {\r |
263 | break;\r | |
264 | }\r | |
5d73d92f | 265 | \r |
266 | if ((Pcir->CodeType == PCI_CODE_TYPE_EFI_IMAGE) &&\r | |
94020bb4 | 267 | (EfiRomHeader->EfiSignature == EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE) &&\r |
268 | ((EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER) ||\r | |
269 | (EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER))) {\r | |
270 | \r | |
271 | ImageOffset = EfiRomHeader->EfiImageHeaderOffset;\r | |
272 | InitializationSize = EfiRomHeader->InitializationSize * 512;\r | |
5d73d92f | 273 | \r |
94020bb4 | 274 | if (InitializationSize <= ImageSize && ImageOffset < InitializationSize) {\r |
5d73d92f | 275 | \r |
276 | ImageBuffer = (VOID *) (UINTN) (RomBarOffset + ImageOffset);\r | |
94020bb4 | 277 | ImageLength = InitializationSize - ImageOffset;\r |
5d73d92f | 278 | DecompressedImageBuffer = NULL;\r |
279 | \r | |
280 | //\r | |
281 | // decompress here if needed\r | |
282 | //\r | |
283 | SkipImage = FALSE;\r | |
284 | if (EfiRomHeader->CompressionType > EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {\r | |
285 | SkipImage = TRUE;\r | |
286 | }\r | |
287 | \r | |
288 | if (EfiRomHeader->CompressionType == EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {\r | |
289 | Status = gBS->LocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID**)&Decompress);\r | |
290 | ASSERT_EFI_ERROR(Status);\r | |
291 | if (EFI_ERROR (Status)) {\r | |
292 | SkipImage = TRUE;\r | |
293 | } else {\r | |
294 | SkipImage = TRUE;\r | |
295 | Status = Decompress->GetInfo (\r | |
296 | Decompress,\r | |
297 | ImageBuffer,\r | |
298 | ImageLength,\r | |
299 | &DestinationSize,\r | |
300 | &ScratchSize\r | |
301 | );\r | |
302 | if (!EFI_ERROR (Status)) {\r | |
3737ac2b | 303 | DecompressedImageBuffer = AllocateZeroPool (DestinationSize);\r |
5d73d92f | 304 | if (ImageBuffer != NULL) {\r |
3737ac2b | 305 | Scratch = AllocateZeroPool (ScratchSize);\r |
5d73d92f | 306 | if (Scratch != NULL) {\r |
307 | Status = Decompress->Decompress (\r | |
308 | Decompress,\r | |
309 | ImageBuffer,\r | |
310 | ImageLength,\r | |
311 | DecompressedImageBuffer,\r | |
312 | DestinationSize,\r | |
313 | Scratch,\r | |
314 | ScratchSize\r | |
315 | );\r | |
316 | if (!EFI_ERROR (Status)) {\r | |
317 | ImageBuffer = DecompressedImageBuffer;\r | |
318 | ImageLength = DestinationSize;\r | |
319 | SkipImage = FALSE;\r | |
320 | }\r | |
321 | \r | |
322 | FreePool (Scratch);\r | |
323 | }\r | |
324 | }\r | |
325 | }\r | |
326 | }\r | |
327 | }\r | |
328 | \r | |
329 | if (!SkipImage) {\r | |
330 | //\r | |
331 | // load image and start image\r | |
332 | //\r | |
333 | UnicodeSPrint (RomFileName, sizeof (RomFileName), L"%s[%d]", FileName, ImageIndex);\r | |
334 | FilePath = FileDevicePath (NULL, RomFileName);\r | |
335 | \r | |
336 | Status = gBS->LoadImage (\r | |
337 | TRUE,\r | |
338 | gImageHandle,\r | |
339 | FilePath,\r | |
340 | ImageBuffer,\r | |
341 | ImageLength,\r | |
342 | &ImageHandle\r | |
343 | );\r | |
344 | if (EFI_ERROR (Status)) {\r | |
4092a8f6 | 345 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_LOAD_FAIL), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex); \r |
5d73d92f | 346 | // PrintToken (STRING_TOKEN (STR_LOADPCIROM_LOAD_IMAGE_ERROR), HiiHandle, ImageIndex, Status);\r |
347 | } else {\r | |
348 | Status = gBS->StartImage (ImageHandle, NULL, NULL);\r | |
349 | if (EFI_ERROR (Status)) {\r | |
4092a8f6 | 350 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_START_FAIL), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex); \r |
5d73d92f | 351 | // PrintToken (STRING_TOKEN (STR_LOADPCIROM_START_IMAGE), HiiHandle, ImageIndex, Status);\r |
352 | } else {\r | |
3737ac2b | 353 | ReturnStatus = Status;\r |
5d73d92f | 354 | }\r |
355 | }\r | |
356 | }\r | |
357 | \r | |
358 | if (DecompressedImageBuffer != NULL) {\r | |
359 | FreePool (DecompressedImageBuffer);\r | |
360 | }\r | |
361 | \r | |
362 | }\r | |
363 | }\r | |
364 | \r | |
365 | RomBarOffset = RomBarOffset + ImageSize;\r | |
366 | ImageIndex++;\r | |
367 | } while (((Pcir->Indicator & 0x80) == 0x00) && ((RomBarOffset - (UINTN) RomBar) < RomSize));\r | |
368 | \r | |
3737ac2b | 369 | return ReturnStatus;\r |
5d73d92f | 370 | }\r |
371 | \r | |
3737ac2b | 372 | /**\r |
373 | Connects all available drives and controllers.\r | |
374 | \r | |
375 | @retval EFI_SUCCESS The operation was successful.\r | |
376 | @retval EFI_ABORTED The abort mechanism was received.\r | |
377 | **/\r | |
5d73d92f | 378 | EFI_STATUS\r |
379 | LoadPciRomConnectAllDriversToAllControllers (\r | |
380 | VOID\r | |
381 | )\r | |
5d73d92f | 382 | {\r |
383 | EFI_STATUS Status;\r | |
5d73d92f | 384 | UINTN HandleCount;\r |
385 | EFI_HANDLE *HandleBuffer;\r | |
fc41b97f RN |
386 | UINTN Index;\r |
387 | \r | |
388 | Status = gBS->LocateHandleBuffer (\r | |
389 | AllHandles,\r | |
390 | NULL,\r | |
391 | NULL,\r | |
392 | &HandleCount,\r | |
393 | &HandleBuffer\r | |
394 | );\r | |
5d73d92f | 395 | if (EFI_ERROR (Status)) {\r |
396 | return Status;\r | |
397 | }\r | |
398 | \r | |
fc41b97f | 399 | for (Index = 0; Index < HandleCount; Index++) {\r |
5d73d92f | 400 | if (ShellGetExecutionBreakFlag ()) {\r |
401 | Status = EFI_ABORTED;\r | |
fc41b97f | 402 | break;\r |
5d73d92f | 403 | }\r |
fc41b97f RN |
404 | gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);\r |
405 | }\r | |
5d73d92f | 406 | \r |
fc41b97f | 407 | if (HandleBuffer != NULL) {\r |
5d73d92f | 408 | FreePool (HandleBuffer);\r |
5d73d92f | 409 | }\r |
5d73d92f | 410 | return Status;\r |
411 | }\r |