blob: 7270db6df43081dbab2d3757697a37b1d98649b3
1 | /* |
2 | * Copyright (C) 2015 The Android Open Source Project |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | #define LOG_TAG "AmlKeymaster" |
18 | |
19 | #include <errno.h> |
20 | #include <stdlib.h> |
21 | #include <string.h> |
22 | #include <unistd.h> |
23 | |
24 | #include <log/log.h> |
25 | |
26 | #include "keymaster_ipc.h" |
27 | #include "aml_keymaster_ipc.h" |
28 | |
29 | TEEC_Result aml_keymaster_connect(TEEC_Context *c, TEEC_Session *s) { |
30 | TEEC_Result result = TEEC_SUCCESS; |
31 | TEEC_UUID svc_id = TA_KEYMASTER_UUID; |
32 | TEEC_Operation operation; |
33 | uint32_t err_origin; |
34 | struct timespec time; |
35 | uint64_t millis = 0; |
36 | |
37 | memset(&operation, 0, sizeof(operation)); |
38 | |
39 | /* Initialize Context */ |
40 | result = TEEC_InitializeContext(NULL, c); |
41 | |
42 | if (result != TEEC_SUCCESS) { |
43 | ALOGD("TEEC_InitializeContext failed with error = %x\n", result); |
44 | return result; |
45 | } |
46 | /* Open Session */ |
47 | result = TEEC_OpenSession(c, s, &svc_id, |
48 | TEEC_LOGIN_PUBLIC, |
49 | NULL, NULL, |
50 | &err_origin); |
51 | |
52 | if (result != TEEC_SUCCESS) { |
53 | ALOGD("TEEC_Opensession failed with code 0x%x origin 0x%x",result, err_origin); |
54 | TEEC_FinalizeContext(c); |
55 | return result; |
56 | } |
57 | |
58 | int res = clock_gettime(CLOCK_BOOTTIME, &time); |
59 | if (res < 0) |
60 | millis = 0; |
61 | else |
62 | millis = (time.tv_sec * 1000) + (time.tv_nsec / 1000 / 1000); |
63 | |
64 | /* Init TA */ |
65 | operation.paramTypes = TEEC_PARAM_TYPES( |
66 | TEEC_VALUE_INPUT, TEEC_NONE, |
67 | TEEC_NONE, TEEC_NONE); |
68 | |
69 | operation.params[0].value.a = (millis >> 32); |
70 | operation.params[0].value.b = (millis & 0xffffffff); |
71 | |
72 | result = TEEC_InvokeCommand(s, |
73 | KM_TA_INIT, |
74 | &operation, |
75 | NULL); |
76 | |
77 | ALOGE("create id: %d, ctx: %p, ctx: %p\n", s->session_id, s->ctx, c); |
78 | return result; |
79 | } |
80 | |
81 | TEEC_Result aml_keymaster_call(TEEC_Session *s, uint32_t cmd, void* in, uint32_t in_size, uint8_t* out, |
82 | uint32_t* out_size) { |
83 | TEEC_Result res = TEEC_SUCCESS; |
84 | TEEC_Operation op; |
85 | uint32_t ret_orig; |
86 | |
87 | memset(&op, 0, sizeof(op)); |
88 | |
89 | op.params[0].tmpref.buffer = in; |
90 | op.params[0].tmpref.size = in_size; |
91 | op.params[1].tmpref.buffer = out; |
92 | op.params[1].tmpref.size = *out_size; |
93 | op.paramTypes = TEEC_PARAM_TYPES( |
94 | TEEC_MEMREF_TEMP_INPUT, |
95 | TEEC_MEMREF_TEMP_OUTPUT, |
96 | TEEC_VALUE_OUTPUT, |
97 | TEEC_NONE); |
98 | |
99 | ALOGE("id: %d, ctx: %p, cmd: %d\n", s->session_id, s->ctx, cmd); |
100 | res = TEEC_InvokeCommand(s, cmd, &op, &ret_orig); |
101 | if (res != TEEC_SUCCESS) { |
102 | ALOGE("Invoke cmd: %u failed with res(%x), ret_orig(%x), return(%d)\n", |
103 | cmd, res, ret_orig, op.params[2].value.a); |
104 | } else { |
105 | *out_size = op.params[2].value.b; |
106 | } |
107 | |
108 | return res; |
109 | } |
110 | |
111 | TEEC_Result aml_keymaster_disconnect(TEEC_Context *c, TEEC_Session *s) { |
112 | TEEC_Operation operation; |
113 | TEEC_Result result = TEEC_SUCCESS; |
114 | |
115 | operation.paramTypes = TEEC_PARAM_TYPES( |
116 | TEEC_NONE, TEEC_NONE, |
117 | TEEC_NONE, TEEC_NONE); |
118 | |
119 | result = TEEC_InvokeCommand(s, |
120 | KM_TA_TERM, |
121 | &operation, |
122 | NULL); |
123 | |
124 | TEEC_CloseSession(s); |
125 | TEEC_FinalizeContext(c); |
126 | |
127 | return result; |
128 | } |
129 |