Skip to content

Commit a98c2f7

Browse files
Phil Elwellpopcornmix
Phil Elwell
authored andcommitted
scripts: Add mkknlimg and knlinfo scripts from tools repo
The Raspberry Pi firmware looks for a trailer on the kernel image to determine whether it was compiled with Device Tree support enabled. If the firmware finds a kernel without this trailer, or which has a trailer indicating that it isn't DT-capable, it disables DT support and reverts to using ATAGs. The mkknlimg utility adds that trailer, having first analysed the image to look for signs of DT support and the kernel version string. knlinfo displays the contents of the trailer in the given kernel image. scripts/mkknlimg: Add support for ARCH_BCM2835 Add a new trailer field indicating whether this is an ARCH_BCM2835 build, as opposed to MACH_BCM2708/9. If the loader finds this flag is set it changes the default base dtb file name from bcm270x... to bcm283y... Also update knlinfo to show the status of the field. scripts/mkknlimg: Improve ARCH_BCM2835 detection The board support code contains sufficient strings to be able to distinguish 2708 vs. 2835 builds, so remove the check for bcm2835-pm-wdt which could exist in either. Also, since the canned configuration is no longer built in (it's a module), remove the config string checking. See: #1157
1 parent f4ccd72 commit a98c2f7

File tree

2 files changed

+412
-0
lines changed

2 files changed

+412
-0
lines changed

scripts/knlinfo

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/usr/bin/env perl
2+
# ----------------------------------------------------------------------
3+
# knlinfo by Phil Elwell for Raspberry Pi
4+
#
5+
# (c) 2014,2015 Raspberry Pi (Trading) Limited <[email protected]>
6+
#
7+
# Licensed under the terms of the GNU General Public License.
8+
# ----------------------------------------------------------------------
9+
10+
use strict;
11+
use integer;
12+
13+
use Fcntl ":seek";
14+
15+
my $trailer_magic = 'RPTL';
16+
17+
my %atom_formats =
18+
(
19+
'DTOK' => \&format_bool,
20+
'KVer' => \&format_string,
21+
'283x' => \&format_bool,
22+
);
23+
24+
if (@ARGV != 1)
25+
{
26+
print ("Usage: knlinfo <kernel image>\n");
27+
exit(1);
28+
}
29+
30+
my $kernel_file = $ARGV[0];
31+
32+
33+
my ($atoms, $pos) = read_trailer($kernel_file);
34+
35+
exit(1) if (!$atoms);
36+
37+
printf("Kernel trailer found at %d/0x%x:\n", $pos, $pos);
38+
39+
foreach my $atom (@$atoms)
40+
{
41+
printf(" %s: %s\n", $atom->[0], format_atom($atom));
42+
}
43+
44+
exit(0);
45+
46+
sub read_trailer
47+
{
48+
my ($kernel_file) = @_;
49+
my $fh;
50+
51+
if (!open($fh, '<', $kernel_file))
52+
{
53+
print ("* Failed to open '$kernel_file'\n");
54+
return undef;
55+
}
56+
57+
if (!seek($fh, -12, SEEK_END))
58+
{
59+
print ("* seek error in '$kernel_file'\n");
60+
return undef;
61+
}
62+
63+
my $last_bytes;
64+
sysread($fh, $last_bytes, 12);
65+
66+
my ($trailer_len, $data_len, $magic) = unpack('VVa4', $last_bytes);
67+
68+
if (($magic ne $trailer_magic) || ($data_len != 4))
69+
{
70+
print ("* no trailer\n");
71+
return undef;
72+
}
73+
if (!seek($fh, -12, SEEK_END))
74+
{
75+
print ("* seek error in '$kernel_file'\n");
76+
return undef;
77+
}
78+
79+
$trailer_len -= 12;
80+
81+
while ($trailer_len > 0)
82+
{
83+
if ($trailer_len < 8)
84+
{
85+
print ("* truncated atom header in trailer\n");
86+
return undef;
87+
}
88+
if (!seek($fh, -8, SEEK_CUR))
89+
{
90+
print ("* seek error in '$kernel_file'\n");
91+
return undef;
92+
}
93+
$trailer_len -= 8;
94+
95+
my $atom_hdr;
96+
sysread($fh, $atom_hdr, 8);
97+
my ($atom_len, $atom_type) = unpack('Va4', $atom_hdr);
98+
99+
if ($trailer_len < $atom_len)
100+
{
101+
print ("* truncated atom data in trailer\n");
102+
return undef;
103+
}
104+
105+
my $rounded_len = (($atom_len + 3) & ~3);
106+
if (!seek($fh, -(8 + $rounded_len), SEEK_CUR))
107+
{
108+
print ("* seek error in '$kernel_file'\n");
109+
return undef;
110+
}
111+
$trailer_len -= $rounded_len;
112+
113+
my $atom_data;
114+
sysread($fh, $atom_data, $atom_len);
115+
116+
if (!seek($fh, -$atom_len, SEEK_CUR))
117+
{
118+
print ("* seek error in '$kernel_file'\n");
119+
return undef;
120+
}
121+
122+
push @$atoms, [ $atom_type, $atom_data ];
123+
}
124+
125+
if (($$atoms[-1][0] eq "\x00\x00\x00\x00") &&
126+
($$atoms[-1][1] eq ""))
127+
{
128+
pop @$atoms;
129+
}
130+
else
131+
{
132+
print ("* end marker missing from trailer\n");
133+
}
134+
135+
return ($atoms, tell($fh));
136+
}
137+
138+
sub format_atom
139+
{
140+
my ($atom) = @_;
141+
142+
my $format_func = $atom_formats{$atom->[0]} || \&format_hex;
143+
return $format_func->($atom->[1]);
144+
}
145+
146+
sub format_bool
147+
{
148+
my ($data) = @_;
149+
return unpack('V', $data) ? 'true' : 'false';
150+
}
151+
152+
sub format_int
153+
{
154+
my ($data) = @_;
155+
return unpack('V', $data);
156+
}
157+
158+
sub format_string
159+
{
160+
my ($data) = @_;
161+
return '"'.$data.'"';
162+
}
163+
164+
sub format_hex
165+
{
166+
my ($data) = @_;
167+
return unpack('H*', $data);
168+
}

0 commit comments

Comments
 (0)