1 From 59cf4bf51eea78b4314cfabff3023f1a95358fa6 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/203] 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/knlinfo | 168 +++++++++++++++++++++++++++++++++
28 scripts/mkknlimg | 275 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
29 2 files changed, 443 insertions(+)
30 create mode 100755 scripts/knlinfo
31 create mode 100755 scripts/mkknlimg
37 +# ----------------------------------------------------------------------
38 +# knlinfo by Phil Elwell for Raspberry Pi
40 +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
42 +# Licensed under the terms of the GNU General Public License.
43 +# ----------------------------------------------------------------------
50 +my $trailer_magic = 'RPTL';
54 + 'DTOK' => \&format_bool,
55 + 'KVer' => \&format_string,
56 + '283x' => \&format_bool,
61 + print ("Usage: knlinfo <kernel image>\n");
65 +my $kernel_file = $ARGV[0];
68 +my ($atoms, $pos) = read_trailer($kernel_file);
70 +exit(1) if (!$atoms);
72 +printf("Kernel trailer found at %d/0x%x:\n", $pos, $pos);
74 +foreach my $atom (@$atoms)
76 + printf(" %s: %s\n", $atom->[0], format_atom($atom));
83 + my ($kernel_file) = @_;
86 + if (!open($fh, '<', $kernel_file))
88 + print ("* Failed to open '$kernel_file'\n");
92 + if (!seek($fh, -12, SEEK_END))
94 + print ("* seek error in '$kernel_file'\n");
99 + sysread($fh, $last_bytes, 12);
101 + my ($trailer_len, $data_len, $magic) = unpack('VVa4', $last_bytes);
103 + if (($magic ne $trailer_magic) || ($data_len != 4))
105 + print ("* no trailer\n");
108 + if (!seek($fh, -12, SEEK_END))
110 + print ("* seek error in '$kernel_file'\n");
114 + $trailer_len -= 12;
116 + while ($trailer_len > 0)
118 + if ($trailer_len < 8)
120 + print ("* truncated atom header in trailer\n");
123 + if (!seek($fh, -8, SEEK_CUR))
125 + print ("* seek error in '$kernel_file'\n");
131 + sysread($fh, $atom_hdr, 8);
132 + my ($atom_len, $atom_type) = unpack('Va4', $atom_hdr);
134 + if ($trailer_len < $atom_len)
136 + print ("* truncated atom data in trailer\n");
140 + my $rounded_len = (($atom_len + 3) & ~3);
141 + if (!seek($fh, -(8 + $rounded_len), SEEK_CUR))
143 + print ("* seek error in '$kernel_file'\n");
146 + $trailer_len -= $rounded_len;
149 + sysread($fh, $atom_data, $atom_len);
151 + if (!seek($fh, -$atom_len, SEEK_CUR))
153 + print ("* seek error in '$kernel_file'\n");
157 + push @$atoms, [ $atom_type, $atom_data ];
160 + if (($$atoms[-1][0] eq "\x00\x00\x00\x00") &&
161 + ($$atoms[-1][1] eq ""))
167 + print ("* end marker missing from trailer\n");
170 + return ($atoms, tell($fh));
177 + my $format_func = $atom_formats{$atom->[0]} || \&format_hex;
178 + return $format_func->($atom->[1]);
184 + return unpack('V', $data) ? 'true' : 'false';
190 + return unpack('V', $data);
196 + return '"'.$data.'"';
202 + return unpack('H*', $data);
205 +++ b/scripts/mkknlimg
208 +# ----------------------------------------------------------------------
209 +# mkknlimg by Phil Elwell for Raspberry Pi
210 +# based on extract-ikconfig by Dick Streefland
212 +# (c) 2009,2010 Dick Streefland <dick@streefland.net>
213 +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
215 +# Licensed under the terms of the GNU General Public License.
216 +# ----------------------------------------------------------------------
222 +my $trailer_magic = 'RPTL';
224 +my $tmpfile1 = "/tmp/mkknlimg_$$.1";
225 +my $tmpfile2 = "/tmp/mkknlimg_$$.2";
230 +while (@ARGV && ($ARGV[0] =~ /^-/))
232 + my $arg = shift(@ARGV);
233 + if ($arg eq '--dtok')
237 + elsif ($arg eq '--283x')
243 + print ("* Unknown option '$arg'\n");
248 +usage() if (@ARGV != 2);
250 +my $kernel_file = $ARGV[0];
251 +my $out_file = $ARGV[1];
253 +if (! -r $kernel_file)
255 + print ("* File '$kernel_file' not found\n");
259 +my @wanted_config_lines =
261 + 'CONFIG_BCM2708_DT',
262 + 'CONFIG_ARCH_BCM2835'
265 +my @wanted_strings =
268 + 'brcm,bcm2835-mmc',
269 + 'brcm,bcm2835-sdhost',
270 + 'brcm,bcm2708-pinctrl',
271 + 'brcm,bcm2835-gpio',
272 + 'brcm,bcm2835-pm-wdt'
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);
293 +$append_trailer = $dtok;
297 + $kver = $res->{''} || '?';
298 + print("Version: $kver\n");
300 + $append_trailer = $dtok;
303 + if (config_bool($res, 'bcm2708_fb') ||
304 + config_bool($res, 'brcm,bcm2835-mmc') ||
305 + config_bool($res, 'brcm,bcm2835-sdhost'))
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;
317 + print ("* This doesn't look like a Raspberry Pi kernel. In pass-through mode.\n");
323 + print ("* Is this a valid kernel? In pass-through mode.\n");
326 +if ($append_trailer)
328 + printf("DT: %s\n", $dtok ? "y" : "n");
329 + printf("283x: %s\n", $is_283x ? "y" : "n");
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) ];
338 + $trailer = pack_trailer(\@atoms);
339 + $atoms[0]->[1] = pack('V', length($trailer));
341 + $trailer = pack_trailer(\@atoms);
347 +if ($out_file eq $kernel_file)
349 + die "* Failed to open '$out_file' for append\n"
350 + if (!open($ofh, '>>', $out_file));
351 + $total_len = tell($ofh);
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));
363 + my $bytes = sysread($ifh, $copybuf, 64*1024);
365 + syswrite($ofh, $copybuf, $bytes);
366 + $total_len += $bytes;
373 + # Pad to word-alignment
374 + syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
375 + syswrite($ofh, $trailer);
380 +exit($trailer ? 0 : 1);
383 + unlink($tmpfile1) if ($tmpfile1);
384 + unlink($tmpfile2) if ($tmpfile2);
390 + print ("Usage: mkknlimg [--dtok] [--283x] <vmlinux|zImage|bzImage> <outfile>\n");
396 + my ($knl, $tmp) = @_;
398 + my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
400 + return undef if (!$ver);
404 + my $res = { ''=>$ver };
405 + my $string_pattern = '^('.join('|', @wanted_strings).')$';
407 + my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`;
408 + foreach my $match (@matches)
411 + $res->{$match} = 1;
414 + my $config_pattern = '^('.join('|', @wanted_config_lines).')=(.*)$';
415 + my $cf1 = 'IKCFG_ST\037\213\010';
416 + my $cf2 = '0123456789';
418 + my $pos = `tr "$cf1\n$cf2" "\n$cf2=" < "$knl" | grep -abo "^$cf2"`;
421 + $pos =~ s/:.*[\r\n]*$//s;
423 + my $err = (system("tail -c+$pos \"$knl\" | zcat > $tmp 2> /dev/null") >> 8);
424 + if (($err == 0) || ($err == 2))
426 + if (open(my $fh, '<', $tmp))
428 + while (my $line = <$fh>)
431 + $res->{$1} = $2 if ($line =~ /$config_pattern/);
445 + my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
447 + my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
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));
458 + return try_extract($tmp2, $tmp1);
467 + my $trailer = pack('VV', 0, 0);
468 + for (my $i = $#$atoms; $i>=0; $i--)
470 + my $atom = $atoms->[$i];
471 + $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]);
478 + my ($configs, $wanted) = @_;
479 + my $val = $configs->{$wanted} || 'n';
480 + return (($val eq 'y') || ($val eq '1'));