17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/stat.h>
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <fcntl.h>
307c478bd9Sstevel@tonic-gate #include <sys/openpromio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>			/* sprintf() */
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * opp_zalloc(): allocates and initializes a struct openpromio
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  *   input: size_t: the size of the variable-length part of the openpromio
407c478bd9Sstevel@tonic-gate  *          const char *: an initial value for oprom_array, if non-NULL
417c478bd9Sstevel@tonic-gate  *  output: struct openpromio: the allocated, initialized openpromio
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static struct openpromio *
opp_zalloc(size_t size,const char * prop)457c478bd9Sstevel@tonic-gate opp_zalloc(size_t size, const char *prop)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate 	struct openpromio *opp = malloc(sizeof (struct openpromio) + size);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	if (opp != NULL) {
507c478bd9Sstevel@tonic-gate 		(void) memset(opp, 0, sizeof (struct openpromio) + size);
517c478bd9Sstevel@tonic-gate 		opp->oprom_size = size;
527c478bd9Sstevel@tonic-gate 		if (prop != NULL)
537c478bd9Sstevel@tonic-gate 			(void) strcpy(opp->oprom_array, prop);
547c478bd9Sstevel@tonic-gate 	}
557c478bd9Sstevel@tonic-gate 	return (opp);
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * goto_rootnode(): moves to the root of the devinfo tree
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  *   input: int: an open descriptor to /dev/openprom
627c478bd9Sstevel@tonic-gate  *  output: int: nonzero on success
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static int
goto_rootnode(int prom_fd)667c478bd9Sstevel@tonic-gate goto_rootnode(int prom_fd)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	struct openpromio op = { sizeof (int), 0 };
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	/* zero it explicitly since a union is involved */
717c478bd9Sstevel@tonic-gate 	op.oprom_node = 0;
727c478bd9Sstevel@tonic-gate 	return (ioctl(prom_fd, OPROMNEXT, &op) == 0);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * return_property(): returns the value of a given property
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  *   input: int: an open descriptor to /dev/openprom
797c478bd9Sstevel@tonic-gate  *          const char *: the property to look for in the current devinfo node
807c478bd9Sstevel@tonic-gate  *  output: the value of that property (dynamically allocated)
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static char *
return_property(int prom_fd,const char * prop)847c478bd9Sstevel@tonic-gate return_property(int prom_fd, const char *prop)
857c478bd9Sstevel@tonic-gate {
86*d14c4fc9SJoshua M. Clulow 	int			proplen;
877c478bd9Sstevel@tonic-gate 	char			*result;
887c478bd9Sstevel@tonic-gate 	struct openpromio	*opp = opp_zalloc(strlen(prop) + 1, prop);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (opp == NULL)
917c478bd9Sstevel@tonic-gate 		return (NULL);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMGETPROPLEN, opp) == -1) {
947c478bd9Sstevel@tonic-gate 		free(opp);
957c478bd9Sstevel@tonic-gate 		return (NULL);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	proplen = opp->oprom_len;
997c478bd9Sstevel@tonic-gate 	if (proplen > (strlen(prop) + 1)) {
1007c478bd9Sstevel@tonic-gate 		free(opp);
1017c478bd9Sstevel@tonic-gate 		opp = opp_zalloc(proplen, prop);
1027c478bd9Sstevel@tonic-gate 		if (opp == NULL)
1037c478bd9Sstevel@tonic-gate 			return (NULL);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMGETPROP, opp) == -1) {
1077c478bd9Sstevel@tonic-gate 		free(opp);
1087c478bd9Sstevel@tonic-gate 		return (NULL);
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	result = strdup(opp->oprom_array);
1127c478bd9Sstevel@tonic-gate 	free(opp);
1137c478bd9Sstevel@tonic-gate 	return (result);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * sanitize_class_id(): translates the class id into a canonical format,
118bbf21555SRichard Lowe  *			so that it can be used easily with dhcptab(5).
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  *   input: char *: the class id to canonicalize
1217c478bd9Sstevel@tonic-gate  *  output: void
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static void
sanitize_class_id(char * src_ptr)1257c478bd9Sstevel@tonic-gate sanitize_class_id(char *src_ptr)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	char	*dst_ptr = src_ptr;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/* remove all spaces and change all commas to periods */
1307c478bd9Sstevel@tonic-gate 	while (*src_ptr != '\0') {
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		switch (*src_ptr) {
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 		case ' ':
1357c478bd9Sstevel@tonic-gate 			break;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		case ',':
1387c478bd9Sstevel@tonic-gate 			*dst_ptr++ = '.';
1397c478bd9Sstevel@tonic-gate 			break;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		default:
1427c478bd9Sstevel@tonic-gate 			*dst_ptr++ = *src_ptr;
1437c478bd9Sstevel@tonic-gate 			break;
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 		src_ptr++;
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	*dst_ptr = '\0';
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * get_class_id(): retrieves the class id from the prom, then canonicalizes it
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  *   input: void
1547c478bd9Sstevel@tonic-gate  *  output: char *: the class id (dynamically allocated and sanitized)
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate char *
get_class_id(void)1587c478bd9Sstevel@tonic-gate get_class_id(void)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	int	prom_fd;
1617c478bd9Sstevel@tonic-gate 	char    *name, *class_id = NULL;
1627c478bd9Sstevel@tonic-gate 	size_t	len;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	prom_fd = open("/dev/openprom", O_RDONLY);
1657c478bd9Sstevel@tonic-gate 	if (prom_fd == -1)
1667c478bd9Sstevel@tonic-gate 		return (NULL);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (goto_rootnode(prom_fd) == 0) {
1697c478bd9Sstevel@tonic-gate 		(void) close(prom_fd);
1707c478bd9Sstevel@tonic-gate 		return (NULL);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * the `name' property is the same as the result of `uname -i', modulo
1757c478bd9Sstevel@tonic-gate 	 * some stylistic issues we fix up via sanitize_class_id() below.
1767c478bd9Sstevel@tonic-gate 	 */
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	name = return_property(prom_fd, "name");
1797c478bd9Sstevel@tonic-gate 	(void) close(prom_fd);
1807c478bd9Sstevel@tonic-gate 	if (name == NULL)
1817c478bd9Sstevel@tonic-gate 		return (NULL);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * if the name is not prefixed with a vendor name, add "SUNW," to make
1857c478bd9Sstevel@tonic-gate 	 * it more likely to be globally unique; see PSARC/2004/674.
1867c478bd9Sstevel@tonic-gate 	 */
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (strchr(name, ',') == NULL) {
1897c478bd9Sstevel@tonic-gate 		len = strlen(name) + sizeof ("SUNW,");
1907c478bd9Sstevel@tonic-gate 		class_id = malloc(len);
1917c478bd9Sstevel@tonic-gate 		if (class_id == NULL) {
1927c478bd9Sstevel@tonic-gate 			free(name);
1937c478bd9Sstevel@tonic-gate 			return (NULL);
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 		(void) snprintf(class_id, len, "SUNW,%s", name);
1967c478bd9Sstevel@tonic-gate 		free(name);
1977c478bd9Sstevel@tonic-gate 	} else {
1987c478bd9Sstevel@tonic-gate 		class_id = name;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	sanitize_class_id(class_id);
2027c478bd9Sstevel@tonic-gate 	return (class_id);
2037c478bd9Sstevel@tonic-gate }
204