def resource(ctx, address, length, option, compress, file): device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: data = mb.flash_read_resource(address, length, option) if ctx.obj['DEBUG']: click.echo() if file is None: click.echo(hexdump(data, address, compress)) else: try: if file.lower().endswith(('.srec', '.s19')): srec = bincopy.BinFile() srec.add_binary(data, address) srec.header = 'mboot' with open(file, "w") as f: f.write(srec.as_srec()) elif file.lower().endswith(('.hex', '.ihex')): ihex = bincopy.BinFile() ihex.add_binary(data, address) with open(file, "w") as f: f.write(ihex.as_ihex()) else: with open(file, "wb") as f: f.write(data) except Exception as e: print_error(f'Could not write to file: {file} \n [{str(e)}]') click.echo(f" Successfully saved into: {file}")
def read(ctx, address, length, mtype, compress, file): mem_id = 0 if mtype == 'INTERNAL' else ExtMemId[mtype] device = scan_interface(ctx.obj['TARGET']) click.echo(" Reading from MCU memory, please wait ! \n") with McuBoot(device, True) as mb: data = mb.read_memory(address, length, mem_id) if ctx.obj['DEBUG']: click.echo() if file is None: click.echo(hexdump(data, address, compress)) else: try: if file.lower().endswith(('.srec', '.s19')): srec = bincopy.BinFile() srec.add_binary(data, address) srec.header = 'mboot' with open(file, "w") as f: f.write(srec.as_srec()) elif file.lower().endswith(('.hex', '.ihex')): ihex = bincopy.BinFile() ihex.add_binary(data, address) with open(file, "w") as f: f.write(ihex.as_ihex()) else: with open(file, "wb") as f: f.write(data) except Exception as e: print_error(f"Could not write to file: {file} \n [{str(e)}]") click.echo(f"\n Successfully saved into: {file}")
def erase(ctx, address, length, mass, mtype): mem_id = 0 if mtype == 'INTERNAL' else ExtMemId[mtype] device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: if mass: values = mb.get_property(PropertyTag.AVAILABLE_COMMANDS) commands = parse_property_value(PropertyTag.AVAILABLE_COMMANDS, values) if CommandTag.FLASH_ERASE_ALL_UNSECURE in commands: mb.flash_erase_all_unsecure() elif CommandTag.FLASH_ERASE_ALL in commands: mb.flash_erase_all(mem_id) else: raise Exception('Not Supported Command') else: if address is None or length is None: raise Exception( "Argument \"-a, --address\" and \"-l, --length\" must be defined !" ) mb.flash_erase_region(address, length, mem_id) if ctx.obj['DEBUG']: click.echo() click.secho(" Erased Successfully.")
def update(ctx, address): device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: mb.reliable_update(address) if ctx.obj['DEBUG']: click.echo()
def kp_read_nvm(ctx, memid): device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: mb.kp_read_nonvolatile(memid) if ctx.obj['DEBUG']: click.echo()
def kp_gen_key(ctx, key_type, key_size): device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: mb.kp_set_intrinsic_key(key_type, key_size) if ctx.obj['DEBUG']: click.echo()
def kp_enroll(ctx): device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: mb.kp_enroll() if ctx.obj['DEBUG']: click.echo()
def write(ctx, address, offset, mtype, erase, verify, file): mem_id = 0 if mtype == 'INTERNAL' else ExtMemId[mtype] in_data = bincopy.BinFile() try: if file.lower().endswith(('.srec', '.s19')): in_data.add_srec_file(file) if address is None: address = in_data.minimum_address elif file.lower().endswith(('.hex', '.ihex')): in_data.add_ihex_file(file) if address is None: address = in_data.minimum_address else: in_data.add_binary_file(file) if address is None: address = 0 data = in_data.as_binary() except Exception as e: print_error(f"Could not read from file: {file} \n [{str(e)}]") raise if offset < len(data): data = data[offset:] device = scan_interface(ctx.obj['TARGET']) click.echo(' Writing into MCU memory, please wait !\n') with McuBoot(device, True) as mb: # Read Flash Sector Size of connected MCU flash_sector_size = mb.get_property(PropertyTag.FLASH_SECTOR_SIZE, mem_id)[0] # flash_sector_size = mb.get_property(PropertyTag.FLASH_SECTOR_SIZE, mem_id)[0] # Align Erase Start Address and Len to Flash Sector Size start_address = (address & ~(flash_sector_size - 1)) length = (len(data) & ~(flash_sector_size - 1)) if (len(data) % flash_sector_size) > 0: length += flash_sector_size # Erase specified region in MCU Flash memory try: mb.flash_erase_region(start_address, length, mem_id) except mboot2.exceptions.McuBootCommandError as e: if e.error_value == mboot2.errorcodes.StatusCode.MEMORY_RANGE_INVALID: pass else: raise (e) # Write data into MCU Flash memory mb.write_memory(address, data, mem_id) if ctx.obj['DEBUG']: click.echo() click.echo(" Writen Successfully.")
def fill(ctx, address, length, pattern): device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: mb.fill_memory(address, length, pattern) if ctx.obj['DEBUG']: click.echo() click.secho(f" Memory filled successfully with pattern: 0x{pattern:X}")
def reset(ctx): device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: mb.reset(reopen=False) if ctx.obj['DEBUG']: click.echo() click.secho(" Reset success")
def execute(ctx, address, argument, stackpointer): device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: mb.execute(address, argument, stackpointer) if ctx.obj['DEBUG']: click.echo() click.secho(" Code executed")
def sbfile(ctx, file): device = scan_interface(ctx.obj['TARGET']) with open(file, 'rb') as f: sb_data = f.read() with McuBoot(device, True) as mb: mb.receive_sb_file(sb_data) if ctx.obj['DEBUG']: click.echo()
def kp_write_kstore(ctx, key_type, file): device = scan_interface(ctx.obj['TARGET']) with open(file, "rb") as f: key_data = f.read() with McuBoot(device, True) as mb: mb.kp_write_key_store(key_type, key_data) if ctx.obj['DEBUG']: click.echo()
def otp(ctx, length, address, data): print_error("ERROR: 'otp' command is not implemented yet") device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: # TODO: write implementation pass if ctx.obj['DEBUG']: click.echo()
def unlock(ctx, key): device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: if key is None: mb.flash_erase_all_unsecure() else: mb.flash_security_disable(key) if ctx.obj['DEBUG']: click.echo() click.echo(" Device unlocked successfully.")
def efuse(ctx, index, value): read_value = 0 device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: if value is not None: mb.efuse_program_once(index, value) read_value = mb.efuse_read_once(index) if ctx.obj['DEBUG']: click.echo() click.echo(f" eFuse[{index}] = 0x{read_value:08X}")
def keyblob(ctx, count, dekfile, blobfile): device = scan_interface(ctx.obj['TARGET']) with open(dekfile, "rb") as f: dek_data = f.read() with McuBoot(device, True) as mb: blob_data = mb.generate_key_blob(dek_data, count) if ctx.obj['DEBUG']: click.echo() with open(blobfile, "wb") as f: f.write(blob_data) click.echo(f" Successfully saved into: {blobfile}")
def kp_read_kstore(ctx, file): device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: key_data = mb.kp_read_key_store() if ctx.obj['DEBUG']: click.echo() if file is None: click.echo(hexdump(key_data)) else: with open(file, "wb") as f: f.write(key_data) click.echo(f" Successfully saved into: {file}")
def info(ctx): properties = [] device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: properties = mb.get_property_list() if ctx.obj['DEBUG']: click.echo() for p in properties: v = p.to_str() if isinstance(v, list): click.echo(f" {p.name}:" + "".join([f"\n - {s}" for s in v])) else: click.echo(f" {p.name}: {v}")
def mlist(ctx): mem_list = {} device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: mem_list = mb.get_memory_list() if ctx.obj['DEBUG']: click.echo() message = '' for key, values in mem_list.items(): message += " {}:\n".format(key.title().replace('_', ' ')) if key in ('internal_ram', 'internal_flash'): for i, item in values.items(): message += " {}) 0x{:08X} - 0x{:08X}, Size: {}".format( i, item['address'], item['address'] + item['size'], size_fmt(item['size'])) if 'sector_size' in item: message += ", Sector Size: {}".format( size_fmt(item['sector_size'])) message += '\n' else: for i, attr in enumerate(values): message += " {}) {}:\n".format(i, attr['mem_name']) if 'address' in attr: message += " Start Address: 0x{:08X}\n".format( attr['address']) if 'size' in attr: message += " Memory Size: {} ({} B)\n".format( size_fmt(attr['size']), attr['size']) if 'page_size' in attr: message += " Page Size: {}\n".format( attr['page_size']) if 'sector_size' in attr: message += " Sector Size: {}\n".format( attr['sector_size']) if 'block_size' in attr: message += " Block Size: {}\n".format( attr['block_size']) message += '\n' click.echo(message)
def mconf(ctx, address, word, mtype, file): memory_id = mtype memory_data = bytes() if word: for w in word: memory_data += w.to_bytes(4, 'little') if file is not None: print_error("Not implemented yet load memory configuration from file") # load memory configuration fom file with open(file, 'r') as f: # TODO: add file parser into memory_data pass if not memory_data: print_error('The argument -w/--word or -f/--file must be specified !') device = scan_interface(ctx.obj['TARGET']) with McuBoot(device, True) as mb: if address is None: # get internal memory start address and size memory_address = mb.get_property(PropertyTag.RAM_START_ADDRESS)[0] memory_size = mb.get_property(PropertyTag.RAM_SIZE)[0] # calculate address address = memory_address + memory_size - len(memory_data) # add additional offset 1024 Bytes address -= 1024 mb.write_memory(address, memory_data) mb.configure_memory(address, memory_id) if ctx.obj['DEBUG']: click.echo()