tpm-common.c 6.2 KB

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