1 /******************************************************************************
2 *
3 * Copyright 2018-2021 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 #define LOG_TAG "nxpese@1.2-service"
19 #include <android/hardware/secure_element/1.2/ISecureElement.h>
20 #include <log/log.h>
21 #include <vendor/nxp/nxpese/1.0/INxpEse.h>
22 #include "VirtualISO.h"
23
24 #include <hidl/LegacySupport.h>
25 #include <string.h>
26 #include <regex>
27 #include "NxpEse.h"
28 #include "SecureElement.h"
29 #ifdef NXP_BOOTTIME_UPDATE
30 #include "eSEClient.h"
31 #endif
32
33 // Generated HIDL files
34 using android::OK;
35 using android::base::StringPrintf;
36 using android::hardware::configureRpcThreadpool;
37 using android::hardware::defaultPassthroughServiceImplementation;
38 using android::hardware::joinRpcThreadpool;
39 using android::hardware::registerPassthroughServiceImplementation;
40 using android::hardware::secure_element::V1_2::ISecureElement;
41 using android::hardware::secure_element::V1_2::implementation::SecureElement;
42 using vendor::nxp::nxpese::V1_0::INxpEse;
43 using vendor::nxp::nxpese::V1_0::implementation::NxpEse;
44 using vendor::nxp::virtual_iso::V1_0::implementation::VirtualISO;
45
46 using android::OK;
47 using android::sp;
48 using android::status_t;
49
main()50 int main() {
51 status_t status;
52
53 char terminalID[5] = "eSE1";
54 const char* SEterminal = "eSEx";
55 bool ret = false;
56
57 android::sp<ISecureElement> se_service = nullptr;
58 android::sp<INxpEse> nxp_se_service = nullptr;
59 android::sp<ISecureElement> virtual_iso_service = nullptr;
60
61 ALOGI("Secure Element HAL Service 1.2 is starting.");
62 try {
63 se_service = new SecureElement();
64 if (se_service == nullptr) {
65 ALOGE("Can not create an instance of Secure Element HAL Iface, exiting.");
66 goto shutdown;
67 }
68 configureRpcThreadpool(1, true /*callerWillJoin*/);
69
70 #ifdef NXP_BOOTTIME_UPDATE
71 checkEseClientUpdate();
72 ret = geteSETerminalId(terminalID);
73 #else
74 ret = true;
75 #endif
76 ALOGI("Terminal val = %s", terminalID);
77 if ((ret) && (strncmp(SEterminal, terminalID, 3) == 0)) {
78 ALOGI("Terminal ID found");
79 status = se_service->registerAsService(terminalID);
80
81 if (status != OK) {
82 ALOGE("Could not register service for Secure Element HAL Iface (%d).",
83 status);
84 goto shutdown;
85 }
86 ALOGI("Secure Element Service is ready");
87
88 ALOGI("NXP Secure Element Extn Service 1.0 is starting.");
89 nxp_se_service = new NxpEse();
90 if (nxp_se_service == nullptr) {
91 ALOGE(
92 "Can not create an instance of NXP Secure Element Extn "
93 "Iface,exiting.");
94 goto shutdown;
95 }
96 status = nxp_se_service->registerAsService();
97 if (status != OK) {
98 ALOGE(
99 "Could not register service for Power Secure Element Extn Iface "
100 "(%d).",
101 status);
102 goto shutdown;
103 }
104 ALOGI("Secure Element Service is ready");
105 }
106
107 ALOGI("Virtual ISO HAL Service 1.0 is starting.");
108 virtual_iso_service = new VirtualISO();
109 if (virtual_iso_service == nullptr) {
110 ALOGE("Can not create an instance of Virtual ISO HAL Iface, exiting.");
111 goto shutdown;
112 }
113 #ifdef NXP_BOOTTIME_UPDATE
114 ret = geteUICCTerminalId(terminalID);
115 #else
116 strncpy(terminalID, "eSE2", 4);
117 ret = true;
118 #endif
119 if ((ret) && (strncmp(SEterminal, terminalID, 3) == 0)) {
120 status = virtual_iso_service->registerAsService(terminalID);
121 if (status != OK) {
122 ALOGE("Could not register service for Virtual ISO HAL Iface (%d).",
123 status);
124 } else {
125 ALOGI("Virtual ISO: Secure Element Service is ready");
126 }
127 }
128
129 #ifdef NXP_BOOTTIME_UPDATE
130 perform_eSEClientUpdate();
131 #endif
132 joinRpcThreadpool();
133 } catch (const std::length_error& e) {
134 ALOGE("Length Exception occurred = %s ", e.what());
135 } catch (const std::__1::ios_base::failure& e) {
136 ALOGE("ios failure Exception occurred = %s ", e.what());
137 } catch (std::__1::regex_error& e) {
138 ALOGE("Regex Exception occurred = %s ", e.what());
139 }
140 // Should not pass this line
141 shutdown:
142 // In normal operation, we don't expect the thread pool to exit
143 ALOGE("Secure Element Service is shutting down");
144 return 1;
145 }
146