xref: /freebsd/stand/uboot/copy.c (revision 9dc70af83e5992e543542adbf5a6edeb38f187f6)
1*9dc70af8SWarner Losh /*-
2*9dc70af8SWarner Losh  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3*9dc70af8SWarner Losh  * Copyright (c) 2007 Semihalf, Rafal Jaworowski <raj@semihalf.com>
4*9dc70af8SWarner Losh  * All rights reserved.
5*9dc70af8SWarner Losh  *
6*9dc70af8SWarner Losh  * Redistribution and use in source and binary forms, with or without
7*9dc70af8SWarner Losh  * modification, are permitted provided that the following conditions
8*9dc70af8SWarner Losh  * are met:
9*9dc70af8SWarner Losh  * 1. Redistributions of source code must retain the above copyright
10*9dc70af8SWarner Losh  *    notice, this list of conditions and the following disclaimer.
11*9dc70af8SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
12*9dc70af8SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
13*9dc70af8SWarner Losh  *    documentation and/or other materials provided with the distribution.
14*9dc70af8SWarner Losh  *
15*9dc70af8SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*9dc70af8SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*9dc70af8SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*9dc70af8SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*9dc70af8SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*9dc70af8SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*9dc70af8SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*9dc70af8SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*9dc70af8SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*9dc70af8SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*9dc70af8SWarner Losh  * SUCH DAMAGE.
26*9dc70af8SWarner Losh  */
27*9dc70af8SWarner Losh 
28*9dc70af8SWarner Losh #include <sys/cdefs.h>
29*9dc70af8SWarner Losh __FBSDID("$FreeBSD$");
30*9dc70af8SWarner Losh #include <sys/param.h>
31*9dc70af8SWarner Losh 
32*9dc70af8SWarner Losh #include <stand.h>
33*9dc70af8SWarner Losh #include <stdint.h>
34*9dc70af8SWarner Losh 
35*9dc70af8SWarner Losh #include "api_public.h"
36*9dc70af8SWarner Losh #include "glue.h"
37*9dc70af8SWarner Losh #include "libuboot.h"
38*9dc70af8SWarner Losh 
39*9dc70af8SWarner Losh /*
40*9dc70af8SWarner Losh  * MD primitives supporting placement of module data
41*9dc70af8SWarner Losh  */
42*9dc70af8SWarner Losh 
43*9dc70af8SWarner Losh #ifdef __arm__
44*9dc70af8SWarner Losh #define	KERN_ALIGN	(2 * 1024 * 1024)
45*9dc70af8SWarner Losh #else
46*9dc70af8SWarner Losh #define	KERN_ALIGN	PAGE_SIZE
47*9dc70af8SWarner Losh #endif
48*9dc70af8SWarner Losh 
49*9dc70af8SWarner Losh /*
50*9dc70af8SWarner Losh  * Avoid low memory, u-boot puts things like args and dtb blobs there.
51*9dc70af8SWarner Losh  */
52*9dc70af8SWarner Losh #define	KERN_MINADDR	max(KERN_ALIGN, (1024 * 1024))
53*9dc70af8SWarner Losh 
54*9dc70af8SWarner Losh extern void _start(void); /* ubldr entry point address. */
55*9dc70af8SWarner Losh 
56*9dc70af8SWarner Losh /*
57*9dc70af8SWarner Losh  * This is called for every object loaded (kernel, module, dtb file, etc).  The
58*9dc70af8SWarner Losh  * expected return value is the next address at or after the given addr which is
59*9dc70af8SWarner Losh  * appropriate for loading the given object described by type and data.  On each
60*9dc70af8SWarner Losh  * call the addr is the next address following the previously loaded object.
61*9dc70af8SWarner Losh  *
62*9dc70af8SWarner Losh  * The first call is for loading the kernel, and the addr argument will be zero,
63*9dc70af8SWarner Losh  * and we search for a big block of ram to load the kernel and modules.
64*9dc70af8SWarner Losh  *
65*9dc70af8SWarner Losh  * On subsequent calls the addr will be non-zero, and we just round it up so
66*9dc70af8SWarner Losh  * that each object begins on a page boundary.
67*9dc70af8SWarner Losh  */
68*9dc70af8SWarner Losh uint64_t
69*9dc70af8SWarner Losh uboot_loadaddr(u_int type, void *data, uint64_t addr)
70*9dc70af8SWarner Losh {
71*9dc70af8SWarner Losh 	struct sys_info *si;
72*9dc70af8SWarner Losh 	uint64_t sblock, eblock, subldr, eubldr;
73*9dc70af8SWarner Losh 	uint64_t biggest_block, this_block;
74*9dc70af8SWarner Losh 	uint64_t biggest_size, this_size;
75*9dc70af8SWarner Losh 	int i;
76*9dc70af8SWarner Losh 	char *envstr;
77*9dc70af8SWarner Losh 
78*9dc70af8SWarner Losh 	if (addr == 0) {
79*9dc70af8SWarner Losh 		/*
80*9dc70af8SWarner Losh 		 * If the loader_kernaddr environment variable is set, blindly
81*9dc70af8SWarner Losh 		 * honor it.  It had better be right.  We force interpretation
82*9dc70af8SWarner Losh 		 * of the value in base-16 regardless of any leading 0x prefix,
83*9dc70af8SWarner Losh 		 * because that's the U-Boot convention.
84*9dc70af8SWarner Losh 		 */
85*9dc70af8SWarner Losh 		envstr = ub_env_get("loader_kernaddr");
86*9dc70af8SWarner Losh 		if (envstr != NULL)
87*9dc70af8SWarner Losh 			return (strtoul(envstr, NULL, 16));
88*9dc70af8SWarner Losh 
89*9dc70af8SWarner Losh 		/*
90*9dc70af8SWarner Losh 		 *  Find addr/size of largest DRAM block.  Carve our own address
91*9dc70af8SWarner Losh 		 *  range out of the block, because loading the kernel over the
92*9dc70af8SWarner Losh 		 *  top ourself is a poor memory-conservation strategy. Avoid
93*9dc70af8SWarner Losh 		 *  memory at beginning of the first block of physical ram,
94*9dc70af8SWarner Losh 		 *  since u-boot likes to pass args and data there.  Assume that
95*9dc70af8SWarner Losh 		 *  u-boot has moved itself to the very top of ram and
96*9dc70af8SWarner Losh 		 *  optimistically assume that we won't run into it up there.
97*9dc70af8SWarner Losh 		 */
98*9dc70af8SWarner Losh 		if ((si = ub_get_sys_info()) == NULL)
99*9dc70af8SWarner Losh 			panic("could not retrieve system info");
100*9dc70af8SWarner Losh 
101*9dc70af8SWarner Losh 		biggest_block = 0;
102*9dc70af8SWarner Losh 		biggest_size = 0;
103*9dc70af8SWarner Losh 		subldr = rounddown2((uintptr_t)_start, KERN_ALIGN);
104*9dc70af8SWarner Losh 		eubldr = roundup2((uint64_t)uboot_heap_end, KERN_ALIGN);
105*9dc70af8SWarner Losh 		for (i = 0; i < si->mr_no; i++) {
106*9dc70af8SWarner Losh 			if (si->mr[i].flags != MR_ATTR_DRAM)
107*9dc70af8SWarner Losh 				continue;
108*9dc70af8SWarner Losh 			sblock = roundup2((uint64_t)si->mr[i].start,
109*9dc70af8SWarner Losh 			    KERN_ALIGN);
110*9dc70af8SWarner Losh 			eblock = rounddown2((uint64_t)si->mr[i].start +
111*9dc70af8SWarner Losh 			    si->mr[i].size, KERN_ALIGN);
112*9dc70af8SWarner Losh 			if (biggest_size == 0)
113*9dc70af8SWarner Losh 				sblock += KERN_MINADDR;
114*9dc70af8SWarner Losh 			if (subldr >= sblock && subldr < eblock) {
115*9dc70af8SWarner Losh 				if (subldr - sblock > eblock - eubldr) {
116*9dc70af8SWarner Losh 					this_block = sblock;
117*9dc70af8SWarner Losh 					this_size  = subldr - sblock;
118*9dc70af8SWarner Losh 				} else {
119*9dc70af8SWarner Losh 					this_block = eubldr;
120*9dc70af8SWarner Losh 					this_size = eblock - eubldr;
121*9dc70af8SWarner Losh 				}
122*9dc70af8SWarner Losh 			} else if (subldr < sblock && eubldr < eblock) {
123*9dc70af8SWarner Losh 				/* Loader is below or engulfs the sblock */
124*9dc70af8SWarner Losh 				this_block = (eubldr < sblock) ? sblock : eubldr;
125*9dc70af8SWarner Losh 				this_size = eblock - this_block;
126*9dc70af8SWarner Losh 			} else {
127*9dc70af8SWarner Losh 				this_block = 0;
128*9dc70af8SWarner Losh 				this_size = 0;
129*9dc70af8SWarner Losh 			}
130*9dc70af8SWarner Losh 			if (biggest_size < this_size) {
131*9dc70af8SWarner Losh 				biggest_block = this_block;
132*9dc70af8SWarner Losh 				biggest_size  = this_size;
133*9dc70af8SWarner Losh 			}
134*9dc70af8SWarner Losh 		}
135*9dc70af8SWarner Losh 		if (biggest_size == 0)
136*9dc70af8SWarner Losh 			panic("Not enough DRAM to load kernel");
137*9dc70af8SWarner Losh #if 0
138*9dc70af8SWarner Losh 		printf("Loading kernel into region 0x%08jx-0x%08jx (%ju MiB)\n",
139*9dc70af8SWarner Losh 		    (uintmax_t)biggest_block,
140*9dc70af8SWarner Losh 		    (uintmax_t)biggest_block + biggest_size - 1,
141*9dc70af8SWarner Losh 		    (uintmax_t)biggest_size / 1024 / 1024);
142*9dc70af8SWarner Losh #endif
143*9dc70af8SWarner Losh 		return (biggest_block);
144*9dc70af8SWarner Losh 	}
145*9dc70af8SWarner Losh 	return roundup2(addr, PAGE_SIZE);
146*9dc70af8SWarner Losh }
147*9dc70af8SWarner Losh 
148*9dc70af8SWarner Losh ssize_t
149*9dc70af8SWarner Losh uboot_copyin(const void *src, vm_offset_t dest, const size_t len)
150*9dc70af8SWarner Losh {
151*9dc70af8SWarner Losh 	bcopy(src, (void *)dest, len);
152*9dc70af8SWarner Losh 	return (len);
153*9dc70af8SWarner Losh }
154*9dc70af8SWarner Losh 
155*9dc70af8SWarner Losh ssize_t
156*9dc70af8SWarner Losh uboot_copyout(const vm_offset_t src, void *dest, const size_t len)
157*9dc70af8SWarner Losh {
158*9dc70af8SWarner Losh 	bcopy((void *)src, dest, len);
159*9dc70af8SWarner Losh 	return (len);
160*9dc70af8SWarner Losh }
161*9dc70af8SWarner Losh 
162*9dc70af8SWarner Losh ssize_t
163*9dc70af8SWarner Losh uboot_readin(readin_handle_t fd, vm_offset_t dest, const size_t len)
164*9dc70af8SWarner Losh {
165*9dc70af8SWarner Losh 	return (VECTX_READ(fd, (void *)dest, len));
166*9dc70af8SWarner Losh }
167