xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/cap.c (revision 1976:f0691a145b7e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211618Srie 
220Sstevel@tonic-gate /*
231618Srie  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * String conversion routine for hardware capabilities types.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate #include	<strings.h>
320Sstevel@tonic-gate #include	<stdio.h>
330Sstevel@tonic-gate #include	<sys/machelf.h>
340Sstevel@tonic-gate #include	<elfcap.h>
35*1976Sab196087 #include	"cap_msg.h"
360Sstevel@tonic-gate #include	"_conv.h"
370Sstevel@tonic-gate 
381618Srie static int
391618Srie conv_cap_1(Xword val, char *str, size_t len, Half mach,
400Sstevel@tonic-gate     int (*fptr)(uint64_t, char *, size_t, int, ushort_t))
410Sstevel@tonic-gate {
420Sstevel@tonic-gate 	size_t	_len;
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 	_len = sprintf(str, MSG_ORIG(MSG_GBL_OSQBRKT), EC_XWORD(val));
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	len -= _len;
470Sstevel@tonic-gate 	str += _len;
480Sstevel@tonic-gate 
491618Srie 	if ((*fptr)(val, str, len, CAP_FMT_DBLSPACE, mach) != 0)
501618Srie 		return (0);
510Sstevel@tonic-gate 
521618Srie 	_len = strlen(str);
531618Srie 	if ((len - _len) >= MSG_GBL_CSQBRKT_SIZE) {
541618Srie 		str += _len;
551618Srie 		(void) strcpy(str, MSG_ORIG(MSG_GBL_CSQBRKT));
560Sstevel@tonic-gate 	}
571618Srie 	return (1);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate 
601618Srie /*
611618Srie  * Establish a buffer size based on the maximum number of hardware capabilities
621618Srie  * that exist.  See common/elfcap.
631618Srie  */
641618Srie #define	HW1SZ	200
650Sstevel@tonic-gate 
660Sstevel@tonic-gate const char *
671618Srie conv_cap_val_hw1(Xword val, Half mach)
680Sstevel@tonic-gate {
691618Srie 	static char	string[HW1SZ];
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	if (val == 0)
720Sstevel@tonic-gate 		return (MSG_ORIG(MSG_GBL_ZERO));
730Sstevel@tonic-gate 
741618Srie 	if (conv_cap_1(val, string, HW1SZ, mach, hwcap_1_val2str) == 0)
751618Srie 		return (conv_invalid_val(string, HW1SZ, val, 0));
760Sstevel@tonic-gate 	return ((const char *)string);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
791618Srie /*
801618Srie  * Establish a buffer size based on the maximum number of software capabilities
811618Srie  * that exist.  See common/elfcap.
821618Srie  */
831618Srie #define	SF1SZ	50
840Sstevel@tonic-gate 
850Sstevel@tonic-gate const char *
861618Srie conv_cap_val_sf1(Xword val, Half mach)
870Sstevel@tonic-gate {
881618Srie 	static char	string[SF1SZ];
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	if (val == 0)
910Sstevel@tonic-gate 		return (MSG_ORIG(MSG_GBL_ZERO));
920Sstevel@tonic-gate 
931618Srie 	if (conv_cap_1(val, string, SF1SZ, mach, sfcap_1_val2str) == 0)
941618Srie 		return (conv_invalid_val(string, SF1SZ, val, 0));
950Sstevel@tonic-gate 	return ((const char *)string);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate const char *
991618Srie conv_cap_tag(Xword tag)
1000Sstevel@tonic-gate {
1011618Srie 	static char		string[CONV_INV_STRSIZE];
1021618Srie 	static const Msg	tags[] = {
1031618Srie 		MSG_CA_SUNW_NULL,	MSG_CA_SUNW_HW_1,
1041618Srie 		MSG_CA_SUNW_SF_1
1051618Srie 	};
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (tag <= CA_SUNW_SF_1)
1081618Srie 		return (MSG_ORIG(tags[tag]));
1090Sstevel@tonic-gate 	else
1101618Srie 		return (conv_invalid_val(string, CONV_INV_STRSIZE, tag, 0));
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate const char *
1141618Srie conv_cap_val(Xword tag, Xword val, Half mach)
1150Sstevel@tonic-gate {
1161618Srie 	static char	string[CONV_INV_STRSIZE];
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (tag == CA_SUNW_HW_1)
1191618Srie 		return (conv_cap_val_hw1(val, mach));
1200Sstevel@tonic-gate 	else if (tag == CA_SUNW_SF_1)
1211618Srie 		return (conv_cap_val_sf1(val, mach));
1220Sstevel@tonic-gate 	else
1231618Srie 		return (conv_invalid_val(string, CONV_INV_STRSIZE, val, 0));
1240Sstevel@tonic-gate }
125