Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / char / tpm / tpm-buf.c
blobe49a19fea3bdf645758891c7cef0548962e035e6
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Handling of TPM command and other buffers.
4 */
6 #include <linux/tpm_command.h>
7 #include <linux/module.h>
8 #include <linux/tpm.h>
10 /**
11 * tpm_buf_init() - Allocate and initialize a TPM command
12 * @buf: A &tpm_buf
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);
21 if (!buf->data)
22 return -ENOMEM;
24 tpm_buf_reset(buf, tag, ordinal);
25 return 0;
27 EXPORT_SYMBOL_GPL(tpm_buf_init);
29 /**
30 * tpm_buf_reset() - Initialize a TPM command
31 * @buf: A &tpm_buf
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);
42 buf->flags = 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);
47 buf->handles = 0;
49 EXPORT_SYMBOL_GPL(tpm_buf_reset);
51 /**
52 * tpm_buf_init_sized() - Allocate and initialize a sized (TPM2B) buffer
53 * @buf: A @tpm_buf
55 * Return: 0 or -ENOMEM
57 int tpm_buf_init_sized(struct tpm_buf *buf)
59 buf->data = (u8 *)__get_free_page(GFP_KERNEL);
60 if (!buf->data)
61 return -ENOMEM;
63 tpm_buf_reset_sized(buf);
64 return 0;
66 EXPORT_SYMBOL_GPL(tpm_buf_init_sized);
68 /**
69 * tpm_buf_reset_sized() - Initialize a sized buffer
70 * @buf: A &tpm_buf
72 void tpm_buf_reset_sized(struct tpm_buf *buf)
74 buf->flags = TPM_BUF_TPM2B;
75 buf->length = 2;
76 buf->data[0] = 0;
77 buf->data[1] = 0;
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);
87 /**
88 * tpm_buf_length() - Return the number of bytes consumed by the data
89 * @buf: A &tpm_buf
91 * Return: The number of bytes consumed by the buffer
93 u32 tpm_buf_length(struct tpm_buf *buf)
95 return buf->length;
97 EXPORT_SYMBOL_GPL(tpm_buf_length);
99 /**
100 * tpm_buf_append() - Append data to an initialized buffer
101 * @buf: A &tpm_buf
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)
109 return;
111 if ((buf->length + new_length) > PAGE_SIZE) {
112 WARN(1, "tpm_buf: write overflow\n");
113 buf->flags |= TPM_BUF_OVERFLOW;
114 return;
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);
122 else
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");
162 return;
165 tpm_buf_append_u32(buf, handle);
166 buf->handles++;
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)
178 off_t next_offset;
180 /* Return silently if overflow has already happened. */
181 if (buf->flags & TPM_BUF_BOUNDARY_ERROR)
182 return;
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;
188 return;
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)
204 u8 value;
206 tpm_buf_read(buf, offset, sizeof(value), &value);
208 return 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)
221 u16 value;
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)
238 u32 value;
240 tpm_buf_read(buf, offset, sizeof(value), &value);
242 return be32_to_cpu(value);
244 EXPORT_SYMBOL_GPL(tpm_buf_read_u32);