1#!/bin/bash
2
3if [ $# -ne 3 ]; then
4  echo "Usage: mk_verified_boot_params.sh <vbmeta.img> <system-qemu.img> <VerifiedBootParams.textproto>"
5#when building vendor.img only, this is expected
6  exit 0
7fi
8
9# Example Output from 'avbtool calculate_kernel_cmdline --image vbmeta.img':
10# (actual output is on a single line)
11#
12# dm="1 vroot none ro 1,0 4666872 verity 1 \
13#     PARTUUID=$(ANDROID_SYSTEM_PARTUUID) \
14#     PARTUUID=$(ANDROID_SYSTEM_PARTUUID) \
15#     4096 4096 583359 583359 sha1 \
16#     d3462e6b89750e3f6bca242551bc5ded22843c8f \
17#     930e57fa675c2e9f4b8bbc960ee165cbabbe651c \
18#     10 $(ANDROID_VERITY_MODE) ignore_zero_blocks \
19#     use_fec_from_device PARTUUID=$(ANDROID_SYSTEM_PARTUUID) \
20#     fec_roots 2 fec_blocks 587954 fec_start 587954" \
21# root=/dev/dm-0
22#
23# The emulator can not use every parameter (for example fec args require a
24# minimum kernel level).  Some parameters must also be substituted.  Therefore
25# this script selects arguments from the tool's output to build the actual
26# kernel commandline as a textproto file.
27
28set -e
29
30function die {
31  echo $1 >&2
32  echo "tools/mk_verified_boot_kernel_options.sh might need a fix"
33  exit 1
34}
35
36# Incrementing major version causes emulator binaries that do not support the
37# version to ignore this file.  This can be useful if there is a change
38# not supported by older emulator binaries.
39readonly MAJOR_VERSION=1
40
41readonly SRCIMG=$1
42readonly QEMU_IMG=$2
43readonly TARGET=$3
44
45# Use sgdisk to determine the partition UUID
46[[ $(${SGDISK:-sgdisk} --info 1 $QEMU_IMG | grep "Partition name:" | awk '{print $3}') == "'system'" ]] || die "Partition 1 is not named 'system'."
47readonly GUID=$(${SGDISK:-sgdisk} --info 1 $QEMU_IMG | grep "Partition unique GUID:" | awk '{print $4}')
48[[ $GUID =~ [[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12} ]] || die "GUID looks incorrect: $GUID"
49
50# Extract the commandline
51readonly CMDLINE=$(${AVBTOOL:-avbtool} calculate_kernel_cmdline --image $SRCIMG)
52
53# Extracts params from CMDLINE to create a commandline usable by the emulator.
54#
55# TODO: fec options do not work yet because they require a kernel of >=4.5.
56# The emulator is running a 4.4 kernel.  This script ignores options
57# for now...
58
59dm_match_regex="dm=\"([^\"]*)\""
60[[ "$CMDLINE" =~ $dm_match_regex ]]
61
62[[ ${#BASH_REMATCH[*]} -eq 2 ]] || die "Missing dm section: $CMDLINE"
63
64readonly DM_SECTION=${BASH_REMATCH[1]}
65readonly DM_SPLIT=($(echo $DM_SECTION | tr ' ' '\n'))
66
67# Capture everything into a named variable
68readonly START_BLOCK=0
69readonly SECTOR_COUNT=${DM_SPLIT[5]}
70readonly VERITY_VERSION=${DM_SPLIT[7]}
71readonly DATA_DEVICE="PARTUUID=$GUID"
72readonly HASH_DEVICE="PARTUUID=$GUID"
73readonly DATA_BLOCK_SIZE=${DM_SPLIT[10]}
74readonly HASH_BLOCK_SIZE=${DM_SPLIT[11]}
75readonly NUM_BLOCKS=${DM_SPLIT[12]}
76readonly HASH_BLOCK_OFFSET=${DM_SPLIT[13]}
77readonly HASH_ALGORITHM=${DM_SPLIT[14]}
78readonly ROOT_DIGEST=${DM_SPLIT[15]}
79readonly SALT=${DM_SPLIT[16]}
80readonly NUM_OPTIONAL_PARAMS=1
81
82# Validity Checks
83[[ $ROOT_DIGEST =~ [[:xdigit:]]{40} ]] || die "ROOT_DIGEST looks incorrect: $ROOT_DIGEST"
84[[ $SALT =~ [[:xdigit:]]{40} ]] || die "SALT looks incorrect: $SALT"
85
86HEADER_COMMENT="# dm=\"1 vroot none ro 1,$START_BLOCK $SECTOR_COUNT verity $VERITY_VERSION $DATA_DEVICE $HASH_DEVICE $DATA_BLOCK_SIZE $HASH_BLOCK_SIZE $NUM_BLOCKS $HASH_BLOCK_OFFSET $HASH_ALGORITHM $ROOT_DIGEST $SALT $NUM_OPTIONAL_PARAMS ignore_zero_blocks\" androidboot.veritymode=enforcing root=/dev/dm-0"
87
88echo $HEADER_COMMENT > $TARGET
89echo "major_version: $MAJOR_VERSION" >> $TARGET
90echo "dm_param: \"1\"" >> $TARGET
91echo "dm_param: \"vroot\"  # name" >> $TARGET
92echo "dm_param: \"none\"  # UUID" >> $TARGET
93echo "dm_param: \"ro\"  # Read-only" >> $TARGET
94echo "dm_param: \"1,$START_BLOCK\"  # Start block" >> $TARGET
95echo "dm_param: \"$SECTOR_COUNT\"  # Sector count" >> $TARGET
96echo "dm_param: \"verity\"  # Type" >> $TARGET
97echo "dm_param: \"$VERITY_VERSION\"  # Version" >> $TARGET
98echo "dm_param: \"$DATA_DEVICE\"  # Data device" >> $TARGET
99echo "dm_param: \"$HASH_DEVICE\"  # Hash device" >> $TARGET
100echo "dm_param: \"$DATA_BLOCK_SIZE\"  # Data block size" >> $TARGET
101echo "dm_param: \"$HASH_BLOCK_SIZE\"  # Hash block size" >> $TARGET
102echo "dm_param: \"$NUM_BLOCKS\"  # Number of blocks" >> $TARGET
103echo "dm_param: \"$HASH_BLOCK_OFFSET\"  # Hash block offset" >> $TARGET
104echo "dm_param: \"$HASH_ALGORITHM\"  # Hash algorithm" >> $TARGET
105echo "dm_param: \"$ROOT_DIGEST\"  # Root digest" >> $TARGET
106echo "dm_param: \"$SALT\"  # Salt" >> $TARGET
107echo "dm_param: \"$NUM_OPTIONAL_PARAMS\"  # Num optional params" >> $TARGET
108echo "dm_param: \"ignore_zero_blocks\"" >> $TARGET
109
110echo "param: \"androidboot.veritymode=enforcing\"" >> $TARGET
111echo "param: \"androidboot.verifiedbootstate=orange\"" >> $TARGET
112echo "param: \"root=/dev/dm-0\"" >> $TARGET
113
114
115
116