forked from puzzleos/stubby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stubby-smash.2.sh
executable file
·138 lines (121 loc) · 3.13 KB
/
stubby-smash.2.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/sh
#
# stubby smash script
#
# Copyright (c) 2020,2021 Cisco Systems, Inc. <[email protected]>
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
STUBBY="stubby.efi"
TEMP_D=""
usage() {
echo "usage: stubby-smash.2.sh -o <output>"
echo " -k <kernel> -i <initrd> -c <cmdline> -s <SBAT>"
echo ""
echo " Combine the <kernel>, <initrd>, <cmdline> and <SBAT> files"
echo " into a single bootable EFI app in <output>."
echo ""
exit 1
}
cleanup() {
if [ -d "${TEMP_D}" ]; then
rm -Rf "${TEMP_D}"
fi
}
error() {
echo "error:" "$@" 1>&2
exit 1
}
deps() {
while [ $# -ne 0 ]; do
command -v "$1" >/dev/null 2>&1 ||
error "please ensure \"$1\" is installed"
shift
done
}
assert_file() {
[ -f "$1" ] || error "$2 '$1' is not a file"
[ -r "$1" ] || error "$2 '$1' file is not readable"
}
# main
#
# dependency checks
deps objcopy
# argument parsing
arg_kernel=""
arg_initrd=""
arg_cmdline=""
arg_sbat=""
arg_output=""
while getopts ":o:k:i:c:s:" opt; do
case $opt in
o)
arg_output=$OPTARG
;;
k)
arg_kernel=$OPTARG
;;
i)
arg_initrd=$OPTARG
;;
c)
arg_cmdline=$OPTARG
;;
s)
arg_sbat=$OPTARG
;;
*)
usage
;;
esac
done
shift $(( $OPTIND - 1 ))
# sanity checks
assert_file "$STUBBY" "stubby efi file"
assert_file "$arg_kernel" "kernel argument"
assert_file "$arg_initrd" "initrd argument"
assert_file "$arg_cmdline" "cmdline argument"
assert_file "$arg_sbat" "sbat argument"
# output check
[ -n "$arg_output" ] || error "Must provide output option (-o FILE.efi)"
[ ! -e "$arg_output" ] ||
error "output file '$arg_output' already exists"
TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") ||
error "failed to make a temp dir"
trap cleanup EXIT
# adjust cmdline to not include trailing newline
cmdline="${TEMP_D}/cmdline"
lines=$(wc -l < "$arg_cmdline")
if [ "$lines" -eq 0 ]; then
# zero lines is "no trailing newline"
cmdline="$arg_cmdline"
elif [ "$lines" -eq 1 ]; then
# one line.. just a trailing newline, strip it.
tr -d '\n' < "$arg_cmdline" > "$cmdline" ||
error "Failed to remove newline from $arg_cmdline"
else
error "$arg_cmdline had $lines lines. Expected 0 or 1."
fi
exec objcopy \
"--add-section=.cmdline=$cmdline" \
"--change-section-vma=.cmdline=0x30000" \
"--add-section=.sbat=$arg_sbat" \
"--change-section-vma=.sbat=0x50000" \
"--set-section-alignment=.sbat=512" \
"--add-section=.linux=$arg_kernel" \
"--change-section-vma=.linux=0x1000000" \
"--add-section=.initrd=$arg_initrd" \
"--change-section-vma=.initrd=0x3000000" \
"$STUBBY" "$arg_output"