1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
/* $Id: TestExecServiceSerial.cpp $ */
/** @file
* TestExecServ - Basic Remote Execution Service, Serial port Transport Layer.
*/
/*
* Copyright (C) 2018-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
* in the VirtualBox distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP RTLOGGROUP_DEFAULT
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/err.h>
#include <iprt/log.h>
#include <iprt/mem.h>
#include <iprt/message.h>
#include <iprt/string.h>
#include <iprt/serialport.h>
#include <iprt/thread.h>
#include <iprt/time.h>
#include "TestExecServiceInternal.h"
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
/** The default baud rate port. */
#define TXS_SERIAL_DEF_BAUDRATE 115200
/** The default serial device to use. */
#if defined(RT_OS_LINUX)
# define TXS_SERIAL_DEF_DEVICE "/dev/ttyS0"
#elif defined(RT_OS_WINDOWS)
# define TXS_SERIAL_DEF_DEVICE "COM1"
#elif defined(RT_OS_SOLARIS)
# define TXS_SERIAL_DEF_DEVICE "<todo>"
#elif defined(RT_OS_FREEBSD)
# define TXS_SERIAL_DEF_DEVICE "<todo>"
#elif defined(RT_OS_DARWIN)
# define TXS_SERIAL_DEF_DEVICE "<todo>"
#else
# error "Port me"
#endif
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/** @name Serial Parameters
* @{ */
/** The addresses to bind to. Empty string means any. */
static uint32_t g_uSerialBaudRate = TXS_SERIAL_DEF_BAUDRATE;
/** The serial port device to use. */
static char g_szSerialDevice[256] = TXS_SERIAL_DEF_DEVICE;
/** @} */
/** The serial port handle. */
static RTSERIALPORT g_hSerialPort = NIL_RTSERIALPORT;
/** The size of the stashed data. */
static size_t g_cbSerialStashed = 0;
/** The size of the stashed data allocation. */
static size_t g_cbSerialStashedAlloced = 0;
/** The stashed data. */
static uint8_t *g_pbSerialStashed = NULL;
/**
* @interface_method_impl{TXSTRANSPORT,pfnNotifyReboot}
*/
static DECLCALLBACK(void) txsSerialNotifyReboot(void)
{
/* nothing to do here */
}
/**
* @interface_method_impl{TXSTRANSPORT,pfnNotifyBye}
*/
static DECLCALLBACK(void) txsSerialNotifyBye(void)
{
/* nothing to do here */
}
/**
* @interface_method_impl{TXSTRANSPORT,pfnNotifyHowdy}
*/
static DECLCALLBACK(void) txsSerialNotifyHowdy(void)
{
/* nothing to do here */
}
/**
* @interface_method_impl{TXSTRANSPORT,pfnBabble}
*/
static DECLCALLBACK(void) txsSerialBabble(PCTXSPKTHDR pPktHdr, RTMSINTERVAL cMsSendTimeout)
{
Assert(g_hSerialPort != NIL_RTSERIALPORT);
/*
* Try send the babble reply.
*/
NOREF(cMsSendTimeout); /** @todo implement the timeout here; non-blocking write + select-on-write. */
int rc;
size_t cbToSend = RT_ALIGN_Z(pPktHdr->cb, TXSPKT_ALIGNMENT);
do rc = RTSerialPortWrite(g_hSerialPort, pPktHdr, cbToSend, NULL);
while (rc == VERR_INTERRUPTED);
/*
* Disconnect the client.
*/
Log(("txsSerialBabble: RTSerialPortWrite rc=%Rrc\n", rc));
}
/**
* @interface_method_impl{TXSTRANSPORT,pfnSendPkt}
*/
static DECLCALLBACK(int) txsSerialSendPkt(PCTXSPKTHDR pPktHdr)
{
Assert(g_hSerialPort != NIL_RTSERIALPORT);
Assert(pPktHdr->cb >= sizeof(TXSPKTHDR));
/*
* Write it.
*/
size_t cbToSend = RT_ALIGN_Z(pPktHdr->cb, TXSPKT_ALIGNMENT);
int rc = RTSerialPortWrite(g_hSerialPort, pPktHdr, cbToSend, NULL);
if ( RT_FAILURE(rc)
&& rc != VERR_INTERRUPTED)
{
/* assume fatal connection error. */
Log(("RTSerialPortWrite -> %Rrc\n", rc));
}
return rc;
}
/**
* @interface_method_impl{TXSTRANSPORT,pfnRecvPkt}
*/
static DECLCALLBACK(int) txsSerialRecvPkt(PPTXSPKTHDR ppPktHdr)
{
Assert(g_hSerialPort != NIL_RTSERIALPORT);
int rc = VINF_SUCCESS;
*ppPktHdr = NULL;
/*
* Read state.
*/
size_t offData = 0;
size_t cbData = 0;
size_t cbDataAlloced;
uint8_t *pbData = NULL;
/*
* Any stashed data?
*/
if (g_cbSerialStashedAlloced)
{
offData = g_cbSerialStashed;
cbDataAlloced = g_cbSerialStashedAlloced;
pbData = g_pbSerialStashed;
g_cbSerialStashed = 0;
g_cbSerialStashedAlloced = 0;
g_pbSerialStashed = NULL;
}
else
{
cbDataAlloced = RT_ALIGN_Z(64, TXSPKT_ALIGNMENT);
pbData = (uint8_t *)RTMemAlloc(cbDataAlloced);
if (!pbData)
return VERR_NO_MEMORY;
}
/*
* Read and valid the length.
*/
while (offData < sizeof(uint32_t))
{
size_t cbRead = sizeof(uint32_t) - offData;
rc = RTSerialPortRead(g_hSerialPort, pbData + offData, cbRead, NULL);
if (RT_FAILURE(rc))
break;
offData += cbRead;
}
if (RT_SUCCESS(rc))
{
ASMCompilerBarrier(); /* paranoia^3 */
cbData = *(uint32_t volatile *)pbData;
if (cbData >= sizeof(TXSPKTHDR) && cbData <= TXSPKT_MAX_SIZE)
{
/*
* Align the length and reallocate the return packet it necessary.
*/
cbData = RT_ALIGN_Z(cbData, TXSPKT_ALIGNMENT);
if (cbData > cbDataAlloced)
{
void *pvNew = RTMemRealloc(pbData, cbData);
if (pvNew)
{
pbData = (uint8_t *)pvNew;
cbDataAlloced = cbData;
}
else
rc = VERR_NO_MEMORY;
}
if (RT_SUCCESS(rc))
{
/*
* Read the remainder of the data.
*/
while (offData < cbData)
{
size_t cbRead = cbData - offData;
rc = RTSerialPortRead(g_hSerialPort, pbData + offData, cbRead, NULL);
if (RT_FAILURE(rc))
break;
offData += cbRead;
}
}
}
else
rc = VERR_NET_PROTOCOL_ERROR;
}
if (RT_SUCCESS(rc))
*ppPktHdr = (PTXSPKTHDR)pbData;
else
{
/*
* Deal with errors.
*/
if (rc == VERR_INTERRUPTED)
{
/* stash it away for the next call. */
g_cbSerialStashed = cbData;
g_cbSerialStashedAlloced = cbDataAlloced;
g_pbSerialStashed = pbData;
}
else
{
RTMemFree(pbData);
/* assume fatal connection error. */
Log(("txsSerialRecvPkt: RTSerialPortRead -> %Rrc\n", rc));
}
}
return rc;
}
/**
* @interface_method_impl{TXSTRANSPORT,pfnPollIn}
*/
static DECLCALLBACK(bool) txsSerialPollIn(void)
{
Assert(g_hSerialPort != NIL_RTSERIALPORT);
uint32_t fEvtsRecv = 0;
int rc = RTSerialPortEvtPoll(g_hSerialPort, RTSERIALPORT_EVT_F_DATA_RX,
&fEvtsRecv, 0/*cMillies*/);
return RT_SUCCESS(rc);
}
/**
* @interface_method_impl{TXSTRANSPORT,pfnTerm}
*/
static DECLCALLBACK(void) txsSerialTerm(void)
{
if (g_hSerialPort != NIL_RTSERIALPORT)
RTSerialPortClose(g_hSerialPort);
/* Clean up stashing. */
if (g_pbSerialStashed)
RTMemFree(g_pbSerialStashed);
g_pbSerialStashed = NULL;
g_cbSerialStashed = 0;
g_cbSerialStashedAlloced = 0;
Log(("txsSerialTerm: done\n"));
}
/**
* @interface_method_impl{TXSTRANSPORT,pfnInit}
*/
static DECLCALLBACK(int) txsSerialInit(void)
{
uint32_t fOpenFlags = RTSERIALPORT_OPEN_F_READ | RTSERIALPORT_OPEN_F_WRITE;
int rc = RTSerialPortOpen(&g_hSerialPort, &g_szSerialDevice[0], fOpenFlags);
if (RT_SUCCESS(rc))
{
RTSERIALPORTCFG SerPortCfg;
SerPortCfg.uBaudRate = g_uSerialBaudRate;
SerPortCfg.enmParity = RTSERIALPORTPARITY_NONE;
SerPortCfg.enmDataBitCount = RTSERIALPORTDATABITS_8BITS;
SerPortCfg.enmStopBitCount = RTSERIALPORTSTOPBITS_ONE;
rc = RTSerialPortCfgSet(g_hSerialPort, &SerPortCfg, NULL);
if (RT_FAILURE(rc))
{
RTMsgError("RTSerialPortCfgSet() failed: %Rrc\n", rc);
RTSerialPortClose(g_hSerialPort);
g_hSerialPort = NIL_RTSERIALPORT;
}
}
else
RTMsgError("RTSerialPortOpen(, %s, %#x) failed: %Rrc\n",
g_szSerialDevice, fOpenFlags, rc);
return rc;
}
/** Options */
enum TXSSERIALOPT
{
TXSSERIALOPT_BAUDRATE = 1000,
TXSSERIALOPT_DEVICE
};
/**
* @interface_method_impl{TXSTRANSPORT,pfnOption}
*/
static DECLCALLBACK(int) txsSerialOption(int ch, PCRTGETOPTUNION pVal)
{
int rc;
switch (ch)
{
case TXSSERIALOPT_DEVICE:
rc = RTStrCopy(g_szSerialDevice, sizeof(g_szSerialDevice), pVal->psz);
if (RT_FAILURE(rc))
return RTMsgErrorRc(VERR_INVALID_PARAMETER, "Serial port device path is too long (%Rrc)", rc);
if (!g_szSerialDevice[0])
strcpy(g_szSerialDevice, TXS_SERIAL_DEF_DEVICE);
return VINF_SUCCESS;
case TXSSERIALOPT_BAUDRATE:
g_uSerialBaudRate = pVal->u32 == 0 ? TXS_SERIAL_DEF_BAUDRATE : pVal->u32;
return VINF_SUCCESS;
}
return VERR_TRY_AGAIN;
}
/**
* @interface_method_impl{TXSTRANSPORT,pfnUsage}
*/
DECLCALLBACK(void) txsSerialUsage(PRTSTREAM pStream)
{
RTStrmPrintf(pStream,
" --serial-device <device>\n"
" Selects the serial port to use.\n"
" Default: %s\n"
" --serial-baudrate <baudrate>\n"
" Selects the baudrate to set the serial port to.\n"
" Default: %u\n"
, TXS_SERIAL_DEF_DEVICE, TXS_SERIAL_DEF_BAUDRATE);
}
/** Command line options for the serial transport layer. */
static const RTGETOPTDEF g_SerialOpts[] =
{
{ "--serial-device", TXSSERIALOPT_DEVICE, RTGETOPT_REQ_STRING },
{ "--serial-baudrate", TXSSERIALOPT_BAUDRATE, RTGETOPT_REQ_UINT32 }
};
/** Serial port transport layer. */
const TXSTRANSPORT g_SerialTransport =
{
/* .szName = */ "serial",
/* .pszDesc = */ "Serial",
/* .cOpts = */ &g_SerialOpts[0],
/* .paOpts = */ RT_ELEMENTS(g_SerialOpts),
/* .pfnUsage = */ txsSerialUsage,
/* .pfnOption = */ txsSerialOption,
/* .pfnInit = */ txsSerialInit,
/* .pfnTerm = */ txsSerialTerm,
/* .pfnPollIn = */ txsSerialPollIn,
/* .pfnPollSetAdd = */ NULL,
/* .pfnRecvPkt = */ txsSerialRecvPkt,
/* .pfnSendPkt = */ txsSerialSendPkt,
/* .pfnBabble = */ txsSerialBabble,
/* .pfnNotifyHowdy = */ txsSerialNotifyHowdy,
/* .pfnNotifyBye = */ txsSerialNotifyBye,
/* .pfnNotifyReboot = */ txsSerialNotifyReboot,
/* .u32EndMarker = */ UINT32_C(0x12345678)
};
|