Skip to content

Commit 17d89b2

Browse files
James Bottomleyjarkkojs
James Bottomley
authored andcommitted
tpm: Move buffer handling from static inlines to real functions
separate out the tpm_buf_... handling functions from static inlines in tpm.h and move them to their own tpm-buf.c file. This is a precursor to adding new functions for other TPM type handling because the amount of code will grow from the current 70 lines in tpm.h to about 200 lines when the additions are done. 200 lines of inline functions is a bit too much to keep in a header file. Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com> Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com> Reviewed-by: Stefan Berger <stefanb@linux.ibm.com> Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> Tested-by: Jarkko Sakkinen <jarkko@kernel.org> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
1 parent 4f0feb5 commit 17d89b2

File tree

3 files changed

+97
-71
lines changed

3 files changed

+97
-71
lines changed

drivers/char/tpm/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ tpm-y += tpm-sysfs.o
1515
tpm-y += eventlog/common.o
1616
tpm-y += eventlog/tpm1.o
1717
tpm-y += eventlog/tpm2.o
18+
tpm-y += tpm-buf.o
1819

1920
tpm-$(CONFIG_ACPI) += tpm_ppi.o eventlog/acpi.o
2021
tpm-$(CONFIG_EFI) += eventlog/efi.o

drivers/char/tpm/tpm-buf.c

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Handling of TPM command and other buffers.
4+
*/
5+
6+
#include <linux/module.h>
7+
#include <linux/tpm.h>
8+
9+
int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
10+
{
11+
buf->data = (u8 *)__get_free_page(GFP_KERNEL);
12+
if (!buf->data)
13+
return -ENOMEM;
14+
15+
buf->flags = 0;
16+
tpm_buf_reset(buf, tag, ordinal);
17+
return 0;
18+
}
19+
EXPORT_SYMBOL_GPL(tpm_buf_init);
20+
21+
void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
22+
{
23+
struct tpm_header *head = (struct tpm_header *)buf->data;
24+
25+
head->tag = cpu_to_be16(tag);
26+
head->length = cpu_to_be32(sizeof(*head));
27+
head->ordinal = cpu_to_be32(ordinal);
28+
}
29+
EXPORT_SYMBOL_GPL(tpm_buf_reset);
30+
31+
void tpm_buf_destroy(struct tpm_buf *buf)
32+
{
33+
free_page((unsigned long)buf->data);
34+
}
35+
EXPORT_SYMBOL_GPL(tpm_buf_destroy);
36+
37+
u32 tpm_buf_length(struct tpm_buf *buf)
38+
{
39+
struct tpm_header *head = (struct tpm_header *)buf->data;
40+
41+
return be32_to_cpu(head->length);
42+
}
43+
EXPORT_SYMBOL_GPL(tpm_buf_length);
44+
45+
void tpm_buf_append(struct tpm_buf *buf,
46+
const unsigned char *new_data,
47+
unsigned int new_len)
48+
{
49+
struct tpm_header *head = (struct tpm_header *)buf->data;
50+
u32 len = tpm_buf_length(buf);
51+
52+
/* Return silently if overflow has already happened. */
53+
if (buf->flags & TPM_BUF_OVERFLOW)
54+
return;
55+
56+
if ((len + new_len) > PAGE_SIZE) {
57+
WARN(1, "tpm_buf: overflow\n");
58+
buf->flags |= TPM_BUF_OVERFLOW;
59+
return;
60+
}
61+
62+
memcpy(&buf->data[len], new_data, new_len);
63+
head->length = cpu_to_be32(len + new_len);
64+
}
65+
EXPORT_SYMBOL_GPL(tpm_buf_append);
66+
67+
void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value)
68+
{
69+
tpm_buf_append(buf, &value, 1);
70+
}
71+
EXPORT_SYMBOL_GPL(tpm_buf_append_u8);
72+
73+
void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value)
74+
{
75+
__be16 value2 = cpu_to_be16(value);
76+
77+
tpm_buf_append(buf, (u8 *)&value2, 2);
78+
}
79+
EXPORT_SYMBOL_GPL(tpm_buf_append_u16);
80+
81+
void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
82+
{
83+
__be32 value2 = cpu_to_be32(value);
84+
85+
tpm_buf_append(buf, (u8 *)&value2, 4);
86+
}
87+
EXPORT_SYMBOL_GPL(tpm_buf_append_u32);

include/linux/tpm.h

+9-71
Original file line numberDiff line numberDiff line change
@@ -326,77 +326,15 @@ struct tpm2_hash {
326326
unsigned int tpm_id;
327327
};
328328

329-
static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
330-
{
331-
struct tpm_header *head = (struct tpm_header *)buf->data;
332-
333-
head->tag = cpu_to_be16(tag);
334-
head->length = cpu_to_be32(sizeof(*head));
335-
head->ordinal = cpu_to_be32(ordinal);
336-
}
337-
338-
static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
339-
{
340-
buf->data = (u8 *)__get_free_page(GFP_KERNEL);
341-
if (!buf->data)
342-
return -ENOMEM;
343-
344-
buf->flags = 0;
345-
tpm_buf_reset(buf, tag, ordinal);
346-
return 0;
347-
}
348-
349-
static inline void tpm_buf_destroy(struct tpm_buf *buf)
350-
{
351-
free_page((unsigned long)buf->data);
352-
}
353-
354-
static inline u32 tpm_buf_length(struct tpm_buf *buf)
355-
{
356-
struct tpm_header *head = (struct tpm_header *)buf->data;
357-
358-
return be32_to_cpu(head->length);
359-
}
360-
361-
static inline void tpm_buf_append(struct tpm_buf *buf,
362-
const unsigned char *new_data,
363-
unsigned int new_len)
364-
{
365-
struct tpm_header *head = (struct tpm_header *)buf->data;
366-
u32 len = tpm_buf_length(buf);
367-
368-
/* Return silently if overflow has already happened. */
369-
if (buf->flags & TPM_BUF_OVERFLOW)
370-
return;
371-
372-
if ((len + new_len) > PAGE_SIZE) {
373-
WARN(1, "tpm_buf: overflow\n");
374-
buf->flags |= TPM_BUF_OVERFLOW;
375-
return;
376-
}
377-
378-
memcpy(&buf->data[len], new_data, new_len);
379-
head->length = cpu_to_be32(len + new_len);
380-
}
381-
382-
static inline void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value)
383-
{
384-
tpm_buf_append(buf, &value, 1);
385-
}
386-
387-
static inline void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value)
388-
{
389-
__be16 value2 = cpu_to_be16(value);
390-
391-
tpm_buf_append(buf, (u8 *) &value2, 2);
392-
}
393-
394-
static inline void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
395-
{
396-
__be32 value2 = cpu_to_be32(value);
397-
398-
tpm_buf_append(buf, (u8 *) &value2, 4);
399-
}
329+
int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal);
330+
void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal);
331+
void tpm_buf_destroy(struct tpm_buf *buf);
332+
u32 tpm_buf_length(struct tpm_buf *buf);
333+
void tpm_buf_append(struct tpm_buf *buf, const unsigned char *new_data,
334+
unsigned int new_len);
335+
void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value);
336+
void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value);
337+
void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value);
400338

401339
/*
402340
* Check if TPM device is in the firmware upgrade mode.

0 commit comments

Comments
 (0)