%global with_armv8 1

Name:           uboot-tools
Version:        2025.01
Release:        7
Summary:        tools for U-Boot
License:        GPL-2.0-or-later and Public Domain and GPL-2.0-only
URL:            http://www.denx.de/wiki/U-Boot
Source0:        https://ftp.denx.de/pub/u-boot/u-boot-%{version}.tar.bz2
Source1:        aarch64-boards
Source2:        10-devicetree.install

Patch1:         uefi-distro-load-FDT-from-any-partition-on-boot-device.patch
Patch2:         uefi-Add-all-options-for-EFI-System-Partitions.patch
Patch3:         uefi-initial-find_fdt_location-for-finding-the-DT-on-disk.patch
Patch4:         disable-VBE-by-default.patch
Patch5:         enable-bootmenu-by-default.patch
Patch6:         use-system-dtc.patch

Patch10:        rpi-Add-identifiers-for-the-new-RPi-5-series.patch
Patch11:        rockchip-Enable-preboot-start-for-pci-usb.patch

Patch6000:      backport-0001-CVE-2026-33243.patch
Patch6001:      backport-0002-CVE-2026-33243.patch
Patch6002:      backport-CVE-2024-42040.patch
Patch6003:      backport-CVE-2026-29009.patch
Patch6004:      backport-527115ef-pylibfdt-swig45.patch
Patch6005:      backport-CVE-2026-74220.patch
Patch6006:      backport-0001-CVE-2026-74221.patch
Patch6007:      backport-0002-CVE-2026-74221.patch
Patch6008:      backport-CVE-2026-71971.patch
Patch6009:      backport-CVE-2026-71972.patch
Patch6010:      backport-0001-CVE-2026-71973.patch
Patch6011:      backport-0002-CVE-2026-71973.patch

BuildRequires:  bison
BuildRequires:  dtc
BuildRequires:  flex
BuildRequires:  gcc
BuildRequires:  gnutls-devel
BuildRequires:  libuuid-devel
BuildRequires:  make
BuildRequires:  ncurses-devel
BuildRequires:  openssl-devel
BuildRequires:  python3-devel
BuildRequires:  python3-setuptools
BuildRequires:  python3-libfdt
%if %{with_armv8}
%ifarch aarch64
BuildRequires:  arm-trusted-firmware-armv8
BuildRequires:  python3-pyelftools
%endif
%endif

Requires:       dtc
Requires:       systemd

%ifarch aarch64
Obsoletes:      uboot-images-elf < 2025.01
Provides:       uboot-images-elf < 2025.01
%endif

%description
This package includes the mkimage program, which allows generation of U-Boot
images in various formats, and the fw_printenv and fw_setenv programs to read
and modify U-Boot's environment.

%if %{with_armv8}
%ifarch aarch64
%package     	-n uboot-images-armv8
Summary:     	u-boot bootloader images for aarch64 boards
Requires:    	uboot-tools
BuildArch:   	noarch

%description 	-n uboot-images-armv8
u-boot bootloader images for aarch64 boards
%endif
%endif

%package_help

%prep
%autosetup -p1 -n u-boot-%{version}

cp %SOURCE1 .

%build
mkdir builds

%make_build HOSTCC="gcc $RPM_OPT_FLAGS" CROSS_COMPILE="" DTC=/usr/bin/dtc tools-only_defconfig O=builds/
%make_build HOSTCC="gcc $RPM_OPT_FLAGS" CROSS_COMPILE="" DTC=/usr/bin/dtc tools-all O=builds/

%if %{with_armv8}
%ifarch aarch64
for board in $(cat %{_arch}-boards)
do
  echo "Building board: $board"
  if [[ " ${rk3399[*]} " == *" $board "* ]]; then
    echo "Board: $board  skipping"
    continue
  fi

  mkdir builds/$(echo $board)/
  sun50i=(a64-olinuxino amarula_a64_relic bananapi_m2_plus_h5  bananapi_m64 libretech_all_h3_cc_h5 nanopi_neo2 nanopi_neo_plus2 orangepi_pc2 orangepi_prime orangepi_win orangepi_zero_plus orangepi_zero_plus2 pine64-lts pine64_plus pinebook pinephone pinetab sopine_baseboard teres_i)
  if [[ " ${sun50i[*]} " == *" $board "* ]]; then
    echo "Board: $board using sun50i_a64"
    cp /usr/share/arm-trusted-firmware/sun50i_a64/* builds/$(echo $board)/
  fi
  sun50h6=(orangepi_lite2 orangepi_one_plus pine_h64)
  if [[ " ${sun50h6[*]} " == *" $board "* ]]; then
    echo "Board: $board using sun50i_h6"
    cp /usr/share/arm-trusted-firmware/sun50i_h6/* builds/$(echo $board)/
  fi
    rk3328=(evb-rk3328 rock64-rk3328)
  if [[ " ${rk3328[*]} " == *" $board "* ]]; then
    echo "Board: $board using rk3328"
    cp /usr/share/arm-trusted-firmware/rk3328/* builds/$(echo $board)/
  fi
  rk3399=(evb-rk3399 ficus-rk3399 khadas-edge-captain-rk3399 khadas-edge-v-rk3399 khadas-edge-rk3399 nanopc-t4-rk3399 nanopi-m4-rk3399 nanopi-neo4-rk3399 orangepi-rk3399 pinebook-pro-rk3399 puma-rk3399 rock960-rk3399 rock-pi-4-rk3399 rockpro64-rk3399 roc-pc-rk3399)
  if [[ " ${rk3399[*]} " == *" $board "* ]]; then
    echo "Board: $board using rk3399"
    cp /usr/share/arm-trusted-firmware/rk3399/* builds/$(echo $board)/
  fi
  # End ATF
  BINMAN_ALLOW_MISSING=1 make $(echo $board)_defconfig O=builds/$(echo $board)/
  BINMAN_ALLOW_MISSING=1 %make_build HOSTCC="gcc $RPM_OPT_FLAGS" CROSS_COMPILE="" DTC=/usr/bin/dtc O=builds/$(echo $board)/
done
%endif
%endif

%install
mkdir -p %{buildroot}%{_bindir}
mkdir -p %{buildroot}%{_sysconfdir}
mkdir -p %{buildroot}%{_mandir}/man1
mkdir -p %{buildroot}%{_datadir}/uboot/

%if %{with_armv8}
%ifarch aarch64
for board in $(cat %{_arch}-boards)
do
  mkdir -p %{buildroot}%{_datadir}/uboot/$(echo $board)/
  for file in spl/*spl.bin u-boot.bin u-boot.dtb u-boot-dtb.img u-boot.img u-boot.itb spl/sunxi-spl.bin
  do
    if [ -f builds/$(echo $board)/$(echo $file) ]; then
      install -p -m 0644 builds/$(echo $board)/$(echo $file) %{buildroot}%{_datadir}/uboot/$(echo $board)/
    fi
  done
done
%endif
%endif

for tool in dumpimage env/fw_printenv fit_check_sign fit_info gdb/gdbcont gdb/gdbsend gen_eth_addr gen_ethaddr_crc ifwitool img2srec kwboot mkeficapsule mkenvimage mkimage mksunxiboot ncb proftool sunxi-spl-image-builder
do
  install -p -m 0755 builds/tools/$tool %{buildroot}%{_bindir}
done
install -p -m 0644 doc/mkimage.1 %{buildroot}%{_mandir}/man1

install -p -m 0755 builds/tools/env/fw_printenv %{buildroot}%{_bindir}
( cd %{buildroot}%{_bindir}; ln -sf fw_printenv fw_setenv )

install -p -m 0644 tools/env/fw_env.config %{buildroot}%{_sysconfdir}

mkdir -p %{buildroot}/lib/kernel/install.d/
install -p -m 0755 %{SOURCE2} %{buildroot}/lib/kernel/install.d/


%files
%defattr(-,root,root)
%doc README
%{_bindir}/*
/lib/kernel/install.d/10-devicetree.install
%dir %{_datadir}/uboot/
%config(noreplace) %{_sysconfdir}/fw_env.config

%if %{with_armv8}
%ifarch aarch64
%files -n uboot-images-armv8
%defattr(-,root,root)
%{_datadir}/uboot/*
%endif
%endif

%files help
%doc README doc/develop/distro.rst doc/README.gpt
%doc doc/develop/uefi doc/usage doc/arch/arm64.rst
%{_mandir}/man1/mkimage.1*

%changelog
* Mon Nov 02 2026 lingsheng <ultra_planet@qq.com> - 2025.01-7
- fix CVE-2026-71971 CVE-2026-71972 CVE-2026-71973

* Wed Oct 28 2026 lingsheng <ultra_planet@qq.com> - 2025.01-6
- fix CVE-2026-74220 CVE-2026-74221

* Tue Sep 29 2026 Funda Wang <fundawang@yeah.net> - 2025.01-5
- build with system dtc to avoid incompatible pylibfdt swig code
- adapt DTC_FLAGS for dtc 1.8: drop removed graph_child_address flag,
  disable node_name_not_empty error on binman template nodes
  (use-system-dtc.patch)
- backport u-boot commit 527115ef6783 to replace SWIG Python 2 macros
  in pylibfdt, fixing the build with swig 4.5
  (backport-527115ef-pylibfdt-swig45.patch)
- remove redundant build dependencies swig, SDL2-devel, xxd, bc and
  perl-interpreter

* Fri Jul 10 2026 lingsheng <ultra_planet@qq.com> - 2025.01-4
- fix CVE-2026-29009

* Thu Apr 09 2026 lingsheng <ultra_planet@qq.com> - 2025.01-3
- fix CVE-2024-42040

* Thu Apr 02 2026 lingsheng <ultra_planet@qq.com> - 2025.01-2
- fix CVE-2026-33243

* Thu Feb 13 2025 lingsheng <lingsheng1@h-partners.com> - 2025.01-1
- update to 2025.01

* Wed Jan 22 2025 lingsheng <lingsheng1@h-partners.com> - 2024.01-2
- fix build with swig 4.3.0

* Thu Feb 29 2024 xingwei <xingwei14@h-partners.com> - 2024.01-1
- Type:requirement
- ID:NA
- SUG:NA
- update to 2024.01

* Wed Sep 28 2022 zhouwenpei <zhouwenpei1@h-partners.com> - 2021.10-7
- fix CVE-2022-30767

* Wed Jul 20 2022 cenhuilin <cenhuilin@kylinos.cn> - 2021.10-6
- fix CVE-2022-33967

* Tue Jul 12 2022 zhouwenpei <zhouwenpei1@h-partners.com> - 2021.10-5
- fix CVE-2022-34835

* Wed May 11 2022 liuyumeng <liuyumeng5@h-partners.com> - 2021.10-4
- fix license error

* Sat May 07 2022 liuyumeng <liuyumeng5@h-partners.com> - 2021.10-3
- fix license error

* Wed Apr 13 2022 yangcheng <yangcheng87@h-partners.com> - 2021.10-2
- Add perl buildrequires to resolve compilation error

* Mon Dec 6 2021 yangcheng <yangcheng87@huawei.com> - 2021.10-1
- Upgrade to 2021.10

* Wed Jul 21 2021 yushaogui <yushaogui@huawei.com> - 2020.07-7
- Delete a Buildrequires for gdb

* Mon Apr 19 2021 liuyumeng <liuyumeng@huawei.com> - 2020.07-6
- Compilation optimzation

* Tue Mar 16 2021 yanglu <yanglu@60huawei.com> - 2020.07-5
- Type:cves
- ID:CVE-2021-27097 CVE-2021-27138
- SUG:NA
- DESC:fix CVE-2021-27097CVE-2021-27138

* Wed Dec 16 2020 zhanzhimin <zhanzhimin@huawei.com> - 2020.07-4
- Update Source0

* Wed Oct 21 2020 jinzhimin <jinzhimin2@huawei.com> - 2020.07-3
- modify buildrequire to python3-unversioned-command 

* Tue Sep 10 2020 chengguipeng<chengguipeng1@huawei.com> - 2020.07-2
- Type:bugfix
- ID:NA
- SUG:NA
- DESC:modify source0 url

* Fri Jul 31 2020 chengguipeng<chengguipeng1@huawei.com> 2020.07-1
- Upgrade to 2020.07-1

* Fri Jun 19 2020 zhujunhao <zhujunhao8@huawei.com> - 2018.09-9
- drop python2 requires

* Fri Mar 20 2020 songnannan <songnannan2@huawei.com> - 2018.09-8
- add gdb in buildrequires

* Sat Feb 29 2020 hexiujun <hexiujun1@huawei.com> - 2018.09-7
- Type:enhancement
- ID:NA
- SUG:NA
- DESC: compatible with python3 compile environment

* Fri Jan 17 2020 Tianfei <tianfei16@huawei.com> - 2018.09-6
- Type:bugfix
- ID:NA
- SUG:NA
- DESC: delete patch

* Tue Jan 14 2020 openEuler Buildteam <buildteam@openeuler.org> - 2018.09-5
- Type:bugfix
- Id:NA
- SUG:NA
- DESC:close 3399

* Fri Oct 25 2019 openEuler Buildteam <buildteam@openeuler.org> - 2018.09-4
- Type:bugfix
- Id:NA
- SUG:NA
- DESC:add the README files to the main package

* Sat Oct 12 2019 openEuler Buildteam <buildteam@openeuler.org> - 2018.09-3
- Package init