test_dtoc.py 30 KB

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