]>
git.proxmox.com Git - mirror_ubuntu-zesty-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
,
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
,
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);
106 $kver = $res->{'kver'} || '?';
107 my $flags = $res->{'flags'};
108 print("Version: $kver\n");
110 if ($flags & FLAG_PI
)
112 $dtok ||= ($flags & FLAG_DTOK
) != 0;
113 $is_270x ||= ($flags & FLAG_270X
) != 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.\n");
124 print ("* Is this a valid kernel?\n");
129 printf("DT: %s\n", $dtok ?
"y" : "n");
130 printf("DDT: %s\n", $ddtk ?
"y" : "n");
131 printf("270x: %s\n", $is_270x ?
"y" : "n");
132 printf("283x: %s\n", $is_283x ?
"y" : "n");
136 push @atoms, [ $trailer_magic, pack('V', 0) ];
137 push @atoms, [ 'KVer', $kver ];
138 push @atoms, [ 'DTOK', pack('V', $dtok) ];
139 push @atoms, [ 'DDTK', pack('V', $ddtk) ];
140 push @atoms, [ '270X', pack('V', $is_270x) ];
141 push @atoms, [ '283X', pack('V', $is_283x) ];
142 push @atoms, [ '283x', pack('V', $is_283x && !$is_270x) ];
144 $trailer = pack_trailer
(\
@atoms);
145 $atoms[0]->[1] = pack('V', length($trailer));
147 $trailer = pack_trailer
(\
@atoms);
153 if ($out_file eq $kernel_file)
155 die "* Failed to open '$out_file' for append\n"
156 if (!open($ofh, '>>', $out_file));
157 $total_len = tell($ofh);
161 die "* Failed to open '$kernel_file'\n"
162 if (!open(my $ifh, '<', $kernel_file));
163 die "* Failed to create '$out_file'\n"
164 if (!open($ofh, '>', $out_file));
169 my $bytes = sysread($ifh, $copybuf, 64*1024);
171 syswrite($ofh, $copybuf, $bytes);
172 $total_len += $bytes;
179 # Pad to word-alignment
180 syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
181 syswrite($ofh, $trailer);
186 exit($trailer ?
0 : 1);
189 unlink($tmpfile1) if ($tmpfile1);
190 unlink($tmpfile2) if ($tmpfile2);
196 print ("Usage: mkknlimg [--dtok] [--270x] [--283x] <vmlinux|zImage|bzImage> <outfile>\n");
202 my ($knl, $tmp) = @_;
204 my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
206 return undef if (!$ver);
210 my $res = { 'kver'=>$ver };
211 $res->{'flags'} = strings_to_flags
($knl, $wanted_strings);
219 my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
221 my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
225 $pos = (split(/[\r\n]+/, $pos))[$idx];
226 return undef if (!defined($pos));
227 $pos =~ s/:.*[\r\n]*$//s;
228 my $cmd = "tail -c+$pos \"$knl\" | $zcat > $tmp2 2> /dev/null
";
229 my $err = (system($cmd) >> 8);
230 return undef if (($err != 0) && ($err != 2));
232 return try_extract($tmp2, $tmp1);
240 my ($knl, $strings) = @_;
241 my $string_pattern = '^('.join('|', keys(%$strings)).')$';
244 my @matches = `strings \"$knl\" | grep -E
\"$string_pattern\"`;
245 foreach my $match (@matches)
248 $flags |= $strings->{$match};
257 my $trailer = pack('VV', 0, 0);
258 for (my $i = $#$atoms; $i>=0; $i--)
260 my $atom = $atoms->[$i];
261 $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]);