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