blob: 5c4bbd673717bc5e434ff2d61598f1e11e6a6b27 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7# Help runqemu boot target board, "QB" means Qemu Boot, the following
8# vars can be set in conf files, such as <bsp.conf> to make it can be
9# boot by runqemu:
10#
11# QB_SYSTEM_NAME: qemu name, e.g., "qemu-system-i386"
12#
13# QB_OPT_APPEND: options to append to qemu, e.g., "-device usb-mouse"
14#
15# QB_DEFAULT_KERNEL: default kernel to boot, e.g., "bzImage"
Andrew Geissler517393d2023-01-13 08:55:19 -060016# e.g., "bzImage-initramfs-qemux86-64.bin" if INITRAMFS_IMAGE_BUNDLE is set to 1.
Patrick Williams92b42cb2022-09-03 06:53:57 -050017#
18# QB_DEFAULT_FSTYPE: default FSTYPE to boot, e.g., "ext4"
19#
20# QB_MEM: memory, e.g., "-m 512"
21#
22# QB_MACHINE: qemu machine, e.g., "-machine virt"
23#
24# QB_CPU: qemu cpu, e.g., "-cpu qemu32"
25#
26# QB_CPU_KVM: the similar to QB_CPU, but used when kvm, e.g., '-cpu kvm64',
27# set it when support kvm.
28#
29# QB_SMP: amount of CPU cores inside qemu guest, each mapped to a thread on the host,
30# e.g. "-smp 8".
31#
32# QB_KERNEL_CMDLINE_APPEND: options to append to kernel's -append
33# option, e.g., "console=ttyS0 console=tty"
34#
35# QB_DTB: qemu dtb name
36#
37# QB_AUDIO_DRV: qemu audio driver, e.g., "alsa", set it when support audio
38#
39# QB_AUDIO_OPT: qemu audio option, e.g., "-device AC97", used
40# when QB_AUDIO_DRV is set.
41#
42# QB_RNG: Pass-through for host random number generator, it can speedup boot
43# in system mode, where system is experiencing entropy starvation
44#
45# QB_KERNEL_ROOT: kernel's root, e.g., /dev/vda
46# By default "/dev/vda rw" gets passed to the kernel.
47# To mount the rootfs read-only QB_KERNEL_ROOT can be set to e.g. "/dev/vda ro".
48#
49# QB_NETWORK_DEVICE: network device, e.g., "-device virtio-net-pci,netdev=net0,mac=@MAC@",
50# it needs work with QB_TAP_OPT and QB_SLIRP_OPT.
51# Note, runqemu will replace @MAC@ with a predefined mac, you can set
52# a custom one, but that may cause conflicts when multiple qemus are
53# running on the same host.
54# Note: If more than one interface of type -device virtio-net-device gets added,
55# QB_NETWORK_DEVICE:prepend might be used, since Qemu enumerates the eth*
56# devices in reverse order to -device arguments.
57#
58# QB_TAP_OPT: network option for 'tap' mode, e.g.,
59# "-netdev tap,id=net0,ifname=@TAP@,script=no,downscript=no"
60# Note, runqemu will replace "@TAP@" with the one which is used, such as tap0, tap1 ...
61#
62# QB_SLIRP_OPT: network option for SLIRP mode, e.g., -netdev user,id=net0"
63#
64# QB_CMDLINE_IP_SLIRP: If QB_NETWORK_DEVICE adds more than one network interface to qemu, usually the
65# ip= kernel comand line argument needs to be changed accordingly. Details are documented
66# in the kernel docuemntation https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt
67# Example to configure only the first interface: "ip=eth0:dhcp"
68# QB_CMDLINE_IP_TAP: This parameter is similar to the QB_CMDLINE_IP_SLIRP parameter. Since the tap interface requires
69# static IP configuration @CLIENT@ and @GATEWAY@ place holders are replaced by the IP and the gateway
70# address of the qemu guest by runqemu.
71# Example: "ip=192.168.7.@CLIENT@::192.168.7.@GATEWAY@:255.255.255.0::eth0"
72#
73# QB_ROOTFS_OPT: used as rootfs, e.g.,
74# "-drive id=disk0,file=@ROOTFS@,if=none,format=raw -device virtio-blk-device,drive=disk0"
75# Note, runqemu will replace "@ROOTFS@" with the one which is used, such as core-image-minimal-qemuarm64.ext4.
76#
77# QB_SERIAL_OPT: serial port, e.g., "-serial mon:stdio"
78#
79# QB_TCPSERIAL_OPT: tcp serial port option, e.g.,
80# " -device virtio-serial-device -chardev socket,id=virtcon,port=@PORT@,host=127.0.0.1 -device virtconsole,chardev=virtcon"
81# Note, runqemu will replace "@PORT@" with the port number which is used.
82#
83# QB_ROOTFS_EXTRA_OPT: extra options to be appended to the rootfs device in case there is none specified by QB_ROOTFS_OPT.
84# Can be used to automatically determine the image from the other variables
85# but define things link 'bootindex' when booting from EFI or 'readonly' when using squashfs
86# without the need to specify a dedicated qemu configuration
87#
88# QB_GRAPHICS: QEMU video card type (e.g. "-vga std")
Andrew Geisslerc5535c92023-01-27 16:10:19 -060089# QB_NFSROOTFS_EXTRA_OPT: extra options to be appended to the nfs rootfs options in kernel boot arg, e.g.,
90# "wsize=4096,rsize=4096"
Patrick Williams92b42cb2022-09-03 06:53:57 -050091#
92# Usage:
93# IMAGE_CLASSES += "qemuboot"
94# See "runqemu help" for more info
95
96QB_MEM ?= "-m 256"
97QB_SMP ?= ""
98QB_SERIAL_OPT ?= "-serial mon:stdio -serial null"
Andrew Geissler517393d2023-01-13 08:55:19 -060099QB_DEFAULT_KERNEL ?= "${@bb.utils.contains("INITRAMFS_IMAGE_BUNDLE", "1", "${KERNEL_IMAGETYPE}-${INITRAMFS_LINK_NAME}.bin", "${KERNEL_IMAGETYPE}", d)}"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500100QB_DEFAULT_FSTYPE ?= "ext4"
101QB_RNG ?= "-object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-pci,rng=rng0"
102QB_OPT_APPEND ?= ""
103QB_NETWORK_DEVICE ?= "-device virtio-net-pci,netdev=net0,mac=@MAC@"
104QB_CMDLINE_IP_SLIRP ?= "ip=dhcp"
Patrick Williams2a254922023-08-11 09:48:11 -0500105QB_CMDLINE_IP_TAP ?= "ip=192.168.7.@CLIENT@::192.168.7.@GATEWAY@:255.255.255.0::eth0:off:8.8.8.8 net.ifnames=0"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500106QB_ROOTFS_EXTRA_OPT ?= ""
107QB_GRAPHICS ?= ""
Andrew Geisslerc5535c92023-01-27 16:10:19 -0600108QB_NFSROOTFS_EXTRA_OPT ?= ""
Patrick Williams92b42cb2022-09-03 06:53:57 -0500109
Andrew Geissler20137392023-10-12 04:59:14 -0600110# With 6.5+ (specifically, if DMA_BOUNCE_UNALIGNED_KMALLOC is set) the SW IO TLB
111# is used, and it defaults to 64MB. This is too much when there's only 256MB of
112# RAM, so request 0 slabs and lets the kernel round up to the appropriate minimum
113# (1MB, typically). In virtual hardware there's very little need for these bounce
114# buffers, so the 64MB would be mostly wasted.
115QB_KERNEL_CMDLINE_APPEND:append = " swiotlb=0"
116
Patrick Williams92b42cb2022-09-03 06:53:57 -0500117# This should be kept align with ROOT_VM
118QB_DRIVE_TYPE ?= "/dev/sd"
119
120inherit image-artifact-names
121
122# Create qemuboot.conf
123addtask do_write_qemuboot_conf after do_rootfs before do_image
124
125def qemuboot_vars(d):
126 build_vars = ['MACHINE', 'TUNE_ARCH', 'DEPLOY_DIR_IMAGE',
127 'KERNEL_IMAGETYPE', 'IMAGE_NAME', 'IMAGE_LINK_NAME',
128 'STAGING_DIR_NATIVE', 'STAGING_BINDIR_NATIVE',
129 'STAGING_DIR_HOST', 'SERIAL_CONSOLES', 'UNINATIVE_LOADER']
130 return build_vars + [k for k in d.keys() if k.startswith('QB_')]
131
132do_write_qemuboot_conf[vardeps] += "${@' '.join(qemuboot_vars(d))}"
133do_write_qemuboot_conf[vardepsexclude] += "TOPDIR"
134python do_write_qemuboot_conf() {
135 import configparser
136
137 qemuboot = "%s/%s.qemuboot.conf" % (d.getVar('IMGDEPLOYDIR'), d.getVar('IMAGE_NAME'))
138 if d.getVar('IMAGE_LINK_NAME'):
139 qemuboot_link = "%s/%s.qemuboot.conf" % (d.getVar('IMGDEPLOYDIR'), d.getVar('IMAGE_LINK_NAME'))
140 else:
141 qemuboot_link = ""
142 finalpath = d.getVar("DEPLOY_DIR_IMAGE")
143 topdir = d.getVar('TOPDIR')
144 cf = configparser.ConfigParser()
145 cf.add_section('config_bsp')
146 for k in sorted(qemuboot_vars(d)):
147 if ":" in k:
148 continue
149 # qemu-helper-native sysroot is not removed by rm_work and
150 # contains all tools required by runqemu
151 if k == 'STAGING_BINDIR_NATIVE':
152 val = os.path.join(d.getVar('BASE_WORKDIR'), d.getVar('BUILD_SYS'),
Patrick Williams2a254922023-08-11 09:48:11 -0500153 'qemu-helper-native/1.0/recipe-sysroot-native/usr/bin/')
Patrick Williams92b42cb2022-09-03 06:53:57 -0500154 else:
155 val = d.getVar(k)
156 if val is None:
157 continue
158 # we only want to write out relative paths so that we can relocate images
159 # and still run them
160 if val.startswith(topdir):
161 val = os.path.relpath(val, finalpath)
162 cf.set('config_bsp', k, '%s' % val)
163
164 # QB_DEFAULT_KERNEL's value of KERNEL_IMAGETYPE is the name of a symlink
165 # to the kernel file, which hinders relocatability of the qb conf.
166 # Read the link and replace it with the full filename of the target.
167 kernel_link = os.path.join(d.getVar('DEPLOY_DIR_IMAGE'), d.getVar('QB_DEFAULT_KERNEL'))
168 kernel = os.path.realpath(kernel_link)
169 # we only want to write out relative paths so that we can relocate images
170 # and still run them
171 kernel = os.path.relpath(kernel, finalpath)
172 cf.set('config_bsp', 'QB_DEFAULT_KERNEL', kernel)
173
174 bb.utils.mkdirhier(os.path.dirname(qemuboot))
175 with open(qemuboot, 'w') as f:
176 cf.write(f)
177
178 if qemuboot_link and qemuboot_link != qemuboot:
179 if os.path.lexists(qemuboot_link):
180 os.remove(qemuboot_link)
181 os.symlink(os.path.basename(qemuboot), qemuboot_link)
182}