1 /*
2  * Copyright (C) 2016 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 <errno.h>
19 #include <getopt.h>
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include <cutils/android_filesystem_config.h>
26 
27 #include "DPUHandler.h"
28 
show_usage_and_exit(int code)29 static void show_usage_and_exit(int code) {
30     LOG(ERROR) << "usage: securedpud -d <trusty_dev>";
31     exit(code);
32 }
33 
parse_device_name(int argc,char * argv[],std::string & device_name)34 static void parse_device_name(int argc, char* argv[], std::string& device_name) {
35     static const char* _sopts = "h:d:";
36     static const struct option _lopts[] = {{"help", no_argument, NULL, 'h'},
37                                            {"trusty_dev", required_argument, NULL, 'd'},
38                                            {0, 0, 0, 0}};
39     int opt;
40     int oidx = 0;
41 
42     while ((opt = getopt_long(argc, argv, _sopts, _lopts, &oidx)) != -1) {
43         switch (opt) {
44             case 'd':
45                 device_name = optarg;
46                 break;
47 
48             default:
49                 LOG(ERROR) << "unrecognized option: " << opt;
50                 show_usage_and_exit(EXIT_FAILURE);
51         }
52     }
53 
54     if (device_name.empty()) {
55         LOG(ERROR) << "missing required argument(s)";
56         show_usage_and_exit(EXIT_FAILURE);
57     }
58 
59     LOG(INFO) << "starting securedpud";
60     LOG(INFO) << "trusty dev: " << device_name;
61 }
62 
main(int argc,char * argv[])63 int main(int argc, char* argv[])
64 {
65     std::string device_name;
66     /* parse arguments */
67     parse_device_name(argc, argv, device_name);
68 
69     android::trusty::secure_dpu::DPUHandler dpu_handler;
70     auto rc = dpu_handler.Init(device_name);
71     if (!rc.ok()) {
72         LOG(ERROR) << rc.error();
73         return EXIT_FAILURE;
74     }
75 
76     /* main loop */
77     while (1) {
78         auto result = dpu_handler.Handle();
79         if (!result.ok()) {
80           LOG(ERROR) << result.error();
81         }
82     }
83     LOG(ERROR) << "exiting securedpud loop";
84 
85     return EXIT_FAILURE;
86 }
87