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