%global formats rom
%global qemuroms 10222000 10ec8029 8086100e 10ec8139 1af41000 80861209 808610d3 15ad07b0
%global buildarches %{ix86} x86_64
%global debug_package %{nil}
%global hash 36a4c85f

Name:                ipxe
Version:             20190125
Release:             1
Summary:             A network boot loader

License:             GPLv2 with additional permissions and BSD
URL:                 http://ipxe.org/
Source0:             %{name}-%{version}-git%{hash}.tar.xz
Patch0001:           0001-build-customize-configuration.patch
Patch0002:           0002-Use-spec-compliant-timeouts.patch

%ifarch %{buildarches}
BuildRequires:       perl-interpreter perl-Getopt-Long syslinux mtools genisoimage edk2-tools
BuildRequires:       xz-devel gcc binutils-devel binutils-x86_64-linux-gnu gcc-x86_64-linux-gnu
Obsoletes:           gpxe-bootimgs gpxe-roms gpxe-roms-qem
Provides:            %{name}-bootimgs %{name}-roms %{name}-roms-qemu
Obsoletes:           %{name}-bootimgs %{name}-roms %{name}-roms-qemu
%endif
Obsoletes: gpxe <= 1.0.1

%description
iPXE is an open source network bootloader. It provides a direct
replacement for proprietary PXE ROMs, with many extra features such as
DNS, HTTP, iSCSI, etc.

%prep
%setup -q -n %{name}-%{version}-git%{hash}
%autopatch -p1


%build
%ifarch %{buildarches}
cd src

rm -rf drivers/net/ath/ath9k

make_ipxe() {
    make %{?_smp_mflags} \
        NO_WERROR=1 V=1 \
        GITVERSION=%{hash} \
        CROSS_COMPILE=x86_64-linux-gnu- \
        "$@"
}

make_ipxe bin-i386-efi/ipxe.efi bin-x86_64-efi/ipxe.efi

make_ipxe ISOLINUX_BIN=/usr/share/syslinux/isolinux.bin \
    bin/undionly.kpxe bin/ipxe.{dsk,iso,usb,lkrn} \
    allroms

mkdir bin-combined
for rom in %{qemuroms}; do
  make_ipxe CONFIG=qemu bin/${rom}.rom
  make_ipxe CONFIG=qemu bin-i386-efi/${rom}.efidrv
  make_ipxe CONFIG=qemu bin-x86_64-efi/${rom}.efidrv
  vid="0x${rom%%????}"
  did="0x${rom#????}"
  EfiRom -f "$vid" -i "$did" --pci23 \
         -b  bin/${rom}.rom \
         -ec bin-i386-efi/${rom}.efidrv \
         -ec bin-x86_64-efi/${rom}.efidrv \
         -o  bin-combined/${rom}.rom
  EfiRom -d  bin-combined/${rom}.rom
  truncate -s \>256K bin-combined/${rom}.rom
  test $(stat -c '%s' bin-combined/${rom}.rom) -le $((256 * 1024))
done

%endif

%install
%ifarch %{buildarches}
mkdir -p %{buildroot}/%{_datadir}/%{name}/
mkdir -p %{buildroot}/%{_datadir}/%{name}.efi/
pushd src/bin/

cp -a undionly.kpxe ipxe.{iso,usb,dsk,lkrn} %{buildroot}/%{_datadir}/%{name}/

for fmt in %{formats};do
 for img in *.${fmt};do
      if [ -e $img ]; then
   cp -a $img %{buildroot}/%{_datadir}/%{name}/
   echo %{_datadir}/%{name}/$img >> ../../${fmt}.list
  fi
 done
done
popd

cp -a src/bin-i386-efi/ipxe.efi %{buildroot}/%{_datadir}/%{name}/ipxe-i386.efi
cp -a src/bin-x86_64-efi/ipxe.efi %{buildroot}/%{_datadir}/%{name}/ipxe-x86_64.efi

for fmt in rom ;do
 for rom in %{qemuroms} ; do
  sed -i -e "/\/${rom}.${fmt}/d" ${fmt}.list
  echo %{_datadir}/%{name}/${rom}.${fmt} >> qemu.${fmt}.list
 done
done
for rom in %{qemuroms}; do
  cp src/bin-combined/${rom}.rom %{buildroot}/%{_datadir}/%{name}.efi/
done
%endif

%ifarch %{buildarches} 
%files -f rom.list
%defattr(-,root,root)
%doc COPYING COPYING.GPLv2 COPYING.UBDL
%dir %{_datadir}/%{name}
%dir %{_datadir}/%{name}.efi
%{_datadir}/%{name}/ipxe.iso
%{_datadir}/%{name}/ipxe.usb
%{_datadir}/%{name}/ipxe.dsk
%{_datadir}/%{name}/ipxe.lkrn
%{_datadir}/%{name}/ipxe-i386.efi
%{_datadir}/%{name}/ipxe-x86_64.efi
%{_datadir}/%{name}/undionly.kpxe
%{_datadir}/%{name}.efi/*.rom
%endif

%changelog
* Thu Sep 19 2019 openEuler Buildteam <buildteam@openeuler.org> - 20190125-1
- Package init

