]>
Commit | Line | Data |
---|---|---|
2ef2b01e A |
1 | /** @file\r |
2 | EBL commands for EFI and PI Devices\r | |
3 | \r | |
60274cca HT |
4 | Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>\r |
5 | Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r | |
2ef2b01e | 6 | \r |
60274cca | 7 | This program and the accompanying materials\r |
2ef2b01e A |
8 | are licensed and made available under the terms and conditions of the BSD License\r |
9 | which accompanies this distribution. The full text of the license may be found at\r | |
10 | http://opensource.org/licenses/bsd-license.php\r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #include "Ebl.h"\r | |
18 | \r | |
19 | \r | |
20 | EFI_DXE_SERVICES *gDS = NULL;\r | |
21 | \r | |
22 | \r | |
23 | /**\r | |
24 | Print information about the File System device.\r | |
25 | \r | |
26 | @param File Open File for the device\r | |
27 | \r | |
28 | **/\r | |
29 | VOID\r | |
30 | EblPrintFsInfo (\r | |
31 | IN EFI_OPEN_FILE *File\r | |
32 | )\r | |
33 | {\r | |
638909f1 | 34 | CHAR16 *Str;\r |
35 | \r | |
2ef2b01e A |
36 | if (File == NULL) {\r |
37 | return;\r | |
38 | }\r | |
39 | \r | |
40 | AsciiPrint (" %a: ", File->DeviceName);\r | |
41 | if (File->FsInfo != NULL) {\r | |
638909f1 | 42 | for (Str = File->FsInfo->VolumeLabel; *Str != '\0'; Str++) {\r |
43 | if (*Str == ' ') {\r | |
44 | // UI makes you enter _ for space, so make the printout match that\r | |
45 | *Str = '_';\r | |
46 | }\r | |
47 | AsciiPrint ("%c", *Str);\r | |
48 | }\r | |
49 | AsciiPrint (":");\r | |
2ef2b01e A |
50 | if (File->FsInfo->ReadOnly) {\r |
51 | AsciiPrint ("ReadOnly");\r | |
52 | }\r | |
53 | }\r | |
54 | \r | |
55 | AsciiPrint ("\n");\r | |
56 | EfiClose (File);\r | |
57 | }\r | |
58 | \r | |
59 | \r | |
60 | /**\r | |
61 | Print information about the FV devices.\r | |
62 | \r | |
63 | @param File Open File for the device\r | |
64 | \r | |
65 | **/\r | |
66 | VOID\r | |
67 | EblPrintFvbInfo (\r | |
68 | IN EFI_OPEN_FILE *File\r | |
69 | )\r | |
70 | {\r | |
71 | if (File == NULL) {\r | |
72 | return;\r | |
73 | }\r | |
74 | \r | |
75 | AsciiPrint (" %a: 0x%08lx - 0x%08lx : 0x%08x\n", File->DeviceName, File->FvStart, File->FvStart + File->FvSize - 1, File->FvSize);\r | |
76 | EfiClose (File);\r | |
77 | }\r | |
78 | \r | |
79 | \r | |
80 | /**\r | |
81 | Print information about the Blk IO devices.\r | |
82 | If the device supports PXE dump out extra information\r | |
83 | \r | |
84 | @param File Open File for the device\r | |
85 | \r | |
86 | **/\r | |
87 | VOID\r | |
88 | EblPrintBlkIoInfo (\r | |
89 | IN EFI_OPEN_FILE *File\r | |
90 | )\r | |
91 | {\r | |
92 | UINT64 DeviceSize;\r | |
6f72e28d | 93 | UINTN Index;\r |
94 | UINTN Max;\r | |
95 | EFI_OPEN_FILE *FsFile;\r | |
2ef2b01e A |
96 | \r |
97 | if (File == NULL) {\r | |
98 | return;\r | |
99 | }\r | |
100 | \r | |
101 | AsciiPrint (" %a: ", File->DeviceName);\r | |
6f72e28d | 102 | \r |
103 | // print out name of file system, if any, on this block device\r | |
104 | Max = EfiGetDeviceCounts (EfiOpenFileSystem);\r | |
105 | if (Max != 0) {\r | |
106 | for (Index = 0; Index < Max; Index++) {\r | |
107 | FsFile = EfiDeviceOpenByType (EfiOpenFileSystem, Index);\r | |
108 | if (FsFile != NULL) {\r | |
109 | if (FsFile->EfiHandle == File->EfiHandle) {\r | |
110 | AsciiPrint ("fs%d: ", Index);\r | |
111 | EfiClose (FsFile);\r | |
112 | break;\r | |
113 | }\r | |
0a0951ea | 114 | EfiClose (FsFile);\r |
6f72e28d | 115 | }\r |
6f72e28d | 116 | }\r |
117 | }\r | |
118 | \r | |
119 | // Print out useful Block IO media properties\r | |
0a0951ea | 120 | if (File->FsBlockIoMedia->RemovableMedia) {\r |
2ef2b01e A |
121 | AsciiPrint ("Removable ");\r |
122 | }\r | |
0a0951ea | 123 | if (!File->FsBlockIoMedia->MediaPresent) {\r |
124 | AsciiPrint ("No Media\n");\r | |
125 | } else {\r | |
126 | if (File->FsBlockIoMedia->LogicalPartition) {\r | |
127 | AsciiPrint ("Partition ");\r | |
128 | }\r | |
129 | DeviceSize = MultU64x32 (File->FsBlockIoMedia->LastBlock + 1, File->FsBlockIoMedia->BlockSize);\r | |
130 | AsciiPrint ("Size = 0x%lX\n", DeviceSize);\r | |
2ef2b01e | 131 | }\r |
2ef2b01e A |
132 | EfiClose (File);\r |
133 | }\r | |
134 | \r | |
2ef2b01e A |
135 | /**\r |
136 | Print information about the Load File devices.\r | |
137 | If the device supports PXE dump out extra information\r | |
138 | \r | |
139 | @param File Open File for the device\r | |
140 | \r | |
141 | **/\r | |
142 | VOID\r | |
143 | EblPrintLoadFileInfo (\r | |
144 | IN EFI_OPEN_FILE *File\r | |
145 | )\r | |
146 | {\r | |
147 | EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r | |
148 | MAC_ADDR_DEVICE_PATH *MacAddr;\r | |
149 | UINTN HwAddressSize;\r | |
150 | UINTN Index;\r | |
151 | \r | |
152 | if (File == NULL) {\r | |
153 | return;\r | |
154 | }\r | |
155 | \r | |
156 | AsciiPrint (" %a: %a ", File->DeviceName, EblLoadFileBootTypeString (File->EfiHandle));\r | |
157 | \r | |
158 | if (File->DevicePath != NULL) {\r | |
159 | // Try to print out the MAC address\r | |
160 | for (DevicePathNode = File->DevicePath; \r | |
161 | !IsDevicePathEnd (DevicePathNode); \r | |
162 | DevicePathNode = NextDevicePathNode (DevicePathNode)) {\r | |
163 | \r | |
164 | if ((DevicePathType (DevicePathNode) == MESSAGING_DEVICE_PATH) && (DevicePathSubType (DevicePathNode) == MSG_MAC_ADDR_DP)) {\r | |
165 | MacAddr = (MAC_ADDR_DEVICE_PATH *)DevicePathNode;\r | |
166 | \r | |
167 | HwAddressSize = sizeof (EFI_MAC_ADDRESS);\r | |
168 | if (MacAddr->IfType == 0x01 || MacAddr->IfType == 0x00) {\r | |
169 | HwAddressSize = 6;\r | |
170 | }\r | |
171 | \r | |
172 | AsciiPrint ("MAC ");\r | |
173 | for (Index = 0; Index < HwAddressSize; Index++) {\r | |
174 | AsciiPrint ("%02x", MacAddr->MacAddress.Addr[Index] & 0xff);\r | |
175 | }\r | |
176 | } \r | |
177 | }\r | |
178 | }\r | |
179 | \r | |
180 | AsciiPrint ("\n");\r | |
181 | EfiClose (File);\r | |
182 | return;\r | |
183 | }\r | |
184 | \r | |
185 | \r | |
186 | \r | |
187 | /**\r | |
188 | Dump information about devices in the system. \r | |
189 | \r | |
190 | fv: PI Firmware Volume\r | |
191 | fs: EFI Simple File System\r | |
192 | blk: EFI Block IO\r | |
193 | LoadFile: EFI Load File Protocol (commonly PXE network boot)\r | |
194 | \r | |
195 | Argv[0] - "device"\r | |
196 | \r | |
197 | @param Argc Number of command arguments in Argv\r | |
198 | @param Argv Array of strings that represent the parsed command line. \r | |
199 | Argv[0] is the comamnd name\r | |
200 | \r | |
201 | @return EFI_SUCCESS\r | |
202 | \r | |
203 | **/\r | |
204 | EFI_STATUS\r | |
205 | EblDeviceCmd (\r | |
206 | IN UINTN Argc,\r | |
207 | IN CHAR8 **Argv\r | |
208 | )\r | |
209 | {\r | |
210 | UINTN Index;\r | |
211 | UINTN CurrentRow;\r | |
212 | UINTN Max;\r | |
f6381f4c | 213 | EFI_OPEN_FILE *File;\r |
2ef2b01e A |
214 | \r |
215 | CurrentRow = 0;\r | |
208a8330 | 216 | \r |
2ef2b01e A |
217 | // Need to call here to make sure Device Counts are valid\r |
218 | EblUpdateDeviceLists ();\r | |
219 | \r | |
f6381f4c | 220 | //\r |
221 | // Probe for media insertion/removal in removable media devices\r | |
222 | //\r | |
223 | Max = EfiGetDeviceCounts (EfiOpenBlockIo);\r | |
224 | if (Max != 0) {\r | |
225 | for (Index = 0; Index < Max; Index++) {\r | |
226 | File = EfiDeviceOpenByType (EfiOpenBlockIo, Index);\r | |
227 | if (File != NULL) {\r | |
228 | if (File->FsBlockIoMedia->RemovableMedia) {\r | |
6191913f | 229 | // Probe to see if media is present (or not) or media changed\r |
230 | // this causes the ReinstallProtocolInterface() to fire in the\r | |
231 | // block io driver to update the system about media change events\r | |
232 | File->FsBlockIo->ReadBlocks (File->FsBlockIo, File->FsBlockIo->Media->MediaId, (EFI_LBA)0, 0, NULL);\r | |
f6381f4c | 233 | }\r |
234 | EfiClose (File);\r | |
235 | }\r | |
236 | }\r | |
237 | }\r | |
238 | \r | |
239 | // Now we can print out the info...\r | |
240 | \r | |
2ef2b01e A |
241 | Max = EfiGetDeviceCounts (EfiOpenFirmwareVolume);\r |
242 | if (Max != 0) {\r | |
243 | AsciiPrint ("Firmware Volume Devices:\n");\r | |
244 | for (Index = 0; Index < Max; Index++) {\r | |
245 | EblPrintFvbInfo (EfiDeviceOpenByType (EfiOpenFirmwareVolume, Index));\r | |
246 | if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r | |
247 | break;\r | |
248 | }\r | |
249 | }\r | |
250 | }\r | |
251 | \r | |
252 | Max = EfiGetDeviceCounts (EfiOpenFileSystem);\r | |
253 | if (Max != 0) {\r | |
254 | AsciiPrint ("File System Devices:\n");\r | |
255 | for (Index = 0; Index < Max; Index++) {\r | |
256 | EblPrintFsInfo (EfiDeviceOpenByType (EfiOpenFileSystem, Index));\r | |
257 | if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r | |
258 | break;\r | |
259 | }\r | |
260 | }\r | |
261 | }\r | |
262 | \r | |
263 | Max = EfiGetDeviceCounts (EfiOpenBlockIo);\r | |
264 | if (Max != 0) {\r | |
265 | AsciiPrint ("Block IO Devices:\n");\r | |
266 | for (Index = 0; Index < Max; Index++) {\r | |
267 | EblPrintBlkIoInfo (EfiDeviceOpenByType (EfiOpenBlockIo, Index));\r | |
268 | if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r | |
269 | break;\r | |
270 | }\r | |
271 | }\r | |
272 | }\r | |
273 | \r | |
274 | Max = EfiGetDeviceCounts (EfiOpenLoadFile);\r | |
275 | if (Max != 0) {\r | |
276 | AsciiPrint ("LoadFile Devices: (usually network)\n");\r | |
277 | for (Index = 0; Index < Max; Index++) {\r | |
278 | EblPrintLoadFileInfo (EfiDeviceOpenByType (EfiOpenLoadFile, Index));\r | |
279 | if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r | |
280 | break;\r | |
281 | }\r | |
282 | }\r | |
283 | }\r | |
284 | \r | |
285 | return EFI_SUCCESS;\r | |
286 | }\r | |
287 | \r | |
288 | \r | |
289 | /**\r | |
290 | Start an EFI image (PE32+ with EFI defined entry point). \r | |
291 | \r | |
292 | Argv[0] - "start"\r | |
293 | Argv[1] - device name and path\r | |
294 | Argv[2] - "" string to pass into image being started\r | |
295 | \r | |
296 | start fs1:\Temp\Fv.Fv "arg to pass" ; load an FV from the disk and pass the\r | |
297 | ; ascii string arg to pass to the image\r | |
298 | start fv0:\FV ; load an FV from an FV (not common)\r | |
299 | start LoadFile0: ; load an FV via a PXE boot\r | |
300 | \r | |
301 | @param Argc Number of command arguments in Argv\r | |
302 | @param Argv Array of strings that represent the parsed command line. \r | |
303 | Argv[0] is the comamnd name\r | |
304 | \r | |
305 | @return EFI_SUCCESS\r | |
306 | \r | |
307 | **/\r | |
308 | EFI_STATUS\r | |
309 | EblStartCmd (\r | |
310 | IN UINTN Argc,\r | |
311 | IN CHAR8 **Argv\r | |
312 | )\r | |
313 | {\r | |
314 | EFI_STATUS Status;\r | |
315 | EFI_OPEN_FILE *File;\r | |
316 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r | |
317 | EFI_HANDLE ImageHandle;\r | |
318 | UINTN ExitDataSize;\r | |
319 | CHAR16 *ExitData;\r | |
320 | VOID *Buffer;\r | |
321 | UINTN BufferSize;\r | |
322 | EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r | |
323 | \r | |
324 | ImageHandle = NULL;\r | |
325 | \r | |
326 | if (Argc < 2) {\r | |
327 | return EFI_INVALID_PARAMETER;\r | |
328 | }\r | |
329 | \r | |
330 | File = EfiOpen (Argv[1], EFI_FILE_MODE_READ, 0);\r | |
331 | if (File == NULL) {\r | |
332 | return EFI_INVALID_PARAMETER; \r | |
333 | }\r | |
334 | \r | |
335 | DevicePath = File->DevicePath;\r | |
336 | if (DevicePath != NULL) {\r | |
337 | // check for device path form: blk, fv, fs, and loadfile\r | |
338 | Status = gBS->LoadImage (FALSE, gImageHandle, DevicePath, NULL, 0, &ImageHandle);\r | |
339 | } else {\r | |
340 | // Check for buffer form: A0x12345678:0x1234 syntax.\r | |
341 | // Means load using buffer starting at 0x12345678 of size 0x1234.\r | |
342 | \r | |
343 | Status = EfiReadAllocatePool (File, &Buffer, &BufferSize);\r | |
344 | if (EFI_ERROR (Status)) {\r | |
345 | EfiClose (File);\r | |
346 | return Status;\r | |
347 | }\r | |
348 | Status = gBS->LoadImage (FALSE, gImageHandle, DevicePath, Buffer, BufferSize, &ImageHandle);\r | |
349 | \r | |
350 | FreePool (Buffer);\r | |
351 | }\r | |
352 | \r | |
353 | EfiClose (File);\r | |
354 | \r | |
355 | if (!EFI_ERROR (Status)) {\r | |
356 | if (Argc >= 3) {\r | |
357 | // Argv[2] is a "" string that we pass directly to the EFI application without the ""\r | |
358 | // We don't pass Argv[0] to the EFI Application (it's name) just the args\r | |
359 | Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&ImageInfo);\r | |
360 | ASSERT_EFI_ERROR (Status);\r | |
361 | \r | |
362 | ImageInfo->LoadOptionsSize = (UINT32)AsciiStrSize (Argv[2]);\r | |
363 | ImageInfo->LoadOptions = AllocatePool (ImageInfo->LoadOptionsSize);\r | |
364 | AsciiStrCpy (ImageInfo->LoadOptions, Argv[2]);\r | |
365 | }\r | |
366 | \r | |
367 | // Transfer control to the EFI image we loaded with LoadImage()\r | |
368 | Status = gBS->StartImage (ImageHandle, &ExitDataSize, &ExitData);\r | |
369 | }\r | |
370 | \r | |
371 | return Status;\r | |
372 | }\r | |
373 | \r | |
374 | \r | |
375 | /**\r | |
376 | Load a Firmware Volume (FV) into memory from a device. This causes drivers in\r | |
377 | the FV to be dispatched if the dependancies of the drivers are met.\r | |
378 | \r | |
379 | Argv[0] - "loadfv"\r | |
380 | Argv[1] - device name and path\r | |
381 | \r | |
382 | loadfv fs1:\Temp\Fv.Fv ; load an FV from the disk\r | |
383 | loadfv fv0:\FV ; load an FV from an FV (not common)\r | |
384 | loadfv LoadFile0: ; load an FV via a PXE boot\r | |
385 | \r | |
386 | @param Argc Number of command arguments in Argv\r | |
387 | @param Argv Array of strings that represent the parsed command line. \r | |
388 | Argv[0] is the comamnd name\r | |
389 | \r | |
390 | @return EFI_SUCCESS\r | |
391 | \r | |
392 | **/\r | |
393 | EFI_STATUS\r | |
394 | EblLoadFvCmd (\r | |
395 | IN UINTN Argc,\r | |
396 | IN CHAR8 **Argv\r | |
397 | )\r | |
398 | {\r | |
399 | EFI_STATUS Status;\r | |
400 | EFI_OPEN_FILE *File;\r | |
401 | VOID *FvStart;\r | |
402 | UINTN FvSize;\r | |
403 | EFI_HANDLE FvHandle;\r | |
404 | \r | |
405 | \r | |
406 | if (Argc < 2) {\r | |
407 | return EFI_INVALID_PARAMETER;\r | |
408 | }\r | |
409 | \r | |
410 | File = EfiOpen (Argv[1], EFI_FILE_MODE_READ, 0);\r | |
411 | if (File == NULL) {\r | |
412 | return EFI_INVALID_PARAMETER; \r | |
413 | }\r | |
414 | \r | |
415 | if (File->Type == EfiOpenMemoryBuffer) {\r | |
416 | // If it is a address just use it.\r | |
417 | Status = gDS->ProcessFirmwareVolume (File->Buffer, File->Size, &FvHandle);\r | |
418 | } else {\r | |
419 | // If it is a file read it into memory and use it\r | |
420 | Status = EfiReadAllocatePool (File, &FvStart, &FvSize);\r | |
421 | EfiClose (File);\r | |
422 | if (EFI_ERROR (Status)) {\r | |
423 | return Status;\r | |
424 | }\r | |
425 | \r | |
426 | Status = gDS->ProcessFirmwareVolume (FvStart, FvSize, &FvHandle);\r | |
427 | FreePool (FvStart);\r | |
428 | }\r | |
429 | return Status;\r | |
430 | }\r | |
431 | \r | |
432 | \r | |
433 | /**\r | |
434 | Perform an EFI connect to connect devices that follow the EFI driver model. \r | |
435 | If it is a PI system also call the dispatcher in case a new FV was made\r | |
436 | availible by one of the connect EFI drivers (this is not a common case).\r | |
437 | \r | |
438 | Argv[0] - "connect"\r | |
439 | \r | |
440 | @param Argc Number of command arguments in Argv\r | |
441 | @param Argv Array of strings that represent the parsed command line. \r | |
442 | Argv[0] is the comamnd name\r | |
443 | \r | |
444 | @return EFI_SUCCESS\r | |
445 | \r | |
446 | **/\r | |
447 | EFI_STATUS\r | |
448 | EblConnectCmd (\r | |
449 | IN UINTN Argc,\r | |
450 | IN CHAR8 **Argv\r | |
451 | )\r | |
452 | {\r | |
453 | EFI_STATUS Status;\r | |
454 | UINTN HandleCount;\r | |
455 | EFI_HANDLE *HandleBuffer;\r | |
456 | UINTN Index;\r | |
457 | BOOLEAN Dispatch;\r | |
458 | EFI_OPEN_FILE *File;\r | |
459 | \r | |
460 | \r | |
461 | if (Argc > 1) {\r | |
462 | if ((*Argv[1] == 'd') || (*Argv[1] == 'D')) {\r | |
463 | Status = gBS->LocateHandleBuffer (\r | |
464 | AllHandles,\r | |
465 | NULL,\r | |
466 | NULL,\r | |
467 | &HandleCount,\r | |
468 | &HandleBuffer\r | |
469 | );\r | |
470 | if (EFI_ERROR (Status)) {\r | |
471 | return Status;\r | |
472 | }\r | |
473 | \r | |
474 | for (Index = 0; Index < HandleCount; Index++) {\r | |
475 | gBS->DisconnectController (HandleBuffer[Index], NULL, NULL);\r | |
476 | }\r | |
477 | \r | |
478 | //\r | |
479 | // Given we disconnect our console we should go and do a connect now\r | |
480 | //\r | |
481 | } else {\r | |
482 | File = EfiOpen (Argv[1], EFI_FILE_MODE_READ, 0);\r | |
483 | if (File != NULL) {\r | |
484 | AsciiPrint ("Connecting %a\n", Argv[1]);\r | |
485 | gBS->ConnectController (File->EfiHandle, NULL, NULL, TRUE);\r | |
486 | EfiClose (File);\r | |
487 | return EFI_SUCCESS;\r | |
488 | }\r | |
489 | }\r | |
490 | }\r | |
491 | \r | |
492 | Dispatch = FALSE;\r | |
493 | do {\r | |
494 | Status = gBS->LocateHandleBuffer (\r | |
495 | AllHandles,\r | |
496 | NULL,\r | |
497 | NULL,\r | |
498 | &HandleCount,\r | |
499 | &HandleBuffer\r | |
500 | );\r | |
501 | if (EFI_ERROR (Status)) {\r | |
502 | return Status;\r | |
503 | }\r | |
504 | \r | |
505 | for (Index = 0; Index < HandleCount; Index++) {\r | |
506 | gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);\r | |
507 | }\r | |
508 | \r | |
509 | FreePool (HandleBuffer);\r | |
510 | \r | |
511 | //\r | |
512 | // Check to see if it's possible to dispatch an more DXE drivers.\r | |
513 | // The BdsLibConnectAllEfi () may have made new DXE drivers show up.\r | |
514 | // If anything is Dispatched Status == EFI_SUCCESS and we will try\r | |
515 | // the connect again.\r | |
516 | //\r | |
517 | if (gDS == NULL) {\r | |
518 | Status = EFI_NOT_FOUND;\r | |
519 | } else {\r | |
520 | Status = gDS->Dispatch ();\r | |
521 | if (!EFI_ERROR (Status)) {\r | |
522 | Dispatch = TRUE;\r | |
523 | }\r | |
524 | }\r | |
525 | \r | |
526 | } while (!EFI_ERROR (Status));\r | |
527 | \r | |
528 | if (Dispatch) {\r | |
529 | AsciiPrint ("Connected and dispatched\n");\r | |
530 | } else {\r | |
531 | AsciiPrint ("Connect\n");\r | |
532 | }\r | |
533 | \r | |
534 | return EFI_SUCCESS;\r | |
535 | }\r | |
536 | \r | |
537 | \r | |
538 | \r | |
539 | CHAR8 *gMemMapType[] = {\r | |
540 | "reserved ",\r | |
541 | "LoaderCode",\r | |
542 | "LoaderData",\r | |
543 | "BS_code ",\r | |
544 | "BS_data ",\r | |
545 | "RT_code ",\r | |
546 | "RT_data ",\r | |
547 | "available ",\r | |
548 | "Unusable ",\r | |
549 | "ACPI_recl ",\r | |
550 | "ACPI_NVS ",\r | |
551 | "MemMapIO ",\r | |
552 | "MemPortIO ",\r | |
553 | "PAL_code "\r | |
554 | };\r | |
555 | \r | |
556 | \r | |
557 | /**\r | |
558 | Dump out the EFI memory map\r | |
559 | \r | |
560 | Argv[0] - "memmap"\r | |
561 | \r | |
562 | @param Argc Number of command arguments in Argv\r | |
563 | @param Argv Array of strings that represent the parsed command line. \r | |
564 | Argv[0] is the comamnd name\r | |
565 | \r | |
566 | @return EFI_SUCCESS\r | |
567 | \r | |
568 | **/\r | |
569 | EFI_STATUS\r | |
570 | EblMemMapCmd (\r | |
571 | IN UINTN Argc,\r | |
572 | IN CHAR8 **Argv\r | |
573 | )\r | |
574 | {\r | |
575 | EFI_STATUS Status;\r | |
576 | EFI_MEMORY_DESCRIPTOR *MemMap;\r | |
577 | EFI_MEMORY_DESCRIPTOR *OrigMemMap;\r | |
578 | UINTN MemMapSize;\r | |
579 | UINTN MapKey;\r | |
580 | UINTN DescriptorSize;\r | |
581 | UINT32 DescriptorVersion;\r | |
582 | UINT64 PageCount[EfiMaxMemoryType];\r | |
583 | UINTN Index;\r | |
584 | UINT64 EntrySize;\r | |
585 | UINTN CurrentRow;\r | |
586 | UINT64 TotalMemory;\r | |
587 | \r | |
588 | ZeroMem (PageCount, sizeof (PageCount));\r | |
589 | \r | |
590 | AsciiPrint ("EFI Memory Map\n");\r | |
591 | \r | |
592 | // First call is to figure out how big the buffer needs to be\r | |
593 | MemMapSize = 0;\r | |
594 | MemMap = NULL;\r | |
595 | Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, &DescriptorVersion);\r | |
596 | if (Status == EFI_BUFFER_TOO_SMALL) {\r | |
597 | // In case the AllocatPool changes the memory map we added in some extra descriptors\r | |
598 | MemMapSize += (DescriptorSize * 0x100);\r | |
599 | OrigMemMap = MemMap = AllocatePool (MemMapSize);\r | |
600 | if (OrigMemMap != NULL) {\r | |
601 | // 2nd time we get the data\r | |
602 | Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, &DescriptorVersion);\r | |
603 | if (!EFI_ERROR (Status)) {\r | |
604 | for (Index = 0, CurrentRow = 0; Index < MemMapSize/DescriptorSize; Index++) {\r | |
605 | EntrySize = LShiftU64 (MemMap->NumberOfPages, 12);\r | |
606 | AsciiPrint ("\n%a %016lx - %016lx: # %08lx %016lx", gMemMapType[MemMap->Type % EfiMaxMemoryType], MemMap->PhysicalStart, MemMap->PhysicalStart + EntrySize -1, MemMap->NumberOfPages, MemMap->Attribute);\r | |
607 | if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r | |
608 | break;\r | |
609 | }\r | |
610 | \r | |
611 | PageCount[MemMap->Type % EfiMaxMemoryType] += MemMap->NumberOfPages;\r | |
612 | MemMap = NEXT_MEMORY_DESCRIPTOR (MemMap, DescriptorSize);\r | |
613 | }\r | |
614 | }\r | |
615 | \r | |
616 | for (Index = 0, TotalMemory = 0; Index < EfiMaxMemoryType; Index++) {\r | |
617 | if (PageCount[Index] != 0) {\r | |
618 | AsciiPrint ("\n %a %,7ld Pages (%,14ld)", gMemMapType[Index], PageCount[Index], LShiftU64 (PageCount[Index], 12));\r | |
619 | if (Index == EfiLoaderCode ||\r | |
620 | Index == EfiLoaderData ||\r | |
621 | Index == EfiBootServicesCode ||\r | |
622 | Index == EfiBootServicesData ||\r | |
623 | Index == EfiRuntimeServicesCode ||\r | |
624 | Index == EfiRuntimeServicesData ||\r | |
625 | Index == EfiConventionalMemory ||\r | |
626 | Index == EfiACPIReclaimMemory ||\r | |
627 | Index == EfiACPIMemoryNVS ||\r | |
628 | Index == EfiPalCode\r | |
629 | ) {\r | |
630 | // Count total memory\r | |
631 | TotalMemory += PageCount[Index];\r | |
632 | }\r | |
633 | }\r | |
634 | }\r | |
635 | \r | |
636 | AsciiPrint ("\nTotal Memory: %,ld MB (%,ld bytes)\n", RShiftU64 (TotalMemory, 8), LShiftU64 (TotalMemory, 12));\r | |
637 | \r | |
638 | FreePool (OrigMemMap);\r | |
639 | \r | |
640 | }\r | |
641 | }\r | |
642 | \r | |
643 | return EFI_SUCCESS;\r | |
644 | }\r | |
645 | \r | |
646 | \r | |
647 | \r | |
648 | \r | |
649 | /**\r | |
650 | Load a file into memory and optionally jump to it. A load addres can be \r | |
651 | specified or automatically allocated. A quoted command line can optionally\r | |
652 | be passed into the image. \r | |
653 | \r | |
654 | Argv[0] - "go"\r | |
655 | Argv[1] - Device Name:path for the file to load\r | |
656 | Argv[2] - Address to load to or '*' if the load address will be allocated\r | |
657 | Argv[3] - Optional Entry point to the image. Image will be called if present\r | |
658 | Argv[4] - "" string that will be passed as Argc & Argv to EntryPoint. Needs \r | |
659 | to include the command name\r | |
660 | \r | |
661 | go fv1:\EblCmdX 0x10000 0x10010 "EblCmdX Arg2 Arg3 Arg4"; - load EblCmdX \r | |
662 | from FV1 to location 0x10000 and call the entry point at 0x10010 passing \r | |
663 | in "EblCmdX Arg2 Arg3 Arg4" as the arguments.\r | |
664 | \r | |
665 | go fv0:\EblCmdX * 0x10 "EblCmdX Arg2 Arg3 Arg4"; - load EblCmdX from FS0 \r | |
666 | to location allocated by this comamnd and call the entry point at offset 0x10 \r | |
667 | passing in "EblCmdX Arg2 Arg3 Arg4" as the arguments.\r | |
668 | \r | |
669 | go fv1:\EblCmdX 0x10000; Load EblCmdX to address 0x10000 and return\r | |
670 | \r | |
671 | @param Argc Number of command arguments in Argv\r | |
672 | @param Argv Array of strings that represent the parsed command line. \r | |
673 | Argv[0] is the comamnd name\r | |
674 | \r | |
675 | @return EFI_SUCCESS\r | |
676 | \r | |
677 | **/\r | |
678 | EFI_STATUS\r | |
679 | EblGoCmd (\r | |
680 | IN UINTN Argc,\r | |
681 | IN CHAR8 **Argv\r | |
682 | )\r | |
683 | {\r | |
684 | EFI_STATUS Status;\r | |
685 | EFI_OPEN_FILE *File;\r | |
686 | VOID *Address;\r | |
687 | UINTN Size;\r | |
688 | EBL_COMMMAND EntryPoint;\r | |
689 | UINTN EntryPointArgc;\r | |
690 | CHAR8 *EntryPointArgv[MAX_ARGS];\r | |
691 | \r | |
692 | \r | |
693 | if (Argc <= 2) {\r | |
694 | // device name and laod address are required\r | |
695 | return EFI_SUCCESS;\r | |
696 | }\r | |
697 | \r | |
698 | File = EfiOpen (Argv[1], EFI_FILE_MODE_READ, 0);\r | |
699 | if (File == NULL) {\r | |
700 | AsciiPrint (" %a is not a valid path\n", Argv[1]);\r | |
701 | return EFI_SUCCESS;\r | |
702 | }\r | |
703 | \r | |
704 | EntryPoint = (EBL_COMMMAND)((Argc > 3) ? (UINTN)AsciiStrHexToUintn (Argv[3]) : (UINTN)NULL);\r | |
705 | if (Argv[2][0] == '*') {\r | |
706 | // * Means allocate the buffer\r | |
707 | Status = EfiReadAllocatePool (File, &Address, &Size);\r | |
708 | \r | |
709 | // EntryPoint is relatvie to the start of the image \r | |
710 | EntryPoint = (EBL_COMMMAND)((UINTN)EntryPoint + (UINTN)Address);\r | |
711 | \r | |
712 | } else {\r | |
713 | Address = (VOID *)AsciiStrHexToUintn (Argv[2]);\r | |
714 | Size = File->Size;\r | |
715 | \r | |
716 | // File->Size for LoadFile is lazy so we need to use the tell to figure it out\r | |
717 | EfiTell (File, NULL);\r | |
718 | Status = EfiRead (File, Address, &Size);\r | |
719 | }\r | |
720 | \r | |
721 | if (!EFI_ERROR (Status)) {\r | |
722 | AsciiPrint ("Loaded %,d bytes to 0x%08x\n", Size, Address);\r | |
723 | \r | |
724 | if (Argc > 3) {\r | |
725 | if (Argc > 4) {\r | |
726 | ParseArguments (Argv[4], &EntryPointArgc, EntryPointArgv);\r | |
727 | } else {\r | |
728 | EntryPointArgc = 1;\r | |
729 | EntryPointArgv[0] = File->FileName;\r | |
730 | }\r | |
731 | \r | |
732 | Status = EntryPoint (EntryPointArgc, EntryPointArgv);\r | |
733 | }\r | |
734 | }\r | |
735 | \r | |
736 | EfiClose (File);\r | |
737 | return Status;\r | |
738 | }\r | |
739 | \r | |
740 | #define FILE_COPY_CHUNK 0x20000\r | |
741 | \r | |
742 | EFI_STATUS\r | |
743 | EblFileCopyCmd (\r | |
744 | IN UINTN Argc,\r | |
745 | IN CHAR8 **Argv\r | |
746 | )\r | |
747 | {\r | |
748 | EFI_OPEN_FILE *Source = NULL;\r | |
749 | EFI_OPEN_FILE *Destination = NULL;\r | |
750 | EFI_STATUS Status = EFI_SUCCESS;\r | |
751 | VOID *Buffer = NULL;\r | |
752 | UINTN Size;\r | |
753 | UINTN Offset;\r | |
754 | UINTN Chunk = FILE_COPY_CHUNK;\r | |
f6381f4c | 755 | \r |
2ef2b01e A |
756 | if (Argc < 3) {\r |
757 | return EFI_INVALID_PARAMETER;\r | |
758 | }\r | |
759 | \r | |
760 | Source = EfiOpen(Argv[1], EFI_FILE_MODE_READ, 0);\r | |
761 | if (Source == NULL) {\r | |
762 | AsciiPrint("Source file open error.\n");\r | |
763 | return EFI_NOT_FOUND;\r | |
764 | }\r | |
765 | \r | |
766 | Destination = EfiOpen(Argv[2], EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0);\r | |
767 | if (Destination == NULL) {\r | |
768 | AsciiPrint("Destination file open error.\n");\r | |
769 | return EFI_NOT_FOUND;\r | |
770 | }\r | |
771 | \r | |
772 | Buffer = AllocatePool(FILE_COPY_CHUNK);\r | |
773 | if (Buffer == NULL) {\r | |
774 | goto Exit;\r | |
775 | }\r | |
776 | \r | |
777 | Size = EfiTell(Source, NULL);\r | |
778 | \r | |
779 | for (Offset = 0; Offset + FILE_COPY_CHUNK <= Size; Offset += Chunk) {\r | |
780 | Chunk = FILE_COPY_CHUNK;\r | |
781 | \r | |
782 | Status = EfiRead(Source, Buffer, &Chunk);\r | |
783 | if (EFI_ERROR(Status)) {\r | |
f6381f4c | 784 | AsciiPrint("Read file error %r\n", Status);\r |
2ef2b01e A |
785 | goto Exit;\r |
786 | }\r | |
787 | \r | |
788 | Status = EfiWrite(Destination, Buffer, &Chunk);\r | |
789 | if (EFI_ERROR(Status)) {\r | |
f6381f4c | 790 | AsciiPrint("Write file error %r\n", Status);\r |
2ef2b01e A |
791 | goto Exit;\r |
792 | } \r | |
793 | }\r | |
794 | \r | |
795 | // Any left over?\r | |
796 | if (Offset < Size) {\r | |
797 | Chunk = Size - Offset;\r | |
798 | \r | |
799 | Status = EfiRead(Source, Buffer, &Chunk);\r | |
800 | if (EFI_ERROR(Status)) {\r | |
f6381f4c | 801 | AsciiPrint("Read file error %r\n", Status);\r |
2ef2b01e A |
802 | goto Exit;\r |
803 | }\r | |
804 | \r | |
805 | Status = EfiWrite(Destination, Buffer, &Chunk);\r | |
806 | if (EFI_ERROR(Status)) {\r | |
f6381f4c | 807 | AsciiPrint("Write file error %r\n", Status);\r |
2ef2b01e A |
808 | goto Exit;\r |
809 | } \r | |
810 | }\r | |
811 | \r | |
f6381f4c | 812 | \r |
2ef2b01e A |
813 | Exit:\r |
814 | if (Source != NULL) {\r | |
815 | Status = EfiClose(Source);\r | |
816 | if (EFI_ERROR(Status)) {\r | |
817 | AsciiPrint("Source close error %r\n", Status);\r | |
818 | }\r | |
819 | }\r | |
2ef2b01e A |
820 | if (Destination != NULL) {\r |
821 | Status = EfiClose(Destination);\r | |
822 | if (EFI_ERROR(Status)) {\r | |
823 | AsciiPrint("Destination close error %r\n", Status);\r | |
824 | }\r | |
825 | }\r | |
826 | \r | |
827 | if (Buffer != NULL) {\r | |
828 | FreePool(Buffer);\r | |
829 | }\r | |
830 | \r | |
831 | return Status;\r | |
832 | }\r | |
833 | \r | |
834 | EFI_STATUS\r | |
835 | EblFileDiffCmd (\r | |
836 | IN UINTN Argc,\r | |
837 | IN CHAR8 **Argv\r | |
838 | )\r | |
839 | {\r | |
840 | EFI_OPEN_FILE *File1 = NULL;\r | |
841 | EFI_OPEN_FILE *File2 = NULL;\r | |
842 | EFI_STATUS Status = EFI_SUCCESS;\r | |
843 | VOID *Buffer1 = NULL;\r | |
844 | VOID *Buffer2 = NULL;\r | |
845 | UINTN Size1;\r | |
846 | UINTN Size2;\r | |
847 | UINTN Offset;\r | |
848 | UINTN Chunk = FILE_COPY_CHUNK;\r | |
849 | \r | |
850 | if (Argc != 3) {\r | |
851 | return EFI_INVALID_PARAMETER;\r | |
852 | }\r | |
853 | \r | |
854 | File1 = EfiOpen(Argv[1], EFI_FILE_MODE_READ, 0);\r | |
855 | if (File1 == NULL) {\r | |
856 | AsciiPrint("File 1 open error.\n");\r | |
857 | return EFI_NOT_FOUND;\r | |
858 | }\r | |
859 | \r | |
860 | File2 = EfiOpen(Argv[2], EFI_FILE_MODE_READ, 0);\r | |
861 | if (File2 == NULL) {\r | |
862 | AsciiPrint("File 2 open error.\n");\r | |
863 | return EFI_NOT_FOUND;\r | |
864 | }\r | |
865 | \r | |
866 | Size1 = EfiTell(File1, NULL);\r | |
867 | Size2 = EfiTell(File2, NULL);\r | |
868 | \r | |
869 | if (Size1 != Size2) {\r | |
870 | AsciiPrint("Files differ.\n");\r | |
871 | goto Exit;\r | |
872 | }\r | |
873 | \r | |
874 | Buffer1 = AllocatePool(FILE_COPY_CHUNK);\r | |
875 | if (Buffer1 == NULL) {\r | |
876 | goto Exit;\r | |
877 | }\r | |
878 | \r | |
879 | Buffer2 = AllocatePool(FILE_COPY_CHUNK);\r | |
880 | if (Buffer2 == NULL) {\r | |
881 | goto Exit;\r | |
882 | } \r | |
883 | \r | |
884 | for (Offset = 0; Offset + FILE_COPY_CHUNK <= Size1; Offset += Chunk) {\r | |
885 | Chunk = FILE_COPY_CHUNK;\r | |
886 | \r | |
887 | Status = EfiRead(File1, Buffer1, &Chunk);\r | |
888 | if (EFI_ERROR(Status)) {\r | |
889 | AsciiPrint("File 1 read error\n");\r | |
890 | goto Exit;\r | |
891 | }\r | |
892 | \r | |
893 | Status = EfiRead(File2, Buffer2, &Chunk);\r | |
894 | if (EFI_ERROR(Status)) {\r | |
895 | AsciiPrint("File 2 read error\n");\r | |
896 | goto Exit;\r | |
897 | }\r | |
898 | \r | |
899 | if (CompareMem(Buffer1, Buffer2, Chunk) != 0) {\r | |
900 | AsciiPrint("Files differ.\n");\r | |
901 | goto Exit;\r | |
902 | };\r | |
903 | }\r | |
904 | \r | |
905 | // Any left over?\r | |
906 | if (Offset < Size1) {\r | |
907 | Chunk = Size1 - Offset;\r | |
908 | \r | |
909 | Status = EfiRead(File1, Buffer1, &Chunk);\r | |
910 | if (EFI_ERROR(Status)) {\r | |
911 | AsciiPrint("File 1 read error\n");\r | |
912 | goto Exit;\r | |
913 | }\r | |
914 | \r | |
915 | Status = EfiRead(File2, Buffer2, &Chunk);\r | |
916 | if (EFI_ERROR(Status)) {\r | |
917 | AsciiPrint("File 2 read error\n");\r | |
918 | goto Exit;\r | |
919 | } \r | |
920 | }\r | |
921 | \r | |
922 | if (CompareMem(Buffer1, Buffer2, Chunk) != 0) {\r | |
923 | AsciiPrint("Files differ.\n");\r | |
924 | } else {\r | |
925 | AsciiPrint("Files are identical.\n");\r | |
926 | }\r | |
927 | \r | |
928 | Exit:\r | |
929 | if (File1 != NULL) {\r | |
930 | Status = EfiClose(File1);\r | |
931 | if (EFI_ERROR(Status)) {\r | |
932 | AsciiPrint("File 1 close error %r\n", Status);\r | |
933 | }\r | |
934 | }\r | |
935 | \r | |
936 | if (File2 != NULL) {\r | |
937 | Status = EfiClose(File2);\r | |
938 | if (EFI_ERROR(Status)) {\r | |
939 | AsciiPrint("File 2 close error %r\n", Status);\r | |
940 | }\r | |
941 | }\r | |
942 | \r | |
943 | if (Buffer1 != NULL) {\r | |
944 | FreePool(Buffer1);\r | |
945 | }\r | |
946 | \r | |
947 | if (Buffer2 != NULL) {\r | |
948 | FreePool(Buffer2);\r | |
949 | }\r | |
950 | \r | |
951 | return Status;\r | |
952 | }\r | |
953 | \r | |
954 | GLOBAL_REMOVE_IF_UNREFERENCED const EBL_COMMAND_TABLE mCmdDeviceTemplate[] =\r | |
955 | {\r | |
956 | {\r | |
957 | "connect",\r | |
958 | "[d]; Connect all EFI devices. d means disconnect",\r | |
959 | NULL,\r | |
960 | EblConnectCmd\r | |
961 | },\r | |
962 | {\r | |
963 | "device",\r | |
964 | "; Show information about boot devices",\r | |
965 | NULL,\r | |
966 | EblDeviceCmd\r | |
967 | },\r | |
968 | {\r | |
969 | "go",\r | |
970 | " dev:path loadaddress entrypoint args; load to given address and jump in", \r | |
971 | NULL,\r | |
972 | EblGoCmd\r | |
973 | },\r | |
974 | {\r | |
975 | "loadfv",\r | |
976 | " devname; Load PI FV from device", \r | |
977 | NULL,\r | |
978 | EblLoadFvCmd\r | |
979 | },\r | |
980 | {\r | |
981 | "start",\r | |
982 | " path; EFI Boot Device:filepath. fs1:\\EFI\\BOOT.EFI", \r | |
983 | NULL,\r | |
984 | EblStartCmd\r | |
985 | },\r | |
986 | {\r | |
987 | "memmap",\r | |
988 | "; dump EFI memory map", \r | |
989 | NULL,\r | |
990 | EblMemMapCmd\r | |
991 | },\r | |
992 | {\r | |
993 | "cp",\r | |
994 | " file1 file2; copy file",\r | |
995 | NULL,\r | |
996 | EblFileCopyCmd\r | |
997 | },\r | |
998 | {\r | |
999 | "diff",\r | |
1000 | " file1 file2; compare files",\r | |
1001 | NULL,\r | |
1002 | EblFileDiffCmd\r | |
1003 | }\r | |
1004 | };\r | |
1005 | \r | |
1006 | \r | |
1007 | /**\r | |
1008 | Initialize the commands in this in this file\r | |
1009 | **/\r | |
1010 | \r | |
1011 | VOID\r | |
1012 | EblInitializeDeviceCmd (\r | |
1013 | VOID\r | |
1014 | )\r | |
1015 | {\r | |
1016 | EfiGetSystemConfigurationTable (&gEfiDxeServicesTableGuid, (VOID **) &gDS);\r | |
1017 | EblAddCommands (mCmdDeviceTemplate, sizeof (mCmdDeviceTemplate)/sizeof (EBL_COMMAND_TABLE));\r | |
1018 | }\r | |
1019 | \r |