You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2019/07/05 15:55:48 UTC

[GitHub] [mynewt-core] agross-korg commented on a change in pull request #1906: hw/bsp/dialog: Add generation of dialog header on images

agross-korg commented on a change in pull request #1906: hw/bsp/dialog: Add generation of dialog header on images
URL: https://github.com/apache/mynewt-core/pull/1906#discussion_r300733787
 
 

 ##########
 File path: hw/bsp/dialog_da1469x-dk-pro/da1469x_header_tool.py
 ##########
 @@ -0,0 +1,282 @@
+#! /usr/bin/env python3
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+import re
+import click
+import io
+import os.path
+import struct
+import binascii
+import time
+from typing import NamedTuple
+import base64
+
+from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
+from cryptography.hazmat.backends import default_backend
+from cryptography.hazmat.primitives import serialization
+
+from cryptography.hazmat.primitives import hashes
+
+# Add in patch to mcuboot.  This is required to pull in keys module from imgtool
+import sys
+sys.path.append(os.getcwd() + "/repos/mcuboot/scripts/imgtool")
+import keys as keys
+
+class product_header(NamedTuple):
+    ident: bytes
+    active_addr: int
+    update_addr: int
+    cmda_reg: int
+    cmdb_reg: int
+    flash_ident: bytes
+    flash_length: int
+    cmd_seq: bytes
+
+class key_revocation_entry(NamedTuple):
+    key_type: int
+    key_index: int
+
+class revocation_section(NamedTuple):
+    ident: bytes
+    length: int
+
+class device_administration_section(NamedTuple):
+    ident: bytes
+    length: int
+
+class signature_section(NamedTuple):
+    sig_idx: int
+    enc_idx: int
+    nonce: bytes
+    ident: bytes
+    length: int
+    signature: bytes
+
+class security_section(NamedTuple):
+    ident: bytes
+    length: int
+
+class fw_header(NamedTuple):
+    ident: bytes
+    length: int
+    crc: int
+    version: bytes
+    timestamp: int
+    ivt_offset: int
+
+def crc16(data : bytearray, offset , length):
+    if data is None or offset < 0 or offset > len(data)- 1 and offset+length > len(data):
+        return 0
+    crc = 0xFFFF
+    for i in range(0, length):
+        crc ^= data[offset + i] << 8
+        for j in range(0,8):
+            if (crc & 0x8000) > 0:
+                crc =(crc << 1) ^ 0x1021
+            else:
+                crc = crc << 1
+    return crc & 0xFFFF
+
+class da1469x_fw_image(object):
+
+    def __init__(self, path, encrypt_file, sig_file, sig_slot, decrypt_slot,
+                 revoke, version):
+        header = product_header(b'Pp', 0x2000, 0x2000, 0xa8a500eb, 0x66,
+                            b'\xaa\x11', 3, b'\x01\x40\07')
+        self.img = struct.pack('<2sIIII2sH3s', *header)
+        self.img += struct.pack('<H', crc16(self.img, 0, len(self.img)))
+        self.img += b'\xff'*(4096*2 - len(self.img))
+
+        self.img_size = os.path.getsize(path)
+        self.aes_key = None
+        self.sig_key = None
+        self.sig_slot = None
+        self.decrypt_slot = None
+        self.revocation_list = revoke
+
+        if version:
+            self.version = version.encode()
+        else:
+            self.version = b'\x00'*16
+
+        with open(path, "rb") as binary_file:
+            # Read the whole file at once
+            self.data = binary_file.read()
+
+        if encrypt_file != None:
 
 Review comment:
   ah right, ill fix that

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services