To easily test using chipsec in a UEFI environment, it is useful to be
able to run it in a virtual machine. On most Linux distributions, QEMU
can be used to start a UEFI shell in a virtual machine. By providing a
`startup.nsh` file, some commands can even be automatically entered!
Moreover:
- OVMF embeds the UEFI shell. There is no need to download a `Shell.efi`
file. But by default QEMU tries to boot with PXE (network boot). I
have not found a nice way to prevent this, so instead I removed the
NIC of the machine with `-nic none` option.
- `qemu-img convert` can create a FAT partition from a directory, thanks
to QEMU's Virtual VFAT storage engine. This enables creating an ESP
(EFI System Partition) without needing to mount a temporary filesystem
and without using GNU mtools commands (`mmd` and `mcopy`).
Provide a shell script which prepares an ESP with chipsec and the Python
module, and launches a QEMU virtual machine from it. It works on Ubuntu
20.04 and 22.04:
sudo apt install ovmf qemu-system-x86 qemu-utils
./scripts/qemu_efi.py
Then in the UEFI shell:
Shell> fs0:
FS0:\> cd chipsec
FS0:\chipsec\> python368.efi chipsec_util.py -i cpu cpuid 0
################################################################
## ##
## CHIPSEC: Platform Hardware Security Assessment Framework ##
## ##
################################################################
[CHIPSEC] Version :
[CHIPSEC] OS : uefi
[CHIPSEC] Python : 3.6.8 (64-bit)
[CHIPSEC] API mode: using CHIPSEC kernel module API
[-] ERROR: Unknown Platform: VID = 0x8086, DID = 0x1237, RID = 0x02
[!] WARNING: *******************************************************************
[!] WARNING: * Unknown platform!
[!] WARNING: * Platform dependent functionality will likely be incorrect
[!] WARNING: * Error Message: "Unknown Platform: VID = 0x8086, DID = 0x1237, RID = 0x02"
[!] WARNING: *******************************************************************
[CHIPSEC] Helper : EfiHelper (None)
[CHIPSEC] Platform: UnknownPlatform
[CHIPSEC] VID: FFFF
[CHIPSEC] DID: FFFF
[CHIPSEC] RID: FF
[CHIPSEC] PCH : Default PCH
[CHIPSEC] VID: FFFF
[CHIPSEC] DID: FFFF
[CHIPSEC] RID: FF
[CHIPSEC] Executing command 'cpu' with args ['cpuid', '0']
[CHIPSEC] CPUID < EAX: 0x00000000
[CHIPSEC] ECX: 0x00000000
[CHIPSEC] CPUID > EAX: 0x0000000D
[CHIPSEC] EBX: 0x68747541
[CHIPSEC] ECX: 0x444D4163
[CHIPSEC] EDX: 0x69746E65
[CHIPSEC] (cpu) time elapsed 0.000
As typing a command in the virtual UEFI Shell can be quite cumbersome,
it is possible to directly provide it to the script. For example:
$ ./scripts/qemu_efi.py -u ' -i' cpu cpuid 0
N.B. the options of chipsec commands are escaped with a space (`' -i'`)
to work around limitations of Python's `argparse` module. In this
example, `-i` is required as chipsec does not support the default
platform emulated by QEMU yet.
Signed-off-by: Nicolas Iooss <nicolas.iooss_git@polytechnique.org>