PageRenderTime 126ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/dtoc/test_dtoc.py

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