xref: /titanic_52/usr/src/cmd/sgs/libconv/common/cap_machelf.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * String conversion routine for hardware capabilities types.
29  */
30 #include	<strings.h>
31 #include	<stdio.h>
32 #include	<_machelf.h>
33 #include	<elfcap.h>
34 #include	"cap_msg.h"
35 #include	"_conv.h"
36 
37 static int
38 conv_cap_1(Xword val, char *str, size_t len, Half mach,
39     Conv_fmt_flags_t fmt_flags, elfcap_to_str_func_t *fptr)
40 {
41 	size_t	_len;
42 	int do_bkt = (fmt_flags & CONV_FMT_NOBKT) == 0;
43 
44 	/*
45 	 * Note that for the purposes of this routine, I consider
46 	 * CONV_FMT_NOBKT to mean no brackets, or anything that
47 	 * is placed outside of them. We also drop the hex version
48 	 * of the flags that are put in front of the opening bracket.
49 	 */
50 	if (do_bkt) {
51 		_len = sprintf(str, MSG_ORIG(MSG_GBL_OSQBRKT), EC_XWORD(val));
52 
53 		len -= _len;
54 		str += _len;
55 	}
56 
57 	if ((*fptr)(ELFCAP_STYLE_UC, val, str, len, ELFCAP_FMT_SNGSPACE,
58 	    mach) != 0)
59 		return (0);
60 
61 	if (do_bkt) {
62 		_len = strlen(str);
63 		if ((len - _len) >= MSG_GBL_CSQBRKT_SIZE) {
64 			str += _len;
65 			(void) strcpy(str, MSG_ORIG(MSG_GBL_CSQBRKT));
66 		}
67 	}
68 	return (1);
69 }
70 
71 const char *
72 conv_cap_val_hw1(Xword val, Half mach, Conv_fmt_flags_t fmt_flags,
73     Conv_cap_val_hw1_buf_t *cap_val_hw1_buf)
74 {
75 	if (val == 0)
76 		return (MSG_ORIG(MSG_GBL_ZERO));
77 
78 	if (conv_cap_1(val, cap_val_hw1_buf->buf, sizeof (cap_val_hw1_buf->buf),
79 	    mach, fmt_flags, elfcap_hw1_to_str) == 0)
80 		return (conv_invalid_val(&cap_val_hw1_buf->inv_buf, val, 0));
81 	return ((const char *)cap_val_hw1_buf->buf);
82 }
83 
84 const char *
85 conv_cap_val_sf1(Xword val, Half mach, Conv_fmt_flags_t fmt_flags,
86     Conv_cap_val_sf1_buf_t *cap_val_sf1_buf)
87 {
88 	if (val == 0)
89 		return (MSG_ORIG(MSG_GBL_ZERO));
90 
91 	if (conv_cap_1(val, cap_val_sf1_buf->buf, sizeof (cap_val_sf1_buf->buf),
92 	    mach, fmt_flags, elfcap_sf1_to_str) == 0)
93 		return (conv_invalid_val(&cap_val_sf1_buf->inv_buf, val, 0));
94 	return ((const char *)cap_val_sf1_buf->buf);
95 }
96 
97 const char *
98 conv_cap_tag(Xword tag, Conv_fmt_flags_t fmt_flags, Conv_inv_buf_t *inv_buf)
99 {
100 #ifdef _ELF64
101 	/*
102 	 * Valid tags all fit in 32-bits, so a value larger than that
103 	 * is garbage. conv_map_ds() sees 32-bit values, so test for garbage
104 	 * here before passing it on.
105 	 *
106 	 * Since there are no valid tags with a value > 32-bits, there
107 	 * is no reason to expend effort decoding the low order bits.
108 	 */
109 	if (tag & 0xffffffff00000000)
110 		return (conv_invalid_val(inv_buf, tag, fmt_flags));
111 #endif
112 
113 	return (conv_map_ds(ELFOSABI_NONE, EM_NONE, tag,
114 	    conv_cap_tag_strings(fmt_flags), fmt_flags, inv_buf));
115 }
116 
117 const char *
118 conv_cap_val(Xword tag, Xword val, Half mach, Conv_cap_val_buf_t *cap_val_buf)
119 {
120 	if (tag == CA_SUNW_HW_1)
121 		return (conv_cap_val_hw1(val, mach, 0,
122 		    &cap_val_buf->cap_val_hw1_buf));
123 	else if (tag == CA_SUNW_SF_1)
124 		return (conv_cap_val_sf1(val, mach, 0,
125 		    &cap_val_buf->cap_val_sf1_buf));
126 	else
127 		return (conv_invalid_val(&cap_val_buf->inv_buf, val, 0));
128 }
129