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