vaseboot/VasEBoot-core/lib/ieee1275/tcg2.c

223 lines
6.7 KiB
C

/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2024 IBM Corporation
* Copyright (C) 2024 Free Software Foundation, Inc.
*
* VAS_EBOOT 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.
*
* VAS_EBOOT 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 VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/types.h>
#include <VasEBoot/tpm.h>
#include <VasEBoot/ieee1275/tpm.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/misc.h>
#include <tcg2.h>
VasEBoot_ieee1275_ihandle_t VasEBoot_ieee1275_tpm_ihandle = VAS_EBOOT_IEEE1275_IHANDLE_INVALID;
VasEBoot_err_t
VasEBoot_ieee1275_tpm_init (void)
{
static bool init_tried = false;
VasEBoot_ieee1275_phandle_t vtpm;
char buffer[20];
if (init_tried == false)
{
init_tried = true;
if (VasEBoot_ieee1275_open ("/vdevice/vtpm", &VasEBoot_ieee1275_tpm_ihandle) < 0 ||
VasEBoot_ieee1275_finddevice ("/vdevice/vtpm", &vtpm) ||
VasEBoot_ieee1275_get_property (vtpm, "compatible", buffer, sizeof (buffer), NULL) ||
VasEBoot_strcmp (buffer, "IBM,vtpm20"))
{
if (VasEBoot_ieee1275_tpm_ihandle != VAS_EBOOT_IEEE1275_IHANDLE_INVALID)
VasEBoot_ieee1275_close (VasEBoot_ieee1275_tpm_ihandle);
VasEBoot_ieee1275_tpm_ihandle = VAS_EBOOT_IEEE1275_IHANDLE_INVALID;
}
}
if (VasEBoot_ieee1275_tpm_ihandle == VAS_EBOOT_IEEE1275_IHANDLE_INVALID)
return VAS_EBOOT_ERR_UNKNOWN_DEVICE;
return VAS_EBOOT_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_ieee1275_ibmvtpm_2hash_ext_log (VasEBoot_uint8_t pcrindex,
VasEBoot_uint32_t eventtype,
const char *description,
VasEBoot_size_t description_size,
void *buf, VasEBoot_size_t size)
{
struct tpm_2hash_ext_log
{
struct VasEBoot_ieee1275_common_hdr common;
VasEBoot_ieee1275_cell_t method;
VasEBoot_ieee1275_cell_t ihandle;
VasEBoot_ieee1275_cell_t size;
VasEBoot_ieee1275_cell_t buf;
VasEBoot_ieee1275_cell_t description_size;
VasEBoot_ieee1275_cell_t description;
VasEBoot_ieee1275_cell_t eventtype;
VasEBoot_ieee1275_cell_t pcrindex;
VasEBoot_ieee1275_cell_t catch_result;
VasEBoot_ieee1275_cell_t rc;
};
struct tpm_2hash_ext_log args;
INIT_IEEE1275_COMMON (&args.common, "call-method", 8, 2);
args.method = (VasEBoot_ieee1275_cell_t) "2hash-ext-log";
args.ihandle = VasEBoot_ieee1275_tpm_ihandle;
args.pcrindex = pcrindex;
args.eventtype = eventtype;
args.description = (VasEBoot_ieee1275_cell_t) description;
args.description_size = description_size;
args.buf = (VasEBoot_ieee1275_cell_t) buf;
args.size = (VasEBoot_ieee1275_cell_t) size;
if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_DEVICE, "2HASH-EXT-LOG failed: Firmware is likely too old.\n");
/*
* catch_result is set if firmware does not support 2hash-ext-log
* rc is VAS_EBOOT_IEEE1275_CELL_FALSE (0) on failure
*/
if ((args.catch_result) || args.rc == VAS_EBOOT_IEEE1275_CELL_FALSE)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_DEVICE, "2HASH-EXT-LOG failed: Firmware is likely too old.\n");
return VAS_EBOOT_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_tcg2_get_max_output_size (VasEBoot_size_t *size)
{
struct tpm_get_maximum_cmd_size
{
struct VasEBoot_ieee1275_common_hdr common;
VasEBoot_ieee1275_cell_t method;
VasEBoot_ieee1275_cell_t ihandle;
VasEBoot_ieee1275_cell_t catch_result;
VasEBoot_ieee1275_cell_t size;
};
struct tpm_get_maximum_cmd_size args;
static bool error_displayed = false;
VasEBoot_err_t err;
err = VasEBoot_ieee1275_tpm_init ();
if (err != VAS_EBOOT_ERR_NONE)
return err;
INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 2);
args.method = (VasEBoot_ieee1275_cell_t) "get-maximum-cmd-size";
args.ihandle = VasEBoot_ieee1275_tpm_ihandle;
if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
return VAS_EBOOT_ERR_INVALID_COMMAND;
/*
* args.catch_result is set if firmware does not support get-maximum-cmd-size.
* rc is VAS_EBOOT_IEEE1275_CELL_FALSE (0) on failure.
*/
if (args.catch_result)
{
if (error_displayed == false)
{
error_displayed = true;
return VasEBoot_error (VAS_EBOOT_ERR_BAD_DEVICE,
"get-maximum-cmd-size failed: Firmware is likely too old");
}
return VAS_EBOOT_ERR_INVALID_COMMAND;
}
*size = args.size;
return VAS_EBOOT_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_tcg2_submit_command (VasEBoot_size_t input_size,
VasEBoot_uint8_t *input,
VasEBoot_size_t output_size,
VasEBoot_uint8_t *output)
{
struct tpm_pass_through_to_tpm
{
struct VasEBoot_ieee1275_common_hdr common;
VasEBoot_ieee1275_cell_t method;
VasEBoot_ieee1275_cell_t ihandle;
VasEBoot_ieee1275_cell_t buf_size;
VasEBoot_ieee1275_cell_t buf_addr;
VasEBoot_ieee1275_cell_t catch_result;
VasEBoot_ieee1275_cell_t resp_size;
};
struct tpm_pass_through_to_tpm args;
static bool error_displayed = false;
VasEBoot_err_t err;
if (input_size == 0 || input == NULL ||
output_size == 0 || output == NULL)
return VAS_EBOOT_ERR_BAD_ARGUMENT;
err = VasEBoot_ieee1275_tpm_init ();
if (err != VAS_EBOOT_ERR_NONE)
return err;
INIT_IEEE1275_COMMON (&args.common, "call-method", 4, 2);
args.method = (VasEBoot_ieee1275_cell_t) "pass-through-to-tpm";
args.ihandle = VasEBoot_ieee1275_tpm_ihandle;
args.buf_size = (VasEBoot_ieee1275_cell_t) input_size;
args.buf_addr = (VasEBoot_ieee1275_cell_t) input;
if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
return VAS_EBOOT_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 VasEBoot_error (VAS_EBOOT_ERR_BAD_DEVICE,
"pass-through-to-tpm failed: Firmware is likely too old");
}
return VAS_EBOOT_ERR_INVALID_COMMAND;
}
VasEBoot_memcpy (output, input, args.resp_size);
return VAS_EBOOT_ERR_NONE;
}
VasEBoot_err_t
VasEBoot_tcg2_cap_pcr (VasEBoot_uint8_t pcr)
{
char separator[4] = {0};
static int error_displayed = 0;
VasEBoot_err_t err;
err = VasEBoot_ieee1275_ibmvtpm_2hash_ext_log (pcr, VAS_EBOOT_EV_SEPARATOR,
separator, sizeof (separator),
separator, sizeof (separator));
if (err != VAS_EBOOT_ERR_NONE && !error_displayed)
{
error_displayed++;
return err;
}
return VAS_EBOOT_ERR_NONE;
}