Content-Security-Policy: default-src 'none'
Content-Type: text/plain; charset=UTF-8
Content-Length: 3843
Content-Disposition: inline; filename="brcmImage.pl"
Last-Modified: Tue, 25 Feb 2025 16:52:25 GMT
Expires: Fri, 23 Feb 2035 16:52:25 GMT
ETag: "9a3acb43a4dbad1b32d0b58b6857e29c915fd9e1"

#!/usr/bin/perl
#
#    Copyright (C) 2009	Henk Vergonet <Henk.Vergonet@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

# Description:
#   Replacement for brcmImagebuilder
#
# Disclaimer:
#   Use this software at your own risk.
#
# Changelog:
#   2009-01-01	Henk.Vergonet at gmail.com
#
use strict;
use Getopt::Std;
use Compress::Zlib;

my $version = "0.1";
my %arg = (
	o => 'bcm963xx_fs_kernel',
	b => 'OpenWrt',
	c => '6348',
	s => 64,
	f => 0xbfc00000,
	x => 0x00010000,
	a => 0x80010000,
	e => 0x80010000,
	i => 2,
);
my $prog = $0;
$prog =~ s/^.*\///;
getopts("r:k:o:lc:b:s:f:i:a:e:tpvh", \%arg);

die "usage: $prog ~opts~

  -r <file>	: input rootfs file
  -k <file>	: input kernel file
  -o <file>	: output image file, default $arg{o}
  -l		: littleendian system, default ".($arg{l} ? 'yes' : 'no')."
  -c <chipid>	: default $arg{c} 
  -b <boardid>	: default $arg{b} 
  -s <size_kb>	: erase sise flash, default $arg{s} 
  -f <baseaddr>	: flash base, default ".sprintf('0x%x', $arg{f})."
  -x <cfelen>	: length of cfe, default ".sprintf('0x%x', $arg{x})."
  -i		: 2=dual image, default $arg{i}

  -a <loadaddr>	: Kernel load address, default ".sprintf('0x%x', $arg{a})."
  -e <entryaddr>: Kernel entry address, default ".sprintf('0x%x', $arg{e})."
  -t		: Prefix kernel with load,entry,size

  -p		: Add a 'gOtO' partition 

  -v		: be more verbose
  -h		: help, version $version

EXAMPLES:
    $prog -k kern -r rootfs
" if $arg{h} || !$arg{k} || !$arg{r};

sub Read_Image
{
	open my $fh, $_[0] or die "open $_[0]: $!";
	local $/;	# Set input to "slurp" mode.
	my $buf = <$fh>;
	close $fh;
	return $buf;
}

sub Padlen
{
	my $p = $_[0] % $_[1];
	return ($p ? $_[1] - $p : 0);
}

sub Pad
{
	my ($buf, $off, $bs) = @_[0..2];
	$buf .= chr(255) x Padlen(length($buf) + $off, $bs);
	return $buf;
}

sub bcmImage
{
	my ($k, $f) = @_[0..1];
	my $tmp = $arg{x} + 0x100 + $arg{f};
	
	# regular: rootfs+kernel
	my ($img, $fa, $ka) = ( $f.$k, $tmp, $tmp + length($f) );

	# test: kernel+rootfs
#	my ($img, $fa, $ka) = ( $k.$f, $tmp + length($k), $tmp );

	$fa = 0 unless length($f);

	my $hdr = pack("a4a20a14a6a16a2a10a12a10a12a10a12a10a2a2a74Na16",
		'6',
		'LinuxInside', 
		'ver. 2.0', 
		$arg{c},
		$arg{b},
		($arg{l} ? '0' : '1'),
		length($img),
		'0',
		'0',
		$fa,
		length($f),
		$ka,
		length($k),
		($arg{i}==2 ? '1' : '0'),
		'',		# if 1, the image is INACTIVE; if 0, active
		'',
		~crc32($k, crc32($f)),
		'');
	$hdr .= pack('Na16', ~crc32($hdr), '');

	printf "kernel at 0x%x length 0x%x(%u)\n", $ka, length($k), length($k)
		if $arg{v};
	printf "rootfs at 0x%x length 0x%x(%u)\n", $fa, length($f), length($f)
		if $arg{v};

	open(FO, ">$arg{o}");
	print FO $hdr;
	print FO $img;
	close FO;
}

# MAIN

my $kern = Read_Image $arg{k};
my $root = Read_Image $arg{r};

$kern = pack('NNN', $arg{a}, $arg{e}, length($kern)).$kern if $arg{t};

# specific fixup for the CFE that expects rootfs-kernel order
if ($arg{p}) {
	$kern = Pad($kern, 0x10c, $arg{s} * 1024);
	my $dummy_root = pack('a4NN',
			'gOtO',
			length($kern)+12,
			length($root)+Padlen(length($root), $arg{s} * 1024)
	);
	$kern .= $root;
	$root = $dummy_root;
}

bcmImage($kern, $root);

'>7</a>
<a id='n8' href='#n8'>8</a>
<a id='n9' href='#n9'>9</a>
<a id='n10' href='#n10'>10</a>
<a id='n11' href='#n11'>11</a>
<a id='n12' href='#n12'>12</a>
<a id='n13' href='#n13'>13</a>
<a id='n14' href='#n14'>14</a>
<a id='n15' href='#n15'>15</a>
<a id='n16' href='#n16'>16</a>
<a id='n17' href='#n17'>17</a>
<a id='n18' href='#n18'>18</a>
<a id='n19' href='#n19'>19</a>
<a id='n20' href='#n20'>20</a>
<a id='n21' href='#n21'>21</a>
<a id='n22' href='#n22'>22</a>
<a id='n23' href='#n23'>23</a>
<a id='n24' href='#n24'>24</a>
<a id='n25' href='#n25'>25</a>
<a id='n26' href='#n26'>26</a>
<a id='n27' href='#n27'>27</a>
<a id='n28' href='#n28'>28</a>
<a id='n29' href='#n29'>29</a>
<a id='n30' href='#n30'>30</a>
<a id='n31' href='#n31'>31</a>
<a id='n32' href='#n32'>32</a>
<a id='n33' href='#n33'>33</a>
<a id='n34' href='#n34'>34</a>
<a id='n35' href='#n35'>35</a>
<a id='n36' href='#n36'>36</a>
<a id='n37' href='#n37'>37</a>
</pre></td>
<td class='lines'><pre><code><style>pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */</style><div class="highlight"><pre><span></span>getvar<span class="o">()</span> <span class="o">{</span>
	<span class="nb">eval</span> <span class="s2">&quot;echo \&quot;\${</span><span class="nv">$1</span><span class="s2">}\&quot;&quot;</span>
<span class="o">}</span>

var2file<span class="o">()</span> <span class="o">{</span>
	<span class="nb">local</span> var
	<span class="nb">eval</span> <span class="s2">&quot;var=\&quot;\${</span><span class="nv">$1</span><span class="s2">}\&quot;&quot;</span>
	<span class="k">if</span> <span class="o">[</span> -n <span class="s2">&quot;</span><span class="nv">$var</span><span class="s2">&quot;</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span> <span class="nb">echo</span> <span class="s2">&quot;</span><span class="nv">$var</span><span class="s2">&quot;</span> &gt; <span class="s2">&quot;</span><span class="nv">$2</span><span class="s2">&quot;</span><span class="p">;</span> <span class="k">fi</span>
<span class="o">}</span>

isset<span class="o">()</span> <span class="o">{</span>
	<span class="nb">local</span> var
	<span class="nb">eval</span> <span class="s2">&quot;var=\&quot;\${</span><span class="nv">$1</span><span class="s2">}\&quot;&quot;</span>
	<span class="o">[</span> -n <span class="s2">&quot;</span><span class="nv">$var</span><span class="s2">&quot;</span> <span class="o">]</span>
<span class="o">}</span>

trapret<span class="o">()</span> <span class="o">{(</span>
	<span class="nb">local</span> <span class="nv">retvals</span><span class="o">=</span><span class="s2">&quot;</span><span class="nv">$1</span><span class="s2">&quot;</span><span class="p">;</span> <span class="nb">shift</span>
	<span class="nb">local</span> <span class="nv">cmd</span><span class="o">=</span><span class="s2">&quot;</span><span class="nv">$1</span><span class="s2">&quot;</span><span class="p">;</span> <span class="nb">shift</span>
	<span class="k">for</span> retval in <span class="k">$(</span><span class="nb">echo</span> <span class="nv">$retvals</span><span class="k">)</span><span class="p">;</span> <span class="k">do</span>
		<span class="nb">local</span> trap_<span class="nv">$retval</span><span class="o">=</span><span class="m">1</span>
	<span class="k">done</span>
	<span class="s2">&quot;</span><span class="nv">$cmd</span><span class="s2">&quot;</span> <span class="s2">&quot;</span><span class="nv">$@</span><span class="s2">&quot;</span> <span class="o">||</span> <span class="o">{</span>
		<span class="nb">local</span> <span class="nv">retval</span><span class="o">=</span><span class="s2">&quot;</span><span class="nv">$?</span><span class="s2">&quot;</span>
		<span class="nb">eval</span> <span class="s2">&quot;trapped=\${trap_</span><span class="nv">$retval</span><span class="s2">}&quot;</span>
		<span class="o">[</span> -n <span class="s2">&quot;</span><span class="nv">$trapped</span><span class="s2">&quot;</span> <span class="o">]</span> <span class="o">||</span> <span class="o">{</span>
			<span class="k">return</span> <span class="nv">$retval</span>
		<span class="o">}</span>
	<span class="o">}</span>
<span class="o">)}</span>

md5s<span class="o">()</span> <span class="o">{</span>
	cat <span class="s2">&quot;</span><span class="nv">$@</span><span class="s2">&quot;</span> <span class="p">|</span> <span class="o">(</span>
		md5sum <span class="m">2</span>&gt;/dev/null <span class="o">||</span>
		md5 
	<span class="o">)</span> <span class="p">|</span> awk <span class="s1">&#39;{print $1}&#39;</span>
<span class="o">}</span>
</pre></div>
</code></pre></td></tr></table>
</div> <!-- class=content -->
<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit v1.2.3</a> (<a href='https://git-scm.com/'>git 2.25.1</a>) at 2025-02-25 16:52:25 +0000</div>
</div> <!-- id=cgit -->
</body>
</html>
