SUMMARY = "STM32MP1 Firmware examples for CM4"
LICENSE = " \
    Apache-2.0 \
    & MIT \
    & BSD-3-Clause \
    "
LIC_FILES_CHKSUM = "file://License.md;md5=6b267c73760fea024ffbc6049db473ba"

COMPATIBLE_MACHINE = "(stm32mpcommon)"

PACKAGE_ARCH = "${MACHINE_ARCH}"

SRC_URI = "git://github.com/STMicroelectronics/STM32CubeMP1;protocol=https;branch=master"
SRCREV  = "66ec26f6422283fe5cd25a32babf925836df9399"

PV = "1.0.0"

S = "${WORKDIR}/git"

B = "${WORKDIR}/build"
# for external source support with devtools
EXTERNALSRC_BUILD_pn-${PN} = "${WORKDIR}/build"

# M4 firwmare build file
SRC_URI += "file://Makefile.stm32"
SRC_URI += "file://parse_project_config.py"

# default service for systemd
inherit systemd update-rc.d
SRC_URI += "file://st-m4firmware-load-default.sh"
SRC_URI += "file://st-m4firmware-load.service"

DEPENDS += " gcc-arm-none-eabi-native "

# Define default board reference for M4
M4_BOARDS ?= "STM32MP157C-EV1 STM32MP157C-DK2"

# Init default copro example to load/execute
DEFAULT_COPRO_FIRMWARE ?= ""

# Use gcc bare toolchain
export CROSS_COMPILE = "${RECIPE_SYSROOT_NATIVE}/${datadir}/gcc-arm-none-eabi/bin/arm-none-eabi-"
export CPU_TYPE = "M4"
export BUILD_CONFIG = "Debug"

RDEPENDS_${PN}-userfs += "busybox"
PACKAGES += "${PN}-userfs"

USERFS_ROOTPATH = "/local/Cube-M4-examples"
FILES_${PN}-userfs = "${prefix}${USERFS_ROOTPATH}"

do_compile() {
    # Compile M4 firmwares listed in bb file. They can be for eval or disco board as specified in bb file
    for BIN_MACHINE in ${M4_BOARDS}; do
        bbnote "Compile M4 project for : ${BIN_MACHINE}"
        for project in ${PROJECTS_LIST} ; do
            bbnote "Parsing M4 project : ${project}"

            if [ "$(echo ${project} | cut -d'/' -f1)" = "${BIN_MACHINE}" ]; then
                bbnote "Selected M4 project : ${project}"

                unset LDFLAGS CFLAGS CPPFLAGS CFLAGS_ASM
                # Export variables as used by Makefile
                export BIN_NAME=$(basename ${project})
                export PROJECT_DIR=${B}/${project}
                export PROJECT_APP="${S}/Projects/${project}/SW4STM32/${BIN_NAME}"

                bbnote "BIN_NAME     : ${BIN_NAME}"
                bbnote "PROJECT_DIR  : ${PROJECT_DIR}"
                bbnote "PROJECT_APP  : ${PROJECT_APP}"
                bbnote "BUILD_CONFIG : ${BUILD_CONFIG}"
                bbnote "CPU_TYPE     : ${CPU_TYPE}"
                bbnote "SOURCE       : ${S}"

                mkdir -p ${PROJECT_DIR}/out/${BUILD_CONFIG}

                # parse project to get file list and build flags
                python ${WORKDIR}/parse_project_config.py ${PROJECT_APP} ${BUILD_CONFIG} ${PROJECT_DIR}

                # make clean
                bbnote "Cleaning M4 project : ${project}"
                oe_runmake -f ${WORKDIR}/Makefile.stm32 clean

                # make build
                bbnote "Building M4 project : ${project}"
                oe_runmake -f ${WORKDIR}/Makefile.stm32 all
            fi
        done
    done
}

do_install() {
    # Install M4 firmwares, scripts and README in userfs:
    # <userfs>/local/Cube-M4-examples/${project} (ie STM32MP157C-EV1/Applications/OpenAMP/OpenAMP_TTY_echo)
    #                        |-- fw_cortex_m4.sh
    #                        |-- README
    #                        |--lib
    #                             |--firmware
    #                                  |-- ELF file for impacted board (ie OpenAMP_TTY_echo.elf)
    for BIN_MACHINE in ${M4_BOARDS}; do
        for project in ${PROJECTS_LIST} ; do
            if [ "$(echo ${project} | cut -d'/' -f1)" = "${BIN_MACHINE}" ]; then
                # BIN_NAME=$(echo ${project} | cut -d'/' -f4)
                BIN_NAME=$(basename ${project})

                # Install M4 firmwares
                install -d ${D}${prefix}${USERFS_ROOTPATH}/${project}/lib/firmware/
                install -m 0755 ${B}/${project}/out/${BUILD_CONFIG}/${BIN_NAME}.elf ${D}${prefix}${USERFS_ROOTPATH}/${project}/lib/firmware/

                # Install sh and README files if any for each example
                if [ -e ${S}/Projects/${project}/Remoteproc/fw_cortex_m4.sh ]; then
                    install -m 0755 ${S}/Projects/${project}/Remoteproc/fw_cortex_m4.sh ${D}${prefix}${USERFS_ROOTPATH}/${project}
                fi
                if [ -e ${S}/Projects/${project}/Remoteproc/README ]; then
                    install -m 0755 ${S}/Projects/${project}/Remoteproc/README ${D}${prefix}${USERFS_ROOTPATH}/${project}
                fi
            fi
        done
    done

    # Install sh script to load/execute DEFAULT_COPRO_FIRMWARE @boot up
    if [ -n "${DEFAULT_COPRO_FIRMWARE}" ]; then
        # Install systemd service
        install -d ${D}${sysconfdir}/init.d/
        install -d ${D}${base_sbindir}/
        install -m 0755 ${WORKDIR}/st-m4firmware-load-default.sh ${D}${sysconfdir}/init.d/st-m4firmware-load-default.sh
        install -m 0755 ${WORKDIR}/st-m4firmware-load-default.sh ${D}${base_sbindir}/st-m4firmware-load-default.sh

        sed -i -e "s:@default_fw@:${DEFAULT_COPRO_FIRMWARE}:" \
        ${D}${sysconfdir}/init.d/st-m4firmware-load-default.sh
        sed -i -e "s:@default_fw@:${DEFAULT_COPRO_FIRMWARE}:" \
        ${D}${base_sbindir}/st-m4firmware-load-default.sh

        sed -i -e "s:@userfs_mount_point@:${STM32MP_USERFS_MOUNTPOINT_IMAGE}:" \
        ${D}${sysconfdir}/init.d/st-m4firmware-load-default.sh
        sed -i -e "s:@userfs_mount_point@:${STM32MP_USERFS_MOUNTPOINT_IMAGE}:" \
        ${D}${base_sbindir}/st-m4firmware-load-default.sh
    fi

    # install systemd service for all machines configurations
    if ${@bb.utils.contains('DISTRO_FEATURES','systemd','true','false',d)}; then
        install -d ${D}${systemd_unitdir}/system
        install -m 644 ${WORKDIR}/st-m4firmware-load.service ${D}/${systemd_unitdir}/system
    fi
}

# -----------------------------------------------------------
# specific for service: start copro m4 firwmare at boot time

SYSTEMD_PACKAGES += " m4projects-stm32mp1 "
SYSTEMD_SERVICE_${PN} = "st-m4firmware-load.service"
SYSTEMD_AUTO_ENABLE_${PN} = "disable"

INITSCRIPT_NAME = "st-m4firmware-load-default.sh"
INITSCRIPT_PARAMS = "stop 22 5 3 ."

INHIBIT_PACKAGE_DEBUG_SPLIT = "1"
INHIBIT_PACKAGE_STRIP = "1"
INHIBIT_SYSROOT_STRIP = "1"

FILES_${PN} += "${nonarch_base_libdir}/firmware ${sysconfdir}/init.d ${systemd_unitdir}/system"
