#!/bin/bash
# Build a small, keyless overlay only when the privileged restart helper asks.
build() {
    local bundle=${STARCHASER_FIRMWARE_BUNDLE:?No firmware seal was supplied}
    local tool file
    add_binary /usr/bin/sh
    add_binary /usr/bin/dd
    add_binary /usr/bin/cat
    add_binary /usr/bin/wc
    add_binary /usr/bin/mktemp
    add_binary /usr/bin/rm
    add_binary /usr/bin/timeout
    add_binary /usr/bin/cryptsetup
    for tool in createprimary load startauthsession policypcr policycountertimer policyauthvalue unseal pcrextend flushcontext; do
        add_binary "/usr/bin/tpm2_$tool" || return 1
    done
    # TCTIs are dlopened, so add_binary's ELF dependency walk cannot find them.
    add_binary /usr/lib/libtss2-tcti-device.so.0
    add_binary /usr/lib/libtss2-tctildr.so.0
    for file in uuid resets restarts public sealed; do
        add_file "$bundle/$file" "/etc/starchaser/firmware-seal/$file" 600 || return 1
    done
    add_file /usr/local/lib/starchaser/starchaser-firmware-unseal /usr/local/lib/starchaser/starchaser-firmware-unseal 755
    add_file /usr/lib/initcpio/starchaser-firmware-unlock.conf /etc/systemd/system/systemd-cryptsetup@cryptroot.service.d/85-starchaser-firmware.conf 644
}
help() {
    echo 'Adds a counter-bound TPM firmware handoff without a plaintext drive key.'
}
