# U-Boot: I.MX
# Contributor: Isaac David <isacdaavid@at@isacdaavid@dot@info>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: Timothy Redaelli <timothy.redaelli@gmail.com>
# Maintainer: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>

# TODO:
# - Add the following boards
#   - 'mx6cuboxi'
#   - 'nitrogen6q'
#   - 'udoo'
#   - 'usbarmory'
#   - 'wandboard'
# - Improve the .install:
#   - Make it more generic: share the main part of the code.
#   - Check for the machine name with the device tree and ask for confirmation
#     if the machine is not the same but don't prevent users from installing
#     this bootloader on a totally different machine as the installation could
#     be done in a chroot or in other ways that packagers can't predict.
#   - Handle more than one boot device (microSD, eMMC, USB, etc)
#   - Point to the device documentation if there is some

pkgbase=uboot4extlinux-imx
pkgname=('uboot4extlinux-tbs2910')
pkgver=2020.04
pkgrel=2
arch=('armv7h')
url="http://git.denx.de/u-boot.git/"
license=('GPL')
makedepends=('bc' 'dtc' 'python' 'python2' 'swig')
backup=(boot/extlinux/extlinux.conf)
source=("ftp://ftp.denx.de/pub/u-boot/u-boot-${pkgver}.tar.bz2"
        'extlinux.conf')
sha256sums=('fe732aaf037d9cc3c0909bad8362af366ae964bbdac6913a34081ff4ad565372'
            'SKIP')

# Distro boot config and documentation for the TBS TBS2910
# The patches are from the next branch of u-boot git
# a bit after the v2020.07-rc4 tag. They should make it in 1 or two
# u-boot release.
source+=('0001-board-tbs2910-disable-loadb-and-loads-commands.patch'
         '0002-board-tbs2910-disable-CONFIG_GZIP.patch'
         '0003-board-tbs2910-move-CONFIG_BOOTCOMMAND-from-header-to.patch'
         '0004-board-tbs2910_defconfig-Add-support-for-booting-gene.patch'
         '0005-board-tbs2910-Enable-distro_boot-support.patch'
         '0006-board-tbs2910-add-documentation.patch')
sha256sums+=('SKIP'
             'SKIP'
             'SKIP'
             'SKIP'
             'SKIP'
             'SKIP')

boards=('tbs2910')

prepare() {
  cd u-boot-${pkgver}

  # TBS2910 patches
  patch -Np1 -i ../0001-board-tbs2910-disable-loadb-and-loads-commands.patch
  patch -Np1 -i ../0002-board-tbs2910-disable-CONFIG_GZIP.patch
  patch -Np1 -i ../0003-board-tbs2910-move-CONFIG_BOOTCOMMAND-from-header-to.patch
  patch -Np1 -i ../0004-board-tbs2910_defconfig-Add-support-for-booting-gene.patch
  patch -Np1 -i ../0005-board-tbs2910-Enable-distro_boot-support.patch
  patch -Np1 -i ../0006-board-tbs2910-add-documentation.patch
}

build() {
  cd u-boot-${pkgver}

  unset CFLAGS CXXFLAGS LDFLAGS

  for i in ${boards[@]}; do
    mkdir -p ../bin_${i}
    make distclean
    make ${i}_defconfig
    echo 'CONFIG_IDENT_STRING=" Parabola GNU/Linux-libre"' >> .config
    make EXTRAVERSION=-${pkgrel}
    mv -f u-boot.imx ../bin_${i}
  done
}

package_uboot4extlinux-tbs2910() {
  pkgdesc="U-Boot with Extlinux support for the TBS2910 Matrix ARM mini PC"
  install=tbs2910.install
  provides=('uboot4extlinux-imx')

  install -d "${pkgdir}"/boot/extlinux
  install -d "${pkgdir}"/boot/u-boot/images/
  install -Dm644 bin_tbs2910/u-boot.imx "${pkgdir}"/boot/u-boot/images/

  install -Dm644 extlinux.conf "${pkgdir}"/boot/extlinux

  # Package documentation as well
  install -d "${pkgdir}"/usr/share/doc/u-boot
  install -Dm644 u-boot-${pkgver}/doc/board/tbs/tbs2910.rst "${pkgdir}"/usr/share/doc/u-boot
}
