brcmImage.pl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright (C) 2009 Henk Vergonet <Henk.Vergonet@gmail.com>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. #
  19. # Description:
  20. # Replacement for brcmImagebuilder
  21. #
  22. # Disclaimer:
  23. # Use this software at your own risk.
  24. #
  25. # Changelog:
  26. # 2009-01-01 Henk.Vergonet at gmail.com
  27. #
  28. use strict;
  29. use Getopt::Std;
  30. use Compress::Zlib;
  31. my $version = "0.1";
  32. my %arg = (
  33. o => 'bcm963xx_fs_kernel',
  34. b => 'OpenWrt',
  35. c => '6348',
  36. s => 64,
  37. f => 0xbfc00000,
  38. x => 0x00010000,
  39. a => 0x80010000,
  40. e => 0x80010000,
  41. i => 2,
  42. );
  43. my $prog = $0;
  44. $prog =~ s/^.*\///;
  45. getopts("r:k:o:lc:b:s:f:i:a:e:tpvh", \%arg);
  46. die "usage: $prog ~opts~
  47. -r <file> : input rootfs file
  48. -k <file> : input kernel file
  49. -o <file> : output image file, default $arg{o}
  50. -l : littleendian system, default ".($arg{l} ? 'yes' : 'no')."
  51. -c <chipid> : default $arg{c}
  52. -b <boardid> : default $arg{b}
  53. -s <size_kb> : erase sise flash, default $arg{s}
  54. -f <baseaddr> : flash base, default ".sprintf('0x%x', $arg{f})."
  55. -x <cfelen> : length of cfe, default ".sprintf('0x%x', $arg{x})."
  56. -i : 2=dual image, default $arg{i}
  57. -a <loadaddr> : Kernel load address, default ".sprintf('0x%x', $arg{a})."
  58. -e <entryaddr>: Kernel entry address, default ".sprintf('0x%x', $arg{e})."
  59. -t : Prefix kernel with load,entry,size
  60. -p : Add a 'gOtO' partition
  61. -v : be more verbose
  62. -h : help, version $version
  63. EXAMPLES:
  64. $prog -k kern -r rootfs
  65. " if $arg{h} || !$arg{k} || !$arg{r};
  66. sub Read_Image
  67. {
  68. open my $fh, $_[0] or die "open $_[0]: $!";
  69. local $/; # Set input to "slurp" mode.
  70. my $buf = <$fh>;
  71. close $fh;
  72. return $buf;
  73. }
  74. sub Padlen
  75. {
  76. my $p = $_[0] % $_[1];
  77. return ($p ? $_[1] - $p : 0);
  78. }
  79. sub Pad
  80. {
  81. my ($buf, $off, $bs) = @_[0..2];
  82. $buf .= chr(255) x Padlen(length($buf) + $off, $bs);
  83. return $buf;
  84. }
  85. sub bcmImage
  86. {
  87. my ($k, $f) = @_[0..1];
  88. my $tmp = $arg{x} + 0x100 + $arg{f};
  89. # regular: rootfs+kernel
  90. my ($img, $fa, $ka) = ( $f.$k, $tmp, $tmp + length($f) );
  91. # test: kernel+rootfs
  92. # my ($img, $fa, $ka) = ( $k.$f, $tmp + length($k), $tmp );
  93. $fa = 0 unless length($f);
  94. my $hdr = pack("a4a20a14a6a16a2a10a12a10a12a10a12a10a2a2a74Na16",
  95. '6',
  96. 'LinuxInside',
  97. 'ver. 2.0',
  98. $arg{c},
  99. $arg{b},
  100. ($arg{l} ? '0' : '1'),
  101. length($img),
  102. '0',
  103. '0',
  104. $fa,
  105. length($f),
  106. $ka,
  107. length($k),
  108. ($arg{i}==2 ? '1' : '0'),
  109. '', # if 1, the image is INACTIVE; if 0, active
  110. '',
  111. ~crc32($k, crc32($f)),
  112. '');
  113. $hdr .= pack('Na16', ~crc32($hdr), '');
  114. printf "kernel at 0x%x length 0x%x(%u)\n", $ka, length($k), length($k)
  115. if $arg{v};
  116. printf "rootfs at 0x%x length 0x%x(%u)\n", $fa, length($f), length($f)
  117. if $arg{v};
  118. open(FO, ">$arg{o}");
  119. print FO $hdr;
  120. print FO $img;
  121. close FO;
  122. }
  123. # MAIN
  124. my $kern = Read_Image $arg{k};
  125. my $root = Read_Image $arg{r};
  126. $kern = pack('NNN', $arg{a}, $arg{e}, length($kern)).$kern if $arg{t};
  127. # specific fixup for the CFE that expects rootfs-kernel order
  128. if ($arg{p}) {
  129. $kern = Pad($kern, 0x10c, $arg{s} * 1024);
  130. my $dummy_root = pack('a4NN',
  131. 'gOtO',
  132. length($kern)+12,
  133. length($root)+Padlen(length($root), $arg{s} * 1024)
  134. );
  135. $kern .= $root;
  136. $root = $dummy_root;
  137. }
  138. bcmImage($kern, $root);