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
29*f9dbc8d9SAndrew Geissler#
30*f9dbc8d9SAndrew Geissler#  MACHINE            = Machine to run test against. Options are "witherspoon",
31*f9dbc8d9SAndrew Geissler#                       "palmetto", "romulus", or undefined (default).  Default
32*f9dbc8d9SAndrew 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"
420205e8daSAndrew Geissler    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!"
500205e8daSAndrew Geissler    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
56*f9dbc8d9SAndrew GeisslerMACHINE=${MACHINE:-versatilepb}
57*f9dbc8d9SAndrew Geissler
58b24dccb2SAlanny Lopez# Enter the base directory
590205e8daSAndrew Geisslercd ${BASE_DIR}
600205e8daSAndrew Geissler
61*f9dbc8d9SAndrew Geissler# Find the correct drive file, and save its name.  OpenBMC has 3 different
62*f9dbc8d9SAndrew Geissler# image formats.  The UBI based one, the standard static.mtd one, and the
63*f9dbc8d9SAndrew Geissler# default QEMU basic image (rootfs.ext4).
64*f9dbc8d9SAndrew Geissler
65*f9dbc8d9SAndrew GeisslerDEFAULT_IMAGE_LOC="./tmp/deploy/images/"
66*f9dbc8d9SAndrew Geissler# First look for a UBI image
67*f9dbc8d9SAndrew Geisslerif [ -d ${DEFAULT_IMAGE_LOC}/${MACHINE} ]; then
68*f9dbc8d9SAndrew Geissler    DRIVE=$(ls ${DEFAULT_IMAGE_LOC}/${MACHINE}/ | grep -m 1 obmc-phosphor-image-${MACHINE}.ubi.mtd)
69*f9dbc8d9SAndrew Geissler
70*f9dbc8d9SAndrew Geissler    # If not found then look for a static mdt
71*f9dbc8d9SAndrew Geissler    if [ -z ${DRIVE+x} ]; then
72*f9dbc8d9SAndrew Geissler        DRIVE=$(ls ${DEFAULT_IMAGE_LOC}/${MACHINE}/ | grep -m 1 obmc-phosphor-image-${MACHINE}.static.mtd)
73*f9dbc8d9SAndrew Geissler    fi
74*f9dbc8d9SAndrew Geisslerfi
75*f9dbc8d9SAndrew Geissler
76*f9dbc8d9SAndrew Geissler# If not found above then use use the default
77*f9dbc8d9SAndrew Geisslerif [ -z ${DRIVE+x} ]; then
78*f9dbc8d9SAndrew Geissler    DRIVE=$(ls ${DEFAULT_IMAGE_LOC}/qemuarm | grep rootfs.ext4)
79*f9dbc8d9SAndrew Geisslerfi
80*f9dbc8d9SAndrew Geissler
81*f9dbc8d9SAndrew Geissler# If no image to boot from found then exit out
82*f9dbc8d9SAndrew Geisslerif [ -z ${DRIVE+x} ]; then
83*f9dbc8d9SAndrew Geissler    echo "No image found to boot from for machine ${MACHINE}"
84*f9dbc8d9SAndrew Geissler    exit -1
85*f9dbc8d9SAndrew Geisslerfi
86b24dccb2SAlanny Lopez
87ebf0794bSAlanny Lopez# Obtain IP from /etc/hosts if IP is not valid set to localhost
88ebf0794bSAlanny LopezIP=$(awk 'END{print $1}' /etc/hosts)
89ebf0794bSAlanny Lopezif [[ "$IP" != *.*.*.* ]]; then
90ebf0794bSAlanny Lopez  IP=127.0.0.1
91ebf0794bSAlanny Lopezfi
92ebf0794bSAlanny Lopez
93b24dccb2SAlanny Lopez# Launch QEMU using the qemu-system-arm
947a88f29eSAndrew Geissler${QEMU_BIN} \
95ebf0794bSAlanny Lopez    -device virtio-net,netdev=mynet \
96ebf0794bSAlanny Lopez    -netdev user,id=mynet,hostfwd=tcp:${IP}:22-:22,hostfwd=tcp:${IP}:443-:443,hostfwd=tcp:${IP}:80-:80 \
970205e8daSAndrew Geissler    -machine versatilepb \
98b24dccb2SAlanny Lopez    -m 256 \
99*f9dbc8d9SAndrew Geissler    -drive file=${DEFAULT_IMAGE_LOC}/qemuarm/${DRIVE},if=virtio,format=raw \
1000205e8daSAndrew Geissler    -show-cursor \
1010205e8daSAndrew Geissler    -usb \
102b24dccb2SAlanny Lopez    -usbdevice tablet \
103b24dccb2SAlanny Lopez    -device virtio-rng-pci \
104b24dccb2SAlanny Lopez    -serial mon:vc \
105b24dccb2SAlanny Lopez    -serial mon:stdio \
106b24dccb2SAlanny Lopez    -serial null \
107*f9dbc8d9SAndrew Geissler    -kernel ${DEFAULT_IMAGE_LOC}/qemuarm/zImage \
108b24dccb2SAlanny Lopez    -append 'root=/dev/vda rw highres=off  console=ttyS0 mem=256M ip=dhcp console=ttyAMA0,115200 console=tty'\
109*f9dbc8d9SAndrew Geissler    -dtb ${DEFAULT_IMAGE_LOC}/qemuarm/zImage-versatile-pb.dtb
110