]> git.proxmox.com Git - mirror_qemu.git/blame - scripts/qemu-binfmt-conf.sh
Merge remote-tracking branch 'remotes/dgibson/tags/ppc-for-3.0-20180703' into staging
[mirror_qemu.git] / scripts / qemu-binfmt-conf.sh
CommitLineData
08785f48 1#!/bin/sh
947aeab3 2# enable automatic i386/ARM/M68K/MIPS/SPARC/PPC/s390/HPPA/Xtensa/microblaze
e4d966cc 3# program execution by the kernel
08785f48 4
bfe69cc8 5qemu_target_list="i386 i486 alpha arm armeb sparc32plus ppc ppc64 ppc64le m68k \
3bef0451 6mips mipsel mipsn32 mipsn32el mips64 mips64el \
947aeab3 7sh4 sh4eb s390x aarch64 aarch64_be hppa riscv32 riscv64 xtensa xtensaeb microblaze microblazeel"
3bef0451
LV
8
9i386_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00'
daf238dc 10i386_mask='\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
3bef0451
LV
11i386_family=i386
12
13i486_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x00'
daf238dc 14i486_mask='\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
3bef0451
LV
15i486_family=i386
16
17alpha_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x26\x90'
daf238dc 18alpha_mask='\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
3bef0451
LV
19alpha_family=alpha
20
21arm_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00'
22arm_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
23arm_family=arm
24
25armeb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28'
26armeb_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
2ced93ee 27armeb_family=armeb
3bef0451
LV
28
29sparc_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02'
daf238dc 30sparc_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
3bef0451
LV
31sparc_family=sparc
32
33sparc32plus_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x12'
daf238dc 34sparc32plus_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
3bef0451
LV
35sparc32plus_family=sparc
36
37ppc_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14'
38ppc_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
39ppc_family=ppc
40
41ppc64_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15'
42ppc64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
43ppc64_family=ppc
44
45ppc64le_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00'
46ppc64le_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00'
47ppc64le_family=ppcle
48
49m68k_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x04'
daf238dc 50m68k_mask='\xff\xff\xff\xff\xff\xff\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
3bef0451
LV
51m68k_family=m68k
52
53# FIXME: We could use the other endianness on a MIPS host.
54
55mips_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
56mips_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
57mips_family=mips
58
59mipsel_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
60mipsel_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
61mipsel_family=mips
62
63mipsn32_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
64mipsn32_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
65mipsn32_family=mips
66
67mipsn32el_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
68mipsn32el_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
69mipsn32el_family=mips
70
71mips64_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
72mips64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
73mips64_family=mips
74
75mips64el_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
76mips64el_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
77mips64el_family=mips
78
79sh4_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a\x00'
daf238dc 80sh4_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
3bef0451
LV
81sh4_family=sh4
82
83sh4eb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a'
daf238dc 84sh4eb_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
3bef0451
LV
85sh4eb_family=sh4
86
87s390x_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x16'
daf238dc 88s390x_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
3bef0451
LV
89s390x_family=s390x
90
91aarch64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00'
92aarch64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
93aarch64_family=arm
94
f772f212
MW
95aarch64_be_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7'
96aarch64_be_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
2ced93ee 97aarch64_be_family=armeb
f772f212 98
e4d966cc
LV
99hppa_magic='\x7f\x45\x4c\x46\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x0f'
100hppa_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
101hppa_family=hppa
102
25fa194b
MC
103riscv32_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00'
104riscv32_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
105riscv32_family=riscv
106
107riscv64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00'
108riscv64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
109riscv64_family=riscv
110
d4090306
MF
111xtensa_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x5e\x00'
112xtensa_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
113xtensa_family=xtensa
114
115xtensaeb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x5e'
116xtensaeb_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
117xtensaeb_family=xtensaeb
118
947aeab3
LV
119microblaze_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xba\xab'
120microblaze_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
121microblaze_family=microblaze
122
123microblazeel_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xab\xba'
124microblazeel_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
125microblazeel_family=microblazeel
126
3bef0451
LV
127qemu_get_family() {
128 cpu=${HOST_ARCH:-$(uname -m)}
129 case "$cpu" in
130 amd64|i386|i486|i586|i686|i86pc|BePC|x86_64)
131 echo "i386"
132 ;;
133 mips*)
134 echo "mips"
135 ;;
136 "Power Macintosh"|ppc64|powerpc|ppc)
137 echo "ppc"
138 ;;
139 ppc64el|ppc64le)
140 echo "ppcle"
141 ;;
2ced93ee 142 arm|armel|armhf|arm64|armv[4-9]*l|aarch64)
3bef0451
LV
143 echo "arm"
144 ;;
2ced93ee
MW
145 armeb|armv[4-9]*b|aarch64_be)
146 echo "armeb"
147 ;;
3bef0451
LV
148 sparc*)
149 echo "sparc"
150 ;;
25fa194b
MC
151 riscv*)
152 echo "riscv"
153 ;;
3bef0451
LV
154 *)
155 echo "$cpu"
156 ;;
157 esac
158}
159
160usage() {
161 cat <<EOF
162Usage: qemu-binfmt-conf.sh [--qemu-path PATH][--debian][--systemd CPU]
163 [--help][--credential yes|no][--exportdir PATH]
164
165 Configure binfmt_misc to use qemu interpreter
166
167 --help: display this usage
168 --qemu-path: set path to qemu interpreter ($QEMU_PATH)
169 --debian: don't write into /proc,
170 instead generate update-binfmts templates
171 --systemd: don't write into /proc,
172 instead generate file for systemd-binfmt.service
3ff48453
LV
173 for the given CPU. If CPU is "ALL", generate a
174 file for all known cpus
3bef0451
LV
175 --exportdir: define where to write configuration files
176 (default: $SYSTEMDDIR or $DEBIANDIR)
177 --credential: if yes, credential and security tokens are
178 calculated according to the binary to interpret
179
180 To import templates with update-binfmts, use :
181
182 sudo update-binfmts --importdir ${EXPORTDIR:-$DEBIANDIR} --import qemu-CPU
183
184 To remove interpreter, use :
185
186 sudo update-binfmts --package qemu-CPU --remove qemu-CPU $QEMU_PATH
187
188 With systemd, binfmt files are loaded by systemd-binfmt.service
189
190 The environment variable HOST_ARCH allows to override 'uname' to generate
191 configuration files for a different architecture than the current one.
192
193 where CPU is one of:
194
195 $qemu_target_list
196
197EOF
198}
199
200qemu_check_access() {
201 if [ ! -w "$1" ] ; then
202 echo "ERROR: cannot write to $1" 1>&2
203 exit 1
204 fi
205}
206
207qemu_check_bintfmt_misc() {
208 # load the binfmt_misc module
209 if [ ! -d /proc/sys/fs/binfmt_misc ]; then
210 if ! /sbin/modprobe binfmt_misc ; then
211 exit 1
212 fi
213 fi
214 if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then
215 if ! mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc ; then
216 exit 1
217 fi
218 fi
219
220 qemu_check_access /proc/sys/fs/binfmt_misc/register
221}
222
223installed_dpkg() {
224 dpkg --status "$1" > /dev/null 2>&1
225}
226
227qemu_check_debian() {
228 if [ ! -e /etc/debian_version ] ; then
229 echo "WARNING: your system is not a Debian based distro" 1>&2
230 elif ! installed_dpkg binfmt-support ; then
231 echo "WARNING: package binfmt-support is needed" 1>&2
232 fi
233 qemu_check_access "$EXPORTDIR"
234}
235
236qemu_check_systemd() {
237 if ! systemctl -q is-enabled systemd-binfmt.service ; then
238 echo "WARNING: systemd-binfmt.service is missing or disabled" 1>&2
239 fi
240 qemu_check_access "$EXPORTDIR"
241}
242
243qemu_generate_register() {
244 echo ":qemu-$cpu:M::$magic:$mask:$qemu:$FLAGS"
245}
246
247qemu_register_interpreter() {
248 echo "Setting $qemu as binfmt interpreter for $cpu"
249 qemu_generate_register > /proc/sys/fs/binfmt_misc/register
250}
251
252qemu_generate_systemd() {
253 echo "Setting $qemu as binfmt interpreter for $cpu for systemd-binfmt.service"
254 qemu_generate_register > "$EXPORTDIR/qemu-$cpu.conf"
255}
256
257qemu_generate_debian() {
258 cat > "$EXPORTDIR/qemu-$cpu" <<EOF
259package qemu-$cpu
260interpreter $qemu
261magic $magic
262mask $mask
263EOF
264 if [ "$FLAGS" = "OC" ] ; then
265 echo "credentials yes" >> "$EXPORTDIR/qemu-$cpu"
266 fi
267}
268
269qemu_set_binfmts() {
270 # probe cpu type
271 host_family=$(qemu_get_family)
272
273 # register the interpreter for each cpu except for the native one
274
275 for cpu in ${qemu_target_list} ; do
276 magic=$(eval echo \$${cpu}_magic)
277 mask=$(eval echo \$${cpu}_mask)
278 family=$(eval echo \$${cpu}_family)
279
280 if [ "$magic" = "" ] || [ "$mask" = "" ] || [ "$family" = "" ] ; then
281 echo "INTERNAL ERROR: unknown cpu $cpu" 1>&2
282 continue
283 fi
284
285 qemu="$QEMU_PATH/qemu-$cpu"
286 if [ "$cpu" = "i486" ] ; then
287 qemu="$QEMU_PATH/qemu-i386"
288 fi
289
290 if [ "$host_family" != "$family" ] ; then
291 $BINFMT_SET
292 fi
293 done
294}
295
296CHECK=qemu_check_bintfmt_misc
297BINFMT_SET=qemu_register_interpreter
298
299SYSTEMDDIR="/etc/binfmt.d"
300DEBIANDIR="/usr/share/binfmts"
301
302QEMU_PATH=/usr/local/bin
303FLAGS=""
304
305options=$(getopt -o ds:Q:e:hc: -l debian,systemd:,qemu-path:,exportdir:,help,credential: -- "$@")
306eval set -- "$options"
307
308while true ; do
309 case "$1" in
310 -d|--debian)
311 CHECK=qemu_check_debian
312 BINFMT_SET=qemu_generate_debian
313 EXPORTDIR=${EXPORTDIR:-$DEBIANDIR}
314 ;;
315 -s|--systemd)
316 CHECK=qemu_check_systemd
317 BINFMT_SET=qemu_generate_systemd
318 EXPORTDIR=${EXPORTDIR:-$SYSTEMDDIR}
319 shift
320 # check given cpu is in the supported CPU list
3ff48453
LV
321 if [ "$1" != "ALL" ] ; then
322 for cpu in ${qemu_target_list} ; do
323 if [ "$cpu" = "$1" ] ; then
324 break
325 fi
326 done
327
6f75023a 328 if [ "$cpu" = "$1" ] ; then
3ff48453
LV
329 qemu_target_list="$1"
330 else
331 echo "ERROR: unknown CPU \"$1\"" 1>&2
332 usage
333 exit 1
3bef0451 334 fi
3bef0451
LV
335 fi
336 ;;
337 -Q|--qemu-path)
338 shift
339 QEMU_PATH="$1"
340 ;;
341 -e|--exportdir)
342 shift
343 EXPORTDIR="$1"
344 ;;
345 -h|--help)
346 usage
347 exit 1
348 ;;
349 -c|--credential)
350 shift
351 if [ "$1" = "yes" ] ; then
352 FLAGS="OC"
353 else
354 FLAGS=""
355 fi
356 ;;
357 *)
358 break
359 ;;
360 esac
361 shift
362done
363
364$CHECK
365qemu_set_binfmts