2 Main file for Mm shell Debug1 function.
4 Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "UefiShellDebug1CommandsLib.h"
16 #include <Library/ShellLib.h>
17 #include <Protocol/PciRootBridgeIo.h>
18 #include <Protocol/DeviceIo.h>
28 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
39 STATIC CONST UINT64 MaxNum
[9] = { 0xff, 0xffff, 0xffffffff, 0xffffffffffffffffULL
};
42 Read some data into a buffer from memory.
44 @param[in] Width The width of each read.
45 @param[in] Addresss The memory location to start reading at.
46 @param[in] Size The size of Buffer in Width sized units.
47 @param[out] Buffer The buffer to read into.
52 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
59 // This function is defective. This ASSERT prevents the defect from affecting anything.
63 if (Width
== EfiPciWidthUint8
) {
64 *(UINT8
*) Buffer
= *(UINT8
*) (UINTN
) Address
;
66 } else if (Width
== EfiPciWidthUint16
) {
67 *(UINT16
*) Buffer
= *(UINT16
*) (UINTN
) Address
;
69 } else if (Width
== EfiPciWidthUint32
) {
70 *(UINT32
*) Buffer
= *(UINT32
*) (UINTN
) Address
;
72 } else if (Width
== EfiPciWidthUint64
) {
73 *(UINT64
*) Buffer
= *(UINT64
*) (UINTN
) Address
;
76 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_READ_ERROR
), gShellDebug1HiiHandle
);
84 Write some data to memory.
86 @param[in] Width The width of each write.
87 @param[in] Addresss The memory location to start writing at.
88 @param[in] Size The size of Buffer in Width sized units.
89 @param[in] Buffer The buffer to write from.
94 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
,
101 // This function is defective. This ASSERT prevents the defect from affecting anything.
105 if (Width
== EfiPciWidthUint8
) {
106 *(UINT8
*) (UINTN
) Address
= *(UINT8
*) Buffer
;
108 } else if (Width
== EfiPciWidthUint16
) {
109 *(UINT16
*) (UINTN
) Address
= *(UINT16
*) Buffer
;
111 } else if (Width
== EfiPciWidthUint32
) {
112 *(UINT32
*) (UINTN
) Address
= *(UINT32
*) Buffer
;
114 } else if (Width
== EfiPciWidthUint64
) {
115 *(UINT64
*) (UINTN
) Address
= *(UINT64
*) Buffer
;
128 Convert a string to it's hex data.
130 @param[in] str The pointer to the string of hex data.
131 @param[out] data The pointer to the buffer to fill. Valid upon a TRUE return.
133 @retval TRUE The conversion was successful.
134 @retval FALSE The conversion failed.
149 // convert hex digits
153 while (TempChar
!= CHAR_NULL
) {
154 if (TempChar
>= 'a' && TempChar
<= 'f') {
155 TempChar
-= 'a' - 'A';
158 if (TempChar
== ' ') {
162 if ((TempChar
>= '0' && TempChar
<= '9') || (TempChar
>= 'A' && TempChar
<= 'F')) {
163 TempUint
= (TempUint
<< 4) | (TempChar
- (TempChar
>= 'A' ? 'A' - 10 : '0'));
178 Get the PCI-E Address from a PCI address format 0x0000ssbbddffrrr
179 where ss is SEGMENT, bb is BUS, dd is DEVICE, ff is FUNCTION
180 and rrr is REGISTER (extension format for PCI-E).
182 @param[in] InputAddress PCI address format on input.
183 @param[out]PciEAddress PCI-E address extention format.
187 GetPciEAddressFromInputAddress (
188 IN UINT64 InputAddress
,
189 OUT UINT64
*PciEAddress
192 *PciEAddress
= RShiftU64(InputAddress
& ~(UINT64
) 0xFFF, 4);
193 *PciEAddress
+= LShiftU64((UINT16
) InputAddress
& 0x0FFF, 32);
197 Function for 'mm' command.
199 @param[in] ImageHandle Handle to the Image (NULL if Internal).
200 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
205 IN EFI_HANDLE ImageHandle
,
206 IN EFI_SYSTEM_TABLE
*SystemTable
210 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*IoDev
;
214 UINT32 SegmentNumber
;
215 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width
;
216 EFI_ACCESS_TYPE AccessType
;
225 EFI_HANDLE
*HandleBuffer
;
229 CHAR16
*ProblemParam
;
230 SHELL_STATUS ShellStatus
;
239 ShellStatus
= SHELL_SUCCESS
;
245 Width
= EfiPciWidthUint8
;
247 AccessType
= EfiMemory
;
253 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
254 if (EFI_ERROR(Status
)) {
255 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
256 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, ProblemParam
);
257 FreePool(ProblemParam
);
258 ShellStatus
= SHELL_INVALID_PARAMETER
;
264 if (ShellCommandLineGetCount(Package
) < 2) {
265 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
266 ShellStatus
= SHELL_INVALID_PARAMETER
;
268 } else if (ShellCommandLineGetCount(Package
) > 3) {
269 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
270 ShellStatus
= SHELL_INVALID_PARAMETER
;
272 } else if (ShellCommandLineGetFlag(Package
, L
"-w") && ShellCommandLineGetValue(Package
, L
"-w") == NULL
) {
273 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_VALUE
), gShellDebug1HiiHandle
, L
"-w");
274 ShellStatus
= SHELL_INVALID_PARAMETER
;
277 if (ShellCommandLineGetFlag(Package
, L
"-mmio")) {
278 AccessType
= EFIMemoryMappedIo
;
279 if (ShellCommandLineGetFlag(Package
, L
"-mem")
280 ||ShellCommandLineGetFlag(Package
, L
"-io")
281 ||ShellCommandLineGetFlag(Package
, L
"-pci")
282 ||ShellCommandLineGetFlag(Package
, L
"-pcie")
284 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
285 ShellStatus
= SHELL_INVALID_PARAMETER
;
288 } else if (ShellCommandLineGetFlag(Package
, L
"-mem")) {
289 AccessType
= EfiMemory
;
290 if (ShellCommandLineGetFlag(Package
, L
"-io")
291 ||ShellCommandLineGetFlag(Package
, L
"-pci")
292 ||ShellCommandLineGetFlag(Package
, L
"-pcie")
294 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
295 ShellStatus
= SHELL_INVALID_PARAMETER
;
298 } else if (ShellCommandLineGetFlag(Package
, L
"-io")) {
300 if (ShellCommandLineGetFlag(Package
, L
"-pci")
301 ||ShellCommandLineGetFlag(Package
, L
"-pcie")
303 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
304 ShellStatus
= SHELL_INVALID_PARAMETER
;
307 } else if (ShellCommandLineGetFlag(Package
, L
"-pci")) {
308 AccessType
= EfiPciConfig
;
309 if (ShellCommandLineGetFlag(Package
, L
"-pcie")
311 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
312 ShellStatus
= SHELL_INVALID_PARAMETER
;
315 } else if (ShellCommandLineGetFlag(Package
, L
"-pcie")) {
316 AccessType
= EfiPciEConfig
;
320 if (ShellCommandLineGetFlag (Package
, L
"-n")) {
324 Temp
= ShellCommandLineGetValue(Package
, L
"-w");
326 ItemValue
= ShellStrToUintn (Temp
);
330 Width
= EfiPciWidthUint8
;
335 Width
= EfiPciWidthUint16
;
340 Width
= EfiPciWidthUint32
;
345 Width
= EfiPciWidthUint64
;
350 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM_VAL
), gShellDebug1HiiHandle
, L
"-w");
351 ShellStatus
= SHELL_INVALID_PARAMETER
;
356 Temp
= ShellCommandLineGetRawValue(Package
, 1);
357 if (!ShellIsHexOrDecimalNumber(Temp
, TRUE
, FALSE
) || EFI_ERROR(ShellConvertStringToUint64(Temp
, (UINT64
*)&Address
, TRUE
, FALSE
))) {
358 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, Temp
);
359 ShellStatus
= SHELL_INVALID_PARAMETER
;
363 Temp
= ShellCommandLineGetRawValue(Package
, 2);
365 if (!ShellIsHexOrDecimalNumber(Temp
, TRUE
, FALSE
) || EFI_ERROR(ShellConvertStringToUint64(Temp
, &Value
, TRUE
, FALSE
))) {
366 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, Temp
);
367 ShellStatus
= SHELL_INVALID_PARAMETER
;
373 ShellStatus
= SHELL_INVALID_PARAMETER
;
378 if (Value
> 0xFFFF) {
379 ShellStatus
= SHELL_INVALID_PARAMETER
;
384 if (Value
> 0xFFFFFFFF) {
385 ShellStatus
= SHELL_INVALID_PARAMETER
;
393 if (ShellStatus
!= SHELL_SUCCESS
) {
394 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, Temp
);
395 ShellStatus
= SHELL_INVALID_PARAMETER
;
400 if ((Address
& (Size
- 1)) != 0) {
401 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_NOT_ALIGNED
), gShellDebug1HiiHandle
, Address
);
402 ShellStatus
= SHELL_INVALID_PARAMETER
;
406 // locate DeviceIO protocol interface
408 if (AccessType
!= EfiMemory
) {
409 Status
= gBS
->LocateHandleBuffer (
411 &gEfiPciRootBridgeIoProtocolGuid
,
416 if (EFI_ERROR (Status
)) {
417 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PCIRBIO_NF
), gShellDebug1HiiHandle
);
418 ShellStatus
= SHELL_NOT_FOUND
;
422 // In the case of PCI or PCIE
423 // Get segment number and mask the segment bits in Address
425 if (AccessType
== EfiPciEConfig
) {
426 SegmentNumber
= (UINT32
) RShiftU64 (Address
, 36) & 0xff;
427 Address
&= 0xfffffffffULL
;
429 if (AccessType
== EfiPciConfig
) {
430 SegmentNumber
= (UINT32
) RShiftU64 (Address
, 32) & 0xff;
431 Address
&= 0xffffffff;
435 // Find the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL of the specified segment number
437 for (Index
= 0; Index
< BufferSize
; Index
++) {
438 Status
= gBS
->HandleProtocol (
440 &gEfiPciRootBridgeIoProtocolGuid
,
443 if (EFI_ERROR (Status
)) {
446 if (IoDev
->SegmentNumber
!= SegmentNumber
) {
451 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_SEGMENT_NOT_FOUND
), gShellDebug1HiiHandle
, SegmentNumber
);
452 ShellStatus
= SHELL_INVALID_PARAMETER
;
457 if (AccessType
== EfiIo
&& Address
+ Size
> 0x10000) {
458 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_ADDRESS_RANGE
), gShellDebug1HiiHandle
);
459 ShellStatus
= SHELL_INVALID_PARAMETER
;
463 if (AccessType
== EfiPciEConfig
) {
464 GetPciEAddressFromInputAddress (Address
, &PciEAddress
);
470 // if (ValueStr != NULL) {
471 // if (AccessType == EFIMemoryMappedIo) {
472 // IoDev->Mem.Write (IoDev, Width, Address, 1, &Value);
473 // } else if (AccessType == EfiIo) {
474 // IoDev->Io.Write (IoDev, Width, Address, 1, &Value);
475 // } else if (AccessType == EfiPciConfig) {
476 // IoDev->Pci.Write (IoDev, Width, Address, 1, &Value);
477 // } else if (AccessType == EfiPciEConfig) {
478 // IoDev->Pci.Write (IoDev, Width, PciEAddress, 1, &Buffer);
480 // WriteMem (Width, Address, 1, &Value);
483 // ASSERT(ShellStatus == SHELL_SUCCESS);
489 // non-interactive mode
493 if (AccessType
== EFIMemoryMappedIo
) {
494 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_MMIO
), gShellDebug1HiiHandle
);
495 IoDev
->Mem
.Read (IoDev
, Width
, Address
, 1, &Buffer
);
496 } else if (AccessType
== EfiIo
) {
497 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_IO
), gShellDebug1HiiHandle
);
498 IoDev
->Io
.Read (IoDev
, Width
, Address
, 1, &Buffer
);
499 } else if (AccessType
== EfiPciConfig
) {
500 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_PCI
), gShellDebug1HiiHandle
);
501 IoDev
->Pci
.Read (IoDev
, Width
, Address
, 1, &Buffer
);
502 } else if (AccessType
== EfiPciEConfig
) {
503 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_PCIE
), gShellDebug1HiiHandle
);
504 IoDev
->Pci
.Read (IoDev
, Width
, PciEAddress
, 1, &Buffer
);
506 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_MEM
), gShellDebug1HiiHandle
);
507 ReadMem (Width
, Address
, 1, &Buffer
);
510 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_ADDRESS
), gShellDebug1HiiHandle
, Address
);
512 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_BUF2
), gShellDebug1HiiHandle
, Buffer
);
513 } else if (Size
== 2) {
514 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_BUF4
), gShellDebug1HiiHandle
, Buffer
);
515 } else if (Size
== 4) {
516 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_BUF8
), gShellDebug1HiiHandle
, Buffer
);
517 } else if (Size
== 8) {
518 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_BUF16
), gShellDebug1HiiHandle
, Buffer
);
521 ShellPrintEx(-1, -1, L
"\r\n");
523 ASSERT(ShellStatus
== SHELL_SUCCESS
);
531 if (AccessType
== EfiIo
&& Address
+ Size
> 0x10000) {
532 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_ADDRESS_RANGE2
), gShellDebug1HiiHandle
);
537 if (AccessType
== EFIMemoryMappedIo
) {
538 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_MMIO
), gShellDebug1HiiHandle
);
539 IoDev
->Mem
.Read (IoDev
, Width
, Address
, 1, &Buffer
);
540 } else if (AccessType
== EfiIo
) {
541 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_IO
), gShellDebug1HiiHandle
);
542 IoDev
->Io
.Read (IoDev
, Width
, Address
, 1, &Buffer
);
543 } else if (AccessType
== EfiPciConfig
) {
544 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_PCI
), gShellDebug1HiiHandle
);
545 IoDev
->Pci
.Read (IoDev
, Width
, Address
, 1, &Buffer
);
546 } else if (AccessType
== EfiPciEConfig
) {
547 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_PCIE
), gShellDebug1HiiHandle
);
548 IoDev
->Pci
.Read (IoDev
, Width
, PciEAddress
, 1, &Buffer
);
550 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_MEM
), gShellDebug1HiiHandle
);
551 ReadMem (Width
, Address
, 1, &Buffer
);
554 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_ADDRESS
), gShellDebug1HiiHandle
, Address
);
557 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_BUF2
), gShellDebug1HiiHandle
, Buffer
);
558 } else if (Size
== 2) {
559 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_BUF4
), gShellDebug1HiiHandle
, Buffer
);
560 } else if (Size
== 4) {
561 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_BUF8
), gShellDebug1HiiHandle
, Buffer
);
562 } else if (Size
== 8) {
563 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_BUF16
), gShellDebug1HiiHandle
, Buffer
);
565 ShellPrintEx(-1, -1, L
" > ");
567 // wait user input to modify
569 if (InputStr
!= NULL
) {
573 ShellPromptForResponse(ShellPromptResponseTypeFreeform
, NULL
, (VOID
**)&InputStr
);
576 // skip space characters
578 for (Index
= 0; InputStr
!= NULL
&& InputStr
[Index
] == ' '; Index
++);
581 // parse input string
583 if (InputStr
!= NULL
&& (InputStr
[Index
] == '.' || InputStr
[Index
] == 'q' || InputStr
[Index
] == 'Q')) {
585 } else if (InputStr
== NULL
|| InputStr
[Index
] == CHAR_NULL
) {
587 // Continue to next address
589 } else if (GetHex (InputStr
+ Index
, &Buffer
) && Buffer
<= MaxNum
[Width
]) {
590 if (AccessType
== EFIMemoryMappedIo
) {
591 IoDev
->Mem
.Write (IoDev
, Width
, Address
, 1, &Buffer
);
592 } else if (AccessType
== EfiIo
) {
593 IoDev
->Io
.Write (IoDev
, Width
, Address
, 1, &Buffer
);
594 } else if (AccessType
== EfiPciConfig
) {
595 IoDev
->Pci
.Write (IoDev
, Width
, Address
, 1, &Buffer
);
596 } else if (AccessType
== EfiPciEConfig
) {
597 IoDev
->Pci
.Write (IoDev
, Width
, PciEAddress
, 1, &Buffer
);
599 WriteMem (Width
, Address
, 1, &Buffer
);
602 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_MM_ERROR
), gShellDebug1HiiHandle
);
604 // PrintToken (STRING_TOKEN (STR_IOMOD_ERROR), HiiHandle);
608 if (AccessType
== EfiPciEConfig
) {
609 GetPciEAddressFromInputAddress (Address
, &PciEAddress
);
611 ShellPrintEx(-1, -1, L
"\r\n");
615 ASSERT(ShellStatus
== SHELL_SUCCESS
);
618 if (InputStr
!= NULL
) {
621 if (HandleBuffer
!= NULL
) {
622 FreePool (HandleBuffer
);
624 if (Package
!= NULL
) {
625 ShellCommandLineFreeVarList (Package
);