[Add kernel_in_ubi feature to separate image format]

[Description]
Add kernel_in_ubi feature to separate image format
1. add kernel in ubi feature
2. mtk.nand and factory/sysupgrade image will gen after do_image_complete

[Release-log]

diff --git a/conf/machine/filogic830-32bit.conf b/conf/machine/filogic830-32bit.conf
index b1c7f99..aef275c 100644
--- a/conf/machine/filogic830-32bit.conf
+++ b/conf/machine/filogic830-32bit.conf
@@ -28,7 +28,7 @@
 
 DISTRO_FEATURES_append = " meshwifi"
 DISTRO_FEATURES_append = " ipv6"
-
+#DISTRO_FEATURES_append = " kernel_in_ubi"
 # kernel 5.4 gold linker 'arm-rdk-linux-gnueabi-ld' not supported
 #DISTRO_FEATURES_append = " referencepltfm "
 #DISTRO_FEATURES_remove = " ld-is-gold"
diff --git a/conf/machine/filogic830.conf b/conf/machine/filogic830.conf
index 468f92e..4410b6d 100644
--- a/conf/machine/filogic830.conf
+++ b/conf/machine/filogic830.conf
@@ -28,6 +28,7 @@
 
 DISTRO_FEATURES_append = " meshwifi"
 DISTRO_FEATURES_append = " ipv6"
+#DISTRO_FEATURES_append = " kernel_in_ubi"
 
 # kernel 5.4 gold linker 'arm-rdk-linux-gnueabi-ld' not supported
 #DISTRO_FEATURES_append = " referencepltfm "
diff --git a/recipes-core/images/rdk-generic-broadband-image.bbappend b/recipes-core/images/rdk-generic-broadband-image.bbappend
index c2f40b6..c2ecfdd 100644
--- a/recipes-core/images/rdk-generic-broadband-image.bbappend
+++ b/recipes-core/images/rdk-generic-broadband-image.bbappend
@@ -48,3 +48,64 @@
 }
 
 ROOTFS_POSTPROCESS_COMMAND_append = "remove_unused_file; "
+
+do_filogic_gen_image(){
+	if ${@bb.utils.contains('DISTRO_FEATURES','kernel_in_ubi','true','false',d)}; then
+        # create factory image align to openwrt (kernel in ubi)   
+
+            echo \[kernel\] > ubinize.cfg
+            echo mode=ubi >> ubinize.cfg
+            echo image=${DEPLOY_DIR_IMAGE}/fitImage >> ubinize.cfg
+            echo vol_id=0 >> ubinize.cfg
+            echo vol_type=dynamic >> ubinize.cfg
+            echo vol_name=kernel >> ubinize.cfg
+            echo \[rootfs\] >> ubinize.cfg
+            echo mode=ubi >> ubinize.cfg
+            echo image=${IMGDEPLOYDIR}/${PN}-${MACHINE}.squashfs-xz >> ubinize.cfg
+            echo vol_id=1 >> ubinize.cfg
+            echo vol_type=dynamic >> ubinize.cfg
+            echo vol_name=rootfs >> ubinize.cfg
+            echo \[rootfs_data\] >> ubinize.cfg
+            echo mode=ubi >> ubinize.cfg
+            echo vol_id=2 >> ubinize.cfg
+            echo vol_type=dynamic >> ubinize.cfg
+            echo vol_name=rootfs_data >> ubinize.cfg
+            echo vol_size=1MiB >> ubinize.cfg
+            echo vol_flags=autoresize >> ubinize.cfg
+            ubinize -o ${DEPLOY_DIR_IMAGE}/${PN}-${MACHINE}-factory.bin ${UBINIZE_ARGS} ubinize.cfg
+            mv ubinize.cfg ${DEPLOY_DIR_IMAGE}/
+
+        # create sysupgrade image align to openwrt
+
+            rm -rf ${IMGDEPLOYDIR}/sysupgrade-${PN}-${MACHINE}
+            rm -rf ${IMGDEPLOYDIR}/${PN}-${MACHINE}-sysupgrade.bin
+
+            mkdir ${IMGDEPLOYDIR}/sysupgrade-${PN}-${MACHINE}
+
+            cp ${DEPLOY_DIR_IMAGE}/fitImage ${IMGDEPLOYDIR}/sysupgrade-${PN}-${MACHINE}/kernel
+            cp ${IMGDEPLOYDIR}/${PN}-${MACHINE}.squashfs-xz ${IMGDEPLOYDIR}/sysupgrade-${PN}-${MACHINE}/root
+
+            cd ${IMGDEPLOYDIR}
+            tar cvf ${PN}-${MACHINE}-sysupgrade.bin sysupgrade-${PN}-${MACHINE}
+            mv ${PN}-${MACHINE}-sysupgrade.bin ${DEPLOY_DIR_IMAGE}/
+    else
+            rm -f ${NAND_FILE}
+
+            # 1. dump fitImage into firmware
+            dd if=${DEPLOY_DIR_IMAGE}/fitImage >> ${NAND_FILE}
+
+            # 2. pad to 256K
+            dd if=${NAND_FILE} of=${NAND_FILE}.new bs=256k conv=sync
+            mv -f ${NAND_FILE}.new ${NAND_FILE}
+
+            # 3. pad to kernel size = 0x800000 = 8388608
+            dd if=${NAND_FILE} of=${NAND_FILE}.new bs=8388608 conv=sync
+            mv -f ${NAND_FILE}.new ${NAND_FILE}
+
+            # 4. dump filesystem into firmware
+            dd if=${IMGDEPLOYDIR}/${PN}-${MACHINE}.${NAND_ROOTFS_TYPE} >> ${NAND_FILE}
+            mv ${NAND_FILE} ${DEPLOY_DIR_IMAGE}/         
+    fi
+
+}
+addtask filogic_gen_image after do_image_complete before do_populate_lic_deploy
diff --git a/recipes-core/images/rdk-generic-broadband-tdk-image.bbappend b/recipes-core/images/rdk-generic-broadband-tdk-image.bbappend
index 4fca211..c669047 100644
--- a/recipes-core/images/rdk-generic-broadband-tdk-image.bbappend
+++ b/recipes-core/images/rdk-generic-broadband-tdk-image.bbappend
@@ -46,3 +46,64 @@
 }
 
 ROOTFS_POSTPROCESS_COMMAND_append = "remove_unused_file; "
+
+do_filogic_gen_image(){
+	if ${@bb.utils.contains('DISTRO_FEATURES','kernel_in_ubi','true','false',d)}; then
+        # create factory image align to openwrt (kernel in ubi)   
+
+            echo \[kernel\] > ubinize.cfg
+            echo mode=ubi >> ubinize.cfg
+            echo image=${DEPLOY_DIR_IMAGE}/fitImage >> ubinize.cfg
+            echo vol_id=0 >> ubinize.cfg
+            echo vol_type=dynamic >> ubinize.cfg
+            echo vol_name=kernel >> ubinize.cfg
+            echo \[rootfs\] >> ubinize.cfg
+            echo mode=ubi >> ubinize.cfg
+            echo image=${IMGDEPLOYDIR}/${PN}-${MACHINE}.squashfs-xz >> ubinize.cfg
+            echo vol_id=1 >> ubinize.cfg
+            echo vol_type=dynamic >> ubinize.cfg
+            echo vol_name=rootfs >> ubinize.cfg
+            echo \[rootfs_data\] >> ubinize.cfg
+            echo mode=ubi >> ubinize.cfg
+            echo vol_id=2 >> ubinize.cfg
+            echo vol_type=dynamic >> ubinize.cfg
+            echo vol_name=rootfs_data >> ubinize.cfg
+            echo vol_size=1MiB >> ubinize.cfg
+            echo vol_flags=autoresize >> ubinize.cfg
+            ubinize -o ${DEPLOY_DIR_IMAGE}/${PN}-${MACHINE}-factory.bin ${UBINIZE_ARGS} ubinize.cfg
+            mv ubinize.cfg ${DEPLOY_DIR_IMAGE}/
+
+        # create sysupgrade image align to openwrt
+
+            rm -rf ${IMGDEPLOYDIR}/sysupgrade-${PN}-${MACHINE}
+            rm -rf ${IMGDEPLOYDIR}/${PN}-${MACHINE}-sysupgrade.bin
+
+            mkdir ${IMGDEPLOYDIR}/sysupgrade-${PN}-${MACHINE}
+
+            cp ${DEPLOY_DIR_IMAGE}/fitImage ${IMGDEPLOYDIR}/sysupgrade-${PN}-${MACHINE}/kernel
+            cp ${IMGDEPLOYDIR}/${PN}-${MACHINE}.squashfs-xz ${IMGDEPLOYDIR}/sysupgrade-${PN}-${MACHINE}/root
+
+            cd ${IMGDEPLOYDIR}
+            tar cvf ${PN}-${MACHINE}-sysupgrade.bin sysupgrade-${PN}-${MACHINE}
+            mv ${PN}-${MACHINE}-sysupgrade.bin ${DEPLOY_DIR_IMAGE}/
+    else
+            rm -f ${NAND_FILE}
+
+            # 1. dump fitImage into firmware
+            dd if=${DEPLOY_DIR_IMAGE}/fitImage >> ${NAND_FILE}
+
+            # 2. pad to 256K
+            dd if=${NAND_FILE} of=${NAND_FILE}.new bs=256k conv=sync
+            mv -f ${NAND_FILE}.new ${NAND_FILE}
+
+            # 3. pad to kernel size = 0x800000 = 8388608
+            dd if=${NAND_FILE} of=${NAND_FILE}.new bs=8388608 conv=sync
+            mv -f ${NAND_FILE}.new ${NAND_FILE}
+
+            # 4. dump filesystem into firmware
+            dd if=${IMGDEPLOYDIR}/${PN}-${MACHINE}.${NAND_ROOTFS_TYPE} >> ${NAND_FILE}
+            mv ${NAND_FILE} ${DEPLOY_DIR_IMAGE}/         
+    fi
+
+}
+addtask filogic_gen_image after do_image_complete before do_populate_lic_deploy
\ No newline at end of file