at-spi2-atk: fix packaging issue
[openadk.git] / scripts / mkknlimg
blob33f81874bf1c8deead34f7fe63debcbb27eff034
1 #!/usr/bin/env perl
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 # ----------------------------------------------------------------------
16 use strict;
17 use warnings;
18 use integer;
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";
30 my $dtok = 0;
31 my $ddtk = 0;
32 my $is_283x = 0;
34 while (@ARGV && ($ARGV[0] =~ /^-/))
36 my $arg = shift(@ARGV);
37 if ($arg eq '--dtok')
39 $dtok = 1;
41 elsif ($arg eq '--ddtk')
43 $ddtk = 1;
45 elsif ($arg eq '--283x')
47 $is_283x = 1;
49 else
51 print ("* Unknown option '$arg'\n");
52 usage();
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");
64 usage();
67 my $wanted_configs =
69 'CONFIG_BCM2708_DT' => FLAG_PI | FLAG_DTOK,
70 'CONFIG_ARCH_BCM2835' => FLAG_PI | FLAG_DTOK | FLAG_283X,
73 my $wanted_strings =
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);
98 my $append_trailer;
99 my $trailer;
100 my $kver = '?';
102 $append_trailer = $dtok;
104 if ($res)
106 $kver = $res->{'kver'} || '?';
107 my $flags = $res->{'flags'};
108 print("Version: $kver\n");
110 if ($flags & FLAG_PI)
112 $append_trailer = 1;
113 $dtok ||= ($flags & FLAG_DTOK) != 0;
114 $is_283x ||= ($flags & FLAG_283X) != 0;
115 $ddtk ||= ($flags & FLAG_DDTK) != 0;
117 else
119 print ("* This doesn't look like a Raspberry Pi kernel. In pass-through mode.\n");
122 elsif (!$dtok)
124 print ("* Is this a valid kernel? In pass-through mode.\n");
127 if ($append_trailer)
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");
133 my @atoms;
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);
147 my $ofh;
148 my $total_len = 0;
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);
156 else
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));
163 my $copybuf;
164 while (1)
166 my $bytes = sysread($ifh, $copybuf, 64*1024);
167 last if (!$bytes);
168 syswrite($ofh, $copybuf, $bytes);
169 $total_len += $bytes;
171 close($ifh);
174 if ($trailer)
176 # Pad to word-alignment
177 syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
178 syswrite($ofh, $trailer);
181 close($ofh);
183 exit($trailer ? 0 : 1);
185 END {
186 unlink($tmpfile1) if ($tmpfile1);
187 unlink($tmpfile2) if ($tmpfile2);
191 sub usage
193 print ("Usage: mkknlimg [--dtok] [--283x] <vmlinux|zImage|bzImage> <outfile>\n");
194 exit(1);
197 sub try_extract
199 my ($knl, $tmp) = @_;
201 my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
203 return undef if (!$ver);
205 chomp($ver);
207 my $res = { 'kver'=>$ver };
208 $res->{'flags'} = strings_to_flags($knl, $wanted_strings) |
209 configs_to_flags($knl, $tmp, $wanted_configs);
211 return $res;
215 sub try_decompress
217 my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
219 my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
220 if ($pos)
222 chomp($pos);
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);
233 return undef;
237 sub strings_to_flags
239 my ($knl, $strings) = @_;
240 my $string_pattern = '^('.join('|', keys(%$strings)).')$';
241 my $flags = 0;
243 my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`;
244 foreach my $match (@matches)
246 chomp($match);
247 $flags |= $strings->{$match};
250 return $flags;
253 sub configs_to_flags
255 my ($knl, $tmp, $configs) = @_;
256 my $config_pattern = '^('.join('|', keys(%$configs)).')=(.*)$';
257 my $cf1 = 'IKCFG_ST\037\213\010';
258 my $cf2 = '0123456789';
259 my $flags = 0;
261 my $pos = `tr "$cf1\n$cf2" "\n$cf2=" < "$knl" | grep -abo "^$cf2"`;
262 if ($pos)
264 $pos =~ s/:.*[\r\n]*$//s;
265 $pos += 8;
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>)
273 chomp($line);
274 if (($line =~ /$config_pattern/) &&
275 (($2 eq 'y') || ($2 eq 'm')))
277 $flags |= $configs->{$1};
281 close($fh);
286 return $flags;
289 sub pack_trailer
291 my ($atoms) = @_;
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]);
298 return $trailer;