xref: /openbmc/qemu/scripts/qemu-binfmt-conf.sh (revision 98979cdc)
1#!/bin/sh
2# enable automatic i386/ARM/M68K/MIPS/SPARC/PPC/s390/HPPA
3# program execution by the kernel
4
5qemu_target_list="i386 i486 alpha arm sparc32plus ppc ppc64 ppc64le m68k \
6mips mipsel mipsn32 mipsn32el mips64 mips64el \
7sh4 sh4eb s390x aarch64 hppa"
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\xff\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\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
15i486_family=i386
16
17alpha_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x26\x90'
18alpha_mask='\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
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'
27armeb_family=arm
28
29sparc_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02'
30sparc_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
31sparc_family=sparc
32
33sparc32plus_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x12'
34sparc32plus_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
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'
50m68k_mask='\xff\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
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'
80sh4_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
81sh4_family=sh4
82
83sh4eb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a'
84sh4eb_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
85sh4eb_family=sh4
86
87s390x_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x16'
88s390x_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
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
95hppa_magic='\x7f\x45\x4c\x46\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x0f'
96hppa_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
97hppa_family=hppa
98
99qemu_get_family() {
100    cpu=${HOST_ARCH:-$(uname -m)}
101    case "$cpu" in
102    amd64|i386|i486|i586|i686|i86pc|BePC|x86_64)
103        echo "i386"
104        ;;
105    mips*)
106        echo "mips"
107        ;;
108    "Power Macintosh"|ppc64|powerpc|ppc)
109        echo "ppc"
110        ;;
111    ppc64el|ppc64le)
112        echo "ppcle"
113        ;;
114    arm|armel|armhf|arm64|armv[4-9]*)
115        echo "arm"
116        ;;
117    sparc*)
118        echo "sparc"
119        ;;
120    *)
121        echo "$cpu"
122        ;;
123    esac
124}
125
126usage() {
127    cat <<EOF
128Usage: qemu-binfmt-conf.sh [--qemu-path PATH][--debian][--systemd CPU]
129                           [--help][--credential yes|no][--exportdir PATH]
130
131       Configure binfmt_misc to use qemu interpreter
132
133       --help:       display this usage
134       --qemu-path:  set path to qemu interpreter ($QEMU_PATH)
135       --debian:     don't write into /proc,
136                     instead generate update-binfmts templates
137       --systemd:    don't write into /proc,
138                     instead generate file for systemd-binfmt.service
139                     for the given CPU
140       --exportdir:  define where to write configuration files
141                     (default: $SYSTEMDDIR or $DEBIANDIR)
142       --credential: if yes, credential and security tokens are
143                     calculated according to the binary to interpret
144
145    To import templates with update-binfmts, use :
146
147        sudo update-binfmts --importdir ${EXPORTDIR:-$DEBIANDIR} --import qemu-CPU
148
149    To remove interpreter, use :
150
151        sudo update-binfmts --package qemu-CPU --remove qemu-CPU $QEMU_PATH
152
153    With systemd, binfmt files are loaded by systemd-binfmt.service
154
155    The environment variable HOST_ARCH allows to override 'uname' to generate
156    configuration files for a different architecture than the current one.
157
158    where CPU is one of:
159
160        $qemu_target_list
161
162EOF
163}
164
165qemu_check_access() {
166    if [ ! -w "$1" ] ; then
167        echo "ERROR: cannot write to $1" 1>&2
168        exit 1
169    fi
170}
171
172qemu_check_bintfmt_misc() {
173    # load the binfmt_misc module
174    if [ ! -d /proc/sys/fs/binfmt_misc ]; then
175      if ! /sbin/modprobe binfmt_misc ; then
176          exit 1
177      fi
178    fi
179    if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then
180      if ! mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc ; then
181          exit 1
182      fi
183    fi
184
185    qemu_check_access /proc/sys/fs/binfmt_misc/register
186}
187
188installed_dpkg() {
189    dpkg --status "$1" > /dev/null 2>&1
190}
191
192qemu_check_debian() {
193    if [ ! -e /etc/debian_version ] ; then
194        echo "WARNING: your system is not a Debian based distro" 1>&2
195    elif ! installed_dpkg binfmt-support ; then
196        echo "WARNING: package binfmt-support is needed" 1>&2
197    fi
198    qemu_check_access "$EXPORTDIR"
199}
200
201qemu_check_systemd() {
202    if ! systemctl -q is-enabled systemd-binfmt.service ; then
203        echo "WARNING: systemd-binfmt.service is missing or disabled" 1>&2
204    fi
205    qemu_check_access "$EXPORTDIR"
206}
207
208qemu_generate_register() {
209    echo ":qemu-$cpu:M::$magic:$mask:$qemu:$FLAGS"
210}
211
212qemu_register_interpreter() {
213    echo "Setting $qemu as binfmt interpreter for $cpu"
214    qemu_generate_register > /proc/sys/fs/binfmt_misc/register
215}
216
217qemu_generate_systemd() {
218    echo "Setting $qemu as binfmt interpreter for $cpu for systemd-binfmt.service"
219    qemu_generate_register > "$EXPORTDIR/qemu-$cpu.conf"
220}
221
222qemu_generate_debian() {
223    cat > "$EXPORTDIR/qemu-$cpu" <<EOF
224package qemu-$cpu
225interpreter $qemu
226magic $magic
227mask $mask
228EOF
229    if [ "$FLAGS" = "OC" ] ; then
230        echo "credentials yes" >> "$EXPORTDIR/qemu-$cpu"
231    fi
232}
233
234qemu_set_binfmts() {
235    # probe cpu type
236    host_family=$(qemu_get_family)
237
238    # register the interpreter for each cpu except for the native one
239
240    for cpu in ${qemu_target_list} ; do
241        magic=$(eval echo \$${cpu}_magic)
242        mask=$(eval echo \$${cpu}_mask)
243        family=$(eval echo \$${cpu}_family)
244
245        if [ "$magic" = "" ] || [ "$mask" = "" ] || [ "$family" = "" ] ; then
246            echo "INTERNAL ERROR: unknown cpu $cpu" 1>&2
247            continue
248        fi
249
250        qemu="$QEMU_PATH/qemu-$cpu"
251        if [ "$cpu" = "i486" ] ; then
252            qemu="$QEMU_PATH/qemu-i386"
253        fi
254
255        if [ "$host_family" != "$family" ] ; then
256            $BINFMT_SET
257        fi
258    done
259}
260
261CHECK=qemu_check_bintfmt_misc
262BINFMT_SET=qemu_register_interpreter
263
264SYSTEMDDIR="/etc/binfmt.d"
265DEBIANDIR="/usr/share/binfmts"
266
267QEMU_PATH=/usr/local/bin
268FLAGS=""
269
270options=$(getopt -o ds:Q:e:hc: -l debian,systemd:,qemu-path:,exportdir:,help,credential: -- "$@")
271eval set -- "$options"
272
273while true ; do
274    case "$1" in
275    -d|--debian)
276        CHECK=qemu_check_debian
277        BINFMT_SET=qemu_generate_debian
278        EXPORTDIR=${EXPORTDIR:-$DEBIANDIR}
279        ;;
280    -s|--systemd)
281        CHECK=qemu_check_systemd
282        BINFMT_SET=qemu_generate_systemd
283        EXPORTDIR=${EXPORTDIR:-$SYSTEMDDIR}
284        shift
285        # check given cpu is in the supported CPU list
286        for cpu in ${qemu_target_list} ; do
287            if [ "$cpu" = "$1" ] ; then
288                break
289            fi
290        done
291
292        if [ "$cpu" = "$1" ] ; then
293            qemu_target_list="$1"
294        else
295            echo "ERROR: unknown CPU \"$1\"" 1>&2
296            usage
297            exit 1
298        fi
299        ;;
300    -Q|--qemu-path)
301        shift
302        QEMU_PATH="$1"
303        ;;
304    -e|--exportdir)
305        shift
306        EXPORTDIR="$1"
307        ;;
308    -h|--help)
309        usage
310        exit 1
311        ;;
312    -c|--credential)
313        shift
314        if [ "$1" = "yes" ] ; then
315            FLAGS="OC"
316        else
317            FLAGS=""
318        fi
319        ;;
320    *)
321        break
322        ;;
323    esac
324    shift
325done
326
327$CHECK
328qemu_set_binfmts
329