From 74e34f6bafc551dcb96cab22c558226f55b5b4d0 Mon Sep 17 00:00:00 2001 From: Craig Hesling Date: Mon, 26 Jul 2021 17:13:22 -0400 Subject: util/flash_jlink: Add check if J-Link server is running This server could be either the local JLinkRemoteServerCLExe program or a physical J-Link connected over the network. Since we are not sure if the remote server is on the localhost, we make a TCP connection to the server to check if it is running. BRANCH=none BUG=none TEST=# No JLinkRemoteServerCLExe running make proj-bloonchipper -j ./util/flash_jlink.py --board bloonchipper # Ensure the cute error is given TEST=JLinkRemoteServerCLExe # in background make proj-bloonchipper -j ./util/flash_jlink.py --board bloonchipper # Ensure all goes well Change-Id: I4f24e5f53545aa3b1482066fe8ba12d5f1715d42 Signed-off-by: Craig Hesling Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3055114 Reviewed-by: Josie Nordrum --- util/flash_jlink.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/util/flash_jlink.py b/util/flash_jlink.py index fdd0bd87a1..e30badd059 100755 --- a/util/flash_jlink.py +++ b/util/flash_jlink.py @@ -17,9 +17,11 @@ import argparse import logging import os import shutil +import socket import subprocess import sys import tempfile +import time # Commands are documented here: https://wiki.segger.com/J-Link_Commander JLINK_COMMANDS = ''' @@ -57,6 +59,32 @@ BOARD_CONFIGS = { } +def is_tcp_port_open(host: str, tcp_port: int) -> bool: + """Checks if the TCP host port is open.""" + + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(2) # 2 Second Timeout + try: + sock.connect((host, tcp_port)) + sock.shutdown(socket.SHUT_RDWR) + except ConnectionRefusedError: + return False + except socket.timeout: + return False + finally: + sock.close() + # Other errors are propagated as odd exceptions. + + # We shutdown and closed the connection, but the server may need a second + # to start listening again. If the following error is seen, this timeout + # should be increased. 300ms seems to be the minimum. + # + # Connecting to J-Link via IP...FAILED: Can not connect to J-Link via \ + # TCP/IP (127.0.0.1, port 19020) + time.sleep(0.5) + return True + + def create_jlink_command_file(firmware_file, config): tmp = tempfile.NamedTemporaryFile() tmp.write(JLINK_COMMANDS.format(FIRMWARE=firmware_file, @@ -72,6 +100,22 @@ def flash(jlink_exe, ip, device, interface, cmd_file): ] if ip: + ip_components = ip.split(':') + if len(ip_components) != 2: + logging.error(f'Given ip arg "{ip}" is malformed.') + return 1 + ip_host = ip_components[0] + try: + ip_port = int(ip_components[1]) + except ValueError: + logging.error( + f'Given ip arg port "{ip_components[1]}" is malformed.') + return 1 + if not is_tcp_port_open(ip_host, ip_port): + logging.error( + f'JLink server doesn\'t seem to be listening on {ip}.') + logging.error('Ensure that JLinkRemoteServerCLExe is running.') + return 1 cmd.extend(['-ip', ip]) cmd.extend([ -- cgit v1.2.1