2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2007 Semihalf, Rafal Jaworowski <raj@semihalf.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
35 #include "api_public.h"
40 * MD primitives supporting placement of module data
44 #define KERN_ALIGN (2 * 1024 * 1024)
46 #define KERN_ALIGN PAGE_SIZE
50 * Avoid low memory, u-boot puts things like args and dtb blobs there.
52 #define KERN_MINADDR max(KERN_ALIGN, (1024 * 1024))
54 extern void _start(void); /* ubldr entry point address. */
57 * This is called for every object loaded (kernel, module, dtb file, etc). The
58 * expected return value is the next address at or after the given addr which is
59 * appropriate for loading the given object described by type and data. On each
60 * call the addr is the next address following the previously loaded object.
62 * The first call is for loading the kernel, and the addr argument will be zero,
63 * and we search for a big block of ram to load the kernel and modules.
65 * On subsequent calls the addr will be non-zero, and we just round it up so
66 * that each object begins on a page boundary.
69 uboot_loadaddr(u_int type, void *data, uint64_t addr)
72 uintptr_t sblock, eblock, subldr, eubldr;
73 uintptr_t biggest_block, this_block;
74 size_t biggest_size, this_size;
80 * If the loader_kernaddr environment variable is set, blindly
81 * honor it. It had better be right. We force interpretation
82 * of the value in base-16 regardless of any leading 0x prefix,
83 * because that's the U-Boot convention.
85 envstr = ub_env_get("loader_kernaddr");
87 return (strtoul(envstr, NULL, 16));
90 * Find addr/size of largest DRAM block. Carve our own address
91 * range out of the block, because loading the kernel over the
92 * top ourself is a poor memory-conservation strategy. Avoid
93 * memory at beginning of the first block of physical ram,
94 * since u-boot likes to pass args and data there. Assume that
95 * u-boot has moved itself to the very top of ram and
96 * optimistically assume that we won't run into it up there.
98 if ((si = ub_get_sys_info()) == NULL)
99 panic("could not retrieve system info");
103 subldr = rounddown2((uintptr_t)_start, KERN_ALIGN);
104 eubldr = roundup2(uboot_heap_end, KERN_ALIGN);
105 for (i = 0; i < si->mr_no; i++) {
106 if (si->mr[i].flags != MR_ATTR_DRAM)
108 sblock = roundup2(si->mr[i].start, KERN_ALIGN);
109 eblock = rounddown2(si->mr[i].start + si->mr[i].size,
111 if (biggest_size == 0)
112 sblock += KERN_MINADDR;
113 if (subldr >= sblock && subldr < eblock) {
114 if (subldr - sblock > eblock - eubldr) {
116 this_size = subldr - sblock;
119 this_size = eblock - eubldr;
122 if (biggest_size < this_size) {
123 biggest_block = this_block;
124 biggest_size = this_size;
127 if (biggest_size == 0)
128 panic("Not enough DRAM to load kernel\n");
130 printf("Loading kernel into region 0x%08x-0x%08x (%u MiB)\n",
131 biggest_block, biggest_block + biggest_size - 1,
132 biggest_size / 1024 / 1024);
134 return (biggest_block);
136 return roundup2(addr, PAGE_SIZE);
140 uboot_copyin(const void *src, vm_offset_t dest, const size_t len)
142 bcopy(src, (void *)dest, len);
147 uboot_copyout(const vm_offset_t src, void *dest, const size_t len)
149 bcopy((void *)src, dest, len);
154 uboot_readin(const int fd, vm_offset_t dest, const size_t len)
156 return (read(fd, (void *)dest, len));