2 # SPDX-License-Identifier: GPL-2.0+
3 # Copyright 2021 Google LLC
4 # Written by Simon Glass <sjg@chromium.org>
8 This tests a few features of fip_util which are not covered by binman's ftest.py
17 # Bring in the patman and dtoc libraries (but don't override the first path
19 OUR_PATH = os.path.dirname(os.path.realpath(__file__))
20 sys.path.insert(2, os.path.join(OUR_PATH, '..'))
22 # pylint: disable=C0413
23 from binman import bintool
24 from binman import fip_util
25 from u_boot_pylib import test_util
26 from u_boot_pylib import tools
28 FIPTOOL = bintool.Bintool.create('fiptool')
29 HAVE_FIPTOOL = FIPTOOL.is_present()
31 # pylint: disable=R0902,R0904
32 class TestFip(unittest.TestCase):
33 """Test of fip_util classes"""
34 #pylint: disable=W0212
36 # Create a temporary directory for test files
37 self._indir = tempfile.mkdtemp(prefix='fip_util.')
38 tools.set_input_dirs([self._indir])
40 # Set up a temporary output directory, used by the tools library when
42 tools.prepare_output_dir(None)
44 self.src_file = os.path.join(self._indir, 'orig.py')
45 self.outname = tools.get_output_filename('out.py')
46 self.args = ['-D', '-s', self._indir, '-o', self.outname]
47 self.readme = os.path.join(self._indir, 'readme.rst')
48 self.macro_dir = os.path.join(self._indir, 'include/tools_share')
49 self.macro_fname = os.path.join(self.macro_dir,
50 'firmware_image_package.h')
51 self.name_dir = os.path.join(self._indir, 'tools/fiptool')
52 self.name_fname = os.path.join(self.name_dir, 'tbbr_config.c')
57 #define UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U \\
58 {{0x65, 0x92, 0x27, 0x03}, {0x2f, 0x74}, {0xe6, 0x44}, 0x8d, 0xff, {0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10} }
59 #define UUID_TRUSTED_UPDATE_FIRMWARE_BL2U \\
60 {{0x60, 0xb3, 0xeb, 0x37}, {0xc1, 0xe5}, {0xea, 0x41}, 0x9d, 0xf3, {0x19, 0xed, 0xa1, 0x1f, 0x68, 0x01} }
66 toc_entry_t toc_entries[] = {
68 .name = "SCP Firmware Updater Configuration FWU SCP_BL2U",
69 .uuid = UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U,
70 .cmdline_name = "scp-fwu-cfg"
73 .name = "AP Firmware Updater Configuration BL2U",
74 .uuid = UUID_TRUSTED_UPDATE_FIRMWARE_BL2U,
75 .cmdline_name = "ap-fwu-cfg"
79 def setup_readme(self):
80 """Set up the readme.txt file"""
81 tools.write_file(self.readme, 'Trusted Firmware-A\n==================',
84 def setup_macro(self, data=macro_contents):
85 """Set up the tbbr_config.c file"""
86 os.makedirs(self.macro_dir)
87 tools.write_file(self.macro_fname, data, binary=False)
89 def setup_name(self, data=name_contents):
90 """Set up the firmware_image_package.h file"""
91 os.makedirs(self.name_dir)
92 tools.write_file(self.name_fname, data, binary=False)
95 """Remove the temporary input directory and its contents"""
97 shutil.rmtree(self._indir)
99 tools.finalise_output_dir()
101 def test_no_readme(self):
102 """Test handling of a missing readme.rst"""
103 with self.assertRaises(Exception) as err:
104 fip_util.main(self.args, self.src_file)
105 self.assertIn('Expected file', str(err.exception))
107 def test_invalid_readme(self):
108 """Test that an invalid readme.rst is detected"""
109 tools.write_file(self.readme, 'blah', binary=False)
110 with self.assertRaises(Exception) as err:
111 fip_util.main(self.args, self.src_file)
112 self.assertIn('does not start with', str(err.exception))
114 def test_no_fip_h(self):
115 """Check handling of missing firmware_image_package.h"""
117 with self.assertRaises(Exception) as err:
118 fip_util.main(self.args, self.src_file)
119 self.assertIn('No such file or directory', str(err.exception))
121 def test_invalid_fip_h(self):
122 """Check failure to parse firmware_image_package.h"""
124 self.setup_macro('blah')
125 with self.assertRaises(Exception) as err:
126 fip_util.main(self.args, self.src_file)
127 self.assertIn('Cannot parse file', str(err.exception))
129 def test_parse_fip_h(self):
130 """Check parsing of firmware_image_package.h"""
132 # Check parsing the header file
134 macros = fip_util.parse_macros(self._indir)
136 'UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U':
137 ('ToC Entry UUIDs', 'UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U',
138 bytes([0x65, 0x92, 0x27, 0x03, 0x2f, 0x74, 0xe6, 0x44,
139 0x8d, 0xff, 0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10])),
140 'UUID_TRUSTED_UPDATE_FIRMWARE_BL2U':
141 ('ToC Entry UUIDs', 'UUID_TRUSTED_UPDATE_FIRMWARE_BL2U',
142 bytes([0x60, 0xb3, 0xeb, 0x37, 0xc1, 0xe5, 0xea, 0x41,
143 0x9d, 0xf3, 0x19, 0xed, 0xa1, 0x1f, 0x68, 0x01])),
145 self.assertEqual(expected_macros, macros)
147 def test_missing_tbbr_c(self):
148 """Check handlinh of missing tbbr_config.c"""
152 # Still need the .c file
153 with self.assertRaises(Exception) as err:
154 fip_util.main(self.args, self.src_file)
155 self.assertIn('tbbr_config.c', str(err.exception))
157 def test_invalid_tbbr_c(self):
158 """Check failure to parse tbbr_config.c"""
161 # Check invalid format for C file
162 self.setup_name('blah')
163 with self.assertRaises(Exception) as err:
164 fip_util.main(self.args, self.src_file)
165 self.assertIn('Cannot parse file', str(err.exception))
167 def test_inconsistent_tbbr_c(self):
168 """Check tbbr_config.c in a format we don't expect"""
170 # This is missing a hex value
173 /* ToC Entry UUIDs */
174 #define UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U \\
175 {{0x65, 0x92, 0x27,}, {0x2f, 0x74}, {0xe6, 0x44}, 0x8d, 0xff, {0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10} }
176 #define UUID_TRUSTED_UPDATE_FIRMWARE_BL2U \\
177 {{0x60, 0xb3, 0xeb, 0x37}, {0xc1, 0xe5}, {0xea, 0x41}, 0x9d, 0xf3, {0x19, 0xed, 0xa1, 0x1f, 0x68, 0x01} }
180 # Check invalid format for C file
181 self.setup_name('blah')
182 with self.assertRaises(Exception) as err:
183 fip_util.main(self.args, self.src_file)
184 self.assertIn('Cannot parse UUID line 5', str(err.exception))
186 def test_parse_tbbr_c(self):
187 """Check parsing tbbr_config.c"""
192 names = fip_util.parse_names(self._indir)
195 'UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U': (
196 'SCP Firmware Updater Configuration FWU SCP_BL2U',
197 'UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U',
199 'UUID_TRUSTED_UPDATE_FIRMWARE_BL2U': (
200 'AP Firmware Updater Configuration BL2U',
201 'UUID_TRUSTED_UPDATE_FIRMWARE_BL2U',
204 self.assertEqual(expected_names, names)
206 def test_uuid_not_in_tbbr_config_c(self):
207 """Check handling a UUID in the header file that's not in the .c file"""
209 self.setup_macro(self.macro_contents + '''
210 #define UUID_TRUSTED_OS_FW_KEY_CERT \\
211 {{0x94, 0x77, 0xd6, 0x03}, {0xfb, 0x60}, {0xe4, 0x11}, 0x85, 0xdd, {0xb7, 0x10, 0x5b, 0x8c, 0xee, 0x04} }
216 macros = fip_util.parse_macros(self._indir)
217 names = fip_util.parse_names(self._indir)
218 with test_util.capture_sys_output() as (stdout, _):
219 fip_util.create_code_output(macros, names)
221 "UUID 'UUID_TRUSTED_OS_FW_KEY_CERT' is not mentioned in tbbr_config.c file",
224 def test_changes(self):
225 """Check handling of a source file that does/doesn't need changes"""
230 # Check generating the file when changes are needed
231 tools.write_file(self.src_file, '''
233 # This is taken from tbbr_config.c in ARM Trusted Firmware
236 FipType('scp-fwu-cfg', 'SCP Firmware Updater Configuration FWU SCP_BL2U',
237 [0x65, 0x92, 0x27, 0x03, 0x2f, 0x74, 0xe6, 0x44,
238 0x8d, 0xff, 0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10]),
242 with test_util.capture_sys_output() as (stdout, _):
243 fip_util.main(self.args, self.src_file)
244 self.assertIn('Needs update', stdout.getvalue())
246 # Check generating the file when no changes are needed
247 tools.write_file(self.src_file, '''
248 # This is taken from tbbr_config.c in ARM Trusted Firmware
251 FipType('scp-fwu-cfg', 'SCP Firmware Updater Configuration FWU SCP_BL2U',
252 [0x65, 0x92, 0x27, 0x03, 0x2f, 0x74, 0xe6, 0x44,
253 0x8d, 0xff, 0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10]),
254 FipType('ap-fwu-cfg', 'AP Firmware Updater Configuration BL2U',
255 [0x60, 0xb3, 0xeb, 0x37, 0xc1, 0xe5, 0xea, 0x41,
256 0x9d, 0xf3, 0x19, 0xed, 0xa1, 0x1f, 0x68, 0x01]),
258 blah blah''', binary=False)
259 with test_util.capture_sys_output() as (stdout, _):
260 fip_util.main(self.args, self.src_file)
261 self.assertIn('is up-to-date', stdout.getvalue())
263 def test_no_debug(self):
264 """Test running without the -D flag"""
269 args = self.args.copy()
271 tools.write_file(self.src_file, '', binary=False)
272 with test_util.capture_sys_output():
273 fip_util.main(args, self.src_file)
275 @unittest.skipIf(not HAVE_FIPTOOL, 'No fiptool available')
276 def test_fiptool_list(self):
277 """Create a FIP and check that fiptool can read it"""
279 tb_fw = b'some more data'
280 fip = fip_util.FipWriter(0x123, 0x10)
281 fip.add_entry('fwu', fwu, 0x456)
282 fip.add_entry('tb-fw', tb_fw, 0)
283 fip.add_entry(bytes(range(16)), tb_fw, 0)
284 data = fip.get_data()
285 fname = tools.get_output_filename('data.fip')
286 tools.write_file(fname, data)
287 result = FIPTOOL.info(fname)
289 '''Firmware Updater NS_BL2U: offset=0xB0, size=0x7, cmdline="--fwu"
290 Trusted Boot Firmware BL2: offset=0xC0, size=0xE, cmdline="--tb-fw"
291 00010203-0405-0607-0809-0A0B0C0D0E0F: offset=0xD0, size=0xE, cmdline="--blob"
295 fwu_data = b'my data'
296 tb_fw_data = b'some more data'
297 other_fw_data = b'even more'
299 def create_fiptool_image(self):
300 """Create an image with fiptool which we can use for testing
303 FipReader: reader for the image
305 fwu = os.path.join(self._indir, 'fwu')
306 tools.write_file(fwu, self.fwu_data)
308 tb_fw = os.path.join(self._indir, 'tb_fw')
309 tools.write_file(tb_fw, self.tb_fw_data)
311 other_fw = os.path.join(self._indir, 'other_fw')
312 tools.write_file(other_fw, self.other_fw_data)
314 fname = tools.get_output_filename('data.fip')
315 uuid = 'e3b78d9e-4a64-11ec-b45c-fba2b9b49788'
316 FIPTOOL.create_new(fname, 8, 0x123, fwu, tb_fw, uuid, other_fw)
318 return fip_util.FipReader(tools.read_file(fname))
320 @unittest.skipIf(not HAVE_FIPTOOL, 'No fiptool available')
321 def test_fiptool_create(self):
322 """Create a FIP with fiptool and check that fip_util can read it"""
323 reader = self.create_fiptool_image()
325 header = reader.header
328 self.assertEqual(0x123 << 32, header.flags)
329 self.assertEqual(fip_util.HEADER_MAGIC, header.name)
330 self.assertEqual(fip_util.HEADER_SERIAL, header.serial)
332 self.assertEqual(3, len(fents))
335 bytes([0x4f, 0x51, 0x1d, 0x11, 0x2b, 0xe5, 0x4e, 0x49,
336 0xb4, 0xc5, 0x83, 0xc2, 0xf7, 0x15, 0x84, 0x0a]), fent.uuid)
337 self.assertEqual(0xb0, fent.offset)
338 self.assertEqual(len(self.fwu_data), fent.size)
339 self.assertEqual(0, fent.flags)
340 self.assertEqual(self.fwu_data, fent.data)
344 bytes([0x5f, 0xf9, 0xec, 0x0b, 0x4d, 0x22, 0x3e, 0x4d,
345 0xa5, 0x44, 0xc3, 0x9d, 0x81, 0xc7, 0x3f, 0x0a]), fent.uuid)
346 self.assertEqual(0xb8, fent.offset)
347 self.assertEqual(len(self.tb_fw_data), fent.size)
348 self.assertEqual(0, fent.flags)
349 self.assertEqual(self.tb_fw_data, fent.data)
353 bytes([0xe3, 0xb7, 0x8d, 0x9e, 0x4a, 0x64, 0x11, 0xec,
354 0xb4, 0x5c, 0xfb, 0xa2, 0xb9, 0xb4, 0x97, 0x88]), fent.uuid)
355 self.assertEqual(0xc8, fent.offset)
356 self.assertEqual(len(self.other_fw_data), fent.size)
357 self.assertEqual(0, fent.flags)
358 self.assertEqual(self.other_fw_data, fent.data)
360 @unittest.skipIf(not HAVE_FIPTOOL, 'No fiptool available')
361 def test_reader_get_entry(self):
362 """Test get_entry() by name and UUID"""
363 reader = self.create_fiptool_image()
365 fent = reader.get_entry('fwu')
366 self.assertEqual(fent, fents[0])
368 fent = reader.get_entry(
369 bytes([0x5f, 0xf9, 0xec, 0x0b, 0x4d, 0x22, 0x3e, 0x4d,
370 0xa5, 0x44, 0xc3, 0x9d, 0x81, 0xc7, 0x3f, 0x0a]))
371 self.assertEqual(fent, fents[1])
373 # Try finding entries that don't exist
374 with self.assertRaises(Exception) as err:
375 fent = reader.get_entry('scp-fwu-cfg')
376 self.assertIn("Cannot find FIP entry 'scp-fwu-cfg'", str(err.exception))
378 with self.assertRaises(Exception) as err:
379 fent = reader.get_entry(bytes(list(range(16))))
381 "Cannot find FIP entry '00010203-0405-0607-0809-0a0b0c0d0e0f'",
384 with self.assertRaises(Exception) as err:
385 fent = reader.get_entry('blah')
386 self.assertIn("Unknown FIP entry type 'blah'", str(err.exception))
388 @unittest.skipIf(not HAVE_FIPTOOL, 'No fiptool available')
389 def test_fiptool_errors(self):
390 """Check some error reporting from fiptool"""
391 with self.assertRaises(Exception) as err:
392 with test_util.capture_sys_output():
394 self.assertIn("unrecognized option '--fred'", str(err.exception))
397 if __name__ == '__main__':