memory: Refactor common shifting code from accessors
[qemu.git] / scripts / qemu-binfmt-conf.sh
blobb5a16742a149c0d2e08c6a34d08ef489f38dce7a
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 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 ppc_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14'
42 ppc_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
43 ppc_family=ppc
45 ppc64_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15'
46 ppc64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
47 ppc64_family=ppc
49 ppc64le_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00'
50 ppc64le_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00'
51 ppc64le_family=ppcle
53 m68k_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x04'
54 m68k_mask='\xff\xff\xff\xff\xff\xff\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
55 m68k_family=m68k
57 # FIXME: We could use the other endianness on a MIPS host.
59 mips_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
60 mips_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
61 mips_family=mips
63 mipsel_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
64 mipsel_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
65 mipsel_family=mips
67 mipsn32_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
68 mipsn32_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
69 mipsn32_family=mips
71 mipsn32el_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
72 mipsn32el_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
73 mipsn32el_family=mips
75 mips64_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
76 mips64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
77 mips64_family=mips
79 mips64el_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
80 mips64el_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
81 mips64el_family=mips
83 sh4_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a\x00'
84 sh4_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
85 sh4_family=sh4
87 sh4eb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a'
88 sh4eb_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
89 sh4eb_family=sh4
91 s390x_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x16'
92 s390x_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
93 s390x_family=s390x
95 aarch64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00'
96 aarch64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
97 aarch64_family=arm
99 aarch64_be_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7'
100 aarch64_be_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
101 aarch64_be_family=armeb
103 hppa_magic='\x7f\x45\x4c\x46\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x0f'
104 hppa_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
105 hppa_family=hppa
107 riscv32_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00'
108 riscv32_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
109 riscv32_family=riscv
111 riscv64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00'
112 riscv64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
113 riscv64_family=riscv
115 xtensa_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x5e\x00'
116 xtensa_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
117 xtensa_family=xtensa
119 xtensaeb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x5e'
120 xtensaeb_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
121 xtensaeb_family=xtensaeb
123 microblaze_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xba\xab'
124 microblaze_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
125 microblaze_family=microblaze
127 microblazeel_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xab\xba'
128 microblazeel_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
129 microblazeel_family=microblazeel
131 or1k_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x5c'
132 or1k_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
133 or1k_family=or1k
135 qemu_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"
141 mips*)
142 echo "mips"
144 "Power Macintosh"|ppc64|powerpc|ppc)
145 echo "ppc"
147 ppc64el|ppc64le)
148 echo "ppcle"
150 arm|armel|armhf|arm64|armv[4-9]*l|aarch64)
151 echo "arm"
153 armeb|armv[4-9]*b|aarch64_be)
154 echo "armeb"
156 sparc*)
157 echo "sparc"
159 riscv*)
160 echo "riscv"
163 echo "$cpu"
165 esac
168 usage() {
169 cat <<EOF
170 Usage: qemu-binfmt-conf.sh [--qemu-path PATH][--debian][--systemd CPU]
171 [--help][--credential yes|no][--exportdir PATH]
172 [--persistent yes|no][--qemu-suffix SUFFIX]
174 Configure binfmt_misc to use qemu interpreter
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.
193 To import templates with update-binfmts, use :
195 sudo update-binfmts --importdir ${EXPORTDIR:-$DEBIANDIR} --import qemu-CPU
197 To remove interpreter, use :
199 sudo update-binfmts --package qemu-CPU --remove qemu-CPU $QEMU_PATH
201 With systemd, binfmt files are loaded by systemd-binfmt.service
203 The environment variable HOST_ARCH allows to override 'uname' to generate
204 configuration files for a different architecture than the current one.
206 where CPU is one of:
208 $qemu_target_list
213 qemu_check_access() {
214 if [ ! -w "$1" ] ; then
215 echo "ERROR: cannot write to $1" 1>&2
216 exit 1
220 qemu_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
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
233 qemu_check_access /proc/sys/fs/binfmt_misc/register
236 installed_dpkg() {
237 dpkg --status "$1" > /dev/null 2>&1
240 qemu_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
246 qemu_check_access "$EXPORTDIR"
249 qemu_check_systemd() {
250 if ! systemctl -q is-enabled systemd-binfmt.service ; then
251 echo "WARNING: systemd-binfmt.service is missing or disabled" 1>&2
253 qemu_check_access "$EXPORTDIR"
256 qemu_generate_register() {
257 flags=""
258 if [ "$CREDENTIAL" = "yes" ] ; then
259 flags="OC"
261 if [ "$PERSISTENT" = "yes" ] ; then
262 flags="${flags}F"
265 echo ":qemu-$cpu:M::$magic:$mask:$qemu:$flags"
268 qemu_register_interpreter() {
269 echo "Setting $qemu as binfmt interpreter for $cpu"
270 qemu_generate_register > /proc/sys/fs/binfmt_misc/register
273 qemu_generate_systemd() {
274 echo "Setting $qemu as binfmt interpreter for $cpu for systemd-binfmt.service"
275 qemu_generate_register > "$EXPORTDIR/qemu-$cpu.conf"
278 qemu_generate_debian() {
279 cat > "$EXPORTDIR/qemu-$cpu" <<EOF
280 package qemu-$cpu
281 interpreter $qemu
282 magic $magic
283 mask $mask
284 credential $CREDENTIAL
288 qemu_set_binfmts() {
289 # probe cpu type
290 host_family=$(qemu_get_family)
292 # register the interpreter for each cpu except for the native one
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)
299 if [ "$magic" = "" ] || [ "$mask" = "" ] || [ "$family" = "" ] ; then
300 echo "INTERNAL ERROR: unknown cpu $cpu" 1>&2
301 continue
304 qemu="$QEMU_PATH/qemu-$cpu"
305 if [ "$cpu" = "i486" ] ; then
306 qemu="$QEMU_PATH/qemu-i386"
309 qemu="$qemu$QEMU_SUFFIX"
310 if [ "$host_family" != "$family" ] ; then
311 $BINFMT_SET
313 done
316 CHECK=qemu_check_bintfmt_misc
317 BINFMT_SET=qemu_register_interpreter
319 SYSTEMDDIR="/etc/binfmt.d"
320 DEBIANDIR="/usr/share/binfmts"
322 QEMU_PATH=/usr/local/bin
323 CREDENTIAL=no
324 PERSISTENT=no
325 QEMU_SUFFIX=""
327 options=$(getopt -o ds:Q:S:e:hc:p: -l debian,systemd:,qemu-path:,qemu-suffix:,exportdir:,help,credential:,persistent: -- "$@")
328 eval set -- "$options"
330 while true ; do
331 case "$1" in
332 -d|--debian)
333 CHECK=qemu_check_debian
334 BINFMT_SET=qemu_generate_debian
335 EXPORTDIR=${EXPORTDIR:-$DEBIANDIR}
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
348 done
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
359 -Q|--qemu-path)
360 shift
361 QEMU_PATH="$1"
363 -F|--qemu-suffix)
364 shift
365 QEMU_SUFFIX="$1"
367 -e|--exportdir)
368 shift
369 EXPORTDIR="$1"
371 -h|--help)
372 usage
373 exit 1
375 -c|--credential)
376 shift
377 CREDENTIAL="$1"
379 -p|--persistent)
380 shift
381 PERSISTENT="$1"
384 break
386 esac
387 shift
388 done
390 $CHECK
391 qemu_set_binfmts