2N/A/*
2N/A *
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <errno.h>
2N/A#include <ctype.h>
2N/A#include <alloca.h>
2N/A#include <limits.h>
2N/A#include <fm/topo_mod.h>
2N/A#include <sys/param.h>
2N/A#include <sys/systeminfo.h>
2N/A#include <sys/fm/protocol.h>
2N/A#include <sys/stat.h>
2N/A
2N/A#include <topo_method.h>
2N/A#include <topo_subr.h>
2N/A#include <legacy_hc.h>
2N/A
2N/Astatic int legacy_hc_enum(topo_mod_t *, tnode_t *, const char *,
2N/A topo_instance_t, topo_instance_t, void *, void *);
2N/Astatic void legacy_hc_release(topo_mod_t *, tnode_t *);
2N/Astatic int legacy_hc_fmri_nvl2str(topo_mod_t *, tnode_t *, topo_version_t,
2N/A nvlist_t *, nvlist_t **);
2N/A
2N/Aconst topo_method_t legacy_hc_methods[] = {
2N/A { TOPO_METH_NVL2STR, TOPO_METH_NVL2STR_DESC, TOPO_METH_NVL2STR_VERSION,
2N/A TOPO_STABILITY_INTERNAL, legacy_hc_fmri_nvl2str },
2N/A { NULL }
2N/A};
2N/A
2N/Astatic const topo_modops_t legacy_hc_ops =
2N/A { legacy_hc_enum, legacy_hc_release };
2N/Astatic const topo_modinfo_t legacy_hc_info =
2N/A { LEGACY_HC, FM_FMRI_SCHEME_LEGACY, LEGACY_HC_VERSION, &legacy_hc_ops };
2N/A
2N/Aint
2N/Alegacy_hc_init(topo_mod_t *mod, topo_version_t version)
2N/A{
2N/A /*
2N/A * Turn on module debugging output
2N/A */
2N/A if (getenv("TOPOLEGACY_HCDEBUG"))
2N/A topo_mod_setdebug(mod);
2N/A
2N/A topo_mod_dprintf(mod, "initializing legacy_hc builtin\n");
2N/A
2N/A if (version != LEGACY_HC_VERSION)
2N/A return (topo_mod_seterrno(mod, EMOD_VER_NEW));
2N/A
2N/A if (topo_mod_register(mod, &legacy_hc_info, TOPO_VERSION) != 0) {
2N/A topo_mod_dprintf(mod, "failed to register legacy_hc: "
2N/A "%s\n", topo_mod_errmsg(mod));
2N/A return (-1); /* mod errno already set */
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Avoid
2N/Alegacy_hc_fini(topo_mod_t *mod)
2N/A{
2N/A topo_mod_unregister(mod);
2N/A}
2N/A
2N/A
2N/A/*ARGSUSED*/
2N/Aint
2N/Alegacy_hc_enum(topo_mod_t *mod, tnode_t *pnode, const char *name,
2N/A topo_instance_t min, topo_instance_t max, void *notused1, void *notused2)
2N/A{
2N/A (void) topo_method_register(mod, pnode, legacy_hc_methods);
2N/A return (0);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic void
2N/Alegacy_hc_release(topo_mod_t *mp, tnode_t *node)
2N/A{
2N/A topo_method_unregister_all(mp, node);
2N/A}
2N/A
2N/A/*
2N/A * Convert an input string to a URI escaped string and return the new string.
2N/A * RFC2396 Section 2.4 says that data must be escaped if it does not have a
2N/A * representation using an unreserved character, where an unreserved character
2N/A * is one that is either alphanumeric or one of the marks defined in S2.3.
2N/A */
2N/Astatic size_t
2N/Amem_fmri_uriescape(const char *s, const char *xmark, char *buf, size_t len)
2N/A{
2N/A static const char rfc2396_mark[] = "-_.!~*'()";
2N/A static const char hex_digits[] = "0123456789ABCDEF";
2N/A static const char empty_str[] = "";
2N/A
2N/A const char *p;
2N/A char c, *q;
2N/A size_t n = 0;
2N/A
2N/A if (s == NULL)
2N/A s = empty_str;
2N/A
2N/A if (xmark == NULL)
2N/A xmark = empty_str;
2N/A
2N/A for (p = s; (c = *p) != '\0'; p++) {
2N/A if (isalnum(c) || strchr(rfc2396_mark, c) || strchr(xmark, c))
2N/A n++; /* represent c as itself */
2N/A else
2N/A n += 3; /* represent c as escape */
2N/A }
2N/A
2N/A if (buf == NULL)
2N/A return (n);
2N/A
2N/A for (p = s, q = buf; (c = *p) != '\0' && q < buf + len; p++) {
2N/A if (isalnum(c) || strchr(rfc2396_mark, c) || strchr(xmark, c)) {
2N/A *q++ = c;
2N/A } else {
2N/A *q++ = '%';
2N/A *q++ = hex_digits[((uchar_t)c & 0xf0) >> 4];
2N/A *q++ = hex_digits[(uchar_t)c & 0xf];
2N/A }
2N/A }
2N/A
2N/A if (q == buf + len)
2N/A q--; /* len is too small: truncate output string */
2N/A
2N/A *q = '\0';
2N/A return (n);
2N/A}
2N/A
2N/Astatic ssize_t
2N/Afmri_nvl2str(topo_mod_t *mod, nvlist_t *nvl, char *buf, size_t buflen)
2N/A{
2N/A uint8_t version;
2N/A ssize_t size;
2N/A char *c;
2N/A char *escc;
2N/A int i;
2N/A
2N/A if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
2N/A version > FM_LEGACY_SCHEME_VERSION ||
2N/A nvlist_lookup_string(nvl, FM_FMRI_LEGACY_HC, &c) != 0)
2N/A return (0);
2N/A
2N/A i = mem_fmri_uriescape(c, ":,/", NULL, 0);
2N/A escc = topo_mod_alloc(mod, i + 1);
2N/A (void) mem_fmri_uriescape(c, ":,/", escc, i + 1);
2N/A size = snprintf(buf, buflen, "legacy-hc:///component=%s", escc);
2N/A topo_mod_free(mod, escc, i + 1);
2N/A
2N/A return (size);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Alegacy_hc_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version,
2N/A nvlist_t *nvl, nvlist_t **out)
2N/A{
2N/A ssize_t len;
2N/A char *name = NULL;
2N/A nvlist_t *fmristr;
2N/A
2N/A if (version > TOPO_METH_NVL2STR_VERSION)
2N/A return (topo_mod_seterrno(mod, EMOD_VER_NEW));
2N/A
2N/A if ((len = fmri_nvl2str(mod, nvl, NULL, 0)) == 0 ||
2N/A (name = topo_mod_alloc(mod, len + 1)) == NULL ||
2N/A fmri_nvl2str(mod, nvl, name, len + 1) == 0) {
2N/A if (name != NULL)
2N/A topo_mod_free(mod, name, len + 1);
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
2N/A }
2N/A
2N/A if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0) {
2N/A topo_mod_free(mod, name, len + 1);
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
2N/A }
2N/A if (nvlist_add_string(fmristr, "fmri-string", name) != 0) {
2N/A topo_mod_free(mod, name, len + 1);
2N/A nvlist_free(fmristr);
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
2N/A }
2N/A topo_mod_free(mod, name, len + 1);
2N/A *out = fmristr;
2N/A
2N/A return (0);
2N/A}