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