Snap for 7110675 from 855e5258c3e0bdbf228cff4adf2888fc32442dc7 to sdk-release

Change-Id: I90645728fd1934157c876f0a962ded0ee8df297a
diff --git a/secure_element/1.0/GtoService.cpp b/secure_element/1.0/GtoService.cpp
index 623cb16..9de5db6 100644
--- a/secure_element/1.0/GtoService.cpp
+++ b/secure_element/1.0/GtoService.cpp
@@ -4,7 +4,7 @@
  * This copy is licensed under the Apache License, Version 2.0 (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
- *     http://www.apache.org/licenses/LICENSE-2.0 or https://www.apache.org/licenses/LICENSE-2.0.html 
+ *     http://www.apache.org/licenses/LICENSE-2.0 or https://www.apache.org/licenses/LICENSE-2.0.html
  *
  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and limitations under the License.
@@ -26,8 +26,8 @@
 using android::status_t;
 
 int main() {
-  ALOGD("Gemalto Secure Element HAL Service 1.0.1 is starting. libse-gto v1.12");
-  sp<ISecureElement> se_service = new SecureElement();
+  ALOGD("Thales Secure Element HAL for eSE1 Service 1.3.0 is starting. libse-gto v1.12");
+  sp<ISecureElement> se_service = new SecureElement("eSE1");
   configureRpcThreadpool(1, true);
   status_t status = se_service->registerAsService("eSE1");
   if (status != OK) {
@@ -36,7 +36,7 @@
         status);
     return -1;
   }
-  
+
   ALOGD("Gemalto Secure Element Service is ready");
   joinRpcThreadpool();
   return 1;
diff --git a/secure_element/1.0/GtoService_ese2.cpp b/secure_element/1.0/GtoService_ese2.cpp
new file mode 100644
index 0000000..dac6ef7
--- /dev/null
+++ b/secure_element/1.0/GtoService_ese2.cpp
@@ -0,0 +1,43 @@
+/*****************************************************************************
+ * Copyright ©2017-2019 Gemalto – a Thales Company. All rights Reserved.
+ *
+ * This copy is licensed under the Apache License, Version 2.0 (the "License");
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *     http://www.apache.org/licenses/LICENSE-2.0 or https://www.apache.org/licenses/LICENSE-2.0.html
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and limitations under the License.
+
+ ****************************************************************************/
+#include <android/hardware/secure_element/1.0/ISecureElement.h>
+#include <hidl/LegacySupport.h>
+#include <log/log.h>
+
+#include "SecureElement.h"
+
+// Generated HIDL files
+using android::hardware::secure_element::V1_0::ISecureElement;
+using android::hardware::secure_element::V1_0::implementation::SecureElement;
+using android::hardware::configureRpcThreadpool;
+using android::hardware::joinRpcThreadpool;
+using android::OK;
+using android::sp;
+using android::status_t;
+
+int main() {
+  ALOGD("Thales Secure Element HAL for eSE2 Service 1.3.0 is starting. libse-gto v1.12");
+  sp<ISecureElement> se_service = new SecureElement("eSE2");
+  configureRpcThreadpool(1, true);
+  status_t status = se_service->registerAsService("eSE2");
+  if (status != OK) {
+    LOG_ALWAYS_FATAL(
+        "registerAsService (%d).",
+        status);
+    return -1;
+  }
+
+  ALOGD("Gemalto Secure Element Service is ready");
+  joinRpcThreadpool();
+  return 1;
+}
diff --git a/secure_element/1.0/SecureElement.cpp b/secure_element/1.0/SecureElement.cpp
index 8913e28..6ec16f0 100644
--- a/secure_element/1.0/SecureElement.cpp
+++ b/secure_element/1.0/SecureElement.cpp
@@ -49,12 +49,18 @@
 
 static struct se_gto_ctx *ctx;
 
-SecureElement::SecureElement(){
+SecureElement::SecureElement(const char* ese_name){
     nbrOpenChannel = 0;
     ctx = NULL;
+
+    if (strcmp(ese_name, "eSE2") == 0) {
+        strcpy( config_filename, "/vendor/etc/libse-gto-hal2.conf");
+    } else {
+        strcpy( config_filename, "/vendor/etc/libse-gto-hal.conf");
+    }
 }
 
-void SecureElement::resetSE(){
+int SecureElement::resetSE(){
     int n;
 
     isBasicChannelOpen = false;
@@ -69,6 +75,8 @@
     } else {
         ALOGE("SecureElement:%s Failed to reset and get ATR: %s\n", __func__, strerror(errno));
     }
+
+    return n;
 }
 
 sp<V1_0::ISecureElementHalCallback> SecureElement::internalClientCallback = nullptr;
@@ -86,7 +94,7 @@
     }
     //settings = default_settings(ctx);
     se_gto_set_log_level(ctx, 4);
-    
+
     openConfigFile(1);
 
     if (se_gto_open(ctx) < 0) {
@@ -94,7 +102,9 @@
         return EXIT_FAILURE;
     }
 
-    resetSE();
+    if (resetSE() < 0) {
+        return EXIT_FAILURE;
+    }
 
     checkSeUp = true;
     turnOffSE = false;
@@ -123,7 +133,6 @@
     if (initializeSE() != EXIT_SUCCESS) {
         ALOGE("SecureElement:%s initializeSE Failed", __func__);
         clientCallback->onStateChange(false);
-        return Void();
     }
 
     if (deinitializeSE() != SecureElementStatus::SUCCESS) {
@@ -578,24 +587,24 @@
 {
     int   r;
     FILE *f;
-    char filename[] = "/vendor/etc/libse-gto-hal.conf";
+
 
     /* filename is not NULL */
-    ALOGD("SecureElement:%s Open Config file : %s", __func__, filename);
-    f = fopen(filename, "r");
+    ALOGD("SecureElement:%s Open Config file : %s", __func__, config_filename);
+    f = fopen(config_filename, "r");
     if (f) {
         r = parseConfigFile(f, verbose);
         if (r == -1) {
-            perror(filename);
-            ALOGE("SecureElement:%s Error parse %s Failed", __func__, filename);
+            perror(config_filename);
+            ALOGE("SecureElement:%s Error parse %s Failed", __func__, config_filename);
         }
         if (fclose(f) != 0) {
             r = -1;
-            ALOGE("SecureElement:%s Error close %s Failed", __func__, filename);
+            ALOGE("SecureElement:%s Error close %s Failed", __func__, config_filename);
         }
     } else {
         r = -1;
-        ALOGE("SecureElement:%s Error open %s Failed", __func__, filename);
+        ALOGE("SecureElement:%s Error open %s Failed", __func__, config_filename);
     }
     return r;
 }
diff --git a/secure_element/1.0/SecureElement.h b/secure_element/1.0/SecureElement.h
index e63c3a8..69e308e 100644
--- a/secure_element/1.0/SecureElement.h
+++ b/secure_element/1.0/SecureElement.h
@@ -34,7 +34,7 @@
 
 struct SecureElement : public ISecureElement , public hidl_death_recipient {
     // Methods from ::android::hardware::secure_element::V1_0::ISecureElement follow.
-    SecureElement();
+    SecureElement(const char* ese_name);
     Return<void> init(const sp<V1_0::ISecureElementHalCallback>& clientCallback) override;
     Return<void> openLogicalChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, openLogicalChannel_cb _hidl_cb) override;
     Return<void> openBasicChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, openBasicChannel_cb _hidl_cb) override;
@@ -52,6 +52,7 @@
     bool turnOffSE = true;
     uint8_t atr[32];
     uint8_t atr_size;
+    char config_filename[100];
     static sp<V1_0::ISecureElementHalCallback> internalClientCallback;
     int initializeSE();
     Return<::android::hardware::secure_element::V1_0::SecureElementStatus> deinitializeSE();
@@ -59,11 +60,9 @@
     static int run_apdu(struct se_gto_ctx *ctx, const uint8_t *apdu, uint8_t *resp, int n, int verbose);
     static int toint(char c);
     static void dump_bytes(const char *pf, char sep, const uint8_t *p, int n, FILE *out);
-    void resetSE();
+    int resetSE();
     int openConfigFile(int verbose);
     int parseConfigFile(FILE *f, int verbose);
-    
-
 };
 
 // FIXME: most likely delete, this is only for passthrough implementations
diff --git a/secure_element/1.0/android.hardware.secure_element@1.0-service-gto-ese2.rc b/secure_element/1.0/android.hardware.secure_element@1.0-service-gto-ese2.rc
new file mode 100644
index 0000000..c03d935
--- /dev/null
+++ b/secure_element/1.0/android.hardware.secure_element@1.0-service-gto-ese2.rc
@@ -0,0 +1,4 @@
+service gto_secure_element_hal_service_ese2 /vendor/bin/hw/android.hardware.secure_element@1.0-service-gto-ese2
+    class hal
+    user secure_element
+    group secure_element
diff --git a/secure_element/1.0/esehal/src/checksum.c b/secure_element/1.0/esehal/src/checksum.c
index d288b17..fe324e1 100644
--- a/secure_element/1.0/esehal/src/checksum.c
+++ b/secure_element/1.0/esehal/src/checksum.c
@@ -33,8 +33,10 @@
     uint8_t        c = 0;
 
     if (p)
-        while (n--)
+        while (n) {
             c ^= *p++;
+            n--;
+        }
     return c;
 }
 
@@ -87,7 +89,9 @@
     const uint8_t *p = s;
 
     if (s)
-        while(n--)
+        while(n) {
             crc = (uint8_t)(crc >> 8) ^ fast[(uint8_t)(crc ^ *p++)];
+            n--;
+        }
     return crc;
 }
diff --git a/secure_element/1.0/esehal/src/iso7816_t1.c b/secure_element/1.0/esehal/src/iso7816_t1.c
index 77baa2a..5f73deb 100644
--- a/secure_element/1.0/esehal/src/iso7816_t1.c
+++ b/secure_element/1.0/esehal/src/iso7816_t1.c
@@ -45,7 +45,7 @@
 
 #define MAX_RETRIES 3
 
-#define MAX_WTX_ROUNDS 100
+#define MAX_WTX_ROUNDS 200
 
 #define WTX_MAX_VALUE 1
 
diff --git a/secure_element/1.0/esehal/src/libse-gto.c b/secure_element/1.0/esehal/src/libse-gto.c
index b87dc5c..b111f38 100644
--- a/secure_element/1.0/esehal/src/libse-gto.c
+++ b/secure_element/1.0/esehal/src/libse-gto.c
@@ -32,6 +32,7 @@
 #include <stdint.h>
 #include <fcntl.h>
 #include <log/log.h>
+#include <sys/ioctl.h>
 
 #include "se-gto/libse-gto.h"
 #include "libse-gto-private.h"
@@ -217,6 +218,26 @@
     return 0;
 }
 
+#define SPI_IOC_MAGIC    'k'
+#define ST54SPI_IOC_WR_POWER _IOW(SPI_IOC_MAGIC, 99, __u32)
+
+int se_gto_Spi_Reset(struct se_gto_ctx *ctx)
+{
+    uint32_t io_code;
+    uint32_t power = 0;
+
+    printf("Send software reset via ioctl\n");
+    io_code = ST54SPI_IOC_WR_POWER;
+    power = 1;
+    if (-1 == ioctl (ctx->t1.spi_fd, io_code, &power)) {
+        perror("unable to soft reset via ioctl\n");
+        return -1;
+    }
+
+    isot1_resync(&ctx->t1);
+    return 0;
+}
+
 int gtoSPI_checkAlive(struct se_gto_ctx *ctx);
 int gtoSPI_checkAlive(struct se_gto_ctx *ctx)
 {
@@ -225,9 +246,13 @@
   unsigned char resp[258] = {0,};
 
   /*Check Alive implem*/
-  ret = se_gto_apdu_transmit(ctx, apdu, 5, resp, sizeof(resp));
-  if(ret < 0){
-    return -1;
+  for(int count = 0; count < 3; count++) {
+      ret = se_gto_apdu_transmit(ctx, apdu, 5, resp, sizeof(resp));
+      if(ret < 0){
+        if (count == 2) return -1;
+        /*Run SPI reset*/
+        se_gto_Spi_Reset(ctx);
+      }
   }
 
   return 0;
diff --git a/secure_element/1.1/GtoService.cpp b/secure_element/1.1/GtoService.cpp
index 263d9e1..c364309 100644
--- a/secure_element/1.1/GtoService.cpp
+++ b/secure_element/1.1/GtoService.cpp
@@ -4,7 +4,7 @@
  * This copy is licensed under the Apache License, Version 2.0 (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
- *     http://www.apache.org/licenses/LICENSE-2.0 or https://www.apache.org/licenses/LICENSE-2.0.html 
+ *     http://www.apache.org/licenses/LICENSE-2.0 or https://www.apache.org/licenses/LICENSE-2.0.html
  *
  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and limitations under the License.
@@ -27,8 +27,8 @@
 
 int main() {
   ALOGD("android::hardware::secure_element::V1_1 is starting.");
-  ALOGD("Thales Secure Element HAL Service Release 1.1.1 ==> libse-gto v1.12");
-  sp<ISecureElement> se_service = new SecureElement();
+  ALOGD("Thales Secure Element HAL for eSE2 Service 1.3.0 is starting. libse-gto v1.12");
+  sp<ISecureElement> se_service = new SecureElement("eSE1");
   configureRpcThreadpool(1, true);
   status_t status = se_service->registerAsService("eSE1");
   if (status != OK) {
@@ -37,7 +37,7 @@
         status);
     return -1;
   }
-  
+
   ALOGD("Thales Secure Element Service is ready");
   joinRpcThreadpool();
   return 1;
diff --git a/secure_element/1.1/GtoService_ese2.cpp b/secure_element/1.1/GtoService_ese2.cpp
new file mode 100644
index 0000000..a699528
--- /dev/null
+++ b/secure_element/1.1/GtoService_ese2.cpp
@@ -0,0 +1,44 @@
+/*****************************************************************************
+ * Copyright ©2017-2019 Gemalto – a Thales Company. All rights Reserved.
+ *
+ * This copy is licensed under the Apache License, Version 2.0 (the "License");
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *     http://www.apache.org/licenses/LICENSE-2.0 or https://www.apache.org/licenses/LICENSE-2.0.html
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and limitations under the License.
+
+ ****************************************************************************/
+#include <android/hardware/secure_element/1.1/ISecureElement.h>
+#include <hidl/LegacySupport.h>
+#include <log/log.h>
+
+#include "SecureElement.h"
+
+// Generated HIDL files
+using android::hardware::secure_element::V1_1::ISecureElement;
+using android::hardware::secure_element::V1_1::implementation::SecureElement;
+using android::hardware::configureRpcThreadpool;
+using android::hardware::joinRpcThreadpool;
+using android::OK;
+using android::sp;
+using android::status_t;
+
+int main() {
+  ALOGD("android::hardware::secure_element::V1_1 is starting.");
+  ALOGD("Thales Secure Element HAL for eSE2 Service 1.3.0 is starting. libse-gto v1.12");
+  sp<ISecureElement> se_service = new SecureElement("eSE2");
+  configureRpcThreadpool(1, true);
+  status_t status = se_service->registerAsService("eSE2");
+  if (status != OK) {
+    LOG_ALWAYS_FATAL(
+        "registerAsService (%d).",
+        status);
+    return -1;
+  }
+
+  ALOGD("Gemalto Secure Element Service is ready");
+  joinRpcThreadpool();
+  return 1;
+}
diff --git a/secure_element/1.1/SecureElement.cpp b/secure_element/1.1/SecureElement.cpp
index 727b5d8..d6b6f11 100644
--- a/secure_element/1.1/SecureElement.cpp
+++ b/secure_element/1.1/SecureElement.cpp
@@ -49,12 +49,18 @@
 
 static struct se_gto_ctx *ctx;
 
-SecureElement::SecureElement(){
+SecureElement::SecureElement(const char* ese_name){
     nbrOpenChannel = 0;
     ctx = NULL;
+
+    if (strcmp(ese_name, "eSE2") == 0) {
+        strcpy( config_filename, "/vendor/etc/libse-gto-hal2.conf");
+    } else {
+        strcpy( config_filename, "/vendor/etc/libse-gto-hal.conf");
+    }
 }
 
-void SecureElement::resetSE(){
+int SecureElement::resetSE(){
     int n;
 
     isBasicChannelOpen = false;
@@ -69,6 +75,8 @@
     } else {
         ALOGE("SecureElement:%s Failed to reset and get ATR: %s\n", __func__, strerror(errno));
     }
+
+    return n;
 }
 
 sp<V1_0::ISecureElementHalCallback> SecureElement::internalClientCallback = nullptr;
@@ -87,7 +95,7 @@
     }
     //settings = default_settings(ctx);
     se_gto_set_log_level(ctx, 4);
-    
+
     openConfigFile(1);
 
     if (se_gto_open(ctx) < 0) {
@@ -95,7 +103,9 @@
         return EXIT_FAILURE;
     }
 
-    resetSE();
+    if (resetSE() < 0) {
+        return EXIT_FAILURE;
+    }
 
     checkSeUp = true;
     turnOffSE = false;
@@ -127,7 +137,6 @@
     if (initializeSE() != EXIT_SUCCESS) {
         ALOGE("SecureElement:%s initializeSE Failed", __func__);
         clientCallback->onStateChange(false);
-        return Void();
     }
 
     if (deinitializeSE() != SecureElementStatus::SUCCESS) {
@@ -155,7 +164,6 @@
     if (initializeSE() != EXIT_SUCCESS) {
         ALOGE("SecureElement:%s initializeSE Failed", __func__);
         clientCallback->onStateChange_1_1(false, "initializeSE Failed");
-        return Void();
     }
 
     if (deinitializeSE() != SecureElementStatus::SUCCESS) {
@@ -610,24 +618,24 @@
 {
     int   r;
     FILE *f;
-    char filename[] = "/vendor/etc/libse-gto-hal.conf";
+
 
     /* filename is not NULL */
-    ALOGD("SecureElement:%s Open Config file : %s", __func__, filename);
-    f = fopen(filename, "r");
+    ALOGD("SecureElement:%s Open Config file : %s", __func__, config_filename);
+    f = fopen(config_filename, "r");
     if (f) {
         r = parseConfigFile(f, verbose);
         if (r == -1) {
-            perror(filename);
-            ALOGE("SecureElement:%s Error parse %s Failed", __func__, filename);
+            perror(config_filename);
+            ALOGE("SecureElement:%s Error parse %s Failed", __func__, config_filename);
         }
         if (fclose(f) != 0) {
             r = -1;
-            ALOGE("SecureElement:%s Error close %s Failed", __func__, filename);
+            ALOGE("SecureElement:%s Error close %s Failed", __func__, config_filename);
         }
     } else {
         r = -1;
-        ALOGE("SecureElement:%s Error open %s Failed", __func__, filename);
+        ALOGE("SecureElement:%s Error open %s Failed", __func__, config_filename);
     }
     return r;
 }
@@ -665,6 +673,7 @@
         }
         checkSeUp = false;
         turnOffSE = false;
+
     }else{
         ALOGD("SecureElement:%s No need to deinitialize SE", __func__);
         mSecureElementStatus = SecureElementStatus::SUCCESS;
diff --git a/secure_element/1.1/SecureElement.h b/secure_element/1.1/SecureElement.h
index 90596e0..2cf4380 100644
--- a/secure_element/1.1/SecureElement.h
+++ b/secure_element/1.1/SecureElement.h
@@ -37,7 +37,7 @@
 
 
 struct SecureElement : public ISecureElement , public hidl_death_recipient {
-    SecureElement();
+    SecureElement(const char* ese_name);
     Return<void> init(const sp<V1_0::ISecureElementHalCallback>& clientCallback) override;
     Return<void> init_1_1(const sp<V1_1::ISecureElementHalCallback>& clientCallback) override;
     Return<void> openLogicalChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, openLogicalChannel_cb _hidl_cb) override;
@@ -55,6 +55,7 @@
     bool turnOffSE = true;
     uint8_t atr[32];
     uint8_t atr_size;
+    char config_filename[100];
     static sp<V1_0::ISecureElementHalCallback> internalClientCallback;
     static sp<V1_1::ISecureElementHalCallback> internalClientCallback_v1_1;
     int initializeSE();
@@ -63,11 +64,9 @@
     static int run_apdu(struct se_gto_ctx *ctx, const uint8_t *apdu, uint8_t *resp, int n, int verbose);
     static int toint(char c);
     static void dump_bytes(const char *pf, char sep, const uint8_t *p, int n, FILE *out);
-    void resetSE();
+    int resetSE();
     int openConfigFile(int verbose);
     int parseConfigFile(FILE *f, int verbose);
-    
-
 };
 
 
diff --git a/secure_element/1.1/android.hardware.secure_element@1.1-service-gto-ese2.rc b/secure_element/1.1/android.hardware.secure_element@1.1-service-gto-ese2.rc
new file mode 100644
index 0000000..7c94429
--- /dev/null
+++ b/secure_element/1.1/android.hardware.secure_element@1.1-service-gto-ese2.rc
@@ -0,0 +1,4 @@
+service gto_secure_element_hal_service_ese2_1_1 /vendor/bin/hw/android.hardware.secure_element@1.1-service-gto-ese2
+    class hal
+    user secure_element
+    group secure_element
diff --git a/secure_element/1.2/GtoService.cpp b/secure_element/1.2/GtoService.cpp
index 72f6542..c7b2309 100644
--- a/secure_element/1.2/GtoService.cpp
+++ b/secure_element/1.2/GtoService.cpp
@@ -4,7 +4,7 @@
  * This copy is licensed under the Apache License, Version 2.0 (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
- *     http://www.apache.org/licenses/LICENSE-2.0 or https://www.apache.org/licenses/LICENSE-2.0.html 
+ *     http://www.apache.org/licenses/LICENSE-2.0 or https://www.apache.org/licenses/LICENSE-2.0.html
  *
  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and limitations under the License.
@@ -27,9 +27,8 @@
 
 int main() {
   ALOGD("android::hardware::secure_element::V1_2 is starting.");
-  ALOGD("Thales Secure Element HAL Service Release 1.2.2 ==> libse-gto v1.12");
-  sp<ISecureElement> se_service = new SecureElement();
-  //sp<ISecureElement> se_service2 = new SecureElement();
+  ALOGD("Thales Secure Element HAL for eSE1 Service 1.3.0 is starting. libse-gto v1.12");
+  sp<ISecureElement> se_service = new SecureElement("eSE1");
   configureRpcThreadpool(1, true);
   status_t status = se_service->registerAsService("eSE1");
   if (status != OK) {
@@ -39,14 +38,6 @@
     return -1;
   }
 
-  /*status_t status = se_service2->registerAsService("eSE2");
-  if (status != OK) {
-    LOG_ALWAYS_FATAL(
-        "registerAsService2 (%d).",
-        status);
-    return -1;
-  }*/
-  
   ALOGD("Thales Secure Element Service is ready");
   joinRpcThreadpool();
   return 1;
diff --git a/secure_element/1.2/GtoService_ese2.cpp b/secure_element/1.2/GtoService_ese2.cpp
new file mode 100644
index 0000000..b6e6fc5
--- /dev/null
+++ b/secure_element/1.2/GtoService_ese2.cpp
@@ -0,0 +1,44 @@
+/*****************************************************************************
+ * Copyright ©2017-2019 Gemalto – a Thales Company. All rights Reserved.
+ *
+ * This copy is licensed under the Apache License, Version 2.0 (the "License");
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *     http://www.apache.org/licenses/LICENSE-2.0 or https://www.apache.org/licenses/LICENSE-2.0.html
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and limitations under the License.
+
+ ****************************************************************************/
+#include <android/hardware/secure_element/1.2/ISecureElement.h>
+#include <hidl/LegacySupport.h>
+#include <log/log.h>
+
+#include "SecureElement.h"
+
+// Generated HIDL files
+using android::hardware::secure_element::V1_2::ISecureElement;
+using android::hardware::secure_element::V1_2::implementation::SecureElement;
+using android::hardware::configureRpcThreadpool;
+using android::hardware::joinRpcThreadpool;
+using android::OK;
+using android::sp;
+using android::status_t;
+
+int main() {
+  ALOGD("android::hardware::secure_element::V1_2 is starting.");
+  ALOGD("Thales Secure Element HAL for eSE2 Service 1.3.0 is starting. libse-gto v1.12");
+  sp<ISecureElement> se_service = new SecureElement("eSE2");
+  configureRpcThreadpool(1, true);
+  status_t status = se_service->registerAsService("eSE2");
+  if (status != OK) {
+    LOG_ALWAYS_FATAL(
+        "registerAsService (%d).",
+        status);
+    return -1;
+  }
+
+  ALOGD("Gemalto Secure Element Service is ready");
+  joinRpcThreadpool();
+  return 1;
+}
diff --git a/secure_element/1.2/SecureElement.cpp b/secure_element/1.2/SecureElement.cpp
index 71613a8..6e7406b 100644
--- a/secure_element/1.2/SecureElement.cpp
+++ b/secure_element/1.2/SecureElement.cpp
@@ -49,9 +49,15 @@
 
 static struct se_gto_ctx *ctx;
 
-SecureElement::SecureElement(){
+SecureElement::SecureElement(const char* ese_name){
     nbrOpenChannel = 0;
     ctx = NULL;
+
+    if (strcmp(ese_name, "eSE2") == 0) {
+        strcpy( config_filename, "/vendor/etc/libse-gto-hal2.conf");
+    } else {
+        strcpy( config_filename, "/vendor/etc/libse-gto-hal.conf");
+    }
 }
 
 int SecureElement::resetSE(){
@@ -89,7 +95,7 @@
     }
     //settings = default_settings(ctx);
     se_gto_set_log_level(ctx, 4);
-    
+
     openConfigFile(1);
 
     if (se_gto_open(ctx) < 0) {
@@ -97,7 +103,9 @@
         return EXIT_FAILURE;
     }
 
-    if( resetSE() < 0) return EXIT_FAILURE;
+    if (resetSE() < 0) {
+        return EXIT_FAILURE;
+    }
 
     checkSeUp = true;
     turnOffSE = false;
@@ -129,7 +137,6 @@
     if (initializeSE() != EXIT_SUCCESS) {
         ALOGE("SecureElement:%s initializeSE Failed", __func__);
         clientCallback->onStateChange(false);
-        return Void();
     }
 
     if (deinitializeSE() != SecureElementStatus::SUCCESS) {
@@ -157,7 +164,6 @@
     if (initializeSE() != EXIT_SUCCESS) {
         ALOGE("SecureElement:%s initializeSE Failed", __func__);
         clientCallback->onStateChange_1_1(false, "initializeSE Failed");
-        return Void();
     }
 
     if (deinitializeSE() != SecureElementStatus::SUCCESS) {
@@ -612,24 +618,24 @@
 {
     int   r;
     FILE *f;
-    char filename[] = "/vendor/etc/libse-gto-hal.conf";
+
 
     /* filename is not NULL */
-    ALOGD("SecureElement:%s Open Config file : %s", __func__, filename);
-    f = fopen(filename, "r");
+    ALOGD("SecureElement:%s Open Config file : %s", __func__, config_filename);
+    f = fopen(config_filename, "r");
     if (f) {
         r = parseConfigFile(f, verbose);
         if (r == -1) {
-            perror(filename);
-            ALOGE("SecureElement:%s Error parse %s Failed", __func__, filename);
+            perror(config_filename);
+            ALOGE("SecureElement:%s Error parse %s Failed", __func__, config_filename);
         }
         if (fclose(f) != 0) {
             r = -1;
-            ALOGE("SecureElement:%s Error close %s Failed", __func__, filename);
+            ALOGE("SecureElement:%s Error close %s Failed", __func__, config_filename);
         }
     } else {
         r = -1;
-        ALOGE("SecureElement:%s Error open %s Failed", __func__, filename);
+        ALOGE("SecureElement:%s Error open %s Failed", __func__, config_filename);
     }
     return r;
 }
@@ -685,10 +691,17 @@
     if (deinitializeSE() != SecureElementStatus::SUCCESS) {
         ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
     }
+
+    if(internalClientCallback_v1_1 != nullptr) {
+        internalClientCallback_v1_1->onStateChange_1_1(false, "SE deinitialized");
+    } else {
+        internalClientCallback->onStateChange(false);
+    }
+
     if(initializeSE() == EXIT_SUCCESS) {
         status = SecureElementStatus::SUCCESS;
     }
-	
+
     ALOGD("SecureElement:%s end", __func__);
 
     return status;
diff --git a/secure_element/1.2/SecureElement.h b/secure_element/1.2/SecureElement.h
index 9f03d39..19d3601 100644
--- a/secure_element/1.2/SecureElement.h
+++ b/secure_element/1.2/SecureElement.h
@@ -37,7 +37,7 @@
 
 
 struct SecureElement : public ISecureElement , public hidl_death_recipient {
-    SecureElement();
+    SecureElement(const char* ese_name);
     Return<void> init(const sp<V1_0::ISecureElementHalCallback>& clientCallback) override;
     Return<void> init_1_1(const sp<V1_1::ISecureElementHalCallback>& clientCallback) override;
     Return<void> openLogicalChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, openLogicalChannel_cb _hidl_cb) override;
@@ -56,6 +56,7 @@
     bool turnOffSE = true;
     uint8_t atr[32];
     uint8_t atr_size;
+    char config_filename[100];
     static sp<V1_0::ISecureElementHalCallback> internalClientCallback;
     static sp<V1_1::ISecureElementHalCallback> internalClientCallback_v1_1;
     int initializeSE();
@@ -67,8 +68,6 @@
     int resetSE();
     int openConfigFile(int verbose);
     int parseConfigFile(FILE *f, int verbose);
-    
-
 };
 
 
diff --git a/secure_element/1.2/android.hardware.secure_element@1.1-service-gto-ese2.rc b/secure_element/1.2/android.hardware.secure_element@1.1-service-gto-ese2.rc
new file mode 100644
index 0000000..7c94429
--- /dev/null
+++ b/secure_element/1.2/android.hardware.secure_element@1.1-service-gto-ese2.rc
@@ -0,0 +1,4 @@
+service gto_secure_element_hal_service_ese2_1_1 /vendor/bin/hw/android.hardware.secure_element@1.1-service-gto-ese2
+    class hal
+    user secure_element
+    group secure_element
diff --git a/secure_element/1.2/android.hardware.secure_element@1.2-service-gto-ese2.rc b/secure_element/1.2/android.hardware.secure_element@1.2-service-gto-ese2.rc
new file mode 100644
index 0000000..165f308
--- /dev/null
+++ b/secure_element/1.2/android.hardware.secure_element@1.2-service-gto-ese2.rc
@@ -0,0 +1,4 @@
+service gto_secure_element_hal_service_ese2_1_2 /vendor/bin/hw/android.hardware.secure_element@1.2-service-gto-ese2
+    class hal
+    user secure_element
+    group secure_element
diff --git a/secure_element/Android.bp b/secure_element/Android.bp
index 3270488..a30615b 100644
--- a/secure_element/Android.bp
+++ b/secure_element/Android.bp
@@ -231,3 +231,239 @@
         "libutils",
     ],
 }
+
+// --------------------------------------------------------------------------------
+
+cc_library_shared {
+    // FIXME: this should only be -impl for a passthrough hal.
+    // In most cases, to convert this to a binderized implementation, you should:
+    // - change '-impl' to '-service' here and make it a cc_binary instead of a
+    //   cc_library_shared.
+    // - add a *.rc file for this module.
+    // - delete HIDL_FETCH_I* functions.
+    // - call configureRpcThreadpool and registerAsService on the instance.
+    // You may also want to append '-impl/-service' with a specific identifier like
+    // '-vendor' or '-<hardware identifier>' etc to distinguish it.
+    name: "android.hardware.secure_element@1.0-impl-gto-ese2",
+    //relative_install_path: "hw",
+    // FIXME: this should be 'vendor: true' for modules that will eventually be
+    // on AOSP.
+    vendor: true,
+    srcs: [
+        "1.0/esehal/src/checksum.c",
+        "1.0/esehal/src/iso7816_t1.c",
+        "1.0/esehal/src/libse-gto.c",
+        "1.0/esehal/src/spi.c",
+        "1.0/esehal/src/transport.c",
+        "1.0/esehal/src/log.c",
+        "1.0/SecureElement.cpp",
+    ],
+
+    local_include_dirs: [
+        "1.0/esehal/src",
+    ],
+
+    cflags: [
+        "-DANDROID",
+        "-DENABLE_LOGGING=1",
+        "-DENABLE_DEBUG=1",
+        "-DBUILDCFG=1",
+        "-DNXP_EXTNS=TRUE",
+        "-Wno-unused-parameter",
+        "-Wno-unused-private-field",
+        "-Wno-error",
+        "-Wreturn-type",
+    ],
+
+    shared_libs: [
+        "libbase",
+        "libcutils",
+        "libhardware",
+        "libhidlbase",
+        "liblog",
+        "libutils",
+        "android.hardware.secure_element@1.0",
+    ],
+
+}
+
+cc_binary {
+    name: "android.hardware.secure_element@1.0-service-gto-ese2",
+    relative_install_path: "hw",
+    init_rc: ["1.0/android.hardware.secure_element@1.0-service-gto-ese2.rc"],
+    vendor: true,
+    defaults: ["hidl_defaults"],
+    srcs: [
+        "1.0/GtoService_ese2.cpp",
+    ],
+
+    shared_libs: [
+        "android.hardware.secure_element@1.0",
+        "android.hardware.secure_element@1.0-impl-gto-ese2",
+        "libbase",
+        "libcutils",
+        "libhardware",
+        "libhidlbase",
+        "liblog",
+        "libutils",
+    ],
+}
+
+cc_library_shared {
+    // FIXME: this should only be -impl for a passthrough hal.
+    // In most cases, to convert this to a binderized implementation, you should:
+    // - change '-impl' to '-service' here and make it a cc_binary instead of a
+    //   cc_library_shared.
+    // - add a *.rc file for this module.
+    // - delete HIDL_FETCH_I* functions.
+    // - call configureRpcThreadpool and registerAsService on the instance.
+    // You may also want to append '-impl/-service' with a specific identifier like
+    // '-vendor' or '-<hardware identifier>' etc to distinguish it.
+    name: "android.hardware.secure_element@1.1-impl-gto-ese2",
+    //relative_install_path: "hw",
+    // FIXME: this should be 'vendor: true' for modules that will eventually be
+    // on AOSP.
+    vendor: true,
+    srcs: [
+        "1.0/esehal/src/checksum.c",
+        "1.0/esehal/src/iso7816_t1.c",
+        "1.0/esehal/src/libse-gto.c",
+        "1.0/esehal/src/spi.c",
+        "1.0/esehal/src/transport.c",
+        "1.0/esehal/src/log.c",
+        "1.1/SecureElement.cpp",
+    ],
+
+    local_include_dirs: [
+        "1.0/esehal/src",
+    ],
+
+    cflags: [
+        "-DANDROID",
+        "-DENABLE_LOGGING=1",
+        "-DENABLE_DEBUG=1",
+        "-DBUILDCFG=1",
+        "-DNXP_EXTNS=TRUE",
+        "-Wno-unused-parameter",
+        "-Wno-unused-private-field",
+        "-Wno-error",
+        "-Wreturn-type",
+    ],
+
+    shared_libs: [
+        "libbase",
+        "libcutils",
+        "libhardware",
+        "libhidlbase",
+        "liblog",
+        "libutils",
+        "android.hardware.secure_element@1.0",
+	"android.hardware.secure_element@1.1",
+    ],
+
+}
+
+cc_binary {
+    name: "android.hardware.secure_element@1.1-service-gto-ese2",
+    relative_install_path: "hw",
+    init_rc: ["1.1/android.hardware.secure_element@1.1-service-gto-ese2.rc"],
+    vendor: true,
+    defaults: ["hidl_defaults"],
+    srcs: [
+        "1.1/GtoService_ese2.cpp",
+    ],
+
+    shared_libs: [
+        "android.hardware.secure_element@1.0",
+        "android.hardware.secure_element@1.0-impl-gto-ese2",
+	"android.hardware.secure_element@1.1",
+        "android.hardware.secure_element@1.1-impl-gto-ese2",
+        "libbase",
+        "libcutils",
+        "libhardware",
+        "libhidlbase",
+        "liblog",
+        "libutils",
+    ],
+}
+
+cc_library_shared {
+    // FIXME: this should only be -impl for a passthrough hal.
+    // In most cases, to convert this to a binderized implementation, you should:
+    // - change '-impl' to '-service' here and make it a cc_binary instead of a
+    //   cc_library_shared.
+    // - add a *.rc file for this module.
+    // - delete HIDL_FETCH_I* functions.
+    // - call configureRpcThreadpool and registerAsService on the instance.
+    // You may also want to append '-impl/-service' with a specific identifier like
+    // '-vendor' or '-<hardware identifier>' etc to distinguish it.
+    name: "android.hardware.secure_element@1.2-impl-gto-ese2",
+    //relative_install_path: "hw",
+    // FIXME: this should be 'vendor: true' for modules that will eventually be
+    // on AOSP.
+    vendor: true,
+    srcs: [
+        "1.0/esehal/src/checksum.c",
+        "1.0/esehal/src/iso7816_t1.c",
+        "1.0/esehal/src/libse-gto.c",
+        "1.0/esehal/src/spi.c",
+        "1.0/esehal/src/transport.c",
+        "1.0/esehal/src/log.c",
+        "1.2/SecureElement.cpp",
+    ],
+
+    local_include_dirs: [
+        "1.0/esehal/src",
+    ],
+
+    cflags: [
+        "-DANDROID",
+        "-DENABLE_LOGGING=1",
+        "-DENABLE_DEBUG=1",
+        "-DBUILDCFG=1",
+        "-DNXP_EXTNS=TRUE",
+        "-Wno-unused-parameter",
+        "-Wno-unused-private-field",
+        "-Wno-error",
+        "-Wreturn-type",
+    ],
+
+    shared_libs: [
+        "libbase",
+        "libcutils",
+        "libhardware",
+        "libhidlbase",
+        "liblog",
+        "libutils",
+        "android.hardware.secure_element@1.0",
+	"android.hardware.secure_element@1.1",
+	"android.hardware.secure_element@1.2",
+    ],
+
+}
+
+cc_binary {
+    name: "android.hardware.secure_element@1.2-service-gto-ese2",
+    relative_install_path: "hw",
+    init_rc: ["1.2/android.hardware.secure_element@1.2-service-gto-ese2.rc"],
+    vendor: true,
+    defaults: ["hidl_defaults"],
+    srcs: [
+        "1.2/GtoService_ese2.cpp",
+    ],
+
+    shared_libs: [
+        "android.hardware.secure_element@1.0",
+        "android.hardware.secure_element@1.0-impl-gto-ese2",
+	"android.hardware.secure_element@1.1",
+        "android.hardware.secure_element@1.1-impl-gto-ese2",
+        "android.hardware.secure_element@1.2",
+        "android.hardware.secure_element@1.2-impl-gto-ese2",
+        "libbase",
+        "libcutils",
+        "libhardware",
+        "libhidlbase",
+        "liblog",
+        "libutils",
+    ],
+}
diff --git a/secure_element/1.0/libse-gto-hal.conf b/secure_element/libse-gto-hal-ese2.conf
similarity index 100%
copy from secure_element/1.0/libse-gto-hal.conf
copy to secure_element/libse-gto-hal-ese2.conf
diff --git a/secure_element/1.0/libse-gto-hal.conf b/secure_element/libse-gto-hal.conf
similarity index 100%
rename from secure_element/1.0/libse-gto-hal.conf
rename to secure_element/libse-gto-hal.conf