-
Notifications
You must be signed in to change notification settings - Fork 8
/
stubby-smash.1.sh
executable file
·89 lines (75 loc) · 2.06 KB
/
stubby-smash.1.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
#!/bin/sh
#
# stubby smash script
#
# Copyright (c) 2020 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"
usage() {
echo "usage: stubby-smash.1.sh <kernel> <initrd> <cmdline> <output>"
echo ""
echo " Combine the <kernel>, <initrd>, and <cmdline> files into a"
echo " single bootable EFI application in <output>."
echo ""
}
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
#
if [ "$1" = "-h" -o "$1" = "--help" ]; then
usage
exit 0
fi
if [ $# -ne 4 ]; then
usage 1>&2;
error "got $# arguments expected 4"
fi
arg_kernel=$1
arg_initrd=$2
arg_cmdline=$3
arg_output=$4
# dependency checks
deps objcopy
# 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"
# output check
[ ! -e "$arg_output" ] ||
error "output file '$arg_output' already exists"
exec objcopy \
"--add-section=.cmdline=$arg_cmdline" \
"--change-section-vma=.cmdline=0x30000" \
"--add-section=.linux=$arg_kernel" \
"--change-section-vma=.linux=0x2000000" \
"--add-section=.initrd=$arg_initrd" \
"--change-section-vma=.initrd=0x3000000" \
"$STUBBY" "$arg_output"