目錄

Ovmf source level debug

因為一個客戶的需求,來花點時間研究一下到底原生的 edk2 能不能支援 source level debug.

從原生的 edk2 code 中可以發現,原本就有提供 SourceLevelDebugPkg,感覺是有機會的。

Install edk2 buildtools

1
2
3
4
5
6
7
8
rem Install Chocolatey for easy install required software
@"%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://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
choco install --force -y visualcppbuildtools git tortoisegit Python3 nasm wget 7zip qemu putty
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

Install WinDbg

Download Windows 8.1 SDK from the SDK archive or via here

These instructions will use the default installation location for WinDbg: C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x86

Install Intel UDK Debugger Tool

警告
The installer will likely complain it cannot find a WinDbg installation as it looks in an older location. Just only need to change to the WinDbg’s installation location is fine.
  • Once installation is complete, you can tweak your Intel UDK configuration file further from this location: C:\Program Files (x86)\Intel\Intel(R) UEFI Development Kit Debugger Tool\SoftDebugger.ini

  • Modify the configuration file as below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[Debug Port]
Channel = TCP
Port = 20716
FlowControl = 0
BaudRate = 115200
Server = localhost

[Target System]
; Debugger needs to know the target processor count in the initialization step.
;  Use a value larger enough.
ProcessorCount = 16
FlashRange        = 0xFF000000:0x1000000
;TemporaryRamRange = 0x80000000:0x80000
NoAccessLimit     = 0x80000000

[Debugger]
Executable = C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x86\windbg.exe
Directory = C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x86

[Features]
; Have WinDbg to load the module symbols when the module is loaded in target,
;  which enables to set unresolved breakpoints.
LoadModuleSymbol = 1

TerminalRedirectionPort = 20715

Build Ovmf firmware

1
2
3
4
5
6
7
8
9
set _WORKING_DIR=c:\edk2

git clone https://github.com/tianocore/edk2.git %_WORKING_DIR%
cd /d %_WORKING_DIR%
git submodule init
git submodule update

rem The latest edk2 tag now(03/12/2021)
git checkout tags/edk2-stable202102 -b edk2-stable202102
  • Build edk2 basetools
1
2
3
4
5
6
7
8
set _TOOLCHAIN=VS2015
set _WORKING_DIR=c:\edk2

call %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"" x86
cd /d %_WORKING_DIR%
edksetup.bat %_TOOLCHAIN%
cd /d %_WORKING_DIR%\BaseTools
nmake
  • Build Ovmf IA32
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
set _TARGET=NOOPT
set _ARCH=IA32
set _TOOLCHAIN=VS2015
set _WORKING_DIR=c:\edk2

call %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"" x86_amd64
cd /d %_WORKING_DIR%
set NASM_PREFIX="C:\Program Files\NASM\"
edksetup.bat %_TOOLCHAIN%
rem BUGBUG
set WINSDK81_PREFIX=c:\Program Files (x86)\Windows Kits\8.1\bin\
build -a %_ARCH% -t %_TOOLCHAIN% -p OvmfPkg\OvmfPkg%_ARCH%.dsc -b %_TARGET% -D SOURCE_DEBUG_ENABLE -D DEBUG_ON_SERIAL_PORT

DEBUG IT

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@echo off

set _TARGET=NOOPT
set _ARCH=IA32
set _TOOLCHAIN=VS2015
set _WORKING_DIR=c:\edk2
set _FD_DIR=%_WORKING_DIR%\Build\Ovmf%_ARCH%\%_TARGET%_%_TOOLCHAIN%\FV

set _QEMU_DIR=c:\Program Files\qemu
set _QEMU="%_QEMU_DIR%\qemu-system-x86_64.exe"
set _PUTTY_EXE=C:\ProgramData\chocolatey\bin\PUTTY.EXE
set _Debugger="C:\Program Files (x86)\Intel\Intel(R) UEFI Development Kit Debugger Tool\eXdi.exe"
rem set _MAPPING_DIR=%_WORKING_DIR%\Build\XXXPkg\%_TARGET%_%_TOOLCHAIN%\%_ARCH%

echo %path%|find /i "%_QEMU_DIR:"=%">nul  || set path=%path%;%_QEMU_DIR%

set _QEMU_PARAMETERS=-nodefaults
set _QEMU_PARAMETERS=%_QEMU_PARAMETERS% -L "%_QEMU_DIR%"
set _QEMU_PARAMETERS=%_QEMU_PARAMETERS% -machine q35
set _QEMU_PARAMETERS=%_QEMU_PARAMETERS% -smp 1
set _QEMU_PARAMETERS=%_QEMU_PARAMETERS% -m 256
set _QEMU_PARAMETERS=%_QEMU_PARAMETERS% -vga std
set _QEMU_PARAMETERS=%_QEMU_PARAMETERS% --net none
set _QEMU_PARAMETERS=%_QEMU_PARAMETERS% -drive if=pflash,format=raw,file=%_FD_DIR%\OVMF_CODE.fd,readonly=on
set _QEMU_PARAMETERS=%_QEMU_PARAMETERS% -drive if=pflash,format=raw,file=%_FD_DIR%\OVMF_VARS.fd
set _QEMU_PARAMETERS=%_QEMU_PARAMETERS% -monitor tcp:localhost:20717,server
set _QEMU_PARAMETERS=%_QEMU_PARAMETERS% -serial tcp:localhost:20716,server
rem set _QEMU_PARAMETERS=%_QEMU_PARAMETERS% -hda fat:rw:%_MAPPING_DIR%

echo on

set _currentDIR=%~dp0
@pushd %CD%

start "Monitor" /B "%_PUTTY_EXE%" telnet://localhost:20717
start "Debugger" /B "%_Debugger%" /LaunchWinDbg
start "Console" /B "%_PUTTY_EXE%" telnet://localhost:20715

rem copy /y %_currentDIR%\startup.nsh %_MAPPING_DIR%
%_QEMU% %_QEMU_PARAMETERS%
@popd

Reference