UEFI学习(一)-- HelloWorld

怎么下载编译环境以及成功编译可以看我上一个博客。
EDK2环境搭建–Windows

ShellPkg下的helloworld

ShellPkg使用的入口函数是:
INTN
EFIAPI
ShellAppMain (
  IN UINTN   Argc,
  IN CHAR16  **Argv
  )

所以在 helloworld.h 内要添加四个头文件:

#include <Uefi.h>
#include <Library/ShellCEntryLib.h>
#include <Library/ShellLib.h>
#include <Library/UefiBootServicesTableLib.h>

helloworld.inf 配置为:

[defines]
INF_VERSION                    = 0x00010005
BASE_NAME                      = helloworld
FILE_GUID                      = 6987936E-ED34-44db-AE97-1FA5E4ED2116 
MODULE_TYPE                    = UEFI_APPLICATION
VERSION_STRING                 = 1.0
ENTRY_POINT                    = ShellCEntryLib

[Sources]
helloworld.c
helloworld.h

[Packages]
MdePkg/MdePkg.dec
ShellPkg/ShellPkg.dec

[LibraryClasses]
ShellCEntryLib
ShellLib
UefiBootServicesTableLib

C code 为:

#include "helloworld.h" 

#define In             gST->ConIn
#define Out            gST->ConOut

INTN
EFIAPI
ShellAppMain (
  IN UINTN   Argc,
  IN CHAR16  **Argv
  )
{

	Out->OutputString(Out,L"helloworld!");

	return 0;
}

但是很尴尬的是,ShellPkg下无法使用模拟器直接进入Shell环境,所以==>

EmulatorPkg下的helloworld

EmulatorPkg的函数入口一般使用:
EFI_STATUS
EFIAPI
UefiMain (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )

所以在 helloworld.h 内要添加两个头文件:

#include <Uefi.h>
#include <Library/UefiBootServicesTableLib.h>

helloworld.inf 配置为:

[defines]
INF_VERSION                    = 0x00010005
BASE_NAME                      = helloworld
FILE_GUID                      = 6987936E-ED34-44db-AE97-1FA5E4ED2116 
MODULE_TYPE                    = UEFI_APPLICATION
VERSION_STRING                 = 1.0
ENTRY_POINT                    = UefiMain

[Sources]
helloworld.c
helloworld.h

[Packages]
MdePkg/MdePkg.dec

[LibraryClasses]
UefiBootServicesTableLib
UefiApplicationEntryPoint
UefiLib

C code 为:

#include "myhelloworld.h"

#define In             gST->ConIn
#define Out            gST->ConOut

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

	Out->OutputString(Out,L"helloworld!");

	return EFI_SUCCESS;
}

编译成功之后,生成的EFI在 edk2-edk2-stable202305\Build\EmulatorX64\DEBUG_VS2019\X64\EmulatorPkg\Application\helloworld\helloworld\OUTPUT

需要注意的是:编译之前一定要把target.txt内的TARGET_ARCH参数修改为X64,不然模拟器用不了。

模拟器路径:
edk2-edk2-stable202305\Build\EmulatorX64\DEBUG_VS2019\X64\WinHost.exe

打开模拟器之后运行efi文件,就可以得到一个helloworld的输出了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值