blob: c3582eaec94bc759845b7584b5cf2c41cd5b113d [file] [log] [blame]
Austin Schuhab8c0c52020-02-22 13:42:37 -08001#!/bin/bash
2
3set -xe
4
Jim Ostrowskic58989a2022-01-29 16:12:08 -08005# Full path to Raspberry Pi Bullseye disk image
Austin Schuhf7970ca2022-02-11 22:19:29 -08006IMAGE="2022-01-28-raspios-bullseye-arm64-lite.img"
Jim Ostrowski138b5d82022-02-26 01:01:45 -08007MOD_IMAGE_NAME=`echo ${IMAGE} | sed s/.img/-frc-mods.img/`
8
9if [ ! -f "$IMAGE" ]; then
10 echo "Attempting to use already modified image"
11 if [ ! -f "$MOD_IMAGE_NAME" ]; then
12 echo "Must provide image filename."
13 echo "Couldn't find $IMAGE or $MOD_IMAGE_NAME"
14 exit 1
15 fi
16 echo "Using already modified image: ${MOD_IMAGE_NAME}"
17 IMAGE=$MOD_IMAGE_NAME
18fi
19
Austin Schuhf7970ca2022-02-11 22:19:29 -080020# Kernel built with build_kernel.sh
21KERNEL="kernel_5.10.tar.gz"
Austin Schuhd4df9552020-09-13 18:59:50 -070022BOOT_PARTITION="${IMAGE}.boot_partition"
23PARTITION="${IMAGE}.partition"
Austin Schuhab8c0c52020-02-22 13:42:37 -080024
25function target() {
Austin Schuhf7970ca2022-02-11 22:19:29 -080026 HOME=/root/ USER=root sudo proot -0 -q qemu-aarch64-static -w / -r "${PARTITION}" "$@"
Austin Schuhab8c0c52020-02-22 13:42:37 -080027}
28
29function user_pi_target() {
Austin Schuhf7970ca2022-02-11 22:19:29 -080030 USER=root sudo proot -0 -q qemu-aarch64-static -w / -r "${PARTITION}" sudo -h 127.0.0.1 -u pi "$@"
Austin Schuhab8c0c52020-02-22 13:42:37 -080031}
32
Austin Schuh90c82782020-02-26 19:54:51 -080033
Austin Schuhab8c0c52020-02-22 13:42:37 -080034mkdir -p "${PARTITION}"
Austin Schuhe6965982020-02-26 23:12:05 -080035mkdir -p "${BOOT_PARTITION}"
36
37if mount | grep "${BOOT_PARTITION}" >/dev/null ;
38then
39 echo "Already mounted"
40else
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080041 OFFSET="$(/sbin/fdisk -lu "${IMAGE}" | grep "${IMAGE}1" | awk '{print 512*$2}')"
Austin Schuhe6965982020-02-26 23:12:05 -080042 sudo mount -o loop,offset=${OFFSET} "${IMAGE}" "${BOOT_PARTITION}"
43fi
44
45# Enable the camera on boot.
46if ! grep "start_x=1" "${BOOT_PARTITION}/config.txt"; then
47 echo "start_x=1" | sudo tee -a "${BOOT_PARTITION}/config.txt"
48fi
49if ! grep "gpu_mem=128" "${BOOT_PARTITION}/config.txt"; then
50 echo "gpu_mem=128" | sudo tee -a "${BOOT_PARTITION}/config.txt"
51fi
Austin Schuhc0ec2a82022-02-24 17:26:29 -080052if ! grep "enable_uart=1" "${BOOT_PARTITION}/config.txt"; then
53 echo "enable_uart=1" | sudo tee -a "${BOOT_PARTITION}/config.txt"
54fi
Jim Ostrowskic58989a2022-01-29 16:12:08 -080055# For now, disable the new libcamera driver in favor of legacy ones
56sudo sed -i s/^camera_auto_detect=1/#camera_auto_detect=1/ "${BOOT_PARTITION}/config.txt"
Austin Schuhc0ec2a82022-02-24 17:26:29 -080057# Enable SPI.
58sudo sed -i s/^.*dtparam=spi=on/dtparam=spi=on/ "${BOOT_PARTITION}/config.txt"
Austin Schuhe6965982020-02-26 23:12:05 -080059
Austin Schuhf7970ca2022-02-11 22:19:29 -080060sudo tar -zxvf "${KERNEL}" --strip-components 2 -C ${BOOT_PARTITION}/ ./fat32
61
Jim Ostrowskic58989a2022-01-29 16:12:08 -080062# Seeing a race condition with umount, so doing lazy umount
63sudo umount -l "${BOOT_PARTITION}"
Austin Schuhe6965982020-02-26 23:12:05 -080064rmdir "${BOOT_PARTITION}"
Austin Schuhab8c0c52020-02-22 13:42:37 -080065
66if mount | grep "${PARTITION}" >/dev/null ;
67then
68 echo "Already mounted"
69else
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080070 OFFSET="$(/sbin/fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print 512*$2}')"
Austin Schuh90c82782020-02-26 19:54:51 -080071
Austin Schuh7da6fe42021-06-20 15:37:55 -070072 if [[ "$(stat -c %s "${IMAGE}")" < 2000000000 ]]; then
Austin Schuh90c82782020-02-26 19:54:51 -080073 echo "Growing image"
Austin Schuhd4df9552020-09-13 18:59:50 -070074 dd if=/dev/zero bs=1G count=1 >> "${IMAGE}"
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080075 START="$(/sbin/fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print $2}')"
Austin Schuh90c82782020-02-26 19:54:51 -080076
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080077 sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | /sbin/fdisk "${IMAGE}"
Austin Schuh90c82782020-02-26 19:54:51 -080078 d # remove old partition
79 2
80 n # new partition
81 p # primary partition
82 2 # partion number 2
83 532480 # start where the old one starts
84 # To the end
85 p # print the in-memory partition table
86 w # Flush
87 q # and we're done
88EOF
89
90 sudo losetup -o "${OFFSET}" -f "${IMAGE}"
91 LOOPBACK="$(sudo losetup --list | grep "${IMAGE}" | awk '{print $1}')"
92 sudo e2fsck -f "${LOOPBACK}"
93 sudo resize2fs "${LOOPBACK}"
94 sudo losetup -d "${LOOPBACK}"
95 fi
96
Austin Schuhab8c0c52020-02-22 13:42:37 -080097 echo "Mounting"
98 sudo mount -o loop,offset=${OFFSET} "${IMAGE}" "${PARTITION}"
99fi
100
Austin Schuhc0ec2a82022-02-24 17:26:29 -0800101if [[ ! -e wiringpi-2.70-1.deb ]]; then
102 wget --continue https://software.frc971.org/Build-Dependencies/wiringpi-2.70-1.deb
103fi
104
Austin Schuhab8c0c52020-02-22 13:42:37 -0800105sudo cp target_configure.sh "${PARTITION}/tmp/"
Austin Schuhc0ec2a82022-02-24 17:26:29 -0800106sudo cp wiringpi-2.70-1.deb "${PARTITION}/tmp/"
Austin Schuhab8c0c52020-02-22 13:42:37 -0800107sudo cp dhcpcd.conf "${PARTITION}/tmp/dhcpcd.conf"
Austin Schuh2fe4b712020-03-15 14:21:45 -0700108sudo cp sctp.conf "${PARTITION}/etc/sysctl.d/sctp.conf"
Austin Schuhf3318a72021-11-01 22:05:32 -0700109sudo cp logind.conf "${PARTITION}/etc/systemd/logind.conf"
Austin Schuhab8c0c52020-02-22 13:42:37 -0800110sudo cp change_hostname.sh "${PARTITION}/tmp/change_hostname.sh"
Austin Schuhc0ec2a82022-02-24 17:26:29 -0800111sudo cp enable_imu.sh "${PARTITION}/tmp/"
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -0800112sudo cp frc971.service "${PARTITION}/etc/systemd/system/frc971.service"
Austin Schuhf7970ca2022-02-11 22:19:29 -0800113sudo cp frc971chrt.service "${PARTITION}/etc/systemd/system/frc971chrt.service"
Austin Schuh92aa2de2020-09-19 19:09:55 -0700114sudo cp rt.conf "${PARTITION}/etc/security/limits.d/rt.conf"
Austin Schuh867bd922021-11-07 16:59:52 -0800115sudo cp usb-mount@.service "${PARTITION}/etc/systemd/system/usb-mount@.service"
116sudo cp 99-usb-mount.rules "${PARTITION}/etc/udev/rules.d/99-usb-mount.rules"
Austin Schuhab8c0c52020-02-22 13:42:37 -0800117
118target /bin/mkdir -p /home/pi/.ssh/
119cat ~/.ssh/id_rsa.pub | target tee /home/pi/.ssh/authorized_keys
120
Austin Schuhf7970ca2022-02-11 22:19:29 -0800121sudo rm -rf "${PARTITION}/lib/modules/"*
122sudo tar -zxvf "${KERNEL}" --strip-components 4 -C "${PARTITION}/lib/modules/" ./ext4/lib/modules/
Austin Schuhc0ec2a82022-02-24 17:26:29 -0800123sudo cp adis16505.ko "${PARTITION}/lib/modules/5.10.78-rt55-v8+/kernel/"
124target /usr/sbin/depmod 5.10.78-rt55-v8+
Austin Schuhf7970ca2022-02-11 22:19:29 -0800125
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800126# Downloads and installs our target libraries
Austin Schuhab8c0c52020-02-22 13:42:37 -0800127target /bin/bash /tmp/target_configure.sh
128
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800129# Add a file to show when this image was last modified and by whom
130TIMESTAMP_FILE="${PARTITION}/home/pi/.ImageModifiedDate.txt"
Jim Ostrowski2a483b32022-02-15 18:19:14 -0800131echo "Date modified:"`date` > "${TIMESTAMP_FILE}"
Jim Ostrowski138b5d82022-02-26 01:01:45 -0800132echo "Image file: ${IMAGE}" >> "${TIMESTAMP_FILE}"
Jim Ostrowski2a483b32022-02-15 18:19:14 -0800133echo "Git tag: "`git rev-parse HEAD` >> "${TIMESTAMP_FILE}"
134echo "User: "`whoami` >> "${TIMESTAMP_FILE}"
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800135
Austin Schuhab8c0c52020-02-22 13:42:37 -0800136# Run a prompt as root inside the target to poke around and check things.
137target /bin/bash --rcfile /root/.bashrc
138
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800139sudo umount -l "${PARTITION}"
Austin Schuhab8c0c52020-02-22 13:42:37 -0800140rmdir "${PARTITION}"
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800141
Jim Ostrowski138b5d82022-02-26 01:01:45 -0800142# Move the image to a different name, to indicate we've modified it
143if [ ${IMAGE} != ${MOD_IMAGE_NAME} ]; then
144 mv ${IMAGE} ${MOD_IMAGE_NAME}
145fi