summaryrefslogtreecommitdiff
path: root/test/nvmem_tpm2_mock.c
blob: 00c87df3b9424ea25253146750aaaede3986eda1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* Copyright 2019 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
/* Stuff from tpm2 directory. */

#include "nvmem_test.h"

#include "console.h"
#include "nvmem.h"
#include "util.h"

#define NVMEM_CR50_SIZE 272

uint32_t s_evictNvStart;
uint32_t s_evictNvEnd;

/* Calculate size of TPM NVMEM. */
#define MOCK_NV_MEMORY_SIZE                                                    \
	(NVMEM_PARTITION_SIZE - sizeof(struct nvmem_tag) - NVMEM_CR50_SIZE)

uint32_t nvmem_user_sizes[NVMEM_NUM_USERS] = {MOCK_NV_MEMORY_SIZE,
					      NVMEM_CR50_SIZE};

/*
 * Sizes of the reserved objects stored in the TPM NVMEM. Note that the second
 * last object is in fact a variable size field starting with 4 bytes of size
 * and then up to 512 bytes of actual index data. The array below assumes that
 * the full 512 bytes of the index space are used.
 */
const uint16_t res_sizes[] = {4,  2,  2,  2,  66,   66,	 66,  66, 66,  66,
			      34, 34, 34, 66, 66,   66,	 8,   4,  134, 28,
			      3,  4,  4,  4,  4,    4,	 2,   15, 2,   8,
			      4,  4,  4,  96, 2844, 424, 516, 8};

static uint16_t res_addrs[ARRAY_SIZE(res_sizes)];

UINT32 NvEarlyStageFindHandle(TPM_HANDLE handle)
{
	size_t i;

	res_addrs[0] = 0;

	for (i = 1; i < ARRAY_SIZE(res_addrs); i++)
		res_addrs[i] = res_addrs[i - 1] + res_sizes[i - 1];

	s_evictNvStart = res_addrs[i - 1] + res_sizes[i - 1];

	s_evictNvEnd = MOCK_NV_MEMORY_SIZE;
	return 0;
}

void NvGetReserved(UINT32 index, NV_RESERVED_ITEM *ri)
{
	uint32_t index_size;

	if (index >= ARRAY_SIZE(res_sizes)) {
		ri->size = 0;
		return;
	}

	ri->offset = res_addrs[index];
	if (index != NV_RAM_INDEX_SPACE) {
		ri->size = res_sizes[index];
		return;
	}

	memcpy(&index_size, nvmem_cache_base(NVMEM_TPM) + ri->offset,
	       sizeof(index_size));

	if (index_size == ~0)
		/* Must be starting with empty flash memeory. */
		index_size = 0;

	ri->size = index_size + sizeof(index_size);
}

UINT16 UINT16_Marshal(UINT16 *source, BYTE **buffer, INT32 *size)
{
	uint16_t value;

	if (!size || (*size < sizeof(value)))
		return 0;

	value = htobe16(*source);

	memcpy(*buffer, &value, sizeof(value));
	*buffer += sizeof(value);
	*size -= sizeof(value);

	return sizeof(value);
}

UINT16 UINT32_Marshal(UINT32 *source, BYTE **buffer, INT32 *size)
{
	uint32_t value;

	if (!size || (*size < sizeof(value)))
		return 0;

	value = htobe32(*source);

	memcpy(*buffer, &value, sizeof(value));
	*buffer += sizeof(value);
	*size -= sizeof(value);

	return sizeof(value);
}

UINT16 UINT64_Marshal(UINT64 *source, BYTE **buffer, INT32 *size)
{
	uint64_t value;

	if (!size || (*size < sizeof(value)))
		return 0;

	value = htobe64(*source);

	memcpy(*buffer, &value, sizeof(value));
	*buffer += sizeof(value);
	*size -= sizeof(value);

	return sizeof(value);
}

UINT16 TPM2B_DIGEST_Marshal(TPM2B_DIGEST *source, BYTE **buffer, INT32 *size)
{
	UINT16 total_size;
	INT32 i;
	uint8_t *p;

	total_size = UINT16_Marshal(&source->t.size, buffer, size);
	p = *buffer;

	for (i = 0; (i < source->t.size) && *size; ++i) {
		*p++ = source->t.buffer[i];
		*size -= 1;
	}

	total_size += i;
	*buffer = p;

	return total_size;
}

uint16_t TPM2B_AUTH_Marshal(TPM2B_AUTH *source, BYTE **buffer, INT32 *size)
{
	return TPM2B_DIGEST_Marshal(source, buffer, size);
}

uint16_t TPM2B_NONCE_Marshal(TPM2B_AUTH *source, BYTE **buffer, INT32 *size)
{
	return TPM2B_DIGEST_Marshal(source, buffer, size);
}

TPM_RC UINT16_Unmarshal(UINT16 *target, BYTE **buffer, INT32 *size)
{
	uint16_t value;

	if (!size || *size < sizeof(value))
		return TPM_RC_INSUFFICIENT;

	memcpy(&value, *buffer, sizeof(value));
	*target = be16toh(value);

	*buffer += sizeof(value);
	*size -= sizeof(value);

	return TPM_RC_SUCCESS;
}

TPM_RC UINT32_Unmarshal(UINT32 *target, BYTE **buffer, INT32 *size)
{
	uint32_t value;

	if (!size || *size < sizeof(value))
		return TPM_RC_INSUFFICIENT;

	memcpy(&value, *buffer, sizeof(value));
	*target = be32toh(value);

	*buffer += sizeof(value);
	*size -= sizeof(value);

	return TPM_RC_SUCCESS;
}

TPM_RC UINT64_Unmarshal(UINT64 *target, BYTE **buffer, INT32 *size)
{
	uint64_t value;

	if (!size || *size < sizeof(value))
		return TPM_RC_INSUFFICIENT;

	memcpy(&value, *buffer, sizeof(value));
	*target = be64toh(value);

	*buffer += sizeof(value);
	*size -= sizeof(value);

	return TPM_RC_SUCCESS;
}

TPM_RC TPM2B_DIGEST_Unmarshal(TPM2B_DIGEST *target, BYTE **buffer, INT32 *size)
{
	TPM_RC result;
	INT32 i;
	uint8_t *p;

	result = UINT16_Unmarshal(&target->t.size, buffer, size);

	if (result != TPM_RC_SUCCESS)
		return result;

	if (target->t.size == 0)
		return TPM_RC_SUCCESS;

	if ((target->t.size > sizeof(TPMU_HA)) || (target->t.size > *size))
		return TPM_RC_SIZE;

	p = *buffer;
	for (i = 0; i < target->t.size; ++i)
		target->t.buffer[i] = *p++;

	*buffer = p;
	*size -= i;

	return TPM_RC_SUCCESS;
}

TPM_RC TPM2B_AUTH_Unmarshal(TPM2B_AUTH *target, BYTE **buffer, INT32 *size)
{
	return TPM2B_DIGEST_Unmarshal(target, buffer, size);
}

TPM_RC TPM2B_NONCE_Unmarshal(TPM2B_AUTH *target, BYTE **buffer, INT32 *size)
{
	return TPM2B_DIGEST_Unmarshal(target, buffer, size);
}

#define ITER_INIT (~0)

static void *get_cache_addr(size_t offset)
{
	return (void *)(((uintptr_t)nvmem_cache_base(NVMEM_TPM)) + offset);
}

static void read_from_cache(size_t offset, size_t size, void *dest)
{
	nvmem_read(offset, size, dest, NVMEM_TPM);
}

static void write_to_cache(size_t offset, size_t size, void *src)
{
	nvmem_write(offset, size, src, NVMEM_TPM);
}

/* Copies of the appropriate functions from NV.c in TPM2 library. */
static uint32_t nv_next(uint32_t *iter)
{
	uint32_t currentIter;

	if (*iter == ITER_INIT)
		*iter = s_evictNvStart;

	if ((*iter + sizeof(uint32_t) > s_evictNvEnd) || !*iter)
		return 0;

	currentIter = *iter;
	read_from_cache(*iter, sizeof(uint32_t), iter);
	if (!*iter || (*iter == ITER_INIT))
		return 0;

	return currentIter + sizeof(uint32_t);
}

static uint32_t nv_get_end(void)
{
	uint32_t iter = ITER_INIT;
	uint32_t endAddr = s_evictNvStart;
	uint32_t currentAddr;

	while ((currentAddr = nv_next(&iter)) != 0)
		endAddr = currentAddr;

	if (endAddr != s_evictNvStart) {
		/* Read offset. */
		endAddr -= sizeof(uint32_t);
		read_from_cache(endAddr, sizeof(uint32_t), &endAddr);
	}
	return endAddr;
}

size_t add_evictable_obj(void *obj, size_t obj_size)
{
	uint32_t end_addr;
	uint32_t next_addr;
	uint32_t list_end = 0;

	end_addr = nv_get_end();

	next_addr = end_addr + sizeof(uint32_t) + obj_size;

	if (next_addr >= s_evictNvEnd) {
		ccprintf("%s: could not fit %zd bytes!\n", __func__, obj_size);
		return 0;
	}

	/* Write next pointer */
	write_to_cache(end_addr, sizeof(uint32_t), &next_addr);
	/* Write entity data. */
	write_to_cache(end_addr + sizeof(uint32_t), obj_size, obj);

	/* Write the end of list if it fits. */
	if (next_addr + sizeof(uint32_t) <= s_evictNvEnd)
		write_to_cache(next_addr, sizeof(list_end), &list_end);

	return obj_size;
}

/*
 * It is the responsibility of the caller to pass the proper address of an
 * object in the cache.
 */
void drop_evictable_obj(void *obj)
{
	uint32_t next_addr;
	uint32_t list_end = 0;
	uint32_t obj_addr;

	obj_addr = (uintptr_t)obj - (uintptr_t)nvmem_cache_base(NVMEM_TPM);
	read_from_cache(obj_addr - sizeof(next_addr), sizeof(next_addr),
			&next_addr);
	ccprintf("%s:%d dropping obj at cache addr %x, offset %x, addr %pP "
		 "next addr %x aka %x (off s_evictNvStart)\n",
		 __func__, __LINE__, obj_addr - s_evictNvStart, obj_addr, obj,
		 next_addr, next_addr - s_evictNvStart);

	/*
	 * Now, to make it easier to add objects behind the current one, let's
	 * pretend there is no more objects.
	 */
	write_to_cache(obj_addr - sizeof(next_addr), sizeof(list_end),
		       &list_end);

	if (!next_addr || (next_addr == s_evictNvEnd))
		return;

	/*
	 * Iterate over objects starting with next_addr, copying them into
	 * obj_addr.
	 */
	obj_addr = next_addr;
	while (1) {
		uint32_t next_next_addr;
		uint32_t next_obj_size;

		read_from_cache(next_addr, sizeof(next_next_addr),
				&next_next_addr);

		if (!next_next_addr || (next_next_addr == s_evictNvEnd))
			return;

		next_obj_size = next_next_addr - obj_addr - sizeof(uint32_t);
		add_evictable_obj(
			(void *)((uintptr_t)nvmem_cache_base(NVMEM_TPM) +
				 next_addr + sizeof(uint32_t)),
			next_obj_size);
		next_addr = next_next_addr;
		obj_addr += next_obj_size + sizeof(next_obj_size);
	}
}

void *evictable_offs_to_addr(uint16_t offset)
{
	return (void *)((uintptr_t)get_cache_addr(s_evictNvStart) + offset);
}