1
0

mkknlimg 4.9 KB

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