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