xref: /OK3568_Linux_fs/external/security/librkcrypto/src/rkcrypto_trace.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (c) 2022 Rockchip Electronics Co. Ltd.
3  */
4 #include <stdarg.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include "rkcrypto_trace.h"
9 
10 #ifdef ANDROID
11 #include <android/log.h>
12 #include <cutils/properties.h>
13 
14 #define RKCRYPTO_TRACE_LEVEL_NAME	"vendor.rkcrypto.trace.level"
15 #else
16 #define RKCRYPTO_TRACE_LEVEL_NAME	"rkcrypto_trace_level"
17 #endif
18 
19 #define RKCRYPTO_LOG_TAG		"rkcrypto"
20 
21 static int trace_level = -1;
22 
rkcrypto_get_trace_level(void)23 static void rkcrypto_get_trace_level(void)
24 {
25 	char pick_value;
26 
27 	if (trace_level >= TRACE_ERROR && trace_level <= TRACE_VERBOSE)
28 		return;
29 
30 #ifdef ANDROID
31 	char property_value[PROPERTY_VALUE_MAX] = {0};
32 
33 	if (property_get(RKCRYPTO_TRACE_LEVEL_NAME, property_value, NULL) > 0)
34 		pick_value = property_value[0];
35 #else
36 	char *env_value = getenv(RKCRYPTO_TRACE_LEVEL_NAME);
37 	if (env_value)
38 		pick_value = env_value[0];
39 #endif
40 	sscanf(&pick_value, "%d", &trace_level);
41 
42 	if (trace_level < TRACE_ERROR || trace_level > TRACE_VERBOSE)
43 		trace_level = TRACE_INFO;
44 }
45 
rkcrypto_set_trace_level(enum RKCRYPTO_TRACE_LEVEL level)46 RK_RES rkcrypto_set_trace_level(enum RKCRYPTO_TRACE_LEVEL level)
47 {
48 	char ch[2];
49 
50 	if (level < TRACE_ERROR || level > TRACE_VERBOSE)
51 		return RK_CRYPTO_ERR_PARAMETER;
52 
53 	sprintf(ch, "%d", level);
54 
55 #ifdef ANDROID
56 	if (property_set(RKCRYPTO_TRACE_LEVEL_NAME, ch)) {
57 		__android_log_print(ANDROID_LOG_ERROR, RKCRYPTO_LOG_TAG, "property_set error!");
58 		return RK_CRYPTO_ERR_GENERIC;
59 	}
60 #else
61 	if (setenv(RKCRYPTO_TRACE_LEVEL_NAME, ch, 1)) {
62 		printf("setenv error!");
63 		return RK_CRYPTO_ERR_GENERIC;
64 	}
65 #endif
66 	trace_level = -1;
67 	return RK_CRYPTO_SUCCESS;
68 }
69 
trace_printf(int level,const char * function,int line,const char * fmt,...)70 void trace_printf(int level, const char *function, int line, const char *fmt, ...)
71 {
72 	rkcrypto_get_trace_level();
73 
74 	if (level > trace_level) {
75 		return;
76 	} else {
77 		int i;
78 		char buffer[512];
79 		va_list args;
80 
81 		va_start(args, fmt);
82 
83 #ifdef ANDROID
84 		int android_prio = ANDROID_LOG_INFO;
85 		char head[100];
86 
87 		switch (level) {
88 		case TRACE_ERROR:
89 			android_prio = ANDROID_LOG_ERROR;
90 			break;
91 		case TRACE_INFO:
92 			android_prio = ANDROID_LOG_INFO;
93 			break;
94 		case TRACE_DEBUG:
95 			android_prio = ANDROID_LOG_DEBUG;
96 			break;
97 		case TRACE_VERBOSE:
98 			android_prio = ANDROID_LOG_VERBOSE;
99 			break;
100 		default:
101 			break;
102 		}
103 
104 		sprintf(head, "%s: [%s, %d]", RKCRYPTO_LOG_TAG, function, line);
105 		vsprintf(buffer, fmt, args);
106 
107 		if (buffer[0] != '\0') {
108 			for (i = strlen(buffer) - 1; i >= 0; i--) {
109 				if (buffer[i] != '\n')
110 					break;
111 			}
112 
113 			buffer[i + 1] = '\0';
114 		}
115 
116 		__android_log_print(android_prio, head, "%s", buffer);
117 #else
118 		char linux_prio = 'I';
119 
120 		switch (level) {
121 		case TRACE_ERROR:
122 			linux_prio = 'E';
123 			break;
124 		case TRACE_INFO:
125 			linux_prio = 'I';
126 			break;
127 		case TRACE_DEBUG:
128 			linux_prio = 'D';
129 			break;
130 		case TRACE_VERBOSE:
131 			linux_prio = 'V';
132 			break;
133 		default:
134 			break;
135 		}
136 
137 		printf("%c %s: [%s, %d]: ", linux_prio, RKCRYPTO_LOG_TAG, function, line);
138 		vsprintf(buffer, fmt, args);
139 
140 		if (buffer[0] != '\0') {
141 			for (i = strlen(buffer) - 1; i >= 0; i--) {
142 				if (buffer[i] != '\n')
143 					break;
144 			}
145 
146 			buffer[i + 1] = '\0';
147 		}
148 
149 		printf("%s\n", buffer);
150 #endif
151 
152 		va_end(args);
153 	}
154 }
155 
hex_dump(int level,const char * function,int line,const char * buffer_name,const void * buffer,int len)156 void hex_dump(int level, const char *function, int line, const char *buffer_name,
157 	      const void *buffer, int len)
158 {
159 	trace_printf(level, function, line, "%s(%d): ", buffer_name, len);
160 
161 	if (level > trace_level) {
162 		return;
163 	} else {
164 		int i, j, line_count;
165 		char ch[4], tmp[64];
166 		char *in = (char *)buffer;
167 
168 		line_count = len / 16;
169 		line_count = len % 16 ? line_count + 1 : line_count;
170 
171 		for (i = 0; i < line_count; i++) {
172 			memset(tmp, 0, sizeof(tmp));
173 			for (j = 16 * i; j < 16 * (i + 1); j++) {
174 				if (j < len)
175 					sprintf(ch, "%02x ", in[j]);
176 				else
177 					sprintf(ch, "-- ");
178 
179 				strcat(tmp, ch);
180 			}
181 
182 #ifdef ANDROID
183 			__android_log_print(ANDROID_LOG_VERBOSE, RKCRYPTO_LOG_TAG, "%s", tmp);
184 			if (i % 16 == 0 && i != 0)
185 				__android_log_print(ANDROID_LOG_VERBOSE, RKCRYPTO_LOG_TAG, "\n");
186 
187 #else
188 			printf("V %s: %s\n", RKCRYPTO_LOG_TAG, tmp);
189 			if (i % 16 == 0 && i != 0)
190 				printf("V %s: \n", RKCRYPTO_LOG_TAG);
191 #endif
192 		}
193 	}
194 }
195