xref: /openbmc/u-boot/tools/dtoc/dtoc.py (revision 7581c01a)
169f2ed77SSimon Glass#!/usr/bin/python
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
29*7581c01aSSimon Glassfrom optparse import OptionParser
3069f2ed77SSimon Glassimport os
3169f2ed77SSimon Glassimport sys
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
37*7581c01aSSimon Glassimport dtb_platdata
3869f2ed77SSimon Glass
3969f2ed77SSimon Glass
4069f2ed77SSimon Glassif __name__ != "__main__":
4169f2ed77SSimon Glass    pass
4269f2ed77SSimon Glass
4369f2ed77SSimon Glassparser = OptionParser()
4469f2ed77SSimon Glassparser.add_option('-d', '--dtb-file', action='store',
4569f2ed77SSimon Glass                  help='Specify the .dtb input file')
4669f2ed77SSimon Glassparser.add_option('--include-disabled', action='store_true',
4769f2ed77SSimon Glass                  help='Include disabled nodes')
4869f2ed77SSimon Glassparser.add_option('-o', '--output', action='store', default='-',
4969f2ed77SSimon Glass                  help='Select output filename')
5069f2ed77SSimon Glass(options, args) = parser.parse_args()
5169f2ed77SSimon Glass
5269f2ed77SSimon Glassif not args:
5369f2ed77SSimon Glass    raise ValueError('Please specify a command: struct, platdata')
5469f2ed77SSimon Glass
55*7581c01aSSimon Glassplat = dtb_platdata.DtbPlatdata(options.dtb_file, options)
5669f2ed77SSimon Glassplat.ScanDtb()
5769f2ed77SSimon Glassplat.ScanTree()
5869f2ed77SSimon Glassplat.SetupOutput(options.output)
5969f2ed77SSimon Glassstructs = plat.ScanStructs()
60439c947fSSimon Glassplat.ScanPhandles()
6169f2ed77SSimon Glass
6269f2ed77SSimon Glassfor cmd in args[0].split(','):
6369f2ed77SSimon Glass    if cmd == 'struct':
6469f2ed77SSimon Glass        plat.GenerateStructs(structs)
6569f2ed77SSimon Glass    elif cmd == 'platdata':
6669f2ed77SSimon Glass        plat.GenerateTables()
6769f2ed77SSimon Glass    else:
6869f2ed77SSimon Glass        raise ValueError("Unknown command '%s': (use: struct, platdata)" % cmd)
69