1#!/bin/sh
2
3dmverity_enabled() {
4    return 0
5}
6
7dmverity_run() {
8    DATA_SIZE="__not_set__"
9    DATA_BLOCK_SIZE="__not_set__"
10    ROOT_HASH="__not_set__"
11    SEPARATE_HASH="__not_set__"
12
13    . /usr/share/misc/dm-verity.env
14
15    C=0
16    delay=${bootparam_rootdelay:-1}
17    timeout=${bootparam_roottimeout:-5}
18
19    # we know exactly what we are looking for; don't need the wide hunt below
20    if [ "${SEPARATE_HASH}" -eq "1" ]; then
21        while [ ! -b "/dev/disk/by-partuuid/${ROOT_UUID}" ]; do
22            if [ $(( $C * $delay )) -gt $timeout ]; then
23                fatal "Root device (data) resolution failed"
24                exit 1
25            fi
26            debug "Sleeping for $delay second(s) to wait for root data to settle..."
27            sleep $delay
28            C=$(( $C + 1 ))
29        done
30
31        veritysetup \
32            --data-block-size=${DATA_BLOCK_SIZE} \
33            create rootfs \
34            /dev/disk/by-partuuid/${ROOT_UUID} \
35            /dev/disk/by-partuuid/${RHASH_UUID} \
36            ${ROOT_HASH}
37
38            mount \
39                 -o ro \
40                /dev/mapper/rootfs \
41                ${ROOTFS_DIR} || exit 2
42
43	    return
44    fi
45
46    RDEV="$(realpath /dev/disk/by-partuuid/${bootparam_root#PARTUUID=} 2>/dev/null)"
47    while [ ! -b "${RDEV}" ]; do
48        if [ $(( $C * $delay )) -gt $timeout ]; then
49            fatal "Root device resolution failed"
50            exit 1
51        fi
52
53        case "${bootparam_root}" in
54            ID=*)
55                RDEV="$(realpath /dev/disk/by-id/${bootparam_root#ID=} 2>/dev/null)"
56                ;;
57            LABEL=*)
58                RDEV="$(realpath /dev/disk/by-label/${bootparam_root#LABEL=} 2>/dev/null)"
59                ;;
60            PARTLABEL=*)
61                RDEV="$(realpath /dev/disk/by-partlabel/${bootparam_root#PARTLABEL=} 2>/dev/null)"
62                ;;
63            PARTUUID=*)
64                RDEV="$(realpath /dev/disk/by-partuuid/${bootparam_root#PARTUUID=} 2>/dev/null)"
65                ;;
66            PATH=*)
67                RDEV="$(realpath /dev/disk/by-path/${bootparam_root#PATH=} 2>/dev/null)"
68                ;;
69            UUID=*)
70                RDEV="$(realpath /dev/disk/by-uuid/${bootparam_root#UUID=} 2>/dev/null)"
71                ;;
72            *)
73                RDEV="${bootparam_root}"
74        esac
75        debug "Sleeping for $delay second(s) to wait root to settle..."
76        sleep $delay
77        C=$(( $C + 1 ))
78
79    done
80
81    veritysetup \
82        --data-block-size=${DATA_BLOCK_SIZE} \
83        --hash-offset=${DATA_SIZE} \
84        create rootfs \
85        ${RDEV} \
86        ${RDEV} \
87        ${ROOT_HASH}
88
89    mount \
90        -o ro \
91        /dev/mapper/rootfs \
92        ${ROOTFS_DIR} || exit 2
93}
94