]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Ebl/EfiDevice.c
Added a PCD to turn on/off probing Block IO devices to detect add/remove/change....
[mirror_edk2.git] / EmbeddedPkg / Ebl / EfiDevice.c
CommitLineData
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
20EFI_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
29VOID\r
30EblPrintFsInfo (\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
66VOID\r
67EblPrintFvbInfo (\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
87VOID\r
88EblPrintBlkIoInfo (\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
142VOID\r
143EblPrintLoadFileInfo (\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
204EFI_STATUS\r
205EblDeviceCmd (\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
213\r
214 CurrentRow = 0;\r
208a8330 215\r
2ef2b01e
A
216 // Need to call here to make sure Device Counts are valid\r
217 EblUpdateDeviceLists ();\r
218\r
f6381f4c 219 // Now we can print out the info...\r
2ef2b01e
A
220 Max = EfiGetDeviceCounts (EfiOpenFirmwareVolume);\r
221 if (Max != 0) {\r
222 AsciiPrint ("Firmware Volume Devices:\n");\r
223 for (Index = 0; Index < Max; Index++) {\r
224 EblPrintFvbInfo (EfiDeviceOpenByType (EfiOpenFirmwareVolume, Index));\r
225 if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r
226 break;\r
227 }\r
228 }\r
229 }\r
230\r
231 Max = EfiGetDeviceCounts (EfiOpenFileSystem);\r
232 if (Max != 0) {\r
233 AsciiPrint ("File System Devices:\n");\r
234 for (Index = 0; Index < Max; Index++) {\r
235 EblPrintFsInfo (EfiDeviceOpenByType (EfiOpenFileSystem, Index));\r
236 if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r
237 break;\r
238 }\r
239 }\r
240 }\r
241\r
242 Max = EfiGetDeviceCounts (EfiOpenBlockIo);\r
243 if (Max != 0) {\r
244 AsciiPrint ("Block IO Devices:\n");\r
245 for (Index = 0; Index < Max; Index++) {\r
246 EblPrintBlkIoInfo (EfiDeviceOpenByType (EfiOpenBlockIo, Index));\r
247 if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r
248 break;\r
249 }\r
250 }\r
251 }\r
252 \r
253 Max = EfiGetDeviceCounts (EfiOpenLoadFile);\r
254 if (Max != 0) {\r
255 AsciiPrint ("LoadFile Devices: (usually network)\n");\r
256 for (Index = 0; Index < Max; Index++) {\r
257 EblPrintLoadFileInfo (EfiDeviceOpenByType (EfiOpenLoadFile, Index));\r
258 if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r
259 break;\r
260 }\r
261 }\r
262 }\r
263\r
264 return EFI_SUCCESS;\r
265}\r
266\r
267\r
268/**\r
269 Start an EFI image (PE32+ with EFI defined entry point). \r
270\r
271 Argv[0] - "start"\r
272 Argv[1] - device name and path\r
273 Argv[2] - "" string to pass into image being started\r
274\r
275 start fs1:\Temp\Fv.Fv "arg to pass" ; load an FV from the disk and pass the\r
276 ; ascii string arg to pass to the image\r
277 start fv0:\FV ; load an FV from an FV (not common)\r
278 start LoadFile0: ; load an FV via a PXE boot\r
279\r
280 @param Argc Number of command arguments in Argv\r
281 @param Argv Array of strings that represent the parsed command line. \r
282 Argv[0] is the comamnd name\r
283\r
284 @return EFI_SUCCESS\r
285\r
286**/\r
287EFI_STATUS\r
288EblStartCmd (\r
289 IN UINTN Argc,\r
290 IN CHAR8 **Argv\r
291 )\r
292{\r
293 EFI_STATUS Status;\r
294 EFI_OPEN_FILE *File;\r
295 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
296 EFI_HANDLE ImageHandle;\r
297 UINTN ExitDataSize;\r
298 CHAR16 *ExitData;\r
299 VOID *Buffer;\r
300 UINTN BufferSize;\r
301 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r
302\r
303 ImageHandle = NULL;\r
304\r
305 if (Argc < 2) {\r
306 return EFI_INVALID_PARAMETER;\r
307 }\r
308\r
309 File = EfiOpen (Argv[1], EFI_FILE_MODE_READ, 0);\r
310 if (File == NULL) {\r
311 return EFI_INVALID_PARAMETER; \r
312 }\r
313\r
314 DevicePath = File->DevicePath;\r
315 if (DevicePath != NULL) {\r
316 // check for device path form: blk, fv, fs, and loadfile\r
317 Status = gBS->LoadImage (FALSE, gImageHandle, DevicePath, NULL, 0, &ImageHandle);\r
318 } else {\r
319 // Check for buffer form: A0x12345678:0x1234 syntax.\r
320 // Means load using buffer starting at 0x12345678 of size 0x1234.\r
321\r
322 Status = EfiReadAllocatePool (File, &Buffer, &BufferSize);\r
323 if (EFI_ERROR (Status)) {\r
324 EfiClose (File);\r
325 return Status;\r
326 }\r
327 Status = gBS->LoadImage (FALSE, gImageHandle, DevicePath, Buffer, BufferSize, &ImageHandle);\r
328\r
329 FreePool (Buffer);\r
330 }\r
331 \r
332 EfiClose (File);\r
333\r
334 if (!EFI_ERROR (Status)) {\r
335 if (Argc >= 3) {\r
336 // Argv[2] is a "" string that we pass directly to the EFI application without the ""\r
337 // We don't pass Argv[0] to the EFI Application (it's name) just the args\r
338 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&ImageInfo);\r
339 ASSERT_EFI_ERROR (Status);\r
340 \r
341 ImageInfo->LoadOptionsSize = (UINT32)AsciiStrSize (Argv[2]);\r
342 ImageInfo->LoadOptions = AllocatePool (ImageInfo->LoadOptionsSize);\r
343 AsciiStrCpy (ImageInfo->LoadOptions, Argv[2]);\r
344 }\r
345\r
346 // Transfer control to the EFI image we loaded with LoadImage()\r
347 Status = gBS->StartImage (ImageHandle, &ExitDataSize, &ExitData);\r
348 }\r
349\r
350 return Status;\r
351}\r
352\r
353\r
354/**\r
355 Load a Firmware Volume (FV) into memory from a device. This causes drivers in\r
356 the FV to be dispatched if the dependancies of the drivers are met.\r
357 \r
358 Argv[0] - "loadfv"\r
359 Argv[1] - device name and path\r
360\r
361 loadfv fs1:\Temp\Fv.Fv ; load an FV from the disk\r
362 loadfv fv0:\FV ; load an FV from an FV (not common)\r
363 loadfv LoadFile0: ; load an FV via a PXE boot\r
364\r
365 @param Argc Number of command arguments in Argv\r
366 @param Argv Array of strings that represent the parsed command line. \r
367 Argv[0] is the comamnd name\r
368\r
369 @return EFI_SUCCESS\r
370\r
371**/\r
372EFI_STATUS\r
373EblLoadFvCmd (\r
374 IN UINTN Argc,\r
375 IN CHAR8 **Argv\r
376 )\r
377{\r
378 EFI_STATUS Status;\r
379 EFI_OPEN_FILE *File;\r
380 VOID *FvStart;\r
381 UINTN FvSize;\r
382 EFI_HANDLE FvHandle;\r
383\r
384\r
385 if (Argc < 2) {\r
386 return EFI_INVALID_PARAMETER;\r
387 }\r
388\r
389 File = EfiOpen (Argv[1], EFI_FILE_MODE_READ, 0);\r
390 if (File == NULL) {\r
391 return EFI_INVALID_PARAMETER; \r
392 }\r
393\r
394 if (File->Type == EfiOpenMemoryBuffer) {\r
395 // If it is a address just use it.\r
396 Status = gDS->ProcessFirmwareVolume (File->Buffer, File->Size, &FvHandle);\r
397 } else {\r
398 // If it is a file read it into memory and use it\r
399 Status = EfiReadAllocatePool (File, &FvStart, &FvSize);\r
400 EfiClose (File);\r
401 if (EFI_ERROR (Status)) {\r
402 return Status;\r
403 }\r
404 \r
405 Status = gDS->ProcessFirmwareVolume (FvStart, FvSize, &FvHandle);\r
406 FreePool (FvStart);\r
407 }\r
408 return Status;\r
409}\r
410\r
411\r
412/**\r
413 Perform an EFI connect to connect devices that follow the EFI driver model. \r
414 If it is a PI system also call the dispatcher in case a new FV was made\r
415 availible by one of the connect EFI drivers (this is not a common case).\r
416 \r
417 Argv[0] - "connect"\r
418\r
419 @param Argc Number of command arguments in Argv\r
420 @param Argv Array of strings that represent the parsed command line. \r
421 Argv[0] is the comamnd name\r
422\r
423 @return EFI_SUCCESS\r
424\r
425**/\r
426EFI_STATUS\r
427EblConnectCmd (\r
428 IN UINTN Argc,\r
429 IN CHAR8 **Argv\r
430 )\r
431{\r
432 EFI_STATUS Status;\r
433 UINTN HandleCount;\r
434 EFI_HANDLE *HandleBuffer;\r
435 UINTN Index;\r
436 BOOLEAN Dispatch;\r
437 EFI_OPEN_FILE *File;\r
438\r
439\r
440 if (Argc > 1) {\r
441 if ((*Argv[1] == 'd') || (*Argv[1] == 'D')) {\r
442 Status = gBS->LocateHandleBuffer (\r
443 AllHandles,\r
444 NULL,\r
445 NULL,\r
446 &HandleCount,\r
447 &HandleBuffer\r
448 );\r
449 if (EFI_ERROR (Status)) {\r
450 return Status;\r
451 }\r
452 \r
453 for (Index = 0; Index < HandleCount; Index++) {\r
454 gBS->DisconnectController (HandleBuffer[Index], NULL, NULL);\r
455 }\r
456 \r
457 //\r
458 // Given we disconnect our console we should go and do a connect now\r
459 //\r
460 } else {\r
461 File = EfiOpen (Argv[1], EFI_FILE_MODE_READ, 0);\r
462 if (File != NULL) {\r
463 AsciiPrint ("Connecting %a\n", Argv[1]);\r
464 gBS->ConnectController (File->EfiHandle, NULL, NULL, TRUE);\r
465 EfiClose (File);\r
466 return EFI_SUCCESS;\r
467 }\r
468 }\r
469 }\r
470\r
471 Dispatch = FALSE;\r
472 do {\r
473 Status = gBS->LocateHandleBuffer (\r
474 AllHandles,\r
475 NULL,\r
476 NULL,\r
477 &HandleCount,\r
478 &HandleBuffer\r
479 );\r
480 if (EFI_ERROR (Status)) {\r
481 return Status;\r
482 }\r
483\r
484 for (Index = 0; Index < HandleCount; Index++) {\r
485 gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);\r
486 }\r
487\r
488 FreePool (HandleBuffer);\r
489\r
490 //\r
491 // Check to see if it's possible to dispatch an more DXE drivers.\r
492 // The BdsLibConnectAllEfi () may have made new DXE drivers show up.\r
493 // If anything is Dispatched Status == EFI_SUCCESS and we will try\r
494 // the connect again.\r
495 //\r
496 if (gDS == NULL) {\r
497 Status = EFI_NOT_FOUND;\r
498 } else {\r
499 Status = gDS->Dispatch ();\r
500 if (!EFI_ERROR (Status)) {\r
501 Dispatch = TRUE;\r
502 }\r
503 }\r
504\r
505 } while (!EFI_ERROR (Status));\r
506\r
507 if (Dispatch) {\r
508 AsciiPrint ("Connected and dispatched\n");\r
509 } else {\r
510 AsciiPrint ("Connect\n");\r
511 }\r
512\r
513 return EFI_SUCCESS;\r
514}\r
515\r
516\r
517\r
518CHAR8 *gMemMapType[] = {\r
519 "reserved ",\r
520 "LoaderCode",\r
521 "LoaderData",\r
522 "BS_code ",\r
523 "BS_data ",\r
524 "RT_code ",\r
525 "RT_data ",\r
526 "available ",\r
527 "Unusable ",\r
528 "ACPI_recl ",\r
529 "ACPI_NVS ",\r
530 "MemMapIO ",\r
531 "MemPortIO ",\r
532 "PAL_code "\r
533};\r
534\r
535\r
536/**\r
537 Dump out the EFI memory map\r
538 \r
539 Argv[0] - "memmap"\r
540\r
541 @param Argc Number of command arguments in Argv\r
542 @param Argv Array of strings that represent the parsed command line. \r
543 Argv[0] is the comamnd name\r
544\r
545 @return EFI_SUCCESS\r
546\r
547**/\r
548EFI_STATUS\r
549EblMemMapCmd (\r
550 IN UINTN Argc,\r
551 IN CHAR8 **Argv\r
552 )\r
553{\r
554 EFI_STATUS Status;\r
555 EFI_MEMORY_DESCRIPTOR *MemMap;\r
556 EFI_MEMORY_DESCRIPTOR *OrigMemMap;\r
557 UINTN MemMapSize;\r
558 UINTN MapKey;\r
559 UINTN DescriptorSize;\r
560 UINT32 DescriptorVersion;\r
561 UINT64 PageCount[EfiMaxMemoryType];\r
562 UINTN Index;\r
563 UINT64 EntrySize;\r
564 UINTN CurrentRow;\r
565 UINT64 TotalMemory;\r
566\r
567 ZeroMem (PageCount, sizeof (PageCount));\r
568\r
569 AsciiPrint ("EFI Memory Map\n");\r
570 \r
571 // First call is to figure out how big the buffer needs to be\r
572 MemMapSize = 0;\r
573 MemMap = NULL;\r
574 Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, &DescriptorVersion);\r
575 if (Status == EFI_BUFFER_TOO_SMALL) {\r
576 // In case the AllocatPool changes the memory map we added in some extra descriptors\r
577 MemMapSize += (DescriptorSize * 0x100);\r
578 OrigMemMap = MemMap = AllocatePool (MemMapSize);\r
579 if (OrigMemMap != NULL) {\r
580 // 2nd time we get the data\r
581 Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, &DescriptorVersion);\r
582 if (!EFI_ERROR (Status)) {\r
583 for (Index = 0, CurrentRow = 0; Index < MemMapSize/DescriptorSize; Index++) {\r
584 EntrySize = LShiftU64 (MemMap->NumberOfPages, 12);\r
585 AsciiPrint ("\n%a %016lx - %016lx: # %08lx %016lx", gMemMapType[MemMap->Type % EfiMaxMemoryType], MemMap->PhysicalStart, MemMap->PhysicalStart + EntrySize -1, MemMap->NumberOfPages, MemMap->Attribute);\r
586 if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r
587 break;\r
588 }\r
589\r
590 PageCount[MemMap->Type % EfiMaxMemoryType] += MemMap->NumberOfPages;\r
591 MemMap = NEXT_MEMORY_DESCRIPTOR (MemMap, DescriptorSize);\r
592 }\r
593 }\r
594 \r
595 for (Index = 0, TotalMemory = 0; Index < EfiMaxMemoryType; Index++) {\r
596 if (PageCount[Index] != 0) {\r
597 AsciiPrint ("\n %a %,7ld Pages (%,14ld)", gMemMapType[Index], PageCount[Index], LShiftU64 (PageCount[Index], 12));\r
598 if (Index == EfiLoaderCode ||\r
599 Index == EfiLoaderData ||\r
600 Index == EfiBootServicesCode ||\r
601 Index == EfiBootServicesData ||\r
602 Index == EfiRuntimeServicesCode ||\r
603 Index == EfiRuntimeServicesData ||\r
604 Index == EfiConventionalMemory ||\r
605 Index == EfiACPIReclaimMemory ||\r
606 Index == EfiACPIMemoryNVS ||\r
607 Index == EfiPalCode\r
608 ) {\r
609 // Count total memory\r
610 TotalMemory += PageCount[Index];\r
611 }\r
612 }\r
613 }\r
614\r
615 AsciiPrint ("\nTotal Memory: %,ld MB (%,ld bytes)\n", RShiftU64 (TotalMemory, 8), LShiftU64 (TotalMemory, 12));\r
616\r
617 FreePool (OrigMemMap);\r
618\r
619 }\r
620 }\r
621\r
622 return EFI_SUCCESS;\r
623}\r
624\r
625\r
626\r
627\r
628/**\r
629 Load a file into memory and optionally jump to it. A load addres can be \r
630 specified or automatically allocated. A quoted command line can optionally\r
631 be passed into the image. \r
632\r
633 Argv[0] - "go"\r
634 Argv[1] - Device Name:path for the file to load\r
635 Argv[2] - Address to load to or '*' if the load address will be allocated\r
636 Argv[3] - Optional Entry point to the image. Image will be called if present\r
637 Argv[4] - "" string that will be passed as Argc & Argv to EntryPoint. Needs \r
638 to include the command name\r
639\r
640 go fv1:\EblCmdX 0x10000 0x10010 "EblCmdX Arg2 Arg3 Arg4"; - load EblCmdX \r
641 from FV1 to location 0x10000 and call the entry point at 0x10010 passing \r
642 in "EblCmdX Arg2 Arg3 Arg4" as the arguments.\r
643\r
644 go fv0:\EblCmdX * 0x10 "EblCmdX Arg2 Arg3 Arg4"; - load EblCmdX from FS0 \r
645 to location allocated by this comamnd and call the entry point at offset 0x10 \r
646 passing in "EblCmdX Arg2 Arg3 Arg4" as the arguments.\r
647\r
648 go fv1:\EblCmdX 0x10000; Load EblCmdX to address 0x10000 and return\r
649\r
650 @param Argc Number of command arguments in Argv\r
651 @param Argv Array of strings that represent the parsed command line. \r
652 Argv[0] is the comamnd name\r
653\r
654 @return EFI_SUCCESS\r
655\r
656**/\r
657EFI_STATUS\r
658EblGoCmd (\r
659 IN UINTN Argc,\r
660 IN CHAR8 **Argv\r
661 )\r
662{\r
663 EFI_STATUS Status;\r
664 EFI_OPEN_FILE *File;\r
665 VOID *Address;\r
666 UINTN Size;\r
667 EBL_COMMMAND EntryPoint;\r
668 UINTN EntryPointArgc;\r
669 CHAR8 *EntryPointArgv[MAX_ARGS];\r
670 \r
671\r
672 if (Argc <= 2) {\r
673 // device name and laod address are required\r
674 return EFI_SUCCESS;\r
675 }\r
676\r
677 File = EfiOpen (Argv[1], EFI_FILE_MODE_READ, 0);\r
678 if (File == NULL) {\r
679 AsciiPrint (" %a is not a valid path\n", Argv[1]);\r
680 return EFI_SUCCESS;\r
681 }\r
682\r
683 EntryPoint = (EBL_COMMMAND)((Argc > 3) ? (UINTN)AsciiStrHexToUintn (Argv[3]) : (UINTN)NULL);\r
684 if (Argv[2][0] == '*') {\r
685 // * Means allocate the buffer\r
686 Status = EfiReadAllocatePool (File, &Address, &Size);\r
687 \r
688 // EntryPoint is relatvie to the start of the image \r
689 EntryPoint = (EBL_COMMMAND)((UINTN)EntryPoint + (UINTN)Address);\r
690\r
691 } else {\r
692 Address = (VOID *)AsciiStrHexToUintn (Argv[2]);\r
693 Size = File->Size;\r
694\r
695 // File->Size for LoadFile is lazy so we need to use the tell to figure it out\r
696 EfiTell (File, NULL);\r
697 Status = EfiRead (File, Address, &Size);\r
698 }\r
699\r
700 if (!EFI_ERROR (Status)) {\r
701 AsciiPrint ("Loaded %,d bytes to 0x%08x\n", Size, Address);\r
702\r
703 if (Argc > 3) {\r
704 if (Argc > 4) {\r
705 ParseArguments (Argv[4], &EntryPointArgc, EntryPointArgv);\r
706 } else {\r
707 EntryPointArgc = 1;\r
708 EntryPointArgv[0] = File->FileName;\r
709 }\r
710 \r
711 Status = EntryPoint (EntryPointArgc, EntryPointArgv);\r
712 }\r
713 }\r
714\r
715 EfiClose (File);\r
716 return Status;\r
717}\r
718\r
719#define FILE_COPY_CHUNK 0x20000\r
720\r
721EFI_STATUS\r
722EblFileCopyCmd (\r
723 IN UINTN Argc,\r
724 IN CHAR8 **Argv\r
725 )\r
726{\r
727 EFI_OPEN_FILE *Source = NULL;\r
728 EFI_OPEN_FILE *Destination = NULL;\r
729 EFI_STATUS Status = EFI_SUCCESS;\r
730 VOID *Buffer = NULL;\r
731 UINTN Size;\r
732 UINTN Offset;\r
733 UINTN Chunk = FILE_COPY_CHUNK;\r
f6381f4c 734\r
2ef2b01e
A
735 if (Argc < 3) {\r
736 return EFI_INVALID_PARAMETER;\r
737 }\r
738 \r
739 Source = EfiOpen(Argv[1], EFI_FILE_MODE_READ, 0);\r
740 if (Source == NULL) {\r
741 AsciiPrint("Source file open error.\n");\r
742 return EFI_NOT_FOUND;\r
743 }\r
744 \r
745 Destination = EfiOpen(Argv[2], EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0);\r
746 if (Destination == NULL) {\r
747 AsciiPrint("Destination file open error.\n");\r
748 return EFI_NOT_FOUND;\r
749 }\r
750\r
751 Buffer = AllocatePool(FILE_COPY_CHUNK);\r
752 if (Buffer == NULL) {\r
753 goto Exit;\r
754 }\r
755 \r
756 Size = EfiTell(Source, NULL);\r
757\r
758 for (Offset = 0; Offset + FILE_COPY_CHUNK <= Size; Offset += Chunk) {\r
759 Chunk = FILE_COPY_CHUNK;\r
760 \r
761 Status = EfiRead(Source, Buffer, &Chunk);\r
762 if (EFI_ERROR(Status)) {\r
f6381f4c 763 AsciiPrint("Read file error %r\n", Status);\r
2ef2b01e
A
764 goto Exit;\r
765 }\r
766\r
767 Status = EfiWrite(Destination, Buffer, &Chunk);\r
768 if (EFI_ERROR(Status)) {\r
f6381f4c 769 AsciiPrint("Write file error %r\n", Status);\r
2ef2b01e
A
770 goto Exit;\r
771 } \r
772 }\r
773 \r
774 // Any left over?\r
775 if (Offset < Size) {\r
776 Chunk = Size - Offset;\r
777 \r
778 Status = EfiRead(Source, Buffer, &Chunk);\r
779 if (EFI_ERROR(Status)) {\r
f6381f4c 780 AsciiPrint("Read file error %r\n", Status);\r
2ef2b01e
A
781 goto Exit;\r
782 }\r
783\r
784 Status = EfiWrite(Destination, Buffer, &Chunk);\r
785 if (EFI_ERROR(Status)) {\r
f6381f4c 786 AsciiPrint("Write file error %r\n", Status);\r
2ef2b01e
A
787 goto Exit;\r
788 } \r
789 }\r
790\r
f6381f4c 791\r
2ef2b01e
A
792Exit:\r
793 if (Source != NULL) {\r
794 Status = EfiClose(Source);\r
795 if (EFI_ERROR(Status)) {\r
796 AsciiPrint("Source close error %r\n", Status);\r
797 }\r
798 }\r
2ef2b01e
A
799 if (Destination != NULL) {\r
800 Status = EfiClose(Destination);\r
801 if (EFI_ERROR(Status)) {\r
802 AsciiPrint("Destination close error %r\n", Status);\r
803 }\r
804 }\r
805 \r
806 if (Buffer != NULL) {\r
807 FreePool(Buffer);\r
808 }\r
809 \r
810 return Status;\r
811}\r
812\r
813EFI_STATUS\r
814EblFileDiffCmd (\r
815 IN UINTN Argc,\r
816 IN CHAR8 **Argv\r
817 )\r
818{\r
819 EFI_OPEN_FILE *File1 = NULL;\r
820 EFI_OPEN_FILE *File2 = NULL;\r
821 EFI_STATUS Status = EFI_SUCCESS;\r
822 VOID *Buffer1 = NULL;\r
823 VOID *Buffer2 = NULL;\r
824 UINTN Size1;\r
825 UINTN Size2;\r
826 UINTN Offset;\r
827 UINTN Chunk = FILE_COPY_CHUNK;\r
828 \r
829 if (Argc != 3) {\r
830 return EFI_INVALID_PARAMETER;\r
831 }\r
832 \r
833 File1 = EfiOpen(Argv[1], EFI_FILE_MODE_READ, 0);\r
834 if (File1 == NULL) {\r
835 AsciiPrint("File 1 open error.\n");\r
836 return EFI_NOT_FOUND;\r
837 }\r
838 \r
839 File2 = EfiOpen(Argv[2], EFI_FILE_MODE_READ, 0);\r
840 if (File2 == NULL) {\r
841 AsciiPrint("File 2 open error.\n");\r
842 return EFI_NOT_FOUND;\r
843 }\r
844\r
845 Size1 = EfiTell(File1, NULL);\r
846 Size2 = EfiTell(File2, NULL);\r
847\r
848 if (Size1 != Size2) {\r
849 AsciiPrint("Files differ.\n");\r
850 goto Exit;\r
851 }\r
852\r
853 Buffer1 = AllocatePool(FILE_COPY_CHUNK);\r
854 if (Buffer1 == NULL) {\r
855 goto Exit;\r
856 }\r
857 \r
858 Buffer2 = AllocatePool(FILE_COPY_CHUNK);\r
859 if (Buffer2 == NULL) {\r
860 goto Exit;\r
861 } \r
862\r
863 for (Offset = 0; Offset + FILE_COPY_CHUNK <= Size1; Offset += Chunk) {\r
864 Chunk = FILE_COPY_CHUNK;\r
865 \r
866 Status = EfiRead(File1, Buffer1, &Chunk);\r
867 if (EFI_ERROR(Status)) {\r
868 AsciiPrint("File 1 read error\n");\r
869 goto Exit;\r
870 }\r
871\r
872 Status = EfiRead(File2, Buffer2, &Chunk);\r
873 if (EFI_ERROR(Status)) {\r
874 AsciiPrint("File 2 read error\n");\r
875 goto Exit;\r
876 }\r
877 \r
878 if (CompareMem(Buffer1, Buffer2, Chunk) != 0) {\r
879 AsciiPrint("Files differ.\n");\r
880 goto Exit;\r
881 };\r
882 }\r
883 \r
884 // Any left over?\r
885 if (Offset < Size1) {\r
886 Chunk = Size1 - Offset;\r
887 \r
888 Status = EfiRead(File1, Buffer1, &Chunk);\r
889 if (EFI_ERROR(Status)) {\r
890 AsciiPrint("File 1 read error\n");\r
891 goto Exit;\r
892 }\r
893\r
894 Status = EfiRead(File2, Buffer2, &Chunk);\r
895 if (EFI_ERROR(Status)) {\r
896 AsciiPrint("File 2 read error\n");\r
897 goto Exit;\r
898 } \r
899 }\r
900 \r
901 if (CompareMem(Buffer1, Buffer2, Chunk) != 0) {\r
902 AsciiPrint("Files differ.\n");\r
903 } else {\r
904 AsciiPrint("Files are identical.\n");\r
905 }\r
906\r
907Exit:\r
908 if (File1 != NULL) {\r
909 Status = EfiClose(File1);\r
910 if (EFI_ERROR(Status)) {\r
911 AsciiPrint("File 1 close error %r\n", Status);\r
912 }\r
913 }\r
914 \r
915 if (File2 != NULL) {\r
916 Status = EfiClose(File2);\r
917 if (EFI_ERROR(Status)) {\r
918 AsciiPrint("File 2 close error %r\n", Status);\r
919 }\r
920 }\r
921 \r
922 if (Buffer1 != NULL) {\r
923 FreePool(Buffer1);\r
924 }\r
925 \r
926 if (Buffer2 != NULL) {\r
927 FreePool(Buffer2);\r
928 }\r
929 \r
930 return Status;\r
931}\r
932\r
933GLOBAL_REMOVE_IF_UNREFERENCED const EBL_COMMAND_TABLE mCmdDeviceTemplate[] =\r
934{\r
935 {\r
936 "connect",\r
937 "[d]; Connect all EFI devices. d means disconnect",\r
938 NULL,\r
939 EblConnectCmd\r
940 },\r
941 {\r
942 "device",\r
943 "; Show information about boot devices",\r
944 NULL,\r
945 EblDeviceCmd\r
946 },\r
947 {\r
948 "go",\r
949 " dev:path loadaddress entrypoint args; load to given address and jump in", \r
950 NULL,\r
951 EblGoCmd\r
952 },\r
953 {\r
954 "loadfv",\r
955 " devname; Load PI FV from device", \r
956 NULL,\r
957 EblLoadFvCmd\r
958 },\r
959 {\r
960 "start",\r
961 " path; EFI Boot Device:filepath. fs1:\\EFI\\BOOT.EFI", \r
962 NULL,\r
963 EblStartCmd\r
964 },\r
965 {\r
966 "memmap",\r
967 "; dump EFI memory map", \r
968 NULL,\r
969 EblMemMapCmd\r
970 },\r
971 {\r
972 "cp",\r
973 " file1 file2; copy file",\r
974 NULL,\r
975 EblFileCopyCmd\r
976 },\r
977 {\r
978 "diff",\r
979 " file1 file2; compare files",\r
980 NULL,\r
981 EblFileDiffCmd\r
982 }\r
983};\r
984\r
985\r
986/**\r
987 Initialize the commands in this in this file\r
988**/\r
989\r
990VOID\r
991EblInitializeDeviceCmd (\r
992 VOID\r
993 )\r
994{\r
995 EfiGetSystemConfigurationTable (&gEfiDxeServicesTableGuid, (VOID **) &gDS);\r
996 EblAddCommands (mCmdDeviceTemplate, sizeof (mCmdDeviceTemplate)/sizeof (EBL_COMMAND_TABLE));\r
997}\r
998\r