All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: U-Boot Mailing List <u-boot@lists.denx.de>
Cc: Bin Meng <bmeng.cn@gmail.com>, Simon Glass <sjg@chromium.org>,
	Caleb Connolly <caleb.connolly@linaro.org>,
	Heinrich Schuchardt <xypron.glpk@gmx.de>,
	Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Jiaxun Yang <jiaxun.yang@flygoat.com>,
	Marek Vasut <marex@denx.de>,
	Mattijs Korpershoek <mkorpershoek@baylibre.com>,
	Nathan Barrett-Morrison <nathan.morrison@timesys.com>,
	Oliver Gaskell <Oliver.Gaskell@analog.com>,
	Patrick Rudolph <patrick.rudolph@9elements.com>,
	Robert Marko <robert.marko@sartura.hr>,
	Sam Protsenko <semen.protsenko@linaro.org>,
	Sumit Garg <sumit.garg@linaro.org>, Tom Rini <trini@konsulko.com>
Subject: [PATCH 01/18] scripts: Add a script for building and booting QEMU
Date: Tue, 12 Nov 2024 06:58:54 -0700	[thread overview]
Message-ID: <20241112135911.630586-2-sjg@chromium.org> (raw)
In-Reply-To: <20241112135911.630586-1-sjg@chromium.org>

It is handy to be able to quickly build and boot a QEMU image for a
particular architecture and distro.

Add a script for this purpose. It supports only arm and x86 at present.
For distros it only supports Ubuntu. Both 32- and 64-bit builds are
supported.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 MAINTAINERS                    |   8 ++
 doc/board/emulation/index.rst  |   1 +
 doc/board/emulation/script.rst |  61 ++++++++++++
 scripts/build-qemu.sh          | 175 +++++++++++++++++++++++++++++++++
 4 files changed, 245 insertions(+)
 create mode 100644 doc/board/emulation/script.rst
 create mode 100755 scripts/build-qemu.sh

diff --git a/MAINTAINERS b/MAINTAINERS
index 0399ed1dbf6..b45bb96d5a5 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1110,6 +1110,14 @@ F:	tools/efivar.py
 F:	tools/file2include.c
 F:	tools/mkeficapsule.c
 
+EMULATION
+M:	Simon Glass <sjg@chromium.org>
+S:	Maintained
+W:	https://docs.u-boot.org/en/latest/board/emulation/script.html
+F:	configs/qemu_x86*
+F:	doc/board/emulation/script.rst
+F:	scripts/build-qemu.sh
+
 ENVIRONMENT
 M:	Joe Hershberger <joe.hershberger@ni.com>
 S:	Maintained
diff --git a/doc/board/emulation/index.rst b/doc/board/emulation/index.rst
index f8908166276..5a2a00ae225 100644
--- a/doc/board/emulation/index.rst
+++ b/doc/board/emulation/index.rst
@@ -8,6 +8,7 @@ Emulation
 
    acpi
    blkdev
+   script
    qemu-arm
    qemu-mips
    qemu-ppce500
diff --git a/doc/board/emulation/script.rst b/doc/board/emulation/script.rst
new file mode 100644
index 00000000000..23981e333cb
--- /dev/null
+++ b/doc/board/emulation/script.rst
@@ -0,0 +1,61 @@
+.. SPDX-License-Identifier: GPL-2.0+
+
+Script for building and running
+===============================
+
+You may find the script `scripts/build-qemu.sh` helpful for building and testing
+U-Boot on QEMU.
+
+If uses a environment variables to control how it works:
+
+ubdir
+    base directory for building U-Boot, with each board being in its own
+    subdirectory
+
+imagedir
+    directory containing OS images, containin a subdirectory for each distro
+    type (e.g. ubuntu/
+
+Once configured, you can build and run QEMU for arm64 like this::
+
+    scripts/build-qemu.sh -rsw
+
+No support is currently included for specifying a root disk, so this script can
+only be used to start installers.
+
+Options
+~~~~~~~
+
+Options are available to control the script:
+
+-a <arch>
+    Select architecture (default arm, x86)
+
+-B
+    Don't build; assume a build exists
+
+-k
+    Use kvm - kernel-based Virtual Machine. By default QEMU uses its own
+    emulator
+
+-o <os>
+    Run an Operating System. For now this only supports 'ubuntu'. The name of
+    the OS file must remain unchanged from its standard name on the Ubuntu
+    website.
+
+-r
+    Run QEMU with the image (by default this is not done)
+
+-R
+    Select OS release (e.g. 24.04).
+
+-s
+    Use serial only (no display)
+
+-w
+    Use word version (32-bit). By default, 64-bit is used
+
+.. note::
+
+    Note: For now this is a shell script, but if it expands it might be better
+    as Python, accepting the slower startup.
diff --git a/scripts/build-qemu.sh b/scripts/build-qemu.sh
new file mode 100755
index 00000000000..0ff53593cf9
--- /dev/null
+++ b/scripts/build-qemu.sh
@@ -0,0 +1,175 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Script to build U-Boot suitable for booting with QEMU, possibly running
+# it, possibly with an OS image
+
+# This just an example. It assumes that
+
+# - you build U-Boot in ${ubdir}/<name> where <name> is the U-Boot board config
+# - your OS images are in ${imagedir}/{distroname}/...
+
+# So far the script supports only ARM and x86.
+
+set -e
+
+usage() {
+	(
+	if [[ -n "$1" ]]; then
+		echo "$1"
+		echo
+	fi
+	echo "Usage: $0 -aBkrsw"
+	echo
+	echo "   -a   - Select architecture (arm, x86)"
+	echo "   -B   - Don't build; assume a build exists"
+	echo "   -k   - Use kvm (kernel-based Virtual Machine)"
+	echo "   -o   - Run Operating System ('ubuntu' only for now)"
+	echo "   -r   - Run QEMU with the image"
+	echo "   -R   - Select OS release (e.g. 24.04)"
+	echo "   -s   - Use serial only (no display)"
+	echo "   -w   - Use word version (32-bit)" ) >&2
+	exit 1
+}
+
+# Directory tree for OS images
+imagedir=${imagedir-/vid/software/linux}
+
+# architecture (arm or x86)
+arch=arm
+
+# 32- or 64-bit build
+bitness=64
+
+# Build U-Boot
+build=yes
+
+# Extra setings
+extra=
+
+# Operating System to boot (ubuntu)
+os=
+
+release=24.04.1
+
+# run the image with QEMU
+run=
+
+# run QEMU without a display (U-Boot must be set to stdout=serial)
+serial=
+
+# Use kvm
+kvm=
+
+# Set ubdir to the build directory where you build U-Boot out-of-tree
+# We avoid in-tree build because it gets confusing trying different builds
+ubdir=${ubdir-/tmp/b}
+
+while getopts "a:Bko:rR:sw" opt; do
+	case "${opt}" in
+	a)
+		arch=$OPTARG
+		;;
+	B)
+		build=
+		;;
+	k)
+		kvm="-enable-kvm"
+		;;
+	o)
+		os=$OPTARG
+
+		# Expand memory and CPUs
+		extra+=" -m 4G -smp 4"
+		;;
+	r)
+		run=1
+		;;
+	R)
+		release=$OPTARG
+		;;
+	s)
+		serial=1
+		;;
+	w)
+		bitness=32
+		;;
+	*)
+		usage
+		;;
+	esac
+done
+
+# Build U-Boot for the selected board
+build_u_boot() {
+	buildman -w -o $DIR --board $BOARD -I || exit $?
+}
+
+# Run QEMU with U-Boot
+run_qemu() {
+	if [[ -n "${os_image}" ]]; then
+		extra+=" -drive if=virtio,file=${os_image},format=raw,id=hd0"
+	fi
+	if [[ -n "${serial}" ]]; then
+		extra+=" -display none -serial mon:stdio"
+	else
+		extra+=" -serial mon:stdio"
+	fi
+	echo "Running ${qemu} ${extra}"
+	"${qemu}" -bios "$DIR/${BIOS}" \
+		-m 512 \
+		-nic none \
+		${kvm} \
+		${extra}
+}
+
+# Check architecture
+case "${arch}" in
+arm)
+	BOARD="qemu_arm"
+	BIOS="u-boot.bin"
+	qemu=qemu-system-arm
+	extra+=" -machine virt"
+	suffix="arm"
+	if [[ "${bitness}" == "64" ]]; then
+		BOARD="qemu_arm64"
+		qemu=qemu-system-aarch64
+		extra+=" -cpu cortex-a57"
+		suffix="arm64"
+	fi
+	;;
+x86)
+	BOARD="qemu-x86"
+	BIOS="u-boot.rom"
+	qemu=qemu-system-i386
+	suffix="i386"
+	if [[ "${bitness}" == "64" ]]; then
+		BOARD="qemu-x86_64"
+		qemu=qemu-system-x86_64
+		suffix="amd64"
+	fi
+	;;
+*)
+	usage "Unknown architecture '${arch}'"
+esac
+
+# Check OS
+case "${os}" in
+ubuntu)
+	os_image="${imagedir}/${os}/${os}-${release}-desktop-${suffix}.iso"
+	;;
+"")
+	;;
+*)
+	usage "Unknown OS '${os}'"
+esac
+
+DIR=${ubdir}/${BOARD}
+
+if [[ -n "${build}" ]]; then
+	build_u_boot
+fi
+
+if [[ -n "${run}" ]]; then
+	run_qemu
+fi
-- 
2.34.1


  reply	other threads:[~2024-11-12 13:59 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-12 13:58 [PATCH 00/18] x86: Improve operation under QEMU Simon Glass
2024-11-12 13:58 ` Simon Glass [this message]
2024-11-12 23:11   ` [PATCH 01/18] scripts: Add a script for building and booting QEMU Tom Rini
2024-11-13  0:49   ` Heinrich Schuchardt
2024-11-13  0:54     ` Tom Rini
2024-11-15 14:21       ` Simon Glass
2024-11-15 15:14         ` Tom Rini
2024-12-07 23:39           ` Simon Glass
2025-01-06 14:55           ` Tom Rini
2025-01-09 12:36             ` Simon Glass
2025-01-09 14:56               ` Tom Rini
2025-01-10 13:39                 ` Simon Glass
2025-01-10 16:01                   ` Tom Rini
2025-01-10 19:50                     ` Simon Glass
2025-01-10 20:46                       ` Tom Rini
2024-11-12 13:58 ` [PATCH 02/18] x86: Expand x86_64 early memory Simon Glass
2024-11-12 13:58 ` [PATCH 03/18] RFC: x86: qemu: Switch to bochs display Simon Glass
2024-11-12 13:58 ` [PATCH 04/18] x86: qemu: Enable dhrystone Simon Glass
2024-11-12 13:58 ` [PATCH 05/18] x86: qemu: Avoid accessing BSS too early Simon Glass
2024-11-12 13:58 ` [PATCH 06/18] x86: Drop mpspec from the SPL build Simon Glass
2024-11-12 13:59 ` [PATCH 07/18] x86: Add some log categories Simon Glass
2024-11-12 13:59 ` [PATCH 08/18] x86: Drop use of CONFIG_REALMODE_DEBUG Simon Glass
2024-11-12 13:59 ` [PATCH 09/18] x86: Avoid clearing the VESA display Simon Glass
2024-11-12 13:59 ` [PATCH 10/18] x86: Add 64-bit entries to the GDT Simon Glass
2024-11-12 13:59 ` [PATCH 11/18] x86: Use defines for the cache flags Simon Glass
2024-11-12 13:59 ` [PATCH 12/18] x86: spl: Drop duplicate CPU init Simon Glass
2024-11-12 13:59 ` [PATCH 13/18] x86: Drop the message about features missing in 64-bit Simon Glass
2024-11-12 13:59 ` [PATCH 14/18] x86: Include stdbool.h in interrupt header Simon Glass
2024-11-12 13:59 ` [PATCH 15/18] x86: Tidy up the GDT size in start/16.S Simon Glass
2024-11-12 13:59 ` [PATCH 16/18] x86: Disable paging before changing to long mode Simon Glass
2024-11-12 13:59 ` [PATCH 17/18] x86: Use the same GDT when jumping " Simon Glass
2024-11-12 13:59 ` [PATCH 18/18] x86: Use a simple jump into " Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241112135911.630586-2-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=Oliver.Gaskell@analog.com \
    --cc=bmeng.cn@gmail.com \
    --cc=caleb.connolly@linaro.org \
    --cc=ilias.apalodimas@linaro.org \
    --cc=jiaxun.yang@flygoat.com \
    --cc=marex@denx.de \
    --cc=mkorpershoek@baylibre.com \
    --cc=nathan.morrison@timesys.com \
    --cc=patrick.rudolph@9elements.com \
    --cc=robert.marko@sartura.hr \
    --cc=semen.protsenko@linaro.org \
    --cc=sumit.garg@linaro.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.