1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7#
8# This class contains functions for recipes that need QEMU or test for its
9# existence.
10#
11
12def qemu_target_binary(data):
13    package_arch = data.getVar("PACKAGE_ARCH")
14    qemu_target_binary = (data.getVar("QEMU_TARGET_BINARY_%s" % package_arch) or "")
15    if qemu_target_binary:
16        return qemu_target_binary
17
18    target_arch = data.getVar("TARGET_ARCH")
19    if target_arch in ("i486", "i586", "i686"):
20        target_arch = "i386"
21    elif target_arch == "powerpc":
22        target_arch = "ppc"
23    elif target_arch == "powerpc64":
24        target_arch = "ppc64"
25    elif target_arch == "powerpc64le":
26        target_arch = "ppc64le"
27
28    return "qemu-" + target_arch
29
30def qemu_wrapper_cmdline(data, rootfs_path, library_paths):
31    import string
32
33    qemu_binary = qemu_target_binary(data)
34    if qemu_binary == "qemu-allarch":
35        qemu_binary = "qemuwrapper"
36
37    qemu_options = data.getVar("QEMU_OPTIONS") or ""
38
39    return "PSEUDO_UNLOAD=1 " + qemu_binary + " " + qemu_options + " -L " + rootfs_path\
40            + " -E LD_LIBRARY_PATH=" + ":".join(library_paths) + " "
41
42# Next function will return a string containing the command that is needed to
43# to run a certain binary through qemu. For example, in order to make a certain
44# postinstall scriptlet run at do_rootfs time and running the postinstall is
45# architecture dependent, we can run it through qemu. For example, in the
46# postinstall scriptlet, we could use the following:
47#
48# ${@qemu_run_binary(d, '$D', '/usr/bin/test_app')} [test_app arguments]
49#
50def qemu_run_binary(data, rootfs_path, binary):
51    libdir = rootfs_path + data.getVar("libdir", False)
52    base_libdir = rootfs_path + data.getVar("base_libdir", False)
53
54    return qemu_wrapper_cmdline(data, rootfs_path, [libdir, base_libdir]) + rootfs_path + binary
55
56# QEMU_EXTRAOPTIONS is not meant to be directly used, the extensions are
57# PACKAGE_ARCH, *NOT* overrides.
58# In some cases (e.g. ppc) simply being arch specific (apparently) isn't good
59# enough and a PACKAGE_ARCH specific -cpu option is needed (hence we have to do
60# this dance). For others (e.g. arm) a -cpu option is not necessary, since the
61# qemu-arm default CPU supports all required architecture levels.
62
63QEMU_OPTIONS = "-r ${OLDEST_KERNEL} ${@d.getVar("QEMU_EXTRAOPTIONS_%s" % d.getVar('PACKAGE_ARCH')) or ""}"
64QEMU_OPTIONS[vardeps] += "QEMU_EXTRAOPTIONS_${PACKAGE_ARCH}"
65
66QEMU_EXTRAOPTIONS_ppce500v2 = " -cpu e500v2"
67QEMU_EXTRAOPTIONS_ppce500mc = " -cpu e500mc"
68QEMU_EXTRAOPTIONS_ppce5500 = " -cpu e500mc"
69QEMU_EXTRAOPTIONS_ppc64e5500 = " -cpu e500mc"
70QEMU_EXTRAOPTIONS_ppce6500 = " -cpu e500mc"
71QEMU_EXTRAOPTIONS_ppc64e6500 = " -cpu e500mc"
72QEMU_EXTRAOPTIONS_ppc7400 = " -cpu 7400"
73QEMU_EXTRAOPTIONS_powerpc64le = " -cpu POWER9"
74# Some packages e.g. fwupd sets PACKAGE_ARCH = MACHINE_ARCH and uses meson which
75# needs right options to usermode qemu
76QEMU_EXTRAOPTIONS_qemuppc = " -cpu 7400"
77QEMU_EXTRAOPTIONS_qemuppc64 = " -cpu POWER9"
78