1#!/bin/bash 2 3print_usage() { 4 echo "Usage: $(basename $0) --arch [arm|arm64] [options]" 5 echo -e "Starts QEMU system mode emulation for the architecture.\n" 6 echo -e " --help\t\t\tDisplay this information." 7 echo -e " --arch {arm|arm64}\t\tSelects architecture QEMU system emulation." 8 echo -e " --sve\t\t\t\tEnables AArch64 SVE mode." 9 echo -e " --mte\t\t\t\tEnables AArch64 MTE mode." 10 echo -e " --sme\t\t\t\tEnables AArch64 SME mode." 11 echo -e " --rootfs {path}\t\tPath of root file system image." 12 echo -e " --qemu {path}\t\t\tPath of pre-installed qemu-system-* executable." 13 echo -e " --kernel {path}\t\tPath of Linux kernel prebuilt image.\n" 14 echo -e "By default this utility will use:" 15 echo -e " QEMU image built from source in qemu.git directory" 16 echo -e " Linux kernel image from linux.build/(arm or arm64) directory." 17 echo -e "Custom Linux kernel image or QEMU binary can be provided using commandline." 18 exit "$1" 19} 20 21invalid_arg() { 22 echo "ERROR: Unrecognized argument: $1" >&2 23 print_usage 1 24} 25 26run_qemu() { 27 QEMU_CORES=2 28 QEMU_MEMORY=1024 29 30 $QEMU_BIN \ 31 -cpu $QEMU_CPU \ 32 -m $QEMU_MEMORY \ 33 -smp $QEMU_CORES \ 34 -kernel $KERNEL_IMG \ 35 -machine $QEMU_MACHINE \ 36 -drive file=$ROOTFS_IMG,if=none,format=raw,id=hd0 \ 37 -device virtio-blk-device,drive=hd0 \ 38 -append "root=/dev/vda rw ip=dhcp mem=1024M raid=noautodetect \ 39 crashkernel=128M rootwait console=ttyAMA0 devtmpfs.mount=0" \ 40 -netdev type=tap,id=net0 \ 41 -device virtio-net-device,netdev=net0 \ 42 -nographic 43} 44 45# Parse options 46while [[ $# -gt 0 ]]; do 47 case "${END_OF_OPT}${1}" in 48 --arch) ARCH=$2; shift;; 49 --rootfs) ROOTFS_IMG=$2; shift;; 50 --kernel) KERNEL_IMG=$2; shift;; 51 --qemu) QEMU_BIN=$2; shift;; 52 --sve) SVE=1;; 53 --mte) MTE=1;; 54 --sme) SME=1;; 55 --help) print_usage 0 ;; 56 *) invalid_arg "$1" ;; 57 esac 58 shift 59done 60 61if [ "$ARCH" == "arm64" ] && [ "$ARCH" == "arm" ]; then 62 echo "Invalid architecture: $ARCH" 63 print_usage 1 64fi 65 66if [[ ! -f "$ROOTFS_IMG" ]]; then 67 echo "No root file system image image available for emulation." 68 exit 69fi 70 71if [[ ! -f "$KERNEL_IMG" ]]; then 72 KERNEL_IMG_PATH=$(pwd)/linux.build/"$ARCH"/arch/"$ARCH"/boot/ 73 74 if [[ ! -d "$KERNEL_IMG_PATH" ]]; then 75 echo "No Linux kernel image available for emulation." 76 exit 77 fi 78 79 if [[ "$ARCH" == "arm" ]]; then 80 KERNEL_IMG=$KERNEL_IMG_PATH/zImage 81 elif [[ "$ARCH" == "arm64" ]]; then 82 KERNEL_IMG=$KERNEL_IMG_PATH/Image 83 fi 84fi 85 86if [[ ! -f "$QEMU_BIN" ]]; then 87 if [[ "$ARCH" == "arm" ]]; then 88 QEMU_BIN=$(pwd)/qemu.git/arm-softmmu/qemu-system-arm 89 elif [[ "$ARCH" == "arm64" ]]; then 90 QEMU_BIN=$(pwd)/qemu.git/aarch64-softmmu/qemu-system-aarch64 91 fi 92 93 if [[ ! -f "$QEMU_BIN" ]]; then 94 echo "QEMU $ARCH system emulation executable not found." 95 exit 96 fi 97fi 98 99if [[ "$ARCH" == "arm" ]]; then 100 QEMU_MACHINE="virt,highmem=off" 101 QEMU_CPU="cortex-a15" 102 103 if [[ $SVE ]]; then 104 echo "warning: --sve is supported by AArch64 targets only" 105 fi 106 if [[ $MTE ]]; then 107 echo "warning: --mte is supported by AArch64 targets only" 108 fi 109 if [[ $SME ]]; then 110 echo "warning: --sme is supported by AArch64 targets only" 111 fi 112elif [[ "$ARCH" == "arm64" ]]; then 113 QEMU_MACHINE=virt 114 QEMU_SVE_MAX_VQ=4 115 QEMU_CPU="cortex-a53" 116 117 if [[ $SVE ]] || [[ $MTE ]] || [[ $SME ]]; then 118 QEMU_CPU="max" 119 fi 120 121 if [[ $SVE ]] || [[ $SME ]]; then 122 QEMU_CPU="$QEMU_CPU,sve-max-vq=$QEMU_SVE_MAX_VQ" 123 fi 124 if [[ $MTE ]]; then 125 QEMU_MACHINE="$QEMU_MACHINE,mte=on" 126 fi 127fi 128 129run_qemu 130