1176348Smarcel/*- 2176348Smarcel * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3176348Smarcel * Copyright (c) 2007 Semihalf, Rafal Jaworowski <raj@semihalf.com> 4176348Smarcel * All rights reserved. 5176348Smarcel * 6176348Smarcel * Redistribution and use in source and binary forms, with or without 7176348Smarcel * modification, are permitted provided that the following conditions 8176348Smarcel * are met: 9176348Smarcel * 1. Redistributions of source code must retain the above copyright 10176348Smarcel * notice, this list of conditions and the following disclaimer. 11176348Smarcel * 2. Redistributions in binary form must reproduce the above copyright 12176348Smarcel * notice, this list of conditions and the following disclaimer in the 13176348Smarcel * documentation and/or other materials provided with the distribution. 14176348Smarcel * 15176348Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16176348Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17176348Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18176348Smarcel * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19176348Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20176348Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21176348Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22176348Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23176348Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24176348Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25176348Smarcel * SUCH DAMAGE. 26176348Smarcel */ 27176348Smarcel 28176348Smarcel#include <sys/cdefs.h> 29176348Smarcel__FBSDID("$FreeBSD: releng/11.0/sys/boot/uboot/lib/copy.c 293792 2016-01-13 00:22:12Z ian $"); 30283035Sian#include <sys/param.h> 31176348Smarcel 32176348Smarcel#include <stand.h> 33235694Skientzle#include <stdint.h> 34176348Smarcel 35235694Skientzle#include "api_public.h" 36235694Skientzle#include "glue.h" 37283035Sian#include "libuboot.h" 38235694Skientzle 39176348Smarcel/* 40176348Smarcel * MD primitives supporting placement of module data 41176348Smarcel */ 42176348Smarcel 43283035Sian#ifdef __arm__ 44283035Sian#define KERN_ALIGN (2 * 1024 * 1024) 45283035Sian#else 46283035Sian#define KERN_ALIGN PAGE_SIZE 47283035Sian#endif 48283035Sian 49283035Sian/* 50283035Sian * Avoid low memory, u-boot puts things like args and dtb blobs there. 51283035Sian */ 52283035Sian#define KERN_MINADDR max(KERN_ALIGN, (1024 * 1024)) 53283035Sian 54283035Sianextern void _start(void); /* ubldr entry point address. */ 55283035Sian 56283035Sian/* 57283035Sian * This is called for every object loaded (kernel, module, dtb file, etc). The 58283035Sian * expected return value is the next address at or after the given addr which is 59283035Sian * appropriate for loading the given object described by type and data. On each 60283035Sian * call the addr is the next address following the previously loaded object. 61283035Sian * 62283035Sian * The first call is for loading the kernel, and the addr argument will be zero, 63283035Sian * and we search for a big block of ram to load the kernel and modules. 64283035Sian * 65283035Sian * On subsequent calls the addr will be non-zero, and we just round it up so 66283035Sian * that each object begins on a page boundary. 67283035Sian */ 68283035Sianuint64_t 69283035Sianuboot_loadaddr(u_int type, void *data, uint64_t addr) 70283035Sian{ 71235694Skientzle struct sys_info *si; 72293053Sian uint64_t sblock, eblock, subldr, eubldr; 73293053Sian uint64_t biggest_block, this_block; 74293053Sian uint64_t biggest_size, this_size; 75235694Skientzle int i; 76293053Sian char *envstr; 77235694Skientzle 78283035Sian if (addr == 0) { 79283035Sian /* 80283035Sian * If the loader_kernaddr environment variable is set, blindly 81283035Sian * honor it. It had better be right. We force interpretation 82283035Sian * of the value in base-16 regardless of any leading 0x prefix, 83283035Sian * because that's the U-Boot convention. 84283035Sian */ 85283035Sian envstr = ub_env_get("loader_kernaddr"); 86283035Sian if (envstr != NULL) 87283035Sian return (strtoul(envstr, NULL, 16)); 88283035Sian 89283035Sian /* 90283035Sian * Find addr/size of largest DRAM block. Carve our own address 91283035Sian * range out of the block, because loading the kernel over the 92283035Sian * top ourself is a poor memory-conservation strategy. Avoid 93283035Sian * memory at beginning of the first block of physical ram, 94283035Sian * since u-boot likes to pass args and data there. Assume that 95283035Sian * u-boot has moved itself to the very top of ram and 96283035Sian * optimistically assume that we won't run into it up there. 97283035Sian */ 98235694Skientzle if ((si = ub_get_sys_info()) == NULL) 99235694Skientzle panic("could not retrieve system info"); 100235694Skientzle 101283035Sian biggest_block = 0; 102283035Sian biggest_size = 0; 103293792Sian subldr = rounddown2((uintptr_t)_start, KERN_ALIGN); 104293053Sian eubldr = roundup2((uint64_t)uboot_heap_end, KERN_ALIGN); 105235694Skientzle for (i = 0; i < si->mr_no; i++) { 106283035Sian if (si->mr[i].flags != MR_ATTR_DRAM) 107283035Sian continue; 108293053Sian sblock = roundup2((uint64_t)si->mr[i].start, 109283035Sian KERN_ALIGN); 110293053Sian eblock = rounddown2((uint64_t)si->mr[i].start + 111293053Sian si->mr[i].size, KERN_ALIGN); 112283035Sian if (biggest_size == 0) 113283035Sian sblock += KERN_MINADDR; 114283035Sian if (subldr >= sblock && subldr < eblock) { 115283035Sian if (subldr - sblock > eblock - eubldr) { 116283035Sian this_block = sblock; 117283035Sian this_size = subldr - sblock; 118283035Sian } else { 119283035Sian this_block = eubldr; 120283035Sian this_size = eblock - eubldr; 121283035Sian } 122284599Ssobomax } else if (subldr < sblock && eubldr < eblock) { 123284599Ssobomax /* Loader is below or engulfs the sblock */ 124284599Ssobomax this_block = (eubldr < sblock) ? sblock : eubldr; 125284599Ssobomax this_size = eblock - this_block; 126284599Ssobomax } else { 127284599Ssobomax this_block = 0; 128284599Ssobomax this_size = 0; 129235694Skientzle } 130283035Sian if (biggest_size < this_size) { 131283035Sian biggest_block = this_block; 132283035Sian biggest_size = this_size; 133283035Sian } 134235694Skientzle } 135283035Sian if (biggest_size == 0) 136283035Sian panic("Not enough DRAM to load kernel\n"); 137283035Sian#if 0 138293053Sian printf("Loading kernel into region 0x%08jx-0x%08jx (%ju MiB)\n", 139293053Sian (uintmax_t)biggest_block, 140293053Sian (uintmax_t)biggest_block + biggest_size - 1, 141293053Sian (uintmax_t)biggest_size / 1024 / 1024); 142283035Sian#endif 143283035Sian return (biggest_block); 144235694Skientzle } 145283035Sian return roundup2(addr, PAGE_SIZE); 146235694Skientzle} 147235694Skientzle 148176348Smarcelssize_t 149176348Smarceluboot_copyin(const void *src, vm_offset_t dest, const size_t len) 150176348Smarcel{ 151283035Sian bcopy(src, (void *)dest, len); 152177152Sobrien return (len); 153176348Smarcel} 154176348Smarcel 155176348Smarcelssize_t 156176348Smarceluboot_copyout(const vm_offset_t src, void *dest, const size_t len) 157176348Smarcel{ 158283035Sian bcopy((void *)src, dest, len); 159176348Smarcel return (len); 160176348Smarcel} 161176348Smarcel 162176348Smarcelssize_t 163176348Smarceluboot_readin(const int fd, vm_offset_t dest, const size_t len) 164176348Smarcel{ 165283035Sian return (read(fd, (void *)dest, len)); 166176348Smarcel} 167