def info(self): """Perform 'info' tasks.""" self.parser.description += " Retrieve device information." self._add_parser_con_arguments() self.parser.add_argument("--format", choices=['text', 'json'], help="presentation format [default: text]") self.args = self.parser.parse_args() cmd = self._command() # Prepare sys info request. request = qmfmlib.QFMRequest(qmfmlib.QFMRequest.REQ_SYS_INFO).content image = qmfmlib.DFUImage() data = image.add_suffix(request) # Write temp output file. This file will be passed on to dfu-utils-qda. file_name = self._create_temp(data) # Download request to device. Using alternate setting 0 (QFM). print("Requesting system information...\t", end="") retv = self.call_tools(cmd + ["-D", file_name, "-a", "0"]) if retv.status: print("[FAIL]") os.remove(file_name) exit(1) print("[DONE]") os.remove(file_name) # Create and delete a temporary file. This is done to check the file # permissions of the output file we give dfu-utils-qda to store the # result of our requested response. file_name = self._create_temp("") os.remove(file_name) # Upload response from device. Using alternate setting 0 (QFM). print("Reading system information...\t\t", end="") retv = self.call_tools(cmd + ["-U", file_name, "-a", "0"]) if retv.status: print("[FAIL]") exit(1) print("[DONE]") in_file = open(file_name, "rb") response = qmfmlib.QFMResponse(in_file.read()) in_file.close() os.remove(file_name) if not response.cmd == qmfmlib.QFMResponse.RESP_SYS_INFO: print("Error: Invalid response.") exit(1) # Parse and Present Sys-Info data. info = qmfmlib.QFMSysInfo(response.content) if self.args.format == "json": print(info.info_json()) else: print(info.info_string())
def erase(self): """Perform 'erase' tasks.""" self.parser.description += "Erase all applications on the device." self._add_parser_con_arguments() self.args = self.parser.parse_args() cmd = self._command() request = qmfmlib.QFMRequest(qmfmlib.QFMRequest.REQ_APP_ERASE).content image = qmfmlib.DFUImage() data = image.add_suffix(request) # Write output file. file_name = self._create_temp(data) print("Erasing all application data...\t\t", end="") retv = self.call_tools(cmd + ["-D", file_name, "-a", "0"]) if retv.status: print("[FAIL]") os.remove(file_name) if retv.status == DFU_STATUS_ERR_TARGET: print("Application erase is not supported by the device.") else: print("Unknown error.") if not self.args.verbose: print("Run in verbose mode for more info.") exit(1) print("[DONE]") os.remove(file_name)
def erase(self): """Perform 'erase' tasks.""" self.parser.description += "Erase all applications on the device." self._add_parser_con_arguments() self.args = self.parser.parse_args() cmd = self._command() request = qmfmlib.QFMRequest(qmfmlib.QFMRequest.REQ_APP_ERASE).content image = qmfmlib.DFUImage() data = image.add_suffix(request) # Write output file. file_name = self._create_temp(data) if not self.call_tools(cmd + ["-D", file_name, "-a", "0"], "Erasing all application data...\t\t"): os.remove(file_name) exit(1) os.remove(file_name)
try: header.set_from_file(args.config_file) args.config_file.close() except IOError as error: parser.error(error) # Create image data for output file. data = None try: image = qmfmlib.QFUImage() data = image.make(header, args.input_file) args.input_file.close() except IOError as error: parser.error(error) dfu_image = qmfmlib.DFUImage() data = dfu_image.add_suffix(data) if args.verbose > 0: header.print_info(parser.prog + ": ") # Write output file. try: fh = open(args.output_file, "wb") fh.write(data) fh.close() if not args.quiet: print("%s: %s written" % (parser.prog, args.output_file)) except IOError as error: parser.error(error)
def set_key(self, key_type): self._add_parser_con_arguments() self.parser.add_argument("new_key_file", metavar="key", type=argparse.FileType('rb'), help="specify the new key file") self.parser.add_argument("--curr-fw-key", dest="curr_fw_key_file", metavar="CURRENT_FW_KEY", type=argparse.FileType('rb'), required=False, help="specify the current fw key file") self.parser.add_argument( "--curr-rv-key", dest="curr_rv_key_file", metavar="CURRENT_RV_KEY", type=argparse.FileType('rb'), required=False, help="specify the current revocation key file") self.args = self.parser.parse_args() cmd = self._command() # Read the content of the new key file self.args.new_key_file.seek(0, os.SEEK_END) # Check if the size is correct if self.args.new_key_file.tell() != 32: self.args.new_key_file.close() raise QMManageException("Incorrect length of the new key") self.args.new_key_file.seek(0, os.SEEK_SET) new_key_content = self.args.new_key_file.read() # Read the content of the current fw key file if self.args.curr_fw_key_file: self.args.curr_fw_key_file.seek(0, os.SEEK_END) # Check if the size is correct if self.args.curr_fw_key_file.tell() != 32: self.args.curr_fw_key_file.close() raise QMManageException("Incorrect length of the current fw \ key") self.args.curr_fw_key_file.seek(0, os.SEEK_SET) curr_fw_key_content = self.args.curr_fw_key_file.read() else: curr_fw_key_content = "" # Read the content of the current revocation key file if self.args.curr_rv_key_file: self.args.curr_rv_key_file.seek(0, os.SEEK_END) # Check if the size is correct if self.args.curr_rv_key_file.tell() != 32: self.args.curr_rv_key_file.close() raise QMManageException("Incorrect length of the current rv \ key") self.args.curr_rv_key_file.seek(0, os.SEEK_SET) curr_rv_key_content = self.args.curr_rv_key_file.read() else: curr_rv_key_content = "" request = qmfmlib.QFMSetKey(new_key_content, curr_fw_key_content, curr_rv_key_content, key_type).content image = qmfmlib.DFUImage() data = image.add_suffix(request) # Write output file. file_name = self._create_temp(data) print("Programming new device key...\t\t", end="") retv = self.call_tools(cmd + ["-D", file_name, "-a", "0"]) if retv.status != 0: print("[FAIL]") if retv.status == DFU_STATUS_ERR_TARGET: print("key provisioning is not supported by the device.") elif retv.status == DFU_STATUS_ERR_VENDOR: print("Key verification failed.") else: print("Unknown error.") if not self.args.verbose: print("Run in verbose mode for more info.") os.remove(file_name) exit(1) print("[DONE]") os.remove(file_name) self.args.new_key_file.close() if self.args.curr_fw_key_file: self.args.curr_fw_key_file.close()