1 /*
2 * Copyright (C) 2025 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <android-base/logging.h>
18 #include <android/binder_manager.h>
19 #include <android/binder_process.h>
20 #include <getopt.h>
21 #include <string>
22 #include "hwcryptokeyimpl.h"
23
showUsageAndExit(int code)24 static void showUsageAndExit(int code) {
25 LOG(ERROR) << "usage: android.hardware.trusty.hwcryptohal-service -d <trusty_dev>";
26 exit(code);
27 }
28
parseDeviceName(int argc,char * argv[],char * & device_name)29 static void parseDeviceName(int argc, char* argv[], char*& device_name) {
30 static const char* _sopts = "h:d:";
31 static const struct option _lopts[] = {{"help", no_argument, nullptr, 'h'},
32 {"trusty_dev", required_argument, nullptr, 'd'},
33 {0, 0, 0, 0}};
34 int opt;
35 int oidx = 0;
36
37 while ((opt = getopt_long(argc, argv, _sopts, _lopts, &oidx)) != -1) {
38 switch (opt) {
39 case 'd':
40 device_name = strdup(optarg);
41 break;
42 case 'h':
43 showUsageAndExit(EXIT_SUCCESS);
44 break;
45 default:
46 LOG(ERROR) << "unrecognized option: " << opt;
47 showUsageAndExit(EXIT_FAILURE);
48 }
49 }
50
51 if (device_name == nullptr) {
52 LOG(ERROR) << "missing required argument(s)";
53 showUsageAndExit(EXIT_FAILURE);
54 }
55
56 LOG(INFO) << "starting android.hardware.trusty.hwcryptohal-service";
57 LOG(INFO) << "trusty dev: " << device_name;
58 }
59
main(int argc,char * argv[])60 int main(int argc, char* argv[]) {
61 char* device_name;
62 parseDeviceName(argc, argv, device_name);
63
64 auto hwCryptoServer = android::trusty::hwcryptohalservice::HwCryptoKey::Create(device_name);
65 if (hwCryptoServer == nullptr) {
66 LOG(ERROR) << "couldn't create hwcrypto service";
67 exit(EXIT_FAILURE);
68 }
69 ABinderProcess_setThreadPoolMaxThreadCount(0);
70 const std::string instance =
71 std::string() + ndk_hwcrypto::IHwCryptoKey::descriptor + "/default";
72 binder_status_t status =
73 AServiceManager_addService(hwCryptoServer->asBinder().get(), instance.c_str());
74 if (status != STATUS_OK) {
75 LOG(ERROR) << "couldn't register hwcrypto service";
76 }
77 CHECK_EQ(status, STATUS_OK);
78 ABinderProcess_joinThreadPool();
79
80 return 0;
81 }
82