OLD | NEW |
1 // Copyright (c) 2009-2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009-2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Contains the implementation of class TpmInit | 5 // Contains the implementation of class TpmInit |
6 | 6 |
7 #include "tpm_init.h" | 7 #include "tpm_init.h" |
8 | 8 |
9 #include <base/logging.h> | 9 #include <base/logging.h> |
| 10 #include <base/platform_thread.h> |
10 #include <base/time.h> | 11 #include <base/time.h> |
11 | 12 |
12 #include "tpm.h" | 13 #include "tpm.h" |
13 | 14 |
14 namespace tpm_init { | 15 namespace tpm_init { |
15 | 16 |
16 // TpmInitTask is a private class used to handle asynchronous initialization of | 17 // TpmInitTask is a private class used to handle asynchronous initialization of |
17 // the TPM. | 18 // the TPM. |
18 class TpmInitTask : public PlatformThread::Delegate { | 19 class TpmInitTask : public PlatformThread::Delegate { |
19 public: | 20 public: |
20 TpmInitTask(); | 21 TpmInitTask() |
21 virtual ~TpmInitTask(); | 22 : default_tpm_(new tpm_init::Tpm()), |
| 23 tpm_(default_tpm_.get()), |
| 24 init_(NULL) { |
| 25 } |
22 | 26 |
23 void Init(TpmInit::TpmInitCallback* notify_callback); | 27 virtual ~TpmInitTask() { |
| 28 } |
24 | 29 |
25 virtual void ThreadMain(); | 30 void Init(TpmInit* init) { |
| 31 init_ = init; |
| 32 tpm_->Init(); |
| 33 } |
26 | 34 |
27 bool IsTpmReady(); | 35 virtual void ThreadMain() { |
28 bool IsTpmEnabled(); | 36 if (init_) { |
29 bool IsTpmOwned(); | 37 init_->ThreadMain(); |
30 bool IsTpmBeingOwned(); | 38 } |
31 bool GetTpmPassword(chromeos::Blob* password); | 39 } |
32 long GetInitializationMillis(); | 40 |
33 bool GetRandomData(int length, chromeos::Blob* data); | 41 void set_tpm(tpm_init::Tpm* tpm) { |
| 42 tpm_ = tpm; |
| 43 } |
| 44 |
| 45 tpm_init::Tpm* get_tpm() { |
| 46 return tpm_; |
| 47 } |
34 | 48 |
35 private: | 49 private: |
36 scoped_ptr<tpm_init::Tpm> default_tpm_; | 50 scoped_ptr<tpm_init::Tpm> default_tpm_; |
37 tpm_init::Tpm* tpm_; | 51 tpm_init::Tpm* tpm_; |
38 bool initialize_took_ownership_; | 52 TpmInit* init_; |
39 bool task_done_; | |
40 long initialization_time_; | |
41 TpmInit::TpmInitCallback* notify_callback_; | |
42 }; | 53 }; |
43 | 54 |
44 TpmInit::TpmInit() | 55 TpmInit::TpmInit() |
45 : tpm_init_task_(new TpmInitTask()), | 56 : tpm_init_task_(new TpmInitTask()), |
46 notify_callback_(NULL) { | 57 notify_callback_(NULL), |
| 58 initialize_called_(false), |
| 59 task_done_(false), |
| 60 initialize_took_ownership_(false), |
| 61 initialization_time_(0) { |
47 } | 62 } |
48 | 63 |
49 TpmInit::~TpmInit() { | 64 TpmInit::~TpmInit() { |
50 } | 65 } |
51 | 66 |
52 void TpmInit::Init(TpmInitCallback* notify_callback) { | 67 void TpmInit::Init(TpmInitCallback* notify_callback) { |
53 notify_callback_ = notify_callback; | 68 notify_callback_ = notify_callback; |
| 69 tpm_init_task_->Init(this); |
54 } | 70 } |
55 | 71 |
56 bool TpmInit::GetRandomData(int length, chromeos::Blob* data) { | 72 bool TpmInit::GetRandomData(int length, chromeos::Blob* data) { |
57 return tpm_init_task_->GetRandomData(length, data); | 73 return tpm_init_task_->get_tpm()->GetRandomData(length, data); |
58 } | 74 } |
59 | 75 |
60 bool TpmInit::StartInitializeTpm() { | 76 bool TpmInit::StartInitializeTpm() { |
61 tpm_init_task_->Init(notify_callback_); | 77 initialize_called_ = true; |
62 if (!PlatformThread::CreateNonJoinable(0, tpm_init_task_.get())) { | 78 if (!PlatformThread::CreateNonJoinable(0, tpm_init_task_.get())) { |
63 LOG(ERROR) << "Unable to create TPM initialization background thread."; | 79 LOG(ERROR) << "Unable to create TPM initialization background thread."; |
64 return false; | 80 return false; |
65 } | 81 } |
66 return true; | 82 return true; |
67 } | 83 } |
68 | 84 |
69 bool TpmInit::IsTpmReady() { | 85 bool TpmInit::IsTpmReady() { |
70 return tpm_init_task_->IsTpmReady(); | 86 // The TPM is not "ready" if the init call has not completed. It may be in |
| 87 // the middle of taking ownership. |
| 88 if (!task_done_) { |
| 89 return false; |
| 90 } |
| 91 // If initialize_took_ownership_ is true, then the TPM went through a full |
| 92 // succesful ownership cycle in InitializeTpm() |
| 93 if (initialize_took_ownership_) { |
| 94 return true; |
| 95 } |
| 96 // If we get here, then the call to InitializeTpm() is complete and it |
| 97 // returned false. That merely means that it did not successfully take |
| 98 // ownership, which is the common case after ownership is established on OOBE. |
| 99 // In that case, the TPM is ready if it is enabled and owned. |
| 100 return (tpm_init_task_->get_tpm()->IsEnabled() && |
| 101 tpm_init_task_->get_tpm()->IsOwned()); |
71 } | 102 } |
72 | 103 |
73 bool TpmInit::IsTpmEnabled() { | 104 bool TpmInit::IsTpmEnabled() { |
74 return tpm_init_task_->IsTpmEnabled(); | 105 return tpm_init_task_->get_tpm()->IsEnabled(); |
75 } | 106 } |
76 | 107 |
77 bool TpmInit::IsTpmOwned() { | 108 bool TpmInit::IsTpmOwned() { |
78 return tpm_init_task_->IsTpmOwned(); | 109 return tpm_init_task_->get_tpm()->IsOwned(); |
79 } | 110 } |
80 | 111 |
81 bool TpmInit::IsTpmBeingOwned() { | 112 bool TpmInit::IsTpmBeingOwned() { |
82 return tpm_init_task_->IsTpmBeingOwned(); | 113 return tpm_init_task_->get_tpm()->IsBeingOwned(); |
| 114 } |
| 115 |
| 116 bool TpmInit::HasInitializeBeenCalled() { |
| 117 return initialize_called_; |
83 } | 118 } |
84 | 119 |
85 bool TpmInit::GetTpmPassword(chromeos::Blob* password) { | 120 bool TpmInit::GetTpmPassword(chromeos::Blob* password) { |
86 return tpm_init_task_->GetTpmPassword(password); | 121 return tpm_init_task_->get_tpm()->GetOwnerPassword(password); |
| 122 } |
| 123 |
| 124 void TpmInit::ClearStoredTpmPassword() { |
| 125 tpm_init_task_->get_tpm()->ClearStoredOwnerPassword(); |
87 } | 126 } |
88 | 127 |
89 long TpmInit::GetInitializationMillis() { | 128 long TpmInit::GetInitializationMillis() { |
90 return tpm_init_task_->GetInitializationMillis(); | 129 return initialization_time_; |
91 } | 130 } |
92 | 131 |
93 TpmInitTask::TpmInitTask() | 132 void TpmInit::ThreadMain() { |
94 : default_tpm_(new tpm_init::Tpm()), | |
95 tpm_(default_tpm_.get()), | |
96 initialize_took_ownership_(false), | |
97 task_done_(false), | |
98 initialization_time_(-1), | |
99 notify_callback_(NULL) { | |
100 } | |
101 | |
102 TpmInitTask::~TpmInitTask() { | |
103 } | |
104 | |
105 void TpmInitTask::Init(TpmInit::TpmInitCallback* notify_callback) { | |
106 notify_callback_ = notify_callback; | |
107 tpm_->Init(); | |
108 } | |
109 | |
110 void TpmInitTask::ThreadMain() { | |
111 base::TimeTicks start = base::TimeTicks::Now(); | 133 base::TimeTicks start = base::TimeTicks::Now(); |
112 bool initialize_result = tpm_->InitializeTpm(&initialize_took_ownership_); | 134 bool initialize_result = tpm_init_task_->get_tpm()->InitializeTpm( |
| 135 &initialize_took_ownership_); |
113 base::TimeDelta delta = (base::TimeTicks::Now() - start); | 136 base::TimeDelta delta = (base::TimeTicks::Now() - start); |
114 initialization_time_ = delta.InMilliseconds(); | 137 initialization_time_ = delta.InMilliseconds(); |
115 if (initialize_took_ownership_) { | 138 if (initialize_took_ownership_) { |
116 LOG(ERROR) << "TPM initialization took " << initialization_time_ << "ms"; | 139 LOG(ERROR) << "TPM initialization took " << initialization_time_ << "ms"; |
117 } | 140 } |
118 task_done_ = true; | 141 task_done_ = true; |
119 if (notify_callback_) { | 142 if (notify_callback_) { |
120 notify_callback_->InitializeTpmComplete(initialize_result, | 143 notify_callback_->InitializeTpmComplete(initialize_result, |
121 initialize_took_ownership_); | 144 initialize_took_ownership_); |
122 } | 145 } |
123 } | 146 } |
124 | 147 |
125 bool TpmInitTask::IsTpmReady() { | |
126 // The TPM is not "ready" if the init call has not completed. It may be in | |
127 // the middle of taking ownership. | |
128 if (!task_done_) { | |
129 return false; | |
130 } | |
131 // If initialize_took_ownership_ is true, then the TPM went through a full | |
132 // succesful ownership cycle in InitializeTpm() | |
133 if (initialize_took_ownership_) { | |
134 return true; | |
135 } | |
136 // If we get here, then the call to InitializeTpm() is complete and it | |
137 // returned false. That merely means that it did not successfully take | |
138 // ownership, which is the common case after ownership is established on OOBE. | |
139 // In that case, the TPM is ready if it is enabled and owned. | |
140 return (tpm_->IsEnabled() && tpm_->IsOwned()); | |
141 } | |
142 | |
143 bool TpmInitTask::IsTpmEnabled() { | |
144 return tpm_->IsEnabled(); | |
145 } | |
146 | |
147 bool TpmInitTask::IsTpmOwned() { | |
148 return tpm_->IsOwned(); | |
149 } | |
150 | |
151 bool TpmInitTask::IsTpmBeingOwned() { | |
152 return tpm_->IsBeingOwned(); | |
153 } | |
154 | |
155 bool TpmInitTask::GetTpmPassword(chromeos::Blob* password) { | |
156 return tpm_->GetOwnerPassword(password); | |
157 } | |
158 | |
159 long TpmInitTask::GetInitializationMillis() { | |
160 return initialization_time_; | |
161 } | |
162 | |
163 bool TpmInitTask::GetRandomData(int length, chromeos::Blob* data) { | |
164 return tpm_->GetRandomData(length, data); | |
165 } | |
166 | |
167 } // namespace tpm_init | 148 } // namespace tpm_init |
OLD | NEW |