]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - scripts/mkknlimg
BCM270X: Disable VEC unless vc4-kms-v3d is present.
[mirror_ubuntu-zesty-kernel.git] / scripts / mkknlimg
1 #!/usr/bin/env perl
2 # ----------------------------------------------------------------------
3 # mkknlimg by Phil Elwell for Raspberry Pi
4 # based on extract-ikconfig by Dick Streefland
5 #
6 # (c) 2009,2010 Dick Streefland <dick@streefland.net>
7 # (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
8 #
9 # Licensed under the terms of the GNU General Public License.
10 # ----------------------------------------------------------------------
11
12 use strict;
13 use warnings;
14 use integer;
15
16 use constant FLAG_PI => 0x01;
17 use constant FLAG_DTOK => 0x02;
18 use constant FLAG_DDTK => 0x04;
19 use constant FLAG_270X => 0x08;
20 use constant FLAG_283X => 0x10;
21
22 my $trailer_magic = 'RPTL';
23
24 my $tmpfile1 = "/tmp/mkknlimg_$$.1";
25 my $tmpfile2 = "/tmp/mkknlimg_$$.2";
26
27 my $dtok = 0;
28 my $ddtk = 0;
29 my $is_270x = 0;
30 my $is_283x = 0;
31
32 while (@ARGV && ($ARGV[0] =~ /^-/))
33 {
34 my $arg = shift(@ARGV);
35 if ($arg eq '--dtok')
36 {
37 $dtok = 1;
38 }
39 elsif ($arg eq '--ddtk')
40 {
41 $ddtk = 1;
42 }
43 elsif ($arg eq '--270x')
44 {
45 $is_270x = 1;
46 }
47 elsif ($arg eq '--283x')
48 {
49 $is_283x = 1;
50 }
51 else
52 {
53 print ("* Unknown option '$arg'\n");
54 usage();
55 }
56 }
57
58 usage() if (@ARGV != 2);
59
60 my $kernel_file = $ARGV[0];
61 my $out_file = $ARGV[1];
62
63 if (! -r $kernel_file)
64 {
65 print ("* File '$kernel_file' not found\n");
66 usage();
67 }
68
69 my $wanted_strings =
70 {
71 'bcm2708_fb' => FLAG_PI,
72 'brcm,bcm2835-mmc' => FLAG_PI,
73 'brcm,bcm2835-sdhost' => FLAG_PI,
74 'brcm,bcm2708-pinctrl' => FLAG_PI | FLAG_DTOK,
75 'brcm,bcm2835-gpio' => FLAG_PI | FLAG_DTOK,
76 'brcm,bcm2708' => FLAG_PI | FLAG_DTOK | FLAG_270X,
77 'brcm,bcm2709' => FLAG_PI | FLAG_DTOK | FLAG_270X,
78 'brcm,bcm2835' => FLAG_PI | FLAG_DTOK | FLAG_283X,
79 'brcm,bcm2836' => FLAG_PI | FLAG_DTOK | FLAG_283X,
80 'of_cfs_init' => FLAG_DTOK | FLAG_DDTK,
81 'vc_cma_init' => FLAG_PI | FLAG_270X,
82 };
83
84 my $res = try_extract($kernel_file, $tmpfile1);
85 $res ||= try_decompress('\037\213\010', 'xy', 'gunzip', 0,
86 $kernel_file, $tmpfile1, $tmpfile2);
87 $res ||= try_decompress('\3757zXZ\000', 'abcde', 'unxz --single-stream', -1,
88 $kernel_file, $tmpfile1, $tmpfile2);
89 $res ||= try_decompress('BZh', 'xy', 'bunzip2', 0,
90 $kernel_file, $tmpfile1, $tmpfile2);
91 $res ||= try_decompress('\135\0\0\0', 'xxx', 'unlzma', 0,
92 $kernel_file, $tmpfile1, $tmpfile2);
93 $res ||= try_decompress('\211\114\132', 'xy', 'lzop -d', 0,
94 $kernel_file, $tmpfile1, $tmpfile2);
95 $res ||= try_decompress('\002\041\114\030', 'xy', 'lz4 -d', 1,
96 $kernel_file, $tmpfile1, $tmpfile2);
97
98 my $append_trailer;
99 my $trailer;
100 my $kver = '?';
101
102 $append_trailer = 1;
103
104 if ($res)
105 {
106 $kver = $res->{'kver'} || '?';
107 my $flags = $res->{'flags'};
108 print("Version: $kver\n");
109
110 if ($flags & FLAG_PI)
111 {
112 $dtok ||= ($flags & FLAG_DTOK) != 0;
113 $is_270x ||= ($flags & FLAG_270X) != 0;
114 $is_283x ||= ($flags & FLAG_283X) != 0;
115 $ddtk ||= ($flags & FLAG_DDTK) != 0;
116 }
117 else
118 {
119 print ("* This doesn't look like a Raspberry Pi kernel.\n");
120 }
121 }
122 elsif (!$dtok)
123 {
124 print ("* Is this a valid kernel?\n");
125 }
126
127 if ($append_trailer)
128 {
129 printf("DT: %s\n", $dtok ? "y" : "n");
130 printf("DDT: %s\n", $ddtk ? "y" : "n");
131 printf("270x: %s\n", $is_270x ? "y" : "n");
132 printf("283x: %s\n", $is_283x ? "y" : "n");
133
134 my @atoms;
135
136 push @atoms, [ $trailer_magic, pack('V', 0) ];
137 push @atoms, [ 'KVer', $kver ];
138 push @atoms, [ 'DTOK', pack('V', $dtok) ];
139 push @atoms, [ 'DDTK', pack('V', $ddtk) ];
140 push @atoms, [ '270X', pack('V', $is_270x) ];
141 push @atoms, [ '283X', pack('V', $is_283x) ];
142 push @atoms, [ '283x', pack('V', $is_283x && !$is_270x) ];
143
144 $trailer = pack_trailer(\@atoms);
145 $atoms[0]->[1] = pack('V', length($trailer));
146
147 $trailer = pack_trailer(\@atoms);
148 }
149
150 my $ofh;
151 my $total_len = 0;
152
153 if ($out_file eq $kernel_file)
154 {
155 die "* Failed to open '$out_file' for append\n"
156 if (!open($ofh, '>>', $out_file));
157 $total_len = tell($ofh);
158 }
159 else
160 {
161 die "* Failed to open '$kernel_file'\n"
162 if (!open(my $ifh, '<', $kernel_file));
163 die "* Failed to create '$out_file'\n"
164 if (!open($ofh, '>', $out_file));
165
166 my $copybuf;
167 while (1)
168 {
169 my $bytes = sysread($ifh, $copybuf, 64*1024);
170 last if (!$bytes);
171 syswrite($ofh, $copybuf, $bytes);
172 $total_len += $bytes;
173 }
174 close($ifh);
175 }
176
177 if ($trailer)
178 {
179 # Pad to word-alignment
180 syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
181 syswrite($ofh, $trailer);
182 }
183
184 close($ofh);
185
186 exit($trailer ? 0 : 1);
187
188 END {
189 unlink($tmpfile1) if ($tmpfile1);
190 unlink($tmpfile2) if ($tmpfile2);
191 }
192
193
194 sub usage
195 {
196 print ("Usage: mkknlimg [--dtok] [--270x] [--283x] <vmlinux|zImage|bzImage> <outfile>\n");
197 exit(1);
198 }
199
200 sub try_extract
201 {
202 my ($knl, $tmp) = @_;
203
204 my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
205
206 return undef if (!$ver);
207
208 chomp($ver);
209
210 my $res = { 'kver'=>$ver };
211 $res->{'flags'} = strings_to_flags($knl, $wanted_strings);
212
213 return $res;
214 }
215
216
217 sub try_decompress
218 {
219 my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
220
221 my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
222 if ($pos)
223 {
224 chomp($pos);
225 $pos = (split(/[\r\n]+/, $pos))[$idx];
226 return undef if (!defined($pos));
227 $pos =~ s/:.*[\r\n]*$//s;
228 my $cmd = "tail -c+$pos \"$knl\" | $zcat > $tmp2 2> /dev/null";
229 my $err = (system($cmd) >> 8);
230 return undef if (($err != 0) && ($err != 2));
231
232 return try_extract($tmp2, $tmp1);
233 }
234
235 return undef;
236 }
237
238 sub strings_to_flags
239 {
240 my ($knl, $strings) = @_;
241 my $string_pattern = '^('.join('|', keys(%$strings)).')$';
242 my $flags = 0;
243
244 my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`;
245 foreach my $match (@matches)
246 {
247 chomp($match);
248 $flags |= $strings->{$match};
249 }
250
251 return $flags;
252 }
253
254 sub pack_trailer
255 {
256 my ($atoms) = @_;
257 my $trailer = pack('VV', 0, 0);
258 for (my $i = $#$atoms; $i>=0; $i--)
259 {
260 my $atom = $atoms->[$i];
261 $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]);
262 }
263 return $trailer;
264 }