• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2018-2022 NXP
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 #include "NxpEse.h"
19 #ifdef NXP_BOOTTIME_UPDATE
20 #include "eSEClient.h"
21 #endif
22 #include <android-base/logging.h>
23 #include <android-base/stringprintf.h>
24 #include <memunreachable/memunreachable.h>
25 
26 #include "phNxpEse_Api.h"
27 
28 namespace vendor {
29 namespace nxp {
30 namespace nxpese {
31 namespace V1_0 {
32 namespace implementation {
33 using android::base::StringPrintf;
34 // using android::hardware::secure_element::V1_0::implementation::SecureElement;
35 static android::sp<ISecureElementHalCallback> seCallback;
36 static android::sp<
37     ::android::hardware::secure_element::V1_1::ISecureElementHalCallback>
38     seCallback_1_1;
39 static android::sp<ISecureElementHalCallback> virtualISOCallback;
40 static android::sp<
41     ::android::hardware::secure_element::V1_1::ISecureElementHalCallback>
42     virtualISOCallback_1_1;
43 bool isSeHalV1_1 = false;
44 // Methods from ::vendor::nxp::nxpese::V1_0::INxpEse follow.
setSeCallBack(const android::sp<ISecureElementHalCallback> & clientCallback)45 Return<void> NxpEse::setSeCallBack(
46     const android::sp<ISecureElementHalCallback>& clientCallback) {
47   seCallback = clientCallback;
48   return Void();
49 }
50 
setSeCallBack_1_1(const sp<::android::hardware::secure_element::V1_1::ISecureElementHalCallback> & clientCallback)51 Return<void> NxpEse::setSeCallBack_1_1(
52     const sp<
53         ::android::hardware::secure_element::V1_1::ISecureElementHalCallback>&
54         clientCallback) {
55   seCallback_1_1 = clientCallback;
56   isSeHalV1_1 = true;
57   return Void();
58 }
59 
setVirtualISOCallBack(const android::sp<ISecureElementHalCallback> & clientCallback)60 Return<void> NxpEse::setVirtualISOCallBack(
61     const android::sp<ISecureElementHalCallback>& clientCallback) {
62   virtualISOCallback = clientCallback;
63   return Void();
64 }
65 
setVirtualISOCallBack_1_1(const android::sp<::android::hardware::secure_element::V1_1::ISecureElementHalCallback> & clientCallback)66 Return<void> NxpEse::setVirtualISOCallBack_1_1(
67     const android::sp<
68         ::android::hardware::secure_element::V1_1::ISecureElementHalCallback>&
69         clientCallback) {
70   virtualISOCallback_1_1 = clientCallback;
71   isSeHalV1_1 = true;
72   return Void();
73 }
initSEService()74 void NxpEse::initSEService() {
75   ESESTATUS status = ESESTATUS_SUCCESS;
76   ESESTATUS deInitStatus = ESESTATUS_SUCCESS;
77   phNxpEse_initParams initParams;
78   memset(&initParams, 0x00, sizeof(phNxpEse_initParams));
79   initParams.initMode = ESE_MODE_NORMAL;
80   initParams.mediaType = ESE_PROTOCOL_MEDIA_SPI_APDU_GATE;
81 
82   if (!seCallback && !isSeHalV1_1) return;
83 
84   if (!seCallback_1_1 && isSeHalV1_1) return;
85 
86   status = phNxpEse_open(initParams);
87   if (status != ESESTATUS_SUCCESS) {
88     goto exit;
89   }
90 
91   status = phNxpEse_SetEndPoint_Cntxt(0);
92   if (status != ESESTATUS_SUCCESS) {
93     goto exit1;
94   }
95   status = phNxpEse_init(initParams);
96   if (status != ESESTATUS_SUCCESS) {
97     goto exit1;
98   }
99   status = phNxpEse_ResetEndPoint_Cntxt(0);
100   if (status != ESESTATUS_SUCCESS) {
101     goto exit2;
102   }
103 
104   LOG(INFO) << "ESE SPI init complete !!!";
105 exit2:
106   deInitStatus = phNxpEse_deInit();
107 exit1:
108   status = phNxpEse_close(deInitStatus);
109 exit:
110   if (status == ESESTATUS_SUCCESS) {
111     if (isSeHalV1_1)
112       seCallback_1_1->onStateChange_1_1(true, "NXP SE HAL init ok");
113     else
114       seCallback->onStateChange(true);
115   } else {
116     LOG(ERROR) << "eSE-Hal Init failed";
117     if (isSeHalV1_1)
118       seCallback_1_1->onStateChange_1_1(false, "NXP SE HAL init not ok");
119     else
120       seCallback->onStateChange(false);
121   }
122 }
123 
initVIrtualISOService()124 void NxpEse::initVIrtualISOService() {
125   ESESTATUS status = ESESTATUS_SUCCESS;
126   phNxpEse_initParams initParams;
127   ESESTATUS deInitStatus = ESESTATUS_SUCCESS;
128   memset(&initParams, 0x00, sizeof(phNxpEse_initParams));
129   initParams.initMode = ESE_MODE_NORMAL;
130   initParams.mediaType = ESE_PROTOCOL_MEDIA_SPI_APDU_GATE;
131 
132   if (!virtualISOCallback && !isSeHalV1_1) return;
133 
134   if (!virtualISOCallback_1_1 && isSeHalV1_1) return;
135 
136   status = phNxpEse_SetEndPoint_Cntxt(1);
137   if (status != ESESTATUS_SUCCESS) {
138     goto exit1;
139   }
140   status = phNxpEse_init(initParams);
141   if (status != ESESTATUS_SUCCESS) {
142     goto exit1;
143   }
144   status = phNxpEse_ResetEndPoint_Cntxt(1);
145   if (status != ESESTATUS_SUCCESS) {
146     goto exit2;
147   }
148 
149   LOG(INFO) << "ESE SPI init complete !!!";
150 exit2:
151   deInitStatus = phNxpEse_deInit();
152 exit1:
153   status = phNxpEse_close(deInitStatus);
154 
155   if (status == ESESTATUS_SUCCESS) {
156     if (isSeHalV1_1)
157       virtualISOCallback_1_1->onStateChange_1_1(true, "NXP SE HAL init ok");
158     else
159       virtualISOCallback->onStateChange(true);
160   } else {
161     LOG(ERROR) << "eSE-Hal Init failed";
162     if (isSeHalV1_1)
163       virtualISOCallback_1_1->onStateChange_1_1(false,
164                                                 "NXP SE HAL init not ok");
165     else
166       virtualISOCallback->onStateChange(false);
167   }
168 }
169 #ifdef NXP_BOOTTIME_UPDATE
ioctlHandler(uint64_t ioctlType,ese_nxp_IoctlInOutData_t & inpOutData)170 Return<void> NxpEse::ioctlHandler(uint64_t ioctlType,
171                                   ese_nxp_IoctlInOutData_t& inpOutData) {
172   switch (ioctlType) {
173     case HAL_ESE_IOCTL_NFC_JCOP_DWNLD: {
174       // nfc_nci_IoctlInOutData_t* inpOutData =
175       // (nfc_nci_IoctlInOutData_t*)inpOutData;
176       int update_state = inpOutData.inp.data.nxpCmd.p_cmd[0];
177       if (update_state == ESE_JCOP_UPDATE_COMPLETED ||
178           update_state == ESE_LS_UPDATE_COMPLETED) {
179         seteSEClientState(update_state);
180         eSEClientUpdate_SE_Thread();
181       }
182     } break;
183   }
184   return Void();
185 }
186 #endif
187 
ioctl(uint64_t ioctlType,const hidl_vec<uint8_t> & inOutData,ioctl_cb _hidl_cb)188 Return<void> NxpEse::ioctl(uint64_t ioctlType,
189                            const hidl_vec<uint8_t>& inOutData,
190                            ioctl_cb _hidl_cb) {
191   ese_nxp_IoctlInOutData_t inpOutData;
192   ese_nxp_IoctlInOutData_t* pInOutData =
193       (ese_nxp_IoctlInOutData_t*)&inOutData[0];
194 
195   /*data from proxy->stub is copied to local data which can be updated by
196    * underlying HAL implementation since it's an inout argument*/
197   memcpy(&inpOutData, pInOutData, sizeof(ese_nxp_IoctlInOutData_t));
198   ESESTATUS status = phNxpEse_spiIoctl(ioctlType, &inpOutData);
199 #ifdef NXP_BOOTTIME_UPDATE
200   ioctlHandler(ioctlType, inpOutData);
201 #endif
202   /*copy data and additional fields indicating status of ioctl operation
203    * and context of the caller. Then invoke the corresponding proxy callback*/
204   inpOutData.out.ioctlType = ioctlType;
205   inpOutData.out.result = status;
206 #ifdef NXP_BOOTTIME_UPDATE
207   if (ioctlType == HAL_ESE_IOCTL_GET_ESE_UPDATE_STATE) {
208     inpOutData.out.data.status =
209         (getJcopUpdateRequired() | (getLsUpdateRequired() << 8));
210   }
211 #endif
212   hidl_vec<uint8_t> outputData;
213   outputData.setToExternal((uint8_t*)&inpOutData.out,
214                            sizeof(ese_nxp_ExtnOutputData_t));
215   LOG(ERROR) << "GET ESE update state2 = " << inpOutData.out.data.status;
216   _hidl_cb(outputData);
217   return Void();
218 }
219 
220 // Methods from ::android::hidl::base::V1_0::IBase follow.
debug(const hidl_handle &,const hidl_vec<hidl_string> &)221 Return<void> NxpEse::debug(const hidl_handle& /* fd */,
222                            const hidl_vec<hidl_string>& /* options */) {
223   LOG(INFO) << "\n SecureElement-NxpEse HAL MemoryLeak Info = \n"
224             << ::android::GetUnreachableMemoryString(true, 10000).c_str();
225   return Void();
226 }
227 
228 }  // namespace implementation
229 }  // namespace V1_0
230 }  // namespace nxpese
231 }  // namespace nxp
232 }  // namespace vendor
233