fip_util_test.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0+
  3. # Copyright 2021 Google LLC
  4. # Written by Simon Glass <sjg@chromium.org>
  5. """Tests for fip_util
  6. This tests a few features of fip_util which are not covered by binman's ftest.py
  7. """
  8. import os
  9. import shutil
  10. import sys
  11. import tempfile
  12. import unittest
  13. # Bring in the patman and dtoc libraries (but don't override the first path
  14. # in PYTHONPATH)
  15. OUR_PATH = os.path.dirname(os.path.realpath(__file__))
  16. sys.path.insert(2, os.path.join(OUR_PATH, '..'))
  17. # pylint: disable=C0413
  18. from binman import bintool
  19. from binman import fip_util
  20. from u_boot_pylib import test_util
  21. from u_boot_pylib import tools
  22. FIPTOOL = bintool.Bintool.create('fiptool')
  23. HAVE_FIPTOOL = FIPTOOL.is_present()
  24. # pylint: disable=R0902,R0904
  25. class TestFip(unittest.TestCase):
  26. """Test of fip_util classes"""
  27. #pylint: disable=W0212
  28. def setUp(self):
  29. # Create a temporary directory for test files
  30. self._indir = tempfile.mkdtemp(prefix='fip_util.')
  31. tools.set_input_dirs([self._indir])
  32. # Set up a temporary output directory, used by the tools library when
  33. # compressing files
  34. tools.prepare_output_dir(None)
  35. self.src_file = os.path.join(self._indir, 'orig.py')
  36. self.outname = tools.get_output_filename('out.py')
  37. self.args = ['-D', '-s', self._indir, '-o', self.outname]
  38. self.readme = os.path.join(self._indir, 'readme.rst')
  39. self.macro_dir = os.path.join(self._indir, 'include/tools_share')
  40. self.macro_fname = os.path.join(self.macro_dir,
  41. 'firmware_image_package.h')
  42. self.name_dir = os.path.join(self._indir, 'tools/fiptool')
  43. self.name_fname = os.path.join(self.name_dir, 'tbbr_config.c')
  44. macro_contents = '''
  45. /* ToC Entry UUIDs */
  46. #define UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U \\
  47. {{0x65, 0x92, 0x27, 0x03}, {0x2f, 0x74}, {0xe6, 0x44}, 0x8d, 0xff, {0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10} }
  48. #define UUID_TRUSTED_UPDATE_FIRMWARE_BL2U \\
  49. {{0x60, 0xb3, 0xeb, 0x37}, {0xc1, 0xe5}, {0xea, 0x41}, 0x9d, 0xf3, {0x19, 0xed, 0xa1, 0x1f, 0x68, 0x01} }
  50. '''
  51. name_contents = '''
  52. toc_entry_t toc_entries[] = {
  53. {
  54. .name = "SCP Firmware Updater Configuration FWU SCP_BL2U",
  55. .uuid = UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U,
  56. .cmdline_name = "scp-fwu-cfg"
  57. },
  58. {
  59. .name = "AP Firmware Updater Configuration BL2U",
  60. .uuid = UUID_TRUSTED_UPDATE_FIRMWARE_BL2U,
  61. .cmdline_name = "ap-fwu-cfg"
  62. },
  63. '''
  64. def setup_readme(self):
  65. """Set up the readme.txt file"""
  66. tools.write_file(self.readme, 'Trusted Firmware-A\n==================',
  67. binary=False)
  68. def setup_macro(self, data=macro_contents):
  69. """Set up the tbbr_config.c file"""
  70. os.makedirs(self.macro_dir)
  71. tools.write_file(self.macro_fname, data, binary=False)
  72. def setup_name(self, data=name_contents):
  73. """Set up the firmware_image_package.h file"""
  74. os.makedirs(self.name_dir)
  75. tools.write_file(self.name_fname, data, binary=False)
  76. def tearDown(self):
  77. """Remove the temporary input directory and its contents"""
  78. if self._indir:
  79. shutil.rmtree(self._indir)
  80. self._indir = None
  81. tools.finalise_output_dir()
  82. def test_no_readme(self):
  83. """Test handling of a missing readme.rst"""
  84. with self.assertRaises(Exception) as err:
  85. fip_util.main(self.args, self.src_file)
  86. self.assertIn('Expected file', str(err.exception))
  87. def test_invalid_readme(self):
  88. """Test that an invalid readme.rst is detected"""
  89. tools.write_file(self.readme, 'blah', binary=False)
  90. with self.assertRaises(Exception) as err:
  91. fip_util.main(self.args, self.src_file)
  92. self.assertIn('does not start with', str(err.exception))
  93. def test_no_fip_h(self):
  94. """Check handling of missing firmware_image_package.h"""
  95. self.setup_readme()
  96. with self.assertRaises(Exception) as err:
  97. fip_util.main(self.args, self.src_file)
  98. self.assertIn('No such file or directory', str(err.exception))
  99. def test_invalid_fip_h(self):
  100. """Check failure to parse firmware_image_package.h"""
  101. self.setup_readme()
  102. self.setup_macro('blah')
  103. with self.assertRaises(Exception) as err:
  104. fip_util.main(self.args, self.src_file)
  105. self.assertIn('Cannot parse file', str(err.exception))
  106. def test_parse_fip_h(self):
  107. """Check parsing of firmware_image_package.h"""
  108. self.setup_readme()
  109. # Check parsing the header file
  110. self.setup_macro()
  111. macros = fip_util.parse_macros(self._indir)
  112. expected_macros = {
  113. 'UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U':
  114. ('ToC Entry UUIDs', 'UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U',
  115. bytes([0x65, 0x92, 0x27, 0x03, 0x2f, 0x74, 0xe6, 0x44,
  116. 0x8d, 0xff, 0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10])),
  117. 'UUID_TRUSTED_UPDATE_FIRMWARE_BL2U':
  118. ('ToC Entry UUIDs', 'UUID_TRUSTED_UPDATE_FIRMWARE_BL2U',
  119. bytes([0x60, 0xb3, 0xeb, 0x37, 0xc1, 0xe5, 0xea, 0x41,
  120. 0x9d, 0xf3, 0x19, 0xed, 0xa1, 0x1f, 0x68, 0x01])),
  121. }
  122. self.assertEqual(expected_macros, macros)
  123. def test_missing_tbbr_c(self):
  124. """Check handlinh of missing tbbr_config.c"""
  125. self.setup_readme()
  126. self.setup_macro()
  127. # Still need the .c file
  128. with self.assertRaises(Exception) as err:
  129. fip_util.main(self.args, self.src_file)
  130. self.assertIn('tbbr_config.c', str(err.exception))
  131. def test_invalid_tbbr_c(self):
  132. """Check failure to parse tbbr_config.c"""
  133. self.setup_readme()
  134. self.setup_macro()
  135. # Check invalid format for C file
  136. self.setup_name('blah')
  137. with self.assertRaises(Exception) as err:
  138. fip_util.main(self.args, self.src_file)
  139. self.assertIn('Cannot parse file', str(err.exception))
  140. def test_inconsistent_tbbr_c(self):
  141. """Check tbbr_config.c in a format we don't expect"""
  142. self.setup_readme()
  143. # This is missing a hex value
  144. self.setup_macro('''
  145. /* ToC Entry UUIDs */
  146. #define UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U \\
  147. {{0x65, 0x92, 0x27,}, {0x2f, 0x74}, {0xe6, 0x44}, 0x8d, 0xff, {0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10} }
  148. #define UUID_TRUSTED_UPDATE_FIRMWARE_BL2U \\
  149. {{0x60, 0xb3, 0xeb, 0x37}, {0xc1, 0xe5}, {0xea, 0x41}, 0x9d, 0xf3, {0x19, 0xed, 0xa1, 0x1f, 0x68, 0x01} }
  150. ''')
  151. # Check invalid format for C file
  152. self.setup_name('blah')
  153. with self.assertRaises(Exception) as err:
  154. fip_util.main(self.args, self.src_file)
  155. self.assertIn('Cannot parse UUID line 5', str(err.exception))
  156. def test_parse_tbbr_c(self):
  157. """Check parsing tbbr_config.c"""
  158. self.setup_readme()
  159. self.setup_macro()
  160. self.setup_name()
  161. names = fip_util.parse_names(self._indir)
  162. expected_names = {
  163. 'UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U': (
  164. 'SCP Firmware Updater Configuration FWU SCP_BL2U',
  165. 'UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U',
  166. 'scp-fwu-cfg'),
  167. 'UUID_TRUSTED_UPDATE_FIRMWARE_BL2U': (
  168. 'AP Firmware Updater Configuration BL2U',
  169. 'UUID_TRUSTED_UPDATE_FIRMWARE_BL2U',
  170. 'ap-fwu-cfg'),
  171. }
  172. self.assertEqual(expected_names, names)
  173. def test_uuid_not_in_tbbr_config_c(self):
  174. """Check handling a UUID in the header file that's not in the .c file"""
  175. self.setup_readme()
  176. self.setup_macro(self.macro_contents + '''
  177. #define UUID_TRUSTED_OS_FW_KEY_CERT \\
  178. {{0x94, 0x77, 0xd6, 0x03}, {0xfb, 0x60}, {0xe4, 0x11}, 0x85, 0xdd, {0xb7, 0x10, 0x5b, 0x8c, 0xee, 0x04} }
  179. ''')
  180. self.setup_name()
  181. macros = fip_util.parse_macros(self._indir)
  182. names = fip_util.parse_names(self._indir)
  183. with test_util.capture_sys_output() as (stdout, _):
  184. fip_util.create_code_output(macros, names)
  185. self.assertIn(
  186. "UUID 'UUID_TRUSTED_OS_FW_KEY_CERT' is not mentioned in tbbr_config.c file",
  187. stdout.getvalue())
  188. def test_changes(self):
  189. """Check handling of a source file that does/doesn't need changes"""
  190. self.setup_readme()
  191. self.setup_macro()
  192. self.setup_name()
  193. # Check generating the file when changes are needed
  194. tools.write_file(self.src_file, '''
  195. # This is taken from tbbr_config.c in ARM Trusted Firmware
  196. FIP_TYPE_LIST = [
  197. # ToC Entry UUIDs
  198. FipType('scp-fwu-cfg', 'SCP Firmware Updater Configuration FWU SCP_BL2U',
  199. [0x65, 0x92, 0x27, 0x03, 0x2f, 0x74, 0xe6, 0x44,
  200. 0x8d, 0xff, 0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10]),
  201. ] # end
  202. blah de blah
  203. ''', binary=False)
  204. with test_util.capture_sys_output() as (stdout, _):
  205. fip_util.main(self.args, self.src_file)
  206. self.assertIn('Needs update', stdout.getvalue())
  207. # Check generating the file when no changes are needed
  208. tools.write_file(self.src_file, '''
  209. # This is taken from tbbr_config.c in ARM Trusted Firmware
  210. FIP_TYPE_LIST = [
  211. # ToC Entry UUIDs
  212. FipType('scp-fwu-cfg', 'SCP Firmware Updater Configuration FWU SCP_BL2U',
  213. [0x65, 0x92, 0x27, 0x03, 0x2f, 0x74, 0xe6, 0x44,
  214. 0x8d, 0xff, 0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10]),
  215. FipType('ap-fwu-cfg', 'AP Firmware Updater Configuration BL2U',
  216. [0x60, 0xb3, 0xeb, 0x37, 0xc1, 0xe5, 0xea, 0x41,
  217. 0x9d, 0xf3, 0x19, 0xed, 0xa1, 0x1f, 0x68, 0x01]),
  218. ] # end
  219. blah blah''', binary=False)
  220. with test_util.capture_sys_output() as (stdout, _):
  221. fip_util.main(self.args, self.src_file)
  222. self.assertIn('is up-to-date', stdout.getvalue())
  223. def test_no_debug(self):
  224. """Test running without the -D flag"""
  225. self.setup_readme()
  226. self.setup_macro()
  227. self.setup_name()
  228. args = self.args.copy()
  229. args.remove('-D')
  230. tools.write_file(self.src_file, '', binary=False)
  231. with test_util.capture_sys_output():
  232. fip_util.main(args, self.src_file)
  233. @unittest.skipIf(not HAVE_FIPTOOL, 'No fiptool available')
  234. def test_fiptool_list(self):
  235. """Create a FIP and check that fiptool can read it"""
  236. fwu = b'my data'
  237. tb_fw = b'some more data'
  238. fip = fip_util.FipWriter(0x123, 0x10)
  239. fip.add_entry('fwu', fwu, 0x456)
  240. fip.add_entry('tb-fw', tb_fw, 0)
  241. fip.add_entry(bytes(range(16)), tb_fw, 0)
  242. data = fip.get_data()
  243. fname = tools.get_output_filename('data.fip')
  244. tools.write_file(fname, data)
  245. result = FIPTOOL.info(fname)
  246. self.assertEqual(
  247. '''Firmware Updater NS_BL2U: offset=0xB0, size=0x7, cmdline="--fwu"
  248. Trusted Boot Firmware BL2: offset=0xC0, size=0xE, cmdline="--tb-fw"
  249. 00010203-0405-0607-0809-0A0B0C0D0E0F: offset=0xD0, size=0xE, cmdline="--blob"
  250. ''',
  251. result)
  252. fwu_data = b'my data'
  253. tb_fw_data = b'some more data'
  254. other_fw_data = b'even more'
  255. def create_fiptool_image(self):
  256. """Create an image with fiptool which we can use for testing
  257. Returns:
  258. FipReader: reader for the image
  259. """
  260. fwu = os.path.join(self._indir, 'fwu')
  261. tools.write_file(fwu, self.fwu_data)
  262. tb_fw = os.path.join(self._indir, 'tb_fw')
  263. tools.write_file(tb_fw, self.tb_fw_data)
  264. other_fw = os.path.join(self._indir, 'other_fw')
  265. tools.write_file(other_fw, self.other_fw_data)
  266. fname = tools.get_output_filename('data.fip')
  267. uuid = 'e3b78d9e-4a64-11ec-b45c-fba2b9b49788'
  268. FIPTOOL.create_new(fname, 8, 0x123, fwu, tb_fw, uuid, other_fw)
  269. return fip_util.FipReader(tools.read_file(fname))
  270. @unittest.skipIf(not HAVE_FIPTOOL, 'No fiptool available')
  271. def test_fiptool_create(self):
  272. """Create a FIP with fiptool and check that fip_util can read it"""
  273. reader = self.create_fiptool_image()
  274. header = reader.header
  275. fents = reader.fents
  276. self.assertEqual(0x123 << 32, header.flags)
  277. self.assertEqual(fip_util.HEADER_MAGIC, header.name)
  278. self.assertEqual(fip_util.HEADER_SERIAL, header.serial)
  279. self.assertEqual(3, len(fents))
  280. fent = fents[0]
  281. self.assertEqual(
  282. bytes([0x4f, 0x51, 0x1d, 0x11, 0x2b, 0xe5, 0x4e, 0x49,
  283. 0xb4, 0xc5, 0x83, 0xc2, 0xf7, 0x15, 0x84, 0x0a]), fent.uuid)
  284. self.assertEqual(0xb0, fent.offset)
  285. self.assertEqual(len(self.fwu_data), fent.size)
  286. self.assertEqual(0, fent.flags)
  287. self.assertEqual(self.fwu_data, fent.data)
  288. fent = fents[1]
  289. self.assertEqual(
  290. bytes([0x5f, 0xf9, 0xec, 0x0b, 0x4d, 0x22, 0x3e, 0x4d,
  291. 0xa5, 0x44, 0xc3, 0x9d, 0x81, 0xc7, 0x3f, 0x0a]), fent.uuid)
  292. self.assertEqual(0xb8, fent.offset)
  293. self.assertEqual(len(self.tb_fw_data), fent.size)
  294. self.assertEqual(0, fent.flags)
  295. self.assertEqual(self.tb_fw_data, fent.data)
  296. fent = fents[2]
  297. self.assertEqual(
  298. bytes([0xe3, 0xb7, 0x8d, 0x9e, 0x4a, 0x64, 0x11, 0xec,
  299. 0xb4, 0x5c, 0xfb, 0xa2, 0xb9, 0xb4, 0x97, 0x88]), fent.uuid)
  300. self.assertEqual(0xc8, fent.offset)
  301. self.assertEqual(len(self.other_fw_data), fent.size)
  302. self.assertEqual(0, fent.flags)
  303. self.assertEqual(self.other_fw_data, fent.data)
  304. @unittest.skipIf(not HAVE_FIPTOOL, 'No fiptool available')
  305. def test_reader_get_entry(self):
  306. """Test get_entry() by name and UUID"""
  307. reader = self.create_fiptool_image()
  308. fents = reader.fents
  309. fent = reader.get_entry('fwu')
  310. self.assertEqual(fent, fents[0])
  311. fent = reader.get_entry(
  312. bytes([0x5f, 0xf9, 0xec, 0x0b, 0x4d, 0x22, 0x3e, 0x4d,
  313. 0xa5, 0x44, 0xc3, 0x9d, 0x81, 0xc7, 0x3f, 0x0a]))
  314. self.assertEqual(fent, fents[1])
  315. # Try finding entries that don't exist
  316. with self.assertRaises(Exception) as err:
  317. fent = reader.get_entry('scp-fwu-cfg')
  318. self.assertIn("Cannot find FIP entry 'scp-fwu-cfg'", str(err.exception))
  319. with self.assertRaises(Exception) as err:
  320. fent = reader.get_entry(bytes(list(range(16))))
  321. self.assertIn(
  322. "Cannot find FIP entry '00010203-0405-0607-0809-0a0b0c0d0e0f'",
  323. str(err.exception))
  324. with self.assertRaises(Exception) as err:
  325. fent = reader.get_entry('blah')
  326. self.assertIn("Unknown FIP entry type 'blah'", str(err.exception))
  327. @unittest.skipIf(not HAVE_FIPTOOL, 'No fiptool available')
  328. def test_fiptool_errors(self):
  329. """Check some error reporting from fiptool"""
  330. with self.assertRaises(Exception) as err:
  331. with test_util.capture_sys_output():
  332. FIPTOOL.create_bad()
  333. self.assertIn("unrecognized option '--fred'", str(err.exception))
  334. if __name__ == '__main__':
  335. unittest.main()