Example #1
0
        public nRFHex(String source, String bootloader = null)
        {
            //"""
            //Constructor that requires a firmware file path.
            //Softdevices can take an optional bootloader file path as parameter.
            //
            //:param str source: The file path for the firmware
            //:param str bootloader: Optional file path to bootloader firmware
            //:return: None
            //"""

            //super(nRFHex, self).__init__()

            this.file_format = "hex";

            if (source.EndsWith(".bin"))
                this.file_format = "bin";

              //TODO this.loadfile(source, this.file_format); loadfile is parent function
            this._removeuicr();
            this.bootloaderhex = null;
            if (bootloader != null)
                this.bootloaderhex = new nRFHex(bootloader);
        }
Example #2
0
        public void generate_package(String filename, bool preserve_work_directory = false)
        {
            /*
            """
            Generates a Nordic DFU package. The package is a zip file containing firmware(s) and metadata required
            for Nordic DFU applications to perform DFU onn nRF5X devices.

            :param str filename: Filename for generated package.
            :param bool preserve_work_directory: True to preserve the temporary working directory.
            Useful for debugging of a package, and if the user wants to look at the generated package without having to
            unzip it.
            :return: None
            """
            */

            String work_directory = /*this.*/__create_temp_workspace();

            if (myPackage._is_bootloader_softdevice_combination(this.firmwares_data))
            {
                //# Removing softdevice and bootloader data from dictionary and adding the combined later
                //softdevice_fw_data = this.firmwares_data.pop(HexType.SOFTDEVICE);
                //bootloader_fw_data = this.firmwares_data.pop(HexType.BOOTLOADER);
                Dictionary<FirmwareKeys,object> softdevice_fw_data = this.firmwares_data[HexType.SOFTDEVICE];
                this.firmwares_data.Remove(HexType.SOFTDEVICE);
                Dictionary<FirmwareKeys, object> bootloader_fw_data = this.firmwares_data[HexType.BOOTLOADER];
                this.firmwares_data.Remove(HexType.BOOTLOADER);

                String softdevice_fw_name = (String)softdevice_fw_data[FirmwareKeys.FIRMWARE_FILENAME];
                String bootloader_fw_name = (String)bootloader_fw_data[FirmwareKeys.FIRMWARE_FILENAME];

                String new_filename = "sd_bl.bin";
                String sd_bl_file_path = os.path.join(work_directory, new_filename);

                nRFHex nrf_hex;
                nrf_hex = new nRFHex(softdevice_fw_name, bootloader_fw_name);
                nrf_hex.tobinfile(sd_bl_file_path);

                int softdevice_size = nrf_hex.size();
                int bootloader_size = nrf_hex.bootloadersize();

                this.__add_firmware_info(HexType.SD_BL,
                                         sd_bl_file_path,
                                         (InitPacketData)softdevice_fw_data[FirmwareKeys.INIT_PACKET_DATA],
                                         softdevice_size,
                                         bootloader_size);
            }

            /*TODOTODO
            foreach(var key in this.firmwares_data)
            {
                Dictionary<FirmwareKeys, object> firmware = this.firmwares_data[(int)key];

                //# Normalize the firmware file and store it in the work directory
                firmware[FirmwareKeys.BIN_FILENAME] = //\
                    Package.normalize_firmware_to_bin(work_directory, firmware[FirmwareKeys.FIRMWARE_FILENAME]);

                //# Calculate the hash for the .bin file located in the work directory
                String bin_file_path = os.path.join(work_directory, (String)firmware[FirmwareKeys.BIN_FILENAME]);

                InitPacketData init_packet_data = (InitPacketData)firmware[FirmwareKeys.INIT_PACKET_DATA];

                if (this.dfu_ver <= 0.5)
                {
                    firmware_hash = Package.calculate_crc16(bin_file_path);
                    init_packet_data[PacketField.NORDIC_PROPRIETARY_OPT_DATA_FIRMWARE_CRC16] = firmware_hash;
                } else if (this.dfu_ver == 0.6)
                {
                    init_packet_data[PacketField.NORDIC_PROPRIETARY_OPT_DATA_EXT_PACKET_ID] = INIT_PACKET_USES_CRC16;
                    firmware_hash = Package.calculate_crc16(bin_file_path);
                    init_packet_data[PacketField.NORDIC_PROPRIETARY_OPT_DATA_FIRMWARE_CRC16] = firmware_hash;
                } else if (this.dfu_ver == 0.7)
                {
                    init_packet_data[PacketField.NORDIC_PROPRIETARY_OPT_DATA_EXT_PACKET_ID] = INIT_PACKET_USES_HASH;
                    init_packet_data[PacketField.NORDIC_PROPRIETARY_OPT_DATA_FIRMWARE_LENGTH] = (int)(Package.calculate_file_size(bin_file_path));
                    firmware_hash = Package.calculate_sha256_hash(bin_file_path);
                    init_packet_data[PacketField.NORDIC_PROPRIETARY_OPT_DATA_FIRMWARE_HASH] = firmware_hash;
                } else if (this.dfu_ver == 0.8)
                {
                    init_packet_data[PacketField.NORDIC_PROPRIETARY_OPT_DATA_EXT_PACKET_ID] = INIT_PACKET_EXT_USES_ECDS;
                    firmware_hash = Package.calculate_sha256_hash(bin_file_path);
                    init_packet_data[PacketField.NORDIC_PROPRIETARY_OPT_DATA_FIRMWARE_LENGTH] = (int)(Package.calculate_file_size(bin_file_path));
                    init_packet_data[PacketField.NORDIC_PROPRIETARY_OPT_DATA_FIRMWARE_HASH] = firmware_hash;
                    temp_packet = this._create_init_packet(firmware);
                    signer = Signing();
                    signer.load_key(this.key_file);
                    signature = signer.sign(temp_packet);
                    init_packet_data[PacketField.NORDIC_PROPRIETARY_OPT_DATA_INIT_PACKET_ECDS] = signature;
                }
                //# Store the .dat file in the work directory
                init_packet = this._create_init_packet(firmware);
                String init_packet_filename = ((String)firmware[FirmwareKeys.BIN_FILENAME]).replace(".bin", ".dat");

                with (open(os.path.join(work_directory, init_packet_filename), "wb") as init_packet_file)
                    init_packet_file.write(init_packet);

                firmware[FirmwareKeys.DAT_FILENAME] = //\
                    init_packet_filename;
            }

            //# Store the manifest to manifest.json
            manifest = this.create_manifest();

            with open(os.path.join(work_directory, Package.MANIFEST_FILENAME), "w") as manifest_file:
                manifest_file.write(manifest);

            //# Package the work_directory to a zip file
            Package.create_zip_package(work_directory, filename);

            //# Delete the temporary directory
            if (!preserve_work_directory)
                shutil.rmtree(work_directory);
            TODOTODO*/
        }