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