1 From c1275c733d61290f7477eb7bbaea96d04206eff3 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
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.
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.
15 knlinfo displays the contents of the trailer in the given kernel image.
17 scripts/mkknlimg: Add support for ARCH_BCM2835
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...
24 Also update knlinfo to show the status of the field.
26 scripts/mkknlimg: Improve ARCH_BCM2835 detection
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.
32 Also, since the canned configuration is no longer built in (it's
33 a module), remove the config string checking.
35 See: https://github.com/raspberrypi/linux/issues/1157
37 scripts: Multi-platform support for mkknlimg and knlinfo
39 The firmware uses tags in the kernel trailer to choose which dtb file
40 to load. Current firmware loads bcm2835-*.dtb if the '283x' tag is true,
41 otherwise it loads bcm270*.dtb. This scheme breaks if an image supports
44 This patch adds '270X' and '283X' tags to indicate support for RPi and
45 upstream platforms, respectively. '283x' (note lower case 'x') is left
46 for old firmware, and is only set if the image only supports upstream
49 scripts/mkknlimg: Append a trailer for all input
51 Now that the firmware assumes an unsigned kernel is DT-capable, it is
52 helpful to be able to mark a kernel as being non-DT-capable.
54 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
56 scripts/knlinfo: Decode DDTK atom
58 Show the DDTK atom as being a boolean.
60 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
62 mkknlimg: Retain downstream-kernel detection
64 With the death of ARCH_BCM2708 and ARCH_BCM2709, a new way is needed to
65 determine if this is a "downstream" build that wants the firmware to
66 load a bcm27xx .dtb. The vc_cma driver is used downstream but not
67 upstream, making vc_cma_init a suitable predicate symbol.
69 scripts/knlinfo | 171 +++++++++++++++++++++++++++++++++++
70 scripts/mkknlimg | 264 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
71 2 files changed, 435 insertions(+)
72 create mode 100755 scripts/knlinfo
73 create mode 100755 scripts/mkknlimg
79 +# ----------------------------------------------------------------------
80 +# knlinfo by Phil Elwell for Raspberry Pi
82 +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
84 +# Licensed under the terms of the GNU General Public License.
85 +# ----------------------------------------------------------------------
92 +my $trailer_magic = 'RPTL';
96 + 'DDTK' => \&format_bool,
97 + 'DTOK' => \&format_bool,
98 + 'KVer' => \&format_string,
99 + '270X' => \&format_bool,
100 + '283X' => \&format_bool,
101 + '283x' => \&format_bool,
106 + print ("Usage: knlinfo <kernel image>\n");
110 +my $kernel_file = $ARGV[0];
113 +my ($atoms, $pos) = read_trailer($kernel_file);
115 +exit(1) if (!$atoms);
117 +printf("Kernel trailer found at %d/0x%x:\n", $pos, $pos);
119 +foreach my $atom (@$atoms)
121 + printf(" %s: %s\n", $atom->[0], format_atom($atom));
128 + my ($kernel_file) = @_;
131 + if (!open($fh, '<', $kernel_file))
133 + print ("* Failed to open '$kernel_file'\n");
137 + if (!seek($fh, -12, SEEK_END))
139 + print ("* seek error in '$kernel_file'\n");
144 + sysread($fh, $last_bytes, 12);
146 + my ($trailer_len, $data_len, $magic) = unpack('VVa4', $last_bytes);
148 + if (($magic ne $trailer_magic) || ($data_len != 4))
150 + print ("* no trailer\n");
153 + if (!seek($fh, -12, SEEK_END))
155 + print ("* seek error in '$kernel_file'\n");
159 + $trailer_len -= 12;
161 + while ($trailer_len > 0)
163 + if ($trailer_len < 8)
165 + print ("* truncated atom header in trailer\n");
168 + if (!seek($fh, -8, SEEK_CUR))
170 + print ("* seek error in '$kernel_file'\n");
176 + sysread($fh, $atom_hdr, 8);
177 + my ($atom_len, $atom_type) = unpack('Va4', $atom_hdr);
179 + if ($trailer_len < $atom_len)
181 + print ("* truncated atom data in trailer\n");
185 + my $rounded_len = (($atom_len + 3) & ~3);
186 + if (!seek($fh, -(8 + $rounded_len), SEEK_CUR))
188 + print ("* seek error in '$kernel_file'\n");
191 + $trailer_len -= $rounded_len;
194 + sysread($fh, $atom_data, $atom_len);
196 + if (!seek($fh, -$atom_len, SEEK_CUR))
198 + print ("* seek error in '$kernel_file'\n");
202 + push @$atoms, [ $atom_type, $atom_data ];
205 + if (($$atoms[-1][0] eq "\x00\x00\x00\x00") &&
206 + ($$atoms[-1][1] eq ""))
212 + print ("* end marker missing from trailer\n");
215 + return ($atoms, tell($fh));
222 + my $format_func = $atom_formats{$atom->[0]} || \&format_hex;
223 + return $format_func->($atom->[1]);
229 + return unpack('V', $data) ? 'y' : 'n';
235 + return unpack('V', $data);
241 + return '"'.$data.'"';
247 + return unpack('H*', $data);
250 +++ b/scripts/mkknlimg
253 +# ----------------------------------------------------------------------
254 +# mkknlimg by Phil Elwell for Raspberry Pi
255 +# based on extract-ikconfig by Dick Streefland
257 +# (c) 2009,2010 Dick Streefland <dick@streefland.net>
258 +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
260 +# Licensed under the terms of the GNU General Public License.
261 +# ----------------------------------------------------------------------
267 +use constant FLAG_PI => 0x01;
268 +use constant FLAG_DTOK => 0x02;
269 +use constant FLAG_DDTK => 0x04;
270 +use constant FLAG_270X => 0x08;
271 +use constant FLAG_283X => 0x10;
273 +my $trailer_magic = 'RPTL';
275 +my $tmpfile1 = "/tmp/mkknlimg_$$.1";
276 +my $tmpfile2 = "/tmp/mkknlimg_$$.2";
283 +while (@ARGV && ($ARGV[0] =~ /^-/))
285 + my $arg = shift(@ARGV);
286 + if ($arg eq '--dtok')
290 + elsif ($arg eq '--ddtk')
294 + elsif ($arg eq '--270x')
298 + elsif ($arg eq '--283x')
304 + print ("* Unknown option '$arg'\n");
309 +usage() if (@ARGV != 2);
311 +my $kernel_file = $ARGV[0];
312 +my $out_file = $ARGV[1];
314 +if (! -r $kernel_file)
316 + print ("* File '$kernel_file' not found\n");
320 +my $wanted_strings =
322 + 'bcm2708_fb' => FLAG_PI,
323 + 'brcm,bcm2835-mmc' => FLAG_PI,
324 + 'brcm,bcm2835-sdhost' => FLAG_PI,
325 + 'brcm,bcm2708-pinctrl' => FLAG_PI | FLAG_DTOK,
326 + 'brcm,bcm2835-gpio' => FLAG_PI | FLAG_DTOK,
327 + 'brcm,bcm2708' => FLAG_PI | FLAG_DTOK | FLAG_270X,
328 + 'brcm,bcm2709' => FLAG_PI | FLAG_DTOK | FLAG_270X,
329 + 'brcm,bcm2835' => FLAG_PI | FLAG_DTOK | FLAG_283X,
330 + 'brcm,bcm2836' => FLAG_PI | FLAG_DTOK | FLAG_283X,
331 + 'of_cfs_init' => FLAG_DTOK | FLAG_DDTK,
332 + 'vc_cma_init' => FLAG_PI | FLAG_270X,
335 +my $res = try_extract($kernel_file, $tmpfile1);
336 +$res ||= try_decompress('\037\213\010', 'xy', 'gunzip', 0,
337 + $kernel_file, $tmpfile1, $tmpfile2);
338 +$res ||= try_decompress('\3757zXZ\000', 'abcde', 'unxz --single-stream', -1,
339 + $kernel_file, $tmpfile1, $tmpfile2);
340 +$res ||= try_decompress('BZh', 'xy', 'bunzip2', 0,
341 + $kernel_file, $tmpfile1, $tmpfile2);
342 +$res ||= try_decompress('\135\0\0\0', 'xxx', 'unlzma', 0,
343 + $kernel_file, $tmpfile1, $tmpfile2);
344 +$res ||= try_decompress('\211\114\132', 'xy', 'lzop -d', 0,
345 + $kernel_file, $tmpfile1, $tmpfile2);
346 +$res ||= try_decompress('\002\041\114\030', 'xy', 'lz4 -d', 1,
347 + $kernel_file, $tmpfile1, $tmpfile2);
353 +$append_trailer = 1;
357 + $kver = $res->{'kver'} || '?';
358 + my $flags = $res->{'flags'};
359 + print("Version: $kver\n");
361 + if ($flags & FLAG_PI)
363 + $dtok ||= ($flags & FLAG_DTOK) != 0;
364 + $is_270x ||= ($flags & FLAG_270X) != 0;
365 + $is_283x ||= ($flags & FLAG_283X) != 0;
366 + $ddtk ||= ($flags & FLAG_DDTK) != 0;
370 + print ("* This doesn't look like a Raspberry Pi kernel.\n");
375 + print ("* Is this a valid kernel?\n");
378 +if ($append_trailer)
380 + printf("DT: %s\n", $dtok ? "y" : "n");
381 + printf("DDT: %s\n", $ddtk ? "y" : "n");
382 + printf("270x: %s\n", $is_270x ? "y" : "n");
383 + printf("283x: %s\n", $is_283x ? "y" : "n");
387 + push @atoms, [ $trailer_magic, pack('V', 0) ];
388 + push @atoms, [ 'KVer', $kver ];
389 + push @atoms, [ 'DTOK', pack('V', $dtok) ];
390 + push @atoms, [ 'DDTK', pack('V', $ddtk) ];
391 + push @atoms, [ '270X', pack('V', $is_270x) ];
392 + push @atoms, [ '283X', pack('V', $is_283x) ];
393 + push @atoms, [ '283x', pack('V', $is_283x && !$is_270x) ];
395 + $trailer = pack_trailer(\@atoms);
396 + $atoms[0]->[1] = pack('V', length($trailer));
398 + $trailer = pack_trailer(\@atoms);
404 +if ($out_file eq $kernel_file)
406 + die "* Failed to open '$out_file' for append\n"
407 + if (!open($ofh, '>>', $out_file));
408 + $total_len = tell($ofh);
412 + die "* Failed to open '$kernel_file'\n"
413 + if (!open(my $ifh, '<', $kernel_file));
414 + die "* Failed to create '$out_file'\n"
415 + if (!open($ofh, '>', $out_file));
420 + my $bytes = sysread($ifh, $copybuf, 64*1024);
422 + syswrite($ofh, $copybuf, $bytes);
423 + $total_len += $bytes;
430 + # Pad to word-alignment
431 + syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
432 + syswrite($ofh, $trailer);
437 +exit($trailer ? 0 : 1);
440 + unlink($tmpfile1) if ($tmpfile1);
441 + unlink($tmpfile2) if ($tmpfile2);
447 + print ("Usage: mkknlimg [--dtok] [--270x] [--283x] <vmlinux|zImage|bzImage> <outfile>\n");
453 + my ($knl, $tmp) = @_;
455 + my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
457 + return undef if (!$ver);
461 + my $res = { 'kver'=>$ver };
462 + $res->{'flags'} = strings_to_flags($knl, $wanted_strings);
470 + my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
472 + my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
476 + $pos = (split(/[\r\n]+/, $pos))[$idx];
477 + return undef if (!defined($pos));
478 + $pos =~ s/:.*[\r\n]*$//s;
479 + my $cmd = "tail -c+$pos \"$knl\" | $zcat > $tmp2 2> /dev/null";
480 + my $err = (system($cmd) >> 8);
481 + return undef if (($err != 0) && ($err != 2));
483 + return try_extract($tmp2, $tmp1);
489 +sub strings_to_flags
491 + my ($knl, $strings) = @_;
492 + my $string_pattern = '^('.join('|', keys(%$strings)).')$';
495 + my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`;
496 + foreach my $match (@matches)
499 + $flags |= $strings->{$match};
508 + my $trailer = pack('VV', 0, 0);
509 + for (my $i = $#$atoms; $i>=0; $i--)
511 + my $atom = $atoms->[$i];
512 + $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]);