Debian Codegraph @

src/nitrokey/trussed/_bootloader/nrf52_upload/dfu/init_packet_pb.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 from . import dfu_cc_pb2 as pb
39
40
41 class InitPacketPB:
42 def __init__(self, from_bytes: bytes) -> None:
43 # construct from a protobuf string/buffer
44 self.packet = pb.Packet()
45 self.packet.ParseFromString(from_bytes)
46
47 if self.packet.HasField("signed_command"):
48 self.init_command = self.packet.signed_command.command.init
49 else:
50 self.init_command = self.packet.command.init
51
52 self._validate()
53
54 def _validate(self) -> None:
55 if (
56 self.init_command.type == pb.APPLICATION
57 or self.init_command.type == pb.EXTERNAL_APPLICATION
58 ) and self.init_command.app_size == 0:
59 raise RuntimeError(
60 "app_size is not set. It must be set when type is APPLICATION/EXTERNAL_APPLICATION"
61 )
62 elif self.init_command.type == pb.SOFTDEVICE and self.init_command.sd_size == 0:
63 raise RuntimeError("sd_size is not set. It must be set when type is SOFTDEVICE")
64 elif self.init_command.type == pb.BOOTLOADER and self.init_command.bl_size == 0:
65 raise RuntimeError("bl_size is not set. It must be set when type is BOOTLOADER")
66 elif self.init_command.type == pb.SOFTDEVICE_BOOTLOADER and (
67 self.init_command.sd_size == 0 or self.init_command.bl_size == 0
68 ):
69 raise RuntimeError(
70 "Either sd_size or bl_size is not set. Both must be set when type "
71 "is SOFTDEVICE_BOOTLOADER"
72 )
73
74 if (
75 self.init_command.fw_version < 0
76 or self.init_command.fw_version > 0xFFFFFFFF
77 or self.init_command.hw_version < 0
78 or self.init_command.hw_version > 0xFFFFFFFF
79 ):
80 raise RuntimeError(
81 "Invalid range of firmware argument. [0 - 0xffffffff] is valid range"
82 )
83
84 def get_init_command_bytes(self) -> bytes:
85 return self.init_command.SerializeToString()
86
87 def __str__(self) -> str:
88 return str(self.init_command)