ThinkNotes

Simple is not easy | 化繁为简,知易行难

0%

UEFI开发(二)

UEFI开发(二)

Library库

https://blog.csdn.net/weixin_45279063/article/details/115324601

以后面要用到的DEBUG库为例:

库的调用方是MdeModulePkg下的某个INF模块,被调用方是MdePkg库(这个属于基础库很常用)

  1. 调用方的INF中要导入库模块的dec和要用到的LibraryClasses
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Packages]
MdePkg/MdePkg.dec #这里包含了库模块MdePkg的dec声明,获取到MdePkg所有对外开放的库接口
MdeModulePkg/MdeModulePkg.dec #这是调用方模块

[LibraryClasses]
DevicePathLib
UefiBootServicesTableLib
UefiRuntimeServicesTableLib
MemoryAllocationLib
BaseMemoryLib
UefiLib
BaseLib
UefiDriverEntryPoint
DebugLib #这里导入了MdePkg库的DebugLib类(头文件),类似其他语言的import或者include namespace
PcdLib
  1. 调用方的c代码中可以直接include库的头文件(不需要用相对地址包含头文件)
1
#include <Library/DebugLib.h>
  1. 调用方使用库的函数体DEBUG();
1
2
3
4
5
6
7
8
DEBUG((DEBUG_INFO, "Initializing XXX controller in slot %d\n", Slot));

Status = InitController(PciIo, Slot);
if (EFI_ERROR(Status))
{
DEBUG((DEBUG_ERROR, "Failed to switch Host to PCIe mode: %r\n", Status));
return Status;
}

DEBUG打印

https://github.com/tianocore/tianocore.github.io/wiki/EDK-II-Debugging

在使用DEBUG INFO的MdeModulePkg模块的dsc中,声明DEBUG库的全局变量PcdDebugPropertyMask和PcdDebugPrintErrorLevel,指定支持INFO级别打印,否则默认只会打印DEBUG(ERROR)

1
2
3
4
[PcdsFixedAtBuild]
# refer to https://github.com/tianocore/tianocore.github.io/wiki/EDK-II-Debugging
gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x0f
gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x80000040

再编译MdeModulePkg inf,输出的efi可见INFO级别的打印生效。

UTF8中文报错

问题log:

1
2
C:\edk2\MdeModulePkg\Bus\Pci\SdMmcPciHcDxe\BayhubHost.h(1): error C2220: the following warning is treated as an error
C:\edk2\MdeModulePkg\Bus\Pci\SdMmcPciHcDxe\BayhubHost.h(1): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss

错误信息指出:
警告C4819被当作错误处理
文件包含无法在当前代码页(936,即中文GBK编码)中表示的字符

解决方法

1
2
3
使用Notepad++重新保存文件:
打开文件,编码选择"UTF-8 with signature (UTF-8-BOM)"或者"UTF-8"
保存文件

原因:

1
2
文件有非ASCII字符(特别是在注释中)
有时文件的首行可能有不可见的BOM(字节顺序标记)或其他特殊字符