From patchwork Sat Sep 29 19:23:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 976686 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=chromium.org Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42MzDx4r4Lz9s3l for ; Sun, 30 Sep 2018 05:31:29 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 49DC4C21F21; Sat, 29 Sep 2018 19:30:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=RCVD_IN_MSPIKE_H2 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 69A84C21F81; Sat, 29 Sep 2018 19:26:42 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 064EDC21F69; Sat, 29 Sep 2018 19:25:48 +0000 (UTC) Received: from mail-vk1-f201.google.com (mail-vk1-f201.google.com [209.85.221.201]) by lists.denx.de (Postfix) with ESMTPS id 51A5AC21EE3 for ; Sat, 29 Sep 2018 19:25:43 +0000 (UTC) Received: by mail-vk1-f201.google.com with SMTP id v129-v6so2345172vke.16 for ; Sat, 29 Sep 2018 12:25:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=bnQVOG4oyKbScsiJ4p5qiRL54XUqXOOu851dXiSxStw=; b=nFqruaShXfKMnANG89oX6fOQ+T3k/3+LgNt2NfAoKEl0BQqGXbu01PgJCqXiaeVs/J 6gbvOJjAATBT50jgFQlvRVsiDOBX91C94uuEhK+Pa94N11SnjnWXglwbHEoLdh6HURp6 gWEwEYoPm7U5gdDqUBXabkPd65luTsZR3RZUL4M75aGggcQCXj34y+bebOn9aEHy8k4e P35gEFy5RxYNkQy6nEks7Mwv9egN1wkWAXPmpEwZZQnsgaztHtY7BgiMx1GeBprFsyZJ 5a18TxWgj8VQeegRhIYsNWXcS9ukUA1pryZHXDd9befhwCjdTr8j23MFILq5bIrKmWbv A48w== X-Gm-Message-State: ABuFfoitGi12vfjT8lJ5aQH0kDDSH01zgCZLhBXGxAafwyOASdHlm9Ve S4xVpSrCaOYzmhR1qzpiTsgr4hY= X-Google-Smtp-Source: ACcGV63O59bQRe9mtRZJO5LibQMoX8U1iqNUd3eEmRCbED+3zsSlIxnLSB6XqARCMzvw3nn/yiVimIA= X-Received: by 2002:a67:2cd6:: with SMTP id s205-v6mr4209422vss.16.1538249142453; Sat, 29 Sep 2018 12:25:42 -0700 (PDT) Date: Sat, 29 Sep 2018 13:23:41 -0600 In-Reply-To: <20180929192353.38640-1-sjg@chromium.org> Message-Id: <20180929192353.38640-7-sjg@chromium.org> Mime-Version: 1.0 References: <20180929192353.38640-1-sjg@chromium.org> X-Mailer: git-send-email 2.19.0.605.g01d371f741-goog From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Stephen Warren , Alexander Graf Subject: [U-Boot] [PATCH v2 06/18] binman: Reorder tests to put helper functions first X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" At present some helper functions are mixed in with the tests. Tidy this up by moving them to the top. For the few helpers that don't need to be full class members, make them nested functions. Signed-off-by: Simon Glass --- Changes in v2: None tools/binman/ftest.py | 249 +++++++++++++++++++++--------------------- 1 file changed, 124 insertions(+), 125 deletions(-) diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py index abf02b62e81..b90928e738a 100644 --- a/tools/binman/ftest.py +++ b/tools/binman/ftest.py @@ -421,6 +421,111 @@ class TestFunctional(unittest.TestCase): AddNode(dtb.GetRoot(), '') return tree + def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False): + """Handle running a test for insertion of microcode + + Args: + dts_fname: Name of test .dts file + nodtb_data: Data that we expect in the first section + ucode_second: True if the microsecond entry is second instead of + third + + Returns: + Tuple: + Contents of first region (U-Boot or SPL) + Offset and size components of microcode pointer, as inserted + in the above (two 4-byte words) + """ + data = self._DoReadFile(dts_fname, True) + + # Now check the device tree has no microcode + if ucode_second: + ucode_content = data[len(nodtb_data):] + ucode_pos = len(nodtb_data) + dtb_with_ucode = ucode_content[16:] + fdt_len = self.GetFdtLen(dtb_with_ucode) + else: + dtb_with_ucode = data[len(nodtb_data):] + fdt_len = self.GetFdtLen(dtb_with_ucode) + ucode_content = dtb_with_ucode[fdt_len:] + ucode_pos = len(nodtb_data) + fdt_len + fname = tools.GetOutputFilename('test.dtb') + with open(fname, 'wb') as fd: + fd.write(dtb_with_ucode) + dtb = fdt.FdtScan(fname) + ucode = dtb.GetNode('/microcode') + self.assertTrue(ucode) + for node in ucode.subnodes: + self.assertFalse(node.props.get('data')) + + # Check that the microcode appears immediately after the Fdt + # This matches the concatenation of the data properties in + # the /microcode/update@xxx nodes in 34_x86_ucode.dts. + ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000, + 0x78235609) + self.assertEqual(ucode_data, ucode_content[:len(ucode_data)]) + + # Check that the microcode pointer was inserted. It should match the + # expected offset and size + pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, + len(ucode_data)) + u_boot = data[:len(nodtb_data)] + return u_boot, pos_and_size + + def _RunPackUbootSingleMicrocode(self): + """Test that x86 microcode can be handled correctly + + We expect to see the following in the image, in order: + u-boot-nodtb.bin with a microcode pointer inserted at the correct + place + u-boot.dtb with the microcode + an empty microcode region + """ + # We need the libfdt library to run this test since only that allows + # finding the offset of a property. This is required by + # Entry_u_boot_dtb_with_ucode.ObtainContents(). + data = self._DoReadFile('35_x86_single_ucode.dts', True) + + second = data[len(U_BOOT_NODTB_DATA):] + + fdt_len = self.GetFdtLen(second) + third = second[fdt_len:] + second = second[:fdt_len] + + ucode_data = struct.pack('>2L', 0x12345678, 0x12345679) + self.assertIn(ucode_data, second) + ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA) + + # Check that the microcode pointer was inserted. It should match the + # expected offset and size + pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, + len(ucode_data)) + first = data[:len(U_BOOT_NODTB_DATA)] + self.assertEqual('nodtb with microcode' + pos_and_size + + ' somewhere in here', first) + + def _PackUbootSplMicrocode(self, dts, ucode_second=False): + """Helper function for microcode tests + + We expect to see the following in the image, in order: + u-boot-spl-nodtb.bin with a microcode pointer inserted at the + correct place + u-boot.dtb with the microcode removed + the microcode + + Args: + dts: Device tree file to use for test + ucode_second: True if the microsecond entry is second instead of + third + """ + # ELF file with a '_dt_ucode_base_size' symbol + with open(self.TestFile('u_boot_ucode_ptr')) as fd: + TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read()) + first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA, + ucode_second=ucode_second) + self.assertEqual('splnodtb with microc' + pos_and_size + + 'ter somewhere in here', first) + def testRun(self): """Test a basic run with valid args""" result = self._RunBinman('-h') @@ -808,57 +913,6 @@ class TestFunctional(unittest.TestCase): data = self._DoReadFile('33_x86-start16.dts') self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)]) - def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False): - """Handle running a test for insertion of microcode - - Args: - dts_fname: Name of test .dts file - nodtb_data: Data that we expect in the first section - ucode_second: True if the microsecond entry is second instead of - third - - Returns: - Tuple: - Contents of first region (U-Boot or SPL) - Offset and size components of microcode pointer, as inserted - in the above (two 4-byte words) - """ - data = self._DoReadFile(dts_fname, True) - - # Now check the device tree has no microcode - if ucode_second: - ucode_content = data[len(nodtb_data):] - ucode_pos = len(nodtb_data) - dtb_with_ucode = ucode_content[16:] - fdt_len = self.GetFdtLen(dtb_with_ucode) - else: - dtb_with_ucode = data[len(nodtb_data):] - fdt_len = self.GetFdtLen(dtb_with_ucode) - ucode_content = dtb_with_ucode[fdt_len:] - ucode_pos = len(nodtb_data) + fdt_len - fname = tools.GetOutputFilename('test.dtb') - with open(fname, 'wb') as fd: - fd.write(dtb_with_ucode) - dtb = fdt.FdtScan(fname) - ucode = dtb.GetNode('/microcode') - self.assertTrue(ucode) - for node in ucode.subnodes: - self.assertFalse(node.props.get('data')) - - # Check that the microcode appears immediately after the Fdt - # This matches the concatenation of the data properties in - # the /microcode/update@xxx nodes in 34_x86_ucode.dts. - ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000, - 0x78235609) - self.assertEqual(ucode_data, ucode_content[:len(ucode_data)]) - - # Check that the microcode pointer was inserted. It should match the - # expected offset and size - pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, - len(ucode_data)) - u_boot = data[:len(nodtb_data)] - return u_boot, pos_and_size - def testPackUbootMicrocode(self): """Test that x86 microcode can be handled correctly @@ -873,38 +927,6 @@ class TestFunctional(unittest.TestCase): self.assertEqual('nodtb with microcode' + pos_and_size + ' somewhere in here', first) - def _RunPackUbootSingleMicrocode(self): - """Test that x86 microcode can be handled correctly - - We expect to see the following in the image, in order: - u-boot-nodtb.bin with a microcode pointer inserted at the correct - place - u-boot.dtb with the microcode - an empty microcode region - """ - # We need the libfdt library to run this test since only that allows - # finding the offset of a property. This is required by - # Entry_u_boot_dtb_with_ucode.ObtainContents(). - data = self._DoReadFile('35_x86_single_ucode.dts', True) - - second = data[len(U_BOOT_NODTB_DATA):] - - fdt_len = self.GetFdtLen(second) - third = second[fdt_len:] - second = second[:fdt_len] - - ucode_data = struct.pack('>2L', 0x12345678, 0x12345679) - self.assertIn(ucode_data, second) - ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA) - - # Check that the microcode pointer was inserted. It should match the - # expected offset and size - pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, - len(ucode_data)) - first = data[:len(U_BOOT_NODTB_DATA)] - self.assertEqual('nodtb with microcode' + pos_and_size + - ' somewhere in here', first) - def testPackUbootSingleMicrocode(self): """Test that x86 microcode can be handled correctly with fdt_normal. """ @@ -1020,28 +1042,6 @@ class TestFunctional(unittest.TestCase): data = self._DoReadFile('48_x86-start16-spl.dts') self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)]) - def _PackUbootSplMicrocode(self, dts, ucode_second=False): - """Helper function for microcode tests - - We expect to see the following in the image, in order: - u-boot-spl-nodtb.bin with a microcode pointer inserted at the - correct place - u-boot.dtb with the microcode removed - the microcode - - Args: - dts: Device tree file to use for test - ucode_second: True if the microsecond entry is second instead of - third - """ - # ELF file with a '_dt_ucode_base_size' symbol - with open(self.TestFile('u_boot_ucode_ptr')) as fd: - TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read()) - first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA, - ucode_second=ucode_second) - self.assertEqual('splnodtb with microc' + pos_and_size + - 'ter somewhere in here', first) - def testPackUbootSplMicrocode(self): """Test that x86 microcode can be handled correctly in SPL""" self._PackUbootSplMicrocode('49_x86_ucode_spl.dts') @@ -1324,19 +1324,18 @@ class TestFunctional(unittest.TestCase): self.assertIn("'fill' entry must have a size property", str(e.exception)) - def _HandleGbbCommand(self, pipe_list): - """Fake calls to the futility utility""" - if pipe_list[0][0] == 'futility': - fname = pipe_list[0][-1] - # Append our GBB data to the file, which will happen every time the - # futility command is called. - with open(fname, 'a') as fd: - fd.write(GBB_DATA) - return command.CommandResult() - def testGbb(self): """Test for the Chromium OS Google Binary Block""" - command.test_result = self._HandleGbbCommand + def _HandleGbbCommand(pipe_list): + """Fake calls to the futility utility""" + if pipe_list[0][0] == 'futility': + fname = pipe_list[0][-1] + # Append our GBB data to the file, which will happen every time + # the futility command is called. + with open(fname, 'a') as fd: + fd.write(GBB_DATA) + return command.CommandResult() + command.test_result = _HandleGbbCommand entry_args = { 'keydir': 'devkeys', 'bmpblk': 'bmpblk.bin', @@ -1361,17 +1360,17 @@ class TestFunctional(unittest.TestCase): self.assertIn("Node '/binman/gbb': GBB must have a fixed size", str(e.exception)) - def _HandleVblockCommand(self, pipe_list): - """Fake calls to the futility utility""" - if pipe_list[0][0] == 'futility': - fname = pipe_list[0][3] - with open(fname, 'wb') as fd: - fd.write(VBLOCK_DATA) - return command.CommandResult() - def testVblock(self): """Test for the Chromium OS Verified Boot Block""" - command.test_result = self._HandleVblockCommand + def _HandleVblockCommand(pipe_list): + """Fake calls to the futility utility""" + if pipe_list[0][0] == 'futility': + fname = pipe_list[0][3] + with open(fname, 'wb') as fd: + fd.write(VBLOCK_DATA) + return command.CommandResult() + + command.test_result = _HandleVblockCommand entry_args = { 'keydir': 'devkeys', }