blob: cb4001be020d77b61b36fdc6ba70763a919b79ac [file] [log] [blame]
Simon Glass2697b7c2021-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 Glassb2833222022-01-09 20:13:59 -070023from binman import bintool
Simon Glass25639a02022-01-09 20:13:47 -070024from binman import fip_util
Simon Glass14d64e32025-04-29 07:21:59 -060025from u_boot_pylib import terminal
Simon Glass131444f2023-02-23 18:18:04 -070026from u_boot_pylib import test_util
27from u_boot_pylib import tools
Simon Glass2697b7c2021-11-23 21:08:58 -070028
Simon Glassb2833222022-01-09 20:13:59 -070029FIPTOOL = bintool.Bintool.create('fiptool')
30HAVE_FIPTOOL = FIPTOOL.is_present()
Simon Glass2697b7c2021-11-23 21:08:58 -070031
32# pylint: disable=R0902,R0904
33class TestFip(unittest.TestCase):
34 """Test of fip_util classes"""
35 #pylint: disable=W0212
36 def setUp(self):
37 # Create a temporary directory for test files
38 self._indir = tempfile.mkdtemp(prefix='fip_util.')
Simon Glass80025522022-01-29 14:14:04 -070039 tools.set_input_dirs([self._indir])
Simon Glass2697b7c2021-11-23 21:08:58 -070040
41 # Set up a temporary output directory, used by the tools library when
42 # compressing files
Simon Glass80025522022-01-29 14:14:04 -070043 tools.prepare_output_dir(None)
Simon Glass2697b7c2021-11-23 21:08:58 -070044
45 self.src_file = os.path.join(self._indir, 'orig.py')
Simon Glass80025522022-01-29 14:14:04 -070046 self.outname = tools.get_output_filename('out.py')
Simon Glass2697b7c2021-11-23 21:08:58 -070047 self.args = ['-D', '-s', self._indir, '-o', self.outname]
48 self.readme = os.path.join(self._indir, 'readme.rst')
49 self.macro_dir = os.path.join(self._indir, 'include/tools_share')
50 self.macro_fname = os.path.join(self.macro_dir,
51 'firmware_image_package.h')
52 self.name_dir = os.path.join(self._indir, 'tools/fiptool')
53 self.name_fname = os.path.join(self.name_dir, 'tbbr_config.c')
54
55 macro_contents = '''
56
57/* ToC Entry UUIDs */
58#define UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U \\
59 {{0x65, 0x92, 0x27, 0x03}, {0x2f, 0x74}, {0xe6, 0x44}, 0x8d, 0xff, {0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10} }
60#define UUID_TRUSTED_UPDATE_FIRMWARE_BL2U \\
61 {{0x60, 0xb3, 0xeb, 0x37}, {0xc1, 0xe5}, {0xea, 0x41}, 0x9d, 0xf3, {0x19, 0xed, 0xa1, 0x1f, 0x68, 0x01} }
62
63'''
64
65 name_contents = '''
66
67toc_entry_t toc_entries[] = {
68 {
69 .name = "SCP Firmware Updater Configuration FWU SCP_BL2U",
70 .uuid = UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U,
71 .cmdline_name = "scp-fwu-cfg"
72 },
73 {
74 .name = "AP Firmware Updater Configuration BL2U",
75 .uuid = UUID_TRUSTED_UPDATE_FIRMWARE_BL2U,
76 .cmdline_name = "ap-fwu-cfg"
77 },
78'''
79
80 def setup_readme(self):
81 """Set up the readme.txt file"""
Simon Glass80025522022-01-29 14:14:04 -070082 tools.write_file(self.readme, 'Trusted Firmware-A\n==================',
Simon Glass2697b7c2021-11-23 21:08:58 -070083 binary=False)
84
85 def setup_macro(self, data=macro_contents):
86 """Set up the tbbr_config.c file"""
87 os.makedirs(self.macro_dir)
Simon Glass80025522022-01-29 14:14:04 -070088 tools.write_file(self.macro_fname, data, binary=False)
Simon Glass2697b7c2021-11-23 21:08:58 -070089
90 def setup_name(self, data=name_contents):
91 """Set up the firmware_image_package.h file"""
92 os.makedirs(self.name_dir)
Simon Glass80025522022-01-29 14:14:04 -070093 tools.write_file(self.name_fname, data, binary=False)
Simon Glass2697b7c2021-11-23 21:08:58 -070094
95 def tearDown(self):
96 """Remove the temporary input directory and its contents"""
97 if self._indir:
98 shutil.rmtree(self._indir)
99 self._indir = None
Simon Glass80025522022-01-29 14:14:04 -0700100 tools.finalise_output_dir()
Simon Glass2697b7c2021-11-23 21:08:58 -0700101
102 def test_no_readme(self):
103 """Test handling of a missing readme.rst"""
104 with self.assertRaises(Exception) as err:
105 fip_util.main(self.args, self.src_file)
106 self.assertIn('Expected file', str(err.exception))
107
108 def test_invalid_readme(self):
109 """Test that an invalid readme.rst is detected"""
Simon Glass80025522022-01-29 14:14:04 -0700110 tools.write_file(self.readme, 'blah', binary=False)
Simon Glass2697b7c2021-11-23 21:08:58 -0700111 with self.assertRaises(Exception) as err:
112 fip_util.main(self.args, self.src_file)
113 self.assertIn('does not start with', str(err.exception))
114
115 def test_no_fip_h(self):
116 """Check handling of missing firmware_image_package.h"""
117 self.setup_readme()
118 with self.assertRaises(Exception) as err:
119 fip_util.main(self.args, self.src_file)
120 self.assertIn('No such file or directory', str(err.exception))
121
122 def test_invalid_fip_h(self):
123 """Check failure to parse firmware_image_package.h"""
124 self.setup_readme()
125 self.setup_macro('blah')
126 with self.assertRaises(Exception) as err:
127 fip_util.main(self.args, self.src_file)
128 self.assertIn('Cannot parse file', str(err.exception))
129
130 def test_parse_fip_h(self):
131 """Check parsing of firmware_image_package.h"""
132 self.setup_readme()
133 # Check parsing the header file
134 self.setup_macro()
135 macros = fip_util.parse_macros(self._indir)
136 expected_macros = {
137 'UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U':
138 ('ToC Entry UUIDs', 'UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U',
139 bytes([0x65, 0x92, 0x27, 0x03, 0x2f, 0x74, 0xe6, 0x44,
140 0x8d, 0xff, 0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10])),
141 'UUID_TRUSTED_UPDATE_FIRMWARE_BL2U':
142 ('ToC Entry UUIDs', 'UUID_TRUSTED_UPDATE_FIRMWARE_BL2U',
143 bytes([0x60, 0xb3, 0xeb, 0x37, 0xc1, 0xe5, 0xea, 0x41,
144 0x9d, 0xf3, 0x19, 0xed, 0xa1, 0x1f, 0x68, 0x01])),
145 }
146 self.assertEqual(expected_macros, macros)
147
148 def test_missing_tbbr_c(self):
149 """Check handlinh of missing tbbr_config.c"""
150 self.setup_readme()
151 self.setup_macro()
152
153 # Still need the .c file
154 with self.assertRaises(Exception) as err:
155 fip_util.main(self.args, self.src_file)
156 self.assertIn('tbbr_config.c', str(err.exception))
157
158 def test_invalid_tbbr_c(self):
159 """Check failure to parse tbbr_config.c"""
160 self.setup_readme()
161 self.setup_macro()
162 # Check invalid format for C file
163 self.setup_name('blah')
164 with self.assertRaises(Exception) as err:
165 fip_util.main(self.args, self.src_file)
166 self.assertIn('Cannot parse file', str(err.exception))
167
168 def test_inconsistent_tbbr_c(self):
169 """Check tbbr_config.c in a format we don't expect"""
170 self.setup_readme()
171 # This is missing a hex value
172 self.setup_macro('''
173
174/* ToC Entry UUIDs */
175#define UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U \\
176 {{0x65, 0x92, 0x27,}, {0x2f, 0x74}, {0xe6, 0x44}, 0x8d, 0xff, {0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10} }
177#define UUID_TRUSTED_UPDATE_FIRMWARE_BL2U \\
178 {{0x60, 0xb3, 0xeb, 0x37}, {0xc1, 0xe5}, {0xea, 0x41}, 0x9d, 0xf3, {0x19, 0xed, 0xa1, 0x1f, 0x68, 0x01} }
179
180''')
181 # Check invalid format for C file
182 self.setup_name('blah')
183 with self.assertRaises(Exception) as err:
184 fip_util.main(self.args, self.src_file)
185 self.assertIn('Cannot parse UUID line 5', str(err.exception))
186
187 def test_parse_tbbr_c(self):
188 """Check parsing tbbr_config.c"""
189 self.setup_readme()
190 self.setup_macro()
191 self.setup_name()
192
193 names = fip_util.parse_names(self._indir)
194
195 expected_names = {
196 'UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U': (
197 'SCP Firmware Updater Configuration FWU SCP_BL2U',
198 'UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U',
199 'scp-fwu-cfg'),
200 'UUID_TRUSTED_UPDATE_FIRMWARE_BL2U': (
201 'AP Firmware Updater Configuration BL2U',
202 'UUID_TRUSTED_UPDATE_FIRMWARE_BL2U',
203 'ap-fwu-cfg'),
204 }
205 self.assertEqual(expected_names, names)
206
207 def test_uuid_not_in_tbbr_config_c(self):
208 """Check handling a UUID in the header file that's not in the .c file"""
209 self.setup_readme()
210 self.setup_macro(self.macro_contents + '''
211#define UUID_TRUSTED_OS_FW_KEY_CERT \\
212 {{0x94, 0x77, 0xd6, 0x03}, {0xfb, 0x60}, {0xe4, 0x11}, 0x85, 0xdd, {0xb7, 0x10, 0x5b, 0x8c, 0xee, 0x04} }
213
214''')
215 self.setup_name()
216
217 macros = fip_util.parse_macros(self._indir)
218 names = fip_util.parse_names(self._indir)
Simon Glass14d64e32025-04-29 07:21:59 -0600219 with terminal.capture() as (stdout, _):
Simon Glass2697b7c2021-11-23 21:08:58 -0700220 fip_util.create_code_output(macros, names)
221 self.assertIn(
222 "UUID 'UUID_TRUSTED_OS_FW_KEY_CERT' is not mentioned in tbbr_config.c file",
223 stdout.getvalue())
224
225 def test_changes(self):
226 """Check handling of a source file that does/doesn't need changes"""
227 self.setup_readme()
228 self.setup_macro()
229 self.setup_name()
230
231 # Check generating the file when changes are needed
Simon Glass80025522022-01-29 14:14:04 -0700232 tools.write_file(self.src_file, '''
Simon Glass2697b7c2021-11-23 21:08:58 -0700233
234# This is taken from tbbr_config.c in ARM Trusted Firmware
235FIP_TYPE_LIST = [
236 # ToC Entry UUIDs
237 FipType('scp-fwu-cfg', 'SCP Firmware Updater Configuration FWU SCP_BL2U',
238 [0x65, 0x92, 0x27, 0x03, 0x2f, 0x74, 0xe6, 0x44,
239 0x8d, 0xff, 0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10]),
240 ] # end
241blah de blah
242 ''', binary=False)
Simon Glass14d64e32025-04-29 07:21:59 -0600243 with terminal.capture() as (stdout, _):
Simon Glass2697b7c2021-11-23 21:08:58 -0700244 fip_util.main(self.args, self.src_file)
245 self.assertIn('Needs update', stdout.getvalue())
246
247 # Check generating the file when no changes are needed
Simon Glass80025522022-01-29 14:14:04 -0700248 tools.write_file(self.src_file, '''
Simon Glass2697b7c2021-11-23 21:08:58 -0700249# This is taken from tbbr_config.c in ARM Trusted Firmware
250FIP_TYPE_LIST = [
251 # ToC Entry UUIDs
252 FipType('scp-fwu-cfg', 'SCP Firmware Updater Configuration FWU SCP_BL2U',
253 [0x65, 0x92, 0x27, 0x03, 0x2f, 0x74, 0xe6, 0x44,
254 0x8d, 0xff, 0x57, 0x9a, 0xc1, 0xff, 0x06, 0x10]),
255 FipType('ap-fwu-cfg', 'AP Firmware Updater Configuration BL2U',
256 [0x60, 0xb3, 0xeb, 0x37, 0xc1, 0xe5, 0xea, 0x41,
257 0x9d, 0xf3, 0x19, 0xed, 0xa1, 0x1f, 0x68, 0x01]),
258 ] # end
259blah blah''', binary=False)
Simon Glass14d64e32025-04-29 07:21:59 -0600260 with terminal.capture() as (stdout, _):
Simon Glass2697b7c2021-11-23 21:08:58 -0700261 fip_util.main(self.args, self.src_file)
262 self.assertIn('is up-to-date', stdout.getvalue())
263
264 def test_no_debug(self):
265 """Test running without the -D flag"""
266 self.setup_readme()
267 self.setup_macro()
268 self.setup_name()
269
270 args = self.args.copy()
271 args.remove('-D')
Simon Glass80025522022-01-29 14:14:04 -0700272 tools.write_file(self.src_file, '', binary=False)
Simon Glass14d64e32025-04-29 07:21:59 -0600273 with terminal.capture():
Simon Glass2697b7c2021-11-23 21:08:58 -0700274 fip_util.main(args, self.src_file)
275
276 @unittest.skipIf(not HAVE_FIPTOOL, 'No fiptool available')
277 def test_fiptool_list(self):
278 """Create a FIP and check that fiptool can read it"""
279 fwu = b'my data'
280 tb_fw = b'some more data'
281 fip = fip_util.FipWriter(0x123, 0x10)
282 fip.add_entry('fwu', fwu, 0x456)
283 fip.add_entry('tb-fw', tb_fw, 0)
284 fip.add_entry(bytes(range(16)), tb_fw, 0)
285 data = fip.get_data()
Simon Glass80025522022-01-29 14:14:04 -0700286 fname = tools.get_output_filename('data.fip')
287 tools.write_file(fname, data)
Simon Glassb2833222022-01-09 20:13:59 -0700288 result = FIPTOOL.info(fname)
Simon Glass2697b7c2021-11-23 21:08:58 -0700289 self.assertEqual(
290 '''Firmware Updater NS_BL2U: offset=0xB0, size=0x7, cmdline="--fwu"
291Trusted Boot Firmware BL2: offset=0xC0, size=0xE, cmdline="--tb-fw"
29200010203-0405-0607-0809-0A0B0C0D0E0F: offset=0xD0, size=0xE, cmdline="--blob"
293''',
Simon Glassb2833222022-01-09 20:13:59 -0700294 result)
Simon Glass2697b7c2021-11-23 21:08:58 -0700295
296 fwu_data = b'my data'
297 tb_fw_data = b'some more data'
298 other_fw_data = b'even more'
299
300 def create_fiptool_image(self):
301 """Create an image with fiptool which we can use for testing
302
303 Returns:
304 FipReader: reader for the image
305 """
306 fwu = os.path.join(self._indir, 'fwu')
Simon Glass80025522022-01-29 14:14:04 -0700307 tools.write_file(fwu, self.fwu_data)
Simon Glass2697b7c2021-11-23 21:08:58 -0700308
309 tb_fw = os.path.join(self._indir, 'tb_fw')
Simon Glass80025522022-01-29 14:14:04 -0700310 tools.write_file(tb_fw, self.tb_fw_data)
Simon Glass2697b7c2021-11-23 21:08:58 -0700311
312 other_fw = os.path.join(self._indir, 'other_fw')
Simon Glass80025522022-01-29 14:14:04 -0700313 tools.write_file(other_fw, self.other_fw_data)
Simon Glass2697b7c2021-11-23 21:08:58 -0700314
Simon Glass80025522022-01-29 14:14:04 -0700315 fname = tools.get_output_filename('data.fip')
Simon Glass2697b7c2021-11-23 21:08:58 -0700316 uuid = 'e3b78d9e-4a64-11ec-b45c-fba2b9b49788'
Simon Glassb2833222022-01-09 20:13:59 -0700317 FIPTOOL.create_new(fname, 8, 0x123, fwu, tb_fw, uuid, other_fw)
Simon Glass2697b7c2021-11-23 21:08:58 -0700318
Simon Glass80025522022-01-29 14:14:04 -0700319 return fip_util.FipReader(tools.read_file(fname))
Simon Glass2697b7c2021-11-23 21:08:58 -0700320
321 @unittest.skipIf(not HAVE_FIPTOOL, 'No fiptool available')
322 def test_fiptool_create(self):
323 """Create a FIP with fiptool and check that fip_util can read it"""
324 reader = self.create_fiptool_image()
325
326 header = reader.header
327 fents = reader.fents
328
329 self.assertEqual(0x123 << 32, header.flags)
330 self.assertEqual(fip_util.HEADER_MAGIC, header.name)
331 self.assertEqual(fip_util.HEADER_SERIAL, header.serial)
332
333 self.assertEqual(3, len(fents))
334 fent = fents[0]
335 self.assertEqual(
336 bytes([0x4f, 0x51, 0x1d, 0x11, 0x2b, 0xe5, 0x4e, 0x49,
337 0xb4, 0xc5, 0x83, 0xc2, 0xf7, 0x15, 0x84, 0x0a]), fent.uuid)
338 self.assertEqual(0xb0, fent.offset)
339 self.assertEqual(len(self.fwu_data), fent.size)
340 self.assertEqual(0, fent.flags)
341 self.assertEqual(self.fwu_data, fent.data)
342
343 fent = fents[1]
344 self.assertEqual(
345 bytes([0x5f, 0xf9, 0xec, 0x0b, 0x4d, 0x22, 0x3e, 0x4d,
346 0xa5, 0x44, 0xc3, 0x9d, 0x81, 0xc7, 0x3f, 0x0a]), fent.uuid)
347 self.assertEqual(0xb8, fent.offset)
348 self.assertEqual(len(self.tb_fw_data), fent.size)
349 self.assertEqual(0, fent.flags)
350 self.assertEqual(self.tb_fw_data, fent.data)
351
352 fent = fents[2]
353 self.assertEqual(
354 bytes([0xe3, 0xb7, 0x8d, 0x9e, 0x4a, 0x64, 0x11, 0xec,
355 0xb4, 0x5c, 0xfb, 0xa2, 0xb9, 0xb4, 0x97, 0x88]), fent.uuid)
356 self.assertEqual(0xc8, fent.offset)
357 self.assertEqual(len(self.other_fw_data), fent.size)
358 self.assertEqual(0, fent.flags)
359 self.assertEqual(self.other_fw_data, fent.data)
360
361 @unittest.skipIf(not HAVE_FIPTOOL, 'No fiptool available')
362 def test_reader_get_entry(self):
363 """Test get_entry() by name and UUID"""
364 reader = self.create_fiptool_image()
365 fents = reader.fents
366 fent = reader.get_entry('fwu')
367 self.assertEqual(fent, fents[0])
368
369 fent = reader.get_entry(
370 bytes([0x5f, 0xf9, 0xec, 0x0b, 0x4d, 0x22, 0x3e, 0x4d,
371 0xa5, 0x44, 0xc3, 0x9d, 0x81, 0xc7, 0x3f, 0x0a]))
372 self.assertEqual(fent, fents[1])
373
374 # Try finding entries that don't exist
375 with self.assertRaises(Exception) as err:
376 fent = reader.get_entry('scp-fwu-cfg')
377 self.assertIn("Cannot find FIP entry 'scp-fwu-cfg'", str(err.exception))
378
379 with self.assertRaises(Exception) as err:
380 fent = reader.get_entry(bytes(list(range(16))))
381 self.assertIn(
382 "Cannot find FIP entry '00010203-0405-0607-0809-0a0b0c0d0e0f'",
383 str(err.exception))
384
385 with self.assertRaises(Exception) as err:
386 fent = reader.get_entry('blah')
387 self.assertIn("Unknown FIP entry type 'blah'", str(err.exception))
388
389 @unittest.skipIf(not HAVE_FIPTOOL, 'No fiptool available')
390 def test_fiptool_errors(self):
391 """Check some error reporting from fiptool"""
392 with self.assertRaises(Exception) as err:
Simon Glass14d64e32025-04-29 07:21:59 -0600393 with terminal.capture():
Simon Glassb2833222022-01-09 20:13:59 -0700394 FIPTOOL.create_bad()
395 self.assertIn("unrecognized option '--fred'", str(err.exception))
Simon Glass2697b7c2021-11-23 21:08:58 -0700396
397
398if __name__ == '__main__':
399 unittest.main()