test_dtoc.py 27 KB

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