test_dtoc.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. def get_dtb_file(dts_fname):
  23. """Compile a .dts file to a .dtb
  24. Args:
  25. dts_fname: Filename of .dts file in the current directory
  26. Returns:
  27. Filename of compiled file in output directory
  28. """
  29. return fdt_util.EnsureCompiled(os.path.join(our_path, dts_fname))
  30. class TestDtoc(unittest.TestCase):
  31. """Tests for dtoc"""
  32. @classmethod
  33. def setUpClass(cls):
  34. tools.PrepareOutputDir(None)
  35. @classmethod
  36. def tearDownClass(cls):
  37. tools._RemoveOutputDir()
  38. def test_name(self):
  39. """Test conversion of device tree names to C identifiers"""
  40. self.assertEqual('serial_at_0x12', conv_name_to_c('serial@0x12'))
  41. self.assertEqual('vendor_clock_frequency',
  42. conv_name_to_c('vendor,clock-frequency'))
  43. self.assertEqual('rockchip_rk3399_sdhci_5_1',
  44. conv_name_to_c('rockchip,rk3399-sdhci-5.1'))
  45. def test_tab_to(self):
  46. """Test operation of tab_to() function"""
  47. self.assertEqual('fred ', tab_to(0, 'fred'))
  48. self.assertEqual('fred\t', tab_to(1, 'fred'))
  49. self.assertEqual('fred was here ', tab_to(1, 'fred was here'))
  50. self.assertEqual('fred was here\t\t', tab_to(3, 'fred was here'))
  51. self.assertEqual('exactly8 ', tab_to(1, 'exactly8'))
  52. self.assertEqual('exactly8\t', tab_to(2, 'exactly8'))
  53. def test_get_value(self):
  54. """Test operation of get_value() function"""
  55. self.assertEqual('0x45',
  56. get_value(fdt.TYPE_INT, struct.pack('>I', 0x45)))
  57. self.assertEqual('0x45',
  58. get_value(fdt.TYPE_BYTE, struct.pack('<I', 0x45)))
  59. self.assertEqual('0x0',
  60. get_value(fdt.TYPE_BYTE, struct.pack('>I', 0x45)))
  61. self.assertEqual('"test"', get_value(fdt.TYPE_STRING, 'test'))
  62. self.assertEqual('true', get_value(fdt.TYPE_BOOL, None))
  63. def test_get_compat_name(self):
  64. """Test operation of get_compat_name() function"""
  65. Prop = collections.namedtuple('Prop', ['value'])
  66. Node = collections.namedtuple('Node', ['props'])
  67. prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1'])
  68. node = Node({'compatible': prop})
  69. self.assertEqual(('rockchip_rk3399_sdhci_5_1', ['arasan_sdhci_5_1']),
  70. get_compat_name(node))
  71. prop = Prop(['rockchip,rk3399-sdhci-5.1'])
  72. node = Node({'compatible': prop})
  73. self.assertEqual(('rockchip_rk3399_sdhci_5_1', []),
  74. get_compat_name(node))
  75. prop = Prop(['rockchip,rk3399-sdhci-5.1', 'arasan,sdhci-5.1', 'third'])
  76. node = Node({'compatible': prop})
  77. self.assertEqual(('rockchip_rk3399_sdhci_5_1',
  78. ['arasan_sdhci_5_1', 'third']),
  79. get_compat_name(node))
  80. def test_empty_file(self):
  81. """Test output from a device tree file with no nodes"""
  82. dtb_file = get_dtb_file('dtoc_test_empty.dts')
  83. output = tools.GetOutputFilename('output')
  84. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  85. with open(output) as infile:
  86. lines = infile.read().splitlines()
  87. self.assertEqual(['#include <stdbool.h>', '#include <libfdt.h>'], lines)
  88. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  89. with open(output) as infile:
  90. lines = infile.read().splitlines()
  91. self.assertEqual(['#include <common.h>', '#include <dm.h>',
  92. '#include <dt-structs.h>', ''], lines)
  93. def test_simple(self):
  94. """Test output from some simple nodes with various types of data"""
  95. dtb_file = get_dtb_file('dtoc_test_simple.dts')
  96. output = tools.GetOutputFilename('output')
  97. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  98. with open(output) as infile:
  99. data = infile.read()
  100. self.assertEqual('''#include <stdbool.h>
  101. #include <libfdt.h>
  102. struct dtd_sandbox_spl_test {
  103. \tbool\t\tboolval;
  104. \tunsigned char\tbytearray[3];
  105. \tunsigned char\tbyteval;
  106. \tfdt32_t\t\tintarray[4];
  107. \tfdt32_t\t\tintval;
  108. \tunsigned char\tlongbytearray[9];
  109. \tconst char *\tstringarray[3];
  110. \tconst char *\tstringval;
  111. };
  112. struct dtd_sandbox_spl_test_2 {
  113. };
  114. ''', data)
  115. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  116. with open(output) as infile:
  117. data = infile.read()
  118. self.assertEqual('''#include <common.h>
  119. #include <dm.h>
  120. #include <dt-structs.h>
  121. static struct dtd_sandbox_spl_test dtv_spl_test = {
  122. \t.bytearray\t\t= {0x6, 0x0, 0x0},
  123. \t.byteval\t\t= 0x5,
  124. \t.intval\t\t\t= 0x1,
  125. \t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11},
  126. \t.stringval\t\t= "message",
  127. \t.boolval\t\t= true,
  128. \t.intarray\t\t= {0x2, 0x3, 0x4, 0x0},
  129. \t.stringarray\t\t= {"multi-word", "message", ""},
  130. };
  131. U_BOOT_DEVICE(spl_test) = {
  132. \t.name\t\t= "sandbox_spl_test",
  133. \t.platdata\t= &dtv_spl_test,
  134. \t.platdata_size\t= sizeof(dtv_spl_test),
  135. };
  136. static struct dtd_sandbox_spl_test dtv_spl_test2 = {
  137. \t.bytearray\t\t= {0x1, 0x23, 0x34},
  138. \t.byteval\t\t= 0x8,
  139. \t.intval\t\t\t= 0x3,
  140. \t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
  141. \t.stringval\t\t= "message2",
  142. \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
  143. \t.stringarray\t\t= {"another", "multi-word", "message"},
  144. };
  145. U_BOOT_DEVICE(spl_test2) = {
  146. \t.name\t\t= "sandbox_spl_test",
  147. \t.platdata\t= &dtv_spl_test2,
  148. \t.platdata_size\t= sizeof(dtv_spl_test2),
  149. };
  150. static struct dtd_sandbox_spl_test dtv_spl_test3 = {
  151. \t.stringarray\t\t= {"one", "", ""},
  152. };
  153. U_BOOT_DEVICE(spl_test3) = {
  154. \t.name\t\t= "sandbox_spl_test",
  155. \t.platdata\t= &dtv_spl_test3,
  156. \t.platdata_size\t= sizeof(dtv_spl_test3),
  157. };
  158. static struct dtd_sandbox_spl_test_2 dtv_spl_test4 = {
  159. };
  160. U_BOOT_DEVICE(spl_test4) = {
  161. \t.name\t\t= "sandbox_spl_test_2",
  162. \t.platdata\t= &dtv_spl_test4,
  163. \t.platdata_size\t= sizeof(dtv_spl_test4),
  164. };
  165. ''', data)
  166. def test_phandle(self):
  167. """Test output from a node containing a phandle reference"""
  168. dtb_file = get_dtb_file('dtoc_test_phandle.dts')
  169. output = tools.GetOutputFilename('output')
  170. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  171. with open(output) as infile:
  172. data = infile.read()
  173. self.assertEqual('''#include <stdbool.h>
  174. #include <libfdt.h>
  175. struct dtd_source {
  176. \tstruct phandle_2_cell clocks[1];
  177. };
  178. struct dtd_target {
  179. \tfdt32_t\t\tintval;
  180. };
  181. ''', data)
  182. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  183. with open(output) as infile:
  184. data = infile.read()
  185. self.assertEqual('''#include <common.h>
  186. #include <dm.h>
  187. #include <dt-structs.h>
  188. static struct dtd_target dtv_phandle_target = {
  189. \t.intval\t\t\t= 0x1,
  190. };
  191. U_BOOT_DEVICE(phandle_target) = {
  192. \t.name\t\t= "target",
  193. \t.platdata\t= &dtv_phandle_target,
  194. \t.platdata_size\t= sizeof(dtv_phandle_target),
  195. };
  196. static struct dtd_source dtv_phandle_source = {
  197. \t.clocks\t\t\t= {{&dtv_phandle_target, 1}},
  198. };
  199. U_BOOT_DEVICE(phandle_source) = {
  200. \t.name\t\t= "source",
  201. \t.platdata\t= &dtv_phandle_source,
  202. \t.platdata_size\t= sizeof(dtv_phandle_source),
  203. };
  204. ''', data)
  205. def test_aliases(self):
  206. """Test output from a node with multiple compatible strings"""
  207. dtb_file = get_dtb_file('dtoc_test_aliases.dts')
  208. output = tools.GetOutputFilename('output')
  209. dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  210. with open(output) as infile:
  211. data = infile.read()
  212. self.assertEqual('''#include <stdbool.h>
  213. #include <libfdt.h>
  214. struct dtd_compat1 {
  215. \tfdt32_t\t\tintval;
  216. };
  217. #define dtd_compat2_1_fred dtd_compat1
  218. #define dtd_compat3 dtd_compat1
  219. ''', data)
  220. dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  221. with open(output) as infile:
  222. data = infile.read()
  223. self.assertEqual('''#include <common.h>
  224. #include <dm.h>
  225. #include <dt-structs.h>
  226. static struct dtd_compat1 dtv_spl_test = {
  227. \t.intval\t\t\t= 0x1,
  228. };
  229. U_BOOT_DEVICE(spl_test) = {
  230. \t.name\t\t= "compat1",
  231. \t.platdata\t= &dtv_spl_test,
  232. \t.platdata_size\t= sizeof(dtv_spl_test),
  233. };
  234. ''', data)