Debian Codegraph @

src/nitrokey/nk3/_bootloader.py

raw
1 # Copyright 2021-2022 Nitrokey Developers
2 #
3 # Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4 # http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5 # http://opensource.org/licenses/MIT>, at your option. This file may not be
6 # copied, modified, or distributed except according to those terms.
7
8 from typing import TYPE_CHECKING, List, Optional, Sequence
9
10 from nitrokey import _VID_NITROKEY
11 from nitrokey.trussed._base import Model
12 from nitrokey.trussed._bootloader import TrussedBootloader
13 from nitrokey.trussed._bootloader.lpc55 import TrussedBootloaderLpc55
14 from nitrokey.trussed._bootloader.nrf52 import SignatureKey, TrussedBootloaderNrf52
15
16
17 class NK3Bootloader(TrussedBootloader):
18 @property
19 def model(self) -> Model:
20 return Model.NK3
21
22 @staticmethod
23 def list() -> List["NK3Bootloader"]:
24 devices: List[NK3Bootloader] = []
25 devices.extend(NK3BootloaderLpc55._list())
26 devices.extend(NK3BootloaderNrf52._list())
27 return devices
28
29 @staticmethod
30 def open(path: str) -> Optional["NK3Bootloader"]:
31 lpc55 = NK3BootloaderLpc55._open(path)
32 if lpc55:
33 return lpc55
34
35 nrf52 = NK3BootloaderNrf52._open(path)
36 if nrf52:
37 return nrf52
38
39 return None
40
41
42 class NK3BootloaderLpc55(TrussedBootloaderLpc55, NK3Bootloader):
43 @property
44 def name(self) -> str:
45 return "Nitrokey 3 Bootloader (LPC55)"
46
47 @property
48 def pid(self) -> int:
49 from . import _PID_NK3_LPC55_BOOTLOADER
50
51 return _PID_NK3_LPC55_BOOTLOADER
52
53 @classmethod
54 def _list(cls) -> List["NK3BootloaderLpc55"]:
55 from . import _PID_NK3_LPC55_BOOTLOADER
56
57 return cls._list_vid_pid(_VID_NITROKEY, _PID_NK3_LPC55_BOOTLOADER)
58
59
60 class NK3BootloaderNrf52(TrussedBootloaderNrf52, NK3Bootloader):
61 @property
62 def name(self) -> str:
63 return "Nitrokey 3 Bootloader (NRF52)"
64
65 @property
66 def pid(self) -> int:
67 from . import _PID_NK3_NRF52_BOOTLOADER
68
69 return _PID_NK3_NRF52_BOOTLOADER
70
71 @classmethod
72 def _list(cls) -> List["NK3BootloaderNrf52"]:
73 from . import _PID_NK3_NRF52_BOOTLOADER
74
75 return cls._list_vid_pid(_VID_NITROKEY, _PID_NK3_NRF52_BOOTLOADER)
76
77 @classmethod
78 def _open(cls, path: str) -> Optional["NK3BootloaderNrf52"]:
79 from . import _PID_NK3_NRF52_BOOTLOADER
80
81 return cls._open_vid_pid(_VID_NITROKEY, _PID_NK3_NRF52_BOOTLOADER, path)
82
83 @property
84 def _signature_keys(self) -> Sequence[SignatureKey]:
85 from . import _NK3_DATA
86
87 return _NK3_DATA.nrf52_signature_keys
88
89
90 if TYPE_CHECKING:
91 from contextlib import AbstractContextManager
92
93 _ACM_NRF52: type[AbstractContextManager[NK3BootloaderNrf52]] = NK3BootloaderNrf52
94 _ACM_LPC55: type[AbstractContextManager[NK3BootloaderLpc55]] = NK3BootloaderLpc55