cap.c revision 6206:6b0ed502a8e7
1221828Sgrehan/*
2221828Sgrehan * CDDL HEADER START
3221828Sgrehan *
4221828Sgrehan * The contents of this file are subject to the terms of the
5221828Sgrehan * Common Development and Distribution License (the "License").
6221828Sgrehan * You may not use this file except in compliance with the License.
7221828Sgrehan *
8221828Sgrehan * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9221828Sgrehan * or http://www.opensolaris.org/os/licensing.
10221828Sgrehan * See the License for the specific language governing permissions
11221828Sgrehan * and limitations under the License.
12221828Sgrehan *
13221828Sgrehan * When distributing Covered Code, include this CDDL HEADER in each
14221828Sgrehan * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15221828Sgrehan * If applicable, add the following below this CDDL HEADER, with the
16221828Sgrehan * fields enclosed by brackets "[]" replaced with your own identifying
17221828Sgrehan * information: Portions Copyright [yyyy] [name of copyright owner]
18221828Sgrehan *
19221828Sgrehan * CDDL HEADER END
20221828Sgrehan */
21221828Sgrehan
22221828Sgrehan/*
23221828Sgrehan * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24221828Sgrehan * Use is subject to license terms.
25221828Sgrehan */
26221828Sgrehan#pragma ident	"%Z%%M%	%I%	%E% SMI"
27221828Sgrehan
28221828Sgrehan/*
29221828Sgrehan * String conversion routine for hardware capabilities types.
30221828Sgrehan */
31221828Sgrehan#include	<strings.h>
32221828Sgrehan#include	<stdio.h>
33221828Sgrehan#include	<_machelf.h>
34221828Sgrehan#include	<elfcap.h>
35221828Sgrehan#include	"cap_msg.h"
36262350Sjhb#include	"_conv.h"
37221828Sgrehan
38221828Sgrehanstatic int
39221828Sgrehanconv_cap_1(Xword val, char *str, size_t len, Half mach,
40221828Sgrehan    Conv_fmt_flags_t fmt_flags, elfcap_to_str_func_t *fptr)
41257396Sneel{
42256176Sneel	size_t	_len;
43221828Sgrehan	int do_bkt = (fmt_flags & CONV_FMT_NOBKT) == 0;
44221828Sgrehan
45221828Sgrehan	/*
46221828Sgrehan	 * Note that for the purposes of this routine, I consider
47221828Sgrehan	 * CONV_FMT_NOBKT to mean no brackets, or anything that
48242404Sgrehan	 * is placed outside of them. We also drop the hex version
49256176Sneel	 * of the flags that are put in front of the opening bracket.
50221828Sgrehan	 */
51221828Sgrehan	if (do_bkt) {
52221828Sgrehan		_len = sprintf(str, MSG_ORIG(MSG_GBL_OSQBRKT), EC_XWORD(val));
53221828Sgrehan
54244167Sgrehan		len -= _len;
55243327Sgrehan		str += _len;
56221828Sgrehan	}
57221828Sgrehan
58267393Sjhb	if ((*fptr)(ELFCAP_STYLE_UC, val, str, len, ELFCAP_FMT_SNGSPACE,
59241744Sgrehan	    mach) != 0)
60221828Sgrehan		return (0);
61242131Sgrehan
62221828Sgrehan	if (do_bkt) {
63257396Sneel		_len = strlen(str);
64267450Sjhb		if ((len - _len) >= MSG_GBL_CSQBRKT_SIZE) {
65221828Sgrehan			str += _len;
66240912Sneel			(void) strcpy(str, MSG_ORIG(MSG_GBL_CSQBRKT));
67253181Sgrehan		}
68221828Sgrehan	}
69221828Sgrehan	return (1);
70221828Sgrehan}
71221828Sgrehan
72221828Sgrehanconst char *
73221828Sgrehanconv_cap_val_hw1(Xword val, Half mach, Conv_fmt_flags_t fmt_flags,
74221828Sgrehan    Conv_cap_val_hw1_buf_t *cap_val_hw1_buf)
75221828Sgrehan{
76261090Sjhb	if (val == 0)
77221828Sgrehan		return (MSG_ORIG(MSG_GBL_ZERO));
78221828Sgrehan
79221828Sgrehan	if (conv_cap_1(val, cap_val_hw1_buf->buf, sizeof (cap_val_hw1_buf->buf),
80221828Sgrehan	    mach, fmt_flags, elfcap_hw1_to_str) == 0)
81221828Sgrehan		return (conv_invalid_val(&cap_val_hw1_buf->inv_buf, val, 0));
82221828Sgrehan	return ((const char *)cap_val_hw1_buf->buf);
83221828Sgrehan}
84221828Sgrehan
85221828Sgrehanconst char *
86267450Sjhbconv_cap_val_sf1(Xword val, Half mach, Conv_fmt_flags_t fmt_flags,
87221828Sgrehan    Conv_cap_val_sf1_buf_t *cap_val_sf1_buf)
88221828Sgrehan{
89267447Sjhb	if (val == 0)
90256755Sgrehan		return (MSG_ORIG(MSG_GBL_ZERO));
91267447Sjhb
92221828Sgrehan	if (conv_cap_1(val, cap_val_sf1_buf->buf, sizeof (cap_val_sf1_buf->buf),
93222105Sgrehan	    mach, fmt_flags, elfcap_sf1_to_str) == 0)
94264273Sjhb		return (conv_invalid_val(&cap_val_sf1_buf->inv_buf, val, 0));
95222105Sgrehan	return ((const char *)cap_val_sf1_buf->buf);
96243327Sgrehan}
97243327Sgrehan
98221828Sgrehanconst char *
99221828Sgrehanconv_cap_tag(Xword tag, Conv_inv_buf_t *inv_buf)
100221828Sgrehan{
101221828Sgrehan	static const Msg	tags[] = {
102221828Sgrehan		MSG_CA_SUNW_NULL,	MSG_CA_SUNW_HW_1,
103221828Sgrehan		MSG_CA_SUNW_SF_1
104221828Sgrehan	};
105221828Sgrehan
106221828Sgrehan	if (tag <= CA_SUNW_SF_1)
107256062Sgrehan		return (MSG_ORIG(tags[tag]));
108221828Sgrehan	else
109221828Sgrehan		return (conv_invalid_val(inv_buf, tag, 0));
110221828Sgrehan}
111221828Sgrehan
112221828Sgrehanconst char *
113256072Sneelconv_cap_val(Xword tag, Xword val, Half mach, Conv_cap_val_buf_t *cap_val_buf)
114221828Sgrehan{
115221828Sgrehan	if (tag == CA_SUNW_HW_1)
116221828Sgrehan		return (conv_cap_val_hw1(val, mach, 0,
117221828Sgrehan		    &cap_val_buf->cap_val_hw1_buf));
118221828Sgrehan	else if (tag == CA_SUNW_SF_1)
119221828Sgrehan		return (conv_cap_val_sf1(val, mach, 0,
120221828Sgrehan		    &cap_val_buf->cap_val_sf1_buf));
121221828Sgrehan	else
122221828Sgrehan		return (conv_invalid_val(&cap_val_buf->inv_buf, val, 0));
123221828Sgrehan}
124221828Sgrehan