You are on page 1of 11

🧱

EDK2 環境建置
Write by @HaCoMaTaTa (Neil)

Windows
󾠮前置作業
Visual Studio
Python
Nasm
ASL Compiler
下載EDK2
󾠯EDK2 設定
編譯Base tool
修改target.txt檔案 (位於 )
󾠰編譯 Hello World
Conf\target.txt

⌨編譯 UEFI Shell模擬器


Qemu 虛擬機器上執⾏UEFI Shell
󾠱EDK2 環境⾃動下載建置
資料蒐集
Linux
參考資料

Windows

󾠮前置作業
Visual Studio
1. 下載 Visual Studio Community (下載點)
@2022/05/12 官⽅使⽤版本為VS2019, 經筆者測試 VS2022 也可使⽤

EDK2 環境建置 1
2. 選擇下列元件後安裝

Python
1. 下載並安裝Python (下載點)
@2022/05/12 截⾄⽬前 官⽅建議Python 版本≥3.7

的安裝過程需要注意要勾選:Add Python 3.x to PATH


Python

Nasm
1. 下載Nasm並安裝在 下 (下載點)
C:\nasm

@2022/05/12 依照EDK2官網⽂件 建議版本⾄少為2.15.05

ASL Compiler
1. 下載 (下載點)
@2022/05/12 EDK2官網⽂件
2. 解壓縮後放置 C:\ASL

EDK2 環境建置 2
下載EDK2
⽬前EDK2 的程式碼 Tianocore 都放到GitHub 上, 故須從GitHub 上下載

1. 從 GitHub 下載穩定版本的Source Code (連接)


a. 以 為例, 點擊進⼊後下載
edk2-stable202111

當中
b. 為主程式, 其餘
edk2-edk2-stable202111.zip submodule- 為必要模組, 全部下載下來
2. 全部解壓後 ,將 submodule- 解壓縮後的檔案移動到 覆蓋
edk2-edk2-stable202111

EDK2 環境建置 3
󾠯EDK2 設定
編譯Base tool
1. 打開 x64_x86_cross Tools Command Prompt

2. 打⼊下列指令編譯

# 假設
Source Code 放在
C:\edk2 下
C:\edk2> edksetup.bat Rebuild

3. 成功後會在 BaseTools\Bin 下產⽣ Win32 folder

修改target.txt檔案 (位於 Conf\target.txt )


將當中的 TARGET TARGET_ARCH TOOL_CHAIN_TAG 修改如下
# TARGET List Optional Zero or more of the following: DEBUG, RELEASE, NOOPT
# UserDefined; separated by a space character.
# If the line is missing or no value is specified, all
# valid targets specified in the platform description file
# will attempt to be built. The following line will build
# DEBUG platform target.
TARGET = RELEASE

# TARGET_ARCH List Optional What kind of architecture is the binary being target for.
# One, or more, of the following, IA32, IPF, X64, EBC, ARM
# or AArch64.
# Multiple values can be specified on a single line, using
# space characters to separate the values. These are used
# during the parsing of a platform description file,
# restricting the build output target(s.)
# The Build Target ARCH is determined by (precedence high to low):

EDK2 環境建置 4
# Command-line: -a ARCH option
# target.txt: TARGET_ARCH values
# DSC file: [Defines] SUPPORTED_ARCHITECTURES tag
# If not specified, then all valid architectures specified
# in the platform file, for which tools are available, will be
#
TARGET_ARCH = X64

# TAGNAME List Optional Specify the name(s) of the tools_def.txt TagName to use.
# If not specified, all applicable TagName tools will be
# used for the build. The list uses space character separation.
TOOL_CHAIN_TAG = VS2019

󾠰編譯 Hello World


1. 打開 x86_x64_cross Tools Command Prompt

打⼊下列指令, 編譯hello world


#編譯位於 MdeModulePkg\Application\HelloWrold下的 HelloWorld
build -a X64 -p MdeModulePkg\MdeModulePkg.dsc -m MdeModulePkg\Application\HelloWorld\HelloWorld.inf

2. 編譯成功後 可在 Build\MdeModule\RELEASE_VS2019\X64 下找到編譯後的檔案 HelloWorld.efi

⌨編譯 UEFI Shell模擬器


由於有時候⾝邊沒有實體機器, 這邊我們編譯 UEFI Shell 模擬器, ⼀些簡單的程式可於模擬器上做驗證
1. 打開 x86_x64_cross Tools Command Prompt

2. 於edk2跟⽬錄下,打⼊下列指令, 編譯 EmulatorPkg, 第⼀次編譯會需要點時間

EDK2 環境建置 5
build -a X64 -p EmulatorPkg\EmulatorPkg.dsc

⚠ 如編譯時出現 以下錯誤
LINK : fatal error LNK1181: cannot open input file 'Gdi32.lib’

確認EDK2所使⽤的Winodws SDK 版號\um\x64


Windows Kits\10\Lib\ 下是否有檔案 GDI32.LIB

如否, 則需安裝 缺失的部分


可參考來源 與 安裝⽅式
3. 編譯完成畫⾯如下

4. 執⾏位於 Build\EmulatorX64\RELEASE_VS2019\X64 底下的 WinHost.exe 啟動

5. 將先前編譯的 HelloWorld.efi 複製到 Build\EmulatorX64\RELEASE_VS2019\X64 底下


6. 如圖打⼊指令執⾏ HelloWorld.efi

EDK2 環境建置 6
Qemu 虛擬機器上執⾏UEFI Shell
OVMF(Open Virtual Machine Firmware) 是使⽤於虛擬機上的UEFI韌體。EDK2提供了相應製作OVMF的⽅法

📝 前置作業 需先下載安裝
[ ] Qemu ( 連結)

1. 打⼊指令編譯OVMF
build -a X64 -p OvmfPkg\OvmfPkgX64.dsc

2. 建置成功後 ,將編譯好的 OVMF.fd 複製到QEMU的安裝⽬錄下 (如下例⼦)

3. 在命令提⽰字元下,切換⾄QEMU⽬錄並打⼊下列指令
當中的 是當進⼊Shell 下時所模擬的硬碟,測試的檔案皆可放進此資料夾,路徑跟資料夾可隨意修改
D:\TMP_EDK2_Area

qemu-system-x86_64.exe -bios "OVMF.fd" -hda fat:rw:D:\TMP_EDK2_Area -net none

EDK2 環境建置 7
4. 執⾏畫⾯

5. 之後如要測試各種tool ,可以將檔案放⼊ D:\TMP_EDK2_Area 中,以下是於QEMU 下執⾏ ru的圖

󾠱EDK2 環境⾃動下載建置
透過Chocolatey⾃動下載安裝所需軟體,再使⽤git下載EDK2,最後編譯並執⾏Qemu

1. 使⽤命令提⽰字元透過power shell 下載 Chocolately ,並利⽤Chocolately 安裝 EDK2 所需環境


@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "
[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://choco
latey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

EDK2 環境建置 8
choco install --force -y visualstudio2019-workload-vctools git tortoisegit Python3 nasm wget 7zip qemu
wget -x --no-check-certificate https://acpica.org/sites/acpica/files/iasl-win-20200528.zip -O %temp%/iasl-win-20200528.zip
7z x %temp%\iasl-win-20200528.zip -oC:\ASL
del /f /q %temp%\iasl-win-20200528.zip
rem Environment Vars (like PATH) have changed. Close/reopen your shell to see the changes (or in powershell/cmd.exe just type `
refreshenv`).
refreshenv
pause

2. 透過git 下載EDK2 source code


set EDK2_PLACE_PATH=c:\edk2
git clone https://github.com/tianocore/edk2.git %EDK2_PLACE_PATH%
cd /d %EDK2_PLACE_PATH%
git submodule init
git submodule update
pause

3. 編譯 EDK2 Build Tools


@echo off

:: VS Build tool path


set VS2019_PATH="C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat"

:: set location of EDK2 folder


set EDK2_LOCATION=c:\edk2

:: Switch to edk2 folder and start to build basetool for edk2


%VS2019_PATH% x86 && ^
cd /d %EDK2_LOCATION% && ^
edksetup.bat VS2019 && ^
cd /d %EDK2_LOCATION%\BaseTools && ^
nmake && ^
call %comspec% /k "cd %EDK2_LOCATION%"

4. 編譯 Qemu Package
@echo off

:: VS Build tool path


set VS2019_PATH="C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat"

:: NASM path
set NASM_PREFIX="C:\Program Files\NASM\"

:: set location of EDK2 folder


set EDK2_LOCATION=c:\edk2

:: Switch to edk2 folder and start to build basetool for edk2


%VS2019_PATH% x86_amd64 && ^
cd /d %EDK2_LOCATION% && ^
edksetup.bat VS2019 && ^
build -a X64 -t VS2019 -p OvmfPkg\OvmfPkgX64.dsc -b DEBUG && ^
pause

5. 執⾏Qemu
@echo off

:: set location of EDK2 folder


set EDK2_LOCATION=c:\edk2
set OVMF_PATH="%EDK2_LOCATION%\Build\OvmfX64\DEBUG_VS2019\FV\OVMF.fd"

:: File of QEMU
set QEMU_PATH="c:\Program Files\qemu\qemu-system-x86_64.exe"
:: Set Location That storage Test File
set QEMU_HDD_AREA=C:\TMP_EDK2_Area
set QEMU_CPU_MODEL=Skylake-Client-v4

EDK2 環境建置 9
mkdir %QEMU_HDD_AREA%
%QEMU_PATH% -machine q35 -cpu %QEMU_CPU_MODEL% ^
-vga cirrus -m 256M -serial stdio -pflash %OVMF_PATH% ^
-hda fat:rw:%QEMU_HDD_AREA% -debugcon file:debug.log -global isa-debugcon.iobase=0x402 ^
-usb ^
-device usb-ehci,id=ehci ^
-device usb-kbd -device usb-mouse

資料蒐集
EDK2 編譯介紹(Windows)
在前⾯幾篇有介紹過如何在 Ubuntu 下建置 edk2 的環境,這⼀篇就來介紹如何在 Windows 下建置 edk2 吧。
https://damn99.com/2020-06-24-edk2-build-introduction-at-windows/

Linux
🚧 施⼯中

參考資料
官⽅ EDK2 GitHub
GitHub - tianocore/edk2: EDK II
A modern, feature-rich, cross-platform firmware development environment for the UEFI and PI specifications
from www.uefi.org. More CI Build information - EmulatorPkg Ubuntu GCC5 Segfaults during execution. -
EmulatorPkg Ubuntu GCC5 Segfaults during execution. More ArmVirtPkg CI Build Information More
https://github.com/tianocore/edk2

官⽅ Windows 建置EDK2 步驟
Windows systems · tianocore/tianocore.github.io Wiki
You can't perform that action at this time. You signed in with another tab or window. You signed out in
another tab or window. Reload to refresh your session. Reload to refresh your session.

https://github.com/tianocore/tianocore.github.io/wiki/Windows-systems

官⽅OVMF 建置與執⾏

EDK2 環境建置 10
How to run OVMF · tianocore/tianocore.github.io Wiki
You can't perform that action at this time. You signed in with another tab or window. You signed out in
another tab or window. Reload to refresh your session. Reload to refresh your session.

https://github.com/tianocore/tianocore.github.io/wiki/How-to-run-OVMF

詳細的建置步驟
UEFI 开发历程1-edk2开发环境的搭建以及在虚拟机QEMU运⾏OVMF固件_⼀个平凡的学徒的博客-CSDN博客_edk2开发
1、下载Visual Studio,打开Visual Studio Installer,选择Comunity版本,⼯作负载的选择如图1-1所⽰,安装。 图1-1 备注:重新安装时
如需更换如图1-2的下载地址,步骤如下: (1)Win+R键。 (2)输⼊"regedit"进⼊注册表。 (3)打开⽬录
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\Setup (4)在如图1-3所⽰处,更改其中的路径值或删除项。 图 1-2 图
https://blog.csdn.net/qq_40945626/article/details/121460583

Ubuntu

EDK2 編譯介紹
介紹如何 在 Ubuntu 18.04 環境下編譯 EDK2 UEFI BIOS

https://damn99.com/2020-05-14-edk2-build-introduction/

編譯OVMF
Step to UEFI (68 ) - 编译⼀个能在 QEMU 上跑的BIOS - WWW.LAB-Z.COM
最近在看 《UEFI 原理与编程》,上⾯提到⼀款虚拟机可以运⾏指定的BIOS,那就是 QEMU (之前我也研究过
如何替换 VirtualBox 的BIOS,结果⾮常沮丧,他不⽀持独⽴的 BIOS ,这意味即便是要在ASL中修改⼀些代码
也要花费⼏个⼩时重新编译整个VirtualBox)。 书中对于如何在QEMU中跑起来没有详述,我花了⼀点时间搞
https://www.lab-z.com/qemuovmf/

EDK2 環境建置 11

You might also like