Пример #1
0
def genpkg(zipfile,
           application,
           application_version,
           bootloader,
           dev_revision,
           dev_type,
           dfu_ver,
           sd_req,
           softdevice,
           key_file):
    """
    Generate a zipfile package for distribution to Apps supporting Nordic DFU OTA.
    The application, bootloader and softdevice files are converted to .bin if it is a .hex file.
    For more information on the generated init packet see:
    http://developer.nordicsemi.com/nRF51_SDK/doc/7.2.0/s110/html/a00065.html
    """
    zipfile_path = zipfile

    if application_version == 'none':
        application_version = None

    if dev_revision == 'none':
        dev_revision = None

    if dev_type == 'none':
        dev_type = None

    sd_req_list = None

    if sd_req.lower() == 'none':
        sd_req_list = []
    elif sd_req:
        try:
            # This will parse any string starting with 0x as base 16.
            sd_req_list = sd_req.split(',')
            sd_req_list = list(map(int_as_text_to_int, sd_req_list))
        except ValueError:
            raise nRFException("Could not parse value for --sd-req. "
                               "Hex values should be prefixed with 0x.")

    if key_file and dfu_ver < 0.8:
        click.echo("Key file was given, setting DFU version to 0.8")

    package = Package(dev_type,
                      dev_revision,
                      application_version,
                      sd_req_list,
                      application,
                      bootloader,
                      softdevice,
                      dfu_ver,
                      key_file)

    package.generate_package(zipfile_path)

    log_message = "Zip created at {0}".format(zipfile_path)
    try:
        click.echo(log_message)
    except OSError:
        print(log_message)
Пример #2
0
    def test_generate_package_sd_bl(self):
        self.p = Package(dev_type=1,
                         dev_rev=2,
                         app_version=100,
                         sd_req=[0x1000, 0xfffe],
                         softdevice_fw="firmwares/foo.hex",
                         bootloader_fw="firmwares/bar.hex")

        pkg_name = "mypackage.zip"

        self.p.generate_package(pkg_name, preserve_work_directory=False)

        expected_zip_content = ["manifest.json", "sd_bl.bin", "sd_bl.dat"]

        with ZipFile(pkg_name, 'r') as pkg:
            infolist = pkg.infolist()

            for file_information in infolist:
                self.assertTrue(
                    file_information.filename in expected_zip_content)
                self.assertGreater(file_information.file_size, 0)

            # Extract all and load json document to see if it is correct regarding to paths
            pkg.extractall(self.work_directory)

            with open(os.path.join(self.work_directory, 'manifest.json'),
                      'r') as f:
                _json = json.load(f)
                self.assertEqual(
                    'sd_bl.bin',
                    _json['manifest']['softdevice_bootloader']['bin_file'])
                self.assertEqual(
                    'sd_bl.dat',
                    _json['manifest']['softdevice_bootloader']['dat_file'])
Пример #3
0
def genpkg(zipfile,
           application,
           application_version,
           bootloader,
           dev_revision,
           dev_type,
           dfu_ver,
           sd_req,
           softdevice,
           key_file):
    """
    Generate a zipfile package for distribution to Apps supporting Nordic DFU OTA.
    The application, bootloader and softdevice files are converted to .bin if it is a .hex file.
    For more information on the generated init packet see:
    http://developer.nordicsemi.com/nRF51_SDK/doc/7.2.0/s110/html/a00065.html
    """
    zipfile_path = zipfile

    if application_version == 'none':
        application_version = None

    if dev_revision == 'none':
        dev_revision = None

    if dev_type == 'none':
        dev_type = None

    sd_req_list = None

    if sd_req.lower() == 'none':
        sd_req_list = []
    elif sd_req:
        try:
            # This will parse any string starting with 0x as base 16.
            sd_req_list = sd_req.split(',')
            sd_req_list = map(int_as_text_to_int, sd_req_list)
        except ValueError:
            raise nRFException("Could not parse value for --sd-req. "
                               "Hex values should be prefixed with 0x.")

    if key_file and dfu_ver < 0.8:
        click.echo("Key file was given, setting DFU version to 0.8")

    package = Package(dev_type,
                      dev_revision,
                      application_version,
                      sd_req_list,
                      application,
                      bootloader,
                      softdevice,
                      dfu_ver,
                      key_file)

    package.generate_package(zipfile_path)

    log_message = "Zip created at {0}".format(zipfile_path)
    click.echo(log_message)
Пример #4
0
def generate(zipfile,
           application,
           application_version,
           bootloader,
           bootloader_version,
           hw_version,
           sd_req,
           softdevice,
           key_file):
    """
    Generate a zip package for distribution to apps that support Nordic DFU OTA.
    The application, bootloader, and SoftDevice files are converted to .bin if supplied as .hex files.
    For more information on the generated package, see:
    http://developer.nordicsemi.com/nRF5_SDK/doc/
    """
    zipfile_path = zipfile

    if application_version == 'none':
        application_version = None

    if bootloader_version == 'none':
        bootloader_version = None

    if hw_version == 'none':
        hw_version = None

    sd_req_list = None

    if sd_req.lower() == 'none':
        sd_req_list = []
    elif sd_req:
        try:
            # This will parse any string starting with 0x as base 16.
            sd_req_list = sd_req.split(',')
            sd_req_list = map(int_as_text_to_int, sd_req_list)
        except ValueError:
            raise NordicSemiException("Could not parse value for --sd-req. "
                                      "Hex values should be prefixed with 0x.")

    signer = Signing()
    default_key = signer.load_key(key_file)
    if default_key:
        display_sec_warning()

    package = Package(hw_version,
                      application_version,
                      bootloader_version,
                      sd_req_list,
                      application,
                      bootloader,
                      softdevice,
                      key_file)

    package.generate_package(zipfile_path)

    log_message = "Zip created at {0}".format(zipfile_path)
    click.echo(log_message)
Пример #5
0
    def test_unpack_package_c(self):
        self.p = Package(app_version=100,
                         sd_req=[0x1000, 0xffff],
                         softdevice_fw="firmwares/bar.hex",
                         key_file="key.pem")
        pkg_name = os.path.join(self.work_directory, "mypackage.zip")
        self.p.generate_package(pkg_name, preserve_work_dir=False)

        unpacked_dir = os.path.join(self.work_directory, "unpacked")
        manifest = self.p.unpack_package(os.path.join(self.work_directory, pkg_name), unpacked_dir)
        self.assertIsNotNone(manifest)
        self.assertEqual(u'bar.bin', manifest.softdevice.bin_file)
Пример #6
0
def create_dfu_server(transport, zip_file_path, opts):
    '''
	Create a DFU server instance.
	:param transpoort: A transport to be used.
	:param zip_file_path: A path to the firmware package.
	:param opts: Optional parameters:
		mcast_dfu: An information if multicast DFU is enabled.
		rate: Multicast block transfer rate, in blocks per second
		reset_suppress: A delay before sending multicast reset command (in milliseconds). -1 means that no reset will be sent.
	'''
    temp_dir = tempfile.mkdtemp(prefix="nrf_dfu_")
    unpacked_zip_path = os.path.join(temp_dir, 'unpacked_zip')
    manifest = Package.unpack_package(zip_file_path, unpacked_zip_path)

    protocol = piccata.core.Coap(transport)
    transport.register_receiver(protocol)

    init_file, image_file = _get_file_names(manifest)

    with open(os.path.join(unpacked_zip_path, init_file), 'rb') as f:
        init_data = f.read()
    with open(os.path.join(unpacked_zip_path, image_file), 'rb') as f:
        image_data = f.read()

    return ThreadDfuServer(protocol, init_data, image_data, opts)
Пример #7
0
    def test_generate_package_sd_bl(self):
        self.p = Package(dev_type=1,
                         dev_rev=2,
                         app_version=100,
                         sd_req=[0x1000, 0xfffe],
                         softdevice_fw="firmwares/foo.hex",
                         bootloader_fw="firmwares/bar.hex")

        pkg_name = "mypackage.zip"

        self.p.generate_package(pkg_name, preserve_work_directory=False)

        expected_zip_content = ["manifest.json", "sd_bl.bin", "sd_bl.dat"]

        with ZipFile(pkg_name, 'r') as pkg:
            infolist = pkg.infolist()

            for file_information in infolist:
                self.assertTrue(file_information.filename in expected_zip_content)
                self.assertGreater(file_information.file_size, 0)

            # Extract all and load json document to see if it is correct regarding to paths
            pkg.extractall(self.work_directory)

            with open(os.path.join(self.work_directory, 'manifest.json'), 'r') as f:
                _json = json.load(f)
                self.assertEqual(u'sd_bl.bin', _json['manifest']['softdevice_bootloader']['bin_file'])
                self.assertEqual(u'sd_bl.dat', _json['manifest']['softdevice_bootloader']['dat_file'])
Пример #8
0
    def __init__(self, zip_file_path, dfu_transport):
        """
        Initializes the dfu upgrade, unpacks zip and registers callbacks.

        @param zip_file_path: Path to the zip file with the firmware to upgrade
        @type zip_file_path: str
        @param dfu_transport: Transport backend to use to upgrade
        @type dfu_transport: nordicsemi.dfu.dfu_transport.DfuTransport
        @return
        """
        self.zip_file_path = zip_file_path
        self.ready_to_send = True
        self.response_opcode_received = None

        self.temp_dir = tempfile.mkdtemp(prefix="nrf_dfu_")
        self.unpacked_zip_path = os.path.join(self.temp_dir, 'unpacked_zip')
        self.manifest = Package.unpack_package(self.zip_file_path,
                                               self.unpacked_zip_path)

        if dfu_transport:
            self.dfu_transport = dfu_transport

        self.dfu_transport.register_events_callback(DfuEvent.TIMEOUT_EVENT,
                                                    self.timeout_event_handler)
        self.dfu_transport.register_events_callback(DfuEvent.ERROR_EVENT,
                                                    self.error_event_handler)
Пример #9
0
    def __init__(self, zip_file_path):
        """
        @param zip_file_path: Path to the zip file with the firmware to upgrade
        """
        zip_file_path = realpath(zip_file_path)
        print(zip_file_path)

        self.temp_dir = mkdtemp(prefix="nrf_dfu_")
        self.unpacked_zip = path_join(self.temp_dir, "unpacked_zip")
        self.manifest = Package.unpack_package(zip_file_path,
                                               self.unpacked_zip)

        self.images = {}

        if self.manifest.softdevice_bootloader:
            k = "softdevice_bootloader"
            self.images[k] = DfuImage(self.unpacked_zip,
                                      self.manifest.softdevice_bootloader)

        if self.manifest.softdevice:
            k = "softdevice"
            self.images[k] = DfuImage(self.unpacked_zip,
                                      self.manifest.softdevice)

        if self.manifest.bootloader:
            k = "bootloader"
            self.images[k] = DfuImage(self.unpacked_zip,
                                      self.manifest.bootloader)

        if self.manifest.application:
            k = "application"
            self.images[k] = DfuImage(self.unpacked_zip,
                                      self.manifest.application)
Пример #10
0
    def test_generate_package_application(self):
        self.p = Package(
            dev_type=1,
            dev_rev=2,
            app_version=100,
            sd_req=[0x1000, 0xFFFE],
            app_fw="firmwares/bar.hex",
            key_file="key.pem",
        )

        pkg_name = "mypackage.zip"

        self.p.generate_package(pkg_name, preserve_work_directory=False)
        expected_zip_content = ["manifest.json", "bar.bin", "bar.dat"]

        with ZipFile(pkg_name, "r") as pkg:
            infolist = pkg.infolist()

            for file_information in infolist:
                self.assertTrue(file_information.filename in expected_zip_content)
                self.assertGreater(file_information.file_size, 0)

            # Extract all and load json document to see if it is correct regarding to paths
            pkg.extractall(self.work_directory)

            with open(os.path.join(self.work_directory, "manifest.json"), "r") as f:
                _json = json.load(f)
                self.assertEqual(u"bar.bin", _json["manifest"]["application"]["bin_file"])
                self.assertEqual(u"bar.dat", _json["manifest"]["application"]["dat_file"])
                self.assertTrue(u"softdevice" not in _json["manifest"])
                self.assertTrue(u"softdevice_bootloader" not in _json["manifest"])
                self.assertTrue(u"bootloader" not in _json["manifest"])
Пример #11
0
    def test_unpack_package_a(self):
        self.p = Package(dev_type=1,
                         dev_rev=2,
                         app_version=100,
                         sd_req=[0x1000, 0xffff],
                         softdevice_fw="firmwares/bar.hex",
                         dfu_ver=0.6)
        pkg_name = os.path.join(self.work_directory, "mypackage.zip")
        self.p.generate_package(pkg_name, preserve_work_directory=False)

        unpacked_dir = os.path.join(self.work_directory, "unpacked")
        manifest = self.p.unpack_package(
            os.path.join(self.work_directory, pkg_name), unpacked_dir)
        self.assertIsNotNone(manifest)
        self.assertEqual('bar.bin', manifest.softdevice.bin_file)
        self.assertEqual(0, manifest.softdevice.init_packet_data.ext_packet_id)
        self.assertIsNotNone(
            manifest.softdevice.init_packet_data.firmware_crc16)
Пример #12
0
def genpkg(zipfile, application, application_version, bootloader, dev_revision,
           dev_type, sd_req, softdevice, key_file):
    """
    Generate a zip package for distribution to apps that support Nordic DFU OTA.
    The application, bootloader, and SoftDevice files are converted to .bin if supplied as .hex files.
    For more information on the generated package, see:
    http://developer.nordicsemi.com/nRF5_SDK/doc/
    """
    zipfile_path = zipfile

    if application_version == 'none':
        application_version = None

    if dev_revision == 'none':
        dev_revision = None

    if dev_type == 'none':
        dev_type = None

    sd_req_list = None

    if sd_req.lower() == 'none':
        sd_req_list = []
    elif sd_req:
        try:
            # This will parse any string starting with 0x as base 16.
            sd_req_list = sd_req.split(',')
            sd_req_list = map(int_as_text_to_int, sd_req_list)
        except ValueError:
            raise nRFException("Could not parse value for --sd-req. "
                               "Hex values should be prefixed with 0x.")

    package = Package(dev_type, dev_revision, application_version, sd_req_list,
                      application, bootloader, softdevice, key_file)

    package.generate_package(zipfile_path)

    log_message = "Zip created at {0}".format(zipfile_path)
    click.echo(log_message)
Пример #13
0
def create_dfu_server(transport, zip_file_path, prefix):
    temp_dir = tempfile.mkdtemp(prefix="nrf_dfu_")
    unpacked_zip_path = os.path.join(temp_dir, 'unpacked_zip')
    manifest = Package.unpack_package(zip_file_path, unpacked_zip_path)

    init_file, image_file = _get_file_names(manifest)

    with open(os.path.join(unpacked_zip_path, init_file), 'rb') as f:
        init_data = f.read()
    with open(os.path.join(unpacked_zip_path, image_file), 'rb') as f:
        image_data = f.read()

    return ThreadDfuServer(transport, init_data, image_data, prefix)
Пример #14
0
    def __init__(self, zip_file_path, dfu_transport):
        """
        Initializes the dfu upgrade, unpacks zip and registers callbacks.

        @param zip_file_path: Path to the zip file with the firmware to upgrade
        @type zip_file_path: str
        @param dfu_transport: Transport backend to use to upgrade
        @type dfu_transport: nordicsemi.dfu.dfu_transport.DfuTransport
        @return
        """
        self.temp_dir           = tempfile.mkdtemp(prefix="nrf_dfu_")
        self.unpacked_zip_path  = os.path.join(self.temp_dir, 'unpacked_zip')
        self.manifest           = Package.unpack_package(zip_file_path, self.unpacked_zip_path)

        self.dfu_transport      = dfu_transport
Пример #15
0
    def test_generate_package_application(self):
        signer = Signing()
        signer.load_key('key.pem')

        self.p = Package(app_version=100,
                         sd_req=[0x1000, 0xfffe],
                         app_fw="firmwares/bar.hex",
                         signer=signer)

        pkg_name = "mypackage.zip"

        self.p.generate_package(pkg_name, preserve_work_dir=False)
        expected_zip_content = ["manifest.json", "bar.bin", "bar.dat"]

        with ZipFile(pkg_name, 'r') as pkg:
            infolist = pkg.infolist()

            for file_information in infolist:
                self.assertTrue(
                    file_information.filename in expected_zip_content)
                self.assertGreater(file_information.file_size, 0)

            # Extract all and load json document to see if it is correct regarding to paths
            pkg.extractall(self.work_directory)

            with open(os.path.join(self.work_directory, 'manifest.json'),
                      'r') as f:
                _json = json.load(f)
                self.assertEqual('bar.bin',
                                 _json['manifest']['application']['bin_file'])
                self.assertEqual('bar.dat',
                                 _json['manifest']['application']['dat_file'])
                self.assertTrue('softdevice' not in _json['manifest'])
                self.assertTrue(
                    'softdevice_bootloader' not in _json['manifest'])
                self.assertTrue('bootloader' not in _json['manifest'])
Пример #16
0
    def test_unpack_package_a(self):
        self.p = Package(dev_type=1,
                         dev_rev=2,
                         app_version=100,
                         sd_req=[0x1000, 0xffff],
                         softdevice_fw="firmwares/bar.hex",
                         dfu_ver=0.6)
        pkg_name = os.path.join(self.work_directory, "mypackage.zip")
        self.p.generate_package(pkg_name, preserve_work_directory=False)

        unpacked_dir = os.path.join(self.work_directory, "unpacked")
        manifest = self.p.unpack_package(os.path.join(self.work_directory, pkg_name), unpacked_dir)
        self.assertIsNotNone(manifest)
        self.assertEqual(u'bar.bin', manifest.softdevice.bin_file)
        self.assertEqual(0, manifest.softdevice.init_packet_data.ext_packet_id)
        self.assertIsNotNone(manifest.softdevice.init_packet_data.firmware_crc16)
Пример #17
0
    def __init__(self, zip_file_path, dfu_transport):
        """
        Initializes the dfu upgrade, unpacks zip and registers callbacks.

        @param zip_file_path: Path to the zip file with the firmware to upgrade
        @type zip_file_path: str
        @param dfu_transport: Transport backend to use to upgrade
        @type dfu_transport: nordicsemi.dfu.dfu_transport.DfuTransport
        @return
        """
        self.temp_dir = tempfile.mkdtemp(prefix="nrf_dfu_")
        self.unpacked_zip_path = os.path.join(self.temp_dir, 'unpacked_zip')
        self.manifest = Package.unpack_package(zip_file_path,
                                               self.unpacked_zip_path)

        self.dfu_transport = dfu_transport
Пример #18
0
    def __init__(self, zip_file_path, dfu_transport, connect_delay):
        """
        Initializes the dfu upgrade, unpacks zip and registers callbacks.

        @param zip_file_path: Path to the zip file with the firmware to upgrade
        @type zip_file_path: str
        @param dfu_transport: Transport backend to use to upgrade
        @type dfu_transport: nordicsemi.dfu.dfu_transport.DfuTransport
        @param connect_delay: Delay in seconds before each connection to the DFU target
        @type connect_delay: int
        @return
        """
        self.temp_dir           = tempfile.mkdtemp(prefix="nrf_dfu_")
        self.unpacked_zip_path  = os.path.join(self.temp_dir, 'unpacked_zip')
        self.manifest           = Package.unpack_package(zip_file_path, self.unpacked_zip_path)

        self.dfu_transport      = dfu_transport

        if connect_delay is not None:
            self.connect_delay = connect_delay
        else:
            self.connect_delay = 3
Пример #19
0
    def __init__(self, zip_file_path, dfu_transport, connect_delay):
        """
        Initializes the dfu upgrade, unpacks zip and registers callbacks.

        @param zip_file_path: Path to the zip file with the firmware to upgrade
        @type zip_file_path: str
        @param dfu_transport: Transport backend to use to upgrade
        @type dfu_transport: nordicsemi.dfu.dfu_transport.DfuTransport
        @param connect_delay: Delay in seconds before each connection to the DFU target
        @type connect_delay: int
        @return
        """
        self.temp_dir = tempfile.mkdtemp(prefix="nrf_dfu_")
        self.unpacked_zip_path = os.path.join(self.temp_dir, 'unpacked_zip')
        self.manifest = Package.unpack_package(zip_file_path,
                                               self.unpacked_zip_path)

        self.dfu_transport = dfu_transport

        if connect_delay is not None:
            self.connect_delay = connect_delay
        else:
            self.connect_delay = 3
Пример #20
0
    def __init__(self, zip_file_path, dfu_transport):
        """
        Initializes the dfu upgrade, unpacks zip and registers callbacks.

        @param zip_file_path: Path to the zip file with the firmware to upgrade
        @type zip_file_path: str
        @param dfu_transport: Transport backend to use to upgrade
        @type dfu_transport: nordicsemi.dfu.dfu_transport.DfuTransport
        @return
        """
        self.zip_file_path = zip_file_path
        self.ready_to_send = True
        self.response_opcode_received = None

        self.temp_dir = tempfile.mkdtemp(prefix="nrf_dfu_")
        self.unpacked_zip_path = os.path.join(self.temp_dir, 'unpacked_zip')
        self.manifest = Package.unpack_package(self.zip_file_path, self.unpacked_zip_path)

        if dfu_transport:
            self.dfu_transport = dfu_transport

        self.dfu_transport.register_events_callback(DfuEvent.TIMEOUT_EVENT, self.timeout_event_handler)
        self.dfu_transport.register_events_callback(DfuEvent.ERROR_EVENT, self.error_event_handler)
Пример #21
0
def display(zip_file):

    package = Package()
    package.parse_package(zip_file, preserve_work_dir=True)

    click.echo("{0}".format(str(package)))
Пример #22
0
def generate(zipfile, debug_mode, application, application_version,
             application_version_string, bootloader, bootloader_version,
             hw_version, sd_req, softdevice, key_file):
    """
    Generate a zip package for distribution to apps that support Nordic DFU OTA.
    The application, bootloader, and SoftDevice files are converted to .bin if supplied as .hex files.
    For more information on the generated package, see:
    http://developer.nordicsemi.com/nRF5_SDK/doc/

    The following combinations are supported by this command:

    * BL only: Supported.

    * SD only: Supported (SD of same Major Version).

    * APP only: Supported.
   
    * BL + SD: Supported.

    * BL + APP: Not supported (use two packages instead).

    * BL + SD + APP: Supported.

    * SD + APP: Supported (SD of same Major Version).
    """
    zipfile_path = zipfile

    # Check combinations
    if bootloader is not None and application is not None and softdevice is None:
        click.echo(
            "Error: Invalid combination: use two .zip packages instead.")
        return

    if debug_mode is None:
        debug_mode = False

    # The user can specify the application version with two different
    # formats. As an integer, e.g. 102130, or as a string
    # "10.21.30". Internally we convert to integer.
    if application_version_string:
        application_version_internal = convert_version_string_to_int(
            application_version_string)
    else:
        application_version_internal = application_version

    if application_version_internal == 'none':
        application_version_internal = None

    if bootloader_version == 'none':
        bootloader_version = None

    if hw_version == 'none':
        hw_version = None

    # Convert multiple value into a single instance
    if len(sd_req) > 1:
        click.echo(
            "Please specify SoftDevice requirements as a comma-separated list: --sd-req 0xXXXX,0xYYYY,..."
        )
        return
    elif len(sd_req) == 0:
        sd_req = None
    else:
        sd_req = sd_req[0]
        if sd_req == 'none':
            sd_req = None

    # Initial consistency checks
    if application_version_internal is not None and application is None:
        click.echo("Error: Application version with no image.")
        return

    if bootloader_version is not None and bootloader is None:
        click.echo("Error: Bootloader version with no image.")
        return

    if debug_mode:
        display_debug_warning()
        # Default to no version checking
        if application_version_internal is None:
            application_version_internal = Package.DEFAULT_APP_VERSION
        if bootloader_version is None:
            bootloader_version = Package.DEFAULT_BL_VERSION
        if hw_version is None:
            hw_version = Package.DEFAULT_HW_VERSION
        if sd_req is None:
            # Use string as this will be mapped into an int below
            sd_req = str(Package.DEFAULT_SD_REQ[0])

    # Version checks
    if hw_version is None:
        click.echo("Error: --hw-version required.")
        return

    if sd_req is None:
        click.echo("Error: --sd-req required.")
        return

    if application is not None and application_version_internal is None:
        click.echo(
            'Error: --application-version or --application-version-string'
            'required with application image.')
        return

    if bootloader is not None and bootloader_version is None:
        click.echo(
            "Error: --bootloader-version required with bootloader image.")
        return

    sd_req_list = []
    if sd_req is not None:
        try:
            # This will parse any string starting with 0x as base 16.
            sd_req_list = sd_req.split(',')
            sd_req_list = map(int_as_text_to_int, sd_req_list)
        except ValueError:
            raise NordicSemiException("Could not parse value for --sd-req. "
                                      "Hex values should be prefixed with 0x.")

    signer = Signing()
    default_key = signer.load_key(key_file)
    if default_key:
        display_sec_warning()

    package = Package(debug_mode, hw_version, application_version_internal,
                      bootloader_version, sd_req_list, application, bootloader,
                      softdevice, key_file)

    package.generate_package(zipfile_path)

    log_message = "Zip created at {0}".format(zipfile_path)
    click.echo(log_message)
Пример #23
0
    def generate(self, arch, app_file, app_ver, bl_ver, bl_sett_ver, custom_bl_sett_addr):
        """
        Populates the settings object based on the given parameters.

        :param arch: Architecture family string, e.g. NRF51
        :param app_file: Path to application file
        :param app_ver: Application version number
        :param bl_ver: Bootloader version number
        :param bl_sett_ver: Bootloader settings version number
        :param custom_bl_sett_addr: Custom start address for the settings page
        :return:
        """

        # Set the architecture
        self.set_arch(arch)

        if custom_bl_sett_addr is not None:
            self.bl_sett_addr = custom_bl_sett_addr

        if bl_sett_ver == 1:
            self.setts = BLDFUSettingsStructV1()
        else:
            raise NordicSemiException("Unknown bootloader settings version")

        self.bl_sett_ver = bl_sett_ver & 0xffffffff
        self.bl_ver = bl_ver & 0xffffffff

        if app_ver is not None:
            self.app_ver = app_ver & 0xffffffff
        else:
            self.app_ver = 0x0 & 0xffffffff

        if app_file is not None:
            # load application to find out size and CRC
            self.temp_dir = tempfile.mkdtemp(prefix="nrf_dfu_bl_sett_")
            self.app_bin = Package.normalize_firmware_to_bin(self.temp_dir, app_file)

            # calculate application size and CRC32
            self.app_sz = int(Package.calculate_file_size(self.app_bin)) & 0xffffffff
            self.app_crc = int(Package.calculate_crc(32, self.app_bin)) & 0xffffffff
            self.bank0_bank_code = 0x1 & 0xffffffff
        else:
            self.app_sz = 0x0 & 0xffffffff
            self.app_crc = 0x0 & 0xffffffff
            self.bank0_bank_code = 0x0 & 0xffffffff

        # build the uint32_t array
        arr = [0x0] * self.setts.uint32_count

        # additional harcoded values
        self.bank_layout = 0x0 & 0xffffffff
        self.bank_current = 0x0 & 0xffffffff

        # fill in the settings
        arr[self.setts.offs_sett_ver] = self.bl_sett_ver
        arr[self.setts.offs_app_ver] = self.app_ver
        arr[self.setts.offs_bl_ver] = self.bl_ver
        arr[self.setts.offs_bank_layout] = self.bank_layout
        arr[self.setts.offs_bank_current] = self.bank_current
        arr[self.setts.offs_bank0_img_sz] = self.app_sz
        arr[self.setts.offs_bank0_img_crc] = self.app_crc
        arr[self.setts.offs_bank0_bank_code] = self.bank0_bank_code

        # calculate the CRC32 from the filled-in settings
        crc_format_str = '<' + ('I' * (self.setts.uint32_count - 1))
        crc_arr = arr[1:]
        crc_data = struct.pack(crc_format_str, *crc_arr)
        self.crc = binascii.crc32(crc_data) & 0xffffffff

        # fill in the calculated CRC32
        arr[self.setts.offs_crc] = self.crc

        format_str = '<' + ('I' * self.setts.uint32_count)

        # Get the packed data to insert into the hex instance
        data = struct.pack(format_str, *arr)

        # insert the data at the correct address
        self.ihex.puts(self.bl_sett_addr, data)
Пример #24
0
    def generate(self, arch, app_file, app_ver, bl_ver, bl_sett_ver, custom_bl_sett_addr, no_backup,
                 backup_address, app_boot_validation_type, sd_boot_validation_type, sd_file, key_file):

        self.set_arch(arch)

        if custom_bl_sett_addr is not None:
            self.bl_sett_addr = custom_bl_sett_addr

        if bl_sett_ver == 1:
            self.setts = BLDFUSettingsStructV1(self.bl_sett_addr)
        elif bl_sett_ver == 2:
            self.setts = BLDFUSettingsStructV2(self.bl_sett_addr)
        else:
            raise NordicSemiException("Unknown bootloader settings version")

        self.bl_sett_ver = bl_sett_ver & 0xffffffff
        self.bl_ver = bl_ver & 0xffffffff

        if app_ver is not None:
            self.app_ver = app_ver & 0xffffffff
        else:
            self.app_ver = 0x0 & 0xffffffff

        if app_file is not None:
            # load application to find out size and CRC
            self.temp_dir = tempfile.mkdtemp(prefix="nrf_dfu_bl_sett_")
            self.app_bin = Package.normalize_firmware_to_bin(self.temp_dir, app_file)

            # calculate application size and CRC32
            self.app_sz = int(Package.calculate_file_size(self.app_bin)) & 0xffffffff
            self.app_crc = int(Package.calculate_crc(32, self.app_bin)) & 0xffffffff
            self.bank0_bank_code = 0x1 & 0xffffffff

            # Calculate Boot validation fields for app
            if app_boot_validation_type == 'VALIDATE_GENERATED_CRC':
                self.app_boot_validation_type = 1 & 0xffffffff
                self.app_boot_validation_bytes = struct.pack('<I', self.app_crc)
            elif app_boot_validation_type == 'VALIDATE_GENERATED_SHA256':
                self.app_boot_validation_type = 2 & 0xffffffff
                sha256 = Package.calculate_sha256_hash(self.app_bin)
                self.app_boot_validation_bytes = bytearray([int(binascii.hexlify(i), 16) for i in list(sha256)][31::-1])
            elif app_boot_validation_type == 'VALIDATE_ECDSA_P256_SHA256':
                self.app_boot_validation_type = 3 & 0xffffffff
                ecdsa = Package.sign_firmware(key_file, self.app_bin)
                self.app_boot_validation_bytes = bytearray([int(binascii.hexlify(i), 16) for i in list(ecdsa)])
            else:  # This also covers 'NO_VALIDATION' case
                self.app_boot_validation_type = 0 & 0xffffffff
                self.app_boot_validation_bytes = bytearray(0)
        else:
            self.app_sz = 0x0 & 0xffffffff
            self.app_crc = 0x0 & 0xffffffff
            self.bank0_bank_code = 0x0 & 0xffffffff
            self.app_boot_validation_type = 0x0 & 0xffffffff
            self.app_boot_validation_bytes = bytearray(0)

        if sd_file is not None:
            # Load SD to calculate CRC
            self.temp_dir = tempfile.mkdtemp(prefix="nrf_dfu_bl_sett")
            temp_sd_file = os.path.join(os.getcwd(), 'temp_sd_file.hex')

            # Load SD hex file and remove MBR before calculating keys
            ih_sd = intelhex.IntelHex(sd_file)
            ih_sd_no_mbr = intelhex.IntelHex()
            ih_sd_no_mbr.merge(ih_sd[0x1000:], overlap='error')
            ih_sd_no_mbr.write_hex_file(temp_sd_file)

            self.sd_bin = Package.normalize_firmware_to_bin(self.temp_dir, temp_sd_file)
            os.remove(temp_sd_file)

            self.sd_sz = int(Package.calculate_file_size(self.sd_bin)) & 0xffffffff

            # Calculate Boot validation fields for SD
            if sd_boot_validation_type == 'VALIDATE_GENERATED_CRC':
                self.sd_boot_validation_type = 1 & 0xffffffff
                sd_crc = int(Package.calculate_crc(32, self.sd_bin)) & 0xffffffff
                self.sd_boot_validation_bytes = struct.pack('<I', sd_crc)
            elif sd_boot_validation_type == 'VALIDATE_GENERATED_SHA256':
                self.sd_boot_validation_type = 2 & 0xffffffff
                sha256 = Package.calculate_sha256_hash(self.sd_bin)
                self.sd_boot_validation_bytes = bytearray([int(binascii.hexlify(i), 16) for i in list(sha256)][31::-1])
            elif sd_boot_validation_type == 'VALIDATE_ECDSA_P256_SHA256':
                self.sd_boot_validation_type = 3 & 0xffffffff
                ecdsa = Package.sign_firmware(key_file, self.sd_bin)
                self.sd_boot_validation_bytes = bytearray([int(binascii.hexlify(i), 16) for i in list(ecdsa)])
            else:  # This also covers 'NO_VALIDATION_CASE'
                self.sd_boot_validation_type = 0 & 0xffffffff
                self.sd_boot_validation_bytes = bytearray(0)
        else:
            self.sd_sz = 0x0 & 0xffffffff
            self.sd_boot_validation_type = 0 & 0xffffffff
            self.sd_boot_validation_bytes = bytearray(0)

        # additional harcoded values
        self.bank_layout = 0x0 & 0xffffffff
        self.bank_current = 0x0 & 0xffffffff

        # Fill the entire settings page with 0's
        for offset in range(0, self.setts.bytes_count):
            self.ihex[self.bl_sett_addr + offset] = 0x00

        self._add_value_tohex(self.setts.sett_ver, self.bl_sett_ver)
        self._add_value_tohex(self.setts.app_ver, self.app_ver)
        self._add_value_tohex(self.setts.bl_ver, self.bl_ver)
        self._add_value_tohex(self.setts.bank_layout, self.bank_layout)
        self._add_value_tohex(self.setts.bank_current, self.bank_current)
        self._add_value_tohex(self.setts.bank0_img_sz, self.app_sz)
        self._add_value_tohex(self.setts.bank0_img_crc, self.app_crc)
        self._add_value_tohex(self.setts.bank0_bank_code, self.bank0_bank_code)
        self._add_value_tohex(self.setts.sd_sz, self.sd_sz)

        self.boot_validation_crc = 0x0 & 0xffffffff
        if self.bl_sett_ver == 2:
            self._add_value_tohex(self.setts.sd_validation_type, self.sd_boot_validation_type, '<b')
            self.ihex.puts(self.setts.sd_validation_bytes, self.sd_boot_validation_bytes)

            self._add_value_tohex(self.setts.app_validation_type, self.app_boot_validation_type, '<b')
            self.ihex.puts(self.setts.app_validation_bytes, self.app_boot_validation_bytes)

            self.boot_validation_crc = self._calculate_crc32_from_hex(self.ihex,
                                                                      start_addr=self.setts.sd_validation_type,
                                                                      end_addr=self.setts.last_addr) & 0xffffffff
            self._add_value_tohex(self.setts.boot_validataion_crc, self.boot_validation_crc)

        self.crc = self._calculate_crc32_from_hex(self.ihex,
                                                  start_addr=self.bl_sett_addr+4,
                                                  end_addr=self.setts.init_cmd - 1) & 0xffffffff
        self._add_value_tohex(self.setts.crc, self.crc)

        if backup_address is None:
            self.backup_address = self.bl_sett_addr - self.bl_sett_backup_offset
        else:
            self.backup_address = backup_address

        if not no_backup:
            for offset in range(0, self.setts.bytes_count):
                self.ihex[self.backup_address + offset] = self.ihex[self.bl_sett_addr + offset]
Пример #25
0
class TestPackage(unittest.TestCase):
    def setUp(self):
        self.work_directory = tempfile.mkdtemp(prefix="nrf_dfu_tests_")

    def tearDown(self):
        shutil.rmtree(self.work_directory, ignore_errors=True)

    def test_generate_package_application(self):
        signer = Signing()
        signer.load_key('key.pem')

        self.p = Package(app_version=100,
                         sd_req=[0x1000, 0xfffe],
                         app_fw="firmwares/bar.hex",
                         signer=signer)

        pkg_name = "mypackage.zip"

        self.p.generate_package(pkg_name, preserve_work_dir=False)
        expected_zip_content = ["manifest.json", "bar.bin", "bar.dat"]

        with ZipFile(pkg_name, 'r') as pkg:
            infolist = pkg.infolist()

            for file_information in infolist:
                self.assertTrue(
                    file_information.filename in expected_zip_content)
                self.assertGreater(file_information.file_size, 0)

            # Extract all and load json document to see if it is correct regarding to paths
            pkg.extractall(self.work_directory)

            with open(os.path.join(self.work_directory, 'manifest.json'),
                      'r') as f:
                _json = json.load(f)
                self.assertEqual('bar.bin',
                                 _json['manifest']['application']['bin_file'])
                self.assertEqual('bar.dat',
                                 _json['manifest']['application']['dat_file'])
                self.assertTrue('softdevice' not in _json['manifest'])
                self.assertTrue(
                    'softdevice_bootloader' not in _json['manifest'])
                self.assertTrue('bootloader' not in _json['manifest'])

    def test_generate_package_sd_bl(self):
        signer = Signing()
        signer.load_key('key.pem')

        self.p = Package(app_version=100,
                         sd_req=[0x1000, 0xfffe],
                         softdevice_fw="firmwares/foo.hex",
                         bootloader_fw="firmwares/bar.hex",
                         signer=signer)

        pkg_name = "mypackage.zip"

        self.p.generate_package(pkg_name, preserve_work_dir=False)

        expected_zip_content = ["manifest.json", "sd_bl.bin", "sd_bl.dat"]

        with ZipFile(pkg_name, 'r') as pkg:
            infolist = pkg.infolist()

            for file_information in infolist:
                self.assertTrue(
                    file_information.filename in expected_zip_content)
                self.assertGreater(file_information.file_size, 0)

            # Extract all and load json document to see if it is correct regarding to paths
            pkg.extractall(self.work_directory)

            with open(os.path.join(self.work_directory, 'manifest.json'),
                      'r') as f:
                _json = json.load(f)
                self.assertEqual(
                    'sd_bl.bin',
                    _json['manifest']['softdevice_bootloader']['bin_file'])
                self.assertEqual(
                    'sd_bl.dat',
                    _json['manifest']['softdevice_bootloader']['dat_file'])

    def test_unpack_package_a(self):
        signer = Signing()
        signer.load_key('key.pem')

        self.p = Package(app_version=100,
                         sd_req=[0x1000, 0xffff],
                         softdevice_fw="firmwares/bar.hex",
                         signer=signer)
        pkg_name = os.path.join(self.work_directory, "mypackage.zip")
        self.p.generate_package(pkg_name, preserve_work_dir=False)

        unpacked_dir = os.path.join(self.work_directory, "unpacked")
        manifest = self.p.unpack_package(
            os.path.join(self.work_directory, pkg_name), unpacked_dir)
        self.assertIsNotNone(manifest)
        self.assertEqual('bar.bin', manifest.softdevice.bin_file)


#         self.assertEqual(0, manifest.softdevice.init_packet_data.ext_packet_id)
#         self.assertIsNotNone(manifest.softdevice.init_packet_data.firmware_crc16)

    def test_unpack_package_b(self):
        signer = Signing()
        signer.load_key('key.pem')

        self.p = Package(app_version=100,
                         sd_req=[0x1000, 0xffff],
                         softdevice_fw="firmwares/bar.hex",
                         signer=signer)
        pkg_name = os.path.join(self.work_directory, "mypackage.zip")
        self.p.generate_package(pkg_name, preserve_work_dir=False)

        unpacked_dir = os.path.join(self.work_directory, "unpacked")
        manifest = self.p.unpack_package(
            os.path.join(self.work_directory, pkg_name), unpacked_dir)
        self.assertIsNotNone(manifest)
        self.assertEqual('bar.bin', manifest.softdevice.bin_file)

    def test_unpack_package_c(self):
        signer = Signing()
        signer.load_key('key.pem')

        self.p = Package(app_version=100,
                         sd_req=[0x1000, 0xffff],
                         softdevice_fw="firmwares/bar.hex",
                         signer=signer)
        pkg_name = os.path.join(self.work_directory, "mypackage.zip")
        self.p.generate_package(pkg_name, preserve_work_dir=False)

        unpacked_dir = os.path.join(self.work_directory, "unpacked")
        manifest = self.p.unpack_package(
            os.path.join(self.work_directory, pkg_name), unpacked_dir)
        self.assertIsNotNone(manifest)
        self.assertEqual('bar.bin', manifest.softdevice.bin_file)
Пример #26
0
def generate(zipfile,
           debug_mode,
           application,
           application_version,
           application_version_string,
           bootloader,
           bootloader_version,
           hw_version,
           sd_req,
           sd_id,
           softdevice,
           key_file,
           zigbee,
           zigbee_manufacturer_id,
           zigbee_image_type,
           zigbee_comment):
    """
    Generate a zip package for distribution to apps that support Nordic DFU OTA.
    The application, bootloader, and SoftDevice files are converted to .bin if supplied as .hex files.
    For more information on the generated package, see:
    http://developer.nordicsemi.com/nRF5_SDK/doc/

    The following combinations are supported by this command:

    * BL only: Supported.

    * SD only: Supported (SD of same Major Version).

    * APP only: Supported.

    * BL + SD: Supported.

    * BL + APP: Not supported (use two packages instead).

    * BL + SD + APP: Supported.

    * SD + APP: Supported (SD of same Major Version).
    """
    zipfile_path = zipfile

    # Check combinations
    if bootloader is not None and application is not None and softdevice is None:
        click.echo("Error: Invalid combination: use two .zip packages instead.")
        return

    if debug_mode is None:
        debug_mode = False

    # The user can specify the application version with two different
    # formats. As an integer, e.g. 102130, or as a string
    # "10.21.30". Internally we convert to integer.
    if application_version_string:
        application_version_internal = convert_version_string_to_int(application_version_string)
    else:
        application_version_internal = application_version

    if application_version_internal == 'none':
        application_version_internal = None

    if bootloader_version == 'none':
        bootloader_version = None

    if hw_version == 'none':
        hw_version = None

    # Convert multiple value into a single instance
    if len(sd_req) > 1:
        click.echo("Please specify SoftDevice requirements as a comma-separated list: --sd-req 0xXXXX,0xYYYY,...")
        return
    elif len(sd_req) == 0:
        sd_req = None
    else:
        sd_req = sd_req[0]
        if sd_req == 'none':
            sd_req = None

    if len(sd_id) > 1:
        click.echo("Please specify SoftDevice requirements as a comma-separated list: --sd-id 0xXXXX,0xYYYY,...")
        return
    elif len(sd_id) == 0:
        sd_id = None
    else:
        sd_id = sd_id[0]
        if sd_id == 'none':
            sd_id = None

    # Initial consistency checks
    if application_version_internal is not None and application is None:
        click.echo("Error: Application version with no image.")
        return

    if bootloader_version is not None and bootloader is None:
        click.echo("Error: Bootloader version with no image.")
        return

    if debug_mode:
        display_debug_warning()
        # Default to no version checking
        if application_version_internal is None:
            application_version_internal=Package.DEFAULT_APP_VERSION
        if bootloader_version is None:
            bootloader_version=Package.DEFAULT_BL_VERSION
        if hw_version is None:
            hw_version=Package.DEFAULT_HW_VERSION
        if sd_req is None:
            # Use string as this will be mapped into an int below
            sd_req=str(Package.DEFAULT_SD_REQ[0])

    # Version checks
    if hw_version is None:
        click.echo("Error: --hw-version required.")
        return

    if sd_req is None:
        click.echo("Error: --sd-req required.")
        return

    if application is not None and application_version_internal is None:
        click.echo('Error: --application-version or --application-version-string'
                   ' required with application image.')
        return

    if bootloader is not None and bootloader_version is None:
        click.echo("Error: --bootloader-version required with bootloader image.")
        return

    if application is not None and softdevice is not None and sd_id is None:
        click.echo("Error: --sd-id required with softdevice and application images.")
        return

    sd_req_list = []
    if sd_req is not None:
        try:
            # This will parse any string starting with 0x as base 16.
            sd_req_list = sd_req.split(',')
            sd_req_list = map(int_as_text_to_int, sd_req_list)
        except ValueError:
            raise NordicSemiException("Could not parse value for --sd-req. "
                                      "Hex values should be prefixed with 0x.")

    sd_id_list = []
    if sd_id is not None:
        try:
            # This will parse any string starting with 0x as base 16.
            sd_id_list = sd_id.split(',')
            sd_id_list = map(int_as_text_to_int, sd_id_list)

            # Copy all IDs from sd_id_list to sd_req_list, without duplicates.
            # This ensures that the softdevice update can be repeated in case
            # SD+(BL)+App update terminates during application update after the
            # softdevice was already updated (with new ID). Such update would
            # have to be repeated and the softdevice would have to be sent again,
            # this time updating itself.
            sd_req_list += set(sd_id_list) - set(sd_req_list)
        except ValueError:
            raise NordicSemiException("Could not parse value for --sd-id. "
                                      "Hex values should be prefixed with 0x.")
    else:
        sd_id_list = sd_req_list

    if key_file is None:
        display_nokey_warning()
    else:
        signer = Signing()
        default_key = signer.load_key(key_file)
        if default_key:
            display_sec_warning()

    if zigbee_comment is None:
        zigbee_comment = ''
    elif any(ord(char) > 127 for char in zigbee_comment): # Check if all the characters belong to the ASCII range
        click.echo('Warning: Non-ASCII characters in the comment are not allowed. Discarding comment.')
        zigbee_comment = ''
    elif len(zigbee_comment) > 30:
        click.echo('Warning: truncating the comment to 30 bytes.')
        zigbee_comment = zigbee_comment[:30]

    if zigbee_manufacturer_id is None:
        zigbee_manufacturer_id = 0xFFFF

    if zigbee_image_type is None:
        zigbee_image_type = 0xFFFF

    package = Package(debug_mode,
                      hw_version,
                      application_version_internal,
                      bootloader_version,
                      sd_req_list,
                      sd_id_list,
                      application,
                      bootloader,
                      softdevice,
                      key_file,
                      zigbee,
                      zigbee_manufacturer_id,
                      zigbee_image_type,
                      zigbee_comment)

    package.generate_package(zipfile_path)

    # Regenerate BLE DFU package for Zigbee DFU purposes.
    if zigbee:
        from shutil import copyfile
        from os import remove

        log_message = "Zigbee update created at {0}".format(package.zigbee_ota_file.filename)
        click.echo(log_message)

        binfile = package.zigbee_ota_file.filename.replace(".zigbee", ".bin")
        copyfile(package.zigbee_ota_file.filename, binfile)
        package = Package(debug_mode,
                          hw_version,
                          application_version_internal,
                          bootloader_version,
                          sd_req_list,
                          sd_id_list,
                          binfile,
                          bootloader,
                          softdevice,
                          key_file)

        package.generate_package(zipfile_path)
        remove(binfile)

    log_message = "Zip created at {0}".format(zipfile_path)
    click.echo(log_message)
Пример #27
0
def generate(zipfile, debug_mode, application, application_version,
             application_version_string, bootloader, bootloader_version,
             hw_version, sd_req, sd_id, softdevice, sd_boot_validation,
             app_boot_validation, key_file, external_app, zigbee,
             zigbee_manufacturer_id, zigbee_image_type, zigbee_comment,
             zigbee_ota_hw_version, zigbee_ota_fw_version,
             zigbee_ota_min_hw_version, zigbee_ota_max_hw_version):
    """
    Generate a zip package for distribution to apps that support Nordic DFU OTA.
    The application, bootloader, and SoftDevice files are converted to .bin if supplied as .hex files.
    For more information on the generated package, see:
    http://developer.nordicsemi.com/nRF5_SDK/doc/

    The following combinations are supported by this command:

    * BL only: Supported.

    * SD only: Supported (SD of same Major Version).

    * APP only: Supported (external or internal).

    * BL + SD: Supported.

    * BL + APP: Not supported (use two packages instead).

    * BL + SD + APP: Supported.

    * SD + APP: Supported (SD of same Major Version).
    """
    zipfile_path = zipfile

    # Check combinations
    if bootloader is not None and application is not None and softdevice is None:
        raise click.UsageError(
            "Invalid combination: use two .zip packages instead.")

    if debug_mode is None:
        debug_mode = False

    # The user can specify the application version with two different
    # formats. As an integer, e.g. 102130, or as a string
    # "10.21.30". Internally we convert to integer.
    if application_version_string:
        application_version_internal = convert_version_string_to_int(
            application_version_string)
        if application_version:
            click.echo(
                'Warning: When both application-version-string and application-version are provided, only the string will be used.'
            )
    else:
        application_version_internal = application_version

    if application_version_internal == 'none':
        application_version_internal = None

    if bootloader_version == 'none':
        bootloader_version = None

    if hw_version == 'none':
        hw_version = None

    if external_app is None:
        external_app = False

    if zigbee_ota_hw_version == 'none':
        zigbee_ota_hw_version = None

    if zigbee_ota_fw_version == 'none':
        zigbee_ota_fw_version = None

    # Convert multiple value into a single instance
    if len(sd_req) > 1:
        raise click.BadParameter(
            "Please specify SoftDevice requirements as a comma-separated list: --sd-req 0xXXXX,0xYYYY,...",
            param_hint='sd_req')
    elif len(sd_req) == 0:
        sd_req = None
    else:
        sd_req = sd_req[0]
        if sd_req == 'none':
            sd_req = None

    if len(sd_id) > 1:
        raise click.BadParameter(
            "Please specify SoftDevice requirements as a comma-separated list: --sd-id 0xXXXX,0xYYYY,...",
            param_hint='sd_req')
    elif len(sd_id) == 0:
        sd_id = None
    else:
        sd_id = sd_id[0]
        if sd_id == 'none':
            sd_id = None

    # Initial consistency checks
    if application_version_internal is not None and application is None:
        raise click.UsageError("Application version with no image.")

    if bootloader_version is not None and bootloader is None:
        raise click.UsageError("Bootloader version with no image.")

    if debug_mode:
        display_debug_warning()
        # Default to no version checking
        if application_version_internal is None:
            application_version_internal = Package.DEFAULT_APP_VERSION
        if bootloader_version is None:
            bootloader_version = Package.DEFAULT_BL_VERSION
        if hw_version is None:
            hw_version = Package.DEFAULT_HW_VERSION
        if sd_req is None:
            # Use string as this will be mapped into an int below
            sd_req = str(Package.DEFAULT_SD_REQ[0])

    # Version checks
    if hw_version is None:
        raise click.UsageError("--hw-version required.")

    if sd_req is None and external_app is False:
        raise click.UsageError("--sd-req required.")

    if application is not None and application_version_internal is None:
        raise click.UsageError(
            '--application-version or --application-version-string'
            ' required with application image.')

    if bootloader is not None and bootloader_version is None:
        raise click.UsageError(
            "--bootloader-version required with bootloader image.")

    # Zigbee only allows App, SoftDevice (minor), bootloader or Softdevice+bootloader
    if zigbee:
        if sum(bool(x) for x in [application, softdevice, bootloader]) != 1:
            click.echo(
                'Error: Provide either --application, --softdevice, or --bootloader'
                ' for Zigbee package generation (not a combination).')

    if application is not None and softdevice is not None and sd_id is None:
        raise click.UsageError(
            "--sd-id required with softdevice and application images.")

    if application is None and external_app is True:
        raise click.UsageError("--external-app requires an application.")

    if application is not None and softdevice is not None and external_app is True:
        raise click.UsageError(
            "--external-app is only possible for application only DFU packages."
        )

    if application is not None and bootloader is not None and external_app is True:
        raise click.UsageError(
            "--external-app is only possible for application only DFU packages."
        )

    if zigbee and zigbee_ota_hw_version is None:
        raise click.UsageError("--zigbee-ota-hw-version is required.")

    if zigbee and zigbee_ota_fw_version is None:
        zigbee_ota_fw_version = 0

    sd_req_list = []
    if sd_req is not None:
        try:
            # This will parse any string starting with 0x as base 16.
            sd_req_list = sd_req.split(',')
            sd_req_list = list(map(int_as_text_to_int, sd_req_list))
        except ValueError:
            raise NordicSemiException("Could not parse value for --sd-req. "
                                      "Hex values should be prefixed with 0x.")

    sd_id_list = []
    if sd_id is not None:
        try:
            # This will parse any string starting with 0x as base 16.
            sd_id_list = sd_id.split(',')
            sd_id_list = list(map(int_as_text_to_int, sd_id_list))

            # Copy all IDs from sd_id_list to sd_req_list, without duplicates.
            # This ensures that the softdevice update can be repeated in case
            # SD+(BL)+App update terminates during application update after the
            # softdevice was already updated (with new ID). Such update would
            # have to be repeated and the softdevice would have to be sent again,
            # this time updating itself.
            sd_req_list += set(sd_id_list) - set(sd_req_list)
        except ValueError:
            raise NordicSemiException("Could not parse value for --sd-id. "
                                      "Hex values should be prefixed with 0x.")
    else:
        sd_id_list = sd_req_list

    if key_file is None:
        display_nokey_warning()
        signer = None
    else:
        signer = Signing()
        default_key = signer.load_key(key_file)
        if default_key:
            display_sec_warning()

    if zigbee_comment is None:
        zigbee_comment = ''
    elif any(ord(char) > 127 for char in zigbee_comment
             ):  # Check if all the characters belong to the ASCII range
        click.echo(
            'Warning: Non-ASCII characters in the comment are not allowed. Discarding comment.'
        )
        zigbee_comment = ''
    elif len(zigbee_comment) > 30:
        click.echo('Warning: truncating the comment to 30 bytes.')
        zigbee_comment = zigbee_comment[:30]

    if zigbee_manufacturer_id is None:
        zigbee_manufacturer_id = 0xFFFF

    if zigbee_image_type is None:
        zigbee_image_type = 0xFFFF

    # Set the external_app to false in --zigbee is set
    inner_external_app = external_app
    if zigbee:
        inner_external_app = False

    if zigbee_ota_min_hw_version is not None and zigbee_ota_min_hw_version > 0xFFFF:
        raise click.BadParameter('Exceeds 2-byte long integer.',
                                 param_hint='zigbee-ota-min-hw-version')

    if zigbee_ota_max_hw_version is not None and zigbee_ota_max_hw_version > 0xFFFF:
        raise click.BadParameter('Exceeds 2-byte long integer.',
                                 param_hint='zigbee-ota-max-hw-version')

    if zigbee and (hw_version > 0xFFFF):
        raise click.BadParameter('Exceeds 2-byte long integer.',
                                 param_hint='hw-version')

    # Warn user if minimal/maximum zigbee ota hardware version are not correct:
    #   * only one of them is given
    #   * minimum version is higher than maximum version
    #   * hw_version is inside the range specified by minimum and maximum hardware version
    if (type(zigbee_ota_min_hw_version) is
            int) != (type(zigbee_ota_max_hw_version) is int):
        click.echo(
            'Warning: min/max zigbee ota hardware version is missing. Discarding min/max hardware version.'
        )
    elif type(zigbee_ota_min_hw_version) is int:
        if zigbee_ota_min_hw_version > zigbee_ota_max_hw_version:
            click.echo(
                'Warning: zigbee-ota-min-hw-version is higher than zigbee-ota-max-hw-version.'
            )
        if (hw_version > zigbee_ota_max_hw_version) or (
                hw_version < zigbee_ota_min_hw_version):
            click.echo(
                'Warning: hw-version is outside the specified range specified by zigbee_ota_min_hw_version and zigbee_ota_max_hw_version.'
            )

    # Generate a DFU package. If --zigbee is set this is the inner DFU package
    # which will be used as a binary input to the outer DFU package
    package = Package(debug_mode, hw_version, application_version_internal,
                      bootloader_version, sd_req_list, sd_id_list, application,
                      bootloader, softdevice, sd_boot_validation,
                      app_boot_validation, signer, inner_external_app, zigbee,
                      zigbee_manufacturer_id, zigbee_image_type,
                      zigbee_comment, zigbee_ota_min_hw_version,
                      zigbee_ota_max_hw_version)

    package.generate_package(zipfile_path)

    if zigbee:
        from shutil import copyfile
        from os import remove

        log_message = "Zigbee update created at {0}".format(
            package.zigbee_ota_file.filename)
        click.echo(log_message)

        # Taking the inner Zigbee package as input for the outer DFU package
        binfile = package.zigbee_ota_file.filename.replace(".zigbee", ".bin")
        copyfile(package.zigbee_ota_file.filename, binfile)

        # Create the outer Zigbee DFU package.
        package = Package(debug_mode, zigbee_ota_hw_version,
                          zigbee_ota_fw_version, None, [0xFFFE], [0xFFFE],
                          binfile, None, None, None, None, signer, True)

        package.generate_package(zipfile_path)
        remove(binfile)

    log_message = "Zip created at {0}".format(zipfile_path)
    click.echo(log_message)
Пример #28
0
    def generate(self, arch, app_file, app_ver, bl_ver, bl_sett_ver):
    
        # Set the architecture
        self.set_arch(arch)

        if bl_sett_ver == 1:
            self.setts = BLDFUSettingsStructV1()
        else:
            raise NordicSemiException("Unknown bootloader settings version")
        
        self.bl_sett_ver = bl_sett_ver & 0xffffffff
        self.bl_ver = bl_ver & 0xffffffff

        if app_ver is not None:
            self.app_ver = app_ver & 0xffffffff
        else:
            self.app_ver = 0x0 & 0xffffffff

        if app_file is not None:
            # load application to find out size and CRC
            self.temp_dir = tempfile.mkdtemp(prefix="nrf_dfu_bl_sett_")
            self.app_bin = Package.normalize_firmware_to_bin(self.temp_dir, app_file)

            # calculate application size and CRC32
            self.app_sz = int(Package.calculate_file_size(self.app_bin)) & 0xffffffff
            self.app_crc = int(Package.calculate_crc(32, self.app_bin)) & 0xffffffff
        else:
            self.app_sz = 0x0 & 0xffffffff
            self.app_crc = 0x0 & 0xffffffff

        # build the uint32_t array
        arr = [0x0] * self.setts.uint32_count

        # additional harcoded values
        self.bank_layout = 0x0 & 0xffffffff
        self.bank_current = 0x0 & 0xffffffff
        self.bank0_bank_code = 0x1 & 0xffffffff

        # fill in the settings
        arr[self.setts.offs_sett_ver] = self.bl_sett_ver
        arr[self.setts.offs_app_ver] = self.app_ver
        arr[self.setts.offs_bl_ver] = self.bl_ver
        arr[self.setts.offs_bank_layout] = self.bank_layout
        arr[self.setts.offs_bank_current] = self.bank_current
        arr[self.setts.offs_bank0_img_sz] = self.app_sz
        arr[self.setts.offs_bank0_img_crc] = self.app_crc
        arr[self.setts.offs_bank0_bank_code] = self.bank0_bank_code 

        # calculate the CRC32 from the filled-in settings
        crc_format_str = '<' + ('I' * (self.setts.uint32_count - 1)) 
        crc_arr = arr[1:]
        crc_data = struct.pack(crc_format_str, *crc_arr)
        self.crc = binascii.crc32(crc_data) & 0xffffffff

        # fill in the calculated CRC32
        arr[self.setts.offs_crc] = self.crc

        format_str = '<' + ('I' * self.setts.uint32_count) 

        # Get the packed data to insert into the hex instance
        data = struct.pack(format_str, *arr)
        
        # insert the data at the correct address
        self.ihex.puts(self.bl_sett_addr, data)
Пример #29
0
def unpack_package(zip_file_path, unpack_path):
    manifest = Package.unpack_package(zip_file_path, unpack_path)
    return _get_file_names(manifest)
Пример #30
0
class TestPackage(unittest.TestCase):
    def setUp(self):
        self.work_directory = tempfile.mkdtemp(prefix="nrf_dfu_tests_")

    def tearDown(self):
        shutil.rmtree(self.work_directory, ignore_errors=True)

    def test_generate_package_application(self):
        self.p = Package(
            dev_type=1,
            dev_rev=2,
            app_version=100,
            sd_req=[0x1000, 0xfffe],
            app_fw="firmwares/bar.hex"
        )

        pkg_name = "mypackage.zip"

        self.p.generate_package(pkg_name, preserve_work_directory=False)
        expected_zip_content = ["manifest.json", "bar.bin", "bar.dat"]

        with ZipFile(pkg_name, 'r') as pkg:
            infolist = pkg.infolist()

            for file_information in infolist:
                self.assertTrue(file_information.filename in expected_zip_content)
                self.assertGreater(file_information.file_size, 0)

            # Extract all and load json document to see if it is correct regarding to paths
            pkg.extractall(self.work_directory)

            with open(os.path.join(self.work_directory, 'manifest.json'), 'r') as f:
                _json = json.load(f)
                self.assertEqual(u'bar.bin', _json['manifest']['application']['bin_file'])
                self.assertEqual(u'bar.dat', _json['manifest']['application']['dat_file'])
                self.assertTrue(u'softdevice' not in _json['manifest'])
                self.assertTrue(u'softdevice_bootloader' not in _json['manifest'])
                self.assertTrue(u'bootloader' not in _json['manifest'])

    def test_generate_package_sd_bl(self):
        self.p = Package(dev_type=1,
                         dev_rev=2,
                         app_version=100,
                         sd_req=[0x1000, 0xfffe],
                         softdevice_fw="firmwares/foo.hex",
                         bootloader_fw="firmwares/bar.hex")

        pkg_name = "mypackage.zip"

        self.p.generate_package(pkg_name, preserve_work_directory=False)

        expected_zip_content = ["manifest.json", "sd_bl.bin", "sd_bl.dat"]

        with ZipFile(pkg_name, 'r') as pkg:
            infolist = pkg.infolist()

            for file_information in infolist:
                self.assertTrue(file_information.filename in expected_zip_content)
                self.assertGreater(file_information.file_size, 0)

            # Extract all and load json document to see if it is correct regarding to paths
            pkg.extractall(self.work_directory)

            with open(os.path.join(self.work_directory, 'manifest.json'), 'r') as f:
                _json = json.load(f)
                self.assertEqual(u'sd_bl.bin', _json['manifest']['softdevice_bootloader']['bin_file'])
                self.assertEqual(u'sd_bl.dat', _json['manifest']['softdevice_bootloader']['dat_file'])

    def test_unpack_package_a(self):
        self.p = Package(dev_type=1,
                         dev_rev=2,
                         app_version=100,
                         sd_req=[0x1000, 0xffff],
                         softdevice_fw="firmwares/bar.hex",
                         dfu_ver=0.6)
        pkg_name = os.path.join(self.work_directory, "mypackage.zip")
        self.p.generate_package(pkg_name, preserve_work_directory=False)

        unpacked_dir = os.path.join(self.work_directory, "unpacked")
        manifest = self.p.unpack_package(os.path.join(self.work_directory, pkg_name), unpacked_dir)
        self.assertIsNotNone(manifest)
        self.assertEqual(u'bar.bin', manifest.softdevice.bin_file)
        self.assertEqual(0, manifest.softdevice.init_packet_data.ext_packet_id)
        self.assertIsNotNone(manifest.softdevice.init_packet_data.firmware_crc16)

    def test_unpack_package_b(self):
        self.p = Package(dev_type=1,
                         dev_rev=2,
                         app_version=100,
                         sd_req=[0x1000, 0xffff],
                         softdevice_fw="firmwares/bar.hex",
                         dfu_ver=0.7)
        pkg_name = os.path.join(self.work_directory, "mypackage.zip")
        self.p.generate_package(pkg_name, preserve_work_directory=False)

        unpacked_dir = os.path.join(self.work_directory, "unpacked")
        manifest = self.p.unpack_package(os.path.join(self.work_directory, pkg_name), unpacked_dir)
        self.assertIsNotNone(manifest)
        self.assertEqual(u'bar.bin', manifest.softdevice.bin_file)
        self.assertEqual(1, manifest.softdevice.init_packet_data.ext_packet_id)
        self.assertIsNone(manifest.softdevice.init_packet_data.firmware_crc16)
        self.assertIsNotNone(manifest.softdevice.init_packet_data.firmware_hash)

    def test_unpack_package_c(self):
        self.p = Package(dev_type=1,
                         dev_rev=2,
                         app_version=100,
                         sd_req=[0x1000, 0xffff],
                         softdevice_fw="firmwares/bar.hex",
                         key_file="key.pem")
        pkg_name = os.path.join(self.work_directory, "mypackage.zip")
        self.p.generate_package(pkg_name, preserve_work_directory=False)

        unpacked_dir = os.path.join(self.work_directory, "unpacked")
        manifest = self.p.unpack_package(os.path.join(self.work_directory, pkg_name), unpacked_dir)
        self.assertIsNotNone(manifest)
        self.assertEqual(u'bar.bin', manifest.softdevice.bin_file)
        self.assertEqual(2, manifest.softdevice.init_packet_data.ext_packet_id)
        self.assertIsNone(manifest.softdevice.init_packet_data.firmware_crc16)
        self.assertIsNotNone(manifest.softdevice.init_packet_data.firmware_hash)
        self.assertIsNotNone(manifest.softdevice.init_packet_data.init_packet_ecds)
        self.assertEqual(manifest.dfu_version, 0.8)
Пример #31
0
    def generate(self, arch, app_file, app_ver, bl_ver, bl_sett_ver,
                 custom_bl_sett_addr, no_backup, backup_address,
                 app_boot_validation_type, sd_boot_validation_type, sd_file,
                 key_file):

        self.set_arch(arch)

        if custom_bl_sett_addr is not None:
            self.bl_sett_addr = custom_bl_sett_addr

        if bl_sett_ver == 1:
            self.setts = BLDFUSettingsStructV1(self.bl_sett_addr)
        elif bl_sett_ver == 2:
            self.setts = BLDFUSettingsStructV2(self.bl_sett_addr)
        else:
            raise NordicSemiException("Unknown bootloader settings version")

        self.bl_sett_ver = bl_sett_ver & 0xffffffff
        self.bl_ver = bl_ver & 0xffffffff

        if app_ver is not None:
            self.app_ver = app_ver & 0xffffffff
        else:
            self.app_ver = 0x0 & 0xffffffff

        if app_file is not None:
            # load application to find out size and CRC
            self.temp_dir = tempfile.mkdtemp(prefix="nrf_dfu_bl_sett_")
            self.app_bin = Package.normalize_firmware_to_bin(
                self.temp_dir, app_file)

            # calculate application size and CRC32
            self.app_sz = int(Package.calculate_file_size(
                self.app_bin)) & 0xffffffff
            self.app_crc = int(Package.calculate_crc(
                32, self.app_bin)) & 0xffffffff
            self.bank0_bank_code = 0x1 & 0xffffffff

            # Calculate Boot validation fields for app
            if app_boot_validation_type == 'VALIDATE_GENERATED_CRC':
                self.app_boot_validation_type = 1 & 0xffffffff
                self.app_boot_validation_bytes = struct.pack(
                    '<I', self.app_crc)
            elif app_boot_validation_type == 'VALIDATE_GENERATED_SHA256':
                self.app_boot_validation_type = 2 & 0xffffffff
                sha256 = Package.calculate_sha256_hash(self.app_bin)
                self.app_boot_validation_bytes = bytearray([
                    int(binascii.hexlify(i), 16) for i in list(sha256)
                ][31::-1])
            elif app_boot_validation_type == 'VALIDATE_ECDSA_P256_SHA256':
                self.app_boot_validation_type = 3 & 0xffffffff
                ecdsa = Package.sign_firmware(key_file, self.app_bin)
                self.app_boot_validation_bytes = bytearray(
                    [int(binascii.hexlify(i), 16) for i in list(ecdsa)])
            else:  # This also covers 'NO_VALIDATION' case
                self.app_boot_validation_type = 0 & 0xffffffff
                self.app_boot_validation_bytes = bytearray(0)
        else:
            self.app_sz = 0x0 & 0xffffffff
            self.app_crc = 0x0 & 0xffffffff
            self.bank0_bank_code = 0x0 & 0xffffffff
            self.app_boot_validation_type = 0x0 & 0xffffffff
            self.app_boot_validation_bytes = bytearray(0)

        if sd_file is not None:
            # Load SD to calculate CRC
            self.temp_dir = tempfile.mkdtemp(prefix="nrf_dfu_bl_sett")
            temp_sd_file = os.path.join(os.getcwd(), 'temp_sd_file.hex')

            # Load SD hex file and remove MBR before calculating keys
            ih_sd = intelhex.IntelHex(sd_file)
            ih_sd_no_mbr = intelhex.IntelHex()
            ih_sd_no_mbr.merge(ih_sd[0x1000:], overlap='error')
            ih_sd_no_mbr.write_hex_file(temp_sd_file)

            self.sd_bin = Package.normalize_firmware_to_bin(
                self.temp_dir, temp_sd_file)
            os.remove(temp_sd_file)

            self.sd_sz = int(Package.calculate_file_size(
                self.sd_bin)) & 0xffffffff

            # Calculate Boot validation fields for SD
            if sd_boot_validation_type == 'VALIDATE_GENERATED_CRC':
                self.sd_boot_validation_type = 1 & 0xffffffff
                sd_crc = int(Package.calculate_crc(32,
                                                   self.sd_bin)) & 0xffffffff
                self.sd_boot_validation_bytes = struct.pack('<I', sd_crc)
            elif sd_boot_validation_type == 'VALIDATE_GENERATED_SHA256':
                self.sd_boot_validation_type = 2 & 0xffffffff
                sha256 = Package.calculate_sha256_hash(self.sd_bin)
                self.sd_boot_validation_bytes = bytearray([
                    int(binascii.hexlify(i), 16) for i in list(sha256)
                ][31::-1])
            elif sd_boot_validation_type == 'VALIDATE_ECDSA_P256_SHA256':
                self.sd_boot_validation_type = 3 & 0xffffffff
                ecdsa = Package.sign_firmware(key_file, self.sd_bin)
                self.sd_boot_validation_bytes = bytearray(
                    [int(binascii.hexlify(i), 16) for i in list(ecdsa)])
            else:  # This also covers 'NO_VALIDATION_CASE'
                self.sd_boot_validation_type = 0 & 0xffffffff
                self.sd_boot_validation_bytes = bytearray(0)
        else:
            self.sd_sz = 0x0 & 0xffffffff
            self.sd_boot_validation_type = 0 & 0xffffffff
            self.sd_boot_validation_bytes = bytearray(0)

        # additional harcoded values
        self.bank_layout = 0x0 & 0xffffffff
        self.bank_current = 0x0 & 0xffffffff

        # Fill the entire settings page with 0's
        for offset in range(0, self.setts.bytes_count):
            self.ihex[self.bl_sett_addr + offset] = 0x00

        self._add_value_tohex(self.setts.sett_ver, self.bl_sett_ver)
        self._add_value_tohex(self.setts.app_ver, self.app_ver)
        self._add_value_tohex(self.setts.bl_ver, self.bl_ver)
        self._add_value_tohex(self.setts.bank_layout, self.bank_layout)
        self._add_value_tohex(self.setts.bank_current, self.bank_current)
        self._add_value_tohex(self.setts.bank0_img_sz, self.app_sz)
        self._add_value_tohex(self.setts.bank0_img_crc, self.app_crc)
        self._add_value_tohex(self.setts.bank0_bank_code, self.bank0_bank_code)
        self._add_value_tohex(self.setts.sd_sz, self.sd_sz)

        self.boot_validation_crc = 0x0 & 0xffffffff
        if self.bl_sett_ver == 2:
            self._add_value_tohex(self.setts.sd_validation_type,
                                  self.sd_boot_validation_type, '<b')
            self.ihex.puts(self.setts.sd_validation_bytes,
                           self.sd_boot_validation_bytes)

            self._add_value_tohex(self.setts.app_validation_type,
                                  self.app_boot_validation_type, '<b')
            self.ihex.puts(self.setts.app_validation_bytes,
                           self.app_boot_validation_bytes)

            self.boot_validation_crc = self._calculate_crc32_from_hex(
                self.ihex,
                start_addr=self.setts.sd_validation_type,
                end_addr=self.setts.last_addr) & 0xffffffff
            self._add_value_tohex(self.setts.boot_validataion_crc,
                                  self.boot_validation_crc)

        self.crc = self._calculate_crc32_from_hex(
            self.ihex,
            start_addr=self.bl_sett_addr + 4,
            end_addr=self.setts.init_cmd - 1) & 0xffffffff
        self._add_value_tohex(self.setts.crc, self.crc)

        if backup_address is None:
            self.backup_address = self.bl_sett_addr - self.bl_sett_backup_offset
        else:
            self.backup_address = backup_address

        if not no_backup:
            for offset in range(0, self.setts.bytes_count):
                self.ihex[self.backup_address +
                          offset] = self.ihex[self.bl_sett_addr + offset]
Пример #32
0
def generate(zipfile,
           debug_mode,
           application,
           application_address,
           application_version,
           application_version_string,
           bootloader,
           bootloader_version,
           hw_version,
           sd_req,
           softdevice,
           key_file):
    """
    Generate a zip package for distribution to apps that support Nordic DFU OTA.
    The application, bootloader, and SoftDevice files are converted to .bin if supplied as .hex files.
    For more information on the generated package, see:
    http://developer.nordicsemi.com/nRF5_SDK/doc/

    The following combinations are supported by this command:

    * BL only: Supported.

    * SD only: Supported (SD of same Major Version).

    * APP only: Supported.
   
    * BL + SD: Supported.

    * BL + APP: Not supported (use two packages instead).

    * BL + SD + APP: Supported.

    * SD + APP: Supported (SD of same Major Version).
    """
    zipfile_path = zipfile

    # Check combinations
    if bootloader is not None and application is not None and softdevice is None:
        click.echo("Error: Invalid combination: use two .zip packages instead.")
        return

    if debug_mode is None:
        debug_mode = False

    # The user can specify the application version with two different
    # formats. As an integer, e.g. 102130, or as a string
    # "10.21.30". Internally we convert to integer.
    if application_version_string:
        application_version_internal = convert_version_string_to_int(application_version_string)
    else:
        application_version_internal = application_version

    if application_version_internal == 'none':
        application_version_internal = None

    if bootloader_version == 'none':
        bootloader_version = None

    if hw_version == 'none':
        hw_version = None

    # Convert multiple value into a single instance
    if len(sd_req) > 1:
        click.echo("Please specify SoftDevice requirements as a comma-separated list: --sd-req 0xXXXX,0xYYYY,...")
        return
    elif len(sd_req) == 0:
        sd_req = None
    else:
        sd_req = sd_req[0]
        if sd_req == 'none':
            sd_req = None

    # Initial consistency checks
    if application_version_internal is not None and application is None:
        click.echo("Error: Application version with no image.")
        return

    if bootloader_version is not None and bootloader is None:
        click.echo("Error: Bootloader version with no image.")
        return

    if debug_mode:
        display_debug_warning()
        # Default to no version checking
        if application_version_internal is None:
            application_version_internal=Package.DEFAULT_APP_VERSION
        if bootloader_version is None:
            bootloader_version=Package.DEFAULT_BL_VERSION
        if hw_version is None:
            hw_version=Package.DEFAULT_HW_VERSION
        if sd_req is None:
            # Use string as this will be mapped into an int below
            sd_req=str(Package.DEFAULT_SD_REQ[0])

    # Version checks
    if hw_version is None:
        click.echo("Error: --hw-version required.")
        return

    if sd_req is None: 
        click.echo("Error: --sd-req required.")
        return

    if application is not None and application_version_internal is None: 
        click.echo('Error: --application-version or --application-version-string'
                   'required with application image.')
        return

    if bootloader is not None and bootloader_version is None: 
        click.echo("Error: --bootloader-version required with bootloader image.")
        return

    sd_req_list = []
    if sd_req is not None:
        try:
            # This will parse any string starting with 0x as base 16.
            sd_req_list = sd_req.split(',')
            sd_req_list = map(int_as_text_to_int, sd_req_list)
        except ValueError:
            raise NordicSemiException("Could not parse value for --sd-req. "
                                      "Hex values should be prefixed with 0x.")

    signer = Signing()
    default_key = signer.load_key(key_file)
    if default_key:
        display_sec_warning()

    package = Package(debug_mode,
                      hw_version,
                      application_version_internal,
                      bootloader_version,
                      sd_req_list,
                      application,
                      application_address,
                      bootloader,
                      softdevice,
                      key_file)

    package.generate_package(zipfile_path)

    log_message = "Zip created at {0}".format(zipfile_path)
    click.echo(log_message)
Пример #33
0
def display(zip_file): 

    package = Package()
    package.parse_package(zip_file, preserve_work_dir=True)

    click.echo("{0}".format(str(package)))