def DumpFlashImage(self, args): flash_fd, flash_path = tempfiles.CreateGRRTempFileVFS() flash_fd.write("\xff" * 1024) flash_fd.close() logs = ["test"] if args.log_level else [] response = chipsec_types.DumpFlashImageResponse(path=flash_path, logs=logs) return [response]
def EficheckDumpImage(self, args): flash_fd, flash_path = tempfiles.CreateGRRTempFileVFS() flash_fd.close() stdout = "Image successfully written to firmware.bin." exec_response = rdf_client_action.ExecuteBinaryResponse( stdout=stdout.encode("utf-8"), exit_status=0) response = rdf_apple_firmware.DumpEfiImageResponse( eficheck_version="1.9.6", response=exec_response, path=flash_path) return [response]
def Run(self, args): """Read from a VFS file and write to a GRRTempFile on disk. If file writing doesn't complete files won't be cleaned up. Args: args: see CopyPathToFile in jobs.proto """ src_fd = vfs.VFSOpen(args.src_path, progress_callback=self.Progress) src_fd.Seek(args.offset) offset = src_fd.Tell() length = args.length or (1024**4) # 1 TB suffix = ".gz" if args.gzip_output else "" dest_fd, dest_pathspec = tempfiles.CreateGRRTempFileVFS( lifetime=args.lifetime, suffix=suffix) dest_file = dest_fd.name with dest_fd: if args.gzip_output: gzip_fd = gzip.GzipFile(dest_file, "wb", 9, dest_fd) # Gzip filehandle needs its own close method called with gzip_fd: written = self._Copy(src_fd, gzip_fd, length) else: written = self._Copy(src_fd, dest_fd, length) self.SendReply( rdf_client.CopyPathToFileRequest( offset=offset, length=written, src_path=args.src_path, dest_path=dest_pathspec, gzip_output=args.gzip_output))
def Run(self, args): # Due to talking raw to hardware, this action has some inevitable risk of # crashing the machine, so we need to flush the transaction log to ensure # we know when this happens. self.SyncTransactionLog() # Temporary extra logging for Ubuntu # TODO(user): Add generic hunt flag to notify syslog before running each # client action. if args.notify_syslog: syslog = logging.getLogger("chipsec_grr") syslog.setLevel(logging.INFO) syslog.addHandler(handlers.SysLogHandler(address="/dev/log")) syslog.info("%s: Runnning DumpFlashImage", config.CONFIG["Client.name"]) self.logs = [] # TODO(hanuszczak): This appears to be something that could be made into # `StringIO` instead of `BytesIO`. self.chipsec_log = io.BytesIO() if args.log_level: logger.logger().UTIL_TRACE = True if args.log_level == 2: logger.logger().VERBOSE = True logger.logger().logfile = self.chipsec_log logger.logger().LOG_TO_FILE = True # Create a temporary file to store the flash image. dest_fd, dest_pathspec = tempfiles.CreateGRRTempFileVFS( suffix=".flash") # Wrap most of Chipsec code to gather its logs in case of failure. try: # Initialise Chipsec (die early if unknown chipset) c = chipset.cs() # Platform = None, Start Driver = False c.init(None, False) s = spi.SPI(c) # Use hal.spi from chipsec to write BIOS to that file. with dest_fd: # Based on Chipsec code, rely on the address of BIOS(=1) region to # determine the size of the flash. _, limit, _ = s.get_SPI_region(1) spi_size = limit + 1 # Read args.chunk_size bytes at a time and heartbeat. bios = [] for i in range(0, spi_size, args.chunk_size): bios.extend(s.read_spi(i, args.chunk_size)) self.Progress() dest_fd.write("".join(bios)) except (chipset.UnknownChipsetError, oshelper.OsHelperError) as err: # If the chipset is not recognised or if the helper threw an error, # report gracefully the error to the flow. if args.log_level: self.LogError(err) tempfiles.DeleteGRRTempFile(dest_pathspec.path) self.SendReply( rdf_chipsec_types.DumpFlashImageResponse(logs=["%s" % err], )) return except Exception as err: # pylint: disable=broad-except # In case an exception is raised, if the verbose mode # is enabled, return the raw logs from Chipsec. if args.log_level: self.LogError(err) tempfiles.DeleteGRRTempFile(dest_pathspec.path) raise if args.log_level: self.logs.extend(self.chipsec_log.getvalue().splitlines()) if args.notify_syslog: syslog.info("%s: DumpFlashImage has completed successfully", config.CONFIG["Client.name"]) self.SendReply( rdf_chipsec_types.DumpFlashImageResponse(path=dest_pathspec, logs=self.logs))
def open(self, directory=None, filename=None, mode="rb"): fd, pathspec = tempfiles.CreateGRRTempFileVFS(filename=filename, mode=mode) self.SendMessage(["file", pathspec.ToPrimitiveDict()]) return fd