2 * Copyright (C) 2010-2014 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <fdt_platform.h>
34 #include <machine/cpufunc.h>
35 #include "bootstrap.h"
36 #include "host_syscall.h"
38 struct arch_switch archsw;
41 extern char bootprog_info[];
43 int kboot_getdev(void **vdev, const char *devspec, const char **path);
44 ssize_t kboot_copyin(const void *src, vm_offset_t dest, const size_t len);
45 ssize_t kboot_copyout(vm_offset_t src, void *dest, const size_t len);
46 ssize_t kboot_readin(const int fd, vm_offset_t dest, const size_t len);
47 int kboot_autoload(void);
48 uint64_t kboot_loadaddr(u_int type, void *data, uint64_t addr);
49 int kboot_setcurrdev(struct env_var *ev, int flags, const void *value);
51 extern int command_fdt_internal(int argc, char *argv[]);
54 kboot_getdev(void **vdev, const char *devspec, const char **path)
57 const char *devpath, *filepath;
61 if (strchr(devspec, ':') != NULL) {
63 filepath = strchr(devspec, ':') + 1;
65 devpath = getenv("currdev");
69 for (i = 0; (dv = devsw[i]) != NULL; i++) {
70 if (strncmp(dv->dv_name, devpath, strlen(dv->dv_name)) == 0)
76 if (path != NULL && filepath != NULL)
78 else if (path != NULL)
79 *path = strchr(devspec, ':') + 1;
82 desc = malloc(sizeof(*desc));
85 desc->d_opendata = strdup(devpath);
93 main(int argc, const char **argv)
96 const size_t heapsize = 15*1024*1024;
97 const char *bootdev = argv[1];
100 * Set the heap to one page after the end of the loader.
102 heapbase = host_getmem(heapsize);
103 setheap(heapbase, heapbase + heapsize);
110 printf("Boot device: %s\n", bootdev);
112 archsw.arch_getdev = kboot_getdev;
113 archsw.arch_copyin = kboot_copyin;
114 archsw.arch_copyout = kboot_copyout;
115 archsw.arch_readin = kboot_readin;
116 archsw.arch_autoload = kboot_autoload;
117 archsw.arch_loadaddr = kboot_loadaddr;
119 printf("\n%s", bootprog_info);
121 setenv("currdev", bootdev, 1);
122 setenv("loaddev", bootdev, 1);
123 setenv("LINES", "24", 1);
125 interact(NULL); /* doesn't return */
139 struct host_timeval tvi, tv;
141 host_gettimeofday(&tvi, NULL);
142 ti = tvi.tv_sec*1000000 + tvi.tv_usec;
144 host_gettimeofday(&tv, NULL);
145 t = tv.tv_sec*1000000 + tv.tv_usec;
146 } while (t < ti + usecs);
152 struct host_timeval tv;
153 host_gettimeofday(&tv, NULL);
169 struct kexec_segment {
176 struct kexec_segment loaded_segments[128];
177 int nkexec_segments = 0;
180 get_phys_buffer(vm_offset_t dest, const size_t len, void **buf)
183 const size_t segsize = 2*1024*1024;
185 for (i = 0; i < nkexec_segments; i++) {
186 if (dest >= (vm_offset_t)loaded_segments[i].mem &&
187 dest < (vm_offset_t)loaded_segments[i].mem +
188 loaded_segments[i].memsz)
192 loaded_segments[nkexec_segments].buf = host_getmem(segsize);
193 loaded_segments[nkexec_segments].bufsz = segsize;
194 loaded_segments[nkexec_segments].mem = (void *)rounddown2(dest,segsize);
195 loaded_segments[nkexec_segments].memsz = segsize;
200 *buf = loaded_segments[i].buf + (dest -
201 (vm_offset_t)loaded_segments[i].mem);
202 return (min(len,loaded_segments[i].bufsz - (dest -
203 (vm_offset_t)loaded_segments[i].mem)));
207 kboot_copyin(const void *src, vm_offset_t dest, const size_t len)
209 ssize_t segsize, remainder;
214 segsize = get_phys_buffer(dest, remainder, &destbuf);
215 bcopy(src, destbuf, segsize);
216 remainder -= segsize;
219 } while (remainder > 0);
225 kboot_copyout(vm_offset_t src, void *dest, const size_t len)
227 ssize_t segsize, remainder;
232 segsize = get_phys_buffer(src, remainder, &srcbuf);
233 bcopy(srcbuf, dest, segsize);
234 remainder -= segsize;
237 } while (remainder > 0);
243 kboot_readin(const int fd, vm_offset_t dest, const size_t len)
246 size_t resid, chunk, get;
252 chunk = min(PAGE_SIZE, len);
255 printf("kboot_readin: buf malloc failed\n");
259 for (resid = len; resid > 0; resid -= got, p += got) {
260 get = min(chunk, resid);
261 got = read(fd, buf, get);
264 printf("kboot_readin: read failed\n");
268 kboot_copyin(buf, p, got);
272 return (len - resid);
283 kboot_loadaddr(u_int type, void *data, uint64_t addr)
286 * Need to stay out of the way of Linux. /chosen/linux,kernel-end does
287 * a better job here, but use a fixed offset for now.
290 if (type == LOAD_ELF)
291 addr = roundup(addr, PAGE_SIZE);
293 addr += 64*1024*1024; /* Stay out of the way of Linux */
299 _start(int argc, const char **argv, char **env)
301 register volatile void **sp asm("r1");
302 main((int)sp[0], (const char **)&sp[1]);
306 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
307 * and declaring it as extern is in contradiction with COMMAND_SET() macro
308 * (which uses static pointer), we're defining wrapper function, which
309 * calls the proper fdt handling routine.
312 command_fdt(int argc, char *argv[])
315 return (command_fdt_internal(argc, argv));
318 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);