参考
- 《UEFI编程实践》
- https://github.com/tianocore/edk2
作者
pengdonglin137@163.com
概述
在Ubuntu中安装ubuntu18.04的容器环境,在其中搭建编译和开发环境,支持运行UEFI模拟器。
步骤
安装docker
下载容器镜像
$ sudo docker pull ubuntu:18.04
启动容器
#!/bin/bash sudo docker run -it -d --name edk2 -v $PWD:/root/work/ \ -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY \ -p 9090:22 \ ubuntu:18.04
端口映射是为了后面ssh登录准备,X11以及DISPLAY是为了后面在容器里面启动模拟器准备
登录容器
$ sudo docker exec -it <container id> /bin/bash
配置软件源
# 备份容器中的原始软件源 root@97be60d27afa:/etc/apt# cp sources.list sources.list.bak # 由于容器里没有编辑工具,可以将sources.list拷贝到共享目录下,然后在host上编辑,然后替换 root@97be60d27afa:~/work# cp /etc/apt/sources.list . # 使用阿里云的软件源 deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted deb http://mirrors.aliyun.com/ubuntu/ bionic universe deb http://mirrors.aliyun.com/ubuntu/ bionic-updates universe deb http://mirrors.aliyun.com/ubuntu/ bionic multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse # 替换 root@97be60d27afa:~/work# cp sources.list /etc/apt/ # 更新 root@97be60d27afa:~/work# apt update
安装必备的软件包
下面这些软件包需要安装:
apt install vim git python3 python3-distutils uuid-dev nasm flex bison build-essential openssh-server
其余的后面编译报错时再安装
下载edk2仓库
# mkdir UEFIWorkspace # cd UEFIWorkspace/ # git clone https://github.com/tianocore/edk2.git # git clone https://github.com/tianocore/edk2-libc.git # git clone https://github.com/acpica/acpica.git
此时在UEFIWorkspace下会看到下面几个目录:
UEFIWorkspace$ ls acpica edk2 edk2-libc
- 同步edk2下的submodule
# cd edk2 # git submodule update --init
编译ACPICA
# make -C acpica/
- 编译报错
/bin/sh: 1: m4: not found make[1]: *** [Makefile:325: obj/aslcompiler.y] Error 127
安装软件包: apt install automake
编译BaseTools
# cd edk2 # make -C BaseTools
- 编译报错
/bin/sh: 1: python: not found GNUmakefile:11: recipe for target 'test' failed make[1]: *** [test] Error 127 make[1]: Leaving directory '/root/work/UEFIWorkspace/edk2/BaseTools/Tests' GNUmakefile:19: recipe for target 'Tests' failed make: *** [Tests] Error 2
创建python软连接:
# cd /usr/bin # ln -sf python3 python
创建设置环境变量的脚本
# 在UEFIWorkspace目录下 UEFIWorkspace# ls acpica edk2 edk2-libc myexport.sh root@97be60d27afa:~/work/UEFIWorkspace# cat myexport.sh #!/bin/bash export WORKSPACE=$PWD export PACKAGES_PATH=$PWD/edk2:$PWD/edk2-libc export IASL_PREFIX=$PWD/acpica/generate/unix/bin/ export PYTHON_COMMAND=/usr/bin/python3
设置环境变量,编译64位模拟器
设置环境变量
root@97be60d27afa:~/work/UEFIWorkspace# . ./myexport.sh root@97be60d27afa:~/work/UEFIWorkspace# . ./edk2/edksetup.sh Using EDK2 in-source Basetools WORKSPACE: /root/work/UEFIWorkspace EDK_TOOLS_PATH: /root/work/UEFIWorkspace/edk2/BaseTools CONF_PATH: /root/work/UEFIWorkspace/edk2/Conf Copying $EDK_TOOLS_PATH/Conf/build_rule.template to /root/work/UEFIWorkspace/edk2/Conf/build_rule.txt Copying $EDK_TOOLS_PATH/Conf/tools_def.template to /root/work/UEFIWorkspace/edk2/Conf/tools_def.txt Copying $EDK_TOOLS_PATH/Conf/target.template to /root/work/UEFIWorkspace/edk2/Conf/target.txt
编译模拟器
# build -p edk2/EmulatorPkg/EmulatorPkg.dsc -t GCC5 -a X64
- 编译报错1
/root/work/UEFIWorkspace/Build/EmulatorX64/DEBUG_GCC5/X64/MdePkg/Library/BaseLib/BaseLib/OUTPUT/X64/DisablePaging64.iii:35: error: symbol `InternalX86DisablePaging64.0' undefined
参考:https://blog.csdn.net/weixin_44490152/article/details/129064676
是由于nasm工具版本太老导致,参考上面的博客,编译安装新的nasm即可。
wget http://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.bz2 tar xfj nasm-2.15.05.tar.bz2 cd nasm-2.15.05/ ./autogen.sh ./configure --prefix=/usr/local/ make sudo make install
- 编译报错2
/root/work/UEFIWorkspace/edk2/EmulatorPkg/Unix/Host/X11GraphicsWindow.c:15:10: fatal error: X11/Xlib.h: No such file or directory #include <X11/Xlib.h> ^~~~~~~~~~~~
安装软件包: apt-get install libghc-x11-dev
编译UEFI程序
先按照上一步的操作设置环境变量。
build -p edk2-libc/AppPkg/AppPkg.dsc -t GCC5 -a X64 build -p edk2/MdeModulePkg/MdeModulePkg.dsc -m edk2/MdeModulePkg/Application/HelloWorld/HelloWorld.inf -a X64 -t GCC5
设置sshd
前面已经安装了openssh-server,下面进行一下配置,允许root用户登录和X11 forward。
允许root用户登录
- 编译/etc/ssh/sshd_config
- 编译/etc/hosts.allow,添加下面的内容
- 重启sshd服务
root@97be60d27afa:~/work/UEFIWorkspace# /etc/init.d/ssh restart * Restarting OpenBSD Secure Shell server sshd [ OK ] root@97be60d27afa:~/work/UEFIWorkspace# /etc/init.d/ssh status * sshd is running
- 修改root用户的密码
# passwd root
X11 Forward
编译/etc/ssh/sshd_config
修改完毕,重启sshd服务。
通过SSH登录容器,并且执行UEFI模拟器
$ ssh -X -p 9090 root@localhost root@97be60d27afa:~/work/UEFIWorkspace# . ./myexport.sh root@97be60d27afa:~/work/UEFIWorkspace# . edk2/edksetup.sh Loading previous configuration from /root/work/UEFIWorkspace/edk2/Conf/BuildEnv.sh Using EDK2 in-source Basetools WORKSPACE: /root/work/UEFIWorkspace EDK_TOOLS_PATH: /root/work/UEFIWorkspace/edk2/BaseTools CONF_PATH: /root/work/UEFIWorkspace/edk2/Conf root@97be60d27afa:~/work/UEFIWorkspace# cd Build/EmulatorX64/DEBUG_GCC5/X64/ root@97be60d27afa:~/work/UEFIWorkspace/Build/EmulatorX64/DEBUG_GCC5/X64# root@97be60d27afa:~/work/UEFIWorkspace/Build/EmulatorX64/DEBUG_GCC5/X64# ./Host EDK II UNIX Host Emulation Environment from http://www.tianocore.org/edk2/ BootMode 0x00 OS Emulator passing in 128 KB of temp RAM at 0x40000000 to SEC FD loaded from ../FV/FV_RECOVERY.fd at 0x102000000 contains SEC Core 0x102000400 Loading /root/work/UEFIWorkspace/Build/EmulatorX64/DEBUG_GCC5/X64/EmulatorPkg/Sec/Sec/DEBUG/EmuSec.dll with entry point 0x1020017b3 SEC Has Started 0x102002800 Loading /root/work/UEFIWorkspace/Build/EmulatorX64/DEBUG_GCC5/X64/MdeModulePkg/Core/Pei/PeiMain/DEBUG/PeiCore.dll with e ntry point 0x10200c230 0x102014880 Loading /root/work/UEFIWorkspace/Build/EmulatorX64/DEBUG_GCC5/X64/MdeModulePkg/Universal/ReportStatusCodeRouter/Pei/Repo rtStatusCodeRouterPei/DEBUG/ReportStatusCodeRouterPei.dll with entry point 0x1020157bb ...
此时会弹出模拟器的GUI:
开发环境打包下载
sudo docker save -o edk2_dev.tar.gz edk2_dev:v1.0 BASH 复制 全屏
- edk2_dev.tar.gz
- UEFIWorkspace.tar.gz
链接:https://pan.baidu.com/s/1eKk2AkfeyA8W7ELSwciJnw?pwd=mfy0
提取码:mfy0