1#!/bin/bash -xe
2###############################################################################
3# Launch QEMU using the raw commands
4#
5#  Can be run by specifying the BASE_DIR and QEMU_ARCH when the script is
6#  called. Additionally, this script is automatically called by running the
7#  run-robot-qemu-test.sh, it's used to launch the QEMU test container.
8#
9###############################################################################
10#
11#  Variables BASE_DIR and QEMU_ARCH are both required but can be optionally
12#  input as parameters following the initial script call. Alternatively, they
13#  can be input by exporting them or sourcing the script into an environment
14#  that has them declared.
15#
16#  Parameters:
17#   parm1:  <optional, QEMU architecture to use >
18#            default is ${QEMU_ARCH} - ppc64le-linux or x86_64-linux
19#   parm2:  <optional, full path to base directory of qemu binary and images >
20#            default is ${HOME}
21#
22# Optional Env Variable:
23#
24#  QEMU_BIN           = Location of qemu-system-arm binary to use when starting
25#                       QEMU relative to upstream workspace.  Default is
26#                       ./tmp/sysroots/${QEMU_ARCH}/usr/bin/qemu-system-arm
27#                       which is the default location when doing a bitbake
28#                       of obmc-phosphor-image
29#
30#  MACHINE            = Machine to run test against. Options are "witherspoon",
31#                       "palmetto", "romulus", or undefined (default).  Default
32#                       will use the versatilepb model.
33###############################################################################
34
35set -uo pipefail
36
37QEMU_ARCH=${1:-$QEMU_ARCH}
38# If QEMU_ARCH is empty exit, it is required to continue
39echo "QEMU_ARCH = $QEMU_ARCH"
40if [[ -z $QEMU_ARCH ]]; then
41    echo "Did not pass in required QEMU arch parameter"
42    exit -1
43fi
44
45BASE_DIR=${2:-$HOME}
46# If BASE_DIR doesn't exist exit, it is required to continue
47echo "BASE_DIR = $BASE_DIR"
48if [[ ! -d $BASE_DIR ]]; then
49    echo "No input directory and HOME not set!"
50    exit -1
51fi
52
53# Set the location of the qemu binary relative to BASE_DIR
54QEMU_BIN=${QEMU_BIN:-./tmp/sysroots/${QEMU_ARCH}/usr/bin/qemu-system-arm}
55
56MACHINE=${MACHINE:-versatilepb}
57
58# Enter the base directory
59cd ${BASE_DIR}
60
61# Find the correct drive file, and save its name.  OpenBMC has 3 different
62# image formats.  The UBI based one, the standard static.mtd one, and the
63# default QEMU basic image (rootfs.ext4).
64
65DEFAULT_IMAGE_LOC="./tmp/deploy/images/"
66# First look for a UBI image
67if [ -d ${DEFAULT_IMAGE_LOC}/${MACHINE} ]; then
68    DRIVE=$(ls ${DEFAULT_IMAGE_LOC}/${MACHINE}/ | grep -m 1 obmc-phosphor-image-${MACHINE}.ubi.mtd)
69
70    # If not found then look for a static mdt
71    if [ -z ${DRIVE+x} ]; then
72        DRIVE=$(ls ${DEFAULT_IMAGE_LOC}/${MACHINE}/ | grep -m 1 obmc-phosphor-image-${MACHINE}.static.mtd)
73    fi
74fi
75
76# If not found above then use use the default
77if [ -z ${DRIVE+x} ]; then
78    DRIVE=$(ls ${DEFAULT_IMAGE_LOC}/qemuarm | grep rootfs.ext4)
79fi
80
81# If no image to boot from found then exit out
82if [ -z ${DRIVE+x} ]; then
83    echo "No image found to boot from for machine ${MACHINE}"
84    exit -1
85fi
86
87# Obtain IP from /etc/hosts if IP is not valid set to localhost
88IP=$(awk 'END{print $1}' /etc/hosts)
89if [[ "$IP" != *.*.*.* ]]; then
90  IP=127.0.0.1
91fi
92
93# Launch QEMU using the qemu-system-arm
94${QEMU_BIN} \
95    -device virtio-net,netdev=mynet \
96    -netdev user,id=mynet,hostfwd=tcp:${IP}:22-:22,hostfwd=tcp:${IP}:443-:443,hostfwd=tcp:${IP}:80-:80 \
97    -machine versatilepb \
98    -m 256 \
99    -drive file=${DEFAULT_IMAGE_LOC}/qemuarm/${DRIVE},if=virtio,format=raw \
100    -show-cursor \
101    -usb \
102    -usbdevice tablet \
103    -device virtio-rng-pci \
104    -serial mon:vc \
105    -serial mon:stdio \
106    -serial null \
107    -kernel ${DEFAULT_IMAGE_LOC}/qemuarm/zImage \
108    -append 'root=/dev/vda rw highres=off  console=ttyS0 mem=256M ip=dhcp console=ttyAMA0,115200 console=tty'\
109    -dtb ${DEFAULT_IMAGE_LOC}/qemuarm/zImage-versatile-pb.dtb
110