7ae4edd5c29d0a7f5383db78a37ac4c2881d6f77
[openwrt/staging/blocktrron.git] /
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
5
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.
11
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.
14
15 knlinfo displays the contents of the trailer in the given kernel image.
16
17 scripts/mkknlimg: Add support for ARCH_BCM2835
18
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...
22 to bcm283y...
23
24 Also update knlinfo to show the status of the field.
25
26 scripts/mkknlimg: Improve ARCH_BCM2835 detection
27
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.
31
32 Also, since the canned configuration is no longer built in (it's
33 a module), remove the config string checking.
34
35 See: https://github.com/raspberrypi/linux/issues/1157
36
37 scripts: Multi-platform support for mkknlimg and knlinfo
38
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
42 multiple platforms.
43
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
47 builds.
48
49 scripts/mkknlimg: Append a trailer for all input
50
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.
53
54 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
55
56 scripts/knlinfo: Decode DDTK atom
57
58 Show the DDTK atom as being a boolean.
59
60 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
61
62 mkknlimg: Retain downstream-kernel detection
63
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.
68 ---
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
74
75 --- /dev/null
76 +++ b/scripts/knlinfo
77 @@ -0,0 +1,171 @@
78 +#!/usr/bin/env perl
79 +# ----------------------------------------------------------------------
80 +# knlinfo by Phil Elwell for Raspberry Pi
81 +#
82 +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
83 +#
84 +# Licensed under the terms of the GNU General Public License.
85 +# ----------------------------------------------------------------------
86 +
87 +use strict;
88 +use integer;
89 +
90 +use Fcntl ":seek";
91 +
92 +my $trailer_magic = 'RPTL';
93 +
94 +my %atom_formats =
95 +(
96 + 'DDTK' => \&format_bool,
97 + 'DTOK' => \&format_bool,
98 + 'KVer' => \&format_string,
99 + '270X' => \&format_bool,
100 + '283X' => \&format_bool,
101 + '283x' => \&format_bool,
102 +);
103 +
104 +if (@ARGV != 1)
105 +{
106 + print ("Usage: knlinfo <kernel image>\n");
107 + exit(1);
108 +}
109 +
110 +my $kernel_file = $ARGV[0];
111 +
112 +
113 +my ($atoms, $pos) = read_trailer($kernel_file);
114 +
115 +exit(1) if (!$atoms);
116 +
117 +printf("Kernel trailer found at %d/0x%x:\n", $pos, $pos);
118 +
119 +foreach my $atom (@$atoms)
120 +{
121 + printf(" %s: %s\n", $atom->[0], format_atom($atom));
122 +}
123 +
124 +exit(0);
125 +
126 +sub read_trailer
127 +{
128 + my ($kernel_file) = @_;
129 + my $fh;
130 +
131 + if (!open($fh, '<', $kernel_file))
132 + {
133 + print ("* Failed to open '$kernel_file'\n");
134 + return undef;
135 + }
136 +
137 + if (!seek($fh, -12, SEEK_END))
138 + {
139 + print ("* seek error in '$kernel_file'\n");
140 + return undef;
141 + }
142 +
143 + my $last_bytes;
144 + sysread($fh, $last_bytes, 12);
145 +
146 + my ($trailer_len, $data_len, $magic) = unpack('VVa4', $last_bytes);
147 +
148 + if (($magic ne $trailer_magic) || ($data_len != 4))
149 + {
150 + print ("* no trailer\n");
151 + return undef;
152 + }
153 + if (!seek($fh, -12, SEEK_END))
154 + {
155 + print ("* seek error in '$kernel_file'\n");
156 + return undef;
157 + }
158 +
159 + $trailer_len -= 12;
160 +
161 + while ($trailer_len > 0)
162 + {
163 + if ($trailer_len < 8)
164 + {
165 + print ("* truncated atom header in trailer\n");
166 + return undef;
167 + }
168 + if (!seek($fh, -8, SEEK_CUR))
169 + {
170 + print ("* seek error in '$kernel_file'\n");
171 + return undef;
172 + }
173 + $trailer_len -= 8;
174 +
175 + my $atom_hdr;
176 + sysread($fh, $atom_hdr, 8);
177 + my ($atom_len, $atom_type) = unpack('Va4', $atom_hdr);
178 +
179 + if ($trailer_len < $atom_len)
180 + {
181 + print ("* truncated atom data in trailer\n");
182 + return undef;
183 + }
184 +
185 + my $rounded_len = (($atom_len + 3) & ~3);
186 + if (!seek($fh, -(8 + $rounded_len), SEEK_CUR))
187 + {
188 + print ("* seek error in '$kernel_file'\n");
189 + return undef;
190 + }
191 + $trailer_len -= $rounded_len;
192 +
193 + my $atom_data;
194 + sysread($fh, $atom_data, $atom_len);
195 +
196 + if (!seek($fh, -$atom_len, SEEK_CUR))
197 + {
198 + print ("* seek error in '$kernel_file'\n");
199 + return undef;
200 + }
201 +
202 + push @$atoms, [ $atom_type, $atom_data ];
203 + }
204 +
205 + if (($$atoms[-1][0] eq "\x00\x00\x00\x00") &&
206 + ($$atoms[-1][1] eq ""))
207 + {
208 + pop @$atoms;
209 + }
210 + else
211 + {
212 + print ("* end marker missing from trailer\n");
213 + }
214 +
215 + return ($atoms, tell($fh));
216 +}
217 +
218 +sub format_atom
219 +{
220 + my ($atom) = @_;
221 +
222 + my $format_func = $atom_formats{$atom->[0]} || \&format_hex;
223 + return $format_func->($atom->[1]);
224 +}
225 +
226 +sub format_bool
227 +{
228 + my ($data) = @_;
229 + return unpack('V', $data) ? 'y' : 'n';
230 +}
231 +
232 +sub format_int
233 +{
234 + my ($data) = @_;
235 + return unpack('V', $data);
236 +}
237 +
238 +sub format_string
239 +{
240 + my ($data) = @_;
241 + return '"'.$data.'"';
242 +}
243 +
244 +sub format_hex
245 +{
246 + my ($data) = @_;
247 + return unpack('H*', $data);
248 +}
249 --- /dev/null
250 +++ b/scripts/mkknlimg
251 @@ -0,0 +1,264 @@
252 +#!/usr/bin/env perl
253 +# ----------------------------------------------------------------------
254 +# mkknlimg by Phil Elwell for Raspberry Pi
255 +# based on extract-ikconfig by Dick Streefland
256 +#
257 +# (c) 2009,2010 Dick Streefland <dick@streefland.net>
258 +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
259 +#
260 +# Licensed under the terms of the GNU General Public License.
261 +# ----------------------------------------------------------------------
262 +
263 +use strict;
264 +use warnings;
265 +use integer;
266 +
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;
272 +
273 +my $trailer_magic = 'RPTL';
274 +
275 +my $tmpfile1 = "/tmp/mkknlimg_$$.1";
276 +my $tmpfile2 = "/tmp/mkknlimg_$$.2";
277 +
278 +my $dtok = 0;
279 +my $ddtk = 0;
280 +my $is_270x = 0;
281 +my $is_283x = 0;
282 +
283 +while (@ARGV && ($ARGV[0] =~ /^-/))
284 +{
285 + my $arg = shift(@ARGV);
286 + if ($arg eq '--dtok')
287 + {
288 + $dtok = 1;
289 + }
290 + elsif ($arg eq '--ddtk')
291 + {
292 + $ddtk = 1;
293 + }
294 + elsif ($arg eq '--270x')
295 + {
296 + $is_270x = 1;
297 + }
298 + elsif ($arg eq '--283x')
299 + {
300 + $is_283x = 1;
301 + }
302 + else
303 + {
304 + print ("* Unknown option '$arg'\n");
305 + usage();
306 + }
307 +}
308 +
309 +usage() if (@ARGV != 2);
310 +
311 +my $kernel_file = $ARGV[0];
312 +my $out_file = $ARGV[1];
313 +
314 +if (! -r $kernel_file)
315 +{
316 + print ("* File '$kernel_file' not found\n");
317 + usage();
318 +}
319 +
320 +my $wanted_strings =
321 +{
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,
333 +};
334 +
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);
348 +
349 +my $append_trailer;
350 +my $trailer;
351 +my $kver = '?';
352 +
353 +$append_trailer = 1;
354 +
355 +if ($res)
356 +{
357 + $kver = $res->{'kver'} || '?';
358 + my $flags = $res->{'flags'};
359 + print("Version: $kver\n");
360 +
361 + if ($flags & FLAG_PI)
362 + {
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;
367 + }
368 + else
369 + {
370 + print ("* This doesn't look like a Raspberry Pi kernel.\n");
371 + }
372 +}
373 +elsif (!$dtok)
374 +{
375 + print ("* Is this a valid kernel?\n");
376 +}
377 +
378 +if ($append_trailer)
379 +{
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");
384 +
385 + my @atoms;
386 +
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) ];
394 +
395 + $trailer = pack_trailer(\@atoms);
396 + $atoms[0]->[1] = pack('V', length($trailer));
397 +
398 + $trailer = pack_trailer(\@atoms);
399 +}
400 +
401 +my $ofh;
402 +my $total_len = 0;
403 +
404 +if ($out_file eq $kernel_file)
405 +{
406 + die "* Failed to open '$out_file' for append\n"
407 + if (!open($ofh, '>>', $out_file));
408 + $total_len = tell($ofh);
409 +}
410 +else
411 +{
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));
416 +
417 + my $copybuf;
418 + while (1)
419 + {
420 + my $bytes = sysread($ifh, $copybuf, 64*1024);
421 + last if (!$bytes);
422 + syswrite($ofh, $copybuf, $bytes);
423 + $total_len += $bytes;
424 + }
425 + close($ifh);
426 +}
427 +
428 +if ($trailer)
429 +{
430 + # Pad to word-alignment
431 + syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
432 + syswrite($ofh, $trailer);
433 +}
434 +
435 +close($ofh);
436 +
437 +exit($trailer ? 0 : 1);
438 +
439 +END {
440 + unlink($tmpfile1) if ($tmpfile1);
441 + unlink($tmpfile2) if ($tmpfile2);
442 +}
443 +
444 +
445 +sub usage
446 +{
447 + print ("Usage: mkknlimg [--dtok] [--270x] [--283x] <vmlinux|zImage|bzImage> <outfile>\n");
448 + exit(1);
449 +}
450 +
451 +sub try_extract
452 +{
453 + my ($knl, $tmp) = @_;
454 +
455 + my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
456 +
457 + return undef if (!$ver);
458 +
459 + chomp($ver);
460 +
461 + my $res = { 'kver'=>$ver };
462 + $res->{'flags'} = strings_to_flags($knl, $wanted_strings);
463 +
464 + return $res;
465 +}
466 +
467 +
468 +sub try_decompress
469 +{
470 + my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
471 +
472 + my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
473 + if ($pos)
474 + {
475 + chomp($pos);
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));
482 +
483 + return try_extract($tmp2, $tmp1);
484 + }
485 +
486 + return undef;
487 +}
488 +
489 +sub strings_to_flags
490 +{
491 + my ($knl, $strings) = @_;
492 + my $string_pattern = '^('.join('|', keys(%$strings)).')$';
493 + my $flags = 0;
494 +
495 + my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`;
496 + foreach my $match (@matches)
497 + {
498 + chomp($match);
499 + $flags |= $strings->{$match};
500 + }
501 +
502 + return $flags;
503 +}
504 +
505 +sub pack_trailer
506 +{
507 + my ($atoms) = @_;
508 + my $trailer = pack('VV', 0, 0);
509 + for (my $i = $#$atoms; $i>=0; $i--)
510 + {
511 + my $atom = $atoms->[$i];
512 + $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]);
513 + }
514 + return $trailer;
515 +}