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