Skip to content

Commit 1604d97

Browse files
committed
[ot] scripts/opentitan: spidevflash.py: add support for Unix connection and retry count
Signed-off-by: Emmanuel Blot <eblot@rivosinc.com>
1 parent af17449 commit 1604d97

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

docs/opentitan/spidevflash.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,43 @@
55
## Usage
66

77
````text
8-
usage: spidevflash.py [-h] -f FILE [-a ADDRESS] [-r HOST] [-p PORT] [-v] [-d]
8+
usage: spidevflash.py [-h] -f FILE [-a ADDRESS] [-S SOCKET] [-R RETRY_COUNT]
9+
[-r HOST] [-p PORT] [-v] [-d]
910
1011
SPI device flasher tool.
1112
1213
options:
1314
-h, --help show this help message and exit
14-
-f FILE, --file FILE Binary file to flash
15-
-a ADDRESS, --address ADDRESS
16-
Address in the SPI flash (default to 0)
17-
-r HOST, --host HOST remote host name (default: localhost)
18-
-p PORT, --port PORT remote host TCP port (defaults to 8004)
15+
-f, --file FILE Binary file to flash
16+
-a, --address ADDRESS
17+
Address in the SPI flash (default: 0)
18+
-S, --socket SOCKET connection string
19+
-R, --retry-count RETRY_COUNT
20+
connection retry count (default: 1)
21+
-r, --host HOST remote host name (default: localhost)
22+
-p, --port PORT remote host TCP port (defaults to 8004)
1923
-v, --verbose increase verbosity
2024
-d, --debug enable debug mode
2125
````
2226

2327
### Arguments
2428

25-
* `-a` specify an alernative start address
29+
* `-a` specify an alernative start address'
2630

2731
* `-d` only useful to debug the script, reports any Python traceback to the standard error stream.
2832

29-
* `-f` specify the binary file to upload
33+
* `-f` specify the binary file to upload'
3034

31-
* `-p` specify an alternative port for the TCP connection on the QEMU instance
35+
* `-p` specify an alternative port for the TCP connection on the QEMU instance. Mutually exclusive
36+
with `-S`.
3237

33-
* `-r` specify the name or address of the remote host running the QEMU instance
38+
* `-R` specify the number of connection attempts before giving up.
3439

40+
* `-r` specify the name or address of the remote host running the QEMU instance. Mutually exclusive
41+
with `-S`.
42+
43+
* `-S` specify a connection string to the remote host running the QEMU instance, _e.g._
44+
`tcp:localhost:8004` or `unix:/tmp/socket`. Mutually exclusive with `-r` and `-p`.
3545
* `-v` can be repeated to increase verbosity of the script, mostly for debug purpose.
3646

3747
### Examples

scripts/opentitan/spidevflash.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
:author: Emmanuel Blot <eblot@rivosinc.com>
66
"""
77

8-
# Copyright (c) 2024 Rivos, Inc.
8+
# Copyright (c) 2024-2025 Rivos, Inc.
99
# SPDX-License-Identifier: Apache2
1010

1111
from argparse import ArgumentParser, FileType
@@ -14,6 +14,7 @@
1414
from os.path import dirname, join as joinpath, normpath
1515
from time import sleep, time as now
1616
from traceback import format_exc
17+
from typing import Optional
1718
import sys
1819

1920
QEMU_PYPATH = joinpath(dirname(dirname(dirname(normpath(__file__)))),
@@ -35,9 +36,30 @@ def __init__(self):
3536
self._log = getLogger('spidev.flash')
3637
self._spidev = SpiDevice()
3738

38-
def connect(self, host: str, port: int):
39-
"""Connect to the remote SPI device and wait for sync."""
40-
self._spidev.connect(host, port)
39+
def connect(self, host: str, port: Optional[int], retry_count: int = 1,
40+
sync_time: Optional[float] = None):
41+
"""Connect to the remote SPI device and wait for sync.
42+
43+
:param host: a hostname or a connection string
44+
:param port: a TCP port, should be None if a connection string is
45+
defined
46+
:param retry_count: how many attempts should be made at most to
47+
coonnect to the remote peer (once per second)
48+
:param sync_time: max allowed synchronization time once a connection
49+
is established to receive a valid JEDEC ID.
50+
51+
Supported connection string format:
52+
- tcp:<host>:<port>
53+
- unix:<path>
54+
"""
55+
while True:
56+
try:
57+
self._spidev.connect(host, port)
58+
break
59+
except TimeoutError:
60+
retry_count -= 1
61+
if not retry_count:
62+
raise
4163
self._wait_for_remote()
4264

4365
def disconnect(self):
@@ -95,12 +117,14 @@ def main():
95117
help='Binary file to flash')
96118
argparser.add_argument('-a', '--address', type=HexInt.parse,
97119
default='0',
98-
help='Address in the SPI flash (default to 0)')
120+
help='Address in the SPI flash (default: 0)')
121+
argparser.add_argument('-S', '--socket',
122+
help='connection string')
123+
argparser.add_argument('-R', '--retry-count', type=int, default=1,
124+
help='connection retry count (default: 1)')
99125
argparser.add_argument('-r', '--host',
100-
default='127.0.0.1',
101126
help='remote host name (default: localhost)')
102127
argparser.add_argument('-p', '--port', type=int,
103-
default=SpiDeviceFlasher.DEFAULT_PORT,
104128
help=f'remote host TCP port (defaults to '
105129
f'{SpiDeviceFlasher.DEFAULT_PORT})')
106130
argparser.add_argument('-v', '--verbose', action='count',
@@ -113,7 +137,15 @@ def main():
113137
configure_loggers(args.verbose, 'spidev')
114138

115139
flasher = SpiDeviceFlasher()
116-
flasher.connect(args.host, args.port)
140+
if args.socket:
141+
if any((args.host, args.port)):
142+
argparser.error('Connection string is mutually exclusive '
143+
'with host and port')
144+
flasher.connect(args.socket, None, retry_count=args.retry_count)
145+
else:
146+
flasher.connect(args.host or 'localhost',
147+
args.port or SpiDeviceFlasher.DEFAULT_PORT,
148+
retry_count=args.retry_count)
117149
data = args.file.read()
118150
args.file.close()
119151
flasher.program(data, args.address)

0 commit comments

Comments
 (0)