|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | # |
3 | | -# Copyright 2021 Nitrokey Developers |
| 3 | +# Copyright 2021-2022 Nitrokey Developers |
4 | 4 | # |
5 | 5 | # Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or |
6 | 6 | # http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or |
|
16 | 16 | from typing import List, Optional, TypeVar |
17 | 17 |
|
18 | 18 | import click |
| 19 | +from spsdk.mboot.exceptions import McuBootConnectionError |
19 | 20 |
|
20 | 21 | from pynitrokey.helpers import ProgressBar, local_critical, local_print |
21 | 22 | from pynitrokey.nk3 import list as list_nk3 |
@@ -366,8 +367,29 @@ def update(ctx: Context, image: Optional[str], experimental: bool) -> None: |
366 | 367 | ) |
367 | 368 | raise click.Abort() |
368 | 369 |
|
369 | | - with _await_bootloader(ctx) as bootloader: |
370 | | - _perform_update(bootloader, data) |
| 370 | + retries = 3 |
| 371 | + exc = None |
| 372 | + for i in range(retries): |
| 373 | + logger.debug( |
| 374 | + f"Trying to connect to bootloader, try {i + 1} of {retries}" |
| 375 | + ) |
| 376 | + try: |
| 377 | + with _await_bootloader(ctx) as bootloader: |
| 378 | + _perform_update(bootloader, data) |
| 379 | + break |
| 380 | + except McuBootConnectionError as e: |
| 381 | + logger.debug("Received connection error", exc_info=True) |
| 382 | + exc = e |
| 383 | + if i + 1 < retries: |
| 384 | + time.sleep(0.5) |
| 385 | + else: |
| 386 | + msgs = ["Failed to connect to Nitrokey 3 bootloader"] |
| 387 | + if platform.system() == "Linux": |
| 388 | + msgs += ["Are the Nitrokey udev rules installed and active?"] |
| 389 | + local_critical( |
| 390 | + *msgs, |
| 391 | + exc, |
| 392 | + ) |
371 | 393 | elif isinstance(device, Nitrokey3Bootloader): |
372 | 394 | _print_version_warning(metadata) |
373 | 395 | _print_update_warning() |
|
0 commit comments