xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/cap_machelf.c (revision 11827:d7ef53deac3f)
19273SAli.Bahrami@Sun.COM /*
29273SAli.Bahrami@Sun.COM  * CDDL HEADER START
39273SAli.Bahrami@Sun.COM  *
49273SAli.Bahrami@Sun.COM  * The contents of this file are subject to the terms of the
59273SAli.Bahrami@Sun.COM  * Common Development and Distribution License (the "License").
69273SAli.Bahrami@Sun.COM  * You may not use this file except in compliance with the License.
79273SAli.Bahrami@Sun.COM  *
89273SAli.Bahrami@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99273SAli.Bahrami@Sun.COM  * or http://www.opensolaris.org/os/licensing.
109273SAli.Bahrami@Sun.COM  * See the License for the specific language governing permissions
119273SAli.Bahrami@Sun.COM  * and limitations under the License.
129273SAli.Bahrami@Sun.COM  *
139273SAli.Bahrami@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
149273SAli.Bahrami@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
159273SAli.Bahrami@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
169273SAli.Bahrami@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
179273SAli.Bahrami@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
189273SAli.Bahrami@Sun.COM  *
199273SAli.Bahrami@Sun.COM  * CDDL HEADER END
209273SAli.Bahrami@Sun.COM  */
219273SAli.Bahrami@Sun.COM 
229273SAli.Bahrami@Sun.COM /*
23*11827SRod.Evans@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
249273SAli.Bahrami@Sun.COM  * Use is subject to license terms.
259273SAli.Bahrami@Sun.COM  */
269273SAli.Bahrami@Sun.COM 
279273SAli.Bahrami@Sun.COM /*
289273SAli.Bahrami@Sun.COM  * String conversion routine for hardware capabilities types.
299273SAli.Bahrami@Sun.COM  */
309273SAli.Bahrami@Sun.COM #include	<strings.h>
319273SAli.Bahrami@Sun.COM #include	<stdio.h>
329273SAli.Bahrami@Sun.COM #include	<_machelf.h>
339273SAli.Bahrami@Sun.COM #include	<elfcap.h>
349273SAli.Bahrami@Sun.COM #include	"cap_msg.h"
359273SAli.Bahrami@Sun.COM #include	"_conv.h"
369273SAli.Bahrami@Sun.COM 
379273SAli.Bahrami@Sun.COM static int
conv_cap(Xword val,char * str,size_t len,Half mach,Conv_fmt_flags_t fmt_flags,elfcap_to_str_func_t * fptr)38*11827SRod.Evans@Sun.COM conv_cap(Xword val, char *str, size_t len, Half mach,
399273SAli.Bahrami@Sun.COM     Conv_fmt_flags_t fmt_flags, elfcap_to_str_func_t *fptr)
409273SAli.Bahrami@Sun.COM {
419273SAli.Bahrami@Sun.COM 	size_t	_len;
429273SAli.Bahrami@Sun.COM 	int do_bkt = (fmt_flags & CONV_FMT_NOBKT) == 0;
439273SAli.Bahrami@Sun.COM 
449273SAli.Bahrami@Sun.COM 	/*
459273SAli.Bahrami@Sun.COM 	 * Note that for the purposes of this routine, I consider
469273SAli.Bahrami@Sun.COM 	 * CONV_FMT_NOBKT to mean no brackets, or anything that
479273SAli.Bahrami@Sun.COM 	 * is placed outside of them. We also drop the hex version
489273SAli.Bahrami@Sun.COM 	 * of the flags that are put in front of the opening bracket.
499273SAli.Bahrami@Sun.COM 	 */
509273SAli.Bahrami@Sun.COM 	if (do_bkt) {
519273SAli.Bahrami@Sun.COM 		_len = sprintf(str, MSG_ORIG(MSG_GBL_OSQBRKT), EC_XWORD(val));
529273SAli.Bahrami@Sun.COM 
539273SAli.Bahrami@Sun.COM 		len -= _len;
549273SAli.Bahrami@Sun.COM 		str += _len;
559273SAli.Bahrami@Sun.COM 	}
569273SAli.Bahrami@Sun.COM 
579273SAli.Bahrami@Sun.COM 	if ((*fptr)(ELFCAP_STYLE_UC, val, str, len, ELFCAP_FMT_SNGSPACE,
589273SAli.Bahrami@Sun.COM 	    mach) != 0)
599273SAli.Bahrami@Sun.COM 		return (0);
609273SAli.Bahrami@Sun.COM 
619273SAli.Bahrami@Sun.COM 	if (do_bkt) {
629273SAli.Bahrami@Sun.COM 		_len = strlen(str);
639273SAli.Bahrami@Sun.COM 		if ((len - _len) >= MSG_GBL_CSQBRKT_SIZE) {
649273SAli.Bahrami@Sun.COM 			str += _len;
659273SAli.Bahrami@Sun.COM 			(void) strcpy(str, MSG_ORIG(MSG_GBL_CSQBRKT));
669273SAli.Bahrami@Sun.COM 		}
679273SAli.Bahrami@Sun.COM 	}
689273SAli.Bahrami@Sun.COM 	return (1);
699273SAli.Bahrami@Sun.COM }
709273SAli.Bahrami@Sun.COM 
719273SAli.Bahrami@Sun.COM const char *
conv_cap_val_hw1(Xword val,Half mach,Conv_fmt_flags_t fmt_flags,Conv_cap_val_hw1_buf_t * cap_val_hw1_buf)729273SAli.Bahrami@Sun.COM conv_cap_val_hw1(Xword val, Half mach, Conv_fmt_flags_t fmt_flags,
739273SAli.Bahrami@Sun.COM     Conv_cap_val_hw1_buf_t *cap_val_hw1_buf)
749273SAli.Bahrami@Sun.COM {
759273SAli.Bahrami@Sun.COM 	if (val == 0)
769273SAli.Bahrami@Sun.COM 		return (MSG_ORIG(MSG_GBL_ZERO));
779273SAli.Bahrami@Sun.COM 
78*11827SRod.Evans@Sun.COM 	if (conv_cap(val, cap_val_hw1_buf->buf, sizeof (cap_val_hw1_buf->buf),
799273SAli.Bahrami@Sun.COM 	    mach, fmt_flags, elfcap_hw1_to_str) == 0)
809273SAli.Bahrami@Sun.COM 		return (conv_invalid_val(&cap_val_hw1_buf->inv_buf, val, 0));
819273SAli.Bahrami@Sun.COM 	return ((const char *)cap_val_hw1_buf->buf);
829273SAli.Bahrami@Sun.COM }
839273SAli.Bahrami@Sun.COM 
849273SAli.Bahrami@Sun.COM const char *
conv_cap_val_hw2(Xword val,Half mach,Conv_fmt_flags_t fmt_flags,Conv_cap_val_hw2_buf_t * cap_val_hw2_buf)85*11827SRod.Evans@Sun.COM conv_cap_val_hw2(Xword val, Half mach, Conv_fmt_flags_t fmt_flags,
86*11827SRod.Evans@Sun.COM     Conv_cap_val_hw2_buf_t *cap_val_hw2_buf)
87*11827SRod.Evans@Sun.COM {
88*11827SRod.Evans@Sun.COM 	if (val == 0)
89*11827SRod.Evans@Sun.COM 		return (MSG_ORIG(MSG_GBL_ZERO));
90*11827SRod.Evans@Sun.COM 
91*11827SRod.Evans@Sun.COM 	if (conv_cap(val, cap_val_hw2_buf->buf, sizeof (cap_val_hw2_buf->buf),
92*11827SRod.Evans@Sun.COM 	    mach, fmt_flags, elfcap_hw2_to_str) == 0)
93*11827SRod.Evans@Sun.COM 		return (conv_invalid_val(&cap_val_hw2_buf->inv_buf, val, 0));
94*11827SRod.Evans@Sun.COM 	return ((const char *)cap_val_hw2_buf->buf);
95*11827SRod.Evans@Sun.COM }
96*11827SRod.Evans@Sun.COM 
97*11827SRod.Evans@Sun.COM const char *
conv_cap_val_sf1(Xword val,Half mach,Conv_fmt_flags_t fmt_flags,Conv_cap_val_sf1_buf_t * cap_val_sf1_buf)989273SAli.Bahrami@Sun.COM conv_cap_val_sf1(Xword val, Half mach, Conv_fmt_flags_t fmt_flags,
999273SAli.Bahrami@Sun.COM     Conv_cap_val_sf1_buf_t *cap_val_sf1_buf)
1009273SAli.Bahrami@Sun.COM {
1019273SAli.Bahrami@Sun.COM 	if (val == 0)
1029273SAli.Bahrami@Sun.COM 		return (MSG_ORIG(MSG_GBL_ZERO));
1039273SAli.Bahrami@Sun.COM 
104*11827SRod.Evans@Sun.COM 	if (conv_cap(val, cap_val_sf1_buf->buf, sizeof (cap_val_sf1_buf->buf),
1059273SAli.Bahrami@Sun.COM 	    mach, fmt_flags, elfcap_sf1_to_str) == 0)
1069273SAli.Bahrami@Sun.COM 		return (conv_invalid_val(&cap_val_sf1_buf->inv_buf, val, 0));
1079273SAli.Bahrami@Sun.COM 	return ((const char *)cap_val_sf1_buf->buf);
1089273SAli.Bahrami@Sun.COM }
1099273SAli.Bahrami@Sun.COM 
1109273SAli.Bahrami@Sun.COM const char *
conv_cap_tag(Xword tag,Conv_fmt_flags_t fmt_flags,Conv_inv_buf_t * inv_buf)1119273SAli.Bahrami@Sun.COM conv_cap_tag(Xword tag, Conv_fmt_flags_t fmt_flags, Conv_inv_buf_t *inv_buf)
1129273SAli.Bahrami@Sun.COM {
1139273SAli.Bahrami@Sun.COM #ifdef _ELF64
1149273SAli.Bahrami@Sun.COM 	/*
1159273SAli.Bahrami@Sun.COM 	 * Valid tags all fit in 32-bits, so a value larger than that
1169273SAli.Bahrami@Sun.COM 	 * is garbage. conv_map_ds() sees 32-bit values, so test for garbage
1179273SAli.Bahrami@Sun.COM 	 * here before passing it on.
1189273SAli.Bahrami@Sun.COM 	 *
1199273SAli.Bahrami@Sun.COM 	 * Since there are no valid tags with a value > 32-bits, there
1209273SAli.Bahrami@Sun.COM 	 * is no reason to expend effort decoding the low order bits.
1219273SAli.Bahrami@Sun.COM 	 */
1229273SAli.Bahrami@Sun.COM 	if (tag & 0xffffffff00000000)
1239273SAli.Bahrami@Sun.COM 		return (conv_invalid_val(inv_buf, tag, fmt_flags));
1249273SAli.Bahrami@Sun.COM #endif
1259273SAli.Bahrami@Sun.COM 
1269273SAli.Bahrami@Sun.COM 	return (conv_map_ds(ELFOSABI_NONE, EM_NONE, tag,
1279273SAli.Bahrami@Sun.COM 	    conv_cap_tag_strings(fmt_flags), fmt_flags, inv_buf));
1289273SAli.Bahrami@Sun.COM }
1299273SAli.Bahrami@Sun.COM 
1309273SAli.Bahrami@Sun.COM const char *
conv_cap_val(Xword tag,Xword val,Half mach,Conv_fmt_flags_t fmt_flags,Conv_cap_val_buf_t * cap_val_buf)131*11827SRod.Evans@Sun.COM conv_cap_val(Xword tag, Xword val, Half mach, Conv_fmt_flags_t fmt_flags,
132*11827SRod.Evans@Sun.COM     Conv_cap_val_buf_t *cap_val_buf)
1339273SAli.Bahrami@Sun.COM {
134*11827SRod.Evans@Sun.COM 	switch (tag) {
135*11827SRod.Evans@Sun.COM 	case CA_SUNW_HW_1:
136*11827SRod.Evans@Sun.COM 		return (conv_cap_val_hw1(val, mach, fmt_flags,
1379273SAli.Bahrami@Sun.COM 		    &cap_val_buf->cap_val_hw1_buf));
138*11827SRod.Evans@Sun.COM 
139*11827SRod.Evans@Sun.COM 	case CA_SUNW_SF_1:
140*11827SRod.Evans@Sun.COM 		return (conv_cap_val_sf1(val, mach, fmt_flags,
1419273SAli.Bahrami@Sun.COM 		    &cap_val_buf->cap_val_sf1_buf));
142*11827SRod.Evans@Sun.COM 
143*11827SRod.Evans@Sun.COM 	case CA_SUNW_HW_2:
144*11827SRod.Evans@Sun.COM 		return (conv_cap_val_hw2(val, mach, fmt_flags,
145*11827SRod.Evans@Sun.COM 		    &cap_val_buf->cap_val_hw2_buf));
146*11827SRod.Evans@Sun.COM 
147*11827SRod.Evans@Sun.COM 	default:
1489273SAli.Bahrami@Sun.COM 		return (conv_invalid_val(&cap_val_buf->inv_buf, val, 0));
149*11827SRod.Evans@Sun.COM 	}
1509273SAli.Bahrami@Sun.COM }
151