1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/promif.h>
30 #include <sys/promimpl.h>
31 
32 int
33 prom_fopen(ihandle_t fsih, char *path)
34 {
35 	cell_t ci[10];
36 	size_t len;
37 
38 #ifdef PROM_32BIT_ADDRS
39 	char *opath = NULL;
40 
41 	if ((uintptr_t)path > (uint32_t)-1) {
42 		opath = path;
43 		len = prom_strlen(opath) + 1; /* include terminating NUL */
44 		path = promplat_alloc(len);
45 		if (path == NULL)
46 			return (0);
47 		(void) prom_strcpy(path, opath);
48 	}
49 #endif
50 	len = prom_strlen(path);
51 
52 	promif_preprom();
53 	ci[0] = p1275_ptr2cell("call-method");	/* Service name */
54 	ci[1] = (cell_t)4;			/* #argument cells */
55 	ci[2] = (cell_t)3;			/* #result cells */
56 	ci[3] = p1275_ptr2cell("open-file");	/* Arg1: Method name */
57 	ci[4] = p1275_ihandle2cell(fsih);	/* Arg2: fs ihandle */
58 	ci[5] = p1275_uint2cell(len);		/* Arg3: Len */
59 	ci[6] = p1275_ptr2cell(path);		/* Arg4: Pathname */
60 
61 	(void) p1275_cif_handler(&ci);
62 
63 	promif_postprom();
64 
65 #ifdef PROM_32BIT_ADDRS
66 	if (opath != NULL)
67 		promplat_free(path, len + 1);
68 #endif
69 
70 	if (ci[7] != 0)				/* Catch result */
71 		return (-1);
72 
73 	if (ci[8] == 0)				/* Res1: failed */
74 		return (-1);
75 
76 	return (p1275_cell2int(ci[9]));		/* Res2: fd */
77 }
78 
79 
80 int
81 prom_fseek(ihandle_t fsih, int fd, unsigned long long offset)
82 {
83 	cell_t ci[10];
84 
85 	ci[0] = p1275_ptr2cell("call-method");	/* Service name */
86 	ci[1] = (cell_t)4;			/* #argument cells */
87 	ci[2] = (cell_t)3;			/* #result cells */
88 	ci[3] = p1275_ptr2cell("seek-file");	/* Arg1: Method name */
89 	ci[4] = p1275_ihandle2cell(fsih);	/* Arg2: fs ihandle */
90 	ci[5] = p1275_int2cell(fd);		/* Arg3: file desc */
91 	ci[6] = p1275_ull2cell_low(offset);	/* Arg4: Offset */
92 
93 	promif_preprom();
94 	(void) p1275_cif_handler(&ci);
95 	promif_postprom();
96 
97 	if (ci[7] != 0)				/* Catch result */
98 		return (-1);
99 
100 	if (ci[8] == 0)				/* Res1: failed */
101 		return (-1);
102 
103 	return (p1275_cell2int(ci[9]));		/* Res2: off */
104 }
105 
106 
107 int
108 prom_fread(ihandle_t fsih, int fd, caddr_t buf, size_t len)
109 {
110 	cell_t ci[10];
111 #ifdef PROM_32BIT_ADDRS
112 	caddr_t obuf = NULL;
113 
114 	if ((uintptr_t)buf > (uint32_t)-1) {
115 		obuf = buf;
116 		buf = promplat_alloc(len);
117 		if (buf == NULL)
118 			return (-1);
119 	}
120 #endif
121 
122 	promif_preprom();
123 
124 	ci[0] = p1275_ptr2cell("call-method");	/* Service name */
125 	ci[1] = (cell_t)5;			/* #argument cells */
126 	ci[2] = (cell_t)2;			/* #result cells */
127 	ci[3] = p1275_ptr2cell("read-file");	/* Arg1: Method name */
128 	ci[4] = p1275_ihandle2cell(fsih);	/* Arg2: fs ihandle */
129 	ci[5] = p1275_int2cell(fd);		/* Arg3: file desc */
130 	ci[6] = p1275_uint2cell(len);		/* Arg4: buffer length */
131 	ci[7] = p1275_ptr2cell(buf);		/* Arg5: buffer address */
132 
133 	(void) p1275_cif_handler(&ci);
134 
135 	promif_postprom();
136 
137 #ifdef PROM_32BIT_ADDRS
138 	if (obuf != NULL) {
139 		promplat_bcopy(buf, obuf, len);
140 		promplat_free(buf, len);
141 	}
142 #endif
143 
144 	if (ci[8] != 0)				/* Catch result */
145 		return (-1);
146 
147 	return (p1275_cell2int(ci[9]));		/* Res2: actual length */
148 }
149 
150 int
151 prom_fsize(ihandle_t fsih, int fd, size_t *size)
152 {
153 	cell_t ci[8];
154 
155 	promif_preprom();
156 
157 	ci[0] = p1275_ptr2cell("call-method");	/* Service name */
158 	ci[1] = (cell_t)3;			/* #argument cells */
159 	ci[2] = (cell_t)2;			/* #result cells */
160 	ci[3] = p1275_ptr2cell("size-file");	/* Arg1: Method name */
161 	ci[4] = p1275_ihandle2cell(fsih);	/* Arg2: fs ihandle */
162 	ci[5] = p1275_int2cell(fd);		/* Arg3: file desc */
163 
164 	(void) p1275_cif_handler(&ci);
165 
166 	promif_postprom();
167 
168 	if (ci[6] != 0)				/* Catch result */
169 		return (-1);
170 
171 	*size = p1275_cell2uint(ci[7]);		/* Res2: size */
172 	return (0);
173 }
174 
175 
176 int
177 prom_compinfo(ihandle_t fsih, int fd, int *iscmp, size_t *fsize, size_t *bsize)
178 {
179 	cell_t ci[10];
180 
181 	promif_preprom();
182 
183 	ci[0] = p1275_ptr2cell("call-method");	/* Service name */
184 	ci[1] = (cell_t)3;			/* #argument cells */
185 	ci[2] = (cell_t)4;			/* #result cells */
186 	ci[3] = p1275_ptr2cell("cinfo-file");	/* Arg1: Method name */
187 	ci[4] = p1275_ihandle2cell(fsih);	/* Arg2: fs ihandle */
188 	ci[5] = p1275_int2cell(fd);		/* Arg3: file desc */
189 
190 	(void) p1275_cif_handler(&ci);
191 
192 	promif_postprom();
193 
194 	if (ci[6] != 0)				/* Catch result */
195 		return (-1);
196 
197 	*iscmp = p1275_cell2int(ci[7]);		/* Res2: iscmp */
198 	*fsize = p1275_cell2uint(ci[8]);	/* Res3: fsize */
199 	*bsize = p1275_cell2uint(ci[9]);	/* Res4: bsize */
200 	return (0);
201 }
202 
203 void
204 prom_fclose(ihandle_t fsih, int fd)
205 {
206 	cell_t ci[7];
207 
208 	ci[0] = p1275_ptr2cell("call-method");	/* Service name */
209 	ci[1] = (cell_t)3;			/* #argument cells */
210 	ci[2] = (cell_t)1;			/* #result cells */
211 	ci[3] = p1275_ptr2cell("close-file");	/* Arg1: Method name */
212 	ci[4] = p1275_ihandle2cell(fsih);	/* Arg2: fs ihandle */
213 	ci[5] = p1275_int2cell(fd);		/* Arg3: file desc */
214 
215 	promif_preprom();
216 	(void) p1275_cif_handler(&ci);
217 	promif_postprom();
218 
219 }
220