Files
kali-arm/nanopc-t3.sh

276 lines
11 KiB
Bash
Raw Normal View History

2021-08-30 15:10:20 -05:00
#!/bin/bash -e
# This is the NanoPC-T3/T4 Kali ARM 64 bit build script - http://www.kali.org/get-kali
2017-09-20 13:23:36 -05:00
# A trusted Kali Linux image created by Offensive Security - http://www.offensive-security.com
2021-08-30 15:10:20 -05:00
# shellcheck disable=SC2154
# Load general functions
# shellcheck source=/dev/null
source ./common.d/functions.sh
2017-09-20 13:23:36 -05:00
2021-08-30 15:10:20 -05:00
# Hardware model
hw_model=${hw_model:-"nanopi3"}
2020-07-21 00:44:37 -05:00
# Architecture
architecture=${architecture:-"arm64"}
2021-08-30 15:10:20 -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
# Checks script enviroment
include check
# Packages build list
include packages
# Load automatic proxy configuration
include proxy_apt
# Execute initial debootstrap
debootstrap_exec http://http.kali.org/kali
# Enable eatmydata in compilation
include eatmydata
2020-07-21 00:44:37 -05:00
# debootstrap second stage
2020-08-02 00:14:21 -05:00
systemd-nspawn_exec eatmydata /debootstrap/debootstrap --second-stage
2021-08-30 15:10:20 -05:00
# Define sources.list
include sources.list
# APT options
include apt_options
2020-07-21 00:44:37 -05:00
# So X doesn't complain, we add kali to hosts
2021-08-30 15:10:20 -05:00
include hosts
# Set hostname
set_hostname "${hostname}"
# Network configs
include network
add_interface eth0
add_interface wlan0
2020-07-21 00:44:37 -05:00
# Copy directory bsp into build dir.
2021-08-30 15:10:20 -05:00
cp -rp bsp "${work_dir}"
2020-07-21 00:44:37 -05:00
2021-08-30 15:10:20 -05:00
# Disable RESUME (suspend/resume is currently broken anyway!) which speeds up boot massively.
mkdir -p ${work_dir}/etc/initramfs-tools/conf.d/
cat << EOF > ${work_dir}/etc/initramfs-tools/conf.d/resume
RESUME=none
EOF
2020-07-21 00:44:37 -05:00
# Third stage
2021-08-30 15:10:20 -05:00
cat <<EOF >"${work_dir}"/third-stage
2020-07-21 00:44:37 -05:00
#!/bin/bash -e
2017-09-20 13:23:36 -05:00
2021-08-30 15:10:20 -05:00
export DEBIAN_FRONTEND=noninteractive
2020-08-02 00:14:21 -05:00
eatmydata apt-get update
2021-08-30 15:10:20 -05:00
eatmydata apt-get -y install ${third_stage_pkgs}
2017-09-20 13:23:36 -05:00
2021-08-30 15:10:20 -05:00
eatmydata apt-get install -y ${packages} || eatmydata apt-get install -y --fix-broken
eatmydata apt-get install -y ${desktop_pkgs} ${extra} || eatmydata apt-get install -y --fix-broken
2020-07-21 00:44:37 -05:00
2021-08-30 15:10:20 -05:00
eatmydata apt-get -y --purge autoremove
2017-09-20 13:23:36 -05:00
2020-07-21 00:44:37 -05:00
# Linux console/Keyboard configuration
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
2018-06-25 19:07:00 -05:00
2020-07-21 00:44:37 -05:00
# Copy all services
2021-08-30 15:10:20 -05:00
cp -p /bsp/services/all/*.service /etc/systemd/system/
# Copy script rpi-resizerootfs
install -m755 /bsp/scripts/rpi-resizerootfs /usr/sbin/
2020-07-21 00:44:37 -05:00
2021-08-30 15:10:20 -05:00
# Enable rpi-resizerootfs first boot
systemctl enable rpi-resizerootfs
2018-06-25 19:07:00 -05:00
# Generate SSH host keys on first run
systemctl enable regenerate_ssh_host_keys
# Allow users to use NM over ssh
install -m644 /bsp/polkit/10-NetworkManager.pkla /var/lib/polkit-1/localauthority/50-local.d
2020-07-21 00:44:37 -05:00
cd /root
apt download -o APT::Sandbox::User=root ca-certificates 2>/dev/null
# Set a REGDOMAIN. This needs to be done or wireless doesn't work correctly on the RPi 3B+
sed -i -e 's/REGDOM.*/REGDOMAIN=00/g' /etc/default/crda
2021-08-30 15:10:20 -05:00
# Enable login over serial
echo "T0:23:respawn:/sbin/agetty -L ttyAMA0 115200 vt100" >> /etc/inittab
2017-09-20 13:23:36 -05:00
2020-07-21 00:44:37 -05:00
# Try and make the console a bit nicer
# Set the terminus font for a bit nicer display.
sed -i -e 's/FONTFACE=.*/FONTFACE="Terminus"/' /etc/default/console-setup
sed -i -e 's/FONTSIZE=.*/FONTSIZE="6x12"/' /etc/default/console-setup
# Fix startup time from 5 minutes to 15 secs on raise interface wlan0
sed -i 's/^TimeoutStartSec=5min/TimeoutStartSec=15/g' "/usr/lib/systemd/system/networking.service"
2020-08-02 00:14:21 -05:00
2021-08-30 15:10:20 -05:00
# Enable runonce
install -m755 /bsp/scripts/runonce /usr/sbin/
cp -rf /bsp/runonce.d /etc
systemctl enable runonce
# Clean up dpkg.eatmydata
2020-08-02 00:14:21 -05:00
rm -f /usr/bin/dpkg
2021-08-30 15:10:20 -05:00
dpkg-divert --remove --rename /usr/bin/dpkg
2017-09-20 13:23:36 -05:00
EOF
2020-07-21 00:44:37 -05:00
# Run third stage
2021-08-30 15:10:20 -05:00
chmod 755 "${work_dir}"/third-stage
2020-07-21 00:44:37 -05:00
systemd-nspawn_exec /third-stage
2017-09-20 13:23:36 -05:00
2021-08-30 15:10:20 -05:00
# Choose a locale
set_locale "$locale"
2020-07-21 00:44:37 -05:00
# Clean system
2021-08-30 15:10:20 -05:00
include clean_system
# Define DNS server after last running systemd-nspawn.
echo "nameserver 8.8.8.8" >"${work_dir}"/etc/resolv.conf
2020-07-21 00:44:37 -05:00
# Disable the use of http proxy in case it is enabled.
2021-08-30 15:10:20 -05:00
disable_proxy
# Mirror & suite replacement
2021-08-30 15:10:20 -05:00
restore_mirror
# Reload sources.list
include sources.list
2017-09-20 13:23:36 -05:00
# Kernel section. If you want to use a custom kernel, or configuration, replace
# them in this section.
2020-07-21 00:44:37 -05:00
git clone --depth 1 https://github.com/friendlyarm/linux -b nanopi2-v4.4.y ${work_dir}/usr/src/kernel
cd ${work_dir}/usr/src/kernel
git rev-parse HEAD > ${work_dir}/usr/src/kernel-at-commit
2017-09-20 13:23:36 -05:00
touch .scmversion
export ARCH=arm64
#export CROSS_COMPILE="${basedir}"/gcc-arm-linux-gnueabihf-4.7/bin/arm-linux-gnueabihf-
export CROSS_COMPILE=aarch64-linux-gnu-
2020-07-21 00:44:37 -05:00
patch -p1 --no-backup-if-mismatch < ${current_dir}/patches/kali-wifi-injection-4.4.patch
make nanopi3_linux_defconfig
2017-09-20 13:23:36 -05:00
make -j $(grep -c processor /proc/cpuinfo)
2020-07-21 00:44:37 -05:00
make modules_install INSTALL_MOD_PATH=${work_dir}
cp arch/arm64/boot/Image ${work_dir}/boot
cp arch/arm64/boot/dts/nexell/*.dtb ${work_dir}/boot/
2017-09-20 13:23:36 -05:00
make mrproper
make nanopi3_linux_defconfig
2020-07-21 00:44:37 -05:00
cd ${current_dir}
2017-09-20 13:23:36 -05:00
2018-06-25 19:07:00 -05:00
# Copy over the firmware for the nanopi3 wifi.
# At some point, nexmon could work for the device, but the support would need to
# be added to nexmon.
2020-07-21 00:44:37 -05:00
mkdir -p ${work_dir}/lib/firmware/ap6212/
wget https://raw.githubusercontent.com/friendlyarm/android_vendor_broadcom_nanopi2/nanopi2-lollipop-mr1/proprietary/nvram_ap6212.txt -O ${work_dir}/lib/firmware/ap6212/nvram.txt
wget https://raw.githubusercontent.com/friendlyarm/android_vendor_broadcom_nanopi2/nanopi2-lollipop-mr1/proprietary/nvram_ap6212a.txt -O ${work_dir}/lib/firmware/ap6212/nvram_ap6212.txt
wget https://raw.githubusercontent.com/friendlyarm/android_vendor_broadcom_nanopi2/nanopi2-lollipop-mr1/proprietary/fw_bcm43438a0.bin -O ${work_dir}/lib/firmware/ap6212/fw_bcm43438a0.bin
wget https://raw.githubusercontent.com/friendlyarm/android_vendor_broadcom_nanopi2/nanopi2-lollipop-mr1/proprietary/fw_bcm43438a1.bin -O ${work_dir}/lib/firmware/ap6212/fw_bcm43438a1.bin
wget https://raw.githubusercontent.com/friendlyarm/android_vendor_broadcom_nanopi2/nanopi2-lollipop-mr1/proprietary/fw_bcm43438a0_apsta.bin -O ${work_dir}/lib/firmware/ap6212/fw_bcm43438a0_apsta.bin
wget https://raw.githubusercontent.com/friendlyarm/android_vendor_broadcom_nanopi2/nanopi2-lollipop-mr1/proprietary/bcm43438a0.hcd -O ${work_dir}/lib/firmware/ap6212/bcm43438a0.hcd
wget https://raw.githubusercontent.com/friendlyarm/android_vendor_broadcom_nanopi2/nanopi2-lollipop-mr1/proprietary/bcm43438a1.hcd -O ${work_dir}/lib/firmware/ap6212/bcm43438a1.hcd
wget https://raw.githubusercontent.com/friendlyarm/android_vendor_broadcom_nanopi2/nanopi2-lollipop-mr1/proprietary/config_ap6212.txt -O ${work_dir}/lib/firmware/ap6212/config.txt
cd ${current_dir}
2017-09-20 13:23:36 -05:00
# Fix up the symlink for building external modules
# kernver is used so we don't need to keep track of what the current compiled
# version is
2020-07-21 00:44:37 -05:00
kernver=$(ls ${work_dir}/lib/modules/)
cd ${work_dir}/lib/modules/${kernver}
2017-09-20 13:23:36 -05:00
rm build
rm source
ln -s /usr/src/kernel build
ln -s /usr/src/kernel source
2020-07-21 00:44:37 -05:00
cd ${current_dir}
2017-09-20 13:23:36 -05:00
2021-08-30 15:10:20 -05:00
cd ${current_dir}
2020-07-21 00:44:37 -05:00
2021-08-30 15:10:20 -05:00
# Calculate the space to create the image and create.
make_image
2021-08-30 15:10:20 -05:00
# Create the disk partitions it
parted -s "${current_dir}"/"${imagename}".img mklabel msdos
parted -s "${current_dir}"/"${imagename}".img mkpart primary ext3 4MiB "${bootsize}"MiB
parted -s -a minimal "${current_dir}"/"${imagename}".img mkpart primary "$fstype" "${bootsize}"MiB 100%
2018-07-06 18:03:37 -05:00
# Set the partition variables
2021-08-30 15:10:20 -05:00
loopdevice=$(losetup --show -fP "${current_dir}/${imagename}.img")
bootp="${loopdevice}p1"
rootp="${loopdevice}p2"
2018-07-06 18:03:37 -05:00
# Create file systems
2021-08-30 15:10:20 -05:00
log "Formating partitions" green
if [[ "$fstype" == "ext4" ]]; then
features="^64bit,^metadata_csum"
elif [[ "$fstype" == "ext3" ]]; then
features="^64bit"
fi
2021-08-30 15:10:20 -05:00
mkfs -O "$features" -t "$fstype" -L BOOT "${bootp}"
mkfs -O "$features" -t "$fstype" -L ROOTFS "${rootp}"
2018-07-06 18:03:37 -05:00
# Create the dirs for the partitions and mount them
2021-08-30 15:10:20 -05:00
mkdir -p "${basedir}"/root/
mount "${rootp}" "${basedir}"/root
mkdir -p "${basedir}"/root/boot
mount "${bootp}" "${basedir}"/root/boot
# We do this here because we don't want to hardcode the UUID for the partition during creation.
# systemd doesn't seem to be generating the fstab properly for some people, so let's create one.
cat <<EOF >"${work_dir}"/etc/fstab
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
UUID=$(blkid -s UUID -o value ${rootp}) / $fstype defaults,noatime 0 1
EOF
2018-06-25 19:07:00 -05:00
2021-08-30 15:10:20 -05:00
log "Rsyncing rootfs into image file" green
rsync -HPavz -q "${work_dir}"/ "${basedir}"/root/
2018-07-06 18:03:37 -05:00
sync
2017-09-20 13:23:36 -05:00
# Samsung bootloaders must be signed.
# These are the same steps that are done by
# https://github.com/friendlyarm/sd-fuse_nanopi2/blob/master/fusing.sh
cd "${basedir}"
2017-09-20 13:23:36 -05:00
mkdir -p bootloader
cd "${basedir}"/bootloader
wget 'https://github.com/friendlyarm/sd-fuse_s5p6818/blob/master/prebuilt/bl1-mmcboot.bin?raw=true' -O "${basedir}"/bootloader/bl1-mmcboot.bin
wget 'https://github.com/friendlyarm/sd-fuse_s5p6818/blob/master/prebuilt/fip-loader.img?raw=true' -O "${basedir}"/bootloader/fip-loader.img
wget 'https://github.com/friendlyarm/sd-fuse_s5p6818/blob/master/prebuilt/fip-secure.img?raw=true' -O "${basedir}"/bootloader/fip-secure.img
wget 'https://github.com/friendlyarm/sd-fuse_s5p6818/blob/master/prebuilt/fip-nonsecure.img?raw=true' -O "${basedir}"/bootloader/fip-nonsecure.img
wget 'https://github.com/friendlyarm/sd-fuse_s5p6818/blob/master/tools/fw_printenv?raw=true' -O "${basedir}"/bootloader/fw_printenv
chmod 755 "${basedir}"/bootloader/fw_printenv
ln -s "${basedir}"/bootloader/fw_printenv "${basedir}"/bootloader/fw_setenv
dd if="${basedir}"/bootloader/bl1-mmcboot.bin of=${loopdevice} bs=512 seek=1
2018-07-16 18:16:08 -05:00
dd if="${basedir}"/bootloader/fip-loader.img of=${loopdevice} bs=512 seek=129
dd if="${basedir}"/bootloader/fip-secure.img of=${loopdevice} bs=512 seek=769
dd if="${basedir}"/bootloader/fip-nonsecure.img of=${loopdevice} bs=512 seek=3841
2018-06-25 19:07:00 -05:00
cat << EOF > "${basedir}"/bootloader/env.conf
# U-Boot environment for Debian, Ubuntu
#
# Copyright (C) Guangzhou FriendlyARM Computer Tech. Co., Ltd.
# (http://www.friendlyarm.com)
#
bootargs console=ttySAC0,115200n8 root=/dev/mmcblk0p2 rootfstype=$fstype rootwait rw consoleblank=0 net.ifnames=0
bootdelay 1
EOF
fw_setenv ${loopdevice} -s env.conf
sync
2018-06-25 19:07:00 -05:00
# It should be possible to build your own u-boot, as part of this, if you
# prefer, it will only generate the fip-nonsecure.img however.
#git clone https://github.com/friendlyarm/u-boot -b nanopi2-v2016.01
#cd u-boot
#make CROSS_COMPILE=aarch64-linux-gnu- s5p6818_nanopi3_defconfig
#make CROSS_COMPILE=aarch64-linux-gnu-
#dd if=fip-nonsecure.img of=$loopdevice bs=512 seek=3841
2017-09-20 13:23:36 -05:00
2021-08-30 15:10:20 -05:00
cd ${current_dir}
# Umount filesystem
umount -l "${rootp}"
# Check filesystem
e2fsck -y -f "$rootp"
# Remove loop devices
kpartx -dv "${loopdevice}"
losetup -d "${loopdevice}"
# Compress image compilation
include compress_img
2018-06-25 19:07:00 -05: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-08-30 15:10:20 -05:00
clean_build