prepare_tpm.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/components/tpm/prepare_tpm.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/callback.h"
  8. #include "base/logging.h"
  9. #include "chromeos/dbus/tpm_manager/tpm_manager.pb.h"
  10. #include "chromeos/dbus/tpm_manager/tpm_manager_client.h"
  11. namespace ash {
  12. namespace {
  13. void OnClearStoredOwnerPassword(
  14. base::OnceClosure preparation_finished_callback,
  15. const ::tpm_manager::ClearStoredOwnerPasswordReply& reply) {
  16. LOG_IF(ERROR, reply.status() != ::tpm_manager::STATUS_SUCCESS)
  17. << "Failed to call ClearStoredOwnerPassword; status: " << reply.status();
  18. std::move(preparation_finished_callback).Run();
  19. }
  20. void OnTakeOwnership(base::OnceClosure preparation_finished_callback,
  21. const ::tpm_manager::TakeOwnershipReply& reply) {
  22. LOG_IF(ERROR, reply.status() != ::tpm_manager::STATUS_SUCCESS)
  23. << "Failed to call TakeOwnership; status: " << reply.status();
  24. std::move(preparation_finished_callback).Run();
  25. }
  26. void OnGetTpmStatus(base::OnceClosure preparation_finished_callback,
  27. const ::tpm_manager::GetTpmNonsensitiveStatusReply& reply) {
  28. if (reply.status() != ::tpm_manager::STATUS_SUCCESS) {
  29. LOG(WARNING) << " Failed to get tpm status; status: " << reply.status();
  30. }
  31. if (reply.status() != ::tpm_manager::STATUS_SUCCESS || !reply.is_enabled()) {
  32. LOG_IF(WARNING, !reply.is_enabled()) << "TPM is reportedly disabled.";
  33. std::move(preparation_finished_callback).Run();
  34. return;
  35. }
  36. if (reply.is_owned()) {
  37. TpmManagerClient::Get()->ClearStoredOwnerPassword(
  38. ::tpm_manager::ClearStoredOwnerPasswordRequest(),
  39. base::BindOnce(OnClearStoredOwnerPassword,
  40. std::move(preparation_finished_callback)));
  41. } else {
  42. TpmManagerClient::Get()->TakeOwnership(
  43. ::tpm_manager::TakeOwnershipRequest(),
  44. base::BindOnce(OnTakeOwnership,
  45. std::move(preparation_finished_callback)));
  46. }
  47. }
  48. } // namespace
  49. void PrepareTpm(base::OnceClosure preparation_finished_callback) {
  50. TpmManagerClient::Get()->GetTpmNonsensitiveStatus(
  51. ::tpm_manager::GetTpmNonsensitiveStatusRequest(),
  52. base::BindOnce(OnGetTpmStatus, std::move(preparation_finished_callback)));
  53. }
  54. } // namespace ash