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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include "api_public.h"
35 #include "bootstrap.h"
39 struct uboot_devdesc currdev;
40 struct arch_switch archsw; /* MI/MD interface boundary */
44 extern char bootprog_name[];
45 extern char bootprog_rev[];
46 extern char bootprog_date[];
47 extern char bootprog_maker[];
49 extern unsigned char _etext[];
50 extern unsigned char _edata[];
51 extern unsigned char __bss_start[];
52 extern unsigned char __sbss_start[];
53 extern unsigned char __sbss_end[];
54 extern unsigned char _end[];
56 #ifdef LOADER_FDT_SUPPORT
57 extern int command_fdt_internal(int argc, char *argv[]);
61 dump_sig(struct api_signature *sig)
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);
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);
87 memsize(struct sys_info *si, int flags)
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);
105 int t[3] = { MR_ATTR_DRAM, MR_ATTR_FLASH, MR_ATTR_SRAM };
108 if ((si = ub_get_sys_info()) == NULL)
109 panic("could not retrieve system info");
111 for (i = 0; i < 3; i++) {
112 size = memsize(si, t[i]);
114 printf("%s:\t %lldMB\n", ub_mem_type(t[i]),
122 struct api_signature *sig = NULL;
123 int diskdev, i, netdev, usedev;
125 const char * loaderdev;
128 * If we can't find the magic signature and related info, exit with a
129 * unique error code that U-Boot reports as "## Application terminated,
130 * rc = 0xnnbadab1". Hopefully 'badab1' looks enough like "bad api" to
131 * provide a clue. It's better than 0xffffffff anyway.
133 if (!api_search_sig(&sig))
136 syscall_ptr = sig->syscall;
137 if (syscall_ptr == NULL)
140 if (sig->version > API_SIG_VERSION)
143 /* Clear BSS sections */
144 bzero(__sbss_start, __sbss_end - __sbss_start);
145 bzero(__bss_start, _end - __bss_start);
152 printf("Compatible API signature found @%x\n", (uint32_t)sig);
158 * Initialise the heap as early as possible. Once this is done,
159 * alloc() is usable. The stack is buried inside us, so this is
162 setheap((void *)end, (void *)(end + 512 * 1024));
165 * Enumerate U-Boot devices
167 if ((devs_no = ub_dev_enum()) == 0)
168 panic("no U-Boot devices found");
169 printf("Number of U-Boot devices: %d\n", devs_no);
172 printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
173 printf("(%s, %s)\n", bootprog_maker, bootprog_date);
177 * March through the device switch probing for things -- sort of.
179 * The devsw array will have one or two items in it. If
180 * LOADER_DISK_SUPPORT is defined the first item will be a disk (which
181 * may not actually work if u-boot didn't supply one). If
182 * LOADER_NET_SUPPORT is defined the next item will be a network
183 * interface. Again it may not actually work at the u-boot level.
185 * The original logic was to always use a disk if it could be
186 * successfully opened, otherwise use the network interface. Now that
187 * logic is amended to first check whether the u-boot environment
188 * contains a loaderdev variable which tells us which device to use. If
189 * it does, we use it and skip the original (second) loop which "probes"
190 * for a device. We still loop over the devsw just in case it ever gets
191 * expanded to hold more than 2 devices (but then unit numbers, which
192 * don't currently exist, may come into play). If the device named by
193 * loaderdev isn't found, fall back using to the old "probe" loop.
195 * The original probe loop still effectively behaves as it always has:
196 * the first usable disk device is choosen, and a network device is used
197 * only if no disk device is found. The logic has been reworked so that
198 * it examines (and thus lists) every potential device along the way
199 * instead of breaking out of the loop when the first device is found.
201 loaderdev = ub_env_get("loaderdev");
203 if (loaderdev != NULL) {
204 for (i = 0; devsw[i] != NULL; i++) {
205 if (strcmp(loaderdev, devsw[i]->dv_name) == 0) {
206 if (devsw[i]->dv_init == NULL)
208 if ((devsw[i]->dv_init)() != 0)
214 printf("U-Boot env contains 'loaderdev=%s', "
215 "device not found.\n", loaderdev);
217 printf("Probing for bootable devices...\n");
220 for (i = 0; devsw[i] != NULL; i++) {
222 if (devsw[i]->dv_init == NULL)
224 if ((devsw[i]->dv_init)() != 0)
227 printf("Bootable device: %s\n", devsw[i]->dv_name);
229 if (strncmp(devsw[i]->dv_name, "disk",
230 strlen(devsw[i]->dv_name)) == 0) {
231 f.f_devdata = &currdev;
232 currdev.d_dev = devsw[i];
233 currdev.d_type = currdev.d_dev->dv_type;
235 currdev.d_disk.slice = 0;
236 if (devsw[i]->dv_open(&f, &currdev) == 0) {
237 devsw[i]->dv_close(&f);
241 } else if (strncmp(devsw[i]->dv_name, "net",
242 strlen(devsw[i]->dv_name)) == 0) {
250 else if (netdev != -1)
253 panic("No bootable devices found!\n");
257 currdev.d_dev = devsw[usedev];
258 currdev.d_type = devsw[usedev]->dv_type;
260 if (currdev.d_type == DEV_TYP_STOR)
261 currdev.d_disk.slice = 0;
263 printf("Current device: %s\n", currdev.d_dev->dv_name);
265 env_setenv("currdev", EV_VOLATILE, uboot_fmtdev(&currdev),
266 uboot_setcurrdev, env_nounset);
267 env_setenv("loaddev", EV_VOLATILE, uboot_fmtdev(&currdev),
268 env_noset, env_nounset);
270 setenv("LINES", "24", 1); /* optional */
271 setenv("prompt", "loader>", 1);
273 archsw.arch_getdev = uboot_getdev;
274 archsw.arch_copyin = uboot_copyin;
275 archsw.arch_copyout = uboot_copyout;
276 archsw.arch_readin = uboot_readin;
277 archsw.arch_autoload = uboot_autoload;
279 interact(); /* doesn't return */
285 COMMAND_SET(heap, "heap", "show heap usage", command_heap);
287 command_heap(int argc, char *argv[])
290 printf("heap base at %p, top at %p, used %d\n", end, sbrk(0),
296 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
298 command_reboot(int argc, char *argv[])
301 printf("Resetting...\n");
304 printf("Reset failed!\n");
308 COMMAND_SET(devinfo, "devinfo", "show U-Boot devices", command_devinfo);
310 command_devinfo(int argc, char *argv[])
314 if ((devs_no = ub_dev_enum()) == 0) {
315 command_errmsg = "no U-Boot devices found!?";
319 printf("U-Boot devices:\n");
320 for (i = 0; i < devs_no; i++) {
327 COMMAND_SET(sysinfo, "sysinfo", "show U-Boot system info", command_sysinfo);
329 command_sysinfo(int argc, char *argv[])
333 if ((si = ub_get_sys_info()) == NULL) {
334 command_errmsg = "could not retrieve U-Boot sys info!?";
338 printf("U-Boot system info:\n");
343 #ifdef LOADER_FDT_SUPPORT
345 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
346 * and declaring it as extern is in contradiction with COMMAND_SET() macro
347 * (which uses static pointer), we're defining wrapper function, which
348 * calls the proper fdt handling routine.
351 command_fdt(int argc, char *argv[])
354 return (command_fdt_internal(argc, argv));
357 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);