main.c revision 265067
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: stable/10/sys/boot/uboot/common/main.c 265067 2014-04-29 00:17:30Z ian $");
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
119int
120main(void)
121{
122	struct api_signature *sig = NULL;
123	int diskdev, i, netdev, usedev;
124	struct open_file f;
125	const char * loaderdev;
126
127	if (!api_search_sig(&sig))
128		return (-1);
129
130	syscall_ptr = sig->syscall;
131	if (syscall_ptr == NULL)
132		return (-2);
133
134	if (sig->version > API_SIG_VERSION)
135		return (-3);
136
137        /* Clear BSS sections */
138	bzero(__sbss_start, __sbss_end - __sbss_start);
139	bzero(__bss_start, _end - __bss_start);
140
141	/*
142         * Set up console.
143         */
144	cons_probe();
145
146	printf("Compatible API signature found @%x\n", (uint32_t)sig);
147
148	dump_sig(sig);
149	dump_addr_info();
150
151	/*
152	 * Initialise the heap as early as possible.  Once this is done,
153	 * alloc() is usable. The stack is buried inside us, so this is
154	 * safe.
155	 */
156	setheap((void *)end, (void *)(end + 512 * 1024));
157
158	/*
159	 * Enumerate U-Boot devices
160	 */
161	if ((devs_no = ub_dev_enum()) == 0)
162		panic("no U-Boot devices found");
163	printf("Number of U-Boot devices: %d\n", devs_no);
164
165	printf("\n");
166	printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
167	printf("(%s, %s)\n", bootprog_maker, bootprog_date);
168	meminfo();
169
170	/*
171	 * March through the device switch probing for things -- sort of.
172	 *
173	 * The devsw array will have one or two items in it. If
174	 * LOADER_DISK_SUPPORT is defined the first item will be a disk (which
175	 * may not actually work if u-boot didn't supply one). If
176	 * LOADER_NET_SUPPORT is defined the next item will be a network
177	 * interface.  Again it may not actually work at the u-boot level.
178	 *
179	 * The original logic was to always use a disk if it could be
180	 * successfully opened, otherwise use the network interface.  Now that
181	 * logic is amended to first check whether the u-boot environment
182	 * contains a loaderdev variable which tells us which device to use.  If
183	 * it does, we use it and skip the original (second) loop which "probes"
184	 * for a device. We still loop over the devsw just in case it ever gets
185	 * expanded to hold more than 2 devices (but then unit numbers, which
186	 * don't currently exist, may come into play).  If the device named by
187	 * loaderdev isn't found, fall back using to the old "probe" loop.
188	 *
189	 * The original probe loop still effectively behaves as it always has:
190	 * the first usable disk device is choosen, and a network device is used
191	 * only if no disk device is found.  The logic has been reworked so that
192	 * it examines (and thus lists) every potential device along the way
193	 * instead of breaking out of the loop when the first device is found.
194	 */
195	loaderdev = ub_env_get("loaderdev");
196	usedev = -1;
197	if (loaderdev != NULL) {
198		for (i = 0; devsw[i] != NULL; i++) {
199			if (strcmp(loaderdev, devsw[i]->dv_name) == 0) {
200				if (devsw[i]->dv_init == NULL)
201					continue;
202				if ((devsw[i]->dv_init)() != 0)
203					continue;
204				usedev = i;
205				goto have_device;
206			}
207		}
208		printf("U-Boot env contains 'loaderdev=%s', "
209		    "device not found.\n", loaderdev);
210	}
211	printf("Probing for bootable devices...\n");
212	diskdev = -1;
213	netdev = -1;
214	for (i = 0; devsw[i] != NULL; i++) {
215
216		if (devsw[i]->dv_init == NULL)
217			continue;
218		if ((devsw[i]->dv_init)() != 0)
219			continue;
220
221		printf("Bootable device: %s\n", devsw[i]->dv_name);
222
223		if (strncmp(devsw[i]->dv_name, "disk",
224		    strlen(devsw[i]->dv_name)) == 0) {
225			f.f_devdata = &currdev;
226			currdev.d_dev = devsw[i];
227			currdev.d_type = currdev.d_dev->dv_type;
228			currdev.d_unit = 0;
229			currdev.d_disk.slice = 0;
230			if (devsw[i]->dv_open(&f, &currdev) == 0) {
231				devsw[i]->dv_close(&f);
232				if (diskdev == -1)
233					diskdev = i;
234			}
235		} else if (strncmp(devsw[i]->dv_name, "net",
236		    strlen(devsw[i]->dv_name)) == 0) {
237			if (netdev == -1)
238				netdev = i;
239		}
240	}
241
242	if (diskdev != -1)
243		usedev = diskdev;
244	else if (netdev != -1)
245		usedev = netdev;
246	else
247		panic("No bootable devices found!\n");
248
249have_device:
250
251	currdev.d_dev = devsw[usedev];
252	currdev.d_type = devsw[usedev]->dv_type;
253	currdev.d_unit = 0;
254	if (currdev.d_type == DEV_TYP_STOR)
255		currdev.d_disk.slice = 0;
256
257	printf("Current device: %s\n", currdev.d_dev->dv_name);
258
259	env_setenv("currdev", EV_VOLATILE, uboot_fmtdev(&currdev),
260	    uboot_setcurrdev, env_nounset);
261	env_setenv("loaddev", EV_VOLATILE, uboot_fmtdev(&currdev),
262	    env_noset, env_nounset);
263
264	setenv("LINES", "24", 1);		/* optional */
265	setenv("prompt", "loader>", 1);
266
267	archsw.arch_getdev = uboot_getdev;
268	archsw.arch_copyin = uboot_copyin;
269	archsw.arch_copyout = uboot_copyout;
270	archsw.arch_readin = uboot_readin;
271	archsw.arch_autoload = uboot_autoload;
272
273	interact();				/* doesn't return */
274
275	return (0);
276}
277
278
279COMMAND_SET(heap, "heap", "show heap usage", command_heap);
280static int
281command_heap(int argc, char *argv[])
282{
283
284	printf("heap base at %p, top at %p, used %d\n", end, sbrk(0),
285	    sbrk(0) - end);
286
287	return (CMD_OK);
288}
289
290COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
291static int
292command_reboot(int argc, char *argv[])
293{
294
295	printf("Resetting...\n");
296	ub_reset();
297
298	printf("Reset failed!\n");
299	while(1);
300}
301
302COMMAND_SET(devinfo, "devinfo", "show U-Boot devices", command_devinfo);
303static int
304command_devinfo(int argc, char *argv[])
305{
306	int i;
307
308	if ((devs_no = ub_dev_enum()) == 0) {
309		command_errmsg = "no U-Boot devices found!?";
310		return (CMD_ERROR);
311	}
312
313	printf("U-Boot devices:\n");
314	for (i = 0; i < devs_no; i++) {
315		ub_dump_di(i);
316		printf("\n");
317	}
318	return (CMD_OK);
319}
320
321COMMAND_SET(sysinfo, "sysinfo", "show U-Boot system info", command_sysinfo);
322static int
323command_sysinfo(int argc, char *argv[])
324{
325	struct sys_info *si;
326
327	if ((si = ub_get_sys_info()) == NULL) {
328		command_errmsg = "could not retrieve U-Boot sys info!?";
329		return (CMD_ERROR);
330	}
331
332	printf("U-Boot system info:\n");
333	ub_dump_si(si);
334	return (CMD_OK);
335}
336
337#ifdef LOADER_FDT_SUPPORT
338/*
339 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
340 * and declaring it as extern is in contradiction with COMMAND_SET() macro
341 * (which uses static pointer), we're defining wrapper function, which
342 * calls the proper fdt handling routine.
343 */
344static int
345command_fdt(int argc, char *argv[])
346{
347
348	return (command_fdt_internal(argc, argv));
349}
350
351COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
352#endif
353