]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/boot/uboot/common/main.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / boot / uboot / common / main.c
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$");
31
32 #include <stand.h>
33
34 #include "api_public.h"
35 #include "bootstrap.h"
36 #include "glue.h"
37 #include "libuboot.h"
38
39 struct uboot_devdesc currdev;
40 struct arch_switch archsw;              /* MI/MD interface boundary */
41 int devs_no;
42
43 extern char end[];
44 extern char bootprog_name[];
45 extern char bootprog_rev[];
46 extern char bootprog_date[];
47 extern char bootprog_maker[];
48
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[];
55
56 static void
57 dump_sig(struct api_signature *sig)
58 {
59 #ifdef DEBUG
60         printf("signature:\n");
61         printf("  version\t= %d\n", sig->version);
62         printf("  checksum\t= 0x%08x\n", sig->checksum);
63         printf("  sc entry\t= 0x%08x\n", sig->syscall);
64 #endif
65 }
66
67 static void
68 dump_addr_info(void)
69 {
70 #ifdef DEBUG
71         printf("\naddresses info:\n");
72         printf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext);
73         printf(" _edata         = 0x%08x\n", (uint32_t)_edata);
74         printf(" __sbss_start   = 0x%08x\n", (uint32_t)__sbss_start);
75         printf(" __sbss_end     = 0x%08x\n", (uint32_t)__sbss_end);
76         printf(" __sbss_start   = 0x%08x\n", (uint32_t)__bss_start);
77         printf(" _end           = 0x%08x\n", (uint32_t)_end);
78         printf(" syscall entry  = 0x%08x\n", (uint32_t)syscall_ptr);
79 #endif
80 }
81
82 static uint64_t
83 memsize(struct sys_info *si, int flags)
84 {
85         uint64_t size;
86         int i;
87
88         size = 0;
89         for (i = 0; i < si->mr_no; i++)
90                 if (si->mr[i].flags == flags && si->mr[i].size)
91                         size += (si->mr[i].size);
92
93         return (size);
94 }
95
96 static void
97 meminfo(void)
98 {
99         uint64_t size;
100         struct sys_info *si;
101         int t[3] = { MR_ATTR_DRAM, MR_ATTR_FLASH, MR_ATTR_SRAM };
102         int i;
103
104         if ((si = ub_get_sys_info()) == NULL)
105                 panic("could not retrieve system info");
106
107         for (i = 0; i < 3; i++) {
108                 size = memsize(si, t[i]);
109                 if (size > 0)
110                         printf("%s:\t %lldMB\n", ub_mem_type(t[i]),
111                             size / 1024 / 1024);
112         }
113 }
114
115 int
116 main(void)
117 {
118         struct api_signature *sig = NULL;
119         int i;
120         struct open_file f;
121
122         if (!api_search_sig(&sig))
123                 return (-1);
124
125         syscall_ptr = sig->syscall;
126         if (syscall_ptr == NULL)
127                 return (-2);
128
129         if (sig->version > API_SIG_VERSION)
130                 return (-3);
131
132         /* Clear BSS sections */
133         bzero(__sbss_start, __sbss_end - __sbss_start);
134         bzero(__bss_start, _end - __bss_start);
135
136         /*
137          * Set up console.
138          */
139         cons_probe();
140
141         printf("Compatible API signature found @%x\n", (uint32_t)sig);
142
143         dump_sig(sig);
144         dump_addr_info();
145
146         /*
147          * Initialise the heap as early as possible.  Once this is done,
148          * alloc() is usable. The stack is buried inside us, so this is
149          * safe.
150          */
151         setheap((void *)end, (void *)(end + 512 * 1024));
152
153         /*
154          * Enumerate U-Boot devices
155          */
156         if ((devs_no = ub_dev_enum()) == 0)
157                 panic("no U-Boot devices found");
158         printf("Number of U-Boot devices: %d\n", devs_no);
159
160         /*
161          * March through the device switch probing for things.
162          */
163         for (i = 0; devsw[i] != NULL; i++)
164                 if (devsw[i]->dv_init != NULL)
165                         (devsw[i]->dv_init)();
166
167         printf("\n");
168         printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
169         printf("(%s, %s)\n", bootprog_maker, bootprog_date);
170         meminfo();
171
172         for (i = 0; devsw[i] != NULL; i++) {
173                 printf("\nDevice %d: %s\n", i, devsw[i]->dv_name);
174
175                 currdev.d_dev = devsw[i];
176                 currdev.d_type = currdev.d_dev->dv_type;
177                 currdev.d_unit = 0;
178
179                 if (strncmp(devsw[i]->dv_name, "disk",
180                     strlen(devsw[i]->dv_name)) == 0) {
181                         f.f_devdata = &currdev;
182                         currdev.d_disk.pnum = 0;
183                         if (devsw[i]->dv_open(&f,&currdev) == 0)
184                                 break;
185                 }
186
187                 if (strncmp(devsw[i]->dv_name, "net",
188                     strlen(devsw[i]->dv_name)) == 0)
189                         break;
190         }
191
192         if (devsw[i] == NULL)
193                 panic("No boot device found!");
194
195         env_setenv("currdev", EV_VOLATILE, uboot_fmtdev(&currdev),
196             uboot_setcurrdev, env_nounset);
197         env_setenv("loaddev", EV_VOLATILE, uboot_fmtdev(&currdev),
198             env_noset, env_nounset);
199
200         setenv("LINES", "24", 1);               /* optional */
201         setenv("prompt", "loader>", 1);
202
203         archsw.arch_getdev = uboot_getdev;
204         archsw.arch_copyin = uboot_copyin;
205         archsw.arch_copyout = uboot_copyout;
206         archsw.arch_readin = uboot_readin;
207         archsw.arch_autoload = uboot_autoload;
208
209         interact();                             /* doesn't return */
210
211         return (0);
212 }
213
214
215 COMMAND_SET(heap, "heap", "show heap usage", command_heap);
216 static int
217 command_heap(int argc, char *argv[])
218 {
219
220         printf("heap base at %p, top at %p, used %d\n", end, sbrk(0),
221             sbrk(0) - end);
222
223         return (CMD_OK);
224 }
225
226 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
227 static int
228 command_reboot(int argc, char *argv[])
229 {
230
231         printf("Resetting...\n");
232         ub_reset();
233
234         printf("Reset failed!\n");
235         while(1);
236 }
237
238 COMMAND_SET(devinfo, "devinfo", "show U-Boot devices", command_devinfo);
239 static int
240 command_devinfo(int argc, char *argv[])
241 {
242         int i;
243
244         if ((devs_no = ub_dev_enum()) == 0) {
245                 command_errmsg = "no U-Boot devices found!?";
246                 return (CMD_ERROR);
247         }
248         
249         printf("U-Boot devices:\n");
250         for (i = 0; i < devs_no; i++) {
251                 ub_dump_di(i);
252                 printf("\n");
253         }
254         return (CMD_OK);
255 }
256
257 COMMAND_SET(sysinfo, "sysinfo", "show U-Boot system info", command_sysinfo);
258 static int
259 command_sysinfo(int argc, char *argv[])
260 {
261         struct sys_info *si;
262
263         if ((si = ub_get_sys_info()) == NULL) {
264                 command_errmsg = "could not retrieve U-Boot sys info!?";
265                 return (CMD_ERROR);
266         }
267
268         printf("U-Boot system info:\n");
269         ub_dump_si(si);
270         return (CMD_OK);
271 }