xref: /openbmc/u-boot/tools/dtoc/dtoc.py (revision cf033e04)
194b13bbaSMasahiro Yamada#!/usr/bin/env python2
283d290c5STom Rini# SPDX-License-Identifier: GPL-2.0+
369f2ed77SSimon Glass#
469f2ed77SSimon Glass# Copyright (C) 2016 Google, Inc
569f2ed77SSimon Glass# Written by Simon Glass <sjg@chromium.org>
669f2ed77SSimon Glass#
769f2ed77SSimon Glass
814f5acfcSSimon Glass"""Device tree to C tool
914f5acfcSSimon Glass
1014f5acfcSSimon GlassThis tool converts a device tree binary file (.dtb) into two C files. The
1114f5acfcSSimon Glassindent is to allow a C program to access data from the device tree without
1214f5acfcSSimon Glasshaving to link against libfdt. By putting the data from the device tree into
1314f5acfcSSimon GlassC structures, normal C code can be used. This helps to reduce the size of the
1414f5acfcSSimon Glasscompiled program.
1514f5acfcSSimon Glass
1614f5acfcSSimon GlassDtoc produces two output files:
1714f5acfcSSimon Glass
1814f5acfcSSimon Glass   dt-structs.h  - contains struct definitions
1914f5acfcSSimon Glass   dt-platdata.c - contains data from the device tree using the struct
2014f5acfcSSimon Glass                      definitions, as well as U-Boot driver definitions.
2114f5acfcSSimon Glass
2214f5acfcSSimon GlassThis tool is used in U-Boot to provide device tree data to SPL without
2314f5acfcSSimon Glassincreasing the code size of SPL. This supports the CONFIG_SPL_OF_PLATDATA
2414f5acfcSSimon Glassoptions. For more information about the use of this options and tool please
2514f5acfcSSimon Glasssee doc/driver-model/of-plat.txt
2614f5acfcSSimon Glass"""
2714f5acfcSSimon Glass
287581c01aSSimon Glassfrom optparse import OptionParser
2969f2ed77SSimon Glassimport os
3069f2ed77SSimon Glassimport sys
31c0791928SSimon Glassimport unittest
3269f2ed77SSimon Glass
3369f2ed77SSimon Glass# Bring in the patman libraries
3469f2ed77SSimon Glassour_path = os.path.dirname(os.path.realpath(__file__))
3569f2ed77SSimon Glasssys.path.append(os.path.join(our_path, '../patman'))
3669f2ed77SSimon Glass
37ed59e005SSimon Glass# Bring in the libfdt module
38ed59e005SSimon Glasssys.path.insert(0, 'scripts/dtc/pylibfdt')
39ed59e005SSimon Glasssys.path.insert(0, os.path.join(our_path,
40ed59e005SSimon Glass                '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
41ed59e005SSimon Glass
427581c01aSSimon Glassimport dtb_platdata
43ba765217SSimon Glassimport test_util
4469f2ed77SSimon Glass
453def0cf2SSimon Glassdef run_tests(args):
463def0cf2SSimon Glass    """Run all the test we have for dtoc
473def0cf2SSimon Glass
483def0cf2SSimon Glass    Args:
49dfe5f5b9SSimon Glass        args: List of positional args provided to dtoc. This can hold a test
50dfe5f5b9SSimon Glass            name to execute (as in 'dtoc -t test_empty_file', for example)
513def0cf2SSimon Glass    """
52c0791928SSimon Glass    import test_dtoc
5369f2ed77SSimon Glass
54c0791928SSimon Glass    result = unittest.TestResult()
55c0791928SSimon Glass    sys.argv = [sys.argv[0]]
563def0cf2SSimon Glass    test_name = args and args[0] or None
57c0791928SSimon Glass    for module in (test_dtoc.TestDtoc,):
583def0cf2SSimon Glass        if test_name:
593def0cf2SSimon Glass            try:
603def0cf2SSimon Glass                suite = unittest.TestLoader().loadTestsFromName(test_name, module)
613def0cf2SSimon Glass            except AttributeError:
623def0cf2SSimon Glass                continue
633def0cf2SSimon Glass        else:
64c0791928SSimon Glass            suite = unittest.TestLoader().loadTestsFromTestCase(module)
65c0791928SSimon Glass        suite.run(result)
66c0791928SSimon Glass
67c0791928SSimon Glass    print result
68c0791928SSimon Glass    for _, err in result.errors:
69c0791928SSimon Glass        print err
70c0791928SSimon Glass    for _, err in result.failures:
71c0791928SSimon Glass        print err
72c0791928SSimon Glass
73ba765217SSimon Glassdef RunTestCoverage():
74ba765217SSimon Glass    """Run the tests and check that we get 100% coverage"""
75ba765217SSimon Glass    sys.argv = [sys.argv[0]]
76ba765217SSimon Glass    test_util.RunTestCoverage('tools/dtoc/dtoc.py', '/dtoc.py',
77ba765217SSimon Glass            ['tools/patman/*.py', '*/fdt*', '*test*'], options.build_dir)
78ba765217SSimon Glass
79ba765217SSimon Glass
80c0791928SSimon Glassif __name__ != '__main__':
81c0791928SSimon Glass    sys.exit(1)
8269f2ed77SSimon Glass
8369f2ed77SSimon Glassparser = OptionParser()
84ba765217SSimon Glassparser.add_option('-B', '--build-dir', type='string', default='b',
85ba765217SSimon Glass        help='Directory containing the build output')
8669f2ed77SSimon Glassparser.add_option('-d', '--dtb-file', action='store',
8769f2ed77SSimon Glass                  help='Specify the .dtb input file')
8869f2ed77SSimon Glassparser.add_option('--include-disabled', action='store_true',
8969f2ed77SSimon Glass                  help='Include disabled nodes')
9069f2ed77SSimon Glassparser.add_option('-o', '--output', action='store', default='-',
9169f2ed77SSimon Glass                  help='Select output filename')
92*11ae93eeSSimon Glassparser.add_option('-P', '--processes', type=int,
93*11ae93eeSSimon Glass                  help='set number of processes to use for running tests')
94c0791928SSimon Glassparser.add_option('-t', '--test', action='store_true', dest='test',
95c0791928SSimon Glass                  default=False, help='run tests')
96ba765217SSimon Glassparser.add_option('-T', '--test-coverage', action='store_true',
97ba765217SSimon Glass                default=False, help='run tests and check for 100% coverage')
9869f2ed77SSimon Glass(options, args) = parser.parse_args()
9969f2ed77SSimon Glass
100c0791928SSimon Glass# Run our meagre tests
101c0791928SSimon Glassif options.test:
1023def0cf2SSimon Glass    run_tests(args)
103c0791928SSimon Glass
104ba765217SSimon Glasselif options.test_coverage:
105ba765217SSimon Glass    RunTestCoverage()
106ba765217SSimon Glass
107c0791928SSimon Glasselse:
108fa0ea5b0SSimon Glass    dtb_platdata.run_steps(args, options.dtb_file, options.include_disabled,
109fa0ea5b0SSimon Glass                           options.output)
110