blob: d3377a92fa131132dae4c06e06a5cb087d486ba8 [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
18## Emulate image.bbclass
19# Handle inherits of any of the image classes we need
20IMAGE_CLASSES ??= ""
21IMGCLASSES = " ${IMAGE_CLASSES}"
22inherit ${IMGCLASSES}
23# Set defaults to satisfy IMAGE_FEATURES check
24IMAGE_FEATURES ?= ""
25IMAGE_FEATURES[type] = "list"
26IMAGE_FEATURES[validitems] += ""
27
28# Toolchain should be baremetal or newlib based.
29# TCLIBC="baremetal" or TCLIBC="newlib"
30COMPATIBLE_HOST:libc-musl:class-target = "null"
31COMPATIBLE_HOST:libc-glibc:class-target = "null"
32
33
34inherit rootfs-postcommands
35
36# Set some defaults, but these should be overriden by each recipe if required
37IMGDEPLOYDIR ?= "${WORKDIR}/deploy-${PN}-image-complete"
38BAREMETAL_BINNAME ?= "hello_baremetal_${MACHINE}"
39IMAGE_LINK_NAME ?= "baremetal-helloworld-image-${MACHINE}"
40IMAGE_NAME_SUFFIX ?= ""
41
42do_rootfs[dirs] = "${IMGDEPLOYDIR} ${DEPLOY_DIR_IMAGE}"
43
44do_image(){
45 install ${D}/${base_libdir}/firmware/${BAREMETAL_BINNAME}.bin ${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.bin
46 install ${D}/${base_libdir}/firmware/${BAREMETAL_BINNAME}.elf ${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.elf
47}
48
49do_image_complete(){
50 :
51}
52
53python do_rootfs(){
54 from oe.utils import execute_pre_post_process
55 from pathlib import Path
56
57 # Write empty manifest file to satisfy test infrastructure
58 deploy_dir = d.getVar('IMGDEPLOYDIR')
59 link_name = d.getVar('IMAGE_LINK_NAME')
60 manifest_name = d.getVar('IMAGE_MANIFEST')
61
62 Path(manifest_name).touch()
63 if os.path.exists(manifest_name) and link_name:
64 manifest_link = deploy_dir + "/" + link_name + ".manifest"
65 if manifest_link != manifest_name:
66 if os.path.lexists(manifest_link):
67 os.remove(manifest_link)
68 os.symlink(os.path.basename(manifest_name), manifest_link)
69 # A lot of postprocess commands assume the existence of rootfs/etc
70 sysconfdir = d.getVar("IMAGE_ROOTFS") + d.getVar('sysconfdir')
71 bb.utils.mkdirhier(sysconfdir)
72
73 execute_pre_post_process(d, d.getVar('ROOTFS_POSTPROCESS_COMMAND'))
74}
75
76
77# Assure binaries, manifest and qemubootconf are populated on DEPLOY_DIR_IMAGE
78do_image_complete[dirs] = "${TOPDIR}"
79SSTATETASKS += "do_image_complete"
80SSTATE_SKIP_CREATION:task-image-complete = '1'
81do_image_complete[sstate-inputdirs] = "${IMGDEPLOYDIR}"
82do_image_complete[sstate-outputdirs] = "${DEPLOY_DIR_IMAGE}"
83do_image_complete[stamp-extra-info] = "${MACHINE_ARCH}"
84addtask do_image_complete after do_image before do_build
85
86python do_image_complete_setscene () {
87 sstate_setscene(d)
88}
89addtask do_image_complete_setscene
90
91# QEMU generic Baremetal/RTOS parameters
92QB_DEFAULT_KERNEL ?= "${IMAGE_LINK_NAME}.bin"
93QB_MEM ?= "-m 256"
94QB_DEFAULT_FSTYPE ?= "bin"
95QB_DTB ?= ""
96QB_OPT_APPEND:append = " -nographic"
97
98# RISC-V tunes set the BIOS, unset, and instruct QEMU to
99# ignore the BIOS and boot from -kernel
100QB_DEFAULT_BIOS:qemuriscv64 = ""
101QB_DEFAULT_BIOS:qemuriscv32 = ""
102QB_OPT_APPEND:append:qemuriscv64 = " -bios none"
103QB_OPT_APPEND:append:qemuriscv32 = " -bios none"
104
105
106# Use the medium-any code model for the RISC-V 64 bit implementation,
107# since medlow can only access addresses below 0x80000000 and RAM
108# starts at 0x80000000 on RISC-V 64
109# Keep RISC-V 32 using -mcmodel=medlow (symbols lie between -2GB:2GB)
110CFLAGS:append:qemuriscv64 = " -mcmodel=medany"
111
112
113# This next part is necessary to trick the build system into thinking
114# its building an image recipe so it generates the qemuboot.conf
115addtask do_rootfs before do_image after do_install
116addtask do_image after do_rootfs before do_image_complete
117addtask do_image_complete after do_image before do_build
118inherit qemuboot
119
120# Based on image.bbclass to make sure we build qemu
121python(){
122 # do_addto_recipe_sysroot doesnt exist for all recipes, but we need it to have
123 # /usr/bin on recipe-sysroot (qemu) populated
124 # The do_addto_recipe_sysroot dependency is coming from EXTRA_IMAGDEPENDS now,
125 # we just need to add the logic to add its dependency to do_image.
126 def extraimage_getdepends(task):
127 deps = ""
128 for dep in (d.getVar('EXTRA_IMAGEDEPENDS') or "").split():
129 # Make sure we only add it for qemu
130 if 'qemu' in dep:
131 if ":" in dep:
132 deps += " %s " % (dep)
133 else:
134 deps += " %s:%s" % (dep, task)
135 return deps
136 d.appendVarFlag('do_image', 'depends', extraimage_getdepends('do_populate_sysroot'))
137}