]>
Commit | Line | Data |
---|---|---|
29048bce PE |
1 | #!/usr/bin/env perl |
2 | # ---------------------------------------------------------------------- | |
3 | # mkknlimg by Phil Elwell for Raspberry Pi | |
4 | # based on extract-ikconfig by Dick Streefland | |
5 | # | |
6 | # (c) 2009,2010 Dick Streefland <dick@streefland.net> | |
7 | # (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org> | |
8 | # | |
9 | # Licensed under the terms of the GNU General Public License. | |
10 | # ---------------------------------------------------------------------- | |
11 | ||
12 | use strict; | |
13 | use warnings; | |
14 | use integer; | |
15 | ||
16 | use constant FLAG_PI => 0x01; | |
17 | use constant FLAG_DTOK => 0x02; | |
18 | use constant FLAG_DDTK => 0x04; | |
19 | use constant FLAG_270X => 0x08; | |
20 | use constant FLAG_283X => 0x10; | |
21 | ||
22 | my $trailer_magic = 'RPTL'; | |
23 | ||
24 | my $tmpfile1 = "/tmp/mkknlimg_$$.1"; | |
25 | my $tmpfile2 = "/tmp/mkknlimg_$$.2"; | |
26 | ||
27 | my $dtok = 0; | |
28 | my $ddtk = 0; | |
29 | my $is_270x = 0; | |
30 | my $is_283x = 0; | |
31 | ||
32 | while (@ARGV && ($ARGV[0] =~ /^-/)) | |
33 | { | |
34 | my $arg = shift(@ARGV); | |
35 | if ($arg eq '--dtok') | |
36 | { | |
37 | $dtok = 1; | |
38 | } | |
39 | elsif ($arg eq '--ddtk') | |
40 | { | |
41 | $ddtk = 1; | |
42 | } | |
43 | elsif ($arg eq '--270x') | |
44 | { | |
45 | $is_270x = 1; | |
46 | } | |
47 | elsif ($arg eq '--283x') | |
48 | { | |
49 | $is_283x = 1; | |
50 | } | |
51 | else | |
52 | { | |
53 | print ("* Unknown option '$arg'\n"); | |
54 | usage(); | |
55 | } | |
56 | } | |
57 | ||
58 | usage() if (@ARGV != 2); | |
59 | ||
60 | my $kernel_file = $ARGV[0]; | |
61 | my $out_file = $ARGV[1]; | |
62 | ||
63 | if (! -r $kernel_file) | |
64 | { | |
65 | print ("* File '$kernel_file' not found\n"); | |
66 | usage(); | |
67 | } | |
68 | ||
69 | my $wanted_strings = | |
70 | { | |
71 | 'bcm2708_fb' => FLAG_PI | FLAG_270X, | |
72 | 'brcm,bcm2835-mmc' => FLAG_PI, | |
73 | 'brcm,bcm2835-sdhost' => FLAG_PI, | |
74 | 'brcm,bcm2708-pinctrl' => FLAG_PI | FLAG_DTOK, | |
75 | 'brcm,bcm2835-gpio' => FLAG_PI | FLAG_DTOK, | |
76 | 'brcm,bcm2708' => FLAG_PI | FLAG_DTOK | FLAG_270X, | |
77 | 'brcm,bcm2709' => FLAG_PI | FLAG_DTOK | FLAG_270X, | |
78 | 'brcm,bcm2835' => FLAG_PI | FLAG_DTOK | FLAG_283X, | |
79 | 'brcm,bcm2836' => FLAG_PI | FLAG_DTOK | FLAG_283X, | |
80 | 'of_cfs_init' => FLAG_DTOK | FLAG_DDTK, | |
81 | 'vc_cma_init' => FLAG_PI | FLAG_270X, | |
82 | 'vc-mem' => FLAG_PI | FLAG_270X, | |
83 | }; | |
84 | ||
85 | my $res = try_extract($kernel_file, $tmpfile1); | |
86 | $res ||= try_decompress('\037\213\010', 'xy', 'gunzip', 0, | |
87 | $kernel_file, $tmpfile1, $tmpfile2); | |
88 | $res ||= try_decompress('\3757zXZ\000', 'abcde', 'unxz --single-stream', -1, | |
89 | $kernel_file, $tmpfile1, $tmpfile2); | |
90 | $res ||= try_decompress('BZh', 'xy', 'bunzip2', 0, | |
91 | $kernel_file, $tmpfile1, $tmpfile2); | |
92 | $res ||= try_decompress('\135\0\0\0', 'xxx', 'unlzma', 0, | |
93 | $kernel_file, $tmpfile1, $tmpfile2); | |
94 | $res ||= try_decompress('\211\114\132', 'xy', 'lzop -d', 0, | |
95 | $kernel_file, $tmpfile1, $tmpfile2); | |
96 | $res ||= try_decompress('\002\041\114\030', 'xy', 'lz4 -d', 1, | |
97 | $kernel_file, $tmpfile1, $tmpfile2); | |
98 | ||
99 | my $append_trailer; | |
100 | my $trailer; | |
101 | my $kver = '?'; | |
102 | ||
103 | $append_trailer = 1; | |
104 | ||
105 | if ($res) | |
106 | { | |
107 | $kver = $res->{'kver'} || '?'; | |
108 | my $flags = $res->{'flags'}; | |
109 | print("Version: $kver\n"); | |
110 | ||
111 | if ($flags & FLAG_PI) | |
112 | { | |
113 | $dtok ||= ($flags & FLAG_DTOK) != 0; | |
114 | $is_270x ||= ($flags & FLAG_270X) != 0; | |
115 | $is_283x ||= ($flags & FLAG_283X) != 0; | |
116 | $ddtk ||= ($flags & FLAG_DDTK) != 0; | |
117 | } | |
118 | else | |
119 | { | |
120 | print ("* This doesn't look like a Raspberry Pi kernel.\n"); | |
121 | } | |
122 | } | |
123 | elsif (!$dtok) | |
124 | { | |
125 | print ("* Is this a valid kernel?\n"); | |
126 | } | |
127 | ||
128 | if ($append_trailer) | |
129 | { | |
130 | printf("DT: %s\n", $dtok ? "y" : "n"); | |
131 | printf("DDT: %s\n", $ddtk ? "y" : "n"); | |
132 | printf("270x: %s\n", $is_270x ? "y" : "n"); | |
133 | printf("283x: %s\n", $is_283x ? "y" : "n"); | |
134 | ||
135 | my @atoms; | |
136 | ||
137 | push @atoms, [ $trailer_magic, pack('V', 0) ]; | |
138 | push @atoms, [ 'KVer', $kver ]; | |
139 | push @atoms, [ 'DTOK', pack('V', $dtok) ]; | |
140 | push @atoms, [ 'DDTK', pack('V', $ddtk) ]; | |
141 | push @atoms, [ '270X', pack('V', $is_270x) ]; | |
142 | push @atoms, [ '283X', pack('V', $is_283x) ]; | |
143 | push @atoms, [ '283x', pack('V', $is_283x && !$is_270x) ]; | |
144 | ||
145 | $trailer = pack_trailer(\@atoms); | |
146 | $atoms[0]->[1] = pack('V', length($trailer)); | |
147 | ||
148 | $trailer = pack_trailer(\@atoms); | |
149 | } | |
150 | ||
151 | my $ofh; | |
152 | my $total_len = 0; | |
153 | ||
154 | if ($out_file eq $kernel_file) | |
155 | { | |
156 | die "* Failed to open '$out_file' for append\n" | |
157 | if (!open($ofh, '>>', $out_file)); | |
158 | $total_len = tell($ofh); | |
159 | } | |
160 | else | |
161 | { | |
162 | die "* Failed to open '$kernel_file'\n" | |
163 | if (!open(my $ifh, '<', $kernel_file)); | |
164 | die "* Failed to create '$out_file'\n" | |
165 | if (!open($ofh, '>', $out_file)); | |
166 | ||
167 | my $copybuf; | |
168 | while (1) | |
169 | { | |
170 | my $bytes = sysread($ifh, $copybuf, 64*1024); | |
171 | last if (!$bytes); | |
172 | syswrite($ofh, $copybuf, $bytes); | |
173 | $total_len += $bytes; | |
174 | } | |
175 | close($ifh); | |
176 | } | |
177 | ||
178 | if ($trailer) | |
179 | { | |
180 | # Pad to word-alignment | |
181 | syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3)); | |
182 | syswrite($ofh, $trailer); | |
183 | } | |
184 | ||
185 | close($ofh); | |
186 | ||
187 | exit($trailer ? 0 : 1); | |
188 | ||
189 | END { | |
190 | unlink($tmpfile1) if ($tmpfile1); | |
191 | unlink($tmpfile2) if ($tmpfile2); | |
192 | } | |
193 | ||
194 | ||
195 | sub usage | |
196 | { | |
197 | print ("Usage: mkknlimg [--dtok] [--270x] [--283x] <vmlinux|zImage|bzImage> <outfile>\n"); | |
198 | exit(1); | |
199 | } | |
200 | ||
201 | sub try_extract | |
202 | { | |
203 | my ($knl, $tmp) = @_; | |
204 | ||
205 | my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`; | |
206 | ||
207 | return undef if (!$ver); | |
208 | ||
209 | chomp($ver); | |
210 | ||
211 | my $res = { 'kver'=>$ver }; | |
212 | $res->{'flags'} = strings_to_flags($knl, $wanted_strings); | |
213 | ||
214 | return $res; | |
215 | } | |
216 | ||
217 | ||
218 | sub try_decompress | |
219 | { | |
220 | my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_; | |
221 | ||
222 | my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`; | |
223 | if ($pos) | |
224 | { | |
225 | chomp($pos); | |
226 | $pos = (split(/[\r\n]+/, $pos))[$idx]; | |
227 | return undef if (!defined($pos)); | |
228 | $pos =~ s/:.*[\r\n]*$//s; | |
229 | my $cmd = "tail -c+$pos \"$knl\" | $zcat > $tmp2 2> /dev/null"; | |
230 | my $err = (system($cmd) >> 8); | |
231 | return undef if (($err != 0) && ($err != 2)); | |
232 | ||
233 | return try_extract($tmp2, $tmp1); | |
234 | } | |
235 | ||
236 | return undef; | |
237 | } | |
238 | ||
239 | sub strings_to_flags | |
240 | { | |
241 | my ($knl, $strings) = @_; | |
242 | my $string_pattern = '^('.join('|', keys(%$strings)).')$'; | |
243 | my $flags = 0; | |
244 | ||
245 | my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`; | |
246 | foreach my $match (@matches) | |
247 | { | |
248 | chomp($match); | |
249 | $flags |= $strings->{$match}; | |
250 | } | |
251 | ||
252 | return $flags; | |
253 | } | |
254 | ||
255 | sub pack_trailer | |
256 | { | |
257 | my ($atoms) = @_; | |
258 | my $trailer = pack('VV', 0, 0); | |
259 | for (my $i = $#$atoms; $i>=0; $i--) | |
260 | { | |
261 | my $atom = $atoms->[$i]; | |
262 | $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]); | |
263 | } | |
264 | return $trailer; | |
265 | } |