2022-11-21 17:40:18 +00:00
|
|
|
{ stdenv, nixosTests, lib, edk2, util-linux, nasm, acpica-tools, llvmPackages
|
2024-09-19 14:19:46 +00:00
|
|
|
, fetchFromGitLab, python3, pexpect, xorriso, qemu, dosfstools, mtools
|
2024-02-29 20:09:43 +00:00
|
|
|
, fdSize2MB ? false
|
|
|
|
, fdSize4MB ? secureBoot
|
2020-04-24 23:36:52 +00:00
|
|
|
, secureBoot ? false
|
2024-02-29 20:09:43 +00:00
|
|
|
, systemManagementModeRequired ? secureBoot && stdenv.hostPlatform.isx86
|
|
|
|
# Whether to create an nvram variables template
|
|
|
|
# which includes the MSFT secure boot keys
|
|
|
|
, msVarsTemplate ? false
|
|
|
|
# When creating the nvram variables template with
|
|
|
|
# the MSFT keys, we also must provide a certificate
|
|
|
|
# to use as the PK and first KEK for the keystore.
|
|
|
|
#
|
|
|
|
# By default, we use Debian's cert. This default
|
2024-09-19 14:19:46 +00:00
|
|
|
# should change to a NixOS cert once we have our
|
2024-02-29 20:09:43 +00:00
|
|
|
# own secure boot signing infrastructure.
|
|
|
|
#
|
|
|
|
# Ignored if msVarsTemplate is false.
|
|
|
|
, vendorPkKek ? "$NIX_BUILD_TOP/debian/PkKek-1-Debian.pem"
|
2021-02-17 17:02:09 +00:00
|
|
|
, httpSupport ? false
|
2021-10-17 02:12:59 +00:00
|
|
|
, tpmSupport ? false
|
2023-05-24 13:37:59 +00:00
|
|
|
, tlsSupport ? false
|
|
|
|
, debug ? false
|
2024-01-02 11:29:13 +00:00
|
|
|
# Usually, this option is broken, do not use it except if you know what you are
|
|
|
|
# doing.
|
|
|
|
, sourceDebug ? false
|
2024-04-21 15:54:59 +00:00
|
|
|
, projectDscPath ? {
|
|
|
|
i686 = "OvmfPkg/OvmfPkgIa32.dsc";
|
|
|
|
x86_64 = "OvmfPkg/OvmfPkgX64.dsc";
|
|
|
|
aarch64 = "ArmVirtPkg/ArmVirtQemu.dsc";
|
|
|
|
riscv64 = "OvmfPkg/RiscVVirt/RiscVVirtQemu.dsc";
|
2024-09-19 14:19:46 +00:00
|
|
|
loongarch64 = "OvmfPkg/LoongArchVirt/LoongArchVirtQemu.dsc";
|
2024-04-21 15:54:59 +00:00
|
|
|
}.${stdenv.hostPlatform.parsed.cpu.name}
|
|
|
|
or (throw "Unsupported OVMF `projectDscPath` on ${stdenv.hostPlatform.parsed.cpu.name}")
|
|
|
|
, fwPrefix ? {
|
|
|
|
i686 = "OVMF";
|
|
|
|
x86_64 = "OVMF";
|
|
|
|
aarch64 = "AAVMF";
|
|
|
|
riscv64 = "RISCV_VIRT";
|
2024-09-19 14:19:46 +00:00
|
|
|
loongarch64 = "LOONGARCH_VIRT";
|
2024-04-21 15:54:59 +00:00
|
|
|
}.${stdenv.hostPlatform.parsed.cpu.name}
|
|
|
|
or (throw "Unsupported OVMF `fwPrefix` on ${stdenv.hostPlatform.parsed.cpu.name}")
|
|
|
|
, metaPlatforms ? edk2.meta.platforms
|
2020-04-24 23:36:52 +00:00
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
platformSpecific = {
|
2024-06-05 15:53:02 +00:00
|
|
|
i686.msVarsArgs = {
|
|
|
|
flavor = "OVMF";
|
|
|
|
archDir = "Ia32";
|
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
x86_64.msVarsArgs = {
|
|
|
|
flavor = "OVMF_4M";
|
|
|
|
archDir = "X64";
|
2024-02-29 20:09:43 +00:00
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
aarch64.msVarsArgs = {
|
|
|
|
flavor = "AAVMF";
|
|
|
|
archDir = "AARCH64";
|
2024-02-29 20:09:43 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
cpuName = stdenv.hostPlatform.parsed.cpu.name;
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
inherit (platformSpecific.${cpuName}) msVarsArgs;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
version = lib.getVersion edk2;
|
2022-09-09 14:08:57 +00:00
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
OvmfPkKek1AppPrefix = "4e32566d-8e9e-4f52-81d3-5bb9715f9727";
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
debian-edk-src = fetchFromGitLab {
|
|
|
|
domain = "salsa.debian.org";
|
|
|
|
owner = "qemu-team";
|
|
|
|
repo = "edk2";
|
|
|
|
nonConeMode = true;
|
|
|
|
sparseCheckout = [
|
|
|
|
"debian/edk2-vars-generator.py"
|
|
|
|
"debian/python"
|
|
|
|
"debian/PkKek-1-*.pem"
|
|
|
|
];
|
|
|
|
rev = "refs/tags/debian/2024.05-1";
|
|
|
|
hash = "sha256-uAjXJaHOVh944ZxcA2IgCsrsncxuhc0JKlsXs0E03s0=";
|
2022-09-09 14:08:57 +00:00
|
|
|
};
|
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
buildPrefix = "Build/*/*";
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
in
|
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
assert msVarsTemplate -> fdSize4MB;
|
2024-09-19 14:19:46 +00:00
|
|
|
assert msVarsTemplate -> platformSpecific ? ${cpuName};
|
2024-02-29 20:09:43 +00:00
|
|
|
assert msVarsTemplate -> platformSpecific.${cpuName} ? msVarsArgs;
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
edk2.mkDerivation projectDscPath (finalAttrs: {
|
2022-03-30 09:31:56 +00:00
|
|
|
pname = "OVMF";
|
|
|
|
inherit version;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
outputs = [ "out" "fd" ];
|
|
|
|
|
2022-11-21 17:40:18 +00:00
|
|
|
nativeBuildInputs = [ util-linux nasm acpica-tools ]
|
2024-02-29 20:09:43 +00:00
|
|
|
++ lib.optionals stdenv.cc.isClang [ llvmPackages.bintools llvmPackages.llvm ]
|
|
|
|
++ lib.optionals msVarsTemplate [ python3 pexpect xorriso qemu dosfstools mtools ];
|
2022-06-16 17:23:12 +00:00
|
|
|
strictDeps = true;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
hardeningDisable = [ "format" "stackprotector" "pic" "fortify" ];
|
|
|
|
|
|
|
|
buildFlags =
|
2023-05-24 13:37:59 +00:00
|
|
|
# IPv6 has no reason to be disabled.
|
|
|
|
[ "-D NETWORK_IP6_ENABLE=TRUE" ]
|
|
|
|
++ lib.optionals debug [ "-D DEBUG_ON_SERIAL_PORT=TRUE" ]
|
|
|
|
++ lib.optionals sourceDebug [ "-D SOURCE_DEBUG_ENABLE=TRUE" ]
|
|
|
|
++ lib.optionals secureBoot [ "-D SECURE_BOOT_ENABLE=TRUE" ]
|
2024-02-29 20:09:43 +00:00
|
|
|
++ lib.optionals systemManagementModeRequired [ "-D SMM_REQUIRE=TRUE" ]
|
2023-08-04 22:07:22 +00:00
|
|
|
++ lib.optionals fdSize2MB ["-D FD_SIZE_2MB"]
|
|
|
|
++ lib.optionals fdSize4MB ["-D FD_SIZE_4MB"]
|
2021-10-17 02:12:59 +00:00
|
|
|
++ lib.optionals httpSupport [ "-D NETWORK_HTTP_ENABLE=TRUE" "-D NETWORK_HTTP_BOOT_ENABLE=TRUE" ]
|
2023-05-24 13:37:59 +00:00
|
|
|
++ lib.optionals tlsSupport [ "-D NETWORK_TLS_ENABLE=TRUE" ]
|
2021-10-17 02:12:59 +00:00
|
|
|
++ lib.optionals tpmSupport [ "-D TPM_ENABLE" "-D TPM2_ENABLE" "-D TPM2_CONFIG_ENABLE"];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
buildConfig = if debug then "DEBUG" else "RELEASE";
|
2023-03-04 12:14:45 +00:00
|
|
|
env.NIX_CFLAGS_COMPILE = lib.optionalString stdenv.cc.isClang "-Qunused-arguments";
|
2022-11-21 17:40:18 +00:00
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
env.PYTHON_COMMAND = "python3";
|
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
postUnpack = lib.optionalDrvAttr msVarsTemplate ''
|
2024-09-19 14:19:46 +00:00
|
|
|
ln -s ${debian-edk-src}/debian
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
postConfigure = lib.optionalDrvAttr msVarsTemplate ''
|
|
|
|
tr -d '\n' < ${vendorPkKek} | sed \
|
|
|
|
-e 's/.*-----BEGIN CERTIFICATE-----/${OvmfPkKek1AppPrefix}:/' \
|
|
|
|
-e 's/-----END CERTIFICATE-----//' > vendor-cert-string
|
|
|
|
export PYTHONPATH=$NIX_BUILD_TOP/debian/python:$PYTHONPATH
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
postBuild = lib.optionalString (stdenv.hostPlatform.isAarch || stdenv.hostPlatform.isLoongArch64) ''
|
2024-02-29 20:09:43 +00:00
|
|
|
(
|
|
|
|
cd ${buildPrefix}/FV
|
|
|
|
cp QEMU_EFI.fd ${fwPrefix}_CODE.fd
|
|
|
|
cp QEMU_VARS.fd ${fwPrefix}_VARS.fd
|
2024-09-19 14:19:46 +00:00
|
|
|
)
|
|
|
|
'' + lib.optionalString stdenv.hostPlatform.isAarch ''
|
2024-02-29 20:09:43 +00:00
|
|
|
# QEMU expects 64MiB CODE and VARS files on ARM/AARCH64 architectures
|
|
|
|
# Truncate the firmware files to the expected size
|
2024-09-19 14:19:46 +00:00
|
|
|
truncate -s 64M ${buildPrefix}/FV/${fwPrefix}_CODE.fd
|
|
|
|
truncate -s 64M ${buildPrefix}/FV/${fwPrefix}_VARS.fd
|
2024-02-29 20:09:43 +00:00
|
|
|
'' + lib.optionalString stdenv.hostPlatform.isRiscV ''
|
|
|
|
truncate -s 32M ${buildPrefix}/FV/${fwPrefix}_CODE.fd
|
|
|
|
truncate -s 32M ${buildPrefix}/FV/${fwPrefix}_VARS.fd
|
|
|
|
'' + lib.optionalString msVarsTemplate ''
|
|
|
|
(
|
|
|
|
cd ${buildPrefix}
|
2024-09-19 14:19:46 +00:00
|
|
|
# locale must be set on Darwin for invocations of mtools to work correctly
|
|
|
|
LC_ALL=C python3 $NIX_BUILD_TOP/debian/edk2-vars-generator.py \
|
2024-02-29 20:09:43 +00:00
|
|
|
--flavor ${msVarsArgs.flavor} \
|
|
|
|
--enrolldefaultkeys ${msVarsArgs.archDir}/EnrollDefaultKeys.efi \
|
|
|
|
--shell ${msVarsArgs.archDir}/Shell.efi \
|
|
|
|
--code FV/${fwPrefix}_CODE.fd \
|
|
|
|
--vars-template FV/${fwPrefix}_VARS.fd \
|
|
|
|
--certificate `< $NIX_BUILD_TOP/$sourceRoot/vendor-cert-string` \
|
|
|
|
--out-file FV/${fwPrefix}_VARS.ms.fd
|
|
|
|
)
|
|
|
|
'';
|
2020-07-18 16:06:22 +00:00
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
# TODO: Usage of -bios OVMF.fd is discouraged: https://lists.katacontainers.io/pipermail/kata-dev/2021-January/001650.html
|
|
|
|
# We should remove the isx86-specifc block here once we're ready to update nixpkgs to stop using that and update the
|
|
|
|
# release notes accordingly.
|
|
|
|
postInstall = ''
|
|
|
|
mkdir -vp $fd/FV
|
2024-04-21 15:54:59 +00:00
|
|
|
'' + lib.optionalString (builtins.elem fwPrefix [
|
2024-09-19 14:19:46 +00:00
|
|
|
"OVMF" "AAVMF" "RISCV_VIRT" "LOONGARCH_VIRT"
|
2024-04-21 15:54:59 +00:00
|
|
|
]) ''
|
2024-02-29 20:09:43 +00:00
|
|
|
mv -v $out/FV/${fwPrefix}_{CODE,VARS}.fd $fd/FV
|
|
|
|
'' + lib.optionalString stdenv.hostPlatform.isx86 ''
|
|
|
|
mv -v $out/FV/${fwPrefix}.fd $fd/FV
|
|
|
|
'' + lib.optionalString msVarsTemplate ''
|
|
|
|
mv -v $out/FV/${fwPrefix}_VARS.ms.fd $fd/FV
|
|
|
|
ln -sv $fd/FV/${fwPrefix}_CODE{,.ms}.fd
|
|
|
|
'' + lib.optionalString stdenv.hostPlatform.isAarch ''
|
|
|
|
mv -v $out/FV/QEMU_{EFI,VARS}.fd $fd/FV
|
2024-09-19 14:19:46 +00:00
|
|
|
# Add symlinks for Fedora dir layout: https://src.fedoraproject.org/rpms/edk2/blob/main/f/edk2.spec
|
2024-02-29 20:09:43 +00:00
|
|
|
mkdir -vp $fd/AAVMF
|
2020-07-18 16:06:22 +00:00
|
|
|
ln -s $fd/FV/AAVMF_CODE.fd $fd/AAVMF/QEMU_EFI-pflash.raw
|
|
|
|
ln -s $fd/FV/AAVMF_VARS.fd $fd/AAVMF/vars-template-pflash.raw
|
2024-02-29 20:09:43 +00:00
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
dontPatchELF = true;
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
passthru =
|
|
|
|
let
|
2024-02-29 20:09:43 +00:00
|
|
|
prefix = "${finalAttrs.finalPackage.fd}/FV/${fwPrefix}";
|
2022-09-09 14:08:57 +00:00
|
|
|
in {
|
|
|
|
firmware = "${prefix}_CODE.fd";
|
|
|
|
variables = "${prefix}_VARS.fd";
|
2024-09-19 14:19:46 +00:00
|
|
|
variablesMs =
|
|
|
|
assert msVarsTemplate;
|
|
|
|
"${prefix}_VARS.ms.fd";
|
2022-09-09 14:08:57 +00:00
|
|
|
# This will test the EFI firmware for the host platform as part of the NixOS Tests setup.
|
|
|
|
tests.basic-systemd-boot = nixosTests.systemd-boot.basic;
|
2024-02-29 20:09:43 +00:00
|
|
|
tests.secureBoot-systemd-boot = nixosTests.systemd-boot.secureBoot;
|
|
|
|
inherit secureBoot systemManagementModeRequired;
|
2022-09-09 14:08:57 +00:00
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
meta = {
|
|
|
|
description = "Sample UEFI firmware for QEMU and KVM";
|
|
|
|
homepage = "https://github.com/tianocore/tianocore.github.io/wiki/OVMF";
|
2021-01-15 22:18:51 +00:00
|
|
|
license = lib.licenses.bsd2;
|
2024-04-21 15:54:59 +00:00
|
|
|
platforms = metaPlatforms;
|
2024-09-19 14:19:46 +00:00
|
|
|
maintainers = with lib.maintainers; [ adamcstephens raitobezarius mjoerg ];
|
|
|
|
broken = stdenv.isDarwin && stdenv.isAarch64;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
2022-09-09 14:08:57 +00:00
|
|
|
})
|