xref: /illumos-gate/usr/src/cmd/sgs/libconv/common/cap.c (revision 71269a2275bf5a143dad6461eee2710a344e7261)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * String conversion routine for hardware capabilities types.
30  */
31 #include	<strings.h>
32 #include	<stdio.h>
33 #include	<_machelf.h>
34 #include	<elfcap.h>
35 #include	"cap_msg.h"
36 #include	"_conv.h"
37 
38 static int
39 conv_cap_1(Xword val, char *str, size_t len, Half mach,
40     Conv_fmt_flags_t fmt_flags, elfcap_to_str_func_t *fptr)
41 {
42 	size_t	_len;
43 	int do_bkt = (fmt_flags & CONV_FMT_NOBKT) == 0;
44 
45 	/*
46 	 * Note that for the purposes of this routine, I consider
47 	 * CONV_FMT_NOBKT to mean no brackets, or anything that
48 	 * is placed outside of them. We also drop the hex version
49 	 * of the flags that are put in front of the opening bracket.
50 	 */
51 	if (do_bkt) {
52 		_len = sprintf(str, MSG_ORIG(MSG_GBL_OSQBRKT), EC_XWORD(val));
53 
54 		len -= _len;
55 		str += _len;
56 	}
57 
58 	if ((*fptr)(ELFCAP_STYLE_UC, val, str, len, ELFCAP_FMT_SNGSPACE,
59 	    mach) != 0)
60 		return (0);
61 
62 	if (do_bkt) {
63 		_len = strlen(str);
64 		if ((len - _len) >= MSG_GBL_CSQBRKT_SIZE) {
65 			str += _len;
66 			(void) strcpy(str, MSG_ORIG(MSG_GBL_CSQBRKT));
67 		}
68 	}
69 	return (1);
70 }
71 
72 const char *
73 conv_cap_val_hw1(Xword val, Half mach, Conv_fmt_flags_t fmt_flags,
74     Conv_cap_val_hw1_buf_t *cap_val_hw1_buf)
75 {
76 	if (val == 0)
77 		return (MSG_ORIG(MSG_GBL_ZERO));
78 
79 	if (conv_cap_1(val, cap_val_hw1_buf->buf, sizeof (cap_val_hw1_buf->buf),
80 	    mach, fmt_flags, elfcap_hw1_to_str) == 0)
81 		return (conv_invalid_val(&cap_val_hw1_buf->inv_buf, val, 0));
82 	return ((const char *)cap_val_hw1_buf->buf);
83 }
84 
85 const char *
86 conv_cap_val_sf1(Xword val, Half mach, Conv_fmt_flags_t fmt_flags,
87     Conv_cap_val_sf1_buf_t *cap_val_sf1_buf)
88 {
89 	if (val == 0)
90 		return (MSG_ORIG(MSG_GBL_ZERO));
91 
92 	if (conv_cap_1(val, cap_val_sf1_buf->buf, sizeof (cap_val_sf1_buf->buf),
93 	    mach, fmt_flags, elfcap_sf1_to_str) == 0)
94 		return (conv_invalid_val(&cap_val_sf1_buf->inv_buf, val, 0));
95 	return ((const char *)cap_val_sf1_buf->buf);
96 }
97 
98 const char *
99 conv_cap_tag(Xword tag, Conv_inv_buf_t *inv_buf)
100 {
101 	static const Msg	tags[] = {
102 		MSG_CA_SUNW_NULL,	MSG_CA_SUNW_HW_1,
103 		MSG_CA_SUNW_SF_1
104 	};
105 
106 	if (tag <= CA_SUNW_SF_1)
107 		return (MSG_ORIG(tags[tag]));
108 	else
109 		return (conv_invalid_val(inv_buf, tag, 0));
110 }
111 
112 const char *
113 conv_cap_val(Xword tag, Xword val, Half mach, Conv_cap_val_buf_t *cap_val_buf)
114 {
115 	if (tag == CA_SUNW_HW_1)
116 		return (conv_cap_val_hw1(val, mach, 0,
117 		    &cap_val_buf->cap_val_hw1_buf));
118 	else if (tag == CA_SUNW_SF_1)
119 		return (conv_cap_val_sf1(val, mach, 0,
120 		    &cap_val_buf->cap_val_sf1_buf));
121 	else
122 		return (conv_invalid_val(&cap_val_buf->inv_buf, val, 0));
123 }
124