1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <sys/stat.h>
30*0Sstevel@tonic-gate #include <sys/types.h>
31*0Sstevel@tonic-gate #include <fcntl.h>
32*0Sstevel@tonic-gate #include <sys/openpromio.h>
33*0Sstevel@tonic-gate #include <stdlib.h>
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate #include <stdio.h> /* sprintf() */
36*0Sstevel@tonic-gate #include <unistd.h>
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate * opp_zalloc(): allocates and initializes a struct openpromio
40*0Sstevel@tonic-gate *
41*0Sstevel@tonic-gate * input: size_t: the size of the variable-length part of the openpromio
42*0Sstevel@tonic-gate * const char *: an initial value for oprom_array, if non-NULL
43*0Sstevel@tonic-gate * output: struct openpromio: the allocated, initialized openpromio
44*0Sstevel@tonic-gate */
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate static struct openpromio *
opp_zalloc(size_t size,const char * prop)47*0Sstevel@tonic-gate opp_zalloc(size_t size, const char *prop)
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate struct openpromio *opp = malloc(sizeof (struct openpromio) + size);
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate if (opp != NULL) {
52*0Sstevel@tonic-gate (void) memset(opp, 0, sizeof (struct openpromio) + size);
53*0Sstevel@tonic-gate opp->oprom_size = size;
54*0Sstevel@tonic-gate if (prop != NULL)
55*0Sstevel@tonic-gate (void) strcpy(opp->oprom_array, prop);
56*0Sstevel@tonic-gate }
57*0Sstevel@tonic-gate return (opp);
58*0Sstevel@tonic-gate }
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate * goto_rootnode(): moves to the root of the devinfo tree
62*0Sstevel@tonic-gate *
63*0Sstevel@tonic-gate * input: int: an open descriptor to /dev/openprom
64*0Sstevel@tonic-gate * output: int: nonzero on success
65*0Sstevel@tonic-gate */
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate static int
goto_rootnode(int prom_fd)68*0Sstevel@tonic-gate goto_rootnode(int prom_fd)
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate struct openpromio op = { sizeof (int), 0 };
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate /* zero it explicitly since a union is involved */
73*0Sstevel@tonic-gate op.oprom_node = 0;
74*0Sstevel@tonic-gate return (ioctl(prom_fd, OPROMNEXT, &op) == 0);
75*0Sstevel@tonic-gate }
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate /*
78*0Sstevel@tonic-gate * return_property(): returns the value of a given property
79*0Sstevel@tonic-gate *
80*0Sstevel@tonic-gate * input: int: an open descriptor to /dev/openprom
81*0Sstevel@tonic-gate * const char *: the property to look for in the current devinfo node
82*0Sstevel@tonic-gate * output: the value of that property (dynamically allocated)
83*0Sstevel@tonic-gate */
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate static char *
return_property(int prom_fd,const char * prop)86*0Sstevel@tonic-gate return_property(int prom_fd, const char *prop)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate int proplen;
89*0Sstevel@tonic-gate char *result;
90*0Sstevel@tonic-gate struct openpromio *opp = opp_zalloc(strlen(prop) + 1, prop);
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate if (opp == NULL)
93*0Sstevel@tonic-gate return (NULL);
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate if (ioctl(prom_fd, OPROMGETPROPLEN, opp) == -1) {
96*0Sstevel@tonic-gate free(opp);
97*0Sstevel@tonic-gate return (NULL);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate proplen = opp->oprom_len;
101*0Sstevel@tonic-gate if (proplen > (strlen(prop) + 1)) {
102*0Sstevel@tonic-gate free(opp);
103*0Sstevel@tonic-gate opp = opp_zalloc(proplen, prop);
104*0Sstevel@tonic-gate if (opp == NULL)
105*0Sstevel@tonic-gate return (NULL);
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate if (ioctl(prom_fd, OPROMGETPROP, opp) == -1) {
109*0Sstevel@tonic-gate free(opp);
110*0Sstevel@tonic-gate return (NULL);
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate result = strdup(opp->oprom_array);
114*0Sstevel@tonic-gate free(opp);
115*0Sstevel@tonic-gate return (result);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate /*
119*0Sstevel@tonic-gate * sanitize_class_id(): translates the class id into a canonical format,
120*0Sstevel@tonic-gate * so that it can be used easily with dhcptab(4).
121*0Sstevel@tonic-gate *
122*0Sstevel@tonic-gate * input: char *: the class id to canonicalize
123*0Sstevel@tonic-gate * output: void
124*0Sstevel@tonic-gate */
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate static void
sanitize_class_id(char * src_ptr)127*0Sstevel@tonic-gate sanitize_class_id(char *src_ptr)
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate char *dst_ptr = src_ptr;
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate /* remove all spaces and change all commas to periods */
132*0Sstevel@tonic-gate while (*src_ptr != '\0') {
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate switch (*src_ptr) {
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate case ' ':
137*0Sstevel@tonic-gate break;
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate case ',':
140*0Sstevel@tonic-gate *dst_ptr++ = '.';
141*0Sstevel@tonic-gate break;
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate default:
144*0Sstevel@tonic-gate *dst_ptr++ = *src_ptr;
145*0Sstevel@tonic-gate break;
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate src_ptr++;
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate *dst_ptr = '\0';
150*0Sstevel@tonic-gate }
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate /*
153*0Sstevel@tonic-gate * get_class_id(): retrieves the class id from the prom, then canonicalizes it
154*0Sstevel@tonic-gate *
155*0Sstevel@tonic-gate * input: void
156*0Sstevel@tonic-gate * output: char *: the class id (dynamically allocated and sanitized)
157*0Sstevel@tonic-gate */
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate char *
get_class_id(void)160*0Sstevel@tonic-gate get_class_id(void)
161*0Sstevel@tonic-gate {
162*0Sstevel@tonic-gate int prom_fd;
163*0Sstevel@tonic-gate char *name, *class_id = NULL;
164*0Sstevel@tonic-gate size_t len;
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gate prom_fd = open("/dev/openprom", O_RDONLY);
167*0Sstevel@tonic-gate if (prom_fd == -1)
168*0Sstevel@tonic-gate return (NULL);
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate if (goto_rootnode(prom_fd) == 0) {
171*0Sstevel@tonic-gate (void) close(prom_fd);
172*0Sstevel@tonic-gate return (NULL);
173*0Sstevel@tonic-gate }
174*0Sstevel@tonic-gate
175*0Sstevel@tonic-gate /*
176*0Sstevel@tonic-gate * the `name' property is the same as the result of `uname -i', modulo
177*0Sstevel@tonic-gate * some stylistic issues we fix up via sanitize_class_id() below.
178*0Sstevel@tonic-gate */
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate name = return_property(prom_fd, "name");
181*0Sstevel@tonic-gate (void) close(prom_fd);
182*0Sstevel@tonic-gate if (name == NULL)
183*0Sstevel@tonic-gate return (NULL);
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate /*
186*0Sstevel@tonic-gate * if the name is not prefixed with a vendor name, add "SUNW," to make
187*0Sstevel@tonic-gate * it more likely to be globally unique; see PSARC/2004/674.
188*0Sstevel@tonic-gate */
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate if (strchr(name, ',') == NULL) {
191*0Sstevel@tonic-gate len = strlen(name) + sizeof ("SUNW,");
192*0Sstevel@tonic-gate class_id = malloc(len);
193*0Sstevel@tonic-gate if (class_id == NULL) {
194*0Sstevel@tonic-gate free(name);
195*0Sstevel@tonic-gate return (NULL);
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate (void) snprintf(class_id, len, "SUNW,%s", name);
198*0Sstevel@tonic-gate free(name);
199*0Sstevel@tonic-gate } else {
200*0Sstevel@tonic-gate class_id = name;
201*0Sstevel@tonic-gate }
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate sanitize_class_id(class_id);
204*0Sstevel@tonic-gate return (class_id);
205*0Sstevel@tonic-gate }
206