tpm-common.c 6.2 KB

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