# U-Boot: Pine64
# Maintainer: Kevin Mihelich <kevin@archlinuxarm.org>

buildarch=8

pkgname=uboot-pine64
pkgver=2018.03
pkgrel=1
pkgdesc="U-Boot for Pine64"
arch=('aarch64')
url='http://www.denx.de/wiki/U-Boot/WebHome'
license=('GPL')
backup=('boot/boot.txt' 'boot/boot.scr')
makedepends=('bc' 'git' 'python' 'swig' 'dtc145')
install=${pkgname}.install
source=("ftp://ftp.denx.de/pub/u-boot/u-boot-${pkgver/rc/-rc}.tar.bz2"
        'git+https://github.com/apritzel/arm-trusted-firmware.git#branch=allwinner'
        '0001-sunxi-disable-direct-MMC-environment.patch'
        '0002-sunxi-revert-disabling-of-features.patch'
        '0003-Set-board-name-for-Pine64.patch'
        '0001-ATF-set-fno-stack-protector.patch'
        'boot.txt'
        'mkscr')
md5sums=('02922bdf0ee003fe25bfc32749ffdeab'
         'SKIP'
         'f7c8d23581c1f8b4343e39a05305552f'
         '8452738859843d26e5763a4fc5797bda'
         'e6f1ffcb18361c238e5127d84f9bc9f8'
         '1d3b375fa978f3297d0fd3316f89e68a'
         'b523aea0f8fda4edc6846b7e7294f76e'
         '021623a04afd29ac3f368977140cfbfd')

prepare() {
  cd u-boot-${pkgver/rc/-rc}
  git apply ../0001-sunxi-disable-direct-MMC-environment.patch
  git apply ../0002-sunxi-revert-disabling-of-features.patch
  git apply ../0003-Set-board-name-for-Pine64.patch

  cd ../arm-trusted-firmware
  git apply ../0001-ATF-set-fno-stack-protector.patch
}

build() {
  unset CFLAGS CXXFLAGS CPPFLAGS LDFLAGS

  cd arm-trusted-firmware

  make PLAT=sun50iw1p1 DEBUG=1 bl31
  cp build/sun50iw1p1/debug/bl31.bin ../u-boot-${pkgver/rc/-rc}

  cd ../u-boot-${pkgver/rc/-rc}

  make distclean
  make pine64_plus_defconfig
  echo 'CONFIG_IDENT_STRING=" Arch Linux ARM"' >> .config
  make EXTRAVERSION=-${pkgrel}
}

package() {
  cd u-boot-${pkgver/rc/-rc}

  mkdir -p "${pkgdir}"/boot

  cat spl/sunxi-spl.bin u-boot.itb > u-boot-sunxi-with-spl.bin
  cp ../boot.txt ../mkscr u-boot-sunxi-with-spl.bin "${pkgdir}"/boot
  tools/mkimage -A arm -O linux -T script -C none -n "U-Boot boot script" -d "${pkgdir}"/boot/boot.txt "${pkgdir}"/boot/boot.scr
}