summaryrefslogtreecommitdiff
path: root/util/ec_openocd.py
diff options
context:
space:
mode:
authorFabio Baltieri <fabiobaltieri@google.com>2023-03-30 17:18:02 +0000
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-04-03 19:04:01 +0000
commit77c838c7b8ed56c92aa1ed6bf8c5df4447253e30 (patch)
tree3e8b4307cd23e32a0fc7b94ae7e2a30e348c3cf5 /util/ec_openocd.py
parentc1624d1d6a1c9e05cf7dd898c63047f8095f6ec2 (diff)
downloadchrome-ec-77c838c7b8ed56c92aa1ed6bf8c5df4447253e30.tar.gz
ec_openocd: fix a bunch of presubmit errors
Fix a bunch of existing presubmit detected cosmetic issues. No presubmit warning after this patch. BRANCH=none BUG=b:276311425 TEST=repo upload --cbr . Change-Id: Iccd2f39ded16fb9e36283cb80652aa56c27df460 Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4386299 Reviewed-by: Robert Zieba <robertzieba@google.com>
Diffstat (limited to 'util/ec_openocd.py')
-rwxr-xr-xutil/ec_openocd.py128
1 files changed, 70 insertions, 58 deletions
diff --git a/util/ec_openocd.py b/util/ec_openocd.py
index 325e1ab7c8..5da0479ed9 100755
--- a/util/ec_openocd.py
+++ b/util/ec_openocd.py
@@ -3,6 +3,9 @@
# Copyright 2022 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
+"""
+Flashes and debugs the EC through openocd
+"""
import argparse
import dataclasses
@@ -16,10 +19,6 @@ import sys
import time
-"""
-Flashes and debugs the EC through openocd
-"""
-
EC_BASE = pathlib.Path(__file__).parent.parent
# GDB variant to use if the board specific one is not found
@@ -28,6 +27,8 @@ FALLBACK_GDB_VARIANT = "gdb-multiarch"
@dataclasses.dataclass
class BoardInfo:
+ """Holds the board specific parameters."""
+
gdb_variant: str
num_breakpoints: int
num_watchpoints: int
@@ -41,6 +42,7 @@ boards = {
def create_openocd_args(interface, board):
+ """Return a list of arguments for initializing OpenOCD."""
if not board in boards:
raise RuntimeError(f"Unsupported board {board}")
@@ -58,6 +60,7 @@ def create_openocd_args(interface, board):
def create_gdb_args(board, port, executable, attach):
+ """Return a list of arguments for initializing GDB."""
if not board in boards:
raise RuntimeError(f"Unsupported board {board}")
@@ -94,73 +97,79 @@ def create_gdb_args(board, port, executable, attach):
def flash(interface, board, image, verify):
+ """Run openocd to flash the specified image file."""
print(f"Flashing image {image}")
# Run OpenOCD and pipe its output to stdout
# OpenOCD will shutdown after the flashing is completed
args = create_openocd_args(interface, board)
args += ["-c", f'init; flash_target "{image}" {int(verify)}; shutdown']
- subprocess.run(args, stdout=sys.stdout, stderr=subprocess.STDOUT)
+ subprocess.run(
+ args, stdout=sys.stdout, stderr=subprocess.STDOUT, check=True
+ )
def debug(interface, board, port, executable, attach):
+ """Start OpenOCD and connect GDB to it."""
# Start OpenOCD in the background
openocd_args = create_openocd_args(interface, board)
openocd_args += ["-c", f"gdb_port {port}"]
+ openocd_out = ""
- openocd = subprocess.Popen(
+ with subprocess.Popen( # pylint: disable=subprocess-popen-preexec-fn
openocd_args,
encoding="utf-8",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
preexec_fn=os.setsid,
- )
-
- # Wait for OpenOCD to start, it'll open a port for GDB connections
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- connected = False
- for i in range(0, 10):
- print("Waiting for OpenOCD to start...")
- connected = sock.connect_ex(("localhost", port))
- if connected:
- break
-
- time.sleep(1000)
-
- if not connected:
- print(f"Failed to connect to OpenOCD on port {port}")
- return
-
- sock.close()
-
- old_sigint = signal.signal(signal.SIGINT, signal.SIG_IGN)
-
- # Start GDB
- gdb_args = create_gdb_args(board, port, executable, attach)
- gdb = subprocess.Popen(
- gdb_args, stdout=sys.stdout, stderr=subprocess.STDOUT, stdin=sys.stdin
- )
-
- openocd_out = ""
- while gdb.poll() == None and openocd.poll() == None:
- (output, _) = openocd.communicate()
- openocd_out += output
-
- signal.signal(signal.SIGINT, old_sigint)
-
- # Wait for OpenOCD to shutdown
- print("Waiting for OpenOCD to finish...")
- if openocd.poll() == None:
- try:
- # Read the last bit of stdout
- (output, _) = openocd.communicate(timeout=3)
- openocd_out += output
- except subprocess.TimeoutExpired:
- # OpenOCD didn't shutdown, kill it
- openocd.kill()
-
- if openocd.returncode != 0:
- print("OpenOCD failed to shutdown cleanly: ")
+ ) as openocd:
+
+ # Wait for OpenOCD to start, it'll open a port for GDB connections
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ connected = False
+ for _ in range(0, 10):
+ print("Waiting for OpenOCD to start...")
+ connected = sock.connect_ex(("localhost", port))
+ if connected:
+ break
+
+ time.sleep(1000)
+
+ if not connected:
+ print(f"Failed to connect to OpenOCD on port {port}")
+ return
+
+ sock.close()
+
+ old_sigint = signal.signal(signal.SIGINT, signal.SIG_IGN)
+
+ # Start GDB
+ gdb_args = create_gdb_args(board, port, executable, attach)
+ with subprocess.Popen(
+ gdb_args,
+ stdout=sys.stdout,
+ stderr=subprocess.STDOUT,
+ stdin=sys.stdin,
+ ) as gdb:
+ while gdb.poll() is None and openocd.poll() is None:
+ (output, _) = openocd.communicate()
+ openocd_out += output
+
+ signal.signal(signal.SIGINT, old_sigint)
+
+ # Wait for OpenOCD to shutdown
+ print("Waiting for OpenOCD to finish...")
+ if openocd.poll() is None:
+ try:
+ # Read the last bit of stdout
+ (output, _) = openocd.communicate(timeout=3)
+ openocd_out += output
+ except subprocess.TimeoutExpired:
+ # OpenOCD didn't shutdown, kill it
+ openocd.kill()
+
+ if openocd.returncode != 0:
+ print("OpenOCD failed to shutdown cleanly: ")
print(openocd_out)
@@ -182,18 +191,21 @@ def debug_external(board, port, executable, attach):
def get_flash_file(board):
+ """Returns the path of the main output file for the board."""
return (
EC_BASE / "build" / "zephyr" / board / "output" / "ec.bin"
).resolve()
def get_executable_file(board):
+ """Returns the path of the main RO executable file for the board."""
return (
EC_BASE / "build" / "zephyr" / board / "output" / "zephyr.ro.elf"
).resolve()
def main():
+ """Main function."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--board",
@@ -233,8 +245,8 @@ def main():
debug_parser = sub_parsers.add_parser(
"debug",
- help="Debugs the target EC through GDB, \
- FILE selects the executable to load debug info from, defaults to using the zephyr RO executable",
+ help="Debugs the target EC through GDB, FILE selects the executable to \
+ load debug info from, defaults to using the zephyr RO executable",
)
debug_parser.set_defaults(command="debug")
debug_parser.add_argument(
@@ -260,18 +272,18 @@ def main():
args = parser.parse_args()
# Get the image path if we were given one
target_file = None
- if args.file != None:
+ if args.file is not None:
target_file = args.file.resolve()
if args.command == "flash":
image_file = (
- get_flash_file(args.board) if target_file == None else target_file
+ get_flash_file(args.board) if target_file is None else target_file
)
flash(args.interface, args.board, image_file, args.verify)
elif args.command == "debug":
executable_file = (
get_executable_file(args.board)
- if target_file == None
+ if target_file is None
else target_file
)
if args.external_gdbserver: