def test_cbfs_stage(self): """Tests handling of a Coreboot Filesystem (CBFS)""" if not elf.ELF_TOOLS: self.skipTest('Python elftools not available') elf_fname = os.path.join(self._indir, 'cbfs-stage.elf') elf.MakeElf(elf_fname, U_BOOT_DATA, U_BOOT_DTB_DATA) size = 0xb0 cbw = CbfsWriter(size) cbw.add_file_stage('u-boot', tools.ReadFile(elf_fname)) data = cbw.get_data() cbfs = self._check_hdr(data, size) load = 0xfef20000 entry = load + 2 cfile = self._check_uboot(cbfs, cbfs_util.TYPE_STAGE, offset=0x28, data=U_BOOT_DATA + U_BOOT_DTB_DATA) self.assertEqual(entry, cfile.entry) self.assertEqual(load, cfile.load) self.assertEqual( len(U_BOOT_DATA) + len(U_BOOT_DTB_DATA), cfile.data_len) # Compare against what cbfstool creates if self.have_cbfstool: cbfs_fname = os.path.join(self._indir, 'test.cbfs') cbfs_util.cbfstool(cbfs_fname, 'create', '-m', 'x86', '-s', '%#x' % size) cbfs_util.cbfstool(cbfs_fname, 'add-stage', '-n', 'u-boot', '-f', elf_fname) self._compare_expected_cbfs(data, cbfs_fname)
def test_cbfstool_failure(self): """Test failure to run cbfstool""" if not self.have_cbfstool: self.skipTest('No cbfstool available') try: # In verbose mode this test fails since stderr is not captured. Fix # this by turning off verbosity. old_verbose = cbfs_util.VERBOSE cbfs_util.VERBOSE = False with test_util.capture_sys_output() as (_stdout, stderr): with self.assertRaises(Exception) as e: cbfs_util.cbfstool('missing-file', 'bad-command') finally: cbfs_util.VERBOSE = old_verbose self.assertIn('Unknown command', stderr.getvalue()) self.assertIn('Failed to run', str(e.exception))
def _get_expected_cbfs(self, size, arch='x86', compress=None, base=None): """Get the file created by cbfstool for a particular scenario Args: size: Size of the CBFS in bytes arch: Architecture of the CBFS, as a string compress: Compression to use, e.g. cbfs_util.COMPRESS_LZMA base: Base address of file, or None to put it anywhere Returns: Resulting CBFS file, or None if cbfstool is not available """ if not self.have_cbfstool or not self.have_lz4: return None cbfs_fname = os.path.join(self._indir, 'test.cbfs') cbfs_util.cbfstool(cbfs_fname, 'create', '-m', arch, '-s', '%#x' % size) if base: base = [(1 << 32) - size + b for b in base] cbfs_util.cbfstool(cbfs_fname, 'add', '-n', 'u-boot', '-t', 'raw', '-c', compress and compress[0] or 'none', '-f', tools.GetInputFilename( compress and 'compress' or 'u-boot.bin'), base=base[0] if base else None) cbfs_util.cbfstool(cbfs_fname, 'add', '-n', 'u-boot-dtb', '-t', 'raw', '-c', compress and compress[1] or 'none', '-f', tools.GetInputFilename( compress and 'compress' or 'u-boot.dtb'), base=base[1] if base else None) return cbfs_fname