# U-Boot: sunXi
# Maintainer: Isaac David <isacdaavid@at@isacdaavid@dot@info>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: Timothy Redaelli <timothy.redaelli@gmail.com>
# Contributor: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
# Maintainer: Parabola Hackers <dev@lists.parabola.nu>

# To add a new board (that uses an Allwinner System On a Chip) you need:
#
#  1. The u-boot configuration name. Example: `A20-OLinuXino_MICRO_defconfig`
#
#     Remove the the `_defconfig` suffix and add this to the `_targets=()`
#     variable near the top of this PKGBUILD.
#
#  2. The name of the board(s). Example: `A20 OLinuXino Micro`.
#
#     This is usually very similar to the u-boot configuration name, but
#     sometimes a single u-boot configuration supports multiple boards and the
#     configuration name doesn't always denote all board names.  For example:
#     uboot4extlinux-am335x_bone supports various BeagleBones (the AM335x GP EVM
#     and the EVM SK).
#
#     Add a `package_uboot4extlinux-*()` function to the bottom of this
#     PKGBUILD, following the existing pattern.  The `*` in the function name
#     should be the above (1) configuration, but converted to all-lowercase.
#
#  3. (Optional) If relevant, the Parabola and/or Arch Linux ARM package names
#     that it replaces, if there are older u-boot packages also supporting that
#     board. Example: `uboot-a20-olinuxino-micro`
#
# After adding a new board in this PKGBUILD, you should also create a
# 'Computers/' page on the Parabola wiki for that board, and update the relevant
# sections of the 'ARM Installation Guide' to refer to it for any board-specific
# details:
#
#  * https://wiki.parabola.nu/ARM_Installation_Guide
#  * https://wiki.parabola.nu/Computers

# Upstream u-boot-libre information ############################################

_pkgname=u-boot-libre
_pkgver=2021.07-r1 # {upstream_uboot_version}-{uboot-libre_suffix}
url="https://libreboot.org/docs/maintain/#resourcesscriptsbuildreleaseu-boot-libre"
license=('GPL')
# According to the Libreboot maintainer, we are supposed to use
# any of the mirrors in https://libreboot.org/download.html#https
# and not use the rsync server. The rsync server is only supposed
# to be used by mirrors as it has limited bandwith.
_mirror="https://mirrors.mit.edu/libreboot/"
source=("${_mirror}/${_pkgname}/${_pkgver}/${_pkgname}-${_pkgver}.tar.lz"{,.sig})

# TODO: We also need to look into drivers/dma/MCD_tasks.c. The debian
#       version of u-boot removes it. It's some data that looks like
#       code, so while there is a valid license, we might lack
#       corresponding source code if it's really some code. The debian
#       commits might have more information about it.

# Hardware information #########################################################

pkgbase=uboot4extlinux-sunxi
# These must match the "${target}_defconfig" `make` targets.
_targets=('A10-OLinuXino-Lime'
          'A10s-OLinuXino-M'
          'A13-OLinuXino'
          'A13-OLinuXinoM'
          'A20-OLinuXino-Lime'
          'A20-OLinuXino-Lime2'
          'A20-OLinuXino-Lime2-eMMC'
          'A20-OLinuXino_MICRO'
          'Bananapi'
          'Bananapro'
          'CHIP'
          'Cubieboard'
          'Cubieboard2'
          'Cubietruck'
          'Linksprite_pcDuino'
          'Linksprite_pcDuino3'
          'Linksprite_pcDuino3_Nano'
          'orangepi_2'
          'orangepi_one'
          'orangepi_pc'
          'orangepi_plus')

# Offset at which to install u-boot
_offset=8192

# Parabola package information #################################################

pkgname=()
for _target in "${_targets[@]}"; do
    pkgname+=("uboot4extlinux-${_target,,}")
done
pkgver="${_pkgver/-/.}"
pkgrel='4'

arch=('armv7h' 'i686' 'x86_64')
makedepends=('bc' 'dtc' 'python' 'python-setuptools' 'swig')
makedepends_i686+=('arm-none-eabi-gcc')
makedepends_ppc64le+=('arm-none-eabi-gcc')
makedepends_x86_64+=('arm-none-eabi-gcc')
depends=(
    uboot-tools # duh
    util-linux  # for `blkid` in our `install.sh.in`
)

source+=('0001-Fix-build-with-python-3.10.patch'
	 'extlinux.conf'
         'install.sh.in'
         'install.txt.gen'
         'pacman-hook.hook.in')

sha512sums=('75b97fd6ca5435311e3b6b12cb44003232b09d87bbccc35144c5f12a3b41892849d56446c8b31fd6ee50f60b45f741d29b5be7fa577cbae62c308196f4bb47a2'
            'SKIP'
            '0f12e1705656edd28e0e1e3923b0b72a191fc4dd60ac4ab81473ce521343e65841561bf29c142310654ab8064c2c5ce0989499517f57278aadaac181bcfbe5c1'
            'dcbcbc8d196c7dd7db1a8a30dfa8c9ece5d241580bdf593df5fa2de527f0dfea2bb76ff9bfe49fb1e9d631bd1e4b95c828a41cfb38dc2ddc9553e18a54a521f0'
            '9bb359773453dfed71c28790c85be0714f961230e9a2160b9e6c05678afaf98e4fe9f9283f6eb4593def4214f3dbfc5409b566b1d845350330859e8e5500b30f'
            'f497ec67904e6c99723cdfec10f776a8672d52180a423605e84d11b7f35d1409eeb98efa2338c94681a2ea3ff1e5a636bc74bd562b3796334bf3c46445b6db51'
            '735e497d96c719a20dd145020110638ef2e3b84e39491e29a884d2684085c1a898b0f62dabfff9bd4ce83bc68febcf638de6ca35a6fa9b157be413b5138f7a07')
validpgpkeys=('98CCDDF8E56047F475C044BDD0C62464FA8B4856') # Leah Rowe <info@minifree.org>

# TODO: We need to fix the timestamps upstream to a valid date
#       (like the first January 1970 at 01:00:00 CET) or something derived
#       from the release version somehow. Else we have the build log spammed
#       with errors like that:
#       make: scripts/Makefile.extrawarn: Timestamp out of range;
#             substituting 1970-01-01 00:59:59.999999999
# TODO: While we're at it we could export SOURCE_DATE_SPOCH to try to
#       make the u-boot package reproducible.
prepare() {
    cd "${_pkgname}-${_pkgver}"
    patch -p1 -i ../0001-Fix-build-with-python-3.10.patch
    cd ..

    find  "${_pkgname}-${_pkgver}" -print0 | xargs -0r touch --no-dereference --date="@${SOURCE_DATE_EPOCH}"

    local _target
    for _target in "${_targets[@]}"; do
	cp -aT -- "${_pkgname}-${_pkgver}" "${_pkgname}-${_pkgver}-${_target,,}"
    done
}

build() {
    unset CFLAGS CXXFLAGS LDFLAGS

    local _make_vars=(
	    ARCH='arm'
	    EXTRAVERSION="-${_pkgver#*-}"
    )
    if [[ "$CARCH" != armv7h ]]; then
	    _make_vars+=(CROSS_COMPILE='arm-none-eabi-')
    fi

    local _i=0 _n=${#_targets[@]}
    local _target
    for _target in "${_targets[@]}"; do
	printf '==> (%d/%d) %s\n' "$((++_i))" "$_n" "$_target"
	pushd "${_pkgname}-${_pkgver}-${_target,,}"

	# Configure
	make "${_make_vars[@]}" "${_target}_defconfig"
	echo 'CONFIG_IDENT_STRING=" Parabola GNU/Linux-libre"' >> .config

	# Build
	make "${_make_vars[@]}"

	popd
    done
}

check() {
    local _passing=true

    local _i=0 _n=${#_targets[@]}
    local _target
    for _target in "${_targets[@]}"; do
	printf '==> (%d/%d) %s\n' "$((++_i))" "$_n" "$_target"

	local _image_file _image_size
	_image_file="${_pkgname}-${_pkgver}-${_target,,}/u-boot-sunxi-with-spl.bin"
	_image_size="$(stat --format='%s' "$_image_file")"
	if [[ $((_offset + _image_size)) -gt $(( 1024 * 1024 ))  ]]; then
            echo "Error: ${_image_file} is too big:"
            echo "         offset: ${_offset}"
            echo "         size:   ${_image_size}"
            echo "       By default, partitioing tools position the first partition at 1MiB."
            echo "       The U-Boot image would overlap with that first partition."
	    _passing=false
	fi
    done

    $_passing
}

_package() {
    # Install the u-boot binary.
    install -Dm644 \
	    "${_pkgname}-${_pkgver}-${pkgname#uboot4extlinux-}/u-boot-sunxi-with-spl.bin" \
            "${pkgdir}/usr/lib/u-boot/${pkgname}/u-boot-sunxi-with-spl.bin"

    # Install Parabola's example config file.
    install -Dm644 \
            extlinux.conf \
            "${pkgdir}/usr/lib/u-boot/${pkgname}/extlinux.conf"

    # Install Parabola's installation script.
    install -Dm755 \
	    <(sed <install.sh.in \
		  -e "s|@uboot_file@|/usr/lib/u-boot/${pkgname}/u-boot-sunxi-with-spl.bin|g" \
		  -e "s|@uboot_offset@|${_offset}|g") \
	    "${pkgdir}/usr/lib/u-boot/${pkgname}/install.sh"

    # Install documentation for that installation script.
    install -Dm644 \
	    <(./install.txt.gen "$pkgname" "/usr/lib/u-boot/${pkgname}/install.sh") \
	    "${pkgdir}/usr/share/doc/u-boot/${pkgname}/install.txt"	    

    # Install a pacman hook that `cat`s that documentation.
    install -Dm644 \
	    <(sed <pacman-hook.hook.in \
		  -e "s|@pkgname[@]|${pkgname}|g") \
            "${pkgdir}/usr/share/libalpm/hooks/${pkgname}.hook"
}

package_uboot4extlinux-a10-olinuxino-lime()       { pkgdesc="U-Boot with Extlinux support for A10 OLinuXino Lime"                 ; _package; }
package_uboot4extlinux-a10s-olinuxino-m()         { pkgdesc="U-Boot with Extlinux support for A10s OLinuXino Micro"               ; _package; replaces=('uboot4extlinux-a10s-olinuxino-micro'); }
package_uboot4extlinux-a13-olinuxino()            { pkgdesc="U-Boot with Extlinux support for A13 OLinuXino"                      ; _package; }
package_uboot4extlinux-a13-olinuxinom()           { pkgdesc="U-Boot with Extlinux support for uboot4extlinux-a13-olinuxino-micro" ; _package; replaces=('uboot4extlinux-a13-olinuxino-micro'); }
package_uboot4extlinux-a20-olinuxino-lime()       { pkgdesc="U-Boot with Extlinux support for A20 OLinuXino Lime"                 ; _package; }
package_uboot4extlinux-a20-olinuxino-lime2()      { pkgdesc="U-Boot with Extlinux support for A20 OLinuXino Lime2"                ; _package; }
package_uboot4extlinux-a20-olinuxino-lime2-emmc() { pkgdesc="U-Boot with Extlinux support for A20 OLinuXino Lime2 with eMMC"      ; _package; }
package_uboot4extlinux-a20-olinuxino_micro()      { pkgdesc="U-Boot with Extlinux support for uboot-a20-olinuxino-micro"          ; _package; replaces=('uboot-a20-olinuxino-micro'); }
package_uboot4extlinux-bananapi()                 { pkgdesc="U-Boot with Extlinux support for Banana Pi"                          ; _package; }
package_uboot4extlinux-bananapro()                { pkgdesc="U-Boot with Extlinux support for Banana Pro"                         ; _package; }
package_uboot4extlinux-chip()                     { pkgdesc="U-Boot with Extlinux support for C.H.I.P"                            ; _package; }
package_uboot4extlinux-cubieboard()               { pkgdesc="U-Boot with Extlinux support for Cubieboard"                         ; _package; }
package_uboot4extlinux-cubieboard2()              { pkgdesc="U-Boot with Extlinux support for Cubieboard 2"                       ; _package; }
package_uboot4extlinux-cubietruck()               { pkgdesc="U-Boot with Extlinux support for Cubietruck"                         ; _package; }
package_uboot4extlinux-linksprite_pcduino()       { pkgdesc="U-Boot with Extlinux support for uboot4extlinux-pcduino"             ; _package; replaces=('uboot4extlinux-pcduino'); }
package_uboot4extlinux-linksprite_pcduino3()      { pkgdesc="U-Boot with Extlinux support for uboot4extlinux-pcduino3"            ; _package; replaces=('uboot4extlinux-pcduino3'); }
package_uboot4extlinux-linksprite_pcduino3_nano() { pkgdesc="U-Boot with Extlinux support for uboot4extlinux-pcduino3-nano"       ; _package; replaces=('uboot4extlinux-pcduino3-nano'); }
package_uboot4extlinux-orangepi_2()               { pkgdesc="U-Boot with Extlinux support for Orange Pi 2"                        ; _package; }
package_uboot4extlinux-orangepi_one()             { pkgdesc="U-Boot with Extlinux support for Orange Pi One"                      ; _package; }
package_uboot4extlinux-orangepi_pc()              { pkgdesc="U-Boot with Extlinux support for Orange Pi PC"                       ; _package; }
package_uboot4extlinux-orangepi_plus()            { pkgdesc="U-Boot with Extlinux support for Orange Pi Plus"                     ; _package; }
