[Bouffalolab] Update SDK repo (#23745)

* Update sdk repo

* Update build scripts and readme

* change submodule fetch

* update submodule path

* update submodule url

* update sdk repo

* Fix restyle

* update comments

* Do not try on default SDK path
diff --git a/.github/workflows/examples-bouffalolab.yaml b/.github/workflows/examples-bouffalolab.yaml
index 11539d5..8e137c4 100644
--- a/.github/workflows/examples-bouffalolab.yaml
+++ b/.github/workflows/examples-bouffalolab.yaml
@@ -31,7 +31,7 @@
     if: github.actor != 'restyled-io[bot]'
 
     container:
-      image: connectedhomeip/chip-build:0.6.06
+      image: connectedhomeip/chip-build-bouffalolab:0.6.12
       volumes:
         - "/tmp/bloat_reports:/tmp/bloat_reports"
     steps:
@@ -44,7 +44,7 @@
           attempt_limit: 3
           attempt_delay: 2000
       - name: Checkout submodules
-        run: scripts/checkout_submodules.py --shallow --platform bl602
+        run: scripts/checkout_submodules.py --shallow --platform bouffalolab --recursive
 
       - name: Set up environment for size reports
         if: ${{ !env.ACT }}
@@ -87,7 +87,7 @@
     if: github.actor != 'restyled-io[bot]'
 
     container:
-      image: connectedhomeip/chip-build:0.6.06
+      image: connectedhomeip/chip-build-bouffalolab:0.6.12
       volumes:
         - "/tmp/bloat_reports:/tmp/bloat_reports"
     steps:
@@ -100,7 +100,7 @@
           attempt_limit: 3
           attempt_delay: 2000
       - name: Checkout submodules
-        run: scripts/checkout_submodules.py --shallow --platform bouffalolab
+        run: scripts/checkout_submodules.py --shallow --platform bouffalolab --recursive
 
       - name: Set up environment for size reports
         if: ${{ !env.ACT }}
diff --git a/.gitmodules b/.gitmodules
index a68fbf7..1d0e33d 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -58,7 +58,7 @@
 	path = third_party/freertos/repo
 	url = https://github.com/FreeRTOS/FreeRTOS-Kernel.git
 	branch  = V10.3.1-kernel-only
-	platforms = ameba,cc13x2_26x2,bl602,efr32,esp32,k32w0,infineon,qpg
+	platforms = ameba,cc13x2_26x2,bouffalolab,efr32,esp32,k32w0,infineon,qpg
 [submodule "simw-top-mini"]
 	path = third_party/simw-top-mini/repo
 	url = https://github.com/NXP/plug-and-trust.git
@@ -264,11 +264,6 @@
 [submodule "third_party/boringssl/repo/src"]
 	path = third_party/boringssl/repo/src
 	url = https://github.com/google/boringssl.git
-[submodule "boufalolab_repo"]
-	path = third_party/bouffalolab/repo
-	url = https://github.com/bouffalolab/bl_iot_sdk.git
-	branch = master
-	platforms = bouffalolab,bl602
 [submodule "third_party/mt793x_sdk/filogic"]
 	path = third_party/mt793x_sdk/filogic
 	url = https://github.com/MediaTek-Labs/genio-matter-bsp.git
@@ -293,4 +288,9 @@
 	path = third_party/open-iot-sdk/storage
 	url = https://git.gitlab.arm.com/iot/open-iot-sdk/storage.git
 	branch = main
-    platforms = openiotsdk
+	platforms = openiotsdk
+[submodule "bouffalolab_sdk"]
+	path = third_party/bouffalolab/repo
+	url = https://github.com/bouffalolab/bl_iot_sdk_tiny.git
+        branch = main
+        platforms = bouffalolab
diff --git a/build_overrides/bouffalolab_iot_sdk.gni b/build_overrides/bouffalolab_iot_sdk.gni
index 165bf59..77ab344 100644
--- a/build_overrides/bouffalolab_iot_sdk.gni
+++ b/build_overrides/bouffalolab_iot_sdk.gni
@@ -19,6 +19,5 @@
   # Root directory for bl702 SDK build files.
   bl702_sdk_build_root = "//third_party/bouffalolab/bl702"
 
-  # Root directory of toolchain of Bouffalolab chips
-  bouffalolab_toolchain = "//third_party/bouffalolab/repo/toolchain/riscv"
+  bouffalolab_sdk_root = "/opt/bouffalolab_sdk"
 }
diff --git a/examples/build_overrides/bouffalolab_iot_sdk.gni b/examples/build_overrides/bouffalolab_iot_sdk.gni
index c11bd8f..1eb018a 100644
--- a/examples/build_overrides/bouffalolab_iot_sdk.gni
+++ b/examples/build_overrides/bouffalolab_iot_sdk.gni
@@ -21,6 +21,5 @@
   bl702_sdk_build_root =
       "//third_party/connectedhomeip/third_party/bouffalolab/bl702"
 
-  # Root directory of toolchain of Bouffalolab chips
-  bouffalolab_toolchain = "/third_party/bouffalolab/repo/toolchain/riscv"
+  bouffalolab_sdk_root = "/opt/bouffalolab_sdk"
 }
diff --git a/examples/lighting-app/bouffalolab/README.md b/examples/lighting-app/bouffalolab/README.md
index efa22fe..a68fad7 100644
--- a/examples/lighting-app/bouffalolab/README.md
+++ b/examples/lighting-app/bouffalolab/README.md
@@ -53,6 +53,22 @@
     > under this environment. If not, please try `scripts/bootstrap.sh` for
     > matter environment update.
 
+-   Setup build environment for `Bouffalo Lab` SoC
+
+    Run `setup.sh` to install `Bouffalo Lab` SDK to /opt/bouffalolab_sdk
+
+    ```
+    cd third_party/bouffalolab/repo
+    sudo bash script/setup.sh
+    ```
+
+    Please execute following command export `BOUFFALOLAB_SDK_ROOT` before
+    building.
+
+    ```
+    export BOUFFALOLAB_SDK_ROOT=/opt/bouffalolab_sdk
+    ```
+
 ## Build CHIP Lighting App example
 
 The following steps take examples for BL602 develop board `BL602-IoT-Matter-V1`
diff --git a/examples/lighting-app/bouffalolab/bl602/BUILD.gn b/examples/lighting-app/bouffalolab/bl602/BUILD.gn
index b9511c8..369b732 100644
--- a/examples/lighting-app/bouffalolab/bl602/BUILD.gn
+++ b/examples/lighting-app/bouffalolab/bl602/BUILD.gn
@@ -38,17 +38,16 @@
   # Dump memory usage at link time.
   chip_print_memory_usage = true
 
-  # PIN code for PASE session establishment.
-  setupPinCode = 20202021
-
-  # Monitor & log memory usage at runtime.
-  enable_heap_monitoring = false
-
   # OTA periodic query timeout in seconds
   ota_periodic_query_timeout = 86400
 
-  board = "BL602-IoT-Matter-V1"
+  enable_heap_monitoring = false
 
+  setupPinCode = 20202021
+  setupDiscriminator = 3840
+
+  board = "BL602-IoT-Matter-V1"
+  module_type = "BL602"
   baudrate = 2000000
 }
 
@@ -196,7 +195,6 @@
     sources += [ "${examples_plat_common_dir}/plat/OTAConfig.cpp" ]
   }
 
-  print(defines)
   ldscript = "${examples_plat_dir}/ldscripts/flash_rom.ld"
 
   ldflags = [ "-T" + rebase_path(ldscript, root_build_dir) ]
diff --git a/examples/lighting-app/bouffalolab/bl702/BUILD.gn b/examples/lighting-app/bouffalolab/bl702/BUILD.gn
index 2863fae..5872505 100644
--- a/examples/lighting-app/bouffalolab/bl702/BUILD.gn
+++ b/examples/lighting-app/bouffalolab/bl702/BUILD.gn
@@ -38,21 +38,20 @@
   # Dump memory usage at link time.
   chip_print_memory_usage = true
 
-  config_cache_size = 8192
-
-  # Monitor & log memory usage at runtime.
-  enable_heap_monitoring = false
-
   # OTA periodic query timeout in seconds
   ota_periodic_query_timeout = 86400
 
+  config_cache_size = 8192
+
+  enable_heap_monitoring = false
+
+  setupPinCode = 20202021
+  setupDiscriminator = 3840
+
   board = "XT-ZB6-DevKit"
   module_type = "BL706C-22"
   enable_psram = true
   baudrate = 2000000
-
-  setupPinCode = 20202021
-  setupDiscriminator = 3840
 }
 
 bl702_sdk("sdk") {
diff --git a/examples/platform/bouffalolab/common/toolchain/riscv_toolchain.gni b/examples/platform/bouffalolab/common/toolchain/riscv_toolchain.gni
index d382cb3..db46fc1 100644
--- a/examples/platform/bouffalolab/common/toolchain/riscv_toolchain.gni
+++ b/examples/platform/bouffalolab/common/toolchain/riscv_toolchain.gni
@@ -23,9 +23,9 @@
     _tool_name_root = ""
 
     if ("linux" == host_os) {
-      _tool_name_root = "${root_build_dir}/../../${bouffalolab_toolchain}/Linux/bin/riscv64-unknown-elf-"
+      _tool_name_root = "${bouffalolab_sdk_root}/toolchain/riscv/Linux/bin/riscv64-unknown-elf-"
     } else if ("mac" == host_os || "darwin" == host_os) {
-      _tool_name_root = "${root_build_dir}/../../${bouffalolab_toolchain}/Darwin/bin/riscv64-unknown-elf-"
+      _tool_name_root = "${bouffalolab_sdk_root}/toolchain/riscv/Darwin/bin/riscv64-unknown-elf-"
     }
 
     ar = _tool_name_root + "ar"
diff --git a/scripts/build/builders/bouffalolab.py b/scripts/build/builders/bouffalolab.py
index 00a9315..9c9c8e7 100644
--- a/scripts/build/builders/bouffalolab.py
+++ b/scripts/build/builders/bouffalolab.py
@@ -107,6 +107,8 @@
         elif enable_shell:
             self.argsOpt.append('chip_build_libshell=true')
 
+        self.argsOpt.append('bouffalolab_sdk_root="%s"' % os.environ['BOUFFALOLAB_SDK_ROOT'])
+
     def GnBuildArgs(self):
         return self.argsOpt
 
diff --git a/scripts/examples/gn_bouffalolab_example.sh b/scripts/examples/gn_bouffalolab_example.sh
index a719563..4918270 100755
--- a/scripts/examples/gn_bouffalolab_example.sh
+++ b/scripts/examples/gn_bouffalolab_example.sh
@@ -141,6 +141,8 @@
         exit 1
     fi
 
+    optArgs=$optArgs' bouffalolab_sdk_root="'$BOUFFALOLAB_SDK_ROOT'"'
+
     example_dir=$MATTER_ROOT/examples/$example_name/bouffalolab/$bouffalo_chip
     output_dir=$MATTER_ROOT/$output_folder
 
diff --git a/third_party/bouffalolab/bl602/bl602_executable.gni b/third_party/bouffalolab/bl602/bl602_executable.gni
index b2e6ae8..5c327b0 100644
--- a/third_party/bouffalolab/bl602/bl602_executable.gni
+++ b/third_party/bouffalolab/bl602/bl602_executable.gni
@@ -26,11 +26,9 @@
 
   objcopy = "riscv64-unknown-elf-objcopy"
   if ("linux" == host_os) {
-    objcopy =
-        "../../${bouffalolab_toolchain}/Linux/bin/riscv64-unknown-elf-objcopy"
+    objcopy = "${bouffalolab_sdk_root}/toolchain/riscv/Linux/bin/riscv64-unknown-elf-objcopy"
   } else if ("mac" == host_os || "darwin" == host_os) {
-    objcopy =
-        "../../${bouffalolab_toolchain}/Darwin/bin/riscv64-unknown-elf-objcopy"
+    objcopy = "${bouffalolab_sdk_root}/toolchain/riscv/Darwin/bin/riscv64-unknown-elf-objcopy"
   }
 
   # Copy flashing dependencies to the output directory so that the output
diff --git a/third_party/bouffalolab/bl702/bl702_executable.gni b/third_party/bouffalolab/bl702/bl702_executable.gni
index 8d72c59..e50120b 100644
--- a/third_party/bouffalolab/bl702/bl702_executable.gni
+++ b/third_party/bouffalolab/bl702/bl702_executable.gni
@@ -26,11 +26,9 @@
 
   objcopy = "riscv64-unknown-elf-objcopy"
   if ("linux" == host_os) {
-    objcopy =
-        "../../${bouffalolab_toolchain}/Linux/bin/riscv64-unknown-elf-objcopy"
+    objcopy = "${bouffalolab_sdk_root}/toolchain/riscv/Linux/bin/riscv64-unknown-elf-objcopy"
   } else if ("mac" == host_os || "darwin" == host_os) {
-    objcopy =
-        "../../${bouffalolab_toolchain}/Darwin/bin/riscv64-unknown-elf-objcopy"
+    objcopy = "${bouffalolab_sdk_root}/toolchain/riscv/Darwin/bin/riscv64-unknown-elf-objcopy"
   }
 
   # Copy flashing dependencies to the output directory so that the output
diff --git a/third_party/bouffalolab/repo b/third_party/bouffalolab/repo
index dc52631..2d6f06e 160000
--- a/third_party/bouffalolab/repo
+++ b/third_party/bouffalolab/repo
@@ -1 +1 @@
-Subproject commit dc526317d2903c4d3a8466226983ce34dc8f23d1
+Subproject commit 2d6f06e8f05dca5c24afa342c9c1b867a3ad400e