#!/bin/sh
# Copyright (C) 2016 Arista Networks, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Aboot stage 0 boot

set -x

kernel=boot/vmlinuz-3.16.0-4-amd64
initrd=boot/initrd.img-3.16.0-4-amd64
kernel_params=kernel-params

aboot_machine="arista_unknown"

target_path=/mnt/flash

# expect the swi to be a non empty file
[ -s "$swipath" ] || exit 1

bootconfigvars="SWI SWI_COPY POST_LEVEL CONSOLESPEED PASSWORD NETDEV NETAUTO NETIP NETMASK NETGW NETDOMAIN NETDNS NETHW memtest"

parse_environment_config() {
    for n in ${bootconfigvars}; do
        eval v="\$$n"
        if [ "$v" ]; then
            echo "$n=$v"
        fi
    done
}

extract_image() {
    ## Remove all the other unnecssary files except swi file, boot-config
    for f in $(ls -A $target_path); do
        if [ $f != "${swipath##*/}" ] && [ $f != "boot-config" ]; then
            rm -rf "$target_path/$f"
        fi
    done

    ## Unzip the image
    unzip -oq "$swipath" -x boot0 -d "$target_path"

    ## Remove installer swi as it has lots of redundunt contents
    rm -f $swipath

    ## detect rootfs type
    rootfs_type=`grep /mnt/flash /proc/mounts | cut -d' ' -f3`

    ## vfat does not support symbol link
    if [ $rootfs_type != "vfat" ]; then
        mkdir -p "$target_path/{{ DOCKERFS_DIR }}"

        ## extract docker archive
        tar xf "$target_path/{{ FILESYSTEM_DOCKERFS }}" -C "$target_path/{{ DOCKERFS_DIR }}"

        ## clean up docker archive
        rm -f "$target_path/{{ FILESYSTEM_DOCKERFS }}"
    else
        echo "/mnt/flash is $rootfs_type, extract {{ FILESYSTEM_DOCKERFS }} in later stage"
    fi

    ## replace with boot swi
    mv "$target_path/{{ ABOOT_BOOT_IMAGE }}" "$swipath"

    ## sync disk operations
    sync
}

write_machine_config() {
    ## Detect SKU and create a hardware description file
    aboot_version=$(grep ^Aboot /etc/cmdline | sed 's/^.*norcal.-//')
    aboot_build_date=$(stat -c %y /bin/sysinit | sed 's/ /T/')
    cat <<EOF > ${target_path}/machine.conf
aboot_version=$aboot_version
aboot_vendor=arista
aboot_platform=x86_64-$aboot_machine
aboot_machine=$aboot_machine
aboot_arch=x86_64
aboot_build_date=$aboot_build_date
EOF
}

platform_specific() {
    local platform="$(grep -Eo 'platform=[^ ]+' /etc/cmdline | cut -f2 -d=)"
    local sid="$(grep -Eo 'sid=[^ ]+' /etc/cmdline | cut -f2 -d=)"
    # This is temporary as the platform= and sid= parameters don't provide enough
    # information to identify the SKU
    # An initramfs hook or a later processing done by the initscripts will be
    # required to read the system eeprom
    if [ "$platform" = "raven" ]; then
        aboot_machine=arista_7050_qx32
        echo "modprobe.blacklist=radeon" >>/tmp/append
        # set varlog size to 100MB
        echo "varlog_size=100" >>/tmp/append
    fi
    if [ "$platform" = "crow" ]; then
        aboot_machine=arista_7050_qx32s
        echo "modprobe.blacklist=radeon" >>/tmp/append
    fi
    if [ "$sid" = "Upperlake" ]; then
        aboot_machine=arista_7060_cx32s
        echo "amd_iommu=off" >> /tmp/append
    fi
}

echo "$append" >/tmp/append
parse_environment_config >>/tmp/append
cat /etc/cmdline | sed "/^\(${bootconfigvars// /\|}\|crashkernel\|loglevel\|ignore_loglevel\)\(\$\|=\)/d;/^\$/d" >>/tmp/append

echo "rw loop=fs.squashfs loopfstype=squashfs apparmor=1 security=apparmor quiet" >>/tmp/append

# process platform specific operations
platform_specific

# use extra parameters from kernel-params hook if the file exists
if [ -f "$target_path/$kernel_params" ]; then
    cat "$target_path/$kernel_params" >>/tmp/append
fi

# setting root partition if not overridden by kernel-params
if ! grep -q "root=" /tmp/append; then
   rootdev=$(mount | grep '/mnt/flash' | cut -f1 -d' ')
   rootfstype=$(mount | grep '/mnt/flash' | cut -f5 -d' ')
   echo "root=$rootdev" >>/tmp/append
fi

# check the hash file in the image, and determine to install or just skip
GIT_REVISION=$(unzip -p "$swipath" .imagehash)
LOCAL_IMAGEHASH=$(cat $target_path/.imagehash 2>/dev/null || true)
if [ "$GIT_REVISION" != "$LOCAL_IMAGEHASH" ]; then
    extract_image
    write_machine_config
fi

# chainloading using kexec
initrd_path="$target_path/$initrd"
kernel_path="$target_path/$kernel"
cmdline="$(tr '\n' ' ' </tmp/append)"

kexec --load --initrd="$initrd_path" --append="$cmdline" "$kernel_path"
[ -z "$testonly" ] || exit 0
kexec --exec