Janis Danisevskis | 7daa66a | 2019-06-06 22:05:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | */ |
David Drysdale | d0149e8 | 2023-02-03 18:25:03 +0000 | [diff] [blame] | 16 | #define LOG_TAG "android.hardware.gatekeeper-service.trusty" |
Janis Danisevskis | 7daa66a | 2019-06-06 22:05:41 -0700 | [diff] [blame] | 17 | |
| 18 | #include <android-base/logging.h> |
David Drysdale | d0149e8 | 2023-02-03 18:25:03 +0000 | [diff] [blame] | 19 | #include <android/binder_manager.h> |
| 20 | #include <android/binder_process.h> |
Arve Hjønnevåg | 2fea1dd | 2023-09-13 17:51:20 -0700 | [diff] [blame] | 21 | #include <getopt.h> |
Janis Danisevskis | 7daa66a | 2019-06-06 22:05:41 -0700 | [diff] [blame] | 22 | |
| 23 | #include "trusty_gatekeeper.h" |
Arve Hjønnevåg | 2fea1dd | 2023-09-13 17:51:20 -0700 | [diff] [blame] | 24 | #include "trusty_gatekeeper_ipc.h" |
Janis Danisevskis | 7daa66a | 2019-06-06 22:05:41 -0700 | [diff] [blame] | 25 | |
David Drysdale | d0149e8 | 2023-02-03 18:25:03 +0000 | [diff] [blame] | 26 | using aidl::android::hardware::gatekeeper::TrustyGateKeeperDevice; |
Janis Danisevskis | 7daa66a | 2019-06-06 22:05:41 -0700 | [diff] [blame] | 27 | |
Arve Hjønnevåg | 2fea1dd | 2023-09-13 17:51:20 -0700 | [diff] [blame] | 28 | static const char* _sopts = "hD:"; |
| 29 | static const struct option _lopts[] = { |
| 30 | {"help", no_argument, 0, 'h'}, |
| 31 | {"dev", required_argument, 0, 'D'}, |
| 32 | {0, 0, 0, 0}, |
| 33 | }; |
| 34 | |
| 35 | static const char* usage = |
| 36 | "Usage: %s [options]\n" |
| 37 | "\n" |
| 38 | "options:\n" |
| 39 | " -h, --help prints this message and exit\n" |
| 40 | " -D, --dev name Trusty device name\n" |
| 41 | "\n"; |
| 42 | |
| 43 | static const char* usage_long = "\n"; |
| 44 | |
| 45 | static void print_usage_and_exit(const char* prog, int code, bool verbose) { |
| 46 | fprintf(stderr, usage, prog); |
| 47 | if (verbose) { |
| 48 | fprintf(stderr, "%s", usage_long); |
| 49 | } |
| 50 | exit(code); |
| 51 | } |
| 52 | |
| 53 | static void parse_options(int argc, char** argv) { |
| 54 | int c; |
| 55 | int oidx = 0; |
| 56 | |
| 57 | while (1) { |
| 58 | c = getopt_long(argc, argv, _sopts, _lopts, &oidx); |
| 59 | if (c == -1) { |
| 60 | break; /* done */ |
| 61 | } |
| 62 | |
| 63 | switch (c) { |
| 64 | case 'D': |
| 65 | trusty_gatekeeper_set_dev_name(optarg); |
| 66 | break; |
| 67 | |
| 68 | case 'h': |
| 69 | print_usage_and_exit(argv[0], EXIT_SUCCESS, true); |
| 70 | break; |
| 71 | |
| 72 | default: |
| 73 | print_usage_and_exit(argv[0], EXIT_FAILURE, false); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | int main(int argc, char** argv) { |
| 79 | parse_options(argc, argv); |
| 80 | |
David Drysdale | d0149e8 | 2023-02-03 18:25:03 +0000 | [diff] [blame] | 81 | ABinderProcess_setThreadPoolMaxThreadCount(0); |
Janis Danisevskis | 7daa66a | 2019-06-06 22:05:41 -0700 | [diff] [blame] | 82 | |
David Drysdale | d0149e8 | 2023-02-03 18:25:03 +0000 | [diff] [blame] | 83 | std::shared_ptr<TrustyGateKeeperDevice> gatekeeper = |
| 84 | ndk::SharedRefBase::make<TrustyGateKeeperDevice>(); |
| 85 | |
| 86 | const std::string instance = std::string() + TrustyGateKeeperDevice::descriptor + "/default"; |
| 87 | binder_status_t status = |
| 88 | AServiceManager_addService(gatekeeper->asBinder().get(), instance.c_str()); |
| 89 | CHECK_EQ(status, STATUS_OK); |
| 90 | |
| 91 | ABinderProcess_joinThreadPool(); |
| 92 | |
Janis Danisevskis | 7daa66a | 2019-06-06 22:05:41 -0700 | [diff] [blame] | 93 | return -1; // Should never get here. |
| 94 | } |