blob: b9a584351a146674292ba299c130c7a7d4cdf4fa [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7# Baremetal image class
8#
9# This class is meant to be inherited by recipes for baremetal/RTOS applications
10# It contains code that would be used by all of them, every recipe just needs to
11# override certain variables.
12#
13# For scalability purposes, code within this class focuses on the "image" wiring
14# to satisfy the OpenEmbedded image creation and testing infrastructure.
15#
16# See meta-skeleton for a working example.
17
Patrick Williams92b42cb2022-09-03 06:53:57 -050018
19# Toolchain should be baremetal or newlib based.
20# TCLIBC="baremetal" or TCLIBC="newlib"
21COMPATIBLE_HOST:libc-musl:class-target = "null"
22COMPATIBLE_HOST:libc-glibc:class-target = "null"
23
24
25inherit rootfs-postcommands
26
27# Set some defaults, but these should be overriden by each recipe if required
28IMGDEPLOYDIR ?= "${WORKDIR}/deploy-${PN}-image-complete"
29BAREMETAL_BINNAME ?= "hello_baremetal_${MACHINE}"
30IMAGE_LINK_NAME ?= "baremetal-helloworld-image-${MACHINE}"
31IMAGE_NAME_SUFFIX ?= ""
32
33do_rootfs[dirs] = "${IMGDEPLOYDIR} ${DEPLOY_DIR_IMAGE}"
34
35do_image(){
36 install ${D}/${base_libdir}/firmware/${BAREMETAL_BINNAME}.bin ${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.bin
37 install ${D}/${base_libdir}/firmware/${BAREMETAL_BINNAME}.elf ${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.elf
38}
39
40do_image_complete(){
41 :
42}
43
44python do_rootfs(){
45 from oe.utils import execute_pre_post_process
46 from pathlib import Path
47
48 # Write empty manifest file to satisfy test infrastructure
49 deploy_dir = d.getVar('IMGDEPLOYDIR')
50 link_name = d.getVar('IMAGE_LINK_NAME')
51 manifest_name = d.getVar('IMAGE_MANIFEST')
52
53 Path(manifest_name).touch()
54 if os.path.exists(manifest_name) and link_name:
55 manifest_link = deploy_dir + "/" + link_name + ".manifest"
56 if manifest_link != manifest_name:
57 if os.path.lexists(manifest_link):
58 os.remove(manifest_link)
59 os.symlink(os.path.basename(manifest_name), manifest_link)
60 # A lot of postprocess commands assume the existence of rootfs/etc
61 sysconfdir = d.getVar("IMAGE_ROOTFS") + d.getVar('sysconfdir')
62 bb.utils.mkdirhier(sysconfdir)
63
64 execute_pre_post_process(d, d.getVar('ROOTFS_POSTPROCESS_COMMAND'))
65}
66
67
68# Assure binaries, manifest and qemubootconf are populated on DEPLOY_DIR_IMAGE
69do_image_complete[dirs] = "${TOPDIR}"
70SSTATETASKS += "do_image_complete"
71SSTATE_SKIP_CREATION:task-image-complete = '1'
72do_image_complete[sstate-inputdirs] = "${IMGDEPLOYDIR}"
73do_image_complete[sstate-outputdirs] = "${DEPLOY_DIR_IMAGE}"
74do_image_complete[stamp-extra-info] = "${MACHINE_ARCH}"
75addtask do_image_complete after do_image before do_build
76
77python do_image_complete_setscene () {
78 sstate_setscene(d)
79}
80addtask do_image_complete_setscene
81
82# QEMU generic Baremetal/RTOS parameters
83QB_DEFAULT_KERNEL ?= "${IMAGE_LINK_NAME}.bin"
84QB_MEM ?= "-m 256"
85QB_DEFAULT_FSTYPE ?= "bin"
86QB_DTB ?= ""
87QB_OPT_APPEND:append = " -nographic"
88
Andrew Geissler517393d2023-01-13 08:55:19 -060089# QEMU x86 requires an .elf kernel to boot rather than a .bin
90QB_DEFAULT_KERNEL:qemux86 ?= "${IMAGE_LINK_NAME}.elf"
91# QEMU x86-64 refuses to boot from -kernel, needs a multiboot compatible image
92QB_DEFAULT_FSTYPE:qemux86-64 ?= "iso"
93
Patrick Williams92b42cb2022-09-03 06:53:57 -050094# RISC-V tunes set the BIOS, unset, and instruct QEMU to
95# ignore the BIOS and boot from -kernel
96QB_DEFAULT_BIOS:qemuriscv64 = ""
97QB_DEFAULT_BIOS:qemuriscv32 = ""
98QB_OPT_APPEND:append:qemuriscv64 = " -bios none"
99QB_OPT_APPEND:append:qemuriscv32 = " -bios none"
100
101
102# Use the medium-any code model for the RISC-V 64 bit implementation,
103# since medlow can only access addresses below 0x80000000 and RAM
104# starts at 0x80000000 on RISC-V 64
105# Keep RISC-V 32 using -mcmodel=medlow (symbols lie between -2GB:2GB)
106CFLAGS:append:qemuriscv64 = " -mcmodel=medany"
107
108
Andrew Geissler517393d2023-01-13 08:55:19 -0600109## Emulate image.bbclass
110# Handle inherits of any of the image classes we need
111IMAGE_CLASSES ??= ""
112IMGCLASSES = " ${IMAGE_CLASSES}"
Patrick Williams56b44a92024-01-19 08:49:29 -0600113inherit_defer ${IMGCLASSES}
Andrew Geissler517393d2023-01-13 08:55:19 -0600114# Set defaults to satisfy IMAGE_FEATURES check
115IMAGE_FEATURES ?= ""
116IMAGE_FEATURES[type] = "list"
117IMAGE_FEATURES[validitems] += ""
118
119
Patrick Williams92b42cb2022-09-03 06:53:57 -0500120# This next part is necessary to trick the build system into thinking
121# its building an image recipe so it generates the qemuboot.conf
122addtask do_rootfs before do_image after do_install
123addtask do_image after do_rootfs before do_image_complete
124addtask do_image_complete after do_image before do_build
125inherit qemuboot
126
127# Based on image.bbclass to make sure we build qemu
128python(){
129 # do_addto_recipe_sysroot doesnt exist for all recipes, but we need it to have
130 # /usr/bin on recipe-sysroot (qemu) populated
131 # The do_addto_recipe_sysroot dependency is coming from EXTRA_IMAGDEPENDS now,
132 # we just need to add the logic to add its dependency to do_image.
133 def extraimage_getdepends(task):
134 deps = ""
135 for dep in (d.getVar('EXTRA_IMAGEDEPENDS') or "").split():
136 # Make sure we only add it for qemu
137 if 'qemu' in dep:
138 if ":" in dep:
139 deps += " %s " % (dep)
140 else:
141 deps += " %s:%s" % (dep, task)
142 return deps
143 d.appendVarFlag('do_image', 'depends', extraimage_getdepends('do_populate_sysroot'))
144}