Skip to content

Commit

Permalink
update spsdk to v2.0 and adapt; fixes #486
Browse files Browse the repository at this point in the history
  • Loading branch information
daringer committed Jan 29, 2024
1 parent 960d456 commit c6b2063
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions pynitrokey/nk3/bootloader/lpc55.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@
import sys
from typing import List, Optional, Tuple

from spsdk.mboot import McuBoot, StatusCode
from spsdk.mboot.interfaces.usb import RawHid
from spsdk.mboot.error_codes import StatusCode
from spsdk.mboot.interfaces.usb import MbootUSBInterface
from spsdk.mboot.mcuboot import McuBoot
from spsdk.mboot.properties import PropertyTag
from spsdk.sbfile.sb2.images import BootImageV21
from spsdk.utils.interfaces.device.usb_device import UsbDevice
from spsdk.utils.usbfilter import USBDeviceFilter

from ..utils import Uuid, Version
from . import FirmwareMetadata, Nitrokey3Bootloader, ProgressCallback, Variant

RKHT = bytes.fromhex("050aad3e77791a81e59c5b2ba5a158937e9460ee325d8ccba09734b8fdebb171")
RKTH = bytes.fromhex("050aad3e77791a81e59c5b2ba5a158937e9460ee325d8ccba09734b8fdebb171")
KEK = bytes([0xAA] * 32)
UUID_LEN = 4
FILENAME_PATTERN = re.compile("(firmware|alpha)-nk3..-lpc55-(?P<version>.*)\\.sb2$")
Expand All @@ -33,7 +35,7 @@
class Nitrokey3BootloaderLpc55(Nitrokey3Bootloader):
"""A Nitrokey 3 device running the LPC55 bootloader."""

def __init__(self, device: RawHid):
def __init__(self, device: UsbDevice):
from .. import PID_NITROKEY3_LPC55_BOOTLOADER, VID_NITROKEY

if (device.vid, device.pid) != (VID_NITROKEY, PID_NITROKEY3_LPC55_BOOTLOADER):
Expand All @@ -43,7 +45,7 @@ def __init__(self, device: RawHid):
f"got {device.vid:x}:{device.pid:x}"
)
self._path = device.path
self.device = McuBoot(device)
self.device = McuBoot(MbootUSBInterface(device))

def __enter__(self) -> "Nitrokey3BootloaderLpc55":
self.device.open()
Expand Down Expand Up @@ -122,7 +124,7 @@ def list() -> List["Nitrokey3BootloaderLpc55"]:
f"0x{VID_NITROKEY:x}:0x{PID_NITROKEY3_LPC55_BOOTLOADER:x}"
)
devices = []
for device in RawHid.enumerate(device_filter):
for device in UsbDevice.enumerate(device_filter):
try:
devices.append(Nitrokey3BootloaderLpc55(device))
except ValueError:
Expand All @@ -134,7 +136,7 @@ def list() -> List["Nitrokey3BootloaderLpc55"]:
@staticmethod
def open(path: str) -> Optional["Nitrokey3BootloaderLpc55"]:
device_filter = USBDeviceFilter(path)
devices = RawHid.enumerate(device_filter)
devices = UsbDevice.enumerate(device_filter)
if len(devices) == 0:
logger.warn(f"No HID device at {path}")
return None
Expand All @@ -156,9 +158,9 @@ def parse_firmware_image(data: bytes) -> FirmwareMetadata:
version = Version.from_bcd_version(image.header.product_version)
metadata = FirmwareMetadata(version=version)
if image.cert_block:
if image.cert_block.rkht == RKHT:
if image.cert_block.rkth == RKTH:
metadata.signed_by = "Nitrokey"
metadata.signed_by_nitrokey = True
else:
metadata.signed_by = f"unknown issuer (RKHT: {image.cert_block.rkht.hex()})"
metadata.signed_by = f"unknown issuer (RKTH: {image.cert_block.rkth.hex()})"
return metadata
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies = [
"python-dateutil ~= 2.7.0",
"pyusb",
"requests",
"spsdk >=1.11.0,<1.12.0",
"spsdk >=2.0,<2.1",
"tqdm",
"tlv8",
"typing_extensions ~= 4.3.0",
Expand Down

0 comments on commit c6b2063

Please sign in to comment.