]> git.proxmox.com Git - mirror_qemu.git/blob - scripts/qemu-binfmt-conf.sh
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[mirror_qemu.git] / scripts / qemu-binfmt-conf.sh
1 #!/bin/sh
2 # enable automatic i386/ARM/M68K/MIPS/SPARC/PPC/s390/HPPA/Xtensa
3 # program execution by the kernel
4
5 qemu_target_list="i386 i486 alpha arm armeb sparc32plus ppc ppc64 ppc64le m68k \
6 mips mipsel mipsn32 mipsn32el mips64 mips64el \
7 sh4 sh4eb s390x aarch64 aarch64_be hppa riscv32 riscv64 xtensa xtensaeb"
8
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\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
11 i386_family=i386
12
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\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
15 i486_family=i386
16
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\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
19 alpha_family=alpha
20
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
24
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
28
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\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
31 sparc_family=sparc
32
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\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
35 sparc32plus_family=sparc
36
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
40
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
44
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
48
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\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
51 m68k_family=m68k
52
53 # FIXME: We could use the other endianness on a MIPS host.
54
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
58
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
62
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
66
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
70
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
74
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
78
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\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
81 sh4_family=sh4
82
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\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
85 sh4eb_family=sh4
86
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\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
89 s390x_family=s390x
90
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
94
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
98
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
102
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
106
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
110
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
114
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
118
119 qemu_get_family() {
120 cpu=${HOST_ARCH:-$(uname -m)}
121 case "$cpu" in
122 amd64|i386|i486|i586|i686|i86pc|BePC|x86_64)
123 echo "i386"
124 ;;
125 mips*)
126 echo "mips"
127 ;;
128 "Power Macintosh"|ppc64|powerpc|ppc)
129 echo "ppc"
130 ;;
131 ppc64el|ppc64le)
132 echo "ppcle"
133 ;;
134 arm|armel|armhf|arm64|armv[4-9]*l|aarch64)
135 echo "arm"
136 ;;
137 armeb|armv[4-9]*b|aarch64_be)
138 echo "armeb"
139 ;;
140 sparc*)
141 echo "sparc"
142 ;;
143 riscv*)
144 echo "riscv"
145 ;;
146 *)
147 echo "$cpu"
148 ;;
149 esac
150 }
151
152 usage() {
153 cat <<EOF
154 Usage: qemu-binfmt-conf.sh [--qemu-path PATH][--debian][--systemd CPU]
155 [--help][--credential yes|no][--exportdir PATH]
156
157 Configure binfmt_misc to use qemu interpreter
158
159 --help: display this usage
160 --qemu-path: set path to qemu interpreter ($QEMU_PATH)
161 --debian: don't write into /proc,
162 instead generate update-binfmts templates
163 --systemd: don't write into /proc,
164 instead generate file for systemd-binfmt.service
165 for the given CPU. If CPU is "ALL", generate a
166 file for all known cpus
167 --exportdir: define where to write configuration files
168 (default: $SYSTEMDDIR or $DEBIANDIR)
169 --credential: if yes, credential and security tokens are
170 calculated according to the binary to interpret
171
172 To import templates with update-binfmts, use :
173
174 sudo update-binfmts --importdir ${EXPORTDIR:-$DEBIANDIR} --import qemu-CPU
175
176 To remove interpreter, use :
177
178 sudo update-binfmts --package qemu-CPU --remove qemu-CPU $QEMU_PATH
179
180 With systemd, binfmt files are loaded by systemd-binfmt.service
181
182 The environment variable HOST_ARCH allows to override 'uname' to generate
183 configuration files for a different architecture than the current one.
184
185 where CPU is one of:
186
187 $qemu_target_list
188
189 EOF
190 }
191
192 qemu_check_access() {
193 if [ ! -w "$1" ] ; then
194 echo "ERROR: cannot write to $1" 1>&2
195 exit 1
196 fi
197 }
198
199 qemu_check_bintfmt_misc() {
200 # load the binfmt_misc module
201 if [ ! -d /proc/sys/fs/binfmt_misc ]; then
202 if ! /sbin/modprobe binfmt_misc ; then
203 exit 1
204 fi
205 fi
206 if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then
207 if ! mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc ; then
208 exit 1
209 fi
210 fi
211
212 qemu_check_access /proc/sys/fs/binfmt_misc/register
213 }
214
215 installed_dpkg() {
216 dpkg --status "$1" > /dev/null 2>&1
217 }
218
219 qemu_check_debian() {
220 if [ ! -e /etc/debian_version ] ; then
221 echo "WARNING: your system is not a Debian based distro" 1>&2
222 elif ! installed_dpkg binfmt-support ; then
223 echo "WARNING: package binfmt-support is needed" 1>&2
224 fi
225 qemu_check_access "$EXPORTDIR"
226 }
227
228 qemu_check_systemd() {
229 if ! systemctl -q is-enabled systemd-binfmt.service ; then
230 echo "WARNING: systemd-binfmt.service is missing or disabled" 1>&2
231 fi
232 qemu_check_access "$EXPORTDIR"
233 }
234
235 qemu_generate_register() {
236 echo ":qemu-$cpu:M::$magic:$mask:$qemu:$FLAGS"
237 }
238
239 qemu_register_interpreter() {
240 echo "Setting $qemu as binfmt interpreter for $cpu"
241 qemu_generate_register > /proc/sys/fs/binfmt_misc/register
242 }
243
244 qemu_generate_systemd() {
245 echo "Setting $qemu as binfmt interpreter for $cpu for systemd-binfmt.service"
246 qemu_generate_register > "$EXPORTDIR/qemu-$cpu.conf"
247 }
248
249 qemu_generate_debian() {
250 cat > "$EXPORTDIR/qemu-$cpu" <<EOF
251 package qemu-$cpu
252 interpreter $qemu
253 magic $magic
254 mask $mask
255 EOF
256 if [ "$FLAGS" = "OC" ] ; then
257 echo "credentials yes" >> "$EXPORTDIR/qemu-$cpu"
258 fi
259 }
260
261 qemu_set_binfmts() {
262 # probe cpu type
263 host_family=$(qemu_get_family)
264
265 # register the interpreter for each cpu except for the native one
266
267 for cpu in ${qemu_target_list} ; do
268 magic=$(eval echo \$${cpu}_magic)
269 mask=$(eval echo \$${cpu}_mask)
270 family=$(eval echo \$${cpu}_family)
271
272 if [ "$magic" = "" ] || [ "$mask" = "" ] || [ "$family" = "" ] ; then
273 echo "INTERNAL ERROR: unknown cpu $cpu" 1>&2
274 continue
275 fi
276
277 qemu="$QEMU_PATH/qemu-$cpu"
278 if [ "$cpu" = "i486" ] ; then
279 qemu="$QEMU_PATH/qemu-i386"
280 fi
281
282 if [ "$host_family" != "$family" ] ; then
283 $BINFMT_SET
284 fi
285 done
286 }
287
288 CHECK=qemu_check_bintfmt_misc
289 BINFMT_SET=qemu_register_interpreter
290
291 SYSTEMDDIR="/etc/binfmt.d"
292 DEBIANDIR="/usr/share/binfmts"
293
294 QEMU_PATH=/usr/local/bin
295 FLAGS=""
296
297 options=$(getopt -o ds:Q:e:hc: -l debian,systemd:,qemu-path:,exportdir:,help,credential: -- "$@")
298 eval set -- "$options"
299
300 while true ; do
301 case "$1" in
302 -d|--debian)
303 CHECK=qemu_check_debian
304 BINFMT_SET=qemu_generate_debian
305 EXPORTDIR=${EXPORTDIR:-$DEBIANDIR}
306 ;;
307 -s|--systemd)
308 CHECK=qemu_check_systemd
309 BINFMT_SET=qemu_generate_systemd
310 EXPORTDIR=${EXPORTDIR:-$SYSTEMDDIR}
311 shift
312 # check given cpu is in the supported CPU list
313 if [ "$1" != "ALL" ] ; then
314 for cpu in ${qemu_target_list} ; do
315 if [ "$cpu" = "$1" ] ; then
316 break
317 fi
318 done
319
320 if [ "$cpu" = "$1" ] ; then
321 qemu_target_list="$1"
322 else
323 echo "ERROR: unknown CPU \"$1\"" 1>&2
324 usage
325 exit 1
326 fi
327 fi
328 ;;
329 -Q|--qemu-path)
330 shift
331 QEMU_PATH="$1"
332 ;;
333 -e|--exportdir)
334 shift
335 EXPORTDIR="$1"
336 ;;
337 -h|--help)
338 usage
339 exit 1
340 ;;
341 -c|--credential)
342 shift
343 if [ "$1" = "yes" ] ; then
344 FLAGS="OC"
345 else
346 FLAGS=""
347 fi
348 ;;
349 *)
350 break
351 ;;
352 esac
353 shift
354 done
355
356 $CHECK
357 qemu_set_binfmts