[Add kernel_in_ubi feature to separate image format]

[Description]
Add kernel_in_ubi feature to separate image format
1. kernel_in_ubi will decide partition layout and enable overlayfs
on/off

[Release-log]

diff --git a/classes/image_types-mtk.bbclass b/classes/image_types-mtk.bbclass
index 7d9fae8..535441d 100644
--- a/classes/image_types-mtk.bbclass
+++ b/classes/image_types-mtk.bbclass
@@ -131,8 +131,8 @@
     assemble_sd_image
 }
 
-NAND_ROOTFS_TYPE ?= "squashfs-xz"
-#NAND_ROOTFS_TYPE ?= "ubi"
+#NAND_ROOTFS_TYPE ?= "squashfs-xz"
+NAND_ROOTFS_TYPE ?= "ubi"
 NAND_FILE ?= "${IMGDEPLOYDIR}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.mtk.nand"
 
 IMAGE_CMD_mtk.nand () {
diff --git a/conf/machine/filogic-32bit.conf b/conf/machine/filogic-32bit.conf
index 812680b..a6f42d5 100644
--- a/conf/machine/filogic-32bit.conf
+++ b/conf/machine/filogic-32bit.conf
@@ -36,7 +36,7 @@
 
 PREFERRED_PROVIDER_hal-wifi = "hal-wifi-cfg80211"
 
-IMAGE_FSTYPES += "ubi squashfs-xz mtk.nand ${INITRAMFS_FSTYPES}"
+IMAGE_FSTYPES += "ubi squashfs-xz ${INITRAMFS_FSTYPES}"
 # Parameters for MT7986 RFB1 NAND (byte)
 # NAND_LOG_BLOCK_SIZE = NAND_PHY_BLOCK_SIZE - 2 Pages
 # NAND_LBLOCK_COUNT = NAND_PHY_BLOCK_COUNT - 53
diff --git a/conf/machine/filogic.conf b/conf/machine/filogic.conf
index d95bf2d..0822fd2 100644
--- a/conf/machine/filogic.conf
+++ b/conf/machine/filogic.conf
@@ -36,7 +36,7 @@
 
 PREFERRED_PROVIDER_hal-wifi = "hal-wifi-cfg80211"
 
-IMAGE_FSTYPES += "ubi squashfs-xz mtk.nand ${INITRAMFS_FSTYPES}"
+IMAGE_FSTYPES += "ubi squashfs-xz ${INITRAMFS_FSTYPES}"
 # Parameters for MT7986 RFB1 NAND (byte)
 # NAND_LOG_BLOCK_SIZE = NAND_PHY_BLOCK_SIZE - 2 Pages
 # NAND_LBLOCK_COUNT = NAND_PHY_BLOCK_COUNT - 53
diff --git a/recipes-devtools/initrd/systemd_%.bbappend b/recipes-devtools/initrd/systemd_%.bbappend
index 42a8ac6..607d3d9 100644
--- a/recipes-devtools/initrd/systemd_%.bbappend
+++ b/recipes-devtools/initrd/systemd_%.bbappend
@@ -5,11 +5,13 @@
                /rom \"
 
 do_install_append() {
+       if ${@bb.utils.contains('DISTRO_FEATURES','kernel_in_ubi','true','false',d)}; then
        install -d ${D}/overlay
        install -d ${D}/rom
        install -m 0755 ${WORKDIR}/init_readonlyfs.sh ${D}${rootlibexecdir}/init_readonlyfs.sh
        [ -e ${D}/sbin/init ] && rm -rf ${D}/sbin/init
        ln -s ${rootlibexecdir}/init_readonlyfs.sh ${D}/sbin/init
+       fi
 }
 
 FILES_${PN} += " ${rootlibexecdir}/init_readonlyfs.sh "
diff --git a/recipes-kernel/linux/linux-mediatek-5.4/003-rdkb-mtd-kernel-ubi-relayout.patch b/recipes-kernel/linux/linux-mediatek-5.4/003-rdkb-mtd-kernel-ubi-relayout.patch
new file mode 100644
index 0000000..de552c2
--- /dev/null
+++ b/recipes-kernel/linux/linux-mediatek-5.4/003-rdkb-mtd-kernel-ubi-relayout.patch
@@ -0,0 +1,26 @@
+diff --git a/arch/arm/boot/dts/mt7986-spim-nand-partition.dtsi b/arch/arm/boot/dts/mt7986-spim-nand-partition.dtsi
+index 4cc7961..924f8cc 100644
+--- a/arch/arm/boot/dts/mt7986-spim-nand-partition.dtsi
++++ b/arch/arm/boot/dts/mt7986-spim-nand-partition.dtsi
+@@ -36,7 +36,7 @@
+ 
+ 			partition@580000 {
+ 				label = "ubi";
+-				reg = <0x580000 0x4000000>;
++				reg = <0x580000 0xFA80000>;
+ 			};
+ 		};
+ 	};
+diff --git a/arch/arm64/boot/dts/mediatek/mt7986-spim-nand-partition.dtsi b/arch/arm64/boot/dts/mediatek/mt7986-spim-nand-partition.dtsi
+index 4cc7961..924f8cc 100644
+--- a/arch/arm64/boot/dts/mediatek/mt7986-spim-nand-partition.dtsi
++++ b/arch/arm64/boot/dts/mediatek/mt7986-spim-nand-partition.dtsi
+@@ -36,7 +36,7 @@
+ 
+ 			partition@580000 {
+ 				label = "ubi";
+-				reg = <0x580000 0x4000000>;
++				reg = <0x580000 0xFA80000>;
+ 			};
+ 		};
+ 	};
diff --git a/recipes-kernel/linux/linux-mediatek_5.4.bb b/recipes-kernel/linux/linux-mediatek_5.4.bb
index ff50b19..f9f7ad6 100644
--- a/recipes-kernel/linux/linux-mediatek_5.4.bb
+++ b/recipes-kernel/linux/linux-mediatek_5.4.bb
@@ -21,6 +21,7 @@
     file://generic/defconfig \
     file://001-rdkb-eth-mtk-change-ifname-for.patch;apply=no \
     file://002-rdkb-mtd-ubi-relayout.patch;apply=no \
+    file://003-rdkb-mtd-kernel-ubi-relayout.patch;apply=no \
     "
 
 SRC_URI_append_mt7986 += " \
@@ -69,7 +70,11 @@
     cd ${S}
         if [ ! -e patch_applied ]; then
             patch -p1 < ${WORKDIR}/001-rdkb-eth-mtk-change-ifname-for.patch
-            
+            if ${@bb.utils.contains('DISTRO_FEATURES','kernel_in_ubi','true','false',d)}; then
+            patch -p1 < ${WORKDIR}/003-rdkb-mtd-kernel-ubi-relayout.patch
+            else
+            patch -p1 < ${WORKDIR}/002-rdkb-mtd-ubi-relayout.patch
+            fi
             patch -p1 < ${WORKDIR}/0600-net-phylink-propagate-resolved-link-config-via-mac_l.patch
             patch -p1 < ${WORKDIR}/9010-iwconfig-wireless-rate-fix.patch
             touch patch_applied