1# SPDX-License-Identifier: MIT
2#
3# Copyright (C) 2020 BayLibre SAS
4# Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
5#
6# This bbclass allows creating of dm-verity protected partition images. It
7# generates a device image file with dm-verity hash data appended at the end
8# plus the corresponding .env file containing additional information needed
9# to mount the image such as the root hash in the form of ell variables. To
10# assure data integrity, the root hash must be stored in a trusted location
11# or cryptographically signed and verified.
12#
13# Usage:
14#     DM_VERITY_IMAGE = "core-image-full-cmdline" # or other image
15#     DM_VERITY_IMAGE_TYPE = "ext4" # or ext2, ext3 & btrfs
16#     IMAGE_CLASSES += "dm-verity-img"
17#
18# The resulting image can then be used to implement the device mapper block
19# integrity checking on the target device.
20
21# Define the location where the DM_VERITY_IMAGE specific dm-verity root hash
22# is stored where it can be installed into associated initramfs rootfs.
23STAGING_VERITY_DIR ?= "${TMPDIR}/work-shared/${MACHINE}/dm-verity"
24
25# Process the output from veritysetup and generate the corresponding .env
26# file. The output from veritysetup is not very machine-friendly so we need to
27# convert it to some better format. Let's drop the first line (doesn't contain
28# any useful info) and feed the rest to a script.
29process_verity() {
30    local ENV="${STAGING_VERITY_DIR}/${IMAGE_BASENAME}.$TYPE.verity.env"
31    install -d ${STAGING_VERITY_DIR}
32    rm -f $ENV
33
34    # Each line contains a key and a value string delimited by ':'. Read the
35    # two parts into separate variables and process them separately. For the
36    # key part: convert the names to upper case and replace spaces with
37    # underscores to create correct shell variable names. For the value part:
38    # just trim all white-spaces.
39    IFS=":"
40    while read KEY VAL; do
41        printf '%s=%s\n' \
42            "$(echo "$KEY" | tr '[:lower:]' '[:upper:]' | sed 's/ /_/g')" \
43            "$(echo "$VAL" | tr -d ' \t')" >> $ENV
44    done
45
46    # Add partition size
47    echo "DATA_SIZE=$SIZE" >> $ENV
48}
49
50verity_setup() {
51    local TYPE=$1
52    local INPUT=${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.$TYPE
53    local SIZE=$(stat --printf="%s" $INPUT)
54    local OUTPUT=$INPUT.verity
55
56    cp -a $INPUT $OUTPUT
57
58    # Let's drop the first line of output (doesn't contain any useful info)
59    # and feed the rest to another function.
60    veritysetup --data-block-size=1024 --hash-offset=$SIZE format $OUTPUT $OUTPUT | tail -n +2 | process_verity
61}
62
63VERITY_TYPES = "ext2.verity ext3.verity ext4.verity btrfs.verity"
64IMAGE_TYPES += "${VERITY_TYPES}"
65CONVERSIONTYPES += "verity"
66CONVERSION_CMD_verity = "verity_setup ${type}"
67CONVERSION_DEPENDS_verity = "cryptsetup-native"
68
69python __anonymous() {
70    verity_image = d.getVar('DM_VERITY_IMAGE')
71    verity_type = d.getVar('DM_VERITY_IMAGE_TYPE')
72    image_fstypes = d.getVar('IMAGE_FSTYPES')
73    pn = d.getVar('PN')
74
75    if not verity_image or not verity_type:
76        bb.warn('dm-verity-img class inherited but not used')
77        return
78
79    if verity_image != pn:
80        return # This doesn't concern this image
81
82    if len(verity_type.split()) is not 1:
83        bb.fatal('DM_VERITY_IMAGE_TYPE must contain exactly one type')
84
85    d.appendVar('IMAGE_FSTYPES', ' %s.verity' % verity_type)
86
87    # If we're using wic: we'll have to use partition images and not the rootfs
88    # source plugin so add the appropriate dependency.
89    if 'wic' in image_fstypes:
90        dep = ' %s:do_image_%s' % (pn, verity_type)
91        d.appendVarFlag('do_image_wic', 'depends', dep)
92}
93