[Telink] Fix Factory Data build and handle it by CI (#25655)

* [Telink] Fix factory data build with correct certs

* [Telink] Handle building Lighting app with Factory Data feature by CI

* [Telink] Add checkout_submodules for gn_gen

* [Telink] fix commands

* [Telink] Add BUILD_TYPE for ninja

* [Telink] Fix typo

* [Telink] Set correct path for find_program
diff --git a/.github/workflows/examples-telink.yaml b/.github/workflows/examples-telink.yaml
index f26f0a3..df6c318 100644
--- a/.github/workflows/examples-telink.yaml
+++ b/.github/workflows/examples-telink.yaml
@@ -130,6 +130,21 @@
             - name: clean out build output
               run: rm -rf ./out
 
+            - name: Build example Telink Lighting App with Factory Data
+              run: |
+                  ./scripts/checkout_submodules.py --shallow --platform linux
+                  ./scripts/build/gn_gen.sh
+                  ./scripts/run_in_build_env.sh "ninja -C ./out/$BUILD_TYPE chip-cert chip-tool spake2p"
+                  ./scripts/run_in_build_env.sh \
+                    "./scripts/build/build_examples.py --target 'telink-tlsr9518adk80d-light-factory-data' build"
+                  .environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \
+                    telink tlsr9518adk80d lighting-app-factory-data \
+                    out/telink-tlsr9518adk80d-light-factory-data/zephyr/zephyr.elf \
+                    /tmp/bloat_reports/
+
+            - name: clean out build output
+              run: rm -rf ./out
+
             - name: Build example Telink Light Switch App
               run: |
                   ./scripts/run_in_build_env.sh \
diff --git a/config/telink/chip-module/generate_factory_data.cmake b/config/telink/chip-module/generate_factory_data.cmake
index 6b7681d..7798f10 100644
--- a/config/telink/chip-module/generate_factory_data.cmake
+++ b/config/telink/chip-module/generate_factory_data.cmake
@@ -69,21 +69,25 @@
 endif()
 
 # find chip cert tool
-find_program(chip_cert_exe NAMES chip-cert REQUIRED)
+find_program(chip_cert_exe NAMES chip-cert PATHS ${CHIP_ROOT}/out/telink REQUIRED)
 string(APPEND script_args "--chip-cert-path ${chip_cert_exe}\n")
 
 # for development purpose user can use default certs instead of generating or providing them
 if(CONFIG_CHIP_FACTORY_DATA_USE_DEFAULT_CERTS)
+    # convert decimal VID to its hexadecimal representation to find out certification files in repository
+    math(EXPR LOCAL_VID "${CONFIG_CHIP_DEVICE_VENDOR_ID}" OUTPUT_FORMAT HEXADECIMAL)
+    string(SUBSTRING ${LOCAL_VID} 2 -1 raw_vid)
+    string(TOUPPER ${raw_vid} raw_vid_upper)
     # convert decimal PID to its hexadecimal representation to find out certification files in repository
     math(EXPR LOCAL_PID "${CONFIG_CHIP_DEVICE_PRODUCT_ID}" OUTPUT_FORMAT HEXADECIMAL)
     string(SUBSTRING ${LOCAL_PID} 2 -1 raw_pid)
     string(TOUPPER ${raw_pid} raw_pid_upper)
     # all certs are located in ${CHIP_ROOT}/credentials/development/attestation
     # it can be used during development without need to generate new certifications
-    string(APPEND script_args "--dac-cert \"${CHIP_ROOT}/credentials/development/attestation/Matter-Development-DAC-${raw_pid_upper}-Cert.pem\"\n")
-    string(APPEND script_args "--dac-key \"${CHIP_ROOT}/credentials/development/attestation/Matter-Development-DAC-${raw_pid_upper}-Key.pem\"\n")
-    string(APPEND script_args "--cert \"${CHIP_ROOT}/credentials/development/attestation/Matter-Development-PAI-noPID-Cert.pem\"\n")
-    string(APPEND script_args "--key \"${CHIP_ROOT}/credentials/development/attestation/Matter-Development-PAI-noPID-Key.pem\"\n")
+    string(APPEND script_args "--dac-cert \"${CHIP_ROOT}/credentials/development/attestation/Matter-Development-DAC-${raw_vid_upper}-${raw_pid_upper}-Cert.pem\"\n")
+    string(APPEND script_args "--dac-key \"${CHIP_ROOT}/credentials/development/attestation/Matter-Development-DAC-${raw_vid_upper}-${raw_pid_upper}-Key.pem\"\n")
+    string(APPEND script_args "--cert \"${CHIP_ROOT}/credentials/development/attestation/Matter-Development-PAI-${raw_vid_upper}-noPID-Cert.pem\"\n")
+    string(APPEND script_args "--key \"${CHIP_ROOT}/credentials/development/attestation/Matter-Development-PAI-${raw_vid_upper}-noPID-Key.pem\"\n")
     string(APPEND script_args "-cd \"${CHIP_ROOT}/credentials/development/cd-certs/Chip-Test-CD-Cert.der\"\n")
     string(APPEND script_args "--pai\n")
 else()
@@ -95,7 +99,7 @@
 endif()
 
 # find chip tool requied for generating QRCode
-find_program(chip_tool_exe NAMES chip-tool REQUIRED)
+find_program(chip_tool_exe NAMES chip-tool PATHS ${CHIP_ROOT}/out/telink REQUIRED)
 string(APPEND script_args "--chip-tool-path ${chip_tool_exe}\n")
 
 # add Password-Authenticated Key Exchange parameters
@@ -104,7 +108,7 @@
 string(APPEND script_args "--passcode ${CONFIG_CHIP_DEVICE_SPAKE2_PASSCODE}\n")
 
 # request spake2p to generate a new spake2_verifier
-find_program(spake_exe NAMES spake2p REQUIRED)
+find_program(spake_exe NAMES spake2p PATHS ${CHIP_ROOT}/out/telink REQUIRED)
 string(APPEND script_args "--spake2-path ${spake_exe}\n")
 
 if(CONFIG_CHIP_DEVICE_ENABLE_KEY)
diff --git a/examples/lighting-app/telink/factory_data.overlay b/examples/lighting-app/telink/factory_data.overlay
new file mode 100644
index 0000000..4f7289d
--- /dev/null
+++ b/examples/lighting-app/telink/factory_data.overlay
@@ -0,0 +1,22 @@
+#
+#    Copyright (c) 2023 Project CHIP Authors
+#
+#    Licensed under the Apache License, Version 2.0 (the "License");
+#    you may not use this file except in compliance with the License.
+#    You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS,
+#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#    See the License for the specific language governing permissions and
+#    limitations under the License.
+#
+
+# This file should be used as a configuration overlay to enable Factory Data.
+
+# Enable factory data support. 
+CONFIG_CHIP_FACTORY_DATA=y
+CONFIG_CHIP_FACTORY_DATA_BUILD=y
+CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=y
diff --git a/scripts/build/build/targets.py b/scripts/build/build/targets.py
index fa19d84..fefec0a 100755
--- a/scripts/build/build/targets.py
+++ b/scripts/build/build/targets.py
@@ -558,6 +558,7 @@
     ])
 
     target.AppendModifier('rpc', enable_rpcs=True)
+    target.AppendModifier('factory-data', enable_factory_data=True)
 
     return target
 
diff --git a/scripts/build/builders/telink.py b/scripts/build/builders/telink.py
index afaff3c..9dc5800 100644
--- a/scripts/build/builders/telink.py
+++ b/scripts/build/builders/telink.py
@@ -108,11 +108,13 @@
                  runner,
                  app: TelinkApp = TelinkApp,
                  board: TelinkBoard = TelinkBoard.TLSR9518ADK80D,
-                 enable_rpcs: bool = False):
+                 enable_rpcs: bool = False,
+                 enable_factory_data: bool = False):
         super(TelinkBuilder, self).__init__(root, runner)
         self.app = app
         self.board = board
         self.enable_rpcs = enable_rpcs
+        self.enable_factory_data = enable_factory_data
 
     def get_cmd_prefixes(self):
         if not self._runner.dry_run:
@@ -136,6 +138,9 @@
         if self.enable_rpcs:
             flags.append("-DOVERLAY_CONFIG=rpc.overlay")
 
+        if self.enable_factory_data:
+            flags.append("-DOVERLAY_CONFIG=factory_data.overlay")
+
         if self.options.pregen_dir:
             flags.append(f"-DCHIP_CODEGEN_PREGEN_DIR={shlex.quote(self.options.pregen_dir)}")
 
diff --git a/scripts/build/testdata/all_targets_linux_x64.txt b/scripts/build/testdata/all_targets_linux_x64.txt
index 0137650..61ded36 100644
--- a/scripts/build/testdata/all_targets_linux_x64.txt
+++ b/scripts/build/testdata/all_targets_linux_x64.txt
@@ -19,5 +19,5 @@
 nrf-native-posix-64-tests
 qpg-qpg6105-{lock,light,shell,persistent-storage}
 tizen-arm-{all-clusters,all-clusters-minimal,chip-tool,light,tests}[-no-ble][-no-wifi][-asan][-ubsan]
-telink-tlsr9518adk80d-{all-clusters,all-clusters-minimal,contact-sensor,light,light-switch,lock,ota-requestor,pump,pump-controller,temperature-measurement,thermostat,window-covering}[-rpc]
+telink-tlsr9518adk80d-{all-clusters,all-clusters-minimal,contact-sensor,light,light-switch,lock,ota-requestor,pump,pump-controller,temperature-measurement,thermostat,window-covering}[-rpc][-factory-data]
 openiotsdk-{shell,lock}