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