-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbu-make-combo-loader.sh.in
executable file
·212 lines (177 loc) · 4.91 KB
/
sbu-make-combo-loader.sh.in
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env bash
usage() {
local old_xtrace
old_xtrace="$(shopt -po xtrace || :)"
set +o xtrace
{
echo "${script_name} - Create an EFI Linux bootloader program from a systemd bootloader stub."
echo "Usage: ${script_name} [flags]"
echo "Option flags:"
echo " -l --linux - Linux kernel file. Default: '${linux}'."
echo " -i --initrd - Optional Linux initrd file. Default: '${initrd}'."
echo " -c --cmdline - Optional Linux kernel cmdline. Default: '${cmdline}'."
echo " -p --splash - Optional splash screen bitmap file. Default: '${splash}'."
echo " -f --config - Optional configuration file. Default: '${config_file}'."
echo " -s --efi-stub - EFI bootloader stub. Default: '${efi_stub}'."
echo " -o --output-file - EFI bootloader output file. Default: '${out_file}'."
echo " -h --help - Show this help and exit."
echo " -v --verbose - Verbose execution."
echo " -g --debug - Extra verbose execution."
echo "Info:"
echo ' @PACKAGE_NAME@ v@PACKAGE_VERSION@'
echo ' @PACKAGE_URL@'
echo " Send bug reports to: Geoff Levand <geoff@infradead.org>."
} >&2
eval "${old_xtrace}"
}
process_opts() {
local short_opts="l:i:c:p:f:s:o:hvg"
local long_opts="linux:,initrd:,cmdline:,splash:,config:,efi-stub:,output-file:,help,verbose,debug"
local opts
opts=$(getopt --options ${short_opts} --long ${long_opts} -n "${script_name}" -- "$@")
eval set -- "${opts}"
while true ; do
# echo "${FUNCNAME[0]}: (${#}) '${*}'"
case "${1}" in
-l | --linux)
linux="${2}"
shift 2
;;
-i | --initrd)
initrd="${2}"
shift 2
;;
-c | --cmdline)
cmdline="${2}"
shift 2
;;
-p | --splash)
splash="${2}"
shift 2
;;
-f | --config)
config_file="${2}"
shift 2
;;
-o | --output-file)
out_file="${2}"
shift 2
;;
-s | --efi-stub)
efi_stub="${2}"
shift 2
;;
-h | --help)
usage=1
shift
;;
-v | --verbose)
verbose=1
shift
;;
-g | --debug)
set -x
verbose=1
debug=1
shift
;;
--)
shift
extra_args="${*}"
break
;;
*)
echo "${script_name}: ERROR: Internal opts: '${*}'" >&2
exit 1
;;
esac
done
}
#===============================================================================
export PS4='\[\e[0;33m\]+ ${BASH_SOURCE##*/}:${LINENO}:(${FUNCNAME[0]:-main}):\[\e[0m\] '
script_name="${0##*/}"
base_name="${script_name##*/%}"
base_name="${base_name%.sh}"
real_source="$(realpath "${BASH_SOURCE}")"
SCRIPT_TOP="$(realpath "${SCRIPT_TOP:-${real_source%/*}}")"
start_time="$(date +%Y.%m.%d-%H.%M.%S)"
SECONDS=0
trap "on_exit 'Failed'" EXIT
trap 'on_err ${FUNCNAME[0]:-main} ${LINENO} ${?}' ERR
set -eE
set -o pipefail
set -o nounset
source "${SCRIPT_TOP}/sbu-lib.sh"
source "${SCRIPT_TOP}/tdd-lib/util.sh"
linux=''
initrd=''
cmdline='console=ttyS0,115200 console=tty0'
splash=''
config_file=''
out_file="/tmp/${script_name%.sh}-${start_time}.efi"
efi_stub='/usr/lib/systemd/boot/efi/linuxx64.efi.stub'
usage=''
verbose=''
debug=''
process_opts "${@}"
if [[ ${config_file} ]]; then
if [[ ! -f "${config_file}" ]]; then
echo "${script_name}: ERROR: Config file not found: '${config_file}'" >&2
usage
exit 1
fi
config_file="$(realpath "${config_file}")"
source "${config_file}"
fi
cmdline_start="${cmdline_start:-0x30000}"
splash_start="${splash_start:-0x40000}"
linux_start="${linux_start:-0x50000}"
initrd_start="${initrd_start:-0x3000000}"
objcopy="${objcopy:-objcopy}"
objdump="${objdump:-objdump}"
if [[ ${usage} ]]; then
usage
trap - EXIT
exit 0
fi
if [[ ${extra_args} ]]; then
set +o xtrace
echo "${script_name}: ERROR: Got extra args: '${extra_args}'" >&2
usage
exit 1
fi
check_progs "${objcopy}"
check_progs "${objdump}"
check_opt 'linux' "${linux}"
check_file "${linux}" ' --linux' ''
check_file "${efi_stub}" ' --efi-stub' ''
tmp_dir="$(mktemp --tmpdir --directory ${script_name}.XXXX)"
out_dir="${out_file%/*}"
mkdir -p "${out_dir}"
out_dir="$(realpath "${out_dir}")"
out_file="${out_dir}/${out_file##*/}"
rm -f "${out_file}"
cmdline_file="${tmp_dir}/cmdline"
echo "${cmdline}" > "${cmdline_file}"
objcopy_args=""
objcopy_args+=" --add-section .cmdline='${cmdline_file}'"
objcopy_args+=" --change-section-vma .cmdline='${cmdline_start}'"
objcopy_args+=" --add-section .linux='${linux}'"
objcopy_args+=" --change-section-vma .linux='${linux_start}'"
if [[ ${splash} ]]; then
check_file "${splash}" ' --splash' ''
objcopy_args+=" --add-section .splash='${splash}'"
objcopy_args+=" --change-section-vma .splash='${splash_start}'"
fi
if [[ ${initrd} ]]; then
check_file "${initrd}" ' --initrd' ''
objcopy_args+=" --add-section .initrd='${initrd}'"
objcopy_args+=" --change-section-vma .initrd='${initrd_start}'"
fi
echo "${script_name}: INFO: Preparing '${out_file}'" >&2
eval "${objcopy} ${objcopy_args} ${efi_stub} ${out_file}"
"${objdump}" -h "${out_file}"
echo '' >&2
echo "${script_name}: INFO: Output in '${out_file}'" >&2
trap "on_exit 'Success'" EXIT
exit 0