blob: 12dceaebc24458689d216c3c182f57414c1cf05b [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=(
15 flex
16 bison
17 gcc-arm-none-eabi
18 gcc-aarch64-linux-gnu
19 device-tree-compiler
20 swig
Henry Speiser31c573a2023-01-28 12:44:59 -080021 debootstrap
Henry Speiser2fc93332023-01-20 22:57:25 -080022)
23for dep in "${REQUIRED_DEPS[@]}"; do
24 if ! dpkg-query -W -f='${Status}' "${dep}" | grep -q "install ok installed"; then
25 missing_deps+=("${dep}")
26 fi
27done
28
29# Print missing dependencies
30if ((${#missing_deps[@]} != 0 )); then
31 echo "Missing dependencies, please install:"
32 echo apt install "${missing_deps[@]}"
33 exit 1
34else
35 echo -e "\033[32mAll dependencies are already installed\033[0m"
36fi
37
Austin Schuh791ac752022-11-26 19:14:39 -080038export CC=aarch64-linux-gnu-
39
40# Reset any existing mounts.
41if mount | grep "${PARTITION}/boot" >/dev/null; then
42 sudo umount "${PARTITION}/boot"
43fi
44
45if mount | grep "${PARTITION}" >/dev/null; then
46 sudo umount "${PARTITION}"
47fi
48
henry19afbff2022-12-17 18:33:40 -080049LOOPBACK="$(sudo losetup --list | awk "/$IMAGE/"'{print $1}')"
Austin Schuh791ac752022-11-26 19:14:39 -080050if [[ -n "${LOOPBACK}" ]]; then
51 echo "Loop still exists..."
52 sudo losetup -d "${LOOPBACK}"
53fi
54
55# Build bl31.elf.
56if [[ ! -e arm-trusted-firmware ]]; then
57 git clone https://github.com/ARM-software/arm-trusted-firmware --depth=1
58fi
59
60pushd arm-trusted-firmware/
61git pull --ff-only
62#make CROSS_COMPILE="${CC}" realclean -j "$(nproc)"
63make CROSS_COMPILE="${CC}" PLAT=rk3399 -j "$(nproc)"
64popd
65
66export BL31="$(pwd)/arm-trusted-firmware/build/rk3399/release/bl31/bl31.elf"
67
68ls -lah "${BL31}"
69
70# Now, build uboot.
71if [[ ! -e u-boot ]]; then
Henry Speiser823acb32023-01-21 10:56:30 -080072 git clone -b "${UBOOT_VERSION}" https://github.com/u-boot/u-boot --depth=1
Austin Schuh791ac752022-11-26 19:14:39 -080073fi
74
75pushd u-boot/
76git fetch origin "${UBOOT_VERSION}"
77git reset --hard "${UBOOT_VERSION}"
78
79#make ARCH=arm CROSS_COMPILE="${CC}" distclean -j $(nproc)
80make ARCH=arm CROSS_COMPILE="${CC}" rock-pi-4-rk3399_defconfig -j "$(nproc)"
81make ARCH=arm CROSS_COMPILE="${CC}" -j "$(nproc)"
82echo "Made uboot"
83popd
84
henry19afbff2022-12-17 18:33:40 -080085function target_mkdir() {
86 target "install -d -m $2 -o $(echo $1 | sed 's/\.[^.]*//') -g $(echo $1 | sed 's/[^.]*\.//') $3"
87}
88
89function copyfile() {
90 sudo cp contents/$3 ${PARTITION}/$3
91 sudo chmod $2 ${PARTITION}/$3
92 target "chown $1 /$3"
93}
94
Austin Schuh791ac752022-11-26 19:14:39 -080095
96# Now build the base set of partitions.
97NEW_IMAGE=0
98if [[ ! -e "${IMAGE}" ]]; then
99 NEW_IMAGE=1
100 dd if=/dev/zero of="${IMAGE}" bs=1 count=0 seek=3G
101 dd if=./u-boot/idbloader.img of="${IMAGE}" seek=64 conv=notrunc
102 dd if=./u-boot/u-boot.itb of="${IMAGE}" seek=16384 conv=notrunc
103
104 sfdisk "${IMAGE}" <<-__EOF__
10516M,64M,L,*
10680M,,L,*
107__EOF__
108
109 sudo losetup -P -f "${IMAGE}"
110 LOOPBACK="$(sudo losetup --list | grep "${IMAGE}" | awk '{print $1}')"
111
112 sudo mkfs.fat "${LOOPBACK}p1"
113 sudo mkfs.ext4 -L rootfs "${LOOPBACK}p2"
114
115 sudo losetup -d "${LOOPBACK}"
116fi
117
118
119# Mount them
120mkdir -p "${PARTITION}"
121
122sudo losetup -P -f "${IMAGE}"
123LOOPBACK="$(sudo losetup --list | grep "${IMAGE}" | awk '{print $1}')"
124sudo mount "${LOOPBACK}p2" "${PARTITION}"
125if [[ ! -e "${PARTITION}/boot" ]]; then
126 sudo mkdir "${PARTITION}/boot"
127fi
128sudo mount "${LOOPBACK}p1" "${PARTITION}/boot"
129
130# Run the string command as root inside the target.
131function target() {
132 sudo chroot --userspec=root.root "${PARTITION}" qemu-aarch64-static \
133 /bin/bash -c "$1"
134}
135
136# Run the string command as the pi user inside the target.
137function pi_target() {
138 sudo chroot --userspec=pi.pi "${PARTITION}" qemu-aarch64-static \
139 /bin/bash -c "$1"
140}
141
142# And, if we made a new image, debootstrap to get things going.
143if (( NEW_IMAGE == 1 )); then
144 sudo debootstrap --arch=arm64 --no-check-gpg --foreign bullseye \
145 "${PARTITION}" http://deb.debian.org/debian/
146 sudo cp /usr/bin/qemu-aarch64-static "${PARTITION}/usr/bin/"
147
148 target "/debootstrap/debootstrap --second-stage"
149
150 target "useradd -m -p '\$y\$j9T\$85lzhdky63CTj.two7Zj20\$pVY53UR0VebErMlm8peyrEjmxeiRw/rfXfx..9.xet1' -s /bin/bash pi"
151
152else
153 sudo cp /usr/bin/qemu-aarch64-static "${PARTITION}/usr/bin/"
154fi
155
156# Install the kernel.
157sudo rm -rf "${PARTITION}/boot/dtbs/${KERNEL_VERSION}/"
158sudo rm -rf "${PARTITION}/lib/modules/${KERNEL_VERSION}/"
159sudo mkdir -p "${PARTITION}/lib/modules/"
160sudo tar --owner=0 --group=0 --no-same-owner --no-same-permissions -xvf \
161 "linux-kernel-${KERNEL_VERSION}.tar.xz" -C "${PARTITION}/boot/" \
162 --strip-components=2 ./boot/
163sudo tar --strip-components=3 -xvf "linux-kernel-${KERNEL_VERSION}.tar.xz" \
164 -C "${PARTITION}/lib/modules/" ./lib/modules
165
166# Now, configure it to start automatically.
167sudo mkdir -p ${PARTITION}/boot/extlinux/
168cat << __EOF__ | sudo tee "${PARTITION}/boot/extlinux/extlinux.conf"
169label Linux ${KERNEL_VERSION}
170 kernel /vmlinuz-${KERNEL_VERSION}
171 append earlycon=uart8250,mmio32,0xff1a0000 earlyprintk console=ttyS2,1500000n8 root=/dev/mmcblk0p2 ro rootfstype=ext4 rootwait
172 fdtdir /dtbs/${KERNEL_VERSION}/
173__EOF__
174
175copyfile root.root 644 etc/apt/sources.list.d/bullseye-backports.list
176copyfile root.root 644 etc/apt/sources.list.d/frc971.list
177
178target "apt-get update"
179target "apt-get -y install -t bullseye-backports systemd"
Austin Schuh6283b262022-12-31 23:11:15 -0800180target "apt-get -y install -t bullseye-backports bpfcc-tools"
Austin Schuh791ac752022-11-26 19:14:39 -0800181
Austin Schuhc5300f22023-01-03 21:20:44 -0800182target "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"
183target "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"
184
185target "apt-get clean"
Austin Schuh791ac752022-11-26 19:14:39 -0800186
187target "usermod -a -G sudo pi"
188target "usermod -a -G video pi"
Austin Schuh31960d02022-12-11 16:51:14 -0800189target "localedef -i en_US -f UTF-8 en_US.UTF-8"
Austin Schuh791ac752022-11-26 19:14:39 -0800190
Austin Schuh791ac752022-11-26 19:14:39 -0800191copyfile root.root 644 etc/fstab
192copyfile root.root 440 etc/sudoers
193copyfile root.root 444 etc/default/cpufrequtils
194target_mkdir root.root 755 etc/systemd/network
195copyfile root.root 644 etc/systemd/network/eth0.network
196target_mkdir pi.pi 755 home/pi/.ssh
197copyfile pi.pi 600 home/pi/.ssh/authorized_keys
198target_mkdir root.root 700 root/bin
199copyfile root.root 500 root/bin/grow.sh
200copyfile root.root 500 root/bin/change_hostname.sh
201copyfile root.root 500 root/bin/deploy_kernel.sh
henryebe91952023-01-17 22:39:20 -0800202copyfile root.root 500 root/bin/chrt.sh
Austin Schuh791ac752022-11-26 19:14:39 -0800203copyfile root.root 644 etc/systemd/system/grow-rootfs.service
204copyfile root.root 644 etc/sysctl.d/sctp.conf
205copyfile root.root 644 etc/systemd/logind.conf
Austin Schuh6283b262022-12-31 23:11:15 -0800206copyfile root.root 555 etc/bash_completion.d/aos_dump_autocomplete
207copyfile root.root 444 etc/modules-load.d/adis16505.conf
Austin Schuh791ac752022-11-26 19:14:39 -0800208copyfile root.root 644 etc/security/limits.d/rt.conf
209copyfile root.root 644 etc/systemd/system/frc971.service
210copyfile root.root 644 etc/systemd/system/frc971chrt.service
211copyfile root.root 644 etc/systemd/system/usb-mount@.service
212copyfile root.root 644 etc/udev/rules.d/99-usb-mount.rules
Austin Schuh6283b262022-12-31 23:11:15 -0800213copyfile root.root 644 etc/udev/rules.d/99-adis16505.rules
Austin Schuhc5300f22023-01-03 21:20:44 -0800214copyfile root.root 644 etc/udev/rules.d/99-mali.rules
Austin Schuh791ac752022-11-26 19:14:39 -0800215
216target "apt-get update"
217target "apt-get -y install -t bullseye-backports systemd"
218
219target "systemctl enable systemd-networkd"
220target "systemctl enable systemd-resolved"
221target "systemctl enable grow-rootfs"
222target "systemctl enable frc971"
223target "systemctl enable frc971chrt"
224target "/root/bin/change_hostname.sh pi-971-1"
225
226
227if [[ ! -e "${PARTITION}/home/pi/.dotfiles" ]]; then
228 pi_target "cd /home/pi/ && \
229 git clone --separate-git-dir=/home/pi/.dotfiles https://github.com/AustinSchuh/.dotfiles.git tmpdotfiles && \
230 rsync --recursive --verbose --exclude .git tmpdotfiles/ /home/pi/ && \
231 rm -r tmpdotfiles && \
232 git --git-dir=/home/pi/.dotfiles/ --work-tree=/home/pi/ config --local status.showUntrackedFiles no && \
233 vim -c \":qa!\""
234
235 target "cd /root/ && \
236 git clone --separate-git-dir=/root/.dotfiles https://github.com/AustinSchuh/.dotfiles.git tmpdotfiles && \
237 rsync --recursive --verbose --exclude .git tmpdotfiles/ /root/ && rm -r tmpdotfiles && \
238 git --git-dir=/root/.dotfiles/ --work-tree=/root/ config --local status.showUntrackedFiles no && \
239 vim -c \":qa!\""
240fi
241
242target "apt-get clean"
243
244sudo chroot ${PARTITION} qemu-aarch64-static /bin/bash
245
246# TODO(austin): This appears to not be working... pi_target doesn't apper to be happy
247sudo chroot --userspec=pi.pi ${PARTITION} qemu-aarch64-static /bin/bash
248
249# TODO(austin): everything else we were doing to the pi's.
250sudo rm ${PARTITION}/usr/bin/qemu-aarch64-static
251sudo umount ${PARTITION}/boot
252sudo umount ${PARTITION}
253rmdir ${PARTITION}