Skip to content
Snippets Groups Projects
freedombone-image-hardware-setup 9.34 KiB
Newer Older
Bob Mottram's avatar
Bob Mottram committed
#!/bin/sh
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
Bob Mottram's avatar
Bob Mottram committed
#
# Hardware setup based on bin/freedombox-hardware-setup from freedom-maker
#
Bob Mottram's avatar
Bob Mottram committed
# Booting beaglebones from a USB drive does not work at present, due to
# missing kernel config. See:
# http://processors.wiki.ti.com/index.php/MUSB_Linux_Driver_Configuration
#
Bob Mottram's avatar
Bob Mottram committed
# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2015-2019 Bob Mottram <bob@freedombone.net>
#
Bob Mottram's avatar
Bob Mottram committed
# This program is free software: you can redistribute it and/or modify
Bob Mottram's avatar
Bob Mottram committed
# it under the terms of the GNU Affero General Public License as published by
Bob Mottram's avatar
Bob Mottram committed
# 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
Bob Mottram's avatar
Bob Mottram committed
# GNU Affero General Public License for more details.
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
# You should have received a copy of the GNU Affero General Public License
Bob Mottram's avatar
Bob Mottram committed
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
PROJECT_NAME='freedombone'

Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAIN=${PROJECT_NAME}-image-hardware-setup
Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAINDIR="/usr/share/locale"

INSTALL_PACKAGES='apt-get -yq install'
INSTALL_PACKAGES_BACKPORTS='apt-get -yq -t stretch-backports install'
Bob Mottram's avatar
Bob Mottram committed
convert_dts() {
    dts="$1"
    $INSTALL_PACKAGES device-tree-compiler
    dtc -I dts -O dtb "${dts}.dts" > "${dts}.dtb"
}

Bob Mottram's avatar
Bob Mottram committed
enable_serial_console() {
Bob Mottram's avatar
Bob Mottram committed
    # By default, spawn a console on the serial port
    device="$1"
Bob Mottram's avatar
Bob Mottram committed
    echo "Adding a getty on the serial port"
Bob Mottram's avatar
Bob Mottram committed
    echo "T0:12345:respawn:/sbin/getty -L $device 115200 vt100" >> /etc/inittab
}

Bob Mottram's avatar
Bob Mottram committed
arm_flash() {
    # allow flash-kernel to work without valid /proc contents
    # ** this doesn't *really* work, since there are too many checks
    #    that fail in an emulated environment!  We'll have to do it by
    #    hand below anyway...
    export FK_MACHINE="$1"
    if ! $INSTALL_PACKAGES flash-kernel; then
        echo "Unable to flash kernel for $FK_MACHINE"
Bob Mottram's avatar
Bob Mottram committed
}

Bob Mottram's avatar
Bob Mottram committed
arm_repack_kernel() {
    dtb="$1"
Bob Mottram's avatar
Bob Mottram committed
    arm_loadaddr="$2"
    arm_initrd_addr="$3"
Bob Mottram's avatar
Bob Mottram committed
    arm_arch="$4"
    # process installed kernel to create uImage, uInitrd, dtb
    # using flash-kernel would be a good approach, except it fails in the
    # cross build environment due to too many environment checks...
    #FK_MACHINE="TI AM335x BeagleBone" flash-kernel
    #  so, let's do it manually...

    # flash-kernel's hook-functions provided to mkinitramfs have the
    # unfortunate side-effect of creating /conf/param.conf in the initrd
    # when run from our emulated chroot environment, which means our root=
    # on the kernel command line is completely ignored!  repack the initrd
    # to remove this evil...

Bob Mottram's avatar
Bob Mottram committed
    echo "info: repacking $dtb kernel and initrd"
Bob Mottram's avatar
Bob Mottram committed

    # shellcheck disable=SC2012,SC2086
Bob Mottram's avatar
Bob Mottram committed
    kernelVersion=$(ls /usr/lib/*/${dtb}.dtb | head -1 | cut -d/ -f4)
Bob Mottram's avatar
Bob Mottram committed
    version=$(echo "$kernelVersion" | sed 's/linux-image-\(.*\)/\1/')
    initRd=initrd.img-$version
    vmlinuz=vmlinuz-$version

    # optionally use a separately compiled kernel
Bob Mottram's avatar
Bob Mottram committed
    dtb_file=/usr/lib/$kernelVersion/${dtb}.dtb
    mkdir /tmp/initrd-repack

    (cd /tmp/initrd-repack || exit 24 ; \
Bob Mottram's avatar
Bob Mottram committed
     zcat "/boot/$initRd" | cpio -i ; \
     rm -f conf/param.conf ; \
     find . | cpio --quiet -o -H newc | \
Bob Mottram's avatar
Bob Mottram committed
         gzip -9 > "/boot/$initRd" )

    rm -rf /tmp/initrd-repack

    (cd /boot || exit 24 ; \
Bob Mottram's avatar
Bob Mottram committed
     cp "${dtb_file}" dtb ; \
Bob Mottram's avatar
Bob Mottram committed
     cat "$vmlinuz" dtb >> temp-kernel ; \
Bob Mottram's avatar
Bob Mottram committed
     mkimage -A "${arm_arch}" -O linux -T kernel -n "Debian kernel ${version}" \
Bob Mottram's avatar
Bob Mottram committed
             -C none -a "${arm_loadaddr}" -e "${arm_loadaddr}" -d temp-kernel uImage ; \
     rm -f temp-kernel ; \
Bob Mottram's avatar
Bob Mottram committed
     mkimage -A "${arm_arch}" -O linux -T ramdisk -C gzip -a "${arm_initrd_addr}" -e "${arm_initrd_addr}" \
             -n "Debian ramdisk ${version}" \
Bob Mottram's avatar
Bob Mottram committed
             -d "$initRd" uInitrd )
Bob Mottram's avatar
Bob Mottram committed
setup_flash_kernel() {
    if [ ! -d /etc/flash-kernel ] ; then
       mkdir /etc/flash-kernel
    fi
Bob Mottram's avatar
Bob Mottram committed
    printf "%s" "$1" > /etc/flash-kernel/machine
Bob Mottram's avatar
Bob Mottram committed

    command_line=""
    if [ -n "$2" ] ; then
        command_line="console=$2"
    fi

    if [ -n "$command_line" ] ; then
Bob Mottram's avatar
Bob Mottram committed
        echo "flash-kernel flash-kernel/linux_cmdline string \"$command_line\"" | debconf-set-selections
    $INSTALL_PACKAGES flash-kernel
arm_setup_boot() {
    dtb="$1"
    arm_root_device='mmcblk0p2'
    if [ "$EXTERNAL_DRIVE" ]; then
        arm_root_device="$EXTERNAL_DRIVE"
    fi

    arm_flash_name=
    if [ "$2" ]; then
        arm_flash_name="$2"
    fi

    # Setup boot.cmd
    if grep -q btrfs /etc/fstab ; then
        fstype=btrfs
    else
        fstype=ext4
    fi

Bob Mottram's avatar
Bob Mottram committed
    mkdir -p /boot/dtbs

    # shellcheck disable=SC2012,SC2086
    kernelVersion=$(ls /usr/lib/*/${dtb}.dtb | head -1 | cut -d/ -f4)
    version=$(echo "$kernelVersion" | sed 's/linux-image-\(.*\)/\1/')
    initRd=initrd.img-$version
    vmlinuz=vmlinuz-$version

    arm_loadaddr='0x46000000'
    arm_initrd_addr='0x48000000'
    arm_fdtaddr='0x47000000'
Bob Mottram's avatar
Bob Mottram committed
    arm_boot_device="mmc \${mmcdev}:\${mmcpart}"
Bob Mottram's avatar
Bob Mottram committed
    arm_boot_start=
Bob Mottram's avatar
Bob Mottram committed
    arm_boot_console='ttyO0,115200n8'
Bob Mottram's avatar
Bob Mottram committed
    arm_arch=arm
Bob Mottram's avatar
Bob Mottram committed
    arm_uenv=
    case "$dtb" in
        "am"*) arm_loadaddr='0x82000000'
               arm_initrd_addr='0x88080000'
               arm_fdtaddr='0x88000000'
Bob Mottram's avatar
Bob Mottram committed
               arm_uenv=1
Bob Mottram's avatar
Bob Mottram committed
               if [ "$EXTERNAL_DRIVE" ]; then
Bob Mottram's avatar
Bob Mottram committed
                   arm_boot_device="usb 0:1"
Bob Mottram's avatar
Bob Mottram committed
                   arm_loadaddr='0x80300000'
                   arm_initrd_addr='0x81600000'
                   arm_fdtaddr='0x815f0000'
                   arm_boot_start='usb start; '
               fi
        "sun"*) arm_boot_device="mmc 0:1"
Bob Mottram's avatar
Bob Mottram committed
                { echo "rtc_sunxi";
Bob Mottram's avatar
Bob Mottram committed
                  echo "vfat"; } >> /etc/initramfs-tools/modules
                ;;
        "rk3328-rock64") arm_arch=arm64
                         cp /root/${PROJECT_NAME}/image_build/dts/rk3328-rock64.dtb /boot/dtbs
Bob Mottram's avatar
Bob Mottram committed
                         arm_boot_device="usb 0:1"
                         arm_loadaddr='0x80300000'
                         arm_initrd_addr='0x81600000'
                         arm_fdtaddr='0x815f0000'
                         arm_boot_start='usb start; '
                         echo "vfat" >> /etc/initramfs-tools/modules
                         ;;
Bob Mottram's avatar
Bob Mottram committed
    if [ $arm_uenv ]; then
Bob Mottram's avatar
Bob Mottram committed
        cat >> /boot/uEnv.txt <<EOF
Bob Mottram's avatar
Bob Mottram committed
mmcroot=/dev/${arm_root_device} ro
mmcrootfstype=$fstype rootwait fixrtc
mmcrootflags=subvol=@

Bob Mottram's avatar
Bob Mottram committed
console=${arm_boot_console}
Bob Mottram's avatar
Bob Mottram committed

kernel_file=$vmlinuz
initrd_file=$initRd

loadaddr=$arm_loadaddr
initrd_addr=$arm_initrd_addr
fdtaddr=$arm_fdtaddr

initrd_high=0xffffffff
fdt_high=0xffffffff
Bob Mottram's avatar
Bob Mottram committed
loadkernel=load ${arm_boot_device} \${loadaddr} \${kernel_file}
loadinitrd=load ${arm_boot_device} \${initrd_addr} \${initrd_file}; setenv initrd_size \${filesize}
loadfdt=load ${arm_boot_device} \${fdtaddr} /dtbs/\${fdtfile}
Bob Mottram's avatar
Bob Mottram committed

loadfiles=run loadkernel; run loadinitrd; run loadfdt
mmcargs=setenv bootargs init=/lib/systemd/systemd console=tty0 console=\${console} root=\${mmcroot} rootfstype=\${mmcrootfstype} rootflags=\${mmcrootflags} ifnames=0 slub_debug=FZP slab_nomerge page_poison=1

Bob Mottram's avatar
Bob Mottram committed
uenvcmd=${arm_boot_start}run loadfiles; run mmcargs; bootz \${loadaddr} \${initrd_addr}:\${initrd_size} \${fdtaddr}
Bob Mottram's avatar
Bob Mottram committed
        # Copy all DTBs
        cp /usr/lib/linux-image-*-armmp/* /boot/dtbs
    else
        cat >> /boot/boot.cmd <<EOF
scsi scan
Bob Mottram's avatar
Bob Mottram committed
setenv bootargs init=/lib/systemd/systemd console=${arm_boot_console} root=/dev/${arm_root_device} panic=10 rootflags=subvol=@ ro ${fstype} rootwait fixrtc earlyprintk ifnames=0 slub_debug=FZP slab_nomerge page_poison=1
Bob Mottram's avatar
Bob Mottram committed
fatload ${arm_boot_device} ${arm_fdtaddr} /dtbs/${dtb}.dtb
fatload ${arm_boot_device} ${arm_loadaddr} ${vmlinuz}
fatload ${arm_boot_device} ${arm_initrd_addr} ${initRd}
bootz ${arm_loadaddr} ${arm_initrd_addr}:\${filesize} ${arm_fdtaddr}
Bob Mottram's avatar
Bob Mottram committed
        # Create boot.scr
        mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr

        # shellcheck disable=SC2086
        cp /usr/lib/$kernelVersion/* /boot/dtbs
    if [ $arm_repack ]; then
        arm_flash "$arm_flash_name"
        arm_repack_kernel "${dtb}" "$arm_loadaddr" "$arm_initrd_addr" "$arm_arch"
    fi

    enable_serial_console ttyS0
Bob Mottram's avatar
Bob Mottram committed
case "$MACHINE" in
        arm_setup_boot am335x-boneblack "TI AM335x BeagleBone Black"
        ;;
    beaglebonegreen)
        arm_setup_boot am335x-bonegreen "TI AM335x BeagleBone Green"
        arm_setup_boot am57xx-beagle-x15 "TI AM5728 BeagleBoard-X15"
Bob Mottram's avatar
Bob Mottram committed
    cubietruck)
Bob Mottram's avatar
Bob Mottram committed
        arm_setup_boot sun7i-a20-cubietruck
Bob Mottram's avatar
Bob Mottram committed
        ;;
    a20-olinuxino-lime)
Bob Mottram's avatar
Bob Mottram committed
        arm_setup_boot sun7i-a20-olinuxino-lime
Bob Mottram's avatar
Bob Mottram committed
        ;;
    a20-olinuxino-lime2)
Bob Mottram's avatar
Bob Mottram committed
        arm_setup_boot sun7i-a20-olinuxino-lime2
Bob Mottram's avatar
Bob Mottram committed
        ;;
    a20-olinuxino-micro)
Bob Mottram's avatar
Bob Mottram committed
        arm_setup_boot sun7i-a20-olinuxino-micro
Bob Mottram's avatar
Bob Mottram committed
        ;;
    banana-pro)
Bob Mottram's avatar
Bob Mottram committed
        arm_setup_boot sun7i-a20-bananapro
Bob Mottram's avatar
Bob Mottram committed
    cubieboard2)
Bob Mottram's avatar
Bob Mottram committed
        arm_setup_boot sun7i-a20-cubieboard2
Bob Mottram's avatar
Bob Mottram committed
        ;;
Bob Mottram's avatar
Bob Mottram committed
    pcduino3)
Bob Mottram's avatar
Bob Mottram committed
        arm_setup_boot sun7i-a20-pcduino3
        if [ -f /root/${PROJECT_NAME}/image_build/dts/rk3328-rock64.dts ]; then
            convert_dts /root/${PROJECT_NAME}/image_build/dts/rk3328-rock64
            arm_setup_boot "rk3328-rock64"
        fi
Bob Mottram's avatar
Bob Mottram committed
esac