main.c revision 176348
1176348Smarcel/*-
2176348Smarcel * Copyright (c) 2000 Benno Rice <benno@jeamland.net>
3176348Smarcel * Copyright (c) 2000 Stephane Potvin <sepotvin@videotron.ca>
4176348Smarcel * Copyright (c) 2007 Semihalf, Rafal Jaworowski <raj@semihalf.com>
5176348Smarcel * All rights reserved.
6176348Smarcel *
7176348Smarcel * Redistribution and use in source and binary forms, with or without
8176348Smarcel * modification, are permitted provided that the following conditions
9176348Smarcel * are met:
10176348Smarcel * 1. Redistributions of source code must retain the above copyright
11176348Smarcel *    notice, this list of conditions and the following disclaimer.
12176348Smarcel * 2. Redistributions in binary form must reproduce the above copyright
13176348Smarcel *    notice, this list of conditions and the following disclaimer in the
14176348Smarcel *    documentation and/or other materials provided with the distribution.
15176348Smarcel *
16176348Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17176348Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18176348Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19176348Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20176348Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21176348Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22176348Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23176348Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24176348Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25176348Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26176348Smarcel * SUCH DAMAGE.
27176348Smarcel */
28176348Smarcel
29176348Smarcel#include <sys/cdefs.h>
30176348Smarcel__FBSDID("$FreeBSD: head/sys/boot/uboot/common/main.c 176348 2008-02-16 22:13:11Z marcel $");
31176348Smarcel
32176348Smarcel#include <stand.h>
33176348Smarcel#include "bootstrap.h"
34176348Smarcel
35176348Smarcel#include "libuboot.h"
36176348Smarcel#include "api_public.h"
37176348Smarcel
38176348Smarcelstruct uboot_devdesc	currdev;
39176348Smarcelstruct arch_switch	archsw;		/* MI/MD interface boundary */
40176348Smarcelint			devs_no;
41176348Smarcel
42176348Smarcelextern char end[];
43176348Smarcelextern char bootprog_name[];
44176348Smarcelextern char bootprog_rev[];
45176348Smarcelextern char bootprog_date[];
46176348Smarcelextern char bootprog_maker[];
47176348Smarcel
48176348Smarcelstatic char bootargs[128];
49176348Smarcel
50176348Smarcelextern unsigned char _etext[];
51176348Smarcelextern unsigned char _edata[];
52176348Smarcelextern unsigned char __bss_start[];
53176348Smarcelextern unsigned char __sbss_start[];
54176348Smarcelextern unsigned char __sbss_end[];
55176348Smarcelextern unsigned char _end[];
56176348Smarcel
57176348Smarcelextern void *		syscall_ptr;
58176348Smarcel
59176348Smarcelstruct sys_info *	ub_get_sys_info(void);
60176348Smarcel
61176348Smarcel
62176348Smarcelvoid dump_si(struct sys_info *si)
63176348Smarcel{
64176348Smarcel#ifdef DEBUG
65176348Smarcel	printf("sys info:\n");
66176348Smarcel	printf("  clkbus\t= 0x%08x\n", si->clk_bus);
67176348Smarcel	printf("  clkcpu\t= 0x%08x\n", si->clk_cpu);
68176348Smarcel	printf("  bar\t\t= 0x%08x\n", si->bar);
69176348Smarcel#endif
70176348Smarcel}
71176348Smarcel
72176348Smarcelstatic void dump_sig(struct api_signature *sig)
73176348Smarcel{
74176348Smarcel#ifdef DEBUG
75176348Smarcel	printf("signature:\n");
76176348Smarcel	printf("  version\t= %d\n", sig->version);
77176348Smarcel	printf("  checksum\t= 0x%08x\n", sig->checksum);
78176348Smarcel	printf("  sc entry\t= 0x%08x\n", sig->syscall);
79176348Smarcel#endif
80176348Smarcel}
81176348Smarcelstatic void
82176348Smarceldump_addr_info(void)
83176348Smarcel{
84176348Smarcel#ifdef DEBUG
85176348Smarcel	printf("\naddresses info:\n");
86176348Smarcel	printf(" _etext (sdata) = 0x%08x\n", (u_int32_t)_etext);
87176348Smarcel	printf(" _edata         = 0x%08x\n", (u_int32_t)_edata);
88176348Smarcel	printf(" __sbss_start   = 0x%08x\n", (u_int32_t)__sbss_start);
89176348Smarcel	printf(" __sbss_end     = 0x%08x\n", (u_int32_t)__sbss_end);
90176348Smarcel	printf(" __sbss_start   = 0x%08x\n", (u_int32_t)__bss_start);
91176348Smarcel	printf(" _end           = 0x%08x\n", (u_int32_t)_end);
92176348Smarcel	printf(" syscall entry  = 0x%08x\n", (u_int32_t)syscall_ptr);
93176348Smarcel#endif
94176348Smarcel}
95176348Smarcel
96176348Smarcelstatic uint64_t
97176348Smarcelmemsize(int flags)
98176348Smarcel{
99176348Smarcel	int i;
100176348Smarcel	struct sys_info * si;
101176348Smarcel
102176348Smarcel	if ((si = ub_get_sys_info()) == NULL)
103176348Smarcel		return 0;
104176348Smarcel
105176348Smarcel	for (i = 0; i < si->mr_no; i++)
106176348Smarcel		if (si->mr[i].flags == flags && si->mr[i].size)
107176348Smarcel			return (si->mr[i].size);
108176348Smarcel
109176348Smarcel	return 0;
110176348Smarcel}
111176348Smarcel
112176348Smarcelint
113176348Smarcelmain(void)
114176348Smarcel{
115176348Smarcel	int		i;
116176348Smarcel	char		*ch;
117176348Smarcel	int		bargc;
118176348Smarcel	char		**bargv;
119176348Smarcel
120176348Smarcel	struct api_signature *sig = NULL;
121176348Smarcel
122176348Smarcel	if (!api_search_sig(&sig))
123176348Smarcel		return -1;
124176348Smarcel
125176348Smarcel	syscall_ptr = sig->syscall;
126176348Smarcel	if (syscall_ptr == NULL)
127176348Smarcel		return -2;
128176348Smarcel
129176348Smarcel	if (sig->version > API_SIG_VERSION)
130176348Smarcel		return -3;
131176348Smarcel
132176348Smarcel        /* Clear BSS sections */
133176348Smarcel	bzero(__sbss_start, __sbss_end - __sbss_start);
134176348Smarcel	bzero(__bss_start, _end - __bss_start);
135176348Smarcel
136176348Smarcel	/*
137176348Smarcel         * Set up console.
138176348Smarcel         */
139176348Smarcel	cons_probe();
140176348Smarcel
141176348Smarcel	printf("Compatible API signature found @%x\n", sig);
142176348Smarcel
143176348Smarcel	dump_sig(sig);
144176348Smarcel	dump_addr_info();
145176348Smarcel
146176348Smarcel	/*
147176348Smarcel	 * Initialise the heap as early as possible.  Once this is done,
148176348Smarcel	 * alloc() is usable. The stack is buried inside us, so this is
149176348Smarcel	 * safe.
150176348Smarcel	 */
151176348Smarcel	setheap((void *)end, (void *)(end + 512 * 1024));
152176348Smarcel
153176348Smarcel	/*
154176348Smarcel	 * Enumerate U-Boot devices
155176348Smarcel	 */
156176348Smarcel	if ((devs_no = ub_dev_enum()) == 0)
157176348Smarcel		panic("no devices found");
158176348Smarcel	printf("Number of U-Boot devices found %d\n", devs_no);
159176348Smarcel
160176348Smarcel	/* XXX all our dv_init()s currently don't do anything... */
161176348Smarcel	/*
162176348Smarcel	 * March through the device switch probing for things.
163176348Smarcel	 */
164176348Smarcel	for (i = 0; devsw[i] != NULL; i++)
165176348Smarcel		if (devsw[i]->dv_init != NULL)
166176348Smarcel			(devsw[i]->dv_init)();
167176348Smarcel
168176348Smarcel	printf("\n");
169176348Smarcel	printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
170176348Smarcel	printf("(%s, %s)\n", bootprog_maker, bootprog_date);
171176348Smarcel	printf("Memory: %lldMB\n", memsize(MR_ATTR_DRAM) / 1024 / 1024);
172176348Smarcel	printf("FLASH:  %lldMB\n", memsize(MR_ATTR_FLASH) / 1024 / 1024);
173176348Smarcel//	printf("SRAM:   %lldMB\n", memsize(MR_ATTR_SRAM) / 1024 / 1024);
174176348Smarcel
175176348Smarcel	/* XXX only support netbooting for now */
176176348Smarcel	for (i = 0; devsw[i] != NULL; i++)
177176348Smarcel		if (strncmp(devsw[i]->dv_name, "net", strlen(devsw[i]->dv_name)) == 0)
178176348Smarcel			break;
179176348Smarcel
180176348Smarcel	if (devsw[i] == NULL)
181176348Smarcel		panic("no network devices?!");
182176348Smarcel
183176348Smarcel	currdev.d_dev = devsw[i];
184176348Smarcel	currdev.d_type = currdev.d_dev->dv_type;
185176348Smarcel	currdev.d_unit = 0;
186176348Smarcel
187176348Smarcel	env_setenv("currdev", EV_VOLATILE, uboot_fmtdev(&currdev),
188176348Smarcel			uboot_setcurrdev, env_nounset);
189176348Smarcel	env_setenv("loaddev", EV_VOLATILE, uboot_fmtdev(&currdev),
190176348Smarcel			env_noset, env_nounset);
191176348Smarcel
192176348Smarcel	setenv("LINES", "24", 1);		/* optional */
193176348Smarcel	setenv("prompt", "loader>", 1);
194176348Smarcel
195176348Smarcel	archsw.arch_getdev = uboot_getdev;
196176348Smarcel	archsw.arch_copyin = uboot_copyin;
197176348Smarcel	archsw.arch_copyout = uboot_copyout;
198176348Smarcel	archsw.arch_readin = uboot_readin;
199176348Smarcel	archsw.arch_autoload = uboot_autoload;
200176348Smarcel
201176348Smarcel	interact();				/* doesn't return */
202176348Smarcel
203176348Smarcel	return 0;
204176348Smarcel}
205176348Smarcel
206176348Smarcel
207176348SmarcelCOMMAND_SET(heap, "heap", "show heap usage", command_heap);
208176348Smarcelstatic int
209176348Smarcelcommand_heap(int argc, char *argv[])
210176348Smarcel{
211176348Smarcel	printf("heap base at %p, top at %p, used %ld\n", end, sbrk(0),
212176348Smarcel		sbrk(0) - end);
213176348Smarcel
214176348Smarcel	return(CMD_OK);
215176348Smarcel}
216176348Smarcel
217176348SmarcelCOMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
218176348Smarcelstatic int
219176348Smarcelcommand_reboot(int argc, char *argv[])
220176348Smarcel{
221176348Smarcel	printf("Resetting...\n");
222176348Smarcel	ub_reset();
223176348Smarcel
224176348Smarcel	printf("Reset failed!\n");
225176348Smarcel	while(1);
226176348Smarcel}
227