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
5*3ebafc43Sjveta  * Common Development and Distribution License (the "License").
6*3ebafc43Sjveta  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*3ebafc43Sjveta  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * This file contains kernel property decode routines adopted from
287c478bd9Sstevel@tonic-gate  * sunddi.c and ddi_impl.c. The following changes have been applied.
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * (1) Replace kmem_alloc by malloc. Remove negative indexing
317c478bd9Sstevel@tonic-gate  * (2) Decoding applies only to prom properties.
327c478bd9Sstevel@tonic-gate  * (3) For strings, the return value is a composite string, not a string array.
337c478bd9Sstevel@tonic-gate  * (4) impl_ddi_prop_int_from_prom() uses _LITTLE_ENDIAN from isa_defs.h
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * XXX This file should be kept in sync with kernel property encoding.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <strings.h>
407c478bd9Sstevel@tonic-gate #include <synch.h>
417c478bd9Sstevel@tonic-gate #include <ctype.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/dditypes.h>
447c478bd9Sstevel@tonic-gate #include <sys/ddipropdefs.h>
457c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include "libdevinfo.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Return an integer in native machine format from an OBP 1275 integer
517c478bd9Sstevel@tonic-gate  * representation, which is big-endian, with no particular alignment
527c478bd9Sstevel@tonic-gate  * guarantees. intp points to the OBP data, and n the number of bytes.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * Byte-swapping may be needed on some implementations.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate int
impl_di_prop_int_from_prom(uchar_t * intp,int n)577c478bd9Sstevel@tonic-gate impl_di_prop_int_from_prom(uchar_t *intp, int n)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	int i = 0;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
627c478bd9Sstevel@tonic-gate 	intp += n;
637c478bd9Sstevel@tonic-gate 	while (n-- > 0) {
647c478bd9Sstevel@tonic-gate 		i = (i << 8) | *(--intp);
657c478bd9Sstevel@tonic-gate 	}
667c478bd9Sstevel@tonic-gate #else
677c478bd9Sstevel@tonic-gate 	while (n-- > 0) {
687c478bd9Sstevel@tonic-gate 		i = (i << 8) | *intp++;
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate #endif	/* defined(_LITTLE_ENDIAN) */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	return (i);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Reset the current location pointer in the property handle to the
777c478bd9Sstevel@tonic-gate  * beginning of the data.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate void
di_prop_reset_pos(prop_handle_t * ph)807c478bd9Sstevel@tonic-gate di_prop_reset_pos(prop_handle_t *ph)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	ph->ph_cur_pos = ph->ph_data;
837c478bd9Sstevel@tonic-gate 	ph->ph_save_pos = ph->ph_data;
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Restore the current location pointer in the property handle to the
887c478bd9Sstevel@tonic-gate  * saved position.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate void
di_prop_save_pos(prop_handle_t * ph)917c478bd9Sstevel@tonic-gate di_prop_save_pos(prop_handle_t *ph)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	ph->ph_save_pos = ph->ph_cur_pos;
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * Save the location that the current location poiner is pointing to..
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate void
di_prop_restore_pos(prop_handle_t * ph)1007c478bd9Sstevel@tonic-gate di_prop_restore_pos(prop_handle_t *ph)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	ph->ph_cur_pos = ph->ph_save_pos;
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Property encode/decode functions
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * Decode an array of integers property
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate static int
di_prop_fm_decode_ints(prop_handle_t * ph,void * data,uint_t * nelements)1137c478bd9Sstevel@tonic-gate di_prop_fm_decode_ints(prop_handle_t *ph, void *data, uint_t *nelements)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	int	i;
1167c478bd9Sstevel@tonic-gate 	int	cnt = 0;
1177c478bd9Sstevel@tonic-gate 	int	*tmp;
1187c478bd9Sstevel@tonic-gate 	int	*intp;
1197c478bd9Sstevel@tonic-gate 	int	n;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * Figure out how many array elements there are by going through the
1237c478bd9Sstevel@tonic-gate 	 * data without decoding it first and counting.
1247c478bd9Sstevel@tonic-gate 	 */
1257c478bd9Sstevel@tonic-gate 	for (;;) {
1267c478bd9Sstevel@tonic-gate 		i = DDI_PROP_INT(ph, DDI_PROP_CMD_SKIP, NULL);
1277c478bd9Sstevel@tonic-gate 		if (i < 0)
1287c478bd9Sstevel@tonic-gate 			break;
1297c478bd9Sstevel@tonic-gate 		cnt++;
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * If there are no elements return an error
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 	if (cnt == 0)
1367c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/*
1397c478bd9Sstevel@tonic-gate 	 * If we cannot skip through the data, we cannot decode it
1407c478bd9Sstevel@tonic-gate 	 */
1417c478bd9Sstevel@tonic-gate 	if (i == DDI_PROP_RESULT_ERROR)
1427c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_DECODE);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 * Reset the data pointer to the beginning of the encoded data
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	di_prop_reset_pos(ph);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * Allocated memory to store the decoded value in.
1517c478bd9Sstevel@tonic-gate 	 */
1527c478bd9Sstevel@tonic-gate 	if ((intp = malloc(cnt * sizeof (int))) == NULL) {
1537c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_DECODE);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	/*
1587c478bd9Sstevel@tonic-gate 	 * Decode each elemente and place it in the space we just allocated
1597c478bd9Sstevel@tonic-gate 	 */
1607c478bd9Sstevel@tonic-gate 	tmp = intp;
1617c478bd9Sstevel@tonic-gate 	for (n = 0; n < cnt; n++, tmp++) {
1627c478bd9Sstevel@tonic-gate 		i = DDI_PROP_INT(ph, DDI_PROP_CMD_DECODE, tmp);
1637c478bd9Sstevel@tonic-gate 		if (i < DDI_PROP_RESULT_OK) {
1647c478bd9Sstevel@tonic-gate 			/*
1657c478bd9Sstevel@tonic-gate 			 * Free the space we just allocated
1667c478bd9Sstevel@tonic-gate 			 * and return an error.
1677c478bd9Sstevel@tonic-gate 			 */
1687c478bd9Sstevel@tonic-gate 			free(intp);
1697c478bd9Sstevel@tonic-gate 			switch (i) {
1707c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
1717c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
1747c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_DECODE);
1757c478bd9Sstevel@tonic-gate 			}
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	*nelements = cnt;
1807c478bd9Sstevel@tonic-gate 	*(int **)data = intp;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * Decode an array of strings.
1877c478bd9Sstevel@tonic-gate  */
1887c478bd9Sstevel@tonic-gate static int
di_prop_fm_decode_strings(prop_handle_t * ph,void * data,uint_t * nelements)1897c478bd9Sstevel@tonic-gate di_prop_fm_decode_strings(prop_handle_t *ph, void *data, uint_t *nelements)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	int		cnt = 0;
1927c478bd9Sstevel@tonic-gate 	char		*strs;
1937c478bd9Sstevel@tonic-gate 	char		*tmp;
1947c478bd9Sstevel@tonic-gate 	int		size;
1957c478bd9Sstevel@tonic-gate 	int		i;
1967c478bd9Sstevel@tonic-gate 	int		n;
1977c478bd9Sstevel@tonic-gate 	int		nbytes;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/*
2007c478bd9Sstevel@tonic-gate 	 * Figure out how much memory we need for the sum total
2017c478bd9Sstevel@tonic-gate 	 */
2027c478bd9Sstevel@tonic-gate 	nbytes = 0;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	for (;;) {
2057c478bd9Sstevel@tonic-gate 		/*
2067c478bd9Sstevel@tonic-gate 		 * Get the decoded size of the current encoded string.
2077c478bd9Sstevel@tonic-gate 		 */
2087c478bd9Sstevel@tonic-gate 		size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_DSIZE, NULL);
2097c478bd9Sstevel@tonic-gate 		if (size < 0)
2107c478bd9Sstevel@tonic-gate 			break;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		cnt++;
2137c478bd9Sstevel@tonic-gate 		nbytes += size;
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	/*
2177c478bd9Sstevel@tonic-gate 	 * If there are no elements return an error
2187c478bd9Sstevel@tonic-gate 	 */
2197c478bd9Sstevel@tonic-gate 	if (cnt == 0)
2207c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * If we cannot skip through the data, we cannot decode it
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 	if (size == DDI_PROP_RESULT_ERROR)
2267c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_DECODE);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * Allocate memory in which to store the decoded strings.
2307c478bd9Sstevel@tonic-gate 	 */
2317c478bd9Sstevel@tonic-gate 	if ((strs = malloc(nbytes)) == NULL) {
2327c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_DECODE);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/*
2367c478bd9Sstevel@tonic-gate 	 * Finally, we can decode each string
2377c478bd9Sstevel@tonic-gate 	 */
2387c478bd9Sstevel@tonic-gate 	di_prop_reset_pos(ph);
2397c478bd9Sstevel@tonic-gate 	tmp = strs;
2407c478bd9Sstevel@tonic-gate 	for (n = 0; n < cnt; n++) {
2417c478bd9Sstevel@tonic-gate 		i = DDI_PROP_STR(ph, DDI_PROP_CMD_DECODE, tmp);
2427c478bd9Sstevel@tonic-gate 		if (i < DDI_PROP_RESULT_OK) {
2437c478bd9Sstevel@tonic-gate 			/*
2447c478bd9Sstevel@tonic-gate 			 * Free the space we just allocated
2457c478bd9Sstevel@tonic-gate 			 * and return an error
2467c478bd9Sstevel@tonic-gate 			 */
2477c478bd9Sstevel@tonic-gate 			free(strs);
2487c478bd9Sstevel@tonic-gate 			switch (i) {
2497c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_EOF:
2507c478bd9Sstevel@tonic-gate 				return (DDI_PROP_END_OF_DATA);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 			case DDI_PROP_RESULT_ERROR:
2537c478bd9Sstevel@tonic-gate 				return (DDI_PROP_CANNOT_DECODE);
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 		tmp += strlen(tmp) + 1;
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	*(char **)data = strs;
2607c478bd9Sstevel@tonic-gate 	*nelements = cnt;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate  * Decode an array of bytes.
2677c478bd9Sstevel@tonic-gate  */
2687c478bd9Sstevel@tonic-gate static int
di_prop_fm_decode_bytes(prop_handle_t * ph,void * data,uint_t * nelements)2697c478bd9Sstevel@tonic-gate di_prop_fm_decode_bytes(prop_handle_t *ph, void *data, uint_t *nelements)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	uchar_t		*tmp;
2727c478bd9Sstevel@tonic-gate 	int		nbytes;
2737c478bd9Sstevel@tonic-gate 	int		i;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/*
2767c478bd9Sstevel@tonic-gate 	 * If there are no elements return an error
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	if (ph->ph_size == 0)
2797c478bd9Sstevel@tonic-gate 		return (DDI_PROP_END_OF_DATA);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	/*
2827c478bd9Sstevel@tonic-gate 	 * Get the size of the encoded array of bytes.
2837c478bd9Sstevel@tonic-gate 	 */
2847c478bd9Sstevel@tonic-gate 	nbytes = DDI_PROP_BYTES(ph, DDI_PROP_CMD_GET_DSIZE,
2857c478bd9Sstevel@tonic-gate 		data, ph->ph_size);
2867c478bd9Sstevel@tonic-gate 	if (nbytes < DDI_PROP_RESULT_OK) {
2877c478bd9Sstevel@tonic-gate 		switch (nbytes) {
2887c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
2897c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
2927c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/*
2977c478bd9Sstevel@tonic-gate 	 * Allocated memory to store the decoded value in.
2987c478bd9Sstevel@tonic-gate 	 */
2997c478bd9Sstevel@tonic-gate 	if ((tmp = malloc(nbytes)) == NULL) {
3007c478bd9Sstevel@tonic-gate 		return (DDI_PROP_CANNOT_DECODE);
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * Decode each element and place it in the space we just allocated
3057c478bd9Sstevel@tonic-gate 	 */
3067c478bd9Sstevel@tonic-gate 	i = DDI_PROP_BYTES(ph, DDI_PROP_CMD_DECODE, tmp, nbytes);
3077c478bd9Sstevel@tonic-gate 	if (i < DDI_PROP_RESULT_OK) {
3087c478bd9Sstevel@tonic-gate 		/*
3097c478bd9Sstevel@tonic-gate 		 * Free the space we just allocated
3107c478bd9Sstevel@tonic-gate 		 * and return an error
3117c478bd9Sstevel@tonic-gate 		 */
3127c478bd9Sstevel@tonic-gate 		free(tmp);
3137c478bd9Sstevel@tonic-gate 		switch (i) {
3147c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_EOF:
3157c478bd9Sstevel@tonic-gate 			return (DDI_PROP_END_OF_DATA);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 		case DDI_PROP_RESULT_ERROR:
3187c478bd9Sstevel@tonic-gate 			return (DDI_PROP_CANNOT_DECODE);
3197c478bd9Sstevel@tonic-gate 		}
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	*(uchar_t **)data = tmp;
3237c478bd9Sstevel@tonic-gate 	*nelements = nbytes;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  * OBP 1275 integer, string and byte operators.
3307c478bd9Sstevel@tonic-gate  *
3317c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_DECODE:
3327c478bd9Sstevel@tonic-gate  *
3337c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot decode the data
3347c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
3357c478bd9Sstevel@tonic-gate  *	DDI_PROP_OK:			data was decoded
3367c478bd9Sstevel@tonic-gate  *
3377c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_ENCODE:
3387c478bd9Sstevel@tonic-gate  *
3397c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot encode the data
3407c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
3417c478bd9Sstevel@tonic-gate  *	DDI_PROP_OK:			data was encoded
3427c478bd9Sstevel@tonic-gate  *
3437c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_SKIP:
3447c478bd9Sstevel@tonic-gate  *
3457c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot skip the data
3467c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
3477c478bd9Sstevel@tonic-gate  *	DDI_PROP_OK:			data was skipped
3487c478bd9Sstevel@tonic-gate  *
3497c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_GET_ESIZE:
3507c478bd9Sstevel@tonic-gate  *
3517c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot get encoded size
3527c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
3537c478bd9Sstevel@tonic-gate  *	> 0:				the encoded size
3547c478bd9Sstevel@tonic-gate  *
3557c478bd9Sstevel@tonic-gate  * DDI_PROP_CMD_GET_DSIZE:
3567c478bd9Sstevel@tonic-gate  *
3577c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_ERROR:		cannot get decoded size
3587c478bd9Sstevel@tonic-gate  *	DDI_PROP_RESULT_EOF:		end of data
3597c478bd9Sstevel@tonic-gate  *	> 0:				the decoded size
3607c478bd9Sstevel@tonic-gate  */
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * OBP 1275 integer operator
3647c478bd9Sstevel@tonic-gate  *
3657c478bd9Sstevel@tonic-gate  * OBP properties are a byte stream of data, so integers may not be
3667c478bd9Sstevel@tonic-gate  * properly aligned. Therefore we need to copy them one byte at a time.
3677c478bd9Sstevel@tonic-gate  */
3687c478bd9Sstevel@tonic-gate int
di_prop_1275_int(prop_handle_t * ph,uint_t cmd,int * data)3697c478bd9Sstevel@tonic-gate di_prop_1275_int(prop_handle_t *ph, uint_t cmd, int *data)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	int	i;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	switch (cmd) {
3747c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_DECODE:
3757c478bd9Sstevel@tonic-gate 		/*
3767c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
3777c478bd9Sstevel@tonic-gate 		 */
3787c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0)
3797c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
3807c478bd9Sstevel@tonic-gate 		if (ph->ph_flags & PH_FROM_PROM) {
3817c478bd9Sstevel@tonic-gate 			i = ph->ph_size < PROP_1275_INT_SIZE ?
3827c478bd9Sstevel@tonic-gate 			    ph->ph_size : PROP_1275_INT_SIZE;
3837c478bd9Sstevel@tonic-gate 			if ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
3847c478bd9Sstevel@tonic-gate 			    ph->ph_size - i))
3857c478bd9Sstevel@tonic-gate 				return (DDI_PROP_RESULT_ERROR);
3867c478bd9Sstevel@tonic-gate 		} else if (ph->ph_size < sizeof (int) ||
3877c478bd9Sstevel@tonic-gate 		    ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
3887c478bd9Sstevel@tonic-gate 		    ph->ph_size - sizeof (int)))) {
3897c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 		/*
3937c478bd9Sstevel@tonic-gate 		 * Copy the integer, using the implementation-specific
3947c478bd9Sstevel@tonic-gate 		 * copy function if the property is coming from the PROM.
3957c478bd9Sstevel@tonic-gate 		 */
3967c478bd9Sstevel@tonic-gate 		if (ph->ph_flags & PH_FROM_PROM) {
3977c478bd9Sstevel@tonic-gate 			*data = impl_di_prop_int_from_prom(
3987c478bd9Sstevel@tonic-gate 			    (uchar_t *)ph->ph_cur_pos,
3997c478bd9Sstevel@tonic-gate 			    (ph->ph_size < PROP_1275_INT_SIZE) ?
4007c478bd9Sstevel@tonic-gate 			    ph->ph_size : PROP_1275_INT_SIZE);
4017c478bd9Sstevel@tonic-gate 		} else {
4027c478bd9Sstevel@tonic-gate 			bcopy(ph->ph_cur_pos, (caddr_t)data, sizeof (int));
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		/*
4067c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next
4077c478bd9Sstevel@tonic-gate 		 * bit of undecoded data.
4087c478bd9Sstevel@tonic-gate 		 */
4097c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos + PROP_1275_INT_SIZE;
4107c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_ENCODE:
4137c478bd9Sstevel@tonic-gate 		/*
4147c478bd9Sstevel@tonic-gate 		 * Check that there is room to encoded the data
4157c478bd9Sstevel@tonic-gate 		 */
4167c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
4177c478bd9Sstevel@tonic-gate 		    ph->ph_size < PROP_1275_INT_SIZE ||
4187c478bd9Sstevel@tonic-gate 		    ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
4197c478bd9Sstevel@tonic-gate 		    ph->ph_size - sizeof (int))))
4207c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		/*
4237c478bd9Sstevel@tonic-gate 		 * Encode the integer into the byte stream one byte at a
4247c478bd9Sstevel@tonic-gate 		 * time.
4257c478bd9Sstevel@tonic-gate 		 */
4267c478bd9Sstevel@tonic-gate 		bcopy((caddr_t)data, ph->ph_cur_pos, sizeof (int));
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		/*
4297c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
4307c478bd9Sstevel@tonic-gate 		 * space where we can store encoded data.
4317c478bd9Sstevel@tonic-gate 		 */
4327c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos + PROP_1275_INT_SIZE;
4337c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_SKIP:
4367c478bd9Sstevel@tonic-gate 		/*
4377c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
4387c478bd9Sstevel@tonic-gate 		 */
4397c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
4407c478bd9Sstevel@tonic-gate 		    ph->ph_size < PROP_1275_INT_SIZE)
4417c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 		if ((caddr_t)ph->ph_cur_pos ==
4457c478bd9Sstevel@tonic-gate 		    (caddr_t)ph->ph_data + ph->ph_size) {
4467c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
4477c478bd9Sstevel@tonic-gate 		} else if ((caddr_t)ph->ph_cur_pos >
4487c478bd9Sstevel@tonic-gate 		    (caddr_t)ph->ph_data + ph->ph_size) {
4497c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		/*
4537c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
4547c478bd9Sstevel@tonic-gate 		 * undecoded data.
4557c478bd9Sstevel@tonic-gate 		 */
4567c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos + PROP_1275_INT_SIZE;
4577c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_ESIZE:
4607c478bd9Sstevel@tonic-gate 		/*
4617c478bd9Sstevel@tonic-gate 		 * Return the size of an encoded integer on OBP
4627c478bd9Sstevel@tonic-gate 		 */
4637c478bd9Sstevel@tonic-gate 		return (PROP_1275_INT_SIZE);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_DSIZE:
4667c478bd9Sstevel@tonic-gate 		/*
4677c478bd9Sstevel@tonic-gate 		 * Return the size of a decoded integer on the system.
4687c478bd9Sstevel@tonic-gate 		 */
4697c478bd9Sstevel@tonic-gate 		return (sizeof (int));
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
4737c478bd9Sstevel@tonic-gate 	return (0);	/* keep gcc happy */
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate /*
4777c478bd9Sstevel@tonic-gate  * 64 bit integer operator
4787c478bd9Sstevel@tonic-gate  *
4797c478bd9Sstevel@tonic-gate  * This is an extension, defined by Sun, to the 1275 integer
4807c478bd9Sstevel@tonic-gate  * operator.  This routine handles the encoding/decoding of
4817c478bd9Sstevel@tonic-gate  * 64 bit integer properties.
4827c478bd9Sstevel@tonic-gate  */
4837c478bd9Sstevel@tonic-gate int
di_prop_int64_op(prop_handle_t * ph,uint_t cmd,int64_t * data)4847c478bd9Sstevel@tonic-gate di_prop_int64_op(prop_handle_t *ph, uint_t cmd, int64_t *data)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate 	switch (cmd) {
4877c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_DECODE:
4887c478bd9Sstevel@tonic-gate 		/*
4897c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
4907c478bd9Sstevel@tonic-gate 		 */
4917c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0)
4927c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
4937c478bd9Sstevel@tonic-gate 		if (ph->ph_flags & PH_FROM_PROM) {
4947c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
4957c478bd9Sstevel@tonic-gate 		} else if (ph->ph_size < sizeof (int64_t) ||
4967c478bd9Sstevel@tonic-gate 		    ((int64_t *)ph->ph_cur_pos > ((int64_t *)ph->ph_data +
4977c478bd9Sstevel@tonic-gate 		    ph->ph_size - sizeof (int64_t)))) {
4987c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		/*
5027c478bd9Sstevel@tonic-gate 		 * Copy the integer, using the implementation-specific
5037c478bd9Sstevel@tonic-gate 		 * copy function if the property is coming from the PROM.
5047c478bd9Sstevel@tonic-gate 		 */
5057c478bd9Sstevel@tonic-gate 		bcopy(ph->ph_cur_pos, (caddr_t)data, sizeof (int64_t));
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		/*
5087c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next
5097c478bd9Sstevel@tonic-gate 		 * bit of undecoded data.
5107c478bd9Sstevel@tonic-gate 		 */
5117c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
5127c478bd9Sstevel@tonic-gate 		    sizeof (int64_t);
5137c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_ENCODE:
5167c478bd9Sstevel@tonic-gate 		/*
5177c478bd9Sstevel@tonic-gate 		 * Check that there is room to encoded the data
5187c478bd9Sstevel@tonic-gate 		 */
5197c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
5207c478bd9Sstevel@tonic-gate 		    ph->ph_size < sizeof (int64_t) ||
5217c478bd9Sstevel@tonic-gate 		    ((int64_t *)ph->ph_cur_pos > ((int64_t *)ph->ph_data +
5227c478bd9Sstevel@tonic-gate 		    ph->ph_size - sizeof (int64_t))))
5237c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		/*
5267c478bd9Sstevel@tonic-gate 		 * Encode the integer into the byte stream one byte at a
5277c478bd9Sstevel@tonic-gate 		 * time.
5287c478bd9Sstevel@tonic-gate 		 */
5297c478bd9Sstevel@tonic-gate 		bcopy((caddr_t)data, ph->ph_cur_pos, sizeof (int64_t));
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 		/*
5327c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
5337c478bd9Sstevel@tonic-gate 		 * space where we can store encoded data.
5347c478bd9Sstevel@tonic-gate 		 */
5357c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
5367c478bd9Sstevel@tonic-gate 		    sizeof (int64_t);
5377c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_SKIP:
5407c478bd9Sstevel@tonic-gate 		/*
5417c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
5427c478bd9Sstevel@tonic-gate 		 */
5437c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
5447c478bd9Sstevel@tonic-gate 		    ph->ph_size < sizeof (int64_t))
5457c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		if ((caddr_t)ph->ph_cur_pos ==
5497c478bd9Sstevel@tonic-gate 		    (caddr_t)ph->ph_data + ph->ph_size) {
5507c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
5517c478bd9Sstevel@tonic-gate 		} else if ((caddr_t)ph->ph_cur_pos >
5527c478bd9Sstevel@tonic-gate 		    (caddr_t)ph->ph_data + ph->ph_size) {
5537c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
5547c478bd9Sstevel@tonic-gate 		}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 		/*
5577c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
5587c478bd9Sstevel@tonic-gate 		 * undecoded data.
5597c478bd9Sstevel@tonic-gate 		 */
5607c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
5617c478bd9Sstevel@tonic-gate 		    sizeof (int64_t);
5627c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_ESIZE:
5657c478bd9Sstevel@tonic-gate 		/*
5667c478bd9Sstevel@tonic-gate 		 * Return the size of an encoded integer on OBP
5677c478bd9Sstevel@tonic-gate 		 */
5687c478bd9Sstevel@tonic-gate 		return (sizeof (int64_t));
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_DSIZE:
5717c478bd9Sstevel@tonic-gate 		/*
5727c478bd9Sstevel@tonic-gate 		 * Return the size of a decoded integer on the system.
5737c478bd9Sstevel@tonic-gate 		 */
5747c478bd9Sstevel@tonic-gate 		return (sizeof (int64_t));
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
5787c478bd9Sstevel@tonic-gate 	return (0);	/* keep gcc happy */
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate /*
5827c478bd9Sstevel@tonic-gate  * OBP 1275 string operator.
5837c478bd9Sstevel@tonic-gate  *
5847c478bd9Sstevel@tonic-gate  * OBP strings are NULL terminated.
5857c478bd9Sstevel@tonic-gate  */
5867c478bd9Sstevel@tonic-gate int
di_prop_1275_string(prop_handle_t * ph,uint_t cmd,char * data)5877c478bd9Sstevel@tonic-gate di_prop_1275_string(prop_handle_t *ph, uint_t cmd, char *data)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	int	n;
5907c478bd9Sstevel@tonic-gate 	char	*p;
5917c478bd9Sstevel@tonic-gate 	char	*end;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	switch (cmd) {
5947c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_DECODE:
5957c478bd9Sstevel@tonic-gate 		/*
5967c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
5977c478bd9Sstevel@tonic-gate 		 */
5987c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
5997c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 		n = strlen((char *)ph->ph_cur_pos) + 1;
6037c478bd9Sstevel@tonic-gate 		if ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
6047c478bd9Sstevel@tonic-gate 		    ph->ph_size - n)) {
6057c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		/*
6097c478bd9Sstevel@tonic-gate 		 * Copy the NULL terminated string
6107c478bd9Sstevel@tonic-gate 		 */
6117c478bd9Sstevel@tonic-gate 		bcopy((char *)ph->ph_cur_pos, data, n);
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 		/*
6147c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
6157c478bd9Sstevel@tonic-gate 		 * undecoded data.
6167c478bd9Sstevel@tonic-gate 		 */
6177c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + n;
6187c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_ENCODE:
6217c478bd9Sstevel@tonic-gate 		/*
6227c478bd9Sstevel@tonic-gate 		 * Check that there is room to encoded the data
6237c478bd9Sstevel@tonic-gate 		 */
6247c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
6257c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		n = strlen(data) + 1;
6297c478bd9Sstevel@tonic-gate 		if ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
6307c478bd9Sstevel@tonic-gate 		    ph->ph_size - n)) {
6317c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		/*
6357c478bd9Sstevel@tonic-gate 		 * Copy the NULL terminated string
6367c478bd9Sstevel@tonic-gate 		 */
6377c478bd9Sstevel@tonic-gate 		bcopy(data, (char *)ph->ph_cur_pos, n);
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		/*
6407c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
6417c478bd9Sstevel@tonic-gate 		 * space where we can store encoded data.
6427c478bd9Sstevel@tonic-gate 		 */
6437c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + n;
6447c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_SKIP:
6477c478bd9Sstevel@tonic-gate 		/*
6487c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
6497c478bd9Sstevel@tonic-gate 		 */
6507c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
6517c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
6527c478bd9Sstevel@tonic-gate 		}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		/*
6557c478bd9Sstevel@tonic-gate 		 * Return the string length plus one for the NULL
6567c478bd9Sstevel@tonic-gate 		 * We know the size of the property, we need to
6577c478bd9Sstevel@tonic-gate 		 * ensure that the string is properly formatted,
6587c478bd9Sstevel@tonic-gate 		 * since we may be looking up random OBP data.
6597c478bd9Sstevel@tonic-gate 		 */
6607c478bd9Sstevel@tonic-gate 		p = (char *)ph->ph_cur_pos;
6617c478bd9Sstevel@tonic-gate 		end = (char *)ph->ph_data + ph->ph_size;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 		if (p == end) {
6647c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
6657c478bd9Sstevel@tonic-gate 		}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		/*
6687c478bd9Sstevel@tonic-gate 		 * Make sure each char is printable
6697c478bd9Sstevel@tonic-gate 		 */
6707c478bd9Sstevel@tonic-gate 		for (n = 0; p < end && isascii(*p) && !iscntrl(*p); n++, p++)
6717c478bd9Sstevel@tonic-gate 			;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		/* Check termination and non-zero length */
6747c478bd9Sstevel@tonic-gate 		if ((*p == 0) && (n != 0)) {
6757c478bd9Sstevel@tonic-gate 			ph->ph_cur_pos = p + 1;
6767c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_OK);
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_ERROR);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_ESIZE:
6827c478bd9Sstevel@tonic-gate 		/*
6837c478bd9Sstevel@tonic-gate 		 * Return the size of the encoded string on OBP.
6847c478bd9Sstevel@tonic-gate 		 */
6857c478bd9Sstevel@tonic-gate 		return (strlen(data) + 1);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_DSIZE:
6887c478bd9Sstevel@tonic-gate 		/*
6897c478bd9Sstevel@tonic-gate 		 * Return the string length plus one for the NULL
6907c478bd9Sstevel@tonic-gate 		 * We know the size of the property, we need to
6917c478bd9Sstevel@tonic-gate 		 * ensure that the string is properly formatted,
6927c478bd9Sstevel@tonic-gate 		 * since we may be looking up random OBP data.
6937c478bd9Sstevel@tonic-gate 		 */
6947c478bd9Sstevel@tonic-gate 		p = (char *)ph->ph_cur_pos;
6957c478bd9Sstevel@tonic-gate 		end = (char *)ph->ph_data + ph->ph_size;
6967c478bd9Sstevel@tonic-gate 		for (n = 0; p < end; n++) {
6977c478bd9Sstevel@tonic-gate 			if (*p++ == '\0') {
6987c478bd9Sstevel@tonic-gate 				ph->ph_cur_pos = p;
6997c478bd9Sstevel@tonic-gate 				return (n+1);
7007c478bd9Sstevel@tonic-gate 			}
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 		/*
7047c478bd9Sstevel@tonic-gate 		 * Add check here to separate EOF and ERROR.
7057c478bd9Sstevel@tonic-gate 		 */
7067c478bd9Sstevel@tonic-gate 		if (p == end)
7077c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_ERROR);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
7147c478bd9Sstevel@tonic-gate 	return (0);	/* keep gcc happy */
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate /*
7187c478bd9Sstevel@tonic-gate  * OBP 1275 byte operator
7197c478bd9Sstevel@tonic-gate  *
7207c478bd9Sstevel@tonic-gate  * Caller must specify the number of bytes to get. OBP encodes bytes
7217c478bd9Sstevel@tonic-gate  * as a byte so there is a 1-to-1 translation.
7227c478bd9Sstevel@tonic-gate  */
7237c478bd9Sstevel@tonic-gate int
di_prop_1275_bytes(prop_handle_t * ph,uint_t cmd,uchar_t * data,uint_t nelements)7247c478bd9Sstevel@tonic-gate di_prop_1275_bytes(prop_handle_t *ph, uint_t cmd, uchar_t *data,
7257c478bd9Sstevel@tonic-gate     uint_t nelements)
7267c478bd9Sstevel@tonic-gate {
7277c478bd9Sstevel@tonic-gate 	switch (cmd) {
7287c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_DECODE:
7297c478bd9Sstevel@tonic-gate 		/*
7307c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
7317c478bd9Sstevel@tonic-gate 		 */
7327c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
7337c478bd9Sstevel@tonic-gate 		    ph->ph_size < nelements ||
7347c478bd9Sstevel@tonic-gate 		    ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
7357c478bd9Sstevel@tonic-gate 		    ph->ph_size - nelements)))
7367c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 		/*
7397c478bd9Sstevel@tonic-gate 		 * Copy out the bytes
7407c478bd9Sstevel@tonic-gate 		 */
7417c478bd9Sstevel@tonic-gate 		bcopy((char *)ph->ph_cur_pos, (char *)data, nelements);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 		/*
7447c478bd9Sstevel@tonic-gate 		 * Move the current location
7457c478bd9Sstevel@tonic-gate 		 */
7467c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
7477c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_ENCODE:
7507c478bd9Sstevel@tonic-gate 		/*
7517c478bd9Sstevel@tonic-gate 		 * Check that there is room to encode the data
7527c478bd9Sstevel@tonic-gate 		 */
7537c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
7547c478bd9Sstevel@tonic-gate 		    ph->ph_size < nelements ||
7557c478bd9Sstevel@tonic-gate 		    ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
7567c478bd9Sstevel@tonic-gate 		    ph->ph_size - nelements)))
7577c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 		/*
7607c478bd9Sstevel@tonic-gate 		 * Copy in the bytes
7617c478bd9Sstevel@tonic-gate 		 */
7627c478bd9Sstevel@tonic-gate 		bcopy((char *)data, (char *)ph->ph_cur_pos, nelements);
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		/*
7657c478bd9Sstevel@tonic-gate 		 * Move the current location to the start of the next bit of
7667c478bd9Sstevel@tonic-gate 		 * space where we can store encoded data.
7677c478bd9Sstevel@tonic-gate 		 */
7687c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
7697c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_SKIP:
7727c478bd9Sstevel@tonic-gate 		/*
7737c478bd9Sstevel@tonic-gate 		 * Check that there is encoded data
7747c478bd9Sstevel@tonic-gate 		 */
7757c478bd9Sstevel@tonic-gate 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
7767c478bd9Sstevel@tonic-gate 		    ph->ph_size < nelements)
7777c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_ERROR);
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 		if ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
7807c478bd9Sstevel@tonic-gate 		    ph->ph_size - nelements))
7817c478bd9Sstevel@tonic-gate 			return (DDI_PROP_RESULT_EOF);
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 		/*
7847c478bd9Sstevel@tonic-gate 		 * Move the current location
7857c478bd9Sstevel@tonic-gate 		 */
7867c478bd9Sstevel@tonic-gate 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
7877c478bd9Sstevel@tonic-gate 		return (DDI_PROP_RESULT_OK);
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_ESIZE:
7907c478bd9Sstevel@tonic-gate 		/*
7917c478bd9Sstevel@tonic-gate 		 * The size in bytes of the encoded size is the
7927c478bd9Sstevel@tonic-gate 		 * same as the decoded size provided by the caller.
7937c478bd9Sstevel@tonic-gate 		 */
7947c478bd9Sstevel@tonic-gate 		return (nelements);
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	case DDI_PROP_CMD_GET_DSIZE:
7977c478bd9Sstevel@tonic-gate 		/*
7987c478bd9Sstevel@tonic-gate 		 * Just return the number of bytes specified by the caller.
7997c478bd9Sstevel@tonic-gate 		 */
8007c478bd9Sstevel@tonic-gate 		return (nelements);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
8057c478bd9Sstevel@tonic-gate 	return (0);	/* keep gcc happy */
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate /*
8097c478bd9Sstevel@tonic-gate  * Used for properties that come from the OBP, hardware configuration files,
8107c478bd9Sstevel@tonic-gate  * or that are created by calls to ddi_prop_update(9F).
8117c478bd9Sstevel@tonic-gate  */
8127c478bd9Sstevel@tonic-gate static struct prop_handle_ops prop_1275_ops = {
8137c478bd9Sstevel@tonic-gate 	di_prop_1275_int,
8147c478bd9Sstevel@tonic-gate 	di_prop_1275_string,
8157c478bd9Sstevel@tonic-gate 	di_prop_1275_bytes,
8167c478bd9Sstevel@tonic-gate 	di_prop_int64_op
8177c478bd9Sstevel@tonic-gate };
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate /*
8207c478bd9Sstevel@tonic-gate  * Now the real thing:
8217c478bd9Sstevel@tonic-gate  * Extract type-specific values of an property
8227c478bd9Sstevel@tonic-gate  */
8237c478bd9Sstevel@tonic-gate int
di_prop_decode_common(void * data,int size,int prop_type,int prom)8247c478bd9Sstevel@tonic-gate di_prop_decode_common(void *data, int size, int prop_type, int prom)
8257c478bd9Sstevel@tonic-gate {
8267c478bd9Sstevel@tonic-gate 	int n;
8277c478bd9Sstevel@tonic-gate 	int nelements;
8287c478bd9Sstevel@tonic-gate 	char *cp, *end;
8297c478bd9Sstevel@tonic-gate 	prop_handle_t ph;
8307c478bd9Sstevel@tonic-gate 	int (*prop_decoder)(prop_handle_t *, void *, uint_t *);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	/*
8337c478bd9Sstevel@tonic-gate 	 * If the encoded data came from software, no decoding needed
8347c478bd9Sstevel@tonic-gate 	 */
8357c478bd9Sstevel@tonic-gate 	if (!prom) {
8367c478bd9Sstevel@tonic-gate 		switch (prop_type) {
8377c478bd9Sstevel@tonic-gate 		case DI_PROP_TYPE_INT:
8387c478bd9Sstevel@tonic-gate 			if (size % sizeof (int))
8397c478bd9Sstevel@tonic-gate 				nelements = -1;
8407c478bd9Sstevel@tonic-gate 			else
8417c478bd9Sstevel@tonic-gate 				nelements = size / sizeof (int);
8427c478bd9Sstevel@tonic-gate 			break;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 		case DI_PROP_TYPE_INT64:
8457c478bd9Sstevel@tonic-gate 			if (size % sizeof (int64_t))
8467c478bd9Sstevel@tonic-gate 				nelements = -1;
8477c478bd9Sstevel@tonic-gate 			else
8487c478bd9Sstevel@tonic-gate 				nelements = size / sizeof (int64_t);
8497c478bd9Sstevel@tonic-gate 			break;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 		case DI_PROP_TYPE_STRING:
8527c478bd9Sstevel@tonic-gate 			nelements = 0;
8537c478bd9Sstevel@tonic-gate 			cp = *(char **)data;
8547c478bd9Sstevel@tonic-gate 			end = cp + size;
8557c478bd9Sstevel@tonic-gate 			/*
8567c478bd9Sstevel@tonic-gate 			 * Don't trust the data passed in by the caller.
8577c478bd9Sstevel@tonic-gate 			 * Check every char to make sure it is indeed a
8587c478bd9Sstevel@tonic-gate 			 * string property.
8597c478bd9Sstevel@tonic-gate 			 */
8607c478bd9Sstevel@tonic-gate 			while (cp < end) {
8617c478bd9Sstevel@tonic-gate 				/* skip to next non-printable char */
8627c478bd9Sstevel@tonic-gate 				for (n = 0; cp < end &&
8637c478bd9Sstevel@tonic-gate 				    isascii(*cp) && !iscntrl(*cp); n++, cp++)
8647c478bd9Sstevel@tonic-gate 					;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 				/*
8677c478bd9Sstevel@tonic-gate 				 * Fail if reached end (i.e. last char != 0),
8687c478bd9Sstevel@tonic-gate 				 * or has a non-printable char. A zero length
8697c478bd9Sstevel@tonic-gate 				 * string is acceptable.
8707c478bd9Sstevel@tonic-gate 				 */
8717c478bd9Sstevel@tonic-gate 				if (cp == end || *cp != 0) {
8727c478bd9Sstevel@tonic-gate 					nelements = -1;
8737c478bd9Sstevel@tonic-gate 					break;
8747c478bd9Sstevel@tonic-gate 				}
8757c478bd9Sstevel@tonic-gate 				/*
8767c478bd9Sstevel@tonic-gate 				 * Increment # strings and keep going
8777c478bd9Sstevel@tonic-gate 				 */
8787c478bd9Sstevel@tonic-gate 				nelements++;
8797c478bd9Sstevel@tonic-gate 				cp++;
8807c478bd9Sstevel@tonic-gate 			}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 			break;
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 		case DI_PROP_TYPE_BYTE:
8857c478bd9Sstevel@tonic-gate 			nelements = size;
8867c478bd9Sstevel@tonic-gate 		}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 		return (nelements);
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	/*
8927c478bd9Sstevel@tonic-gate 	 * Get the encoded data
8937c478bd9Sstevel@tonic-gate 	 */
8947c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&ph, sizeof (prop_handle_t));
8957c478bd9Sstevel@tonic-gate 	ph.ph_data = *(uchar_t **)data;
8967c478bd9Sstevel@tonic-gate 	ph.ph_size = size;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	/*
8997c478bd9Sstevel@tonic-gate 	 * The data came from prom, use the 1275 OBP decode/encode routines.
9007c478bd9Sstevel@tonic-gate 	 */
9017c478bd9Sstevel@tonic-gate 	ph.ph_cur_pos = ph.ph_data;
9027c478bd9Sstevel@tonic-gate 	ph.ph_save_pos = ph.ph_data;
9037c478bd9Sstevel@tonic-gate 	ph.ph_ops = &prop_1275_ops;
9047c478bd9Sstevel@tonic-gate 	ph.ph_flags = PH_FROM_PROM;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	switch (prop_type) {
9077c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_INT:
9087c478bd9Sstevel@tonic-gate 		prop_decoder = di_prop_fm_decode_ints;
9097c478bd9Sstevel@tonic-gate 		break;
9107c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_STRING:
9117c478bd9Sstevel@tonic-gate 		prop_decoder = di_prop_fm_decode_strings;
9127c478bd9Sstevel@tonic-gate 		break;
9137c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_BYTE:
9147c478bd9Sstevel@tonic-gate 	default:
9157c478bd9Sstevel@tonic-gate 		prop_decoder = di_prop_fm_decode_bytes;
9167c478bd9Sstevel@tonic-gate 		break;
9177c478bd9Sstevel@tonic-gate 	}
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	if ((*prop_decoder)(&ph, data, (uint_t *)&nelements)
9207c478bd9Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS)
9217c478bd9Sstevel@tonic-gate 		return (-1);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	/*
9247c478bd9Sstevel@tonic-gate 	 * Free the encoded data
9257c478bd9Sstevel@tonic-gate 	 */
9267c478bd9Sstevel@tonic-gate 	if (size != 0)
9277c478bd9Sstevel@tonic-gate 		free(ph.ph_data);
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	return (nelements);
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate 
932*3ebafc43Sjveta void
di_slot_names_free(int count,di_slot_name_t * slot_names)933*3ebafc43Sjveta di_slot_names_free(int count, di_slot_name_t *slot_names)
934*3ebafc43Sjveta {
935*3ebafc43Sjveta 	if (slot_names == NULL)
936*3ebafc43Sjveta 		return;
937*3ebafc43Sjveta 
938*3ebafc43Sjveta 	while (--count >= 0) {
939*3ebafc43Sjveta 		if (slot_names[count].name != NULL)
940*3ebafc43Sjveta 			free(slot_names[count].name);
941*3ebafc43Sjveta 	}
942*3ebafc43Sjveta 	free(slot_names);
943*3ebafc43Sjveta }
944*3ebafc43Sjveta 
945*3ebafc43Sjveta /*
946*3ebafc43Sjveta  * 1275 "slot-names" format: [int][string1][string2]...[stringN]
947*3ebafc43Sjveta  *	- [int] is a 1275 encoded integer
948*3ebafc43Sjveta  *      - [string1]...[stringN] are concatenated null-terminated strings
949*3ebafc43Sjveta  *      - each bit position in [int] represents a pci device number
950*3ebafc43Sjveta  *	- each bit which is set in [int] represents a slot with a device
951*3ebafc43Sjveta  *	  number of that bit position
952*3ebafc43Sjveta  *      - each string in [string1]...[stringN] identifies a slot name only
953*3ebafc43Sjveta  *	  for the bits which are set in [int]
954*3ebafc43Sjveta  *	- the ordering of strings follow the ordering of bits set in [int]
955*3ebafc43Sjveta  *
956*3ebafc43Sjveta  * an allocated array of di_slot_name_t is returned through prop_data if
957*3ebafc43Sjveta  * [int] is non-zero and the number of entries as the return value;
958*3ebafc43Sjveta  * use di_slot_names_free() to free the array
959*3ebafc43Sjveta  */
960*3ebafc43Sjveta int
di_slot_names_decode(uchar_t * rawdata,int rawlen,di_slot_name_t ** prop_data)961*3ebafc43Sjveta di_slot_names_decode(uchar_t *rawdata, int rawlen,
962*3ebafc43Sjveta     di_slot_name_t **prop_data)
963*3ebafc43Sjveta {
964*3ebafc43Sjveta 	char *sp, *maxsp;
965*3ebafc43Sjveta 	int count, i;
966*3ebafc43Sjveta 	size_t len;
967*3ebafc43Sjveta 	int slots;
968*3ebafc43Sjveta 	int maxcount = 0;
969*3ebafc43Sjveta 	int maxslots = 0;
970*3ebafc43Sjveta 	di_slot_name_t *slot_names = NULL;
971*3ebafc43Sjveta 
972*3ebafc43Sjveta 	if (rawlen < sizeof (slots))
973*3ebafc43Sjveta 		goto ERROUT;
974*3ebafc43Sjveta 
975*3ebafc43Sjveta 	slots = impl_di_prop_int_from_prom(rawdata, sizeof (slots));
976*3ebafc43Sjveta 	if (slots == 0) {
977*3ebafc43Sjveta 		*prop_data = NULL;
978*3ebafc43Sjveta 		return (0);
979*3ebafc43Sjveta 	}
980*3ebafc43Sjveta 
981*3ebafc43Sjveta 	maxslots = sizeof (slots) * 8;
982*3ebafc43Sjveta 	count = 0;
983*3ebafc43Sjveta 	for (i = 0; i < maxslots; i++) {
984*3ebafc43Sjveta 		if (slots & (1 << i))
985*3ebafc43Sjveta 			count++;
986*3ebafc43Sjveta 	}
987*3ebafc43Sjveta 	maxslots = i;
988*3ebafc43Sjveta 	maxcount = count;
989*3ebafc43Sjveta 	slot_names = malloc(sizeof (*slot_names) * maxcount);
990*3ebafc43Sjveta 	bzero(slot_names, sizeof (*slot_names) * maxcount);
991*3ebafc43Sjveta 
992*3ebafc43Sjveta 	/* also handle unterminated strings */
993*3ebafc43Sjveta 	sp = (char *)(rawdata + sizeof (slots));
994*3ebafc43Sjveta 	maxsp = sp + (rawlen - sizeof (slots));
995*3ebafc43Sjveta 	count = 0;
996*3ebafc43Sjveta 	for (i = 0; i < maxslots; i++) {
997*3ebafc43Sjveta 		if (slots & (1 << i)) {
998*3ebafc43Sjveta 			if (sp > maxsp)
999*3ebafc43Sjveta 				break;
1000*3ebafc43Sjveta 			len = strnlen(sp, (maxsp - sp) + 1);
1001*3ebafc43Sjveta 			if (len == 0)
1002*3ebafc43Sjveta 				break;
1003*3ebafc43Sjveta 
1004*3ebafc43Sjveta 			slot_names[count].name =
1005*3ebafc43Sjveta 			    malloc(sizeof (char) * (len + 1));
1006*3ebafc43Sjveta 			(void) strlcpy(slot_names[count].name, sp, len + 1);
1007*3ebafc43Sjveta 
1008*3ebafc43Sjveta 			slot_names[count].num = i;
1009*3ebafc43Sjveta 
1010*3ebafc43Sjveta 			sp += len + 1;
1011*3ebafc43Sjveta 			count++;
1012*3ebafc43Sjveta 		}
1013*3ebafc43Sjveta 	}
1014*3ebafc43Sjveta 
1015*3ebafc43Sjveta 	/*
1016*3ebafc43Sjveta 	 * check if the number of strings match with the number of slots;
1017*3ebafc43Sjveta 	 * we can also get a lesser string count even when there appears to be
1018*3ebafc43Sjveta 	 * the correct number of strings if one or more pair of strings are
1019*3ebafc43Sjveta 	 * seperated by more than one NULL byte
1020*3ebafc43Sjveta 	 */
1021*3ebafc43Sjveta 	if (count != maxcount)
1022*3ebafc43Sjveta 		goto ERROUT;
1023*3ebafc43Sjveta 
1024*3ebafc43Sjveta 	*prop_data = slot_names;
1025*3ebafc43Sjveta 	return (maxcount);
1026*3ebafc43Sjveta 	/*NOTREACHED*/
1027*3ebafc43Sjveta ERROUT:
1028*3ebafc43Sjveta 	di_slot_names_free(maxcount, slot_names);
1029*3ebafc43Sjveta 	*prop_data = NULL;
1030*3ebafc43Sjveta 	return (-1);
1031*3ebafc43Sjveta }
1032