1*b7d3956bSstephh /*
2*b7d3956bSstephh  *
3*b7d3956bSstephh  * CDDL HEADER START
4*b7d3956bSstephh  *
5*b7d3956bSstephh  * The contents of this file are subject to the terms of the
6*b7d3956bSstephh  * Common Development and Distribution License (the "License").
7*b7d3956bSstephh  * You may not use this file except in compliance with the License.
8*b7d3956bSstephh  *
9*b7d3956bSstephh  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*b7d3956bSstephh  * or http://www.opensolaris.org/os/licensing.
11*b7d3956bSstephh  * See the License for the specific language governing permissions
12*b7d3956bSstephh  * and limitations under the License.
13*b7d3956bSstephh  *
14*b7d3956bSstephh  * When distributing Covered Code, include this CDDL HEADER in each
15*b7d3956bSstephh  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*b7d3956bSstephh  * If applicable, add the following below this CDDL HEADER, with the
17*b7d3956bSstephh  * fields enclosed by brackets "[]" replaced with your own identifying
18*b7d3956bSstephh  * information: Portions Copyright [yyyy] [name of copyright owner]
19*b7d3956bSstephh  *
20*b7d3956bSstephh  * CDDL HEADER END
21*b7d3956bSstephh  */
22*b7d3956bSstephh 
23*b7d3956bSstephh /*
24*b7d3956bSstephh  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25*b7d3956bSstephh  * Use is subject to license terms.
26*b7d3956bSstephh  */
27*b7d3956bSstephh 
28*b7d3956bSstephh #include <stdio.h>
29*b7d3956bSstephh #include <stdlib.h>
30*b7d3956bSstephh #include <string.h>
31*b7d3956bSstephh #include <errno.h>
32*b7d3956bSstephh #include <ctype.h>
33*b7d3956bSstephh #include <alloca.h>
34*b7d3956bSstephh #include <limits.h>
35*b7d3956bSstephh #include <fm/topo_mod.h>
36*b7d3956bSstephh #include <sys/param.h>
37*b7d3956bSstephh #include <sys/systeminfo.h>
38*b7d3956bSstephh #include <sys/fm/protocol.h>
39*b7d3956bSstephh #include <sys/stat.h>
40*b7d3956bSstephh 
41*b7d3956bSstephh #include <topo_method.h>
42*b7d3956bSstephh #include <topo_subr.h>
43*b7d3956bSstephh #include <legacy_hc.h>
44*b7d3956bSstephh 
45*b7d3956bSstephh static int legacy_hc_enum(topo_mod_t *, tnode_t *, const char *,
46*b7d3956bSstephh     topo_instance_t, topo_instance_t, void *, void *);
47*b7d3956bSstephh static void legacy_hc_release(topo_mod_t *, tnode_t *);
48*b7d3956bSstephh static int legacy_hc_fmri_nvl2str(topo_mod_t *, tnode_t *, topo_version_t,
49*b7d3956bSstephh     nvlist_t *, nvlist_t **);
50*b7d3956bSstephh 
51*b7d3956bSstephh const topo_method_t legacy_hc_methods[] = {
52*b7d3956bSstephh 	{ TOPO_METH_NVL2STR, TOPO_METH_NVL2STR_DESC, TOPO_METH_NVL2STR_VERSION,
53*b7d3956bSstephh 	    TOPO_STABILITY_INTERNAL, legacy_hc_fmri_nvl2str },
54*b7d3956bSstephh 	{ NULL }
55*b7d3956bSstephh };
56*b7d3956bSstephh 
57*b7d3956bSstephh static const topo_modops_t legacy_hc_ops =
58*b7d3956bSstephh 	{ legacy_hc_enum, legacy_hc_release };
59*b7d3956bSstephh static const topo_modinfo_t legacy_hc_info =
60*b7d3956bSstephh 	{ LEGACY_HC, FM_FMRI_SCHEME_LEGACY, LEGACY_HC_VERSION, &legacy_hc_ops };
61*b7d3956bSstephh 
62*b7d3956bSstephh int
legacy_hc_init(topo_mod_t * mod,topo_version_t version)63*b7d3956bSstephh legacy_hc_init(topo_mod_t *mod, topo_version_t version)
64*b7d3956bSstephh {
65*b7d3956bSstephh 	/*
66*b7d3956bSstephh 	 * Turn on module debugging output
67*b7d3956bSstephh 	 */
68*b7d3956bSstephh 	if (getenv("TOPOLEGACY_HCDEBUG"))
69*b7d3956bSstephh 		topo_mod_setdebug(mod);
70*b7d3956bSstephh 
71*b7d3956bSstephh 	topo_mod_dprintf(mod, "initializing legacy_hc builtin\n");
72*b7d3956bSstephh 
73*b7d3956bSstephh 	if (version != LEGACY_HC_VERSION)
74*b7d3956bSstephh 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
75*b7d3956bSstephh 
76*b7d3956bSstephh 	if (topo_mod_register(mod, &legacy_hc_info, TOPO_VERSION) != 0) {
77*b7d3956bSstephh 		topo_mod_dprintf(mod, "failed to register legacy_hc: "
78*b7d3956bSstephh 		    "%s\n", topo_mod_errmsg(mod));
79*b7d3956bSstephh 		return (-1); /* mod errno already set */
80*b7d3956bSstephh 	}
81*b7d3956bSstephh 
82*b7d3956bSstephh 	return (0);
83*b7d3956bSstephh }
84*b7d3956bSstephh 
85*b7d3956bSstephh void
legacy_hc_fini(topo_mod_t * mod)86*b7d3956bSstephh legacy_hc_fini(topo_mod_t *mod)
87*b7d3956bSstephh {
88*b7d3956bSstephh 	topo_mod_unregister(mod);
89*b7d3956bSstephh }
90*b7d3956bSstephh 
91*b7d3956bSstephh 
92*b7d3956bSstephh /*ARGSUSED*/
93*b7d3956bSstephh int
legacy_hc_enum(topo_mod_t * mod,tnode_t * pnode,const char * name,topo_instance_t min,topo_instance_t max,void * notused1,void * notused2)94*b7d3956bSstephh legacy_hc_enum(topo_mod_t *mod, tnode_t *pnode, const char *name,
95*b7d3956bSstephh     topo_instance_t min, topo_instance_t max, void *notused1, void *notused2)
96*b7d3956bSstephh {
97*b7d3956bSstephh 	(void) topo_method_register(mod, pnode, legacy_hc_methods);
98*b7d3956bSstephh 	return (0);
99*b7d3956bSstephh }
100*b7d3956bSstephh 
101*b7d3956bSstephh /*ARGSUSED*/
102*b7d3956bSstephh static void
legacy_hc_release(topo_mod_t * mp,tnode_t * node)103*b7d3956bSstephh legacy_hc_release(topo_mod_t *mp, tnode_t *node)
104*b7d3956bSstephh {
105*b7d3956bSstephh 	topo_method_unregister_all(mp, node);
106*b7d3956bSstephh }
107*b7d3956bSstephh 
108*b7d3956bSstephh /*
109*b7d3956bSstephh  * Convert an input string to a URI escaped string and return the new string.
110*b7d3956bSstephh  * RFC2396 Section 2.4 says that data must be escaped if it does not have a
111*b7d3956bSstephh  * representation using an unreserved character, where an unreserved character
112*b7d3956bSstephh  * is one that is either alphanumeric or one of the marks defined in S2.3.
113*b7d3956bSstephh  */
114*b7d3956bSstephh static size_t
mem_fmri_uriescape(const char * s,const char * xmark,char * buf,size_t len)115*b7d3956bSstephh mem_fmri_uriescape(const char *s, const char *xmark, char *buf, size_t len)
116*b7d3956bSstephh {
117*b7d3956bSstephh 	static const char rfc2396_mark[] = "-_.!~*'()";
118*b7d3956bSstephh 	static const char hex_digits[] = "0123456789ABCDEF";
119*b7d3956bSstephh 	static const char empty_str[] = "";
120*b7d3956bSstephh 
121*b7d3956bSstephh 	const char *p;
122*b7d3956bSstephh 	char c, *q;
123*b7d3956bSstephh 	size_t n = 0;
124*b7d3956bSstephh 
125*b7d3956bSstephh 	if (s == NULL)
126*b7d3956bSstephh 		s = empty_str;
127*b7d3956bSstephh 
128*b7d3956bSstephh 	if (xmark == NULL)
129*b7d3956bSstephh 		xmark = empty_str;
130*b7d3956bSstephh 
131*b7d3956bSstephh 	for (p = s; (c = *p) != '\0'; p++) {
132*b7d3956bSstephh 		if (isalnum(c) || strchr(rfc2396_mark, c) || strchr(xmark, c))
133*b7d3956bSstephh 			n++;    /* represent c as itself */
134*b7d3956bSstephh 		else
135*b7d3956bSstephh 			n += 3; /* represent c as escape */
136*b7d3956bSstephh 	}
137*b7d3956bSstephh 
138*b7d3956bSstephh 	if (buf == NULL)
139*b7d3956bSstephh 		return (n);
140*b7d3956bSstephh 
141*b7d3956bSstephh 	for (p = s, q = buf; (c = *p) != '\0' && q < buf + len; p++) {
142*b7d3956bSstephh 		if (isalnum(c) || strchr(rfc2396_mark, c) || strchr(xmark, c)) {
143*b7d3956bSstephh 			*q++ = c;
144*b7d3956bSstephh 		} else {
145*b7d3956bSstephh 			*q++ = '%';
146*b7d3956bSstephh 			*q++ = hex_digits[((uchar_t)c & 0xf0) >> 4];
147*b7d3956bSstephh 			*q++ = hex_digits[(uchar_t)c & 0xf];
148*b7d3956bSstephh 		}
149*b7d3956bSstephh 	}
150*b7d3956bSstephh 
151*b7d3956bSstephh 	if (q == buf + len)
152*b7d3956bSstephh 		q--; /* len is too small: truncate output string */
153*b7d3956bSstephh 
154*b7d3956bSstephh 	*q = '\0';
155*b7d3956bSstephh 	return (n);
156*b7d3956bSstephh }
157*b7d3956bSstephh 
158*b7d3956bSstephh static ssize_t
fmri_nvl2str(topo_mod_t * mod,nvlist_t * nvl,char * buf,size_t buflen)159*b7d3956bSstephh fmri_nvl2str(topo_mod_t *mod, nvlist_t *nvl, char *buf, size_t buflen)
160*b7d3956bSstephh {
161*b7d3956bSstephh 	uint8_t version;
162*b7d3956bSstephh 	ssize_t size;
163*b7d3956bSstephh 	char *c;
164*b7d3956bSstephh 	char *escc;
165*b7d3956bSstephh 	int i;
166*b7d3956bSstephh 
167*b7d3956bSstephh 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
168*b7d3956bSstephh 	    version > FM_LEGACY_SCHEME_VERSION ||
169*b7d3956bSstephh 	    nvlist_lookup_string(nvl, FM_FMRI_LEGACY_HC, &c) != 0)
170*b7d3956bSstephh 		return (0);
171*b7d3956bSstephh 
172*b7d3956bSstephh 	i = mem_fmri_uriescape(c, ":,/", NULL, 0);
173*b7d3956bSstephh 	escc = topo_mod_alloc(mod, i + 1);
174*b7d3956bSstephh 	(void) mem_fmri_uriescape(c, ":,/", escc, i + 1);
175*b7d3956bSstephh 	size = snprintf(buf, buflen, "legacy-hc:///component=%s", escc);
176*b7d3956bSstephh 	topo_mod_free(mod, escc, i + 1);
177*b7d3956bSstephh 
178*b7d3956bSstephh 	return (size);
179*b7d3956bSstephh }
180*b7d3956bSstephh 
181*b7d3956bSstephh /*ARGSUSED*/
182*b7d3956bSstephh static int
legacy_hc_fmri_nvl2str(topo_mod_t * mod,tnode_t * node,topo_version_t version,nvlist_t * nvl,nvlist_t ** out)183*b7d3956bSstephh legacy_hc_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version,
184*b7d3956bSstephh     nvlist_t *nvl, nvlist_t **out)
185*b7d3956bSstephh {
186*b7d3956bSstephh 	ssize_t len;
187*b7d3956bSstephh 	char *name = NULL;
188*b7d3956bSstephh 	nvlist_t *fmristr;
189*b7d3956bSstephh 
190*b7d3956bSstephh 	if (version > TOPO_METH_NVL2STR_VERSION)
191*b7d3956bSstephh 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
192*b7d3956bSstephh 
193*b7d3956bSstephh 	if ((len = fmri_nvl2str(mod, nvl, NULL, 0)) == 0 ||
194*b7d3956bSstephh 	    (name = topo_mod_alloc(mod, len + 1)) == NULL ||
195*b7d3956bSstephh 	    fmri_nvl2str(mod, nvl, name, len + 1) == 0) {
196*b7d3956bSstephh 		if (name != NULL)
197*b7d3956bSstephh 			topo_mod_free(mod, name, len + 1);
198*b7d3956bSstephh 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
199*b7d3956bSstephh 	}
200*b7d3956bSstephh 
201*b7d3956bSstephh 	if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0) {
202*b7d3956bSstephh 		topo_mod_free(mod, name, len + 1);
203*b7d3956bSstephh 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
204*b7d3956bSstephh 	}
205*b7d3956bSstephh 	if (nvlist_add_string(fmristr, "fmri-string", name) != 0) {
206*b7d3956bSstephh 		topo_mod_free(mod, name, len + 1);
207*b7d3956bSstephh 		nvlist_free(fmristr);
208*b7d3956bSstephh 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
209*b7d3956bSstephh 	}
210*b7d3956bSstephh 	topo_mod_free(mod, name, len + 1);
211*b7d3956bSstephh 	*out = fmristr;
212*b7d3956bSstephh 
213*b7d3956bSstephh 	return (0);
214*b7d3956bSstephh }
215