1#!/usr/bin/env perl
2#
3# Script for generating redboot configs, based on brcmImage.pl
4#
5# Copyright (C) 2015 ��lvaro Fern��ndez Rojas <noltari@gmail.com>
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20#
21
22use strict;
23use Getopt::Std;
24use File::stat;
25
26my $version = "0.1";
27my %arg = (
28	o => 'redboot.script',
29	s => 0x1000,
30	f => 0xbe430000,
31	a => 0x80010000,
32	l => 0x7c0000,
33	t => 20,
34);
35my $prog = $0;
36$prog =~ s/^.*\///;
37getopts("r:k:o:s:f:a:l:t:vh", \%arg);
38
39die "usage: $prog ~opts~
40
41  -r <file>	: input rootfs file
42  -k <file>	: input kernel file
43  -o <file>	: output image file, default $arg{o}
44  -s <size_kb>	: redboot script size, default ".sprintf('%d', parse_num($arg{s}))."
45  -f <baseaddr>	: flash base, default ".sprintf('0x%x', parse_num($arg{f}))."
46  -a <loadaddr>	: Kernel load address, default ".sprintf('0x%x', parse_num($arg{a}))."
47  -l <linux_kb>	: linux partition size, default ".sprintf('0x%x', parse_num($arg{l}))."
48  -t <timeout> 	: redboot script timeout, default ".sprintf('%d', parse_num($arg{t}))."
49  -v		: be more verbose
50  -h		: help, version $version
51
52EXAMPLES:
53    $prog -k kern -r rootfs
54" if $arg{h} || !$arg{k} || !$arg{r};
55
56sub parse_num
57{
58	my $num = @_[0];
59	if (index(lc($num), lc("0x")) == 0) {
60		return hex($num);
61	} else {
62		return $num + 0;
63	}
64}
65
66sub gen_script
67{
68	my $kernel_off = parse_num($arg{s});
69	my $kernel_addr = parse_num($arg{f});
70	my $kernel_len = stat($arg{k})->size;
71
72	my $rootfs_off = $kernel_off + $kernel_len;
73	my $rootfs_addr = $kernel_addr + $kernel_len;
74	my $rootfs_len = parse_num($arg{l}) - $kernel_len;
75	my $rootfs_size = stat($arg{r})->size;
76
77	my $load_addr = parse_num($arg{a});
78
79	my $timeout = parse_num($arg{t});
80
81	if ($arg{v}) {
82		printf "kernel_off: 0x%x(%u)\n", $kernel_off, $kernel_off;
83		printf "kernel_addr: 0x%x(%u)\n", $kernel_addr, $kernel_addr;
84		printf "kernel_len: 0x%x(%u)\n", $kernel_len, $kernel_len;
85
86		printf "rootfs_off: 0x%x(%u)\n", $rootfs_off, $rootfs_off;
87		printf "rootfs_addr: 0x%x(%u)\n", $rootfs_addr, $rootfs_addr;
88		printf "rootfs_len: 0x%x(%u)\n", $rootfs_len, $rootfs_len;
89		printf "rootfs_size: 0x%x(%u)\n", $rootfs_size, $rootfs_size;
90	}
91
92	open(FO, ">$arg{o}");
93	printf FO "fis init -f\n";
94	printf FO "\n";
95	printf FO "fconfig boot_script true\n";
96	printf FO "fconfig boot_script_data\n";
97	printf FO "fis load -b 0x%x -d kernel\n", $load_addr;
98	printf FO "exec -c \"noinitrd\" 0x%x\n", $load_addr;
99	printf FO "\n";
100	printf FO "fconfig boot_script_timeout %d\n", $timeout;
101	printf FO "\n";
102	printf FO "fis create -o 0x%x -f 0x%x -l 0x%x kernel\n", $kernel_off, $kernel_addr, $kernel_len;
103	printf FO "\n";
104	printf FO "fis create -o 0x%x -s 0x%x -f 0x%x -l 0x%x rootfs\n", $rootfs_off, $rootfs_size, $rootfs_addr, $rootfs_len;
105	printf FO "\n";
106	printf FO "reset\n";
107	close FO;
108}
109
110# MAIN
111gen_script();
112