0049-scripts-Add-mkknlimg-and-knlinfo-scripts-from-tools-.patch 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. From c5e1c42c941a7dbcb700d34b99e20e3f67725489 Mon Sep 17 00:00:00 2001
  2. From: Phil Elwell <phil@raspberrypi.org>
  3. Date: Mon, 11 May 2015 09:00:42 +0100
  4. Subject: [PATCH] scripts: Add mkknlimg and knlinfo scripts from tools repo
  5. The Raspberry Pi firmware looks for a trailer on the kernel image to
  6. determine whether it was compiled with Device Tree support enabled.
  7. If the firmware finds a kernel without this trailer, or which has a
  8. trailer indicating that it isn't DT-capable, it disables DT support
  9. and reverts to using ATAGs.
  10. The mkknlimg utility adds that trailer, having first analysed the
  11. image to look for signs of DT support and the kernel version string.
  12. knlinfo displays the contents of the trailer in the given kernel image.
  13. scripts/mkknlimg: Add support for ARCH_BCM2835
  14. Add a new trailer field indicating whether this is an ARCH_BCM2835
  15. build, as opposed to MACH_BCM2708/9. If the loader finds this flag
  16. is set it changes the default base dtb file name from bcm270x...
  17. to bcm283y...
  18. Also update knlinfo to show the status of the field.
  19. scripts/mkknlimg: Improve ARCH_BCM2835 detection
  20. The board support code contains sufficient strings to be able to
  21. distinguish 2708 vs. 2835 builds, so remove the check for
  22. bcm2835-pm-wdt which could exist in either.
  23. Also, since the canned configuration is no longer built in (it's
  24. a module), remove the config string checking.
  25. See: https://github.com/raspberrypi/linux/issues/1157
  26. ---
  27. scripts/knlinfo | 168 ++++++++++++++++++++++++++++++++++++++
  28. scripts/mkknlimg | 244 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
  29. 2 files changed, 412 insertions(+)
  30. create mode 100755 scripts/knlinfo
  31. create mode 100755 scripts/mkknlimg
  32. --- /dev/null
  33. +++ b/scripts/knlinfo
  34. @@ -0,0 +1,168 @@
  35. +#!/usr/bin/env perl
  36. +# ----------------------------------------------------------------------
  37. +# knlinfo by Phil Elwell for Raspberry Pi
  38. +#
  39. +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
  40. +#
  41. +# Licensed under the terms of the GNU General Public License.
  42. +# ----------------------------------------------------------------------
  43. +
  44. +use strict;
  45. +use integer;
  46. +
  47. +use Fcntl ":seek";
  48. +
  49. +my $trailer_magic = 'RPTL';
  50. +
  51. +my %atom_formats =
  52. +(
  53. + 'DTOK' => \&format_bool,
  54. + 'KVer' => \&format_string,
  55. + '283x' => \&format_bool,
  56. +);
  57. +
  58. +if (@ARGV != 1)
  59. +{
  60. + print ("Usage: knlinfo <kernel image>\n");
  61. + exit(1);
  62. +}
  63. +
  64. +my $kernel_file = $ARGV[0];
  65. +
  66. +
  67. +my ($atoms, $pos) = read_trailer($kernel_file);
  68. +
  69. +exit(1) if (!$atoms);
  70. +
  71. +printf("Kernel trailer found at %d/0x%x:\n", $pos, $pos);
  72. +
  73. +foreach my $atom (@$atoms)
  74. +{
  75. + printf(" %s: %s\n", $atom->[0], format_atom($atom));
  76. +}
  77. +
  78. +exit(0);
  79. +
  80. +sub read_trailer
  81. +{
  82. + my ($kernel_file) = @_;
  83. + my $fh;
  84. +
  85. + if (!open($fh, '<', $kernel_file))
  86. + {
  87. + print ("* Failed to open '$kernel_file'\n");
  88. + return undef;
  89. + }
  90. +
  91. + if (!seek($fh, -12, SEEK_END))
  92. + {
  93. + print ("* seek error in '$kernel_file'\n");
  94. + return undef;
  95. + }
  96. +
  97. + my $last_bytes;
  98. + sysread($fh, $last_bytes, 12);
  99. +
  100. + my ($trailer_len, $data_len, $magic) = unpack('VVa4', $last_bytes);
  101. +
  102. + if (($magic ne $trailer_magic) || ($data_len != 4))
  103. + {
  104. + print ("* no trailer\n");
  105. + return undef;
  106. + }
  107. + if (!seek($fh, -12, SEEK_END))
  108. + {
  109. + print ("* seek error in '$kernel_file'\n");
  110. + return undef;
  111. + }
  112. +
  113. + $trailer_len -= 12;
  114. +
  115. + while ($trailer_len > 0)
  116. + {
  117. + if ($trailer_len < 8)
  118. + {
  119. + print ("* truncated atom header in trailer\n");
  120. + return undef;
  121. + }
  122. + if (!seek($fh, -8, SEEK_CUR))
  123. + {
  124. + print ("* seek error in '$kernel_file'\n");
  125. + return undef;
  126. + }
  127. + $trailer_len -= 8;
  128. +
  129. + my $atom_hdr;
  130. + sysread($fh, $atom_hdr, 8);
  131. + my ($atom_len, $atom_type) = unpack('Va4', $atom_hdr);
  132. +
  133. + if ($trailer_len < $atom_len)
  134. + {
  135. + print ("* truncated atom data in trailer\n");
  136. + return undef;
  137. + }
  138. +
  139. + my $rounded_len = (($atom_len + 3) & ~3);
  140. + if (!seek($fh, -(8 + $rounded_len), SEEK_CUR))
  141. + {
  142. + print ("* seek error in '$kernel_file'\n");
  143. + return undef;
  144. + }
  145. + $trailer_len -= $rounded_len;
  146. +
  147. + my $atom_data;
  148. + sysread($fh, $atom_data, $atom_len);
  149. +
  150. + if (!seek($fh, -$atom_len, SEEK_CUR))
  151. + {
  152. + print ("* seek error in '$kernel_file'\n");
  153. + return undef;
  154. + }
  155. +
  156. + push @$atoms, [ $atom_type, $atom_data ];
  157. + }
  158. +
  159. + if (($$atoms[-1][0] eq "\x00\x00\x00\x00") &&
  160. + ($$atoms[-1][1] eq ""))
  161. + {
  162. + pop @$atoms;
  163. + }
  164. + else
  165. + {
  166. + print ("* end marker missing from trailer\n");
  167. + }
  168. +
  169. + return ($atoms, tell($fh));
  170. +}
  171. +
  172. +sub format_atom
  173. +{
  174. + my ($atom) = @_;
  175. +
  176. + my $format_func = $atom_formats{$atom->[0]} || \&format_hex;
  177. + return $format_func->($atom->[1]);
  178. +}
  179. +
  180. +sub format_bool
  181. +{
  182. + my ($data) = @_;
  183. + return unpack('V', $data) ? 'true' : 'false';
  184. +}
  185. +
  186. +sub format_int
  187. +{
  188. + my ($data) = @_;
  189. + return unpack('V', $data);
  190. +}
  191. +
  192. +sub format_string
  193. +{
  194. + my ($data) = @_;
  195. + return '"'.$data.'"';
  196. +}
  197. +
  198. +sub format_hex
  199. +{
  200. + my ($data) = @_;
  201. + return unpack('H*', $data);
  202. +}
  203. --- /dev/null
  204. +++ b/scripts/mkknlimg
  205. @@ -0,0 +1,244 @@
  206. +#!/usr/bin/env perl
  207. +# ----------------------------------------------------------------------
  208. +# mkknlimg by Phil Elwell for Raspberry Pi
  209. +# based on extract-ikconfig by Dick Streefland
  210. +#
  211. +# (c) 2009,2010 Dick Streefland <dick@streefland.net>
  212. +# (c) 2014,2015 Raspberry Pi (Trading) Limited <info@raspberrypi.org>
  213. +#
  214. +# Licensed under the terms of the GNU General Public License.
  215. +# ----------------------------------------------------------------------
  216. +
  217. +use strict;
  218. +use warnings;
  219. +use integer;
  220. +
  221. +my $trailer_magic = 'RPTL';
  222. +
  223. +my $tmpfile1 = "/tmp/mkknlimg_$$.1";
  224. +my $tmpfile2 = "/tmp/mkknlimg_$$.2";
  225. +
  226. +my $dtok = 0;
  227. +my $is_283x = 0;
  228. +
  229. +while (@ARGV && ($ARGV[0] =~ /^-/))
  230. +{
  231. + my $arg = shift(@ARGV);
  232. + if ($arg eq '--dtok')
  233. + {
  234. + $dtok = 1;
  235. + }
  236. + elsif ($arg eq '--283x')
  237. + {
  238. + $is_283x = 1;
  239. + }
  240. + else
  241. + {
  242. + print ("* Unknown option '$arg'\n");
  243. + usage();
  244. + }
  245. +}
  246. +
  247. +usage() if (@ARGV != 2);
  248. +
  249. +my $kernel_file = $ARGV[0];
  250. +my $out_file = $ARGV[1];
  251. +
  252. +if (! -r $kernel_file)
  253. +{
  254. + print ("* File '$kernel_file' not found\n");
  255. + usage();
  256. +}
  257. +
  258. +my @wanted_strings =
  259. +(
  260. + 'bcm2708_fb',
  261. + 'brcm,bcm2835-mmc',
  262. + 'brcm,bcm2835-sdhost',
  263. + 'brcm,bcm2708-pinctrl',
  264. + 'brcm,bcm2835-gpio',
  265. + 'brcm,bcm2835',
  266. + 'brcm,bcm2836'
  267. +);
  268. +
  269. +my $res = try_extract($kernel_file, $tmpfile1);
  270. +$res = try_decompress('\037\213\010', 'xy', 'gunzip', 0,
  271. + $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
  272. +$res = try_decompress('\3757zXZ\000', 'abcde', 'unxz --single-stream', -1,
  273. + $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
  274. +$res = try_decompress('BZh', 'xy', 'bunzip2', 0,
  275. + $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
  276. +$res = try_decompress('\135\0\0\0', 'xxx', 'unlzma', 0,
  277. + $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
  278. +$res = try_decompress('\211\114\132', 'xy', 'lzop -d', 0,
  279. + $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
  280. +$res = try_decompress('\002\041\114\030', 'xy', 'lz4 -d', 1,
  281. + $kernel_file, $tmpfile1, $tmpfile2) if (!$res);
  282. +
  283. +my $append_trailer;
  284. +my $trailer;
  285. +my $kver = '?';
  286. +
  287. +$append_trailer = $dtok;
  288. +
  289. +if ($res)
  290. +{
  291. + $kver = $res->{''} || '?';
  292. + print("Version: $kver\n");
  293. +
  294. + $append_trailer = $dtok;
  295. + if (!$dtok)
  296. + {
  297. + if (config_bool($res, 'bcm2708_fb') ||
  298. + config_bool($res, 'brcm,bcm2835-mmc') ||
  299. + config_bool($res, 'brcm,bcm2835-sdhost'))
  300. + {
  301. + $dtok ||= config_bool($res, 'brcm,bcm2708-pinctrl');
  302. + $dtok ||= config_bool($res, 'brcm,bcm2835-gpio');
  303. + $is_283x ||= config_bool($res, 'brcm,bcm2835');
  304. + $is_283x ||= config_bool($res, 'brcm,bcm2836');
  305. + $dtok ||= $is_283x;
  306. + $append_trailer = 1;
  307. + }
  308. + else
  309. + {
  310. + print ("* This doesn't look like a Raspberry Pi kernel. In pass-through mode.\n");
  311. + }
  312. + }
  313. +}
  314. +elsif (!$dtok)
  315. +{
  316. + print ("* Is this a valid kernel? In pass-through mode.\n");
  317. +}
  318. +
  319. +if ($append_trailer)
  320. +{
  321. + printf("DT: %s\n", $dtok ? "y" : "n");
  322. + printf("283x: %s\n", $is_283x ? "y" : "n");
  323. +
  324. + my @atoms;
  325. +
  326. + push @atoms, [ $trailer_magic, pack('V', 0) ];
  327. + push @atoms, [ 'KVer', $kver ];
  328. + push @atoms, [ 'DTOK', pack('V', $dtok) ];
  329. + push @atoms, [ '283x', pack('V', $is_283x) ];
  330. +
  331. + $trailer = pack_trailer(\@atoms);
  332. + $atoms[0]->[1] = pack('V', length($trailer));
  333. +
  334. + $trailer = pack_trailer(\@atoms);
  335. +}
  336. +
  337. +my $ofh;
  338. +my $total_len = 0;
  339. +
  340. +if ($out_file eq $kernel_file)
  341. +{
  342. + die "* Failed to open '$out_file' for append\n"
  343. + if (!open($ofh, '>>', $out_file));
  344. + $total_len = tell($ofh);
  345. +}
  346. +else
  347. +{
  348. + die "* Failed to open '$kernel_file'\n"
  349. + if (!open(my $ifh, '<', $kernel_file));
  350. + die "* Failed to create '$out_file'\n"
  351. + if (!open($ofh, '>', $out_file));
  352. +
  353. + my $copybuf;
  354. + while (1)
  355. + {
  356. + my $bytes = sysread($ifh, $copybuf, 64*1024);
  357. + last if (!$bytes);
  358. + syswrite($ofh, $copybuf, $bytes);
  359. + $total_len += $bytes;
  360. + }
  361. + close($ifh);
  362. +}
  363. +
  364. +if ($trailer)
  365. +{
  366. + # Pad to word-alignment
  367. + syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3));
  368. + syswrite($ofh, $trailer);
  369. +}
  370. +
  371. +close($ofh);
  372. +
  373. +exit($trailer ? 0 : 1);
  374. +
  375. +END {
  376. + unlink($tmpfile1) if ($tmpfile1);
  377. + unlink($tmpfile2) if ($tmpfile2);
  378. +}
  379. +
  380. +
  381. +sub usage
  382. +{
  383. + print ("Usage: mkknlimg [--dtok] [--283x] <vmlinux|zImage|bzImage> <outfile>\n");
  384. + exit(1);
  385. +}
  386. +
  387. +sub try_extract
  388. +{
  389. + my ($knl, $tmp) = @_;
  390. +
  391. + my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`;
  392. +
  393. + return undef if (!$ver);
  394. +
  395. + chomp($ver);
  396. +
  397. + my $res = { ''=>$ver };
  398. + my $string_pattern = '^('.join('|', @wanted_strings).')$';
  399. +
  400. + my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`;
  401. + foreach my $match (@matches)
  402. + {
  403. + chomp($match);
  404. + $res->{$match} = 1;
  405. + }
  406. +
  407. + return $res;
  408. +}
  409. +
  410. +
  411. +sub try_decompress
  412. +{
  413. + my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_;
  414. +
  415. + my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`;
  416. + if ($pos)
  417. + {
  418. + chomp($pos);
  419. + $pos = (split(/[\r\n]+/, $pos))[$idx];
  420. + return undef if (!defined($pos));
  421. + $pos =~ s/:.*[\r\n]*$//s;
  422. + my $cmd = "tail -c+$pos \"$knl\" | $zcat > $tmp2 2> /dev/null";
  423. + my $err = (system($cmd) >> 8);
  424. + return undef if (($err != 0) && ($err != 2));
  425. +
  426. + return try_extract($tmp2, $tmp1);
  427. + }
  428. +
  429. + return undef;
  430. +}
  431. +
  432. +sub pack_trailer
  433. +{
  434. + my ($atoms) = @_;
  435. + my $trailer = pack('VV', 0, 0);
  436. + for (my $i = $#$atoms; $i>=0; $i--)
  437. + {
  438. + my $atom = $atoms->[$i];
  439. + $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]);
  440. + }
  441. + return $trailer;
  442. +}
  443. +
  444. +sub config_bool
  445. +{
  446. + my ($configs, $wanted) = @_;
  447. + my $val = $configs->{$wanted} || 'n';
  448. + return (($val eq 'y') || ($val eq '1'));
  449. +}