def send_payload(self, payload, block_size, num_blocks): while payload: data = payload[:block_size] packet = Packet(PacketType.PT_DATA, data=data) packet.write(self.ch) payload = payload[block_size:] done = False while not done: packet = Packet(None) packet.read(self.ch) if packet.type == PacketType.PT_CONSOLE: print(packet.data.decode("utf-8"), end='', flush=True) elif packet.type == PacketType.PT_STATUS: if packet.status != 0: raise RuntimeError('command failed: {}'.format( packet.status)) done = True
def partition_write(self, partition_name, input): if isinstance(partition_name, str): partition_name = bytes(partition_name, 'utf-8') else: raise ValueError() if isinstance(input, str): is_file = True elif isinstance(input, bytes): is_file = False else: raise ValueError() if is_file: data_size = os.path.getsize(input) file = open(input, 'rb') else: data_size = len(input) offset = pack('<Q', 0) length = pack('<Q', data_size) mmc = pack('<I', 0) if data_size % Device.MMC_BLOCK_SIZE != 0: raise RuntimeError('unimplemented') self.execute_command(Command.CMD_WRITE_P, args=offset + length + mmc + partition_name) left = data_size while left > 0: if is_file: packet = Packet(PacketType.PT_DATA, data=file.read(Device.MMC_BLOCK_SIZE)) else: packet = Packet(PacketType.PT_DATA, data=input[:Device.MMC_BLOCK_SIZE]) input = input[Device.MMC_BLOCK_SIZE:] packet.write(self.ch) left -= Device.MMC_BLOCK_SIZE print('%d bytes left ' % left, flush=True, end='\r')