blob: cd80e58c566afa5904d5c8f0f336467d864813e1 [file] [log] [blame]
Austin Schuh791ac752022-11-26 19:14:39 -08001#!/bin/bash
2
3set -eux
4set -o pipefail
5
6UBOOT_VERSION=v2022.10
7
8IMAGE="arm64_bullseye_debian.img"
9KERNEL_VERSION=6.0.8-rt14-rockpi4b
10PARTITION="${IMAGE}.partition"
11
Henry Speiser2fc93332023-01-20 22:57:25 -080012# Check if dependencies are missing.
13missing_deps=()
14REQUIRED_DEPS=(
Henry Speiser2fc93332023-01-20 22:57:25 -080015 bison
Henry Speiser31c573a2023-01-28 12:44:59 -080016 debootstrap
Jim Ostrowski5f989ee2023-02-26 17:50:21 -080017 device-tree-compiler
18 flex
19 gcc-aarch64-linux-gnu
20 gcc-arm-none-eabi
21 swig
22 u-boot-tools
Maxwell Henderson7715c0b2023-09-04 16:13:45 -070023 qemu-user-static
Henry Speiser2fc93332023-01-20 22:57:25 -080024)
25for dep in "${REQUIRED_DEPS[@]}"; do
26 if ! dpkg-query -W -f='${Status}' "${dep}" | grep -q "install ok installed"; then
27 missing_deps+=("${dep}")
28 fi
29done
30
31# Print missing dependencies
32if ((${#missing_deps[@]} != 0 )); then
33 echo "Missing dependencies, please install:"
34 echo apt install "${missing_deps[@]}"
35 exit 1
36else
37 echo -e "\033[32mAll dependencies are already installed\033[0m"
38fi
39
Austin Schuh791ac752022-11-26 19:14:39 -080040export CC=aarch64-linux-gnu-
41
42# Reset any existing mounts.
43if mount | grep "${PARTITION}/boot" >/dev/null; then
44 sudo umount "${PARTITION}/boot"
45fi
46
47if mount | grep "${PARTITION}" >/dev/null; then
48 sudo umount "${PARTITION}"
49fi
50
henry19afbff2022-12-17 18:33:40 -080051LOOPBACK="$(sudo losetup --list | awk "/$IMAGE/"'{print $1}')"
Austin Schuh791ac752022-11-26 19:14:39 -080052if [[ -n "${LOOPBACK}" ]]; then
53 echo "Loop still exists..."
54 sudo losetup -d "${LOOPBACK}"
55fi
56
57# Build bl31.elf.
58if [[ ! -e arm-trusted-firmware ]]; then
59 git clone https://github.com/ARM-software/arm-trusted-firmware --depth=1
60fi
61
62pushd arm-trusted-firmware/
63git pull --ff-only
64#make CROSS_COMPILE="${CC}" realclean -j "$(nproc)"
65make CROSS_COMPILE="${CC}" PLAT=rk3399 -j "$(nproc)"
66popd
67
68export BL31="$(pwd)/arm-trusted-firmware/build/rk3399/release/bl31/bl31.elf"
69
70ls -lah "${BL31}"
71
72# Now, build uboot.
73if [[ ! -e u-boot ]]; then
Henry Speiser823acb32023-01-21 10:56:30 -080074 git clone -b "${UBOOT_VERSION}" https://github.com/u-boot/u-boot --depth=1
Austin Schuh791ac752022-11-26 19:14:39 -080075fi
76
77pushd u-boot/
78git fetch origin "${UBOOT_VERSION}"
79git reset --hard "${UBOOT_VERSION}"
80
81#make ARCH=arm CROSS_COMPILE="${CC}" distclean -j $(nproc)
82make ARCH=arm CROSS_COMPILE="${CC}" rock-pi-4-rk3399_defconfig -j "$(nproc)"
83make ARCH=arm CROSS_COMPILE="${CC}" -j "$(nproc)"
84echo "Made uboot"
85popd
86
henry19afbff2022-12-17 18:33:40 -080087function target_mkdir() {
88 target "install -d -m $2 -o $(echo $1 | sed 's/\.[^.]*//') -g $(echo $1 | sed 's/[^.]*\.//') $3"
89}
90
91function copyfile() {
92 sudo cp contents/$3 ${PARTITION}/$3
93 sudo chmod $2 ${PARTITION}/$3
94 target "chown $1 /$3"
95}
96
Austin Schuh791ac752022-11-26 19:14:39 -080097
98# Now build the base set of partitions.
99NEW_IMAGE=0
100if [[ ! -e "${IMAGE}" ]]; then
101 NEW_IMAGE=1
102 dd if=/dev/zero of="${IMAGE}" bs=1 count=0 seek=3G
103 dd if=./u-boot/idbloader.img of="${IMAGE}" seek=64 conv=notrunc
104 dd if=./u-boot/u-boot.itb of="${IMAGE}" seek=16384 conv=notrunc
105
106 sfdisk "${IMAGE}" <<-__EOF__
10716M,64M,L,*
10880M,,L,*
109__EOF__
110
111 sudo losetup -P -f "${IMAGE}"
112 LOOPBACK="$(sudo losetup --list | grep "${IMAGE}" | awk '{print $1}')"
113
114 sudo mkfs.fat "${LOOPBACK}p1"
115 sudo mkfs.ext4 -L rootfs "${LOOPBACK}p2"
116
117 sudo losetup -d "${LOOPBACK}"
118fi
119
120
121# Mount them
122mkdir -p "${PARTITION}"
123
124sudo losetup -P -f "${IMAGE}"
125LOOPBACK="$(sudo losetup --list | grep "${IMAGE}" | awk '{print $1}')"
126sudo mount "${LOOPBACK}p2" "${PARTITION}"
127if [[ ! -e "${PARTITION}/boot" ]]; then
128 sudo mkdir "${PARTITION}/boot"
129fi
130sudo mount "${LOOPBACK}p1" "${PARTITION}/boot"
131
132# Run the string command as root inside the target.
133function target() {
Austin Schuh4a716562023-01-28 13:43:02 -0800134 sudo chroot --userspec=root:root "${PARTITION}" qemu-aarch64-static \
Austin Schuh791ac752022-11-26 19:14:39 -0800135 /bin/bash -c "$1"
136}
137
138# Run the string command as the pi user inside the target.
139function pi_target() {
Austin Schuh4a716562023-01-28 13:43:02 -0800140 sudo chroot --userspec=pi:pi --groups=pi "${PARTITION}" qemu-aarch64-static \
Austin Schuh791ac752022-11-26 19:14:39 -0800141 /bin/bash -c "$1"
142}
143
144# And, if we made a new image, debootstrap to get things going.
145if (( NEW_IMAGE == 1 )); then
146 sudo debootstrap --arch=arm64 --no-check-gpg --foreign bullseye \
147 "${PARTITION}" http://deb.debian.org/debian/
148 sudo cp /usr/bin/qemu-aarch64-static "${PARTITION}/usr/bin/"
149
150 target "/debootstrap/debootstrap --second-stage"
151
152 target "useradd -m -p '\$y\$j9T\$85lzhdky63CTj.two7Zj20\$pVY53UR0VebErMlm8peyrEjmxeiRw/rfXfx..9.xet1' -s /bin/bash pi"
153
154else
155 sudo cp /usr/bin/qemu-aarch64-static "${PARTITION}/usr/bin/"
156fi
157
158# Install the kernel.
159sudo rm -rf "${PARTITION}/boot/dtbs/${KERNEL_VERSION}/"
160sudo rm -rf "${PARTITION}/lib/modules/${KERNEL_VERSION}/"
161sudo mkdir -p "${PARTITION}/lib/modules/"
162sudo tar --owner=0 --group=0 --no-same-owner --no-same-permissions -xvf \
163 "linux-kernel-${KERNEL_VERSION}.tar.xz" -C "${PARTITION}/boot/" \
164 --strip-components=2 ./boot/
165sudo tar --strip-components=3 -xvf "linux-kernel-${KERNEL_VERSION}.tar.xz" \
166 -C "${PARTITION}/lib/modules/" ./lib/modules
167
168# Now, configure it to start automatically.
Austin Schuh74617c02023-02-05 18:44:18 -0800169cat << __EOF__ | sudo tee "${PARTITION}/boot/sdcard_extlinux.conf"
Austin Schuh791ac752022-11-26 19:14:39 -0800170label Linux ${KERNEL_VERSION}
171 kernel /vmlinuz-${KERNEL_VERSION}
Ravago Jones1b1ad652023-02-26 18:17:00 -0800172 append earlycon=uart8250,mmio32,0xff1a0000 earlyprintk console=ttyS2,1500000n8 root=/dev/mmcblk0p2 ro rootfstype=ext4 rootflags=data=journal rootwait
Austin Schuh791ac752022-11-26 19:14:39 -0800173 fdtdir /dtbs/${KERNEL_VERSION}/
174__EOF__
Austin Schuh74617c02023-02-05 18:44:18 -0800175cat << __EOF__ | sudo tee "${PARTITION}/boot/emmc_extlinux.conf"
176label Linux ${KERNEL_VERSION}
177 kernel /vmlinuz-${KERNEL_VERSION}
Ravago Jones1b1ad652023-02-26 18:17:00 -0800178 append earlycon=uart8250,mmio32,0xff1a0000 earlyprintk console=ttyS2,1500000n8 root=/dev/mmcblk1p2 ro rootfstype=ext4 rootflags=data=journal rootwait
Austin Schuh74617c02023-02-05 18:44:18 -0800179 fdtdir /dtbs/${KERNEL_VERSION}/
180__EOF__
Austin Schuh791ac752022-11-26 19:14:39 -0800181
Austin Schuh74617c02023-02-05 18:44:18 -0800182mkimage -c none -A arm -T script -d contents/boot/boot.script contents/boot/boot.scr
183copyfile root.root 644 boot/boot.scr
184rm contents/boot/boot.scr
Austin Schuh791ac752022-11-26 19:14:39 -0800185copyfile root.root 644 etc/apt/sources.list.d/bullseye-backports.list
186copyfile root.root 644 etc/apt/sources.list.d/frc971.list
187
188target "apt-get update"
Austin Schuh4a716562023-01-28 13:43:02 -0800189target "apt-get -y install -t bullseye-backports systemd systemd-resolved"
190
191# Make systemd-resolved work.
192target_mkdir root.root 755 run/systemd
193target_mkdir systemd-resolve.systemd-resolve 755 run/systemd/resolve
194copyfile systemd-resolve.systemd-resolve 644 run/systemd/resolve/stub-resolv.conf
195target "systemctl enable systemd-resolved"
196
Austin Schuh6283b262022-12-31 23:11:15 -0800197target "apt-get -y install -t bullseye-backports bpfcc-tools"
Austin Schuh791ac752022-11-26 19:14:39 -0800198
Austin Schuh709c2322023-02-17 13:30:32 -0800199target "apt-get install -y sudo openssh-server python3 bash-completion git v4l-utils cpufrequtils pmount rsync vim-nox chrony libopencv-calib3d4.5 libopencv-contrib4.5 libopencv-core4.5 libopencv-features2d4.5 libopencv-flann4.5 libopencv-highgui4.5 libopencv-imgcodecs4.5 libopencv-imgproc4.5 libopencv-ml4.5 libopencv-objdetect4.5 libopencv-photo4.5 libopencv-shape4.5 libopencv-stitching4.5 libopencv-superres4.5 libopencv-video4.5 libopencv-videoio4.5 libopencv-videostab4.5 libopencv-viz4.5 libnice10 pmount libnice-dev feh libgstreamer1.0-0 libgstreamer-plugins-base1.0-0 libgstreamer-plugins-bad1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-nice usbutils locales trace-cmd clinfo jq strace sysstat lm-sensors"
Austin Schuhc5300f22023-01-03 21:20:44 -0800200target "cd /tmp && wget https://software.frc971.org/Build-Dependencies/libmali-midgard-t86x-r14p0-x11_1.9-1_arm64.deb && sudo dpkg -i libmali-midgard-t86x-r14p0-x11_1.9-1_arm64.deb && rm libmali-midgard-t86x-r14p0-x11_1.9-1_arm64.deb"
201
202target "apt-get clean"
Austin Schuh791ac752022-11-26 19:14:39 -0800203
204target "usermod -a -G sudo pi"
205target "usermod -a -G video pi"
Austin Schuh31960d02022-12-11 16:51:14 -0800206target "localedef -i en_US -f UTF-8 en_US.UTF-8"
Austin Schuh791ac752022-11-26 19:14:39 -0800207
Austin Schuh791ac752022-11-26 19:14:39 -0800208copyfile root.root 644 etc/fstab
209copyfile root.root 440 etc/sudoers
210copyfile root.root 444 etc/default/cpufrequtils
211target_mkdir root.root 755 etc/systemd/network
212copyfile root.root 644 etc/systemd/network/eth0.network
213target_mkdir pi.pi 755 home/pi/.ssh
214copyfile pi.pi 600 home/pi/.ssh/authorized_keys
215target_mkdir root.root 700 root/bin
216copyfile root.root 500 root/bin/grow.sh
217copyfile root.root 500 root/bin/change_hostname.sh
218copyfile root.root 500 root/bin/deploy_kernel.sh
henryebe91952023-01-17 22:39:20 -0800219copyfile root.root 500 root/bin/chrt.sh
Austin Schuh791ac752022-11-26 19:14:39 -0800220copyfile root.root 644 etc/systemd/system/grow-rootfs.service
Austin Schuh74617c02023-02-05 18:44:18 -0800221copyfile root.root 644 etc/systemd/system/mount-boot.service
Austin Schuh791ac752022-11-26 19:14:39 -0800222copyfile root.root 644 etc/sysctl.d/sctp.conf
223copyfile root.root 644 etc/systemd/logind.conf
Austin Schuh6283b262022-12-31 23:11:15 -0800224copyfile root.root 555 etc/bash_completion.d/aos_dump_autocomplete
225copyfile root.root 444 etc/modules-load.d/adis16505.conf
Austin Schuh791ac752022-11-26 19:14:39 -0800226copyfile root.root 644 etc/security/limits.d/rt.conf
227copyfile root.root 644 etc/systemd/system/frc971.service
228copyfile root.root 644 etc/systemd/system/frc971chrt.service
229copyfile root.root 644 etc/systemd/system/usb-mount@.service
230copyfile root.root 644 etc/udev/rules.d/99-usb-mount.rules
Austin Schuh6283b262022-12-31 23:11:15 -0800231copyfile root.root 644 etc/udev/rules.d/99-adis16505.rules
Austin Schuhc5300f22023-01-03 21:20:44 -0800232copyfile root.root 644 etc/udev/rules.d/99-mali.rules
Filip Kujawac3aa10b2023-04-08 18:01:43 -0700233copyfile root.root 644 etc/udev/rules.d/99-coral-perms.rules
Austin Schuh174f23e2023-02-17 13:30:47 -0800234copyfile root.root 644 etc/chrony/chrony.conf
Austin Schuh791ac752022-11-26 19:14:39 -0800235
236target "apt-get update"
Austin Schuh791ac752022-11-26 19:14:39 -0800237
238target "systemctl enable systemd-networkd"
Austin Schuh791ac752022-11-26 19:14:39 -0800239target "systemctl enable grow-rootfs"
Austin Schuh74617c02023-02-05 18:44:18 -0800240target "systemctl enable mount-boot"
Austin Schuh791ac752022-11-26 19:14:39 -0800241target "systemctl enable frc971"
242target "systemctl enable frc971chrt"
243target "/root/bin/change_hostname.sh pi-971-1"
244
245
246if [[ ! -e "${PARTITION}/home/pi/.dotfiles" ]]; then
247 pi_target "cd /home/pi/ && \
248 git clone --separate-git-dir=/home/pi/.dotfiles https://github.com/AustinSchuh/.dotfiles.git tmpdotfiles && \
249 rsync --recursive --verbose --exclude .git tmpdotfiles/ /home/pi/ && \
250 rm -r tmpdotfiles && \
Austin Schuh4a716562023-01-28 13:43:02 -0800251 git --git-dir=/home/pi/.dotfiles/ --work-tree=/home/pi/ config --local status.showUntrackedFiles no"
252 pi_target "vim -c \":qa!\""
Austin Schuh791ac752022-11-26 19:14:39 -0800253
254 target "cd /root/ && \
255 git clone --separate-git-dir=/root/.dotfiles https://github.com/AustinSchuh/.dotfiles.git tmpdotfiles && \
256 rsync --recursive --verbose --exclude .git tmpdotfiles/ /root/ && rm -r tmpdotfiles && \
Austin Schuh4a716562023-01-28 13:43:02 -0800257 git --git-dir=/root/.dotfiles/ --work-tree=/root/ config --local status.showUntrackedFiles no"
258 target "vim -c \":qa!\""
Austin Schuh791ac752022-11-26 19:14:39 -0800259fi
260
261target "apt-get clean"
262
Jim Ostrowski5f989ee2023-02-26 17:50:21 -0800263# Add a file to show when this image was last modified and by whom
264TIMESTAMP_FILE="${PARTITION}/home/pi/.ImageModifiedDate.txt"
265echo "Date modified:"`date` > "${TIMESTAMP_FILE}"
266echo "Image file: ${IMAGE}" >> "${TIMESTAMP_FILE}"
267echo "Git tag: "`git rev-parse HEAD` >> "${TIMESTAMP_FILE}"
268echo "User: "`whoami` >> "${TIMESTAMP_FILE}"
269
Austin Schuh791ac752022-11-26 19:14:39 -0800270sudo chroot ${PARTITION} qemu-aarch64-static /bin/bash
271
272# TODO(austin): This appears to not be working... pi_target doesn't apper to be happy
Austin Schuh4a716562023-01-28 13:43:02 -0800273sudo chroot --userspec=pi:pi --groups=pi ${PARTITION} qemu-aarch64-static /bin/bash
Austin Schuh791ac752022-11-26 19:14:39 -0800274
275# TODO(austin): everything else we were doing to the pi's.
276sudo rm ${PARTITION}/usr/bin/qemu-aarch64-static
277sudo umount ${PARTITION}/boot
278sudo umount ${PARTITION}
279rmdir ${PARTITION}