xref: /aosp_15_r20/system/core/trusty/gatekeeper/trusty_gatekeeper_ipc.c (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
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 "TrustyGateKeeper"
18 
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 
25 #include <log/log.h>
26 #include <trusty/tipc.h>
27 
28 #include "trusty_gatekeeper_ipc.h"
29 #include "gatekeeper_ipc.h"
30 
31 static const char* trusty_device_name = "/dev/trusty-ipc-dev0";
32 static int handle_ = 0;
33 
trusty_gatekeeper_set_dev_name(const char * device_name)34 void trusty_gatekeeper_set_dev_name(const char* device_name) {
35     trusty_device_name = device_name;
36 }
37 
trusty_gatekeeper_connect()38 int trusty_gatekeeper_connect() {
39     int rc = tipc_connect(trusty_device_name, GATEKEEPER_PORT);
40     if (rc < 0) {
41         return rc;
42     }
43 
44     handle_ = rc;
45     return 0;
46 }
47 
trusty_gatekeeper_call(uint32_t cmd,void * in,uint32_t in_size,uint8_t * out,uint32_t * out_size)48 int trusty_gatekeeper_call(uint32_t cmd, void *in, uint32_t in_size, uint8_t *out,
49                            uint32_t *out_size) {
50     if (handle_ == 0) {
51         ALOGE("not connected\n");
52         return -EINVAL;
53     }
54 
55     size_t msg_size = in_size + sizeof(struct gatekeeper_message);
56     struct gatekeeper_message *msg = malloc(msg_size);
57     msg->cmd = cmd;
58     memcpy(msg->payload, in, in_size);
59 
60     ssize_t rc = write(handle_, msg, msg_size);
61     free(msg);
62 
63     if (rc < 0) {
64         ALOGE("failed to send cmd (%d) to %s: %s\n", cmd,
65                 GATEKEEPER_PORT, strerror(errno));
66         return -errno;
67     }
68 
69     rc = read(handle_, out, *out_size);
70     if (rc < 0) {
71         ALOGE("failed to retrieve response for cmd (%d) to %s: %s\n",
72                 cmd, GATEKEEPER_PORT, strerror(errno));
73         return -errno;
74     }
75 
76     if ((size_t) rc < sizeof(struct gatekeeper_message)) {
77         ALOGE("invalid response size (%d)\n", (int) rc);
78         return -EINVAL;
79     }
80 
81     msg = (struct gatekeeper_message *) out;
82 
83     if ((cmd | GK_RESP_BIT) != msg->cmd) {
84         ALOGE("invalid command (%d)\n", msg->cmd);
85         return -EINVAL;
86     }
87 
88     *out_size = ((size_t) rc) - sizeof(struct gatekeeper_message);
89     return rc;
90 }
91 
trusty_gatekeeper_disconnect()92 void trusty_gatekeeper_disconnect() {
93     if (handle_ != 0) {
94         tipc_close(handle_);
95     }
96 }
97 
98