-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign_image.sh
executable file
·113 lines (95 loc) · 2.89 KB
/
sign_image.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/sh
if [ -z "$FUTILITY" ]; then
FUTILITY="futility"
fi
if [ ! -x "$(command -v $FUTILITY)" ]; then
echo "futility not found (install vboot-kernel-utils), exitting..."
exit 1
fi
VBOOT_KEY_URL="https://chromium.googlesource.com/chromiumos/platform/vboot_reference/+/master/tests/devkeys"
VBOOT_PUBKEY="kernel_data_key.vbpubk"
VBOOT_PRIVKEY="kernel_data_key.vbprivk"
VBOOT_PUBKEY_URL="${VBOOT_KEY_URL}/${VBOOT_PUBKEY}?format=TEXT"
VBOOT_PRIVKEY_URL="${VBOOT_KEY_URL}/${VBOOT_PRIVKEY}?format=TEXT"
VBOOT_KEY_DIR="./signing_keys"
VBOOT_PUBKEYF="${VBOOT_KEY_DIR}/${VBOOT_PUBKEY}"
VBOOT_PRIVKEYF="${VBOOT_KEY_DIR}/${VBOOT_PRIVKEY}"
mkdir -p "$VBOOT_KEY_DIR"
if [ ! -f "$VBOOT_PUBKEYF" ] || [ ! -f "$VBOOT_PRIVKEYF" ]; then
rm -f "$VBOOT_PUBKEYF"
rm -f "$VBOOT_PRIVKEYF"
if [ ! -x "$(command -v wget)" ]; then
echo "Wget is not installed, exitting..."
exit 1
fi
# should be on most systems
if [ ! -x "$(command -v perl)" ]; then
echo "Perl is not installed, exitting..."
exit 1
fi
echo "Signing keys not found, downloading..."
wget "$VBOOT_PUBKEY_URL" -O "$VBOOT_PUBKEYF.base64"
if [ $? -eq 0 ]; then
wget "$VBOOT_PRIVKEY_URL" -O "$VBOOT_PRIVKEYF.base64"
fi
if [ $? -ne 0 ]; then
echo "Wget failed, exitting..."
exit 1
fi
cat "$VBOOT_PUBKEYF.base64" | \
perl -MMIME::Base64 -ne 'printf "%s\n",decode_base64($_)' > \
"$VBOOT_PUBKEYF"
cat "$VBOOT_PRIVKEYF.base64" | \
perl -MMIME::Base64 -ne 'printf "%s\n",decode_base64($_)' > \
"$VBOOT_PRIVKEYF"
rm -f "$VBOOT_PUBKEYF.base64"
rm -f "$VBOOT_PRIVKEYF.base64"
fi
if [ ! -f "$VBOOT_PUBKEYF" ] || [ ! -f "$VBOOT_PRIVKEYF" ]; then
echo "Signing keys not found, exitting..."
exit 1
fi
IMG_UNSIGNED="./boot.img.unsigned"
if [ -n "$1" ]; then
IMG_UNSIGNED="$1"
fi
if [ ! -f "$IMG_UNSIGNED" ]; then
echo "Image not found, exitting..."
exit 1
fi
OUTPUT="./boot.img"
if [ -n "$2" ]; then
OUTPUT="$2"
fi
EMPTY="$(mktemp ./tmp.XXXXXXXX)"
echo " " > "$EMPTY"
KEYBLOCK="${OUTPUT}.keyblock"
echo "Cleaning up old files..."
rm -f "$KEYBLOCK" "$OUTPUT"
if [ $? -ne 0 ]; then
echo "Cleanup failed, exitting..."
rm -f "$EMPTY"
exit 1
fi
echo "Signing..."
"$FUTILITY" vbutil_keyblock --pack "$KEYBLOCK" \
--datapubkey "$VBOOT_PUBKEYF" --signprivate "$VBOOT_PRIVKEYF"
if [ $? -ne 0 ]; then
echo "Error writing keyblock, exitting..."
rm -f "$EMPTY"
exit 1
fi
"$FUTILITY" vbutil_kernel --pack "$OUTPUT" \
--keyblock "$KEYBLOCK" --signprivate "$VBOOT_PRIVKEYF" --version 1 \
--vmlinuz "$IMG_UNSIGNED" --config "$EMPTY" --arch arm \
--bootloader "$EMPTY" --flags 0x1
if [ $? -ne 0 ]; then
echo "Signing failed, exitting..."
rm -f "$KEYBLOCK"
rm -f "$EMPTY"
exit 1
fi
echo "Cleaning up temporary files..."
rm -f "$EMPTY"
rm -f "$KEYBLOCK"
echo "Created ${OUTPUT}."