【ubuntu 21.10】EDK2 开发环境搭建记录

0.添加 Ubuntu 16.04 源

sudo apt install vim -y
sudo vim /etc/apt/sources.list

i进行编辑
在末端加上以下代码
Esc,再输入:wq退出

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse

接着输入

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 40976EAF437D05B5
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32

1.安装依赖

sudo apt-get install build-essential uuid-dev nasm gcc-10 gcc-5 g++-10 python iasl -y
sudo apt install lib32z1 python3-distutils python3-pil python3-git gettext libx11-dev libxext-dev -y

查看安装好的gcc g++版本

ls /usr/bin/gcc*
ls /usr/bin/g++*

改变优先级

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 100

2.获取源码

git clone --recursive https://github.com.cnpmjs.org/tianocore/edk2.git

如果你网速慢
在开始clone submodule时同时按CtrlC
随即输入

cd edk2
git submodule set-url ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3 https://github.com.cnpmjs.org/ucb-bar/berkeley-softfloat-3.git
git submodule set-url CryptoPkg/Library/OpensslLib/openssl https://github.com.cnpmjs.org/openssl/openssl.git
git submodule set-url BaseTools/Source/C/BrotliCompress/brotli https://github.com.cnpmjs.org/google/brotli.git
git submodule set-url UnitTestFrameworkPkg/Library/CmockaLib/cmocka https://git.cryptomilk.org/projects/cmocka.git
git submodule set-url ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3 https://github.com.cnpmjs.org/ucb-bar/berkeley-softfloat-3.git
git submodule set-url MdeModulePkg/Library/BrotliCustomDecompressLib/brotli https://github.com.cnpmjs.org/google/brotli.git
git submodule set-url MdeModulePkg/Universal/RegularExpressionDxe/oniguruma https://github.com.cnpmjs.org/kkos/oniguruma.git
git submodule set-url RedfishPkg/Library/JsonLib/jansson https://github.com.cnpmjs.org/akheron/jansson.git

git submodule update

3.编译base tool

make -C ./BaseTools

4.初始化编译环境

source edksetup.sh

5.命令行编译platform pkg 和 OVMF

build -p ./EmulatorPkg/EmulatorPkg.dsc -a X64 -b DEBUG -t GCC5 -D BUILD_64 -D UNIX_SEC_BUILD -n 4
build -a X64 -t GCC5 -p OvmfPkg/OvmfPkgX64.dsc

说明:

-p PLATFORMFILE: 目标平台描述文件
-a TARGETARCH: 目标平台X64/IA32
-b BUILDTARGET: 可选项(DEBUG, RELEASE, NOOPT),将只编译dsc文件中特定的模块
-m MODULEFILE: 编译目标module
-t TOOLCHAIN : 使用目标编译器编译
-n THREADNUMBER : 多线程编译
-D MACROS: Macro格式: “Name [= Value]”,传入宏定义

6.运行

cd ./Build/EmulatorX64/DEBUG_GCC5/X64
./Host
qemu-system-x86_64 -bios Build/OvmfX64/DEBUG_GCC5/FV/OVMF.fd -M pc -m 2048 -cpu qemu64 -vga cirrus -name UEFI -boot order=dc

若编译成功,但是启动Emulator错误
原因和GCC优化有关,指定 -O0即可
打开edk2/Conf/tool_def,把相关的编译参数进行替换 -Os改为-O0 ​

2 Likes