1*daae0a01SMasahiro Yamada#!/usr/bin/env python2 269f2ed77SSimon Glass# 369f2ed77SSimon Glass# Copyright (C) 2016 Google, Inc 469f2ed77SSimon Glass# Written by Simon Glass <sjg@chromium.org> 569f2ed77SSimon Glass# 669f2ed77SSimon Glass# SPDX-License-Identifier: GPL-2.0+ 769f2ed77SSimon Glass# 869f2ed77SSimon Glass 914f5acfcSSimon Glass"""Device tree to C tool 1014f5acfcSSimon Glass 1114f5acfcSSimon GlassThis tool converts a device tree binary file (.dtb) into two C files. The 1214f5acfcSSimon Glassindent is to allow a C program to access data from the device tree without 1314f5acfcSSimon Glasshaving to link against libfdt. By putting the data from the device tree into 1414f5acfcSSimon GlassC structures, normal C code can be used. This helps to reduce the size of the 1514f5acfcSSimon Glasscompiled program. 1614f5acfcSSimon Glass 1714f5acfcSSimon GlassDtoc produces two output files: 1814f5acfcSSimon Glass 1914f5acfcSSimon Glass dt-structs.h - contains struct definitions 2014f5acfcSSimon Glass dt-platdata.c - contains data from the device tree using the struct 2114f5acfcSSimon Glass definitions, as well as U-Boot driver definitions. 2214f5acfcSSimon Glass 2314f5acfcSSimon GlassThis tool is used in U-Boot to provide device tree data to SPL without 2414f5acfcSSimon Glassincreasing the code size of SPL. This supports the CONFIG_SPL_OF_PLATDATA 2514f5acfcSSimon Glassoptions. For more information about the use of this options and tool please 2614f5acfcSSimon Glasssee doc/driver-model/of-plat.txt 2714f5acfcSSimon Glass""" 2814f5acfcSSimon Glass 297581c01aSSimon Glassfrom optparse import OptionParser 3069f2ed77SSimon Glassimport os 3169f2ed77SSimon Glassimport sys 32c0791928SSimon Glassimport unittest 3369f2ed77SSimon Glass 3469f2ed77SSimon Glass# Bring in the patman libraries 3569f2ed77SSimon Glassour_path = os.path.dirname(os.path.realpath(__file__)) 3669f2ed77SSimon Glasssys.path.append(os.path.join(our_path, '../patman')) 3769f2ed77SSimon Glass 387581c01aSSimon Glassimport dtb_platdata 3969f2ed77SSimon Glass 40c0791928SSimon Glassdef run_tests(): 41c0791928SSimon Glass """Run all the test we have for dtoc""" 42c0791928SSimon Glass import test_dtoc 4369f2ed77SSimon Glass 44c0791928SSimon Glass result = unittest.TestResult() 45c0791928SSimon Glass sys.argv = [sys.argv[0]] 46c0791928SSimon Glass for module in (test_dtoc.TestDtoc,): 47c0791928SSimon Glass suite = unittest.TestLoader().loadTestsFromTestCase(module) 48c0791928SSimon Glass suite.run(result) 49c0791928SSimon Glass 50c0791928SSimon Glass print result 51c0791928SSimon Glass for _, err in result.errors: 52c0791928SSimon Glass print err 53c0791928SSimon Glass for _, err in result.failures: 54c0791928SSimon Glass print err 55c0791928SSimon Glass 56c0791928SSimon Glassif __name__ != '__main__': 57c0791928SSimon Glass sys.exit(1) 5869f2ed77SSimon Glass 5969f2ed77SSimon Glassparser = OptionParser() 6069f2ed77SSimon Glassparser.add_option('-d', '--dtb-file', action='store', 6169f2ed77SSimon Glass help='Specify the .dtb input file') 6269f2ed77SSimon Glassparser.add_option('--include-disabled', action='store_true', 6369f2ed77SSimon Glass help='Include disabled nodes') 6469f2ed77SSimon Glassparser.add_option('-o', '--output', action='store', default='-', 6569f2ed77SSimon Glass help='Select output filename') 66c0791928SSimon Glassparser.add_option('-t', '--test', action='store_true', dest='test', 67c0791928SSimon Glass default=False, help='run tests') 6869f2ed77SSimon Glass(options, args) = parser.parse_args() 6969f2ed77SSimon Glass 70c0791928SSimon Glass# Run our meagre tests 71c0791928SSimon Glassif options.test: 72c0791928SSimon Glass run_tests() 73c0791928SSimon Glass 74c0791928SSimon Glasselse: 75fa0ea5b0SSimon Glass dtb_platdata.run_steps(args, options.dtb_file, options.include_disabled, 76fa0ea5b0SSimon Glass options.output) 77