1 From d57d7f5a9a634a635704cedb1c966905825cdc89 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 056/773] scripts: Add mkknlimg and knlinfo scripts from tools
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.
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.
16 knlinfo displays the contents of the trailer in the given kernel image.
18 scripts/mkknlimg: Add support for ARCH_BCM2835
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...
25 Also update knlinfo to show the status of the field.
27 scripts/mkknlimg: Improve ARCH_BCM2835 detection
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.
33 Also, since the canned configuration is no longer built in (it's
34 a module), remove the config string checking.
36 See: https://github.com/raspberrypi/linux/issues/1157
38 scripts: Multi-platform support for mkknlimg and knlinfo
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
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
50 scripts/mkknlimg: Append a trailer for all input
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.
55 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
57 scripts/knlinfo: Decode DDTK atom
59 Show the DDTK atom as being a boolean.
61 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
63 mkknlimg: Retain downstream-kernel detection
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.
70 mkknlimg: Find some more downstream-only strings
72 See: https://github.com/raspberrypi/linux/issues/1920
74 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
76 scripts: Update mkknlimg, just in case
78 With the removal of the vc_cma driver, mkknlimg lost an indication that
79 the user had built a downstream kernel. Update the script, adding a few
80 more key strings, in case it is still being used.
82 Note that mkknlimg is now deprecated, except to tag kernels as upstream
83 (283x), and thus requiring upstream DTBs.
85 See: https://github.com/raspberrypi/linux/issues/2239
87 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
89 scripts/knlinfo | 171 +++++++++++++++++++++++++++++++
90 scripts/mkknlimg | 262 +++++++++++++++++++++++++++++++++++++++++++++++
91 2 files changed, 433 insertions(+)
92 create mode 100755 scripts/knlinfo
93 create mode 100755 scripts/mkknlimg
99 +# ----------------------------------------------------------------------
100 +# knlinfo by Phil Elwell for Raspberry Pi
102 +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
104 +# Licensed under the terms of the GNU General Public License.
105 +# ----------------------------------------------------------------------
112 +my $trailer_magic = 'RPTL';
116 + 'DDTK' => \&format_bool,
117 + 'DTOK' => \&format_bool,
118 + 'KVer' => \&format_string,
119 + '270X' => \&format_bool,
120 + '283X' => \&format_bool,
121 + '283x' => \&format_bool,
126 + print ("Usage: knlinfo <kernel image>\n");
130 +my $kernel_file = $ARGV[0];
133 +my ($atoms, $pos) = read_trailer($kernel_file);
135 +exit(1) if (!$atoms);
137 +printf("Kernel trailer found at %d/0x%x:\n", $pos, $pos);
139 +foreach my $atom (@$atoms)
141 + printf(" %s: %s\n", $atom->[0], format_atom($atom));
148 + my ($kernel_file) = @_;
151 + if (!open($fh, '<', $kernel_file))
153 + print ("* Failed to open '$kernel_file'\n");
157 + if (!seek($fh, -12, SEEK_END))
159 + print ("* seek error in '$kernel_file'\n");
164 + sysread($fh, $last_bytes, 12);
166 + my ($trailer_len, $data_len, $magic) = unpack('VVa4', $last_bytes);
168 + if (($magic ne $trailer_magic) || ($data_len != 4))
170 + print ("* no trailer\n");
173 + if (!seek($fh, -12, SEEK_END))
175 + print ("* seek error in '$kernel_file'\n");
179 + $trailer_len -= 12;
181 + while ($trailer_len > 0)
183 + if ($trailer_len < 8)
185 + print ("* truncated atom header in trailer\n");
188 + if (!seek($fh, -8, SEEK_CUR))
190 + print ("* seek error in '$kernel_file'\n");
196 + sysread($fh, $atom_hdr, 8);
197 + my ($atom_len, $atom_type) = unpack('Va4', $atom_hdr);
199 + if ($trailer_len < $atom_len)
201 + print ("* truncated atom data in trailer\n");
205 + my $rounded_len = (($atom_len + 3) & ~3);
206 + if (!seek($fh, -(8 + $rounded_len), SEEK_CUR))
208 + print ("* seek error in '$kernel_file'\n");
211 + $trailer_len -= $rounded_len;
214 + sysread($fh, $atom_data, $atom_len);
216 + if (!seek($fh, -$atom_len, SEEK_CUR))
218 + print ("* seek error in '$kernel_file'\n");
222 + push @$atoms, [ $atom_type, $atom_data ];
225 + if (($$atoms[-1][0] eq "\x00\x00\x00\x00") &&
226 + ($$atoms[-1][1] eq ""))
232 + print ("* end marker missing from trailer\n");
235 + return ($atoms, tell($fh));
242 + my $format_func = $atom_formats{$atom->[0]} || \&format_hex;
243 + return $format_func->($atom->[1]);
249 + return unpack('V', $data) ? 'y' : 'n';
255 + return unpack('V', $data);
261 + return '"'.$data.'"';
267 + return unpack('H*', $data);
270 +++ b/scripts/mkknlimg
273 +# ----------------------------------------------------------------------
274 +# mkknlimg by Phil Elwell for Raspberry Pi
275 +# based on extract-ikconfig by Dick Streefland
277 +# (c) 2009,2010 Dick Streefland <dick@streefland.net>
278 +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
280 +# Licensed under the terms of the GNU General Public License.
281 +# ----------------------------------------------------------------------
287 +use constant FLAG_PI => 0x01;
288 +use constant FLAG_DTOK => 0x02;
289 +use constant FLAG_DDTK => 0x04;
290 +use constant FLAG_270X => 0x08;
291 +use constant FLAG_283X => 0x10;
293 +my $trailer_magic = 'RPTL';
295 +my $tmpfile1 = "/tmp/mkknlimg_$$.1";
296 +my $tmpfile2 = "/tmp/mkknlimg_$$.2";
303 +while (@ARGV && ($ARGV[0] =~ /^-/))
305 + my $arg = shift(@ARGV);
306 + if ($arg eq '--dtok')
310 + elsif ($arg eq '--ddtk')
314 + elsif ($arg eq '--270x')
318 + elsif ($arg eq '--283x')
324 + print ("* Unknown option '$arg'\n");
329 +usage() if (@ARGV != 2);
331 +my $kernel_file = $ARGV[0];
332 +my $out_file = $ARGV[1];
334 +if (! -r $kernel_file)
336 + print ("* File '$kernel_file' not found\n");
340 +my $wanted_strings =
342 + 'brcm,bcm2835-mmc' => FLAG_PI,
343 + 'brcm,bcm2835-sdhost' => FLAG_PI,
344 + 'brcm,bcm2835-gpio' => FLAG_PI | FLAG_DTOK,
345 + 'brcm,bcm2708-fb' => FLAG_PI | FLAG_DTOK | FLAG_270X,
346 + 'brcm,bcm2708-usb' => FLAG_PI | FLAG_DTOK | FLAG_270X,
347 + 'brcm,bcm2835' => FLAG_PI | FLAG_DTOK | FLAG_283X,
348 + 'brcm,bcm2836' => FLAG_PI | FLAG_DTOK | FLAG_283X,
349 + 'brcm,bcm2837' => FLAG_PI | FLAG_DTOK | FLAG_283X,
350 + 'of_cfs_init' => FLAG_DTOK | FLAG_DDTK,
353 +my $res = try_extract($kernel_file, $tmpfile1);
354 +$res ||= try_decompress('\037\213\010', 'xy', 'gunzip', 0,
355 + $kernel_file, $tmpfile1, $tmpfile2);
356 +$res ||= try_decompress('\3757zXZ\000', 'abcde', 'unxz --single-stream', -1,
357 + $kernel_file, $tmpfile1, $tmpfile2);
358 +$res ||= try_decompress('BZh', 'xy', 'bunzip2', 0,
359 + $kernel_file, $tmpfile1, $tmpfile2);
360 +$res ||= try_decompress('\135\0\0\0', 'xxx', 'unlzma', 0,
361 + $kernel_file, $tmpfile1, $tmpfile2);
362 +$res ||= try_decompress('\211\114\132', 'xy', 'lzop -d', 0,
363 + $kernel_file, $tmpfile1, $tmpfile2);
364 +$res ||= try_decompress('\002\041\114\030', 'xy', 'lz4 -d', 1,
365 + $kernel_file, $tmpfile1, $tmpfile2);
371 +$append_trailer = 1;
375 + $kver = $res->{'kver'} || '?';
376 + my $flags = $res->{'flags'};
377 + print("Version: $kver\n");
379 + if ($flags & FLAG_PI)
381 + $dtok ||= ($flags & FLAG_DTOK) != 0;
382 + $is_270x ||= ($flags & FLAG_270X) != 0;
383 + $is_283x ||= ($flags & FLAG_283X) != 0;
384 + $ddtk ||= ($flags & FLAG_DDTK) != 0;
388 + print ("* This doesn't look like a Raspberry Pi kernel.\n");
393 + print ("* Is this a valid kernel?\n");
396 +if ($append_trailer)
398 + printf("DT: %s\n", $dtok ? "y" : "n");
399 + printf("DDT: %s\n", $ddtk ? "y" : "n");
400 + printf("270x: %s\n", $is_270x ? "y" : "n");
401 + printf("283x: %s\n", $is_283x ? "y" : "n");
405 + push @atoms, [ $trailer_magic, pack('V', 0) ];
406 + push @atoms, [ 'KVer', $kver ];
407 + push @atoms, [ 'DTOK', pack('V', $dtok) ];
408 + push @atoms, [ 'DDTK', pack('V', $ddtk) ];
409 + push @atoms, [ '270X', pack('V', $is_270x) ];
410 + push @atoms, [ '283X', pack('V', $is_283x) ];
411 + push @atoms, [ '283x', pack('V', $is_283x && !$is_270x) ];
413 + $trailer = pack_trailer(\@atoms);
414 + $atoms[0]->[1] = pack('V', length($trailer));
416 + $trailer = pack_trailer(\@atoms);
422 +if ($out_file eq $kernel_file)
424 + die "* Failed to open '$out_file' for append\n"
425 + if (!open($ofh, '>>', $out_file));
426 + $total_len = tell($ofh);
430 + die "* Failed to open '$kernel_file'\n"
431 + if (!open(my $ifh, '<', $kernel_file));
432 + die "* Failed to create '$out_file'\n"
433 + if (!open($ofh, '>', $out_file));
438 + my $bytes = sysread($ifh, $copybuf, 64*1024);
440 + syswrite($ofh, $copybuf, $bytes);
441 + $total_len += $bytes;
448 + # Pad to word-alignment
449 + syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
450 + syswrite($ofh, $trailer);
455 +exit($trailer ? 0 : 1);
458 + unlink($tmpfile1) if ($tmpfile1);
459 + unlink($tmpfile2) if ($tmpfile2);
465 + print ("Usage: mkknlimg [--dtok] [--270x] [--283x] <vmlinux|zImage|bzImage> <outfile>\n");
471 + my ($knl, $tmp) = @_;
473 + my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
475 + return undef if (!$ver);
479 + my $res = { 'kver'=>$ver };
480 + $res->{'flags'} = strings_to_flags($knl, $wanted_strings);
488 + my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
490 + my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
494 + $pos = (split(/[\r\n]+/, $pos))[$idx];
495 + return undef if (!defined($pos));
496 + $pos =~ s/:.*[\r\n]*$//s;
497 + my $cmd = "tail -c+$pos \"$knl\" | $zcat > $tmp2 2> /dev/null";
498 + my $err = (system($cmd) >> 8);
499 + return undef if (($err != 0) && ($err != 2));
501 + return try_extract($tmp2, $tmp1);
507 +sub strings_to_flags
509 + my ($knl, $strings) = @_;
510 + my $string_pattern = '^('.join('|', keys(%$strings)).')$';
513 + my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`;
514 + foreach my $match (@matches)
517 + $flags |= $strings->{$match};
526 + my $trailer = pack('VV', 0, 0);
527 + for (my $i = $#$atoms; $i>=0; $i--)
529 + my $atom = $atoms->[$i];
530 + $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]);