1*4882a593Smuzhiyun#!/usr/bin/env python2 2*4882a593Smuzhiyun# 3*4882a593Smuzhiyun# Copyright (C) 2016 Google, Inc 4*4882a593Smuzhiyun# Written by Simon Glass <sjg@chromium.org> 5*4882a593Smuzhiyun# 6*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0+ 7*4882a593Smuzhiyun# 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun"""Device tree to C tool 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunThis tool converts a device tree binary file (.dtb) into two C files. The 12*4882a593Smuzhiyunindent is to allow a C program to access data from the device tree without 13*4882a593Smuzhiyunhaving to link against libfdt. By putting the data from the device tree into 14*4882a593SmuzhiyunC structures, normal C code can be used. This helps to reduce the size of the 15*4882a593Smuzhiyuncompiled program. 16*4882a593Smuzhiyun 17*4882a593SmuzhiyunDtoc produces two output files: 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun dt-structs.h - contains struct definitions 20*4882a593Smuzhiyun dt-platdata.c - contains data from the device tree using the struct 21*4882a593Smuzhiyun definitions, as well as U-Boot driver definitions. 22*4882a593Smuzhiyun 23*4882a593SmuzhiyunThis tool is used in U-Boot to provide device tree data to SPL without 24*4882a593Smuzhiyunincreasing the code size of SPL. This supports the CONFIG_SPL_OF_PLATDATA 25*4882a593Smuzhiyunoptions. For more information about the use of this options and tool please 26*4882a593Smuzhiyunsee doc/driver-model/of-plat.txt 27*4882a593Smuzhiyun""" 28*4882a593Smuzhiyun 29*4882a593Smuzhiyunfrom optparse import OptionParser 30*4882a593Smuzhiyunimport os 31*4882a593Smuzhiyunimport sys 32*4882a593Smuzhiyunimport unittest 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun# Bring in the patman libraries 35*4882a593Smuzhiyunour_path = os.path.dirname(os.path.realpath(__file__)) 36*4882a593Smuzhiyunsys.path.append(os.path.join(our_path, '../patman')) 37*4882a593Smuzhiyun 38*4882a593Smuzhiyunimport dtb_platdata 39*4882a593Smuzhiyun 40*4882a593Smuzhiyundef run_tests(): 41*4882a593Smuzhiyun """Run all the test we have for dtoc""" 42*4882a593Smuzhiyun import test_dtoc 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun result = unittest.TestResult() 45*4882a593Smuzhiyun sys.argv = [sys.argv[0]] 46*4882a593Smuzhiyun for module in (test_dtoc.TestDtoc,): 47*4882a593Smuzhiyun suite = unittest.TestLoader().loadTestsFromTestCase(module) 48*4882a593Smuzhiyun suite.run(result) 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun print result 51*4882a593Smuzhiyun for _, err in result.errors: 52*4882a593Smuzhiyun print err 53*4882a593Smuzhiyun for _, err in result.failures: 54*4882a593Smuzhiyun print err 55*4882a593Smuzhiyun 56*4882a593Smuzhiyunif __name__ != '__main__': 57*4882a593Smuzhiyun sys.exit(1) 58*4882a593Smuzhiyun 59*4882a593Smuzhiyunparser = OptionParser() 60*4882a593Smuzhiyunparser.add_option('-d', '--dtb-file', action='store', 61*4882a593Smuzhiyun help='Specify the .dtb input file') 62*4882a593Smuzhiyunparser.add_option('--include-disabled', action='store_true', 63*4882a593Smuzhiyun help='Include disabled nodes') 64*4882a593Smuzhiyunparser.add_option('-o', '--output', action='store', default='-', 65*4882a593Smuzhiyun help='Select output filename') 66*4882a593Smuzhiyunparser.add_option('-t', '--test', action='store_true', dest='test', 67*4882a593Smuzhiyun default=False, help='run tests') 68*4882a593Smuzhiyun(options, args) = parser.parse_args() 69*4882a593Smuzhiyun 70*4882a593Smuzhiyun# Run our meagre tests 71*4882a593Smuzhiyunif options.test: 72*4882a593Smuzhiyun run_tests() 73*4882a593Smuzhiyun 74*4882a593Smuzhiyunelse: 75*4882a593Smuzhiyun dtb_platdata.run_steps(args, options.dtb_file, options.include_disabled, 76*4882a593Smuzhiyun options.output) 77