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