diff mbox series

[v3,1/7] tcgbios: Change format of S_CRTM_VERSION string to ucs-2

Message ID 20210709025313.674287-2-stefanb@linux.vnet.ibm.com
State Superseded
Headers show
Series tcgbios: Use the proper hashes for the TPM 2 PCR banks | expand

Commit Message

Stefan Berger July 9, 2021, 2:53 a.m. UTC
From: Stefan Berger <stefanb@linux.ibm.com>

Change the format of the S_CRTM_VERSION string to ucs-2 since this
is what seems to be commonly used by other firmwares following
insight from a TCG work group member.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
 lib/libtpm/tcgbios.c | 45 ++++++++++++++++++++++++++++++++++++--------
 1 file changed, 37 insertions(+), 8 deletions(-)

Comments

Alexey Kardashevskiy July 9, 2021, 5:13 a.m. UTC | #1
On 09/07/2021 12:53, Stefan Berger wrote:
> From: Stefan Berger <stefanb@linux.ibm.com>
> 
> Change the format of the S_CRTM_VERSION string to ucs-2 since this
> is what seems to be commonly used by other firmwares following
> insight from a TCG work group member.
> 
> Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>


How about this 
https://github.com/aik/SLOF-debug/commit/9f6430ca494a9a80469b93b7e34705212e788b61 
?

The whole branch is at https://github.com/aik/SLOF-debug/commits/master


> ---
>   lib/libtpm/tcgbios.c | 45 ++++++++++++++++++++++++++++++++++++--------
>   1 file changed, 37 insertions(+), 8 deletions(-)
> 
> diff --git a/lib/libtpm/tcgbios.c b/lib/libtpm/tcgbios.c
> index d611576..690c4b2 100644
> --- a/lib/libtpm/tcgbios.c
> +++ b/lib/libtpm/tcgbios.c
> @@ -1106,6 +1106,29 @@ uint32_t tpm_measure_gpt(void)
>   					  (const uint8_t *)uefi_gpt_data, sz);
>   }
>   
> +/* convert a normal string of given length into a ucs-2 string */
> +static char *string2ucs2(const char *str, size_t str_length,
> +			 size_t *ucs2_length)
> +{
> +	char *ucs2;
> +	size_t i;
> +
> +	if (str_length > 0) {
> +		*ucs2_length = str_length * 2;
> +		if (str[str_length - 1] != 0)
> +			*ucs2_length += 2;
> +	} else {
> +		*ucs2_length = 2;
> +	}
> +	ucs2 = SLOF_alloc_mem(*ucs2_length);
> +	if (ucs2) {
> +		memset(ucs2, 0, *ucs2_length);
> +		for (i = 0; i < str_length; i++)
> +			ucs2[i * 2] = str[i];
> +	}
> +	return ucs2;
> +}
> +
>   uint32_t tpm_measure_scrtm(void)
>   {
>   	uint32_t rc;
> @@ -1115,19 +1138,25 @@ uint32_t tpm_measure_scrtm(void)
>   	char *slof_text_start = (char *)&_slof_text;
>   	uint32_t slof_text_length = (long)&_slof_text_end - (long)&_slof_text;
>   	const char *scrtm = "S-CRTM Contents";
> +	char *ucs2_version;
> +	size_t ucs2_length;
>   
>   	version_end = strchr(version_start, '\r');
>   	version_length = version_end - version_start;
>   
> -	dprintf("Measure S-CRTM Version: addr = %p, length = %d\n",
> -		version_start, version_length);
> +	ucs2_version = string2ucs2(version_start, version_length, &ucs2_length);
> +	if (ucs2_version) {
> +		dprintf("Measure S-CRTM Version: addr = %p, length = %d\n",
> +			ucs2_version, ucs2_length);
>   
> -	rc = tpm_add_measurement_to_log(0, EV_S_CRTM_VERSION,
> -					version_start, version_length,
> -					(uint8_t *)version_start,
> -					version_length);
> -	if (rc)
> -		return rc;
> +		rc = tpm_add_measurement_to_log(0, EV_S_CRTM_VERSION,
> +						ucs2_version, ucs2_length,
> +						(uint8_t *)ucs2_version,
> +						ucs2_length);
> +		SLOF_free_mem(ucs2_version, ucs2_length);
> +		if (rc)
> +			return rc;
> +	}
>   
>   	dprintf("Measure S-CRTM Content (text): start = %p, length = %d\n",
>   		slof_text_start, slof_text_length);
>
Stefan Berger July 9, 2021, 1:50 p.m. UTC | #2
On 7/9/21 1:13 AM, Alexey Kardashevskiy wrote:
>
>
> On 09/07/2021 12:53, Stefan Berger wrote:
>> From: Stefan Berger <stefanb@linux.ibm.com>
>>
>> Change the format of the S_CRTM_VERSION string to ucs-2 since this
>> is what seems to be commonly used by other firmwares following
>> insight from a TCG work group member.
>>
>> Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
>
>
> How about this 
> https://github.com/aik/SLOF-debug/commit/9f6430ca494a9a80469b93b7e34705212e788b61 
> ?

Yes, this works. I will repost with this patch.
diff mbox series

Patch

diff --git a/lib/libtpm/tcgbios.c b/lib/libtpm/tcgbios.c
index d611576..690c4b2 100644
--- a/lib/libtpm/tcgbios.c
+++ b/lib/libtpm/tcgbios.c
@@ -1106,6 +1106,29 @@  uint32_t tpm_measure_gpt(void)
 					  (const uint8_t *)uefi_gpt_data, sz);
 }
 
+/* convert a normal string of given length into a ucs-2 string */
+static char *string2ucs2(const char *str, size_t str_length,
+			 size_t *ucs2_length)
+{
+	char *ucs2;
+	size_t i;
+
+	if (str_length > 0) {
+		*ucs2_length = str_length * 2;
+		if (str[str_length - 1] != 0)
+			*ucs2_length += 2;
+	} else {
+		*ucs2_length = 2;
+	}
+	ucs2 = SLOF_alloc_mem(*ucs2_length);
+	if (ucs2) {
+		memset(ucs2, 0, *ucs2_length);
+		for (i = 0; i < str_length; i++)
+			ucs2[i * 2] = str[i];
+	}
+	return ucs2;
+}
+
 uint32_t tpm_measure_scrtm(void)
 {
 	uint32_t rc;
@@ -1115,19 +1138,25 @@  uint32_t tpm_measure_scrtm(void)
 	char *slof_text_start = (char *)&_slof_text;
 	uint32_t slof_text_length = (long)&_slof_text_end - (long)&_slof_text;
 	const char *scrtm = "S-CRTM Contents";
+	char *ucs2_version;
+	size_t ucs2_length;
 
 	version_end = strchr(version_start, '\r');
 	version_length = version_end - version_start;
 
-	dprintf("Measure S-CRTM Version: addr = %p, length = %d\n",
-		version_start, version_length);
+	ucs2_version = string2ucs2(version_start, version_length, &ucs2_length);
+	if (ucs2_version) {
+		dprintf("Measure S-CRTM Version: addr = %p, length = %d\n",
+			ucs2_version, ucs2_length);
 
-	rc = tpm_add_measurement_to_log(0, EV_S_CRTM_VERSION,
-					version_start, version_length,
-					(uint8_t *)version_start,
-					version_length);
-	if (rc)
-		return rc;
+		rc = tpm_add_measurement_to_log(0, EV_S_CRTM_VERSION,
+						ucs2_version, ucs2_length,
+						(uint8_t *)ucs2_version,
+						ucs2_length);
+		SLOF_free_mem(ucs2_version, ucs2_length);
+		if (rc)
+			return rc;
+	}
 
 	dprintf("Measure S-CRTM Content (text): start = %p, length = %d\n",
 		slof_text_start, slof_text_length);