From a13dee8d19a9e4c5a9406a2ac010c3cde3254197 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Wed, 19 May 2021 15:59:33 +0200 Subject: [PATCH] tool/dts: for extracting content from dts files The new tool eases the inspection and pruning of device-tree source files. Fixes #4165 --- tool/dts/README | 26 +++ tool/dts/extract | 470 +++++++++++++++++++++++++++++++++++++++++++++++ tool/dts/parse | 261 ++++++++++++++++++++++++++ 3 files changed, 757 insertions(+) create mode 100644 tool/dts/README create mode 100755 tool/dts/extract create mode 100755 tool/dts/parse diff --git a/tool/dts/README b/tool/dts/README new file mode 100644 index 0000000000..6503c679de --- /dev/null +++ b/tool/dts/README @@ -0,0 +1,26 @@ + + Utilities for extracting information from device-tree sources + +Device trees are prominently used in the Linux world for describing the +structure of SoCs as for the parametrisation of device drivers. The utilities +found in this directory ease the extraction of interesting information from +device-tree files. + +The utilities found in this directory operate on device-tree source (dts) +files as opposed to device-tree binaries (dtb) because dts files contain +information about the inter-dependencies between nodes in textual form +(labels) instead of mere numbers (phandles). + +In the Linux source tree, dts files can be found at _arch//boot/dts/_. +Most dts files leverage the C preprocessor to include fragments in the form of +dtsi files. To generate the complete device-tree source information for a +given dts file, the C preprocessor must be invoked as follows: + +! cpp -Iinclude -x assembler-with-cpp -P + +The _parse_ tool generates a syntax tree from a dts file. It is not meant to +be invoked directly but it is used as a helper by other tools. + +The _extract_ tool can be used to query structural information from a dts +file and to prune a device tree specifically for a subset of devices. For +usage information, execute the tool without arguments. diff --git a/tool/dts/extract b/tool/dts/extract new file mode 100755 index 0000000000..7eba61e77d --- /dev/null +++ b/tool/dts/extract @@ -0,0 +1,470 @@ +#!/usr/bin/tclsh + +# +# \brief Extract subtree from device-tree source +# \author Norman Feske +# \date 2021-05-17 +# + +## +# Return true if command-line switch was specified +# +proc get_cmd_switch { arg_name } { + global argv + return [expr [lsearch $argv $arg_name] >= 0] +} + + +## +# Return command-line argument value +# +# If a argument name is specified multiple times, a +# list of argument values is returned. +# +proc get_cmd_arg { arg_name default_value } { + global argv + + # find argument name in argv list + set arg_idx_list [lsearch -all $argv $arg_name] + + if {[llength $arg_idx_list] == 0} { return $default_value } + + set result {} + foreach arg_idx $arg_idx_list { + set next_idx [expr $arg_idx + 1] + + # stop if argv ends with the argument name + if {$next_idx >= [llength $argv]} continue + + # return list element following the argument name + lappend result [lindex $argv $next_idx] + } + + # if argument occurred only once, return its value + if {[llength $result] == 1} { return [lindex $result 0] } + + # if argument occurred multiple times, contain list of arguments + return $result +} + + +################################################# +## Read input and fill internal representation ## +################################################# + +## +# Find location of 'parse' +# +# We expect to find 'parse' in the same directory as we are located. The +# question is: Where are we? +## +proc parse_file { } { + global argv0 + + set path $argv0 + + if {[file type $path] == "link"} { + set path [file readlink $path] } + + set parse_file "[file dirname $path]/parse" + + if {![file exists $parse_file]} { + puts stderr "Error: Could not find 'parse' in '$path'." + exit -1 + } + return $parse_file +} + + +set input_source [lindex $argv end] +if {[catch { + set tokens [exec [parse_file] $input_source] +}]} { + foreach line { + "" + "Extract subtree from device-tree source" + "\n usage: extract \[options\] " + "" + "Supported options are:" + "" + " --nodes Print list of DTS nodes" + " --labels Print list of labels with their DTS paths" + " --references Print referenced labels for each DTS node" + " --dot-graph Generate dot file for graphviz" + " --select