]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - scripts/mkknlimg
2 # ----------------------------------------------------------------------
3 # mkknlimg by Phil Elwell for Raspberry Pi
4 # based on extract-ikconfig by Dick Streefland
6 # (c) 2009,2010 Dick Streefland <dick@streefland.net>
7 # (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
9 # Licensed under the terms of the GNU General Public License.
10 # ----------------------------------------------------------------------
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;
22 my $trailer_magic = 'RPTL';
24 my $tmpfile1 = "/tmp/mkknlimg_$$.1";
25 my $tmpfile2 = "/tmp/mkknlimg_$$.2";
32 while (@ARGV && ($ARGV[0] =~ /^-/))
34 my $arg = shift(@ARGV);
39 elsif ($arg eq '--ddtk')
43 elsif ($arg eq '--270x')
47 elsif ($arg eq '--283x')
53 print ("* Unknown option '$arg'\n");
58 usage
() if (@ARGV != 2);
60 my $kernel_file = $ARGV[0];
61 my $out_file = $ARGV[1];
63 if (! -r
$kernel_file)
65 print ("* File '$kernel_file' not found\n");
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
,
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);
107 $kver = $res->{'kver'} || '?';
108 my $flags = $res->{'flags'};
109 print("Version: $kver\n");
111 if ($flags & FLAG_PI
)
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;
120 print ("* This doesn't look like a Raspberry Pi kernel.\n");
125 print ("* Is this a valid kernel?\n");
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");
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) ];
145 $trailer = pack_trailer
(\
@atoms);
146 $atoms[0]->[1] = pack('V', length($trailer));
148 $trailer = pack_trailer
(\
@atoms);
154 if ($out_file eq $kernel_file)
156 die "* Failed to open '$out_file' for append\n"
157 if (!open($ofh, '>>', $out_file));
158 $total_len = tell($ofh);
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));
170 my $bytes = sysread($ifh, $copybuf, 64*1024);
172 syswrite($ofh, $copybuf, $bytes);
173 $total_len += $bytes;
180 # Pad to word-alignment
181 syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
182 syswrite($ofh, $trailer);
187 exit($trailer ?
0 : 1);
190 unlink($tmpfile1) if ($tmpfile1);
191 unlink($tmpfile2) if ($tmpfile2);
197 print ("Usage: mkknlimg [--dtok] [--270x] [--283x] <vmlinux|zImage|bzImage> <outfile>\n");
203 my ($knl, $tmp) = @_;
205 my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
207 return undef if (!$ver);
211 my $res = { 'kver'=>$ver };
212 $res->{'flags'} = strings_to_flags
($knl, $wanted_strings);
220 my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
222 my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
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));
233 return try_extract($tmp2, $tmp1);
241 my ($knl, $strings) = @_;
242 my $string_pattern = '^('.join('|', keys(%$strings)).')$';
245 my @matches = `strings \"$knl\" | grep -E
\"$string_pattern\"`;
246 foreach my $match (@matches)
249 $flags |= $strings->{$match};
258 my $trailer = pack('VV', 0, 0);
259 for (my $i = $#$atoms; $i>=0; $i--)
261 my $atom = $atoms->[$i];
262 $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]);