Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / tools / promif / common / prom_prop.c
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* Hypervisor Software File: prom_prop.c
5*
6* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
7*
8* - Do no alter or remove copyright notices
9*
10* - Redistribution and use of this software in source and binary forms, with
11* or without modification, are permitted provided that the following
12* conditions are met:
13*
14* - Redistribution of source code must retain the above copyright notice,
15* this list of conditions and the following disclaimer.
16*
17* - Redistribution in binary form must reproduce the above copyright notice,
18* this list of conditions and the following disclaimer in the
19* documentation and/or other materials provided with the distribution.
20*
21* Neither the name of Sun Microsystems, Inc. or the names of contributors
22* may be used to endorse or promote products derived from this software
23* without specific prior written permission.
24*
25* This software is provided "AS IS," without a warranty of any kind.
26* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
27* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
28* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
29* MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
30* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
31* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
32* OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
33* FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
34* DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
35* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
36* SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37*
38* You acknowledge that this software is not designed, licensed or
39* intended for use in the design, construction, operation or maintenance of
40* any nuclear facility.
41*
42* ========== Copyright Header End ============================================
43*/
44/*
45 * Copyright (c) 2000-2003 Sun Microsystems, Inc.
46 * All rights reserved.
47 * Use is subject to license terms.
48 */
49
50#pragma ident "@(#)prom_prop.c 1.1 00/08/07 SMI"
51
52#include <sys/promif.h>
53#include <sys/promimpl.h>
54
55int
56prom_getproplen(phandle_t nodeid, caddr_t name)
57{
58 cell_t ci[6];
59
60 ci[0] = p1275_ptr2cell("getproplen"); /* Service name */
61 ci[1] = (cell_t)2; /* #argument cells */
62 ci[2] = (cell_t)1; /* #return cells */
63 ci[3] = p1275_phandle2cell(nodeid); /* Arg1: package */
64 ci[4] = p1275_ptr2cell(name); /* Arg2: Property name */
65 ci[5] = (cell_t)-1; /* Res1: Prime result */
66
67 (void) p1275_cif_handler(&ci);
68
69 return (p1275_cell2int(ci[5])); /* Res1: Property length */
70}
71
72int
73prom_getprop(phandle_t nodeid, caddr_t name, caddr_t buf)
74{
75 int32_t len, rv;
76 cell_t ci[8];
77
78 /*
79 * This function assumes the buffer is large enough to
80 * hold the result, so we pass in the length of the
81 * property as the length of the buffer.
82 *
83 * Note that we ignore the "length" result of the service.
84 */
85
86 if ((len = prom_getproplen(nodeid, name)) <= 0)
87 return (len);
88
89 ci[0] = p1275_ptr2cell("getprop"); /* Service name */
90 ci[1] = (cell_t)4; /* #argument cells */
91 ci[2] = (cell_t)0; /* #result cells */
92 ci[3] = p1275_phandle2cell(nodeid); /* Arg1: package */
93 ci[4] = p1275_ptr2cell(name); /* Arg2: property name */
94 ci[5] = p1275_ptr2cell(buf); /* Arg3: buffer address */
95 ci[6] = len; /* Arg4: buf len (assumed) */
96
97 rv = p1275_cif_handler(&ci);
98
99 if (rv != 0)
100 return (-1);
101 return (len); /* Return known length */
102}
103
104int
105prom_bounded_getprop(phandle_t nodeid, caddr_t name, caddr_t buf,
106 int32_t buflen)
107{
108 cell_t ci[8];
109
110 ci[0] = p1275_ptr2cell("getprop"); /* Service name */
111 ci[1] = (cell_t)4; /* #argument cells */
112 ci[2] = (cell_t)1; /* #result cells */
113 ci[3] = p1275_phandle2cell(nodeid); /* Arg1: package */
114 ci[4] = p1275_ptr2cell(name); /* Arg2: property name */
115 ci[5] = p1275_ptr2cell(buf); /* Arg3: buffer address */
116 ci[6] = p1275_int2cell(buflen); /* Arg4: buffer length */
117 ci[7] = (cell_t)-1; /* Res1: Prime result */
118
119 (void) p1275_cif_handler(&ci);
120
121 return (p1275_cell2int(ci[7])); /* Res1: Returned length */
122}
123
124int
125prom_nextprop(phandle_t nodeid, caddr_t previous, caddr_t next)
126{
127 cell_t ci[7];
128
129 (void) prom_strcpy(next, ""); /* Prime result, in case call fails */
130
131 ci[0] = p1275_ptr2cell("nextprop"); /* Service name */
132 ci[1] = (cell_t)3; /* #argument cells */
133 ci[2] = (cell_t)1; /* #result cells */
134 ci[3] = p1275_phandle2cell(nodeid); /* Arg1: phandle */
135 ci[4] = p1275_ptr2cell(previous); /* Arg2: addr of prev name */
136 ci[5] = p1275_ptr2cell(next); /* Arg3: addr of 32 byte buf */
137 ci[6] = -1;
138
139 (void) p1275_cif_handler(&ci);
140
141 return (p1275_cell2int(ci[6]));
142}
143
144int
145prom_setprop(phandle_t nodeid, caddr_t name, caddr_t buf, int32_t buflen)
146{
147 cell_t ci[8];
148
149 ci[0] = p1275_ptr2cell("setprop"); /* Service name */
150 ci[1] = (cell_t)4; /* #argument cells */
151 ci[2] = (cell_t)1; /* #result cells */
152 ci[3] = p1275_phandle2cell(nodeid); /* Arg1: phandle */
153 ci[4] = p1275_ptr2cell(name); /* Arg2: property name */
154 ci[5] = p1275_ptr2cell(buf); /* Arg3: New value ptr */
155 ci[6] = p1275_int2cell(buflen); /* Arg4: New value len */
156 ci[7] = (cell_t)-1; /* Res1: Prime result */
157
158 (void) p1275_cif_handler(&ci);
159
160 return (p1275_cell2int(ci[7])); /* Res1: Actual new size */
161}
162
163/*
164 * prom_decode_composite_string:
165 *
166 * Returns successive strings in a composite string property.
167 * A composite string property is a buffer containing one or more
168 * NULL terminated strings contained within the length of the buffer.
169 *
170 * Always call with the base address and length of the property buffer.
171 * On the first call, call with prev == 0, call successively
172 * with prev set to the last value returned from this function
173 * until the routine returns zero which means no more string values.
174 */
175char *
176prom_decode_composite_string(void *buf, size_t buflen, char *prev)
177{
178 if ((buf == 0) || (buflen == 0) || ((int)buflen == -1))
179 return ((char *)0);
180
181 if (prev == 0)
182 return ((char *)buf);
183
184 prev += prom_strlen(prev) + 1;
185 if (prev >= ((char *)buf + buflen))
186 return ((char *)0);
187 return (prev);
188}