xref: /qemu/scripts/qemu-binfmt-conf.sh (revision 62815d85)
1#!/bin/sh
2# Enable automatic program execution by the kernel.
3
4qemu_target_list="i386 i486 alpha arm armeb sparc32plus ppc ppc64 ppc64le m68k \
5mips mipsel mipsn32 mipsn32el mips64 mips64el \
6sh4 sh4eb s390x aarch64 aarch64_be hppa riscv32 riscv64 xtensa xtensaeb \
7microblaze microblazeel or1k x86_64"
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
41ppc_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14'
42ppc_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
43ppc_family=ppc
44
45ppc64_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15'
46ppc64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
47ppc64_family=ppc
48
49ppc64le_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00'
50ppc64le_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00'
51ppc64le_family=ppcle
52
53m68k_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x04'
54m68k_mask='\xff\xff\xff\xff\xff\xff\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
55m68k_family=m68k
56
57# FIXME: We could use the other endianness on a MIPS host.
58
59mips_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
60mips_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
61mips_family=mips
62
63mipsel_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
64mipsel_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
65mipsel_family=mips
66
67mipsn32_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
68mipsn32_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
69mipsn32_family=mips
70
71mipsn32el_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
72mipsn32el_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
73mipsn32el_family=mips
74
75mips64_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
76mips64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
77mips64_family=mips
78
79mips64el_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
80mips64el_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
81mips64el_family=mips
82
83sh4_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a\x00'
84sh4_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
85sh4_family=sh4
86
87sh4eb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a'
88sh4eb_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
89sh4eb_family=sh4
90
91s390x_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x16'
92s390x_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
93s390x_family=s390x
94
95aarch64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00'
96aarch64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
97aarch64_family=arm
98
99aarch64_be_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7'
100aarch64_be_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
101aarch64_be_family=armeb
102
103hppa_magic='\x7f\x45\x4c\x46\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x0f'
104hppa_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
105hppa_family=hppa
106
107riscv32_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00'
108riscv32_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
109riscv32_family=riscv
110
111riscv64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00'
112riscv64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
113riscv64_family=riscv
114
115xtensa_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x5e\x00'
116xtensa_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
117xtensa_family=xtensa
118
119xtensaeb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x5e'
120xtensaeb_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
121xtensaeb_family=xtensaeb
122
123microblaze_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xba\xab'
124microblaze_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
125microblaze_family=microblaze
126
127microblazeel_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xab\xba'
128microblazeel_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
129microblazeel_family=microblazeel
130
131or1k_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x5c'
132or1k_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
133or1k_family=or1k
134
135qemu_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
168usage() {
169    cat <<EOF
170Usage: 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
210EOF
211}
212
213qemu_check_access() {
214    if [ ! -w "$1" ] ; then
215        echo "ERROR: cannot write to $1" 1>&2
216        exit 1
217    fi
218}
219
220qemu_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
236installed_dpkg() {
237    dpkg --status "$1" > /dev/null 2>&1
238}
239
240qemu_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
249qemu_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
256qemu_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
268qemu_register_interpreter() {
269    echo "Setting $qemu as binfmt interpreter for $cpu"
270    qemu_generate_register > /proc/sys/fs/binfmt_misc/register
271}
272
273qemu_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
278qemu_generate_debian() {
279    cat > "$EXPORTDIR/qemu-$cpu" <<EOF
280package qemu-$cpu
281interpreter $qemu
282magic $magic
283mask $mask
284credential $CREDENTIAL
285EOF
286}
287
288qemu_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
316CHECK=qemu_check_bintfmt_misc
317BINFMT_SET=qemu_register_interpreter
318
319SYSTEMDDIR="/etc/binfmt.d"
320DEBIANDIR="/usr/share/binfmts"
321
322QEMU_PATH=/usr/local/bin
323CREDENTIAL=no
324PERSISTENT=no
325QEMU_SUFFIX=""
326
327options=$(getopt -o ds:Q:S:e:hc:p: -l debian,systemd:,qemu-path:,qemu-suffix:,exportdir:,help,credential:,persistent: -- "$@")
328eval set -- "$options"
329
330while 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
388done
389
390$CHECK
391qemu_set_binfmts
392