Merge remote-tracking branch 'remotes/dgibson/tags/ppc-for-3.0-20180801' into staging
[qemu/ar7.git] / scripts / qemu-binfmt-conf.sh
blobb0dc8a714a4a29e69150f16c5c778d5b0ab7b78e
1 #!/bin/sh
2 # Enable automatic program execution by the kernel.
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"
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
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
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\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
19 alpha_family=alpha
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'
23 arm_family=arm
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'
27 armeb_family=armeb
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\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
31 sparc_family=sparc
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\x00\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'
39 ppc_family=ppc
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'
43 ppc64_family=ppc
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'
47 ppc64le_family=ppcle
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\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
51 m68k_family=m68k
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'
57 mips_family=mips
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'
61 mipsel_family=mips
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'
65 mipsn32_family=mips
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'
69 mipsn32el_family=mips
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'
73 mips64_family=mips
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'
77 mips64el_family=mips
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\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
81 sh4_family=sh4
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\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
85 sh4eb_family=sh4
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\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
89 s390x_family=s390x
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'
93 aarch64_family=arm
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'
101 hppa_family=hppa
103 riscv32_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00'
104 riscv32_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
105 riscv32_family=riscv
107 riscv64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00'
108 riscv64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
109 riscv64_family=riscv
111 xtensa_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x5e\x00'
112 xtensa_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
113 xtensa_family=xtensa
115 xtensaeb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x5e'
116 xtensaeb_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
117 xtensaeb_family=xtensaeb
119 microblaze_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xba\xab'
120 microblaze_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
121 microblaze_family=microblaze
123 microblazeel_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xab\xba'
124 microblazeel_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
125 microblazeel_family=microblazeel
127 or1k_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x5c'
128 or1k_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
129 or1k_family=or1k
131 qemu_get_family() {
132 cpu=${HOST_ARCH:-$(uname -m)}
133 case "$cpu" in
134 amd64|i386|i486|i586|i686|i86pc|BePC|x86_64)
135 echo "i386"
137 mips*)
138 echo "mips"
140 "Power Macintosh"|ppc64|powerpc|ppc)
141 echo "ppc"
143 ppc64el|ppc64le)
144 echo "ppcle"
146 arm|armel|armhf|arm64|armv[4-9]*l|aarch64)
147 echo "arm"
149 armeb|armv[4-9]*b|aarch64_be)
150 echo "armeb"
152 sparc*)
153 echo "sparc"
155 riscv*)
156 echo "riscv"
159 echo "$cpu"
161 esac
164 usage() {
165 cat <<EOF
166 Usage: qemu-binfmt-conf.sh [--qemu-path PATH][--debian][--systemd CPU]
167 [--help][--credential yes|no][--exportdir PATH]
168 [--persistent yes|no][--qemu-suffix SUFFIX]
170 Configure binfmt_misc to use qemu interpreter
172 --help: display this usage
173 --qemu-path: set path to qemu interpreter ($QEMU_PATH)
174 --qemu-suffix: add a suffix to the default interpreter name
175 --debian: don't write into /proc,
176 instead generate update-binfmts templates
177 --systemd: don't write into /proc,
178 instead generate file for systemd-binfmt.service
179 for the given CPU. If CPU is "ALL", generate a
180 file for all known cpus
181 --exportdir: define where to write configuration files
182 (default: $SYSTEMDDIR or $DEBIANDIR)
183 --credential: if yes, credential and security tokens are
184 calculated according to the binary to interpret
185 --persistent: if yes, the interpreter is loaded when binfmt is
186 configured and remains in memory. All future uses
187 are cloned from the open file.
189 To import templates with update-binfmts, use :
191 sudo update-binfmts --importdir ${EXPORTDIR:-$DEBIANDIR} --import qemu-CPU
193 To remove interpreter, use :
195 sudo update-binfmts --package qemu-CPU --remove qemu-CPU $QEMU_PATH
197 With systemd, binfmt files are loaded by systemd-binfmt.service
199 The environment variable HOST_ARCH allows to override 'uname' to generate
200 configuration files for a different architecture than the current one.
202 where CPU is one of:
204 $qemu_target_list
209 qemu_check_access() {
210 if [ ! -w "$1" ] ; then
211 echo "ERROR: cannot write to $1" 1>&2
212 exit 1
216 qemu_check_bintfmt_misc() {
217 # load the binfmt_misc module
218 if [ ! -d /proc/sys/fs/binfmt_misc ]; then
219 if ! /sbin/modprobe binfmt_misc ; then
220 exit 1
223 if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then
224 if ! mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc ; then
225 exit 1
229 qemu_check_access /proc/sys/fs/binfmt_misc/register
232 installed_dpkg() {
233 dpkg --status "$1" > /dev/null 2>&1
236 qemu_check_debian() {
237 if [ ! -e /etc/debian_version ] ; then
238 echo "WARNING: your system is not a Debian based distro" 1>&2
239 elif ! installed_dpkg binfmt-support ; then
240 echo "WARNING: package binfmt-support is needed" 1>&2
242 qemu_check_access "$EXPORTDIR"
245 qemu_check_systemd() {
246 if ! systemctl -q is-enabled systemd-binfmt.service ; then
247 echo "WARNING: systemd-binfmt.service is missing or disabled" 1>&2
249 qemu_check_access "$EXPORTDIR"
252 qemu_generate_register() {
253 flags=""
254 if [ "$CREDENTIAL" = "yes" ] ; then
255 flags="OC"
257 if [ "$PERSISTENT" = "yes" ] ; then
258 flags="${flags}F"
261 echo ":qemu-$cpu:M::$magic:$mask:$qemu:$flags"
264 qemu_register_interpreter() {
265 echo "Setting $qemu as binfmt interpreter for $cpu"
266 qemu_generate_register > /proc/sys/fs/binfmt_misc/register
269 qemu_generate_systemd() {
270 echo "Setting $qemu as binfmt interpreter for $cpu for systemd-binfmt.service"
271 qemu_generate_register > "$EXPORTDIR/qemu-$cpu.conf"
274 qemu_generate_debian() {
275 cat > "$EXPORTDIR/qemu-$cpu" <<EOF
276 package qemu-$cpu
277 interpreter $qemu
278 magic $magic
279 mask $mask
280 credential $CREDENTIAL
284 qemu_set_binfmts() {
285 # probe cpu type
286 host_family=$(qemu_get_family)
288 # register the interpreter for each cpu except for the native one
290 for cpu in ${qemu_target_list} ; do
291 magic=$(eval echo \$${cpu}_magic)
292 mask=$(eval echo \$${cpu}_mask)
293 family=$(eval echo \$${cpu}_family)
295 if [ "$magic" = "" ] || [ "$mask" = "" ] || [ "$family" = "" ] ; then
296 echo "INTERNAL ERROR: unknown cpu $cpu" 1>&2
297 continue
300 qemu="$QEMU_PATH/qemu-$cpu"
301 if [ "$cpu" = "i486" ] ; then
302 qemu="$QEMU_PATH/qemu-i386"
305 qemu="$qemu$QEMU_SUFFIX"
306 if [ "$host_family" != "$family" ] ; then
307 $BINFMT_SET
309 done
312 CHECK=qemu_check_bintfmt_misc
313 BINFMT_SET=qemu_register_interpreter
315 SYSTEMDDIR="/etc/binfmt.d"
316 DEBIANDIR="/usr/share/binfmts"
318 QEMU_PATH=/usr/local/bin
319 CREDENTIAL=no
320 PERSISTENT=no
321 QEMU_SUFFIX=""
323 options=$(getopt -o ds:Q:S:e:hc:p: -l debian,systemd:,qemu-path:,qemu-suffix:,exportdir:,help,credential:,persistent: -- "$@")
324 eval set -- "$options"
326 while true ; do
327 case "$1" in
328 -d|--debian)
329 CHECK=qemu_check_debian
330 BINFMT_SET=qemu_generate_debian
331 EXPORTDIR=${EXPORTDIR:-$DEBIANDIR}
333 -s|--systemd)
334 CHECK=qemu_check_systemd
335 BINFMT_SET=qemu_generate_systemd
336 EXPORTDIR=${EXPORTDIR:-$SYSTEMDDIR}
337 shift
338 # check given cpu is in the supported CPU list
339 if [ "$1" != "ALL" ] ; then
340 for cpu in ${qemu_target_list} ; do
341 if [ "$cpu" = "$1" ] ; then
342 break
344 done
346 if [ "$cpu" = "$1" ] ; then
347 qemu_target_list="$1"
348 else
349 echo "ERROR: unknown CPU \"$1\"" 1>&2
350 usage
351 exit 1
355 -Q|--qemu-path)
356 shift
357 QEMU_PATH="$1"
359 -F|--qemu-suffix)
360 shift
361 QEMU_SUFFIX="$1"
363 -e|--exportdir)
364 shift
365 EXPORTDIR="$1"
367 -h|--help)
368 usage
369 exit 1
371 -c|--credential)
372 shift
373 CREDENTIAL="$1"
375 -p|--persistent)
376 shift
377 PERSISTENT="$1"
380 break
382 esac
383 shift
384 done
386 $CHECK
387 qemu_set_binfmts