test_dtoc.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 tools
  20. our_path = os.path.dirname(os.path.realpath(__file__))
  21. HEADER = '''/*
  22. * DO NOT MODIFY
  23. *
  24. * This file was generated by dtoc from a .dtb (device tree binary) file.
  25. */
  26. #include <stdbool.h>
  27. #include <linux/libfdt.h>'''
  28. C_HEADER = '''/*
  29. * DO NOT MODIFY
  30. *
  31. * This file was generated by dtoc from a .dtb (device tree binary) file.
  32. */
  33. #include <common.h>
  34. #include <dm.h>
  35. #include <dt-structs.h>
  36. '''
  37. def get_dtb_file(dts_fname):
  38. """Compile a .dts file to a .dtb
  39. Args:
  40. dts_fname: Filename of .dts file in the current directory
  41. Returns:
  42. Filename of compiled file in output directory
  43. """
  44. return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname))
  45. class TestDtoc(unittest.TestCase):
  46. """Tests for dtoc"""
  47. @classmethod
  48. def setUpClass(cls):
  49. tools.PrepareOutputDir(None)
  50. @classmethod
  51. def tearDownClass(cls):
  52. tools._RemoveOutputDir()
  53. def _WritePythonString(self, fname, data):
  54. """Write a string with tabs expanded as done in this Python file
  55. Args:
  56. fname: Filename to write to
  57. data: Raw string to convert
  58. """
  59. data = data.replace('\t', '\\t')
  60. with open(fname, 'w') as fd:
  61. fd.write(data)
  62. def _CheckStrings(self, expected, actual):
  63. """Check that a string matches its expected value
  64. If the strings do not match, they are written to the /tmp directory in
  65. the same Python format as is used here in the test. This allows for
  66. easy comparison and update of the tests.
  67. Args:
  68. expected: Expected string
  69. actual: Actual string
  70. """
  71. if expected != actual:
  72. self._WritePythonString('/tmp/binman.expected', expected)
  73. self._WritePythonString('/tmp/binman.actual', actual)
  74. print 'Failures written to /tmp/binman.{expected,actual}'
  75. self.assertEquals(expected, actual)
  76. def test_name(self):
  77. """Test conversion of device tree names to C identifiers"""
  78. self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
  79. self.assertEqual('vendor_clock_frequency',
  80. conv_name_to_c('vendor,clock-frequency'))
  81. self.assertEqual('rockchip_rk3399_sdhci_5_1',
  82. conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
  83. def test_tab_to(self):
  84. """Test operation of tab_to() function"""
  85. self.assertEqual('fred ', tab_to(0, 'fred'))
  86. self.assertEqual('fred\t', tab_to(1, 'fred'))
  87. self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
  88. self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
  89. self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
  90. self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
  91. def test_get_value(self):
  92. """Test operation of get_value() function"""
  93. self.assertEqual('0x45',
  94. get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
  95. self.assertEqual('0x45',
  96. get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
  97. self.assertEqual('0x0',
  98. get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
  99. self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
  100. self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
  101. def test_get_compat_name(self):
  102. """Test operation of get_compat_name() function"""
  103. Prop = collections.namedtuple('Prop', ['value'])
  104. Node = collections.namedtuple('Node', ['props'])
  105. prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
  106. node = Node({'compatible': prop})
  107. self.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']),
  108. get_compat_name(node))
  109. prop = Prop(['rockchip,rk3399-sdhci-5.1'])
  110. node = Node({'compatible': prop})
  111. self.assertEqual(('rockchip_rk3399_sdhci_5_1', []),
  112. get_compat_name(node))
  113. prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
  114. node = Node({'compatible': prop})
  115. self.assertEqual(('rockchip_rk3399_sdhci_5_1',
  116. ['arasan_sdhci_5_1', 'third']),
  117. get_compat_name(node))
  118. def test_empty_file(self):
  119. """Test output from a device tree file with no nodes"""
  120. dtb_file = get_dtb_file('dtoc_test_empty.dts')
  121. output = tools.GetOutputFilename('output')
  122. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  123. with open(output) as infile:
  124. lines = infile.read().splitlines()
  125. self.assertEqual(HEADER.splitlines(), lines)
  126. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  127. with open(output) as infile:
  128. lines = infile.read().splitlines()
  129. self.assertEqual(C_HEADER.splitlines() + [''], lines)
  130. def test_simple(self):
  131. """Test output from some simple nodes with various types of data"""
  132. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  133. output = tools.GetOutputFilename('output')
  134. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  135. with open(output) as infile:
  136. data = infile.read()
  137. self._CheckStrings(HEADER + '''
  138. struct dtd_sandbox_i2c_test {
  139. };
  140. struct dtd_sandbox_pmic_test {
  141. \tbool\t\tlow_power;
  142. \tfdt64_t\t\treg[2];
  143. };
  144. struct dtd_sandbox_spl_test {
  145. \tbool\t\tboolval;
  146. \tunsigned char\tbytearray[3];
  147. \tunsigned char\tbyteval;
  148. \tfdt32_t\t\tintarray[4];
  149. \tfdt32_t\t\tintval;
  150. \tunsigned char\tlongbytearray[9];
  151. \tunsigned char\tnotstring[5];
  152. \tconst char *\tstringarray[3];
  153. \tconst char *\tstringval;
  154. };
  155. struct dtd_sandbox_spl_test_2 {
  156. };
  157. ''', data)
  158. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  159. with open(output) as infile:
  160. data = infile.read()
  161. self._CheckStrings(C_HEADER + '''
  162. static struct dtd_sandbox_spl_test dtv_spl_test = {
  163. \t.bytearray\t\t= {0x6, 0x0, 0x0},
  164. \t.byteval\t\t= 0x5,
  165. \t.intval\t\t\t= 0x1,
  166. \t.notstring\t\t= {0x20, 0x21, 0x22, 0x10, 0x0},
  167. \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
  168. \t\t0x11},
  169. \t.stringval\t\t= "message",
  170. \t.boolval\t\t= true,
  171. \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
  172. \t.stringarray\t\t= {"multi-word", "message", ""},
  173. };
  174. U_BOOT_DEVICE(spl_test) = {
  175. \t.name\t\t= "sandbox_spl_test",
  176. \t.platdata\t= &dtv_spl_test,
  177. \t.platdata_size\t= sizeof(dtv_spl_test),
  178. };
  179. static struct dtd_sandbox_spl_test dtv_spl_test2 = {
  180. \t.bytearray\t\t= {0x1, 0x23, 0x34},
  181. \t.byteval\t\t= 0x8,
  182. \t.intval\t\t\t= 0x3,
  183. \t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  184. \t\t0x0},
  185. \t.stringval\t\t= "message2",
  186. \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
  187. \t.stringarray\t\t= {"another", "multi-word", "message"},
  188. };
  189. U_BOOT_DEVICE(spl_test2) = {
  190. \t.name\t\t= "sandbox_spl_test",
  191. \t.platdata\t= &dtv_spl_test2,
  192. \t.platdata_size\t= sizeof(dtv_spl_test2),
  193. };
  194. static struct dtd_sandbox_spl_test dtv_spl_test3 = {
  195. \t.stringarray\t\t= {"one", "", ""},
  196. };
  197. U_BOOT_DEVICE(spl_test3) = {
  198. \t.name\t\t= "sandbox_spl_test",
  199. \t.platdata\t= &dtv_spl_test3,
  200. \t.platdata_size\t= sizeof(dtv_spl_test3),
  201. };
  202. static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
  203. };
  204. U_BOOT_DEVICE(spl_test4) = {
  205. \t.name\t\t= "sandbox_spl_test_2",
  206. \t.platdata\t= &dtv_spl_test4,
  207. \t.platdata_size\t= sizeof(dtv_spl_test4),
  208. };
  209. static struct dtd_sandbox_i2c_test dtv_i2c_at_0 = {
  210. };
  211. U_BOOT_DEVICE(i2c_at_0) = {
  212. \t.name\t\t= "sandbox_i2c_test",
  213. \t.platdata\t= &dtv_i2c_at_0,
  214. \t.platdata_size\t= sizeof(dtv_i2c_at_0),
  215. };
  216. static struct dtd_sandbox_pmic_test dtv_pmic_at_9 = {
  217. \t.low_power\t\t= true,
  218. \t.reg\t\t\t= {0x9, 0x0},
  219. };
  220. U_BOOT_DEVICE(pmic_at_9) = {
  221. \t.name\t\t= "sandbox_pmic_test",
  222. \t.platdata\t= &dtv_pmic_at_9,
  223. \t.platdata_size\t= sizeof(dtv_pmic_at_9),
  224. };
  225. ''', data)
  226. def test_phandle(self):
  227. """Test output from a node containing a phandle reference"""
  228. dtb_file = get_dtb_file('dtoc_test_phandle.dts')
  229. output = tools.GetOutputFilename('output')
  230. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  231. with open(output) as infile:
  232. data = infile.read()
  233. self._CheckStrings(HEADER + '''
  234. struct dtd_source {
  235. \tstruct phandle_2_arg clocks[4];
  236. };
  237. struct dtd_target {
  238. \tfdt32_t\t\tintval;
  239. };
  240. ''', data)
  241. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  242. with open(output) as infile:
  243. data = infile.read()
  244. self._CheckStrings(C_HEADER + '''
  245. static struct dtd_target dtv_phandle_target = {
  246. \t.intval\t\t\t= 0x0,
  247. };
  248. U_BOOT_DEVICE(phandle_target) = {
  249. \t.name\t\t= "target",
  250. \t.platdata\t= &dtv_phandle_target,
  251. \t.platdata_size\t= sizeof(dtv_phandle_target),
  252. };
  253. static struct dtd_target dtv_phandle2_target = {
  254. \t.intval\t\t\t= 0x1,
  255. };
  256. U_BOOT_DEVICE(phandle2_target) = {
  257. \t.name\t\t= "target",
  258. \t.platdata\t= &dtv_phandle2_target,
  259. \t.platdata_size\t= sizeof(dtv_phandle2_target),
  260. };
  261. static struct dtd_target dtv_phandle3_target = {
  262. \t.intval\t\t\t= 0x2,
  263. };
  264. U_BOOT_DEVICE(phandle3_target) = {
  265. \t.name\t\t= "target",
  266. \t.platdata\t= &dtv_phandle3_target,
  267. \t.platdata_size\t= sizeof(dtv_phandle3_target),
  268. };
  269. static struct dtd_source dtv_phandle_source = {
  270. \t.clocks\t\t\t= {
  271. \t\t\t{&dtv_phandle_target, {}},
  272. \t\t\t{&dtv_phandle2_target, {11}},
  273. \t\t\t{&dtv_phandle3_target, {12, 13}},
  274. \t\t\t{&dtv_phandle_target, {}},},
  275. };
  276. U_BOOT_DEVICE(phandle_source) = {
  277. \t.name\t\t= "source",
  278. \t.platdata\t= &dtv_phandle_source,
  279. \t.platdata_size\t= sizeof(dtv_phandle_source),
  280. };
  281. ''', data)
  282. def test_aliases(self):
  283. """Test output from a node with multiple compatible strings"""
  284. dtb_file = get_dtb_file('dtoc_test_aliases.dts')
  285. output = tools.GetOutputFilename('output')
  286. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  287. with open(output) as infile:
  288. data = infile.read()
  289. self._CheckStrings(HEADER + '''
  290. struct dtd_compat1 {
  291. \tfdt32_t\t\tintval;
  292. };
  293. #define dtd_compat2_1_fred dtd_compat1
  294. #define dtd_compat3 dtd_compat1
  295. ''', data)
  296. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  297. with open(output) as infile:
  298. data = infile.read()
  299. self._CheckStrings(C_HEADER + '''
  300. static struct dtd_compat1 dtv_spl_test = {
  301. \t.intval\t\t\t= 0x1,
  302. };
  303. U_BOOT_DEVICE(spl_test) = {
  304. \t.name\t\t= "compat1",
  305. \t.platdata\t= &dtv_spl_test,
  306. \t.platdata_size\t= sizeof(dtv_spl_test),
  307. };
  308. ''', data)
  309. def test_addresses64(self):
  310. """Test output from a node with a 'reg' property with na=2, ns=2"""
  311. dtb_file = get_dtb_file('dtoc_test_addr64.dts')
  312. output = tools.GetOutputFilename('output')
  313. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  314. with open(output) as infile:
  315. data = infile.read()
  316. self._CheckStrings(HEADER + '''
  317. struct dtd_test1 {
  318. \tfdt64_t\t\treg[2];
  319. };
  320. struct dtd_test2 {
  321. \tfdt64_t\t\treg[2];
  322. };
  323. struct dtd_test3 {
  324. \tfdt64_t\t\treg[4];
  325. };
  326. ''', data)
  327. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  328. with open(output) as infile:
  329. data = infile.read()
  330. self._CheckStrings(C_HEADER + '''
  331. static struct dtd_test1 dtv_test1 = {
  332. \t.reg\t\t\t= {0x1234, 0x5678},
  333. };
  334. U_BOOT_DEVICE(test1) = {
  335. \t.name\t\t= "test1",
  336. \t.platdata\t= &dtv_test1,
  337. \t.platdata_size\t= sizeof(dtv_test1),
  338. };
  339. static struct dtd_test2 dtv_test2 = {
  340. \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654},
  341. };
  342. U_BOOT_DEVICE(test2) = {
  343. \t.name\t\t= "test2",
  344. \t.platdata\t= &dtv_test2,
  345. \t.platdata_size\t= sizeof(dtv_test2),
  346. };
  347. static struct dtd_test3 dtv_test3 = {
  348. \t.reg\t\t\t= {0x1234567890123456, 0x9876543210987654, 0x2, 0x3},
  349. };
  350. U_BOOT_DEVICE(test3) = {
  351. \t.name\t\t= "test3",
  352. \t.platdata\t= &dtv_test3,
  353. \t.platdata_size\t= sizeof(dtv_test3),
  354. };
  355. ''', data)
  356. def test_addresses32(self):
  357. """Test output from a node with a 'reg' property with na=1, ns=1"""
  358. dtb_file = get_dtb_file('dtoc_test_addr32.dts')
  359. output = tools.GetOutputFilename('output')
  360. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  361. with open(output) as infile:
  362. data = infile.read()
  363. self._CheckStrings(HEADER + '''
  364. struct dtd_test1 {
  365. \tfdt32_t\t\treg[2];
  366. };
  367. struct dtd_test2 {
  368. \tfdt32_t\t\treg[4];
  369. };
  370. ''', data)
  371. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  372. with open(output) as infile:
  373. data = infile.read()
  374. self._CheckStrings(C_HEADER + '''
  375. static struct dtd_test1 dtv_test1 = {
  376. \t.reg\t\t\t= {0x1234, 0x5678},
  377. };
  378. U_BOOT_DEVICE(test1) = {
  379. \t.name\t\t= "test1",
  380. \t.platdata\t= &dtv_test1,
  381. \t.platdata_size\t= sizeof(dtv_test1),
  382. };
  383. static struct dtd_test2 dtv_test2 = {
  384. \t.reg\t\t\t= {0x12345678, 0x98765432, 0x2, 0x3},
  385. };
  386. U_BOOT_DEVICE(test2) = {
  387. \t.name\t\t= "test2",
  388. \t.platdata\t= &dtv_test2,
  389. \t.platdata_size\t= sizeof(dtv_test2),
  390. };
  391. ''', data)
  392. def test_addresses64_32(self):
  393. """Test output from a node with a 'reg' property with na=2, ns=1"""
  394. dtb_file = get_dtb_file('dtoc_test_addr64_32.dts')
  395. output = tools.GetOutputFilename('output')
  396. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  397. with open(output) as infile:
  398. data = infile.read()
  399. self._CheckStrings(HEADER + '''
  400. struct dtd_test1 {
  401. \tfdt64_t\t\treg[2];
  402. };
  403. struct dtd_test2 {
  404. \tfdt64_t\t\treg[2];
  405. };
  406. struct dtd_test3 {
  407. \tfdt64_t\t\treg[4];
  408. };
  409. ''', data)
  410. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  411. with open(output) as infile:
  412. data = infile.read()
  413. self._CheckStrings(C_HEADER + '''
  414. static struct dtd_test1 dtv_test1 = {
  415. \t.reg\t\t\t= {0x123400000000, 0x5678},
  416. };
  417. U_BOOT_DEVICE(test1) = {
  418. \t.name\t\t= "test1",
  419. \t.platdata\t= &dtv_test1,
  420. \t.platdata_size\t= sizeof(dtv_test1),
  421. };
  422. static struct dtd_test2 dtv_test2 = {
  423. \t.reg\t\t\t= {0x1234567890123456, 0x98765432},
  424. };
  425. U_BOOT_DEVICE(test2) = {
  426. \t.name\t\t= "test2",
  427. \t.platdata\t= &dtv_test2,
  428. \t.platdata_size\t= sizeof(dtv_test2),
  429. };
  430. static struct dtd_test3 dtv_test3 = {
  431. \t.reg\t\t\t= {0x1234567890123456, 0x98765432, 0x2, 0x3},
  432. };
  433. U_BOOT_DEVICE(test3) = {
  434. \t.name\t\t= "test3",
  435. \t.platdata\t= &dtv_test3,
  436. \t.platdata_size\t= sizeof(dtv_test3),
  437. };
  438. ''', data)
  439. def test_addresses32_64(self):
  440. """Test output from a node with a 'reg' property with na=1, ns=2"""
  441. dtb_file = get_dtb_file('dtoc_test_addr32_64.dts')
  442. output = tools.GetOutputFilename('output')
  443. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  444. with open(output) as infile:
  445. data = infile.read()
  446. self._CheckStrings(HEADER + '''
  447. struct dtd_test1 {
  448. \tfdt64_t\t\treg[2];
  449. };
  450. struct dtd_test2 {
  451. \tfdt64_t\t\treg[2];
  452. };
  453. struct dtd_test3 {
  454. \tfdt64_t\t\treg[4];
  455. };
  456. ''', data)
  457. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  458. with open(output) as infile:
  459. data = infile.read()
  460. self._CheckStrings(C_HEADER + '''
  461. static struct dtd_test1 dtv_test1 = {
  462. \t.reg\t\t\t= {0x1234, 0x567800000000},
  463. };
  464. U_BOOT_DEVICE(test1) = {
  465. \t.name\t\t= "test1",
  466. \t.platdata\t= &dtv_test1,
  467. \t.platdata_size\t= sizeof(dtv_test1),
  468. };
  469. static struct dtd_test2 dtv_test2 = {
  470. \t.reg\t\t\t= {0x12345678, 0x9876543210987654},
  471. };
  472. U_BOOT_DEVICE(test2) = {
  473. \t.name\t\t= "test2",
  474. \t.platdata\t= &dtv_test2,
  475. \t.platdata_size\t= sizeof(dtv_test2),
  476. };
  477. static struct dtd_test3 dtv_test3 = {
  478. \t.reg\t\t\t= {0x12345678, 0x9876543210987654, 0x2, 0x3},
  479. };
  480. U_BOOT_DEVICE(test3) = {
  481. \t.name\t\t= "test3",
  482. \t.platdata\t= &dtv_test3,
  483. \t.platdata_size\t= sizeof(dtv_test3),
  484. };
  485. ''', data)