6 my $trailer_magic = 'RPTL';
8 my $tmpfile1 = "/tmp/mkknlimg_$$.1";
9 my $tmpfile2 = "/tmp/mkknlimg_$$.2";
13 while ($ARGV[0] =~ /^-/)
15 my $arg = shift(@ARGV);
22 print ("* Unknown option '$arg'\n");
27 usage
() if (@ARGV != 2);
29 my $kernel_file = $ARGV[0];
30 my $out_file = $ARGV[1];
32 if (! -r
$kernel_file)
34 print ("* File '$kernel_file' not found\n");
38 my @wanted_config_lines =
46 'brcm,bcm2708-pinctrl',
51 my $res = try_extract
($kernel_file, $tmpfile1);
52 $res = try_decompress
('\037\213\010', 'xy', 'gunzip', 0,
53 $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
54 $res = try_decompress
('\3757zXZ\000', 'abcde', 'unxz --single-stream', -1,
55 $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
56 $res = try_decompress
('BZh', 'xy', 'bunzip2', 0,
57 $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
58 $res = try_decompress
('\135\0\0\0', 'xxx', 'unlzma', 0,
59 $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
60 $res = try_decompress
('\211\114\132', 'xy', 'lzop -d', 0,
61 $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
62 $res = try_decompress
('\002\041\114\030', 'xy', 'lz4 -d', 1,
63 $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
68 $append_trailer = $dtok;
72 print("Version: $res->{''}\n");
74 $append_trailer = $dtok;
77 if (config_bool
($res, 'bcm2708_fb'))
79 $dtok ||= config_bool
($res, 'CONFIG_BCM2708_DT');
80 $dtok ||= config_bool
($res, 'brcm,bcm2708-pinctrl');
81 $dtok ||= config_bool
($res, 'brcm,bcm2835-gpio');
86 print ("* This doesn't look like a Raspberry Pi kernel. In pass-through mode.\n");
92 print ("* Is this a valid kernel? In pass-through mode.\n");
97 printf("DT: %s\n", $dtok ?
"y" : "n");
101 push @atoms, [ $trailer_magic, pack('V', 0) ];
102 push @atoms, [ 'KVer', $res->{''} ];
103 push @atoms, [ 'DTOK', pack('V', $dtok) ];
105 $trailer = pack_trailer
(\
@atoms);
106 $atoms[0]->[1] = pack('V', length($trailer));
108 $trailer = pack_trailer
(\
@atoms);
114 if ($out_file eq $kernel_file)
116 die "* Failed to open '$out_file' for append\n"
117 if (!open($ofh, '>>', $out_file));
118 $total_len = tell($ofh);
122 die "* Failed to open '$kernel_file'\n"
123 if (!open(my $ifh, '<', $kernel_file));
124 die "* Failed to create '$out_file'\n"
125 if (!open($ofh, '>', $out_file));
130 my $bytes = sysread($ifh, $copybuf, 64*1024);
132 syswrite($ofh, $copybuf, $bytes);
133 $total_len += $bytes;
140 # Pad to word-alignment
141 syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
142 syswrite($ofh, $trailer);
147 exit($trailer ?
0 : 1);
150 unlink($tmpfile1) if ($tmpfile1);
151 unlink($tmpfile2) if ($tmpfile2);
157 print ("Usage: mkknlimg [--dtok] <vmlinux|zImage|bzImage> <outfile>\n");
163 my ($knl, $tmp) = @_;
165 my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
167 return undef if (!$ver);
171 my $res = { ''=>$ver };
172 my $string_pattern = '^('.join('|', @wanted_strings).')$';
174 my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`;
175 foreach my $match (@matches)
181 my $config_pattern = '^('.join('|', @wanted_config_lines).')=(.*)$';
182 my $cf1 = 'IKCFG_ST\037\213\010';
183 my $cf2 = '0123456789';
185 my $pos = `tr "$cf1\n$cf2" "\n$cf2=" < "$knl" | grep -abo "^$cf2"`;
188 $pos =~ s/:.*[\r\n]*$//s;
190 my $err = (system("tail -c+$pos \"$knl\" | zcat > $tmp 2> /dev/null") >> 8);
191 if (($err == 0) || ($err == 2))
193 if (open(my $fh, '<', $tmp))
195 while (my $line = <$fh>)
198 $res->{$1} = $2 if ($line =~ /$config_pattern/);
212 my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
214 my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
218 $pos = (split(/[\r\n]+/, $pos))[$idx];
219 $pos =~ s/:.*[\r\n]*$//s;
220 my $cmd = "tail -c+$pos \"$knl\" | $zcat > $tmp2 2> /dev/null";
221 my $err = (system($cmd) >> 8);
222 return undef if (($err != 0) && ($err != 2));
224 return try_extract
($tmp2, $tmp1);
233 my $trailer = pack('VV', 0, 0);
234 for (my $i = $#$atoms; $i>=0; $i--)
236 my $atom = $atoms->[$i];
237 $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]);
244 my ($configs, $wanted) = @_;
245 return (($configs->{$wanted} eq 'y') || ($configs->{$wanted} eq '1'));