1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <stddef.h>
26 #include <test-runner-arch.h>
27 #include <trusty/sysdeps.h>
28 #include <utils.h>
29 #include <virtio-console.h>
30 #include <virtio.h>
31
32 enum msg_type {
33 MSG_LOG,
34 MSG_EXIT,
35 };
36
37 struct msg_log {
38 unsigned char size;
39 char data[];
40 } __attribute__((__packed__));
41
42 struct msg_exit {
43 char status;
44 } __attribute__((__packed__));
45
46 struct msg {
47 unsigned char type;
48 union {
49 struct msg_log log;
50 struct msg_exit exit;
51 };
52 } __attribute__((__packed__));
53
54 static const char* testruner_msg_devname = "testrunner0";
55
56 static struct virtq input;
57 static struct virtq_raw input_raw;
58 static struct virtq output;
59 static struct virtq_raw output_raw;
60
61 #define TESTRUNNER0_BUF_SIZE 64
62 static char msg_buf[TESTRUNNER0_BUF_SIZE];
63
testrunner_msg_scan(struct virtio_console * console)64 static int testrunner_msg_scan(struct virtio_console* console) {
65 for (size_t i = 0; i < MAX_PORTS; i++) {
66 if (!trusty_strcmp(testruner_msg_devname, console->ports[i].name)) {
67 if (!console->ports[i].host_connected) {
68 return -1;
69 }
70
71 return i;
72 }
73 }
74
75 return -1;
76 }
77
init_log(struct virtio_console * console)78 int init_log(struct virtio_console* console) {
79 int port_id = testrunner_msg_scan(console);
80 if (port_id < 0) {
81 return -1;
82 }
83
84 vq_init(&input, &input_raw, console->vio, true);
85 vq_init(&output, &output_raw, console->vio, false);
86
87 virtio_console_connect_port(console, port_id, &input, &output);
88
89 return 0;
90 }
91
host_get_cmdline(char * buf,size_t size)92 size_t host_get_cmdline(char* buf, size_t size) {
93 uint32_t len;
94
95 if (input.raw) {
96 len = recv_vq(&input, buf, size);
97
98 /*
99 * Make sure message is always null terminated, message buffer
100 * exceed maxium size would be dropped.
101 */
102 len = MIN(size - 1, len);
103 buf[len] = '\0';
104
105 return len;
106 } else {
107 return 0;
108 }
109 }
110
log_buf(const char * buf,size_t size)111 void log_buf(const char* buf, size_t size) {
112 size_t remain = size;
113 size_t offset = 0;
114 size_t copied = 0;
115 struct msg* log_msg = (struct msg*)&msg_buf;
116
117 log_msg->type = MSG_LOG;
118 while (remain) {
119 copied = MIN((TESTRUNNER0_BUF_SIZE - sizeof(struct msg)), remain);
120 log_msg->log.size = copied;
121
122 trusty_memcpy(log_msg->log.data, buf + offset, copied);
123
124 /*
125 * Always send all message buffers to host each time, it makes
126 * host side easy to handle messages. With this logic, each
127 * message received in host side contains a message header and
128 * a message body only.
129 */
130 send_vq(&output, msg_buf, TESTRUNNER0_BUF_SIZE);
131
132 remain -= copied;
133 offset += copied;
134 }
135 }
136
host_exit(uint32_t code)137 void host_exit(uint32_t code) {
138 struct msg* exit_msg = (struct msg*)&msg_buf;
139
140 exit_msg->type = MSG_EXIT;
141 exit_msg->exit.status = code;
142
143 send_vq(&output, msg_buf, TESTRUNNER0_BUF_SIZE);
144 }
145
log_msg(const char * msg)146 void log_msg(const char* msg) {
147 log_buf(msg, trusty_strlen(msg));
148 }
149
abort_msg(const char * msg)150 void abort_msg(const char* msg) {
151 log_msg(msg);
152 host_exit(2);
153 }
154