def test_generate_without_application_or_sd_file(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=None, no_backup=False, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type=None, sd_file=None, key_file=None) self.assertEqual(nRFArch.NRF52, settings.arch) self.assertEqual('nRF52', settings.arch_str) self.assertEqual(0x0007F000, settings.bl_sett_addr) self.assertEqual(0x12ECF0A5, settings.crc) self.assertEqual(0x00000002, settings.bl_sett_ver) self.assertEqual(0x00000001, settings.app_ver) self.assertEqual(0x00000001, settings.bl_ver) self.assertEqual(0x00000000, settings.bank_layout) self.assertEqual(0x00000000, settings.bank_current) self.assertEqual(0x00000000, settings.app_sz) self.assertEqual(0x00000000, settings.app_crc) self.assertEqual(0x00000000, settings.bank0_bank_code) self.assertEqual(0x00000000, settings.sd_sz) self.assertEqual(0xACDA1BA2, settings.boot_validation_crc) self.assertEqual(0x00, settings.sd_boot_validation_type) # self.assertEqual(0x5B00BDCE, settings.sd_boot_validation_bytes) self.assertEqual(0x00, settings.app_boot_validation_type)
def generate(hex_file, family, application, application_version, application_version_string, bootloader_version, bl_settings_version, start_address, no_backup, backup_address): # Initial consistency checks if family is None: click.echo("Error: IC Family required.") return # 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 is not None: if not os.path.isfile(application): click.echo("Error: Application file not found.") return if application_version_internal is None: click.echo("Error: Application version required.") return if bootloader_version is None: click.echo("Error: Bootloader version required.") return if bl_settings_version is None: click.echo("Error: Bootloader DFU settings version required.") return if (no_backup is not None) and (backup_address is not None): click.echo("Error: Bootloader DFU settings backup page cannot be specified if backup is disabled.") return if no_backup is None: no_backup = False if no_backup == False: display_settings_backup_warning() if (start_address is not None) and (backup_address is None): click.echo("WARNING: Using default offset in order to calculate bootloader settings backup page") sett = BLDFUSettings() sett.generate(arch=family, app_file=application, app_ver=application_version_internal, bl_ver=bootloader_version, bl_sett_ver=bl_settings_version, custom_bl_sett_addr=start_address, no_backup=no_backup, backup_address=backup_address) sett.tohexfile(hex_file) click.echo("\nGenerated Bootloader DFU settings .hex file and stored it in: {}".format(hex_file)) click.echo("{0}".format(str(sett)))
def test_generate_with_application_file(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file='firmwares/s132_nrf52_mini.hex', app_ver=1, bl_ver=1, bl_sett_ver=1, custom_bl_sett_addr=None, no_backup=False, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type=None, sd_file=None, key_file=None) self.assertEqual(nRFArch.NRF52, settings.arch) self.assertEqual('nRF52', settings.arch_str) self.assertEqual(0x0007F000, settings.bl_sett_addr) self.assertEqual(0xEB5917FB, settings.crc) self.assertEqual(0x00000001, settings.bl_sett_ver) self.assertEqual(0x00000001, settings.app_ver) self.assertEqual(0x00000001, settings.bl_ver) self.assertEqual(0x00000000, settings.bank_layout) self.assertEqual(0x00000000, settings.bank_current) self.assertEqual(0x00003000, settings.app_sz) self.assertEqual(0x5F045729, settings.app_crc) self.assertEqual(0x00000001, settings.bank0_bank_code)
def test_generate_with_backup_page_check_size(self): settings_raw = BLDFUSettings() settings_raw.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=None, no_backup=True, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type=None, sd_file=None, key_file=None) settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=None, no_backup=False, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type=None, sd_file=None, key_file=None) self.assertEqual(len(list(settings.ihex.todict().keys())), len(list(settings_raw.ihex.todict().keys())) * 2)
def test_generate_without_application_file(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=1, custom_bl_sett_addr=None, no_backup=False, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type=None, sd_file=None, signer=None) self.assertEqual(nRFArch.NRF52, settings.arch) self.assertEqual('nRF52', settings.arch_str) self.assertEqual(0x0007F000, settings.bl_sett_addr) self.assertEqual(0xEAA3288E, settings.crc) self.assertEqual(0x00000001, settings.bl_sett_ver) self.assertEqual(0x00000001, settings.app_ver) self.assertEqual(0x00000001, settings.bl_ver) self.assertEqual(0x00000000, settings.bank_layout) self.assertEqual(0x00000000, settings.bank_current) self.assertEqual(0x00000000, settings.app_sz) self.assertEqual(0x00000000, settings.app_crc) self.assertEqual(0x00000000, settings.bank0_bank_code)
def test_generate_without_application_or_sd_file(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=None, no_backup=False, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type=None, sd_file=None, key_file=None) self.assertEqual(nRFArch.NRF52, settings.arch) self.assertEqual('nRF52', settings.arch_str) self.assertEqual(0x0007F000, settings.bl_sett_addr) self.assertEqual(0x12ECF0A5, settings.crc) self.assertEqual(0x00000002, settings.bl_sett_ver) self.assertEqual(0x00000001, settings.app_ver) self.assertEqual(0x00000001, settings.bl_ver) self.assertEqual(0x00000000, settings.bank_layout) self.assertEqual(0x00000000, settings.bank_current) self.assertEqual(0x00000000, settings.app_sz) self.assertEqual(0x00000000, settings.app_crc) self.assertEqual(0x00000000, settings.bank0_bank_code) self.assertEqual(0x00000000, settings.sd_sz) self.assertEqual(0xACDA1BA2, settings.boot_validation_crc) self.assertEqual(0x00, settings.sd_boot_validation_type) # self.assertEqual(0x5B00BDCE, settings.sd_boot_validation_bytes) self.assertEqual(0x00, settings.app_boot_validation_type)
def test_generate_with_backup_page_check_size(self): settings_raw = BLDFUSettings() settings_raw.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=None, no_backup=True, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type=None, sd_file=None, key_file=None) settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=None, no_backup=False, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type=None, sd_file=None, key_file=None) self.assertEqual(len(settings.ihex.todict().keys()), len(settings_raw.ihex.todict().keys()) * 2)
def test_generate_with_application_file(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file='firmwares/s132_nrf52_mini.hex', app_ver=1, bl_ver=1, bl_sett_ver=1, custom_bl_sett_addr=None, no_backup=False, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type=None, sd_file=None, key_file=None) self.assertEqual(nRFArch.NRF52, settings.arch) self.assertEqual('nRF52', settings.arch_str) self.assertEqual(0x0007F000, settings.bl_sett_addr) self.assertEqual(0xEB5917FB, settings.crc) self.assertEqual(0x00000001, settings.bl_sett_ver) self.assertEqual(0x00000001, settings.app_ver) self.assertEqual(0x00000001, settings.bl_ver) self.assertEqual(0x00000000, settings.bank_layout) self.assertEqual(0x00000000, settings.bank_current) self.assertEqual(0x00003000, settings.app_sz) self.assertEqual(0x5F045729, settings.app_crc) self.assertEqual(0x00000001, settings.bank0_bank_code)
def test_generate_with_custom_start_address(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=1, custom_bl_sett_addr=0x0006F000) self.assertEqual(settings.bl_sett_addr, 0x0006F000)
def test_generate_with_backup_page_custom_address(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=1, custom_bl_sett_addr=None, no_backup=False, backup_address=0x0006F000) self.assertEqual(settings.backup_address, 0x0006F000) self.assertTrue(0x0006F000 in settings.ihex.todict().keys())
def generate(hex_file, family, application, application_version, application_version_string, bootloader_version, bl_settings_version, start_address): # Initial consistency checks if family is None: click.echo("Error: IC Family required.") return # 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 is not None: if not os.path.isfile(application): click.echo("Error: Application file not found.") return if application_version_internal is None: click.echo("Error: Application version required.") return if bootloader_version is None: click.echo("Error: Bootloader version required.") return if bl_settings_version is None: click.echo("Error: Bootloader DFU settings version required.") return sett = BLDFUSettings() sett.generate(arch=family, app_file=application, app_ver=application_version_internal, bl_ver=bootloader_version, bl_sett_ver=bl_settings_version, custom_bl_sett_addr=start_address) sett.tohexfile(hex_file) click.echo( "\nGenerated Bootloader DFU settings .hex file and stored it in: {}". format(hex_file)) click.echo("{0}".format(str(sett)))
def test_generate_with_custom_start_address(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=0x0006F000, no_backup=False, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type=None, sd_file=None, key_file=None) self.assertEqual(settings.bl_sett_addr, 0x0006F000)
def test_generate_with_custom_start_address(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=0x0006F000, no_backup=False, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type=None, sd_file=None, key_file=None) self.assertEqual(settings.bl_sett_addr, 0x0006F000)
def test_generate_with_backup_page_default_address(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=0x0006F000, no_backup=False, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type=None, sd_file=None, key_file=None) self.assertEqual(settings.backup_address, (0x0006F000 - settings.bl_sett_backup_offset)) self.assertTrue((0x0006F000 - settings.bl_sett_backup_offset) in settings.ihex.todict().keys())
def test_generate_with_backup_page_check_values(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=None, no_backup=False, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type=None, sd_file=None, key_file=None) backup_dict = {(k + settings.bl_sett_backup_offset): v for k, v in settings.ihex.todict().items() if k < settings.bl_sett_addr} settings_dict = {k: v for k, v in settings.ihex.todict().items() if k >= settings.bl_sett_addr} self.assertEqual(backup_dict, settings_dict)
def generate(hex_file, family, application, application_version, application_version_string, bootloader_version, bl_settings_version): # Initial consistency checks if family is None: click.echo("Error: IC Family required.") return # 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 is not None: if not os.path.isfile(application): click.echo("Error: Application file not found.") return if application_version_internal is None: click.echo("Error: Application version required.") return if bootloader_version is None: click.echo("Error: Bootloader version required.") return if bl_settings_version is None: click.echo("Error: Bootloader DFU settings version required.") return sett = BLDFUSettings() sett.generate(arch=family, app_file=application, app_ver=application_version_internal, bl_ver=bootloader_version, bl_sett_ver=bl_settings_version) sett.tohexfile(hex_file) click.echo("\nGenerated Bootloader DFU settings .hex file and stored it in: {}".format(hex_file)) click.echo("{0}".format(str(sett)))
def test_generate_with_sd_boot_validation_ecdsa(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=None, no_backup=True, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type='VALIDATE_ECDSA_P256_SHA256', sd_file='firmwares/s132_nrf52_mini.hex', key_file='key.pem') # Since ECDSA contains a random component the signature will be different every time # it is generated. Therefore only overall structure of the boot validation will be checked. self.assertEqual(0x03, settings.sd_boot_validation_type) self.assertEqual(64, len(settings.sd_boot_validation_bytes))
def test_generate_with_backup_page_default_address(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=0x0006F000, no_backup=False, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type=None, sd_file=None, key_file=None) self.assertEqual(settings.backup_address, (0x0006F000 - settings.bl_sett_backup_offset)) self.assertTrue((0x0006F000 - settings.bl_sett_backup_offset ) in list(settings.ihex.todict().keys()))
def test_generate_with_sd_boot_validation_crc(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=None, no_backup=True, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type='VALIDATE_GENERATED_CRC', sd_file='firmwares/s132_nrf52_mini.hex', key_file=None) self.assertEqual(0x4637019F, settings.crc) self.assertEqual(0xCB5F90FB, settings.boot_validation_crc) self.assertEqual(0x01, settings.sd_boot_validation_type) self.assertEqual(0x5F045729, struct.unpack('<I', settings.sd_boot_validation_bytes)[0])
def test_generate_with_sd_boot_validation_ecdsa(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=None, no_backup=True, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type='VALIDATE_ECDSA_P256_SHA256', sd_file='firmwares/s132_nrf52_mini.hex', key_file='key.pem') # Since ECDSA contains a random component the signature will be different everytime # it is generated. Therefore only overall structure of the boot validation will be checked. self.assertEqual(0x03, settings.sd_boot_validation_type) self.assertEqual(64, len(settings.sd_boot_validation_bytes))
def test_generate_with_sd_boot_validation_sha256(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=None, no_backup=True, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type='VALIDATE_GENERATED_SHA256', sd_file='firmwares/s132_nrf52_mini.hex', key_file=None) self.assertEqual(0x4637019F, settings.crc) self.assertEqual(0x9C761426, settings.boot_validation_crc) self.assertEqual(0x02, settings.sd_boot_validation_type) self.assertEqual('036F52C9EBB53819D6E2B6FB57803823E864783B04D7331B46C0B5897CA9F1C7', binascii.hexlify(settings.sd_boot_validation_bytes).upper())
def test_generate_with_backup_page_check_values(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=1, custom_bl_sett_addr=None, no_backup=False, backup_address=None) backup_dict = {(k + settings.bl_sett_backup_offset): v for k, v in settings.ihex.todict().items() if k < settings.bl_sett_addr} settings_dict = { k: v for k, v in settings.ihex.todict().items() if k >= settings.bl_sett_addr } self.assertEqual(backup_dict, settings_dict)
def test_generate_with_sd_boot_validation_sha256(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=None, no_backup=True, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type='VALIDATE_GENERATED_SHA256', sd_file='firmwares/s132_nrf52_mini.hex', key_file=None) self.assertEqual(0x4637019F, settings.crc) self.assertEqual(0x9C761426, settings.boot_validation_crc) self.assertEqual(0x02, settings.sd_boot_validation_type) self.assertEqual( '036F52C9EBB53819D6E2B6FB57803823E864783B04D7331B46C0B5897CA9F1C7', binascii.hexlify(settings.sd_boot_validation_bytes).upper())
def test_generate_without_application_file(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=1, custom_bl_sett_addr=None) self.assertEqual(nRFArch.NRF52, settings.arch) self.assertEqual('nRF52', settings.arch_str) self.assertEqual(0x0007F000, settings.bl_sett_addr) self.assertEqual(0x2070A601, settings.crc) self.assertEqual(0x00000001, settings.bl_sett_ver) self.assertEqual(0x00000001, settings.app_ver) self.assertEqual(0x00000001, settings.bl_ver) self.assertEqual(0x00000000, settings.bank_layout) self.assertEqual(0x00000000, settings.bank_current) self.assertEqual(0x00000000, settings.app_sz) self.assertEqual(0x00000000, settings.app_crc) self.assertEqual(0x00000001, settings.bank0_bank_code)
def test_generate_with_sd_boot_validation_crc(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file=None, app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=None, no_backup=True, backup_address=None, app_boot_validation_type=None, sd_boot_validation_type='VALIDATE_GENERATED_CRC', sd_file='firmwares/s132_nrf52_mini.hex', key_file=None) self.assertEqual(0x4637019F, settings.crc) self.assertEqual(0xCB5F90FB, settings.boot_validation_crc) self.assertEqual(0x01, settings.sd_boot_validation_type) self.assertEqual( 0x5F045729, struct.unpack('<I', settings.sd_boot_validation_bytes)[0])
def test_generate_with_app_boot_validation_sha256(self): settings = BLDFUSettings() settings.generate(arch='NRF52', app_file='firmwares/s132_nrf52_mini.hex', app_ver=1, bl_ver=1, bl_sett_ver=2, custom_bl_sett_addr=None, no_backup=True, backup_address=None, app_boot_validation_type='VALIDATE_GENERATED_SHA256', sd_boot_validation_type=None, sd_file=None, key_file=None) self.assertEqual(0x1316CFD0, settings.crc) self.assertEqual(0xF78E451E, settings.boot_validation_crc) self.assertEqual(0x02, settings.app_boot_validation_type) self.assertEqual( bytes.fromhex( '036F52C9EBB53819D6E2B6FB57803823E864783B04D7331B46C0B5897CA9F1C7' ), settings.app_boot_validation_bytes)
def generate(hex_file, family, application, application_version, bootloader_version, bl_settings_version): # Initial consistency checks if family is None: click.echo("Error: IC Family required.") return if application is not None: if not os.path.isfile(application): click.echo("Error: Application file not found.") return if application_version is None: click.echo("Error: Application version required.") return if bootloader_version is None: click.echo("Error: Bootloader version required.") return if bl_settings_version is None: click.echo("Error: Bootloader DFU settings version required.") return sett = BLDFUSettings() sett.generate(arch=family, app_file=application, app_ver=application_version, bl_ver=bootloader_version, bl_sett_ver=bl_settings_version) sett.tohexfile(hex_file) click.echo( "\nGenerated Bootloader DFU settings .hex file and stored it in: {}". format(hex_file)) click.echo("{0}".format(str(sett)))
def generate(hex_file, family, application, application_version, application_version_string, bootloader_version, bl_settings_version, start_address, no_backup, backup_address, app_boot_validation, sd_boot_validation, softdevice, key_file): # 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 is not None: if not os.path.isfile(application): raise click.FileError(application, hint="Application file not found") if application_version_internal is None: raise click.UsageError( '--application-version or --application-version-string' ' required with application image.') if (no_backup is not None) and (backup_address is not None): raise click.BadParameter( "Bootloader DFU settings backup page cannot be specified if backup is disabled.", param_hint='backup_address') if no_backup is None: no_backup = False if no_backup is False: display_settings_backup_warning() if (start_address is not None) and (backup_address is None): click.echo( "WARNING: Using default offset in order to calculate bootloader settings backup page" ) if bl_settings_version == 1 and (app_boot_validation or sd_boot_validation): raise click.BadParameter( "Bootloader settings version 1 does not support boot validation.", param_hint='bl_settings_version') # load signing key (if needed) only once if 'VALIDATE_ECDSA_P256_SHA256' in (app_boot_validation, sd_boot_validation): if not os.path.isfile(key_file): raise click.UsageError( "Key file must be given when 'VALIDATE_ECDSA_P256_SHA256' boot validation is used" ) signer = Signing() default_key = signer.load_key(key_file) if default_key: display_sec_warning() else: signer = None if app_boot_validation and not application: raise click.UsageError( "--application hex file must be set when using --app_boot_validation" ) if sd_boot_validation and not softdevice: raise click.UsageError( "--softdevice hex file must be set when using --sd_boot_validation" ) # Default boot validation cases if app_boot_validation is None and application is not None and bl_settings_version == 2: app_boot_validation = DEFAULT_BOOT_VALIDATION if sd_boot_validation is None and softdevice is not None and bl_settings_version == 2: sd_boot_validation = DEFAULT_BOOT_VALIDATION sett = BLDFUSettings() sett.generate(arch=family, app_file=application, app_ver=application_version_internal, bl_ver=bootloader_version, bl_sett_ver=bl_settings_version, custom_bl_sett_addr=start_address, no_backup=no_backup, backup_address=backup_address, app_boot_validation_type=app_boot_validation, sd_boot_validation_type=sd_boot_validation, sd_file=softdevice, signer=signer) sett.tohexfile(hex_file) click.echo( "\nGenerated Bootloader DFU settings .hex file and stored it in: {}". format(hex_file)) click.echo("{0}".format(str(sett)))