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