travis.yml: duplicate before_script for MacOSX
[qemu/ar7.git] / scripts / qemu-binfmt-conf.sh
blob9f1580a91c7d3ad64120fe8ee66d1420b8ab890d
1 #!/bin/sh
2 # Enable automatic program execution by the kernel.
4 qemu_target_list="i386 i486 alpha arm armeb sparc sparc32plus sparc64 \
5 ppc ppc64 ppc64le m68k mips mipsel mipsn32 mipsn32el mips64 mips64el \
6 sh4 sh4eb s390x aarch64 aarch64_be hppa riscv32 riscv64 xtensa xtensaeb \
7 microblaze microblazeel or1k x86_64"
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 x86_64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x3e\x00'
18 x86_64_mask='\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
19 x86_64_family=i386
21 alpha_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x26\x90'
22 alpha_mask='\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
23 alpha_family=alpha
25 arm_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00'
26 arm_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
27 arm_family=arm
29 armeb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28'
30 armeb_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
31 armeb_family=armeb
33 sparc_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02'
34 sparc_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
35 sparc_family=sparc
37 sparc32plus_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x12'
38 sparc32plus_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
39 sparc32plus_family=sparc
41 sparc64_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2b'
42 sparc64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
43 sparc64_family=sparc
45 ppc_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14'
46 ppc_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
47 ppc_family=ppc
49 ppc64_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15'
50 ppc64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
51 ppc64_family=ppc
53 ppc64le_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00'
54 ppc64le_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00'
55 ppc64le_family=ppcle
57 m68k_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x04'
58 m68k_mask='\xff\xff\xff\xff\xff\xff\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
59 m68k_family=m68k
61 # FIXME: We could use the other endianness on a MIPS host.
63 mips_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
64 mips_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
65 mips_family=mips
67 mipsel_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
68 mipsel_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
69 mipsel_family=mips
71 mipsn32_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
72 mipsn32_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
73 mipsn32_family=mips
75 mipsn32el_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
76 mipsn32el_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
77 mipsn32el_family=mips
79 mips64_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
80 mips64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
81 mips64_family=mips
83 mips64el_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
84 mips64el_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
85 mips64el_family=mips
87 sh4_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a\x00'
88 sh4_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
89 sh4_family=sh4
91 sh4eb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a'
92 sh4eb_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
93 sh4eb_family=sh4
95 s390x_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x16'
96 s390x_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
97 s390x_family=s390x
99 aarch64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00'
100 aarch64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
101 aarch64_family=arm
103 aarch64_be_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7'
104 aarch64_be_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
105 aarch64_be_family=armeb
107 hppa_magic='\x7f\x45\x4c\x46\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x0f'
108 hppa_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
109 hppa_family=hppa
111 riscv32_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00'
112 riscv32_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
113 riscv32_family=riscv
115 riscv64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00'
116 riscv64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
117 riscv64_family=riscv
119 xtensa_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x5e\x00'
120 xtensa_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
121 xtensa_family=xtensa
123 xtensaeb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x5e'
124 xtensaeb_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
125 xtensaeb_family=xtensaeb
127 microblaze_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xba\xab'
128 microblaze_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
129 microblaze_family=microblaze
131 microblazeel_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xab\xba'
132 microblazeel_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
133 microblazeel_family=microblazeel
135 or1k_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x5c'
136 or1k_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
137 or1k_family=or1k
139 qemu_get_family() {
140 cpu=${HOST_ARCH:-$(uname -m)}
141 case "$cpu" in
142 amd64|i386|i486|i586|i686|i86pc|BePC|x86_64)
143 echo "i386"
145 mips*)
146 echo "mips"
148 "Power Macintosh"|ppc64|powerpc|ppc)
149 echo "ppc"
151 ppc64el|ppc64le)
152 echo "ppcle"
154 arm|armel|armhf|arm64|armv[4-9]*l|aarch64)
155 echo "arm"
157 armeb|armv[4-9]*b|aarch64_be)
158 echo "armeb"
160 sparc*)
161 echo "sparc"
163 riscv*)
164 echo "riscv"
167 echo "$cpu"
169 esac
172 usage() {
173 cat <<EOF
174 Usage: qemu-binfmt-conf.sh [--qemu-path PATH][--debian][--systemd CPU]
175 [--help][--credential yes|no][--exportdir PATH]
176 [--persistent yes|no][--qemu-suffix SUFFIX]
178 Configure binfmt_misc to use qemu interpreter
180 --help: display this usage
181 --qemu-path: set path to qemu interpreter ($QEMU_PATH)
182 --qemu-suffix: add a suffix to the default interpreter name
183 --debian: don't write into /proc,
184 instead generate update-binfmts templates
185 --systemd: don't write into /proc,
186 instead generate file for systemd-binfmt.service
187 for the given CPU. If CPU is "ALL", generate a
188 file for all known cpus
189 --exportdir: define where to write configuration files
190 (default: $SYSTEMDDIR or $DEBIANDIR)
191 --credential: if yes, credential and security tokens are
192 calculated according to the binary to interpret
193 --persistent: if yes, the interpreter is loaded when binfmt is
194 configured and remains in memory. All future uses
195 are cloned from the open file.
197 To import templates with update-binfmts, use :
199 sudo update-binfmts --importdir ${EXPORTDIR:-$DEBIANDIR} --import qemu-CPU
201 To remove interpreter, use :
203 sudo update-binfmts --package qemu-CPU --remove qemu-CPU $QEMU_PATH
205 With systemd, binfmt files are loaded by systemd-binfmt.service
207 The environment variable HOST_ARCH allows to override 'uname' to generate
208 configuration files for a different architecture than the current one.
210 where CPU is one of:
212 $qemu_target_list
217 qemu_check_access() {
218 if [ ! -w "$1" ] ; then
219 echo "ERROR: cannot write to $1" 1>&2
220 exit 1
224 qemu_check_bintfmt_misc() {
225 # load the binfmt_misc module
226 if [ ! -d /proc/sys/fs/binfmt_misc ]; then
227 if ! /sbin/modprobe binfmt_misc ; then
228 exit 1
231 if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then
232 if ! mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc ; then
233 exit 1
237 qemu_check_access /proc/sys/fs/binfmt_misc/register
240 installed_dpkg() {
241 dpkg --status "$1" > /dev/null 2>&1
244 qemu_check_debian() {
245 if [ ! -e /etc/debian_version ] ; then
246 echo "WARNING: your system is not a Debian based distro" 1>&2
247 elif ! installed_dpkg binfmt-support ; then
248 echo "WARNING: package binfmt-support is needed" 1>&2
250 qemu_check_access "$EXPORTDIR"
253 qemu_check_systemd() {
254 if ! systemctl -q is-enabled systemd-binfmt.service ; then
255 echo "WARNING: systemd-binfmt.service is missing or disabled" 1>&2
257 qemu_check_access "$EXPORTDIR"
260 qemu_generate_register() {
261 flags=""
262 if [ "$CREDENTIAL" = "yes" ] ; then
263 flags="OC"
265 if [ "$PERSISTENT" = "yes" ] ; then
266 flags="${flags}F"
269 echo ":qemu-$cpu:M::$magic:$mask:$qemu:$flags"
272 qemu_register_interpreter() {
273 echo "Setting $qemu as binfmt interpreter for $cpu"
274 qemu_generate_register > /proc/sys/fs/binfmt_misc/register
277 qemu_generate_systemd() {
278 echo "Setting $qemu as binfmt interpreter for $cpu for systemd-binfmt.service"
279 qemu_generate_register > "$EXPORTDIR/qemu-$cpu.conf"
282 qemu_generate_debian() {
283 cat > "$EXPORTDIR/qemu-$cpu" <<EOF
284 package qemu-$cpu
285 interpreter $qemu
286 magic $magic
287 mask $mask
288 credential $CREDENTIAL
292 qemu_set_binfmts() {
293 # probe cpu type
294 host_family=$(qemu_get_family)
296 # register the interpreter for each cpu except for the native one
298 for cpu in ${qemu_target_list} ; do
299 magic=$(eval echo \$${cpu}_magic)
300 mask=$(eval echo \$${cpu}_mask)
301 family=$(eval echo \$${cpu}_family)
303 if [ "$magic" = "" ] || [ "$mask" = "" ] || [ "$family" = "" ] ; then
304 echo "INTERNAL ERROR: unknown cpu $cpu" 1>&2
305 continue
308 qemu="$QEMU_PATH/qemu-$cpu"
309 if [ "$cpu" = "i486" ] ; then
310 qemu="$QEMU_PATH/qemu-i386"
313 qemu="$qemu$QEMU_SUFFIX"
314 if [ "$host_family" != "$family" ] ; then
315 $BINFMT_SET
317 done
320 CHECK=qemu_check_bintfmt_misc
321 BINFMT_SET=qemu_register_interpreter
323 SYSTEMDDIR="/etc/binfmt.d"
324 DEBIANDIR="/usr/share/binfmts"
326 QEMU_PATH=/usr/local/bin
327 CREDENTIAL=no
328 PERSISTENT=no
329 QEMU_SUFFIX=""
331 options=$(getopt -o ds:Q:S:e:hc:p: -l debian,systemd:,qemu-path:,qemu-suffix:,exportdir:,help,credential:,persistent: -- "$@")
332 eval set -- "$options"
334 while true ; do
335 case "$1" in
336 -d|--debian)
337 CHECK=qemu_check_debian
338 BINFMT_SET=qemu_generate_debian
339 EXPORTDIR=${EXPORTDIR:-$DEBIANDIR}
341 -s|--systemd)
342 CHECK=qemu_check_systemd
343 BINFMT_SET=qemu_generate_systemd
344 EXPORTDIR=${EXPORTDIR:-$SYSTEMDDIR}
345 shift
346 # check given cpu is in the supported CPU list
347 if [ "$1" != "ALL" ] ; then
348 for cpu in ${qemu_target_list} ; do
349 if [ "$cpu" = "$1" ] ; then
350 break
352 done
354 if [ "$cpu" = "$1" ] ; then
355 qemu_target_list="$1"
356 else
357 echo "ERROR: unknown CPU \"$1\"" 1>&2
358 usage
359 exit 1
363 -Q|--qemu-path)
364 shift
365 QEMU_PATH="$1"
367 -F|--qemu-suffix)
368 shift
369 QEMU_SUFFIX="$1"
371 -e|--exportdir)
372 shift
373 EXPORTDIR="$1"
375 -h|--help)
376 usage
377 exit 1
379 -c|--credential)
380 shift
381 CREDENTIAL="$1"
383 -p|--persistent)
384 shift
385 PERSISTENT="$1"
388 break
390 esac
391 shift
392 done
394 $CHECK
395 qemu_set_binfmts