10205e8daSAndrew Geissler#!/bin/bash -xe
2b24dccb2SAlanny Lopez###############################################################################
30205e8daSAndrew Geissler# Launch QEMU using the raw commands
40205e8daSAndrew Geissler#
5b24dccb2SAlanny Lopez#  Can be run by specifying the BASE_DIR and QEMU_ARCH when the script is
6b24dccb2SAlanny Lopez#  called. Additionally, this script is automatically called by running the
7b24dccb2SAlanny Lopez#  run-robot-qemu-test.sh, it's used to launch the QEMU test container.
8b24dccb2SAlanny Lopez#
9b24dccb2SAlanny Lopez###############################################################################
10b24dccb2SAlanny Lopez#
11b24dccb2SAlanny Lopez#  Variables BASE_DIR and QEMU_ARCH are both required but can be optionally
12b24dccb2SAlanny Lopez#  input as parameters following the initial script call. Alternatively, they
13b24dccb2SAlanny Lopez#  can be input by exporting them or sourcing the script into an environment
14b24dccb2SAlanny Lopez#  that has them declared.
15b24dccb2SAlanny Lopez#
160205e8daSAndrew Geissler#  Parameters:
170205e8daSAndrew Geissler#   parm1:  <optional, QEMU architecture to use >
180205e8daSAndrew Geissler#            default is ${QEMU_ARCH} - ppc64le-linux or x86_64-linux
190205e8daSAndrew Geissler#   parm2:  <optional, full path to base directory of qemu binary and images >
200205e8daSAndrew Geissler#            default is ${HOME}
21b24dccb2SAlanny Lopez#
227a88f29eSAndrew Geissler# Optional Env Variable:
237a88f29eSAndrew Geissler#
247a88f29eSAndrew Geissler#  QEMU_BIN           = Location of qemu-system-arm binary to use when starting
257a88f29eSAndrew Geissler#                       QEMU relative to upstream workspace.  Default is
267a88f29eSAndrew Geissler#                       ./tmp/sysroots/${QEMU_ARCH}/usr/bin/qemu-system-arm
277a88f29eSAndrew Geissler#                       which is the default location when doing a bitbake
287a88f29eSAndrew Geissler#                       of obmc-phosphor-image
29f9dbc8d9SAndrew Geissler#
30f9dbc8d9SAndrew Geissler#  MACHINE            = Machine to run test against. Options are "witherspoon",
31f9dbc8d9SAndrew Geissler#                       "palmetto", "romulus", or undefined (default).  Default
32f9dbc8d9SAndrew Geissler#                       will use the versatilepb model.
33b24dccb2SAlanny Lopez###############################################################################
340205e8daSAndrew Geissler
350205e8daSAndrew Geisslerset -uo pipefail
360205e8daSAndrew Geissler
370205e8daSAndrew GeisslerQEMU_ARCH=${1:-$QEMU_ARCH}
38b24dccb2SAlanny Lopez# If QEMU_ARCH is empty exit, it is required to continue
390205e8daSAndrew Geisslerecho "QEMU_ARCH = $QEMU_ARCH"
400205e8daSAndrew Geisslerif [[ -z $QEMU_ARCH ]]; then
410205e8daSAndrew Geissler    echo "Did not pass in required QEMU arch parameter"
42384d741bSPatrick Williams    exit 1
430205e8daSAndrew Geisslerfi
440205e8daSAndrew Geissler
450205e8daSAndrew GeisslerBASE_DIR=${2:-$HOME}
46b24dccb2SAlanny Lopez# If BASE_DIR doesn't exist exit, it is required to continue
470205e8daSAndrew Geisslerecho "BASE_DIR = $BASE_DIR"
480205e8daSAndrew Geisslerif [[ ! -d $BASE_DIR ]]; then
490205e8daSAndrew Geissler    echo "No input directory and HOME not set!"
50384d741bSPatrick Williams    exit 1
510205e8daSAndrew Geisslerfi
520205e8daSAndrew Geissler
537a88f29eSAndrew Geissler# Set the location of the qemu binary relative to BASE_DIR
547a88f29eSAndrew GeisslerQEMU_BIN=${QEMU_BIN:-./tmp/sysroots/${QEMU_ARCH}/usr/bin/qemu-system-arm}
557a88f29eSAndrew Geissler
564290d580SAndrew GeisslerDEFAULT_MACHINE=versatilepb
574290d580SAndrew GeisslerMACHINE=${MACHINE:-${DEFAULT_MACHINE}}
58f9dbc8d9SAndrew Geissler
59b24dccb2SAlanny Lopez# Enter the base directory
60384d741bSPatrick Williamscd "${BASE_DIR}"
610205e8daSAndrew Geissler
62f9dbc8d9SAndrew Geissler# Find the correct drive file, and save its name.  OpenBMC has 3 different
63f9dbc8d9SAndrew Geissler# image formats.  The UBI based one, the standard static.mtd one, and the
64f9dbc8d9SAndrew Geissler# default QEMU basic image (rootfs.ext4).
65e4146670SNan ZhouDEFAULT_IMAGE_LOC="${DEFAULT_IMAGE_LOC:-./tmp/deploy/images/}"
66e4146670SNan Zhouif [ -f "${DEFAULT_IMAGE_LOC}/${MACHINE}/obmc-phosphor-image-${MACHINE}".ubi.mtd ]; then
67384d741bSPatrick Williams    DRIVE="obmc-phosphor-image-${MACHINE}.ubi.mtd"
68e4146670SNan Zhouelif [ -f "${DEFAULT_IMAGE_LOC}/${MACHINE}/obmc-phosphor-image-${MACHINE}".static.mtd ]; then
69384d741bSPatrick Williams    DRIVE="obmc-phosphor-image-${MACHINE}.static.mtd"
704290d580SAndrew Geisslerelse
71384d741bSPatrick Williams    # shellcheck disable=SC2010
72e4146670SNan Zhou    DRIVE=$(ls "${DEFAULT_IMAGE_LOC}"/qemuarm | grep rootfs.ext4)
73f9dbc8d9SAndrew Geisslerfi
74f9dbc8d9SAndrew Geissler
7586de2961SCharles Paul Hofer# Copy the drive file off to /tmp so that QEMU does not write anything back
7686de2961SCharles Paul Hofer# to the drive file and make it unusable for future QEMU runs.
7786de2961SCharles Paul Hofer
7886de2961SCharles Paul HoferTMP_DRIVE_PATH=$(mktemp "/tmp/${DRIVE}-XXXXX")
7986de2961SCharles Paul Hofer
8086de2961SCharles Paul Hofer# The drive file is stored in different locations depending on if we are
8186de2961SCharles Paul Hofer# using the default or real platforms.
82384d741bSPatrick Williamsif [ "${MACHINE}" = "${DEFAULT_MACHINE}" ]; then
83e4146670SNan Zhou    cp "${DEFAULT_IMAGE_LOC}/qemuarm/${DRIVE}" "${TMP_DRIVE_PATH}"
8486de2961SCharles Paul Hoferelse
85e4146670SNan Zhou    cp "${DEFAULT_IMAGE_LOC}/${MACHINE}/${DRIVE}" "${TMP_DRIVE_PATH}"
8686de2961SCharles Paul Hoferfi
8786de2961SCharles Paul Hofer
88ebf0794bSAlanny Lopez# Obtain IP from /etc/hosts if IP is not valid set to localhost
89ebf0794bSAlanny LopezIP=$(awk 'END{print $1}' /etc/hosts)
90ebf0794bSAlanny Lopezif [[ "$IP" != *.*.*.* ]]; then
91ebf0794bSAlanny Lopez    IP=127.0.0.1
92ebf0794bSAlanny Lopezfi
93ebf0794bSAlanny Lopez
948ccdf1beSAndrew Geissler# Forward all needed ports for the robot test framework to run
958ccdf1beSAndrew Geissler# Since this is run in docker, the standard ports can be used
968ccdf1beSAndrew GeisslerNET_FORWARDING=hostfwd=:${IP}:22-:22,hostfwd=:${IP}:443-:443,hostfwd=tcp:${IP}:80-:80,hostfwd=tcp:${IP}:2200-:2200,hostfwd=udp:${IP}:623-:623,hostfwd=udp:${IP}:664-:664
978ccdf1beSAndrew Geissler
988ccdf1beSAndrew Geissler# Most system only have one NIC so set this as default
998ccdf1beSAndrew GeisslerNIC="-net nic,model=ftgmac100,netdev=netdev1 -netdev user,id=netdev1,$NET_FORWARDING"
100384d741bSPatrick Williamsif [ "${MACHINE}" = "tacoma" ]; then
1018ccdf1beSAndrew Geissler    # Tacoma requires us to specify up to four NICs, with the third one being
1028ccdf1beSAndrew Geissler    # the active device.
1038ccdf1beSAndrew Geissler    NIC="-net nic,model=ftgmac100,netdev=netdev1 -netdev user,id=netdev1 "
1048ccdf1beSAndrew Geissler    NIC+="-net nic,model=ftgmac100,netdev=netdev2 -netdev user,id=netdev2 "
1058ccdf1beSAndrew Geissler    NIC+="-net nic,model=ftgmac100,netdev=netdev3 -netdev user,id=netdev3,$NET_FORWARDING "
1068ccdf1beSAndrew Geissler    NIC+="-net nic,model=ftgmac100,netdev=netdev4 -netdev user,id=netdev4"
1078ccdf1beSAndrew Geisslerfi
1088ccdf1beSAndrew Geissler
1094290d580SAndrew Geissler# The syntax to start old qemu / default version requires different syntax
1104290d580SAndrew Geissler# then new qemu with the real platforms
111384d741bSPatrick Williamsif [ "${MACHINE}" = "${DEFAULT_MACHINE}" ]; then
1124290d580SAndrew Geissler    # Launch default QEMU using the qemu-system-arm
1137a88f29eSAndrew Geissler    ${QEMU_BIN} \
114ebf0794bSAlanny Lopez        -device virtio-net,netdev=mynet \
115*4920cb53SPatrick Williams        -netdev "user,id=mynet,hostfwd=tcp:${IP}:22-:22,hostfwd=tcp:${IP}:443-:443,hostfwd=tcp:${IP}:80-:80,hostfwd=tcp:${IP}:2200-:2200,hostfwd=udp:${IP}:623-:623,hostfwd=udp:${IP}:664-:664" \
1160205e8daSAndrew Geissler        -machine versatilepb \
117b24dccb2SAlanny Lopez        -m 256 \
118384d741bSPatrick Williams        -drive file="${TMP_DRIVE_PATH}",if=virtio,format=raw \
1190205e8daSAndrew Geissler        -show-cursor \
1200205e8daSAndrew Geissler        -usb \
121b24dccb2SAlanny Lopez        -usbdevice tablet \
122b24dccb2SAlanny Lopez        -device virtio-rng-pci \
123b24dccb2SAlanny Lopez        -serial mon:vc \
124b24dccb2SAlanny Lopez        -serial mon:stdio \
125b24dccb2SAlanny Lopez        -serial null \
126e4146670SNan Zhou        -kernel "${DEFAULT_IMAGE_LOC}"/qemuarm/zImage \
127b24dccb2SAlanny Lopez        -append 'root=/dev/vda rw highres=off  console=ttyS0 mem=256M ip=dhcp console=ttyAMA0,115200 console=tty'\
128e4146670SNan Zhou        -dtb "${DEFAULT_IMAGE_LOC}"/qemuarm/zImage-versatile-pb.dtb
1294290d580SAndrew Geisslerelse
130384d741bSPatrick Williams    # shellcheck disable=SC2086 # NIC is intentionally word-split.
1314290d580SAndrew Geissler    ${QEMU_BIN} \
132384d741bSPatrick Williams        -machine "${MACHINE}"-bmc \
1334290d580SAndrew Geissler        -nographic \
134384d741bSPatrick Williams        -drive file="${TMP_DRIVE_PATH}",format=raw,if=mtd \
1358ccdf1beSAndrew Geissler        ${NIC}
1364290d580SAndrew Geisslerfi
137