File: tcg2.c

package info (click to toggle)
grub2 2.14~git20250718.0e36779-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 60,688 kB
  • sloc: ansic: 541,811; asm: 68,074; sh: 9,803; cpp: 2,095; makefile: 1,895; python: 1,518; sed: 446; lex: 393; yacc: 268; awk: 85; lisp: 54; perl: 31
file content (157 lines) | stat: -rw-r--r-- 4,449 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2024 IBM Corporation
 *  Copyright (C) 2024 Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/types.h>
#include <grub/tpm.h>
#include <grub/ieee1275/tpm.h>
#include <grub/mm.h>
#include <grub/misc.h>

#include <tcg2.h>

grub_ieee1275_ihandle_t grub_ieee1275_tpm_ihandle = GRUB_IEEE1275_IHANDLE_INVALID;

grub_err_t
grub_ieee1275_tpm_init (void)
{
  static bool init_tried = false;
  grub_ieee1275_phandle_t vtpm;
  char buffer[20];

  if (init_tried == false)
    {
      init_tried = true;

      if (grub_ieee1275_open ("/vdevice/vtpm", &grub_ieee1275_tpm_ihandle) < 0 ||
	  grub_ieee1275_finddevice ("/vdevice/vtpm", &vtpm) ||
	  grub_ieee1275_get_property (vtpm, "compatible", buffer, sizeof (buffer), NULL) ||
	  grub_strcmp (buffer, "IBM,vtpm20"))
	{
	  if (grub_ieee1275_tpm_ihandle != GRUB_IEEE1275_IHANDLE_INVALID)
	    grub_ieee1275_close (grub_ieee1275_tpm_ihandle);

	  grub_ieee1275_tpm_ihandle = GRUB_IEEE1275_IHANDLE_INVALID;
	}
    }

  if (grub_ieee1275_tpm_ihandle == GRUB_IEEE1275_IHANDLE_INVALID)
    return GRUB_ERR_UNKNOWN_DEVICE;

  return GRUB_ERR_NONE;
}

grub_err_t
grub_tcg2_get_max_output_size (grub_size_t *size)
{
  struct tpm_get_maximum_cmd_size
  {
    struct grub_ieee1275_common_hdr common;
    grub_ieee1275_cell_t method;
    grub_ieee1275_cell_t ihandle;
    grub_ieee1275_cell_t catch_result;
    grub_ieee1275_cell_t size;
  };
  struct tpm_get_maximum_cmd_size args;
  static bool error_displayed = false;
  grub_err_t err;

  err = grub_ieee1275_tpm_init ();
  if (err != GRUB_ERR_NONE)
      return err;

  INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 2);
  args.method = (grub_ieee1275_cell_t) "get-maximum-cmd-size";
  args.ihandle = grub_ieee1275_tpm_ihandle;

  if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
    return GRUB_ERR_INVALID_COMMAND;

  /*
   * args.catch_result is set if firmware does not support get-maximum-cmd-size.
   * rc is GRUB_IEEE1275_CELL_FALSE (0) on failure.
   */
  if (args.catch_result)
    {
      if (error_displayed == false)
	{
	  error_displayed = true;
	  return grub_error (GRUB_ERR_BAD_DEVICE,
			     "get-maximum-cmd-size failed: Firmware is likely too old");
	}
      return GRUB_ERR_INVALID_COMMAND;
    }

  *size = args.size;

  return GRUB_ERR_NONE;
}

grub_err_t
grub_tcg2_submit_command (grub_size_t input_size,
			  grub_uint8_t *input,
			  grub_size_t output_size,
			  grub_uint8_t *output)
{
  struct tpm_pass_through_to_tpm
  {
    struct grub_ieee1275_common_hdr common;
    grub_ieee1275_cell_t method;
    grub_ieee1275_cell_t ihandle;
    grub_ieee1275_cell_t buf_size;
    grub_ieee1275_cell_t buf_addr;
    grub_ieee1275_cell_t catch_result;
    grub_ieee1275_cell_t resp_size;
  };
  struct tpm_pass_through_to_tpm args;
  static bool error_displayed = false;
  grub_err_t err;

  if (input_size == 0 || input == NULL ||
      output_size == 0 || output == NULL)
    return GRUB_ERR_BAD_ARGUMENT;

  err = grub_ieee1275_tpm_init ();
  if (err != GRUB_ERR_NONE)
      return err;

  INIT_IEEE1275_COMMON (&args.common, "call-method", 4, 2);
  args.method = (grub_ieee1275_cell_t) "pass-through-to-tpm";
  args.ihandle = grub_ieee1275_tpm_ihandle;
  args.buf_size = (grub_ieee1275_cell_t) input_size;
  args.buf_addr = (grub_ieee1275_cell_t) input;

  if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
    return GRUB_ERR_INVALID_COMMAND;

  /* args.catch_result is set if firmware does not support pass-through-to-tpm. */
  if (args.catch_result)
    {
      if (error_displayed == false)
	{
	  error_displayed = true;
	  return grub_error (GRUB_ERR_BAD_DEVICE,
			     "pass-through-to-tpm failed: Firmware is likely too old");
	}
      return GRUB_ERR_INVALID_COMMAND;
    }

  grub_memcpy (output, input, args.resp_size);

  return GRUB_ERR_NONE;
}