mkknlimg 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #!/usr/bin/env perl
  2. # ----------------------------------------------------------------------
  3. # mkknlimg by Phil Elwell for Raspberry Pi
  4. # based on extract-ikconfig by Dick Streefland
  5. #
  6. # (c) 2009,2010 Dick Streefland <dick@streefland.net>
  7. # (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
  8. #
  9. # Licensed under the terms of the GNU General Public License.
  10. # ----------------------------------------------------------------------
  11. use strict;
  12. use warnings;
  13. use integer;
  14. my $trailer_magic = 'RPTL';
  15. my $tmpfile1 = "/tmp/mkknlimg_$$.1";
  16. my $tmpfile2 = "/tmp/mkknlimg_$$.2";
  17. my $dtok = 0;
  18. my $is_283x = 0;
  19. while (@ARGV && ($ARGV[0] =~ /^-/))
  20. {
  21. my $arg = shift(@ARGV);
  22. if ($arg eq '--dtok')
  23. {
  24. $dtok = 1;
  25. }
  26. elsif ($arg eq '--283x')
  27. {
  28. $is_283x = 1;
  29. }
  30. else
  31. {
  32. print ("* Unknown option '$arg'\n");
  33. usage();
  34. }
  35. }
  36. usage() if (@ARGV != 2);
  37. my $kernel_file = $ARGV[0];
  38. my $out_file = $ARGV[1];
  39. if (! -r $kernel_file)
  40. {
  41. print ("* File '$kernel_file' not found\n");
  42. usage();
  43. }
  44. my @wanted_config_lines =
  45. (
  46. 'CONFIG_BCM2708_DT',
  47. 'CONFIG_ARCH_BCM2835'
  48. );
  49. my @wanted_strings =
  50. (
  51. 'bcm2708_fb',
  52. 'brcm,bcm2835-mmc',
  53. 'brcm,bcm2835-sdhost',
  54. 'brcm,bcm2708-pinctrl',
  55. 'brcm,bcm2835-gpio',
  56. 'brcm,bcm2835-pm-wdt'
  57. );
  58. my $res = try_extract($kernel_file, $tmpfile1);
  59. $res = try_decompress('\037\213\010', 'xy', 'gunzip', 0,
  60. $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
  61. $res = try_decompress('\3757zXZ\000', 'abcde', 'unxz --single-stream', -1,
  62. $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
  63. $res = try_decompress('BZh', 'xy', 'bunzip2', 0,
  64. $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
  65. $res = try_decompress('\135\0\0\0', 'xxx', 'unlzma', 0,
  66. $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
  67. $res = try_decompress('\211\114\132', 'xy', 'lzop -d', 0,
  68. $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
  69. $res = try_decompress('\002\041\114\030', 'xy', 'lz4 -d', 1,
  70. $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
  71. my $append_trailer;
  72. my $trailer;
  73. my $kver = '?';
  74. $append_trailer = $dtok;
  75. if ($res)
  76. {
  77. $kver = $res->{''} || '?';
  78. print("Version: $kver\n");
  79. $append_trailer = $dtok;
  80. if (!$dtok)
  81. {
  82. if (config_bool($res, 'bcm2708_fb') ||
  83. config_bool($res, 'brcm,bcm2835-mmc') ||
  84. config_bool($res, 'brcm,bcm2835-sdhost'))
  85. {
  86. $dtok ||= config_bool($res, 'CONFIG_BCM2708_DT');
  87. $dtok ||= config_bool($res, 'CONFIG_ARCH_BCM2835');
  88. $dtok ||= config_bool($res, 'brcm,bcm2708-pinctrl');
  89. $dtok ||= config_bool($res, 'brcm,bcm2835-gpio');
  90. $is_283x ||= config_bool($res, 'CONFIG_ARCH_BCM2835');
  91. $is_283x ||= config_bool($res, 'brcm,bcm2835-pm-wdt');
  92. $append_trailer = 1;
  93. }
  94. else
  95. {
  96. print ("* This doesn't look like a Raspberry Pi kernel. In pass-through mode.\n");
  97. }
  98. }
  99. }
  100. elsif (!$dtok)
  101. {
  102. print ("* Is this a valid kernel? In pass-through mode.\n");
  103. }
  104. if ($append_trailer)
  105. {
  106. printf("DT: %s\n", $dtok ? "y" : "n");
  107. printf("283x: %s\n", $is_283x ? "y" : "n");
  108. my @atoms;
  109. push @atoms, [ $trailer_magic, pack('V', 0) ];
  110. push @atoms, [ 'KVer', $kver ];
  111. push @atoms, [ 'DTOK', pack('V', $dtok) ];
  112. push @atoms, [ '283x', pack('V', $is_283x) ];
  113. $trailer = pack_trailer(\@atoms);
  114. $atoms[0]->[1] = pack('V', length($trailer));
  115. $trailer = pack_trailer(\@atoms);
  116. }
  117. my $ofh;
  118. my $total_len = 0;
  119. if ($out_file eq $kernel_file)
  120. {
  121. die "* Failed to open '$out_file' for append\n"
  122. if (!open($ofh, '>>', $out_file));
  123. $total_len = tell($ofh);
  124. }
  125. else
  126. {
  127. die "* Failed to open '$kernel_file'\n"
  128. if (!open(my $ifh, '<', $kernel_file));
  129. die "* Failed to create '$out_file'\n"
  130. if (!open($ofh, '>', $out_file));
  131. my $copybuf;
  132. while (1)
  133. {
  134. my $bytes = sysread($ifh, $copybuf, 64*1024);
  135. last if (!$bytes);
  136. syswrite($ofh, $copybuf, $bytes);
  137. $total_len += $bytes;
  138. }
  139. close($ifh);
  140. }
  141. if ($trailer)
  142. {
  143. # Pad to word-alignment
  144. syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
  145. syswrite($ofh, $trailer);
  146. }
  147. close($ofh);
  148. exit($trailer ? 0 : 1);
  149. END {
  150. unlink($tmpfile1) if ($tmpfile1);
  151. unlink($tmpfile2) if ($tmpfile2);
  152. }
  153. sub usage
  154. {
  155. print ("Usage: mkknlimg [--dtok] [--283x] <vmlinux|zImage|bzImage> <outfile>\n");
  156. exit(1);
  157. }
  158. sub try_extract
  159. {
  160. my ($knl, $tmp) = @_;
  161. my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
  162. return undef if (!$ver);
  163. chomp($ver);
  164. my $res = { ''=>$ver };
  165. my $string_pattern = '^('.join('|', @wanted_strings).')$';
  166. my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`;
  167. foreach my $match (@matches)
  168. {
  169. chomp($match);
  170. $res->{$match} = 1;
  171. }
  172. my $config_pattern = '^('.join('|', @wanted_config_lines).')=(.*)$';
  173. my $cf1 = 'IKCFG_ST\037\213\010';
  174. my $cf2 = '0123456789';
  175. my $pos = `tr "$cf1\n$cf2" "\n$cf2=" < "$knl" | grep -abo "^$cf2"`;
  176. if ($pos)
  177. {
  178. $pos =~ s/:.*[\r\n]*$//s;
  179. $pos += 8;
  180. my $err = (system("tail -c+$pos \"$knl\" | zcat > $tmp 2> /dev/null") >> 8);
  181. if (($err == 0) || ($err == 2))
  182. {
  183. if (open(my $fh, '<', $tmp))
  184. {
  185. while (my $line = <$fh>)
  186. {
  187. chomp($line);
  188. $res->{$1} = $2 if ($line =~ /$config_pattern/);
  189. }
  190. close($fh);
  191. }
  192. }
  193. }
  194. return $res;
  195. }
  196. sub try_decompress
  197. {
  198. my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
  199. my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
  200. if ($pos)
  201. {
  202. chomp($pos);
  203. $pos = (split(/[\r\n]+/, $pos))[$idx];
  204. return undef if (!defined($pos));
  205. $pos =~ s/:.*[\r\n]*$//s;
  206. my $cmd = "tail -c+$pos \"$knl\" | $zcat > $tmp2 2> /dev/null";
  207. my $err = (system($cmd) >> 8);
  208. return undef if (($err != 0) && ($err != 2));
  209. return try_extract($tmp2, $tmp1);
  210. }
  211. return undef;
  212. }
  213. sub pack_trailer
  214. {
  215. my ($atoms) = @_;
  216. my $trailer = pack('VV', 0, 0);
  217. for (my $i = $#$atoms; $i>=0; $i--)
  218. {
  219. my $atom = $atoms->[$i];
  220. $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]);
  221. }
  222. return $trailer;
  223. }
  224. sub config_bool
  225. {
  226. my ($configs, $wanted) = @_;
  227. my $val = $configs->{$wanted} || 'n';
  228. return (($val eq 'y') || ($val eq '1'));
  229. }