怎么下载编译环境以及成功编译可以看我上一个博客。
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的输出了。