-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpsp-proxy-provider-serial.c
More file actions
518 lines (444 loc) · 13.6 KB
/
psp-proxy-provider-serial.c
File metadata and controls
518 lines (444 loc) · 13.6 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/** @file
* PSP proxy library to interface with the hardware of the PSP - remote access over serial
*/
/*
* Copyright (C) 2020 Alexander Eichner <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _DEFAULT_SOURCE
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <common/cdefs.h>
#include <common/types.h>
#include "psp-proxy-provider.h"
/**
* Internal PSP proxy provider context.
*/
typedef struct PSPPROXYPROVCTXINT
{
/** The file descriptor of the device proxying our calls. */
int iFdDev;
/** Flag whether we are currently in blocking mode. */
bool fBlocking;
} PSPPROXYPROVCTXINT;
/** Pointer to an internal PSP proxy context. */
typedef PSPPROXYPROVCTXINT *PPSPPROXYPROVCTXINT;
/**
* Baud rate conversion descriptor.
*/
typedef struct PSPPROXYSERIALBAUDRATECONV
{
/** The baudrate. */
uint32_t u32BaudRate;
/** The termios API identifier. */
speed_t TermiosSpeed;
} PSPPROXYSERIALBAUDRATECONV;
/** Pointer to a baud rate conversion descriptor. */
typedef PSPPROXYSERIALBAUDRATECONV *PPSPPROXYSERIALBAUDRATECONV;
/** Pointer to a const baud rate conversion descriptor. */
typedef const PSPPROXYSERIALBAUDRATECONV *PCPSPPROXYSERIALBAUDRATECONV;
/** Baud rate conversion table. */
static const PSPPROXYSERIALBAUDRATECONV g_aBaudRateConv[] =
{
{ 9600, B9600 },
{ 19200, B19200 },
{ 38400, B38400 },
{ 57600, B57600 },
{ 115200, B115200 }
};
/**
* Ensures that the correct blocking mode is set.
*
* @returns Status code.
* @param pThis The serial providcer context.
* @param fBlocking Flag whether to switch to blocking or non blocking mode.
*/
static int serialProvCtxEnsureBlockingMode(PPSPPROXYPROVCTXINT pThis, bool fBlocking)
{
if (pThis->fBlocking == fBlocking)
return 0;
int rc = 0;
int fFcntl = fcntl(pThis->iFdDev, F_GETFL, 0);
if (fFcntl >= 0)
{
if (fBlocking)
fFcntl &= ~O_NONBLOCK;
else
fFcntl |= O_NONBLOCK;
int rcPsx = fcntl(pThis->iFdDev, F_SETFL, fFcntl);
if (rcPsx != -1)
pThis->fBlocking = fBlocking;
else
rc = -1;
}
else
rc = -1;
return rc;
}
/**
* Converts the given baud rate to the termios speed.
*
* @returns Speed identifier or B0 if no matching one was found.
* @param u32Baudrate The baudrate to convert.
*/
static inline speed_t serialProvCtxBaudrateToTermiosSpeed(uint32_t u32Baudrate)
{
for (uint32_t i = 0; i < ELEMENTS(g_aBaudRateConv); i++)
{
if (g_aBaudRateConv[i].u32BaudRate == u32Baudrate)
return g_aBaudRateConv[i].TermiosSpeed;
}
return B0;
}
/**
* Parses the given device config and returns the individual parameters.
*
* @returns Status code.
* @param pszDevice The device string with the parameters.
* @param ppszDevice Where to store the pointer to the device path to use on success.
* Must be freed with free().
* @param pu32Baudrate Where to store the baud rate on success.
* @param pcDataBits Where to store the number of data bits.
* @param pchParity Where to store the parity.
* @param pcStopBits Where to store the number of stop bits.
*/
static int serialProvCtxParseDevice(const char *pszDevice, char **ppszDevice, uint32_t *pu32Baudrate,
uint8_t *pcDataBits, char *pchParity, uint8_t *pcStopBits)
{
char szDevice[256]; /* Should be plenty. */
memset(&szDevice[0], 0, sizeof(szDevice));
strncpy(&szDevice[0], pszDevice, sizeof(szDevice));
if (szDevice[sizeof(szDevice) - 1] != '\0') /* No termination means buffer overflow -> error. */
return -1;
int rc = 0;
char *pszStart = strchr(&szDevice[0], ':');
if (pszStart)
{
*pszStart = '\0';
pszStart++;
char *pszSep = strchr(pszStart, ':');
if (pszSep)
{
*pszSep = '\0';
pszSep++;
/* Baud rate. */
errno = 0;
*pu32Baudrate = strtoul(pszStart, NULL, 10);
if (!errno)
{
pszStart = pszSep;
pszSep = strchr(pszStart, ':');
if (pszSep)
{
*pszSep = '\0';
pszSep++;
/* Data bit count. */
uint32_t u32Tmp = strtoul(pszStart, NULL, 10);
if ( !errno
&& u32Tmp >= 5
&& u32Tmp <= 8)
{
*pcDataBits = (uint8_t)u32Tmp;
pszStart = pszSep;
pszSep = strchr(pszStart, ':');
if ( pszSep
&& pszSep - pszStart == 1
&& ( *pszStart == 'n'
|| *pszStart == 'o'
|| *pszStart == 'e'))
{
*pchParity = *pszStart;
pszSep++;
pszStart = pszSep;
if ( ( *pszStart == '1'
|| *pszStart == '2')
&& pszStart[1] == '\0')
{
*pcStopBits = *pszStart == '1' ? 1 : 2;
*ppszDevice = strdup(&szDevice[0]);
if (!*ppszDevice)
rc = -1;
}
else
rc = -1;
}
else
rc = -1;
}
else
rc = -1;
}
else
rc = -1;
}
else
rc = -1;
}
else
rc = -1;
}
else
rc = -1;
return rc;
}
/**
* Sets the given serial config.
*
* @returns Status code.
* @param pThis The serial provider context.
* @param u32Baudrate The baudrate to set.
* @param cDataBits Number of databits to set.
* @param chParity Parity to set.
* @param cStopBits Number of stop bits to set.
*/
static int serialProvCtxSetTermiosCfg(PPSPPROXYPROVCTXINT pThis, uint32_t u32Baudrate,
uint8_t cDataBits, char chParity, uint8_t cStopBits)
{
int rc = 0;
speed_t Speed = serialProvCtxBaudrateToTermiosSpeed(u32Baudrate);
if (Speed != B0)
{
struct termios TermiosCfg;
memset(&TermiosCfg, 0, sizeof(TermiosCfg));
cfsetispeed(&TermiosCfg, Speed);
cfsetospeed(&TermiosCfg, Speed);
TermiosCfg.c_cflag |= CREAD | CLOCAL;
switch (cDataBits)
{
case 5:
TermiosCfg.c_cflag |= CS5;
break;
case 6:
TermiosCfg.c_cflag |= CS6;
break;
case 7:
TermiosCfg.c_cflag |= CS7;
break;
case 8:
TermiosCfg.c_cflag |= CS8;
break;
default:
return -1; /* Should not happen as the input is checked serialProvCtxParseDevice(). */
}
switch (chParity)
{
case 'n':
break;
case 'o':
TermiosCfg.c_cflag |= PARENB | PARODD;
break;
case 'e':
TermiosCfg.c_cflag |= PARENB;
break;
default:
return -1; /* Should not happen as the input is checked serialProvCtxParseDevice(). */
}
if (cStopBits == 2)
TermiosCfg.c_cflag |= CSTOPB;
/* Set to raw input mode. */
TermiosCfg.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL | ECHOK | ISIG | IEXTEN);
TermiosCfg.c_cc[VMIN] = 0;
TermiosCfg.c_cc[VTIME] = 0;
/* Flush everything and set new config. */
int rcPsx = tcflush(pThis->iFdDev, TCIOFLUSH);
if (!rcPsx)
{
rcPsx = tcsetattr(pThis->iFdDev, TCSANOW, &TermiosCfg);
if (rcPsx)
rc = -1;
}
else
rc = -1;
}
else
rc = -1;
return rc;
}
/**
* @copydoc{PSPPROXYPROV,pfnCtxInit}
*/
static int serialProvCtxInit(PSPPROXYPROVCTX hProvCtx, const char *pszDevice)
{
PPSPPROXYPROVCTXINT pThis = hProvCtx;
char *pszDevPath = NULL;
uint32_t u32Baudrate = 0;
uint8_t cDataBits = 0;
char chParity = '\0';
uint8_t cStopBits = 0;
int rc = serialProvCtxParseDevice(pszDevice, &pszDevPath, &u32Baudrate,
&cDataBits, &chParity, &cStopBits);
if (!rc)
{
int iFd = open(pszDevPath, O_RDWR);
free(pszDevPath); /* Free device path in any case as it is not required anymore. */
if (iFd > 0)
{
pThis->iFdDev = iFd;
pThis->fBlocking = true;
rc = serialProvCtxSetTermiosCfg(pThis, u32Baudrate, cDataBits, chParity, cStopBits);
if (!rc)
return rc;
close(iFd);
}
else
rc = -1; /** @todo Error handling. */
}
return rc;
}
/**
* @copydoc{PSPPROXYPROV,pfnCtxDestroy}
*/
static void serialProvCtxDestroy(PSPPROXYPROVCTX hProvCtx)
{
PPSPPROXYPROVCTXINT pThis = hProvCtx;
close(pThis->iFdDev);
pThis->iFdDev = 0;
}
/**
* @copydoc{PSPPROXYPROV,pfnPeek}
*/
static size_t serialProvCtxPeek(PSPPROXYPROVCTX hProvCtx)
{
PPSPPROXYPROVCTXINT pThis = hProvCtx;
int cbAvail = 0;
int rc = ioctl(pThis->iFdDev, FIONREAD, &cbAvail);
if (rc)
return 0;
return cbAvail;
}
/**
* @copydoc{PSPPROXYPROV,pfnRead}
*/
static int serialProvCtxRead(PSPPROXYPROVCTX hProvCtx, void *pvDst, size_t cbRead, size_t *pcbRead)
{
PPSPPROXYPROVCTXINT pThis = hProvCtx;
*pcbRead = 0;
if (serialProvCtxEnsureBlockingMode(pThis, false /*fBlocking*/) == -1)
return -1;
ssize_t cbRet = read(pThis->iFdDev, pvDst, cbRead);
if (cbRet > 0)
{
*pcbRead = cbRead;
return 0;
}
if (!cbRet)
return -1;
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
*pcbRead = 0;
return 0;
}
return -1;
}
/**
* @copydoc{PSPSTUBPDUIOIF,pfnWrite}
*/
static int serialProvCtxWrite(PSPPROXYPROVCTX hProvCtx, const void *pvPkt, size_t cbPkt)
{
PPSPPROXYPROVCTXINT pThis = hProvCtx;
if (serialProvCtxEnsureBlockingMode(pThis, true /*fBlocking*/) == -1)
return -1;
ssize_t cbRet = write(pThis->iFdDev, pvPkt, cbPkt);
if (cbRet == cbPkt)
return 0;
return -1;
}
/**
* @copydoc{PSPPROXYPROV,pfnPoll}
*/
static int serialProvCtxPoll(PSPPROXYPROVCTX hProvCtx, uint32_t cMillies)
{
PPSPPROXYPROVCTXINT pThis = hProvCtx;
struct pollfd PollFd;
PollFd.fd = pThis->iFdDev;
PollFd.events = POLLIN | POLLHUP | POLLERR;
PollFd.revents = 0;
int rc = 0;
for (;;)
{
int rcPsx = poll(&PollFd, 1, cMillies);
if (rcPsx == 1)
break; /* Stop polling if the single descriptor has events. */
if (rcPsx == -1)
rc = -1; /** @todo Better status codes for the individual errors. */
}
return rc;
}
/**
* @copydoc{PSPPROXYPROV,pfnInterrupt}
*/
static int serialProvCtxInterrupt(PSPPROXYPROVCTX hProvCtx)
{
return -1; /** @todo */
}
/**
* Provider registration structure.
*/
const PSPPROXYPROV g_PspProxyProvSerial =
{
/** pszId */
"serial",
/** pszDesc */
"PSP access through a serial connection, device schema looks like serial://<device path>:<baudrate>:<data bit count>:<parity [e|n|o]>:<stop bit count>",
/** cbCtx */
sizeof(PSPPROXYPROVCTXINT),
/** fFeatures */
0,
/** pfnCtxInit */
serialProvCtxInit,
/** pfnCtxDestroy */
serialProvCtxDestroy,
/** pfnCtxPeek */
serialProvCtxPeek,
/** pfnCtxRead */
serialProvCtxRead,
/** pfnCtxWrite */
serialProvCtxWrite,
/** pfnCtxPoll */
serialProvCtxPoll,
/** pfnCtxInterrupt */
serialProvCtxInterrupt,
/** pfnCtxX86SmnRead */
NULL,
/** pfnCtxX86SmnWrite */
NULL,
/** pfnCtxX86MemAlloc */
NULL,
/** pfnCtxX86MemFree */
NULL,
/** pfnCtxX86MemRead */
NULL,
/** pfnCtxX86MemWrite */
NULL,
/** pfnCtxX86PhysMemRead */
NULL,
/** pfnCtxX86PhysMemWrite */
NULL,
/** pfnCtxEmuWaitForWork */
NULL,
/** pfnCtxEmuSetResult */
NULL
};