2 # enable automatic i386/ARM/M68K/MIPS/SPARC/PPC/s390/HPPA
3 # program execution by the kernel
5 qemu_target_list
="i386 i486 alpha arm armeb sparc32plus ppc ppc64 ppc64le m68k \
6 mips mipsel mipsn32 mipsn32el mips64 mips64el \
7 sh4 sh4eb s390x aarch64 aarch64_be hppa"
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\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
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\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
17 alpha_magic
='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x26\x90'
18 alpha_mask
='\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
21 arm_magic
='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00'
22 arm_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
25 armeb_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28'
26 armeb_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
29 sparc_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02'
30 sparc_mask
='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
33 sparc32plus_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x12'
34 sparc32plus_mask
='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
35 sparc32plus_family
=sparc
37 ppc_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14'
38 ppc_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
41 ppc64_magic
='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15'
42 ppc64_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
45 ppc64le_magic
='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00'
46 ppc64le_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00'
49 m68k_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x04'
50 m68k_mask
='\xff\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
53 # FIXME: We could use the other endianness on a MIPS host.
55 mips_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
56 mips_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
59 mipsel_magic
='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
60 mipsel_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
63 mipsn32_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
64 mipsn32_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
67 mipsn32el_magic
='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
68 mipsn32el_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
71 mips64_magic
='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
72 mips64_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
75 mips64el_magic
='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
76 mips64el_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
79 sh4_magic
='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a\x00'
80 sh4_mask
='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
83 sh4eb_magic
='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a'
84 sh4eb_mask
='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
87 s390x_magic
='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x16'
88 s390x_mask
='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
91 aarch64_magic
='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00'
92 aarch64_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
95 aarch64_be_magic
='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7'
96 aarch64_be_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
97 aarch64_be_family
=armeb
99 hppa_magic
='\x7f\x45\x4c\x46\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x0f'
100 hppa_mask
='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
104 cpu
=${HOST_ARCH:-$(uname -m)}
106 amd64|i386|i486|i586|i686|i86pc|BePC|x86_64
)
112 "Power Macintosh"|ppc64|powerpc|ppc
)
118 arm|armel|armhf|arm64|armv
[4-9]*l|aarch64
)
121 armeb|armv
[4-9]*b|aarch64_be
)
135 Usage: qemu-binfmt-conf.sh [--qemu-path PATH][--debian][--systemd CPU]
136 [--help][--credential yes|no][--exportdir PATH]
138 Configure binfmt_misc to use qemu interpreter
140 --help: display this usage
141 --qemu-path: set path to qemu interpreter ($QEMU_PATH)
142 --debian: don't write into /proc,
143 instead generate update-binfmts templates
144 --systemd: don't write into /proc,
145 instead generate file for systemd-binfmt.service
147 --exportdir: define where to write configuration files
148 (default: $SYSTEMDDIR or $DEBIANDIR)
149 --credential: if yes, credential and security tokens are
150 calculated according to the binary to interpret
152 To import templates with update-binfmts, use :
154 sudo update-binfmts --importdir ${EXPORTDIR:-$DEBIANDIR} --import qemu-CPU
156 To remove interpreter, use :
158 sudo update-binfmts --package qemu-CPU --remove qemu-CPU $QEMU_PATH
160 With systemd, binfmt files are loaded by systemd-binfmt.service
162 The environment variable HOST_ARCH allows to override 'uname' to generate
163 configuration files for a different architecture than the current one.
172 qemu_check_access
() {
173 if [ ! -w "$1" ] ; then
174 echo "ERROR: cannot write to $1" 1>&2
179 qemu_check_bintfmt_misc
() {
180 # load the binfmt_misc module
181 if [ ! -d /proc
/sys
/fs
/binfmt_misc
]; then
182 if ! /sbin
/modprobe binfmt_misc
; then
186 if [ ! -f /proc
/sys
/fs
/binfmt_misc
/register
]; then
187 if ! mount binfmt_misc
-t binfmt_misc
/proc
/sys
/fs
/binfmt_misc
; then
192 qemu_check_access
/proc
/sys
/fs
/binfmt_misc
/register
196 dpkg
--status "$1" > /dev
/null
2>&1
199 qemu_check_debian
() {
200 if [ ! -e /etc
/debian_version
] ; then
201 echo "WARNING: your system is not a Debian based distro" 1>&2
202 elif ! installed_dpkg binfmt-support
; then
203 echo "WARNING: package binfmt-support is needed" 1>&2
205 qemu_check_access
"$EXPORTDIR"
208 qemu_check_systemd
() {
209 if ! systemctl
-q is-enabled systemd-binfmt.service
; then
210 echo "WARNING: systemd-binfmt.service is missing or disabled" 1>&2
212 qemu_check_access
"$EXPORTDIR"
215 qemu_generate_register
() {
216 echo ":qemu-$cpu:M::$magic:$mask:$qemu:$FLAGS"
219 qemu_register_interpreter
() {
220 echo "Setting $qemu as binfmt interpreter for $cpu"
221 qemu_generate_register
> /proc
/sys
/fs
/binfmt_misc
/register
224 qemu_generate_systemd
() {
225 echo "Setting $qemu as binfmt interpreter for $cpu for systemd-binfmt.service"
226 qemu_generate_register
> "$EXPORTDIR/qemu-$cpu.conf"
229 qemu_generate_debian
() {
230 cat > "$EXPORTDIR/qemu-$cpu" <<EOF
236 if [ "$FLAGS" = "OC" ] ; then
237 echo "credentials yes" >> "$EXPORTDIR/qemu-$cpu"
243 host_family
=$
(qemu_get_family
)
245 # register the interpreter for each cpu except for the native one
247 for cpu
in ${qemu_target_list} ; do
248 magic
=$
(eval echo \$
${cpu}_magic
)
249 mask
=$
(eval echo \$
${cpu}_mask
)
250 family
=$
(eval echo \$
${cpu}_family
)
252 if [ "$magic" = "" ] ||
[ "$mask" = "" ] ||
[ "$family" = "" ] ; then
253 echo "INTERNAL ERROR: unknown cpu $cpu" 1>&2
257 qemu
="$QEMU_PATH/qemu-$cpu"
258 if [ "$cpu" = "i486" ] ; then
259 qemu
="$QEMU_PATH/qemu-i386"
262 if [ "$host_family" != "$family" ] ; then
268 CHECK
=qemu_check_bintfmt_misc
269 BINFMT_SET
=qemu_register_interpreter
271 SYSTEMDDIR
="/etc/binfmt.d"
272 DEBIANDIR
="/usr/share/binfmts"
274 QEMU_PATH
=/usr
/local
/bin
277 options
=$
(getopt
-o ds
:Q
:e
:hc
: -l debian
,systemd
:,qemu-path
:,exportdir
:,help,credential
: -- "$@")
278 eval set -- "$options"
283 CHECK
=qemu_check_debian
284 BINFMT_SET
=qemu_generate_debian
285 EXPORTDIR
=${EXPORTDIR:-$DEBIANDIR}
288 CHECK
=qemu_check_systemd
289 BINFMT_SET
=qemu_generate_systemd
290 EXPORTDIR
=${EXPORTDIR:-$SYSTEMDDIR}
292 # check given cpu is in the supported CPU list
293 for cpu
in ${qemu_target_list} ; do
294 if [ "$cpu" = "$1" ] ; then
299 if [ "$cpu" = "$1" ] ; then
300 qemu_target_list
="$1"
302 echo "ERROR: unknown CPU \"$1\"" 1>&2
321 if [ "$1" = "yes" ] ; then