ftest.py 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2016 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # To run a single test, change to this directory, and:
  6. #
  7. # python -m unittest func_test.TestFunctional.testHelp
  8. from optparse import OptionParser
  9. import os
  10. import shutil
  11. import struct
  12. import sys
  13. import tempfile
  14. import unittest
  15. import binman
  16. import cmdline
  17. import command
  18. import control
  19. import elf
  20. import fdt
  21. import fdt_util
  22. import fmap_util
  23. import test_util
  24. import state
  25. import tools
  26. import tout
  27. # Contents of test files, corresponding to different entry types
  28. U_BOOT_DATA = '1234'
  29. U_BOOT_IMG_DATA = 'img'
  30. U_BOOT_SPL_DATA = '56780123456789abcde'
  31. U_BOOT_TPL_DATA = 'tpl'
  32. BLOB_DATA = '89'
  33. ME_DATA = '0abcd'
  34. VGA_DATA = 'vga'
  35. U_BOOT_DTB_DATA = 'udtb'
  36. U_BOOT_SPL_DTB_DATA = 'spldtb'
  37. U_BOOT_TPL_DTB_DATA = 'tpldtb'
  38. X86_START16_DATA = 'start16'
  39. X86_START16_SPL_DATA = 'start16spl'
  40. X86_START16_TPL_DATA = 'start16tpl'
  41. U_BOOT_NODTB_DATA = 'nodtb with microcode pointer somewhere in here'
  42. U_BOOT_SPL_NODTB_DATA = 'splnodtb with microcode pointer somewhere in here'
  43. FSP_DATA = 'fsp'
  44. CMC_DATA = 'cmc'
  45. VBT_DATA = 'vbt'
  46. MRC_DATA = 'mrc'
  47. TEXT_DATA = 'text'
  48. TEXT_DATA2 = 'text2'
  49. TEXT_DATA3 = 'text3'
  50. CROS_EC_RW_DATA = 'ecrw'
  51. GBB_DATA = 'gbbd'
  52. BMPBLK_DATA = 'bmp'
  53. VBLOCK_DATA = 'vblk'
  54. FILES_DATA = ("sorry I'm late\nOh, don't bother apologising, I'm " +
  55. "sorry you're alive\n")
  56. COMPRESS_DATA = 'data to compress'
  57. class TestFunctional(unittest.TestCase):
  58. """Functional tests for binman
  59. Most of these use a sample .dts file to build an image and then check
  60. that it looks correct. The sample files are in the test/ subdirectory
  61. and are numbered.
  62. For each entry type a very small test file is created using fixed
  63. string contents. This makes it easy to test that things look right, and
  64. debug problems.
  65. In some cases a 'real' file must be used - these are also supplied in
  66. the test/ diurectory.
  67. """
  68. @classmethod
  69. def setUpClass(self):
  70. global entry
  71. import entry
  72. # Handle the case where argv[0] is 'python'
  73. self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
  74. self._binman_pathname = os.path.join(self._binman_dir, 'binman')
  75. # Create a temporary directory for input files
  76. self._indir = tempfile.mkdtemp(prefix='binmant.')
  77. # Create some test files
  78. TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA)
  79. TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA)
  80. TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA)
  81. TestFunctional._MakeInputFile('tpl/u-boot-tpl.bin', U_BOOT_TPL_DATA)
  82. TestFunctional._MakeInputFile('blobfile', BLOB_DATA)
  83. TestFunctional._MakeInputFile('me.bin', ME_DATA)
  84. TestFunctional._MakeInputFile('vga.bin', VGA_DATA)
  85. self._ResetDtbs()
  86. TestFunctional._MakeInputFile('u-boot-x86-16bit.bin', X86_START16_DATA)
  87. TestFunctional._MakeInputFile('spl/u-boot-x86-16bit-spl.bin',
  88. X86_START16_SPL_DATA)
  89. TestFunctional._MakeInputFile('tpl/u-boot-x86-16bit-tpl.bin',
  90. X86_START16_TPL_DATA)
  91. TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA)
  92. TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin',
  93. U_BOOT_SPL_NODTB_DATA)
  94. TestFunctional._MakeInputFile('fsp.bin', FSP_DATA)
  95. TestFunctional._MakeInputFile('cmc.bin', CMC_DATA)
  96. TestFunctional._MakeInputFile('vbt.bin', VBT_DATA)
  97. TestFunctional._MakeInputFile('mrc.bin', MRC_DATA)
  98. TestFunctional._MakeInputFile('ecrw.bin', CROS_EC_RW_DATA)
  99. TestFunctional._MakeInputDir('devkeys')
  100. TestFunctional._MakeInputFile('bmpblk.bin', BMPBLK_DATA)
  101. self._output_setup = False
  102. # ELF file with a '_dt_ucode_base_size' symbol
  103. with open(self.TestFile('u_boot_ucode_ptr')) as fd:
  104. TestFunctional._MakeInputFile('u-boot', fd.read())
  105. # Intel flash descriptor file
  106. with open(self.TestFile('descriptor.bin')) as fd:
  107. TestFunctional._MakeInputFile('descriptor.bin', fd.read())
  108. shutil.copytree(self.TestFile('files'),
  109. os.path.join(self._indir, 'files'))
  110. TestFunctional._MakeInputFile('compress', COMPRESS_DATA)
  111. @classmethod
  112. def tearDownClass(self):
  113. """Remove the temporary input directory and its contents"""
  114. if self._indir:
  115. shutil.rmtree(self._indir)
  116. self._indir = None
  117. def setUp(self):
  118. # Enable this to turn on debugging output
  119. # tout.Init(tout.DEBUG)
  120. command.test_result = None
  121. def tearDown(self):
  122. """Remove the temporary output directory"""
  123. tools._FinaliseForTest()
  124. @classmethod
  125. def _ResetDtbs(self):
  126. TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
  127. TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA)
  128. TestFunctional._MakeInputFile('tpl/u-boot-tpl.dtb', U_BOOT_TPL_DTB_DATA)
  129. def _RunBinman(self, *args, **kwargs):
  130. """Run binman using the command line
  131. Args:
  132. Arguments to pass, as a list of strings
  133. kwargs: Arguments to pass to Command.RunPipe()
  134. """
  135. result = command.RunPipe([[self._binman_pathname] + list(args)],
  136. capture=True, capture_stderr=True, raise_on_error=False)
  137. if result.return_code and kwargs.get('raise_on_error', True):
  138. raise Exception("Error running '%s': %s" % (' '.join(args),
  139. result.stdout + result.stderr))
  140. return result
  141. def _DoBinman(self, *args):
  142. """Run binman using directly (in the same process)
  143. Args:
  144. Arguments to pass, as a list of strings
  145. Returns:
  146. Return value (0 for success)
  147. """
  148. args = list(args)
  149. if '-D' in sys.argv:
  150. args = args + ['-D']
  151. (options, args) = cmdline.ParseArgs(args)
  152. options.pager = 'binman-invalid-pager'
  153. options.build_dir = self._indir
  154. # For testing, you can force an increase in verbosity here
  155. # options.verbosity = tout.DEBUG
  156. return control.Binman(options, args)
  157. def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False,
  158. entry_args=None, images=None, use_real_dtb=False):
  159. """Run binman with a given test file
  160. Args:
  161. fname: Device-tree source filename to use (e.g. 05_simple.dts)
  162. debug: True to enable debugging output
  163. map: True to output map files for the images
  164. update_dtb: Update the offset and size of each entry in the device
  165. tree before packing it into the image
  166. entry_args: Dict of entry args to supply to binman
  167. key: arg name
  168. value: value of that arg
  169. images: List of image names to build
  170. """
  171. args = ['-p', '-I', self._indir, '-d', self.TestFile(fname)]
  172. if debug:
  173. args.append('-D')
  174. if map:
  175. args.append('-m')
  176. if update_dtb:
  177. args.append('-up')
  178. if not use_real_dtb:
  179. args.append('--fake-dtb')
  180. if entry_args:
  181. for arg, value in entry_args.iteritems():
  182. args.append('-a%s=%s' % (arg, value))
  183. if images:
  184. for image in images:
  185. args += ['-i', image]
  186. return self._DoBinman(*args)
  187. def _SetupDtb(self, fname, outfile='u-boot.dtb'):
  188. """Set up a new test device-tree file
  189. The given file is compiled and set up as the device tree to be used
  190. for ths test.
  191. Args:
  192. fname: Filename of .dts file to read
  193. outfile: Output filename for compiled device-tree binary
  194. Returns:
  195. Contents of device-tree binary
  196. """
  197. if not self._output_setup:
  198. tools.PrepareOutputDir(self._indir, True)
  199. self._output_setup = True
  200. dtb = fdt_util.EnsureCompiled(self.TestFile(fname))
  201. with open(dtb) as fd:
  202. data = fd.read()
  203. TestFunctional._MakeInputFile(outfile, data)
  204. return data
  205. def _GetDtbContentsForSplTpl(self, dtb_data, name):
  206. """Create a version of the main DTB for SPL or SPL
  207. For testing we don't actually have different versions of the DTB. With
  208. U-Boot we normally run fdtgrep to remove unwanted nodes, but for tests
  209. we don't normally have any unwanted nodes.
  210. We still want the DTBs for SPL and TPL to be different though, since
  211. otherwise it is confusing to know which one we are looking at. So add
  212. an 'spl' or 'tpl' property to the top-level node.
  213. """
  214. dtb = fdt.Fdt.FromData(dtb_data)
  215. dtb.Scan()
  216. dtb.GetNode('/binman').AddZeroProp(name)
  217. dtb.Sync(auto_resize=True)
  218. dtb.Pack()
  219. return dtb.GetContents()
  220. def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False,
  221. update_dtb=False, entry_args=None, reset_dtbs=True):
  222. """Run binman and return the resulting image
  223. This runs binman with a given test file and then reads the resulting
  224. output file. It is a shortcut function since most tests need to do
  225. these steps.
  226. Raises an assertion failure if binman returns a non-zero exit code.
  227. Args:
  228. fname: Device-tree source filename to use (e.g. 05_simple.dts)
  229. use_real_dtb: True to use the test file as the contents of
  230. the u-boot-dtb entry. Normally this is not needed and the
  231. test contents (the U_BOOT_DTB_DATA string) can be used.
  232. But in some test we need the real contents.
  233. map: True to output map files for the images
  234. update_dtb: Update the offset and size of each entry in the device
  235. tree before packing it into the image
  236. Returns:
  237. Tuple:
  238. Resulting image contents
  239. Device tree contents
  240. Map data showing contents of image (or None if none)
  241. Output device tree binary filename ('u-boot.dtb' path)
  242. """
  243. dtb_data = None
  244. # Use the compiled test file as the u-boot-dtb input
  245. if use_real_dtb:
  246. dtb_data = self._SetupDtb(fname)
  247. infile = os.path.join(self._indir, 'u-boot.dtb')
  248. # For testing purposes, make a copy of the DT for SPL and TPL. Add
  249. # a node indicating which it is, so aid verification.
  250. for name in ['spl', 'tpl']:
  251. dtb_fname = '%s/u-boot-%s.dtb' % (name, name)
  252. outfile = os.path.join(self._indir, dtb_fname)
  253. TestFunctional._MakeInputFile(dtb_fname,
  254. self._GetDtbContentsForSplTpl(dtb_data, name))
  255. try:
  256. retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb,
  257. entry_args=entry_args, use_real_dtb=use_real_dtb)
  258. self.assertEqual(0, retcode)
  259. out_dtb_fname = tools.GetOutputFilename('u-boot.dtb.out')
  260. # Find the (only) image, read it and return its contents
  261. image = control.images['image']
  262. image_fname = tools.GetOutputFilename('image.bin')
  263. self.assertTrue(os.path.exists(image_fname))
  264. if map:
  265. map_fname = tools.GetOutputFilename('image.map')
  266. with open(map_fname) as fd:
  267. map_data = fd.read()
  268. else:
  269. map_data = None
  270. with open(image_fname) as fd:
  271. return fd.read(), dtb_data, map_data, out_dtb_fname
  272. finally:
  273. # Put the test file back
  274. if reset_dtbs and use_real_dtb:
  275. self._ResetDtbs()
  276. def _DoReadFile(self, fname, use_real_dtb=False):
  277. """Helper function which discards the device-tree binary
  278. Args:
  279. fname: Device-tree source filename to use (e.g. 05_simple.dts)
  280. use_real_dtb: True to use the test file as the contents of
  281. the u-boot-dtb entry. Normally this is not needed and the
  282. test contents (the U_BOOT_DTB_DATA string) can be used.
  283. But in some test we need the real contents.
  284. Returns:
  285. Resulting image contents
  286. """
  287. return self._DoReadFileDtb(fname, use_real_dtb)[0]
  288. @classmethod
  289. def _MakeInputFile(self, fname, contents):
  290. """Create a new test input file, creating directories as needed
  291. Args:
  292. fname: Filename to create
  293. contents: File contents to write in to the file
  294. Returns:
  295. Full pathname of file created
  296. """
  297. pathname = os.path.join(self._indir, fname)
  298. dirname = os.path.dirname(pathname)
  299. if dirname and not os.path.exists(dirname):
  300. os.makedirs(dirname)
  301. with open(pathname, 'wb') as fd:
  302. fd.write(contents)
  303. return pathname
  304. @classmethod
  305. def _MakeInputDir(self, dirname):
  306. """Create a new test input directory, creating directories as needed
  307. Args:
  308. dirname: Directory name to create
  309. Returns:
  310. Full pathname of directory created
  311. """
  312. pathname = os.path.join(self._indir, dirname)
  313. if not os.path.exists(pathname):
  314. os.makedirs(pathname)
  315. return pathname
  316. @classmethod
  317. def TestFile(self, fname):
  318. return os.path.join(self._binman_dir, 'test', fname)
  319. def AssertInList(self, grep_list, target):
  320. """Assert that at least one of a list of things is in a target
  321. Args:
  322. grep_list: List of strings to check
  323. target: Target string
  324. """
  325. for grep in grep_list:
  326. if grep in target:
  327. return
  328. self.fail("Error: '%' not found in '%s'" % (grep_list, target))
  329. def CheckNoGaps(self, entries):
  330. """Check that all entries fit together without gaps
  331. Args:
  332. entries: List of entries to check
  333. """
  334. offset = 0
  335. for entry in entries.values():
  336. self.assertEqual(offset, entry.offset)
  337. offset += entry.size
  338. def GetFdtLen(self, dtb):
  339. """Get the totalsize field from a device-tree binary
  340. Args:
  341. dtb: Device-tree binary contents
  342. Returns:
  343. Total size of device-tree binary, from the header
  344. """
  345. return struct.unpack('>L', dtb[4:8])[0]
  346. def _GetPropTree(self, dtb, prop_names):
  347. def AddNode(node, path):
  348. if node.name != '/':
  349. path += '/' + node.name
  350. for subnode in node.subnodes:
  351. for prop in subnode.props.values():
  352. if prop.name in prop_names:
  353. prop_path = path + '/' + subnode.name + ':' + prop.name
  354. tree[prop_path[len('/binman/'):]] = fdt_util.fdt32_to_cpu(
  355. prop.value)
  356. AddNode(subnode, path)
  357. tree = {}
  358. AddNode(dtb.GetRoot(), '')
  359. return tree
  360. def testRun(self):
  361. """Test a basic run with valid args"""
  362. result = self._RunBinman('-h')
  363. def testFullHelp(self):
  364. """Test that the full help is displayed with -H"""
  365. result = self._RunBinman('-H')
  366. help_file = os.path.join(self._binman_dir, 'README')
  367. # Remove possible extraneous strings
  368. extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n'
  369. gothelp = result.stdout.replace(extra, '')
  370. self.assertEqual(len(gothelp), os.path.getsize(help_file))
  371. self.assertEqual(0, len(result.stderr))
  372. self.assertEqual(0, result.return_code)
  373. def testFullHelpInternal(self):
  374. """Test that the full help is displayed with -H"""
  375. try:
  376. command.test_result = command.CommandResult()
  377. result = self._DoBinman('-H')
  378. help_file = os.path.join(self._binman_dir, 'README')
  379. finally:
  380. command.test_result = None
  381. def testHelp(self):
  382. """Test that the basic help is displayed with -h"""
  383. result = self._RunBinman('-h')
  384. self.assertTrue(len(result.stdout) > 200)
  385. self.assertEqual(0, len(result.stderr))
  386. self.assertEqual(0, result.return_code)
  387. def testBoard(self):
  388. """Test that we can run it with a specific board"""
  389. self._SetupDtb('05_simple.dts', 'sandbox/u-boot.dtb')
  390. TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA)
  391. result = self._DoBinman('-b', 'sandbox')
  392. self.assertEqual(0, result)
  393. def testNeedBoard(self):
  394. """Test that we get an error when no board ius supplied"""
  395. with self.assertRaises(ValueError) as e:
  396. result = self._DoBinman()
  397. self.assertIn("Must provide a board to process (use -b <board>)",
  398. str(e.exception))
  399. def testMissingDt(self):
  400. """Test that an invalid device-tree file generates an error"""
  401. with self.assertRaises(Exception) as e:
  402. self._RunBinman('-d', 'missing_file')
  403. # We get one error from libfdt, and a different one from fdtget.
  404. self.AssertInList(["Couldn't open blob from 'missing_file'",
  405. 'No such file or directory'], str(e.exception))
  406. def testBrokenDt(self):
  407. """Test that an invalid device-tree source file generates an error
  408. Since this is a source file it should be compiled and the error
  409. will come from the device-tree compiler (dtc).
  410. """
  411. with self.assertRaises(Exception) as e:
  412. self._RunBinman('-d', self.TestFile('01_invalid.dts'))
  413. self.assertIn("FATAL ERROR: Unable to parse input tree",
  414. str(e.exception))
  415. def testMissingNode(self):
  416. """Test that a device tree without a 'binman' node generates an error"""
  417. with self.assertRaises(Exception) as e:
  418. self._DoBinman('-d', self.TestFile('02_missing_node.dts'))
  419. self.assertIn("does not have a 'binman' node", str(e.exception))
  420. def testEmpty(self):
  421. """Test that an empty binman node works OK (i.e. does nothing)"""
  422. result = self._RunBinman('-d', self.TestFile('03_empty.dts'))
  423. self.assertEqual(0, len(result.stderr))
  424. self.assertEqual(0, result.return_code)
  425. def testInvalidEntry(self):
  426. """Test that an invalid entry is flagged"""
  427. with self.assertRaises(Exception) as e:
  428. result = self._RunBinman('-d',
  429. self.TestFile('04_invalid_entry.dts'))
  430. self.assertIn("Unknown entry type 'not-a-valid-type' in node "
  431. "'/binman/not-a-valid-type'", str(e.exception))
  432. def testSimple(self):
  433. """Test a simple binman with a single file"""
  434. data = self._DoReadFile('05_simple.dts')
  435. self.assertEqual(U_BOOT_DATA, data)
  436. def testSimpleDebug(self):
  437. """Test a simple binman run with debugging enabled"""
  438. data = self._DoTestFile('05_simple.dts', debug=True)
  439. def testDual(self):
  440. """Test that we can handle creating two images
  441. This also tests image padding.
  442. """
  443. retcode = self._DoTestFile('06_dual_image.dts')
  444. self.assertEqual(0, retcode)
  445. image = control.images['image1']
  446. self.assertEqual(len(U_BOOT_DATA), image._size)
  447. fname = tools.GetOutputFilename('image1.bin')
  448. self.assertTrue(os.path.exists(fname))
  449. with open(fname) as fd:
  450. data = fd.read()
  451. self.assertEqual(U_BOOT_DATA, data)
  452. image = control.images['image2']
  453. self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size)
  454. fname = tools.GetOutputFilename('image2.bin')
  455. self.assertTrue(os.path.exists(fname))
  456. with open(fname) as fd:
  457. data = fd.read()
  458. self.assertEqual(U_BOOT_DATA, data[3:7])
  459. self.assertEqual(chr(0) * 3, data[:3])
  460. self.assertEqual(chr(0) * 5, data[7:])
  461. def testBadAlign(self):
  462. """Test that an invalid alignment value is detected"""
  463. with self.assertRaises(ValueError) as e:
  464. self._DoTestFile('07_bad_align.dts')
  465. self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power "
  466. "of two", str(e.exception))
  467. def testPackSimple(self):
  468. """Test that packing works as expected"""
  469. retcode = self._DoTestFile('08_pack.dts')
  470. self.assertEqual(0, retcode)
  471. self.assertIn('image', control.images)
  472. image = control.images['image']
  473. entries = image.GetEntries()
  474. self.assertEqual(5, len(entries))
  475. # First u-boot
  476. self.assertIn('u-boot', entries)
  477. entry = entries['u-boot']
  478. self.assertEqual(0, entry.offset)
  479. self.assertEqual(len(U_BOOT_DATA), entry.size)
  480. # Second u-boot, aligned to 16-byte boundary
  481. self.assertIn('u-boot-align', entries)
  482. entry = entries['u-boot-align']
  483. self.assertEqual(16, entry.offset)
  484. self.assertEqual(len(U_BOOT_DATA), entry.size)
  485. # Third u-boot, size 23 bytes
  486. self.assertIn('u-boot-size', entries)
  487. entry = entries['u-boot-size']
  488. self.assertEqual(20, entry.offset)
  489. self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
  490. self.assertEqual(23, entry.size)
  491. # Fourth u-boot, placed immediate after the above
  492. self.assertIn('u-boot-next', entries)
  493. entry = entries['u-boot-next']
  494. self.assertEqual(43, entry.offset)
  495. self.assertEqual(len(U_BOOT_DATA), entry.size)
  496. # Fifth u-boot, placed at a fixed offset
  497. self.assertIn('u-boot-fixed', entries)
  498. entry = entries['u-boot-fixed']
  499. self.assertEqual(61, entry.offset)
  500. self.assertEqual(len(U_BOOT_DATA), entry.size)
  501. self.assertEqual(65, image._size)
  502. def testPackExtra(self):
  503. """Test that extra packing feature works as expected"""
  504. retcode = self._DoTestFile('09_pack_extra.dts')
  505. self.assertEqual(0, retcode)
  506. self.assertIn('image', control.images)
  507. image = control.images['image']
  508. entries = image.GetEntries()
  509. self.assertEqual(5, len(entries))
  510. # First u-boot with padding before and after
  511. self.assertIn('u-boot', entries)
  512. entry = entries['u-boot']
  513. self.assertEqual(0, entry.offset)
  514. self.assertEqual(3, entry.pad_before)
  515. self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size)
  516. # Second u-boot has an aligned size, but it has no effect
  517. self.assertIn('u-boot-align-size-nop', entries)
  518. entry = entries['u-boot-align-size-nop']
  519. self.assertEqual(12, entry.offset)
  520. self.assertEqual(4, entry.size)
  521. # Third u-boot has an aligned size too
  522. self.assertIn('u-boot-align-size', entries)
  523. entry = entries['u-boot-align-size']
  524. self.assertEqual(16, entry.offset)
  525. self.assertEqual(32, entry.size)
  526. # Fourth u-boot has an aligned end
  527. self.assertIn('u-boot-align-end', entries)
  528. entry = entries['u-boot-align-end']
  529. self.assertEqual(48, entry.offset)
  530. self.assertEqual(16, entry.size)
  531. # Fifth u-boot immediately afterwards
  532. self.assertIn('u-boot-align-both', entries)
  533. entry = entries['u-boot-align-both']
  534. self.assertEqual(64, entry.offset)
  535. self.assertEqual(64, entry.size)
  536. self.CheckNoGaps(entries)
  537. self.assertEqual(128, image._size)
  538. def testPackAlignPowerOf2(self):
  539. """Test that invalid entry alignment is detected"""
  540. with self.assertRaises(ValueError) as e:
  541. self._DoTestFile('10_pack_align_power2.dts')
  542. self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power "
  543. "of two", str(e.exception))
  544. def testPackAlignSizePowerOf2(self):
  545. """Test that invalid entry size alignment is detected"""
  546. with self.assertRaises(ValueError) as e:
  547. self._DoTestFile('11_pack_align_size_power2.dts')
  548. self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a "
  549. "power of two", str(e.exception))
  550. def testPackInvalidAlign(self):
  551. """Test detection of an offset that does not match its alignment"""
  552. with self.assertRaises(ValueError) as e:
  553. self._DoTestFile('12_pack_inv_align.dts')
  554. self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match "
  555. "align 0x4 (4)", str(e.exception))
  556. def testPackInvalidSizeAlign(self):
  557. """Test that invalid entry size alignment is detected"""
  558. with self.assertRaises(ValueError) as e:
  559. self._DoTestFile('13_pack_inv_size_align.dts')
  560. self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match "
  561. "align-size 0x4 (4)", str(e.exception))
  562. def testPackOverlap(self):
  563. """Test that overlapping regions are detected"""
  564. with self.assertRaises(ValueError) as e:
  565. self._DoTestFile('14_pack_overlap.dts')
  566. self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps "
  567. "with previous entry '/binman/u-boot' ending at 0x4 (4)",
  568. str(e.exception))
  569. def testPackEntryOverflow(self):
  570. """Test that entries that overflow their size are detected"""
  571. with self.assertRaises(ValueError) as e:
  572. self._DoTestFile('15_pack_overflow.dts')
  573. self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) "
  574. "but entry size is 0x3 (3)", str(e.exception))
  575. def testPackImageOverflow(self):
  576. """Test that entries which overflow the image size are detected"""
  577. with self.assertRaises(ValueError) as e:
  578. self._DoTestFile('16_pack_image_overflow.dts')
  579. self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section "
  580. "size 0x3 (3)", str(e.exception))
  581. def testPackImageSize(self):
  582. """Test that the image size can be set"""
  583. retcode = self._DoTestFile('17_pack_image_size.dts')
  584. self.assertEqual(0, retcode)
  585. self.assertIn('image', control.images)
  586. image = control.images['image']
  587. self.assertEqual(7, image._size)
  588. def testPackImageSizeAlign(self):
  589. """Test that image size alignemnt works as expected"""
  590. retcode = self._DoTestFile('18_pack_image_align.dts')
  591. self.assertEqual(0, retcode)
  592. self.assertIn('image', control.images)
  593. image = control.images['image']
  594. self.assertEqual(16, image._size)
  595. def testPackInvalidImageAlign(self):
  596. """Test that invalid image alignment is detected"""
  597. with self.assertRaises(ValueError) as e:
  598. self._DoTestFile('19_pack_inv_image_align.dts')
  599. self.assertIn("Section '/binman': Size 0x7 (7) does not match "
  600. "align-size 0x8 (8)", str(e.exception))
  601. def testPackAlignPowerOf2(self):
  602. """Test that invalid image alignment is detected"""
  603. with self.assertRaises(ValueError) as e:
  604. self._DoTestFile('20_pack_inv_image_align_power2.dts')
  605. self.assertIn("Section '/binman': Alignment size 131 must be a power of "
  606. "two", str(e.exception))
  607. def testImagePadByte(self):
  608. """Test that the image pad byte can be specified"""
  609. with open(self.TestFile('bss_data')) as fd:
  610. TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
  611. data = self._DoReadFile('21_image_pad.dts')
  612. self.assertEqual(U_BOOT_SPL_DATA + (chr(0xff) * 1) + U_BOOT_DATA, data)
  613. def testImageName(self):
  614. """Test that image files can be named"""
  615. retcode = self._DoTestFile('22_image_name.dts')
  616. self.assertEqual(0, retcode)
  617. image = control.images['image1']
  618. fname = tools.GetOutputFilename('test-name')
  619. self.assertTrue(os.path.exists(fname))
  620. image = control.images['image2']
  621. fname = tools.GetOutputFilename('test-name.xx')
  622. self.assertTrue(os.path.exists(fname))
  623. def testBlobFilename(self):
  624. """Test that generic blobs can be provided by filename"""
  625. data = self._DoReadFile('23_blob.dts')
  626. self.assertEqual(BLOB_DATA, data)
  627. def testPackSorted(self):
  628. """Test that entries can be sorted"""
  629. data = self._DoReadFile('24_sorted.dts')
  630. self.assertEqual(chr(0) * 1 + U_BOOT_SPL_DATA + chr(0) * 2 +
  631. U_BOOT_DATA, data)
  632. def testPackZeroOffset(self):
  633. """Test that an entry at offset 0 is not given a new offset"""
  634. with self.assertRaises(ValueError) as e:
  635. self._DoTestFile('25_pack_zero_size.dts')
  636. self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps "
  637. "with previous entry '/binman/u-boot' ending at 0x4 (4)",
  638. str(e.exception))
  639. def testPackUbootDtb(self):
  640. """Test that a device tree can be added to U-Boot"""
  641. data = self._DoReadFile('26_pack_u_boot_dtb.dts')
  642. self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data)
  643. def testPackX86RomNoSize(self):
  644. """Test that the end-at-4gb property requires a size property"""
  645. with self.assertRaises(ValueError) as e:
  646. self._DoTestFile('27_pack_4gb_no_size.dts')
  647. self.assertIn("Section '/binman': Section size must be provided when "
  648. "using end-at-4gb", str(e.exception))
  649. def testPackX86RomOutside(self):
  650. """Test that the end-at-4gb property checks for offset boundaries"""
  651. with self.assertRaises(ValueError) as e:
  652. self._DoTestFile('28_pack_4gb_outside.dts')
  653. self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) is outside "
  654. "the section starting at 0xffffffe0 (4294967264)",
  655. str(e.exception))
  656. def testPackX86Rom(self):
  657. """Test that a basic x86 ROM can be created"""
  658. data = self._DoReadFile('29_x86-rom.dts')
  659. self.assertEqual(U_BOOT_DATA + chr(0) * 7 + U_BOOT_SPL_DATA +
  660. chr(0) * 2, data)
  661. def testPackX86RomMeNoDesc(self):
  662. """Test that an invalid Intel descriptor entry is detected"""
  663. TestFunctional._MakeInputFile('descriptor.bin', '')
  664. with self.assertRaises(ValueError) as e:
  665. self._DoTestFile('31_x86-rom-me.dts')
  666. self.assertIn("Node '/binman/intel-descriptor': Cannot find FD "
  667. "signature", str(e.exception))
  668. def testPackX86RomBadDesc(self):
  669. """Test that the Intel requires a descriptor entry"""
  670. with self.assertRaises(ValueError) as e:
  671. self._DoTestFile('30_x86-rom-me-no-desc.dts')
  672. self.assertIn("Node '/binman/intel-me': No offset set with "
  673. "offset-unset: should another entry provide this correct "
  674. "offset?", str(e.exception))
  675. def testPackX86RomMe(self):
  676. """Test that an x86 ROM with an ME region can be created"""
  677. data = self._DoReadFile('31_x86-rom-me.dts')
  678. self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)])
  679. def testPackVga(self):
  680. """Test that an image with a VGA binary can be created"""
  681. data = self._DoReadFile('32_intel-vga.dts')
  682. self.assertEqual(VGA_DATA, data[:len(VGA_DATA)])
  683. def testPackStart16(self):
  684. """Test that an image with an x86 start16 region can be created"""
  685. data = self._DoReadFile('33_x86-start16.dts')
  686. self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)])
  687. def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False):
  688. """Handle running a test for insertion of microcode
  689. Args:
  690. dts_fname: Name of test .dts file
  691. nodtb_data: Data that we expect in the first section
  692. ucode_second: True if the microsecond entry is second instead of
  693. third
  694. Returns:
  695. Tuple:
  696. Contents of first region (U-Boot or SPL)
  697. Offset and size components of microcode pointer, as inserted
  698. in the above (two 4-byte words)
  699. """
  700. data = self._DoReadFile(dts_fname, True)
  701. # Now check the device tree has no microcode
  702. if ucode_second:
  703. ucode_content = data[len(nodtb_data):]
  704. ucode_pos = len(nodtb_data)
  705. dtb_with_ucode = ucode_content[16:]
  706. fdt_len = self.GetFdtLen(dtb_with_ucode)
  707. else:
  708. dtb_with_ucode = data[len(nodtb_data):]
  709. fdt_len = self.GetFdtLen(dtb_with_ucode)
  710. ucode_content = dtb_with_ucode[fdt_len:]
  711. ucode_pos = len(nodtb_data) + fdt_len
  712. fname = tools.GetOutputFilename('test.dtb')
  713. with open(fname, 'wb') as fd:
  714. fd.write(dtb_with_ucode)
  715. dtb = fdt.FdtScan(fname)
  716. ucode = dtb.GetNode('/microcode')
  717. self.assertTrue(ucode)
  718. for node in ucode.subnodes:
  719. self.assertFalse(node.props.get('data'))
  720. # Check that the microcode appears immediately after the Fdt
  721. # This matches the concatenation of the data properties in
  722. # the /microcode/update@xxx nodes in 34_x86_ucode.dts.
  723. ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000,
  724. 0x78235609)
  725. self.assertEqual(ucode_data, ucode_content[:len(ucode_data)])
  726. # Check that the microcode pointer was inserted. It should match the
  727. # expected offset and size
  728. pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
  729. len(ucode_data))
  730. u_boot = data[:len(nodtb_data)]
  731. return u_boot, pos_and_size
  732. def testPackUbootMicrocode(self):
  733. """Test that x86 microcode can be handled correctly
  734. We expect to see the following in the image, in order:
  735. u-boot-nodtb.bin with a microcode pointer inserted at the correct
  736. place
  737. u-boot.dtb with the microcode removed
  738. the microcode
  739. """
  740. first, pos_and_size = self._RunMicrocodeTest('34_x86_ucode.dts',
  741. U_BOOT_NODTB_DATA)
  742. self.assertEqual('nodtb with microcode' + pos_and_size +
  743. ' somewhere in here', first)
  744. def _RunPackUbootSingleMicrocode(self):
  745. """Test that x86 microcode can be handled correctly
  746. We expect to see the following in the image, in order:
  747. u-boot-nodtb.bin with a microcode pointer inserted at the correct
  748. place
  749. u-boot.dtb with the microcode
  750. an empty microcode region
  751. """
  752. # We need the libfdt library to run this test since only that allows
  753. # finding the offset of a property. This is required by
  754. # Entry_u_boot_dtb_with_ucode.ObtainContents().
  755. data = self._DoReadFile('35_x86_single_ucode.dts', True)
  756. second = data[len(U_BOOT_NODTB_DATA):]
  757. fdt_len = self.GetFdtLen(second)
  758. third = second[fdt_len:]
  759. second = second[:fdt_len]
  760. ucode_data = struct.pack('>2L', 0x12345678, 0x12345679)
  761. self.assertIn(ucode_data, second)
  762. ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA)
  763. # Check that the microcode pointer was inserted. It should match the
  764. # expected offset and size
  765. pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
  766. len(ucode_data))
  767. first = data[:len(U_BOOT_NODTB_DATA)]
  768. self.assertEqual('nodtb with microcode' + pos_and_size +
  769. ' somewhere in here', first)
  770. def testPackUbootSingleMicrocode(self):
  771. """Test that x86 microcode can be handled correctly with fdt_normal.
  772. """
  773. self._RunPackUbootSingleMicrocode()
  774. def testUBootImg(self):
  775. """Test that u-boot.img can be put in a file"""
  776. data = self._DoReadFile('36_u_boot_img.dts')
  777. self.assertEqual(U_BOOT_IMG_DATA, data)
  778. def testNoMicrocode(self):
  779. """Test that a missing microcode region is detected"""
  780. with self.assertRaises(ValueError) as e:
  781. self._DoReadFile('37_x86_no_ucode.dts', True)
  782. self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode "
  783. "node found in ", str(e.exception))
  784. def testMicrocodeWithoutNode(self):
  785. """Test that a missing u-boot-dtb-with-ucode node is detected"""
  786. with self.assertRaises(ValueError) as e:
  787. self._DoReadFile('38_x86_ucode_missing_node.dts', True)
  788. self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
  789. "microcode region u-boot-dtb-with-ucode", str(e.exception))
  790. def testMicrocodeWithoutNode2(self):
  791. """Test that a missing u-boot-ucode node is detected"""
  792. with self.assertRaises(ValueError) as e:
  793. self._DoReadFile('39_x86_ucode_missing_node2.dts', True)
  794. self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
  795. "microcode region u-boot-ucode", str(e.exception))
  796. def testMicrocodeWithoutPtrInElf(self):
  797. """Test that a U-Boot binary without the microcode symbol is detected"""
  798. # ELF file without a '_dt_ucode_base_size' symbol
  799. try:
  800. with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
  801. TestFunctional._MakeInputFile('u-boot', fd.read())
  802. with self.assertRaises(ValueError) as e:
  803. self._RunPackUbootSingleMicrocode()
  804. self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate "
  805. "_dt_ucode_base_size symbol in u-boot", str(e.exception))
  806. finally:
  807. # Put the original file back
  808. with open(self.TestFile('u_boot_ucode_ptr')) as fd:
  809. TestFunctional._MakeInputFile('u-boot', fd.read())
  810. def testMicrocodeNotInImage(self):
  811. """Test that microcode must be placed within the image"""
  812. with self.assertRaises(ValueError) as e:
  813. self._DoReadFile('40_x86_ucode_not_in_image.dts', True)
  814. self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode "
  815. "pointer _dt_ucode_base_size at fffffe14 is outside the "
  816. "section ranging from 00000000 to 0000002e", str(e.exception))
  817. def testWithoutMicrocode(self):
  818. """Test that we can cope with an image without microcode (e.g. qemu)"""
  819. with open(self.TestFile('u_boot_no_ucode_ptr')) as fd:
  820. TestFunctional._MakeInputFile('u-boot', fd.read())
  821. data, dtb, _, _ = self._DoReadFileDtb('44_x86_optional_ucode.dts', True)
  822. # Now check the device tree has no microcode
  823. self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)])
  824. second = data[len(U_BOOT_NODTB_DATA):]
  825. fdt_len = self.GetFdtLen(second)
  826. self.assertEqual(dtb, second[:fdt_len])
  827. used_len = len(U_BOOT_NODTB_DATA) + fdt_len
  828. third = data[used_len:]
  829. self.assertEqual(chr(0) * (0x200 - used_len), third)
  830. def testUnknownPosSize(self):
  831. """Test that microcode must be placed within the image"""
  832. with self.assertRaises(ValueError) as e:
  833. self._DoReadFile('41_unknown_pos_size.dts', True)
  834. self.assertIn("Section '/binman': Unable to set offset/size for unknown "
  835. "entry 'invalid-entry'", str(e.exception))
  836. def testPackFsp(self):
  837. """Test that an image with a FSP binary can be created"""
  838. data = self._DoReadFile('42_intel-fsp.dts')
  839. self.assertEqual(FSP_DATA, data[:len(FSP_DATA)])
  840. def testPackCmc(self):
  841. """Test that an image with a CMC binary can be created"""
  842. data = self._DoReadFile('43_intel-cmc.dts')
  843. self.assertEqual(CMC_DATA, data[:len(CMC_DATA)])
  844. def testPackVbt(self):
  845. """Test that an image with a VBT binary can be created"""
  846. data = self._DoReadFile('46_intel-vbt.dts')
  847. self.assertEqual(VBT_DATA, data[:len(VBT_DATA)])
  848. def testSplBssPad(self):
  849. """Test that we can pad SPL's BSS with zeros"""
  850. # ELF file with a '__bss_size' symbol
  851. with open(self.TestFile('bss_data')) as fd:
  852. TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
  853. data = self._DoReadFile('47_spl_bss_pad.dts')
  854. self.assertEqual(U_BOOT_SPL_DATA + (chr(0) * 10) + U_BOOT_DATA, data)
  855. with open(self.TestFile('u_boot_ucode_ptr')) as fd:
  856. TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
  857. with self.assertRaises(ValueError) as e:
  858. data = self._DoReadFile('47_spl_bss_pad.dts')
  859. self.assertIn('Expected __bss_size symbol in spl/u-boot-spl',
  860. str(e.exception))
  861. def testPackStart16Spl(self):
  862. """Test that an image with an x86 start16 SPL region can be created"""
  863. data = self._DoReadFile('48_x86-start16-spl.dts')
  864. self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)])
  865. def _PackUbootSplMicrocode(self, dts, ucode_second=False):
  866. """Helper function for microcode tests
  867. We expect to see the following in the image, in order:
  868. u-boot-spl-nodtb.bin with a microcode pointer inserted at the
  869. correct place
  870. u-boot.dtb with the microcode removed
  871. the microcode
  872. Args:
  873. dts: Device tree file to use for test
  874. ucode_second: True if the microsecond entry is second instead of
  875. third
  876. """
  877. # ELF file with a '_dt_ucode_base_size' symbol
  878. with open(self.TestFile('u_boot_ucode_ptr')) as fd:
  879. TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
  880. first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA,
  881. ucode_second=ucode_second)
  882. self.assertEqual('splnodtb with microc' + pos_and_size +
  883. 'ter somewhere in here', first)
  884. def testPackUbootSplMicrocode(self):
  885. """Test that x86 microcode can be handled correctly in SPL"""
  886. self._PackUbootSplMicrocode('49_x86_ucode_spl.dts')
  887. def testPackUbootSplMicrocodeReorder(self):
  888. """Test that order doesn't matter for microcode entries
  889. This is the same as testPackUbootSplMicrocode but when we process the
  890. u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode
  891. entry, so we reply on binman to try later.
  892. """
  893. self._PackUbootSplMicrocode('58_x86_ucode_spl_needs_retry.dts',
  894. ucode_second=True)
  895. def testPackMrc(self):
  896. """Test that an image with an MRC binary can be created"""
  897. data = self._DoReadFile('50_intel_mrc.dts')
  898. self.assertEqual(MRC_DATA, data[:len(MRC_DATA)])
  899. def testSplDtb(self):
  900. """Test that an image with spl/u-boot-spl.dtb can be created"""
  901. data = self._DoReadFile('51_u_boot_spl_dtb.dts')
  902. self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)])
  903. def testSplNoDtb(self):
  904. """Test that an image with spl/u-boot-spl-nodtb.bin can be created"""
  905. data = self._DoReadFile('52_u_boot_spl_nodtb.dts')
  906. self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)])
  907. def testSymbols(self):
  908. """Test binman can assign symbols embedded in U-Boot"""
  909. elf_fname = self.TestFile('u_boot_binman_syms')
  910. syms = elf.GetSymbols(elf_fname, ['binman', 'image'])
  911. addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start')
  912. self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr)
  913. with open(self.TestFile('u_boot_binman_syms')) as fd:
  914. TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read())
  915. data = self._DoReadFile('53_symbols.dts')
  916. sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20)
  917. expected = (sym_values + U_BOOT_SPL_DATA[16:] + chr(0xff) +
  918. U_BOOT_DATA +
  919. sym_values + U_BOOT_SPL_DATA[16:])
  920. self.assertEqual(expected, data)
  921. def testPackUnitAddress(self):
  922. """Test that we support multiple binaries with the same name"""
  923. data = self._DoReadFile('54_unit_address.dts')
  924. self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data)
  925. def testSections(self):
  926. """Basic test of sections"""
  927. data = self._DoReadFile('55_sections.dts')
  928. expected = (U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12 +
  929. U_BOOT_DATA + '&' * 4)
  930. self.assertEqual(expected, data)
  931. def testMap(self):
  932. """Tests outputting a map of the images"""
  933. _, _, map_data, _ = self._DoReadFileDtb('55_sections.dts', map=True)
  934. self.assertEqual('''ImagePos Offset Size Name
  935. 00000000 00000000 00000028 main-section
  936. 00000000 00000000 00000010 section@0
  937. 00000000 00000000 00000004 u-boot
  938. 00000010 00000010 00000010 section@1
  939. 00000010 00000000 00000004 u-boot
  940. 00000020 00000020 00000004 section@2
  941. 00000020 00000000 00000004 u-boot
  942. ''', map_data)
  943. def testNamePrefix(self):
  944. """Tests that name prefixes are used"""
  945. _, _, map_data, _ = self._DoReadFileDtb('56_name_prefix.dts', map=True)
  946. self.assertEqual('''ImagePos Offset Size Name
  947. 00000000 00000000 00000028 main-section
  948. 00000000 00000000 00000010 section@0
  949. 00000000 00000000 00000004 ro-u-boot
  950. 00000010 00000010 00000010 section@1
  951. 00000010 00000000 00000004 rw-u-boot
  952. ''', map_data)
  953. def testUnknownContents(self):
  954. """Test that obtaining the contents works as expected"""
  955. with self.assertRaises(ValueError) as e:
  956. self._DoReadFile('57_unknown_contents.dts', True)
  957. self.assertIn("Section '/binman': Internal error: Could not complete "
  958. "processing of contents: remaining [<_testing.Entry__testing ",
  959. str(e.exception))
  960. def testBadChangeSize(self):
  961. """Test that trying to change the size of an entry fails"""
  962. with self.assertRaises(ValueError) as e:
  963. self._DoReadFile('59_change_size.dts', True)
  964. self.assertIn("Node '/binman/_testing': Cannot update entry size from "
  965. '2 to 1', str(e.exception))
  966. def testUpdateFdt(self):
  967. """Test that we can update the device tree with offset/size info"""
  968. _, _, _, out_dtb_fname = self._DoReadFileDtb('60_fdt_update.dts',
  969. update_dtb=True)
  970. dtb = fdt.Fdt(out_dtb_fname)
  971. dtb.Scan()
  972. props = self._GetPropTree(dtb, ['offset', 'size', 'image-pos'])
  973. self.assertEqual({
  974. 'image-pos': 0,
  975. 'offset': 0,
  976. '_testing:offset': 32,
  977. '_testing:size': 1,
  978. '_testing:image-pos': 32,
  979. 'section@0/u-boot:offset': 0,
  980. 'section@0/u-boot:size': len(U_BOOT_DATA),
  981. 'section@0/u-boot:image-pos': 0,
  982. 'section@0:offset': 0,
  983. 'section@0:size': 16,
  984. 'section@0:image-pos': 0,
  985. 'section@1/u-boot:offset': 0,
  986. 'section@1/u-boot:size': len(U_BOOT_DATA),
  987. 'section@1/u-boot:image-pos': 16,
  988. 'section@1:offset': 16,
  989. 'section@1:size': 16,
  990. 'section@1:image-pos': 16,
  991. 'size': 40
  992. }, props)
  993. def testUpdateFdtBad(self):
  994. """Test that we detect when ProcessFdt never completes"""
  995. with self.assertRaises(ValueError) as e:
  996. self._DoReadFileDtb('61_fdt_update_bad.dts', update_dtb=True)
  997. self.assertIn('Could not complete processing of Fdt: remaining '
  998. '[<_testing.Entry__testing', str(e.exception))
  999. def testEntryArgs(self):
  1000. """Test passing arguments to entries from the command line"""
  1001. entry_args = {
  1002. 'test-str-arg': 'test1',
  1003. 'test-int-arg': '456',
  1004. }
  1005. self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args)
  1006. self.assertIn('image', control.images)
  1007. entry = control.images['image'].GetEntries()['_testing']
  1008. self.assertEqual('test0', entry.test_str_fdt)
  1009. self.assertEqual('test1', entry.test_str_arg)
  1010. self.assertEqual(123, entry.test_int_fdt)
  1011. self.assertEqual(456, entry.test_int_arg)
  1012. def testEntryArgsMissing(self):
  1013. """Test missing arguments and properties"""
  1014. entry_args = {
  1015. 'test-int-arg': '456',
  1016. }
  1017. self._DoReadFileDtb('63_entry_args_missing.dts', entry_args=entry_args)
  1018. entry = control.images['image'].GetEntries()['_testing']
  1019. self.assertEqual('test0', entry.test_str_fdt)
  1020. self.assertEqual(None, entry.test_str_arg)
  1021. self.assertEqual(None, entry.test_int_fdt)
  1022. self.assertEqual(456, entry.test_int_arg)
  1023. def testEntryArgsRequired(self):
  1024. """Test missing arguments and properties"""
  1025. entry_args = {
  1026. 'test-int-arg': '456',
  1027. }
  1028. with self.assertRaises(ValueError) as e:
  1029. self._DoReadFileDtb('64_entry_args_required.dts')
  1030. self.assertIn("Node '/binman/_testing': Missing required "
  1031. 'properties/entry args: test-str-arg, test-int-fdt, test-int-arg',
  1032. str(e.exception))
  1033. def testEntryArgsInvalidFormat(self):
  1034. """Test that an invalid entry-argument format is detected"""
  1035. args = ['-d', self.TestFile('64_entry_args_required.dts'), '-ano-value']
  1036. with self.assertRaises(ValueError) as e:
  1037. self._DoBinman(*args)
  1038. self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception))
  1039. def testEntryArgsInvalidInteger(self):
  1040. """Test that an invalid entry-argument integer is detected"""
  1041. entry_args = {
  1042. 'test-int-arg': 'abc',
  1043. }
  1044. with self.assertRaises(ValueError) as e:
  1045. self._DoReadFileDtb('62_entry_args.dts', entry_args=entry_args)
  1046. self.assertIn("Node '/binman/_testing': Cannot convert entry arg "
  1047. "'test-int-arg' (value 'abc') to integer",
  1048. str(e.exception))
  1049. def testEntryArgsInvalidDatatype(self):
  1050. """Test that an invalid entry-argument datatype is detected
  1051. This test could be written in entry_test.py except that it needs
  1052. access to control.entry_args, which seems more than that module should
  1053. be able to see.
  1054. """
  1055. entry_args = {
  1056. 'test-bad-datatype-arg': '12',
  1057. }
  1058. with self.assertRaises(ValueError) as e:
  1059. self._DoReadFileDtb('65_entry_args_unknown_datatype.dts',
  1060. entry_args=entry_args)
  1061. self.assertIn('GetArg() internal error: Unknown data type ',
  1062. str(e.exception))
  1063. def testText(self):
  1064. """Test for a text entry type"""
  1065. entry_args = {
  1066. 'test-id': TEXT_DATA,
  1067. 'test-id2': TEXT_DATA2,
  1068. 'test-id3': TEXT_DATA3,
  1069. }
  1070. data, _, _, _ = self._DoReadFileDtb('66_text.dts',
  1071. entry_args=entry_args)
  1072. expected = (TEXT_DATA + chr(0) * (8 - len(TEXT_DATA)) + TEXT_DATA2 +
  1073. TEXT_DATA3 + 'some text')
  1074. self.assertEqual(expected, data)
  1075. def testEntryDocs(self):
  1076. """Test for creation of entry documentation"""
  1077. with test_util.capture_sys_output() as (stdout, stderr):
  1078. control.WriteEntryDocs(binman.GetEntryModules())
  1079. self.assertTrue(len(stdout.getvalue()) > 0)
  1080. def testEntryDocsMissing(self):
  1081. """Test handling of missing entry documentation"""
  1082. with self.assertRaises(ValueError) as e:
  1083. with test_util.capture_sys_output() as (stdout, stderr):
  1084. control.WriteEntryDocs(binman.GetEntryModules(), 'u_boot')
  1085. self.assertIn('Documentation is missing for modules: u_boot',
  1086. str(e.exception))
  1087. def testFmap(self):
  1088. """Basic test of generation of a flashrom fmap"""
  1089. data = self._DoReadFile('67_fmap.dts')
  1090. fhdr, fentries = fmap_util.DecodeFmap(data[32:])
  1091. expected = U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12
  1092. self.assertEqual(expected, data[:32])
  1093. self.assertEqual('__FMAP__', fhdr.signature)
  1094. self.assertEqual(1, fhdr.ver_major)
  1095. self.assertEqual(0, fhdr.ver_minor)
  1096. self.assertEqual(0, fhdr.base)
  1097. self.assertEqual(16 + 16 +
  1098. fmap_util.FMAP_HEADER_LEN +
  1099. fmap_util.FMAP_AREA_LEN * 3, fhdr.image_size)
  1100. self.assertEqual('FMAP', fhdr.name)
  1101. self.assertEqual(3, fhdr.nareas)
  1102. for fentry in fentries:
  1103. self.assertEqual(0, fentry.flags)
  1104. self.assertEqual(0, fentries[0].offset)
  1105. self.assertEqual(4, fentries[0].size)
  1106. self.assertEqual('RO_U_BOOT', fentries[0].name)
  1107. self.assertEqual(16, fentries[1].offset)
  1108. self.assertEqual(4, fentries[1].size)
  1109. self.assertEqual('RW_U_BOOT', fentries[1].name)
  1110. self.assertEqual(32, fentries[2].offset)
  1111. self.assertEqual(fmap_util.FMAP_HEADER_LEN +
  1112. fmap_util.FMAP_AREA_LEN * 3, fentries[2].size)
  1113. self.assertEqual('FMAP', fentries[2].name)
  1114. def testBlobNamedByArg(self):
  1115. """Test we can add a blob with the filename coming from an entry arg"""
  1116. entry_args = {
  1117. 'cros-ec-rw-path': 'ecrw.bin',
  1118. }
  1119. data, _, _, _ = self._DoReadFileDtb('68_blob_named_by_arg.dts',
  1120. entry_args=entry_args)
  1121. def testFill(self):
  1122. """Test for an fill entry type"""
  1123. data = self._DoReadFile('69_fill.dts')
  1124. expected = 8 * chr(0xff) + 8 * chr(0)
  1125. self.assertEqual(expected, data)
  1126. def testFillNoSize(self):
  1127. """Test for an fill entry type with no size"""
  1128. with self.assertRaises(ValueError) as e:
  1129. self._DoReadFile('70_fill_no_size.dts')
  1130. self.assertIn("'fill' entry must have a size property",
  1131. str(e.exception))
  1132. def _HandleGbbCommand(self, pipe_list):
  1133. """Fake calls to the futility utility"""
  1134. if pipe_list[0][0] == 'futility':
  1135. fname = pipe_list[0][-1]
  1136. # Append our GBB data to the file, which will happen every time the
  1137. # futility command is called.
  1138. with open(fname, 'a') as fd:
  1139. fd.write(GBB_DATA)
  1140. return command.CommandResult()
  1141. def testGbb(self):
  1142. """Test for the Chromium OS Google Binary Block"""
  1143. command.test_result = self._HandleGbbCommand
  1144. entry_args = {
  1145. 'keydir': 'devkeys',
  1146. 'bmpblk': 'bmpblk.bin',
  1147. }
  1148. data, _, _, _ = self._DoReadFileDtb('71_gbb.dts', entry_args=entry_args)
  1149. # Since futility
  1150. expected = GBB_DATA + GBB_DATA + 8 * chr(0) + (0x2180 - 16) * chr(0)
  1151. self.assertEqual(expected, data)
  1152. def testGbbTooSmall(self):
  1153. """Test for the Chromium OS Google Binary Block being large enough"""
  1154. with self.assertRaises(ValueError) as e:
  1155. self._DoReadFileDtb('72_gbb_too_small.dts')
  1156. self.assertIn("Node '/binman/gbb': GBB is too small",
  1157. str(e.exception))
  1158. def testGbbNoSize(self):
  1159. """Test for the Chromium OS Google Binary Block having a size"""
  1160. with self.assertRaises(ValueError) as e:
  1161. self._DoReadFileDtb('73_gbb_no_size.dts')
  1162. self.assertIn("Node '/binman/gbb': GBB must have a fixed size",
  1163. str(e.exception))
  1164. def _HandleVblockCommand(self, pipe_list):
  1165. """Fake calls to the futility utility"""
  1166. if pipe_list[0][0] == 'futility':
  1167. fname = pipe_list[0][3]
  1168. with open(fname, 'wb') as fd:
  1169. fd.write(VBLOCK_DATA)
  1170. return command.CommandResult()
  1171. def testVblock(self):
  1172. """Test for the Chromium OS Verified Boot Block"""
  1173. command.test_result = self._HandleVblockCommand
  1174. entry_args = {
  1175. 'keydir': 'devkeys',
  1176. }
  1177. data, _, _, _ = self._DoReadFileDtb('74_vblock.dts',
  1178. entry_args=entry_args)
  1179. expected = U_BOOT_DATA + VBLOCK_DATA + U_BOOT_DTB_DATA
  1180. self.assertEqual(expected, data)
  1181. def testVblockNoContent(self):
  1182. """Test we detect a vblock which has no content to sign"""
  1183. with self.assertRaises(ValueError) as e:
  1184. self._DoReadFile('75_vblock_no_content.dts')
  1185. self.assertIn("Node '/binman/vblock': Vblock must have a 'content' "
  1186. 'property', str(e.exception))
  1187. def testVblockBadPhandle(self):
  1188. """Test that we detect a vblock with an invalid phandle in contents"""
  1189. with self.assertRaises(ValueError) as e:
  1190. self._DoReadFile('76_vblock_bad_phandle.dts')
  1191. self.assertIn("Node '/binman/vblock': Cannot find node for phandle "
  1192. '1000', str(e.exception))
  1193. def testVblockBadEntry(self):
  1194. """Test that we detect an entry that points to a non-entry"""
  1195. with self.assertRaises(ValueError) as e:
  1196. self._DoReadFile('77_vblock_bad_entry.dts')
  1197. self.assertIn("Node '/binman/vblock': Cannot find entry for node "
  1198. "'other'", str(e.exception))
  1199. def testTpl(self):
  1200. """Test that an image with TPL and ots device tree can be created"""
  1201. # ELF file with a '__bss_size' symbol
  1202. with open(self.TestFile('bss_data')) as fd:
  1203. TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read())
  1204. data = self._DoReadFile('78_u_boot_tpl.dts')
  1205. self.assertEqual(U_BOOT_TPL_DATA + U_BOOT_TPL_DTB_DATA, data)
  1206. def testUsesPos(self):
  1207. """Test that the 'pos' property cannot be used anymore"""
  1208. with self.assertRaises(ValueError) as e:
  1209. data = self._DoReadFile('79_uses_pos.dts')
  1210. self.assertIn("Node '/binman/u-boot': Please use 'offset' instead of "
  1211. "'pos'", str(e.exception))
  1212. def testFillZero(self):
  1213. """Test for an fill entry type with a size of 0"""
  1214. data = self._DoReadFile('80_fill_empty.dts')
  1215. self.assertEqual(chr(0) * 16, data)
  1216. def testTextMissing(self):
  1217. """Test for a text entry type where there is no text"""
  1218. with self.assertRaises(ValueError) as e:
  1219. self._DoReadFileDtb('66_text.dts',)
  1220. self.assertIn("Node '/binman/text': No value provided for text label "
  1221. "'test-id'", str(e.exception))
  1222. def testPackStart16Tpl(self):
  1223. """Test that an image with an x86 start16 TPL region can be created"""
  1224. data = self._DoReadFile('81_x86-start16-tpl.dts')
  1225. self.assertEqual(X86_START16_TPL_DATA, data[:len(X86_START16_TPL_DATA)])
  1226. def testSelectImage(self):
  1227. """Test that we can select which images to build"""
  1228. with test_util.capture_sys_output() as (stdout, stderr):
  1229. retcode = self._DoTestFile('06_dual_image.dts', images=['image2'])
  1230. self.assertEqual(0, retcode)
  1231. self.assertIn('Skipping images: image1', stdout.getvalue())
  1232. self.assertFalse(os.path.exists(tools.GetOutputFilename('image1.bin')))
  1233. self.assertTrue(os.path.exists(tools.GetOutputFilename('image2.bin')))
  1234. def testUpdateFdtAll(self):
  1235. """Test that all device trees are updated with offset/size info"""
  1236. data, _, _, _ = self._DoReadFileDtb('82_fdt_update_all.dts',
  1237. use_real_dtb=True, update_dtb=True)
  1238. base_expected = {
  1239. 'section:image-pos': 0,
  1240. 'u-boot-tpl-dtb:size': 513,
  1241. 'u-boot-spl-dtb:size': 513,
  1242. 'u-boot-spl-dtb:offset': 493,
  1243. 'image-pos': 0,
  1244. 'section/u-boot-dtb:image-pos': 0,
  1245. 'u-boot-spl-dtb:image-pos': 493,
  1246. 'section/u-boot-dtb:size': 493,
  1247. 'u-boot-tpl-dtb:image-pos': 1006,
  1248. 'section/u-boot-dtb:offset': 0,
  1249. 'section:size': 493,
  1250. 'offset': 0,
  1251. 'section:offset': 0,
  1252. 'u-boot-tpl-dtb:offset': 1006,
  1253. 'size': 1519
  1254. }
  1255. # We expect three device-tree files in the output, one after the other.
  1256. # Read them in sequence. We look for an 'spl' property in the SPL tree,
  1257. # and 'tpl' in the TPL tree, to make sure they are distinct from the
  1258. # main U-Boot tree. All three should have the same postions and offset.
  1259. start = 0
  1260. for item in ['', 'spl', 'tpl']:
  1261. dtb = fdt.Fdt.FromData(data[start:])
  1262. dtb.Scan()
  1263. props = self._GetPropTree(dtb, ['offset', 'size', 'image-pos',
  1264. 'spl', 'tpl'])
  1265. expected = dict(base_expected)
  1266. if item:
  1267. expected[item] = 0
  1268. self.assertEqual(expected, props)
  1269. start += dtb._fdt_obj.totalsize()
  1270. def testUpdateFdtOutput(self):
  1271. """Test that output DTB files are updated"""
  1272. try:
  1273. data, dtb_data, _, _ = self._DoReadFileDtb('82_fdt_update_all.dts',
  1274. use_real_dtb=True, update_dtb=True, reset_dtbs=False)
  1275. # Unfortunately, compiling a source file always results in a file
  1276. # called source.dtb (see fdt_util.EnsureCompiled()). The test
  1277. # source file (e.g. test/75_fdt_update_all.dts) thus does not enter
  1278. # binman as a file called u-boot.dtb. To fix this, copy the file
  1279. # over to the expected place.
  1280. #tools.WriteFile(os.path.join(self._indir, 'u-boot.dtb'),
  1281. #tools.ReadFile(tools.GetOutputFilename('source.dtb')))
  1282. start = 0
  1283. for fname in ['u-boot.dtb.out', 'spl/u-boot-spl.dtb.out',
  1284. 'tpl/u-boot-tpl.dtb.out']:
  1285. dtb = fdt.Fdt.FromData(data[start:])
  1286. size = dtb._fdt_obj.totalsize()
  1287. pathname = tools.GetOutputFilename(os.path.split(fname)[1])
  1288. outdata = tools.ReadFile(pathname)
  1289. name = os.path.split(fname)[0]
  1290. if name:
  1291. orig_indata = self._GetDtbContentsForSplTpl(dtb_data, name)
  1292. else:
  1293. orig_indata = dtb_data
  1294. self.assertNotEqual(outdata, orig_indata,
  1295. "Expected output file '%s' be updated" % pathname)
  1296. self.assertEqual(outdata, data[start:start + size],
  1297. "Expected output file '%s' to match output image" %
  1298. pathname)
  1299. start += size
  1300. finally:
  1301. self._ResetDtbs()
  1302. def _decompress(self, data):
  1303. out = os.path.join(self._indir, 'lz4.tmp')
  1304. with open(out, 'wb') as fd:
  1305. fd.write(data)
  1306. return tools.Run('lz4', '-dc', out)
  1307. '''
  1308. try:
  1309. orig = lz4.frame.decompress(data)
  1310. except AttributeError:
  1311. orig = lz4.decompress(data)
  1312. '''
  1313. def testCompress(self):
  1314. """Test compression of blobs"""
  1315. data, _, _, out_dtb_fname = self._DoReadFileDtb('83_compress.dts',
  1316. use_real_dtb=True, update_dtb=True)
  1317. dtb = fdt.Fdt(out_dtb_fname)
  1318. dtb.Scan()
  1319. props = self._GetPropTree(dtb, ['size', 'uncomp-size'])
  1320. orig = self._decompress(data)
  1321. self.assertEquals(COMPRESS_DATA, orig)
  1322. expected = {
  1323. 'blob:uncomp-size': len(COMPRESS_DATA),
  1324. 'blob:size': len(data),
  1325. 'size': len(data),
  1326. }
  1327. self.assertEqual(expected, props)
  1328. def testFiles(self):
  1329. """Test bringing in multiple files"""
  1330. data = self._DoReadFile('84_files.dts')
  1331. self.assertEqual(FILES_DATA, data)
  1332. def testFilesCompress(self):
  1333. """Test bringing in multiple files and compressing them"""
  1334. data = self._DoReadFile('85_files_compress.dts')
  1335. image = control.images['image']
  1336. entries = image.GetEntries()
  1337. files = entries['files']
  1338. entries = files._section._entries
  1339. orig = ''
  1340. for i in range(1, 3):
  1341. key = '%d.dat' % i
  1342. start = entries[key].image_pos
  1343. len = entries[key].size
  1344. chunk = data[start:start + len]
  1345. orig += self._decompress(chunk)
  1346. self.assertEqual(FILES_DATA, orig)
  1347. def testFilesMissing(self):
  1348. """Test missing files"""
  1349. with self.assertRaises(ValueError) as e:
  1350. data = self._DoReadFile('86_files_none.dts')
  1351. self.assertIn("Node '/binman/files': Pattern \'files/*.none\' matched "
  1352. 'no files', str(e.exception))
  1353. def testFilesNoPattern(self):
  1354. """Test missing files"""
  1355. with self.assertRaises(ValueError) as e:
  1356. data = self._DoReadFile('87_files_no_pattern.dts')
  1357. self.assertIn("Node '/binman/files': Missing 'pattern' property",
  1358. str(e.exception))
  1359. def testExpandSize(self):
  1360. """Test an expanding entry"""
  1361. data, _, map_data, _ = self._DoReadFileDtb('88_expand_size.dts',
  1362. map=True)
  1363. expect = ('a' * 8 + U_BOOT_DATA +
  1364. MRC_DATA + 'b' * 1 + U_BOOT_DATA +
  1365. 'c' * 8 + U_BOOT_DATA +
  1366. 'd' * 8)
  1367. self.assertEqual(expect, data)
  1368. self.assertEqual('''ImagePos Offset Size Name
  1369. 00000000 00000000 00000028 main-section
  1370. 00000000 00000000 00000008 fill
  1371. 00000008 00000008 00000004 u-boot
  1372. 0000000c 0000000c 00000004 section
  1373. 0000000c 00000000 00000003 intel-mrc
  1374. 00000010 00000010 00000004 u-boot2
  1375. 00000014 00000014 0000000c section2
  1376. 00000014 00000000 00000008 fill
  1377. 0000001c 00000008 00000004 u-boot
  1378. 00000020 00000020 00000008 fill2
  1379. ''', map_data)
  1380. def testExpandSizeBad(self):
  1381. """Test an expanding entry which fails to provide contents"""
  1382. with self.assertRaises(ValueError) as e:
  1383. self._DoReadFileDtb('89_expand_size_bad.dts', map=True)
  1384. self.assertIn("Node '/binman/_testing': Cannot obtain contents when "
  1385. 'expanding entry', str(e.exception))
  1386. if __name__ == "__main__":
  1387. unittest.main()