OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Google Factory Tool: Write Protection for Firmware | 7 """Google Factory Tool: Write Protection for Firmware |
8 | 8 |
9 This module enables or verifies that firmware write protection is properly | 9 This module enables or verifies that firmware write protection is properly |
10 activated. | 10 activated. |
11 | 11 |
12 WARNING: THE RO SECTIONS OF YOUR EEPROM WILL BECOME READONLY AFTER RUNNING THIS. | 12 WARNING: THE RO SECTIONS OF YOUR EEPROM WILL BECOME READONLY AFTER RUNNING THIS. |
13 """ | 13 """ |
14 | 14 |
15 import sys | 15 import sys |
16 | 16 |
17 import flashrom_util | 17 import flashrom_util |
18 import gft_common | 18 import gft_common |
19 | 19 |
20 from gft_common import DebugMsg, VerboseMsg, ErrorDie | 20 from gft_common import DebugMsg, VerboseMsg, ErrorDie |
21 | 21 |
22 | 22 |
23 def EnableWriteProtect(target): | 23 def EnableWriteProtect(target, image=None): |
24 """ Enables and verifies firmware write protection status. | 24 """ Enables and verifies firmware write protection status. |
25 ARGS | 25 ARGS |
26 target: the flashrom_util target code, 'bios' or 'ec'. | 26 target: the flashrom_util target code, 'bios' or 'ec'. |
27 verbose: verbosity for flashrom_util. | 27 image: a reference image for layout detection. |
28 """ | 28 """ |
29 | 29 |
30 flashrom = flashrom_util.flashrom_util(verbose_msg=VerboseMsg, | 30 flashrom = flashrom_util.flashrom_util(verbose_msg=VerboseMsg, |
31 exception_type=gft_common.GFTError, | 31 exception_type=gft_common.GFTError, |
32 system_output=gft_common.SystemOutput) | 32 system_output=gft_common.SystemOutput) |
33 | 33 |
34 # The EEPROM should be programmed as: | 34 # The EEPROM should be programmed as: |
| 35 # x86: |
35 # (BIOS) LSB [ RW | RO ] MSB | 36 # (BIOS) LSB [ RW | RO ] MSB |
36 # (EC) LSB [ RO | RW ] MSB | 37 # (EC) LSB [ RO | RW ] MSB |
| 38 # arm: |
| 39 # (BIOS) LSB [ RO | RW ] MSB |
37 # Each part of RW/RO section occupies half of the EEPROM. | 40 # Each part of RW/RO section occupies half of the EEPROM. |
| 41 # To simplify the arch detection, we trust the FMAP. |
| 42 |
| 43 layout_rw_ro = 'rw|ro' |
| 44 layout_ro_rw = 'ro|rw' |
38 | 45 |
39 eeprom_sets = ({ # BIOS | 46 eeprom_sets = ({ # BIOS |
40 'name': 'BIOS', | 47 'name': 'BIOS', |
41 'layout': 'rw|ro', | 48 'layout': layout_rw_ro, |
42 'target': 'bios', | 49 'target': 'bios', |
43 'trust_fmap': False, | 50 'fmap_conversion': {'RO_SECTION': 'ro'}, |
44 }, { # Embedded Controller | 51 }, { # Embedded Controller |
45 'name': 'EC', | 52 'name': 'EC', |
46 'layout': 'ro|rw', | 53 'layout': layout_ro_rw, |
47 'target': 'ec', | 54 'target': 'ec', |
48 'trust_fmap': True, | 55 'fmap_conversion': {'EC_RO': 'ro'}, |
49 'fmap_conversion': {'EC_RO': 'ro', 'EC_RW': 'rw'}, | |
50 }) | 56 }) |
51 | 57 |
52 # TODO(hungte) BIOS on ARM is using different layout, | |
53 # so we must also trust fmap for BIOS in the future. | |
54 matched = [eeprom for eeprom in eeprom_sets | 58 matched = [eeprom for eeprom in eeprom_sets |
55 if eeprom['target'] == target] | 59 if eeprom['target'] == target] |
56 if not matched: | 60 if not matched: |
57 ErrorDie('enable_write_protect: unknown target: ' + target) | 61 ErrorDie('enable_write_protect: unknown target: ' + target) |
58 conf = matched[0] | 62 conf = matched[0] |
59 name = conf['name'] | 63 name = conf['name'] |
60 | 64 |
61 # select target | 65 # select target |
62 if not flashrom.select_target(target): | 66 if not flashrom.select_target(target): |
63 ErrorDie('wpfw: Cannot select target ' + name) | 67 ErrorDie('wpfw: Cannot select target ' + name) |
64 eeprom_size = flashrom.get_size() | 68 eeprom_size = flashrom.get_size() |
65 | 69 |
66 # build layout | 70 # build layout |
67 if not eeprom_size: | 71 if not eeprom_size: |
68 ErrorDie('wpfw: cannot get size for ' + name) | 72 ErrorDie('wpfw: cannot get size for ' + name) |
69 layout = None | 73 layout = None |
70 if conf['trust_fmap']: | 74 layout_desc = conf['layout'] |
71 DebugMsg(' - Trying to use FMAP layout for %s' % name) | 75 DebugMsg(' - Trying to use FMAP layout for %s' % name) |
| 76 if not image: |
72 image = flashrom.read_whole() | 77 image = flashrom.read_whole() |
73 assert eeprom_size == len(image) | 78 assert eeprom_size == len(image) |
74 layout = flashrom_util.decode_fmap_layout(conf['fmap_conversion'], image) | 79 fmap_layout = flashrom_util.decode_fmap_layout(conf['fmap_conversion'], image) |
75 if 'ro' not in layout: | 80 |
76 layout = None | 81 if 'ro' in fmap_layout: |
| 82 # Verify if the layout is in valid size. Most chips support only setting |
| 83 # write protection on half of the total size, so we always devide the |
| 84 # flashrom into 2 slots. |
| 85 slot_size = int(eeprom_size / 2) |
| 86 # fmap_layout is a (offset, offset) structure, not (offset, size) |
| 87 (ro_begin_offset, ro_end_offset) = fmap_layout['ro'] |
| 88 ro_begin_slot = int(ro_begin_offset / slot_size) |
| 89 ro_end_slot = int(ro_end_offset / slot_size) |
| 90 error_reason = None |
| 91 if ro_begin_slot != ro_end_slot: |
| 92 error_reason = 'section accross valid slot range' |
| 93 # flashrom layout does not really support a section of '1 byte in size'. |
| 94 # Both 0/1 sized sections are invalid. |
| 95 if ro_begin_offset >= ro_end_offset: |
| 96 error_reason = 'invalid section size' |
| 97 if error_reason: |
| 98 ErrorDie('Invalid RO section in layout: %s (%s,%s)' % |
| 99 (error_reason, ro_begin_offset, ro_end_offset)) |
| 100 assert ro_begin_slot == 0 or ro_begin_slot == 1 |
| 101 |
| 102 # decide ro, rw according to the offset of 'ro'. |
| 103 if ro_begin_slot == 0: |
| 104 layout_desc = layout_ro_rw |
77 else: | 105 else: |
78 VerboseMsg(' - Using layout by FMAP in %s' % name) | 106 layout_desc = layout_rw_ro |
| 107 VerboseMsg(' - Using layout by FMAP in %s: %s' % (name, layout_desc)) |
| 108 else: |
| 109 VerboseMsg(' - Using hard-coded layout for %s: %s' % name, layout_desc) |
79 | 110 |
80 if not layout: | 111 layout = flashrom.detect_layout(layout_desc, eeprom_size, None) |
81 # do not trust current image when detecting layout. | |
82 layout = flashrom.detect_layout(conf['layout'], eeprom_size, None) | |
83 VerboseMsg(' - Using hard-coded layout for %s' % name) | |
84 if not layout: | 112 if not layout: |
85 ErrorDie('wpfw: cannot detect %s layout' % name) | 113 ErrorDie('wpfw: cannot detect %s layout' % name) |
86 | 114 |
87 # verify if the layout is half of firmware. | |
88 if layout['ro'][1] - layout['ro'][0] + 1 != eeprom_size / 2: | |
89 ErrorDie('Invalid RO section in flash rom layout.') | |
90 | |
91 VerboseMsg(' - Enable Write Protection for ' + name) | 115 VerboseMsg(' - Enable Write Protection for ' + name) |
92 # only configure (enable) write protection if current status is not | 116 # only configure (enable) write protection if current status is not |
93 # correct, because sometimes the factory test is executed several | 117 # correct, because sometimes the factory test is executed several |
94 # times without resetting WP status. | 118 # times without resetting WP status. |
95 if not flashrom.verify_write_protect(layout, 'ro'): | 119 if not flashrom.verify_write_protect(layout, 'ro'): |
96 if not (flashrom.enable_write_protect(layout, 'ro') | 120 if not (flashrom.enable_write_protect(layout, 'ro') |
97 and flashrom.verify_write_protect(layout, 'ro')): | 121 and flashrom.verify_write_protect(layout, 'ro')): |
98 ErrorDie('wpfw: cannot enable write protection for ' + name) | 122 ErrorDie('wpfw: cannot enable write protection for ' + name) |
99 VerboseMsg(' - Check Write Protection for ' + name) | 123 VerboseMsg(' - Check Write Protection for ' + name) |
100 flashrom.disable_write_protect() | 124 flashrom.disable_write_protect() |
(...skipping 16 matching lines...) Expand all Loading... |
117 print 'Usage: %s target_code(bios/ec)\n' % sys.argv[0] | 141 print 'Usage: %s target_code(bios/ec)\n' % sys.argv[0] |
118 sys.exit(1) | 142 sys.exit(1) |
119 | 143 |
120 gft_common.SetDebugLevel(True) | 144 gft_common.SetDebugLevel(True) |
121 gft_common.SetVerboseLevel(True) | 145 gft_common.SetVerboseLevel(True) |
122 EnableWriteProtect(sys.argv[1]) | 146 EnableWriteProtect(sys.argv[1]) |
123 | 147 |
124 | 148 |
125 if __name__ == '__main__': | 149 if __name__ == '__main__': |
126 _main() | 150 _main() |
OLD | NEW |