blob: 56aa56f4643fd8d7b2be310e68a5ba194c6831c4 [file] [log] [blame]
Simon Glassed16b122021-11-23 21:08:58 -07001#!/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
6"""Tests for fip_util
7
8This tests a few features of fip_util which are not covered by binman's ftest.py
9"""
10
11import os
12import shutil
13import sys
14import tempfile
15import unittest
16
17# Bring in the patman and dtoc libraries (but don't override the first path
18# in PYTHONPATH)
19OUR_PATH = os.path.dirname(os.path.realpath(__file__))
20sys.path.insert(2, os.path.join(OUR_PATH, '..'))
21
22# pylint: disable=C0413
Simon Glass388f04f2022-01-09 20:13:59 -070023from binman import bintool
Simon Glass7f295832022-01-09 20:13:47 -070024from binman import fip_util
Simon Glass4583c002023-02-23 18:18:04 -070025from u_boot_pylib import test_util
26from u_boot_pylib import tools
Simon Glassed16b122021-11-23 21:08:58 -070027
Simon Glass388f04f2022-01-09 20:13:59 -070028FIPTOOL = bintool.Bintool.create('fiptool')
29HAVE_FIPTOOL = FIPTOOL.is_present()
Simon Glassed16b122021-11-23 21:08:58 -070030
31# pylint: disable=R0902,R0904
32class TestFip(unittest.TestCase):
33 """Test of fip_util classes"""
34 #pylint: disable=W0212
35 def setUp(self):
36 # Create a temporary directory for test files
37 self._indir = tempfile.mkdtemp(prefix='fip_util.')
Simon Glassc1aa66e2022-01-29 14:14:04 -070038 tools.set_input_dirs([self._indir])
Simon Glassed16b122021-11-23 21:08:58 -070039
40 # Set up a temporary output directory, used by the tools library when
41 # compressing files
Simon Glassc1aa66e2022-01-29 14:14:04 -070042 tools.prepare_output_dir(None)
Simon Glassed16b122021-11-23 21:08:58 -070043
44 self.src_file = os.path.join(self._indir, 'orig.py')
Simon Glassc1aa66e2022-01-29 14:14:04 -070045 self.outname = tools.get_output_filename('out.py')
Simon Glassed16b122021-11-23 21:08:58 -070046 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')
53
54 macro_contents = '''
55
56/* ToC Entry UUIDs */
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} }
61
62'''
63
64 name_contents = '''
65
66toc_entry_t toc_entries[] = {
67 {
68 .name = "SCP Firmware Updater Configuration FWU SCP_BL2U",
69 .uuid = UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U,
70 .cmdline_name = "scp-fwu-cfg"
71 },
72 {
73 .name = "AP Firmware Updater Configuration BL2U",
74 .uuid = UUID_TRUSTED_UPDATE_FIRMWARE_BL2U,
75 .cmdline_name = "ap-fwu-cfg"
76 },
77'''
78
79 def setup_readme(self):
80 """Set up the readme.txt file"""
Simon Glassc1aa66e2022-01-29 14:14:04 -070081 tools.write_file(self.readme, 'Trusted Firmware-A\n==================',
Simon Glassed16b122021-11-23 21:08:58 -070082 binary=False)
83
84 def setup_macro(self, data=macro_contents):
85 """Set up the tbbr_config.c file"""
86 os.makedirs(self.macro_dir)
Simon Glassc1aa66e2022-01-29 14:14:04 -070087 tools.write_file(self.macro_fname, data, binary=False)
Simon Glassed16b122021-11-23 21:08:58 -070088
89 def setup_name(self, data=name_contents):
90 """Set up the firmware_image_package.h file"""
91 os.makedirs(self.name_dir)
Simon Glassc1aa66e2022-01-29 14:14:04 -070092 tools.write_file(self.name_fname, data, binary=False)
Simon Glassed16b122021-11-23 21:08:58 -070093
94 def tearDown(self):
95 """Remove the temporary input directory and its contents"""
96 if self._indir:
97 shutil.rmtree(self._indir)
98 self._indir = None
Simon Glassc1aa66e2022-01-29 14:14:04 -070099 tools.finalise_output_dir()
Simon Glassed16b122021-11-23 21:08:58 -0700100
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))
106
107 def test_invalid_readme(self):
108 """Test that an invalid readme.rst is detected"""
Simon Glassc1aa66e2022-01-29 14:14:04 -0700109 tools.write_file(self.readme, 'blah', binary=False)
Simon Glassed16b122021-11-23 21:08:58 -0700110 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))
113
114 def test_no_fip_h(self):
115 """Check handling of missing firmware_image_package.h"""
116 self.setup_readme()
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))
120
121 def test_invalid_fip_h(self):
122 """Check failure to parse firmware_image_package.h"""
123 self.setup_readme()
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))
128
129 def test_parse_fip_h(self):
130 """Check parsing of firmware_image_package.h"""
131 self.setup_readme()
132 # Check parsing the header file
133 self.setup_macro()
134 macros = fip_util.parse_macros(self._indir)
135 expected_macros = {
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])),
144 }
145 self.assertEqual(expected_macros, macros)
146
147 def test_missing_tbbr_c(self):
148 """Check handlinh of missing tbbr_config.c"""
149 self.setup_readme()
150 self.setup_macro()
151
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))
156
157 def test_invalid_tbbr_c(self):
158 """Check failure to parse tbbr_config.c"""
159 self.setup_readme()
160 self.setup_macro()
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))
166
167 def test_inconsistent_tbbr_c(self):
168 """Check tbbr_config.c in a format we don't expect"""
169 self.setup_readme()
170 # This is missing a hex value
171 self.setup_macro('''
172
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} }
178
179''')
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))
185
186 def test_parse_tbbr_c(self):
187 """Check parsing tbbr_config.c"""
188 self.setup_readme()
189 self.setup_macro()
190 self.setup_name()
191
192 names = fip_util.parse_names(self._indir)
193
194 expected_names = {
195 'UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U': (
196 'SCP Firmware Updater Configuration FWU SCP_BL2U',
197 'UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U',
198 'scp-fwu-cfg'),
199 'UUID_TRUSTED_UPDATE_FIRMWARE_BL2U': (
200 'AP Firmware Updater Configuration BL2U',
201 'UUID_TRUSTED_UPDATE_FIRMWARE_BL2U',
202 'ap-fwu-cfg'),
203 }
204 self.assertEqual(expected_names, names)
205
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"""
208 self.setup_readme()
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} }
212
213''')
214 self.setup_name()
215
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)
220 self.assertIn(
221 "UUID 'UUID_TRUSTED_OS_FW_KEY_CERT' is not mentioned in tbbr_config.c file",
222 stdout.getvalue())
223
224 def test_changes(self):
225 """Check handling of a source file that does/doesn't need changes"""
226 self.setup_readme()
227 self.setup_macro()
228 self.setup_name()
229
230 # Check generating the file when changes are needed
Simon Glassc1aa66e2022-01-29 14:14:04 -0700231 tools.write_file(self.src_file, '''
Simon Glassed16b122021-11-23 21:08:58 -0700232
233# This is taken from tbbr_config.c in ARM Trusted Firmware
234FIP_TYPE_LIST = [
235 # ToC Entry UUIDs
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]),
239 ] # end
240blah de blah
241 ''', binary=False)
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())
245
246 # Check generating the file when no changes are needed
Simon Glassc1aa66e2022-01-29 14:14:04 -0700247 tools.write_file(self.src_file, '''
Simon Glassed16b122021-11-23 21:08:58 -0700248# This is taken from tbbr_config.c in ARM Trusted Firmware
249FIP_TYPE_LIST = [
250 # ToC Entry UUIDs
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]),
257 ] # end
258blah 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())
262
263 def test_no_debug(self):
264 """Test running without the -D flag"""
265 self.setup_readme()
266 self.setup_macro()
267 self.setup_name()
268
269 args = self.args.copy()
270 args.remove('-D')
Simon Glassc1aa66e2022-01-29 14:14:04 -0700271 tools.write_file(self.src_file, '', binary=False)
Simon Glassed16b122021-11-23 21:08:58 -0700272 with test_util.capture_sys_output():
273 fip_util.main(args, self.src_file)
274
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"""
278 fwu = b'my data'
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()
Simon Glassc1aa66e2022-01-29 14:14:04 -0700285 fname = tools.get_output_filename('data.fip')
286 tools.write_file(fname, data)
Simon Glass388f04f2022-01-09 20:13:59 -0700287 result = FIPTOOL.info(fname)
Simon Glassed16b122021-11-23 21:08:58 -0700288 self.assertEqual(
289 '''Firmware Updater NS_BL2U: offset=0xB0, size=0x7, cmdline="--fwu"
290Trusted Boot Firmware BL2: offset=0xC0, size=0xE, cmdline="--tb-fw"
29100010203-0405-0607-0809-0A0B0C0D0E0F: offset=0xD0, size=0xE, cmdline="--blob"
292''',
Simon Glass388f04f2022-01-09 20:13:59 -0700293 result)
Simon Glassed16b122021-11-23 21:08:58 -0700294
295 fwu_data = b'my data'
296 tb_fw_data = b'some more data'
297 other_fw_data = b'even more'
298
299 def create_fiptool_image(self):
300 """Create an image with fiptool which we can use for testing
301
302 Returns:
303 FipReader: reader for the image
304 """
305 fwu = os.path.join(self._indir, 'fwu')
Simon Glassc1aa66e2022-01-29 14:14:04 -0700306 tools.write_file(fwu, self.fwu_data)
Simon Glassed16b122021-11-23 21:08:58 -0700307
308 tb_fw = os.path.join(self._indir, 'tb_fw')
Simon Glassc1aa66e2022-01-29 14:14:04 -0700309 tools.write_file(tb_fw, self.tb_fw_data)
Simon Glassed16b122021-11-23 21:08:58 -0700310
311 other_fw = os.path.join(self._indir, 'other_fw')
Simon Glassc1aa66e2022-01-29 14:14:04 -0700312 tools.write_file(other_fw, self.other_fw_data)
Simon Glassed16b122021-11-23 21:08:58 -0700313
Simon Glassc1aa66e2022-01-29 14:14:04 -0700314 fname = tools.get_output_filename('data.fip')
Simon Glassed16b122021-11-23 21:08:58 -0700315 uuid = 'e3b78d9e-4a64-11ec-b45c-fba2b9b49788'
Simon Glass388f04f2022-01-09 20:13:59 -0700316 FIPTOOL.create_new(fname, 8, 0x123, fwu, tb_fw, uuid, other_fw)
Simon Glassed16b122021-11-23 21:08:58 -0700317
Simon Glassc1aa66e2022-01-29 14:14:04 -0700318 return fip_util.FipReader(tools.read_file(fname))
Simon Glassed16b122021-11-23 21:08:58 -0700319
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()
324
325 header = reader.header
326 fents = reader.fents
327
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)
331
332 self.assertEqual(3, len(fents))
333 fent = fents[0]
334 self.assertEqual(
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)
341
342 fent = fents[1]
343 self.assertEqual(
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)
350
351 fent = fents[2]
352 self.assertEqual(
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)
359
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()
364 fents = reader.fents
365 fent = reader.get_entry('fwu')
366 self.assertEqual(fent, fents[0])
367
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])
372
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))
377
378 with self.assertRaises(Exception) as err:
379 fent = reader.get_entry(bytes(list(range(16))))
380 self.assertIn(
381 "Cannot find FIP entry '00010203-0405-0607-0809-0a0b0c0d0e0f'",
382 str(err.exception))
383
384 with self.assertRaises(Exception) as err:
385 fent = reader.get_entry('blah')
386 self.assertIn("Unknown FIP entry type 'blah'", str(err.exception))
387
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():
Simon Glass388f04f2022-01-09 20:13:59 -0700393 FIPTOOL.create_bad()
394 self.assertIn("unrecognized option '--fred'", str(err.exception))
Simon Glassed16b122021-11-23 21:08:58 -0700395
396
397if __name__ == '__main__':
398 unittest.main()