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