tpm.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2004 IBM Corporation
  4. * Copyright (C) 2015 Intel Corporation
  5. *
  6. * Authors:
  7. * Leendert van Doorn <[email protected]>
  8. * Dave Safford <[email protected]>
  9. * Reiner Sailer <[email protected]>
  10. * Kylene Hall <[email protected]>
  11. *
  12. * Maintained by: <[email protected]>
  13. *
  14. * Device driver for TCG/TCPA TPM (trusted platform module).
  15. * Specifications at www.trustedcomputinggroup.org
  16. */
  17. #ifndef __TPM_H__
  18. #define __TPM_H__
  19. #include <linux/module.h>
  20. #include <linux/delay.h>
  21. #include <linux/mutex.h>
  22. #include <linux/sched.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/io.h>
  25. #include <linux/tpm.h>
  26. #include <linux/tpm_eventlog.h>
  27. #ifdef CONFIG_X86
  28. #include <asm/intel-family.h>
  29. #endif
  30. #define TPM_MINOR 224 /* officially assigned */
  31. #define TPM_BUFSIZE 4096
  32. #define TPM_NUM_DEVICES 65536
  33. #define TPM_RETRY 50
  34. enum tpm_timeout {
  35. TPM_TIMEOUT = 5, /* msecs */
  36. TPM_TIMEOUT_RETRY = 100, /* msecs */
  37. TPM_TIMEOUT_RANGE_US = 300, /* usecs */
  38. TPM_TIMEOUT_POLL = 1, /* msecs */
  39. TPM_TIMEOUT_USECS_MIN = 100, /* usecs */
  40. TPM_TIMEOUT_USECS_MAX = 500 /* usecs */
  41. };
  42. /* TPM addresses */
  43. enum tpm_addr {
  44. TPM_SUPERIO_ADDR = 0x2E,
  45. TPM_ADDR = 0x4E,
  46. };
  47. #define TPM_WARN_RETRY 0x800
  48. #define TPM_WARN_DOING_SELFTEST 0x802
  49. #define TPM_ERR_DEACTIVATED 0x6
  50. #define TPM_ERR_DISABLED 0x7
  51. #define TPM_ERR_FAILEDSELFTEST 0x1C
  52. #define TPM_ERR_INVALID_POSTINIT 38
  53. #define TPM_TAG_RQU_COMMAND 193
  54. /* TPM2 specific constants. */
  55. #define TPM2_SPACE_BUFFER_SIZE 16384 /* 16 kB */
  56. struct stclear_flags_t {
  57. __be16 tag;
  58. u8 deactivated;
  59. u8 disableForceClear;
  60. u8 physicalPresence;
  61. u8 physicalPresenceLock;
  62. u8 bGlobalLock;
  63. } __packed;
  64. struct tpm1_version {
  65. u8 major;
  66. u8 minor;
  67. u8 rev_major;
  68. u8 rev_minor;
  69. } __packed;
  70. struct tpm1_version2 {
  71. __be16 tag;
  72. struct tpm1_version version;
  73. } __packed;
  74. struct timeout_t {
  75. __be32 a;
  76. __be32 b;
  77. __be32 c;
  78. __be32 d;
  79. } __packed;
  80. struct duration_t {
  81. __be32 tpm_short;
  82. __be32 tpm_medium;
  83. __be32 tpm_long;
  84. } __packed;
  85. struct permanent_flags_t {
  86. __be16 tag;
  87. u8 disable;
  88. u8 ownership;
  89. u8 deactivated;
  90. u8 readPubek;
  91. u8 disableOwnerClear;
  92. u8 allowMaintenance;
  93. u8 physicalPresenceLifetimeLock;
  94. u8 physicalPresenceHWEnable;
  95. u8 physicalPresenceCMDEnable;
  96. u8 CEKPUsed;
  97. u8 TPMpost;
  98. u8 TPMpostLock;
  99. u8 FIPS;
  100. u8 operator;
  101. u8 enableRevokeEK;
  102. u8 nvLocked;
  103. u8 readSRKPub;
  104. u8 tpmEstablished;
  105. u8 maintenanceDone;
  106. u8 disableFullDALogicInfo;
  107. } __packed;
  108. typedef union {
  109. struct permanent_flags_t perm_flags;
  110. struct stclear_flags_t stclear_flags;
  111. __u8 owned;
  112. __be32 num_pcrs;
  113. struct tpm1_version version1;
  114. struct tpm1_version2 version2;
  115. __be32 manufacturer_id;
  116. struct timeout_t timeout;
  117. struct duration_t duration;
  118. } cap_t;
  119. enum tpm_capabilities {
  120. TPM_CAP_FLAG = 4,
  121. TPM_CAP_PROP = 5,
  122. TPM_CAP_VERSION_1_1 = 0x06,
  123. TPM_CAP_VERSION_1_2 = 0x1A,
  124. };
  125. enum tpm_sub_capabilities {
  126. TPM_CAP_PROP_PCR = 0x101,
  127. TPM_CAP_PROP_MANUFACTURER = 0x103,
  128. TPM_CAP_FLAG_PERM = 0x108,
  129. TPM_CAP_FLAG_VOL = 0x109,
  130. TPM_CAP_PROP_OWNER = 0x111,
  131. TPM_CAP_PROP_TIS_TIMEOUT = 0x115,
  132. TPM_CAP_PROP_TIS_DURATION = 0x120,
  133. };
  134. enum tpm2_pt_props {
  135. TPM2_PT_NONE = 0x00000000,
  136. TPM2_PT_GROUP = 0x00000100,
  137. TPM2_PT_FIXED = TPM2_PT_GROUP * 1,
  138. TPM2_PT_FAMILY_INDICATOR = TPM2_PT_FIXED + 0,
  139. TPM2_PT_LEVEL = TPM2_PT_FIXED + 1,
  140. TPM2_PT_REVISION = TPM2_PT_FIXED + 2,
  141. TPM2_PT_DAY_OF_YEAR = TPM2_PT_FIXED + 3,
  142. TPM2_PT_YEAR = TPM2_PT_FIXED + 4,
  143. TPM2_PT_MANUFACTURER = TPM2_PT_FIXED + 5,
  144. TPM2_PT_VENDOR_STRING_1 = TPM2_PT_FIXED + 6,
  145. TPM2_PT_VENDOR_STRING_2 = TPM2_PT_FIXED + 7,
  146. TPM2_PT_VENDOR_STRING_3 = TPM2_PT_FIXED + 8,
  147. TPM2_PT_VENDOR_STRING_4 = TPM2_PT_FIXED + 9,
  148. TPM2_PT_VENDOR_TPM_TYPE = TPM2_PT_FIXED + 10,
  149. TPM2_PT_FIRMWARE_VERSION_1 = TPM2_PT_FIXED + 11,
  150. TPM2_PT_FIRMWARE_VERSION_2 = TPM2_PT_FIXED + 12,
  151. TPM2_PT_INPUT_BUFFER = TPM2_PT_FIXED + 13,
  152. TPM2_PT_HR_TRANSIENT_MIN = TPM2_PT_FIXED + 14,
  153. TPM2_PT_HR_PERSISTENT_MIN = TPM2_PT_FIXED + 15,
  154. TPM2_PT_HR_LOADED_MIN = TPM2_PT_FIXED + 16,
  155. TPM2_PT_ACTIVE_SESSIONS_MAX = TPM2_PT_FIXED + 17,
  156. TPM2_PT_PCR_COUNT = TPM2_PT_FIXED + 18,
  157. TPM2_PT_PCR_SELECT_MIN = TPM2_PT_FIXED + 19,
  158. TPM2_PT_CONTEXT_GAP_MAX = TPM2_PT_FIXED + 20,
  159. TPM2_PT_NV_COUNTERS_MAX = TPM2_PT_FIXED + 22,
  160. TPM2_PT_NV_INDEX_MAX = TPM2_PT_FIXED + 23,
  161. TPM2_PT_MEMORY = TPM2_PT_FIXED + 24,
  162. TPM2_PT_CLOCK_UPDATE = TPM2_PT_FIXED + 25,
  163. TPM2_PT_CONTEXT_HASH = TPM2_PT_FIXED + 26,
  164. TPM2_PT_CONTEXT_SYM = TPM2_PT_FIXED + 27,
  165. TPM2_PT_CONTEXT_SYM_SIZE = TPM2_PT_FIXED + 28,
  166. TPM2_PT_ORDERLY_COUNT = TPM2_PT_FIXED + 29,
  167. TPM2_PT_MAX_COMMAND_SIZE = TPM2_PT_FIXED + 30,
  168. TPM2_PT_MAX_RESPONSE_SIZE = TPM2_PT_FIXED + 31,
  169. TPM2_PT_MAX_DIGEST = TPM2_PT_FIXED + 32,
  170. TPM2_PT_MAX_OBJECT_CONTEXT = TPM2_PT_FIXED + 33,
  171. TPM2_PT_MAX_SESSION_CONTEXT = TPM2_PT_FIXED + 34,
  172. TPM2_PT_PS_FAMILY_INDICATOR = TPM2_PT_FIXED + 35,
  173. TPM2_PT_PS_LEVEL = TPM2_PT_FIXED + 36,
  174. TPM2_PT_PS_REVISION = TPM2_PT_FIXED + 37,
  175. TPM2_PT_PS_DAY_OF_YEAR = TPM2_PT_FIXED + 38,
  176. TPM2_PT_PS_YEAR = TPM2_PT_FIXED + 39,
  177. TPM2_PT_SPLIT_MAX = TPM2_PT_FIXED + 40,
  178. TPM2_PT_TOTAL_COMMANDS = TPM2_PT_FIXED + 41,
  179. TPM2_PT_LIBRARY_COMMANDS = TPM2_PT_FIXED + 42,
  180. TPM2_PT_VENDOR_COMMANDS = TPM2_PT_FIXED + 43,
  181. TPM2_PT_NV_BUFFER_MAX = TPM2_PT_FIXED + 44,
  182. TPM2_PT_MODES = TPM2_PT_FIXED + 45,
  183. TPM2_PT_MAX_CAP_BUFFER = TPM2_PT_FIXED + 46,
  184. TPM2_PT_VAR = TPM2_PT_GROUP * 2,
  185. TPM2_PT_PERMANENT = TPM2_PT_VAR + 0,
  186. TPM2_PT_STARTUP_CLEAR = TPM2_PT_VAR + 1,
  187. TPM2_PT_HR_NV_INDEX = TPM2_PT_VAR + 2,
  188. TPM2_PT_HR_LOADED = TPM2_PT_VAR + 3,
  189. TPM2_PT_HR_LOADED_AVAIL = TPM2_PT_VAR + 4,
  190. TPM2_PT_HR_ACTIVE = TPM2_PT_VAR + 5,
  191. TPM2_PT_HR_ACTIVE_AVAIL = TPM2_PT_VAR + 6,
  192. TPM2_PT_HR_TRANSIENT_AVAIL = TPM2_PT_VAR + 7,
  193. TPM2_PT_HR_PERSISTENT = TPM2_PT_VAR + 8,
  194. TPM2_PT_HR_PERSISTENT_AVAIL = TPM2_PT_VAR + 9,
  195. TPM2_PT_NV_COUNTERS = TPM2_PT_VAR + 10,
  196. TPM2_PT_NV_COUNTERS_AVAIL = TPM2_PT_VAR + 11,
  197. TPM2_PT_ALGORITHM_SET = TPM2_PT_VAR + 12,
  198. TPM2_PT_LOADED_CURVES = TPM2_PT_VAR + 13,
  199. TPM2_PT_LOCKOUT_COUNTER = TPM2_PT_VAR + 14,
  200. TPM2_PT_MAX_AUTH_FAIL = TPM2_PT_VAR + 15,
  201. TPM2_PT_LOCKOUT_INTERVAL = TPM2_PT_VAR + 16,
  202. TPM2_PT_LOCKOUT_RECOVERY = TPM2_PT_VAR + 17,
  203. TPM2_PT_NV_WRITE_RECOVERY = TPM2_PT_VAR + 18,
  204. TPM2_PT_AUDIT_COUNTER_0 = TPM2_PT_VAR + 19,
  205. TPM2_PT_AUDIT_COUNTER_1 = TPM2_PT_VAR + 20,
  206. };
  207. /* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18
  208. * bytes, but 128 is still a relatively large number of random bytes and
  209. * anything much bigger causes users of struct tpm_cmd_t to start getting
  210. * compiler warnings about stack frame size. */
  211. #define TPM_MAX_RNG_DATA 128
  212. extern struct class *tpm_class;
  213. extern struct class *tpmrm_class;
  214. extern dev_t tpm_devt;
  215. extern const struct file_operations tpm_fops;
  216. extern const struct file_operations tpmrm_fops;
  217. extern struct idr dev_nums_idr;
  218. ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz);
  219. int tpm_get_timeouts(struct tpm_chip *);
  220. int tpm_auto_startup(struct tpm_chip *chip);
  221. int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr);
  222. int tpm1_auto_startup(struct tpm_chip *chip);
  223. int tpm1_do_selftest(struct tpm_chip *chip);
  224. int tpm1_get_timeouts(struct tpm_chip *chip);
  225. unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
  226. int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
  227. const char *log_msg);
  228. int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
  229. ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
  230. const char *desc, size_t min_cap_length);
  231. int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max);
  232. int tpm1_get_pcr_allocation(struct tpm_chip *chip);
  233. unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
  234. int tpm_pm_suspend(struct device *dev);
  235. int tpm_pm_resume(struct device *dev);
  236. static inline void tpm_msleep(unsigned int delay_msec)
  237. {
  238. usleep_range((delay_msec * 1000) - TPM_TIMEOUT_RANGE_US,
  239. delay_msec * 1000);
  240. };
  241. int tpm_chip_bootstrap(struct tpm_chip *chip);
  242. int tpm_chip_start(struct tpm_chip *chip);
  243. void tpm_chip_stop(struct tpm_chip *chip);
  244. struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip);
  245. struct tpm_chip *tpm_chip_alloc(struct device *dev,
  246. const struct tpm_class_ops *ops);
  247. struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
  248. const struct tpm_class_ops *ops);
  249. int tpm_chip_register(struct tpm_chip *chip);
  250. void tpm_chip_unregister(struct tpm_chip *chip);
  251. void tpm_sysfs_add_device(struct tpm_chip *chip);
  252. #ifdef CONFIG_ACPI
  253. extern void tpm_add_ppi(struct tpm_chip *chip);
  254. #else
  255. static inline void tpm_add_ppi(struct tpm_chip *chip)
  256. {
  257. }
  258. #endif
  259. int tpm2_get_timeouts(struct tpm_chip *chip);
  260. int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
  261. struct tpm_digest *digest, u16 *digest_size_ptr);
  262. int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
  263. struct tpm_digest *digests);
  264. int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max);
  265. ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
  266. u32 *value, const char *desc);
  267. ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip);
  268. int tpm2_auto_startup(struct tpm_chip *chip);
  269. void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
  270. unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
  271. int tpm2_probe(struct tpm_chip *chip);
  272. int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip);
  273. int tpm2_find_cc(struct tpm_chip *chip, u32 cc);
  274. int tpm2_init_space(struct tpm_space *space, unsigned int buf_size);
  275. void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space);
  276. void tpm2_flush_space(struct tpm_chip *chip);
  277. int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
  278. size_t cmdsiz);
  279. int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space, void *buf,
  280. size_t *bufsiz);
  281. int tpm_devs_add(struct tpm_chip *chip);
  282. void tpm_devs_remove(struct tpm_chip *chip);
  283. void tpm_bios_log_setup(struct tpm_chip *chip);
  284. void tpm_bios_log_teardown(struct tpm_chip *chip);
  285. int tpm_dev_common_init(void);
  286. void tpm_dev_common_exit(void);
  287. #endif