Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame^] | 1 | # |
| 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 |
| 20 | IMAGE_CLASSES ??= "" |
| 21 | IMGCLASSES = " ${IMAGE_CLASSES}" |
| 22 | inherit ${IMGCLASSES} |
| 23 | # Set defaults to satisfy IMAGE_FEATURES check |
| 24 | IMAGE_FEATURES ?= "" |
| 25 | IMAGE_FEATURES[type] = "list" |
| 26 | IMAGE_FEATURES[validitems] += "" |
| 27 | |
| 28 | # Toolchain should be baremetal or newlib based. |
| 29 | # TCLIBC="baremetal" or TCLIBC="newlib" |
| 30 | COMPATIBLE_HOST:libc-musl:class-target = "null" |
| 31 | COMPATIBLE_HOST:libc-glibc:class-target = "null" |
| 32 | |
| 33 | |
| 34 | inherit rootfs-postcommands |
| 35 | |
| 36 | # Set some defaults, but these should be overriden by each recipe if required |
| 37 | IMGDEPLOYDIR ?= "${WORKDIR}/deploy-${PN}-image-complete" |
| 38 | BAREMETAL_BINNAME ?= "hello_baremetal_${MACHINE}" |
| 39 | IMAGE_LINK_NAME ?= "baremetal-helloworld-image-${MACHINE}" |
| 40 | IMAGE_NAME_SUFFIX ?= "" |
| 41 | |
| 42 | do_rootfs[dirs] = "${IMGDEPLOYDIR} ${DEPLOY_DIR_IMAGE}" |
| 43 | |
| 44 | do_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 | |
| 49 | do_image_complete(){ |
| 50 | : |
| 51 | } |
| 52 | |
| 53 | python 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 |
| 78 | do_image_complete[dirs] = "${TOPDIR}" |
| 79 | SSTATETASKS += "do_image_complete" |
| 80 | SSTATE_SKIP_CREATION:task-image-complete = '1' |
| 81 | do_image_complete[sstate-inputdirs] = "${IMGDEPLOYDIR}" |
| 82 | do_image_complete[sstate-outputdirs] = "${DEPLOY_DIR_IMAGE}" |
| 83 | do_image_complete[stamp-extra-info] = "${MACHINE_ARCH}" |
| 84 | addtask do_image_complete after do_image before do_build |
| 85 | |
| 86 | python do_image_complete_setscene () { |
| 87 | sstate_setscene(d) |
| 88 | } |
| 89 | addtask do_image_complete_setscene |
| 90 | |
| 91 | # QEMU generic Baremetal/RTOS parameters |
| 92 | QB_DEFAULT_KERNEL ?= "${IMAGE_LINK_NAME}.bin" |
| 93 | QB_MEM ?= "-m 256" |
| 94 | QB_DEFAULT_FSTYPE ?= "bin" |
| 95 | QB_DTB ?= "" |
| 96 | QB_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 |
| 100 | QB_DEFAULT_BIOS:qemuriscv64 = "" |
| 101 | QB_DEFAULT_BIOS:qemuriscv32 = "" |
| 102 | QB_OPT_APPEND:append:qemuriscv64 = " -bios none" |
| 103 | QB_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) |
| 110 | CFLAGS: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 |
| 115 | addtask do_rootfs before do_image after do_install |
| 116 | addtask do_image after do_rootfs before do_image_complete |
| 117 | addtask do_image_complete after do_image before do_build |
| 118 | inherit qemuboot |
| 119 | |
| 120 | # Based on image.bbclass to make sure we build qemu |
| 121 | python(){ |
| 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 | } |