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