Skip to content

Commit e3c3834

Browse files
Merge pull request Nitrokey#177 from Nitrokey/update-connection-error
nk3 update: Handle bootloader connection issues
2 parents 9455b06 + 61ce433 commit e3c3834

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

pynitrokey/cli/nk3/__init__.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright 2021 Nitrokey Developers
3+
# Copyright 2021-2022 Nitrokey Developers
44
#
55
# Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
66
# http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
@@ -16,6 +16,7 @@
1616
from typing import List, Optional, TypeVar
1717

1818
import click
19+
from spsdk.mboot.exceptions import McuBootConnectionError
1920

2021
from pynitrokey.helpers import ProgressBar, local_critical, local_print
2122
from pynitrokey.nk3 import list as list_nk3
@@ -366,8 +367,29 @@ def update(ctx: Context, image: Optional[str], experimental: bool) -> None:
366367
)
367368
raise click.Abort()
368369

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+
)
371393
elif isinstance(device, Nitrokey3Bootloader):
372394
_print_version_warning(metadata)
373395
_print_update_warning()

pynitrokey/stubs/spsdk/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@
66
# http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
77
# http://opensource.org/licenses/MIT>, at your option. This file may not be
88
# copied, modified, or distributed except according to those terms.
9+
10+
11+
class SPSDKError(Exception):
12+
...
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2022 Nitrokey Developers
4+
#
5+
# Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
6+
# http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
7+
# http://opensource.org/licenses/MIT>, at your option. This file may not be
8+
# copied, modified, or distributed except according to those terms.
9+
10+
from spsdk import SPSDKError
11+
12+
class McuBootError(SPSDKError): ...
13+
class McuBootConnectionError(McuBootError): ...

0 commit comments

Comments
 (0)