aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/brcmImage.pl
blob: 9a3acb43a4dbad1b32d0b58b6857e29c915fd9e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/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);
n>; pinctrl-names = "default"; force_gen1 = <1>; }; mdio0: mdio@37000000 { #address-cells = <1>; #size-cells = <0>; compatible = "qcom,ipq8064-mdio", "syscon"; reg = <0x37000000 0x200000>; resets = <&gcc GMAC_CORE1_RESET>; reset-names = "stmmaceth"; clocks = <&gcc GMAC_CORE1_CLK>; clock-names = "stmmaceth"; pinctrl-0 = <&mdio0_pins>; pinctrl-names = "default"; phy0: ethernet-phy@0 { reg = <0>; qca,ar8327-initvals = < 0x00004 0x7600000 /* PAD0_MODE */ 0x00008 0x1000000 /* PAD5_MODE */ 0x0000c 0x80 /* PAD6_MODE */ 0x000e4 0xaa545 /* MAC_POWER_SEL */ 0x000e0 0xc74164de /* SGMII_CTRL */ 0x0007c 0x4e /* PORT0_STATUS */ 0x00094 0x4e /* PORT6_STATUS */ 0x00970 0x1e864443 /* QM_PORT0_CTRL0 */ 0x00974 0x000001c6 /* QM_PORT0_CTRL1 */ 0x00978 0x19008643 /* QM_PORT1_CTRL0 */ 0x0097c 0x000001c6 /* QM_PORT1_CTRL1 */ 0x00980 0x19008643 /* QM_PORT2_CTRL0 */ 0x00984 0x000001c6 /* QM_PORT2_CTRL1 */ 0x00988 0x19008643 /* QM_PORT3_CTRL0 */ 0x0098c 0x000001c6 /* QM_PORT3_CTRL1 */ 0x00990 0x19008643 /* QM_PORT4_CTRL0 */ 0x00994 0x000001c6 /* QM_PORT4_CTRL1 */ 0x00998 0x1e864443 /* QM_PORT5_CTRL0 */ 0x0099c 0x000001c6 /* QM_PORT5_CTRL1 */ 0x009a0 0x1e864443 /* QM_PORT6_CTRL0 */ 0x009a4 0x000001c6 /* QM_PORT6_CTRL1 */ >; }; phy4: ethernet-phy@4 { reg = <4>; qca,ar8327-initvals = < 0x000e4 0x6a545 /* MAC_POWER_SEL */ 0x0000c 0x80 /* PAD6_MODE */ >; }; }; gmac1: ethernet@37200000 { status = "okay"; phy-mode = "rgmii"; qcom,id = <1>; qcom,phy_mdio_addr = <4>; qcom,poll_required = <0>; qcom,rgmii_delay = <1>; qcom,phy_mii_type = <0>; qcom,emulation = <0>; qcom,irq = <255>; mdiobus = <&mdio0>; pinctrl-0 = <&rgmii2_pins>; pinctrl-names = "default"; fixed-link { speed = <1000>; full-duplex; }; }; gmac2: ethernet@37400000 { status = "okay"; phy-mode = "sgmii"; qcom,id = <2>; qcom,phy_mdio_addr = <0>; /* none */ qcom,poll_required = <0>; /* no polling */ qcom,rgmii_delay = <0>; qcom,phy_mii_type = <1>; qcom,emulation = <0>; qcom,irq = <258>; mdiobus = <&mdio0>; fixed-link { speed = <1000>; full-duplex; }; }; amba { sdcc1: sdcc@12400000 { status = "okay"; }; }; }; keys { compatible = "gpio-keys"; pinctrl-0 = <&button_pins>; pinctrl-names = "default"; wifi { label = "wifi"; gpios = <&qcom_pinmux 53 GPIO_ACTIVE_LOW>; linux,code = <KEY_RFKILL>; linux,input-type = <EV_SW>; }; reset { label = "reset"; gpios = <&qcom_pinmux 54 GPIO_ACTIVE_LOW>; linux,code = <KEY_RESTART>; }; wps { label = "wps"; gpios = <&qcom_pinmux 65 GPIO_ACTIVE_LOW>; linux,code = <KEY_WPS_BUTTON>; }; }; leds { compatible = "gpio-leds"; pinctrl-0 = <&led_pins>; pinctrl-names = "default"; internet { label = "nbg6817:white:internet"; gpios = <&qcom_pinmux 64 GPIO_ACTIVE_HIGH>; }; power: power { label = "nbg6817:white:power"; gpios = <&qcom_pinmux 9 GPIO_ACTIVE_HIGH>; default-state = "keep"; }; wifi2g { label = "nbg6817:amber:wifi2g"; gpios = <&qcom_pinmux 33 GPIO_ACTIVE_HIGH>; }; /* wifi2g amber from the manual is missing */ wifi5g { label = "nbg6817:amber:wifi5g"; gpios = <&qcom_pinmux 26 GPIO_ACTIVE_HIGH>; }; /* wifi5g amber from the manual is missing */ }; }; &adm_dma { status = "okay"; };