master
sh 442 lines 15.6 KB
Raw
1 #!/bin/sh
2 # Enable automatic program execution by the kernel.
3
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 hexagon loongarch64"
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\x00\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\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
15 i486_family=i386
16
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
20
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
24
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
28
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
32
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
36
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
40
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
44
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
48
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
52
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
56
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
60
61 # FIXME: We could use the other endianness on a MIPS host.
62
63 mips_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
64 mips_mask='\xff\xff\xff\xff\xff\xff\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20'
65 mips_family=mips
66
67 mipsel_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
68 mipsel_mask='\xff\xff\xff\xff\xff\xff\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x00\x00\x00'
69 mipsel_family=mips
70
71 mipsn32_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20'
72 mipsn32_mask='\xff\xff\xff\xff\xff\xff\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20'
73 mipsn32_family=mips
74
75 mipsn32el_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x00\x00\x00'
76 mipsn32el_mask='\xff\xff\xff\xff\xff\xff\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x00\x00\x00'
77 mipsn32el_family=mips
78
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\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
81 mips64_family=mips
82
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\x00\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
85 mips64el_family=mips
86
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
90
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
94
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
98
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
102
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
106
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
110
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
114
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
118
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
122
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
126
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
130
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
134
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
138
139 hexagon_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xa4\x00'
140 hexagon_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
141 hexagon_family=hexagon
142
143 loongarch64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02\x01'
144 loongarch64_mask='\xff\xff\xff\xff\xff\xff\xff\xfc\x00\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
145 loongarch64_family=loongarch
146
147 # Converts the name of a host CPU architecture to the corresponding QEMU
148 # target.
149 #
150 # FIXME: This can probably be simplified a lot by dropping most entries.
151 # Remember that the script is only used on Linux, so we only need to
152 # handle the strings Linux uses to report the host CPU architecture.
153 qemu_normalize() {
154 cpu="$1"
155 case "$cpu" in
156 i[3-6]86)
157 echo "i386"
158 ;;
159 amd64)
160 echo "x86_64"
161 ;;
162 powerpc)
163 echo "ppc"
164 ;;
165 ppc64el)
166 echo "ppc64le"
167 ;;
168 armel|armhf|armv[4-9]*l)
169 echo "arm"
170 ;;
171 armv[4-9]*b)
172 echo "armeb"
173 ;;
174 arm64)
175 echo "aarch64"
176 ;;
177 *)
178 echo "$cpu"
179 ;;
180 esac
181 }
182
183 usage() {
184 cat <<EOF
185 Usage: qemu-binfmt-conf.sh [--qemu-path PATH][--debian][--systemd CPU]
186 [--help][--credential yes|no][--exportdir PATH]
187 [--persistent yes|no][--qemu-suffix SUFFIX]
188 [--preserve-argv0 yes|no]
189
190 Configure binfmt_misc to use qemu interpreter
191
192 --help: display this usage
193 --qemu-path: set path to qemu interpreter ($QEMU_PATH)
194 --qemu-suffix: add a suffix to the default interpreter name
195 --debian: don't write into /proc,
196 instead generate update-binfmts templates
197 --systemd: don't write into /proc,
198 instead generate file for systemd-binfmt.service
199 for the given CPU. If CPU is "ALL", generate a
200 file for all known cpus
201 --exportdir: define where to write configuration files
202 (default: $SYSTEMDDIR or $DEBIANDIR)
203 --credential: if yes, credential and security tokens are
204 calculated according to the binary to interpret
205 --persistent: if yes, the interpreter is loaded when binfmt is
206 configured and remains in memory. All future uses
207 are cloned from the open file.
208 --ignore-family: if yes, it is assumed that the host CPU (e.g. riscv64)
209 can't natively run programs targeting a CPU that is
210 part of the same family (e.g. riscv32).
211 --preserve-argv0 preserve argv[0]
212
213 To import templates with update-binfmts, use :
214
215 sudo update-binfmts --importdir ${EXPORTDIR:-$DEBIANDIR} --import qemu-CPU
216
217 To remove interpreter, use :
218
219 sudo update-binfmts --package qemu-CPU --remove qemu-CPU $QEMU_PATH
220
221 With systemd, binfmt files are loaded by systemd-binfmt.service
222
223 The environment variable HOST_ARCH allows to override 'uname' to generate
224 configuration files for a different architecture than the current one.
225
226 where CPU is one of:
227
228 $qemu_target_list
229
230 EOF
231 }
232
233 qemu_check_access() {
234 if [ ! -w "$1" ] ; then
235 echo "ERROR: cannot write to $1" 1>&2
236 exit 1
237 fi
238 }
239
240 qemu_check_bintfmt_misc() {
241 # load the binfmt_misc module
242 if [ ! -d /proc/sys/fs/binfmt_misc ]; then
243 if ! /sbin/modprobe binfmt_misc ; then
244 exit 1
245 fi
246 fi
247 if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then
248 if ! mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc ; then
249 exit 1
250 fi
251 fi
252
253 qemu_check_access /proc/sys/fs/binfmt_misc/register
254 }
255
256 installed_dpkg() {
257 dpkg --status "$1" > /dev/null 2>&1
258 }
259
260 qemu_check_debian() {
261 if [ ! -e /etc/debian_version ] ; then
262 echo "WARNING: your system is not a Debian based distro" 1>&2
263 elif ! installed_dpkg binfmt-support ; then
264 echo "WARNING: package binfmt-support is needed" 1>&2
265 fi
266 qemu_check_access "$EXPORTDIR"
267 }
268
269 qemu_check_systemd() {
270 if ! systemctl -q is-enabled systemd-binfmt.service ; then
271 echo "WARNING: systemd-binfmt.service is missing or disabled" 1>&2
272 fi
273 qemu_check_access "$EXPORTDIR"
274 }
275
276 qemu_generate_register() {
277 flags=""
278 if [ "$CREDENTIAL" = "yes" ] ; then
279 flags="OC"
280 fi
281 if [ "$PERSISTENT" = "yes" ] ; then
282 flags="${flags}F"
283 fi
284 if [ "$PRESERVE_ARG0" = "yes" ] ; then
285 flags="${flags}P"
286 fi
287
288 echo ":qemu-$cpu:M::$magic:$mask:$qemu:$flags"
289 }
290
291 qemu_register_interpreter() {
292 echo "Setting $qemu as binfmt interpreter for $cpu"
293 qemu_generate_register > /proc/sys/fs/binfmt_misc/register
294 }
295
296 qemu_generate_systemd() {
297 echo "Setting $qemu as binfmt interpreter for $cpu for systemd-binfmt.service"
298 qemu_generate_register > "$EXPORTDIR/qemu-$cpu.conf"
299 }
300
301 qemu_generate_debian() {
302 cat > "$EXPORTDIR/qemu-$cpu" <<EOF
303 package qemu-$cpu
304 interpreter $qemu
305 magic $magic
306 mask $mask
307 credentials $CREDENTIAL
308 preserve $PRESERVE_ARG0
309 fix_binary $PERSISTENT
310 EOF
311 }
312
313 qemu_set_binfmts() {
314 # probe cpu type
315 host_cpu=$(qemu_normalize ${HOST_ARCH:-$(uname -m)})
316 host_family=$(eval echo \$${host_cpu}_family)
317
318 if [ "$host_family" = "" ] ; then
319 echo "INTERNAL ERROR: unknown host cpu $host_cpu" 1>&2
320 exit 1
321 fi
322
323 # register the interpreter for each cpu except for the native one
324
325 for cpu in ${qemu_target_list} ; do
326 magic=$(eval echo \$${cpu}_magic)
327 mask=$(eval echo \$${cpu}_mask)
328 family=$(eval echo \$${cpu}_family)
329
330 target="$cpu"
331 if [ "$cpu" = "i486" ] ; then
332 target="i386"
333 fi
334
335 qemu="$QEMU_PATH/qemu-$target$QEMU_SUFFIX"
336
337 if [ "$magic" = "" ] || [ "$mask" = "" ] || [ "$family" = "" ] ; then
338 echo "INTERNAL ERROR: unknown cpu $cpu" 1>&2
339 continue
340 fi
341
342 if [ "$host_family" = "$family" ] ; then
343 # When --ignore-family is used, we have to generate rules even
344 # for targets that are in the same family as the host CPU. The
345 # only exception is of course when the CPU types exactly match
346 if [ "$target" = "$host_cpu" ] || [ "$IGNORE_FAMILY" = "no" ] ; then
347 continue
348 fi
349 fi
350
351 $BINFMT_SET
352 done
353 }
354
355 CHECK=qemu_check_bintfmt_misc
356 BINFMT_SET=qemu_register_interpreter
357
358 SYSTEMDDIR="/etc/binfmt.d"
359 DEBIANDIR="/usr/share/binfmts"
360
361 QEMU_PATH=/usr/local/bin
362 CREDENTIAL=no
363 PERSISTENT=no
364 PRESERVE_ARG0=no
365 QEMU_SUFFIX=""
366 IGNORE_FAMILY=no
367
368 _longopts="debian,systemd:,qemu-path:,qemu-suffix:,exportdir:,help,credential:,\
369 persistent:,preserve-argv0:,ignore-family:"
370 options=$(getopt -o ds:Q:S:e:hc:p:g:F:i: -l ${_longopts} -- "$@")
371 eval set -- "$options"
372
373 while true ; do
374 case "$1" in
375 -d|--debian)
376 CHECK=qemu_check_debian
377 BINFMT_SET=qemu_generate_debian
378 EXPORTDIR=${EXPORTDIR:-$DEBIANDIR}
379 ;;
380 -s|--systemd)
381 CHECK=qemu_check_systemd
382 BINFMT_SET=qemu_generate_systemd
383 EXPORTDIR=${EXPORTDIR:-$SYSTEMDDIR}
384 shift
385 # check given cpu is in the supported CPU list
386 if [ "$1" != "ALL" ] ; then
387 for cpu in ${qemu_target_list} ; do
388 if [ "$cpu" = "$1" ] ; then
389 break
390 fi
391 done
392
393 if [ "$cpu" = "$1" ] ; then
394 qemu_target_list="$1"
395 else
396 echo "ERROR: unknown CPU \"$1\"" 1>&2
397 usage
398 exit 1
399 fi
400 fi
401 ;;
402 -Q|--qemu-path)
403 shift
404 QEMU_PATH="$1"
405 ;;
406 -F|--qemu-suffix)
407 shift
408 QEMU_SUFFIX="$1"
409 ;;
410 -e|--exportdir)
411 shift
412 EXPORTDIR="$1"
413 ;;
414 -h|--help)
415 usage
416 exit 1
417 ;;
418 -c|--credential)
419 shift
420 CREDENTIAL="$1"
421 ;;
422 -p|--persistent)
423 shift
424 PERSISTENT="$1"
425 ;;
426 -g|--preserve-argv0)
427 shift
428 PRESERVE_ARG0="$1"
429 ;;
430 -i|--ignore-family)
431 shift
432 IGNORE_FAMILY="$1"
433 ;;
434 *)
435 break
436 ;;
437 esac
438 shift
439 done
440
441 $CHECK
442 qemu_set_binfmts