Commit | Line | Data |
---|---|---|
cd9434cc T |
1 | /* |
2 | * Copyright (c) 2013 TRUSTONIC LIMITED | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * 1. Redistributions of source code must retain the above copyright notice, | |
9 | * this list of conditions and the following disclaimer. | |
10 | * | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * | |
15 | * 3. Neither the name of the TRUSTONIC LIMITED nor the names of its | |
16 | * contributors may be used to endorse or promote products derived from | |
17 | * this software without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | |
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
26 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
27 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
28 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
29 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 | */ | |
31 | ||
32 | #include <string.h> | |
33 | #include <stdlib.h> | |
34 | #include <MobiCoreDriverApi.h> | |
35 | ||
36 | #include "tools.h" | |
37 | #include "logging.h" | |
38 | #include "pacmp3.h" | |
39 | #include "pacmtl.h" | |
40 | #include "registry.h" | |
41 | #include "trustletchannel.h" | |
42 | #include "contentmanager.h" | |
43 | ||
44 | static CMTHANDLE handle_=NULL; | |
45 | ||
46 | void closeCmtlSession() | |
47 | { | |
48 | tltChannelClose(handle_); | |
49 | handle_=NULL; | |
50 | } | |
51 | ||
52 | rootpaerror_t openCmtlSession() | |
53 | { | |
54 | mcResult_t error=0; | |
55 | rootpaerror_t ret=ROOTPA_OK; | |
56 | ||
57 | if(handle_) | |
58 | { | |
59 | closeCmtlSession(); | |
60 | } | |
61 | ||
62 | handle_=tltChannelOpen(sizeOfCmp(), &error); | |
63 | if(NULL==handle_) | |
64 | { | |
65 | if(MC_DRV_ERR_NO_FREE_MEMORY==error) | |
66 | { | |
67 | ret=ROOTPA_ERROR_OUT_OF_MEMORY; | |
68 | } | |
69 | else | |
70 | { | |
71 | ret=ROOTPA_ERROR_MOBICORE_CONNECTION; | |
72 | } | |
73 | } | |
74 | return ret; | |
75 | } | |
76 | ||
77 | rootpaerror_t executeOneCmpCommand(CMTHANDLE handle, CmpMessage* commandP, CmpMessage* responseP); | |
78 | ||
79 | rootpaerror_t executeContentManagementCommands(int numberOfCommands, CmpMessage* commandsP, CmpMessage* responsesP, uint32_t* internalError) | |
80 | { | |
81 | rootpaerror_t ret=ROOTPA_OK ; | |
82 | rootpaerror_t iRet=ROOTPA_OK ; | |
83 | bool selfOpened=false; | |
84 | CMTHANDLE handle; | |
85 | LOGD(">>executeContentManagementCommands"); | |
86 | ||
87 | *internalError=0; | |
88 | ||
89 | if(handle_==NULL) | |
90 | { | |
91 | // doProvisioining opens session earlier. Lock opens and closes session when called by client | |
92 | // this is for commands that do not require the client to call the lock. | |
93 | ||
94 | ret=openCmtlSession(); | |
95 | selfOpened=true; | |
96 | } | |
97 | handle=handle_; | |
98 | ||
99 | if (handle) | |
100 | { | |
101 | int i; | |
102 | for(i=0; i<numberOfCommands;i++) | |
103 | { | |
104 | responsesP[i].hdr.id=commandsP[i].hdr.id; // match the id; | |
105 | responsesP[i].hdr.ignoreError=commandsP[i].hdr.ignoreError; | |
106 | ||
107 | if(commandsP[i].length>0) | |
108 | { | |
109 | if(((iRet=executeOneCmpCommand(handle, &commandsP[i], &responsesP[i]))!=ROOTPA_OK)) | |
110 | { | |
111 | // returning actual error in case of the command failed | |
112 | ret=iRet; | |
113 | if(ROOTPA_OK==responsesP[i].hdr.ret) | |
114 | { | |
115 | responsesP[i].hdr.ret=ret; | |
116 | } | |
117 | ||
118 | if(commandsP[i].hdr.ignoreError==false) | |
119 | { | |
120 | LOGE("executeContentManagementCommands, ignoreError==false, returning %d", ret); | |
121 | return ret; | |
122 | } | |
123 | } | |
124 | } | |
125 | else | |
126 | { | |
127 | LOGE("executeContentManagementCommands, empty command"); | |
128 | } | |
129 | } | |
130 | ||
131 | if(ret!=ROOTPA_OK) | |
132 | { | |
133 | *internalError = handle->lasterror; | |
134 | } | |
135 | } | |
136 | else | |
137 | { | |
138 | LOGE("no handle %d", *internalError); | |
139 | if(MC_DRV_ERR_NO_FREE_MEMORY == *internalError) | |
140 | { | |
141 | ret=ROOTPA_ERROR_OUT_OF_MEMORY; | |
142 | } | |
143 | else | |
144 | { | |
145 | ret=ROOTPA_ERROR_MOBICORE_CONNECTION; | |
146 | } | |
147 | } | |
148 | ||
149 | if(selfOpened) | |
150 | { | |
151 | closeCmtlSession(); | |
152 | } | |
153 | ||
154 | LOGD("<<executeContentManagementCommands %d", ret); | |
155 | return ret; | |
156 | } | |
157 | ||
158 | /** | |
159 | */ | |
160 | rootpaerror_t executeOneCmpCommand(CMTHANDLE handle, CmpMessage* commandP, CmpMessage* responseP) | |
161 | { | |
162 | mcResult_t mcRet; | |
163 | cmpCommandId_t commandId; | |
164 | rootpaerror_t ret; | |
165 | uint32_t neededBytes; | |
166 | LOGD(">>executeOneCmpCommand"); | |
167 | if (unlikely( bad_write_ptr(handle,sizeof(CMTSTRUCT)))) | |
168 | { | |
169 | return ROOTPA_ERROR_INTERNAL; | |
170 | } | |
171 | if(unlikely(commandP->contentP==NULL || commandP->length< sizeof(cmpCommandId_t))) | |
172 | { | |
173 | return ROOTPA_ERROR_INTERNAL; | |
174 | } | |
175 | ||
176 | mcRet=MC_DRV_OK; | |
177 | commandId=getCmpCommandId(commandP->contentP); | |
178 | ||
179 | handle->mappedSize=getTotalMappedBufferSize(commandP); | |
180 | if(0==handle->mappedSize) | |
181 | { | |
182 | LOGE("<<executeOneCmpCommand, command %d not supported", commandId); | |
183 | return ROOTPA_COMMAND_NOT_SUPPORTED; | |
184 | } | |
185 | ||
186 | ret=ROOTPA_OK; | |
187 | while(true) | |
188 | { | |
189 | handle->mappedP=(uint8_t*)malloc((size_t) handle->mappedSize); | |
190 | if(NULL==handle->mappedP) | |
191 | { | |
192 | ret=ROOTPA_ERROR_OUT_OF_MEMORY; | |
193 | break; | |
194 | } | |
195 | memset(handle->mappedP, 0,handle->mappedSize); | |
196 | mcRet=mcMap(&handle->session, handle->mappedP, handle->mappedSize, &handle->mapInfo); | |
197 | if(mcRet!=MC_DRV_OK) | |
198 | { | |
199 | LOGE("executeOneCmpCommand not able to map memory %d", mcRet); | |
200 | ret=ROOTPA_ERROR_MOBICORE_CONNECTION; | |
201 | commandP->hdr.intRet=mcRet; | |
202 | responseP->hdr.intRet=mcRet; | |
203 | break; | |
204 | } | |
205 | ||
206 | if((ret = prepareCommand(commandId, commandP, handle, responseP))!=ROOTPA_OK) | |
207 | { | |
208 | LOGE("prepareCommand failed %d", ret); | |
209 | break; | |
210 | } | |
211 | ||
212 | if (unlikely( !tltChannelTransmit(handle, NOTIFICATION_WAIT_TIMEOUT_MS))) | |
213 | { | |
214 | ret=ROOTPA_ERROR_MOBICORE_CONNECTION; | |
215 | commandP->hdr.intRet=handle->lasterror; | |
216 | responseP->hdr.intRet=handle->lasterror; | |
217 | break; | |
218 | } | |
219 | ||
220 | neededBytes=getNeededBytesFromResponse(handle->wsmP); | |
221 | ||
222 | if(0==neededBytes) | |
223 | { | |
224 | break; | |
225 | } | |
226 | ||
227 | if((uint32_t)-1==neededBytes) | |
228 | { | |
229 | ret=ROOTPA_ERROR_MOBICORE_CONNECTION; | |
230 | break; | |
231 | } | |
232 | ||
233 | if(neededBytes <= handle->mappedSize) | |
234 | { | |
235 | LOGE("executeOneCmpCommand, there is something wrong. CMTL is requesting smaller buffer than we originally had. Command: %d, original %d requested %d", | |
236 | commandId, handle->mappedSize, neededBytes); | |
237 | ret=ROOTPA_ERROR_MOBICORE_CONNECTION; | |
238 | break; | |
239 | } | |
240 | ||
241 | // this is Info level LOGI on purpose | |
242 | LOGI("executeOneCmpCommand, updating RootPA recommended (%d bytes was not enough for %d response, allocating %d bytes and retrying)", handle->mappedSize, commandId, neededBytes); | |
243 | mcRet=mcUnmap(&handle->session, handle->mappedP, &handle->mapInfo); | |
244 | if(mcRet!=MC_DRV_OK) | |
245 | { | |
246 | LOGE("executeOneCmpCommand not able to free mapped memory %d", mcRet); | |
247 | ret=ROOTPA_ERROR_MOBICORE_CONNECTION; | |
248 | commandP->hdr.intRet=mcRet; | |
249 | responseP->hdr.intRet=mcRet; | |
250 | break; | |
251 | } | |
252 | ||
253 | free(handle->mappedP); | |
254 | memset(&handle->mapInfo, 0 , sizeof(handle->mapInfo)); | |
255 | handle->mappedSize=neededBytes; | |
256 | } | |
257 | ||
258 | if(ROOTPA_OK==ret) | |
259 | { | |
260 | ret=handleResponse(commandId, responseP, handle); | |
261 | } | |
262 | else | |
263 | { | |
264 | responseP->hdr.ret=ret; | |
265 | } | |
266 | LOGD("cleaning up mapped memory %ld",(long int) handle->mappedP); | |
267 | mcRet=mcUnmap(&handle->session, handle->mappedP, &handle->mapInfo); | |
268 | if(mcRet!=MC_DRV_OK) | |
269 | { | |
270 | LOGE("executeOneCmpCommand not able to free mapped memory %d", mcRet); | |
271 | ret=ROOTPA_ERROR_MOBICORE_CONNECTION; | |
272 | } | |
273 | LOGD("freeing mapped memory %ld", (long int) handle->mappedP); | |
274 | free(handle->mappedP); | |
275 | if(commandP->hdr.ret==ROOTPA_OK) commandP->hdr.ret=ret; | |
276 | if(responseP->hdr.ret==ROOTPA_OK) responseP->hdr.ret=ret; | |
277 | LOGD("<<executeOneCmpCommand %d %d",commandId, ret); | |
278 | return ret; | |
279 | } | |
280 | ||
281 | rootpaerror_t uploadSo(uint8_t* containerDataP, uint32_t containerLength, uint32_t* regRetP) | |
282 | { | |
283 | *regRetP = regWriteAuthToken((AUTHTOKENCONTAINERP) containerDataP, containerLength); | |
284 | if( *regRetP != MC_DRV_OK) | |
285 | { | |
286 | LOGE("uploadSo regWriteAuthToken failed %d", *regRetP); | |
287 | return ROOTPA_ERROR_REGISTRY; | |
288 | } | |
289 | return ROOTPA_OK; | |
290 | } |