3 # Originaly from: https://github.com/raspberrypi/tools/blob/master/mkimage/mkknlimg
4 # Original cset : f5642106425d430e1f82ee064121a5fd0e05a386
6 # ----------------------------------------------------------------------
7 # mkknlimg by Phil Elwell for Raspberry Pi
8 # based on extract-ikconfig by Dick Streefland
10 # (c) 2009,2010 Dick Streefland <dick@streefland.net>
11 # (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
13 # Licensed under the terms of the GNU General Public License.
14 # ----------------------------------------------------------------------
20 use constant FLAG_PI
=> 1;
21 use constant FLAG_DTOK
=> 2;
22 use constant FLAG_DDTK
=> 4;
23 use constant FLAG_283X
=> 8;
25 my $trailer_magic = 'RPTL';
27 my $tmpfile1 = "/tmp/mkknlimg_$$.1";
28 my $tmpfile2 = "/tmp/mkknlimg_$$.2";
34 while (@ARGV && ($ARGV[0] =~ /^-/))
36 my $arg = shift(@ARGV);
41 elsif ($arg eq '--ddtk')
45 elsif ($arg eq '--283x')
51 print ("* Unknown option '$arg'\n");
56 usage
() if (@ARGV != 2);
58 my $kernel_file = $ARGV[0];
59 my $out_file = $ARGV[1];
61 if (! -r
$kernel_file)
63 print ("* File '$kernel_file' not found\n");
69 'CONFIG_BCM2708_DT' => FLAG_PI
| FLAG_DTOK
,
70 'CONFIG_ARCH_BCM2835' => FLAG_PI
| FLAG_DTOK
| FLAG_283X
,
75 'bcm2708_fb' => FLAG_PI
,
76 'brcm,bcm2835-mmc' => FLAG_PI
,
77 'brcm,bcm2835-sdhost' => FLAG_PI
,
78 'brcm,bcm2708-pinctrl' => FLAG_PI
| FLAG_DTOK
,
79 'brcm,bcm2835-gpio' => FLAG_PI
| FLAG_DTOK
,
80 'brcm,bcm2835-pm-wdt' => FLAG_PI
| FLAG_DTOK
| FLAG_283X
,
81 'of_overlay_apply' => FLAG_DTOK
| FLAG_DDTK
,
84 my $res = try_extract
($kernel_file, $tmpfile1);
85 $res ||= try_decompress
('\037\213\010', 'xy', 'gunzip', 0,
86 $kernel_file, $tmpfile1, $tmpfile2);
87 $res ||= try_decompress
('\3757zXZ\000', 'abcde', 'unxz --single-stream', -1,
88 $kernel_file, $tmpfile1, $tmpfile2);
89 $res ||= try_decompress
('BZh', 'xy', 'bunzip2', 0,
90 $kernel_file, $tmpfile1, $tmpfile2);
91 $res ||= try_decompress
('\135\0\0\0', 'xxx', 'unlzma', 0,
92 $kernel_file, $tmpfile1, $tmpfile2);
93 $res ||= try_decompress
('\211\114\132', 'xy', 'lzop -d', 0,
94 $kernel_file, $tmpfile1, $tmpfile2);
95 $res ||= try_decompress
('\002\041\114\030', 'xy', 'lz4 -d', 1,
96 $kernel_file, $tmpfile1, $tmpfile2);
102 $append_trailer = $dtok;
106 $kver = $res->{'kver'} || '?';
107 my $flags = $res->{'flags'};
108 print("Version: $kver\n");
110 if ($flags & FLAG_PI
)
113 $dtok ||= ($flags & FLAG_DTOK
) != 0;
114 $is_283x ||= ($flags & FLAG_283X
) != 0;
115 $ddtk ||= ($flags & FLAG_DDTK
) != 0;
119 print ("* This doesn't look like a Raspberry Pi kernel. In pass-through mode.\n");
124 print ("* Is this a valid kernel? In pass-through mode.\n");
129 printf("DT: %s\n", $dtok ?
"y" : "n");
130 printf("DDT: %s\n", $ddtk ?
"y" : "n");
131 printf("283x: %s\n", $is_283x ?
"y" : "n");
135 push @atoms, [ $trailer_magic, pack('V', 0) ];
136 push @atoms, [ 'KVer', $kver ];
137 push @atoms, [ 'DTOK', pack('V', $dtok) ];
138 push @atoms, [ 'DDTK', pack('V', $ddtk) ];
139 push @atoms, [ '283x', pack('V', $is_283x) ];
141 $trailer = pack_trailer
(\
@atoms);
142 $atoms[0]->[1] = pack('V', length($trailer));
144 $trailer = pack_trailer
(\
@atoms);
150 if ($out_file eq $kernel_file)
152 die "* Failed to open '$out_file' for append\n"
153 if (!open($ofh, '>>', $out_file));
154 $total_len = tell($ofh);
158 die "* Failed to open '$kernel_file'\n"
159 if (!open(my $ifh, '<', $kernel_file));
160 die "* Failed to create '$out_file'\n"
161 if (!open($ofh, '>', $out_file));
166 my $bytes = sysread($ifh, $copybuf, 64*1024);
168 syswrite($ofh, $copybuf, $bytes);
169 $total_len += $bytes;
176 # Pad to word-alignment
177 syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
178 syswrite($ofh, $trailer);
183 exit($trailer ?
0 : 1);
186 unlink($tmpfile1) if ($tmpfile1);
187 unlink($tmpfile2) if ($tmpfile2);
193 print ("Usage: mkknlimg [--dtok] [--283x] <vmlinux|zImage|bzImage> <outfile>\n");
199 my ($knl, $tmp) = @_;
201 my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
203 return undef if (!$ver);
207 my $res = { 'kver'=>$ver };
208 $res->{'flags'} = strings_to_flags
($knl, $wanted_strings) |
209 configs_to_flags
($knl, $tmp, $wanted_configs);
217 my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
219 my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
223 $pos = (split(/[\r\n]+/, $pos))[$idx];
224 return undef if (!defined($pos));
225 $pos =~ s/:.*[\r\n]*$//s;
226 my $cmd = "tail -c+$pos \"$knl\" | $zcat > $tmp2 2> /dev/null";
227 my $err = (system($cmd) >> 8);
228 return undef if (($err != 0) && ($err != 2));
230 return try_extract
($tmp2, $tmp1);
239 my ($knl, $strings) = @_;
240 my $string_pattern = '^('.join('|', keys(%$strings)).')$';
243 my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`;
244 foreach my $match (@matches)
247 $flags |= $strings->{$match};
255 my ($knl, $tmp, $configs) = @_;
256 my $config_pattern = '^('.join('|', keys(%$configs)).')=(.*)$';
257 my $cf1 = 'IKCFG_ST\037\213\010';
258 my $cf2 = '0123456789';
261 my $pos = `tr "$cf1\n$cf2" "\n$cf2=" < "$knl" | grep -abo "^$cf2"`;
264 $pos =~ s/:.*[\r\n]*$//s;
266 my $err = (system("tail -c+$pos \"$knl\" | zcat > $tmp 2> /dev/null") >> 8);
267 if (($err == 0) || ($err == 2))
269 if (open(my $fh, '<', $tmp))
271 while (my $line = <$fh>)
274 if (($line =~ /$config_pattern/) &&
275 (($2 eq 'y') || ($2 eq 'm')))
277 $flags |= $configs->{$1};
292 my $trailer = pack('VV', 0, 0);
293 for (my $i = $#$atoms; $i>=0; $i--)
295 my $atom = $atoms->[$i];
296 $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]);