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