Add test methods to cover all code paths of the nxp_imx93cst etype,
reusing the pattern of the nxp_imx8mcst as done in commit 0cab35362d77
("binman: test: Fix code coverage for iMX8 and cst bintool") by Simon
Glass.

This brings nxp_imx93cst to 100% coverage.

Signed-off-by: Jérémie Dautheribes (Schneider Electric) 
<[email protected]>
---
 tools/binman/ftest.py                      | 97 ++++++++++++++++++++++++++++++
 tools/binman/test/vendor/nxp_imx93_csf.dts | 18 ++++++
 2 files changed, 115 insertions(+)

diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py
index 783836045cb..822ad5c6f07 100644
--- a/tools/binman/ftest.py
+++ b/tools/binman/ftest.py
@@ -35,6 +35,7 @@ from dtoc import fdt
 from dtoc import fdt_util
 from binman.etype import fdtmap
 from binman.etype import image_header
+from binman.etype import nxp_imx93cst
 from binman.image import Image
 from u_boot_pylib import command
 from u_boot_pylib import terminal
@@ -8158,6 +8159,102 @@ fdt         fdtmap                Extract the 
devicetree blob from the fdtmap
             result = cst.fetch(bintool.FETCH_BUILD)
             self.assertEqual(('cst', None), result)
 
+    def _MakeImx93SplContainer(self, offset=0):
+        """Create a fake AHAB image containing an SPL container
+
+        The SPL container header tag is placed at the given offset, and the
+        returned buffer is large enough to also cover the signature block
+        referenced by that header. The image-entry flags hold the A55
+        core/type (0x23) as their low byte.
+
+        Args:
+            offset: Offset of the SPL container header. Use
+                nxp_imx93cst.SPL_CONTAINER_OFFSET to build an image which
+                starts with the ELE FW instead.
+
+        Returns:
+            bytes: Fake AHAB image data
+        """
+        data = bytearray(offset + 0x100)
+        data[offset + 3] = nxp_imx93cst.CONTAINER_HDR_TAG
+        data[offset + 12:offset + 14] = struct.pack('<H', 0x90)
+        data[offset + 40:offset + 44] = struct.pack('<I', 0x123)
+        return bytes(data)
+
+    def testNxpImx93cstNormal(self):
+        """Test CST signing with an i.MX93 SPL container (no ELE FW)"""
+        self._MakeInputFile('imx93-container.bin',
+                            self._MakeImx93SplContainer())
+
+        with terminal.capture() as (_, stderr):
+            self._DoTestFile('vendor/nxp_imx93_csf.dts',
+                             force_missing_bintools='cst')
+        err = stderr.getvalue()
+        self.assertRegex(err, "Image 'image'.*missing bintools.*: cst")
+
+        cfg = tools.read_file(tools.get_output_filename(
+            'nxp.csf-config-txt.nxp-imx93cst'), binary=False)
+        self.assertIn('Offsets = 0x0 0x90', cfg)
+
+    def testNxpImx93cstELE(self):
+        """Test CST signing where the SPL container starts with the ELE FW"""
+        # Create a fake AHAB image starting with the NXP-signed ELE
+        # container. The low byte of the first image entry flags (0x66) is
+        # the ELE core/type, so the SPL container header starts at
+        # SPL_CONTAINER_OFFSET and the signature block is covered too.
+        ele_data = bytearray(self._MakeImx93SplContainer(
+            nxp_imx93cst.SPL_CONTAINER_OFFSET))
+        ele_data[3] = nxp_imx93cst.CONTAINER_HDR_TAG
+        ele_data[40:44] = struct.pack('<I', 0x866)
+        self._MakeInputFile('imx93-container.bin', bytes(ele_data))
+
+        with terminal.capture() as (_, stderr):
+            self._DoTestFile('vendor/nxp_imx93_csf.dts',
+                             force_missing_bintools='cst')
+        err = stderr.getvalue()
+        self.assertRegex(err, "Image 'image'.*missing bintools.*: cst")
+
+        cfg = tools.read_file(tools.get_output_filename(
+            'nxp.csf-config-txt.nxp-imx93cst'), binary=False)
+        self.assertIn('Offsets = 0x400 0x490', cfg)
+
+    def testNxpImx93cstUnknownTag(self):
+        """Test CST with unknown input tag passes data through"""
+        # Trigger the pass-through path using an input without the AHAB
+        # container tag
+        data = b'\x00' * 64
+        self._MakeInputFile('imx93-container.bin', data)
+        self._DoTestFile('vendor/nxp_imx93_csf.dts',
+                         force_missing_bintools='cst')
+
+        # The pass-through branch returns the input data unchanged, so the
+        # produced image must be exactly the 64 zero bytes
+        out = tools.read_file(tools.get_output_filename('image.bin'))
+        self.assertEqual(out, b'\x00' * 64)
+
+    def testNxpImx93cstSigned(self):
+        """Test CST-signing-success path with mocked cst invocation"""
+        self._MakeInputFile('imx93-container.bin',
+                            self._MakeImx93SplContainer())
+
+        with self._MockCstRunCmd():
+            data = self._DoReadFile('vendor/nxp_imx93_csf.dts')
+
+        # The output must be the fake 32-byte CST blob produced by cst
+        self.assertEqual(data, b'\x00' * 32)
+
+    def testNxpImx93cstShortInput(self):
+        """Test BuildSectionData() passes a too-short input through"""
+        # The input is too short to hold the container header fields read by
+        # BuildSectionData(), so it must be passed through unchanged
+        data = b'\x00' * 32
+        self._MakeInputFile('imx93-container.bin', data)
+        self._DoTestFile('vendor/nxp_imx93_csf.dts',
+                         force_missing_bintools='cst')
+
+        out = tools.read_file(tools.get_output_filename('image.bin'))
+        self.assertEqual(out, data)
+
     def testNxpImx8MFSPI(self):
         """Test that binman can produce an iMX8m FSPI image"""
         self._DoTestFile('vendor/nxp_imx8m_fspi.dts')
diff --git a/tools/binman/test/vendor/nxp_imx93_csf.dts 
b/tools/binman/test/vendor/nxp_imx93_csf.dts
new file mode 100644
index 00000000000..bbe8eda4b70
--- /dev/null
+++ b/tools/binman/test/vendor/nxp_imx93_csf.dts
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+/dts-v1/;
+
+/ {
+       #address-cells = <1>;
+       #size-cells = <1>;
+
+       binman {
+               nxp-imx93cst {
+                       args;
+
+                       blob {
+                               filename = "imx93-container.bin";
+                       };
+               };
+       };
+};

-- 
2.55.0

Reply via email to