Chromium Code Reviews| 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 This module provides convenience routines to access Flash ROM (EEPROM). | 7 This module provides convenience routines to access Flash ROM (EEPROM). |
| 8 - flashrom_util is a low level wrapper of flashrom(8) program. | 8 - flashrom_util is a low level wrapper of flashrom(8) program. |
| 9 - FlashromUtility is a high level object which provides more advanced | 9 - FlashromUtility is a high level object which provides more advanced |
| 10 features like journaling-alike (log-based) changing. | 10 features like journaling-alike (log-based) changing. |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 assert section_name in layout_map, "Invalid section: " + section_name | 318 assert section_name in layout_map, "Invalid section: " + section_name |
| 319 pos = layout_map[section_name] | 319 pos = layout_map[section_name] |
| 320 if pos[0] >= pos[1] or pos[1] >= len(base_image): | 320 if pos[0] >= pos[1] or pos[1] >= len(base_image): |
| 321 raise TestError('INTERNAL ERROR: invalid layout map.') | 321 raise TestError('INTERNAL ERROR: invalid layout map.') |
| 322 if len(data) != pos[1] - pos[0] + 1: | 322 if len(data) != pos[1] - pos[0] + 1: |
| 323 raise TestError('INTERNAL ERROR: unmatched data size.') | 323 raise TestError('INTERNAL ERROR: unmatched data size.') |
| 324 return base_image[0 : pos[0]] + data + base_image[pos[1] + 1 :] | 324 return base_image[0 : pos[0]] + data + base_image[pos[1] + 1 :] |
| 325 | 325 |
| 326 def get_size(self): | 326 def get_size(self): |
| 327 """ Gets size of current flash ROM """ | 327 """ Gets size of current flash ROM """ |
| 328 # TODO(hungte) Newer version of tool (flashrom) may support --get-size | 328 cmd = '%s"%s" --get-size' % (self.cmd_prefix, self.tool_path) |
| 329 # command which is faster in future. Right now we use back-compatible | 329 if self.verbose: |
| 330 # method: read whole and then get length. | 330 print 'flashrom_util.get_size(): ', cmd |
| 331 image = self.read_whole() | 331 output = utils.system_output(cmd, ignore_status=True) |
| 332 return len(image) | 332 last_line = output.rpartition('\n')[2] |
| 333 try: | |
| 334 size = long(last_line) | |
| 335 except ValueError: | |
| 336 raise TestError('INTERNAL ERROR: unable to get the flash size.') | |
| 337 return size | |
| 333 | 338 |
| 334 def detect_target_map(self): | 339 def detect_target_map(self): |
| 335 """ | 340 """ |
| 336 Detects the target selection map. | 341 Detects the target selection map. |
| 337 Use machine architecture in current implementation. | 342 Use machine architecture in current implementation. |
| 338 """ | 343 """ |
| 339 arch = utils.get_arch() | 344 arch = utils.get_arch() |
| 340 for regex, target_map in DEFAULT_ARCH_TARGET_MAP.items(): | 345 for regex, target_map in DEFAULT_ARCH_TARGET_MAP.items(): |
| 341 if re.match(regex, arch): | 346 if re.match(regex, arch): |
| 342 return target_map | 347 return target_map |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 750 sys.exit(1) | 755 sys.exit(1) |
| 751 | 756 |
| 752 | 757 |
| 753 class mock_utils(object): | 758 class mock_utils(object): |
| 754 """ a mock for autotest_li.client.bin.utils """ | 759 """ a mock for autotest_li.client.bin.utils """ |
| 755 def get_arch(self): | 760 def get_arch(self): |
| 756 arch = os.popen('uname -m').read().rstrip() | 761 arch = os.popen('uname -m').read().rstrip() |
| 757 arch = re.sub(r"i\d86", r"i386", arch, 1) | 762 arch = re.sub(r"i\d86", r"i386", arch, 1) |
| 758 return arch | 763 return arch |
| 759 | 764 |
| 760 def system(self, cmd, ignore_status=False): | 765 def run_command(self, cmd, ignore_status=False): |
| 761 p = subprocess.Popen(cmd, shell=True, | 766 p = subprocess.Popen(cmd, shell=True, |
| 762 stdout=subprocess.PIPE, | 767 stdout=subprocess.PIPE, |
| 763 stderr=subprocess.PIPE) | 768 stderr=subprocess.PIPE) |
| 764 p.wait() | 769 p.wait() |
| 765 if p.returncode: | 770 if p.returncode and not ignore_status: |
|
Hung-Te
2010/09/02 02:10:22
oops, please keep the print stdout.read() and err_
Tom Wai-Hong Tam
2010/09/02 02:55:24
OK. Will change it in other CL: http://codereview.
| |
| 766 err_msg = p.stderr.read() | 771 raise TestError("failed to execute: %s\nError messages: %s" % ( |
| 767 print p.stdout.read() | 772 cmd, p.stderr.read())) |
| 768 print err_msg | 773 return (p.returncode, p.stdout.read()) |
| 769 if not ignore_status: | 774 |
| 770 raise TestError("failed to execute: %s\nError messages: %s" % ( | 775 def system(self, cmd, ignore_status=False): |
| 771 cmd, err_msg())) | 776 (returncode, output) = self.run_command(cmd, ignore_status) |
| 772 return p.returncode | 777 return returncode |
| 778 | |
| 779 def system_output(self, cmd, ignore_status=False): | |
| 780 (returncode, output) = self.run_command(cmd, ignore_status) | |
| 781 return output | |
| 773 | 782 |
| 774 | 783 |
| 775 # import autotest or mock utilities | 784 # import autotest or mock utilities |
| 776 try: | 785 try: |
| 777 # print 'using autotest' | 786 # print 'using autotest' |
| 778 from autotest_lib.client.bin import test, utils | 787 from autotest_lib.client.bin import test, utils |
| 779 from autotest_lib.client.common_lib.error import TestError | 788 from autotest_lib.client.common_lib.error import TestError |
| 780 except ImportError: | 789 except ImportError: |
| 781 # print 'using mocks' | 790 # print 'using mocks' |
| 782 utils = mock_utils() | 791 utils = mock_utils() |
| 783 TestError = mock_TestError | 792 TestError = mock_TestError |
| 784 | 793 |
| 785 | 794 |
| 786 # main stub | 795 # main stub |
| 787 if __name__ == "__main__": | 796 if __name__ == "__main__": |
| 788 # TODO(hungte) provide unit tests or command line usage | 797 # TODO(hungte) provide unit tests or command line usage |
| 789 pass | 798 pass |
| OLD | NEW |