xref: /illumos-gate/usr/src/cmd/sgs/libconv/common/cap.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 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	<limits.h>
34 #include	<sys/machelf.h>
35 #include	<elfcap.h>
36 #include	"_conv.h"
37 #include	"cap_msg.h"
38 
39 void
40 conv_cap_1_str(uint64_t val, char *str, size_t len, ushort_t mach,
41     int (*fptr)(uint64_t, char *, size_t, int, ushort_t))
42 {
43 	size_t	_len;
44 
45 	_len = sprintf(str, MSG_ORIG(MSG_GBL_OSQBRKT), EC_XWORD(val));
46 
47 	len -= _len;
48 	str += _len;
49 
50 	if ((*fptr)(val, str, len, CAP_FMT_DBLSPACE, mach) == 0) {
51 		_len = strlen(str);
52 
53 		if ((len - _len) >= MSG_GBL_CSQBRKT_SIZE) {
54 			str += _len;
55 			(void) strcpy(str, MSG_ORIG(MSG_GBL_CSQBRKT));
56 		}
57 	}
58 }
59 
60 #define	HW1SZ	100
61 
62 const char *
63 conv_hwcap_1_str(uint64_t val, ushort_t mach)
64 {
65 	static char	string[HW1SZ] = { '\0' };
66 
67 	if (val == 0)
68 		return (MSG_ORIG(MSG_GBL_ZERO));
69 
70 	conv_cap_1_str(val, string, HW1SZ, mach, hwcap_1_val2str);
71 	return ((const char *)string);
72 }
73 
74 #define	SF1SZ	40
75 
76 const char *
77 conv_sfcap_1_str(uint64_t val, ushort_t mach)
78 {
79 	static char	string[SF1SZ] = { '\0' };
80 
81 	if (val == 0)
82 		return (MSG_ORIG(MSG_GBL_ZERO));
83 
84 	conv_cap_1_str(val, string, SF1SZ, mach, sfcap_1_val2str);
85 	return ((const char *)string);
86 }
87 
88 static const Msg cap_tags[] = {
89 	MSG_CA_SUNW_NULL,	MSG_CA_SUNW_HW_1,	MSG_CA_SUNW_SF_1
90 };
91 
92 const char *
93 conv_captag_str(uint64_t tag)
94 {
95 	static char	string[STRSIZE] = { '\0' };
96 
97 	if (tag <= CA_SUNW_SF_1)
98 		return (MSG_ORIG(cap_tags[tag]));
99 	else
100 		return (conv_invalid_str(string, STRSIZE, tag, 0));
101 }
102 
103 const char *
104 conv_capval_str(uint64_t tag, uint64_t val, ushort_t mach)
105 {
106 	static char	string[STRSIZE] = { '\0' };
107 
108 	if (tag == CA_SUNW_HW_1)
109 		return (conv_hwcap_1_str(val, mach));
110 	else if (tag == CA_SUNW_SF_1)
111 		return (conv_sfcap_1_str(val, mach));
112 	else
113 		return (conv_invalid_str(string, STRSIZE, val, 0));
114 }
115