i386_copy.c revision 55211
155211Smsmith/*- 255211Smsmith * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 355211Smsmith * All rights reserved. 455211Smsmith * 555211Smsmith * Redistribution and use in source and binary forms, with or without 655211Smsmith * modification, are permitted provided that the following conditions 755211Smsmith * are met: 855211Smsmith * 1. Redistributions of source code must retain the above copyright 955211Smsmith * notice, this list of conditions and the following disclaimer. 1055211Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1155211Smsmith * notice, this list of conditions and the following disclaimer in the 1255211Smsmith * documentation and/or other materials provided with the distribution. 1355211Smsmith * 1455211Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1555211Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1655211Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1755211Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1855211Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1955211Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2055211Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2155211Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2255211Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2355211Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2455211Smsmith * SUCH DAMAGE. 2555211Smsmith * 2655211Smsmith * $FreeBSD: head/sys/boot/i386/libi386/i386_copy.c 55211 1999-12-29 09:54:46Z msmith $ 2738712Smsmith */ 2855211Smsmith 2938712Smsmith/* 3038712Smsmith * MD primitives supporting placement of module data 3138712Smsmith * 3238712Smsmith * XXX should check load address/size against memory top. 3338712Smsmith */ 3438712Smsmith#include <stand.h> 3538712Smsmith 3638712Smsmith#include "libi386.h" 3739441Smsmith#include "btxv86.h" 3838712Smsmith 3940107Smsmith#define READIN_BUF (16 * 1024) 4038712Smsmith 4138712Smsmithint 4238712Smsmithi386_copyin(void *src, vm_offset_t dest, size_t len) 4338712Smsmith{ 4455211Smsmith if (dest + len >= memtop) { 4555211Smsmith errno = EFBIG; 4655211Smsmith return(-1); 4755211Smsmith } 4839441Smsmith 4939441Smsmith bcopy(src, PTOV(dest), len); 5038712Smsmith return(len); 5138712Smsmith} 5238712Smsmith 5338712Smsmithint 5438764Smsmithi386_copyout(vm_offset_t src, void *dest, size_t len) 5538764Smsmith{ 5655211Smsmith if (src + len >= memtop) { 5755211Smsmith errno = EFBIG; 5855211Smsmith return(-1); 5955211Smsmith } 6039441Smsmith 6139441Smsmith bcopy(PTOV(src), dest, len); 6238764Smsmith return(len); 6338764Smsmith} 6438764Smsmith 6538764Smsmith 6638764Smsmithint 6738712Smsmithi386_readin(int fd, vm_offset_t dest, size_t len) 6838712Smsmith{ 6938712Smsmith void *buf; 7043068Srnordier size_t resid, chunk, get; 7143068Srnordier ssize_t got; 7238712Smsmith 7339441Smsmith if (dest + len >= memtop) 7439441Smsmith return(0); 7539441Smsmith 7638712Smsmith chunk = min(READIN_BUF, len); 7738712Smsmith buf = malloc(chunk); 7838712Smsmith if (buf == NULL) 7938712Smsmith return(0); 8038712Smsmith 8138712Smsmith for (resid = len; resid > 0; resid -= got, dest += got) { 8238712Smsmith get = min(chunk, resid); 8338712Smsmith got = read(fd, buf, get); 8438712Smsmith if (got <= 0) 8538712Smsmith break; 8639733Speter bcopy(buf, PTOV(dest), got); 8738712Smsmith } 8838712Smsmith free(buf); 8938712Smsmith return(len - resid); 9038712Smsmith} 9138712Smsmith 9238712Smsmith 93