main.c revision 235094
1/*-
2 * Copyright (c) 2000 Benno Rice <benno@jeamland.net>
3 * Copyright (c) 2000 Stephane Potvin <sepotvin@videotron.ca>
4 * Copyright (c) 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/boot/uboot/common/main.c 235094 2012-05-06 16:01:58Z kientzle $");
31
32#include <stand.h>
33
34#include "api_public.h"
35#include "bootstrap.h"
36#include "glue.h"
37#include "libuboot.h"
38
39struct uboot_devdesc currdev;
40struct arch_switch archsw;		/* MI/MD interface boundary */
41int devs_no;
42
43extern char end[];
44extern char bootprog_name[];
45extern char bootprog_rev[];
46extern char bootprog_date[];
47extern char bootprog_maker[];
48
49extern unsigned char _etext[];
50extern unsigned char _edata[];
51extern unsigned char __bss_start[];
52extern unsigned char __sbss_start[];
53extern unsigned char __sbss_end[];
54extern unsigned char _end[];
55
56#ifdef LOADER_FDT_SUPPORT
57extern int command_fdt_internal(int argc, char *argv[]);
58#endif
59
60static void
61dump_sig(struct api_signature *sig)
62{
63#ifdef DEBUG
64	printf("signature:\n");
65	printf("  version\t= %d\n", sig->version);
66	printf("  checksum\t= 0x%08x\n", sig->checksum);
67	printf("  sc entry\t= 0x%08x\n", sig->syscall);
68#endif
69}
70
71static void
72dump_addr_info(void)
73{
74#ifdef DEBUG
75	printf("\naddresses info:\n");
76	printf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext);
77	printf(" _edata         = 0x%08x\n", (uint32_t)_edata);
78	printf(" __sbss_start   = 0x%08x\n", (uint32_t)__sbss_start);
79	printf(" __sbss_end     = 0x%08x\n", (uint32_t)__sbss_end);
80	printf(" __sbss_start   = 0x%08x\n", (uint32_t)__bss_start);
81	printf(" _end           = 0x%08x\n", (uint32_t)_end);
82	printf(" syscall entry  = 0x%08x\n", (uint32_t)syscall_ptr);
83#endif
84}
85
86static uint64_t
87memsize(struct sys_info *si, int flags)
88{
89	uint64_t size;
90	int i;
91
92	size = 0;
93	for (i = 0; i < si->mr_no; i++)
94		if (si->mr[i].flags == flags && si->mr[i].size)
95			size += (si->mr[i].size);
96
97	return (size);
98}
99
100static void
101meminfo(void)
102{
103	uint64_t size;
104	struct sys_info *si;
105	int t[3] = { MR_ATTR_DRAM, MR_ATTR_FLASH, MR_ATTR_SRAM };
106	int i;
107
108	if ((si = ub_get_sys_info()) == NULL)
109		panic("could not retrieve system info");
110
111	for (i = 0; i < 3; i++) {
112		size = memsize(si, t[i]);
113		if (size > 0)
114			printf("%s:\t %lldMB\n", ub_mem_type(t[i]),
115			    size / 1024 / 1024);
116	}
117}
118
119static uint64_t
120uboot_loadaddr(u_int type, void *data, uint64_t addr)
121{
122	printf("uboot_loadaddr: type=%d data=0x%x addr=0x%x\n",
123	    type, data, addr);
124	if (type == 1)
125		return 0x40000000;
126	return (addr);
127}
128
129int
130main(void)
131{
132	struct api_signature *sig = NULL;
133	int i;
134	struct open_file f;
135
136	if (!api_search_sig(&sig))
137		return (-1);
138
139	syscall_ptr = sig->syscall;
140	if (syscall_ptr == NULL)
141		return (-2);
142
143	if (sig->version > API_SIG_VERSION)
144		return (-3);
145
146        /* Clear BSS sections */
147	bzero(__sbss_start, __sbss_end - __sbss_start);
148	bzero(__bss_start, _end - __bss_start);
149
150	/*
151         * Set up console.
152         */
153	cons_probe();
154
155	printf("Compatible API signature found @%x\n", (uint32_t)sig);
156
157	dump_sig(sig);
158	dump_addr_info();
159
160	/*
161	 * Initialise the heap as early as possible.  Once this is done,
162	 * alloc() is usable. The stack is buried inside us, so this is
163	 * safe.
164	 */
165	setheap((void *)end, (void *)(end + 512 * 1024));
166
167	/*
168	 * Enumerate U-Boot devices
169	 */
170	if ((devs_no = ub_dev_enum()) == 0)
171		panic("no U-Boot devices found");
172	printf("Number of U-Boot devices: %d\n", devs_no);
173
174	printf("\n");
175	printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
176	printf("(%s, %s)\n", bootprog_maker, bootprog_date);
177	meminfo();
178
179	/*
180	 * March through the device switch probing for things.
181	 */
182	for (i = 0; devsw[i] != NULL; i++) {
183
184		if (devsw[i]->dv_init == NULL)
185			continue;
186		if ((devsw[i]->dv_init)() != 0)
187			continue;
188
189		printf("\nDevice: %s\n", devsw[i]->dv_name);
190
191		currdev.d_dev = devsw[i];
192		currdev.d_type = currdev.d_dev->dv_type;
193		currdev.d_unit = 0;
194
195		if (strncmp(devsw[i]->dv_name, "disk",
196		    strlen(devsw[i]->dv_name)) == 0) {
197			f.f_devdata = &currdev;
198			currdev.d_disk.pnum = 0;
199			if (devsw[i]->dv_open(&f,&currdev) == 0)
200				break;
201		}
202
203		if (strncmp(devsw[i]->dv_name, "net",
204		    strlen(devsw[i]->dv_name)) == 0)
205			break;
206	}
207
208	if (devsw[i] == NULL)
209		panic("No boot device found!");
210
211	env_setenv("currdev", EV_VOLATILE, uboot_fmtdev(&currdev),
212	    uboot_setcurrdev, env_nounset);
213	env_setenv("loaddev", EV_VOLATILE, uboot_fmtdev(&currdev),
214	    env_noset, env_nounset);
215
216	setenv("LINES", "24", 1);		/* optional */
217	setenv("prompt", "loader>", 1);
218
219	archsw.arch_getdev = uboot_getdev;
220	archsw.arch_copyin = uboot_copyin;
221	archsw.arch_copyout = uboot_copyout;
222	archsw.arch_readin = uboot_readin;
223	archsw.arch_autoload = uboot_autoload;
224	archsw.arch_loadaddr = uboot_loadaddr;
225
226	interact();				/* doesn't return */
227
228	return (0);
229}
230
231
232COMMAND_SET(heap, "heap", "show heap usage", command_heap);
233static int
234command_heap(int argc, char *argv[])
235{
236
237	printf("heap base at %p, top at %p, used %d\n", end, sbrk(0),
238	    sbrk(0) - end);
239
240	return (CMD_OK);
241}
242
243COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
244static int
245command_reboot(int argc, char *argv[])
246{
247
248	printf("Resetting...\n");
249	ub_reset();
250
251	printf("Reset failed!\n");
252	while(1);
253}
254
255COMMAND_SET(devinfo, "devinfo", "show U-Boot devices", command_devinfo);
256static int
257command_devinfo(int argc, char *argv[])
258{
259	int i;
260
261	if ((devs_no = ub_dev_enum()) == 0) {
262		command_errmsg = "no U-Boot devices found!?";
263		return (CMD_ERROR);
264	}
265
266	printf("U-Boot devices:\n");
267	for (i = 0; i < devs_no; i++) {
268		ub_dump_di(i);
269		printf("\n");
270	}
271	return (CMD_OK);
272}
273
274COMMAND_SET(sysinfo, "sysinfo", "show U-Boot system info", command_sysinfo);
275static int
276command_sysinfo(int argc, char *argv[])
277{
278	struct sys_info *si;
279
280	if ((si = ub_get_sys_info()) == NULL) {
281		command_errmsg = "could not retrieve U-Boot sys info!?";
282		return (CMD_ERROR);
283	}
284
285	printf("U-Boot system info:\n");
286	ub_dump_si(si);
287	return (CMD_OK);
288}
289
290#ifdef LOADER_FDT_SUPPORT
291/*
292 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
293 * and declaring it as extern is in contradiction with COMMAND_SET() macro
294 * (which uses static pointer), we're defining wrapper function, which
295 * calls the proper fdt handling routine.
296 */
297static int
298command_fdt(int argc, char *argv[])
299{
300
301	return (command_fdt_internal(argc, argv));
302}
303
304COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
305#endif
306