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