1
+ /*
2
+ * Copyright (C) 2011-2022 Intel Corporation. All rights reserved.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions
6
+ * are met:
7
+ *
8
+ * * Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ * * Redistributions in binary form must reproduce the above copyright
11
+ * notice, this list of conditions and the following disclaimer in
12
+ * the documentation and/or other materials provided with the
13
+ * distribution.
14
+ * * Neither the name of Intel Corporation nor the names of its
15
+ * contributors may be used to endorse or promote products derived
16
+ * from this software without specific prior written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ *
30
+ */
31
+
32
+ #include <sys/socket.h>
33
+ #include <linux/vm_sockets.h>
34
+ #include "qgs_msg_lib.h"
35
+
36
+ #include <stdint.h>
37
+ #include <string.h>
38
+ #include <stdio.h>
39
+ #include <unistd.h>
40
+ #include <stdlib.h>
41
+
42
+
43
+ static const unsigned HEADER_SIZE = 4 ;
44
+
45
+ #define HEX_DUMP_SIZE 16
46
+ static void print_hex_dump (const char * title , const char * prefix_str ,
47
+ const uint8_t * buf , uint32_t len ) {
48
+ const uint8_t * ptr = buf ;
49
+ uint32_t i , rowsize = HEX_DUMP_SIZE ;
50
+
51
+ if (!len || !buf )
52
+ return ;
53
+
54
+ fprintf (stdout , "\t\t%s" , title );
55
+
56
+ for (i = 0 ; i < len ; i ++ ) {
57
+ if (!(i % rowsize ))
58
+ fprintf (stdout , "\n%s%.8x:" , prefix_str , i );
59
+ if (ptr [i ] <= 0x0f )
60
+ fprintf (stdout , " 0%x" , ptr [i ]);
61
+ else
62
+ fprintf (stdout , " %x" , ptr [i ]);
63
+ }
64
+
65
+ fprintf (stdout , "\n" );
66
+ }
67
+
68
+ int main (int argc , char * argv [])
69
+ {
70
+ (void )argc ;
71
+ (void )argv ;
72
+ int s = -1 ;
73
+ int ret = 0 ;
74
+
75
+ uint8_t buf [4 * 1024 ] = {0 };
76
+ uint32_t msg_size = 0 ;
77
+ uint32_t in_msg_size = 0 ;
78
+ uint32_t recieved_bytes = 0 ;
79
+
80
+ uint16_t tdqe_isvsvn ;
81
+ uint16_t pce_isvsvn ;
82
+ const uint8_t * p_platform_id = NULL ;
83
+ uint32_t platform_id_size = 0 ;
84
+ const uint8_t * p_cpusvn = NULL ;
85
+ uint32_t cpusvn_size = 0 ;
86
+
87
+ qgs_msg_error_t qgs_msg_ret = QGS_MSG_SUCCESS ;
88
+ qgs_msg_header_t * p_header = NULL ;
89
+ uint8_t * p_req = NULL ;
90
+
91
+ qgs_msg_ret = qgs_msg_gen_get_platform_info_req (& p_req , & msg_size );
92
+ if (QGS_MSG_SUCCESS != qgs_msg_ret ) {
93
+ fprintf (stderr , "\nqgs_msg_gen_get_platform_info_req return 0x%x\n" , qgs_msg_ret );
94
+ ret = 1 ;
95
+ goto ret_point ;
96
+ }
97
+
98
+ buf [0 ] = (uint8_t )((msg_size >> 24 ) & 0xFF );
99
+ buf [1 ] = (uint8_t )((msg_size >> 16 ) & 0xFF );
100
+ buf [2 ] = (uint8_t )((msg_size >> 8 ) & 0xFF );
101
+ buf [3 ] = (uint8_t )(msg_size & 0xFF );
102
+
103
+ memcpy (buf + HEADER_SIZE , p_req , msg_size );
104
+ qgs_msg_free (p_req );
105
+
106
+ s = socket (AF_VSOCK , SOCK_STREAM , 0 );
107
+ if (-1 == s ) {
108
+ fprintf (stderr , "\nsocket return 0x%x\n" , qgs_msg_ret );
109
+ ret = 1 ;
110
+ goto ret_point ;
111
+ }
112
+ struct sockaddr_vm vm_addr ;
113
+ memset (& vm_addr , 0 , sizeof (vm_addr ));
114
+ vm_addr .svm_family = AF_VSOCK ;
115
+ vm_addr .svm_reserved1 = 0 ;
116
+ vm_addr .svm_port = 4050 ;
117
+ vm_addr .svm_cid = VMADDR_CID_HOST ;
118
+ if (connect (s , (struct sockaddr * )& vm_addr , sizeof (vm_addr ))) {
119
+ fprintf (stderr , "\nconnect error\n" );
120
+ ret = 1 ;
121
+ goto ret_point ;
122
+ }
123
+
124
+ // Write to socket
125
+ if (HEADER_SIZE + msg_size != send (s , buf , HEADER_SIZE + msg_size , 0 )) {
126
+ fprintf (stderr , "\nsend error\n" );
127
+ ret = 1 ;
128
+ goto ret_point ;
129
+ }
130
+
131
+ // Read the response size header
132
+ if (HEADER_SIZE != recv (s , buf , HEADER_SIZE , 0 )) {
133
+ fprintf (stderr , "\nrecv error\n" );
134
+ ret = 1 ;
135
+ goto ret_point ;
136
+ }
137
+
138
+ // decode the size
139
+ for (unsigned i = 0 ; i < HEADER_SIZE ; ++ i ) {
140
+ in_msg_size = in_msg_size * 256 + ((buf [i ]) & 0xFF );
141
+ }
142
+
143
+ if (sizeof (buf ) - HEADER_SIZE < in_msg_size ) {
144
+ fprintf (stderr , "\nReply message body is too big" );
145
+ ret = 1 ;
146
+ goto ret_point ;
147
+ }
148
+ while ( recieved_bytes < in_msg_size ) {
149
+ int recv_ret = (int )recv (s , buf + HEADER_SIZE + recieved_bytes ,
150
+ in_msg_size - recieved_bytes , 0 );
151
+ if (recv_ret < 0 ) {
152
+ fprintf (stderr , "\nrecv return value < 0" );
153
+ ret = 1 ;
154
+ goto ret_point ;
155
+ }
156
+ recieved_bytes += (uint32_t )recv_ret ;
157
+ }
158
+
159
+ qgs_msg_ret = qgs_msg_inflate_get_platform_info_resp (buf + HEADER_SIZE , in_msg_size ,
160
+ & tdqe_isvsvn , & pce_isvsvn , & p_platform_id , & platform_id_size , & p_cpusvn , & cpusvn_size );
161
+
162
+ if (QGS_MSG_SUCCESS != qgs_msg_ret ) {
163
+ fprintf (stderr , "\nqgs_msg_inflate_get_platform_info_resp return 0x%x\n" , qgs_msg_ret );
164
+ ret = 1 ;
165
+ goto ret_point ;
166
+ }
167
+
168
+ // We've called qgs_msg_inflate_get_quote_resp, the message type should be GET_QUOTE_RESP
169
+ p_header = (qgs_msg_header_t * )(buf + HEADER_SIZE );
170
+ if (p_header -> error_code != 0 ) {
171
+ fprintf (stderr , "\nerror code in resp msg is 0x%x" , p_header -> error_code );
172
+ ret = 1 ;
173
+ goto ret_point ;
174
+ }
175
+ fprintf (stdout , "\nPCE_ISVSVN: %d\tTDQE_ISVSVN: %d\n" , pce_isvsvn , tdqe_isvsvn );
176
+ print_hex_dump ("\n\t\tQEID\n" , " " , p_platform_id , platform_id_size );
177
+ print_hex_dump ("\n\t\tCPUSVN\n" , " " , p_cpusvn , cpusvn_size );
178
+ ret = 0 ;
179
+
180
+ ret_point :
181
+ if (s >= 0 ) {
182
+ close (s );
183
+ }
184
+
185
+ return ret ;
186
+ }
0 commit comments