UEFI学习(二)-- 搭建属于自己的Package和Library

本文介绍了如何在UEFI环境中创建自己的Pkg包,编写HelloWorld程序,并构建自定义Library。通过建立框架,配置DEC和DSC文件,以及在项目中引用自定义Library,简化代码复用和管理。最后,展示了如何在其他Pkg中调用自定义Library的函数,提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为说之前有接触过UEFI,所以对框架有一些了解,在编写HelloWorld成功后,就想开创自己的Pkg,方便平时寻找自己写的code和编译出来的efi文件。

之前关于EDK2的环境搭建以及怎么编译HelloWorld都在专栏内,想看的可以自己翻。

这次学习需要用到更改GUID的工具,链接为:GUID生成工具

一、搭建框架

我们先仿照其他的Pkg,创建文件/文件夹搭出来框架。
目前框架为:
OemPkg_Frame

ApplicationInclude\LibraryLibrary
这个里面存放自己编写的Code文件,包括.inf .c .h之类的这里面存放自己创建的Library的.h文件这里面存放自己创建的Library的.inf .c文件
直接在OemPkg下去新建OemPkg.dec和OemPkg.dsc两个文件

二、创建Package

创建属于自己的Package重点在于.dec 和 .dsc两个文件
.dec的code为:

[Defines]
  DEC_SPECIFICATION              = 0x00010005
  PACKAGE_NAME                   = OemPkg
  PACKAGE_GUID                   = 4fabc2a5-5b5b-430d-8399-c5daa0024fe7
  PACKAGE_VERSION                = 1.02
//GUID记得更改

.dsc的code为:

[Defines]
  PLATFORM_NAME                  = OemPkg
  PLATFORM_GUID                  = b190f3ad-8814-4ffb-a915-f85cad5a59d3
  PLATFORM_VERSION               = 0.01
  DSC_SPECIFICATION              = 0x00010005
  OUTPUT_DIRECTORY               = Build/OemPkg
  SUPPORTED_ARCHITECTURES        = IA32|X64
  BUILD_TARGETS                  = DEBUG|RELEASE|NOOPT
  SKUID_IDENTIFIER               = DEFAULT
  DEFINE DEBUG_ENABLE_OUTPUT     = FALSE

#!include MdePkg/MdeLibs.dsc.inc

[LibraryClasses]
  #
  # Entry Point Libraries
  #
  UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf
  ShellCEntryLib|ShellPkg/Library/UefiShellCEntryLib/UefiShellCEntryLib.inf
  UefiDriverEntryPoint|MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf
  #
  # Common Libraries
  #
  RegisterFilterLib|MdePkg/Library/RegisterFilterLibNull/RegisterFilterLibNull.inf #Hermes_Debug+
  BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
  BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
  UefiLib|MdePkg/Library/UefiLib/UefiLib.inf
  PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf
  PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
  MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf
  UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf
  UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
  !if $(DEBUG_ENABLE_OUTPUT)
    DebugLib|MdePkg/Library/UefiDebugLibConOut/UefiDebugLibConOut.inf
    DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/BaseDebugPrintErrorLevelLib.inf
  !else   ## DEBUG_ENABLE_OUTPUT
    DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf
  !endif  ## DEBUG_ENABLE_OUTPUT

  DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
  PeCoffGetEntryPointLib|MdePkg/Library/BasePeCoffGetEntryPointLib/BasePeCoffGetEntryPointLib.inf
  IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
  PciLib|MdePkg/Library/BasePciLibCf8/BasePciLibCf8.inf
  PciCf8Lib|MdePkg/Library/BasePciCf8Lib/BasePciCf8Lib.inf
  SynchronizationLib|MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf
  UefiRuntimeLib|MdePkg/Library/UefiRuntimeLib/UefiRuntimeLib.inf
  HiiLib|MdeModulePkg/Library/UefiHiiLib/UefiHiiLib.inf
  UefiHiiServicesLib|MdeModulePkg/Library/UefiHiiServicesLib/UefiHiiServicesLib.inf
  PerformanceLib|MdeModulePkg/Library/DxePerformanceLib/DxePerformanceLib.inf
  HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf
  FileHandleLib|MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.inf
  SortLib|MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf

  ShellLib|ShellPkg/Library/UefiShellLib/UefiShellLib.inf

  CacheMaintenanceLib|MdePkg/Library/BaseCacheMaintenanceLib/BaseCacheMaintenanceLib.inf

  #lbdebug
	DxeServicesLib|MdePkg/Library/DxeServicesLib/DxeServicesLib.inf

[Components]
  

在Application 文件夹下放入UEFI学习(一)中创建好的HelloWorld,再在dsc文件下的Components模块下加入对应的路径

另外,在Conf 下的 target.txt 文件中,修改 ACTIVE_PLATFORM = OemPkg/OemPkg.dsc

运行build就可以成功编译了。

三、创建属于自己的Library

UEFI编写的东西叫做EFI驱动嘛,但是每次都是调用EDK2里面写好的Lirbrary,总感觉差了点意思,

所以我这里新建了一个属于自己的Library,之后编写EFI驱动的时候就可以调用自己Lirbrary里面的函数了,听起来就很带感。

框架和Package都搭好了,创建Library也就很简单了,

1. 框架

按照 一、框架 中的内容,在 Include -> Library 路径下创建 OemVarLib.h文件
在Library -> OemBarLib路径下创建 OemVarLib.c 和 OemVarLib.inf 文件

2.在.dec中声明Lirbrary文件

直接在code后面添加[Include]模块和[LibraryClasses]模块就行了.
code为:

[Defines]
  DEC_SPECIFICATION              = 0x00010005
  PACKAGE_NAME                   = OemPkg
  PACKAGE_GUID                   = 4fabc2a5-5b5b-430d-8399-c5daa0024fe7
  PACKAGE_VERSION                = 1.02

[Includes]
  Include 

[LibraryClasses]
  OemVarLib|Include/Library/OemVarLib.h

3.在.dsc中声明Lirbrary文件

code为:

[Defines]
  PLATFORM_NAME                  = OemPkg
  PLATFORM_GUID                  = b190f3ad-8814-4ffb-a915-f85cad5a59d3
  PLATFORM_VERSION               = 0.01
  DSC_SPECIFICATION              = 0x00010005
  OUTPUT_DIRECTORY               = Build/OemPkg
  SUPPORTED_ARCHITECTURES        = IA32|X64
  BUILD_TARGETS                  = DEBUG|RELEASE|NOOPT
  SKUID_IDENTIFIER               = DEFAULT
  DEFINE DEBUG_ENABLE_OUTPUT     = FALSE

#!include MdePkg/MdeLibs.dsc.inc

[LibraryClasses]
  #
  # Entry Point Libraries
  #
  UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf
  ShellCEntryLib|ShellPkg/Library/UefiShellCEntryLib/UefiShellCEntryLib.inf
  UefiDriverEntryPoint|MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf
  #
  # Common Libraries
  #
  RegisterFilterLib|MdePkg/Library/RegisterFilterLibNull/RegisterFilterLibNull.inf #Hermes_Debug+
  BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
  BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
  UefiLib|MdePkg/Library/UefiLib/UefiLib.inf
  PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf
  PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
  MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf
  UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf
  UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
  !if $(DEBUG_ENABLE_OUTPUT)
    DebugLib|MdePkg/Library/UefiDebugLibConOut/UefiDebugLibConOut.inf
    DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/BaseDebugPrintErrorLevelLib.inf
  !else   ## DEBUG_ENABLE_OUTPUT
    DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf
  !endif  ## DEBUG_ENABLE_OUTPUT

  DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
  PeCoffGetEntryPointLib|MdePkg/Library/BasePeCoffGetEntryPointLib/BasePeCoffGetEntryPointLib.inf
  IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
  PciLib|MdePkg/Library/BasePciLibCf8/BasePciLibCf8.inf
  PciCf8Lib|MdePkg/Library/BasePciCf8Lib/BasePciCf8Lib.inf
  SynchronizationLib|MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf
  UefiRuntimeLib|MdePkg/Library/UefiRuntimeLib/UefiRuntimeLib.inf
  HiiLib|MdeModulePkg/Library/UefiHiiLib/UefiHiiLib.inf
  UefiHiiServicesLib|MdeModulePkg/Library/UefiHiiServicesLib/UefiHiiServicesLib.inf
  PerformanceLib|MdeModulePkg/Library/DxePerformanceLib/DxePerformanceLib.inf
  HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf
  FileHandleLib|MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.inf
  SortLib|MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf

  ShellLib|ShellPkg/Library/UefiShellLib/UefiShellLib.inf

  CacheMaintenanceLib|MdePkg/Library/BaseCacheMaintenanceLib/BaseCacheMaintenanceLib.inf

  #lbdebug
	DxeServicesLib|MdePkg/Library/DxeServicesLib/DxeServicesLib.inf

  OemVarLib|OemPkg/Library/OemVarLib/OemVarLib.inf

[Components]
  OemPkg/Application/myhelloworld/myhelloworld.inf

4.更改HelloWorld的code

.h

#ifndef _MYHELLOWORLD_
#define _MYHELLOWORLD_

#include <Uefi.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/OemVarLib.h>  //添加要调用的函数的头文件

#endif //_MYHELLOWORLD_

.c

#include "myhelloworld.h"

EFI_STATUS
EFIAPI
UefiMain (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{

	//Out->OutputString(Out,L"helloworld!");
  Oem_OutputStringHelloWorld(); //被调用的函数

	return EFI_SUCCESS;
}

都弄好之后,编译!就可以成功的输出HelloWorld了!

之后在其他的Pkg内也可以用同样的方法去调用函数,就不需要每次都在C code内再去编写函数了,特别方便的嘞!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值