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 """Provides convenience routines to access the GBB on the current BIOS. | 6 """Provides convenience routines to access the GBB on the current BIOS. |
7 | 7 |
8 GBBUtility is a wrapper of gbb_utility program. | 8 GBBUtility is a wrapper of gbb_utility program. |
9 """ | 9 """ |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 It accesses the GBB on the current BIOS image. | 22 It accesses the GBB on the current BIOS image. |
23 """ | 23 """ |
24 def __init__(self, | 24 def __init__(self, |
25 gbb_command='gbb_utility', | 25 gbb_command='gbb_utility', |
26 temp_dir=None, | 26 temp_dir=None, |
27 keep_temp_files=False): | 27 keep_temp_files=False): |
28 self._gbb_command = gbb_command | 28 self._gbb_command = gbb_command |
29 self._temp_dir = temp_dir | 29 self._temp_dir = temp_dir |
30 self._keep_temp_files = keep_temp_files | 30 self._keep_temp_files = keep_temp_files |
31 self._bios_file = None | 31 self._need_commit = False |
| 32 self._clear_cached() |
32 | 33 |
33 | 34 |
34 def __del__(self): | 35 def __del__(self): |
35 if self._bios_file: | 36 if self._gbb_file: |
36 self._remove_temp_file(self._bios_file) | 37 self._remove_temp_file(self._gbb_file) |
| 38 if self._need_commit: |
| 39 raise error.TestError( |
| 40 'You changed somethings; should commit or discard them.') |
37 | 41 |
38 | 42 |
39 def _get_temp_filename(self, prefix='tmp'): | 43 def _clear_cached(self): |
| 44 if self._gbb_file: |
| 45 self._remove_temp_file(self._gbb_file) |
| 46 self._gbb_file = None |
| 47 self._bmpfv = None |
| 48 self._recoverykey = None |
| 49 self._rootkey = None |
| 50 self._hwid = None |
| 51 |
| 52 |
| 53 def _get_temp_filename(self, prefix='tmp_'): |
40 """Returns the name of a temporary file in self._temp_dir.""" | 54 """Returns the name of a temporary file in self._temp_dir.""" |
41 (fd, name) = tempfile.mkstemp(prefix=prefix, dir=self._temp_dir) | 55 (fd, name) = tempfile.mkstemp(prefix=prefix, dir=self._temp_dir) |
42 os.close(fd) | 56 os.close(fd) |
43 return name | 57 return name |
44 | 58 |
45 | 59 |
46 def _remove_temp_file(self, filename): | 60 def _remove_temp_file(self, filename): |
47 """Removes a temporary file if self._keep_temp_files is false.""" | 61 """Removes a temporary file if self._keep_temp_files is false.""" |
48 if self._keep_temp_files: | 62 if self._keep_temp_files: |
49 return | 63 return |
50 if os.path.exists(filename): | 64 if os.path.exists(filename): |
51 os.remove(filename) | 65 os.remove(filename) |
52 | 66 |
53 | 67 |
54 def _read_bios(self, force=False): | 68 def _get_current_gbb_file(self): |
55 """Reads the BIOS to a file, self._bios_file.""" | 69 """Gets the GBB in BIOS to a file, self._gbb_file.""" |
56 if not self._bios_file or force: | 70 if not self._gbb_file: |
57 flashrom = flashrom_util.flashrom_util() | 71 flashrom = flashrom_util.FlashromUtility() |
58 if not flashrom.select_bios_flashrom(): | 72 flashrom.initialize(flashrom.TARGET_BIOS) |
59 raise error.TestError('Unable to select BIOS flashrom') | 73 |
60 bios_file = self._get_temp_filename('bios') | 74 gbb_data = flashrom.read_section('FV_GBB') |
61 if not flashrom.read_whole_to_file(bios_file): | 75 gbb_file = self._get_temp_filename('current_gbb_') |
62 raise error.TestError('Unable to read the BIOS image') | 76 utils.open_write_close(gbb_file, gbb_data) |
63 self._bios_file = bios_file | 77 self._gbb_file = gbb_file |
| 78 |
| 79 return self._gbb_file |
64 | 80 |
65 | 81 |
66 def _run_gbb_utility(self, args='', output_file=''): | 82 def _run_gbb_utility(self, args, output_file=''): |
67 """Runs gbb_utility on the current BIOS firmware data.""" | 83 """Runs gbb_utility on the current BIOS firmware data.""" |
68 self._read_bios() | 84 gbb_file = self._get_current_gbb_file() |
69 cmd = 'gbb_utility %s "%s" "%s"' % (args, self._bios_file, | 85 cmd = 'gbb_utility %s "%s" "%s"' % (args, gbb_file, output_file) |
70 output_file) | |
71 result = utils.system_output(cmd) | 86 result = utils.system_output(cmd) |
72 return result | 87 return result |
73 | 88 |
74 | 89 |
75 def _get_gbb_value(self, key): | 90 def _get_gbb_value(self, key): |
76 """Gets the GBB value which needs to be output to a file in gbb_utility. | 91 """Gets the GBB value which needs to be output to a file in gbb_utility. |
77 | 92 |
78 @param key: The key of the value you want to get. Should be 'bmfv', | 93 @param key: The key of the value you want to get. Should be 'bmfv', |
79 'recoverykey', or 'rootkey'. | 94 'recoverykey', or 'rootkey'. |
80 @return: The returned GBB value. | 95 @return: The returned GBB value. |
81 """ | 96 """ |
82 value_file = self._get_temp_filename(key) | 97 value_file = self._get_temp_filename('get_%s_' % key) |
83 self._run_gbb_utility('--get --%s=%s' % (key, value_file)) | 98 self._run_gbb_utility('--get --%s=%s' % (key, value_file)) |
84 with open(value_file, 'rb') as f: | 99 value = utils.read_file(value_file) |
85 value = f.read() | |
86 self._remove_temp_file(value_file) | 100 self._remove_temp_file(value_file) |
87 return value | 101 return value |
88 | 102 |
89 | 103 |
90 def get_bmpfv(self): | 104 def get_bmpfv(self): |
91 return self._get_gbb_value('bmpfv') | 105 if not self._bmpfv: |
| 106 self._bmpfv = self._get_gbb_value('bmpfv') |
| 107 return self._bmpfv |
92 | 108 |
93 | 109 |
94 def get_hwid(self): | 110 def get_hwid(self): |
95 result = _self._run_gbb_utility(self, '--get --hwid') | 111 if not self._hwid: |
96 return result.strip().partition('hardware_id: ')[2] | 112 result = _self._run_gbb_utility(self, '--get --hwid') |
| 113 self._hwid = result.strip().partition('hardware_id: ')[2] |
| 114 return self._hwid |
97 | 115 |
98 | 116 |
99 def get_recoverykey(self): | 117 def get_recoverykey(self): |
100 return self._get_gbb_value('recoverykey') | 118 if not self._recoverykey: |
| 119 self._recoverykey = self._get_gbb_value('recoverykey') |
| 120 return self._recoverykey |
101 | 121 |
102 | 122 |
103 def get_rootkey(self): | 123 def get_rootkey(self): |
104 return self._get_gbb_value('rootkey') | 124 if not self._rootkey: |
| 125 self._rootkey = self._get_gbb_value('rootkey') |
| 126 return self._rootkey |
| 127 |
| 128 |
| 129 def set_bmpfv(self, bmpfv): |
| 130 self._bmpfv = bmpfv |
| 131 self._need_commit = True |
| 132 |
| 133 |
| 134 def set_hwid(self, hwid): |
| 135 self._hwid = hwid |
| 136 self._need_commit = True |
| 137 |
| 138 |
| 139 def set_recoverykey(self, recoverykey): |
| 140 self._recoverykey = recoverykey |
| 141 self._need_commit = True |
| 142 |
| 143 |
| 144 def set_rootkey(self, rootkey): |
| 145 self._rootkey = rootkey |
| 146 self._need_commit = True |
| 147 |
| 148 |
| 149 def commit(self): |
| 150 """Commit all changes to the current BIOS.""" |
| 151 if self._need_commit: |
| 152 args = '--set' |
| 153 if self._bmpfv: |
| 154 bmpfv_file = self._get_temp_filename('set_bmpfv_') |
| 155 utils.open_write_close(bmpfv_file, self._bmpfv) |
| 156 args += ' --bmpfv=%s' % bmpfv_file |
| 157 if self._hwid: |
| 158 args += ' --hwid="%s"' % self._hwid |
| 159 if self._recoverykey: |
| 160 recoverykey_file = self._get_temp_filename('set_recoverykey_') |
| 161 utils.open_write_close(recoverykey_file, self._recoverykey) |
| 162 args += ' --recoverykey=%s' % recoverykey_file |
| 163 if self._rootkey: |
| 164 rootkey_file = self._get_temp_filename('set_rootkey_') |
| 165 utils.open_write_close(rootkey_file, self._rootkey) |
| 166 args += ' --rootkey=%s' % rootkey_file |
| 167 |
| 168 flashrom = flashrom_util.FlashromUtility() |
| 169 flashrom.initialize(flashrom.TARGET_BIOS) |
| 170 |
| 171 new_gbb_file = self._get_temp_filename('new_gbb_') |
| 172 self._run_gbb_utility(args, output_file=new_gbb_file) |
| 173 new_gbb = utils.read_file(new_gbb_file) |
| 174 |
| 175 flashrom.write_section('FV_GBB', new_gbb) |
| 176 flashrom.commit() |
| 177 |
| 178 if self._bmpfv: |
| 179 self._remove_temp_file(bmpfv_file) |
| 180 if self._recoverykey: |
| 181 self._remove_temp_file(recoverykey_file) |
| 182 if self._rootkey: |
| 183 self._remove_temp_file(rootkey_file) |
| 184 self._need_commit = False |
| 185 self._clear_cached() |
| 186 |
| 187 |
| 188 def discard(self): |
| 189 """Discard all uncommitted changes.""" |
| 190 if self._need_commit: |
| 191 self._need_commit = False |
| 192 self._clear_cached() |
OLD | NEW |