tpm_init_temp_fix.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Copyright (c) 2010 The Chromium OS 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. *
  5. * Temporary fix for running the TPM selftest and other essential
  6. * intializations that we forgot to put in the BIOS.
  7. *
  8. * This works in a very specific situation: we assume TPM_Startup has been
  9. * executed, but we don't know if the self test has run. On a ST TPM version
  10. * 1.2.7.0, GetCapabilities fails before the self test has run, so we use that
  11. * to check if we need to run it.
  12. *
  13. * This also enables the TPM if it is disabled, and activates it if it is
  14. * deactivated.
  15. *
  16. * Exit status always 0. Prints "reboot" to request reboot, "fail" for errors,
  17. * "success" when everything worked.
  18. */
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <syslog.h>
  22. #include "tlcl.h"
  23. int main(int argc, char* argv[]) {
  24. uint32_t result;
  25. uint8_t disable, deactivated;
  26. int pri = LOG_USER | LOG_ERR;
  27. TlclLibInit();
  28. TlclStartup(); /* ignore result */
  29. /* On the dogfood device, GetFlags causes an assertion failure because the
  30. * device uses an older TPM which is not compatible with the current spec.
  31. * We take advantage of this to cause the program to exit and not run the
  32. * self test again (which takes 1 second).
  33. */
  34. result = TlclGetFlags(NULL, NULL, NULL);
  35. result = TlclSelfTestFull();
  36. if (result != 0) {
  37. syslog(pri, "TPM selftest failed with code 0x%x\n", result);
  38. printf("fail\n");
  39. return 0;
  40. }
  41. /* Optional one-time enabling of TPM. */
  42. result = TlclAssertPhysicalPresence();
  43. if (result != 0) {
  44. syslog(pri, "TPM assertpp failed with code 0x%x\n", result);
  45. printf("fail\n");
  46. return 0;
  47. }
  48. result = TlclGetFlags(&disable, &deactivated, NULL);
  49. if (result != 0) {
  50. syslog(pri, "TPM getflags failed with code 0x%x\n", result);
  51. printf("fail\n");
  52. return 0;
  53. }
  54. if (disable) {
  55. result = TlclSetEnable();
  56. if (result != 0) {
  57. syslog(pri, "TPM physical enable failed with code 0x%x\n", result);
  58. printf("fail\n");
  59. return 0;
  60. }
  61. }
  62. if (deactivated) {
  63. result = TlclSetDeactivated(0);
  64. if (result != 0) {
  65. syslog(pri, "TPM physical activate failed with code 0x%x\n", result);
  66. printf("fail\n");
  67. } else {
  68. printf("reboot\n");
  69. }
  70. return 0; /* needs reboot */
  71. }
  72. printf("success\n");
  73. return 0;
  74. }