Skip to content

Commit

Permalink
nk3: Show bootloader error if firmware update fails
Browse files Browse the repository at this point in the history
This patch adds the bootloader error code and message to the output if
the firmware update fails.  Previously we only printed a generic “update
failed” error message.
  • Loading branch information
robin-nitrokey committed Jan 17, 2022
1 parent 613e238 commit 5e71d7a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pynitrokey/cli/nk3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ def _perform_update(device: Nitrokey3Bootloader, image: bytes) -> None:
logger.debug("Firmware update finished successfully")
device.reboot()
else:
local_critical("Firmware update failed")
(code, message) = device.status
local_critical(f"Firmware update failed with status code {code}: {message}")


@nk3.command()
Expand Down
10 changes: 8 additions & 2 deletions pynitrokey/nk3/bootloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import logging
import platform
import sys
from typing import List, Optional
from typing import List, Optional, Tuple

from spsdk.mboot import McuBoot
from spsdk.mboot import McuBoot, StatusCode
from spsdk.mboot.interfaces import RawHid
from spsdk.mboot.properties import PropertyTag
from spsdk.sbfile.images import BootImageV21
Expand Down Expand Up @@ -57,6 +57,12 @@ def path(self) -> str:
def name(self) -> str:
return "Nitrokey 3 Bootloader"

@property
def status(self) -> Tuple[int, str]:
code = self.device.status_code
message = StatusCode.desc(code)
return (code, message)

def close(self) -> None:
self.device.close()

Expand Down
7 changes: 7 additions & 0 deletions pynitrokey/stubs/spsdk/mboot/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
# http://opensource.org/licenses/MIT>, at your option. This file may not be
# copied, modified, or distributed except according to those terms.

from enum import Enum
from typing import List, Optional

from .interfaces import Interface
from .properties import PropertyTag

class StatusCode(int):
@classmethod
def desc(cls, key: int) -> str: ...

class McuBoot:
def __init__(self, device: Interface) -> None: ...
@property
def status_code(self) -> StatusCode: ...
def open(self) -> None: ...
def close(self) -> None: ...
def receive_sb_file(self, data: bytes) -> bool: ...
Expand Down

0 comments on commit 5e71d7a

Please sign in to comment.