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 """ This module provides convenience routines to access Flash ROM (EEPROM) | 6 """ This module provides convenience routines to access Flash ROM (EEPROM) |
| 7 | 7 |
| 8 saft_flashrom_util is based on utility 'flashrom'. | 8 saft_flashrom_util is based on utility 'flashrom'. |
| 9 | 9 |
| 10 Original tool syntax: | 10 Original tool syntax: |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 210 keep_temp_files: boolean flag to control cleaning of temporary files | 210 keep_temp_files: boolean flag to control cleaning of temporary files |
| 211 """ | 211 """ |
| 212 | 212 |
| 213 def __init__(self, verbose=False, keep_temp_files=False): | 213 def __init__(self, verbose=False, keep_temp_files=False): |
| 214 """ constructor of flashrom_util. help(flashrom_util) for more info """ | 214 """ constructor of flashrom_util. help(flashrom_util) for more info """ |
| 215 self.verbose = verbose | 215 self.verbose = verbose |
| 216 self.keep_temp_files = keep_temp_files | 216 self.keep_temp_files = keep_temp_files |
| 217 self.bios_layout = {} | 217 self.bios_layout = {} |
| 218 self.os_if = chromeos_interface.ChromeOSInterface(True) | 218 self.os_if = chromeos_interface.ChromeOSInterface(True) |
| 219 self.os_if.init(tempfile.gettempdir()) | 219 self.os_if.init(tempfile.gettempdir()) |
| 220 self._target_command = '' | |
| 220 self._enable_bios_access() | 221 self._enable_bios_access() |
| 221 | 222 |
| 222 def _enable_bios_access(self): | 223 def _enable_bios_access(self): |
| 223 if not self.os_if.target_hosted(): | 224 if not self.os_if.target_hosted(): |
| 224 return | 225 return |
| 225 value = int(self.os_if.run_shell_command_get_output( | 226 self._target_command = '-p internal:bus=spi' |
|
vb
2011/03/02 17:50:08
why not to make it 'flashrom -p internal:bus=spi'
Hung-Te
2011/03/03 06:52:29
Because I'm not sure if SAFT will need to deal wit
| |
| 226 'iotools mmio_read32 0xfed1f410')[0], 0) | |
| 227 value = (value & 0xffff0000) + 0x460 | |
| 228 self.os_if.run_shell_command( | |
| 229 'iotools mmio_write32 0xfed1f410 0x%x' % value) | |
| 230 | 227 |
| 231 def get_temp_filename(self, prefix): | 228 def get_temp_filename(self, prefix): |
| 232 ''' (internal) Returns name of a temporary file in self.tmp_root ''' | 229 ''' (internal) Returns name of a temporary file in self.tmp_root ''' |
| 233 (fd, name) = tempfile.mkstemp(prefix=prefix) | 230 (fd, name) = tempfile.mkstemp(prefix=prefix) |
| 234 os.close(fd) | 231 os.close(fd) |
| 235 return name | 232 return name |
| 236 | 233 |
| 237 def remove_temp_file(self, filename): | 234 def remove_temp_file(self, filename): |
| 238 """ (internal) Removes a temp file if self.keep_temp_files is false. """ | 235 """ (internal) Removes a temp file if self.keep_temp_files is false. """ |
| 239 if self.keep_temp_files: | 236 if self.keep_temp_files: |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 | 287 |
| 291 scraper = LayoutScraper(self.os_if) | 288 scraper = LayoutScraper(self.os_if) |
| 292 self.bios_layout = scraper.get_layout(file_name) | 289 self.bios_layout = scraper.get_layout(file_name) |
| 293 | 290 |
| 294 def read_whole(self): | 291 def read_whole(self): |
| 295 ''' | 292 ''' |
| 296 Reads whole flash ROM data. | 293 Reads whole flash ROM data. |
| 297 Returns the data read from flash ROM, or empty string for other error. | 294 Returns the data read from flash ROM, or empty string for other error. |
| 298 ''' | 295 ''' |
| 299 tmpfn = self.get_temp_filename('rd_') | 296 tmpfn = self.get_temp_filename('rd_') |
| 300 cmd = 'flashrom -r "%s"' % (tmpfn) | 297 cmd = 'flashrom %s -r "%s"' % (self._target_command, tmpfn) |
| 301 if self.verbose: | 298 if self.verbose: |
| 302 print 'flashrom_util.read_whole(): ', cmd | 299 print 'flashrom_util.read_whole(): ', cmd |
| 303 | 300 |
| 304 self.os_if.run_shell_command(cmd) | 301 self.os_if.run_shell_command(cmd) |
| 305 result = open(tmpfn, 'rb').read() | 302 result = open(tmpfn, 'rb').read() |
| 306 self.set_bios_layout(tmpfn) | 303 self.set_bios_layout(tmpfn) |
| 307 | 304 |
| 308 # clean temporary resources | 305 # clean temporary resources |
| 309 self.remove_temp_file(tmpfn) | 306 self.remove_temp_file(tmpfn) |
| 310 return result | 307 return result |
| 311 | 308 |
| 312 def write_partial(self, base_image, write_list, write_layout_map=None): | 309 def write_partial(self, base_image, write_list, write_layout_map=None): |
| 313 ''' | 310 ''' |
| 314 Writes data in sections of write_list to flash ROM. | 311 Writes data in sections of write_list to flash ROM. |
| 315 An exception is raised if write operation fails. | 312 An exception is raised if write operation fails. |
| 316 ''' | 313 ''' |
| 317 | 314 |
| 318 if write_layout_map: | 315 if write_layout_map: |
| 319 layout_map = write_layout_map | 316 layout_map = write_layout_map |
| 320 else: | 317 else: |
| 321 layout_map = self.bios_layout | 318 layout_map = self.bios_layout |
| 322 | 319 |
| 323 tmpfn = self.get_temp_filename('wr_') | 320 tmpfn = self.get_temp_filename('wr_') |
| 324 open(tmpfn, 'wb').write(base_image) | 321 open(tmpfn, 'wb').write(base_image) |
| 325 layout_fn = self.create_layout_file(layout_map) | 322 layout_fn = self.create_layout_file(layout_map) |
| 326 | 323 |
| 327 cmd = 'flashrom -l "%s" -i %s -w "%s"' % ( | 324 cmd = 'flashrom %s -l "%s" -i %s -w "%s"' % ( |
| 328 layout_fn, ' -i '.join(write_list), tmpfn) | 325 self._target_command, layout_fn, ' -i '.join(write_list), tmpfn) |
| 329 if self.verbose: | 326 if self.verbose: |
| 330 print 'flashrom.write_partial(): ', cmd | 327 print 'flashrom.write_partial(): ', cmd |
| 331 | 328 |
| 332 self.os_if.run_shell_command(cmd) | 329 self.os_if.run_shell_command(cmd) |
| 333 | 330 |
| 334 # clean temporary resources | 331 # clean temporary resources |
| 335 self.remove_temp_file(tmpfn) | 332 self.remove_temp_file(tmpfn) |
| 336 self.remove_temp_file(layout_fn) | 333 self.remove_temp_file(layout_fn) |
| 337 | 334 |
| 338 def write_whole(self, base_image): | 335 def write_whole(self, base_image): |
| 339 '''Write the whole base image. ''' | 336 '''Write the whole base image. ''' |
| 340 layout_map = { 'all': (0, len(base_image) - 1) } | 337 layout_map = { 'all': (0, len(base_image) - 1) } |
| 341 self.write_partial(base_image, ('all',), layout_map) | 338 self.write_partial(base_image, ('all',), layout_map) |
| OLD | NEW |