tpm-common.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013 The Chromium OS Authors.
  4. */
  5. #include <common.h>
  6. #include <command.h>
  7. #include <dm.h>
  8. #include <env.h>
  9. #include <malloc.h>
  10. #include <asm/unaligned.h>
  11. #include <linux/string.h>
  12. #include <tpm-common.h>
  13. #include "tpm-user-utils.h"
  14. static struct udevice *tpm_dev;
  15. /**
  16. * Print a byte string in hexdecimal format, 16-bytes per line.
  17. *
  18. * @param data byte string to be printed
  19. * @param count number of bytes to be printed
  20. */
  21. void print_byte_string(u8 *data, size_t count)
  22. {
  23. int i, print_newline = 0;
  24. for (i = 0; i < count; i++) {
  25. printf(" %02x", data[i]);
  26. print_newline = (i % 16 == 15);
  27. if (print_newline)
  28. putc('\n');
  29. }
  30. /* Avoid duplicated newline at the end */
  31. if (!print_newline)
  32. putc('\n');
  33. }
  34. /**
  35. * Convert a text string of hexdecimal values into a byte string.
  36. *
  37. * @param bytes text string of hexdecimal values with no space
  38. * between them
  39. * @param data output buffer for byte string. The caller has to make
  40. * sure it is large enough for storing the output. If
  41. * NULL is passed, a large enough buffer will be allocated,
  42. * and the caller must free it.
  43. * @param count_ptr output variable for the length of byte string
  44. * @return pointer to output buffer
  45. */
  46. void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr)
  47. {
  48. char byte[3];
  49. size_t count, length;
  50. int i;
  51. if (!bytes)
  52. return NULL;
  53. length = strlen(bytes);
  54. count = length / 2;
  55. if (!data)
  56. data = malloc(count);
  57. if (!data)
  58. return NULL;
  59. byte[2] = '\0';
  60. for (i = 0; i < length; i += 2) {
  61. byte[0] = bytes[i];
  62. byte[1] = bytes[i + 1];
  63. data[i / 2] = (u8)hextoul(byte, NULL);
  64. }
  65. if (count_ptr)
  66. *count_ptr = count;
  67. return data;
  68. }
  69. /**
  70. * report_return_code() - Report any error and return failure or success
  71. *
  72. * @param return_code TPM command return code
  73. * @return value of enum command_ret_t
  74. */
  75. int report_return_code(int return_code)
  76. {
  77. if (return_code) {
  78. printf("Error: %d\n", return_code);
  79. return CMD_RET_FAILURE;
  80. } else {
  81. return CMD_RET_SUCCESS;
  82. }
  83. }
  84. /**
  85. * Return number of values defined by a type string.
  86. *
  87. * @param type_str type string
  88. * @return number of values of type string
  89. */
  90. int type_string_get_num_values(const char *type_str)
  91. {
  92. return strlen(type_str);
  93. }
  94. /**
  95. * Return total size of values defined by a type string.
  96. *
  97. * @param type_str type string
  98. * @return total size of values of type string, or 0 if type string
  99. * contains illegal type character.
  100. */
  101. size_t type_string_get_space_size(const char *type_str)
  102. {
  103. size_t size;
  104. for (size = 0; *type_str; type_str++) {
  105. switch (*type_str) {
  106. case 'b':
  107. size += 1;
  108. break;
  109. case 'w':
  110. size += 2;
  111. break;
  112. case 'd':
  113. size += 4;
  114. break;
  115. default:
  116. return 0;
  117. }
  118. }
  119. return size;
  120. }
  121. /**
  122. * Allocate a buffer large enough to hold values defined by a type
  123. * string. The caller has to free the buffer.
  124. *
  125. * @param type_str type string
  126. * @param count pointer for storing size of buffer
  127. * @return pointer to buffer or NULL on error
  128. */
  129. void *type_string_alloc(const char *type_str, u32 *count)
  130. {
  131. void *data;
  132. size_t size;
  133. size = type_string_get_space_size(type_str);
  134. if (!size)
  135. return NULL;
  136. data = malloc(size);
  137. if (data)
  138. *count = size;
  139. return data;
  140. }
  141. /**
  142. * Pack values defined by a type string into a buffer. The buffer must have
  143. * large enough space.
  144. *
  145. * @param type_str type string
  146. * @param values text strings of values to be packed
  147. * @param data output buffer of values
  148. * @return 0 on success, non-0 on error
  149. */
  150. int type_string_pack(const char *type_str, char * const values[],
  151. u8 *data)
  152. {
  153. size_t offset;
  154. u32 value;
  155. for (offset = 0; *type_str; type_str++, values++) {
  156. value = simple_strtoul(values[0], NULL, 0);
  157. switch (*type_str) {
  158. case 'b':
  159. data[offset] = value;
  160. offset += 1;
  161. break;
  162. case 'w':
  163. put_unaligned_be16(value, data + offset);
  164. offset += 2;
  165. break;
  166. case 'd':
  167. put_unaligned_be32(value, data + offset);
  168. offset += 4;
  169. break;
  170. default:
  171. return -1;
  172. }
  173. }
  174. return 0;
  175. }
  176. /**
  177. * Read values defined by a type string from a buffer, and write these values
  178. * to environment variables.
  179. *
  180. * @param type_str type string
  181. * @param data input buffer of values
  182. * @param vars names of environment variables
  183. * @return 0 on success, non-0 on error
  184. */
  185. int type_string_write_vars(const char *type_str, u8 *data,
  186. char * const vars[])
  187. {
  188. size_t offset;
  189. u32 value;
  190. for (offset = 0; *type_str; type_str++, vars++) {
  191. switch (*type_str) {
  192. case 'b':
  193. value = data[offset];
  194. offset += 1;
  195. break;
  196. case 'w':
  197. value = get_unaligned_be16(data + offset);
  198. offset += 2;
  199. break;
  200. case 'd':
  201. value = get_unaligned_be32(data + offset);
  202. offset += 4;
  203. break;
  204. default:
  205. return -1;
  206. }
  207. if (env_set_ulong(*vars, value))
  208. return -1;
  209. }
  210. return 0;
  211. }
  212. static int tpm_show_device(void)
  213. {
  214. struct udevice *dev;
  215. char buf[80];
  216. int n = 0, rc;
  217. for_each_tpm_device(dev) {
  218. rc = tpm_get_desc(dev, buf, sizeof(buf));
  219. if (rc < 0)
  220. printf("device %d: can't get info\n", n);
  221. else
  222. printf("device %d: %s\n", n, buf);
  223. n++;
  224. };
  225. return 0;
  226. }
  227. static int tpm_set_device(unsigned long num)
  228. {
  229. struct udevice *dev;
  230. unsigned long n = 0;
  231. int rc = CMD_RET_FAILURE;
  232. for_each_tpm_device(dev) {
  233. if (n == num) {
  234. rc = 0;
  235. break;
  236. }
  237. n++;
  238. }
  239. if (!rc)
  240. tpm_dev = dev;
  241. return rc;
  242. }
  243. int get_tpm(struct udevice **devp)
  244. {
  245. int rc;
  246. /*
  247. * To keep a backward compatibility with previous code,
  248. * if a tpm device is not explicitly set, we set the first one.
  249. */
  250. if (!tpm_dev) {
  251. rc = tpm_set_device(0);
  252. if (rc) {
  253. printf("Couldn't set TPM 0 (rc = %d)\n", rc);
  254. return CMD_RET_FAILURE;
  255. }
  256. }
  257. if (devp)
  258. *devp = tpm_dev;
  259. return 0;
  260. }
  261. int do_tpm_device(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  262. {
  263. unsigned long num;
  264. int rc;
  265. if (argc == 2) {
  266. num = dectoul(argv[1], NULL);
  267. rc = tpm_set_device(num);
  268. if (rc)
  269. printf("Couldn't set TPM %lu (rc = %d)\n", num, rc);
  270. } else {
  271. rc = tpm_show_device();
  272. }
  273. return rc;
  274. }
  275. int do_tpm_info(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  276. {
  277. struct udevice *dev;
  278. char buf[80];
  279. int rc;
  280. rc = get_tpm(&dev);
  281. if (rc)
  282. return rc;
  283. rc = tpm_get_desc(dev, buf, sizeof(buf));
  284. if (rc < 0) {
  285. printf("Couldn't get TPM info (%d)\n", rc);
  286. return CMD_RET_FAILURE;
  287. }
  288. printf("%s\n", buf);
  289. return 0;
  290. }
  291. int do_tpm_init(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  292. {
  293. struct udevice *dev;
  294. int rc;
  295. if (argc != 1)
  296. return CMD_RET_USAGE;
  297. rc = get_tpm(&dev);
  298. if (rc)
  299. return rc;
  300. return report_return_code(tpm_init(dev));
  301. }
  302. int do_tpm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  303. {
  304. struct cmd_tbl *tpm_commands, *cmd;
  305. struct tpm_chip_priv *priv;
  306. struct udevice *dev;
  307. unsigned int size;
  308. int ret;
  309. if (argc < 2)
  310. return CMD_RET_USAGE;
  311. ret = get_tpm(&dev);
  312. if (ret)
  313. return ret;
  314. priv = dev_get_uclass_priv(dev);
  315. /* Below getters return NULL if the desired stack is not built */
  316. switch (priv->version) {
  317. case TPM_V1:
  318. tpm_commands = get_tpm1_commands(&size);
  319. break;
  320. case TPM_V2:
  321. tpm_commands = get_tpm2_commands(&size);
  322. break;
  323. default:
  324. tpm_commands = NULL;
  325. }
  326. if (!tpm_commands)
  327. return CMD_RET_USAGE;
  328. cmd = find_cmd_tbl(argv[1], tpm_commands, size);
  329. if (!cmd)
  330. return CMD_RET_USAGE;
  331. return cmd->cmd(cmdtp, flag, argc - 1, argv + 1);
  332. }