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