def ObtainContents(self): gbb = 'gbb.bin' fname = tools.GetOutputFilename(gbb) if not self.size: self.Raise('GBB must have a fixed size') gbb_size = self.size bmpfv_size = gbb_size - 0x2180 if bmpfv_size < 0: self.Raise('GBB is too small (minimum 0x2180 bytes)') sizes = [0x100, 0x1000, bmpfv_size, 0x1000] sizes = ['%#x' % size for size in sizes] keydir = tools.GetInputFilename(self.keydir) gbb_set_command = [ 'gbb_utility', '-s', '--hwid=%s' % self.hardware_id, '--rootkey=%s/root_key.vbpubk' % keydir, '--recoverykey=%s/recovery_key.vbpubk' % keydir, '--flags=%d' % self.gbb_flags, '--bmpfv=%s' % tools.GetInputFilename(self.bmpblk), fname ] tools.Run('futility', 'gbb_utility', '-c', ','.join(sizes), fname) tools.Run('futility', *gbb_set_command) self.SetContents(tools.ReadFile(fname)) return True
def setUpClass(cls): # Create a temporary directory for test files cls._indir = tempfile.mkdtemp(prefix='cbfs_util.') tools.SetInputDirs([cls._indir]) # Set up some useful data files TestCbfs._make_input_file('u-boot.bin', U_BOOT_DATA) TestCbfs._make_input_file('u-boot.dtb', U_BOOT_DTB_DATA) TestCbfs._make_input_file('compress', COMPRESS_DATA) # Set up a temporary output directory, used by the tools library when # compressing files tools.PrepareOutputDir(None) cls.have_cbfstool = True try: tools.Run('which', 'cbfstool') except: cls.have_cbfstool = False cls.have_lz4 = True try: tools.Run('lz4', '--no-frame-crc', '-c', tools.GetInputFilename('u-boot.bin'), binary=True) except: cls.have_lz4 = False
def RunTools(self, tools, out, tmpdir): """Create a vblock for the given firmware image""" self.path = os.path.join(tmpdir, 'vblock.%s' % self.label) input_data = os.path.join(tmpdir, 'input.%s' % self.label) try: prefix = self.pack.props['keydir'] + '/' # Join up the data files to be signed data = self.pack.tools.ReadFileAndConcat( self.value, self.compress, self.with_index)[0] tools.WriteFile(input_data, data) args = [ '--vblock', self.path, '--keyblock', prefix + self.keyblock, '--signprivate', prefix + self.signprivate, '--version', '%d' % self.version, '--fv', input_data, '--kernelkey', prefix + self.kernelkey, '--flags', '%d' % self.preamble_flags, ] out.Notice("Sign '%s' into %s" % (', '.join(self.value), self.label)) stdout = tools.Run('vbutil_firmware', args) out.Debug(stdout) # Update value to the actual filename to be used self.value = [self.path] except CmdError as err: raise PackError('Cannot make key block: vbutil_firmware failed\n%s' % err)
def ProduceFinalImage(self, tools, out, tmpdir, image_fname): """Produce the final image for an Intel ME system Some Intel systems require that an image contains the Management Engine firmware, and also a firmware descriptor. This function takes the existing image, removes the front part of it, and replaces it with these required pieces using ifdtool. Args: tools: Tools object to use to run tools. out: Output object to send output to tmpdir: Temporary directory to use to create required files. image_fname: Output image filename """ out.Progress('Setting up Intel ME') data = tools.ReadFile(image_fname) # We can assume that the ifd section is at the start of the image. if self.offset != 0: raise ConfigError('IFD section must be at offset 0 in the image') data = data[self.size:] input_fname = os.path.join(tmpdir, 'ifd-input.bin') tools.WriteFile(input_fname, data) ifd_output = os.path.join(tmpdir, 'image.ifd') # This works by modifying a skeleton file. shutil.copyfile(tools.Filename(self.pack.props['skeleton']), ifd_output) args = ['-i', 'BIOS:%s' % input_fname, ifd_output] tools.Run('ifdtool', args) # ifdtool puts the output in a file with '.new' tacked on the end. shutil.move(ifd_output + '.new', image_fname) tools.OutputSize('IFD image', image_fname)
def ObtainContents(self): # Join up the data files to be signed input_data = '' for entry_phandle in self.content: data = self.section.GetContentsByPhandle(entry_phandle, self) if data is None: # Data not available yet return False input_data += data uniq = self.GetUniqueName() output_fname = tools.GetOutputFilename('vblock.%s' % uniq) input_fname = tools.GetOutputFilename('input.%s' % uniq) tools.WriteFile(input_fname, input_data) prefix = self.keydir + '/' args = [ 'vbutil_firmware', '--vblock', output_fname, '--keyblock', prefix + self.keyblock, '--signprivate', prefix + self.signprivate, '--version', '%d' % self.version, '--fv', input_fname, '--kernelkey', prefix + self.kernelkey, '--flags', '%d' % self.preamble_flags, ] #out.Notice("Sign '%s' into %s" % (', '.join(self.value), self.label)) stdout = tools.Run('futility', *args) self.SetContents(tools.ReadFile(output_fname)) return True
def ReadBlobContents(self): if self._strip: uniq = self.GetUniqueName() out_fname = tools.GetOutputFilename('%s.stripped' % uniq) tools.WriteFile(out_fname, tools.ReadFile(self._pathname)) tools.Run('strip', out_fname) self._pathname = out_fname Entry_blob.ReadBlobContents(self) return True
def BuildElfTestFiles(target_dir): """Build ELF files used for testing in binman This compiles and links the test files into the specified directory. It the Makefile and source files in the binman test/ directory. Args: target_dir: Directory to put the files into """ if not os.path.exists(target_dir): os.mkdir(target_dir) testdir = os.path.join(binman_dir, 'test') # If binman is involved from the main U-Boot Makefile the -r and -R # flags are set in MAKEFLAGS. This prevents this Makefile from working # correctly. So drop any make flags here. if 'MAKEFLAGS' in os.environ: del os.environ['MAKEFLAGS'] tools.Run('make', '-C', target_dir, '-f', os.path.join(testdir, 'Makefile'), 'SRC=%s/' % testdir)
def GetSymbols(fname, patterns): """Get the symbols from an ELF file Args: fname: Filename of the ELF file to read patterns: List of regex patterns to search for, each a string Returns: None, if the file does not exist, or Dict: key: Name of symbol value: Hex value of symbol """ stdout = tools.Run('objdump', '-t', fname) lines = stdout.splitlines() if patterns: re_syms = re.compile('|'.join(patterns)) else: re_syms = None syms = {} syms_started = False for line in lines: if not line or not syms_started: if 'SYMBOL TABLE' in line: syms_started = True line = None # Otherwise code coverage complains about 'continue' continue if re_syms and not re_syms.search(line): continue space_pos = line.find(' ') value, rest = line[:space_pos], line[space_pos + 1:] flags = rest[:7] parts = rest[7:].split() section, size = parts[:2] if len(parts) > 2: name = parts[2] if parts[2] != '.hidden' else parts[3] syms[name] = Symbol(section, int(value, 16), int(size, 16), flags[1] == 'w') # Sort dict by address return OrderedDict(sorted(syms.items(), key=lambda x: x[1].address))
def ReadBlobContents(self): # We assume the data is small enough to fit into memory. If this # is used for large filesystem image that might not be true. # In that case, Image.BuildImage() could be adjusted to use a # new Entry method which can read in chunks. Then we could copy # the data in chunks and avoid reading it all at once. For now # this seems like an unnecessary complication. data = tools.ReadFile(self._pathname) if self._compress == 'lz4': self._uncompressed_size = len(data) ''' import lz4 # Import this only if needed (python-lz4 dependency) try: data = lz4.frame.compress(data) except AttributeError: data = lz4.compress(data) ''' data = tools.Run('lz4', '-c', self._pathname, binary=True) self.SetContents(data) return True