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