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