-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlinux-config-from-dt
executable file
·172 lines (140 loc) · 3.98 KB
/
linux-config-from-dt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/perl -w
#
# © Copyright 2014 by Geert Uytterhoeven
#
# This file is subject to the terms and conditions of the GNU General Public
# License.
#
sub usage()
{
my $name = $0;
$name =~ s@.*/@@g;
die <<EOF;
Usage: $name [options] [dts-file | dtb-file]...
Valid options:
-h, --help Display this help
-d, --debug Enable debug mode
-v, --verbose Enable verbose mode
EOF
}
$debug = 0; # FIXME
$verbose = 1; # FIXME
%compat_done = (); # compatible-properties we've found a driver for
%compat_seen = (); # compatible-properties we've already seen before
%source_seen = (); # source files we need to compile
# FIXME %symbols = (); # config symbols we must enable
# scripts/dtc/dtc -I dts -O dts <file>.dts
# scripts/dtc/dtc -I dtb -O dts <file>.dtb
sub handle_compatible()
{
my $compatible = shift;
my $compat;
$compatible =~ s/^"//;
$compatible =~ s/"$//;
# Old dtc returned one or more compatible values wrapped by double
# quotes and separated by a comma and a space.
$compatible =~ s/\", "/ /g;
# New dtc returns a single string wrapped by double quotes, with
# multiple compatible values separated by the literal "\0".
$compatible =~ s/\\0/ /g;
my @compatibles = split(" ", $compatible);
# Add the first property without vendor prefix for modalias matching
if (($generic) = $compatibles[0] =~ /,(.*)"/) {
push @compatibles, "$generic";
}
for $compat (@compatibles) {
# Return if we already found a driver for it
return if (exists($compat_done{$compat}));
# Skip to next alternative if we couldn't find a driver for it
next if (exists($compat_seen{$compat}));
print "$compat " if $verbose;
$compat_seen{$compat} = 1;
# Find source file that contains a driver for this value
my $files;
if ($compat =~ /,/) {
# Vendor-specific compatible properties may be handled
# all over the tree
$files = '"*.c"';
} else {
# Limit generic compatible properties to drivers
$files = '"drivers/*.c" "sound/*.c"';
}
my $sources = `git grep -l \\"$compat\\" -- $files`;
chomp($sources);
if ($sources eq "") {
print "was not found, continuing...\n" if $verbose;
next;
}
print "is provided by:\n" if $verbose;
$compat_done{$compat} = 1;
my @sources = split("\n", $sources);
for $source (@sources) {
print "\t$source\n" if $verbose;
if (exists($source_seen{$source})) {
# Return if we already handled this source file
print "\t\tseen\n";
return;
}
$source_seen{$source} = 1;
# Find the Kconfig symbol that enables the driver
my ($dir, $base) = $source =~ m{(.*)/(.*).c};
print "\tdir = $dir, base = $base\n" if $debug;
my $lines = `git grep -w "$base.o" -- $dir/Makefile $dir/Kbuild`;
my @lines = split("\n", $lines);
for $line (@lines) {
print "\tline = $line" if $debug;
my ($config) = $line =~ m{(CONFIG_[A-Za-z0-9_]*)};
if (!defined($config)) {
print "\t\tNo config symbol\n";
next;
}
print "\t\tneeds $config\n";
# FIXME Find dependencies for the config symbol
}
}
return;
}
print "No match found for $compatible\n";
}
sub read_dts()
{
my $file = shift;
my $type = "dts"; # Assume DTS by default
my $line;
$type = "dtb" if $file =~ /\.dtb$/;
open(DTS, "dtc -I $type -O dts $file |") or die "Cannot run dtc";
while ($line = <DTS>) {
chomp($line);
next if not $line =~ /compatible =/;
# Extract all compatible entries for this device
$line =~ s/^\t*//;
print "$line\n" if $debug;
$line =~ s/^compatible = //;
$line =~ s/;$//;
# Look'em up
&handle_compatible($line);
}
close(DTS);
}
while (defined($ARGV[0])) {
$option = $ARGV[0];
last if not $option =~ /^-/;
if ($option eq '-h' or $option eq '--help') {
&usage();
} elsif ($option eq '-d' or $option eq '--debug') {
$debug = 1;
} elsif ($option eq '-v' or $option eq '--verbose') {
$verbose = 1;
} elsif ($option eq '--') {
shift @ARGV;
last;
} else {
print STDERR "Unknown option $option\n";
&usage();
}
shift @ARGV;
}
&usage if ($#ARGV < 0);
for $file (@ARGV) {
&read_dts($file);
}