diff options
author | Tom Hughes <tomhughes@chromium.org> | 2022-09-21 14:08:36 -0700 |
---|---|---|
committer | Tom Hughes <tomhughes@chromium.org> | 2022-09-22 12:59:38 -0700 |
commit | c453fd704268ef72de871b0c5ac7a989de662334 (patch) | |
tree | fcf6ce5810f9ff9e3c8cce434812dd75492269ed /extra/cr50_rma_open/cr50_rma_open.py | |
parent | 6c1587ca70f558b4f96b3f0b18ad8b027d3ba99d (diff) | |
parent | 28712dae9d7ed1e694f7622cc083afa71090d4d5 (diff) | |
download | chrome-ec-firmware-fpmcu-dartmonkey-release.tar.gz |
Merge remote-tracking branch cros/main into firmware-fpmcu-dartmonkey-releasefirmware-fpmcu-dartmonkey-release
Generated by: ./util/update_release_branch.py --board dartmonkey --relevant_paths_file
./util/fingerprint-relevant-paths.txt firmware-fpmcu-dartmonkey-release
Relevant changes:
git log --oneline 6c1587ca70..28712dae9d -- board/nocturne_fp
board/dartmonkey common/fpsensor docs/fingerprint driver/fingerprint
util/getversion.sh
ded9307b79 util/getversion.sh: Fix version when not in a git repo
956055e692 board: change Google USB vendor info
71b2ef709d Update license boilerplate text in source code files
33e11afda0 Revert "fpsensor: Build fpsensor source file with C++"
c8d0360723 fpsensor: Build fpsensor source file with C++
bc113abd53 fpsensor: Fix g++ compiler error
150a58a0dc fpsensor: Fix fp_set_sensor_mode return type
b33b5ce85b fpsensor: Remove nested designators for C++ compatibility
2e864b2539 tree-wide: const-ify argv for console commands
56d8b360f9 test: Add test for get ikm failure when seed not set
3a3d6c3690 test: Add test for fpsensor trivial key failure
233e6bbd08 fpsensor_crypto: Abstract calls to hmac_SHA256
0a041b285b docs/fingerprint: Typo correction
c03fab67e2 docs/fingerprint: Fix the path of fputils.py
0b5d4baf5a util/getversion.sh: Fix empty file list handling
6e128fe760 FPMCU dev board environment with Satlab
3eb29b6aa5 builtin: Move ssize_t to sys/types.h
345d62ebd1 docs/fingerprint: Update power numbers for latest dartmonkey release
c25ffdb316 common: Conditionally support printf %l and %i modifiers
9a3c514b45 test: Add a test to check if the debugger is connected
54e603413f Move standard library tests to their own file
43fa6b4bf8 docs/fingerprint: Update power numbers for latest bloonchipper release
25536f9a84 driver/fingerprint/fpc/bep/fpc_sensor_spi.c: Format with clang-format
4face99efd driver/fingerprint/fpc/libfp/fpc_sensor_pal.h: Format with clang-format
738de2b575 trng: Rename rand to trng_rand
14b8270edd docs/fingerprint: Update dragonclaw power numbers
0b268f93d1 driver/fingerprint/fpc/libfp/fpc_private.c: Format with clang-format
f80da163f2 driver/fingerprint/fpc/libfp/fpc_private.h: Format with clang-format
a0751778f4 board/nocturne_fp/ro_workarounds.c: Format with clang-format
5e9c85c9b1 driver/fingerprint/fpc/libfp/fpc_sensor_pal.c: Format with clang-format
c1f9dd3cf8 driver/fingerprint/fpc/libfp/fpc_bio_algorithm.h: Format with clang-format
eb1e1bed8d driver/fingerprint/fpc/libfp/fpc1145_private.h: Format with clang-format
6e7b611821 driver/fingerprint/fpc/bep/fpc_bio_algorithm.h: Format with clang-format
e0589cd5e2 driver/fingerprint/fpc/bep/fpc1035_private.h: Format with clang-format
58f0246dbe board/nocturne_fp/board_ro.c: Format with clang-format
7905e556a0 common/fpsensor/fpsensor_crypto.c: Format with clang-format
21289d170c driver/fingerprint/fpc/bep/fpc1025_private.h: Format with clang-format
98a20f937e common/fpsensor/fpsensor_state.c: Format with clang-format
a2d255d8af common/fpsensor/fpsensor.c: Format with clang-format
84e53a65da board/nocturne_fp/board.h: Format with clang-format
73055eeb3f driver/fingerprint/fpc/bep/fpc_private.c: Format with clang-format
0f7b5cb509 common/fpsensor/fpsensor_private.h: Format with clang-format
1ceade6e65 driver/fingerprint/fpc/bep/fpc_private.h: Format with clang-format
dca9d74321 Revert "trng: Rename rand to trng_rand"
a6b0b3554f trng: Rename rand to trng_rand
28d0b75b70 third_party/boringssl: Remove unused header
BRANCH=None
BUG=b:244387210 b:242720240 b:215613183 b:242720910 b:236386294
BUG=b:234181908 b:244781166 b:234781655 b:234143158 b:234181908
BUG=b:237344361 b:236025198 b:234181908 b:180945056 chromium:1098010
BUG=b:246424843 b:234181908 b:131913998
TEST=`make -j buildall`
TEST=./util/run_device_tests.py --board dartmonkey
Test "aes": PASSED
Test "cec": PASSED
Test "cortexm_fpu": PASSED
Test "crc": PASSED
Test "flash_physical": PASSED
Test "flash_write_protect": PASSED
Test "fpsensor_hw": PASSED
Test "fpsensor_spi_ro": PASSED
Test "fpsensor_spi_rw": PASSED
Test "fpsensor_uart_ro": PASSED
Test "fpsensor_uart_rw": PASSED
Test "mpu_ro": PASSED
Test "mpu_rw": PASSED
Test "mutex": PASSED
Test "pingpong": PASSED
Test "printf": PASSED
Test "queue": PASSED
Test "rollback_region0": PASSED
Test "rollback_region1": PASSED
Test "rollback_entropy": PASSED
Test "rtc": PASSED
Test "sha256": PASSED
Test "sha256_unrolled": PASSED
Test "static_if": PASSED
Test "stdlib": PASSED
Test "system_is_locked_wp_on": PASSED
Test "system_is_locked_wp_off": PASSED
Test "timer_dos": PASSED
Test "utils": PASSED
Test "utils_str": PASSED
Test "panic_data_dartmonkey_v2.0.2887": PASSED
Test "panic_data_nocturne_fp_v2.2.64": PASSED
Test "panic_data_nami_fp_v2.2.144": PASSED
Force-Relevant-Builds: all
Signed-off-by: Tom Hughes <tomhughes@chromium.org>
Change-Id: I2c312583a709fedae8fe11d92c22328c3b634bc7
Diffstat (limited to 'extra/cr50_rma_open/cr50_rma_open.py')
-rwxr-xr-x | extra/cr50_rma_open/cr50_rma_open.py | 443 |
1 files changed, 255 insertions, 188 deletions
diff --git a/extra/cr50_rma_open/cr50_rma_open.py b/extra/cr50_rma_open/cr50_rma_open.py index 42ddbbac2d..dc9c144158 100755 --- a/extra/cr50_rma_open/cr50_rma_open.py +++ b/extra/cr50_rma_open/cr50_rma_open.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# Copyright 2018 The Chromium OS Authors. All rights reserved. +# Copyright 2018 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -50,23 +50,22 @@ import subprocess import sys import time -import serial +import serial # pylint:disable=import-error SCRIPT_VERSION = 5 CCD_IS_UNRESTRICTED = 1 << 0 WP_IS_DISABLED = 1 << 1 TESTLAB_IS_ENABLED = 1 << 2 RMA_OPENED = CCD_IS_UNRESTRICTED | WP_IS_DISABLED -URL = ('https://www.google.com/chromeos/partner/console/cr50reset?' - 'challenge=%s&hwid=%s') -RMA_SUPPORT_PROD = '0.3.3' -RMA_SUPPORT_PREPVT = '0.4.5' -DEV_MODE_OPEN_PROD = '0.3.9' -DEV_MODE_OPEN_PREPVT = '0.4.7' -TESTLAB_PROD = '0.3.10' -CR50_USB = '18d1:5014' -CR50_LSUSB_CMD = ['lsusb', '-vd', CR50_USB] -ERASED_BID = 'ffffffff' +URL = "https://www.google.com/chromeos/partner/console/cr50reset?challenge=%s&hwid=%s" +RMA_SUPPORT_PROD = "0.3.3" +RMA_SUPPORT_PREPVT = "0.4.5" +DEV_MODE_OPEN_PROD = "0.3.9" +DEV_MODE_OPEN_PREPVT = "0.4.7" +TESTLAB_PROD = "0.3.10" +CR50_USB = "18d1:5014" +CR50_LSUSB_CMD = ["lsusb", "-vd", CR50_USB] +ERASED_BID = "ffffffff" DEBUG_MISSING_USB = """ Unable to find Cr50 Device 18d1:5014 @@ -128,13 +127,14 @@ DEBUG_DUT_CONTROL_OSERROR = """ Run from chroot if you are trying to use a /dev/pts ccd servo console """ + class RMAOpen(object): """Used to find the cr50 console and run RMA open""" - ENABLE_TESTLAB_CMD = 'ccd testlab enabled\n' + ENABLE_TESTLAB_CMD = "ccd testlab enabled\n" def __init__(self, device=None, usb_serial=None, servo_port=None, ip=None): - self.servo_port = servo_port if servo_port else '9999' + self.servo_port = servo_port if servo_port else "9999" self.ip = ip if device: self.set_cr50_device(device) @@ -142,18 +142,18 @@ class RMAOpen(object): self.find_cr50_servo_uart() else: self.find_cr50_device(usb_serial) - logging.info('DEVICE: %s', self.device) + logging.info("DEVICE: %s", self.device) self.check_version() self.print_platform_info() - logging.info('Cr50 setup ok') + logging.info("Cr50 setup ok") self.update_ccd_state() self.using_ccd = self.device_is_running_with_servo_ccd() def _dut_control(self, control): """Run dut-control and return the response""" try: - cmd = ['dut-control', '-p', self.servo_port, control] - return subprocess.check_output(cmd, encoding='utf-8').strip() + cmd = ["dut-control", "-p", self.servo_port, control] + return subprocess.check_output(cmd, encoding="utf-8").strip() except OSError: logging.warning(DEBUG_DUT_CONTROL_OSERROR) raise @@ -163,8 +163,8 @@ class RMAOpen(object): Find the console and configure it, so it can be used with this script. """ - self._dut_control('cr50_uart_timestamp:off') - self.device = self._dut_control('cr50_uart_pty').split(':')[-1] + self._dut_control("cr50_uart_timestamp:off") + self.device = self._dut_control("cr50_uart_pty").split(":")[-1] def set_cr50_device(self, device): """Save the device used for the console""" @@ -183,38 +183,38 @@ class RMAOpen(object): try: ser = serial.Serial(self.device, timeout=1) except OSError: - logging.warning('Permission denied %s', self.device) - logging.warning('Try running cr50_rma_open with sudo') + logging.warning("Permission denied %s", self.device) + logging.warning("Try running cr50_rma_open with sudo") raise - write_cmd = cmd + '\n\n' - ser.write(write_cmd.encode('utf-8')) + write_cmd = cmd + "\n\n" + ser.write(write_cmd.encode("utf-8")) if nbytes: output = ser.read(nbytes) else: output = ser.readall() ser.close() - output = output.decode('utf-8').strip() if output else '' + output = output.decode("utf-8").strip() if output else "" # Return only the command output - split_cmd = cmd + '\r' + split_cmd = cmd + "\r" if cmd and split_cmd in output: - return ''.join(output.rpartition(split_cmd)[1::]).split('>')[0] + return "".join(output.rpartition(split_cmd)[1::]).split(">")[0] return output def device_is_running_with_servo_ccd(self): """Return True if the device is a servod ccd console""" # servod uses /dev/pts consoles. Non-servod uses /dev/ttyUSBX - if '/dev/pts' not in self.device: + if "/dev/pts" not in self.device: return False # If cr50 doesn't show rdd is connected, cr50 the device must not be # a ccd device - if 'Rdd: connected' not in self.send_cmd_get_output('ccdstate'): + if "Rdd: connected" not in self.send_cmd_get_output("ccdstate"): return False # Check if the servod is running with ccd. This requires the script # is run in the chroot, so run it last. - if 'ccd_cr50' not in self._dut_control('servo_type'): + if "ccd_cr50" not in self._dut_control("servo_type"): return False - logging.info('running through servod ccd') + logging.info("running through servod ccd") return True def get_rma_challenge(self): @@ -239,14 +239,14 @@ class RMAOpen(object): Returns: The RMA challenge with all whitespace removed. """ - output = self.send_cmd_get_output('rma_auth').strip() - logging.info('rma_auth output:\n%s', output) + output = self.send_cmd_get_output("rma_auth").strip() + logging.info("rma_auth output:\n%s", output) # Extract the challenge from the console output - if 'generated challenge:' in output: - return output.split('generated challenge:')[-1].strip() - challenge = ''.join(re.findall(r' \S{5}' * 4, output)) + if "generated challenge:" in output: + return output.split("generated challenge:")[-1].strip() + challenge = "".join(re.findall(r" \S{5}" * 4, output)) # Remove all whitespace - return re.sub(r'\s', '', challenge) + return re.sub(r"\s", "", challenge) def generate_challenge_url(self, hwid): """Get the rma_auth challenge @@ -257,12 +257,14 @@ class RMAOpen(object): challenge = self.get_rma_challenge() self.print_platform_info() - logging.info('CHALLENGE: %s', challenge) - logging.info('HWID: %s', hwid) + logging.info("CHALLENGE: %s", challenge) + logging.info("HWID: %s", hwid) url = URL % (challenge, hwid) - logging.info('GOTO:\n %s', url) - logging.info('If the server fails to debug the challenge make sure the ' - 'RLZ is allowlisted') + logging.info("GOTO:\n %s", url) + logging.info( + "If the server fails to debug the challenge make sure the " + "RLZ is allowlisted" + ) def try_authcode(self, authcode): """Try opening cr50 with the authcode @@ -272,48 +274,48 @@ class RMAOpen(object): """ # rma_auth may cause the system to reboot. Don't wait to read all that # output. Read the first 300 bytes and call it a day. - output = self.send_cmd_get_output('rma_auth ' + authcode, nbytes=300) - logging.info('CR50 RESPONSE: %s', output) - logging.info('waiting for cr50 reboot') + output = self.send_cmd_get_output("rma_auth " + authcode, nbytes=300) + logging.info("CR50 RESPONSE: %s", output) + logging.info("waiting for cr50 reboot") # Cr50 may be rebooting. Wait a bit time.sleep(5) if self.using_ccd: # After reboot, reset the ccd endpoints - self._dut_control('power_state:ccd_reset') + self._dut_control("power_state:ccd_reset") # Update the ccd state after the authcode attempt self.update_ccd_state() - authcode_match = 'process_response: success!' in output + authcode_match = "process_response: success!" in output if not self.check(CCD_IS_UNRESTRICTED): if not authcode_match: logging.warning(DEBUG_AUTHCODE_MISMATCH) - message = 'Authcode mismatch. Check args and url' + message = "Authcode mismatch. Check args and url" else: - message = 'Could not set all capability privileges to Always' + message = "Could not set all capability privileges to Always" raise ValueError(message) def wp_is_force_disabled(self): """Returns True if write protect is forced disabled""" - output = self.send_cmd_get_output('wp') - wp_state = output.split('Flash WP:', 1)[-1].split('\n', 1)[0].strip() - logging.info('wp: %s', wp_state) - return wp_state == 'forced disabled' + output = self.send_cmd_get_output("wp") + wp_state = output.split("Flash WP:", 1)[-1].split("\n", 1)[0].strip() + logging.info("wp: %s", wp_state) + return wp_state == "forced disabled" def testlab_is_enabled(self): """Returns True if testlab mode is enabled""" - output = self.send_cmd_get_output('ccd testlab') - testlab_state = output.split('mode')[-1].strip().lower() - logging.info('testlab: %s', testlab_state) - return testlab_state == 'enabled' + output = self.send_cmd_get_output("ccd testlab") + testlab_state = output.split("mode")[-1].strip().lower() + logging.info("testlab: %s", testlab_state) + return testlab_state == "enabled" def ccd_is_restricted(self): """Returns True if any of the capabilities are still restricted""" - output = self.send_cmd_get_output('ccd') - if 'Capabilities' not in output: - raise ValueError('Could not get ccd output') - logging.debug('CURRENT CCD SETTINGS:\n%s', output) - restricted = 'IfOpened' in output or 'IfUnlocked' in output - logging.info('ccd: %srestricted', '' if restricted else 'Un') + output = self.send_cmd_get_output("ccd") + if "Capabilities" not in output: + raise ValueError("Could not get ccd output") + logging.debug("CURRENT CCD SETTINGS:\n%s", output) + restricted = "IfOpened" in output or "IfUnlocked" in output + logging.info("ccd: %srestricted", "" if restricted else "Un") return restricted def update_ccd_state(self): @@ -339,9 +341,10 @@ class RMAOpen(object): def _capabilities_allow_open_from_console(self): """Return True if ccd open is Always allowed from usb""" - output = self.send_cmd_get_output('ccd') - return (re.search('OpenNoDevMode.*Always', output) and - re.search('OpenFromUSB.*Always', output)) + output = self.send_cmd_get_output("ccd") + return re.search("OpenNoDevMode.*Always", output) and re.search( + "OpenFromUSB.*Always", output + ) def _requires_dev_mode_open(self): """Return True if the image requires dev mode to open""" @@ -354,78 +357,83 @@ class RMAOpen(object): def _run_on_dut(self, command): """Run the command on the DUT.""" - return subprocess.check_output(['ssh', self.ip, command], - encoding='utf-8') + return subprocess.check_output( + ["ssh", self.ip, command], encoding="utf-8" + ) def _open_in_dev_mode(self): """Open Cr50 when it's in dev mode""" - output = self.send_cmd_get_output('ccd') + output = self.send_cmd_get_output("ccd") # If the device is already open, nothing needs to be done. - if 'State: Open' not in output: + if "State: Open" not in output: # Verify the device is in devmode before trying to run open. - if 'dev_mode' not in output: - logging.warning('Enter dev mode to open ccd or update to %s', - TESTLAB_PROD) - raise ValueError('DUT not in dev mode') + if "dev_mode" not in output: + logging.warning( + "Enter dev mode to open ccd or update to %s", TESTLAB_PROD + ) + raise ValueError("DUT not in dev mode") if not self.ip: - logging.warning("If your DUT doesn't have ssh support, run " - "'gsctool -a -o' from the AP") - raise ValueError('Cannot run ccd open without dut ip') - self._run_on_dut('gsctool -a -o') + logging.warning( + "If your DUT doesn't have ssh support, run " + "'gsctool -a -o' from the AP" + ) + raise ValueError("Cannot run ccd open without dut ip") + self._run_on_dut("gsctool -a -o") # Wait >1 second for cr50 to update ccd state time.sleep(3) - output = self.send_cmd_get_output('ccd') - if 'State: Open' not in output: - raise ValueError('Could not open cr50') - logging.info('ccd is open') + output = self.send_cmd_get_output("ccd") + if "State: Open" not in output: + raise ValueError("Could not open cr50") + logging.info("ccd is open") def enable_testlab(self): """Disable write protect""" if not self._has_testlab_support(): - logging.warning('Testlab mode is not supported in prod iamges') + logging.warning("Testlab mode is not supported in prod iamges") return # Some cr50 images need to be in dev mode before they can be opened. if self._requires_dev_mode_open(): self._open_in_dev_mode() else: - self.send_cmd_get_output('ccd open') - logging.info('Enabling testlab mode reqires pressing the power button.') - logging.info('Once the process starts keep tapping the power button ' - 'for 10 seconds.') + self.send_cmd_get_output("ccd open") + logging.info("Enabling testlab mode reqires pressing the power button.") + logging.info( + "Once the process starts keep tapping the power button for 10 seconds." + ) input("Press Enter when you're ready to start...") end_time = time.time() + 15 ser = serial.Serial(self.device, timeout=1) - printed_lines = '' - output = '' + printed_lines = "" + output = "" # start ccd testlab enable - ser.write(self.ENABLE_TESTLAB_CMD.encode('utf-8')) - logging.info('start pressing the power button\n\n') + ser.write(self.ENABLE_TESTLAB_CMD.encode("utf-8")) + logging.info("start pressing the power button\n\n") # Print all of the cr50 output as we get it, so the user will have more # information about pressing the power button. Tapping the power button # a couple of times should do it, but this will give us more confidence # the process is still running/worked. try: while time.time() < end_time: - output += ser.read(100).decode('utf-8') - full_lines = output.rsplit('\n', 1)[0] + output += ser.read(100).decode("utf-8") + full_lines = output.rsplit("\n", 1)[0] new_lines = full_lines if printed_lines: new_lines = full_lines.split(printed_lines, 1)[-1].strip() - logging.info('\n%s', new_lines) + logging.info("\n%s", new_lines) printed_lines = full_lines # Make sure the process hasn't ended. If it has, print the last # of the output and exit. new_lines = output.split(printed_lines, 1)[-1] - if 'CCD test lab mode enabled' in output: + if "CCD test lab mode enabled" in output: # print the last of the ou logging.info(new_lines) break - elif 'Physical presence check timeout' in output: + elif "Physical presence check timeout" in output: logging.info(new_lines) - logging.warning('Did not detect power button press in time') - raise ValueError('Could not enable testlab mode try again') + logging.warning("Did not detect power button press in time") + raise ValueError("Could not enable testlab mode try again") finally: ser.close() # Wait for the ccd hook to update things @@ -433,44 +441,50 @@ class RMAOpen(object): # Update the state after attempting to disable write protect self.update_ccd_state() if not self.check(TESTLAB_IS_ENABLED): - raise ValueError('Could not enable testlab mode try again') + raise ValueError("Could not enable testlab mode try again") def wp_disable(self): """Disable write protect""" - logging.info('Disabling write protect') - self.send_cmd_get_output('wp disable') + logging.info("Disabling write protect") + self.send_cmd_get_output("wp disable") # Update the state after attempting to disable write protect self.update_ccd_state() if not self.check(WP_IS_DISABLED): - raise ValueError('Could not disable write protect') + raise ValueError("Could not disable write protect") def check_version(self): """Make sure cr50 is running a version that supports RMA Open""" - output = self.send_cmd_get_output('version') + output = self.send_cmd_get_output("version") if not output.strip(): logging.warning(DEBUG_DEVICE, self.device) - raise ValueError('Could not communicate with %s' % self.device) + raise ValueError("Could not communicate with %s" % self.device) - version = re.search(r'RW.*\* ([\d\.]+)/', output).group(1) - logging.info('Running Cr50 Version: %s', version) - self.running_ver_fields = [int(field) for field in version.split('.')] + version = re.search(r"RW.*\* ([\d\.]+)/", output).group(1) + logging.info("Running Cr50 Version: %s", version) + self.running_ver_fields = [int(field) for field in version.split(".")] # prePVT images have even major versions. Prod have odd self.is_prepvt = self.running_ver_fields[1] % 2 == 0 rma_support = RMA_SUPPORT_PREPVT if self.is_prepvt else RMA_SUPPORT_PROD - logging.info('%s RMA support added in: %s', - 'prePVT' if self.is_prepvt else 'prod', rma_support) + logging.info( + "%s RMA support added in: %s", + "prePVT" if self.is_prepvt else "prod", + rma_support, + ) if not self.is_prepvt and self._running_version_is_older(TESTLAB_PROD): - raise ValueError('Update cr50. No testlab support in old prod ' - 'images.') + raise ValueError( + "Update cr50. No testlab support in old prod images." + ) if self._running_version_is_older(rma_support): - raise ValueError('%s does not have RMA support. Update to at ' - 'least %s' % (version, rma_support)) + raise ValueError( + "%s does not have RMA support. Update to at least %s" + % (version, rma_support) + ) def _running_version_is_older(self, target_ver): """Returns True if running version is older than target_ver.""" - target_ver_fields = [int(field) for field in target_ver.split('.')] + target_ver_fields = [int(field) for field in target_ver.split(".")] for i, field in enumerate(self.running_ver_fields): if field > int(target_ver_fields[i]): return False @@ -486,11 +500,11 @@ class RMAOpen(object): is no output or sysinfo doesn't contain the devid. """ self.set_cr50_device(device) - sysinfo = self.send_cmd_get_output('sysinfo') + sysinfo = self.send_cmd_get_output("sysinfo") # Make sure there is some output, and it shows it's from Cr50 - if not sysinfo or 'cr50' not in sysinfo: + if not sysinfo or "cr50" not in sysinfo: return False - logging.debug('Sysinfo output: %s', sysinfo) + logging.debug("Sysinfo output: %s", sysinfo) # The cr50 device id should be in the sysinfo output, if we found # the right console. Make sure it is return devid in sysinfo @@ -508,104 +522,150 @@ class RMAOpen(object): ValueError if the console can't be found with the given serialname """ usb_serial = self.find_cr50_usb(usb_serial) - logging.info('SERIALNAME: %s', usb_serial) - devid = '0x' + ' 0x'.join(usb_serial.lower().split('-')) - logging.info('DEVID: %s', devid) + logging.info("SERIALNAME: %s", usb_serial) + devid = "0x" + " 0x".join(usb_serial.lower().split("-")) + logging.info("DEVID: %s", devid) # Get all the usb devices - devices = glob.glob('/dev/ttyUSB*') + devices = glob.glob("/dev/ttyUSB*") # Typically Cr50 has the lowest number. Sort the devices, so we're more # likely to try the cr50 console first. devices.sort() # Find the one that is the cr50 console for device in devices: - logging.info('testing %s', device) + logging.info("testing %s", device) if self.device_matches_devid(devid, device): - logging.info('found device: %s', device) + logging.info("found device: %s", device) return logging.warning(DEBUG_CONNECTION) - raise ValueError('Found USB device, but could not communicate with ' - 'cr50 console') + raise ValueError( + "Found USB device, but could not communicate with cr50 console" + ) def print_platform_info(self): """Print the cr50 BID RLZ code""" - bid_output = self.send_cmd_get_output('bid') - bid = re.search(r'Board ID: (\S+?)[:,]', bid_output).group(1) + bid_output = self.send_cmd_get_output("bid") + bid = re.search(r"Board ID: (\S+?)[:,]", bid_output).group(1) if bid == ERASED_BID: logging.warning(DEBUG_ERASED_BOARD_ID) - raise ValueError('Cannot run RMA Open when board id is erased') + raise ValueError("Cannot run RMA Open when board id is erased") bid = int(bid, 16) - chrs = [chr((bid >> (8 * i)) & 0xff) for i in range(4)] - logging.info('RLZ: %s', ''.join(chrs[::-1])) + chrs = [chr((bid >> (8 * i)) & 0xFF) for i in range(4)] + logging.info("RLZ: %s", "".join(chrs[::-1])) @staticmethod def find_cr50_usb(usb_serial): """Make sure the Cr50 USB device exists""" try: - output = subprocess.check_output(CR50_LSUSB_CMD, encoding='utf-8') + output = subprocess.check_output(CR50_LSUSB_CMD, encoding="utf-8") except: logging.warning(DEBUG_MISSING_USB) - raise ValueError('Could not find Cr50 USB device') - serialnames = re.findall(r'iSerial +\d+ (\S+)\s', output) + raise ValueError("Could not find Cr50 USB device") + serialnames = re.findall(r"iSerial +\d+ (\S+)\s", output) if usb_serial: if usb_serial not in serialnames: logging.warning(DEBUG_SERIALNAME) raise ValueError('Could not find usb device "%s"' % usb_serial) return usb_serial if len(serialnames) > 1: - logging.info('Found Cr50 device serialnames %s', - ', '.join(serialnames)) + logging.info( + "Found Cr50 device serialnames %s", ", ".join(serialnames) + ) logging.warning(DEBUG_TOO_MANY_USB_DEVICES) - raise ValueError('Too many cr50 usb devices') + raise ValueError("Too many cr50 usb devices") return serialnames[0] def print_dut_state(self): """Print CCD RMA and testlab mode state.""" if not self.check(CCD_IS_UNRESTRICTED): - logging.info('CCD is still restricted.') - logging.info('Run cr50_rma_open.py -g -i $HWID to generate a url') - logging.info('Run cr50_rma_open.py -a $AUTHCODE to open cr50 with ' - 'an authcode') + logging.info("CCD is still restricted.") + logging.info("Run cr50_rma_open.py -g -i $HWID to generate a url") + logging.info( + "Run cr50_rma_open.py -a $AUTHCODE to open cr50 with an authcode" + ) elif not self.check(WP_IS_DISABLED): - logging.info('WP is still enabled.') - logging.info('Run cr50_rma_open.py -w to disable write protect') + logging.info("WP is still enabled.") + logging.info("Run cr50_rma_open.py -w to disable write protect") if self.check(RMA_OPENED): - logging.info('RMA Open complete') + logging.info("RMA Open complete") if not self.check(TESTLAB_IS_ENABLED) and self.is_prepvt: - logging.info('testlab mode is disabled.') - logging.info('If you are prepping a device for the testlab, you ' - 'should enable testlab mode.') - logging.info('Run cr50_rma_open.py -t to enable testlab mode') + logging.info("testlab mode is disabled.") + logging.info( + "If you are prepping a device for the testlab, you " + "should enable testlab mode." + ) + logging.info("Run cr50_rma_open.py -t to enable testlab mode") def parse_args(argv): """Get cr50_rma_open args.""" parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.RawTextHelpFormatter) - parser.add_argument('-g', '--generate_challenge', action='store_true', - help='Generate Cr50 challenge. Must be used with -i') - parser.add_argument('-t', '--enable_testlab', action='store_true', - help='enable testlab mode') - parser.add_argument('-w', '--wp_disable', action='store_true', - help='Disable write protect') - parser.add_argument('-c', '--check_connection', action='store_true', - help='Check cr50 console connection works') - parser.add_argument('-s', '--serialname', type=str, default='', - help='The cr50 usb serialname') - parser.add_argument('-D', '--debug', action='store_true', - help='print debug messages') - parser.add_argument('-d', '--device', type=str, default='', - help='cr50 console device ex /dev/ttyUSB0') - parser.add_argument('-i', '--hwid', type=str, default='', - help='The board hwid. Needed to generate a challenge') - parser.add_argument('-a', '--authcode', type=str, default='', - help='The authcode string from the challenge url') - parser.add_argument('-P', '--servo_port', type=str, default='', - help='the servo port') - parser.add_argument('-I', '--ip', type=str, default='', - help='The DUT IP. Necessary to do ccd open') + description=__doc__, formatter_class=argparse.RawTextHelpFormatter + ) + parser.add_argument( + "-g", + "--generate_challenge", + action="store_true", + help="Generate Cr50 challenge. Must be used with -i", + ) + parser.add_argument( + "-t", + "--enable_testlab", + action="store_true", + help="enable testlab mode", + ) + parser.add_argument( + "-w", "--wp_disable", action="store_true", help="Disable write protect" + ) + parser.add_argument( + "-c", + "--check_connection", + action="store_true", + help="Check cr50 console connection works", + ) + parser.add_argument( + "-s", + "--serialname", + type=str, + default="", + help="The cr50 usb serialname", + ) + parser.add_argument( + "-D", "--debug", action="store_true", help="print debug messages" + ) + parser.add_argument( + "-d", + "--device", + type=str, + default="", + help="cr50 console device ex /dev/ttyUSB0", + ) + parser.add_argument( + "-i", + "--hwid", + type=str, + default="", + help="The board hwid. Needed to generate a challenge", + ) + parser.add_argument( + "-a", + "--authcode", + type=str, + default="", + help="The authcode string from the challenge url", + ) + parser.add_argument( + "-P", "--servo_port", type=str, default="", help="the servo port" + ) + parser.add_argument( + "-I", + "--ip", + type=str, + default="", + help="The DUT IP. Necessary to do ccd open", + ) return parser.parse_args(argv) @@ -614,52 +674,59 @@ def main(argv): opts = parse_args(argv) loglevel = logging.INFO - log_format = '%(levelname)7s' + log_format = "%(levelname)7s" if opts.debug: loglevel = logging.DEBUG - log_format += ' - %(lineno)3d:%(funcName)-15s' - log_format += ' - %(message)s' + log_format += " - %(lineno)3d:%(funcName)-15s" + log_format += " - %(message)s" logging.basicConfig(level=loglevel, format=log_format) tried_authcode = False - logging.info('Running cr50_rma_open version %s', SCRIPT_VERSION) + logging.info("Running cr50_rma_open version %s", SCRIPT_VERSION) - cr50_rma_open = RMAOpen(opts.device, opts.serialname, opts.servo_port, - opts.ip) + cr50_rma_open = RMAOpen( + opts.device, opts.serialname, opts.servo_port, opts.ip + ) if opts.check_connection: sys.exit(0) if not cr50_rma_open.check(CCD_IS_UNRESTRICTED): if opts.generate_challenge: if not opts.hwid: - logging.warning('--hwid necessary to generate challenge url') + logging.warning("--hwid necessary to generate challenge url") sys.exit(0) cr50_rma_open.generate_challenge_url(opts.hwid) sys.exit(0) elif opts.authcode: - logging.info('Using authcode: %s', opts.authcode) + logging.info("Using authcode: %s", opts.authcode) cr50_rma_open.try_authcode(opts.authcode) tried_authcode = True - if not cr50_rma_open.check(WP_IS_DISABLED) and (tried_authcode or - opts.wp_disable): + if not cr50_rma_open.check(WP_IS_DISABLED) and ( + tried_authcode or opts.wp_disable + ): if not cr50_rma_open.check(CCD_IS_UNRESTRICTED): - raise ValueError("Can't disable write protect unless ccd is " - "open. Run through the rma open process first") + raise ValueError( + "Can't disable write protect unless ccd is " + "open. Run through the rma open process first" + ) if tried_authcode: - logging.warning('RMA Open did not disable write protect. File a ' - 'bug') - logging.warning('Trying to disable it manually') + logging.warning( + "RMA Open did not disable write protect. File a bug" + ) + logging.warning("Trying to disable it manually") cr50_rma_open.wp_disable() if not cr50_rma_open.check(TESTLAB_IS_ENABLED) and opts.enable_testlab: if not cr50_rma_open.check(CCD_IS_UNRESTRICTED): - raise ValueError("Can't enable testlab mode unless ccd is open." - "Run through the rma open process first") + raise ValueError( + "Can't enable testlab mode unless ccd is open." + "Run through the rma open process first" + ) cr50_rma_open.enable_testlab() cr50_rma_open.print_dut_state() -if __name__ == '__main__': +if __name__ == "__main__": sys.exit(main(sys.argv[1:])) |