Files
arm-kali/nanopi2.sh

361 lines
14 KiB
Bash
Raw Normal View History

2021-09-16 06:42:05 +01:00
#!/usr/bin/env bash
#
2021-09-27 10:51:02 +01:00
# Kali Linux ARM build-script for NanoPi2 (32-bit)
# Source: https://gitlab.com/kalilinux/build-scripts/kali-arm
2021-09-16 06:42:05 +01:00
#
# This is a community script - you will need to generate your own image to use
# More information: https://www.kali.org/docs/arm/nanopi2/
#
2016-01-30 14:18:34 -06:00
2021-09-16 06:42:05 +01:00
# Stop on error
set -e
2016-01-30 14:18:34 -06:00
2020-07-22 13:52:33 -05:00
# Uncomment to activate debug
# debug=true
2018-06-22 18:44:57 -05:00
# shellcheck disable=SC2154
# Load general functions
# shellcheck source=/dev/null
source ./common.d/functions.sh
2016-01-30 14:18:34 -06:00
2020-08-02 00:14:21 -05:00
# Hardware model
hw_model=${hw_model:-"nanopi2"}
# Architecture
architecture=${architecture:-"armhf"}
# 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 environment
include check
# Packages build list
include packages
# Execute initial debootstrap
debootstrap_exec http://http.kali.org/kali
# Enable eatmydata in compilation
include eatmydata
2020-07-22 13:52:33 -05:00
# debootstrap second stage
2020-08-02 00:14:21 -05:00
systemd-nspawn_exec eatmydata /debootstrap/debootstrap --second-stage
# Define sources.list
include sources.list
# APT options
include apt_options
2020-07-22 13:52:33 -05:00
# So X doesn't complain, we add kali to hosts
include hosts
# Set hostname
set_hostname "${hostname}"
# Network configs
include network
add_interface eth0
2016-01-30 14:18:34 -06:00
2021-09-16 06:58:38 +01:00
# Copy directory bsp into build dir
status "Copy directory bsp into build dir"
cp -rp bsp "${work_dir}"
2020-07-22 13:52:33 -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
2016-01-30 14:18:34 -06:00
2020-07-22 13:52:33 -05:00
# Third stage
2021-09-22 16:37:07 +02:00
cat <<EOF > "${work_dir}"/third-stage
#!/usr/bin/env bash
set -e
status_3i=0
status_3t=\$(grep '^status_stage3 ' \$0 | wc -l)
2016-01-30 14:18:34 -06:00
2021-09-22 16:37:07 +02:00
status_stage3() {
status_3i=\$((status_3i+1))
echo " [i] Stage 3 (\${status_3i}/\${status_3t}): \$1"
}
2016-01-30 14:18:34 -06:00
2021-09-22 16:37:07 +02:00
status_stage3 'Update apt'
export DEBIAN_FRONTEND=noninteractive
eatmydata apt-get update
2021-09-22 16:37:07 +02:00
status_stage3 'Install core packages'
eatmydata apt-get -y install ${third_stage_pkgs}
2021-09-22 16:37:07 +02:00
status_stage3 'Install packages'
eatmydata apt-get install -y ${packages} || eatmydata apt-get install -y --fix-broken
2021-09-22 16:37:07 +02:00
status_stage3 'Install desktop packages'
eatmydata apt-get install -y ${desktop_pkgs} ${extra} || eatmydata apt-get install -y --fix-broken
2020-07-22 13:52:33 -05:00
2021-09-22 17:47:15 +02:00
status_stage3 'ntp does not always sync the date, but systemd-timesyncd does, so we remove ntp and reinstall it with this'
2021-09-22 16:37:07 +02:00
eatmydata apt-get install -y systemd-timesyncd --autoremove
2016-01-30 14:18:34 -06:00
2021-09-22 16:37:07 +02:00
status_stage3 'Clean up'
eatmydata apt-get -y --purge autoremove
2016-01-30 14:18:34 -06:00
2021-09-22 16:37:07 +02:00
status_stage3 'Linux console/keyboard configuration'
2020-07-22 13:52:33 -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-09-22 16:37:07 +02:00
status_stage3 'Copy all services'
2020-07-22 13:52:33 -05:00
cp -p /bsp/services/all/*.service /etc/systemd/system/
# Regenerated the shared-mime-info database on the first boot
2021-09-16 06:58:38 +01:00
# since it fails to do so properly in a chroot
systemctl enable smi-hack
2021-09-22 16:37:07 +02:00
status_stage3 'Generate SSH host keys on first run'
systemctl enable regenerate_ssh_host_keys
status_stage3 'Enable sshd'
systemctl enable ssh
2016-01-30 14:18:34 -06:00
2021-09-22 16:37:07 +02:00
status_stage3 'Allow users to use NetworkManager over ssh'
install -m644 /bsp/polkit/10-NetworkManager.pkla /var/lib/polkit-1/localauthority/50-local.d
2021-09-22 16:37:07 +02:00
status_stage3 'Install ca-certificate'
cd /root
2020-07-22 13:52:33 -05:00
apt download -o APT::Sandbox::User=root ca-certificates 2>/dev/null
2021-09-22 16:37:07 +02:00
status_stage3 'Set a REGDOMAIN'
2020-07-22 13:52:33 -05:00
sed -i -e 's/REGDOM.*/REGDOMAIN=00/g' /etc/default/crda
2016-01-30 14:18:34 -06:00
2021-09-22 16:37:07 +02:00
status_stage3 'Enable login over serial'
2020-07-22 13:52:33 -05:00
echo "T0:23:respawn:/sbin/agetty -L ttyAMA0 115200 vt100" >> /etc/inittab
2016-01-30 14:18:34 -06:00
2021-09-22 16:37:07 +02:00
status_stage3 'Try and make the console a bit nicer. Set the terminus font for a bit nicer display'
2020-07-22 13:52:33 -05: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-22 16:37:07 +02:00
status_stage3 'Fix startup time from 5 minutes to 15 secs on raise interface wlan0'
2020-07-22 13:52:33 -05:00
sed -i 's/^TimeoutStartSec=5min/TimeoutStartSec=15/g' "/usr/lib/systemd/system/networking.service"
2020-08-02 00:14:21 -05:00
2021-09-22 16:37:07 +02:00
status_stage3 'Enable runonce'
install -m755 /bsp/scripts/runonce /usr/sbin/
cp -rf /bsp/runonce.d /etc
systemctl enable runonce
status_stage3 'Clean up dpkg.eatmydata'
2020-08-02 00:14:21 -05:00
rm -f /usr/bin/dpkg
2021-09-22 16:37:07 +02:00
dpkg-divert --remove --rename /usr/bin/dpkg
2016-01-30 14:18:34 -06:00
EOF
2020-07-22 13:52:33 -05:00
# Run third stage
chmod 0755 "${work_dir}"/third-stage
status "Run third stage"
2020-07-22 13:52:33 -05:00
systemd-nspawn_exec /third-stage
2016-01-30 14:18:34 -06:00
2020-07-22 13:52:33 -05:00
# Clean system
include clean_system
trap clean_build ERR SIGTERM SIGINT
2016-01-30 14:18:34 -06:00
# Disable the use of http proxy in case it is enabled.
disable_proxy
# Mirror & suite replacement
restore_mirror
# Reload sources.list
include sources.list
2016-01-30 14:18:34 -06:00
2021-09-16 06:58:38 +01:00
# We need an older gcc because of kernel age
cd "${base_dir}"
git clone --depth 1 https://gitlab.com/kalilinux/packages/gcc-arm-linux-gnueabihf-4-7.git gcc-arm-linux-gnueabihf-4.7
2016-01-30 14:18:34 -06:00
# Kernel section. If you want to use a custom kernel, or configuration, replace
2021-09-16 06:58:38 +01:00
# them in this section
2020-07-22 13:52:33 -05:00
git clone --depth 1 https://github.com/friendlyarm/linux-3.4.y -b nanopi2-lollipop-mr1 ${work_dir}/usr/src/kernel
cd ${work_dir}/usr/src/kernel
git rev-parse HEAD > ${work_dir}/usr/src/kernel-at-commit
2016-01-30 14:18:34 -06:00
touch .scmversion
export ARCH=arm
export CROSS_COMPILE="${base_dir}"/gcc-arm-linux-gnueabihf-4.7/bin/arm-linux-gnueabihf-
2020-07-22 13:52:33 -05:00
patch -p1 --no-backup-if-mismatch < ${current_dir}/patches/mac80211.patch
# Ugh, this patch is needed because the ethernet driver uses parts of netdev
# from a newer kernel?
2020-07-22 13:52:33 -05:00
patch -p1 --no-backup-if-mismatch < ${current_dir}/patches/0001-Remove-define.patch
cp ${current_dir}/kernel-configs/nanopi2* ${work_dir}/usr/src/
cp ../nanopi2-vendor.config .config
2016-01-30 14:18:34 -06:00
make -j $(grep -c processor /proc/cpuinfo)
make uImage
2020-07-22 13:52:33 -05:00
make modules_install INSTALL_MOD_PATH=${work_dir}
2021-09-16 06:58:38 +01:00
# We copy this twice because you can't do symlinks on fat partitions
# Also, the uImage known as uImage.hdmi is used by uboot if hdmi output is
2021-09-16 06:58:38 +01:00
# detected
2020-07-22 13:52:33 -05:00
cp arch/arm/boot/uImage ${work_dir}/boot/uImage-720p
cp arch/arm/boot/uImage ${work_dir}/boot/uImage.hdmi
2021-09-16 06:58:38 +01:00
# Friendlyarm suggests staying at 720p for now
#cp ../nanopi2-1080p.config .config
#make -j $(grep -c processor /proc/cpuinfo)
#make uImage
2020-07-22 13:52:33 -05:00
#cp arch/arm/boot/uImage ${work_dir}/boot/uImage-1080p
#cp ../nanopi2-lcd-hd101.config .config
#make -j $(grep -c processor /proc/cpuinfo)
#make uImage
2020-07-22 13:52:33 -05:00
#cp arch/arm/boot/uImage ${work_dir}/boot/uImage-hd101
#cp ../nanopi2-lcd-hd700.config .config
#make -j $(grep -c processor /proc/cpuinfo)
#make uImage
2020-07-22 13:52:33 -05:00
#cp arch/arm/boot/uImage ${work_dir}/boot/uImage-hd700
#cp ../nanopi2-lcd.config .config
#make -j $(grep -c processor /proc/cpuinfo)
#make uImage
# The default uImage is for lcd usage, so we copy the lcd one twice
2021-09-16 06:58:38 +01:00
# so people have a backup in case they overwrite uImage for some reason
2020-07-22 13:52:33 -05:00
#cp arch/arm/boot/uImage ${work_dir}/boot/uImage-s70
#cp arch/arm/boot/uImage ${work_dir}/boot/uImage.lcd
#cp arch/arm/boot/uImage ${work_dir}/boot/uImage
cd "${base_dir}"
2016-01-30 14:18:34 -06:00
# FriendlyARM suggest using backports for wifi with their devices, and the
2021-09-16 06:58:38 +01:00
# recommended version is the 4.4.2
2020-07-22 13:52:33 -05:00
cd ${work_dir}/usr/src/
#wget https://www.kernel.org/pub/linux/kernel/projects/backports/stable/v4.4.2/backports-4.4.2-1.tar.xz
#tar -xf backports-4.4.2-1.tar.xz
git clone https://github.com/friendlyarm/wireless
cd wireless
cd backports-4.4.2-1
2020-07-22 13:52:33 -05:00
patch -p1 --no-backup-if-mismatch < ${current_dir}/patches/kali-wifi-injection-4.4.patch
cd ..
2020-07-22 13:52:33 -05:00
#cp ${current_dir}/kernel-configs/backports.config .config
#make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j $(grep -c processor /proc/cpuinfo) KLIB_BUILD=${work_dir}/usr/src/kernel KLIB=${work_dir}
#make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KLIB_BUILD=${work_dir}/usr/src/kernel KLIB=${work_dir} INSTALL_MOD_PATH=${work_dir} install
#make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KLIB_BUILD=${work_dir}/usr/src/kernel KLIB=${work_dir} mrproper
#cp ${current_dir}/kernel-configs/backports.config .config
XCROSS="${base_dir}"/gcc-arm-linux-gnueabihf-4.7/bin/arm-linux-gnueabihf- ANDROID=n ./build.sh -k ${work_dir}/usr/src/kernel -c nanopi2 -o ${work_dir}
cd "${base_dir}"
# Now we clean up the kernel build
2020-07-22 13:52:33 -05:00
cd ${work_dir}/usr/src/kernel
make mrproper
cd "${base_dir}"
2021-09-16 06:58:38 +01:00
# Copy over the firmware for the nanopi2/3 wifi
# At some point, nexmon could work for the device, but the support would need to
2021-09-16 06:58:38 +01:00
# be added to nexmon
2020-07-22 13:52:33 -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 "${base_dir}"
2016-01-30 14:18:34 -06:00
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-22 13:52:33 -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
cd "${base_dir}"
2017-09-20 13:23:36 -05:00
# Calculate the space to create the image and create
make_image
# Create the disk partitions
status "Create the disk partitions"
2021-09-19 13:45:29 +01:00
parted -s "${image_dir}/${image_name}.img" mklabel msdos
2021-09-22 18:06:15 +02:00
parted -s "${image_dir}/${image_name}.img" mkpart primary fat32 4MiB "${bootsize}"MiB
parted -s -a minimal "${image_dir}/${image_name}.img" mkpart primary "$fstype" "${bootsize}"MiB 100%
2018-07-06 18:03:37 -05:00
# Set the partition variables
loopdevice=`losetup -f --show ${current_dir}/${image_name}.img`
2018-07-06 18:03:37 -05:00
device=`kpartx -va ${loopdevice} | sed 's/.*\(loop[0-9]\+\)p.*/\1/g' | head -1`
sleep 5
device="/dev/mapper/${device}"
bootp=${device}p1
rootp=${device}p2
# Create file systems
status "Formatting partitions" green
2020-07-22 13:52:33 -05:00
mkfs.ext3 -L BOOT ${bootp}
if [[ $fstype == ext4 ]]; then
features="-O ^64bit,^metadata_csum"
elif [[ $fstype == ext3 ]]; then
features="-O ^64bit"
fi
mkfs $features -t $fstype -L ROOTFS ${rootp}
2018-07-06 18:03:37 -05:00
# Create the dirs for the partitions and mount them
status "Create the dirs for the partitions and mount them"
mkdir -p "${base_dir}"/root
mount ${rootp} "${base_dir}"/root
mkdir -p "${base_dir}"/root/boot
mount ${bootp} "${base_dir}"/root/boot
2018-07-06 18:03:37 -05:00
2021-09-16 06:58:38 +01:00
# We do this down here to get rid of the build system's resolv.conf after running through the build
echo "nameserver ${nameserver}" > "${work_dir}"/etc/resolv.conf
2016-01-30 14:18:34 -06:00
2021-09-16 06:58:38 +01:00
# Create an fstab so that we don't mount / read-only
UUID=$(blkid -s UUID -o value ${rootp})
echo "UUID=$UUID / $fstype errors=remount-ro 0 1" >> ${work_dir}/etc/fstab
2018-07-06 18:03:37 -05:00
echo "Rsyncing rootfs into image file"
rsync -HPavz -q ${work_dir}/ ${base_dir}/root/
2017-09-20 13:23:36 -05:00
2016-01-30 14:18:34 -06:00
# Unmount partitions
2018-07-06 18:03:37 -05:00
sync
umount -l ${bootp}
umount -l ${rootp}
kpartx -dv ${loopdevice}
2016-01-30 14:18:34 -06:00
2021-09-16 06:58:38 +01:00
# Samsung bootloaders must be signed
2016-01-30 14:18:34 -06:00
# These are the same steps that are done by
# https://github.com/friendlyarm/sd-fuse_nanopi2/blob/master/fusing.sh
2018-06-23 19:42:37 -05:00
2021-09-16 06:58:38 +01:00
# Download the latest prebuilt from the above url
mkdir -p "${base_dir}"/bootloader
cd "${base_dir}"/bootloader
wget 'https://github.com/friendlyarm/sd-fuse_nanopi2/blob/96e1ba9603d237d0169485801764c5ce9591bf5e/prebuilt/2ndboot.bin?raw=true' -O 2ndboot.bin
wget 'https://github.com/friendlyarm/sd-fuse_nanopi2/blob/96e1ba9603d237d0169485801764c5ce9591bf5e/prebuilt/boot.TBI?raw=true' -O boot.TBI
wget 'https://github.com/friendlyarm/sd-fuse_nanopi2/blob/96e1ba9603d237d0169485801764c5ce9591bf5e/prebuilt/bootloader' -O bootloader
#wget https://raw.githubusercontent.com/friendlyarm/sd-fuse_nanopi2/master/prebuilt/bl1-mmcboot.bin
#wget https://raw.githubusercontent.com/friendlyarm/sd-fuse_nanopi2/master/prebuilt/bl_mon.img
#wget https://raw.githubusercontent.com/friendlyarm/sd-fuse_nanopi2/master/prebuilt/bootloader.img # This is u-boot
#wget https://raw.githubusercontent.com/friendlyarm/sd-fuse_nanopi2/master/prebuilt/loader-mmc.img
wget https://raw.githubusercontent.com/friendlyarm/sd-fuse_nanopi2/master/tools/fw_printenv
2021-09-17 10:42:05 +01:00
chmod 0755 fw_printenv
ln -s fw_printenv fw_setenv
2018-06-23 19:42:37 -05:00
dd if=2ndboot.bin of=${loopdevice} bs=512 seek=1
dd if=boot.TBI of=${loopdevice} bs=512 seek=64 count=1
dd if=bootloader of=${loopdevice} bs=512 seek=65
2018-06-23 19:42:37 -05:00
cat << EOF > ${base_dir}/bootloader/env.conf
# U-Boot environment for Debian, Ubuntu
#
2021-09-16 06:58:38 +01:00
# Copyright (C) Guangzhou FriendlyARM Computer Tech. Co., Ltd
# (http://www.friendlyarm.com)
#
bootargs console=ttyAMA0,115200n8 root=/dev/mmcblk0p2 rootfstype=$fstype rootwait rw consoleblank=0 net.ifnames=0
bootdelay 1
EOF
./fw_setenv ${loopdevice} -s env.conf
2016-01-30 14:18:34 -06:00
sync
cd "${base_dir}"
2016-01-30 14:18:34 -06:00
# Remove loop devices
2018-07-06 18:03:37 -05:00
losetup -d ${loopdevice}
2016-01-30 14:18:34 -06:00
2018-07-06 18:03:37 -05:00
# Compress image compilation
include compress_img
# 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.
clean_build