nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1 | /****************************************************************************** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | * |
| 15 | * Copyright 2018 NXP |
| 16 | * |
| 17 | ******************************************************************************/ |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 18 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 19 | #include <android-base/stringprintf.h> |
| 20 | #include <base/logging.h> |
| 21 | #include "PowerSwitch.h" |
| 22 | #include "RoutingManager.h" |
| 23 | #include <nativehelper/ScopedPrimitiveArray.h> |
| 24 | #include <nativehelper/ScopedUtfChars.h> |
| 25 | #include <nativehelper/ScopedLocalRef.h> |
| 26 | #include "JavaClassConstants.h" |
| 27 | #include "NfcJniUtil.h" |
| 28 | #include "config.h" |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 29 | #include "SecureElement.h" |
Suraj Uday Kotharkar | a09a2b7 | 2019-04-17 15:07:44 +0530 | [diff] [blame^] | 30 | #include "NfcAdaptation.h" |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 31 | |
| 32 | using android::base::StringPrintf; |
| 33 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 34 | namespace android |
| 35 | { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 36 | static const int EE_ERROR_INIT = -3; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 37 | /******************************************************************************* |
| 38 | ** |
| 39 | ** Function: nativeNfcSecureElement_doOpenSecureElementConnection |
| 40 | ** |
| 41 | ** Description: Connect to the secure element. |
| 42 | ** e: JVM environment. |
| 43 | ** o: Java object. |
| 44 | ** |
| 45 | ** Returns: Handle of secure element. values < 0 represent failure. |
| 46 | ** |
| 47 | *******************************************************************************/ |
| 48 | static jint nativeNfcSecureElement_doOpenSecureElementConnection (JNIEnv*, jobject) |
| 49 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 50 | LOG(INFO) << StringPrintf("%s: Enter; ", __func__); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 51 | bool stat = false; |
hariprasad nalacheruvu | ba06cec | 2018-06-06 17:55:23 +0530 | [diff] [blame] | 52 | const int32_t recvBufferMaxSize = 1024; |
| 53 | uint8_t recvBuffer [recvBufferMaxSize]; |
| 54 | int32_t recvBufferActualSize = 0; |
| 55 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 56 | jint secElemHandle = EE_ERROR_INIT; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 57 | NFCSTATUS status = NFCSTATUS_FAILED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 58 | SecureElement &se = SecureElement::getInstance(); |
hariprasad nalacheruvu | f032814 | 2018-11-07 14:03:02 +0530 | [diff] [blame] | 59 | se.mModeSetNtfstatus = NFA_STATUS_FAILED; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 60 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 61 | /* Tell the controller to power up to get ready for sec elem operations */ |
| 62 | PowerSwitch::getInstance ().setLevel (PowerSwitch::FULL_POWER); |
| 63 | PowerSwitch::getInstance ().setModeOn (PowerSwitch::SE_CONNECTED); |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 64 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 65 | /* If controller is not routing AND there is no pipe connected, |
| 66 | then turn on the sec elem */ |
| 67 | stat = se.activate(SecureElement::ESE_ID); // It is to get the current activated handle. |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 68 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 69 | if((stat) && (nfcFL.eseFL._NCI_NFCEE_PWR_LINK_CMD)) |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 70 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 71 | status = se.setNfccPwrConfig(se.POWER_ALWAYS_ON|se.COMM_LINK_ACTIVE); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 72 | } |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 73 | if(status != NFA_STATUS_OK) |
| 74 | { |
hariprasad nalacheruvu | f032814 | 2018-11-07 14:03:02 +0530 | [diff] [blame] | 75 | LOG(INFO) << StringPrintf("%s: power link command failed", __func__); |
| 76 | stat =false; |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 77 | } |
| 78 | else |
| 79 | { |
nxpandroid | 82f7e9e | 2018-04-05 14:54:41 +0530 | [diff] [blame] | 80 | stat = se.SecEle_Modeset(se.NFCEE_ENABLE); |
hariprasad nalacheruvu | f032814 | 2018-11-07 14:03:02 +0530 | [diff] [blame] | 81 | if(se.mModeSetNtfstatus != NFA_STATUS_OK) |
| 82 | { |
| 83 | stat = false; |
| 84 | LOG(INFO) << StringPrintf("%s: Mode set ntf STATUS_FAILED", __func__); |
hariprasad nalacheruvu | f032814 | 2018-11-07 14:03:02 +0530 | [diff] [blame] | 85 | |
hariprasad nalacheruvu | 7254b03 | 2018-11-13 15:43:47 +0530 | [diff] [blame] | 86 | SyncEventGuard guard (se.mEERecoveryComplete); |
| 87 | { |
| 88 | se.mEERecoveryComplete.wait(); |
| 89 | LOG(INFO) << StringPrintf("%s: Recovery complete", __func__); |
| 90 | } |
| 91 | if(se.mErrorRecovery) |
| 92 | { |
| 93 | stat = true; |
| 94 | } |
| 95 | } |
hariprasad nalacheruvu | ba06cec | 2018-06-06 17:55:23 +0530 | [diff] [blame] | 96 | if(stat == true) |
| 97 | { |
hariprasad nalacheruvu | f032814 | 2018-11-07 14:03:02 +0530 | [diff] [blame] | 98 | se.mIsWiredModeOpen = true; |
Suraj Uday Kotharkar | 9e250fb | 2018-06-08 21:06:52 +0530 | [diff] [blame] | 99 | stat = se.apduGateReset(se.mActiveEeHandle, recvBuffer, &recvBufferActualSize); |
| 100 | if (stat) |
| 101 | { |
| 102 | secElemHandle = se.mActiveEeHandle; |
| 103 | } |
hariprasad nalacheruvu | ba06cec | 2018-06-06 17:55:23 +0530 | [diff] [blame] | 104 | } |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 105 | } |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 106 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 107 | /* if code fails to connect to the secure element, and nothing is active, then |
| 108 | * tell the controller to power down |
| 109 | */ |
| 110 | if ((!stat) && (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::SE_CONNECTED))) |
| 111 | { |
| 112 | LOG(INFO) << StringPrintf("%s: stat fails; ", __func__); |
| 113 | PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER); |
| 114 | se.deactivate(SecureElement::ESE_ID); |
| 115 | se.mIsWiredModeOpen = false; |
| 116 | stat = false; |
| 117 | } |
| 118 | LOG(INFO) << StringPrintf("%s: exit; return handle=0x%X", __func__, secElemHandle); |
| 119 | return secElemHandle; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
| 123 | /******************************************************************************* |
| 124 | ** |
| 125 | ** Function: nativeNfcSecureElement_doDisconnectSecureElementConnection |
| 126 | ** |
| 127 | ** Description: Disconnect from the secure element. |
| 128 | ** e: JVM environment. |
| 129 | ** o: Java object. |
| 130 | ** handle: Handle of secure element. |
| 131 | ** |
| 132 | ** Returns: True if ok. |
| 133 | ** |
| 134 | *******************************************************************************/ |
| 135 | static jboolean nativeNfcSecureElement_doDisconnectSecureElementConnection (JNIEnv*, jobject, jint handle) |
| 136 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 137 | LOG(INFO) << StringPrintf("%s: enter; handle=0x%04x", __func__, handle); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 138 | bool stat = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 139 | NFCSTATUS status = NFCSTATUS_FAILED; |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 140 | SecureElement &se = SecureElement::getInstance(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 141 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 142 | if(!se.mIsWiredModeOpen) |
| 143 | return false; |
Sreenivas | 88ee6aa | 2018-05-14 13:17:57 +0530 | [diff] [blame] | 144 | /* release any pending transceive wait */ |
| 145 | se.releasePendingTransceive(); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 146 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 147 | status = se.setNfccPwrConfig(se.POWER_ALWAYS_ON); |
| 148 | if(status != NFA_STATUS_OK) |
| 149 | { |
| 150 | LOG(INFO) << StringPrintf("%s: power link command failed", __func__); |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 151 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 152 | else |
| 153 | { |
Sreenivas | f83b876 | 2018-04-09 16:09:49 +0530 | [diff] [blame] | 154 | status = se.sendEvent(SecureElement::EVT_END_OF_APDU_TRANSFER); |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 155 | if(status == NFA_STATUS_OK) |
Sreenivas | f83b876 | 2018-04-09 16:09:49 +0530 | [diff] [blame] | 156 | stat = true; |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 157 | } |
Sreenivas | 88ee6aa | 2018-05-14 13:17:57 +0530 | [diff] [blame] | 158 | se.mIsWiredModeOpen = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 159 | |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 160 | /* if nothing is active after this, then tell the controller to power down */ |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 161 | if (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::SE_CONNECTED)) |
| 162 | PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER); |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 163 | LOG(INFO) << StringPrintf("%s: exit", __func__); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 164 | return stat ? JNI_TRUE : JNI_FALSE; |
| 165 | } |
| 166 | /******************************************************************************* |
| 167 | ** |
| 168 | ** Function: nativeNfcSecureElement_doResetSecureElement |
| 169 | ** |
| 170 | ** Description: Reset the secure element. |
| 171 | ** e: JVM environment. |
| 172 | ** o: Java object. |
| 173 | ** handle: Handle of secure element. |
| 174 | ** |
| 175 | ** Returns: True if ok. |
| 176 | ** |
| 177 | *******************************************************************************/ |
| 178 | static jboolean nativeNfcSecureElement_doResetSecureElement (JNIEnv*, jobject, jint handle) |
| 179 | { |
Suraj Uday Kotharkar | a09a2b7 | 2019-04-17 15:07:44 +0530 | [diff] [blame^] | 180 | bool stat = false; |
| 181 | int ret = -1; |
| 182 | NfcAdaptation& theInstance = NfcAdaptation::GetInstance(); |
| 183 | tHAL_NFC_ENTRY* halFuncEntries = theInstance.GetHalEntryFuncs (); |
| 184 | nfc_nci_IoctlInOutData_t inpOutData; |
| 185 | inpOutData.inp.level = NCI_ESE_HARD_RESET_IOCTL; |
| 186 | LOG(INFO) << StringPrintf("%s: Entry", __func__); |
| 187 | if(NULL == halFuncEntries) { |
| 188 | LOG(INFO) << StringPrintf("%s: halFuncEntries is NULL", __func__); |
| 189 | } else { |
| 190 | ret = halFuncEntries->ioctl(HAL_NFC_IOCTL_ESE_HARD_RESET, (void*)&inpOutData); |
| 191 | if(ret < 0) { |
| 192 | LOG(INFO) << StringPrintf("%s: IOCTL failed", __func__); |
| 193 | } else { |
| 194 | stat = true; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 195 | } |
Suraj Uday Kotharkar | a09a2b7 | 2019-04-17 15:07:44 +0530 | [diff] [blame^] | 196 | } |
| 197 | LOG(INFO) << StringPrintf("%s: exit", __func__); |
| 198 | return stat; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /******************************************************************************* |
| 202 | ** |
| 203 | ** Function: nativeNfcSecureElement_doGetAtr |
| 204 | ** |
| 205 | ** Description: GetAtr from the connected eSE. |
| 206 | ** e: JVM environment. |
| 207 | ** o: Java object. |
| 208 | ** handle: Handle of secure element. |
| 209 | ** |
| 210 | ** Returns: Buffer of received data. |
| 211 | ** |
| 212 | *******************************************************************************/ |
| 213 | static jbyteArray nativeNfcSecureElement_doGetAtr (JNIEnv* e, jobject, jint handle) |
| 214 | { |
| 215 | bool stat = false; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 216 | const int32_t recvBufferMaxSize = 1024; |
| 217 | uint8_t recvBuffer [recvBufferMaxSize]; |
| 218 | int32_t recvBufferActualSize = 0; |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 219 | LOG(INFO) << StringPrintf("%s: enter; handle=0x%04x", __func__, handle); |
| 220 | SecureElement &se = SecureElement::getInstance(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 221 | |
hariprasad nalacheruvu | ba06cec | 2018-06-06 17:55:23 +0530 | [diff] [blame] | 222 | stat = se.getAtr(recvBuffer, &recvBufferActualSize); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 223 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 224 | //copy results back to java |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 225 | jbyteArray result = e->NewByteArray(recvBufferActualSize); |
| 226 | if (result != NULL) { |
| 227 | e->SetByteArrayRegion(result, 0, recvBufferActualSize, (jbyte *) recvBuffer); |
| 228 | } |
| 229 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 230 | LOG(INFO) << StringPrintf("%s: exit: recv len=%d", __func__, recvBufferActualSize); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 231 | |
| 232 | return result; |
| 233 | } |
| 234 | |
| 235 | /******************************************************************************* |
| 236 | ** |
| 237 | ** Function: nativeNfcSecureElement_doTransceive |
| 238 | ** |
| 239 | ** Description: Send data to the secure element; retrieve response. |
| 240 | ** e: JVM environment. |
| 241 | ** o: Java object. |
| 242 | ** handle: Secure element's handle. |
| 243 | ** data: Data to send. |
| 244 | ** |
| 245 | ** Returns: Buffer of received data. |
| 246 | ** |
| 247 | *******************************************************************************/ |
| 248 | static jbyteArray nativeNfcSecureElement_doTransceive (JNIEnv* e, jobject, jint handle, jbyteArray data) |
| 249 | { |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 250 | const int32_t recvBufferMaxSize = 0x8800;//1024; 34k |
| 251 | uint8_t recvBuffer [recvBufferMaxSize]; |
| 252 | int32_t recvBufferActualSize = 0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 253 | ScopedByteArrayRW bytes(e, data); |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 254 | LOG(INFO) << StringPrintf("%s: enter; handle=0x%X; buf len=%zu", __func__, handle, bytes.size()); |
| 255 | |
| 256 | SecureElement &se = SecureElement::getInstance(); |
| 257 | if(!se.mIsWiredModeOpen) |
| 258 | return NULL; |
| 259 | |
Sreenivas | 426138e | 2018-04-04 14:56:08 +0530 | [diff] [blame] | 260 | se.transceive(reinterpret_cast<uint8_t*>(&bytes[0]), bytes.size(), recvBuffer, recvBufferMaxSize, recvBufferActualSize, se.SmbTransceiveTimeOutVal); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 261 | |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 262 | //copy results back to java |
| 263 | jbyteArray result = e->NewByteArray(recvBufferActualSize); |
| 264 | if (result != NULL) |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 265 | { |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 266 | e->SetByteArrayRegion(result, 0, recvBufferActualSize, (jbyte *) recvBuffer); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 267 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 268 | |
| 269 | LOG(INFO) << StringPrintf("%s: exit: recv len=%d", __func__, recvBufferActualSize); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 270 | return result; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /***************************************************************************** |
| 274 | ** |
| 275 | ** Description: JNI functions |
| 276 | ** |
| 277 | *****************************************************************************/ |
| 278 | static JNINativeMethod gMethods[] = |
| 279 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 280 | {"doNativeOpenSecureElementConnection", "()I", (void *) nativeNfcSecureElement_doOpenSecureElementConnection}, |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 281 | {"doNativeDisconnectSecureElementConnection", "(I)Z", (void *) nativeNfcSecureElement_doDisconnectSecureElementConnection}, |
| 282 | {"doNativeResetSecureElement", "(I)Z", (void *) nativeNfcSecureElement_doResetSecureElement}, |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 283 | {"doTransceive", "(I[B)[B", (void *) nativeNfcSecureElement_doTransceive}, |
| 284 | {"doNativeGetAtr", "(I)[B", (void *) nativeNfcSecureElement_doGetAtr}, |
| 285 | }; |
| 286 | |
| 287 | |
| 288 | /******************************************************************************* |
| 289 | ** |
| 290 | ** Function: register_com_android_nfc_NativeNfcSecureElement |
| 291 | ** |
| 292 | ** Description: Regisgter JNI functions with Java Virtual Machine. |
| 293 | ** e: Environment of JVM. |
| 294 | ** |
| 295 | ** Returns: Status of registration. |
| 296 | ** |
| 297 | *******************************************************************************/ |
| 298 | int register_com_android_nfc_NativeNfcSecureElement(JNIEnv *e) |
| 299 | { |
| 300 | return jniRegisterNativeMethods(e, gNativeNfcSecureElementClassName, |
| 301 | gMethods, NELEM(gMethods)); |
| 302 | } |
| 303 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 304 | } // namespace android |