Step to UEFI (242)写一个输出当前 IP 的函数

前面的文章中,我们写过输出当前IP 的代码,这次,我们要尝试将这个代码编写为C的函数方便我们调用。

因为 X64 无法内嵌汇编,我们值得参考其他的函数,用如下三步可以实现:

1. \MdePkg\Include\Library\BaseLib.h 中加入函数头定义

/**
Output current RIP to debug port (0x402 for QEMU)

**/
VOID
EFIAPI
AsmCurrentRIP(
	VOID
);

2. \MdePkg\Library\BaseLib\X64\CurrentRIP.nasm 加入我们的函数汇编实现

;------------------------------------------------------------------------------
;
; Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
; SPDX-License-Identifier: BSD-2-Clause-Patent
;
; Module Name:
;
;   CurrentRIP.Asm
;
; Abstract:
;
;  Output current RIP to debug port (0x402 for QEMU)
;  
;
; Notes:
;
;------------------------------------------------------------------------------

    DEFAULT REL
    SECTION .text

;------------------------------------------------------------------------------
; VOID
; EFIAPI
; AsmCurrentRIP (
;   VOID
;   );
;------------------------------------------------------------------------------
global ASM_PFX(AsmCurrentRIP)
ASM_PFX(AsmCurrentRIP):
    lea     rax,[$]
    mov     dx,0x402
	
	; 7-0 Bits
    out     dx,al       

    ; 15-8 Bits
	shr     rax,8
    out     dx,al       
	
	; 23-16 Bits
	shr     rax,8
    out     dx,al       

	; 31-24 Bits
	shr     rax,8
    out     dx,al       

	; 39-32 Bits
	shr     rax,8
    out     dx,al       

	; 47-40 Bits
	shr     rax,8
    out     dx,al       

	; 55-48 Bits
	shr     rax,8
    out     dx,al       

	; 63-56 Bits
	shr     rax,8
    out     dx,al     

ret

3. \MdePkg\Library\BaseLib\BaseLib.inf 加入上面的文件

[Sources.X64]
  X64/Thunk16.nasm
  X64/CpuIdEx.nasm
  X64/CpuId.nasm
  X64/LongJump.nasm
  X64/SetJump.nasm
  X64/SwitchStack.nasm
  X64/EnableCache.nasm
  X64/DisableCache.nasm
  X64/WriteTr.nasm
  X64/Lfence.nasm
  X64/CurrentRIP.nasm
  X64/CpuBreakpoint.c | MSFT
  X64/WriteMsr64.c | MSFT
  X64/ReadMsr64.c | MSFT
  X64/CpuPause.nasm| MSFT
  X64/DisableInterrupts.nasm| MSFT

接下来就可以使用这个函数了,比如,我们在\OvmfPkg\Sec\SecMain.c中使用这个函数。

  if (!SevEsIsEnabled ()) {
    //
    // For non SEV-ES guests, just load the IDTR.
    //
    AsmWriteIdtr (&IdtDescriptor);
  } else {
    //
    // Under SEV-ES, the hypervisor can't modify CR0 and so can't enable
    // caching in order to speed up the boot. Enable caching early for
    // an SEV-ES guest.
    //
    AsmEnableCache ();
  }
AsmCurrentRIP();
  DEBUG ((DEBUG_INFO,
    "SecCoreStartupWithStack(0x%x, 0x%x)\n",
    (UINT32)(UINTN)BootFv,
    (UINT32)(UINTN)TopOfCurrentStack
));

之后的运行中,我们也可以在串口 Log 中刚看到输出的 IP。

实际上这样的作法是错误的,有兴趣的朋友可以思考一下:我想输出当前代码运行的IP,上面的方法错在哪里?

接下来,我们在 SecMain.inf 中加入如下命令,生成 SecMain.cod 进行查看:

[BuildOptions]
  MSFT:*_*_X64_CC_FLAGS  = /FAsc /Od

在 SecMain.cod 中我们看到的结果如下:
; 908  :     //
; 909  :     // Under SEV-ES, the hypervisor can't modify CR0 and so can't enable
; 910  :     // caching in order to speed up the boot. Enable caching early for
; 911  :     // an SEV-ES guest.
; 912  :     //
; 913  :     AsmEnableCache ();

  00131	e8 00 00 00 00	 call	 AsmEnableCache
$LN25@SecCoreSta:

; 914  :   }
; 915  : AsmCurrentRIP();

  00136	e8 00 00 00 00	 call	 AsmCurrentRIP
$LN13@SecCoreSta:

; 916  :   DEBUG ((DEBUG_INFO,

  0013b	e8 00 00 00 00	 call	 DebugPrintEnabled
  00140	0f b6 c0	 movzx	 eax, al
  00143	85 c0		 test	 eax, eax
  00145	74 38		 je	 SHORT $LN26@SecCoreSta
$LN16@SecCoreSta:

; 917  :     "SecCoreStartupWithStack(0x%x, 0x%x)\n",
; 918  :     (UINT32)(UINTN)BootFv,
; 919  :     (UINT32)(UINTN)TopOfCurrentStack
; 920  :     ));

那么问题来了,我们是在 AsmCurrentRIP() 中输出的当前 IP。作为一个函数,它的地址是确定的。因此,你在不同的地方调用这个函数,实际上会得到同样的结果。因此,上面的方法是错误的。继续思考,当我们 Call 到这个函数中之后,堆栈中存着调用处的信息(更准确的说是返回位置的信息)。因此,稍微修改我们之前的代码,输出堆栈中的信息即可。

首先,修改后的代码如下:

;------------------------------------------------------------------------------
;
; Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
; SPDX-License-Identifier: BSD-2-Clause-Patent
;
; Module Name:
;
;   CurrentRIP.Asm
;
; Abstract:
;
;  Output current RIP to debug port (0x402 for QEMU)
;  
;
; Notes:
;
;------------------------------------------------------------------------------

    DEFAULT REL
    SECTION .text

;------------------------------------------------------------------------------
; VOID
; EFIAPI
; AsmCurrentRIP (
;   VOID
;   );
;------------------------------------------------------------------------------
global ASM_PFX(AsmCurrentRIP)
ASM_PFX(AsmCurrentRIP):
    mov     rax,[rsp]
    mov     dx,0x402
	
	; 7-0 Bits
    out     dx,al       

    ; 15-8 Bits
	shr     rax,8
    out     dx,al       
	
	; 23-16 Bits
	shr     rax,8
    out     dx,al       

	; 31-24 Bits
	shr     rax,8
    out     dx,al       

	; 39-32 Bits
	shr     rax,8
    out     dx,al       

	; 47-40 Bits
	shr     rax,8
    out     dx,al       

	; 55-48 Bits
	shr     rax,8
    out     dx,al       

	; 63-56 Bits
	shr     rax,8
    out     dx,al     

ret

在 SecMain.c 中调用如下:

//
  // Find PEI Core entry point. It will report SEC and Pei Core debug information if remote debug
  // is enabled.
  //
  BootFv = (EFI_FIRMWARE_VOLUME_HEADER *)SecCoreData->BootFirmwareVolumeBase;
  FindAndReportEntryPoints (&BootFv, &PeiCoreEntryPoint);
  SecCoreData->BootFirmwareVolumeBase = BootFv;
  SecCoreData->BootFirmwareVolumeSize = (UINTN) BootFv->FvLength;
  AsmCurrentRIP(); //ZivDebug
  DEBUG ((DEBUG_INFO,
    "Check [BootFirmwareVolumeBase]-0x%X [PeiCoreEntryPoint]=0x%X BootFirmwareVolumeBase=0x%X \n",
	(UINT64)BootFv,
    (UINT64)(PeiCoreEntryPoint),
	(UINT64)(*PeiCoreEntryPoint)
    ));
  AsmCurrentRIP(); //ZivDebug	
  //
  // Transfer the control to the PEI core
  //
  (*PeiCoreEntryPoint) (SecCoreData, (EFI_PEI_PPI_DESCRIPTOR *)&mPrivateDispatchTable);

在对应的 Cod 文件中查看到的结果如下:

; 1016 :   AsmCurrentRIP(); //ZivDebug

  00050	e8 00 00 00 00	 call	 AsmCurrentRIP
$LN4@SecStartup:

; 1017 :   DEBUG ((DEBUG_INFO,

  00055	e8 00 00 00 00	 call	 DebugPrintEnabled
  0005a	0f b6 c0	 movzx	 eax, al
  0005d	85 c0		 test	 eax, eax
  0005f	74 3c		 je	 SHORT $LN11@SecStartup
$LN7@SecStartup:

; 1018 :     "Check [BootFirmwareVolumeBase]-0x%X [PeiCoreEntryPoint]=0x%X BootFirmwareVolumeBase=0x%X \n",
; 1019 : 	(UINT64)BootFv,
; 1020 :     (UINT64)(PeiCoreEntryPoint),
; 1021 : 	(UINT64)(*PeiCoreEntryPoint)
; 1022 :     ));

  00061	b9 40 00 00 00	 mov	 ecx, 64			; 00000040H
  00066	e8 00 00 00 00	 call	 DebugPrintLevelEnabled
  0006b	0f b6 c0	 movzx	 eax, al
  0006e	85 c0		 test	 eax, eax
  00070	74 25		 je	 SHORT $LN12@SecStartup
  00072	48 8b 44 24 40	 mov	 rax, QWORD PTR PeiCoreEntryPoint$[rsp]
  00077	48 89 44 24 20	 mov	 QWORD PTR [rsp+32], rax
  0007c	4c 8b 4c 24 40	 mov	 r9, QWORD PTR PeiCoreEntryPoint$[rsp]
  00081	4c 8b 44 24 30	 mov	 r8, QWORD PTR BootFv$[rsp]
  00086	48 8d 15 00 00
	00 00		 lea	 rdx, OFFSET FLAT:??_C@_0FL@FMODEEHL@Check?5?$FLBootFirmwareVolumeBase?$FN?90@
  0008d	b9 40 00 00 00	 mov	 ecx, 64			; 00000040H
  00092	e8 00 00 00 00	 call	 DebugPrint
$LN12@SecStartup:
  00097	33 c0		 xor	 eax, eax
  00099	85 c0		 test	 eax, eax
  0009b	75 c4		 jne	 SHORT $LN7@SecStartup
$LN11@SecStartup:

; 1017 :   DEBUG ((DEBUG_INFO,

  0009d	33 c0		 xor	 eax, eax
  0009f	85 c0		 test	 eax, eax
  000a1	75 b2		 jne	 SHORT $LN4@SecStartup

; 1023 :   AsmCurrentRIP(); //ZivDebug	

  000a3	e8 00 00 00 00	 call	 AsmCurrentRIP

第一次call 是在 AsmCurrentRIP 00050, 第二次是call 是在 000a3的位置,二者相差 (a3-50=53)。接下来查看 Log 中的输出结果:

红色位置就是2次 Call 输出结果

前面是 FFFC E309 后一个是 FFFC E35C二者相差也是 53,因此,可以确定函数输出没问题。

晚上上述实验之后,我开始翻阅 《Step To UEFI》系列文章,找到了《Step to UEFI (201)直接取得函数返回地址》【参考1】就是说可以直接通过 VC 内置函数取得调用处下一条指令的地址。

参考:

1.http://www.lab-z.com/stu201aor/

Micron 内存颗粒命名规则

在阅读Micron 内存颗粒的 DataSheet过程中经常碰到 -125e,-062e 这样的代码,起初我以为是JEDEC规定的代号,但是多次搜索无果,最近偶然间搞清楚了,这个是 Micron 家自己的命名。

以MT40A2G8SA-062E:F【参考1】为例,Datasheet中可以看到如下定义:

Timing -Cycle Time

  • 0.625ns@CL=22(DDR4-3200)  -062E
  • 0.682ns@CL=21(DDR4-2933)  -068

简单的说 -062 就是代表了 0.625ns,而0.6225=1秒/(1600Mhz),这里3200Mhz 的内存内部主频是 1600。同样的  0.6818=1秒/(2933Mhz/2)。对于-062E 最后的 E 则是表示不同的时序。在【参考2】有如下的解释:

代号对应的基本参数

所以,这里只是Micron 自定义的编号。此外,Micron内存模组有时候还会使用 nGm 这种方式进行编号。例如,内存条产品 MTA36ASF4G72PZ – 32GB 【参考1】:

– 0.62ns @ CL = 22 (DDR4-3200) -3G2

– 0.682ns @ CL = 21 (DDR4-2933) -2G9

– 0.75ns @ CL = 19 (DDR4-2666) -2G6

– 0.83ns @ CL = 17 (DDR4-2400) -2G3

表明最高速度是 n.mG 。

参考:

  1. https://media-www.micron.com/-/media/client/global/documents/products/data-sheet/dram/ddr4/16gb_ddr4_sdram.pdf?rev=40fa0d2bdf344b0fb7a36356d8b6b8f7
  2. https://www.micron.com/-/media/client/global/documents/products/part-numbering-guide/numdram.pdf
  3. https://www.micron.com/-/media/client/global/documents/products/data-sheet/modules/rdimm/ddr4/asf36c4gx72pz.pdf

Ampere Computing 职位(2022 Jan)

Req IdTitleLocationLink for Job Description
NS870Bigdata System and Software EngineerSHhttps://amperecomputing.com/apply/?p=job%2FoeMyffwx
NS1213System Control Firmware EngineerSHhttps://amperecomputing.com/apply/?p=job/o7gWhfwk&nl=1
NS935Platform Applications EngineerSH/BJhttps://jobs.jobvite.com/amperecomputing/job/oTd6ffwb
NS1173Cloud Solution ArchitectSHhttps://amperecomputing.com/apply/?p=job%2FopnChfwp
NS1153Account ManagerBJhttps://amperecomputing.com/apply/?p=job%2FopHxhfwE
NS1167Field Applications EngineerSH/BJhttps://amperecomputing.com/apply/?p=job%2FoxpBhfwy
NS1187Sales Development ManagerSH/BJ/SZhttps://amperecomputing.com/apply/?p=job/oVyLhfwf&nl=1
NS1146IT Service Desk / System AdministratorSHhttps://amperecomputing.com/apply/?p=job/o4zuhfw8&nl=1

有兴趣的朋友可以直接联系 jun.chen@amperecomputing.com

Step to UEFI (241)SecMain 中的 CONSTRUCTOR

在 SecCoreStartupWithStack() 函数中,有下面这一条调用:

ProcessLibraryConstructorList (NULL, NULL);

具体调用的代码在\Build\OvmfX64\DEBUG_VS2015x86\X64\OvmfPkg\Sec\SecMain\DEBUG\AutoGen.c 中:

VOID
EFIAPI
ProcessLibraryConstructorList (
  VOID
  )
{
  RETURN_STATUS  Status;

  Status = PlatformRomDebugLibIoPortConstructor ();
  ASSERT_RETURN_ERROR (Status);

  Status = AcpiTimerLibConstructor ();
  ASSERT_RETURN_ERROR (Status);

  Status = LzmaDecompressLibConstructor ();
  ASSERT_RETURN_ERROR (Status);
}
  1. PlatformRomDebugLibIoPortConstructor() 在 DebugLibDetectRom.c 中,函数是空的直接 return 了事;
  2. AcpiTimerLibConstructor() 在 BaseRomAcpiTimerLib.c 中,可以看到是取得当前一些 IO Base 的信息
/**
  The constructor function enables ACPI IO space.

  If ACPI I/O space not enabled, this function will enable it.
  It will always return RETURN_SUCCESS.

  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.

**/
RETURN_STATUS
EFIAPI
AcpiTimerLibConstructor (
  VOID
  )
{
  UINT16 HostBridgeDevId;
  UINTN Pmba;
  UINT32 PmbaAndVal;
  UINT32 PmbaOrVal;
  UINTN AcpiCtlReg;
  UINT8 AcpiEnBit;

  //
  // Query Host Bridge DID to determine platform type
  //
  HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);
  switch (HostBridgeDevId) {
    case INTEL_82441_DEVICE_ID:
      Pmba       = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA);
      PmbaAndVal = ~(UINT32)PIIX4_PMBA_MASK;
      PmbaOrVal  = PIIX4_PMBA_VALUE;
      AcpiCtlReg = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMREGMISC);
      AcpiEnBit  = PIIX4_PMREGMISC_PMIOSE;
      break;
    case INTEL_Q35_MCH_DEVICE_ID:
      Pmba       = POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);
      PmbaAndVal = ~(UINT32)ICH9_PMBASE_MASK;
      PmbaOrVal  = ICH9_PMBASE_VALUE;
      AcpiCtlReg = POWER_MGMT_REGISTER_Q35 (ICH9_ACPI_CNTL);
      AcpiEnBit  = ICH9_ACPI_CNTL_ACPI_EN;
      break;
    default:
      DEBUG ((DEBUG_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",
        __FUNCTION__, HostBridgeDevId));
      ASSERT (FALSE);
      return RETURN_UNSUPPORTED;
  }

  //
  // Check to see if the Power Management Base Address is already enabled
  //
  if ((PciRead8 (AcpiCtlReg) & AcpiEnBit) == 0) {
    //
    // If the Power Management Base Address is not programmed,
    // then program it now.
    //
    PciAndThenOr32 (Pmba, PmbaAndVal, PmbaOrVal);

    //
    // Enable PMBA I/O port decodes
    //
    PciOr8 (AcpiCtlReg, AcpiEnBit);
  }

  return RETURN_SUCCESS;
}

3.LzmaDecompressLibConstructor 函数定义在\MdeModulePkg\Library\LzmaCustomDecompressLib\GuidedSectionExtraction.c 中:

/**
  Register LzmaDecompress and LzmaDecompressGetInfo handlers with LzmaCustomerDecompressGuid.

  @retval  RETURN_SUCCESS            Register successfully.
  @retval  RETURN_OUT_OF_RESOURCES   No enough memory to store this handler.
**/
EFI_STATUS
EFIAPI
LzmaDecompressLibConstructor (
  VOID
  )
{
  return ExtractGuidedSectionRegisterHandlers (
          &gLzmaCustomDecompressGuid,
          LzmaGuidedSectionGetInfo,
          LzmaGuidedSectionExtraction
          );
}

在 OvmfPkgX64.dsc 中[LibraryClasses.common.SEC] 这个Section可以看到如下三个 Lib:

  1. DebugLib|OvmfPkg/Library/PlatformDebugLibIoPort/PlatformRomDebugLibIoPort.inf
  2. TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf
  3. <LibraryClasses> NULL|MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaCustomDecompressLib.inf

上述的INF 文件中都有定义   CONSTRUCTOR   ,例如: 

CONSTRUCTOR                    = LzmaDecompressLibConstructor

因此,就是说SecMain.inf给出了引用的 LIB,然后在编译过程中如果发现对应的 LIB 有指定CONSTRUCTOR   , 那么就会构造 ProcessLibraryConstructorList() 。这样做的目的是:保证在调用一些LIB之前,已经完成了对应的初始化。类似的还有ProcessLibraryDestructorList(),可以用来完成一些LIB的收尾动作。

接下来的一个问题是:谁做了这个自动生成的动作?

答案是: build.py

在\BaseTools\Source\Python\AutoGen\GenC.py 有下面的函数:

## Create code for library constructor
#
#   @param      Info        The ModuleAutoGen object
#   @param      AutoGenC    The TemplateString object for C code
#   @param      AutoGenH    The TemplateString object for header file
#
def CreateLibraryConstructorCode(Info, AutoGenC, AutoGenH):

在其中加入如下 代码用于验证:

    for Lib in DependentLibraryList:
        if len(Lib.ConstructorList) <= 0:
            continue
        Dict = {'Function':Lib.ConstructorList}
        if Lib.ModuleType in [SUP_MODULE_BASE, SUP_MODULE_SEC]:
            ConstructorPrototypeString.Append("//www.lab-z.com testing");
            ConstructorPrototypeString.Append(gLibraryStructorPrototype[SUP_MODULE_BASE].Replace(Dict))
            ConstructorCallingString.Append(gLibraryStructorCall[SUP_MODULE_BASE].Replace(Dict))

重新编译代码,再次查看\Build\OvmfX64\DEBUG_VS2015x86\X64\OvmfPkg\Sec\SecMain\DEBUG\AutoGen.c可以看到我们测试的注释代码已经写入 AutoGen.c 了:

GLOBAL_REMOVE_IF_UNREFERENCED const BOOLEAN _gPcd_FixedAtBuild_PcdCpuSmmStackGuard = _PCD_VALUE_PcdCpuSmmStackGuard;
extern const  BOOLEAN  _gPcd_FixedAtBuild_PcdCpuSmmStackGuard;
#define _PCD_GET_MODE_BOOL_PcdCpuSmmStackGuard  _gPcd_FixedAtBuild_PcdCpuSmmStackGuard
//#define _PCD_SET_MODE_BOOL_PcdCpuSmmStackGuard  ASSERT(FALSE)  // It is not allowed to set value for a FIXED_AT_BUILD PCD

//www.lab-z.com testing
RETURN_STATUS
EFIAPI
PlatformRomDebugLibIoPortConstructor (
  VOID
  );
//www.lab-z.com testing
RETURN_STATUS
EFIAPI
AcpiTimerLibConstructor (
  VOID
  );
//www.lab-z.com testing
RETURN_STATUS
EFIAPI
LzmaDecompressLibConstructor (
  VOID
  );


VOID
EFIAPI
ProcessLibraryConstructorList (
  VOID
  )
{
  RETURN_STATUS  Status;

  Status = PlatformRomDebugLibIoPortConstructor ();
  ASSERT_RETURN_ERROR (Status);

  Status = AcpiTimerLibConstructor ();
  ASSERT_RETURN_ERROR (Status);

  Status = LzmaDecompressLibConstructor ();
  ASSERT_RETURN_ERROR (Status);

}

上述实验是基于 edk202108, 如果你在其他版本实验,有可能调用的是 build.exe,实验无法成功。

能在 ADL 上使用的 RU

今天忽然发现之前的RU 无法在 Intel 最新的 ADL 平台上工作(现象是运行之后黑屏)。我忽然也意识到在后台留言“密码“二字的朋友,他们需要 RU 压缩包的解压密码。

特此声明: RU 系列的作者是: AMI 的 James。

正式的下载网站是https://github.com/JamesAmiTw/ru-uefi , 其中的压缩包密码可以在

他个人网站 http://ruexe.blogspot.com/ 看到(该网站需要科学上网)。。

为了方便大家,这里放上一个最新的无密码 RU (截至2021年12月27日)。有需要的朋友可以下载。

=============================================================

2024年1月5日

更新的版本,解压密码 cc92d99a-ab87-43c3-99df-a5957ea53be9

=============================================================

最新版本更新在 https://www.lab-z.com/intru/ 页面

Step to UEFI (240)分析第一条 Debug Log 的输出方法

前面提到,Debug Log 中的第一条信息位于 SecMain.c 中的 SecCoreStartupWithStack() 函数中:

  DEBUG ((DEBUG_INFO,
    "SecCoreStartupWithStack(0x%x, 0x%x)\n",
    (UINT32)(UINTN)BootFv,
    (UINT32)(UINTN)TopOfCurrentStack
));

这次就分析一下 OVMF 这段代码的具体实现方式。DEBUG 这个宏,之前我们有分析过,有兴趣的读者可以在【参考1】看到之前的文章。

1.这里的 DEBUG 宏可以在  \MdePkg\Include\Library/DebugLib.h 看到,这个和我们之前遇到的是完全相同的定义:

/**
  Macro that calls DebugPrint().

  If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
  bit of PcdDebugProperyMask is set, then this macro passes Expression to
  DebugPrint().

  @param  Expression  Expression containing an error level, a format string,
                      and a variable argument list based on the format string.


**/
#if !defined(MDEPKG_NDEBUG)
  #define DEBUG(Expression)        \
    do {                           \
      if (DebugPrintEnabled ()) {  \
        _DEBUG (Expression);       \
      }                            \
    } while (FALSE)
#else
  #define DEBUG(Expression)
#endif

1.1 其中的 DebugPrintEnabled () 定义在 \OvmfPkg\Library\PlatformDebugLibIoPort\DebugLib.c 文件中,OVMF 这个函数是检查编译期定义

/**
  Returns TRUE if DEBUG() macros are enabled.

  This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
  PcdDebugProperyMask is set.  Otherwise FALSE is returned.

  @retval  TRUE    The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
  @retval  FALSE   The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.

**/
BOOLEAN
EFIAPI
DebugPrintEnabled (
  VOID
  )
{
  return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) &amp; DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
}

1.2 _DEBUG 的定义在 DebugLib.h 文件中,其中 DebugPrint() 是真正实现功能的函数

/**
  Internal worker macro that calls DebugPrint().

  This macro calls DebugPrint() passing in the debug error level, a format
  string, and a variable argument list.
  __VA_ARGS__ is not supported by EBC compiler, Microsoft Visual Studio .NET 2003
  and Microsoft Windows Server 2003 Driver Development Kit (Microsoft WINDDK) version 3790.1830.

  @param  Expression  Expression containing an error level, a format string,
                      and a variable argument list based on the format string.

**/

#if !defined(MDE_CPU_EBC) &amp;&amp; (!defined (_MSC_VER) || _MSC_VER > 1400)
  #define _DEBUG_PRINT(PrintLevel, ...)              \
    do {                                             \
      if (DebugPrintLevelEnabled (PrintLevel)) {     \
        DebugPrint (PrintLevel, ##__VA_ARGS__);      \
      }                                              \
    } while (FALSE)
  #define _DEBUG(Expression)   _DEBUG_PRINT Expression
#else
#define _DEBUG(Expression)   DebugPrint Expression
#endif

1.2.1 \OvmfPkg\Library\PlatformDebugLibIoPort\DebugLib.c 中定义了 DebugPrint() 函数:

/**
  Prints a debug message to the debug output device if the specified error level is enabled.

  If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
  GetDebugPrintErrorLevel (), then print the message specified by Format and the
  associated variable argument list to the debug output device.

  If Format is NULL, then ASSERT().

  @param  ErrorLevel  The error level of the debug message.
  @param  Format      Format string for the debug message to print.
  @param  ...         Variable argument list whose contents are accessed
                      based on the format string specified by Format.

**/
VOID
EFIAPI
DebugPrint (
  IN  UINTN        ErrorLevel,
  IN  CONST CHAR8  *Format,
  ...
  )
{
  VA_LIST         Marker;

  VA_START (Marker, Format);
  DebugVPrint (ErrorLevel, Format, Marker);
  VA_END (Marker);
}

1.2.2 同一个文件中,定义了函数DebugVPrint()

/**
  Prints a debug message to the debug output device if the specified
  error level is enabled.

  If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
  GetDebugPrintErrorLevel (), then print the message specified by Format and
  the associated variable argument list to the debug output device.

  If Format is NULL, then ASSERT().

  @param  ErrorLevel    The error level of the debug message.
  @param  Format        Format string for the debug message to print.
  @param  VaListMarker  VA_LIST marker for the variable argument list.

**/
VOID
EFIAPI
DebugVPrint (
  IN  UINTN         ErrorLevel,
  IN  CONST CHAR8   *Format,
  IN  VA_LIST       VaListMarker
  )
{
  DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);
}

1.2.3 同一个文件中 DebugPrintMarker() 代码如下:

/**
  Prints a debug message to the debug output device if the specified
  error level is enabled base on Null-terminated format string and a
  VA_LIST argument list or a BASE_LIST argument list.

  If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
  GetDebugPrintErrorLevel (), then print the message specified by Format and
  the associated variable argument list to the debug output device.

  If Format is NULL, then ASSERT().

  @param  ErrorLevel      The error level of the debug message.
  @param  Format          Format string for the debug message to print.
  @param  VaListMarker    VA_LIST marker for the variable argument list.
  @param  BaseListMarker  BASE_LIST marker for the variable argument list.

**/
VOID
DebugPrintMarker (
  IN  UINTN         ErrorLevel,
  IN  CONST CHAR8   *Format,
  IN  VA_LIST       VaListMarker,
  IN  BASE_LIST     BaseListMarker
  )
{
  CHAR8    Buffer[MAX_DEBUG_MESSAGE_LENGTH];
  UINTN    Length;

  //
  // If Format is NULL, then ASSERT().
  //
  ASSERT (Format != NULL);

  //
  // Check if the global mask disables this message or the device is inactive
  //
  if ((ErrorLevel &amp; GetDebugPrintErrorLevel ()) == 0 ||
      !PlatformDebugLibIoPortFound ()) {
    return;
  }

  //
  // Convert the DEBUG() message to an ASCII String
  //
  if (BaseListMarker == NULL) {
    Length = AsciiVSPrint (Buffer, sizeof (Buffer), Format, VaListMarker);
  } else {
    Length = AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);
  }

  //
  // Send the print string to the debug I/O port
  //
  IoWriteFifo8 (PcdGet16 (PcdDebugIoPort), Length, Buffer);
}

其中PcdDebugIoPort定义在 \OvmfPkg\OvmfPkg.dec文件中:

  ## This flag is used to control the destination port for PlatformDebugLibIoPort
  gUefiOvmfPkgTokenSpaceGuid.PcdDebugIoPort|0x402|UINT16|4

因此,就是说这里的DEBUG 是通过向 0x402 直接写入 ASCII 来实现的。

参考:

1. https://www.lab-z.com/stu170dbg/

注册表关闭 ModernStandby 的方法

在注册表 HKLM \ System \ CurrentControlSet \ Control \ Power 创建 PlatformAoAcOverride 属性为 DWORD32 值为0.

上述方法来自:

1.https://zhuanlan.zhihu.com/p/339761713 在Win10 v2004以上版本启用S3睡眠并禁用Modern Standby待机的终极解决方案

2.http://blog.sinovale.com/3867.html 戴尔dell笔记本无法进入睡眠的解决办法

3.https://www.reddit.com/r/Dell/comments/h0r56s/getting_back_s3_sleep_and_disabling_modern/

Step to UEFI (239)MainAsm跳转到SecEntry的证明

前面提到了 Main.asm 中的 Main16 最后会跳转到 SecEntry.nasm 中,这次用实验来证明这一点。

在Main.asm跳转代码如下,前面找到的地址放在 rsi 中,然后直接跳转到该地址:

BITS    64

    ;
    ; Some values were calculated in 32-bit mode.  Make sure the upper
    ; 32-bits of 64-bit registers are zero for these values.
    ;
    mov     rax, 0x00000000ffffffff
    and     rsi, rax
    and     rbp, rax
    and     rsp, rax

    ;
    ; RSI - SEC Core entry point
    ; RBP - Start of BFV
    ;

    ;
    ; Restore initial EAX value into the RAX register
    ;
    mov     rax, rsp

    ;
    ; Jump to the 64-bit SEC entry point
    ;
    jmp     rsi

这里加入代码,从0x402 Port输出 rsi的值:

    ;
    ; Restore initial EAX value into the RAX register
    ;
    mov     rax, rsp

    mov     rax,rsi
    mov     dx,0x402
	
	; 7-0 Bits
    out     dx,al       

    ; 15-8 Bits
	shr     rax,8
    out     dx,al       
	
	; 23-16 Bits
	shr     rax,8
    out     dx,al       

	; 31-24 Bits
	shr     rax,8
    out     dx,al       

	; 39-32 Bits
	shr     rax,8
    out     dx,al       

	; 47-40 Bits
	shr     rax,8
    out     dx,al       

	; 55-48 Bits
	shr     rax,8
    out     dx,al       

	; 63-56 Bits
	shr     rax,8
    out     dx,al       

接下来在SecEntry.nasm添加代码,输出当前 rip 的值:

;
; SecCore Entry Point
;
; Processor is in flat protected mode
;
; @param[in]  RAX   Initial value of the EAX register (BIST: Built-in Self Test)
; @param[in]  DI    'BP': boot-strap processor, or 'AP': application processor
; @param[in]  RBP   Pointer to the start of the Boot Firmware Volume
; @param[in]  DS    Selector allowing flat access to all addresses
; @param[in]  ES    Selector allowing flat access to all addresses
; @param[in]  FS    Selector allowing flat access to all addresses
; @param[in]  GS    Selector allowing flat access to all addresses
; @param[in]  SS    Selector allowing flat access to all addresses
;
; @return     None  This routine does not return
;
global ASM_PFX(_ModuleEntryPoint)
ASM_PFX(_ModuleEntryPoint):
    lea     rax,[$]
    mov     dx,0x402
	
	; 7-0 Bits
    out     dx,al       

    ; 15-8 Bits
	shr     rax,8
    out     dx,al       
	
	; 23-16 Bits
	shr     rax,8
    out     dx,al       

	; 31-24 Bits
	shr     rax,8
    out     dx,al       

	; 39-32 Bits
	shr     rax,8
    out     dx,al       

	; 47-40 Bits
	shr     rax,8
    out     dx,al       

	; 55-48 Bits
	shr     rax,8
    out     dx,al       

	; 63-56 Bits
	shr     rax,8
    out     dx,al     

    ;
    ; Fill the temporary RAM with the initial stack value.
    ; The loop below will seed the heap as well, but that's harmless.
    ;
    mov     rax, (FixedPcdGet32 (PcdInitValueInTempStack) << 32) | FixedPcdGet32 (PcdInitValueInTempStack)
                                                              ; qword to store
    mov     rdi, FixedPcdGet32 (PcdOvmfSecPeiTempRamBase)     ; base address,
                                                              ;   relative to
                                                              ;   ES
    mov     rcx, FixedPcdGet32 (PcdOvmfSecPeiTempRamSize) / 8 ; qword count
    cld                                                       ; store from base
                                                              ;   up
    rep stosq

在 QEMU 上运行后,用十六进制工具查看 debug.log:

可以看到输出的 0xFFFC C374, 这样可以证明跳转到 SecEntry.nasm 继续执行的。

记录一个“诡异”的矩阵键盘问题

最近用船型开关做了一个矩阵键盘,电路图如下:

简单的说就是 KEY_ROW1-3 轮流为 HIGH 然后通过读取 KEY_COL1 引脚的电平就能得到当前开关状态。编写的取得按键信息的代码如下:

int GetPressed() {
  int result = 0;
  
  digitalWrite(KEY_ROW1, HIGH);
  digitalWrite(KEY_ROW2, LOW);
  digitalWrite(KEY_ROW3, LOW);

  // Upper line
  result = (digitalRead(KEY_COL1) << 3) +
           (digitalRead(KEY_COL2) << 2) +
           (digitalRead(KEY_COL3) << 1) +
           digitalRead(KEY_COL4);

  // Middle line
  digitalWrite(KEY_ROW1, LOW);
  digitalWrite(KEY_ROW2, HIGH);
  digitalWrite(KEY_ROW3, LOW);

  result = ((digitalRead(KEY_COL1) << 3) +
            (digitalRead(KEY_COL2) << 2) +
            (digitalRead(KEY_COL3) << 1) +
            (digitalRead(KEY_COL4))) +
           result * 10;
  
  
  // Buttom line
  digitalWrite(KEY_ROW1, LOW);
  digitalWrite(KEY_ROW2, LOW);
  digitalWrite(KEY_ROW3, HIGH);
  result = (digitalRead(KEY_COL1) << 3) +
           (digitalRead(KEY_COL2) << 2) +
           (digitalRead(KEY_COL3) << 1) +
           (digitalRead(KEY_COL4) << 0) +
           result * 10;
  return result;
}

调试中我惊奇的发现每次读取KEY_COL1的值竟然不同,示意如下:

即使我将 digitalRead直接写成(GPIO.in >> KEY_COL1) & 0x1 也有同样的现象。

于是针对这个问题做了如下实验:

  1. 直接在 FireBeetle上跑,不插入在矩阵键盘中,无现象;
  2. 将 x 定义为全局变量,同样现象;
  3. 试图关掉ESP32 的编译优化功能,没有找到对应项目

从1的结果判断这个问题和我的硬件是有关的,最终拿出了示波器,果真,上了之后能够看到如下波形(黄色是KEY_COL1,绿色是KEY_ROW1):可以看到当拉高之后接收端马上变高,但是拉低之后过了很久才会降低。于是怀疑的点就是读取太快,上一个拉高的影响还没有完全结束,导致了误判。

有了上面的猜想就编写一个代码, 不断拉高ROW1 和 ROW3 ,然后看看是否会出现这样的问题,结果显示:如果一个 ROW上有一个按键是闭合的,那么另外一个 ROW的读取很大概率产生误判。

  digitalWrite(KEY_ROW1, HIGH);
  digitalWrite(KEY_ROW2, LOW);
  digitalWrite(KEY_ROW3,  LOW);

c1=(digitalRead(KEY_COL1) &lt;&lt; 3) +
           (digitalRead(KEY_COL2) &lt;&lt; 2) +
           (digitalRead(KEY_COL3) &lt;&lt; 1) +
           digitalRead(KEY_COL4);

  digitalWrite(KEY_ROW1, LOW);
  digitalWrite(KEY_ROW2, LOW);
  digitalWrite(KEY_ROW3,  HIGH);

c2=(digitalRead(KEY_COL1) &lt;&lt; 3) +
           (digitalRead(KEY_COL2) &lt;&lt; 2) +
           (digitalRead(KEY_COL3) &lt;&lt; 1) +
           digitalRead(KEY_COL4);

下面黄色是KEY_COL1 Pin (读取)和KEY_ROW3(发送)的波形图(我的示波器只有两个通道),可以看到ROW1拉高的影响持续了到KEY_ROW3的发生,因此再次读取 KEY_COL1的结果是错误的。

找到了原因问题就好解决了,每次 digitalRead() 之后加一个一个 delay 让它完成放电就能保证结果的正确性了。

所谓

矩阵键盘惹人愁,

现象诡异结果谬。

I/O 若水不停歇,

CPU更快似光流。