1 From 3703cda20110756a6531a570a4e52b2b42d99bf9 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/knlinfo | 168 ++++++++++++++++++++++++++++++++++++++
38 scripts/mkknlimg | 244 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
39 2 files changed, 412 insertions(+)
40 create mode 100755 scripts/knlinfo
41 create mode 100755 scripts/mkknlimg
47 +# ----------------------------------------------------------------------
48 +# knlinfo by Phil Elwell for Raspberry Pi
50 +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
52 +# Licensed under the terms of the GNU General Public License.
53 +# ----------------------------------------------------------------------
60 +my $trailer_magic = 'RPTL';
64 + 'DTOK' => \&format_bool,
65 + 'KVer' => \&format_string,
66 + '283x' => \&format_bool,
71 + print ("Usage: knlinfo <kernel image>\n");
75 +my $kernel_file = $ARGV[0];
78 +my ($atoms, $pos) = read_trailer($kernel_file);
80 +exit(1) if (!$atoms);
82 +printf("Kernel trailer found at %d/0x%x:\n", $pos, $pos);
84 +foreach my $atom (@$atoms)
86 + printf(" %s: %s\n", $atom->[0], format_atom($atom));
93 + my ($kernel_file) = @_;
96 + if (!open($fh, '<', $kernel_file))
98 + print ("* Failed to open '$kernel_file'\n");
102 + if (!seek($fh, -12, SEEK_END))
104 + print ("* seek error in '$kernel_file'\n");
109 + sysread($fh, $last_bytes, 12);
111 + my ($trailer_len, $data_len, $magic) = unpack('VVa4', $last_bytes);
113 + if (($magic ne $trailer_magic) || ($data_len != 4))
115 + print ("* no trailer\n");
118 + if (!seek($fh, -12, SEEK_END))
120 + print ("* seek error in '$kernel_file'\n");
124 + $trailer_len -= 12;
126 + while ($trailer_len > 0)
128 + if ($trailer_len < 8)
130 + print ("* truncated atom header in trailer\n");
133 + if (!seek($fh, -8, SEEK_CUR))
135 + print ("* seek error in '$kernel_file'\n");
141 + sysread($fh, $atom_hdr, 8);
142 + my ($atom_len, $atom_type) = unpack('Va4', $atom_hdr);
144 + if ($trailer_len < $atom_len)
146 + print ("* truncated atom data in trailer\n");
150 + my $rounded_len = (($atom_len + 3) & ~3);
151 + if (!seek($fh, -(8 + $rounded_len), SEEK_CUR))
153 + print ("* seek error in '$kernel_file'\n");
156 + $trailer_len -= $rounded_len;
159 + sysread($fh, $atom_data, $atom_len);
161 + if (!seek($fh, -$atom_len, SEEK_CUR))
163 + print ("* seek error in '$kernel_file'\n");
167 + push @$atoms, [ $atom_type, $atom_data ];
170 + if (($$atoms[-1][0] eq "\x00\x00\x00\x00") &&
171 + ($$atoms[-1][1] eq ""))
177 + print ("* end marker missing from trailer\n");
180 + return ($atoms, tell($fh));
187 + my $format_func = $atom_formats{$atom->[0]} || \&format_hex;
188 + return $format_func->($atom->[1]);
194 + return unpack('V', $data) ? 'true' : 'false';
200 + return unpack('V', $data);
206 + return '"'.$data.'"';
212 + return unpack('H*', $data);
215 +++ b/scripts/mkknlimg
218 +# ----------------------------------------------------------------------
219 +# mkknlimg by Phil Elwell for Raspberry Pi
220 +# based on extract-ikconfig by Dick Streefland
222 +# (c) 2009,2010 Dick Streefland <dick@streefland.net>
223 +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
225 +# Licensed under the terms of the GNU General Public License.
226 +# ----------------------------------------------------------------------
232 +my $trailer_magic = 'RPTL';
234 +my $tmpfile1 = "/tmp/mkknlimg_$$.1";
235 +my $tmpfile2 = "/tmp/mkknlimg_$$.2";
240 +while (@ARGV && ($ARGV[0] =~ /^-/))
242 + my $arg = shift(@ARGV);
243 + if ($arg eq '--dtok')
247 + elsif ($arg eq '--283x')
253 + print ("* Unknown option '$arg'\n");
258 +usage() if (@ARGV != 2);
260 +my $kernel_file = $ARGV[0];
261 +my $out_file = $ARGV[1];
263 +if (! -r $kernel_file)
265 + print ("* File '$kernel_file' not found\n");
269 +my @wanted_strings =
272 + 'brcm,bcm2835-mmc',
273 + 'brcm,bcm2835-sdhost',
274 + 'brcm,bcm2708-pinctrl',
275 + 'brcm,bcm2835-gpio',
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);
298 +$append_trailer = $dtok;
302 + $kver = $res->{''} || '?';
303 + print("Version: $kver\n");
305 + $append_trailer = $dtok;
308 + if (config_bool($res, 'bcm2708_fb') ||
309 + config_bool($res, 'brcm,bcm2835-mmc') ||
310 + config_bool($res, 'brcm,bcm2835-sdhost'))
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;
321 + print ("* This doesn't look like a Raspberry Pi kernel. In pass-through mode.\n");
327 + print ("* Is this a valid kernel? In pass-through mode.\n");
330 +if ($append_trailer)
332 + printf("DT: %s\n", $dtok ? "y" : "n");
333 + printf("283x: %s\n", $is_283x ? "y" : "n");
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) ];
342 + $trailer = pack_trailer(\@atoms);
343 + $atoms[0]->[1] = pack('V', length($trailer));
345 + $trailer = pack_trailer(\@atoms);
351 +if ($out_file eq $kernel_file)
353 + die "* Failed to open '$out_file' for append\n"
354 + if (!open($ofh, '>>', $out_file));
355 + $total_len = tell($ofh);
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));
367 + my $bytes = sysread($ifh, $copybuf, 64*1024);
369 + syswrite($ofh, $copybuf, $bytes);
370 + $total_len += $bytes;
377 + # Pad to word-alignment
378 + syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
379 + syswrite($ofh, $trailer);
384 +exit($trailer ? 0 : 1);
387 + unlink($tmpfile1) if ($tmpfile1);
388 + unlink($tmpfile2) if ($tmpfile2);
394 + print ("Usage: mkknlimg [--dtok] [--283x] <vmlinux|zImage|bzImage> <outfile>\n");
400 + my ($knl, $tmp) = @_;
402 + my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
404 + return undef if (!$ver);
408 + my $res = { ''=>$ver };
409 + my $string_pattern = '^('.join('|', @wanted_strings).')$';
411 + my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`;
412 + foreach my $match (@matches)
415 + $res->{$match} = 1;
424 + my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
426 + my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
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));
437 + return try_extract($tmp2, $tmp1);
446 + my $trailer = pack('VV', 0, 0);
447 + for (my $i = $#$atoms; $i>=0; $i--)
449 + my $atom = $atoms->[$i];
450 + $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]);
457 + my ($configs, $wanted) = @_;
458 + my $val = $configs->{$wanted} || 'n';
459 + return (($val eq 'y') || ($val eq '1'));