130 lines · bash
1#!/bin/bash2 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" >&223 print_usage 124}25 26run_qemu() {27 QEMU_CORES=228 QEMU_MEMORY=102429 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 -nographic43}44 45# Parse options46while [[ $# -gt 0 ]]; do47 case "${END_OF_OPT}${1}" in48 --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 esac58 shift59done60 61if [ "$ARCH" == "arm64" ] && [ "$ARCH" == "arm" ]; then62 echo "Invalid architecture: $ARCH"63 print_usage 164fi65 66if [[ ! -f "$ROOTFS_IMG" ]]; then67 echo "No root file system image image available for emulation."68 exit69fi70 71if [[ ! -f "$KERNEL_IMG" ]]; then72 KERNEL_IMG_PATH=$(pwd)/linux.build/"$ARCH"/arch/"$ARCH"/boot/73 74 if [[ ! -d "$KERNEL_IMG_PATH" ]]; then75 echo "No Linux kernel image available for emulation."76 exit77 fi78 79 if [[ "$ARCH" == "arm" ]]; then80 KERNEL_IMG=$KERNEL_IMG_PATH/zImage81 elif [[ "$ARCH" == "arm64" ]]; then82 KERNEL_IMG=$KERNEL_IMG_PATH/Image83 fi84fi85 86if [[ ! -f "$QEMU_BIN" ]]; then87 if [[ "$ARCH" == "arm" ]]; then88 QEMU_BIN=$(pwd)/qemu.git/arm-softmmu/qemu-system-arm89 elif [[ "$ARCH" == "arm64" ]]; then90 QEMU_BIN=$(pwd)/qemu.git/aarch64-softmmu/qemu-system-aarch6491 fi92 93 if [[ ! -f "$QEMU_BIN" ]]; then94 echo "QEMU $ARCH system emulation executable not found."95 exit96 fi97fi98 99if [[ "$ARCH" == "arm" ]]; then100 QEMU_MACHINE="virt,highmem=off"101 QEMU_CPU="cortex-a15"102 103 if [[ $SVE ]]; then104 echo "warning: --sve is supported by AArch64 targets only"105 fi106 if [[ $MTE ]]; then107 echo "warning: --mte is supported by AArch64 targets only"108 fi109 if [[ $SME ]]; then110 echo "warning: --sme is supported by AArch64 targets only"111 fi112elif [[ "$ARCH" == "arm64" ]]; then113 QEMU_MACHINE=virt114 QEMU_SVE_MAX_VQ=4115 QEMU_CPU="cortex-a53"116 117 if [[ $SVE ]] || [[ $MTE ]] || [[ $SME ]]; then118 QEMU_CPU="max"119 fi120 121 if [[ $SVE ]] || [[ $SME ]]; then122 QEMU_CPU="$QEMU_CPU,sve-max-vq=$QEMU_SVE_MAX_VQ"123 fi124 if [[ $MTE ]]; then125 QEMU_MACHINE="$QEMU_MACHINE,mte=on"126 fi127fi128 129run_qemu130