1 // SPDX-License-Identifier: GPL-2.0
3 * Handling of TPM command and other buffers.
6 #include <linux/tpm_command.h>
7 #include <linux/module.h>
11 * tpm_buf_init() - Allocate and initialize a TPM command
13 * @tag: TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS
14 * @ordinal: A command ordinal
16 * Return: 0 or -ENOMEM
18 int tpm_buf_init(struct tpm_buf
*buf
, u16 tag
, u32 ordinal
)
20 buf
->data
= (u8
*)__get_free_page(GFP_KERNEL
);
24 tpm_buf_reset(buf
, tag
, ordinal
);
27 EXPORT_SYMBOL_GPL(tpm_buf_init
);
30 * tpm_buf_reset() - Initialize a TPM command
32 * @tag: TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS
33 * @ordinal: A command ordinal
35 void tpm_buf_reset(struct tpm_buf
*buf
, u16 tag
, u32 ordinal
)
37 struct tpm_header
*head
= (struct tpm_header
*)buf
->data
;
39 WARN_ON(tag
!= TPM_TAG_RQU_COMMAND
&& tag
!= TPM2_ST_NO_SESSIONS
&&
40 tag
!= TPM2_ST_SESSIONS
&& tag
!= 0);
43 buf
->length
= sizeof(*head
);
44 head
->tag
= cpu_to_be16(tag
);
45 head
->length
= cpu_to_be32(sizeof(*head
));
46 head
->ordinal
= cpu_to_be32(ordinal
);
49 EXPORT_SYMBOL_GPL(tpm_buf_reset
);
52 * tpm_buf_init_sized() - Allocate and initialize a sized (TPM2B) buffer
55 * Return: 0 or -ENOMEM
57 int tpm_buf_init_sized(struct tpm_buf
*buf
)
59 buf
->data
= (u8
*)__get_free_page(GFP_KERNEL
);
63 tpm_buf_reset_sized(buf
);
66 EXPORT_SYMBOL_GPL(tpm_buf_init_sized
);
69 * tpm_buf_reset_sized() - Initialize a sized buffer
72 void tpm_buf_reset_sized(struct tpm_buf
*buf
)
74 buf
->flags
= TPM_BUF_TPM2B
;
79 EXPORT_SYMBOL_GPL(tpm_buf_reset_sized
);
81 void tpm_buf_destroy(struct tpm_buf
*buf
)
83 free_page((unsigned long)buf
->data
);
85 EXPORT_SYMBOL_GPL(tpm_buf_destroy
);
88 * tpm_buf_length() - Return the number of bytes consumed by the data
91 * Return: The number of bytes consumed by the buffer
93 u32
tpm_buf_length(struct tpm_buf
*buf
)
97 EXPORT_SYMBOL_GPL(tpm_buf_length
);
100 * tpm_buf_append() - Append data to an initialized buffer
102 * @new_data: A data blob
103 * @new_length: Size of the appended data
105 void tpm_buf_append(struct tpm_buf
*buf
, const u8
*new_data
, u16 new_length
)
107 /* Return silently if overflow has already happened. */
108 if (buf
->flags
& TPM_BUF_OVERFLOW
)
111 if ((buf
->length
+ new_length
) > PAGE_SIZE
) {
112 WARN(1, "tpm_buf: write overflow\n");
113 buf
->flags
|= TPM_BUF_OVERFLOW
;
117 memcpy(&buf
->data
[buf
->length
], new_data
, new_length
);
118 buf
->length
+= new_length
;
120 if (buf
->flags
& TPM_BUF_TPM2B
)
121 ((__be16
*)buf
->data
)[0] = cpu_to_be16(buf
->length
- 2);
123 ((struct tpm_header
*)buf
->data
)->length
= cpu_to_be32(buf
->length
);
125 EXPORT_SYMBOL_GPL(tpm_buf_append
);
127 void tpm_buf_append_u8(struct tpm_buf
*buf
, const u8 value
)
129 tpm_buf_append(buf
, &value
, 1);
131 EXPORT_SYMBOL_GPL(tpm_buf_append_u8
);
133 void tpm_buf_append_u16(struct tpm_buf
*buf
, const u16 value
)
135 __be16 value2
= cpu_to_be16(value
);
137 tpm_buf_append(buf
, (u8
*)&value2
, 2);
139 EXPORT_SYMBOL_GPL(tpm_buf_append_u16
);
141 void tpm_buf_append_u32(struct tpm_buf
*buf
, const u32 value
)
143 __be32 value2
= cpu_to_be32(value
);
145 tpm_buf_append(buf
, (u8
*)&value2
, 4);
147 EXPORT_SYMBOL_GPL(tpm_buf_append_u32
);
150 * tpm_buf_append_handle() - Add a handle
151 * @chip: &tpm_chip instance
152 * @buf: &tpm_buf instance
153 * @handle: a TPM object handle
155 * Add a handle to the buffer, and increase the count tracking the number of
156 * handles in the command buffer. Works only for command buffers.
158 void tpm_buf_append_handle(struct tpm_chip
*chip
, struct tpm_buf
*buf
, u32 handle
)
160 if (buf
->flags
& TPM_BUF_TPM2B
) {
161 dev_err(&chip
->dev
, "Invalid buffer type (TPM2B)\n");
165 tpm_buf_append_u32(buf
, handle
);
170 * tpm_buf_read() - Read from a TPM buffer
171 * @buf: &tpm_buf instance
172 * @offset: offset within the buffer
173 * @count: the number of bytes to read
174 * @output: the output buffer
176 static void tpm_buf_read(struct tpm_buf
*buf
, off_t
*offset
, size_t count
, void *output
)
180 /* Return silently if overflow has already happened. */
181 if (buf
->flags
& TPM_BUF_BOUNDARY_ERROR
)
184 next_offset
= *offset
+ count
;
185 if (next_offset
> buf
->length
) {
186 WARN(1, "tpm_buf: read out of boundary\n");
187 buf
->flags
|= TPM_BUF_BOUNDARY_ERROR
;
191 memcpy(output
, &buf
->data
[*offset
], count
);
192 *offset
= next_offset
;
196 * tpm_buf_read_u8() - Read 8-bit word from a TPM buffer
197 * @buf: &tpm_buf instance
198 * @offset: offset within the buffer
200 * Return: next 8-bit word
202 u8
tpm_buf_read_u8(struct tpm_buf
*buf
, off_t
*offset
)
206 tpm_buf_read(buf
, offset
, sizeof(value
), &value
);
210 EXPORT_SYMBOL_GPL(tpm_buf_read_u8
);
213 * tpm_buf_read_u16() - Read 16-bit word from a TPM buffer
214 * @buf: &tpm_buf instance
215 * @offset: offset within the buffer
217 * Return: next 16-bit word
219 u16
tpm_buf_read_u16(struct tpm_buf
*buf
, off_t
*offset
)
223 tpm_buf_read(buf
, offset
, sizeof(value
), &value
);
225 return be16_to_cpu(value
);
227 EXPORT_SYMBOL_GPL(tpm_buf_read_u16
);
230 * tpm_buf_read_u32() - Read 32-bit word from a TPM buffer
231 * @buf: &tpm_buf instance
232 * @offset: offset within the buffer
234 * Return: next 32-bit word
236 u32
tpm_buf_read_u32(struct tpm_buf
*buf
, off_t
*offset
)
240 tpm_buf_read(buf
, offset
, sizeof(value
), &value
);
242 return be32_to_cpu(value
);
244 EXPORT_SYMBOL_GPL(tpm_buf_read_u32
);