# Maintainer: David P. <megver83@parabola.nu>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: Márcio Silva <coadde@hyperbola.info>
# Contributor: fauno <fauno@parabola.nu>
# Contributor: Sorin-Mihai Vârgolici <smv@yobicore.org>
# Contributor: Michał Masłowski <mtjm@mtjm.eu>
# Contributor: Luke R. <g4jc@openmailbox.org>
# Contributor: Andreas Grapentin <andreas@grapentin.org>

# Based on linux package

_replacesarchkernel=('linux%') # '%' gets replaced with kernel suffix
_replacesoldkernels=() # '%' gets replaced with kernel suffix
_replacesoldmodules=() # '%' gets replaced with kernel suffix

pkgbase=linux-libre
pkgver=7.0.10.parabola1
pkgrel=1
pkgdesc='Linux-libre'
url='https://linux-libre.fsfla.org/'
arch=(
  x86_64
  i686
  armv7h
)
license=(GPL-2.0-only)
makedepends=(
  bc
  binutils
  cpio
  gettext
  glibc
  libelf
  libgcc
  openssl
  'pahole>=1:1.30' # minimun version required for vmlinux.h
  perl
  python
  #rust
  #rust-bindgen
  #rust-src
  tar
  xxhash
  xz
  zlib
  zstd

  # htmldocs
  graphviz
  imagemagick
  python-sphinx
  python-yaml
  texlive-latexextra
)
makedepends_x86_64=(
  # RUST is only enabled in x86_64
  rust
  rust-bindgen
  rust-src
)
makedepends_armv7h=(
  # required by linux-libre-chromebook
  uboot-tools
  vboot-utils
  dtc
  openssl-1.1 # uboot-tools needs an update, meanwhile use compatible openssl
)
options=(
  !debug
  !strip
)
_upstream_ver=${pkgver%.*}
_srctag=v${_upstream_ver}-arch${pkgver##*.parabola}
_srcname=linux-7.0
source=(
  "https://linux-libre.fsfla.org/pub/linux-libre/releases/${_srcname##*-}-gnu/linux-libre-${_srcname##*-}-gnu.tar.xz"{,.sign}
  "https://linux-libre.fsfla.org/pub/linux-libre/releases/$_upstream_ver-gnu/patch-${_srcname##*-}-gnu-$_upstream_ver-gnu.xz"{,.sign}
  "https://repo.parabola.nu/other/linux-libre/logos/logo_linux_"{clut224.ppm,vga16.ppm,mono.pbm}{,.sig}
  "https://github.com/archlinux/linux/releases/download/$_srctag/linux-$_srctag.patch.zst"{,.sig}

  # maintain the TTY over USB disconnects
  # http://www.coreboot.org/EHCI_Gadget_Debug
  0001-usb-serial-gadget-no-TTY-hangup-on-USB-disconnect-WI.patch
  # fix Atmel maXTouch touchscreen support
  # https://labs.parabola.nu/issues/877
  # http://www.fsfla.org/pipermail/linux-libre/2015-November/003202.html
  0002-fix-Atmel-maXTouch-touchscreen-support.patch
)
source_x86_64=(
  config.x86_64
  fix-firmware-rust.patch
)
source_i686=(
  config.i686
  # avoid using zstd compression in ultra mode (exhausts virtual memory)
  no-ultra-zstd.patch
)
source_armv7h=(
  config.armv7h
  # armv7h preset file for mkinitcpio ramdisk
  linux-armv7h.preset
  # files for signing veyron Chromebook kernels
  "kernel"{.its,.keyblock,_data_key.vbprivk}
  # Arch Linux ARM patches
  # https://github.com/archlinuxarm/PKGBUILDs/tree/master/core/linux-armv7
  0001-ARM-atags-add-support-for-Marvell-s-u-boot.patch
  0002-ARM-atags-fdt-retrieve-MAC-addresses-from-Marvell-bo.patch
  0003-fix-mvsdio-eMMC-timing.patch
  0004-set-default-cubietruck-led-triggers.patch
  0005-exynos4412-odroid-set-higher-minimum-buck2-regulator.patch
  0006-USB-Armory-MkII-support.patch
  # ChromiumOS patch
  # https://labs.parabola.nu/issues/2372
  0001-CHROMIUM-block-partitions-efi-Add-support-for-IGNORE.patch
)
validpgpkeys=(
  474402C8C582DAFBE389C427BCB7CF877E7D47A7  # Alexandre Oliva
  6DB9C4B4F0D8C0DC432CF6E4227CA7C556B2BA78  # David P.
  83BC8889351B5DEBBB68416EB8AC08600F108CDF  # Jan Alexander Steffens (heftig)
)
sha256sums=('14e107d8650fa2fe75a3876b0f3f46de11d8eb40cb7312cdd46b213d9bce3804'
            'SKIP'
            '4a6d245b5cc8845842efbd928440ae8ced843f0e16e0b8b46b90a45fd1b72fd9'
            'SKIP'
            'bfd4a7f61febe63c880534dcb7c31c5b932dde6acf991810b41a939a93535494'
            'SKIP'
            '6de8a8319271809ffdb072b68d53d155eef12438e6d04ff06a5a4db82c34fa8a'
            'SKIP'
            '13bd7a8d9ed6b6bc971e4cd162262c5a20448a83796af39ce394d827b0e5de74'
            'SKIP'
            '5aa17c8f41de0cfe212cdbaf97be5cddf304f08bd558b6763c41611cd73c3698'
            'SKIP'
            '0376bd5efa31d4e2a9d52558777cebd9f0941df8e1adab916c868bf0c05f2fc3'
            '351fd96be8cd5ebd0435c0a8a978673fc023e3b1026085e67f86d815b2285e25')
sha256sums_x86_64=('e0e4c1fbc60b07d98cc07d2b1f808bf58581adc41bdfdd92762184843d3754ba'
                   '7785f7796d94e6af76a2ee2540b9c8a329e5424f8cc348b970c6180d0ca4f2b8')
sha256sums_i686=('81362cefae8c1fa0cfb2db3d77e13051a1037caf036fa658e19480d23fde7d72'
                 'd32270be5fd9c3e3ba50f3aef33f6cfcb85be0c8216f03b777287cc621fdff28')
sha256sums_armv7h=('70fa541aa5a9d7a4b9963820ac06d5db959686d768e5a83c81f39fcc5853cda3'
                   '384ef83da92d605a6a40c7eca13e5afee04dc748484271fc53e2503e9a520195'
                   '523d52a6fc9df4c43c8401f17cd796ebd281845d05358c9564a64d8c5a63f028'
                   '4e708c9ec43ac4a5d718474c9431ba6b6da3e64a9dda6afd2853a9e9e3079ffb'
                   'bc9e707a86e55a93f423e7bcdae4a25fd470b868e53829b91bbe2ccfbc6da27b'
                   '64181f970a54a3404ab423406b21308bd80537222aed7053a868a83ac191c2c9'
                   'be1d84b99d70b31049e3ea6f4a0b694ecc78b15c8eb49372840ac22574e1ada9'
                   '1b626f9f75d4383f3794b59642c3002379b04f31ade4f0c73db1bb30844772f0'
                   '76335e05445425678b94d15bc8314b3b42f86ee144b2fbdf5dc1b6d828c7ec9e'
                   'b8e4a3d2ef42a20c65a4e90fd3ff1fc0c2feaa47f97c09eb8dd24e61ad18974c'
                   'f82e21d4da0feeb6a3a2dfe2f70962057a4717da649c86343c390ff7c0968bde'
                   'f4d4b44adc4b47b18bac33aa2c46b458c1b7ceb277416eff59054b6d7687c6da')
b2sums=('45893cd901662c1a684f649aebdd2e765c16cfe520d7bc08ae5150748c6cd809452258f89f2cd331bdb8dff898a4a60e798a397f9aa22080476eab3145ec14d6'
        'SKIP'
        '8b2e1372a3da2f103164eebae7d27ea8169037bd8ca6eddd380c2976026e28d8ad66ba62896086f0034ccdb01ec900d0e7493343941d12b625bb7a00f22737d9'
        'SKIP'
        '73fee2ae5cb1ffd3e6584e56da86a8b1ff6c713aae54d77c0dab113890fc673dc5f300eb9ed93fb367b045ece8fa80304ff277fe61665eccf7b7ce24f0c045eb'
        'SKIP'
        'd02a1153a4285b32c774dca4560fe37907ccf30b8e487a681b717ed95ae9bed5988875c0a118938e5885ae9d2857e53a6f216b732b6fa3368e3c5fe08c86382c'
        'SKIP'
        '580911af9431c066bbc072fd22d5e2ef65f12d8358cec5ff5a4f1b7deebb86cef6b5c1ad631f42350af72c51d44d2093c71f761234fb224a8b9dbb3b64b8201d'
        'SKIP'
        '0701e826f811a79123f89c0d034cb753d3a6237ee5e387c8c927efc1114dbeac6ef095e88eedecf18b9d69fcefa605b2425ffb119c5e40026e388464f75c350e'
        'SKIP'
        'c2214154c36900e311531bfe68184f31639f5c50fed23bc3803a7f18439b7ff258552a39f02fed0ea92f10744e17a6c55cef0ef1a98187f978fe480fb3dddc14'
        '0c7ceba7cd90087db3296610a07886f337910bad265a32c052d3a703e6eb8e53f355ab9948d72d366408d968d8ee7435084dd89bef5ed0b69355fd884c2cd468')
b2sums_x86_64=('2922cc9958b89914846b6aa2bf4cde3419de7b1e9e2f267b56153d3341d5709187176ba0a4d5bffa029a367171e3e28cd1499cd87c2e1d1164736a40d43515e8'
               '134cc519fcc14e1ab1894cf7ffd2c273af71dded5e16305e4b988952c02d38fc10b38812c207a41fff5bd2ca8d8daa363b2b46bcdc3472a8a9d15c2db5e630ea')
b2sums_i686=('91e240b6bf4d8c9df73d6d1e7e53156efc23d4789d6f92636c1aa6cfbf24d5bb7f153ae3d47d32f0aaada9bd1108cfe77f8fcd6c559e091eefa11b67b71634a9'
             '165ab9dd8cedeaae5327accc1581c19cf0be55f923b03feb889cad3351b74c7c4cd3d3c206938e5152bfe1d947513dea8f630f8f5544099ec13d16d254725c40')
b2sums_armv7h=('aa4e823828ce1d1f2b1d47df121c7e03b199f19dbce478aeb9cd118de5024e8346ac876903dc939c9c8337962687859c85ee4e5daeda5bbbe8800a4fec583f94'
               '99d4004343704cc06287c1b34bee4d52f4916353556abd7e18fd9e6d063de18945df4ddccb493590853b33e9e3d0bf64926bf8dbd64ce6f9086add9a10989e76'
               '9336fb5d1f98a77144673750b0f924b15b60e98e4bba0d13d1b1a5c977f14538f97143147081c94e0d8555b5dbd246fe0a410c7146a9b0866f3728176548fb0a'
               'eeed12b2ab60c3d3aad598a1d44b4f23560d818e8fe1ef143f857c8e176652df53501b192ade7d4d915d425fff818b2a232d46bae0d3a0f46b8959e614e0ede2'
               'aaa4e28a31967cc3a7fe25a86ba35fdfa210cd8b1a9cc96298349cbf01d60cdf146ee519d6803d05b175873f1b3367e47194a178db7ed97c802e59b38f8c303c'
               'd90f8d0d4d9c654995f18102c085acf811d312c4d8eb409ffc58d79afbcfe89f852a1e7c72409e714a6440edf1761ab16067e6390f2976b83085dd62b0de0eec'
               'b9243d4f148afbb5315c0714b3fc0fa178a2a0859cbae5889ac549a763b6ea43d2d79bf593c180c851c0a7fb2422a2f114004992a93c2721d6d2b41912c443aa'
               'fa16aa8640538879f664f692f89f996ce3cbe453e12e224a0fcae8840b8e2bf950692f7830b8dc43b32d91584556027fee22a2b8a579daff6292c2e0b3c70fc1'
               'cd68d065bf2c27fc32832ceb8fc9493f51c61f115c1b4214d6713f244e77954ea6ba74dd75fdfbd78ff4e5d70ea6a5487843fd3d2cf69fdbc822eed4941c4865'
               'dcb4b48205c6e4518a0bbbb5a2afbef5ef538878dfe363190a765b453a6ce334b454b7054d4b4cd568466daa0ee8cbbcf27a964205b4c009ea4c6ef714aad6f6'
               '6eaedfc0372874c32f2fea23124d9520cfdb0e59ef30d758db65a685c61c82ab67a493b081b9bd46f65107bcfeda5b1de3b6358351d8dd0de969bcdc8a3fa16f'
               '741ad7ffc9e8200657315fc4111066b2477cde35ecdc5e73976457f17106f80d148d86bf97b92c523fef1cc5a26ddb867d16330f592f2aa4d886be596bedc8df')

_replacesarchkernel=("${_replacesarchkernel[@]/\%/${pkgbase#linux-libre}}")
_replacesoldkernels=("${_replacesoldkernels[@]/\%/${pkgbase#linux-libre}}")
_replacesoldmodules=("${_replacesoldmodules[@]/\%/${pkgbase#linux-libre}}")

export KBUILD_BUILD_HOST=parabola
export KBUILD_BUILD_USER=$pkgbase
export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"

prepare() {
  cd $_srcname

  if [ "${_srcname##*-}" != "$_upstream_ver" ]; then
    echo "Applying upstream patch..."
    patch -Np1 < "../patch-${_srcname##*-}-gnu-$_upstream_ver-gnu"
  fi

  echo "Adding freedo as boot logo..."
  install -m644 -t drivers/video/logo \
    ../logo_linux_{clut224.ppm,vga16.ppm,mono.pbm}

  echo "Setting version..."
  echo "-$pkgrel" > localversion.10-pkgrel
  echo "${pkgbase#linux-libre}" > localversion.20-pkgname

  # save extraversion as localversion to fit Arch patch while keeping the original value
  awk '/^EXTRAVERSION/ {print $3}' Makefile > localversion.00-extraversion
  sed -i '/^EXTRAVERSION[[:space:]]*=/ s/=.*$/=/' Makefile

  local source=("${source[@]}")
  eval "source+=(\${source_${CARCH}[@]})"

  local src
  for src in "${source[@]}"; do
    src="${src%%::*}"
    src="${src##*/}"
    src="${src%.zst}"
    [[ $src = *.patch ]] || continue
    echo "Applying patch $src..."
    patch -Np1 < "../$src"
  done

  # save extraversion as localversion after patching for correct string concatenation order
  awk '/^EXTRAVERSION/ {gsub(/arch/, "parabola", $3); print $3}' Makefile > localversion.01-extraversion
  sed -i '/^EXTRAVERSION[[:space:]]*=/ s/=.*$/=/' Makefile

  echo "Setting config..."
  cp ../config.$CARCH .config
  make olddefconfig
  diff -u ../config.$CARCH .config || :

  make -s kernelrelease > version
  echo "Prepared $pkgbase version $(<version)"

  if ! [[ $(scripts/config -s CONFIG_DEBUG_INFO_BTF) = y ]]; then
    echo "CONFIG_DEBUG_INFO_BTF is required to build vmlinux.h"
    exit 1
  fi
}

build() {
  cd $_srcname
  make all
  make -C tools/bpf/bpftool vmlinux.h feature-clang-bpf-co-re=1
  make htmldocs SPHINXOPTS=-QT
}

_package() {
  pkgdesc="The $pkgdesc kernel and modules"
  depends=(
    coreutils
    initramfs
    kmod
  )
  optdepends=(
    "$pkgbase-headers: headers and scripts for building modules"
    'linux-libre-firmware: firmware images needed for some devices'
    #'scx-scheds: to use sched-ext schedulers'
    'wireless-regdb: to set the correct wireless channels of your country'
  )
  optdepends_x86_64=(
    'scx-scheds: to use sched-ext schedulers' # only available for x86_64
  )
  provides=(
    KSMBD-MODULE
    NTSYNC-MODULE
    VIRTUALBOX-GUEST-MODULES
    WIREGUARD-MODULE
  )
  replaces=(
    virtualbox-guest-modules-arch
    wireguard-arch
  )
  provides+=(
    "${_replacesarchkernel[@]/%/=$_upstream_ver}"
    "LINUX-ABI_VERSION=$_upstream_ver"
  )
  conflicts+=(
    "${_replacesarchkernel[@]}"
    "${_replacesoldkernels[@]}"
    "${_replacesoldmodules[@]}"
  )
  replaces+=(
    "${_replacesarchkernel[@]}"
    "${_replacesoldkernels[@]}"
    "${_replacesoldmodules[@]}"
  )

  cd $_srcname
  local modulesdir="$pkgdir/usr/lib/modules/$(<version)"

  echo "Installing boot image..."
  # systemd expects to find the kernel here to allow hibernation
  # https://github.com/systemd/systemd/commit/edda44605f06a41fb86b7ab8128dcf99161d2344
  install -Dm644 "$(make -s image_name)" "$modulesdir/vmlinuz"

  # Used by mkinitcpio to name the kernel
  echo "$pkgbase" | install -Dm644 /dev/stdin "$modulesdir/pkgbase"

  echo "Installing modules..."
  ZSTD_CLEVEL=19 make INSTALL_MOD_PATH="$pkgdir/usr" INSTALL_MOD_STRIP=1 \
    DEPMOD=/doesnt/exist modules_install  # Suppress depmod

  # remove build link
  rm "$modulesdir"/build

  if [ "$CARCH" = "armv7h" ]; then
    echo "Installing dtbs..."
    make INSTALL_DTBS_PATH="$pkgdir/boot/dtbs/$pkgbase" dtbs_install

    # sed expression for following substitutions
    local _subst="
      s|%PKGBASE%|${pkgbase}|g
      s|%KERNVER%|$(<version)|g
    "

    # install mkinitcpio preset file
    sed "${_subst}" ../linux-armv7h.preset |
      install -Dm644 /dev/stdin "${pkgdir}/etc/mkinitcpio.d/${pkgbase}.preset"
  fi
}

_package-headers() {
  pkgdesc="Headers and scripts for building modules for the $pkgdesc kernel"
  depends=(
    binutils
    glibc
    libelf
    libgcc
    openssl
    pahole
    xxhash
    zlib
    zstd
  )
  provides=(LINUX-HEADERS)
  provides+=(
    "${_replacesarchkernel[@]/%/-headers=$_upstream_ver}"
  )
  conflicts+=(
    "${_replacesarchkernel[@]/%/-headers}"
    "${_replacesoldkernels[@]/%/-headers}"
  )
  replaces+=(
    "${_replacesarchkernel[@]/%/-headers}"
    "${_replacesoldkernels[@]/%/-headers}"
  )

  cd $_srcname
  local builddir="$pkgdir/usr/lib/modules/$(<version)/build"

  case "$CARCH" in
    i686|x86_64) karch=x86;;
    armv7h) karch=arm;;
    *) echo "Unknown CARCH $CARCH"; exit 1 ;;
  esac

  echo "Installing build files..."
  install -Dt "$builddir" -m644 .config Makefile Module.symvers System.map \
    localversion.* version vmlinux tools/bpf/bpftool/vmlinux.h
  install -Dt "$builddir/kernel" -m644 kernel/Makefile
  install -Dt "$builddir/arch/$karch" -m644 arch/$karch/Makefile
  [ "$CARCH" != i686 ] || install -Dt "$builddir/arch/x86" -m644 arch/x86/Makefile_32.cpu
  cp -t "$builddir" -a scripts
  ln -srt "$builddir" "$builddir/scripts/gdb/vmlinux-gdb.py"

  if [[ $(scripts/config -s CONFIG_HAVE_STACK_VALIDATION) = y ]]; then
    install -Dt "$builddir/tools/objtool" tools/objtool/objtool
  fi

  if [[ $(scripts/config -s CONFIG_DEBUG_INFO_BTF_MODULES) = y ]]; then
    install -Dt "$builddir/tools/bpf/resolve_btfids" tools/bpf/resolve_btfids/resolve_btfids
  fi

  echo "Installing headers..."
  cp -t "$builddir" -a include
  cp -t "$builddir/arch/$karch" -a arch/$karch/include
  install -Dt "$builddir/arch/$karch/kernel" -m644 arch/$karch/kernel/asm-offsets.s

  install -Dt "$builddir/drivers/md" -m644 drivers/md/*.h
  install -Dt "$builddir/net/mac80211" -m644 net/mac80211/*.h

  # https://bugs.archlinux.org/task/13146
  install -Dt "$builddir/drivers/media/i2c" -m644 drivers/media/i2c/msp3400-driver.h

  # https://bugs.archlinux.org/task/20402
  install -Dt "$builddir/drivers/media/usb/dvb-usb" -m644 drivers/media/usb/dvb-usb/*.h
  install -Dt "$builddir/drivers/media/dvb-frontends" -m644 drivers/media/dvb-frontends/*.h
  install -Dt "$builddir/drivers/media/tuners" -m644 drivers/media/tuners/*.h

  # https://bugs.archlinux.org/task/71392
  install -Dt "$builddir/drivers/iio/common/hid-sensors" -m644 drivers/iio/common/hid-sensors/*.h

  echo "Installing KConfig files..."
  find . -name 'Kconfig*' -exec install -Dm644 {} "$builddir/{}" \;

  echo "Installing Rust files..."
  if [[ $(scripts/config -s CONFIG_RUST) = y ]]; then
    install -Dt "$builddir/rust" -m644 rust/*.rmeta
    install -Dt "$builddir/rust" rust/*.so
  fi

  echo "Installing unstripped VDSO..."
  make INSTALL_MOD_PATH="$pkgdir/usr" vdso_install \
    link=  # Suppress build-id symlinks

  echo "Removing unneeded architectures..."
  local arch
  for arch in "$builddir"/arch/*/; do
    [[ $arch = */$karch/ ]] && continue
    echo "Removing $(basename "$arch")"
    rm -r "$arch"
  done

  echo "Removing documentation..."
  rm -r "$builddir/Documentation"

  echo "Removing broken symlinks..."
  find -L "$builddir" -type l -printf 'Removing %P\n' -delete

  echo "Removing loose objects..."
  find "$builddir" -type f -name '*.o' -printf 'Removing %P\n' -delete

  echo "Stripping build tools..."
  local file
  while read -rd '' file; do
    case "$(file -Sib "$file")" in
      application/x-sharedlib\;*)      # Libraries (.so)
        strip -v $STRIP_SHARED "$file" ;;
      application/x-archive\;*)        # Libraries (.a)
        strip -v $STRIP_STATIC "$file" ;;
      application/x-executable\;*)     # Binaries
        strip -v $STRIP_BINARIES "$file" ;;
      application/x-pie-executable\;*) # Relocatable binaries
        strip -v $STRIP_SHARED "$file" ;;
    esac
  done < <(find "$builddir" -type f -perm -u+x ! -name vmlinux -print0)

  echo "Stripping vmlinux..."
  strip -v $STRIP_STATIC "$builddir/vmlinux"

  echo "Adding symlink..."
  mkdir -p "$pkgdir/usr/src"
  ln -sr "$builddir" "$pkgdir/usr/src/$pkgbase"
}

_package-docs() {
  pkgdesc="Documentation for the $pkgdesc kernel"
  provides=(
    "${_replacesarchkernel[@]/%/-docs=$_upstream_ver}"
  )
  conflicts=(
    "${_replacesarchkernel[@]/%/-docs}"
    "${_replacesoldkernels[@]/%/-docs}"
  )
  replaces=(
    "${_replacesarchkernel[@]/%/-docs}"
    "${_replacesoldkernels[@]/%/-docs}"
  )

  cd $_srcname
  local builddir="$pkgdir/usr/lib/modules/$(<version)/build"

  echo "Installing documentation..."
  local src dst
  while read -rd '' src; do
    dst="${src#Documentation/}"
    dst="$builddir/Documentation/${dst#output/}"
    install -Dm644 "$src" "$dst"
  done < <(find Documentation -name '.*' -prune -o ! -type d -print0)

  echo "Adding symlink..."
  mkdir -p "$pkgdir/usr/share/doc"
  ln -sr "$builddir/Documentation" "$pkgdir/usr/share/doc/$pkgbase"
}

_package-chromebook() {
  pkgdesc="$pkgdesc kernel sign for Veyron Chromebooks"
  depends=(linux-libre=$_upstream_ver)
  provides=("${_replacesarchkernel[@]/%/-armv7-chromebook=$_upstream_ver}")
  conflicts=("${_replacesarchkernel[@]/%/-armv7-chromebook}" "${_replacesoldkernels[@]/%/-armv7-chromebook}")
  replaces=("${_replacesarchkernel[@]/%/-armv7-chromebook}" "${_replacesoldkernels[@]/%/-armv7-chromebook}")
  install=$pkgbase-chromebook.install

  cd $_srcname

  cp ../kernel.its .
  mkimage -D "-I dts -O dtb -p 2048" -f kernel.its kernel.signed
  dd if=/dev/zero of=bootloader.bin bs=512 count=1
  echo 'console=tty0 init=/sbin/init root=PARTUUID=%U/PARTNROFF=1 rootwait rw noinitrd' > cmdline

  echo "Creating kernel sign..."
  vbutil_kernel \
    --pack vmlinux.kpart \
    --version 1 \
    --vmlinuz kernel.signed \
    --arch arm \
    --keyblock ../kernel.keyblock \
    --signprivate ../kernel_data_key.vbprivk \
    --config cmdline \
    --bootloader bootloader.bin

  echo "Installing kernel sign..."
  mkdir -p "$pkgdir/boot"
  cp vmlinux.kpart "$pkgdir/boot"
}

pkgname=(
  "$pkgbase"
  "$pkgbase-headers"
  "$pkgbase-docs"
)
[ "$CARCH" = "armv7h" ] && pkgname+=("$pkgbase-chromebook")
for _p in "${pkgname[@]}"; do
  eval "package_$_p() {
    $(declare -f "_package${_p#$pkgbase}")
    _package${_p#$pkgbase}
  }"
done
# vim:set ts=8 sts=2 sw=2 et:
