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 riscv32 riscv64" 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 103riscv32_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00' 104riscv32_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' 105riscv32_family=riscv 106 107riscv64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00' 108riscv64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' 109riscv64_family=riscv 110 111qemu_get_family() { 112 cpu=${HOST_ARCH:-$(uname -m)} 113 case "$cpu" in 114 amd64|i386|i486|i586|i686|i86pc|BePC|x86_64) 115 echo "i386" 116 ;; 117 mips*) 118 echo "mips" 119 ;; 120 "Power Macintosh"|ppc64|powerpc|ppc) 121 echo "ppc" 122 ;; 123 ppc64el|ppc64le) 124 echo "ppcle" 125 ;; 126 arm|armel|armhf|arm64|armv[4-9]*l|aarch64) 127 echo "arm" 128 ;; 129 armeb|armv[4-9]*b|aarch64_be) 130 echo "armeb" 131 ;; 132 sparc*) 133 echo "sparc" 134 ;; 135 riscv*) 136 echo "riscv" 137 ;; 138 *) 139 echo "$cpu" 140 ;; 141 esac 142} 143 144usage() { 145 cat <<EOF 146Usage: qemu-binfmt-conf.sh [--qemu-path PATH][--debian][--systemd CPU] 147 [--help][--credential yes|no][--exportdir PATH] 148 149 Configure binfmt_misc to use qemu interpreter 150 151 --help: display this usage 152 --qemu-path: set path to qemu interpreter ($QEMU_PATH) 153 --debian: don't write into /proc, 154 instead generate update-binfmts templates 155 --systemd: don't write into /proc, 156 instead generate file for systemd-binfmt.service 157 for the given CPU 158 --exportdir: define where to write configuration files 159 (default: $SYSTEMDDIR or $DEBIANDIR) 160 --credential: if yes, credential and security tokens are 161 calculated according to the binary to interpret 162 163 To import templates with update-binfmts, use : 164 165 sudo update-binfmts --importdir ${EXPORTDIR:-$DEBIANDIR} --import qemu-CPU 166 167 To remove interpreter, use : 168 169 sudo update-binfmts --package qemu-CPU --remove qemu-CPU $QEMU_PATH 170 171 With systemd, binfmt files are loaded by systemd-binfmt.service 172 173 The environment variable HOST_ARCH allows to override 'uname' to generate 174 configuration files for a different architecture than the current one. 175 176 where CPU is one of: 177 178 $qemu_target_list 179 180EOF 181} 182 183qemu_check_access() { 184 if [ ! -w "$1" ] ; then 185 echo "ERROR: cannot write to $1" 1>&2 186 exit 1 187 fi 188} 189 190qemu_check_bintfmt_misc() { 191 # load the binfmt_misc module 192 if [ ! -d /proc/sys/fs/binfmt_misc ]; then 193 if ! /sbin/modprobe binfmt_misc ; then 194 exit 1 195 fi 196 fi 197 if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then 198 if ! mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc ; then 199 exit 1 200 fi 201 fi 202 203 qemu_check_access /proc/sys/fs/binfmt_misc/register 204} 205 206installed_dpkg() { 207 dpkg --status "$1" > /dev/null 2>&1 208} 209 210qemu_check_debian() { 211 if [ ! -e /etc/debian_version ] ; then 212 echo "WARNING: your system is not a Debian based distro" 1>&2 213 elif ! installed_dpkg binfmt-support ; then 214 echo "WARNING: package binfmt-support is needed" 1>&2 215 fi 216 qemu_check_access "$EXPORTDIR" 217} 218 219qemu_check_systemd() { 220 if ! systemctl -q is-enabled systemd-binfmt.service ; then 221 echo "WARNING: systemd-binfmt.service is missing or disabled" 1>&2 222 fi 223 qemu_check_access "$EXPORTDIR" 224} 225 226qemu_generate_register() { 227 echo ":qemu-$cpu:M::$magic:$mask:$qemu:$FLAGS" 228} 229 230qemu_register_interpreter() { 231 echo "Setting $qemu as binfmt interpreter for $cpu" 232 qemu_generate_register > /proc/sys/fs/binfmt_misc/register 233} 234 235qemu_generate_systemd() { 236 echo "Setting $qemu as binfmt interpreter for $cpu for systemd-binfmt.service" 237 qemu_generate_register > "$EXPORTDIR/qemu-$cpu.conf" 238} 239 240qemu_generate_debian() { 241 cat > "$EXPORTDIR/qemu-$cpu" <<EOF 242package qemu-$cpu 243interpreter $qemu 244magic $magic 245mask $mask 246EOF 247 if [ "$FLAGS" = "OC" ] ; then 248 echo "credentials yes" >> "$EXPORTDIR/qemu-$cpu" 249 fi 250} 251 252qemu_set_binfmts() { 253 # probe cpu type 254 host_family=$(qemu_get_family) 255 256 # register the interpreter for each cpu except for the native one 257 258 for cpu in ${qemu_target_list} ; do 259 magic=$(eval echo \$${cpu}_magic) 260 mask=$(eval echo \$${cpu}_mask) 261 family=$(eval echo \$${cpu}_family) 262 263 if [ "$magic" = "" ] || [ "$mask" = "" ] || [ "$family" = "" ] ; then 264 echo "INTERNAL ERROR: unknown cpu $cpu" 1>&2 265 continue 266 fi 267 268 qemu="$QEMU_PATH/qemu-$cpu" 269 if [ "$cpu" = "i486" ] ; then 270 qemu="$QEMU_PATH/qemu-i386" 271 fi 272 273 if [ "$host_family" != "$family" ] ; then 274 $BINFMT_SET 275 fi 276 done 277} 278 279CHECK=qemu_check_bintfmt_misc 280BINFMT_SET=qemu_register_interpreter 281 282SYSTEMDDIR="/etc/binfmt.d" 283DEBIANDIR="/usr/share/binfmts" 284 285QEMU_PATH=/usr/local/bin 286FLAGS="" 287 288options=$(getopt -o ds:Q:e:hc: -l debian,systemd:,qemu-path:,exportdir:,help,credential: -- "$@") 289eval set -- "$options" 290 291while true ; do 292 case "$1" in 293 -d|--debian) 294 CHECK=qemu_check_debian 295 BINFMT_SET=qemu_generate_debian 296 EXPORTDIR=${EXPORTDIR:-$DEBIANDIR} 297 ;; 298 -s|--systemd) 299 CHECK=qemu_check_systemd 300 BINFMT_SET=qemu_generate_systemd 301 EXPORTDIR=${EXPORTDIR:-$SYSTEMDDIR} 302 shift 303 # check given cpu is in the supported CPU list 304 for cpu in ${qemu_target_list} ; do 305 if [ "$cpu" = "$1" ] ; then 306 break 307 fi 308 done 309 310 if [ "$cpu" = "$1" ] ; then 311 qemu_target_list="$1" 312 else 313 echo "ERROR: unknown CPU \"$1\"" 1>&2 314 usage 315 exit 1 316 fi 317 ;; 318 -Q|--qemu-path) 319 shift 320 QEMU_PATH="$1" 321 ;; 322 -e|--exportdir) 323 shift 324 EXPORTDIR="$1" 325 ;; 326 -h|--help) 327 usage 328 exit 1 329 ;; 330 -c|--credential) 331 shift 332 if [ "$1" = "yes" ] ; then 333 FLAGS="OC" 334 else 335 FLAGS="" 336 fi 337 ;; 338 *) 339 break 340 ;; 341 esac 342 shift 343done 344 345$CHECK 346qemu_set_binfmts 347