blob: 07bf0fd52113b9a82834cbb727234b35d8c65f23 [file] [log] [blame]
Alexandru Gagniuc16521e72021-09-15 14:33:01 -05001# SPDX-License-Identifier: GPL-2.0+
2#
3# Copyright (c) 2021 Alexandru Gagniuc <mr.nuke.me@gmail.com>
4
5"""
6Check hashes produced by mkimage against known values
7
8This test checks the correctness of mkimage's hashes. by comparing the mkimage
9output of a fixed data block with known good hashes.
10This test doesn't run the sandbox. It only checks the host tool 'mkimage'
11"""
12
Simon Glass998a2f22022-08-06 17:51:49 -060013import os
Alexandru Gagniuc16521e72021-09-15 14:33:01 -050014import pytest
Simon Glassdb0e4532025-02-09 09:07:16 -070015import utils
Alexandru Gagniuc16521e72021-09-15 14:33:01 -050016
17kernel_hashes = {
18 "sha512" : "f18c1486a2c29f56360301576cdfce4dfd8e8e932d0ed8e239a1f314b8ae1d77b2a58cd7fe32e4075e69448e623ce53b0b6aa6ce5626d2c189a5beae29a68d93",
19 "sha384" : "16e28976740048485d08d793d8bf043ebc7826baf2bc15feac72825ad67530ceb3d09e0deb6932c62a5a0e9f3936baf4",
20 "sha256" : "2955c56bc1e5050c111ba6e089e0f5342bb47dedf77d87e3f429095feb98a7e5",
21 "sha1" : "652383e1a6d946953e1f65092c9435f6452c2ab7",
22 "md5" : "4879e5086e4c76128e525b5fe2af55f1",
23 "crc32" : "32eddfdf",
24 "crc16-ccitt" : "d4be"
25}
26
27class ReadonlyFitImage(object):
28 """ Helper to manipulate a FIT image on disk """
Simon Glass32701112025-02-09 09:07:17 -070029 def __init__(self, ubman, file_name):
Alexandru Gagniuc16521e72021-09-15 14:33:01 -050030 self.fit = file_name
Simon Glass32701112025-02-09 09:07:17 -070031 self.ubman = ubman
Alexandru Gagniuc16521e72021-09-15 14:33:01 -050032 self.hashable_nodes = set()
33
34 def __fdt_list(self, path):
Simon Glass32701112025-02-09 09:07:17 -070035 return utils.run_and_log(self.ubman, f'fdtget -l {self.fit} {path}')
Alexandru Gagniuc16521e72021-09-15 14:33:01 -050036
37 def __fdt_get(self, node, prop):
Simon Glass32701112025-02-09 09:07:17 -070038 val = utils.run_and_log(self.ubman, f'fdtget {self.fit} {node} {prop}')
Alexandru Gagniuc16521e72021-09-15 14:33:01 -050039 return val.rstrip('\n')
40
41 def __fdt_get_sexadecimal(self, node, prop):
Simon Glass32701112025-02-09 09:07:17 -070042 numbers = utils.run_and_log(self.ubman,
Simon Glassdb0e4532025-02-09 09:07:16 -070043 f'fdtget -tbx {self.fit} {node} {prop}')
Alexandru Gagniuc16521e72021-09-15 14:33:01 -050044
45 sexadecimal = ''
46 for num in numbers.rstrip('\n').split(' '):
47 sexadecimal += num.zfill(2)
48 return sexadecimal
49
50 def find_hashable_image_nodes(self):
51 for node in self.__fdt_list('/images').split():
52 # We only have known hashes for the kernel node
53 if 'kernel' not in node:
54 continue
55 self.hashable_nodes.add(f'/images/{node}')
56
57 return self.hashable_nodes
58
59 def verify_hashes(self):
60 for image in self.hashable_nodes:
61 algos = set()
62 for node in self.__fdt_list(image).split():
63 if "hash-" not in node:
64 continue
65
66 raw_hash = self.__fdt_get_sexadecimal(f'{image}/{node}', 'value')
67 algo = self.__fdt_get(f'{image}/{node}', 'algo')
68 algos.add(algo)
69
70 good_hash = kernel_hashes[algo]
71 if good_hash != raw_hash:
72 raise ValueError(f'{image} Borked hash: {algo}');
73
74 # Did we test all the hashes we set out to test?
75 missing_algos = kernel_hashes.keys() - algos
76 if (missing_algos):
77 raise ValueError(f'Missing hashes from FIT: {missing_algos}')
78
79
80@pytest.mark.buildconfigspec('hash')
81@pytest.mark.requiredtool('dtc')
82@pytest.mark.requiredtool('fdtget')
83@pytest.mark.requiredtool('fdtput')
Simon Glassddba5202025-02-09 09:07:14 -070084def test_mkimage_hashes(ubman):
Alexandru Gagniuc16521e72021-09-15 14:33:01 -050085 """ Test that hashes generated by mkimage are correct. """
86
87 def assemble_fit_image(dest_fit, its, destdir):
88 dtc_args = f'-I dts -O dtb -i {destdir}'
Simon Glass32701112025-02-09 09:07:17 -070089 utils.run_and_log(ubman, [mkimage, '-D', dtc_args, '-f', its, dest_fit])
Alexandru Gagniuc16521e72021-09-15 14:33:01 -050090
91 def dtc(dts):
92 dtb = dts.replace('.dts', '.dtb')
Simon Glass32701112025-02-09 09:07:17 -070093 utils.run_and_log(ubman,
Simon Glassdb0e4532025-02-09 09:07:16 -070094 f'dtc {datadir}/{dts} -O dtb -o {tempdir}/{dtb}')
Alexandru Gagniuc16521e72021-09-15 14:33:01 -050095
Simon Glass32701112025-02-09 09:07:17 -070096 mkimage = ubman.config.build_dir + '/tools/mkimage'
97 datadir = ubman.config.source_dir + '/test/py/tests/vboot/'
98 tempdir = os.path.join(ubman.config.result_dir, 'hashes')
Simon Glass998a2f22022-08-06 17:51:49 -060099 os.makedirs(tempdir, exist_ok=True)
100
Alexandru Gagniuc16521e72021-09-15 14:33:01 -0500101 fit_file = f'{tempdir}/test.fit'
102 dtc('sandbox-kernel.dts')
103
104 # Create a fake kernel image -- Avoid zeroes or crc16 will be zero
105 with open(f'{tempdir}/test-kernel.bin', 'w') as fd:
106 fd.write(500 * chr(0xa5))
107
108 assemble_fit_image(fit_file, f'{datadir}/hash-images.its', tempdir)
109
Simon Glass32701112025-02-09 09:07:17 -0700110 fit = ReadonlyFitImage(ubman, fit_file)
Alexandru Gagniuc16521e72021-09-15 14:33:01 -0500111 nodes = fit.find_hashable_image_nodes()
112 if len(nodes) == 0:
113 raise ValueError('FIT image has no "/image" nodes with "hash-..."')
114
115 fit.verify_hashes()