#!/usr/bin/ash
# mkinitcpio runtime hook: full-disk encryption of the root filesystem
# (docs/ROADMAP.md, encryption section). Runs in busybox ash.
#  - Root is LUKS2: ask for the password via unl0kr and open it; an interrupted
#    encryption is resumed on the way.
#  - Root is unencrypted and /var/lib/plinius-crypt/request exists (set by
#    plinius-encrypt): offer to set a new password or to continue without encryption
#    (withdraws the request); with a password, encrypt offline before mounting.
#  - Otherwise: do nothing, the system boots without a prompt.
# Progress, results and errors are shown full screen by plinius-progress; without a display
# only the console messages remain.

PLINIUS_CRYPT_NAME=plinius-root
PLINIUS_CRYPT_REQUEST=var/lib/plinius-crypt/request
# Room for the LUKS2 header at the start; the filesystem is shrunk by this amount first
PLINIUS_CRYPT_HEADER_MIB=32
PLINIUS_CRYPT_MNT=/plinius-crypt
# New password: this many attempts, then the system boots unencrypted (the request stays)
PLINIUS_CRYPT_ROUNDS=5
# Exit code of the command run by with_progress (a pipeline only returns the last one's)
PLINIUS_CRYPT_RC=/run/plinius-crypt/rc
# Rates a new password (plinius-security), if the install hook found it
PLINIUS_PWRATE=/usr/lib/plinius-security/plinius-pwrate
# cryptsetup options for the progress view: one JSON line per second on stdout
PLINIUS_CRYPT_PROGRESS="--progress-json --progress-frequency 1"

# Translations of the prompts for the system language, chosen when the initramfs is built
# (install/plinius-crypt): one line per message, English text TAB translation
PLINIUS_CRYPT_L10N=/etc/plinius-crypt.l10n

# Translated text for message $1, or $1 itself if there is no translation
tr_msg() {
    local t
    t=$(awk -F '\t' -v k="$1" '$1 == k { print $2; exit }' "$PLINIUS_CRYPT_L10N" 2>/dev/null)
    printf '%s' "${t:-$1}"
}

# Prompt on the console in case unl0kr does not run (no display, no input device)
console_pass() {
    local pass
    printf '%s: ' "$1" > /dev/console
    stty -echo < /dev/console 2>/dev/null
    read -r pass < /dev/console
    stty echo < /dev/console 2>/dev/null
    printf '\n' > /dev/console
    [ -n "$pass" ] && printf '%s' "$pass"
}

# Ask for a password with prompt $1 via unl0kr, falling back to the console; prints it on stdout.
# Optional $2: hint shown above the prompt, e.g. why the password is asked again.
# Optional $3: note shown below the prompt, right above the field (already translated; not logged).
ask_pass() {
    # Log the prompt to the console (test scripts wait for it, image/test-crypt.sh). To stderr:
    # ask_pass runs in $(...), stdout carries only the password.
    # The log keeps the English text; the user sees the translation.
    local text
    [ -n "$2" ] && msg ":: plinius-crypt: $2" >&2
    msg ":: plinius-crypt: $1" >&2
    text=$(tr_msg "$1")
    [ -n "$2" ] && text="$(tr_msg "$2")

$text"
    [ -n "$3" ] && text="$text
$3"
    # All texts of unl0kr in the system language: message, placeholder of the password field and
    # the confirmation of its shutdown button. That title stays short: unl0kr's dialog is 400 px
    # wide with a header of fixed height, a second line would be cut off.
    unl0kr -n -m "$text" -p "$(tr_msg "Enter password...")" \
        --shutdown-title "$(tr_msg "Switch off?")" \
        --shutdown-yes "$(tr_msg "Yes")" --shutdown-no "$(tr_msg "No")" \
        2>/dev/null && return 0
    # Without a pause the caller's loop would spin
    sleep 1
    console_pass "$text"
}

# Set the hint for the next ask_pass in the calling function's local "hint" (ash scopes locals
# dynamically). A function rather than an assignment so that xgettext finds the texts.
set_hint() {
    hint=$1
}

# Show title $1 and explanation $2 full screen until the user taps the button, at most
# $3 seconds; icon $4 (warning, ok). Texts in English, translated here. Always succeeds:
# without a display the console message is all there is.
show_message() {
    msg ":: plinius-crypt: $1"
    plinius-progress -i "$4" -t "$(tr_msg "$1")" -m "$(tr_msg "$2")" \
        -b "$(tr_msg "Continue")" -T "$3" 2>/dev/null || :
}

# Run command "$@" (stdin passed through) and show the cryptsetup progress it prints as JSON
# on stdout full screen under title $1. Returns the command's exit code. If the view cannot
# start, the progress lines are discarded so the command never blocks on a full pipe.
with_progress() {
    local title rc
    title=$(tr_msg "$1")
    shift
    { "$@"; echo $? > "$PLINIUS_CRYPT_RC"; } |
        { plinius-progress -p -t "$title" \
            -m "$(tr_msg "This takes a few minutes. Do not switch off the device.")" \
            -r "$(tr_msg "about %s min left")" 2>/dev/null || cat > /dev/null; }
    read -r rc < "$PLINIUS_CRYPT_RC"
    return "${rc:-1}"
}

# Size of block device $1 in bytes
device_bytes() {
    local name
    name=$(basename "$(readlink -f "$1")")
    echo $(( $(cat "/sys/class/block/$name/size") * 512 ))
}

# rate_password PASSWORD FILE: only the rating of PASSWORD (no secret), for GNOME Settings
# (Device Security). /run is carried over into the system, where plinius-crypt-strength.service
# keeps it in /var/lib/plinius-crypt: /run/plinius-crypt/strength (a new password) always,
# /run/plinius-crypt/strength-unlock (the password just typed to unlock) only if no rating is
# kept yet, e.g. on a device encrypted by a ramdisk without plinius-pwrate. The rating here has
# no dictionary check (no cracklib), so it never replaces one made in the system.
rate_password() {
    [ -x "$PLINIUS_PWRATE" ] || return 0
    printf '%s\n' "$1" | "$PLINIUS_PWRATE" > "$2" 2>/dev/null || rm -f "$2"
}

# Unlock LUKS device $1 (resuming an interrupted reencryption first); retries until the
# password is right and sets root= to the mapper device
open_luks() {
    local dev=$1 hint="" pass
    while :; do
        pass=$(ask_pass "Password to unlock" "$hint" "$PLINIUS_BOOTCHECK_NOTE") || continue
        set_hint "Wrong password."
        printf '%s' "$pass" | cryptsetup open --test-passphrase --key-file - "$dev" || continue
        if cryptsetup luksDump "$dev" | grep -q reencrypt; then
            msg ":: Resuming the interrupted encryption. Do not power off."
            # shellcheck disable=SC2086
            if ! printf '%s' "$pass" | with_progress "Resuming the encryption" \
                cryptsetup reencrypt --resume-only $PLINIUS_CRYPT_PROGRESS --key-file - "$dev"; then
                set_hint "The encryption could not be resumed."
                continue
            fi
        fi
        if printf '%s' "$pass" | cryptsetup open --key-file - "$dev" "$PLINIUS_CRYPT_NAME"; then
            root=/dev/mapper/$PLINIUS_CRYPT_NAME
            rate_password "$pass" /run/plinius-crypt/strength-unlock
            return 0
        fi
    done
}

# True if the unencrypted root $1 carries the encryption request from plinius-encrypt
encrypt_requested() {
    local dev=$1 found=1
    mkdir -p "$PLINIUS_CRYPT_MNT"
    mount -o ro "$dev" "$PLINIUS_CRYPT_MNT" 2>/dev/null || return 1
    [ -e "$PLINIUS_CRYPT_MNT/$PLINIUS_CRYPT_REQUEST" ] && found=0
    umount "$PLINIUS_CRYPT_MNT"
    return $found
}

# True if the request on device $1 comes from the image of a freshly installed device: it says
# "fresh" (Plinius Boot writes it at the installation). A request from plinius-encrypt never does.
request_fresh() {
    local dev=$1 found=1
    mkdir -p "$PLINIUS_CRYPT_MNT"
    mount -o ro "$dev" "$PLINIUS_CRYPT_MNT" 2>/dev/null || return 1
    grep -qx fresh "$PLINIUS_CRYPT_MNT/$PLINIUS_CRYPT_REQUEST" 2>/dev/null && found=0
    umount "$PLINIUS_CRYPT_MNT"
    return $found
}

# Remove the encryption request from the root filesystem on device $1
remove_request() {
    mount "$1" "$PLINIUS_CRYPT_MNT" && rm -f "$PLINIUS_CRYPT_MNT/$PLINIUS_CRYPT_REQUEST"
    umount "$PLINIUS_CRYPT_MNT"
}

# Offer the choice between setting a password and continuing without encryption, in the look of
# the system's first PIN prompt (plinius-progress -F).
# Returns 0 for "set password", 2 for "without encryption" (the request on $1 is then removed)
# and 1 if the choice cannot be shown (no display: go on with the console prompt).
choose_encryption() {
    local rc
    msg ":: plinius-crypt: Encrypt the device?"
    plinius-progress -F -t "$(tr_msg "Encrypt the device?")" -m "$(tr_msg "Encryption was requested for this device. You choose a password that is asked at every start and should differ from the screen lock PIN. Encrypting then takes a few minutes.")" \
        -b "$(tr_msg "Set password")" -B "$(tr_msg "Continue without encryption")" 2>/dev/null
    rc=$?
    case $rc in
        0) return 0 ;;
        2)
            msg ":: plinius-crypt: Continue without encryption"
            remove_request "$1"
            return 2
            ;;
        *) return 1 ;;
    esac
}

# Shrink the filesystem on $1 by the header size and encrypt it in place, password on stdin.
# With $2 = fresh (a request from the image on a freshly installed device) only the part in use is
# encrypted: the filesystem shrinks to its smallest size, the rest of the device is discarded and
# left out (cryptsetup --device-size), and the filesystem grows again once the device is open
# (grow_fresh). Seconds instead of minutes on a device that holds little more than the system;
# what was there before behind the filesystem is discarded, not encrypted (docs/SICHERHEIT.md).
# Measured in a VM on 08.11.: the opened device spans the whole partition, the files stay intact.
# Runs inside with_progress: stdout carries only cryptsetup's progress, all else goes to stderr.
encrypt_device() {
    local dev=$1 fresh=$2 newk size rc
    e2fsck -f -p "$dev" < /dev/null >&2
    rc=$?
    [ $rc -le 1 ] || { err "e2fsck reported errors ($rc), not encrypting" >&2; return 1; }
    if [ "$fresh" = fresh ]; then
        # "The filesystem on X is now N (4k) blocks long." -> bytes
        size=$(resize2fs -M "$dev" < /dev/null 2>&1 |
            sed -n 's/.* is now \([0-9]*\) (\([0-9]*\)k) blocks long.*/\1 \2/p')
        [ -n "$size" ] || { err "resize2fs -M failed, not encrypting" >&2; return 1; }
        size=$(( ${size% *} * ${size#* } * 1024 ))
        msg ":: Encrypting the first $((size / 1048576)) MiB in use" >&2
        blkdiscard -o "$size" "$dev" >&2 || msg ":: The rest of the device could not be discarded" >&2
        # shellcheck disable=SC2086
        cryptsetup reencrypt --encrypt --type luks2 \
            --reduce-device-size "${PLINIUS_CRYPT_HEADER_MIB}M" \
            --device-size $((size + PLINIUS_CRYPT_HEADER_MIB * 1048576)) $PLINIUS_CRYPT_PROGRESS \
            --batch-mode --key-file - "$dev"
        return
    fi
    newk=$(( $(device_bytes "$dev") / 1024 - PLINIUS_CRYPT_HEADER_MIB * 1024 ))
    resize2fs "$dev" "${newk}K" < /dev/null >&2 || return 1
    # shellcheck disable=SC2086
    cryptsetup reencrypt --encrypt --type luks2 \
        --reduce-device-size "${PLINIUS_CRYPT_HEADER_MIB}M" $PLINIUS_CRYPT_PROGRESS \
        --batch-mode --key-file - "$dev"
}

# After a fresh encryption (encrypt_device): grow the filesystem on the opened device $1 over all of
# it again. Should that fail, the system still starts, with a smaller data partition.
grow_fresh() {
    resize2fs "$1" < /dev/null > /dev/null 2>&1 || err "the file system could not grow to the whole device"
}

# The look while encryption is set up: black, without the background picture, like the first
# choice (plinius-progress -F). A file of its own in unl0kr's configuration, only in RAM and only
# until encrypt_now ends; the red one of the boot chain check (90-) still wins.
PLINIUS_CRYPT_BLACK_CONF=/etc/unl0kr.conf.d/80-plinius-encrypt.conf
PLINIUS_CRYPT_BLACK=/run/plinius-crypt/black-background.bin

# Offer encryption and, if chosen, ask for a new password twice and encrypt $1 in place; on
# success root= points to the opened mapper device and the request is removed. Shows the
# outcome on screen, all of it on black. Fails if the device stays unencrypted.
encrypt_now() {
    local rc
    # A 1x1 XRGB8888 LVGL image (glass-background.py) of #000000, which unl0kr scales up
    printf '\031\021\000\000\001\000\001\000\004\000\000\000\000\000\000\377' > "$PLINIUS_CRYPT_BLACK"
    mkdir -p "${PLINIUS_CRYPT_BLACK_CONF%/*}"
    printf '[theme]\nbackground=%s\n' "$PLINIUS_CRYPT_BLACK" > "$PLINIUS_CRYPT_BLACK_CONF"
    encrypt_steps "$@"
    rc=$?
    rm -f "$PLINIUS_CRYPT_BLACK_CONF" "$PLINIUS_CRYPT_BLACK"
    return $rc
}

encrypt_steps() {
    local dev=$1 pass pass2 rounds=0 hint="" fresh=""
    request_fresh "$dev" && fresh=fresh
    choose_encryption "$dev"
    [ $? -eq 2 ] && return 1
    while :; do
        rounds=$((rounds + 1))
        if [ $rounds -gt $PLINIUS_CRYPT_ROUNDS ]; then
            err "No new password, the system boots unencrypted"
            show_message "Encryption not set up" \
                "No matching password was entered. The system starts unencrypted and asks again at the next start." \
                120 warning
            return 1
        fi
        pass=$(ask_pass "Set up encryption: new password" "$hint" \
            "$(tr_msg "Leave empty to go back.")")
        if [ -z "$pass" ]; then
            # An empty password leads back to the choice; choosing "set password" there
            # starts afresh (without a display this round still counts)
            choose_encryption "$dev"
            case $? in
                0) rounds=0 ;;
                2) return 1 ;;
            esac
            hint=""
            continue
        fi
        pass2=$(ask_pass "Repeat new password")
        [ "$pass" = "$pass2" ] && break
        set_hint "The passwords do not match. Please enter them again."
    done

    msg ":: Encrypting $dev. Depending on its size this takes a few minutes. Do not power off."
    if ! printf '%s' "$pass" | with_progress "Encrypting the device" encrypt_device "$dev" "$fresh"; then
        err "Encryption failed"
        # A LUKS header already in place means reencryption is in progress; it resumes
        # at the next unlock (run_hook). Otherwise the system boots unencrypted.
        if cryptsetup isLuks "$dev"; then
            show_message "Encryption interrupted" \
                "Enter the password to continue the encryption." 120 warning
        else
            show_message "Encryption failed" \
                "Your data are unchanged and the system starts unencrypted. Encryption is tried again at the next start." \
                120 warning
        fi
        return 1
    fi
    printf '%s' "$pass" | cryptsetup open --key-file - "$dev" "$PLINIUS_CRYPT_NAME" || return 1
    root=/dev/mapper/$PLINIUS_CRYPT_NAME
    [ -z "$fresh" ] || grow_fresh "$root"
    rate_password "$pass" /run/plinius-crypt/strength

    remove_request "$root"
    msg ":: Encryption complete."
    show_message "Encryption complete" \
        "The device is now encrypted. From now on it asks for this password at every start." \
        10 ok
}

# --- Boot chain before the password (docs/PLAN-VERSCHLUESSELUNG.md, "Bootcheck in der Abfrage") ---
# plinius-bootcheck keeps a copy of its reference in the LUKS2 header, as token
# PLINIUS_BOOTCHECK_TOKEN: {"type":"plinius-bootcheck","keyslots":[],"card":DEVICE,
# "entries":["HASH PATH",...]}, the partitions of the boot chain and the files "abm:/FILE" of the boot
# menu card DEVICE with their SHA-256 ("-" for a missing one). Before the password is asked for, they
# are compared with what is there now.
# Only ever a warning, never an all-clear: the check runs in the very ramdisk it would have to judge,
# and whoever swaps the ramdisk swaps the check with it. So nothing is shown while all is as accepted.
# The token can be forged without the password; plinius-bootcheck compares it with its encrypted
# reference after the start and also notices a start without the mark this check leaves.
PLINIUS_BOOTCHECK_TOKEN=31
PLINIUS_BOOTCHECK_DIR=/run/plinius-crypt/bootcheck
PLINIUS_BOOTCHECK_MARK=/run/plinius-crypt/prompt-check
PLINIUS_BOOTCHECK_CARD=$PLINIUS_CRYPT_MNT-card
# The look of the prompt while a change is not accepted, for this start only: red accent and background
PLINIUS_BOOTCHECK_ALERT=/etc/unl0kr.conf.d/90-plinius-alert.conf
PLINIUS_BOOTCHECK_RED=/run/plinius-crypt/alert-background.bin
# The warning waits this many seconds for an answer, then the (red) prompt comes
PLINIUS_BOOTCHECK_WAIT=300
# Shown below the password field after a warning (translated by bootcheck_warn)
PLINIUS_BOOTCHECK_NOTE=

# The entries of the token on stdin, one "HASH PATH" per line; only well-formed ones
bootcheck_entries() {
    sed -n 's/.*"entries": *\[\([^]]*\)\].*/\1/p' | tr ',' '\n' |
        awk '{ gsub(/^ *"|" *$/, "") }
            NF == 2 && ($1 == "-" || ($1 ~ /^[0-9a-f]+$/ && length($1) == 64)) &&
            $2 ~ /^[A-Za-z0-9_.\/:@+-]+$/ && $2 !~ /\/\.\.(\/|$)/'
}

# Text $1 with its "%s" replaced by $2 (not printf: a translation may hold another "%")
bootcheck_fill() {
    awk -v t="$1" -v s="$2" 'BEGIN { i = index(t, "%s"); print (i ? substr(t, 1, i - 1) s substr(t, i + 2) : t) }'
}

# The name of checked part $1 for the user, as GNOME Settings shows it (cc-plinius-security.c)
bootcheck_name() {
    local label part
    case $1 in
        abm:/db/entries/*.conf)
            label=${1#abm:/db/entries/}
            bootcheck_fill "$(tr_msg "Boot menu entry “%s”")" "${label%.conf}"
            return ;;
        abm:*)
            bootcheck_fill "$(tr_msg "Boot menu card: %s")" "${1#abm:}"
            return ;;
    esac
    label=${1##*/}
    case ${label%_[ab]} in
        lk) part=$(tr_msg "Bootloader") ;;
        boot) part=$(tr_msg "Kernel") ;;
        init_boot) part=$(tr_msg "Ramdisk") ;;
        vendor_boot) part=$(tr_msg "Vendor Ramdisk") ;;
        dtbo) part=$(tr_msg "Device Tree") ;;
        vbmeta | vbmeta_system | vbmeta_vendor) part=$(tr_msg "Boot Verification Data") ;;
        *) printf '%s\n' "$1"; return ;;
    esac
    printf '%s (%s)\n' "$part" "$label"
}

# Hash what the token in PLINIUS_BOOTCHECK_DIR names; the paths that differ go to "changed" there,
# partitions this ramdisk does not see to "unseen" (they are left to the system's check)
bootcheck_hash() {
    local d=$PLINIUS_BOOTCHECK_DIR card mnt="" want path f now
    card=$(sed -n 's/.*"card": *"\([A-Za-z0-9_.\/@+-]*\)".*/\1/p' "$d/token")
    bootcheck_entries < "$d/token" > "$d/entries"
    : > "$d/changed.new"
    if [ -n "$card" ] && [ -b "$card" ] && grep -q ' abm:/' "$d/entries"; then
        mkdir -p "$PLINIUS_BOOTCHECK_CARD"
        # noload: no journal replay, nothing is written to the card
        mount -t ext4 -o ro,noload "$card" "$PLINIUS_BOOTCHECK_CARD" 2>/dev/null &&
            mnt=$PLINIUS_BOOTCHECK_CARD
    fi
    while read -r want path; do
        case $path in
            abm:/*)
                # Without the card its entries keep their reference, as in plinius-bootcheck
                [ -n "$mnt" ] || continue
                f=$mnt/${path#abm:/}
                [ -f "$f" ] || f=
                ;;
            /dev/disk/by-partlabel/*)
                if [ ! -e "$path" ]; then
                    [ "$want" = - ] || echo "$path" >> "$d/unseen"
                    continue
                fi
                f=$path
                ;;
            # Nothing else is read here (a forged token could name /dev/zero)
            *) continue ;;
        esac
        now=-
        [ -z "$f" ] || now=$(sha256sum < "$f" | cut -d' ' -f1)
        [ "$now" = "$want" ] || echo "$path" >> "$d/changed.new"
    done < "$d/entries"
    [ -z "$mnt" ] || umount "$mnt"
    mv "$d/changed.new" "$d/changed"
}

# Start the check of LUKS2 device $1 in the background, once: the display comes up meanwhile
bootcheck_begin() {
    local d=$PLINIUS_BOOTCHECK_DIR
    [ -d "$d" ] && return 0
    mkdir -p "$d"
    if ! cryptsetup token export --token-id "$PLINIUS_BOOTCHECK_TOKEN" "$1" > "$d/token" 2>/dev/null ||
        ! grep -q '"type": *"plinius-bootcheck"' "$d/token"; then
        msg "boot chain: no reference in the LUKS2 header"
        rm -f "$d/token"
        return 0
    fi
    bootcheck_hash &
    echo $! > "$d/pid"
}

# The mark for plinius-bootcheck in the system: this prompt ran, with result $1
bootcheck_mark() {
    printf '%s\n' "$1" > "$PLINIUS_BOOTCHECK_MARK"
}

# The red look for the rest of this start (the prompt, and the boot logo after it); only in RAM
bootcheck_alert() {
    # A 1x1 XRGB8888 LVGL image (glass-background.py) of #c01c28, which unl0kr scales up
    printf '\031\021\000\000\001\000\001\000\004\000\000\000\050\034\300\377' > "$PLINIUS_BOOTCHECK_RED"
    mkdir -p "${PLINIUS_BOOTCHECK_ALERT%/*}"
    printf '[theme]\naccent=#c01c28\nbackground=%s\n' "$PLINIUS_BOOTCHECK_RED" > "$PLINIUS_BOOTCHECK_ALERT"
}

# Wait for the check of bootcheck_begin and warn full screen if the boot chain differs from the
# reference: go on to the password, or switch off. Leaves the mark either way.
bootcheck_warn() {
    local d=$PLINIUS_BOOTCHECK_DIR names
    if [ ! -s "$d/pid" ]; then
        bootcheck_mark "no reference"
        return 0
    fi
    wait "$(cat "$d/pid")" 2>/dev/null
    rm -f "$d/pid"
    [ ! -s "$d/unseen" ] || msg "boot chain: not found here, left to the system: $(tr '\n' ' ' < "$d/unseen")"
    if [ ! -f "$d/changed" ]; then
        err "boot chain: the check did not finish"
        bootcheck_mark failed
        return 0
    fi
    if [ ! -s "$d/changed" ]; then
        msg "boot chain: unchanged"
        bootcheck_mark unchanged
        return 0
    fi
    msg "boot chain: CHANGED since the last accepted state: $(tr '\n' ' ' < "$d/changed")"
    bootcheck_mark changed
    bootcheck_alert
    PLINIUS_BOOTCHECK_NOTE=$(tr_msg "Warning: the start system was changed.")
    names=$(while read -r p; do bootcheck_name "$p"; done < "$d/changed" |
        awk '{ printf "%s%s", (NR > 1 ? ", " : ""), $0 }')
    msg ":: plinius-crypt: Start system changed"
    plinius-progress -i warning -t "$(tr_msg "Start system changed")" \
        -m "$(bootcheck_fill "$(tr_msg "Since the last accepted state, these parts of the start system were changed: %s. If you did not change them yourself, someone may have tampered with the phone to capture this password. Only enter it if you know where the change comes from.")" "$names")" \
        -b "$(tr_msg "Enter password anyway")" -B "$(tr_msg "Switch off")" \
        -T "$PLINIUS_BOOTCHECK_WAIT" 2>/dev/null
    if [ $? -eq 2 ]; then
        msg ":: plinius-crypt: Switch off"
        sync
        poweroff -f
        err "could not switch off"
    fi
    return 0
}

# Entry point called by the busybox init of mkinitcpio
run_hook() {
    local dev
    # Marker for the running system (mkinitcpio carries /run over): plinius-encrypt only
    # files a request if this initramfs really runs the hook
    mkdir -p /run/plinius-crypt
    modprobe -a -q dm-crypt >/dev/null 2>&1
    dev=$(resolve_device "$root") || return 0

    if cryptsetup isLuks "$dev"; then
        bootcheck_begin "$dev"
        bootcheck_warn
        open_luks "$dev"
    elif encrypt_requested "$dev"; then
        # On failure with the LUKS header already written, the encryption resumes on unlock
        encrypt_now "$dev" || { cryptsetup isLuks "$dev" && open_luks "$dev"; }
    fi
}

# vim: set ft=sh ts=4 sw=4 et:
