summaryrefslogtreecommitdiff
path: root/src/tcs/tcs_req_mgr.c
blob: 701cedb6d0dc1763be923fec704f74094d82574e (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

/*
 * Licensed Materials - Property of IBM
 *
 * trousers - An open source TCG Software Stack
 *
 * (C) Copyright International Business Machines Corp. 2004-2006
 *
 */


#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include "trousers/tss.h"
#include "tcs_tsp.h"
#include "tcs_utils.h"
#include "tddl.h"
#include "req_mgr.h"
#include "tcslog.h"

static struct tpm_req_mgr *trm;

#ifdef TSS_DEBUG
#define TSS_TPM_DEBUG
#endif

TSS_RESULT
req_mgr_submit_req(BYTE *blob)
{
	TSS_RESULT result;
	BYTE loc_buf[TSS_TPM_TXBLOB_SIZE];
	UINT32 size = TSS_TPM_TXBLOB_SIZE;
	UINT32 retry = TSS_REQ_MGR_MAX_RETRIES;

	MUTEX_LOCK(trm->queue_lock);

#ifdef TSS_TPM_DEBUG
	LogBlobData("To TPM:", Decode_UINT32(&blob[2]), blob);
#endif

	do {
		result = Tddli_TransmitData(blob, Decode_UINT32(&blob[2]), loc_buf, &size);
	} while (!result && (Decode_UINT32(&loc_buf[6]) == TCPA_E_RETRY) && --retry);

	if (!result)
		memcpy(blob, loc_buf, Decode_UINT32(&loc_buf[2]));

#ifdef TSS_TPM_DEBUG
	LogBlobData("From TPM:", size, loc_buf);
#endif

	MUTEX_UNLOCK(trm->queue_lock);

	return result;
}

TSS_RESULT
req_mgr_init()
{
	if ((trm = calloc(1, sizeof(struct tpm_req_mgr))) == NULL) {
		LogError("malloc of %zd bytes failed.", sizeof(struct tpm_req_mgr));
		return TSS_E_OUTOFMEMORY;
	}

	MUTEX_INIT(trm->queue_lock);

	return Tddli_Open();
}

TSS_RESULT
req_mgr_final()
{
	free(trm);

	return Tddli_Close();
}