%global debug_package %{nil}
%global __python %{__python}
%define GITDATE 201905
%define OPENSSLVER 1_1_1b

Name:           ovmf
Version:        %{GITDATE}
Release:        1%{?dist}
Summary:        UEFI firmware for 64-bit virtual machines
Group:          Applications/Emulators
License:        BSD and OpenSSL and MIT
URL:            http://www.tianocore.org
Source0:        https://github.com/tianocore/edk2/archive/edk2-stable%{GITDATE}.tar.gz
Source1:        https://github.com/openssl/openssl/archive/OpenSSL_%{OPENSSLVER}.tar.gz
Source2:        ovmf-vars-generator
Source3:        edk2-ovmf-logo.bmp
ExclusiveArch:  x86_64
BuildRequires:  python2-devel
BuildRequires:  libuuid-devel
BuildRequires:  /usr/bin/iasl
BuildRequires:  binutils gcc git
BuildRequires:  nasm
BuildRequires:  dosfstools
BuildRequires:  mtools
BuildRequires:  genisoimage
BuildRequires:  qemu-kvm >= 1.5.3-44
BuildRequires:  kernel >= 3.10.0-52
BuildRequires:  rpmdevtools


%description
EDK II is a modern, feature-rich, cross-platform firmware development
environment for the UEFI and PI specifications. This package contains sample
64-bit UEFI firmware builds for QEMU and KVM.


%prep
%setup -q -n edk2-edk2-stable%{GITDATE}
tar xvf %{SOURCE1} -C CryptoPkg/Library/OpensslLib/
mv CryptoPkg/Library/OpensslLib/openssl-OpenSSL_%{OPENSSLVER}/* CryptoPkg/Library/OpensslLib/openssl/
cp %{SOURCE2} .
cp %{SOURCE3} MdeModulePkg/Logo/Logo.bmp


%build
make -j32 -C BaseTools/
source ./edksetup.sh
make -C "$EDK_TOOLS_PATH"

SMP_MFLAGS="%{?_smp_mflags}"
if [[ x"$SMP_MFLAGS" = x-j* ]]; then
        CC_FLAGS="$CC_FLAGS -n ${SMP_MFLAGS#-j}"
elif [ -n "%{?jobs}" ]; then
        CC_FLAGS="$CC_FLAGS -n %{?jobs}"
fi

CC_FLAGS="$CC_FLAGS --cmd-len=65536 -t GCC48 -b DEBUG --hash"

# Build with SB but without SMM; include UEFI shell.
build ${CC_FLAGS} -D FD_SIZE_4MB -a X64 -p OvmfPkg/OvmfPkgX64.dsc -D SECURE_BOOT_ENABLE
# Build with SB and SMM; exclude UEFI shell.
build -D SECURE_BOOT_ENABLE -D EXCLUDE_SHELL_FROM_FD ${CC_FLAGS} -a IA32 -a X64 -p OvmfPkg/OvmfPkgIa32X64.dsc -D SMM_REQUIRE -D FD_SIZE_4MB
# Sanity check: the varstore templates must be identical.
cmp Build/OvmfX64/DEBUG_GCC4?/FV/OVMF_VARS.fd Build/Ovmf3264/DEBUG_GCC4?/FV/OVMF_VARS.fd

# Prepare an ISO image that boots the UEFI shell.
(
  UEFI_SHELL_BINARY=Build/Ovmf3264/DEBUG_GCC48/X64/Shell.efi
  ENROLLER_BINARY=Build/Ovmf3264/DEBUG_GCC48/X64/EnrollDefaultKeys.efi
  UEFI_SHELL_IMAGE=uefi_shell.img
  ISO_IMAGE=UefiShell.iso

  UEFI_SHELL_BINARY_BNAME=$(basename -- "$UEFI_SHELL_BINARY")
  UEFI_SHELL_SIZE=$(stat --format=%s -- "$UEFI_SHELL_BINARY")
  ENROLLER_SIZE=$(stat --format=%s -- "$ENROLLER_BINARY")

  # add 1MB then 10% for metadata
  UEFI_SHELL_IMAGE_KB=$((
    (UEFI_SHELL_SIZE + ENROLLER_SIZE + 1 * 1024 * 1024) * 11 / 10 / 1024
  ))

  # create non-partitioned FAT image
  rm -f -- "$UEFI_SHELL_IMAGE"
  mkdosfs -C "$UEFI_SHELL_IMAGE" -n UEFI_SHELL -- "$UEFI_SHELL_IMAGE_KB"

  # copy the shell binary into the FAT image
  export MTOOLS_SKIP_CHECK=1
  mmd   -i "$UEFI_SHELL_IMAGE"                       ::efi
  mmd   -i "$UEFI_SHELL_IMAGE"                       ::efi/boot
  mcopy -i "$UEFI_SHELL_IMAGE"  "$UEFI_SHELL_BINARY" ::efi/boot/bootx64.efi
  mcopy -i "$UEFI_SHELL_IMAGE"  "$ENROLLER_BINARY"   ::
  mdir  -i "$UEFI_SHELL_IMAGE"  -/                   ::

  # build ISO with FAT image file as El Torito EFI boot image
  genisoimage -input-charset ASCII -J -rational-rock \
    -efi-boot "$UEFI_SHELL_IMAGE" -no-emul-boot \
    -o "$ISO_IMAGE" -- "$UEFI_SHELL_IMAGE"
)

# Enroll the default certificates in a separate variable store template. Base
# RHEL7 qemu-kvm does not emulate SMM, but we don't need SMM for the enrollment
# here.
chmod +x ./ovmf-vars-generator
./ovmf-vars-generator --verbose --verbose \
  --qemu-binary        /usr/bin/qemu-system-x86_64 \
  --ovmf-binary        Build/OvmfX64/DEBUG_GCC4?/FV/OVMF_CODE.fd \
  --ovmf-template-vars Build/OvmfX64/DEBUG_GCC4?/FV/OVMF_VARS.fd \
  --uefi-shell-iso     UefiShell.iso \
  --skip-testing \
  --disable-smm \
  OVMF_VARS.secboot.fd


%install

copy_license() {
    install -m 0644 $1 $RPM_BUILD_ROOT%{_docdir}/%{name}/Licenses/$2-License.txt
}

mkdir -p $RPM_BUILD_ROOT%{_docdir}/%{name}/Licenses
copy_license License.txt edk2
copy_license OvmfPkg/License.txt OvmfPkg

mkdir -p $RPM_BUILD_ROOT%{_datadir}/OVMF

install -m 0644 Build/OvmfX64/DEBUG_GCC4?/FV/OVMF_CODE.fd  $RPM_BUILD_ROOT%{_datadir}/OVMF/OVMF_CODE.fd
install -m 0644 Build/Ovmf3264/DEBUG_GCC4?/FV/OVMF_CODE.fd $RPM_BUILD_ROOT%{_datadir}/OVMF/OVMF_CODE.secboot.fd
install -m 0644 Build/OvmfX64/DEBUG_GCC4?/FV/OVMF_VARS.fd $RPM_BUILD_ROOT%{_datadir}/OVMF/OVMF_VARS.fd
install -m 0644 OVMF_VARS.secboot.fd                      $RPM_BUILD_ROOT%{_datadir}/OVMF/OVMF_VARS.secboot.fd
install -m 0644 UefiShell.iso                             $RPM_BUILD_ROOT%{_datadir}/OVMF/UefiShell.iso
install -m 0644 OvmfPkg/README                            $RPM_BUILD_ROOT%{_docdir}/%{name}/README

copy_license CryptoPkg/Library/OpensslLib/openssl/LICENSE OpensslLib


%files
%defattr(-,root,root,-)
%dir %{_docdir}/%{name}/Licenses
%doc %{_docdir}/%{name}/Licenses/edk2-License.txt
%doc %{_docdir}/%{name}/Licenses/OvmfPkg-License.txt
%doc %{_docdir}/%{name}/Licenses/OpensslLib-License.txt
%doc %{_docdir}/%{name}/README
%dir %{_datadir}/OVMF/
%{_datadir}/OVMF/OVMF_CODE.fd
%{_datadir}/OVMF/OVMF_CODE.secboot.fd
%{_datadir}/OVMF/OVMF_VARS.fd
%{_datadir}/OVMF/OVMF_VARS.secboot.fd
%{_datadir}/OVMF/UefiShell.iso


%changelog
