OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 2 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 | 6 |
7 import hashlib | 7 import hashlib |
8 import optparse | 8 import optparse |
9 import os | 9 import os |
10 import sys | 10 import sys |
11 import tempfile | 11 import tempfile |
12 | 12 |
13 | 13 |
14 # This file may be shared by autotest framework and some command line tools | 14 # This file may be shared by autotest framework and some command line tools |
15 # setting PYTHONPATH, so we need to try different importing paths here. | 15 # setting PYTHONPATH, so we need to try different importing paths here. |
16 try: | 16 try: |
17 from autotest_lib.client.common_lib import flashrom_util | 17 from autotest_lib.client.cros import flashrom_util |
18 from autotest_lib.client.common_lib import site_fmap | 18 from autotest_lib.client.cros import fmap |
19 except ImportError: | 19 except ImportError: |
20 # try to load from pre-defined PYTHONPATH | 20 # try to load from pre-defined PYTHONPATH |
21 import flashrom_util | 21 import flashrom_util |
22 import site_fmap | 22 import fmap |
23 | 23 |
24 | 24 |
25 def get_bios_ro_hash(file_source=None, exception_type=Exception): | 25 def get_bios_ro_hash(file_source=None, exception_type=Exception): |
26 """ | 26 """ |
27 Returns a hash of Read Only (BIOS) firmware parts, | 27 Returns a hash of Read Only (BIOS) firmware parts, |
28 to confirm we have proper keys / boot code / recovery image installed. | 28 to confirm we have proper keys / boot code / recovery image installed. |
29 | 29 |
30 Args: | 30 Args: |
31 file_source: None to read BIOS from system flash rom, or any string | 31 file_source: None to read BIOS from system flash rom, or any string |
32 value as the file name of firmware image to read. | 32 value as the file name of firmware image to read. |
33 """ | 33 """ |
34 # hash_ro_list: RO section to be hashed | 34 # hash_ro_list: RO section to be hashed |
35 hash_src = '' | 35 hash_src = '' |
36 hash_ro_list = ['FV_BSTUB', 'FV_GBB', 'FVDEV'] | 36 hash_ro_list = ['FV_BSTUB', 'FV_GBB', 'FVDEV'] |
37 hash_ro_section = 'RO_SECTION' | 37 hash_ro_section = 'RO_SECTION' |
38 | 38 |
39 flashrom = flashrom_util.FlashromUtility() | 39 flashrom = flashrom_util.FlashromUtility() |
40 flashrom.initialize(flashrom.TARGET_BIOS, target_file=file_source) | 40 flashrom.initialize(flashrom.TARGET_BIOS, target_file=file_source) |
41 | 41 |
42 image = flashrom.get_current_image() | 42 image = flashrom.get_current_image() |
43 fmap_obj = site_fmap.fmap_decode(image) | 43 fmap_obj = fmap.fmap_decode(image) |
44 if not fmap_obj: | 44 if not fmap_obj: |
45 raise exception_type('No FMAP structure in flashrom.') | 45 raise exception_type('No FMAP structure in flashrom.') |
46 | 46 |
47 # XXX Allowing the FMAP to override our default layout may be an exploit | 47 # XXX Allowing the FMAP to override our default layout may be an exploit |
48 # here, because vendor can provide fake (non-used) GBB/BSTUB in unused | 48 # here, because vendor can provide fake (non-used) GBB/BSTUB in unused |
49 # area. However since the flash memory layout may change, we need to | 49 # area. However since the flash memory layout may change, we need to |
50 # trust FMAP here. | 50 # trust FMAP here. |
51 # TODO(hungte) we can check that FMAP must reside in RO section, and the | 51 # TODO(hungte) we can check that FMAP must reside in RO section, and the |
52 # BSTUB must be aligned to bottom of firmware. | 52 # BSTUB must be aligned to bottom of firmware. |
53 hash_src = hash_src + site_fmap.fmap_encode(fmap_obj) | 53 hash_src = hash_src + fmap.fmap_encode(fmap_obj) |
54 | 54 |
55 # New firmware spec defined new "RO_SECTION" which includes all sections to | 55 # New firmware spec defined new "RO_SECTION" which includes all sections to |
56 # be hashed. When that section exists, we should override hash_ro_list. | 56 # be hashed. When that section exists, we should override hash_ro_list. |
57 if hash_ro_section in flashrom.layout: | 57 if hash_ro_section in flashrom.layout: |
58 hash_ro_list = [hash_ro_section] | 58 hash_ro_list = [hash_ro_section] |
59 | 59 |
60 for section in hash_ro_list: | 60 for section in hash_ro_list: |
61 src = flashrom.read_section(section) | 61 src = flashrom.read_section(section) |
62 if not src: | 62 if not src: |
63 raise exception_type('Cannot get section [%s] from flashrom' % | 63 raise exception_type('Cannot get section [%s] from flashrom' % |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 elif target == 'ec': | 148 elif target == 'ec': |
149 print get_ec_hash(image) | 149 print get_ec_hash(image) |
150 | 150 |
151 # Remove the temporary GBB-modified file. | 151 # Remove the temporary GBB-modified file. |
152 if modified_image: | 152 if modified_image: |
153 os.remove(modified_image) | 153 os.remove(modified_image) |
154 | 154 |
155 | 155 |
156 if __name__ == "__main__": | 156 if __name__ == "__main__": |
157 main() | 157 main() |
OLD | NEW |