cbfs_util_test.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. #!/usr/bin/env python
  2. # SPDX-License-Identifier: GPL-2.0+
  3. # Copyright 2019 Google LLC
  4. # Written by Simon Glass <sjg@chromium.org>
  5. """Tests for cbfs_util
  6. These create and read various CBFSs and compare the results with expected
  7. values and with cbfstool
  8. """
  9. import io
  10. import os
  11. import shutil
  12. import struct
  13. import tempfile
  14. import unittest
  15. from binman import cbfs_util
  16. from binman.cbfs_util import CbfsWriter
  17. from binman import elf
  18. from patman import test_util
  19. from patman import tools
  20. U_BOOT_DATA = b'1234'
  21. U_BOOT_DTB_DATA = b'udtb'
  22. COMPRESS_DATA = b'compress xxxxxxxxxxxxxxxxxxxxxx data'
  23. class TestCbfs(unittest.TestCase):
  24. """Test of cbfs_util classes"""
  25. #pylint: disable=W0212
  26. @classmethod
  27. def setUpClass(cls):
  28. # Create a temporary directory for test files
  29. cls._indir = tempfile.mkdtemp(prefix='cbfs_util.')
  30. tools.SetInputDirs([cls._indir])
  31. # Set up some useful data files
  32. TestCbfs._make_input_file('u-boot.bin', U_BOOT_DATA)
  33. TestCbfs._make_input_file('u-boot.dtb', U_BOOT_DTB_DATA)
  34. TestCbfs._make_input_file('compress', COMPRESS_DATA)
  35. # Set up a temporary output directory, used by the tools library when
  36. # compressing files
  37. tools.PrepareOutputDir(None)
  38. cls.have_cbfstool = True
  39. try:
  40. tools.Run('which', 'cbfstool')
  41. except:
  42. cls.have_cbfstool = False
  43. cls.have_lz4 = True
  44. try:
  45. tools.Run('lz4', '--no-frame-crc', '-c',
  46. tools.GetInputFilename('u-boot.bin'), binary=True)
  47. except:
  48. cls.have_lz4 = False
  49. @classmethod
  50. def tearDownClass(cls):
  51. """Remove the temporary input directory and its contents"""
  52. if cls._indir:
  53. shutil.rmtree(cls._indir)
  54. cls._indir = None
  55. tools.FinaliseOutputDir()
  56. @classmethod
  57. def _make_input_file(cls, fname, contents):
  58. """Create a new test input file, creating directories as needed
  59. Args:
  60. fname: Filename to create
  61. contents: File contents to write in to the file
  62. Returns:
  63. Full pathname of file created
  64. """
  65. pathname = os.path.join(cls._indir, fname)
  66. tools.WriteFile(pathname, contents)
  67. return pathname
  68. def _check_hdr(self, data, size, offset=0, arch=cbfs_util.ARCHITECTURE_X86):
  69. """Check that the CBFS has the expected header
  70. Args:
  71. data: Data to check
  72. size: Expected ROM size
  73. offset: Expected offset to first CBFS file
  74. arch: Expected architecture
  75. Returns:
  76. CbfsReader object containing the CBFS
  77. """
  78. cbfs = cbfs_util.CbfsReader(data)
  79. self.assertEqual(cbfs_util.HEADER_MAGIC, cbfs.magic)
  80. self.assertEqual(cbfs_util.HEADER_VERSION2, cbfs.version)
  81. self.assertEqual(size, cbfs.rom_size)
  82. self.assertEqual(0, cbfs.boot_block_size)
  83. self.assertEqual(cbfs_util.ENTRY_ALIGN, cbfs.align)
  84. self.assertEqual(offset, cbfs.cbfs_offset)
  85. self.assertEqual(arch, cbfs.arch)
  86. return cbfs
  87. def _check_uboot(self, cbfs, ftype=cbfs_util.TYPE_RAW, offset=0x38,
  88. data=U_BOOT_DATA, cbfs_offset=None):
  89. """Check that the U-Boot file is as expected
  90. Args:
  91. cbfs: CbfsReader object to check
  92. ftype: Expected file type
  93. offset: Expected offset of file
  94. data: Expected data in file
  95. cbfs_offset: Expected CBFS offset for file's data
  96. Returns:
  97. CbfsFile object containing the file
  98. """
  99. self.assertIn('u-boot', cbfs.files)
  100. cfile = cbfs.files['u-boot']
  101. self.assertEqual('u-boot', cfile.name)
  102. self.assertEqual(offset, cfile.offset)
  103. if cbfs_offset is not None:
  104. self.assertEqual(cbfs_offset, cfile.cbfs_offset)
  105. self.assertEqual(data, cfile.data)
  106. self.assertEqual(ftype, cfile.ftype)
  107. self.assertEqual(cbfs_util.COMPRESS_NONE, cfile.compress)
  108. self.assertEqual(len(data), cfile.memlen)
  109. return cfile
  110. def _check_dtb(self, cbfs, offset=0x38, data=U_BOOT_DTB_DATA,
  111. cbfs_offset=None):
  112. """Check that the U-Boot dtb file is as expected
  113. Args:
  114. cbfs: CbfsReader object to check
  115. offset: Expected offset of file
  116. data: Expected data in file
  117. cbfs_offset: Expected CBFS offset for file's data
  118. """
  119. self.assertIn('u-boot-dtb', cbfs.files)
  120. cfile = cbfs.files['u-boot-dtb']
  121. self.assertEqual('u-boot-dtb', cfile.name)
  122. self.assertEqual(offset, cfile.offset)
  123. if cbfs_offset is not None:
  124. self.assertEqual(cbfs_offset, cfile.cbfs_offset)
  125. self.assertEqual(U_BOOT_DTB_DATA, cfile.data)
  126. self.assertEqual(cbfs_util.TYPE_RAW, cfile.ftype)
  127. self.assertEqual(cbfs_util.COMPRESS_NONE, cfile.compress)
  128. self.assertEqual(len(U_BOOT_DTB_DATA), cfile.memlen)
  129. def _check_raw(self, data, size, offset=0, arch=cbfs_util.ARCHITECTURE_X86):
  130. """Check that two raw files are added as expected
  131. Args:
  132. data: Data to check
  133. size: Expected ROM size
  134. offset: Expected offset to first CBFS file
  135. arch: Expected architecture
  136. """
  137. cbfs = self._check_hdr(data, size, offset=offset, arch=arch)
  138. self._check_uboot(cbfs)
  139. self._check_dtb(cbfs)
  140. def _get_expected_cbfs(self, size, arch='x86', compress=None, base=None):
  141. """Get the file created by cbfstool for a particular scenario
  142. Args:
  143. size: Size of the CBFS in bytes
  144. arch: Architecture of the CBFS, as a string
  145. compress: Compression to use, e.g. cbfs_util.COMPRESS_LZMA
  146. base: Base address of file, or None to put it anywhere
  147. Returns:
  148. Resulting CBFS file, or None if cbfstool is not available
  149. """
  150. if not self.have_cbfstool or not self.have_lz4:
  151. return None
  152. cbfs_fname = os.path.join(self._indir, 'test.cbfs')
  153. cbfs_util.cbfstool(cbfs_fname, 'create', '-m', arch, '-s', '%#x' % size)
  154. if base:
  155. base = [(1 << 32) - size + b for b in base]
  156. cbfs_util.cbfstool(cbfs_fname, 'add', '-n', 'u-boot', '-t', 'raw',
  157. '-c', compress and compress[0] or 'none',
  158. '-f', tools.GetInputFilename(
  159. compress and 'compress' or 'u-boot.bin'),
  160. base=base[0] if base else None)
  161. cbfs_util.cbfstool(cbfs_fname, 'add', '-n', 'u-boot-dtb', '-t', 'raw',
  162. '-c', compress and compress[1] or 'none',
  163. '-f', tools.GetInputFilename(
  164. compress and 'compress' or 'u-boot.dtb'),
  165. base=base[1] if base else None)
  166. return cbfs_fname
  167. def _compare_expected_cbfs(self, data, cbfstool_fname):
  168. """Compare against what cbfstool creates
  169. This compares what binman creates with what cbfstool creates for what
  170. is proportedly the same thing.
  171. Args:
  172. data: CBFS created by binman
  173. cbfstool_fname: CBFS created by cbfstool
  174. """
  175. if not self.have_cbfstool or not self.have_lz4:
  176. return
  177. expect = tools.ReadFile(cbfstool_fname)
  178. if expect != data:
  179. tools.WriteFile('/tmp/expect', expect)
  180. tools.WriteFile('/tmp/actual', data)
  181. print('diff -y <(xxd -g1 /tmp/expect) <(xxd -g1 /tmp/actual) | colordiff')
  182. self.fail('cbfstool produced a different result')
  183. def test_cbfs_functions(self):
  184. """Test global functions of cbfs_util"""
  185. self.assertEqual(cbfs_util.ARCHITECTURE_X86, cbfs_util.find_arch('x86'))
  186. self.assertIsNone(cbfs_util.find_arch('bad-arch'))
  187. self.assertEqual(cbfs_util.COMPRESS_LZMA, cbfs_util.find_compress('lzma'))
  188. self.assertIsNone(cbfs_util.find_compress('bad-comp'))
  189. def test_cbfstool_failure(self):
  190. """Test failure to run cbfstool"""
  191. if not self.have_cbfstool:
  192. self.skipTest('No cbfstool available')
  193. try:
  194. # In verbose mode this test fails since stderr is not captured. Fix
  195. # this by turning off verbosity.
  196. old_verbose = cbfs_util.VERBOSE
  197. cbfs_util.VERBOSE = False
  198. with test_util.capture_sys_output() as (_stdout, stderr):
  199. with self.assertRaises(Exception) as e:
  200. cbfs_util.cbfstool('missing-file', 'bad-command')
  201. finally:
  202. cbfs_util.VERBOSE = old_verbose
  203. self.assertIn('Unknown command', stderr.getvalue())
  204. self.assertIn('Failed to run', str(e.exception))
  205. def test_cbfs_raw(self):
  206. """Test base handling of a Coreboot Filesystem (CBFS)"""
  207. size = 0xb0
  208. cbw = CbfsWriter(size)
  209. cbw.add_file_raw('u-boot', U_BOOT_DATA)
  210. cbw.add_file_raw('u-boot-dtb', U_BOOT_DTB_DATA)
  211. data = cbw.get_data()
  212. self._check_raw(data, size)
  213. cbfs_fname = self._get_expected_cbfs(size=size)
  214. self._compare_expected_cbfs(data, cbfs_fname)
  215. def test_cbfs_invalid_file_type(self):
  216. """Check handling of an invalid file type when outputiing a CBFS"""
  217. size = 0xb0
  218. cbw = CbfsWriter(size)
  219. cfile = cbw.add_file_raw('u-boot', U_BOOT_DATA)
  220. # Change the type manually before generating the CBFS, and make sure
  221. # that the generator complains
  222. cfile.ftype = 0xff
  223. with self.assertRaises(ValueError) as e:
  224. cbw.get_data()
  225. self.assertIn('Unknown type 0xff when writing', str(e.exception))
  226. def test_cbfs_invalid_file_type_on_read(self):
  227. """Check handling of an invalid file type when reading the CBFS"""
  228. size = 0xb0
  229. cbw = CbfsWriter(size)
  230. cbw.add_file_raw('u-boot', U_BOOT_DATA)
  231. data = cbw.get_data()
  232. # Read in the first file header
  233. cbr = cbfs_util.CbfsReader(data, read=False)
  234. with io.BytesIO(data) as fd:
  235. self.assertTrue(cbr._find_and_read_header(fd, len(data)))
  236. pos = fd.tell()
  237. hdr_data = fd.read(cbfs_util.FILE_HEADER_LEN)
  238. magic, size, ftype, attr, offset = struct.unpack(
  239. cbfs_util.FILE_HEADER_FORMAT, hdr_data)
  240. # Create a new CBFS with a change to the file type
  241. ftype = 0xff
  242. newdata = data[:pos]
  243. newdata += struct.pack(cbfs_util.FILE_HEADER_FORMAT, magic, size, ftype,
  244. attr, offset)
  245. newdata += data[pos + cbfs_util.FILE_HEADER_LEN:]
  246. # Read in this CBFS and make sure that the reader complains
  247. with self.assertRaises(ValueError) as e:
  248. cbfs_util.CbfsReader(newdata)
  249. self.assertIn('Unknown type 0xff when reading', str(e.exception))
  250. def test_cbfs_no_space(self):
  251. """Check handling of running out of space in the CBFS"""
  252. size = 0x60
  253. cbw = CbfsWriter(size)
  254. cbw.add_file_raw('u-boot', U_BOOT_DATA)
  255. with self.assertRaises(ValueError) as e:
  256. cbw.get_data()
  257. self.assertIn('No space for header', str(e.exception))
  258. def test_cbfs_no_space_skip(self):
  259. """Check handling of running out of space in CBFS with file header"""
  260. size = 0x5c
  261. cbw = CbfsWriter(size, arch=cbfs_util.ARCHITECTURE_PPC64)
  262. cbw._add_fileheader = True
  263. cbw.add_file_raw('u-boot', U_BOOT_DATA)
  264. with self.assertRaises(ValueError) as e:
  265. cbw.get_data()
  266. self.assertIn('No space for data before offset', str(e.exception))
  267. def test_cbfs_no_space_pad(self):
  268. """Check handling of running out of space in CBFS with file header"""
  269. size = 0x70
  270. cbw = CbfsWriter(size)
  271. cbw._add_fileheader = True
  272. cbw.add_file_raw('u-boot', U_BOOT_DATA)
  273. with self.assertRaises(ValueError) as e:
  274. cbw.get_data()
  275. self.assertIn('No space for data before pad offset', str(e.exception))
  276. def test_cbfs_bad_header_ptr(self):
  277. """Check handling of a bad master-header pointer"""
  278. size = 0x70
  279. cbw = CbfsWriter(size)
  280. cbw.add_file_raw('u-boot', U_BOOT_DATA)
  281. data = cbw.get_data()
  282. # Add one to the pointer to make it invalid
  283. newdata = data[:-4] + struct.pack('<I', cbw._header_offset + 1)
  284. # We should still be able to find the master header by searching
  285. with test_util.capture_sys_output() as (stdout, _stderr):
  286. cbfs = cbfs_util.CbfsReader(newdata)
  287. self.assertIn('Relative offset seems wrong', stdout.getvalue())
  288. self.assertIn('u-boot', cbfs.files)
  289. self.assertEqual(size, cbfs.rom_size)
  290. def test_cbfs_bad_header(self):
  291. """Check handling of a bad master header"""
  292. size = 0x70
  293. cbw = CbfsWriter(size)
  294. cbw.add_file_raw('u-boot', U_BOOT_DATA)
  295. data = cbw.get_data()
  296. # Drop most of the header and try reading the modified CBFS
  297. newdata = data[:cbw._header_offset + 4]
  298. with test_util.capture_sys_output() as (stdout, _stderr):
  299. with self.assertRaises(ValueError) as e:
  300. cbfs_util.CbfsReader(newdata)
  301. self.assertIn('Relative offset seems wrong', stdout.getvalue())
  302. self.assertIn('Cannot find master header', str(e.exception))
  303. def test_cbfs_bad_file_header(self):
  304. """Check handling of a bad file header"""
  305. size = 0x70
  306. cbw = CbfsWriter(size)
  307. cbw.add_file_raw('u-boot', U_BOOT_DATA)
  308. data = cbw.get_data()
  309. # Read in the CBFS master header (only), then stop
  310. cbr = cbfs_util.CbfsReader(data, read=False)
  311. with io.BytesIO(data) as fd:
  312. self.assertTrue(cbr._find_and_read_header(fd, len(data)))
  313. pos = fd.tell()
  314. # Remove all but 4 bytes of the file headerm and try to read the file
  315. newdata = data[:pos + 4]
  316. with test_util.capture_sys_output() as (stdout, _stderr):
  317. with io.BytesIO(newdata) as fd:
  318. fd.seek(pos)
  319. self.assertEqual(False, cbr._read_next_file(fd))
  320. self.assertIn('File header at 0x0 ran out of data', stdout.getvalue())
  321. def test_cbfs_bad_file_string(self):
  322. """Check handling of an incomplete filename string"""
  323. size = 0x70
  324. cbw = CbfsWriter(size)
  325. cbw.add_file_raw('16-characters xx', U_BOOT_DATA)
  326. data = cbw.get_data()
  327. # Read in the CBFS master header (only), then stop
  328. cbr = cbfs_util.CbfsReader(data, read=False)
  329. with io.BytesIO(data) as fd:
  330. self.assertTrue(cbr._find_and_read_header(fd, len(data)))
  331. pos = fd.tell()
  332. # Create a new CBFS with only the first 16 bytes of the file name, then
  333. # try to read the file
  334. newdata = data[:pos + cbfs_util.FILE_HEADER_LEN + 16]
  335. with test_util.capture_sys_output() as (stdout, _stderr):
  336. with io.BytesIO(newdata) as fd:
  337. fd.seek(pos)
  338. self.assertEqual(False, cbr._read_next_file(fd))
  339. self.assertIn('String at %#x ran out of data' %
  340. cbfs_util.FILE_HEADER_LEN, stdout.getvalue())
  341. def test_cbfs_debug(self):
  342. """Check debug output"""
  343. size = 0x70
  344. cbw = CbfsWriter(size)
  345. cbw.add_file_raw('u-boot', U_BOOT_DATA)
  346. data = cbw.get_data()
  347. try:
  348. cbfs_util.DEBUG = True
  349. with test_util.capture_sys_output() as (stdout, _stderr):
  350. cbfs_util.CbfsReader(data)
  351. self.assertEqual('name u-boot\ndata %s\n' % U_BOOT_DATA,
  352. stdout.getvalue())
  353. finally:
  354. cbfs_util.DEBUG = False
  355. def test_cbfs_bad_attribute(self):
  356. """Check handling of bad attribute tag"""
  357. if not self.have_lz4:
  358. self.skipTest('lz4 --no-frame-crc not available')
  359. size = 0x140
  360. cbw = CbfsWriter(size)
  361. cbw.add_file_raw('u-boot', COMPRESS_DATA, None,
  362. compress=cbfs_util.COMPRESS_LZ4)
  363. data = cbw.get_data()
  364. # Search the CBFS for the expected compression tag
  365. with io.BytesIO(data) as fd:
  366. while True:
  367. pos = fd.tell()
  368. tag, = struct.unpack('>I', fd.read(4))
  369. if tag == cbfs_util.FILE_ATTR_TAG_COMPRESSION:
  370. break
  371. # Create a new CBFS with the tag changed to something invalid
  372. newdata = data[:pos] + struct.pack('>I', 0x123) + data[pos + 4:]
  373. with test_util.capture_sys_output() as (stdout, _stderr):
  374. cbfs_util.CbfsReader(newdata)
  375. self.assertEqual('Unknown attribute tag 123\n', stdout.getvalue())
  376. def test_cbfs_missing_attribute(self):
  377. """Check handling of an incomplete attribute tag"""
  378. if not self.have_lz4:
  379. self.skipTest('lz4 --no-frame-crc not available')
  380. size = 0x140
  381. cbw = CbfsWriter(size)
  382. cbw.add_file_raw('u-boot', COMPRESS_DATA, None,
  383. compress=cbfs_util.COMPRESS_LZ4)
  384. data = cbw.get_data()
  385. # Read in the CBFS master header (only), then stop
  386. cbr = cbfs_util.CbfsReader(data, read=False)
  387. with io.BytesIO(data) as fd:
  388. self.assertTrue(cbr._find_and_read_header(fd, len(data)))
  389. pos = fd.tell()
  390. # Create a new CBFS with only the first 4 bytes of the compression tag,
  391. # then try to read the file
  392. tag_pos = pos + cbfs_util.FILE_HEADER_LEN + cbfs_util.FILENAME_ALIGN
  393. newdata = data[:tag_pos + 4]
  394. with test_util.capture_sys_output() as (stdout, _stderr):
  395. with io.BytesIO(newdata) as fd:
  396. fd.seek(pos)
  397. self.assertEqual(False, cbr._read_next_file(fd))
  398. self.assertIn('Attribute tag at %x ran out of data' % tag_pos,
  399. stdout.getvalue())
  400. def test_cbfs_file_master_header(self):
  401. """Check handling of a file containing a master header"""
  402. size = 0x100
  403. cbw = CbfsWriter(size)
  404. cbw._add_fileheader = True
  405. cbw.add_file_raw('u-boot', U_BOOT_DATA)
  406. data = cbw.get_data()
  407. cbr = cbfs_util.CbfsReader(data)
  408. self.assertIn('u-boot', cbr.files)
  409. self.assertEqual(size, cbr.rom_size)
  410. def test_cbfs_arch(self):
  411. """Test on non-x86 architecture"""
  412. size = 0x100
  413. cbw = CbfsWriter(size, arch=cbfs_util.ARCHITECTURE_PPC64)
  414. cbw.add_file_raw('u-boot', U_BOOT_DATA)
  415. cbw.add_file_raw('u-boot-dtb', U_BOOT_DTB_DATA)
  416. data = cbw.get_data()
  417. self._check_raw(data, size, offset=0x40,
  418. arch=cbfs_util.ARCHITECTURE_PPC64)
  419. # Compare against what cbfstool creates
  420. cbfs_fname = self._get_expected_cbfs(size=size, arch='ppc64')
  421. self._compare_expected_cbfs(data, cbfs_fname)
  422. def test_cbfs_stage(self):
  423. """Tests handling of a Coreboot Filesystem (CBFS)"""
  424. if not elf.ELF_TOOLS:
  425. self.skipTest('Python elftools not available')
  426. elf_fname = os.path.join(self._indir, 'cbfs-stage.elf')
  427. elf.MakeElf(elf_fname, U_BOOT_DATA, U_BOOT_DTB_DATA)
  428. size = 0xb0
  429. cbw = CbfsWriter(size)
  430. cbw.add_file_stage('u-boot', tools.ReadFile(elf_fname))
  431. data = cbw.get_data()
  432. cbfs = self._check_hdr(data, size)
  433. load = 0xfef20000
  434. entry = load + 2
  435. cfile = self._check_uboot(cbfs, cbfs_util.TYPE_STAGE, offset=0x28,
  436. data=U_BOOT_DATA + U_BOOT_DTB_DATA)
  437. self.assertEqual(entry, cfile.entry)
  438. self.assertEqual(load, cfile.load)
  439. self.assertEqual(len(U_BOOT_DATA) + len(U_BOOT_DTB_DATA),
  440. cfile.data_len)
  441. # Compare against what cbfstool creates
  442. if self.have_cbfstool:
  443. cbfs_fname = os.path.join(self._indir, 'test.cbfs')
  444. cbfs_util.cbfstool(cbfs_fname, 'create', '-m', 'x86', '-s',
  445. '%#x' % size)
  446. cbfs_util.cbfstool(cbfs_fname, 'add-stage', '-n', 'u-boot',
  447. '-f', elf_fname)
  448. self._compare_expected_cbfs(data, cbfs_fname)
  449. def test_cbfs_raw_compress(self):
  450. """Test base handling of compressing raw files"""
  451. if not self.have_lz4:
  452. self.skipTest('lz4 --no-frame-crc not available')
  453. size = 0x140
  454. cbw = CbfsWriter(size)
  455. cbw.add_file_raw('u-boot', COMPRESS_DATA, None,
  456. compress=cbfs_util.COMPRESS_LZ4)
  457. cbw.add_file_raw('u-boot-dtb', COMPRESS_DATA, None,
  458. compress=cbfs_util.COMPRESS_LZMA)
  459. data = cbw.get_data()
  460. cbfs = self._check_hdr(data, size)
  461. self.assertIn('u-boot', cbfs.files)
  462. cfile = cbfs.files['u-boot']
  463. self.assertEqual(cfile.name, 'u-boot')
  464. self.assertEqual(cfile.offset, 56)
  465. self.assertEqual(cfile.data, COMPRESS_DATA)
  466. self.assertEqual(cfile.ftype, cbfs_util.TYPE_RAW)
  467. self.assertEqual(cfile.compress, cbfs_util.COMPRESS_LZ4)
  468. self.assertEqual(cfile.memlen, len(COMPRESS_DATA))
  469. self.assertIn('u-boot-dtb', cbfs.files)
  470. cfile = cbfs.files['u-boot-dtb']
  471. self.assertEqual(cfile.name, 'u-boot-dtb')
  472. self.assertEqual(cfile.offset, 56)
  473. self.assertEqual(cfile.data, COMPRESS_DATA)
  474. self.assertEqual(cfile.ftype, cbfs_util.TYPE_RAW)
  475. self.assertEqual(cfile.compress, cbfs_util.COMPRESS_LZMA)
  476. self.assertEqual(cfile.memlen, len(COMPRESS_DATA))
  477. cbfs_fname = self._get_expected_cbfs(size=size, compress=['lz4', 'lzma'])
  478. self._compare_expected_cbfs(data, cbfs_fname)
  479. def test_cbfs_raw_space(self):
  480. """Test files with unused space in the CBFS"""
  481. size = 0xf0
  482. cbw = CbfsWriter(size)
  483. cbw.add_file_raw('u-boot', U_BOOT_DATA)
  484. cbw.add_file_raw('u-boot-dtb', U_BOOT_DTB_DATA)
  485. data = cbw.get_data()
  486. self._check_raw(data, size)
  487. cbfs_fname = self._get_expected_cbfs(size=size)
  488. self._compare_expected_cbfs(data, cbfs_fname)
  489. def test_cbfs_offset(self):
  490. """Test a CBFS with files at particular offsets"""
  491. size = 0x200
  492. cbw = CbfsWriter(size)
  493. cbw.add_file_raw('u-boot', U_BOOT_DATA, 0x40)
  494. cbw.add_file_raw('u-boot-dtb', U_BOOT_DTB_DATA, 0x140)
  495. data = cbw.get_data()
  496. cbfs = self._check_hdr(data, size)
  497. self._check_uboot(cbfs, ftype=cbfs_util.TYPE_RAW, offset=0x40,
  498. cbfs_offset=0x40)
  499. self._check_dtb(cbfs, offset=0x40, cbfs_offset=0x140)
  500. cbfs_fname = self._get_expected_cbfs(size=size, base=(0x40, 0x140))
  501. self._compare_expected_cbfs(data, cbfs_fname)
  502. def test_cbfs_invalid_file_type_header(self):
  503. """Check handling of an invalid file type when outputting a header"""
  504. size = 0xb0
  505. cbw = CbfsWriter(size)
  506. cfile = cbw.add_file_raw('u-boot', U_BOOT_DATA, 0)
  507. # Change the type manually before generating the CBFS, and make sure
  508. # that the generator complains
  509. cfile.ftype = 0xff
  510. with self.assertRaises(ValueError) as e:
  511. cbw.get_data()
  512. self.assertIn('Unknown file type 0xff', str(e.exception))
  513. def test_cbfs_offset_conflict(self):
  514. """Test a CBFS with files that want to overlap"""
  515. size = 0x200
  516. cbw = CbfsWriter(size)
  517. cbw.add_file_raw('u-boot', U_BOOT_DATA, 0x40)
  518. cbw.add_file_raw('u-boot-dtb', U_BOOT_DTB_DATA, 0x80)
  519. with self.assertRaises(ValueError) as e:
  520. cbw.get_data()
  521. self.assertIn('No space for data before pad offset', str(e.exception))
  522. def test_cbfs_check_offset(self):
  523. """Test that we can discover the offset of a file after writing it"""
  524. size = 0xb0
  525. cbw = CbfsWriter(size)
  526. cbw.add_file_raw('u-boot', U_BOOT_DATA)
  527. cbw.add_file_raw('u-boot-dtb', U_BOOT_DTB_DATA)
  528. data = cbw.get_data()
  529. cbfs = cbfs_util.CbfsReader(data)
  530. self.assertEqual(0x38, cbfs.files['u-boot'].cbfs_offset)
  531. self.assertEqual(0x78, cbfs.files['u-boot-dtb'].cbfs_offset)
  532. if __name__ == '__main__':
  533. unittest.main()