ofw_copy.c revision 100318
1208560Sraj/*-
2208560Sraj * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3208560Sraj * All rights reserved.
4208560Sraj *
5208560Sraj * Redistribution and use in source and binary forms, with or without
6208560Sraj * modification, are permitted provided that the following conditions
7208560Sraj * are met:
8208560Sraj * 1. Redistributions of source code must retain the above copyright
9208560Sraj *    notice, this list of conditions and the following disclaimer.
10208560Sraj * 2. Redistributions in binary form must reproduce the above copyright
11208560Sraj *    notice, this list of conditions and the following disclaimer in the
12208560Sraj *    documentation and/or other materials provided with the distribution.
13208560Sraj *
14208560Sraj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15208560Sraj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16208560Sraj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17208560Sraj * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18208560Sraj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19208560Sraj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20208560Sraj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21208560Sraj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22208560Sraj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23208560Sraj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24208560Sraj * SUCH DAMAGE.
25208560Sraj *
26208560Sraj * $FreeBSD: head/sys/boot/ofw/libofw/ofw_copy.c 100318 2002-07-18 12:39:02Z benno $
27208560Sraj */
28208560Sraj/*
29208560Sraj * MD primitives supporting placement of module data
30208560Sraj *
31208560Sraj * XXX should check load address/size against memory top.
32208560Sraj */
33208560Sraj#include <stand.h>
34208560Sraj
35208560Sraj#include "libofw.h"
36208560Sraj
37208560Sraj#define	READIN_BUF	(4 * 1024)
38208560Sraj#define	PAGE_SIZE	0x1000
39208560Sraj#define	PAGE_MASK	0x0fff
40208560Sraj
41208560Sraj#define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))
42208560Sraj
43208560Srajssize_t
44208560Srajofw_copyin(const void *src, vm_offset_t dest, const size_t len)
45208560Sraj{
46208560Sraj	void	*destp, *addr;
47208560Sraj	size_t	dlen;
48208560Sraj
49208560Sraj	destp = (void *)(dest & ~PAGE_MASK);
50208560Sraj	dlen = roundup(len, PAGE_SIZE);
51208560Sraj
52208560Sraj	if (OF_call_method("claim", memory, 3, 1, destp, dlen, 0, &addr)
53208560Sraj	    == -1) {
54208560Sraj		printf("ofw_copyin: physical claim failed\n");
55208560Sraj		return (0);
56208560Sraj	}
57208560Sraj
58208560Sraj	if (OF_call_method("claim", mmu, 3, 1, destp, dlen, 0, &addr) == -1) {
59208560Sraj		printf("ofw_copyin: virtual claim failed\n");
60208560Sraj		return (0);
61208560Sraj	}
62208560Sraj
63208560Sraj	if (OF_call_method("map", mmu, 4, 0, destp, destp, dlen, 0) == -1) {
64208560Sraj		printf("ofw_copyin: map failed\n");
65208560Sraj		return (0);
66208560Sraj	}
67208560Sraj
68208560Sraj	bcopy(src, (void *)dest, len);
69208560Sraj	return(len);
70208560Sraj}
71208560Sraj
72208560Srajssize_t
73208560Srajofw_copyout(const vm_offset_t src, void *dest, const size_t len)
74208560Sraj{
75208560Sraj	bcopy((void *)src, dest, len);
76208560Sraj	return(len);
77208560Sraj}
78208560Sraj
79208560Srajssize_t
80208560Srajofw_readin(const int fd, vm_offset_t dest, const size_t len)
81208560Sraj{
82208560Sraj	void		*buf;
83208560Sraj	size_t		resid, chunk, get;
84208560Sraj	ssize_t		got;
85208560Sraj	vm_offset_t	p;
86208560Sraj
87208560Sraj	p = dest;
88208560Sraj
89208560Sraj	chunk = min(READIN_BUF, len);
90208560Sraj	buf = malloc(chunk);
91208560Sraj	if (buf == NULL) {
92208560Sraj		printf("ofw_readin: buf malloc failed\n");
93208560Sraj		return(0);
94208560Sraj	}
95208560Sraj
96208560Sraj	for (resid = len; resid > 0; resid -= got, p += got) {
97208560Sraj		get = min(chunk, resid);
98208560Sraj		got = read(fd, buf, get);
99208560Sraj
100208560Sraj		if (got <= 0) {
101208560Sraj			printf("ofw_readin: read failed\n");
102208560Sraj			break;
103208560Sraj		}
104208560Sraj
105208560Sraj		ofw_copyin(buf, p, got);
106208560Sraj	}
107208560Sraj
108208560Sraj	free(buf);
109208560Sraj	return(len - resid);
110208560Sraj}
111208560Sraj