mkknlimg 6.4 KB

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