#!/bin/sh
# systemd generator: $1 is the directory for generated units (see man systemd.generator).
# The boot partition number comes from the firmware: a 4-byte big-endian integer in the device tree.
PARTITION=$(od -An -tu4 --endian=big /proc/device-tree/chosen/bootloader/partition 2>/dev/null | tr -d ' ')
# Not started by the Pi firmware's partition switch (0, or missing): leave /boot/firmware unmounted
case "$PARTITION" in 2|3) ;; *) exit 0 ;; esac
cat > "$1/boot-firmware.mount" << UNIT
[Unit]
Description=Boot partition the firmware started from (A/B slot)
# Checked (fsck.vfat -a) before it is mounted: FAT has no journal, and a power cut while cmdline.txt or
# config.txt was written can leave it marked dirty or with lost clusters. Wants, not fstab's Requires: a
# check that fails still mounts it, as before (mark-good, RAUC and the settings need it)
Wants=systemd-fsck@dev-mmcblk0p$PARTITION.service
After=systemd-fsck@dev-mmcblk0p$PARTITION.service

[Mount]
What=/dev/mmcblk0p$PARTITION
Where=/boot/firmware
Type=vfat
Options=defaults,noatime
UNIT
mkdir -p "$1/local-fs.target.wants"
ln -sf ../boot-firmware.mount "$1/local-fs.target.wants/boot-firmware.mount"
