1 /*
2  * Copyright (C) 2012 Samsung Electronics Co., LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 
19 #include "tlwvdrm_api.h"
20 #define LOG_TAG "drm_content_protect"
21 #include "log.h"
22 #include "tlc_communication.h"
23 #include "content_protect.h"
24 
25 mc_comm_ctx cp_ctx;
26 
27 // -------------------------------------------------------------
tlc_initialize(void)28 static mcResult_t tlc_initialize(void) {
29 	mcResult_t	mcRet;
30 
31 	memset(&cp_ctx, 0x00, sizeof(cp_ctx));
32 	cp_ctx.device_id	= MC_DEVICE_ID_DEFAULT;
33 	cp_ctx.uuid 	= (mcUuid_t)TL_WV_DRM_UUID;
34 	cp_ctx.initialized = false;
35 
36 	mcRet = tlc_open(&cp_ctx);
37 	if (MC_DRV_OK != mcRet) {
38 		   LOG_E("open TL session failed!");
39 		   return mcRet;
40 	}
41 
42 	cp_ctx.initialized = true;
43 
44 	return MC_DRV_OK;
45 }
46 
47 // -------------------------------------------------------------
tlc_terminate(void)48 static mcResult_t tlc_terminate(void) {
49 	mcResult_t mcRet;
50 
51 	if (cp_ctx.initialized == true) {
52 		mcRet = tlc_close(&cp_ctx);
53 		if (MC_DRV_OK != mcRet) {
54 			   LOG_E("close TL session failed!");
55 			   return mcRet;
56 		}
57 
58 		memset(&cp_ctx, 0x00, sizeof(cp_ctx));
59 		cp_ctx.initialized = false;
60 	}
61 
62 	return MC_DRV_OK;
63 }
64 
CP_Enable_Path_Protection(uint32_t protect_ip)65 extern "C" cpResult_t CP_Enable_Path_Protection(uint32_t protect_ip)
66 {
67 	cpResult_t cp_result = CP_SUCCESS;
68 	mcResult_t mcRet;
69 	tciMessage_t *tci = NULL;
70 
71 	LOG_I("[CONTENT_PROTECT] : CP_Enable_Path_Protection");
72 	do {
73 		// -------------------------------------------------------------
74 		// Step 1: Call the Trustlet Open function.
75 		mcRet = tlc_initialize();
76 		if (MC_DRV_OK != mcRet) {
77 			LOG_E("Tlc Open Error");
78 			cp_result = CP_ERROR_ENABLE_PATH_PROTECTION_FAILED;
79 			break;
80 		}
81 
82 		// -------------------------------------------------------------
83 		// Step 2: Check TCI buffer.
84 		tci = cp_ctx.tci_msg;
85 		if (NULL == tci) {
86 			LOG_E("TCI has not been set up properly - exiting");
87 			cp_result = CP_ERROR_ENABLE_PATH_PROTECTION_FAILED;
88 			break;
89 		}
90 
91 		// -------------------------------------------------------------
92 		// Step 3: Call the Trustlet functions
93 		// Step 3.1: Prepare command message in TCI
94 		tci->cmd.id = CMD_WV_DRM_ENABLE_PATH_PROTECTION;
95 		memcpy(tci->cmd.data, &protect_ip, sizeof(protect_ip));
96 		tci->cmd.data_len = sizeof(protect_ip);
97 
98 		// -------------------------------------------------------------
99 		// Step 3.2: Send Trustlet TCI Message
100 		mcRet = tlc_communicate(&cp_ctx);
101 		if (MC_DRV_OK != mcRet) {
102 			LOG_E("Tlc Communicate Error");
103 			cp_result = CP_ERROR_ENABLE_PATH_PROTECTION_FAILED;
104 			break;
105 		}
106 
107 		// -------------------------------------------------------------
108 		// Step 3.3: Verify that the Trustlet sent a response
109 		if ((RSP_ID(CMD_WV_DRM_ENABLE_PATH_PROTECTION) != tci->resp.id)) {
110 			LOG_E("Trustlet did not send a response: %d", tci->resp.id);
111 			cp_result = CP_ERROR_ENABLE_PATH_PROTECTION_FAILED;
112 			break;
113 		}
114 
115 		// -------------------------------------------------------------
116 		// Step 3.4: Check the Trustlet return code
117 		if (tci->resp.return_code != RET_TL_WV_DRM_OK) {
118 			LOG_E("Trustlet did not send a valid return code: %d", tci->resp.return_code);
119 			cp_result = CP_ERROR_ENABLE_PATH_PROTECTION_FAILED;
120 			break;
121 		}
122 	} while(0);
123 
124 	tlc_terminate();
125 	LOG_I("[CONTENT_PROTECT] : CP_Enable_Path_Protection. return value(%d)", cp_result);
126 	return cp_result;
127 }
128 
CP_Disable_Path_Protection(uint32_t protect_ip)129 extern "C" cpResult_t CP_Disable_Path_Protection(uint32_t protect_ip)
130 {
131 	cpResult_t cp_result = CP_SUCCESS;
132 	mcResult_t mcRet;
133 	tciMessage_t *tci = NULL;
134 
135 	LOG_I("[CONTENT_PROTECT] : CP_Disable_Path_Protection");
136 	do {
137 		// -------------------------------------------------------------
138 		// Step 1: Call the Trustlet Open function.
139 		mcRet = tlc_initialize();
140 		if (MC_DRV_OK != mcRet) {
141 			LOG_E("Tlc Open Error");
142 			cp_result = CP_ERROR_DISABLE_PATH_PROTECTION_FAILED;
143 			break;
144 		}
145 
146 		// -------------------------------------------------------------
147 		// Step 2: Check TCI buffer.
148 		tci = cp_ctx.tci_msg;
149 		if (NULL == tci) {
150 			LOG_E("TCI has not been set up properly - exiting");
151 			cp_result = CP_ERROR_DISABLE_PATH_PROTECTION_FAILED;
152 			break;
153 		}
154 
155 		// -------------------------------------------------------------
156 		// Step 3: Call the Trustlet functions
157 		// Step 3.1: Prepare command message in TCI
158 		tci->cmd.id = CMD_WV_DRM_DISABLE_PATH_PROTECTION;
159 		memcpy(tci->cmd.data, &protect_ip, sizeof(protect_ip));
160 		tci->cmd.data_len = sizeof(protect_ip);
161 
162 		// -------------------------------------------------------------
163 		// Step 3.2: Send Trustlet TCI Message
164 		mcRet = tlc_communicate(&cp_ctx);
165 		if (MC_DRV_OK != mcRet) {
166 			LOG_E("Tlc Communicate Error");
167 			cp_result = CP_ERROR_DISABLE_PATH_PROTECTION_FAILED;
168 			break;
169 		}
170 
171 		// -------------------------------------------------------------
172 		// Step 3.3: Verify that the Trustlet sent a response
173 		if ((RSP_ID(CMD_WV_DRM_DISABLE_PATH_PROTECTION) != tci->resp.id)) {
174 			LOG_E("Trustlet did not send a response: %d", tci->resp.id);
175 			cp_result = CP_ERROR_DISABLE_PATH_PROTECTION_FAILED;
176 			break;
177 		}
178 
179 		// -------------------------------------------------------------
180 		// Step 3.4: Check the Trustlet return code
181 		if (tci->resp.return_code != RET_TL_WV_DRM_OK) {
182 			LOG_E("Trustlet did not send a valid return code: %d", tci->resp.return_code);
183 			cp_result = CP_ERROR_DISABLE_PATH_PROTECTION_FAILED;
184 			break;
185 		}
186 	} while(0);
187 
188 	tlc_terminate();
189 	LOG_I("[CONTENT_PROTECT] : CP_Disable_Path_Protection. return value(%d)", cp_result);
190 	return cp_result;
191 }
192 
193