1 /*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2007 Semihalf, Rafal Jaworowski <raj@semihalf.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29
30 #include <stand.h>
31 #include <stdint.h>
32
33 #include "api_public.h"
34 #include "glue.h"
35 #include "libuboot.h"
36
37 /*
38 * MD primitives supporting placement of module data
39 */
40
41 #ifdef __arm__
42 #define KERN_ALIGN (2 * 1024 * 1024)
43 #else
44 #define KERN_ALIGN PAGE_SIZE
45 #endif
46
47 /*
48 * Avoid low memory, u-boot puts things like args and dtb blobs there.
49 */
50 #define KERN_MINADDR max(KERN_ALIGN, (1024 * 1024))
51
52 extern void _start(void); /* ubldr entry point address. */
53
54 uint64_t loadbase;
55 bool loadbase_set = false;
56
57 /*
58 * This is called for every object loaded (kernel, module, dtb file, etc). The
59 * expected return value is the next address at or after the given addr which is
60 * appropriate for loading the given object described by type and data. On each
61 * call the addr is the next address following the previously loaded object.
62 */
63 static uint64_t
uboot_loadaddr(void)64 uboot_loadaddr(void)
65 {
66 struct sys_info *si;
67 uint64_t sblock, eblock, subldr, eubldr;
68 uint64_t biggest_block, this_block;
69 uint64_t biggest_size, this_size;
70 int i;
71 char *envstr;
72
73 /*
74 * If the loader_kernaddr environment variable is set, blindly
75 * honor it. It had better be right. We force interpretation
76 * of the value in base-16 regardless of any leading 0x prefix,
77 * because that's the U-Boot convention.
78 */
79 envstr = ub_env_get("loader_kernaddr");
80 if (envstr != NULL)
81 return (strtoul(envstr, NULL, 16));
82
83 /*
84 * Find addr/size of largest DRAM block. Carve our own address
85 * range out of the block, because loading the kernel over the
86 * top ourself is a poor memory-conservation strategy. Avoid
87 * memory at beginning of the first block of physical ram,
88 * since u-boot likes to pass args and data there. Assume that
89 * u-boot has moved itself to the very top of ram and
90 * optimistically assume that we won't run into it up there.
91 */
92 if ((si = ub_get_sys_info()) == NULL)
93 panic("could not retrieve system info");
94
95 biggest_block = 0;
96 biggest_size = 0;
97 subldr = rounddown2((uintptr_t)_start, KERN_ALIGN);
98 eubldr = roundup2((uint64_t)uboot_heap_end, KERN_ALIGN);
99 for (i = 0; i < si->mr_no; i++) {
100 if (si->mr[i].flags != MR_ATTR_DRAM)
101 continue;
102 sblock = roundup2((uint64_t)si->mr[i].start,
103 KERN_ALIGN);
104 eblock = rounddown2((uint64_t)si->mr[i].start +
105 si->mr[i].size, KERN_ALIGN);
106 if (biggest_size == 0)
107 sblock += KERN_MINADDR;
108 if (subldr >= sblock && subldr < eblock) {
109 if (subldr - sblock > eblock - eubldr) {
110 this_block = sblock;
111 this_size = subldr - sblock;
112 } else {
113 this_block = eubldr;
114 this_size = eblock - eubldr;
115 }
116 } else if (subldr < sblock && eubldr < eblock) {
117 /* Loader is below or engulfs the sblock */
118 this_block = (eubldr < sblock) ? sblock : eubldr;
119 this_size = eblock - this_block;
120 } else {
121 this_block = 0;
122 this_size = 0;
123 }
124 if (biggest_size < this_size) {
125 biggest_block = this_block;
126 biggest_size = this_size;
127 }
128 }
129 if (biggest_size == 0)
130 panic("Not enough DRAM to load kernel");
131 #if 0
132 printf("Loading kernel into region 0x%08jx-0x%08jx (%ju MiB)\n",
133 (uintmax_t)biggest_block,
134 (uintmax_t)biggest_block + biggest_size - 1,
135 (uintmax_t)biggest_size / 1024 / 1024);
136 #endif
137 return (biggest_block);
138 }
139
140 ssize_t
uboot_copyin(const void * src,vm_offset_t dest,const size_t len)141 uboot_copyin(const void *src, vm_offset_t dest, const size_t len)
142 {
143 if (!loadbase_set) {
144 loadbase = uboot_loadaddr();
145 loadbase_set = true;
146 }
147
148 bcopy(src, (void *)(dest + loadbase), len);
149 return (len);
150 }
151
152 ssize_t
uboot_copyout(const vm_offset_t src,void * dest,const size_t len)153 uboot_copyout(const vm_offset_t src, void *dest, const size_t len)
154 {
155 bcopy((void *)(src + loadbase), dest, len);
156 return (len);
157 }
158
159 ssize_t
uboot_readin(readin_handle_t fd,vm_offset_t dest,const size_t len)160 uboot_readin(readin_handle_t fd, vm_offset_t dest, const size_t len)
161 {
162 return (VECTX_READ(fd, (void *)dest, len));
163 }
164