test_dtoc.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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 unittest
  13. from dtoc import dtb_platdata
  14. from dtb_platdata import conv_name_to_c
  15. from dtb_platdata import get_compat_name
  16. from dtb_platdata import get_value
  17. from dtb_platdata import tab_to
  18. from dtoc import fdt
  19. from dtoc import fdt_util
  20. from patman import test_util
  21. from patman import tools
  22. our_path = os.path.dirname(os.path.realpath(__file__))
  23. HEADER = '''/*
  24. * DO NOT MODIFY
  25. *
  26. * This file was generated by dtoc from a .dtb (device tree binary) file.
  27. */
  28. #include <stdbool.h>
  29. #include <linux/libfdt.h>'''
  30. C_HEADER = '''/*
  31. * DO NOT MODIFY
  32. *
  33. * This file was generated by dtoc from a .dtb (device tree binary) file.
  34. */
  35. #include <common.h>
  36. #include <dm.h>
  37. #include <dt-structs.h>
  38. '''
  39. def get_dtb_file(dts_fname, capture_stderr=False):
  40. """Compile a .dts file to a .dtb
  41. Args:
  42. dts_fname: Filename of .dts file in the current directory
  43. capture_stderr: True to capture and discard stderr output
  44. Returns:
  45. Filename of compiled file in output directory
  46. """
  47. return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname),
  48. capture_stderr=capture_stderr)
  49. class TestDtoc(unittest.TestCase):
  50. """Tests for dtoc"""
  51. @classmethod
  52. def setUpClass(cls):
  53. tools.PrepareOutputDir(None)
  54. @classmethod
  55. def tearDownClass(cls):
  56. tools._RemoveOutputDir()
  57. def _WritePythonString(self, fname, data):
  58. """Write a string with tabs expanded as done in this Python file
  59. Args:
  60. fname: Filename to write to
  61. data: Raw string to convert
  62. """
  63. data = data.replace('\t', '\\t')
  64. with open(fname, 'w') as fd:
  65. fd.write(data)
  66. def _CheckStrings(self, expected, actual):
  67. """Check that a string matches its expected value
  68. If the strings do not match, they are written to the /tmp directory in
  69. the same Python format as is used here in the test. This allows for
  70. easy comparison and update of the tests.
  71. Args:
  72. expected: Expected string
  73. actual: Actual string
  74. """
  75. if expected != actual:
  76. self._WritePythonString('/tmp/binman.expected', expected)
  77. self._WritePythonString('/tmp/binman.actual', actual)
  78. print('Failures written to /tmp/binman.{expected,actual}')
  79. self.assertEquals(expected, actual)
  80. def test_name(self):
  81. """Test conversion of device tree names to C identifiers"""
  82. self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
  83. self.assertEqual('vendor_clock_frequency',
  84. conv_name_to_c('vendor,clock-frequency'))
  85. self.assertEqual('rockchip_rk3399_sdhci_5_1',
  86. conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
  87. def test_tab_to(self):
  88. """Test operation of tab_to() function"""
  89. self.assertEqual('fred ', tab_to(0, 'fred'))
  90. self.assertEqual('fred\t', tab_to(1, 'fred'))
  91. self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
  92. self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
  93. self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
  94. self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
  95. def test_get_value(self):
  96. """Test operation of get_value() function"""
  97. self.assertEqual('0x45',
  98. get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
  99. self.assertEqual('0x45',
  100. get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
  101. self.assertEqual('0x0',
  102. get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
  103. self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
  104. self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
  105. def test_get_compat_name(self):
  106. """Test operation of get_compat_name() function"""
  107. Prop = collections.namedtuple('Prop', ['value'])
  108. Node = collections.namedtuple('Node', ['props'])
  109. prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
  110. node = Node({'compatible': prop})
  111. self.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']),
  112. get_compat_name(node))
  113. prop = Prop(['rockchip,rk3399-sdhci-5.1'])
  114. node = Node({'compatible': prop})
  115. self.assertEqual(('rockchip_rk3399_sdhci_5_1', []),
  116. get_compat_name(node))
  117. prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
  118. node = Node({'compatible': prop})
  119. self.assertEqual(('rockchip_rk3399_sdhci_5_1',
  120. ['arasan_sdhci_5_1', 'third']),
  121. get_compat_name(node))
  122. def test_empty_file(self):
  123. """Test output from a device tree file with no nodes"""
  124. dtb_file = get_dtb_file('dtoc_test_empty.dts')
  125. output = tools.GetOutputFilename('output')
  126. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  127. with open(output) as infile:
  128. lines = infile.read().splitlines()
  129. self.assertEqual(HEADER.splitlines(), lines)
  130. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  131. with open(output) as infile:
  132. lines = infile.read().splitlines()
  133. self.assertEqual(C_HEADER.splitlines() + [''], lines)
  134. def test_simple(self):
  135. """Test output from some simple nodes with various types of data"""
  136. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  137. output = tools.GetOutputFilename('output')
  138. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  139. with open(output) as infile:
  140. data = infile.read()
  141. self._CheckStrings(HEADER + '''
  142. struct dtd_sandbox_i2c_test {
  143. };
  144. struct dtd_sandbox_pmic_test {
  145. \tbool\t\tlow_power;
  146. \tfdt64_t\t\treg[2];
  147. };
  148. struct dtd_sandbox_spl_test {
  149. \tbool\t\tboolval;
  150. \tunsigned char\tbytearray[3];
  151. \tunsigned char\tbyteval;
  152. \tfdt32_t\t\tintarray[4];
  153. \tfdt32_t\t\tintval;
  154. \tunsigned char\tlongbytearray[9];
  155. \tunsigned char\tnotstring[5];
  156. \tconst char *\tstringarray[3];
  157. \tconst char *\tstringval;
  158. };
  159. struct dtd_sandbox_spl_test_2 {
  160. };
  161. ''', data)
  162. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  163. with open(output) as infile:
  164. data = infile.read()
  165. self._CheckStrings(C_HEADER + '''
  166. static const struct dtd_sandbox_spl_test dtv_spl_test = {
  167. \t.boolval\t\t= true,
  168. \t.bytearray\t\t= {0x6, 0x0, 0x0},
  169. \t.byteval\t\t= 0x5,
  170. \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
  171. \t.intval\t\t\t= 0x1,
  172. \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
  173. \t\t0x11},
  174. \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
  175. \t.stringarray\t\t= {"multi-word", "message", ""},
  176. \t.stringval\t\t= "message",
  177. };
  178. U_BOOT_DEVICE(spl_test) = {
  179. \t.name\t\t= "sandbox_spl_test",
  180. \t.platdata\t= &dtv_spl_test,
  181. \t.platdata_size\t= sizeof(dtv_spl_test),
  182. };
  183. static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
  184. \t.bytearray\t\t= {0x1, 0x23, 0x34},
  185. \t.byteval\t\t= 0x8,
  186. \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
  187. \t.intval\t\t\t= 0x3,
  188. \t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  189. \t\t0x0},
  190. \t.stringarray\t\t= {"another", "multi-word", "message"},
  191. \t.stringval\t\t= "message2",
  192. };
  193. U_BOOT_DEVICE(spl_test2) = {
  194. \t.name\t\t= "sandbox_spl_test",
  195. \t.platdata\t= &dtv_spl_test2,
  196. \t.platdata_size\t= sizeof(dtv_spl_test2),
  197. };
  198. static const struct dtd_sandbox_spl_test dtv_spl_test3 = {
  199. \t.stringarray\t\t= {"one", "", ""},
  200. };
  201. U_BOOT_DEVICE(spl_test3) = {
  202. \t.name\t\t= "sandbox_spl_test",
  203. \t.platdata\t= &dtv_spl_test3,
  204. \t.platdata_size\t= sizeof(dtv_spl_test3),
  205. };
  206. static const struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
  207. };
  208. U_BOOT_DEVICE(spl_test4) = {
  209. \t.name\t\t= "sandbox_spl_test_2",
  210. \t.platdata\t= &dtv_spl_test4,
  211. \t.platdata_size\t= sizeof(dtv_spl_test4),
  212. };
  213. static const struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
  214. };
  215. U_BOOT_DEVICE(i2c_at_0) = {
  216. \t.name\t\t= "sandbox_i2c_test",
  217. \t.platdata\t= &dtv_i2c_at_0,
  218. \t.platdata_size\t= sizeof(dtv_i2c_at_0),
  219. };
  220. static const struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
  221. \t.low_power\t\t= true,
  222. \t.reg\t\t\t= {0x9, 0x0},
  223. };
  224. U_BOOT_DEVICE(pmic_at_9) = {
  225. \t.name\t\t= "sandbox_pmic_test",
  226. \t.platdata\t= &dtv_pmic_at_9,
  227. \t.platdata_size\t= sizeof(dtv_pmic_at_9),
  228. };
  229. ''', data)
  230. def test_phandle(self):
  231. """Test output from a node containing a phandle reference"""
  232. dtb_file = get_dtb_file('dtoc_test_phandle.dts')
  233. output = tools.GetOutputFilename('output')
  234. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  235. with open(output) as infile:
  236. data = infile.read()
  237. self._CheckStrings(HEADER + '''
  238. struct dtd_source {
  239. \tstruct phandle_2_arg clocks[4];
  240. };
  241. struct dtd_target {
  242. \tfdt32_t\t\tintval;
  243. };
  244. ''', data)
  245. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  246. with open(output) as infile:
  247. data = infile.read()
  248. self._CheckStrings(C_HEADER + '''
  249. static const struct dtd_target dtv_phandle_target = {
  250. \t.intval\t\t\t= 0x0,
  251. };
  252. U_BOOT_DEVICE(phandle_target) = {
  253. \t.name\t\t= "target",
  254. \t.platdata\t= &dtv_phandle_target,
  255. \t.platdata_size\t= sizeof(dtv_phandle_target),
  256. };
  257. static const struct dtd_target dtv_phandle2_target = {
  258. \t.intval\t\t\t= 0x1,
  259. };
  260. U_BOOT_DEVICE(phandle2_target) = {
  261. \t.name\t\t= "target",
  262. \t.platdata\t= &dtv_phandle2_target,
  263. \t.platdata_size\t= sizeof(dtv_phandle2_target),
  264. };
  265. static const struct dtd_target dtv_phandle3_target = {
  266. \t.intval\t\t\t= 0x2,
  267. };
  268. U_BOOT_DEVICE(phandle3_target) = {
  269. \t.name\t\t= "target",
  270. \t.platdata\t= &dtv_phandle3_target,
  271. \t.platdata_size\t= sizeof(dtv_phandle3_target),
  272. };
  273. static const struct dtd_source dtv_phandle_source = {
  274. \t.clocks\t\t\t= {
  275. \t\t\t{&dtv_phandle_target, {}},
  276. \t\t\t{&dtv_phandle2_target, {11}},
  277. \t\t\t{&dtv_phandle3_target, {12, 13}},
  278. \t\t\t{&dtv_phandle_target, {}},},
  279. };
  280. U_BOOT_DEVICE(phandle_source) = {
  281. \t.name\t\t= "source",
  282. \t.platdata\t= &dtv_phandle_source,
  283. \t.platdata_size\t= sizeof(dtv_phandle_source),
  284. };
  285. static const struct dtd_source dtv_phandle_source2 = {
  286. \t.clocks\t\t\t= {
  287. \t\t\t{&dtv_phandle_target, {}},},
  288. };
  289. U_BOOT_DEVICE(phandle_source2) = {
  290. \t.name\t\t= "source",
  291. \t.platdata\t= &dtv_phandle_source2,
  292. \t.platdata_size\t= sizeof(dtv_phandle_source2),
  293. };
  294. ''', data)
  295. def test_phandle_single(self):
  296. """Test output from a node containing a phandle reference"""
  297. dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
  298. output = tools.GetOutputFilename('output')
  299. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  300. with open(output) as infile:
  301. data = infile.read()
  302. self._CheckStrings(HEADER + '''
  303. struct dtd_source {
  304. \tstruct phandle_0_arg clocks[1];
  305. };
  306. struct dtd_target {
  307. \tfdt32_t\t\tintval;
  308. };
  309. ''', data)
  310. def test_phandle_reorder(self):
  311. """Test that phandle targets are generated before their references"""
  312. dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
  313. output = tools.GetOutputFilename('output')
  314. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  315. with open(output) as infile:
  316. data = infile.read()
  317. self._CheckStrings(C_HEADER + '''
  318. static const struct dtd_target dtv_phandle_target = {
  319. };
  320. U_BOOT_DEVICE(phandle_target) = {
  321. \t.name\t\t= "target",
  322. \t.platdata\t= &dtv_phandle_target,
  323. \t.platdata_size\t= sizeof(dtv_phandle_target),
  324. };
  325. static const struct dtd_source dtv_phandle_source2 = {
  326. \t.clocks\t\t\t= {
  327. \t\t\t{&dtv_phandle_target, {}},},
  328. };
  329. U_BOOT_DEVICE(phandle_source2) = {
  330. \t.name\t\t= "source",
  331. \t.platdata\t= &dtv_phandle_source2,
  332. \t.platdata_size\t= sizeof(dtv_phandle_source2),
  333. };
  334. ''', data)
  335. def test_phandle_bad(self):
  336. """Test a node containing an invalid phandle fails"""
  337. dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts',
  338. capture_stderr=True)
  339. output = tools.GetOutputFilename('output')
  340. with self.assertRaises(ValueError) as e:
  341. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  342. self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
  343. str(e.exception))
  344. def test_phandle_bad2(self):
  345. """Test a phandle target missing its #*-cells property"""
  346. dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts',
  347. capture_stderr=True)
  348. output = tools.GetOutputFilename('output')
  349. with self.assertRaises(ValueError) as e:
  350. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  351. self.assertIn("Node 'phandle-target' has no '#clock-cells' property",
  352. str(e.exception))
  353. def test_aliases(self):
  354. """Test output from a node with multiple compatible strings"""
  355. dtb_file = get_dtb_file('dtoc_test_aliases.dts')
  356. output = tools.GetOutputFilename('output')
  357. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  358. with open(output) as infile:
  359. data = infile.read()
  360. self._CheckStrings(HEADER + '''
  361. struct dtd_compat1 {
  362. \tfdt32_t\t\tintval;
  363. };
  364. #define dtd_compat2_1_fred dtd_compat1
  365. #define dtd_compat3 dtd_compat1
  366. ''', data)
  367. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  368. with open(output) as infile:
  369. data = infile.read()
  370. self._CheckStrings(C_HEADER + '''
  371. static const struct dtd_compat1 dtv_spl_test = {
  372. \t.intval\t\t\t= 0x1,
  373. };
  374. U_BOOT_DEVICE(spl_test) = {
  375. \t.name\t\t= "compat1",
  376. \t.platdata\t= &dtv_spl_test,
  377. \t.platdata_size\t= sizeof(dtv_spl_test),
  378. };
  379. ''', data)
  380. def test_addresses64(self):
  381. """Test output from a node with a 'reg' property with na=2, ns=2"""
  382. dtb_file = get_dtb_file('dtoc_test_addr64.dts')
  383. output = tools.GetOutputFilename('output')
  384. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  385. with open(output) as infile:
  386. data = infile.read()
  387. self._CheckStrings(HEADER + '''
  388. struct dtd_test1 {
  389. \tfdt64_t\t\treg[2];
  390. };
  391. struct dtd_test2 {
  392. \tfdt64_t\t\treg[2];
  393. };
  394. struct dtd_test3 {
  395. \tfdt64_t\t\treg[4];
  396. };
  397. ''', data)
  398. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  399. with open(output) as infile:
  400. data = infile.read()
  401. self._CheckStrings(C_HEADER + '''
  402. static const struct dtd_test1 dtv_test1 = {
  403. \t.reg\t\t\t= {0x1234, 0x5678},
  404. };
  405. U_BOOT_DEVICE(test1) = {
  406. \t.name\t\t= "test1",
  407. \t.platdata\t= &dtv_test1,
  408. \t.platdata_size\t= sizeof(dtv_test1),
  409. };
  410. static const struct dtd_test2 dtv_test2 = {
  411. \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
  412. };
  413. U_BOOT_DEVICE(test2) = {
  414. \t.name\t\t= "test2",
  415. \t.platdata\t= &dtv_test2,
  416. \t.platdata_size\t= sizeof(dtv_test2),
  417. };
  418. static const struct dtd_test3 dtv_test3 = {
  419. \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
  420. };
  421. U_BOOT_DEVICE(test3) = {
  422. \t.name\t\t= "test3",
  423. \t.platdata\t= &dtv_test3,
  424. \t.platdata_size\t= sizeof(dtv_test3),
  425. };
  426. ''', data)
  427. def test_addresses32(self):
  428. """Test output from a node with a 'reg' property with na=1, ns=1"""
  429. dtb_file = get_dtb_file('dtoc_test_addr32.dts')
  430. output = tools.GetOutputFilename('output')
  431. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  432. with open(output) as infile:
  433. data = infile.read()
  434. self._CheckStrings(HEADER + '''
  435. struct dtd_test1 {
  436. \tfdt32_t\t\treg[2];
  437. };
  438. struct dtd_test2 {
  439. \tfdt32_t\t\treg[4];
  440. };
  441. ''', data)
  442. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  443. with open(output) as infile:
  444. data = infile.read()
  445. self._CheckStrings(C_HEADER + '''
  446. static const struct dtd_test1 dtv_test1 = {
  447. \t.reg\t\t\t= {0x1234, 0x5678},
  448. };
  449. U_BOOT_DEVICE(test1) = {
  450. \t.name\t\t= "test1",
  451. \t.platdata\t= &dtv_test1,
  452. \t.platdata_size\t= sizeof(dtv_test1),
  453. };
  454. static const struct dtd_test2 dtv_test2 = {
  455. \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
  456. };
  457. U_BOOT_DEVICE(test2) = {
  458. \t.name\t\t= "test2",
  459. \t.platdata\t= &dtv_test2,
  460. \t.platdata_size\t= sizeof(dtv_test2),
  461. };
  462. ''', data)
  463. def test_addresses64_32(self):
  464. """Test output from a node with a 'reg' property with na=2, ns=1"""
  465. dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
  466. output = tools.GetOutputFilename('output')
  467. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  468. with open(output) as infile:
  469. data = infile.read()
  470. self._CheckStrings(HEADER + '''
  471. struct dtd_test1 {
  472. \tfdt64_t\t\treg[2];
  473. };
  474. struct dtd_test2 {
  475. \tfdt64_t\t\treg[2];
  476. };
  477. struct dtd_test3 {
  478. \tfdt64_t\t\treg[4];
  479. };
  480. ''', data)
  481. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  482. with open(output) as infile:
  483. data = infile.read()
  484. self._CheckStrings(C_HEADER + '''
  485. static const struct dtd_test1 dtv_test1 = {
  486. \t.reg\t\t\t= {0x123400000000, 0x5678},
  487. };
  488. U_BOOT_DEVICE(test1) = {
  489. \t.name\t\t= "test1",
  490. \t.platdata\t= &dtv_test1,
  491. \t.platdata_size\t= sizeof(dtv_test1),
  492. };
  493. static const struct dtd_test2 dtv_test2 = {
  494. \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
  495. };
  496. U_BOOT_DEVICE(test2) = {
  497. \t.name\t\t= "test2",
  498. \t.platdata\t= &dtv_test2,
  499. \t.platdata_size\t= sizeof(dtv_test2),
  500. };
  501. static const struct dtd_test3 dtv_test3 = {
  502. \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
  503. };
  504. U_BOOT_DEVICE(test3) = {
  505. \t.name\t\t= "test3",
  506. \t.platdata\t= &dtv_test3,
  507. \t.platdata_size\t= sizeof(dtv_test3),
  508. };
  509. ''', data)
  510. def test_addresses32_64(self):
  511. """Test output from a node with a 'reg' property with na=1, ns=2"""
  512. dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
  513. output = tools.GetOutputFilename('output')
  514. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  515. with open(output) as infile:
  516. data = infile.read()
  517. self._CheckStrings(HEADER + '''
  518. struct dtd_test1 {
  519. \tfdt64_t\t\treg[2];
  520. };
  521. struct dtd_test2 {
  522. \tfdt64_t\t\treg[2];
  523. };
  524. struct dtd_test3 {
  525. \tfdt64_t\t\treg[4];
  526. };
  527. ''', data)
  528. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  529. with open(output) as infile:
  530. data = infile.read()
  531. self._CheckStrings(C_HEADER + '''
  532. static const struct dtd_test1 dtv_test1 = {
  533. \t.reg\t\t\t= {0x1234, 0x567800000000},
  534. };
  535. U_BOOT_DEVICE(test1) = {
  536. \t.name\t\t= "test1",
  537. \t.platdata\t= &dtv_test1,
  538. \t.platdata_size\t= sizeof(dtv_test1),
  539. };
  540. static const struct dtd_test2 dtv_test2 = {
  541. \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
  542. };
  543. U_BOOT_DEVICE(test2) = {
  544. \t.name\t\t= "test2",
  545. \t.platdata\t= &dtv_test2,
  546. \t.platdata_size\t= sizeof(dtv_test2),
  547. };
  548. static const struct dtd_test3 dtv_test3 = {
  549. \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
  550. };
  551. U_BOOT_DEVICE(test3) = {
  552. \t.name\t\t= "test3",
  553. \t.platdata\t= &dtv_test3,
  554. \t.platdata_size\t= sizeof(dtv_test3),
  555. };
  556. ''', data)
  557. def test_bad_reg(self):
  558. """Test that a reg property with an invalid type generates an error"""
  559. # Capture stderr since dtc will emit warnings for this file
  560. dtb_file = get_dtb_file('dtoc_test_bad_reg.dts', capture_stderr=True)
  561. output = tools.GetOutputFilename('output')
  562. with self.assertRaises(ValueError) as e:
  563. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  564. self.assertIn("Node 'spl-test' reg property is not an int",
  565. str(e.exception))
  566. def test_bad_reg2(self):
  567. """Test that a reg property with an invalid cell count is detected"""
  568. # Capture stderr since dtc will emit warnings for this file
  569. dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts', capture_stderr=True)
  570. output = tools.GetOutputFilename('output')
  571. with self.assertRaises(ValueError) as e:
  572. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  573. self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
  574. str(e.exception))
  575. def test_add_prop(self):
  576. """Test that a subequent node can add a new property to a struct"""
  577. dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
  578. output = tools.GetOutputFilename('output')
  579. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  580. with open(output) as infile:
  581. data = infile.read()
  582. self._CheckStrings(HEADER + '''
  583. struct dtd_sandbox_spl_test {
  584. \tfdt32_t\t\tintarray;
  585. \tfdt32_t\t\tintval;
  586. };
  587. ''', data)
  588. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  589. with open(output) as infile:
  590. data = infile.read()
  591. self._CheckStrings(C_HEADER + '''
  592. static const struct dtd_sandbox_spl_test dtv_spl_test = {
  593. \t.intval\t\t\t= 0x1,
  594. };
  595. U_BOOT_DEVICE(spl_test) = {
  596. \t.name\t\t= "sandbox_spl_test",
  597. \t.platdata\t= &dtv_spl_test,
  598. \t.platdata_size\t= sizeof(dtv_spl_test),
  599. };
  600. static const struct dtd_sandbox_spl_test dtv_spl_test2 = {
  601. \t.intarray\t\t= 0x5,
  602. };
  603. U_BOOT_DEVICE(spl_test2) = {
  604. \t.name\t\t= "sandbox_spl_test",
  605. \t.platdata\t= &dtv_spl_test2,
  606. \t.platdata_size\t= sizeof(dtv_spl_test2),
  607. };
  608. ''', data)
  609. def testStdout(self):
  610. """Test output to stdout"""
  611. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  612. with test_util.capture_sys_output() as (stdout, stderr):
  613. dtb_platdata.run_steps(['struct'], dtb_file, False, '-')
  614. def testNoCommand(self):
  615. """Test running dtoc without a command"""
  616. with self.assertRaises(ValueError) as e:
  617. dtb_platdata.run_steps([], '', False, '')
  618. self.assertIn("Please specify a command: struct, platdata",
  619. str(e.exception))
  620. def testBadCommand(self):
  621. """Test running dtoc with an invalid command"""
  622. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  623. output = tools.GetOutputFilename('output')
  624. with self.assertRaises(ValueError) as e:
  625. dtb_platdata.run_steps(['invalid-cmd'], dtb_file, False, output)
  626. self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
  627. str(e.exception))