test_dtoc.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0+
  3. # Copyright (c) 2012 The Chromium OS Authors.
  4. #
  5. """Tests for the dtb_platdata module
  6. This includes unit tests for some functions and functional tests for the dtoc
  7. tool.
  8. """
  9. import collections
  10. import copy
  11. import glob
  12. import os
  13. import struct
  14. import unittest
  15. from dtb_platdata import get_value
  16. from dtb_platdata import tab_to
  17. from dtoc import dtb_platdata
  18. from dtoc import fdt
  19. from dtoc import fdt_util
  20. from dtoc import src_scan
  21. from dtoc.src_scan import conv_name_to_c
  22. from dtoc.src_scan import get_compat_name
  23. from patman import test_util
  24. from patman import tools
  25. OUR_PATH = os.path.dirname(os.path.realpath(__file__))
  26. HEADER = '''/*
  27. * DO NOT MODIFY
  28. *
  29. * Defines the structs used to hold devicetree data.
  30. * This was generated by dtoc from a .dtb (device tree binary) file.
  31. */
  32. #include <stdbool.h>
  33. #include <linux/libfdt.h>'''
  34. C_HEADER = '''/*
  35. * DO NOT MODIFY
  36. *
  37. * Declares the U_BOOT_DRIVER() records and platform data.
  38. * This was generated by dtoc from a .dtb (device tree binary) file.
  39. */
  40. /* Allow use of U_BOOT_DRVINFO() in this file */
  41. #define DT_PLAT_C
  42. #include <common.h>
  43. #include <dm.h>
  44. #include <dt-structs.h>
  45. '''
  46. # Scanner saved from a previous run of the tests (to speed things up)
  47. saved_scan = None
  48. # This is a test so is allowed to access private things in the module it is
  49. # testing
  50. # pylint: disable=W0212
  51. def get_dtb_file(dts_fname, capture_stderr=False):
  52. """Compile a .dts file to a .dtb
  53. Args:
  54. dts_fname (str): Filename of .dts file in the current directory
  55. capture_stderr (bool): True to capture and discard stderr output
  56. Returns:
  57. str: Filename of compiled file in output directory
  58. """
  59. return fdt_util.EnsureCompiled(os.path.join(OUR_PATH, 'test', dts_fname),
  60. capture_stderr=capture_stderr)
  61. def setup():
  62. global saved_scan
  63. # Disable warnings so that calls to get_normalized_compat_name() will not
  64. # output things.
  65. saved_scan = src_scan.Scanner(None, True, False)
  66. saved_scan.scan_drivers()
  67. def copy_scan():
  68. """Get a copy of saved_scan so that each test can start clean"""
  69. return copy.deepcopy(saved_scan)
  70. class TestDtoc(unittest.TestCase):
  71. """Tests for dtoc"""
  72. @classmethod
  73. def setUpClass(cls):
  74. tools.PrepareOutputDir(None)
  75. cls.maxDiff = None
  76. @classmethod
  77. def tearDownClass(cls):
  78. tools.FinaliseOutputDir()
  79. @staticmethod
  80. def _write_python_string(fname, data):
  81. """Write a string with tabs expanded as done in this Python file
  82. Args:
  83. fname (str): Filename to write to
  84. data (str): Raw string to convert
  85. """
  86. data = data.replace('\t', '\\t')
  87. with open(fname, 'w') as fout:
  88. fout.write(data)
  89. def _check_strings(self, expected, actual):
  90. """Check that a string matches its expected value
  91. If the strings do not match, they are written to the /tmp directory in
  92. the same Python format as is used here in the test. This allows for
  93. easy comparison and update of the tests.
  94. Args:
  95. expected (str): Expected string
  96. actual (str): Actual string
  97. """
  98. if expected != actual:
  99. self._write_python_string('/tmp/binman.expected', expected)
  100. self._write_python_string('/tmp/binman.actual', actual)
  101. print('Failures written to /tmp/binman.{expected,actual}')
  102. self.assertEqual(expected, actual)
  103. @staticmethod
  104. def run_test(args, dtb_file, output):
  105. """Run a test using dtoc
  106. Args:
  107. args (list of str): List of arguments for dtoc
  108. dtb_file (str): Filename of .dtb file
  109. output (str): Filename of output file
  110. """
  111. dtb_platdata.run_steps(args, dtb_file, False, output, [], True,
  112. None, None, scan=copy_scan())
  113. def test_name(self):
  114. """Test conversion of device tree names to C identifiers"""
  115. self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
  116. self.assertEqual('vendor_clock_frequency',
  117. conv_name_to_c('vendor,clock-frequency'))
  118. self.assertEqual('rockchip_rk3399_sdhci_5_1',
  119. conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
  120. def test_tab_to(self):
  121. """Test operation of tab_to() function"""
  122. self.assertEqual('fred ', tab_to(0, 'fred'))
  123. self.assertEqual('fred\t', tab_to(1, 'fred'))
  124. self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
  125. self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
  126. self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
  127. self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
  128. def test_get_value(self):
  129. """Test operation of get_value() function"""
  130. self.assertEqual('0x45',
  131. get_value(fdt.Type.INT, struct.pack('>I', 0x45)))
  132. self.assertEqual('0x45',
  133. get_value(fdt.Type.BYTE, struct.pack('<I', 0x45)))
  134. self.assertEqual('0x0',
  135. get_value(fdt.Type.BYTE, struct.pack('>I', 0x45)))
  136. self.assertEqual('"test"', get_value(fdt.Type.STRING, 'test'))
  137. self.assertEqual('true', get_value(fdt.Type.BOOL, None))
  138. def test_get_compat_name(self):
  139. """Test operation of get_compat_name() function"""
  140. Prop = collections.namedtuple('Prop', ['value'])
  141. Node = collections.namedtuple('Node', ['props'])
  142. prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
  143. node = Node({'compatible': prop})
  144. self.assertEqual((['rockchip_rk3399_sdhci_5_1', 'arasan_sdhci_5_1']),
  145. get_compat_name(node))
  146. prop = Prop(['rockchip,rk3399-sdhci-5.1'])
  147. node = Node({'compatible': prop})
  148. self.assertEqual((['rockchip_rk3399_sdhci_5_1']),
  149. get_compat_name(node))
  150. prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
  151. node = Node({'compatible': prop})
  152. self.assertEqual((['rockchip_rk3399_sdhci_5_1',
  153. 'arasan_sdhci_5_1', 'third']),
  154. get_compat_name(node))
  155. def test_empty_file(self):
  156. """Test output from a device tree file with no nodes"""
  157. dtb_file = get_dtb_file('dtoc_test_empty.dts')
  158. output = tools.GetOutputFilename('output')
  159. # Run this one without saved_scan to complete test coverage
  160. dtb_platdata.run_steps(['struct'], dtb_file, False, output, [], True)
  161. with open(output) as infile:
  162. lines = infile.read().splitlines()
  163. self.assertEqual(HEADER.splitlines(), lines)
  164. self.run_test(['platdata'], dtb_file, output)
  165. with open(output) as infile:
  166. lines = infile.read().splitlines()
  167. self.assertEqual(C_HEADER.splitlines() + [''], lines)
  168. struct_text = HEADER + '''
  169. struct dtd_sandbox_i2c_test {
  170. };
  171. struct dtd_sandbox_pmic_test {
  172. \tbool\t\tlow_power;
  173. \tfdt64_t\t\treg[2];
  174. };
  175. struct dtd_sandbox_spl_test {
  176. \tconst char * acpi_name;
  177. \tbool\t\tboolval;
  178. \tunsigned char\tbytearray[3];
  179. \tunsigned char\tbyteval;
  180. \tfdt32_t\t\tintarray[4];
  181. \tfdt32_t\t\tintval;
  182. \tunsigned char\tlongbytearray[9];
  183. \tunsigned char\tnotstring[5];
  184. \tconst char *\tstringarray[3];
  185. \tconst char *\tstringval;
  186. };
  187. '''
  188. platdata_text = C_HEADER + '''
  189. /* Node /i2c@0 index 0 */
  190. static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
  191. };
  192. U_BOOT_DRVINFO(i2c_at_0) = {
  193. \t.name\t\t= "sandbox_i2c_test",
  194. \t.plat\t= &dtv_i2c_at_0,
  195. \t.plat_size\t= sizeof(dtv_i2c_at_0),
  196. \t.parent_idx\t= -1,
  197. };
  198. /* Node /i2c@0/pmic@9 index 1 */
  199. static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
  200. \t.low_power\t\t= true,
  201. \t.reg\t\t\t= {0x9, 0x0},
  202. };
  203. U_BOOT_DRVINFO(pmic_at_9) = {
  204. \t.name\t\t= "sandbox_pmic_test",
  205. \t.plat\t= &dtv_pmic_at_9,
  206. \t.plat_size\t= sizeof(dtv_pmic_at_9),
  207. \t.parent_idx\t= 0,
  208. };
  209. /* Node /spl-test index 2 */
  210. static struct dtd_sandbox_spl_test dtv_spl_test = {
  211. \t.boolval\t\t= true,
  212. \t.bytearray\t\t= {0x6, 0x0, 0x0},
  213. \t.byteval\t\t= 0x5,
  214. \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
  215. \t.intval\t\t\t= 0x1,
  216. \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
  217. \t\t0x11},
  218. \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
  219. \t.stringarray\t\t= {"multi-word", "message", ""},
  220. \t.stringval\t\t= "message",
  221. };
  222. U_BOOT_DRVINFO(spl_test) = {
  223. \t.name\t\t= "sandbox_spl_test",
  224. \t.plat\t= &dtv_spl_test,
  225. \t.plat_size\t= sizeof(dtv_spl_test),
  226. \t.parent_idx\t= -1,
  227. };
  228. /* Node /spl-test2 index 3 */
  229. static struct dtd_sandbox_spl_test dtv_spl_test2 = {
  230. \t.acpi_name\t\t= "\\\\_SB.GPO0",
  231. \t.bytearray\t\t= {0x1, 0x23, 0x34},
  232. \t.byteval\t\t= 0x8,
  233. \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
  234. \t.intval\t\t\t= 0x3,
  235. \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
  236. \t\t0x0},
  237. \t.stringarray\t\t= {"another", "multi-word", "message"},
  238. \t.stringval\t\t= "message2",
  239. };
  240. U_BOOT_DRVINFO(spl_test2) = {
  241. \t.name\t\t= "sandbox_spl_test",
  242. \t.plat\t= &dtv_spl_test2,
  243. \t.plat_size\t= sizeof(dtv_spl_test2),
  244. \t.parent_idx\t= -1,
  245. };
  246. /* Node /spl-test3 index 4 */
  247. static struct dtd_sandbox_spl_test dtv_spl_test3 = {
  248. \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
  249. \t\t0x0},
  250. \t.stringarray\t\t= {"one", "", ""},
  251. };
  252. U_BOOT_DRVINFO(spl_test3) = {
  253. \t.name\t\t= "sandbox_spl_test",
  254. \t.plat\t= &dtv_spl_test3,
  255. \t.plat_size\t= sizeof(dtv_spl_test3),
  256. \t.parent_idx\t= -1,
  257. };
  258. '''
  259. def test_simple(self):
  260. """Test output from some simple nodes with various types of data"""
  261. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  262. output = tools.GetOutputFilename('output')
  263. self.run_test(['struct'], dtb_file, output)
  264. with open(output) as infile:
  265. data = infile.read()
  266. self._check_strings(self.struct_text, data)
  267. self.run_test(['platdata'], dtb_file, output)
  268. with open(output) as infile:
  269. data = infile.read()
  270. self._check_strings(self.platdata_text, data)
  271. # Try the 'all' command
  272. self.run_test(['all'], dtb_file, output)
  273. data = tools.ReadFile(output, binary=False)
  274. self._check_strings(self.platdata_text + self.struct_text, data)
  275. def test_driver_alias(self):
  276. """Test output from a device tree file with a driver alias"""
  277. dtb_file = get_dtb_file('dtoc_test_driver_alias.dts')
  278. output = tools.GetOutputFilename('output')
  279. self.run_test(['struct'], dtb_file, output)
  280. with open(output) as infile:
  281. data = infile.read()
  282. self._check_strings(HEADER + '''
  283. struct dtd_sandbox_gpio {
  284. \tconst char *\tgpio_bank_name;
  285. \tbool\t\tgpio_controller;
  286. \tfdt32_t\t\tsandbox_gpio_count;
  287. };
  288. ''', data)
  289. self.run_test(['platdata'], dtb_file, output)
  290. with open(output) as infile:
  291. data = infile.read()
  292. self._check_strings(C_HEADER + '''
  293. /* Node /gpios@0 index 0 */
  294. static struct dtd_sandbox_gpio dtv_gpios_at_0 = {
  295. \t.gpio_bank_name\t\t= "a",
  296. \t.gpio_controller\t= true,
  297. \t.sandbox_gpio_count\t= 0x14,
  298. };
  299. U_BOOT_DRVINFO(gpios_at_0) = {
  300. \t.name\t\t= "sandbox_gpio",
  301. \t.plat\t= &dtv_gpios_at_0,
  302. \t.plat_size\t= sizeof(dtv_gpios_at_0),
  303. \t.parent_idx\t= -1,
  304. };
  305. ''', data)
  306. def test_invalid_driver(self):
  307. """Test output from a device tree file with an invalid driver"""
  308. dtb_file = get_dtb_file('dtoc_test_invalid_driver.dts')
  309. output = tools.GetOutputFilename('output')
  310. with test_util.capture_sys_output() as _:
  311. dtb_platdata.run_steps(['struct'], dtb_file, False, output, [],
  312. scan=copy_scan())
  313. with open(output) as infile:
  314. data = infile.read()
  315. self._check_strings(HEADER + '''
  316. struct dtd_invalid {
  317. };
  318. ''', data)
  319. with test_util.capture_sys_output() as _:
  320. dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [],
  321. scan=copy_scan())
  322. with open(output) as infile:
  323. data = infile.read()
  324. self._check_strings(C_HEADER + '''
  325. /* Node /spl-test index 0 */
  326. static struct dtd_invalid dtv_spl_test = {
  327. };
  328. U_BOOT_DRVINFO(spl_test) = {
  329. \t.name\t\t= "invalid",
  330. \t.plat\t= &dtv_spl_test,
  331. \t.plat_size\t= sizeof(dtv_spl_test),
  332. \t.parent_idx\t= -1,
  333. };
  334. ''', data)
  335. def test_phandle(self):
  336. """Test output from a node containing a phandle reference"""
  337. dtb_file = get_dtb_file('dtoc_test_phandle.dts')
  338. output = tools.GetOutputFilename('output')
  339. self.run_test(['struct'], dtb_file, output)
  340. with open(output) as infile:
  341. data = infile.read()
  342. self._check_strings(HEADER + '''
  343. struct dtd_source {
  344. \tstruct phandle_2_arg clocks[4];
  345. };
  346. struct dtd_target {
  347. \tfdt32_t\t\tintval;
  348. };
  349. ''', data)
  350. self.run_test(['platdata'], dtb_file, output)
  351. with open(output) as infile:
  352. data = infile.read()
  353. self._check_strings(C_HEADER + '''
  354. /* Node /phandle2-target index 0 */
  355. static struct dtd_target dtv_phandle2_target = {
  356. \t.intval\t\t\t= 0x1,
  357. };
  358. U_BOOT_DRVINFO(phandle2_target) = {
  359. \t.name\t\t= "target",
  360. \t.plat\t= &dtv_phandle2_target,
  361. \t.plat_size\t= sizeof(dtv_phandle2_target),
  362. \t.parent_idx\t= -1,
  363. };
  364. /* Node /phandle3-target index 1 */
  365. static struct dtd_target dtv_phandle3_target = {
  366. \t.intval\t\t\t= 0x2,
  367. };
  368. U_BOOT_DRVINFO(phandle3_target) = {
  369. \t.name\t\t= "target",
  370. \t.plat\t= &dtv_phandle3_target,
  371. \t.plat_size\t= sizeof(dtv_phandle3_target),
  372. \t.parent_idx\t= -1,
  373. };
  374. /* Node /phandle-source index 2 */
  375. static struct dtd_source dtv_phandle_source = {
  376. \t.clocks\t\t\t= {
  377. \t\t\t{4, {}},
  378. \t\t\t{0, {11}},
  379. \t\t\t{1, {12, 13}},
  380. \t\t\t{4, {}},},
  381. };
  382. U_BOOT_DRVINFO(phandle_source) = {
  383. \t.name\t\t= "source",
  384. \t.plat\t= &dtv_phandle_source,
  385. \t.plat_size\t= sizeof(dtv_phandle_source),
  386. \t.parent_idx\t= -1,
  387. };
  388. /* Node /phandle-source2 index 3 */
  389. static struct dtd_source dtv_phandle_source2 = {
  390. \t.clocks\t\t\t= {
  391. \t\t\t{4, {}},},
  392. };
  393. U_BOOT_DRVINFO(phandle_source2) = {
  394. \t.name\t\t= "source",
  395. \t.plat\t= &dtv_phandle_source2,
  396. \t.plat_size\t= sizeof(dtv_phandle_source2),
  397. \t.parent_idx\t= -1,
  398. };
  399. /* Node /phandle-target index 4 */
  400. static struct dtd_target dtv_phandle_target = {
  401. \t.intval\t\t\t= 0x0,
  402. };
  403. U_BOOT_DRVINFO(phandle_target) = {
  404. \t.name\t\t= "target",
  405. \t.plat\t= &dtv_phandle_target,
  406. \t.plat_size\t= sizeof(dtv_phandle_target),
  407. \t.parent_idx\t= -1,
  408. };
  409. ''', data)
  410. def test_phandle_single(self):
  411. """Test output from a node containing a phandle reference"""
  412. dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
  413. output = tools.GetOutputFilename('output')
  414. self.run_test(['struct'], dtb_file, output)
  415. with open(output) as infile:
  416. data = infile.read()
  417. self._check_strings(HEADER + '''
  418. struct dtd_source {
  419. \tstruct phandle_0_arg clocks[1];
  420. };
  421. struct dtd_target {
  422. \tfdt32_t\t\tintval;
  423. };
  424. ''', data)
  425. def test_phandle_reorder(self):
  426. """Test that phandle targets are generated before their references"""
  427. dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
  428. output = tools.GetOutputFilename('output')
  429. self.run_test(['platdata'], dtb_file, output)
  430. with open(output) as infile:
  431. data = infile.read()
  432. self._check_strings(C_HEADER + '''
  433. /* Node /phandle-source2 index 0 */
  434. static struct dtd_source dtv_phandle_source2 = {
  435. \t.clocks\t\t\t= {
  436. \t\t\t{1, {}},},
  437. };
  438. U_BOOT_DRVINFO(phandle_source2) = {
  439. \t.name\t\t= "source",
  440. \t.plat\t= &dtv_phandle_source2,
  441. \t.plat_size\t= sizeof(dtv_phandle_source2),
  442. \t.parent_idx\t= -1,
  443. };
  444. /* Node /phandle-target index 1 */
  445. static struct dtd_target dtv_phandle_target = {
  446. };
  447. U_BOOT_DRVINFO(phandle_target) = {
  448. \t.name\t\t= "target",
  449. \t.plat\t= &dtv_phandle_target,
  450. \t.plat_size\t= sizeof(dtv_phandle_target),
  451. \t.parent_idx\t= -1,
  452. };
  453. ''', data)
  454. def test_phandle_cd_gpio(self):
  455. """Test that phandle targets are generated when unsing cd-gpios"""
  456. dtb_file = get_dtb_file('dtoc_test_phandle_cd_gpios.dts')
  457. output = tools.GetOutputFilename('output')
  458. dtb_platdata.run_steps(['platdata'], dtb_file, False, output, [], True,
  459. scan=copy_scan())
  460. with open(output) as infile:
  461. data = infile.read()
  462. self._check_strings(C_HEADER + '''
  463. /* Node /phandle2-target index 0 */
  464. static struct dtd_target dtv_phandle2_target = {
  465. \t.intval\t\t\t= 0x1,
  466. };
  467. U_BOOT_DRVINFO(phandle2_target) = {
  468. \t.name\t\t= "target",
  469. \t.plat\t= &dtv_phandle2_target,
  470. \t.plat_size\t= sizeof(dtv_phandle2_target),
  471. \t.parent_idx\t= -1,
  472. };
  473. /* Node /phandle3-target index 1 */
  474. static struct dtd_target dtv_phandle3_target = {
  475. \t.intval\t\t\t= 0x2,
  476. };
  477. U_BOOT_DRVINFO(phandle3_target) = {
  478. \t.name\t\t= "target",
  479. \t.plat\t= &dtv_phandle3_target,
  480. \t.plat_size\t= sizeof(dtv_phandle3_target),
  481. \t.parent_idx\t= -1,
  482. };
  483. /* Node /phandle-source index 2 */
  484. static struct dtd_source dtv_phandle_source = {
  485. \t.cd_gpios\t\t= {
  486. \t\t\t{4, {}},
  487. \t\t\t{0, {11}},
  488. \t\t\t{1, {12, 13}},
  489. \t\t\t{4, {}},},
  490. };
  491. U_BOOT_DRVINFO(phandle_source) = {
  492. \t.name\t\t= "source",
  493. \t.plat\t= &dtv_phandle_source,
  494. \t.plat_size\t= sizeof(dtv_phandle_source),
  495. \t.parent_idx\t= -1,
  496. };
  497. /* Node /phandle-source2 index 3 */
  498. static struct dtd_source dtv_phandle_source2 = {
  499. \t.cd_gpios\t\t= {
  500. \t\t\t{4, {}},},
  501. };
  502. U_BOOT_DRVINFO(phandle_source2) = {
  503. \t.name\t\t= "source",
  504. \t.plat\t= &dtv_phandle_source2,
  505. \t.plat_size\t= sizeof(dtv_phandle_source2),
  506. \t.parent_idx\t= -1,
  507. };
  508. /* Node /phandle-target index 4 */
  509. static struct dtd_target dtv_phandle_target = {
  510. \t.intval\t\t\t= 0x0,
  511. };
  512. U_BOOT_DRVINFO(phandle_target) = {
  513. \t.name\t\t= "target",
  514. \t.plat\t= &dtv_phandle_target,
  515. \t.plat_size\t= sizeof(dtv_phandle_target),
  516. \t.parent_idx\t= -1,
  517. };
  518. ''', data)
  519. def test_phandle_bad(self):
  520. """Test a node containing an invalid phandle fails"""
  521. dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
  522. capture_stderr=True)
  523. output = tools.GetOutputFilename('output')
  524. with self.assertRaises(ValueError) as exc:
  525. self.run_test(['struct'], dtb_file, output)
  526. self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
  527. str(exc.exception))
  528. def test_phandle_bad2(self):
  529. """Test a phandle target missing its #*-cells property"""
  530. dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
  531. capture_stderr=True)
  532. output = tools.GetOutputFilename('output')
  533. with self.assertRaises(ValueError) as exc:
  534. self.run_test(['struct'], dtb_file, output)
  535. self.assertIn("Node 'phandle-target' has no cells property",
  536. str(exc.exception))
  537. def test_addresses64(self):
  538. """Test output from a node with a 'reg' property with na=2, ns=2"""
  539. dtb_file = get_dtb_file('dtoc_test_addr64.dts')
  540. output = tools.GetOutputFilename('output')
  541. self.run_test(['struct'], dtb_file, output)
  542. with open(output) as infile:
  543. data = infile.read()
  544. self._check_strings(HEADER + '''
  545. struct dtd_test1 {
  546. \tfdt64_t\t\treg[2];
  547. };
  548. struct dtd_test2 {
  549. \tfdt64_t\t\treg[2];
  550. };
  551. struct dtd_test3 {
  552. \tfdt64_t\t\treg[4];
  553. };
  554. ''', data)
  555. self.run_test(['platdata'], dtb_file, output)
  556. with open(output) as infile:
  557. data = infile.read()
  558. self._check_strings(C_HEADER + '''
  559. /* Node /test1 index 0 */
  560. static struct dtd_test1 dtv_test1 = {
  561. \t.reg\t\t\t= {0x1234, 0x5678},
  562. };
  563. U_BOOT_DRVINFO(test1) = {
  564. \t.name\t\t= "test1",
  565. \t.plat\t= &dtv_test1,
  566. \t.plat_size\t= sizeof(dtv_test1),
  567. \t.parent_idx\t= -1,
  568. };
  569. /* Node /test2 index 1 */
  570. static struct dtd_test2 dtv_test2 = {
  571. \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
  572. };
  573. U_BOOT_DRVINFO(test2) = {
  574. \t.name\t\t= "test2",
  575. \t.plat\t= &dtv_test2,
  576. \t.plat_size\t= sizeof(dtv_test2),
  577. \t.parent_idx\t= -1,
  578. };
  579. /* Node /test3 index 2 */
  580. static struct dtd_test3 dtv_test3 = {
  581. \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
  582. };
  583. U_BOOT_DRVINFO(test3) = {
  584. \t.name\t\t= "test3",
  585. \t.plat\t= &dtv_test3,
  586. \t.plat_size\t= sizeof(dtv_test3),
  587. \t.parent_idx\t= -1,
  588. };
  589. ''', data)
  590. def test_addresses32(self):
  591. """Test output from a node with a 'reg' property with na=1, ns=1"""
  592. dtb_file = get_dtb_file('dtoc_test_addr32.dts')
  593. output = tools.GetOutputFilename('output')
  594. self.run_test(['struct'], dtb_file, output)
  595. with open(output) as infile:
  596. data = infile.read()
  597. self._check_strings(HEADER + '''
  598. struct dtd_test1 {
  599. \tfdt32_t\t\treg[2];
  600. };
  601. struct dtd_test2 {
  602. \tfdt32_t\t\treg[4];
  603. };
  604. ''', data)
  605. self.run_test(['platdata'], dtb_file, output)
  606. with open(output) as infile:
  607. data = infile.read()
  608. self._check_strings(C_HEADER + '''
  609. /* Node /test1 index 0 */
  610. static struct dtd_test1 dtv_test1 = {
  611. \t.reg\t\t\t= {0x1234, 0x5678},
  612. };
  613. U_BOOT_DRVINFO(test1) = {
  614. \t.name\t\t= "test1",
  615. \t.plat\t= &dtv_test1,
  616. \t.plat_size\t= sizeof(dtv_test1),
  617. \t.parent_idx\t= -1,
  618. };
  619. /* Node /test2 index 1 */
  620. static struct dtd_test2 dtv_test2 = {
  621. \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
  622. };
  623. U_BOOT_DRVINFO(test2) = {
  624. \t.name\t\t= "test2",
  625. \t.plat\t= &dtv_test2,
  626. \t.plat_size\t= sizeof(dtv_test2),
  627. \t.parent_idx\t= -1,
  628. };
  629. ''', data)
  630. def test_addresses64_32(self):
  631. """Test output from a node with a 'reg' property with na=2, ns=1"""
  632. dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
  633. output = tools.GetOutputFilename('output')
  634. self.run_test(['struct'], dtb_file, output)
  635. with open(output) as infile:
  636. data = infile.read()
  637. self._check_strings(HEADER + '''
  638. struct dtd_test1 {
  639. \tfdt64_t\t\treg[2];
  640. };
  641. struct dtd_test2 {
  642. \tfdt64_t\t\treg[2];
  643. };
  644. struct dtd_test3 {
  645. \tfdt64_t\t\treg[4];
  646. };
  647. ''', data)
  648. self.run_test(['platdata'], dtb_file, output)
  649. with open(output) as infile:
  650. data = infile.read()
  651. self._check_strings(C_HEADER + '''
  652. /* Node /test1 index 0 */
  653. static struct dtd_test1 dtv_test1 = {
  654. \t.reg\t\t\t= {0x123400000000, 0x5678},
  655. };
  656. U_BOOT_DRVINFO(test1) = {
  657. \t.name\t\t= "test1",
  658. \t.plat\t= &dtv_test1,
  659. \t.plat_size\t= sizeof(dtv_test1),
  660. \t.parent_idx\t= -1,
  661. };
  662. /* Node /test2 index 1 */
  663. static struct dtd_test2 dtv_test2 = {
  664. \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
  665. };
  666. U_BOOT_DRVINFO(test2) = {
  667. \t.name\t\t= "test2",
  668. \t.plat\t= &dtv_test2,
  669. \t.plat_size\t= sizeof(dtv_test2),
  670. \t.parent_idx\t= -1,
  671. };
  672. /* Node /test3 index 2 */
  673. static struct dtd_test3 dtv_test3 = {
  674. \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
  675. };
  676. U_BOOT_DRVINFO(test3) = {
  677. \t.name\t\t= "test3",
  678. \t.plat\t= &dtv_test3,
  679. \t.plat_size\t= sizeof(dtv_test3),
  680. \t.parent_idx\t= -1,
  681. };
  682. ''', data)
  683. def test_addresses32_64(self):
  684. """Test output from a node with a 'reg' property with na=1, ns=2"""
  685. dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
  686. output = tools.GetOutputFilename('output')
  687. self.run_test(['struct'], dtb_file, output)
  688. with open(output) as infile:
  689. data = infile.read()
  690. self._check_strings(HEADER + '''
  691. struct dtd_test1 {
  692. \tfdt64_t\t\treg[2];
  693. };
  694. struct dtd_test2 {
  695. \tfdt64_t\t\treg[2];
  696. };
  697. struct dtd_test3 {
  698. \tfdt64_t\t\treg[4];
  699. };
  700. ''', data)
  701. self.run_test(['platdata'], dtb_file, output)
  702. with open(output) as infile:
  703. data = infile.read()
  704. self._check_strings(C_HEADER + '''
  705. /* Node /test1 index 0 */
  706. static struct dtd_test1 dtv_test1 = {
  707. \t.reg\t\t\t= {0x1234, 0x567800000000},
  708. };
  709. U_BOOT_DRVINFO(test1) = {
  710. \t.name\t\t= "test1",
  711. \t.plat\t= &dtv_test1,
  712. \t.plat_size\t= sizeof(dtv_test1),
  713. \t.parent_idx\t= -1,
  714. };
  715. /* Node /test2 index 1 */
  716. static struct dtd_test2 dtv_test2 = {
  717. \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
  718. };
  719. U_BOOT_DRVINFO(test2) = {
  720. \t.name\t\t= "test2",
  721. \t.plat\t= &dtv_test2,
  722. \t.plat_size\t= sizeof(dtv_test2),
  723. \t.parent_idx\t= -1,
  724. };
  725. /* Node /test3 index 2 */
  726. static struct dtd_test3 dtv_test3 = {
  727. \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
  728. };
  729. U_BOOT_DRVINFO(test3) = {
  730. \t.name\t\t= "test3",
  731. \t.plat\t= &dtv_test3,
  732. \t.plat_size\t= sizeof(dtv_test3),
  733. \t.parent_idx\t= -1,
  734. };
  735. ''', data)
  736. def test_bad_reg(self):
  737. """Test that a reg property with an invalid type generates an error"""
  738. # Capture stderr since dtc will emit warnings for this file
  739. dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
  740. output = tools.GetOutputFilename('output')
  741. with self.assertRaises(ValueError) as exc:
  742. self.run_test(['struct'], dtb_file, output)
  743. self.assertIn("Node 'spl-test' reg property is not an int",
  744. str(exc.exception))
  745. def test_bad_reg2(self):
  746. """Test that a reg property with an invalid cell count is detected"""
  747. # Capture stderr since dtc will emit warnings for this file
  748. dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
  749. output = tools.GetOutputFilename('output')
  750. with self.assertRaises(ValueError) as exc:
  751. self.run_test(['struct'], dtb_file, output)
  752. self.assertIn(
  753. "Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
  754. str(exc.exception))
  755. def test_add_prop(self):
  756. """Test that a subequent node can add a new property to a struct"""
  757. dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
  758. output = tools.GetOutputFilename('output')
  759. self.run_test(['struct'], dtb_file, output)
  760. with open(output) as infile:
  761. data = infile.read()
  762. self._check_strings(HEADER + '''
  763. struct dtd_sandbox_spl_test {
  764. \tfdt32_t\t\tintarray;
  765. \tfdt32_t\t\tintval;
  766. };
  767. ''', data)
  768. self.run_test(['platdata'], dtb_file, output)
  769. with open(output) as infile:
  770. data = infile.read()
  771. self._check_strings(C_HEADER + '''
  772. /* Node /spl-test index 0 */
  773. static struct dtd_sandbox_spl_test dtv_spl_test = {
  774. \t.intval\t\t\t= 0x1,
  775. };
  776. U_BOOT_DRVINFO(spl_test) = {
  777. \t.name\t\t= "sandbox_spl_test",
  778. \t.plat\t= &dtv_spl_test,
  779. \t.plat_size\t= sizeof(dtv_spl_test),
  780. \t.parent_idx\t= -1,
  781. };
  782. /* Node /spl-test2 index 1 */
  783. static struct dtd_sandbox_spl_test dtv_spl_test2 = {
  784. \t.intarray\t\t= 0x5,
  785. };
  786. U_BOOT_DRVINFO(spl_test2) = {
  787. \t.name\t\t= "sandbox_spl_test",
  788. \t.plat\t= &dtv_spl_test2,
  789. \t.plat_size\t= sizeof(dtv_spl_test2),
  790. \t.parent_idx\t= -1,
  791. };
  792. ''', data)
  793. def test_stdout(self):
  794. """Test output to stdout"""
  795. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  796. with test_util.capture_sys_output() as (stdout, _):
  797. self.run_test(['struct'], dtb_file, None)
  798. self._check_strings(self.struct_text, stdout.getvalue())
  799. def test_multi_to_file(self):
  800. """Test output of multiple pieces to a single file"""
  801. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  802. output = tools.GetOutputFilename('output')
  803. self.run_test(['all'], dtb_file, output)
  804. data = tools.ReadFile(output, binary=False)
  805. self._check_strings(self.platdata_text + self.struct_text, data)
  806. def test_no_command(self):
  807. """Test running dtoc without a command"""
  808. with self.assertRaises(ValueError) as exc:
  809. self.run_test([], '', '')
  810. self.assertIn("Please specify a command: struct, platdata",
  811. str(exc.exception))
  812. def test_bad_command(self):
  813. """Test running dtoc with an invalid command"""
  814. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  815. output = tools.GetOutputFilename('output')
  816. with self.assertRaises(ValueError) as exc:
  817. self.run_test(['invalid-cmd'], dtb_file, output)
  818. self.assertIn("Unknown command 'invalid-cmd': (use: platdata, struct)",
  819. str(exc.exception))
  820. def test_output_conflict(self):
  821. """Test a conflict between and output dirs and output file"""
  822. with self.assertRaises(ValueError) as exc:
  823. dtb_platdata.run_steps(['all'], None, False, 'out', ['cdir'], True,
  824. scan=copy_scan())
  825. self.assertIn("Must specify either output or output_dirs, not both",
  826. str(exc.exception))
  827. def test_output_dirs(self):
  828. """Test outputting files to a directory"""
  829. # Remove the directory so that files from other tests are not there
  830. tools._RemoveOutputDir()
  831. tools.PrepareOutputDir(None)
  832. # This should create the .dts and .dtb in the output directory
  833. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  834. outdir = tools.GetOutputDir()
  835. fnames = glob.glob(outdir + '/*')
  836. self.assertEqual(2, len(fnames))
  837. dtb_platdata.run_steps(['all'], dtb_file, False, None, [outdir], True,
  838. scan=copy_scan())
  839. fnames = glob.glob(outdir + '/*')
  840. self.assertEqual(4, len(fnames))
  841. leafs = set(os.path.basename(fname) for fname in fnames)
  842. self.assertEqual(
  843. {'dt-structs-gen.h', 'source.dts', 'dt-plat.c', 'source.dtb'},
  844. leafs)