ed2af04ada70adc0238e58fd03f9e33e103cef5c
[openwrt/staging/dangole.git] /
1 From 1451eae50acf35f852b7218722a0f28d78eaeb98 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 053/454] 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: Multi-platform support for mkknlimg and knlinfo
39
40 The firmware uses tags in the kernel trailer to choose which dtb file
41 to load. Current firmware loads bcm2835-*.dtb if the '283x' tag is true,
42 otherwise it loads bcm270*.dtb. This scheme breaks if an image supports
43 multiple platforms.
44
45 This patch adds '270X' and '283X' tags to indicate support for RPi and
46 upstream platforms, respectively. '283x' (note lower case 'x') is left
47 for old firmware, and is only set if the image only supports upstream
48 builds.
49
50 scripts/mkknlimg: Append a trailer for all input
51
52 Now that the firmware assumes an unsigned kernel is DT-capable, it is
53 helpful to be able to mark a kernel as being non-DT-capable.
54
55 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
56
57 scripts/knlinfo: Decode DDTK atom
58
59 Show the DDTK atom as being a boolean.
60
61 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
62
63 mkknlimg: Retain downstream-kernel detection
64
65 With the death of ARCH_BCM2708 and ARCH_BCM2709, a new way is needed to
66 determine if this is a "downstream" build that wants the firmware to
67 load a bcm27xx .dtb. The vc_cma driver is used downstream but not
68 upstream, making vc_cma_init a suitable predicate symbol.
69
70 mkknlimg: Find some more downstream-only strings
71
72 See: https://github.com/raspberrypi/linux/issues/1920
73
74 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
75 ---
76 scripts/knlinfo | 171 ++++++++++++++++++++++++++++++
77 scripts/mkknlimg | 265 +++++++++++++++++++++++++++++++++++++++++++++++
78 2 files changed, 436 insertions(+)
79 create mode 100755 scripts/knlinfo
80 create mode 100755 scripts/mkknlimg
81
82 --- /dev/null
83 +++ b/scripts/knlinfo
84 @@ -0,0 +1,171 @@
85 +#!/usr/bin/env perl
86 +# ----------------------------------------------------------------------
87 +# knlinfo by Phil Elwell for Raspberry Pi
88 +#
89 +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
90 +#
91 +# Licensed under the terms of the GNU General Public License.
92 +# ----------------------------------------------------------------------
93 +
94 +use strict;
95 +use integer;
96 +
97 +use Fcntl ":seek";
98 +
99 +my $trailer_magic = 'RPTL';
100 +
101 +my %atom_formats =
102 +(
103 + 'DDTK' => \&format_bool,
104 + 'DTOK' => \&format_bool,
105 + 'KVer' => \&format_string,
106 + '270X' => \&format_bool,
107 + '283X' => \&format_bool,
108 + '283x' => \&format_bool,
109 +);
110 +
111 +if (@ARGV != 1)
112 +{
113 + print ("Usage: knlinfo <kernel image>\n");
114 + exit(1);
115 +}
116 +
117 +my $kernel_file = $ARGV[0];
118 +
119 +
120 +my ($atoms, $pos) = read_trailer($kernel_file);
121 +
122 +exit(1) if (!$atoms);
123 +
124 +printf("Kernel trailer found at %d/0x%x:\n", $pos, $pos);
125 +
126 +foreach my $atom (@$atoms)
127 +{
128 + printf(" %s: %s\n", $atom->[0], format_atom($atom));
129 +}
130 +
131 +exit(0);
132 +
133 +sub read_trailer
134 +{
135 + my ($kernel_file) = @_;
136 + my $fh;
137 +
138 + if (!open($fh, '<', $kernel_file))
139 + {
140 + print ("* Failed to open '$kernel_file'\n");
141 + return undef;
142 + }
143 +
144 + if (!seek($fh, -12, SEEK_END))
145 + {
146 + print ("* seek error in '$kernel_file'\n");
147 + return undef;
148 + }
149 +
150 + my $last_bytes;
151 + sysread($fh, $last_bytes, 12);
152 +
153 + my ($trailer_len, $data_len, $magic) = unpack('VVa4', $last_bytes);
154 +
155 + if (($magic ne $trailer_magic) || ($data_len != 4))
156 + {
157 + print ("* no trailer\n");
158 + return undef;
159 + }
160 + if (!seek($fh, -12, SEEK_END))
161 + {
162 + print ("* seek error in '$kernel_file'\n");
163 + return undef;
164 + }
165 +
166 + $trailer_len -= 12;
167 +
168 + while ($trailer_len > 0)
169 + {
170 + if ($trailer_len < 8)
171 + {
172 + print ("* truncated atom header in trailer\n");
173 + return undef;
174 + }
175 + if (!seek($fh, -8, SEEK_CUR))
176 + {
177 + print ("* seek error in '$kernel_file'\n");
178 + return undef;
179 + }
180 + $trailer_len -= 8;
181 +
182 + my $atom_hdr;
183 + sysread($fh, $atom_hdr, 8);
184 + my ($atom_len, $atom_type) = unpack('Va4', $atom_hdr);
185 +
186 + if ($trailer_len < $atom_len)
187 + {
188 + print ("* truncated atom data in trailer\n");
189 + return undef;
190 + }
191 +
192 + my $rounded_len = (($atom_len + 3) & ~3);
193 + if (!seek($fh, -(8 + $rounded_len), SEEK_CUR))
194 + {
195 + print ("* seek error in '$kernel_file'\n");
196 + return undef;
197 + }
198 + $trailer_len -= $rounded_len;
199 +
200 + my $atom_data;
201 + sysread($fh, $atom_data, $atom_len);
202 +
203 + if (!seek($fh, -$atom_len, SEEK_CUR))
204 + {
205 + print ("* seek error in '$kernel_file'\n");
206 + return undef;
207 + }
208 +
209 + push @$atoms, [ $atom_type, $atom_data ];
210 + }
211 +
212 + if (($$atoms[-1][0] eq "\x00\x00\x00\x00") &&
213 + ($$atoms[-1][1] eq ""))
214 + {
215 + pop @$atoms;
216 + }
217 + else
218 + {
219 + print ("* end marker missing from trailer\n");
220 + }
221 +
222 + return ($atoms, tell($fh));
223 +}
224 +
225 +sub format_atom
226 +{
227 + my ($atom) = @_;
228 +
229 + my $format_func = $atom_formats{$atom->[0]} || \&format_hex;
230 + return $format_func->($atom->[1]);
231 +}
232 +
233 +sub format_bool
234 +{
235 + my ($data) = @_;
236 + return unpack('V', $data) ? 'y' : 'n';
237 +}
238 +
239 +sub format_int
240 +{
241 + my ($data) = @_;
242 + return unpack('V', $data);
243 +}
244 +
245 +sub format_string
246 +{
247 + my ($data) = @_;
248 + return '"'.$data.'"';
249 +}
250 +
251 +sub format_hex
252 +{
253 + my ($data) = @_;
254 + return unpack('H*', $data);
255 +}
256 --- /dev/null
257 +++ b/scripts/mkknlimg
258 @@ -0,0 +1,265 @@
259 +#!/usr/bin/env perl
260 +# ----------------------------------------------------------------------
261 +# mkknlimg by Phil Elwell for Raspberry Pi
262 +# based on extract-ikconfig by Dick Streefland
263 +#
264 +# (c) 2009,2010 Dick Streefland <dick@streefland.net>
265 +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
266 +#
267 +# Licensed under the terms of the GNU General Public License.
268 +# ----------------------------------------------------------------------
269 +
270 +use strict;
271 +use warnings;
272 +use integer;
273 +
274 +use constant FLAG_PI => 0x01;
275 +use constant FLAG_DTOK => 0x02;
276 +use constant FLAG_DDTK => 0x04;
277 +use constant FLAG_270X => 0x08;
278 +use constant FLAG_283X => 0x10;
279 +
280 +my $trailer_magic = 'RPTL';
281 +
282 +my $tmpfile1 = "/tmp/mkknlimg_$$.1";
283 +my $tmpfile2 = "/tmp/mkknlimg_$$.2";
284 +
285 +my $dtok = 0;
286 +my $ddtk = 0;
287 +my $is_270x = 0;
288 +my $is_283x = 0;
289 +
290 +while (@ARGV && ($ARGV[0] =~ /^-/))
291 +{
292 + my $arg = shift(@ARGV);
293 + if ($arg eq '--dtok')
294 + {
295 + $dtok = 1;
296 + }
297 + elsif ($arg eq '--ddtk')
298 + {
299 + $ddtk = 1;
300 + }
301 + elsif ($arg eq '--270x')
302 + {
303 + $is_270x = 1;
304 + }
305 + elsif ($arg eq '--283x')
306 + {
307 + $is_283x = 1;
308 + }
309 + else
310 + {
311 + print ("* Unknown option '$arg'\n");
312 + usage();
313 + }
314 +}
315 +
316 +usage() if (@ARGV != 2);
317 +
318 +my $kernel_file = $ARGV[0];
319 +my $out_file = $ARGV[1];
320 +
321 +if (! -r $kernel_file)
322 +{
323 + print ("* File '$kernel_file' not found\n");
324 + usage();
325 +}
326 +
327 +my $wanted_strings =
328 +{
329 + 'bcm2708_fb' => FLAG_PI | FLAG_270X,
330 + 'brcm,bcm2835-mmc' => FLAG_PI,
331 + 'brcm,bcm2835-sdhost' => FLAG_PI,
332 + 'brcm,bcm2708-pinctrl' => FLAG_PI | FLAG_DTOK,
333 + 'brcm,bcm2835-gpio' => FLAG_PI | FLAG_DTOK,
334 + 'brcm,bcm2708' => FLAG_PI | FLAG_DTOK | FLAG_270X,
335 + 'brcm,bcm2709' => FLAG_PI | FLAG_DTOK | FLAG_270X,
336 + 'brcm,bcm2835' => FLAG_PI | FLAG_DTOK | FLAG_283X,
337 + 'brcm,bcm2836' => FLAG_PI | FLAG_DTOK | FLAG_283X,
338 + 'of_cfs_init' => FLAG_DTOK | FLAG_DDTK,
339 + 'vc_cma_init' => FLAG_PI | FLAG_270X,
340 + 'vc-mem' => FLAG_PI | FLAG_270X,
341 +};
342 +
343 +my $res = try_extract($kernel_file, $tmpfile1);
344 +$res ||= try_decompress('\037\213\010', 'xy', 'gunzip', 0,
345 + $kernel_file, $tmpfile1, $tmpfile2);
346 +$res ||= try_decompress('\3757zXZ\000', 'abcde', 'unxz --single-stream', -1,
347 + $kernel_file, $tmpfile1, $tmpfile2);
348 +$res ||= try_decompress('BZh', 'xy', 'bunzip2', 0,
349 + $kernel_file, $tmpfile1, $tmpfile2);
350 +$res ||= try_decompress('\135\0\0\0', 'xxx', 'unlzma', 0,
351 + $kernel_file, $tmpfile1, $tmpfile2);
352 +$res ||= try_decompress('\211\114\132', 'xy', 'lzop -d', 0,
353 + $kernel_file, $tmpfile1, $tmpfile2);
354 +$res ||= try_decompress('\002\041\114\030', 'xy', 'lz4 -d', 1,
355 + $kernel_file, $tmpfile1, $tmpfile2);
356 +
357 +my $append_trailer;
358 +my $trailer;
359 +my $kver = '?';
360 +
361 +$append_trailer = 1;
362 +
363 +if ($res)
364 +{
365 + $kver = $res->{'kver'} || '?';
366 + my $flags = $res->{'flags'};
367 + print("Version: $kver\n");
368 +
369 + if ($flags & FLAG_PI)
370 + {
371 + $dtok ||= ($flags & FLAG_DTOK) != 0;
372 + $is_270x ||= ($flags & FLAG_270X) != 0;
373 + $is_283x ||= ($flags & FLAG_283X) != 0;
374 + $ddtk ||= ($flags & FLAG_DDTK) != 0;
375 + }
376 + else
377 + {
378 + print ("* This doesn't look like a Raspberry Pi kernel.\n");
379 + }
380 +}
381 +elsif (!$dtok)
382 +{
383 + print ("* Is this a valid kernel?\n");
384 +}
385 +
386 +if ($append_trailer)
387 +{
388 + printf("DT: %s\n", $dtok ? "y" : "n");
389 + printf("DDT: %s\n", $ddtk ? "y" : "n");
390 + printf("270x: %s\n", $is_270x ? "y" : "n");
391 + printf("283x: %s\n", $is_283x ? "y" : "n");
392 +
393 + my @atoms;
394 +
395 + push @atoms, [ $trailer_magic, pack('V', 0) ];
396 + push @atoms, [ 'KVer', $kver ];
397 + push @atoms, [ 'DTOK', pack('V', $dtok) ];
398 + push @atoms, [ 'DDTK', pack('V', $ddtk) ];
399 + push @atoms, [ '270X', pack('V', $is_270x) ];
400 + push @atoms, [ '283X', pack('V', $is_283x) ];
401 + push @atoms, [ '283x', pack('V', $is_283x && !$is_270x) ];
402 +
403 + $trailer = pack_trailer(\@atoms);
404 + $atoms[0]->[1] = pack('V', length($trailer));
405 +
406 + $trailer = pack_trailer(\@atoms);
407 +}
408 +
409 +my $ofh;
410 +my $total_len = 0;
411 +
412 +if ($out_file eq $kernel_file)
413 +{
414 + die "* Failed to open '$out_file' for append\n"
415 + if (!open($ofh, '>>', $out_file));
416 + $total_len = tell($ofh);
417 +}
418 +else
419 +{
420 + die "* Failed to open '$kernel_file'\n"
421 + if (!open(my $ifh, '<', $kernel_file));
422 + die "* Failed to create '$out_file'\n"
423 + if (!open($ofh, '>', $out_file));
424 +
425 + my $copybuf;
426 + while (1)
427 + {
428 + my $bytes = sysread($ifh, $copybuf, 64*1024);
429 + last if (!$bytes);
430 + syswrite($ofh, $copybuf, $bytes);
431 + $total_len += $bytes;
432 + }
433 + close($ifh);
434 +}
435 +
436 +if ($trailer)
437 +{
438 + # Pad to word-alignment
439 + syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
440 + syswrite($ofh, $trailer);
441 +}
442 +
443 +close($ofh);
444 +
445 +exit($trailer ? 0 : 1);
446 +
447 +END {
448 + unlink($tmpfile1) if ($tmpfile1);
449 + unlink($tmpfile2) if ($tmpfile2);
450 +}
451 +
452 +
453 +sub usage
454 +{
455 + print ("Usage: mkknlimg [--dtok] [--270x] [--283x] <vmlinux|zImage|bzImage> <outfile>\n");
456 + exit(1);
457 +}
458 +
459 +sub try_extract
460 +{
461 + my ($knl, $tmp) = @_;
462 +
463 + my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
464 +
465 + return undef if (!$ver);
466 +
467 + chomp($ver);
468 +
469 + my $res = { 'kver'=>$ver };
470 + $res->{'flags'} = strings_to_flags($knl, $wanted_strings);
471 +
472 + return $res;
473 +}
474 +
475 +
476 +sub try_decompress
477 +{
478 + my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
479 +
480 + my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
481 + if ($pos)
482 + {
483 + chomp($pos);
484 + $pos = (split(/[\r\n]+/, $pos))[$idx];
485 + return undef if (!defined($pos));
486 + $pos =~ s/:.*[\r\n]*$//s;
487 + my $cmd = "tail -c+$pos \"$knl\" | $zcat > $tmp2 2> /dev/null";
488 + my $err = (system($cmd) >> 8);
489 + return undef if (($err != 0) && ($err != 2));
490 +
491 + return try_extract($tmp2, $tmp1);
492 + }
493 +
494 + return undef;
495 +}
496 +
497 +sub strings_to_flags
498 +{
499 + my ($knl, $strings) = @_;
500 + my $string_pattern = '^('.join('|', keys(%$strings)).')$';
501 + my $flags = 0;
502 +
503 + my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`;
504 + foreach my $match (@matches)
505 + {
506 + chomp($match);
507 + $flags |= $strings->{$match};
508 + }
509 +
510 + return $flags;
511 +}
512 +
513 +sub pack_trailer
514 +{
515 + my ($atoms) = @_;
516 + my $trailer = pack('VV', 0, 0);
517 + for (my $i = $#$atoms; $i>=0; $i--)
518 + {
519 + my $atom = $atoms->[$i];
520 + $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]);
521 + }
522 + return $trailer;
523 +}