OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Provides convenience routines to access the GBB on the current BIOS. |
| 7 |
| 8 GBBUtility is a wrapper of gbb_utility program. |
| 9 """ |
| 10 |
| 11 import os |
| 12 import tempfile |
| 13 |
| 14 from autotest_lib.client.common_lib import error |
| 15 from autotest_lib.client.common_lib import flashrom_util |
| 16 from autotest_lib.client.common_lib import utils |
| 17 |
| 18 |
| 19 class GBBUtility(object): |
| 20 """GBBUtility is a wrapper of gbb_utility program. |
| 21 |
| 22 It accesses the GBB on the current BIOS image. |
| 23 """ |
| 24 def __init__(self, |
| 25 gbb_command='gbb_utility', |
| 26 temp_dir=None, |
| 27 keep_temp_files=False): |
| 28 self._gbb_command = gbb_command |
| 29 self._temp_dir = temp_dir |
| 30 self._keep_temp_files = keep_temp_files |
| 31 self._bios_file = None |
| 32 |
| 33 |
| 34 def __del__(self): |
| 35 if self._bios_file: |
| 36 self._remove_temp_file(self._bios_file) |
| 37 |
| 38 |
| 39 def _get_temp_filename(self, prefix='tmp'): |
| 40 """Returns the name of a temporary file in self._temp_dir.""" |
| 41 (fd, name) = tempfile.mkstemp(prefix=prefix, dir=self._temp_dir) |
| 42 os.close(fd) |
| 43 return name |
| 44 |
| 45 |
| 46 def _remove_temp_file(self, filename): |
| 47 """Removes a temporary file if self._keep_temp_files is false.""" |
| 48 if self._keep_temp_files: |
| 49 return |
| 50 if os.path.exists(filename): |
| 51 os.remove(filename) |
| 52 |
| 53 |
| 54 def _read_bios(self, force=False): |
| 55 """Reads the BIOS to a file, self._bios_file.""" |
| 56 if not self._bios_file or force: |
| 57 flashrom = flashrom_util.flashrom_util() |
| 58 if not flashrom.select_bios_flashrom(): |
| 59 raise error.TestError('Unable to select BIOS flashrom') |
| 60 bios_file = self._get_temp_filename('bios') |
| 61 if not flashrom.read_whole_to_file(bios_file): |
| 62 raise error.TestError('Unable to read the BIOS image') |
| 63 self._bios_file = bios_file |
| 64 |
| 65 |
| 66 def _run_gbb_utility(self, args='', output_file=''): |
| 67 """Runs gbb_utility on the current BIOS firmware data.""" |
| 68 self._read_bios() |
| 69 cmd = 'gbb_utility %s "%s" "%s"' % (args, self._bios_file, |
| 70 output_file) |
| 71 result = utils.system_output(cmd) |
| 72 return result |
| 73 |
| 74 |
| 75 def _get_gbb_value(self, key): |
| 76 """Gets the GBB value which needs to be output to a file in gbb_utility. |
| 77 |
| 78 @param key: The key of the value you want to get. Should be 'bmfv', |
| 79 'recoverykey', or 'rootkey'. |
| 80 @return: The returned GBB value. |
| 81 """ |
| 82 value_file = self._get_temp_filename(key) |
| 83 self._run_gbb_utility('--get --%s=%s' % (key, value_file)) |
| 84 with open(value_file, 'rb') as f: |
| 85 value = f.read() |
| 86 self._remove_temp_file(value_file) |
| 87 return value |
| 88 |
| 89 |
| 90 def get_bmpfv(self): |
| 91 return self._get_gbb_value('bmpfv') |
| 92 |
| 93 |
| 94 def get_hwid(self): |
| 95 result = _self._run_gbb_utility(self, '--get --hwid') |
| 96 return result.strip().partition('hardware_id: ')[2] |
| 97 |
| 98 |
| 99 def get_recoverykey(self): |
| 100 return self._get_gbb_value('recoverykey') |
| 101 |
| 102 |
| 103 def get_rootkey(self): |
| 104 return self._get_gbb_value('rootkey') |
OLD | NEW |