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 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 18 | |
| 19 | # Toolchain should be baremetal or newlib based. |
| 20 | # TCLIBC="baremetal" or TCLIBC="newlib" |
| 21 | COMPATIBLE_HOST:libc-musl:class-target = "null" |
| 22 | COMPATIBLE_HOST:libc-glibc:class-target = "null" |
| 23 | |
| 24 | |
| 25 | inherit rootfs-postcommands |
| 26 | |
| 27 | # Set some defaults, but these should be overriden by each recipe if required |
| 28 | IMGDEPLOYDIR ?= "${WORKDIR}/deploy-${PN}-image-complete" |
| 29 | BAREMETAL_BINNAME ?= "hello_baremetal_${MACHINE}" |
| 30 | IMAGE_LINK_NAME ?= "baremetal-helloworld-image-${MACHINE}" |
| 31 | IMAGE_NAME_SUFFIX ?= "" |
| 32 | |
| 33 | do_rootfs[dirs] = "${IMGDEPLOYDIR} ${DEPLOY_DIR_IMAGE}" |
| 34 | |
| 35 | do_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 | |
| 40 | do_image_complete(){ |
| 41 | : |
| 42 | } |
| 43 | |
| 44 | python 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 |
| 69 | do_image_complete[dirs] = "${TOPDIR}" |
| 70 | SSTATETASKS += "do_image_complete" |
| 71 | SSTATE_SKIP_CREATION:task-image-complete = '1' |
| 72 | do_image_complete[sstate-inputdirs] = "${IMGDEPLOYDIR}" |
| 73 | do_image_complete[sstate-outputdirs] = "${DEPLOY_DIR_IMAGE}" |
| 74 | do_image_complete[stamp-extra-info] = "${MACHINE_ARCH}" |
| 75 | addtask do_image_complete after do_image before do_build |
| 76 | |
| 77 | python do_image_complete_setscene () { |
| 78 | sstate_setscene(d) |
| 79 | } |
| 80 | addtask do_image_complete_setscene |
| 81 | |
| 82 | # QEMU generic Baremetal/RTOS parameters |
| 83 | QB_DEFAULT_KERNEL ?= "${IMAGE_LINK_NAME}.bin" |
| 84 | QB_MEM ?= "-m 256" |
| 85 | QB_DEFAULT_FSTYPE ?= "bin" |
| 86 | QB_DTB ?= "" |
| 87 | QB_OPT_APPEND:append = " -nographic" |
| 88 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 89 | # QEMU x86 requires an .elf kernel to boot rather than a .bin |
| 90 | QB_DEFAULT_KERNEL:qemux86 ?= "${IMAGE_LINK_NAME}.elf" |
| 91 | # QEMU x86-64 refuses to boot from -kernel, needs a multiboot compatible image |
| 92 | QB_DEFAULT_FSTYPE:qemux86-64 ?= "iso" |
| 93 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 94 | # RISC-V tunes set the BIOS, unset, and instruct QEMU to |
| 95 | # ignore the BIOS and boot from -kernel |
| 96 | QB_DEFAULT_BIOS:qemuriscv64 = "" |
| 97 | QB_DEFAULT_BIOS:qemuriscv32 = "" |
| 98 | QB_OPT_APPEND:append:qemuriscv64 = " -bios none" |
| 99 | QB_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) |
| 106 | CFLAGS:append:qemuriscv64 = " -mcmodel=medany" |
| 107 | |
| 108 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 109 | ## Emulate image.bbclass |
| 110 | # Handle inherits of any of the image classes we need |
| 111 | IMAGE_CLASSES ??= "" |
| 112 | IMGCLASSES = " ${IMAGE_CLASSES}" |
| 113 | inherit ${IMGCLASSES} |
| 114 | # Set defaults to satisfy IMAGE_FEATURES check |
| 115 | IMAGE_FEATURES ?= "" |
| 116 | IMAGE_FEATURES[type] = "list" |
| 117 | IMAGE_FEATURES[validitems] += "" |
| 118 | |
| 119 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 120 | # This next part is necessary to trick the build system into thinking |
| 121 | # its building an image recipe so it generates the qemuboot.conf |
| 122 | addtask do_rootfs before do_image after do_install |
| 123 | addtask do_image after do_rootfs before do_image_complete |
| 124 | addtask do_image_complete after do_image before do_build |
| 125 | inherit qemuboot |
| 126 | |
| 127 | # Based on image.bbclass to make sure we build qemu |
| 128 | python(){ |
| 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 | } |