1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 
27 #include "hdcp.h"
28 
29 void mod_hdcp_dump_binary_message(uint8_t *msg, uint32_t msg_size,
30 		uint8_t *buf, uint32_t buf_size)
31 {
32 	const uint8_t bytes_per_line = 16,
33 			byte_size = 3,
34 			newline_size = 1,
35 			terminator_size = 1;
36 	uint32_t line_count = msg_size / bytes_per_line,
37 			trailing_bytes = msg_size % bytes_per_line;
38 	uint32_t target_size = (byte_size * bytes_per_line + newline_size) * line_count +
39 			byte_size * trailing_bytes + newline_size + terminator_size;
40 	uint32_t buf_pos = 0;
41 	uint32_t i = 0;
42 
43 	if (buf_size >= target_size) {
44 		for (i = 0; i < msg_size; i++) {
45 			if (i % bytes_per_line == 0)
46 				buf[buf_pos++] = '\n';
47 			sprintf(&buf[buf_pos], "%02X ", msg[i]);
48 			buf_pos += byte_size;
49 		}
50 		buf[buf_pos++] = '\0';
51 	}
52 }
53 
54 char *mod_hdcp_status_to_str(int32_t status)
55 {
56 	switch (status) {
57 	case MOD_HDCP_STATUS_SUCCESS:
58 		return "MOD_HDCP_STATUS_SUCCESS";
59 	case MOD_HDCP_STATUS_FAILURE:
60 		return "MOD_HDCP_STATUS_FAILURE";
61 	case MOD_HDCP_STATUS_RESET_NEEDED:
62 		return "MOD_HDCP_STATUS_RESET_NEEDED";
63 	case MOD_HDCP_STATUS_DISPLAY_OUT_OF_BOUND:
64 		return "MOD_HDCP_STATUS_DISPLAY_OUT_OF_BOUND";
65 	case MOD_HDCP_STATUS_DISPLAY_NOT_FOUND:
66 		return "MOD_HDCP_STATUS_DISPLAY_NOT_FOUND";
67 	case MOD_HDCP_STATUS_INVALID_STATE:
68 		return "MOD_HDCP_STATUS_INVALID_STATE";
69 	case MOD_HDCP_STATUS_NOT_IMPLEMENTED:
70 		return "MOD_HDCP_STATUS_NOT_IMPLEMENTED";
71 	case MOD_HDCP_STATUS_INTERNAL_POLICY_FAILURE:
72 		return "MOD_HDCP_STATUS_INTERNAL_POLICY_FAILURE";
73 	case MOD_HDCP_STATUS_UPDATE_TOPOLOGY_FAILURE:
74 		return "MOD_HDCP_STATUS_UPDATE_TOPOLOGY_FAILURE";
75 	case MOD_HDCP_STATUS_CREATE_PSP_SERVICE_FAILURE:
76 		return "MOD_HDCP_STATUS_CREATE_PSP_SERVICE_FAILURE";
77 	case MOD_HDCP_STATUS_DESTROY_PSP_SERVICE_FAILURE:
78 		return "MOD_HDCP_STATUS_DESTROY_PSP_SERVICE_FAILURE";
79 	case MOD_HDCP_STATUS_HDCP1_CREATE_SESSION_FAILURE:
80 		return "MOD_HDCP_STATUS_HDCP1_CREATE_SESSION_FAILURE";
81 	case MOD_HDCP_STATUS_HDCP1_DESTROY_SESSION_FAILURE:
82 		return "MOD_HDCP_STATUS_HDCP1_DESTROY_SESSION_FAILURE";
83 	case MOD_HDCP_STATUS_HDCP1_VALIDATE_ENCRYPTION_FAILURE:
84 		return "MOD_HDCP_STATUS_HDCP1_VALIDATE_ENCRYPTION_FAILURE";
85 	case MOD_HDCP_STATUS_HDCP1_NOT_HDCP_REPEATER:
86 		return "MOD_HDCP_STATUS_HDCP1_NOT_HDCP_REPEATER";
87 	case MOD_HDCP_STATUS_HDCP1_NOT_CAPABLE:
88 		return "MOD_HDCP_STATUS_HDCP1_NOT_CAPABLE";
89 	case MOD_HDCP_STATUS_HDCP1_R0_PRIME_PENDING:
90 		return "MOD_HDCP_STATUS_HDCP1_R0_PRIME_PENDING";
91 	case MOD_HDCP_STATUS_HDCP1_VALIDATE_RX_FAILURE:
92 		return "MOD_HDCP_STATUS_HDCP1_VALIDATE_RX_FAILURE";
93 	case MOD_HDCP_STATUS_HDCP1_BKSV_REVOKED:
94 		return "MOD_HDCP_STATUS_HDCP1_BKSV_REVOKED";
95 	case MOD_HDCP_STATUS_HDCP1_KSV_LIST_NOT_READY:
96 		return "MOD_HDCP_STATUS_HDCP1_KSV_LIST_NOT_READY";
97 	case MOD_HDCP_STATUS_HDCP1_VALIDATE_KSV_LIST_FAILURE:
98 		return "MOD_HDCP_STATUS_HDCP1_VALIDATE_KSV_LIST_FAILURE";
99 	case MOD_HDCP_STATUS_HDCP1_KSV_LIST_REVOKED:
100 		return "MOD_HDCP_STATUS_HDCP1_KSV_LIST_REVOKED";
101 	case MOD_HDCP_STATUS_HDCP1_ENABLE_ENCRYPTION_FAILURE:
102 		return "MOD_HDCP_STATUS_HDCP1_ENABLE_ENCRYPTION_FAILURE";
103 	case MOD_HDCP_STATUS_HDCP1_ENABLE_STREAM_ENCRYPTION_FAILURE:
104 		return "MOD_HDCP_STATUS_HDCP1_ENABLE_STREAM_ENCRYPTION_FAILURE";
105 	case MOD_HDCP_STATUS_HDCP1_MAX_CASCADE_EXCEEDED_FAILURE:
106 		return "MOD_HDCP_STATUS_HDCP1_MAX_CASCADE_EXCEEDED_FAILURE";
107 	case MOD_HDCP_STATUS_HDCP1_MAX_DEVS_EXCEEDED_FAILURE:
108 		return "MOD_HDCP_STATUS_HDCP1_MAX_DEVS_EXCEEDED_FAILURE";
109 	case MOD_HDCP_STATUS_HDCP1_DEVICE_COUNT_MISMATCH_FAILURE:
110 		return "MOD_HDCP_STATUS_HDCP1_DEVICE_COUNT_MISMATCH_FAILURE";
111 	case MOD_HDCP_STATUS_HDCP1_LINK_INTEGRITY_FAILURE:
112 		return "MOD_HDCP_STATUS_HDCP1_LINK_INTEGRITY_FAILURE";
113 	case MOD_HDCP_STATUS_HDCP1_REAUTH_REQUEST_ISSUED:
114 		return "MOD_HDCP_STATUS_HDCP1_REAUTH_REQUEST_ISSUED";
115 	case MOD_HDCP_STATUS_HDCP1_LINK_MAINTENANCE_FAILURE:
116 		return "MOD_HDCP_STATUS_HDCP1_LINK_MAINTENANCE_FAILURE";
117 	case MOD_HDCP_STATUS_HDCP1_INVALID_BKSV:
118 		return "MOD_HDCP_STATUS_HDCP1_INVALID_BKSV";
119 	case MOD_HDCP_STATUS_DDC_FAILURE:
120 		return "MOD_HDCP_STATUS_DDC_FAILURE";
121 	case MOD_HDCP_STATUS_INVALID_OPERATION:
122 		return "MOD_HDCP_STATUS_INVALID_OPERATION";
123 	case MOD_HDCP_STATUS_HDCP2_NOT_CAPABLE:
124 		return "MOD_HDCP_STATUS_HDCP2_NOT_CAPABLE";
125 	case MOD_HDCP_STATUS_HDCP2_CREATE_SESSION_FAILURE:
126 		return "MOD_HDCP_STATUS_HDCP2_CREATE_SESSION_FAILURE";
127 	case MOD_HDCP_STATUS_HDCP2_DESTROY_SESSION_FAILURE:
128 		return "MOD_HDCP_STATUS_HDCP2_DESTROY_SESSION_FAILURE";
129 	case MOD_HDCP_STATUS_HDCP2_PREP_AKE_INIT_FAILURE:
130 		return "MOD_HDCP_STATUS_HDCP2_PREP_AKE_INIT_FAILURE";
131 	case MOD_HDCP_STATUS_HDCP2_AKE_CERT_PENDING:
132 		return "MOD_HDCP_STATUS_HDCP2_AKE_CERT_PENDING";
133 	case MOD_HDCP_STATUS_HDCP2_H_PRIME_PENDING:
134 		return "MOD_HDCP_STATUS_HDCP2_H_PRIME_PENDING";
135 	case MOD_HDCP_STATUS_HDCP2_PAIRING_INFO_PENDING:
136 		return "MOD_HDCP_STATUS_HDCP2_PAIRING_INFO_PENDING";
137 	case MOD_HDCP_STATUS_HDCP2_VALIDATE_AKE_CERT_FAILURE:
138 		return "MOD_HDCP_STATUS_HDCP2_VALIDATE_AKE_CERT_FAILURE";
139 	case MOD_HDCP_STATUS_HDCP2_AKE_CERT_REVOKED:
140 		return "MOD_HDCP_STATUS_HDCP2_AKE_CERT_REVOKED";
141 	case MOD_HDCP_STATUS_HDCP2_VALIDATE_H_PRIME_FAILURE:
142 		return "MOD_HDCP_STATUS_HDCP2_VALIDATE_H_PRIME_FAILURE";
143 	case MOD_HDCP_STATUS_HDCP2_VALIDATE_PAIRING_INFO_FAILURE:
144 		return "MOD_HDCP_STATUS_HDCP2_VALIDATE_PAIRING_INFO_FAILURE";
145 	case MOD_HDCP_STATUS_HDCP2_PREP_LC_INIT_FAILURE:
146 		return "MOD_HDCP_STATUS_HDCP2_PREP_LC_INIT_FAILURE";
147 	case MOD_HDCP_STATUS_HDCP2_L_PRIME_PENDING:
148 		return "MOD_HDCP_STATUS_HDCP2_L_PRIME_PENDING";
149 	case MOD_HDCP_STATUS_HDCP2_VALIDATE_L_PRIME_FAILURE:
150 		return "MOD_HDCP_STATUS_HDCP2_VALIDATE_L_PRIME_FAILURE";
151 	case MOD_HDCP_STATUS_HDCP2_PREP_EKS_FAILURE:
152 		return "MOD_HDCP_STATUS_HDCP2_PREP_EKS_FAILURE";
153 	case MOD_HDCP_STATUS_HDCP2_ENABLE_ENCRYPTION_FAILURE:
154 		return "MOD_HDCP_STATUS_HDCP2_ENABLE_ENCRYPTION_FAILURE";
155 	case MOD_HDCP_STATUS_HDCP2_VALIDATE_RX_ID_LIST_FAILURE:
156 		return "MOD_HDCP_STATUS_HDCP2_VALIDATE_RX_ID_LIST_FAILURE";
157 	case MOD_HDCP_STATUS_HDCP2_RX_ID_LIST_REVOKED:
158 		return "MOD_HDCP_STATUS_HDCP2_RX_ID_LIST_REVOKED";
159 	case MOD_HDCP_STATUS_HDCP2_RX_ID_LIST_NOT_READY:
160 		return "MOD_HDCP_STATUS_HDCP2_RX_ID_LIST_NOT_READY";
161 	case MOD_HDCP_STATUS_HDCP2_ENABLE_STREAM_ENCRYPTION_FAILURE:
162 		return "MOD_HDCP_STATUS_HDCP2_ENABLE_STREAM_ENCRYPTION_FAILURE";
163 	case MOD_HDCP_STATUS_HDCP2_STREAM_READY_PENDING:
164 		return "MOD_HDCP_STATUS_HDCP2_STREAM_READY_PENDING";
165 	case MOD_HDCP_STATUS_HDCP2_VALIDATE_STREAM_READY_FAILURE:
166 		return "MOD_HDCP_STATUS_HDCP2_VALIDATE_STREAM_READY_FAILURE";
167 	case MOD_HDCP_STATUS_HDCP2_PREPARE_STREAM_MANAGEMENT_FAILURE:
168 		return "MOD_HDCP_STATUS_HDCP2_PREPARE_STREAM_MANAGEMENT_FAILURE";
169 	case MOD_HDCP_STATUS_HDCP2_REAUTH_REQUEST:
170 		return "MOD_HDCP_STATUS_HDCP2_REAUTH_REQUEST";
171 	case MOD_HDCP_STATUS_HDCP2_REAUTH_LINK_INTEGRITY_FAILURE:
172 		return "MOD_HDCP_STATUS_HDCP2_REAUTH_LINK_INTEGRITY_FAILURE";
173 	case MOD_HDCP_STATUS_HDCP2_DEVICE_COUNT_MISMATCH_FAILURE:
174 		return "MOD_HDCP_STATUS_HDCP2_DEVICE_COUNT_MISMATCH_FAILURE";
175 	default:
176 		return "MOD_HDCP_STATUS_UNKNOWN";
177 	}
178 }
179 
180 char *mod_hdcp_state_id_to_str(int32_t id)
181 {
182 	switch (id) {
183 	case HDCP_UNINITIALIZED:
184 		return "HDCP_UNINITIALIZED";
185 	case HDCP_INITIALIZED:
186 		return "HDCP_INITIALIZED";
187 	case HDCP_CP_NOT_DESIRED:
188 		return "HDCP_CP_NOT_DESIRED";
189 	case H1_A0_WAIT_FOR_ACTIVE_RX:
190 		return "H1_A0_WAIT_FOR_ACTIVE_RX";
191 	case H1_A1_EXCHANGE_KSVS:
192 		return "H1_A1_EXCHANGE_KSVS";
193 	case H1_A2_COMPUTATIONS_A3_VALIDATE_RX_A6_TEST_FOR_REPEATER:
194 		return "H1_A2_COMPUTATIONS_A3_VALIDATE_RX_A6_TEST_FOR_REPEATER";
195 	case H1_A45_AUTHENTICATED:
196 		return "H1_A45_AUTHENTICATED";
197 	case H1_A8_WAIT_FOR_READY:
198 		return "H1_A8_WAIT_FOR_READY";
199 	case H1_A9_READ_KSV_LIST:
200 		return "H1_A9_READ_KSV_LIST";
201 	case D1_A0_DETERMINE_RX_HDCP_CAPABLE:
202 		return "D1_A0_DETERMINE_RX_HDCP_CAPABLE";
203 	case D1_A1_EXCHANGE_KSVS:
204 		return "D1_A1_EXCHANGE_KSVS";
205 	case D1_A23_WAIT_FOR_R0_PRIME:
206 		return "D1_A23_WAIT_FOR_R0_PRIME";
207 	case D1_A2_COMPUTATIONS_A3_VALIDATE_RX_A5_TEST_FOR_REPEATER:
208 		return "D1_A2_COMPUTATIONS_A3_VALIDATE_RX_A5_TEST_FOR_REPEATER";
209 	case D1_A4_AUTHENTICATED:
210 		return "D1_A4_AUTHENTICATED";
211 	case D1_A6_WAIT_FOR_READY:
212 		return "D1_A6_WAIT_FOR_READY";
213 	case D1_A7_READ_KSV_LIST:
214 		return "D1_A7_READ_KSV_LIST";
215 	case H2_A0_KNOWN_HDCP2_CAPABLE_RX:
216 		return "H2_A0_KNOWN_HDCP2_CAPABLE_RX";
217 	case H2_A1_SEND_AKE_INIT:
218 		return "H2_A1_SEND_AKE_INIT";
219 	case H2_A1_VALIDATE_AKE_CERT:
220 		return "H2_A1_VALIDATE_AKE_CERT";
221 	case H2_A1_SEND_NO_STORED_KM:
222 		return "H2_A1_SEND_NO_STORED_KM";
223 	case H2_A1_READ_H_PRIME:
224 		return "H2_A1_READ_H_PRIME";
225 	case H2_A1_READ_PAIRING_INFO_AND_VALIDATE_H_PRIME:
226 		return "H2_A1_READ_PAIRING_INFO_AND_VALIDATE_H_PRIME";
227 	case H2_A1_SEND_STORED_KM:
228 		return "H2_A1_SEND_STORED_KM";
229 	case H2_A1_VALIDATE_H_PRIME:
230 		return "H2_A1_VALIDATE_H_PRIME";
231 	case H2_A2_LOCALITY_CHECK:
232 		return "H2_A2_LOCALITY_CHECK";
233 	case H2_A3_EXCHANGE_KS_AND_TEST_FOR_REPEATER:
234 		return "H2_A3_EXCHANGE_KS_AND_TEST_FOR_REPEATER";
235 	case H2_ENABLE_ENCRYPTION:
236 		return "H2_ENABLE_ENCRYPTION";
237 	case H2_A5_AUTHENTICATED:
238 		return "H2_A5_AUTHENTICATED";
239 	case H2_A6_WAIT_FOR_RX_ID_LIST:
240 		return "H2_A6_WAIT_FOR_RX_ID_LIST";
241 	case H2_A78_VERIFY_RX_ID_LIST_AND_SEND_ACK:
242 		return "H2_A78_VERIFY_RX_ID_LIST_AND_SEND_ACK";
243 	case H2_A9_SEND_STREAM_MANAGEMENT:
244 		return "H2_A9_SEND_STREAM_MANAGEMENT";
245 	case H2_A9_VALIDATE_STREAM_READY:
246 		return "H2_A9_VALIDATE_STREAM_READY";
247 	case D2_A0_DETERMINE_RX_HDCP_CAPABLE:
248 		return "D2_A0_DETERMINE_RX_HDCP_CAPABLE";
249 	case D2_A1_SEND_AKE_INIT:
250 		return "D2_A1_SEND_AKE_INIT";
251 	case D2_A1_VALIDATE_AKE_CERT:
252 		return "D2_A1_VALIDATE_AKE_CERT";
253 	case D2_A1_SEND_NO_STORED_KM:
254 		return "D2_A1_SEND_NO_STORED_KM";
255 	case D2_A1_READ_H_PRIME:
256 		return "D2_A1_READ_H_PRIME";
257 	case D2_A1_READ_PAIRING_INFO_AND_VALIDATE_H_PRIME:
258 		return "D2_A1_READ_PAIRING_INFO_AND_VALIDATE_H_PRIME";
259 	case D2_A1_SEND_STORED_KM:
260 		return "D2_A1_SEND_STORED_KM";
261 	case D2_A1_VALIDATE_H_PRIME:
262 		return "D2_A1_VALIDATE_H_PRIME";
263 	case D2_A2_LOCALITY_CHECK:
264 		return "D2_A2_LOCALITY_CHECK";
265 	case D2_A34_EXCHANGE_KS_AND_TEST_FOR_REPEATER:
266 		return "D2_A34_EXCHANGE_KS_AND_TEST_FOR_REPEATER";
267 	case D2_SEND_CONTENT_STREAM_TYPE:
268 		return "D2_SEND_CONTENT_STREAM_TYPE";
269 	case D2_ENABLE_ENCRYPTION:
270 		return "D2_ENABLE_ENCRYPTION";
271 	case D2_A5_AUTHENTICATED:
272 		return "D2_A5_AUTHENTICATED";
273 	case D2_A6_WAIT_FOR_RX_ID_LIST:
274 		return "D2_A6_WAIT_FOR_RX_ID_LIST";
275 	case D2_A78_VERIFY_RX_ID_LIST_AND_SEND_ACK:
276 		return "D2_A78_VERIFY_RX_ID_LIST_AND_SEND_ACK";
277 	case D2_A9_SEND_STREAM_MANAGEMENT:
278 		return "D2_A9_SEND_STREAM_MANAGEMENT";
279 	case D2_A9_VALIDATE_STREAM_READY:
280 		return "D2_A9_VALIDATE_STREAM_READY";
281 	default:
282 		return "UNKNOWN_STATE_ID";
283 	}
284 }
285 
286