tpmtest_timing.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /* Timing test for various TPM operations. This is mostly a sanity check to
  6. * make sure the part doesn't have ridicolously bad timing on simple
  7. * operations.
  8. */
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include <sys/time.h>
  13. #include <time.h>
  14. #include "tlcl.h"
  15. #include "tlcl_tests.h"
  16. #include "utility.h"
  17. /* Runs [op] and ensures it returns success and doesn't run longer than
  18. * [time_limit] in milliseconds.
  19. */
  20. #define TTPM_CHECK(op, time_limit) do { \
  21. struct timeval before, after; \
  22. int time; \
  23. uint32_t __result; \
  24. gettimeofday(&before, NULL); \
  25. __result = op; \
  26. if (__result != TPM_SUCCESS) { \
  27. printf(#op ": error 0x%x\n", __result); \
  28. errors++; \
  29. } \
  30. gettimeofday(&after, NULL); \
  31. time = (int) ((after.tv_sec - before.tv_sec) * 1000 + \
  32. (after.tv_usec - before.tv_usec) / 1000); \
  33. printf(#op ": %d ms\n", time); \
  34. if (time > time_limit) { \
  35. printf(#op " exceeded " #time_limit " ms\n"); \
  36. time_limit_exceeded = 1; \
  37. } \
  38. } while (0)
  39. int main(int argc, char** argv) {
  40. uint32_t x;
  41. uint8_t in[20], out[20];
  42. int time_limit_exceeded = 0;
  43. int errors = 0;
  44. TlclLibInit();
  45. TTPM_CHECK(0, 50);
  46. TTPM_CHECK(TlclStartupIfNeeded(), 50);
  47. TTPM_CHECK(TlclContinueSelfTest(), 100);
  48. TTPM_CHECK(TlclSelfTestFull(), 1000);
  49. TTPM_CHECK(TlclAssertPhysicalPresence(), 100);
  50. TTPM_CHECK(TlclWrite(INDEX0, (uint8_t*) &x, sizeof(x)), 100);
  51. TTPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x)), 100);
  52. TTPM_CHECK(TlclExtend(0, in, out), 200);
  53. TTPM_CHECK(TlclSetGlobalLock(), 50);
  54. TTPM_CHECK(TlclLockPhysicalPresence(), 100);
  55. if (time_limit_exceeded || errors > 0) {
  56. printf("TEST FAILED\n");
  57. exit(1);
  58. } else {
  59. printf("TEST SUCCEEDED\n");
  60. return 0;
  61. }
  62. }