add dvb-firmware package
[openadk.git] / scripts / mkknlimg
blobcfd76d56975d602fe3085c13a091498febe99bdd
1 #!/usr/bin/env perl
3 use strict;
4 use integer;
6 my $trailer_magic = 'RPTL';
8 my $tmpfile1 = "/tmp/mkknlimg_$$.1";
9 my $tmpfile2 = "/tmp/mkknlimg_$$.2";
11 my $dtok = 0;
13 while ($ARGV[0] =~ /^-/)
15 my $arg = shift(@ARGV);
16 if ($arg eq '--dtok')
18 $dtok = 1;
20 else
22 print ("* Unknown option '$arg'\n");
23 usage();
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");
35 usage();
38 my @wanted_config_lines =
40 'CONFIG_BCM2708_DT'
43 my @wanted_strings =
45 'bcm2708_fb',
46 'brcm,bcm2708-pinctrl',
47 'brcm,bcm2835-gpio',
48 'of_find_property'
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);
65 my $append_trailer;
66 my $trailer;
68 $append_trailer = $dtok;
70 if ($res)
72 print("Version: $res->{''}\n");
74 $append_trailer = $dtok;
75 if (!$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');
82 $append_trailer = 1;
84 else
86 print ("* This doesn't look like a Raspberry Pi kernel. In pass-through mode.\n");
90 elsif (!$dtok)
92 print ("* Is this a valid kernel? In pass-through mode.\n");
95 if ($append_trailer)
97 printf("DT: %s\n", $dtok ? "y" : "n");
99 my @atoms;
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);
111 my $ofh;
112 my $total_len = 0;
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);
120 else
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));
127 my $copybuf;
128 while (1)
130 my $bytes = sysread($ifh, $copybuf, 64*1024);
131 last if (!$bytes);
132 syswrite($ofh, $copybuf, $bytes);
133 $total_len += $bytes;
135 close($ifh);
138 if ($trailer)
140 # Pad to word-alignment
141 syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
142 syswrite($ofh, $trailer);
145 close($ofh);
147 exit($trailer ? 0 : 1);
149 END {
150 unlink($tmpfile1) if ($tmpfile1);
151 unlink($tmpfile2) if ($tmpfile2);
155 sub usage
157 print ("Usage: mkknlimg [--dtok] <vmlinux|zImage|bzImage> <outfile>\n");
158 exit(1);
161 sub try_extract
163 my ($knl, $tmp) = @_;
165 my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
167 return undef if (!$ver);
169 chomp($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)
177 chomp($match);
178 $res->{$match} = 1;
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"`;
186 if ($pos)
188 $pos =~ s/:.*[\r\n]*$//s;
189 $pos += 8;
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>)
197 chomp($line);
198 $res->{$1} = $2 if ($line =~ /$config_pattern/);
201 close($fh);
206 return $res;
210 sub try_decompress
212 my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
214 my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
215 if ($pos)
217 chomp($pos);
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);
227 return undef;
230 sub pack_trailer
232 my ($atoms) = @_;
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]);
239 return $trailer;
242 sub config_bool
244 my ($configs, $wanted) = @_;
245 return (($configs->{$wanted} eq 'y') || ($configs->{$wanted} eq '1'));