brcm2708: add linux 4.4 support
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.4 / 0050-scripts-Add-mkknlimg-and-knlinfo-scripts-from-tools-.patch
1 From 4ddb3fae0a5c5b6969168134b4352bceccf51b9c Mon Sep 17 00:00:00 2001
2 From: Phil Elwell <phil@raspberrypi.org>
3 Date: Mon, 11 May 2015 09:00:42 +0100
4 Subject: [PATCH 050/127] scripts: Add mkknlimg and knlinfo scripts from tools
5 repo
6
7 The Raspberry Pi firmware looks for a trailer on the kernel image to
8 determine whether it was compiled with Device Tree support enabled.
9 If the firmware finds a kernel without this trailer, or which has a
10 trailer indicating that it isn't DT-capable, it disables DT support
11 and reverts to using ATAGs.
12
13 The mkknlimg utility adds that trailer, having first analysed the
14 image to look for signs of DT support and the kernel version string.
15
16 knlinfo displays the contents of the trailer in the given kernel image.
17
18 scripts/mkknlimg: Add support for ARCH_BCM2835
19
20 Add a new trailer field indicating whether this is an ARCH_BCM2835
21 build, as opposed to MACH_BCM2708/9. If the loader finds this flag
22 is set it changes the default base dtb file name from bcm270x...
23 to bcm283y...
24
25 Also update knlinfo to show the status of the field.
26
27 scripts/mkknlimg: Improve ARCH_BCM2835 detection
28
29 The board support code contains sufficient strings to be able to
30 distinguish 2708 vs. 2835 builds, so remove the check for
31 bcm2835-pm-wdt which could exist in either.
32
33 Also, since the canned configuration is no longer built in (it's
34 a module), remove the config string checking.
35
36 See: https://github.com/raspberrypi/linux/issues/1157
37 ---
38 scripts/knlinfo | 168 ++++++++++++++++++++++++++++++++++++++
39 scripts/mkknlimg | 244 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
40 2 files changed, 412 insertions(+)
41 create mode 100755 scripts/knlinfo
42 create mode 100755 scripts/mkknlimg
43
44 --- /dev/null
45 +++ b/scripts/knlinfo
46 @@ -0,0 +1,168 @@
47 +#!/usr/bin/env perl
48 +# ----------------------------------------------------------------------
49 +# knlinfo by Phil Elwell for Raspberry Pi
50 +#
51 +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
52 +#
53 +# Licensed under the terms of the GNU General Public License.
54 +# ----------------------------------------------------------------------
55 +
56 +use strict;
57 +use integer;
58 +
59 +use Fcntl ":seek";
60 +
61 +my $trailer_magic = 'RPTL';
62 +
63 +my %atom_formats =
64 +(
65 + 'DTOK' => \&format_bool,
66 + 'KVer' => \&format_string,
67 + '283x' => \&format_bool,
68 +);
69 +
70 +if (@ARGV != 1)
71 +{
72 + print ("Usage: knlinfo <kernel image>\n");
73 + exit(1);
74 +}
75 +
76 +my $kernel_file = $ARGV[0];
77 +
78 +
79 +my ($atoms, $pos) = read_trailer($kernel_file);
80 +
81 +exit(1) if (!$atoms);
82 +
83 +printf("Kernel trailer found at %d/0x%x:\n", $pos, $pos);
84 +
85 +foreach my $atom (@$atoms)
86 +{
87 + printf(" %s: %s\n", $atom->[0], format_atom($atom));
88 +}
89 +
90 +exit(0);
91 +
92 +sub read_trailer
93 +{
94 + my ($kernel_file) = @_;
95 + my $fh;
96 +
97 + if (!open($fh, '<', $kernel_file))
98 + {
99 + print ("* Failed to open '$kernel_file'\n");
100 + return undef;
101 + }
102 +
103 + if (!seek($fh, -12, SEEK_END))
104 + {
105 + print ("* seek error in '$kernel_file'\n");
106 + return undef;
107 + }
108 +
109 + my $last_bytes;
110 + sysread($fh, $last_bytes, 12);
111 +
112 + my ($trailer_len, $data_len, $magic) = unpack('VVa4', $last_bytes);
113 +
114 + if (($magic ne $trailer_magic) || ($data_len != 4))
115 + {
116 + print ("* no trailer\n");
117 + return undef;
118 + }
119 + if (!seek($fh, -12, SEEK_END))
120 + {
121 + print ("* seek error in '$kernel_file'\n");
122 + return undef;
123 + }
124 +
125 + $trailer_len -= 12;
126 +
127 + while ($trailer_len > 0)
128 + {
129 + if ($trailer_len < 8)
130 + {
131 + print ("* truncated atom header in trailer\n");
132 + return undef;
133 + }
134 + if (!seek($fh, -8, SEEK_CUR))
135 + {
136 + print ("* seek error in '$kernel_file'\n");
137 + return undef;
138 + }
139 + $trailer_len -= 8;
140 +
141 + my $atom_hdr;
142 + sysread($fh, $atom_hdr, 8);
143 + my ($atom_len, $atom_type) = unpack('Va4', $atom_hdr);
144 +
145 + if ($trailer_len < $atom_len)
146 + {
147 + print ("* truncated atom data in trailer\n");
148 + return undef;
149 + }
150 +
151 + my $rounded_len = (($atom_len + 3) & ~3);
152 + if (!seek($fh, -(8 + $rounded_len), SEEK_CUR))
153 + {
154 + print ("* seek error in '$kernel_file'\n");
155 + return undef;
156 + }
157 + $trailer_len -= $rounded_len;
158 +
159 + my $atom_data;
160 + sysread($fh, $atom_data, $atom_len);
161 +
162 + if (!seek($fh, -$atom_len, SEEK_CUR))
163 + {
164 + print ("* seek error in '$kernel_file'\n");
165 + return undef;
166 + }
167 +
168 + push @$atoms, [ $atom_type, $atom_data ];
169 + }
170 +
171 + if (($$atoms[-1][0] eq "\x00\x00\x00\x00") &&
172 + ($$atoms[-1][1] eq ""))
173 + {
174 + pop @$atoms;
175 + }
176 + else
177 + {
178 + print ("* end marker missing from trailer\n");
179 + }
180 +
181 + return ($atoms, tell($fh));
182 +}
183 +
184 +sub format_atom
185 +{
186 + my ($atom) = @_;
187 +
188 + my $format_func = $atom_formats{$atom->[0]} || \&format_hex;
189 + return $format_func->($atom->[1]);
190 +}
191 +
192 +sub format_bool
193 +{
194 + my ($data) = @_;
195 + return unpack('V', $data) ? 'true' : 'false';
196 +}
197 +
198 +sub format_int
199 +{
200 + my ($data) = @_;
201 + return unpack('V', $data);
202 +}
203 +
204 +sub format_string
205 +{
206 + my ($data) = @_;
207 + return '"'.$data.'"';
208 +}
209 +
210 +sub format_hex
211 +{
212 + my ($data) = @_;
213 + return unpack('H*', $data);
214 +}
215 --- /dev/null
216 +++ b/scripts/mkknlimg
217 @@ -0,0 +1,244 @@
218 +#!/usr/bin/env perl
219 +# ----------------------------------------------------------------------
220 +# mkknlimg by Phil Elwell for Raspberry Pi
221 +# based on extract-ikconfig by Dick Streefland
222 +#
223 +# (c) 2009,2010 Dick Streefland <dick@streefland.net>
224 +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
225 +#
226 +# Licensed under the terms of the GNU General Public License.
227 +# ----------------------------------------------------------------------
228 +
229 +use strict;
230 +use warnings;
231 +use integer;
232 +
233 +my $trailer_magic = 'RPTL';
234 +
235 +my $tmpfile1 = "/tmp/mkknlimg_$$.1";
236 +my $tmpfile2 = "/tmp/mkknlimg_$$.2";
237 +
238 +my $dtok = 0;
239 +my $is_283x = 0;
240 +
241 +while (@ARGV && ($ARGV[0] =~ /^-/))
242 +{
243 + my $arg = shift(@ARGV);
244 + if ($arg eq '--dtok')
245 + {
246 + $dtok = 1;
247 + }
248 + elsif ($arg eq '--283x')
249 + {
250 + $is_283x = 1;
251 + }
252 + else
253 + {
254 + print ("* Unknown option '$arg'\n");
255 + usage();
256 + }
257 +}
258 +
259 +usage() if (@ARGV != 2);
260 +
261 +my $kernel_file = $ARGV[0];
262 +my $out_file = $ARGV[1];
263 +
264 +if (! -r $kernel_file)
265 +{
266 + print ("* File '$kernel_file' not found\n");
267 + usage();
268 +}
269 +
270 +my @wanted_strings =
271 +(
272 + 'bcm2708_fb',
273 + 'brcm,bcm2835-mmc',
274 + 'brcm,bcm2835-sdhost',
275 + 'brcm,bcm2708-pinctrl',
276 + 'brcm,bcm2835-gpio',
277 + 'brcm,bcm2835',
278 + 'brcm,bcm2836'
279 +);
280 +
281 +my $res = try_extract($kernel_file, $tmpfile1);
282 +$res = try_decompress('\037\213\010', 'xy', 'gunzip', 0,
283 + $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
284 +$res = try_decompress('\3757zXZ\000', 'abcde', 'unxz --single-stream', -1,
285 + $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
286 +$res = try_decompress('BZh', 'xy', 'bunzip2', 0,
287 + $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
288 +$res = try_decompress('\135\0\0\0', 'xxx', 'unlzma', 0,
289 + $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
290 +$res = try_decompress('\211\114\132', 'xy', 'lzop -d', 0,
291 + $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
292 +$res = try_decompress('\002\041\114\030', 'xy', 'lz4 -d', 1,
293 + $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
294 +
295 +my $append_trailer;
296 +my $trailer;
297 +my $kver = '?';
298 +
299 +$append_trailer = $dtok;
300 +
301 +if ($res)
302 +{
303 + $kver = $res->{''} || '?';
304 + print("Version: $kver\n");
305 +
306 + $append_trailer = $dtok;
307 + if (!$dtok)
308 + {
309 + if (config_bool($res, 'bcm2708_fb') ||
310 + config_bool($res, 'brcm,bcm2835-mmc') ||
311 + config_bool($res, 'brcm,bcm2835-sdhost'))
312 + {
313 + $dtok ||= config_bool($res, 'brcm,bcm2708-pinctrl');
314 + $dtok ||= config_bool($res, 'brcm,bcm2835-gpio');
315 + $is_283x ||= config_bool($res, 'brcm,bcm2835');
316 + $is_283x ||= config_bool($res, 'brcm,bcm2836');
317 + $dtok ||= $is_283x;
318 + $append_trailer = 1;
319 + }
320 + else
321 + {
322 + print ("* This doesn't look like a Raspberry Pi kernel. In pass-through mode.\n");
323 + }
324 + }
325 +}
326 +elsif (!$dtok)
327 +{
328 + print ("* Is this a valid kernel? In pass-through mode.\n");
329 +}
330 +
331 +if ($append_trailer)
332 +{
333 + printf("DT: %s\n", $dtok ? "y" : "n");
334 + printf("283x: %s\n", $is_283x ? "y" : "n");
335 +
336 + my @atoms;
337 +
338 + push @atoms, [ $trailer_magic, pack('V', 0) ];
339 + push @atoms, [ 'KVer', $kver ];
340 + push @atoms, [ 'DTOK', pack('V', $dtok) ];
341 + push @atoms, [ '283x', pack('V', $is_283x) ];
342 +
343 + $trailer = pack_trailer(\@atoms);
344 + $atoms[0]->[1] = pack('V', length($trailer));
345 +
346 + $trailer = pack_trailer(\@atoms);
347 +}
348 +
349 +my $ofh;
350 +my $total_len = 0;
351 +
352 +if ($out_file eq $kernel_file)
353 +{
354 + die "* Failed to open '$out_file' for append\n"
355 + if (!open($ofh, '>>', $out_file));
356 + $total_len = tell($ofh);
357 +}
358 +else
359 +{
360 + die "* Failed to open '$kernel_file'\n"
361 + if (!open(my $ifh, '<', $kernel_file));
362 + die "* Failed to create '$out_file'\n"
363 + if (!open($ofh, '>', $out_file));
364 +
365 + my $copybuf;
366 + while (1)
367 + {
368 + my $bytes = sysread($ifh, $copybuf, 64*1024);
369 + last if (!$bytes);
370 + syswrite($ofh, $copybuf, $bytes);
371 + $total_len += $bytes;
372 + }
373 + close($ifh);
374 +}
375 +
376 +if ($trailer)
377 +{
378 + # Pad to word-alignment
379 + syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
380 + syswrite($ofh, $trailer);
381 +}
382 +
383 +close($ofh);
384 +
385 +exit($trailer ? 0 : 1);
386 +
387 +END {
388 + unlink($tmpfile1) if ($tmpfile1);
389 + unlink($tmpfile2) if ($tmpfile2);
390 +}
391 +
392 +
393 +sub usage
394 +{
395 + print ("Usage: mkknlimg [--dtok] [--283x] <vmlinux|zImage|bzImage> <outfile>\n");
396 + exit(1);
397 +}
398 +
399 +sub try_extract
400 +{
401 + my ($knl, $tmp) = @_;
402 +
403 + my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
404 +
405 + return undef if (!$ver);
406 +
407 + chomp($ver);
408 +
409 + my $res = { ''=>$ver };
410 + my $string_pattern = '^('.join('|', @wanted_strings).')$';
411 +
412 + my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`;
413 + foreach my $match (@matches)
414 + {
415 + chomp($match);
416 + $res->{$match} = 1;
417 + }
418 +
419 + return $res;
420 +}
421 +
422 +
423 +sub try_decompress
424 +{
425 + my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
426 +
427 + my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
428 + if ($pos)
429 + {
430 + chomp($pos);
431 + $pos = (split(/[\r\n]+/, $pos))[$idx];
432 + return undef if (!defined($pos));
433 + $pos =~ s/:.*[\r\n]*$//s;
434 + my $cmd = "tail -c+$pos \"$knl\" | $zcat > $tmp2 2> /dev/null";
435 + my $err = (system($cmd) >> 8);
436 + return undef if (($err != 0) && ($err != 2));
437 +
438 + return try_extract($tmp2, $tmp1);
439 + }
440 +
441 + return undef;
442 +}
443 +
444 +sub pack_trailer
445 +{
446 + my ($atoms) = @_;
447 + my $trailer = pack('VV', 0, 0);
448 + for (my $i = $#$atoms; $i>=0; $i--)
449 + {
450 + my $atom = $atoms->[$i];
451 + $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]);
452 + }
453 + return $trailer;
454 +}
455 +
456 +sub config_bool
457 +{
458 + my ($configs, $wanted) = @_;
459 + my $val = $configs->{$wanted} || 'n';
460 + return (($val eq 'y') || ($val eq '1'));
461 +}