util: drop unix_nonblocking_connect()
[qemu/cris-port.git] / scripts / qemu-binfmt-conf.sh
blobde4d1c13d49baa0435c486a4fd824f5d889a2e9e
1 #!/bin/sh
2 # enable automatic i386/ARM/M68K/MIPS/SPARC/PPC/s390 program execution by the kernel
4 qemu_target_list="i386 i486 alpha arm sparc32plus ppc ppc64 ppc64le m68k \
5 mips mipsel mipsn32 mipsn32el mips64 mips64el \
6 sh4 sh4eb s390x aarch64"
8 i386_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00'
9 i386_mask='\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
10 i386_family=i386
12 i486_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x00'
13 i486_mask='\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
14 i486_family=i386
16 alpha_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x26\x90'
17 alpha_mask='\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
18 alpha_family=alpha
20 arm_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00'
21 arm_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
22 arm_family=arm
24 armeb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28'
25 armeb_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
26 armeb_family=arm
28 sparc_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02'
29 sparc_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
30 sparc_family=sparc
32 sparc32plus_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x12'
33 sparc32plus_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
34 sparc32plus_family=sparc
36 ppc_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14'
37 ppc_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
38 ppc_family=ppc
40 ppc64_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15'
41 ppc64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
42 ppc64_family=ppc
44 ppc64le_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00'
45 ppc64le_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00'
46 ppc64le_family=ppcle
48 m68k_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x04'
49 m68k_mask='\xff\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
50 m68k_family=m68k
52 # FIXME: We could use the other endianness on a MIPS host.
54 mips_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
55 mips_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
56 mips_family=mips
58 mipsel_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
59 mipsel_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
60 mipsel_family=mips
62 mipsn32_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
63 mipsn32_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
64 mipsn32_family=mips
66 mipsn32el_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
67 mipsn32el_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
68 mipsn32el_family=mips
70 mips64_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'
71 mips64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
72 mips64_family=mips
74 mips64el_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'
75 mips64el_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
76 mips64el_family=mips
78 sh4_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a\x00'
79 sh4_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
80 sh4_family=sh4
82 sh4eb_magic='\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x2a'
83 sh4eb_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
84 sh4eb_family=sh4
86 s390x_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x16'
87 s390x_mask='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
88 s390x_family=s390x
90 aarch64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00'
91 aarch64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
92 aarch64_family=arm
94 qemu_get_family() {
95 cpu=${HOST_ARCH:-$(uname -m)}
96 case "$cpu" in
97 amd64|i386|i486|i586|i686|i86pc|BePC|x86_64)
98 echo "i386"
100 mips*)
101 echo "mips"
103 "Power Macintosh"|ppc64|powerpc|ppc)
104 echo "ppc"
106 ppc64el|ppc64le)
107 echo "ppcle"
109 arm|armel|armhf|arm64|armv[4-9]*)
110 echo "arm"
112 sparc*)
113 echo "sparc"
116 echo "$cpu"
118 esac
121 usage() {
122 cat <<EOF
123 Usage: qemu-binfmt-conf.sh [--qemu-path PATH][--debian][--systemd CPU]
124 [--help][--credential yes|no][--exportdir PATH]
126 Configure binfmt_misc to use qemu interpreter
128 --help: display this usage
129 --qemu-path: set path to qemu interpreter ($QEMU_PATH)
130 --debian: don't write into /proc,
131 instead generate update-binfmts templates
132 --systemd: don't write into /proc,
133 instead generate file for systemd-binfmt.service
134 for the given CPU
135 --exportdir: define where to write configuration files
136 (default: $SYSTEMDDIR or $DEBIANDIR)
137 --credential: if yes, credential and security tokens are
138 calculated according to the binary to interpret
140 To import templates with update-binfmts, use :
142 sudo update-binfmts --importdir ${EXPORTDIR:-$DEBIANDIR} --import qemu-CPU
144 To remove interpreter, use :
146 sudo update-binfmts --package qemu-CPU --remove qemu-CPU $QEMU_PATH
148 With systemd, binfmt files are loaded by systemd-binfmt.service
150 The environment variable HOST_ARCH allows to override 'uname' to generate
151 configuration files for a different architecture than the current one.
153 where CPU is one of:
155 $qemu_target_list
160 qemu_check_access() {
161 if [ ! -w "$1" ] ; then
162 echo "ERROR: cannot write to $1" 1>&2
163 exit 1
167 qemu_check_bintfmt_misc() {
168 # load the binfmt_misc module
169 if [ ! -d /proc/sys/fs/binfmt_misc ]; then
170 if ! /sbin/modprobe binfmt_misc ; then
171 exit 1
174 if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then
175 if ! mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc ; then
176 exit 1
180 qemu_check_access /proc/sys/fs/binfmt_misc/register
183 installed_dpkg() {
184 dpkg --status "$1" > /dev/null 2>&1
187 qemu_check_debian() {
188 if [ ! -e /etc/debian_version ] ; then
189 echo "WARNING: your system is not a Debian based distro" 1>&2
190 elif ! installed_dpkg binfmt-support ; then
191 echo "WARNING: package binfmt-support is needed" 1>&2
193 qemu_check_access "$EXPORTDIR"
196 qemu_check_systemd() {
197 if ! systemctl -q is-enabled systemd-binfmt.service ; then
198 echo "WARNING: systemd-binfmt.service is missing or disabled" 1>&2
200 qemu_check_access "$EXPORTDIR"
203 qemu_generate_register() {
204 echo ":qemu-$cpu:M::$magic:$mask:$qemu:$FLAGS"
207 qemu_register_interpreter() {
208 echo "Setting $qemu as binfmt interpreter for $cpu"
209 qemu_generate_register > /proc/sys/fs/binfmt_misc/register
212 qemu_generate_systemd() {
213 echo "Setting $qemu as binfmt interpreter for $cpu for systemd-binfmt.service"
214 qemu_generate_register > "$EXPORTDIR/qemu-$cpu.conf"
217 qemu_generate_debian() {
218 cat > "$EXPORTDIR/qemu-$cpu" <<EOF
219 package qemu-$cpu
220 interpreter $qemu
221 magic $magic
222 mask $mask
224 if [ "$FLAGS" = "OC" ] ; then
225 echo "credentials yes" >> "$EXPORTDIR/qemu-$cpu"
229 qemu_set_binfmts() {
230 # probe cpu type
231 host_family=$(qemu_get_family)
233 # register the interpreter for each cpu except for the native one
235 for cpu in ${qemu_target_list} ; do
236 magic=$(eval echo \$${cpu}_magic)
237 mask=$(eval echo \$${cpu}_mask)
238 family=$(eval echo \$${cpu}_family)
240 if [ "$magic" = "" ] || [ "$mask" = "" ] || [ "$family" = "" ] ; then
241 echo "INTERNAL ERROR: unknown cpu $cpu" 1>&2
242 continue
245 qemu="$QEMU_PATH/qemu-$cpu"
246 if [ "$cpu" = "i486" ] ; then
247 qemu="$QEMU_PATH/qemu-i386"
250 if [ "$host_family" != "$family" ] ; then
251 $BINFMT_SET
253 done
256 CHECK=qemu_check_bintfmt_misc
257 BINFMT_SET=qemu_register_interpreter
259 SYSTEMDDIR="/etc/binfmt.d"
260 DEBIANDIR="/usr/share/binfmts"
262 QEMU_PATH=/usr/local/bin
263 FLAGS=""
265 options=$(getopt -o ds:Q:e:hc: -l debian,systemd:,qemu-path:,exportdir:,help,credential: -- "$@")
266 eval set -- "$options"
268 while true ; do
269 case "$1" in
270 -d|--debian)
271 CHECK=qemu_check_debian
272 BINFMT_SET=qemu_generate_debian
273 EXPORTDIR=${EXPORTDIR:-$DEBIANDIR}
275 -s|--systemd)
276 CHECK=qemu_check_systemd
277 BINFMT_SET=qemu_generate_systemd
278 EXPORTDIR=${EXPORTDIR:-$SYSTEMDDIR}
279 shift
280 # check given cpu is in the supported CPU list
281 for cpu in ${qemu_target_list} ; do
282 if [ "$cpu" == "$1" ] ; then
283 break
285 done
287 if [ "$cpu" == "$1" ] ; then
288 qemu_target_list="$1"
289 else
290 echo "ERROR: unknown CPU \"$1\"" 1>&2
291 usage
292 exit 1
295 -Q|--qemu-path)
296 shift
297 QEMU_PATH="$1"
299 -e|--exportdir)
300 shift
301 EXPORTDIR="$1"
303 -h|--help)
304 usage
305 exit 1
307 -c|--credential)
308 shift
309 if [ "$1" = "yes" ] ; then
310 FLAGS="OC"
311 else
312 FLAGS=""
316 break
318 esac
319 shift
320 done
322 $CHECK
323 qemu_set_binfmts