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