1//
2// Copyright (C) 2015 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 <string>
18
19#include <base/command_line.h>
20#include <brillo/syslog_logging.h>
21#if defined(USE_TPM2)
22#include <trunks/trunks_factory_impl.h>
23#endif
24
25#include "tpm_manager/common/tpm_manager_constants.h"
26#if defined(USE_BINDER_IPC)
27#include "tpm_manager/server/binder_service.h"
28#else
29#include "tpm_manager/server/dbus_service.h"
30#endif
31#include "tpm_manager/server/local_data_store_impl.h"
32#include "tpm_manager/server/tpm_manager_service.h"
33
34#if defined(USE_TPM2)
35#include "tpm_manager/server/tpm2_initializer_impl.h"
36#include "tpm_manager/server/tpm2_nvram_impl.h"
37#include "tpm_manager/server/tpm2_status_impl.h"
38#else
39#include "tpm_manager/server/tpm_initializer_impl.h"
40#include "tpm_manager/server/tpm_nvram_impl.h"
41#include "tpm_manager/server/tpm_status_impl.h"
42#endif
43
44namespace {
45
46constexpr char kWaitForOwnershipTriggerSwitch[] = "wait_for_ownership_trigger";
47constexpr char kLogToStderrSwitch[] = "log_to_stderr";
48
49}  // namespace
50
51int main(int argc, char* argv[]) {
52  base::CommandLine::Init(argc, argv);
53  base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
54  int flags = brillo::kLogToSyslog;
55  if (cl->HasSwitch(kLogToStderrSwitch)) {
56    flags |= brillo::kLogToStderr;
57  }
58  brillo::InitLog(flags);
59
60  tpm_manager::LocalDataStoreImpl local_data_store;
61#if defined(USE_TPM2)
62  trunks::TrunksFactoryImpl trunks_factory;
63  // Tolerate some delay in trunksd being up and ready.
64  constexpr int kTrunksDaemonTimeoutMS = 30000;  // 30 seconds
65  int ms_waited = 0;
66  while (!trunks_factory.Initialize() && ms_waited < kTrunksDaemonTimeoutMS) {
67    usleep(300000);
68    ms_waited += 300;
69  }
70  tpm_manager::Tpm2StatusImpl tpm_status(trunks_factory);
71  tpm_manager::Tpm2InitializerImpl tpm_initializer(
72      trunks_factory, &local_data_store, &tpm_status);
73  tpm_manager::Tpm2NvramImpl tpm_nvram(trunks_factory, &local_data_store);
74#else
75  tpm_manager::TpmStatusImpl tpm_status;
76  tpm_manager::TpmInitializerImpl tpm_initializer(&local_data_store,
77                                                  &tpm_status);
78  tpm_manager::TpmNvramImpl tpm_nvram(&local_data_store);
79#endif
80  tpm_manager::TpmManagerService tpm_manager_service(
81      cl->HasSwitch(kWaitForOwnershipTriggerSwitch), &local_data_store,
82      &tpm_status, &tpm_initializer, &tpm_nvram);
83
84#if defined(USE_BINDER_IPC)
85  tpm_manager::BinderService ipc_service(&tpm_manager_service,
86                                         &tpm_manager_service);
87#else
88  tpm_manager::DBusService ipc_service(&tpm_manager_service,
89                                       &tpm_manager_service);
90#endif
91  CHECK(tpm_manager_service.Initialize()) << "Failed to initialize service.";
92  LOG(INFO) << "Starting TPM Manager...";
93  return ipc_service.Run();
94}
95