2021-09-16 06:42:05 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
#
|
|
|
|
|
# Kali Linux ARM build-script for Gateworks Newport (64-bit) - Cavium Octeon
|
|
|
|
|
# https://gitlab.com/kalilinux/build-scripts/kali-arm
|
|
|
|
|
#
|
|
|
|
|
# This is a community script - you will need to generate your own image to use
|
|
|
|
|
# More information: https://www.kali.org/docs/arm/gateworks-newport/
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
# Stop on error
|
|
|
|
|
set -e
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-08-30 15:11:12 -05:00
|
|
|
# shellcheck disable=SC2154
|
|
|
|
|
# Load general functions
|
|
|
|
|
# shellcheck source=/dev/null
|
|
|
|
|
source ./common.d/functions.sh
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-08-30 15:11:12 -05:00
|
|
|
# Hardware model
|
|
|
|
|
hw_model=${hw_model:-"gateworks-newport"}
|
2021-06-03 10:04:19 -04:00
|
|
|
# Architecture
|
|
|
|
|
architecture=${architecture:-"arm64"}
|
2021-08-30 15:11:12 -05:00
|
|
|
# Variant name for image and dir build
|
|
|
|
|
variant=${variant:-"${architecture}"}
|
|
|
|
|
# Desktop manager (xfce, gnome, i3, kde, lxde, mate, e17 or none)
|
|
|
|
|
desktop=${desktop:-"xfce"}
|
|
|
|
|
|
|
|
|
|
# Load common variables
|
|
|
|
|
include variables
|
2021-09-16 06:58:38 +01:00
|
|
|
# Checks script environment
|
2021-08-30 15:11:12 -05:00
|
|
|
include check
|
|
|
|
|
# Packages build list
|
|
|
|
|
include packages
|
|
|
|
|
# Execute initial debootstrap
|
|
|
|
|
debootstrap_exec http://http.kali.org/kali
|
|
|
|
|
# Enable eatmydata in compilation
|
|
|
|
|
include eatmydata
|
2021-06-03 10:04:19 -04:00
|
|
|
# debootstrap second stage
|
|
|
|
|
systemd-nspawn_exec eatmydata /debootstrap/debootstrap --second-stage
|
2021-08-30 15:11:12 -05:00
|
|
|
# Define sources.list
|
|
|
|
|
include sources.list
|
|
|
|
|
# APT options
|
|
|
|
|
include apt_options
|
2021-06-03 10:04:19 -04:00
|
|
|
# So X doesn't complain, we add kali to hosts
|
2021-08-30 15:11:12 -05:00
|
|
|
include hosts
|
|
|
|
|
# Set hostname
|
|
|
|
|
set_hostname "${hostname}"
|
|
|
|
|
# Network configs
|
|
|
|
|
include network
|
|
|
|
|
add_interface eth0
|
2021-09-17 16:02:12 +01:00
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# Copy directory bsp into build dir
|
2021-09-19 10:56:45 +01:00
|
|
|
status "Copy directory bsp into build dir"
|
2021-08-30 15:11:12 -05:00
|
|
|
cp -rp bsp "${work_dir}"
|
2021-06-03 10:04:19 -04:00
|
|
|
|
|
|
|
|
# Third stage
|
2021-09-19 20:03:59 +01:00
|
|
|
cat <<EOF > "${work_dir}"/third-stage
|
2021-09-17 16:02:12 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
set -e
|
2021-09-19 10:56:45 +01:00
|
|
|
status_3i=0
|
2021-09-19 11:34:26 +01:00
|
|
|
status_3t=\$(grep '^status_stage3 ' \$0 | wc -l)
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3() {
|
|
|
|
|
status_3i=\$((status_3i+1))
|
|
|
|
|
echo " [i] Stage 3 (\${status_3i}/\${status_3t}): \$1"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status_stage3 'Update apt'
|
2021-08-30 15:11:12 -05:00
|
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
|
|
|
eatmydata apt-get update
|
2021-09-19 10:56:45 +01:00
|
|
|
|
|
|
|
|
status_stage3 'Install core packages'
|
2021-08-30 15:11:12 -05:00
|
|
|
eatmydata apt-get -y install ${third_stage_pkgs}
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Install packages'
|
2021-08-30 15:11:12 -05:00
|
|
|
eatmydata apt-get install -y ${packages} || eatmydata apt-get install -y --fix-broken
|
2021-09-19 10:56:45 +01:00
|
|
|
|
|
|
|
|
status_stage3 'Install desktop packages'
|
2021-08-30 15:11:12 -05:00
|
|
|
eatmydata apt-get install -y ${desktop_pkgs} ${extra} || eatmydata apt-get install -y --fix-broken
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Clean up'
|
2021-08-30 15:11:12 -05:00
|
|
|
eatmydata apt-get -y --purge autoremove
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Linux console/keyboard configuration'
|
2021-08-30 15:11:12 -05:00
|
|
|
echo 'console-common console-data/keymap/policy select Select keymap from full list' | debconf-set-selections
|
|
|
|
|
echo 'console-common console-data/keymap/full select en-latin1-nodeadkeys' | debconf-set-selections
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Copy all services'
|
2021-08-30 15:11:12 -05:00
|
|
|
cp -p /bsp/services/all/*.service /etc/systemd/system/
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Copy script rpi-resizerootfs'
|
2021-06-03 10:04:19 -04:00
|
|
|
install -m755 /bsp/scripts/rpi-resizerootfs /usr/sbin/
|
2021-08-30 15:11:12 -05:00
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Enable rpi-resizerootfs first boot'
|
2021-06-03 10:04:19 -04:00
|
|
|
systemctl enable rpi-resizerootfs
|
|
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Generate SSH host keys on first run'
|
2021-06-03 10:04:19 -04:00
|
|
|
systemctl enable regenerate_ssh_host_keys
|
|
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Allow users to use NetworkManager over ssh'
|
2021-06-03 10:04:19 -04:00
|
|
|
install -m644 /bsp/polkit/10-NetworkManager.pkla /var/lib/polkit-1/localauthority/50-local.d
|
|
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Install ca-certificate'
|
2021-06-03 10:04:19 -04:00
|
|
|
cd /root
|
|
|
|
|
apt download -o APT::Sandbox::User=root ca-certificates 2>/dev/null
|
|
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Set a REGDOMAIN'
|
2021-08-30 15:11:12 -05:00
|
|
|
sed -i -e 's/REGDOM.*/REGDOMAIN=00/g' /etc/default/crda
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-09-20 06:22:23 -05:00
|
|
|
status_stage3 'We replace the u-boot menu defaults here so we can make sure the build system does not poison it'
|
2021-09-16 06:58:38 +01:00
|
|
|
# We use _EOF_ so that the third-stage script doesn't end prematurely
|
2021-06-03 10:04:19 -04:00
|
|
|
cat << '_EOF_' > /etc/default/u-boot
|
|
|
|
|
U_BOOT_PARAMETERS="console=ttyS0,115200 console=tty1 root=/dev/mmcblk0p1 rootwait panic=10 rw rootfstype=$fstype net.ifnames=0"
|
|
|
|
|
_EOF_
|
|
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Enable login over serial'
|
2021-08-30 15:11:12 -05:00
|
|
|
echo "T1:12345:respawn:/sbin/getty -L ttymxc1 115200 vt100" >> /etc/inittab
|
|
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Try and make the console a bit nicer. Set the terminus font for a bit nicer display'
|
2021-06-03 10:04:19 -04:00
|
|
|
sed -i -e 's/FONTFACE=.*/FONTFACE="Terminus"/' /etc/default/console-setup
|
|
|
|
|
sed -i -e 's/FONTSIZE=.*/FONTSIZE="6x12"/' /etc/default/console-setup
|
|
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Fix startup time from 5 minutes to 15 secs on raise interface wlan0'
|
2021-08-30 15:11:12 -05:00
|
|
|
sed -i 's/^TimeoutStartSec=5min/TimeoutStartSec=15/g' "/usr/lib/systemd/system/networking.service"
|
|
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Enable runonce'
|
2021-08-30 15:11:12 -05:00
|
|
|
install -m755 /bsp/scripts/runonce /usr/sbin/
|
|
|
|
|
cp -rf /bsp/runonce.d /etc
|
|
|
|
|
systemctl enable runonce
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status_stage3 'Clean up dpkg.eatmydata'
|
2021-06-03 10:04:19 -04:00
|
|
|
rm -f /usr/bin/dpkg
|
|
|
|
|
dpkg-divert --remove --rename /usr/bin/dpkg
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
# Run third stage
|
2021-09-17 10:42:05 +01:00
|
|
|
chmod 0755 "${work_dir}"/third-stage
|
2021-09-19 10:56:45 +01:00
|
|
|
status "Run third stage"
|
2021-06-03 10:04:19 -04:00
|
|
|
systemd-nspawn_exec /third-stage
|
|
|
|
|
|
|
|
|
|
# Clean system
|
2021-08-30 15:11:12 -05:00
|
|
|
include clean_system
|
2021-09-17 16:47:51 +01:00
|
|
|
trap clean_build ERR SIGTERM SIGINT
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-09-19 10:11:39 +01:00
|
|
|
cd "${base_dir}/"
|
2021-09-17 16:02:12 +01:00
|
|
|
|
|
|
|
|
# Do the kernel stuff
|
2021-09-19 10:56:45 +01:00
|
|
|
status "Kernel stuff"
|
2021-06-03 10:04:19 -04:00
|
|
|
git clone --depth 1 -b v5.4.45-newport https://github.com/gateworks/linux-newport ${work_dir}/usr/src/kernel
|
|
|
|
|
cd ${work_dir}/usr/src/kernel
|
2021-09-16 06:58:38 +01:00
|
|
|
# Don't change the version because of our patches
|
2021-06-03 10:04:19 -04:00
|
|
|
touch .scmversion
|
|
|
|
|
export ARCH=arm64
|
2021-09-18 11:50:14 +02:00
|
|
|
export CROSS_COMPILE=aarch64-linux-gnu-
|
2021-06-03 10:04:19 -04:00
|
|
|
patch -p1 < ${current_dir}/patches/kali-wifi-injection-5.4.patch
|
|
|
|
|
patch -p1 < ${current_dir}/patches/0001-wireless-carl9170-Enable-sniffer-mode-promisc-flag-t.patch
|
|
|
|
|
cp ${current_dir}/kernel-configs/gateworks-newport-5.4.45.config .config
|
|
|
|
|
cp ${current_dir}/kernel-configs/gateworks-newport-5.4.45.config ${work_dir}/usr/src/gateworks-newport-5.4.45.config
|
|
|
|
|
#build
|
|
|
|
|
make -j $(grep -c processor /proc/cpuinfo)
|
|
|
|
|
# install compressed kernel in a kernel.itb
|
|
|
|
|
mkimage -f auto -A arm64 -O linux -T kernel -C gzip -n "Newport Kali Kernel" -a 20080000 -e 20080000 -d arch/arm64/boot/Image.gz kernel.itb
|
|
|
|
|
cp kernel.itb ${work_dir}/boot
|
|
|
|
|
# install kernel modules
|
|
|
|
|
make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=${work_dir} modules_install
|
|
|
|
|
make INSTALL_HDR_PATH=${work_dir}/usr headers_install
|
|
|
|
|
# cryptodev-linux build/install
|
|
|
|
|
git clone --depth 1 https://github.com/cryptodev-linux/cryptodev-linux ${work_dir}/usr/src/cryptodev-linux
|
|
|
|
|
cd ${work_dir}/usr/src
|
|
|
|
|
make -C cryptodev-linux KERNEL_DIR=${work_dir}/usr/src/kernel
|
|
|
|
|
make -C cryptodev-linux KERNEL_DIR=${work_dir}/usr/src/kernel DESTDIR=${work_dir} INSTALL_MOD_PATH=${work_dir} install
|
|
|
|
|
# wireguard-linux-compat build/install
|
|
|
|
|
git clone --depth 1 https://git.zx2c4.com/wireguard-linux-compat ${work_dir}/usr/src/wireguard-linux-compat
|
|
|
|
|
make -C ${work_dir}/usr/src/kernel M=../wireguard-linux-compat/src modules
|
|
|
|
|
make -C ${work_dir}/usr/src/kernel M=../wireguard-linux-compat/src INSTALL_MOD_PATH=${work_dir} modules_install
|
|
|
|
|
# cleanup
|
|
|
|
|
cd ${work_dir}/usr/src/kernel
|
|
|
|
|
make mrproper
|
|
|
|
|
|
|
|
|
|
# U-boot script
|
2021-09-19 10:56:45 +01:00
|
|
|
status "U-boot script"
|
2021-06-05 14:16:17 +00:00
|
|
|
install -m644 ${current_dir}/bsp/bootloader/gateworks-newport/newport.scr ${work_dir}/boot/newport.script
|
|
|
|
|
mkimage -A arm64 -T script -C none -d ${work_dir}/boot/newport.script ${work_dir}/boot/newport.scr
|
|
|
|
|
rm ${work_dir}/boot/newport.script
|
2021-06-03 10:04:19 -04:00
|
|
|
|
|
|
|
|
# reboot script
|
2021-09-19 10:56:45 +01:00
|
|
|
status "Reboot script"
|
2021-06-03 10:04:19 -04:00
|
|
|
cat << EOF > ${work_dir}/lib/systemd/system-shutdown/gsc-poweroff
|
2021-09-19 10:56:45 +01:00
|
|
|
#!/usr/bin/env bash
|
2021-06-03 10:04:19 -04:00
|
|
|
# use GSC to power cycle the system
|
|
|
|
|
echo 2 > /sys/bus/i2c/devices/0-0020/powerdown
|
|
|
|
|
done
|
|
|
|
|
EOF
|
|
|
|
|
chmod +x ${work_dir}/lib/systemd/system-shutdown/gsc-poweroff
|
|
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# Calculate the space to create the image
|
2021-06-03 10:04:19 -04:00
|
|
|
root_size=$(du -s -B1 ${work_dir} --exclude=${work_dir}/boot | cut -f1)
|
|
|
|
|
root_extra=$((${root_size}/1024/1000*5*1024/5))
|
|
|
|
|
raw_size=$(($((${free_space}*1024))+${root_extra}))
|
|
|
|
|
|
|
|
|
|
# Weird Boot Partition
|
2021-09-19 10:56:45 +01:00
|
|
|
status "Creating image file ${image_name}.img"
|
2021-09-20 06:14:13 -05:00
|
|
|
mkdir -p "${image_dir}"
|
|
|
|
|
wget http://dev.gateworks.com/newport/boot_firmware/firmware-newport.img -O "${base_dir}/${image_name}.img"
|
2021-09-19 13:45:29 +01:00
|
|
|
fallocate -l $(echo ${raw_size}Ki | numfmt --from=iec-i --to=si) "${image_dir}/${image_name}.img"
|
2021-09-20 06:14:13 -05:00
|
|
|
dd if=${base_dir}/${image_name}.img of="${image_dir}/${image_name}.img" bs=16M seek=1
|
2021-09-19 13:45:29 +01:00
|
|
|
echo ", +" | sfdisk -N 2 "${image_dir}/${image_name}.img"
|
2021-06-03 10:04:19 -04:00
|
|
|
|
|
|
|
|
# Set the partition variables
|
2021-09-19 13:45:29 +01:00
|
|
|
loopdevice=$(losetup -f --show "${image_dir}/${image_name}.img")
|
2021-09-17 16:02:12 +01:00
|
|
|
device=$(kpartx -va ${loopdevice} | sed 's/.*\(loop[0-9]\+\)p.*/\1/g' | head -1)
|
2021-06-03 10:04:19 -04:00
|
|
|
sleep 5
|
|
|
|
|
device="/dev/mapper/${device}"
|
|
|
|
|
rootp=${device}p2
|
|
|
|
|
|
|
|
|
|
# Create file systems
|
|
|
|
|
if [[ $fstype == ext4 ]]; then
|
|
|
|
|
features="-O ^64bit,^metadata_csum"
|
|
|
|
|
elif [[ $fstype == ext3 ]]; then
|
|
|
|
|
features="-O ^64bit"
|
|
|
|
|
fi
|
2021-09-17 16:02:12 +01:00
|
|
|
mkfs -O "$features" -t "$fstype" -L ROOTFS "${rootp}"
|
2021-06-03 10:04:19 -04:00
|
|
|
|
|
|
|
|
# Create the dirs for the partitions and mount them
|
2021-09-19 10:56:45 +01:00
|
|
|
status "Create the dirs for the partitions and mount them"
|
2021-09-19 10:11:39 +01:00
|
|
|
mkdir -p "${base_dir}"/root
|
|
|
|
|
mount ${rootp} "${base_dir}"/root
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# Create an fstab so that we don't mount / read-only
|
2021-09-19 10:56:45 +01:00
|
|
|
status "/etc/fstab"
|
2021-06-03 10:04:19 -04:00
|
|
|
UUID=$(blkid -s UUID -o value ${rootp})
|
|
|
|
|
echo "UUID=$UUID / $fstype errors=remount-ro 0 1" >> ${work_dir}/etc/fstab
|
|
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
status "Rsyncing rootfs into image file"
|
2021-09-19 10:11:39 +01:00
|
|
|
rsync -HPavz -q ${work_dir}/ ${base_dir}/root/
|
2021-06-03 10:04:19 -04:00
|
|
|
sync
|
2021-09-19 10:56:45 +01:00
|
|
|
# Flush buffers and bytes - this is nicked from the Devuan arm-sdk
|
2021-09-17 09:46:40 -05:00
|
|
|
blockdev --flushbufs "${loopdevice}"
|
|
|
|
|
python -c 'import os; os.fsync(open("'${loopdevice}'", "r+b"))'
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
# Unmount filesystem
|
|
|
|
|
status "Unmount filesystem"
|
|
|
|
|
umount -l "${rootp}"
|
2021-09-17 16:02:12 +01:00
|
|
|
|
|
|
|
|
# Remove loop devices
|
2021-09-19 10:56:45 +01:00
|
|
|
status "Remove loop devices"
|
|
|
|
|
kpartx -dv "${loopdevice}"
|
|
|
|
|
losetup -d "${loopdevice}"
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-09-19 10:56:45 +01:00
|
|
|
# Compress image compilation
|
|
|
|
|
include compress_img
|
2021-06-03 10:04:19 -04:00
|
|
|
|
2021-09-16 06:58:38 +01:00
|
|
|
# Clean up all the temporary build stuff and remove the directories
|
|
|
|
|
# Comment this out to keep things around if you want to see what may have gone wrong
|
2021-09-19 10:56:45 +01:00
|
|
|
clean_build
|