#! /bin/sh
# Setup binfmt_misc

PROCDIR="/proc/sys/fs/binfmt_misc"

FORMATS="ARM ARMS PPC PPCS"

#ARM_SIM="/home/paul/arm/bin/arm-none-elf-run"
ARM_SIM="/bin/qemu-arm-static"
ARM_MAGIC='\x7f\x45\x4c\x46\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00'
ARM_MMASK='\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff'
# Also do the same for shared libraries
ARMS_SIM="$ARM_SIM"
ARMS_MAGIC='\x7f\x45\x4c\x46\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x28\x00'
ARMS_MMASK='\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff'

PPC_SIM="/bin/qemu-ppc-static"
PPC_MAGIC='\x7f\x45\x4c\x46\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14'
PPC_MMASK='\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff'
PPCS_SIM="$ARM_SIM"
PPCS_MAGIC='\x7f\x45\x4c\x46\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\\x00x03\x00\x14'
PPCS_MMASK='\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff'

start_binfmt ()
{
  for f in $FORMATS; do
    eval echo ":${f}:M::\$${f}_MAGIC:\$${f}_MMASK:\$${f}_SIM:" \
      > ${PROCDIR}/register
  done
}

stop_binfmt ()
{
  echo "-1" > ${PROCDIR}/status
}

# Abort is the filesystem is not mounted
[ -f $REGFILE ] || exit 0

case "$1" in
  start)
    start_binfmt
    ;;

  stop)
    stop_binfmt
    ;;

  restart)
    stop_binfmt
    start_binfmt
    ;;

  *)
    echo "Usage: /etc/init.d/binfmt_misc {start|stop|restart}"
    exit 1
    ;;
esac

