1#
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"
16#                                            e.g., "bzImage-initramfs-qemux86-64.bin" if INITRAMFS_IMAGE_BUNDLE is set to 1.
17#
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 command line argument needs to be changed accordingly. Details are documented
66#                      in the kernel documentation 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")
89# 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"
91#
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"
99QB_DEFAULT_KERNEL ?= "${@bb.utils.contains("INITRAMFS_IMAGE_BUNDLE", "1", "${KERNEL_IMAGETYPE}-${INITRAMFS_LINK_NAME}.bin", "${KERNEL_IMAGETYPE}", d)}"
100QB_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@"
104
105# qemurunner needs ip information first, so append QB_NO_PNI
106#
107QB_NO_PNI ?= "${@bb.utils.contains('DISTRO_FEATURES', 'pni-names', '', 'net.ifnames=0', d)}"
108QB_CMDLINE_IP_SLIRP ?= "ip=dhcp"
109QB_CMDLINE_IP_TAP ?= "ip=192.168.7.@CLIENT@::192.168.7.@GATEWAY@:255.255.255.0::eth0:off:8.8.8.8 ${QB_NO_PNI}"
110
111QB_ROOTFS_EXTRA_OPT ?= ""
112QB_GRAPHICS ?= ""
113QB_NFSROOTFS_EXTRA_OPT ?= ""
114
115# With 6.5+ (specifically, if DMA_BOUNCE_UNALIGNED_KMALLOC is set) the SW IO TLB
116# is used, and it defaults to 64MB. This is too much when there's only 256MB of
117# RAM, so request 0 slabs and lets the kernel round up to the appropriate minimum
118# (1MB, typically). In virtual hardware there's very little need for these bounce
119# buffers, so the 64MB would be mostly wasted.
120QB_KERNEL_CMDLINE_APPEND:append = " swiotlb=0"
121
122# This should be kept align with ROOT_VM
123QB_DRIVE_TYPE ?= "/dev/sd"
124
125inherit image-artifact-names
126
127# Create qemuboot.conf
128addtask do_write_qemuboot_conf after do_rootfs before do_image
129
130def qemuboot_vars(d):
131    build_vars = ['MACHINE', 'TUNE_ARCH', 'DEPLOY_DIR_IMAGE',
132                'KERNEL_IMAGETYPE', 'IMAGE_NAME', 'IMAGE_LINK_NAME',
133                'STAGING_DIR_NATIVE', 'STAGING_BINDIR_NATIVE',
134                'STAGING_DIR_HOST', 'SERIAL_CONSOLES', 'UNINATIVE_LOADER']
135    return build_vars + [k for k in d.keys() if k.startswith('QB_')]
136
137do_write_qemuboot_conf[vardeps] += "${@' '.join(qemuboot_vars(d))}"
138do_write_qemuboot_conf[vardepsexclude] += "TOPDIR"
139python do_write_qemuboot_conf() {
140    import configparser
141
142    qemuboot = "%s/%s.qemuboot.conf" % (d.getVar('IMGDEPLOYDIR'), d.getVar('IMAGE_NAME'))
143    if d.getVar('IMAGE_LINK_NAME'):
144        qemuboot_link = "%s/%s.qemuboot.conf" % (d.getVar('IMGDEPLOYDIR'), d.getVar('IMAGE_LINK_NAME'))
145    else:
146        qemuboot_link = ""
147    finalpath = d.getVar("DEPLOY_DIR_IMAGE")
148    topdir = d.getVar('TOPDIR')
149    cf = configparser.ConfigParser()
150    cf.add_section('config_bsp')
151    for k in sorted(qemuboot_vars(d)):
152        if ":" in k:
153            continue
154        # qemu-helper-native sysroot is not removed by rm_work and
155        # contains all tools required by runqemu
156        if k == 'STAGING_BINDIR_NATIVE':
157            val = os.path.join(d.getVar('BASE_WORKDIR'), d.getVar('BUILD_SYS'),
158                               'qemu-helper-native/1.0/recipe-sysroot-native/usr/bin/')
159        else:
160            val = d.getVar(k)
161        if val is None:
162            continue
163        # we only want to write out relative paths so that we can relocate images
164        # and still run them
165        if val.startswith(topdir):
166            val = os.path.relpath(val, finalpath)
167        cf.set('config_bsp', k, '%s' % val)
168
169    # QB_DEFAULT_KERNEL's value of KERNEL_IMAGETYPE is the name of a symlink
170    # to the kernel file, which hinders relocatability of the qb conf.
171    # Read the link and replace it with the full filename of the target.
172    kernel_link = os.path.join(d.getVar('DEPLOY_DIR_IMAGE'), d.getVar('QB_DEFAULT_KERNEL'))
173    kernel = os.path.realpath(kernel_link)
174    # we only want to write out relative paths so that we can relocate images
175    # and still run them
176    kernel = os.path.relpath(kernel, finalpath)
177    cf.set('config_bsp', 'QB_DEFAULT_KERNEL', kernel)
178
179    bb.utils.mkdirhier(os.path.dirname(qemuboot))
180    with open(qemuboot, 'w') as f:
181        cf.write(f)
182
183    if qemuboot_link and qemuboot_link != qemuboot:
184        if os.path.lexists(qemuboot_link):
185           os.remove(qemuboot_link)
186        os.symlink(os.path.basename(qemuboot), qemuboot_link)
187}
188
189EXTRA_IMAGEDEPENDS += "qemu-system-native qemu-helper-native:do_addto_recipe_sysroot"
190