i386_copy.c revision 119482
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.
2538712Smsmith */
2655211Smsmith
27119482Sobrien#include <sys/cdefs.h>
28119482Sobrien__FBSDID("$FreeBSD: head/sys/boot/i386/libi386/i386_copy.c 119482 2003-08-25 23:28:32Z obrien $");
29119482Sobrien
3038712Smsmith/*
3138712Smsmith * MD primitives supporting placement of module data
3238712Smsmith *
3338712Smsmith * XXX should check load address/size against memory top.
3438712Smsmith */
3538712Smsmith#include <stand.h>
3638712Smsmith
3738712Smsmith#include "libi386.h"
3839441Smsmith#include "btxv86.h"
3938712Smsmith
4040107Smsmith#define READIN_BUF	(16 * 1024)
4138712Smsmith
4264187Sjhbssize_t
4364187Sjhbi386_copyin(const void *src, vm_offset_t dest, const size_t len)
4438712Smsmith{
4555211Smsmith    if (dest + len >= memtop) {
4655211Smsmith	errno = EFBIG;
4755211Smsmith	return(-1);
4855211Smsmith    }
4939441Smsmith
5039441Smsmith    bcopy(src, PTOV(dest), len);
5138712Smsmith    return(len);
5238712Smsmith}
5338712Smsmith
5464187Sjhbssize_t
5564187Sjhbi386_copyout(const vm_offset_t src, void *dest, const size_t len)
5638764Smsmith{
5755211Smsmith    if (src + len >= memtop) {
5855211Smsmith	errno = EFBIG;
5955211Smsmith	return(-1);
6055211Smsmith    }
6139441Smsmith
6239441Smsmith    bcopy(PTOV(src), dest, len);
6338764Smsmith    return(len);
6438764Smsmith}
6538764Smsmith
6638764Smsmith
6764187Sjhbssize_t
6864187Sjhbi386_readin(const int fd, vm_offset_t dest, const size_t len)
6938712Smsmith{
7038712Smsmith    void	*buf;
7143068Srnordier    size_t	resid, chunk, get;
7243068Srnordier    ssize_t	got;
7338712Smsmith
7439441Smsmith    if (dest + len >= memtop)
7539441Smsmith	return(0);
7639441Smsmith
7738712Smsmith    chunk = min(READIN_BUF, len);
7838712Smsmith    buf = malloc(chunk);
7938712Smsmith    if (buf == NULL)
8038712Smsmith	return(0);
8138712Smsmith
8238712Smsmith    for (resid = len; resid > 0; resid -= got, dest += got) {
8338712Smsmith	get = min(chunk, resid);
8438712Smsmith	got = read(fd, buf, get);
8538712Smsmith	if (got <= 0)
8638712Smsmith	    break;
8764187Sjhb	bcopy(buf, PTOV(dest), (size_t)got);
8838712Smsmith    }
8938712Smsmith    free(buf);
9038712Smsmith    return(len - resid);
9138712Smsmith}
92