rollback_index.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Copyright (c) 2013 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. * Functions for querying, manipulating and locking rollback indices
  6. * stored in the TPM NVRAM.
  7. */
  8. #ifndef VBOOT_REFERENCE_ROLLBACK_INDEX_H_
  9. #define VBOOT_REFERENCE_ROLLBACK_INDEX_H_
  10. #include "sysincludes.h"
  11. #include "tss_constants.h"
  12. /* TPM NVRAM location indices. */
  13. #define FIRMWARE_NV_INDEX 0x1007
  14. #define KERNEL_NV_INDEX 0x1008
  15. /* This is just an opaque space for backup purposes */
  16. #define BACKUP_NV_INDEX 0x1009
  17. #define BACKUP_NV_SIZE 16
  18. #define FWMP_NV_INDEX 0x100a
  19. #define FWMP_NV_MAX_SIZE 128
  20. #define REC_HASH_NV_INDEX 0x100b
  21. #define REC_HASH_NV_SIZE VB2_SHA256_DIGEST_SIZE
  22. /* Structure definitions for TPM spaces */
  23. /* Kernel space - KERNEL_NV_INDEX, locked with physical presence. */
  24. #define ROLLBACK_SPACE_KERNEL_VERSION 2
  25. #define ROLLBACK_SPACE_KERNEL_UID 0x4752574C /* 'GRWL' */
  26. typedef struct RollbackSpaceKernel {
  27. /* Struct version, for backwards compatibility */
  28. uint8_t struct_version;
  29. /* Unique ID to detect space redefinition */
  30. uint32_t uid;
  31. /* Kernel versions */
  32. uint32_t kernel_versions;
  33. /* Reserved for future expansion */
  34. uint8_t reserved[3];
  35. /* Checksum (v2 and later only) */
  36. uint8_t crc8;
  37. } __attribute__((packed)) RollbackSpaceKernel;
  38. /* Flags for firmware space */
  39. /*
  40. * Last boot was developer mode. TPM ownership is cleared when transitioning
  41. * to/from developer mode.
  42. */
  43. #define FLAG_LAST_BOOT_DEVELOPER 0x01
  44. /*
  45. * Some systems may not have a dedicated dev-mode switch, but enter and leave
  46. * dev-mode through some recovery-mode magic keypresses. For those systems, the
  47. * dev-mode "switch" state is in this bit (0=normal, 1=dev). To make it work, a
  48. * new flag is passed to VbInit(), indicating that the system lacks a physical
  49. * dev-mode switch. If a physical switch is present, this bit is ignored.
  50. */
  51. #define FLAG_VIRTUAL_DEV_MODE_ON 0x02
  52. /* Firmware space - FIRMWARE_NV_INDEX, locked with global lock. */
  53. #define ROLLBACK_SPACE_FIRMWARE_VERSION 2
  54. typedef struct RollbackSpaceFirmware {
  55. /* Struct version, for backwards compatibility */
  56. uint8_t struct_version;
  57. /* Flags (see FLAG_* above) */
  58. uint8_t flags;
  59. /* Firmware versions */
  60. uint32_t fw_versions;
  61. /* Reserved for future expansion */
  62. uint8_t reserved[3];
  63. /* Checksum (v2 and later only) */
  64. uint8_t crc8;
  65. } __attribute__((packed)) RollbackSpaceFirmware;
  66. #define FWMP_HASH_SIZE 32 /* Enough for SHA-256 */
  67. /* Firmware management parameters */
  68. struct RollbackSpaceFwmp {
  69. /* CRC-8 of fields following struct_size */
  70. uint8_t crc;
  71. /* Structure size in bytes */
  72. uint8_t struct_size;
  73. /* Structure version */
  74. uint8_t struct_version;
  75. /* Reserved; ignored by current reader */
  76. uint8_t reserved0;
  77. /* Flags; see enum fwmp_flags */
  78. uint32_t flags;
  79. /* Hash of developer kernel key */
  80. uint8_t dev_key_hash[FWMP_HASH_SIZE];
  81. } __attribute__((packed));
  82. #define ROLLBACK_SPACE_FWMP_VERSION 0x10 /* 1.0 */
  83. enum fwmp_flags {
  84. FWMP_DEV_DISABLE_BOOT = (1 << 0),
  85. FWMP_DEV_DISABLE_RECOVERY = (1 << 1),
  86. FWMP_DEV_ENABLE_USB = (1 << 2),
  87. FWMP_DEV_ENABLE_LEGACY = (1 << 3),
  88. FWMP_DEV_ENABLE_OFFICIAL_ONLY = (1 << 4),
  89. FWMP_DEV_USE_KEY_HASH = (1 << 5),
  90. };
  91. /* All functions return TPM_SUCCESS (zero) if successful, non-zero if error */
  92. /*
  93. * These functions are callable from VbSelectAndLoadKernel(). They may use
  94. * global variables.
  95. */
  96. /**
  97. * Read stored kernel version.
  98. */
  99. uint32_t RollbackKernelRead(uint32_t *version);
  100. /**
  101. * Write stored kernel version.
  102. */
  103. uint32_t RollbackKernelWrite(uint32_t version);
  104. /**
  105. * Lock must be called. Internally, it's ignored in recovery mode.
  106. */
  107. uint32_t RollbackKernelLock(int recovery_mode);
  108. /**
  109. * Read and validate firmware management parameters.
  110. *
  111. * Absence of a FWMP is not an error; in this case, fwmp will be cleared.
  112. *
  113. * Returns non-zero if error.
  114. */
  115. uint32_t RollbackFwmpRead(struct RollbackSpaceFwmp *fwmp);
  116. /****************************************************************************/
  117. /*
  118. * The following functions are internal apis, listed here for use by unit tests
  119. * only.
  120. */
  121. /**
  122. * Issue a TPM_Clear and reenable/reactivate the TPM.
  123. */
  124. uint32_t TPMClearAndReenable(void);
  125. /**
  126. * Like TlclWrite(), but checks for write errors due to hitting the 64-write
  127. * limit and clears the TPM when that happens. This can only happen when the
  128. * TPM is unowned, so it is OK to clear it (and we really have no choice).
  129. * This is not expected to happen frequently, but it could happen.
  130. */
  131. uint32_t SafeWrite(uint32_t index, const void *data, uint32_t length);
  132. /**
  133. * Utility function to turn the virtual dev-mode flag on or off. 0=off, 1=on.
  134. */
  135. uint32_t SetVirtualDevMode(int val);
  136. #endif /* VBOOT_REFERENCE_ROLLBACK_INDEX_H_ */