1 /*
2  * Copyright (C) 2020 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 "spiproxyd"
18 
19 #include <assert.h>
20 #include <fcntl.h>
21 #include <getopt.h>
22 #include <log/log.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <trusty/tipc.h>
26 #include <unistd.h>
27 
handle_msg(int trusty_dev_fd,int spi_dev_fd)28 int handle_msg(int trusty_dev_fd, int spi_dev_fd) {
29     int rc;
30     uint8_t msg_buf[4096];
31     size_t msg_len;
32 
33     /* read request from SPI Trusty app */
34     rc = read(trusty_dev_fd, &msg_buf, sizeof(msg_buf));
35     if (rc < 0) {
36         ALOGE("failed (%d) to read request from TA\n", rc);
37         return rc;
38     }
39     msg_len = rc;
40 
41     /* forward request to SPI host device */
42     rc = write(spi_dev_fd, &msg_buf, msg_len);
43     if (rc < 0 || (size_t)rc != msg_len) {
44         ALOGE("failed (%d) to forward request to host\n", rc);
45         return rc < 0 ? rc : -1;
46     }
47 
48     /* read response from SPI host device */
49     rc = read(spi_dev_fd, &msg_buf, sizeof(msg_buf));
50     if (rc < 0) {
51         ALOGE("failed (%d) to read response from host\n", rc);
52         return rc;
53     }
54     msg_len = rc;
55 
56     /* forward response to SPI Trusty app */
57     rc = write(trusty_dev_fd, &msg_buf, msg_len);
58     if (rc < 0 || (size_t)rc != msg_len) {
59         ALOGE("failed (%d) to forward response to TA\n", rc);
60         return rc < 0 ? rc : -1;
61     }
62 
63     return 0;
64 }
65 
event_loop(int trusty_dev_fd,int spi_dev_fd)66 int event_loop(int trusty_dev_fd, int spi_dev_fd) {
67     while (true) {
68         int rc = handle_msg(trusty_dev_fd, spi_dev_fd);
69         if (rc < 0) {
70             ALOGE("exiting event loop\n");
71             return EXIT_FAILURE;
72         }
73     }
74 }
75 
show_usage()76 static void show_usage() {
77     ALOGE("usage: spiproxyd -t TRUSTY_DEVICE -s SPI_DEVICE -p SPI_PROXY_PORT\n");
78 }
79 
parse_args(int argc,char * argv[],const char ** trusty_dev_name,const char ** spi_dev_name,const char ** spi_proxy_port)80 static void parse_args(int argc, char* argv[], const char** trusty_dev_name,
81                        const char** spi_dev_name, const char** spi_proxy_port) {
82     int opt;
83     while ((opt = getopt(argc, argv, "ht:s:p:")) != -1) {
84         switch (opt) {
85             case 'h':
86                 show_usage();
87                 exit(EXIT_SUCCESS);
88                 break;
89             case 't':
90                 *trusty_dev_name = strdup(optarg);
91                 break;
92             case 's':
93                 *spi_dev_name = strdup(optarg);
94                 break;
95             case 'p':
96                 *spi_proxy_port = strdup(optarg);
97                 break;
98             default:
99                 show_usage();
100                 exit(EXIT_FAILURE);
101                 break;
102         }
103     }
104 
105     if (!*trusty_dev_name || !*spi_dev_name || !*spi_proxy_port) {
106         show_usage();
107         exit(EXIT_FAILURE);
108     }
109 }
110 
main(int argc,char * argv[])111 int main(int argc, char* argv[]) {
112     int rc;
113     const char* trusty_dev_name = NULL;
114     const char* spi_dev_name = NULL;
115     const char* spi_proxy_port = NULL;
116     int trusty_dev_fd;
117     int spi_dev_fd;
118 
119     parse_args(argc, argv, &trusty_dev_name, &spi_dev_name, &spi_proxy_port);
120 
121     rc = tipc_connect(trusty_dev_name, spi_proxy_port);
122     if (rc < 0) {
123         ALOGE("failed (%d) to connect to SPI proxy port\n", rc);
124         return rc;
125     }
126     trusty_dev_fd = rc;
127 
128     rc = open(spi_dev_name, O_RDWR, 0);
129     if (rc < 0) {
130         ALOGE("failed (%d) to open SPI device\n", rc);
131         return rc;
132     }
133     spi_dev_fd = rc;
134 
135     return event_loop(trusty_dev_fd, spi_dev_fd);
136 }
137