Debian Codegraph @

src/nitrokey/trussed/_bootloader/nrf52_upload/dfu/manifest.py

raw
1 #
2 # Copyright (c) 2016 Nordic Semiconductor ASA
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without modification,
6 # are permitted provided that the following conditions are met:
7 #
8 # 1. Redistributions of source code must retain the above copyright notice, this
9 # list of conditions and the following disclaimer.
10 #
11 # 2. Redistributions in binary form must reproduce the above copyright notice, this
12 # list of conditions and the following disclaimer in the documentation and/or
13 # other materials provided with the distribution.
14 #
15 # 3. Neither the name of Nordic Semiconductor ASA nor the names of other
16 # contributors to this software may be used to endorse or promote products
17 # derived from this software without specific prior written permission.
18 #
19 # 4. This software must only be used in or with a processor manufactured by Nordic
20 # Semiconductor ASA, or in or with a processor manufactured by a third party that
21 # is used in combination with a processor manufactured by Nordic Semiconductor.
22 #
23 # 5. Any software provided in binary or object form under this license must not be
24 # reverse engineered, decompiled, modified and/or disassembled.
25 #
26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
27 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
30 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
33 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #
37
38 # Python libraries
39 import json
40 from typing import Any, Optional, Union
41
42
43 class FWMetaData:
44 def __init__(
45 self,
46 is_debug: Optional[Any] = None,
47 hw_version: Optional[Any] = None,
48 fw_version: Optional[Any] = None,
49 softdevice_req: Optional[Any] = None,
50 sd_size: Optional[Any] = None,
51 bl_size: Optional[Any] = None,
52 ) -> None:
53 """
54 The FWMetaData data model.
55
56 :param bool is_debug: debug mode on
57 :param int hw_version: hardware version
58 :param int fw_version: application or bootloader version
59 :param list softdevice_req: softdevice requirements
60 :param int sd_size SoftDevice size
61 :param int bl_size Bootloader size
62 :return:FWMetaData
63 """
64 self.is_debug = is_debug
65 self.hw_version = hw_version
66 self.fw_version = fw_version
67 self.softdevice_req = softdevice_req
68 self.sd_size = sd_size
69 self.bl_size = bl_size
70
71
72 class Firmware:
73 info_read_only_metadata: Optional[FWMetaData]
74
75 def __init__(
76 self,
77 bin_file: Optional[str] = None,
78 dat_file: Optional[str] = None,
79 info_read_only_metadata: Optional[dict[str, Any]] = None,
80 ) -> None:
81 """
82 The firmware datamodel
83
84 :param str bin_file: Firmware binary file
85 :param str dat_file: Firmware .dat file (init packet for Nordic DFU)
86 :param int info_read_only_metadata: The metadata about this firwmare image
87 :return:
88 """
89 self.dat_file = dat_file
90 self.bin_file = bin_file
91
92 if info_read_only_metadata:
93 self.info_read_only_metadata = FWMetaData(**info_read_only_metadata)
94 else:
95 self.info_read_only_metadata = None
96
97
98 class SoftdeviceBootloaderFirmware(Firmware):
99 def __init__(
100 self,
101 bin_file: Optional[str] = None,
102 dat_file: Optional[str] = None,
103 info_read_only_metadata: Optional[dict[str, Any]] = None,
104 ) -> None:
105 """
106 The SoftdeviceBootloaderFirmware data model
107
108 :param str bin_file: Firmware binary file
109 :param str dat_file: Firmware .dat file (init packet for Nordic DFU)
110 :param int info_read_only_metadata: The metadata about this firwmare image
111 :return: SoftdeviceBootloaderFirmware
112 """
113 super().__init__(bin_file, dat_file, info_read_only_metadata)
114
115
116 class Manifest:
117 def __init__(
118 self,
119 application: Optional[dict[str, Any]] = None,
120 bootloader: Optional[dict[str, Any]] = None,
121 softdevice: Optional[dict[str, Any]] = None,
122 softdevice_bootloader: Optional[dict[str, Any]] = None,
123 ) -> None:
124 """
125 The Manifest data model.
126
127 :param dict application: Application firmware in package
128 :param dict bootloader: Bootloader firmware in package
129 :param dict softdevice: Softdevice firmware in package
130 :param dict softdevice_bootloader: Combined softdevice and bootloader firmware in package
131 :return: Manifest
132 """
133 self.softdevice_bootloader = (
134 SoftdeviceBootloaderFirmware(**softdevice_bootloader) if softdevice_bootloader else None
135 )
136
137 self.softdevice = Firmware(**softdevice) if softdevice else None
138 self.bootloader = Firmware(**bootloader) if bootloader else None
139 self.application = Firmware(**application) if application else None
140
141 @staticmethod
142 def from_json(data: Union[bytes, str]) -> "Manifest":
143 """
144 Parses a manifest according to Nordic DFU package specification.
145
146 :param str data: The manifest in string format
147 :return: Manifest
148 """
149 kwargs = json.loads(data)
150 return Manifest(**kwargs["manifest"])