test_dtoc.py 21 KB

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