xref: /titanic_50/usr/src/cmd/sgs/libconv/common/cap_machelf.c (revision 0a1278f26ea4b7c8c0285d4f2d6c5b680904aa01)
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 2010 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(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(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_hw2(Xword val, Half mach, Conv_fmt_flags_t fmt_flags,
86     Conv_cap_val_hw2_buf_t *cap_val_hw2_buf)
87 {
88 	if (val == 0)
89 		return (MSG_ORIG(MSG_GBL_ZERO));
90 
91 	if (conv_cap(val, cap_val_hw2_buf->buf, sizeof (cap_val_hw2_buf->buf),
92 	    mach, fmt_flags, elfcap_hw2_to_str) == 0)
93 		return (conv_invalid_val(&cap_val_hw2_buf->inv_buf, val, 0));
94 	return ((const char *)cap_val_hw2_buf->buf);
95 }
96 
97 const char *
98 conv_cap_val_sf1(Xword val, Half mach, Conv_fmt_flags_t fmt_flags,
99     Conv_cap_val_sf1_buf_t *cap_val_sf1_buf)
100 {
101 	if (val == 0)
102 		return (MSG_ORIG(MSG_GBL_ZERO));
103 
104 	if (conv_cap(val, cap_val_sf1_buf->buf, sizeof (cap_val_sf1_buf->buf),
105 	    mach, fmt_flags, elfcap_sf1_to_str) == 0)
106 		return (conv_invalid_val(&cap_val_sf1_buf->inv_buf, val, 0));
107 	return ((const char *)cap_val_sf1_buf->buf);
108 }
109 
110 const char *
111 conv_cap_tag(Xword tag, Conv_fmt_flags_t fmt_flags, Conv_inv_buf_t *inv_buf)
112 {
113 #ifdef _ELF64
114 	/*
115 	 * Valid tags all fit in 32-bits, so a value larger than that
116 	 * is garbage. conv_map_ds() sees 32-bit values, so test for garbage
117 	 * here before passing it on.
118 	 *
119 	 * Since there are no valid tags with a value > 32-bits, there
120 	 * is no reason to expend effort decoding the low order bits.
121 	 */
122 	if (tag & 0xffffffff00000000)
123 		return (conv_invalid_val(inv_buf, tag, fmt_flags));
124 #endif
125 
126 	return (conv_map_ds(ELFOSABI_NONE, EM_NONE, tag,
127 	    conv_cap_tag_strings(fmt_flags), fmt_flags, inv_buf));
128 }
129 
130 const char *
131 conv_cap_val(Xword tag, Xword val, Half mach, Conv_fmt_flags_t fmt_flags,
132     Conv_cap_val_buf_t *cap_val_buf)
133 {
134 	switch (tag) {
135 	case CA_SUNW_HW_1:
136 		return (conv_cap_val_hw1(val, mach, fmt_flags,
137 		    &cap_val_buf->cap_val_hw1_buf));
138 
139 	case CA_SUNW_SF_1:
140 		return (conv_cap_val_sf1(val, mach, fmt_flags,
141 		    &cap_val_buf->cap_val_sf1_buf));
142 
143 	case CA_SUNW_HW_2:
144 		return (conv_cap_val_hw2(val, mach, fmt_flags,
145 		    &cap_val_buf->cap_val_hw2_buf));
146 
147 	default:
148 		return (conv_invalid_val(&cap_val_buf->inv_buf, val, 0));
149 	}
150 }
151