-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpsp-stub-pdu.c
1385 lines (1210 loc) · 49.4 KB
/
psp-stub-pdu.c
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file
* PSP proxy library to interface with the hardware of the PSP - PDU protocol handling.
*/
/*
* 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
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <common/status.h>
#include <common/cdefs.h>
#include <psp-stub/psp-serial-stub.h>
#include "psp-stub-pdu.h"
/** Maximum number of CCDs supported at the moment. */
#define PSP_CCDS_MAX 16
/**
* PDU receive states.
*/
typedef enum PSPSERIALPDURECVSTATE
{
/** Invalid receive state. */
PSPSERIALPDURECVSTATE_INVALID = 0,
/** Waiting for the magic. */
PSPSERIALPDURECVSTATE_MAGIC,
/** Currently receiveing the header. */
PSPSERIALPDURECVSTATE_HDR,
/** Currently receiveing the payload. */
PSPSERIALPDURECVSTATE_PAYLOAD,
/** Currently receiving the footer. */
PSPSERIALPDURECVSTATE_FOOTER,
/** 32bit hack. */
PSPSERIALPDURECVSTATE_32BIT_HACK = 0x7fffffff
} PSPSERIALPDURECVSTATE;
/**
* Internal PSP PDU context.
*/
typedef struct PSPSTUBPDUCTXINT
{
/** Proxy provider interface table. */
PCPSPPROXYPROV pProvIf;
/** Proxy provider handle. */
PSPPROXYPROVCTX hProvCtx;
/** Proxy I/O interface callbacks. */
PCPSPPROXYIOIF pProxyIoIf;
/** Proxy context handle. */
PSPPROXYCTX hProxyCtx;
/** Opaque user data to pass to the pProxyIoIf callbacks. */
void *pvProxyIoUser;
/** Number of PDUs sent so far. */
uint32_t cPdusSent;
/** Next PDU counter value expected for a received PDU. */
uint32_t cPduRecvNext;
/** Beacons seen. */
uint32_t cBeaconsSeen;
/** The PDU receive state. */
PSPSERIALPDURECVSTATE enmPduRecvState;
/** Number of bytes to receive remaining in the current state. */
size_t cbPduRecvLeft;
/** Current offset into the PDU buffer. */
uint32_t offPduRecv;
/** The PDU receive buffer. */
uint8_t abPdu[4096];
/** Flag whether a connection was established. */
bool fConnect;
/** Maximum PDU length supported. */
uint32_t cbPduMax;
/** Status code of the last request. */
PSPSTS rcReqLast;
/** Size of the scratch space area in bytes. */
uint32_t cbScratch;
/** Start address of the scratch space area. */
PSPADDR PspAddrScratch;
/** Number of sockets in the system. */
uint32_t cSysSockets;
/** Number of CCDs in the systems */
uint32_t cCcdsPerSocket;
/** Total number of CCDs in the remote system. */
uint32_t cCcds;
/** Log message buffer. */
char achLogMsg[1024];
/** Number of bytes valid in the log message buffer. */
size_t cchLogMsgAvail;
/** Number of CCDs for which we received a IRQ status change notification. */
uint32_t cCcdsIrqChange;
/** Array of per CCD IRQ notification received since last time flag. */
bool afPerCcdIrqNotRcvd[PSP_CCDS_MAX];
/** Array of per CCD IRQ flags. */
bool afPerCcdIrq[PSP_CCDS_MAX];
/** Array of per CCD FIRQ flags. */
bool afPerCcdFirq[PSP_CCDS_MAX];
} PSPSTUBPDUCTXINT;
/** Pointer to an internal PSP proxy context. */
typedef PSPSTUBPDUCTXINT *PPSPSTUBPDUCTXINT;
/**
* Resets the PDU receive state machine.
*
* @returns nothing.
* @param pThis The serial stub instance data.
*/
static void pspStubPduCtxRecvReset(PPSPSTUBPDUCTXINT pThis)
{
pThis->enmPduRecvState = PSPSERIALPDURECVSTATE_MAGIC;
pThis->cbPduRecvLeft = sizeof(uint32_t); //sizeof(PSPSERIALPDUHDR);
pThis->offPduRecv = 0;
}
/**
* Validates the given PDU header.
*
* @returns Status code.
* @param pThis The serial stub instance data.
* @param pHdr PDU header to validate.
*/
static int pspStubPduCtxHdrValidate(PPSPSTUBPDUCTXINT pThis, PCPSPSERIALPDUHDR pHdr)
{
if (pHdr->u32Magic != PSP_SERIAL_PSP_2_EXT_PDU_START_MAGIC)
return -1;
if (pHdr->u.Fields.cbPdu > sizeof(pThis->abPdu) - sizeof(PSPSERIALPDUHDR) - sizeof(PSPSERIALPDUFOOTER))
return -1;
if (!( ( pHdr->u.Fields.enmRrnId >= PSPSERIALPDURRNID_NOTIFICATION_FIRST
&& pHdr->u.Fields.enmRrnId < PSPSERIALPDURRNID_NOTIFICATION_INVALID_FIRST)
|| ( pHdr->u.Fields.enmRrnId >= PSPSERIALPDURRNID_RESPONSE_FIRST
&& pHdr->u.Fields.enmRrnId < PSPSERIALPDURRNID_RESPONSE_INVALID_FIRST)))
return -1;
if ( pHdr->u.Fields.cPdus != pThis->cPduRecvNext + 1
&& pThis->fConnect)
return -1;
if (pHdr->u.Fields.idCcd >= pThis->cCcds)
return -1;
return 0;
}
/**
* Validates the complete PDU, the header was validated mostly at an earlier stage already.
*
* @returns Status code.
* @param pThis The serial stub instance data.
* @param pHdr The header of the PDU to validate.
*/
static int pspStubPduCtxValidate(PPSPSTUBPDUCTXINT pThis, PCPSPSERIALPDUHDR pHdr)
{
uint32_t uChkSum = 0;
size_t cbPad = ((pHdr->u.Fields.cbPdu + 7) & ~(size_t)7) - pHdr->u.Fields.cbPdu;
for (uint32_t i = 0; i < sizeof(pHdr->u.ab); i++)
uChkSum += pHdr->u.ab[i];
/* Verify padding is all 0 by including it in the checksum. */
uint8_t *pbPayload = (uint8_t *)(pHdr + 1);
for (uint32_t i = 0; i < pHdr->u.Fields.cbPdu + cbPad; i++)
uChkSum += *pbPayload++;
/* Check whether the footer magic and checksum are valid. */
PCPSPSERIALPDUFOOTER pFooter = (PCPSPSERIALPDUFOOTER)pbPayload;
if ( uChkSum + pFooter->u32ChkSum != 0
|| pFooter->u32Magic != PSP_SERIAL_PSP_2_EXT_PDU_END_MAGIC)
return -1;
return 0;
}
/**
* Processes the current state and advances to the next one.
*
* @returns Status code.
* @param pThis The serial stub instance data.
* @param ppPduRcvd Where to store the pointer to the received complete PDU on success.
*/
static int pspStubPduCtxRecvAdvance(PPSPSTUBPDUCTXINT pThis, PCPSPSERIALPDUHDR *ppPduRcvd)
{
int rc = 0;
*ppPduRcvd = NULL;
switch (pThis->enmPduRecvState)
{
case PSPSERIALPDURECVSTATE_MAGIC:
{
if (*(uint32_t *)&pThis->abPdu[0] == PSP_SERIAL_PSP_2_EXT_PDU_START_MAGIC)
{
pThis->enmPduRecvState = PSPSERIALPDURECVSTATE_HDR;
pThis->cbPduRecvLeft = sizeof(PSPSERIALPDUHDR) - sizeof(uint32_t); /* Magic was already received. */
}
else
{
/* Remove the first byte and teceive the next byte (the last 3 bytes could belong to the magic). */
pThis->abPdu[0] = pThis->abPdu[1];
pThis->abPdu[1] = pThis->abPdu[2];
pThis->abPdu[2] = pThis->abPdu[3];
pThis->cbPduRecvLeft = 1;
pThis->offPduRecv = 3;
}
break;
}
case PSPSERIALPDURECVSTATE_HDR:
{
/* Validate header. */
PCPSPSERIALPDUHDR pHdr = (PCPSPSERIALPDUHDR)&pThis->abPdu[0];
int rc2 = pspStubPduCtxHdrValidate(pThis, pHdr);
if (!rc2)
{
/* No payload means going directly to the footer. */
if (pHdr->u.Fields.cbPdu)
{
size_t cbPad = ((pHdr->u.Fields.cbPdu + 7) & ~(size_t)7) - pHdr->u.Fields.cbPdu;
pThis->enmPduRecvState = PSPSERIALPDURECVSTATE_PAYLOAD;
pThis->cbPduRecvLeft = pHdr->u.Fields.cbPdu + cbPad;
}
else
{
pThis->enmPduRecvState = PSPSERIALPDURECVSTATE_FOOTER;
pThis->cbPduRecvLeft = sizeof(PSPSERIALPDUFOOTER);
}
}
else
{
/** @todo Send out of band error. */
pspStubPduCtxRecvReset(pThis);
}
break;
}
case PSPSERIALPDURECVSTATE_PAYLOAD:
{
/* Just advance to the next state. */
pThis->enmPduRecvState = PSPSERIALPDURECVSTATE_FOOTER;
pThis->cbPduRecvLeft = sizeof(PSPSERIALPDUFOOTER);
break;
}
case PSPSERIALPDURECVSTATE_FOOTER:
{
/* Validate the footer and complete PDU. */
PCPSPSERIALPDUHDR pHdr = (PCPSPSERIALPDUHDR)&pThis->abPdu[0];
rc = pspStubPduCtxValidate(pThis, pHdr);
if (!rc)
{
pThis->cPduRecvNext++;
*ppPduRcvd = pHdr;
}
/** @todo Send out of band error. */
/* Start receiving a new PDU in any case. */
pspStubPduCtxRecvReset(pThis);
break;
}
default:
rc = -1;
}
return rc;
}
/**
* Waits for a PDU to be received or until the given timeout elapsed.
*
* @returns Status code.
* @param pThis The serial stub instance data.
* @param ppPduRcvd Where to store the pointer to the received complete PDU on success.
* @param cMillies Amount of milliseconds to wait until a timeout is returned.
*/
static int pspStubPduCtxRecv(PPSPSTUBPDUCTXINT pThis, PCPSPSERIALPDUHDR *ppPduRcvd, uint32_t cMillies)
{
int rc = 0;
/** @todo Timeout handling. */
do
{
rc = pThis->pProvIf->pfnCtxPoll(pThis->hProvCtx, cMillies);
if (rc == STS_ERR_PSP_PROXY_TIMEOUT)
break;
if (!rc)
{
size_t cbAvail = pThis->pProvIf->pfnCtxPeek(pThis->hProvCtx);
if (cbAvail)
{
/* Only read what is required for the current state. */
/** @todo If the connection turns out to be unreliable we have to do a marker search first. */
size_t cbThisRecv = MIN(cbAvail, pThis->cbPduRecvLeft);
rc = pThis->pProvIf->pfnCtxRead(pThis->hProvCtx, &pThis->abPdu[pThis->offPduRecv], cbThisRecv, &cbThisRecv);
if (!rc)
{
pThis->offPduRecv += cbThisRecv;
pThis->cbPduRecvLeft -= cbThisRecv;
/* Advance state machine and process the data if this state is complete. */
if (!pThis->cbPduRecvLeft)
{
rc = pspStubPduCtxRecvAdvance(pThis, ppPduRcvd);
if ( !rc
&& *ppPduRcvd != NULL)
break; /* We received a complete and valid PDU. */
}
}
}
}
} while (!rc);
return rc;
}
/**
* Waits for a PDU with the specific ID to be received.
*
* @returns nothing.
* @param pThis The serial stub instance data.
* @param pPdu The log message PDU.
*/
static void pspStubPduCtxLogMsgHandle(PPSPSTUBPDUCTXINT pThis, PCPSPSERIALPDUHDR pPdu)
{
size_t cchMsg = pPdu->u.Fields.cbPdu;
const char *pbMsg = (const char *)(pPdu + 1);
/* Drop any log message PDU which is too big to fit into the message buffer. */
if (sizeof(pThis->achLogMsg) - pThis->cchLogMsgAvail >= cchMsg)
{
memcpy(&pThis->achLogMsg[pThis->cchLogMsgAvail], pbMsg, cchMsg);
pThis->cchLogMsgAvail += cchMsg;
for (;;)
{
/* Parse the buffer for newlines and hand them over to the callback. */
char *pszNewLine = strchr(&pThis->achLogMsg[0], '\n');
if (pszNewLine)
{
/* Terminate the string after the newline. */
pszNewLine++;
char chOld = *pszNewLine;
*pszNewLine = '\0';
pThis->pProxyIoIf->pfnLogMsg(pThis->hProxyCtx, pThis->pvProxyIoUser, &pThis->achLogMsg[0]);
/* Restore old value and move everything remaining to the front, clearing out the remainder. */
*pszNewLine = chOld;
size_t cchMsg = pszNewLine - &pThis->achLogMsg[0];
/** @todo assert(cchMsg <= pThis->cchLogMsgAvail) */
memmove(&pThis->achLogMsg[0], pszNewLine, pThis->cchLogMsgAvail - cchMsg);
pThis->cchLogMsgAvail -= cchMsg;
memset(&pThis->achLogMsg[pThis->cchLogMsgAvail], 0, sizeof(pThis->achLogMsg) - pThis->cchLogMsgAvail);
if (!pThis->cchLogMsgAvail)
break;
}
else
break;
}
}
}
/**
* handles an output buffer write.
*
* @returns nothing.
* @param pThis The serial stub instance data.
* @param pPdu The log message PDU.
*/
static void pspStubPduCtxOutBufWriteHandle(PPSPSTUBPDUCTXINT pThis, PCPSPSERIALPDUHDR pPdu)
{
PCPSPSERIALOUTBUFNOT pNot = (PCPSPSERIALOUTBUFNOT)(pPdu + 1);
const uint8_t *pbData = (const uint8_t *)(pNot + 1);
size_t cbData = pPdu->u.Fields.cbPdu - sizeof(*pNot);
pThis->pProxyIoIf->pfnOutBufWrite(pThis->hProxyCtx, pThis->pvProxyIoUser, pNot->idOutBuf,
pbData, cbData);
}
/**
* Waits for a PDU with the specific ID to be received.
*
* @returns Status code.
* @param pThis The serial stub instance data.
* @param enmRrnId The PDU ID to wait for.
* @param ppPduRcvd Where to store the pointer to the received complete PDU on success.
* @param ppvPayload Where to store the pointer to the payload data on success.
* @param pcbPayload Where to store the size of the payload in bytes on success.
* @param cMillies Amount of milliseconds to wait until a timeout is returned.
*/
static int pspStubPduCtxRecvId(PPSPSTUBPDUCTXINT pThis, PSPSERIALPDURRNID enmRrnId, PCPSPSERIALPDUHDR *ppPduRcvd,
void **ppvPayload, size_t *pcbPayload, uint32_t cMillies)
{
int rc = 0;
while (!rc)
{
PCPSPSERIALPDUHDR pPdu = NULL;
rc = pspStubPduCtxRecv(pThis, &pPdu, cMillies);
if (rc == STS_ERR_PSP_PROXY_TIMEOUT)
break;
if (!rc)
{
if (pPdu->u.Fields.enmRrnId != enmRrnId)
{
if (pPdu->u.Fields.enmRrnId == PSPSERIALPDURRNID_NOTIFICATION_LOG_MSG)
{
if ( pThis->pProxyIoIf
&& pThis->pProxyIoIf->pfnLogMsg)
pspStubPduCtxLogMsgHandle(pThis, pPdu);
continue;
}
else if (pPdu->u.Fields.enmRrnId == PSPSERIALPDURRNID_NOTIFICATION_OUT_BUF)
{
if ( pThis->pProxyIoIf
&& pThis->pProxyIoIf->pfnOutBufWrite)
pspStubPduCtxOutBufWriteHandle(pThis, pPdu);
continue;
}
else if (pPdu->u.Fields.enmRrnId == PSPSERIALPDURRNID_NOTIFICATION_IRQ)
{
uint32_t idCcd = pPdu->u.Fields.idCcd;
PCPSPSERIALIRQNOT pIrqNot = (PCPSPSERIALIRQNOT)(pPdu + 1);
if (idCcd < PSP_CCDS_MAX)
{
if (!pThis->afPerCcdIrqNotRcvd[idCcd])
{
pThis->afPerCcdIrqNotRcvd[idCcd] = true;
pThis->afPerCcdIrq[idCcd] = (pIrqNot->fIrqCur & PSP_SERIAL_NOTIFICATION_IRQ_PENDING_IRQ) ? true : false;
pThis->afPerCcdFirq[idCcd] = (pIrqNot->fIrqCur & PSP_SERIAL_NOTIFICATION_IRQ_PENDING_FIQ) ? true : false;
pThis->cCcdsIrqChange++;
}
}
else
{
rc = -1;
break;
}
continue;
}
else if (pPdu->u.Fields.enmRrnId == PSPSERIALPDURRNID_NOTIFICATION_BEACON)
{
/*
* Beacons are only ignored if not in connected mode or when
* the counter matches what we've seen so far.
*
* A reset counter means that the target reset.
*/
PCPSPSERIALBEACONNOT pBeacon = (PCPSPSERIALBEACONNOT)(pPdu + 1);
if ( !pThis->fConnect
|| pBeacon->cBeaconsSent == pThis->cBeaconsSeen + 1)
{
pThis->cBeaconsSeen++;
continue;
}
}
rc = -1; /* Unexpected PDU received or system resetted. */
break;
}
else
{
/* Return the PDU. */
*ppPduRcvd = pPdu;
if (ppvPayload)
*ppvPayload = (void *)(pPdu + 1);
if (pcbPayload)
*pcbPayload = pPdu->u.Fields.cbPdu;
break;
}
}
}
return rc;
}
/**
* Sends the given PDU.
*
* @returns Status code.
* @param pThis The serial stub instance data.
* @param idCcd The CCD ID the PDU is designated for.
* @param enmPduRrnId The Request/Response/Notification ID.
* @param pvPayload Pointer to the PDU payload to send, optional.
* @param cbPayload Size of the PDU payload in bytes.
*/
static int pspStubPduCtxSend(PPSPSTUBPDUCTXINT pThis, uint32_t idCcd, PSPSERIALPDURRNID enmPduRrnId, const void *pvPayload, size_t cbPayload)
{
PSPSERIALPDUHDR PduHdr;
PSPSERIALPDUFOOTER PduFooter;
uint8_t abPad[7] = { 0 };
size_t cbPad = ((cbPayload + 7) & ~(size_t)7) - cbPayload; /* Pad the payload to an 8 byte alignment so the footer is properly aligned. */
/* Initialize header and footer. */
PduHdr.u32Magic = PSP_SERIAL_EXT_2_PSP_PDU_START_MAGIC;
PduHdr.u.Fields.cbPdu = cbPayload;
PduHdr.u.Fields.cPdus = ++pThis->cPdusSent;
PduHdr.u.Fields.enmRrnId = enmPduRrnId;
PduHdr.u.Fields.idCcd = idCcd;
PduHdr.u.Fields.tsMillies = 0;
uint32_t uChkSum = 0;
for (uint32_t i = 0; i < sizeof(PduHdr.u.ab); i++)
uChkSum += PduHdr.u.ab[i];
const uint8_t *pbPayload = (const uint8_t *)pvPayload;
for (size_t i = 0; i < cbPayload; i++)
uChkSum += pbPayload[i];
/* The padding needs no checksum during generation as it is always 0. */
PduFooter.u32ChkSum = (0xffffffff - uChkSum) + 1;
PduFooter.u32Magic = PSP_SERIAL_EXT_2_PSP_PDU_END_MAGIC;
/* Send everything, header first, then payload and any padding and footer last. */
int rc = pThis->pProvIf->pfnCtxWrite(pThis->hProvCtx, &PduHdr, sizeof(PduHdr));
if (!rc && pvPayload && cbPayload)
rc = pThis->pProvIf->pfnCtxWrite(pThis->hProvCtx, pvPayload, cbPayload);
if (!rc && cbPad)
rc = pThis->pProvIf->pfnCtxWrite(pThis->hProvCtx, &abPad[0], cbPad);
if (!rc)
rc = pThis->pProvIf->pfnCtxWrite(pThis->hProvCtx, &PduFooter, sizeof(PduFooter));
return rc;
}
/**
* Sends the given request with payload and waits for the appropriate response returning the
* payload data in the given buffer.
*
* @returns Status code.
* @param pThis The serial stub instance data.
* @param idCcd The CCD ID the PDU is designated for.
* @param enmReq The request to issue.
* @param enmResp The expected response.
* @param pvReqPayload The request payload data.
* @param cbReqPayload Size of the request payload data in bytes.
* @param pvResp Where to store the response data on success.
* @param cbResp Size of the response buffer.
* @param cMillies Timeout in milliseconds.
*/
static int pspStubPduCtxReqResp(PPSPSTUBPDUCTXINT pThis, uint32_t idCcd, PSPSERIALPDURRNID enmReq,
PSPSERIALPDURRNID enmResp,
const void *pvReqPayload, size_t cbReqPayload, void *pvResp, size_t cbResp,
uint32_t cMillies)
{
int rc = pspStubPduCtxSend(pThis, idCcd, enmReq, pvReqPayload, cbReqPayload);
if (!rc)
{
PCPSPSERIALPDUHDR pPdu = NULL;
void *pvPduResp = NULL;
size_t cbPduResp = 0;
rc = pspStubPduCtxRecvId(pThis, enmResp, &pPdu, &pvPduResp, &cbPduResp, cMillies);
if (!rc)
{
pThis->rcReqLast = pPdu->u.Fields.rcReq;
if (pPdu->u.Fields.rcReq == STS_INF_SUCCESS)
{
if (cbPduResp == cbResp)
{
if (cbPduResp)
memcpy(pvResp, pvPduResp, cbPduResp);
}
else
rc = STS_ERR_PSP_PROXY_REQ_RESP_PAYLOAD_SZ_MISMATCH;
}
else
rc = STS_ERR_PSP_PROXY_REQ_COMPLETED_WITH_ERROR;
}
}
return rc;
}
/**
* Wrapper to send a write request consisting of two consecutive payload parts but
* doesn't has any payload data in the response.
*
* @returns Status code.
* @param pThis The serial stub instance data.
* @param idCcd The CCD ID the PDU is designated for.
* @param enmReq The request to issue.
* @param enmResp The expected response.
* @param pvReqPayload1 Stage 1 request payload data.
* @param cbReqPayload1 Size of the stage 1 request payload data in bytes.
* @param pvReqPayload2 Stage 2 request payload data.
* @param cbReqPayload2 Size of the stage 2 request payload data in bytes.
* @param cMillies Timeout in milliseconds.
*/
static int pspStubPduCtxReqRespWr(PPSPSTUBPDUCTXINT pThis, uint32_t idCcd, PSPSERIALPDURRNID enmReq,
PSPSERIALPDURRNID enmResp,
const void *pvReqPayload1, size_t cbReqPayload1,
const void *pvReqPayload2, size_t cbReqPayload2,
uint32_t cMillies)
{
int rc = 0;
/* Quick and dirty way, allocate buffer and merge the payload buffers. */
void *pvTmp = malloc(cbReqPayload1 + cbReqPayload2);
if (pvTmp)
{
memcpy(pvTmp, pvReqPayload1, cbReqPayload1);
memcpy((uint8_t *)pvTmp + cbReqPayload1, pvReqPayload2, cbReqPayload2);
rc = pspStubPduCtxReqResp(pThis, idCcd, enmReq, enmResp, pvTmp, cbReqPayload1 + cbReqPayload2,
NULL /*pvRespPayload*/, 0 /*cbRespPayload*/, cMillies);
free(pvTmp);
}
else
rc = -1;
return rc;
}
int pspStubPduCtxCreate(PPSPSTUBPDUCTX phPduCtx, PCPSPPROXYPROV pProvIf, PSPPROXYPROVCTX hProvCtx,
PCPSPPROXYIOIF pProxyIoIf, PSPPROXYCTX hProxyCtx, void *pvUser)
{
int rc = 0;
PPSPSTUBPDUCTXINT pThis = (PPSPSTUBPDUCTXINT)calloc(1, sizeof(*pThis));
if (pThis)
{
pThis->pProvIf = pProvIf;
pThis->hProvCtx = hProvCtx;
pThis->pProxyIoIf = pProxyIoIf;
pThis->hProxyCtx = hProxyCtx;
pThis->pvProxyIoUser = pvUser;
pThis->cBeaconsSeen = 0;
pThis->cCcds = 1; /* To make validation succeed during the initial connect phase. */
pThis->fConnect = false;
pThis->rcReqLast = STS_INF_SUCCESS;
pspStubPduCtxRecvReset(pThis);
*phPduCtx = pThis;
}
else
rc = -1;
return rc;
}
void pspStubPduCtxDestroy(PSPSTUBPDUCTX hPduCtx)
{
PPSPSTUBPDUCTXINT pThis = hPduCtx;
free(pThis);
}
int pspStubPduCtxConnect(PSPSTUBPDUCTX hPduCtx, uint32_t cMillies)
{
PPSPSTUBPDUCTXINT pThis = hPduCtx;
pThis->cchLogMsgAvail = 0;
memset(&pThis->achLogMsg[0], 0, sizeof(pThis->achLogMsg));
/* Wait for a beacon PDU. */
/** @todo Timeout handling. */
PCPSPSERIALPDUHDR pPdu = NULL;
PCPSPSERIALBEACONNOT pBeacon = NULL;
size_t cbBeacon = 0;
int rc = pspStubPduCtxRecvId(pThis, PSPSERIALPDURRNID_NOTIFICATION_BEACON, &pPdu,
(void **)&pBeacon, &cbBeacon, cMillies);
if (!rc)
{
if (cbBeacon == sizeof(PSPSERIALBEACONNOT))
{
/* Remember the beacon count for later when we successfully connected. */
uint32_t cBeaconsSeen = pBeacon->cBeaconsSent;
/* Send connect request. */
rc = pspStubPduCtxSend(pThis, 0 /*idCcd*/, PSPSERIALPDURRNID_REQUEST_CONNECT, NULL /*pvPayload*/, 0 /*cbPayload*/);
if (!rc)
{
PCPSPSERIALCONNECTRESP pConResp = NULL;
size_t cbConResp = 0;
rc = pspStubPduCtxRecvId(pThis, PSPSERIALPDURRNID_RESPONSE_CONNECT, &pPdu,
(void **)&pConResp, &cbConResp, cMillies);
if (!rc)
{
pThis->cbPduMax = pConResp->cbPduMax;
pThis->cbScratch = pConResp->cbScratch;
pThis->PspAddrScratch = pConResp->PspAddrScratch;
pThis->cSysSockets = pConResp->cSysSockets;
pThis->cCcdsPerSocket = pConResp->cCcdsPerSocket;
pThis->cCcds = pThis->cSysSockets * pThis->cCcdsPerSocket;
pThis->fConnect = true;
pThis->cBeaconsSeen = cBeaconsSeen;
pThis->cPduRecvNext = 1;
}
}
}
else
rc = -1;
}
return rc;
}
int pspStubPduCtxQueryInfo(PSPSTUBPDUCTX hPduCtx, uint32_t idCcd, PSPADDR *pPspAddrScratchStart, size_t *pcbScratch)
{
PPSPSTUBPDUCTXINT pThis = hPduCtx;
*pPspAddrScratchStart = pThis->PspAddrScratch;
*pcbScratch = pThis->cbScratch;
return 0;
}
int pspStubPduCtxQueryLastReqRc(PSPSTUBPDUCTX hPduCtx, PSPSTS *pReqRcLast)
{
PPSPSTUBPDUCTXINT pThis = hPduCtx;
*pReqRcLast = pThis->rcReqLast;
return STS_INF_SUCCESS;
}
int pspStubPduCtxPspSmnRead(PSPSTUBPDUCTX hPduCtx, uint32_t idCcd, uint32_t idCcdTgt, SMNADDR uSmnAddr, uint32_t cbVal, void *pvVal)
{
PPSPSTUBPDUCTXINT pThis = hPduCtx;
PSPSERIALSMNMEMXFERREQ Req;
size_t cbPduPayloadMax = pThis->cbPduMax
- sizeof(Req)
- sizeof(PSPSERIALPDUHDR)
- sizeof(PSPSERIALPDUFOOTER);
if (cbVal <= cbPduPayloadMax)
{
/* Fast path without splitting it up into multiple PDUs. */
Req.SmnAddrStart = uSmnAddr;
Req.cbXfer = cbVal;
return pspStubPduCtxReqResp(pThis, idCcd, PSPSERIALPDURRNID_REQUEST_PSP_SMN_READ,
PSPSERIALPDURRNID_RESPONSE_PSP_SMN_READ,
&Req, sizeof(Req), pvVal, cbVal, 10000);
}
/* Slow path. */
uint8_t *pbDst = (uint8_t *)pvVal;
int rc = 0;
while ( cbVal
&& !rc)
{
size_t cbThisRead = MIN(cbVal, cbPduPayloadMax);
Req.SmnAddrStart = uSmnAddr;
Req.cbXfer = cbThisRead;
rc = pspStubPduCtxReqResp(pThis, idCcd, PSPSERIALPDURRNID_REQUEST_PSP_SMN_READ,
PSPSERIALPDURRNID_RESPONSE_PSP_SMN_READ,
&Req, sizeof(Req), pbDst, cbThisRead, 10000);
if (!rc)
{
pbDst += cbThisRead;
uSmnAddr += cbThisRead;
cbVal -= cbThisRead;
}
}
return rc;
}
int pspStubPduCtxPspSmnWrite(PSPSTUBPDUCTX hPduCtx, uint32_t idCcd, uint32_t idCcdTgt, SMNADDR uSmnAddr, uint32_t cbVal, const void *pvVal)
{
PPSPSTUBPDUCTXINT pThis = hPduCtx;
PSPSERIALSMNMEMXFERREQ Req;
size_t cbPduPayloadMax = pThis->cbPduMax
- sizeof(Req)
- sizeof(PSPSERIALPDUHDR)
- sizeof(PSPSERIALPDUFOOTER);
if (cbVal <= cbPduPayloadMax)
{
Req.SmnAddrStart = uSmnAddr;
Req.cbXfer = cbVal;
return pspStubPduCtxReqRespWr(pThis, idCcd, PSPSERIALPDURRNID_REQUEST_PSP_SMN_WRITE,
PSPSERIALPDURRNID_RESPONSE_PSP_SMN_WRITE,
&Req, sizeof(Req), pvVal, cbVal, 10000);
}
/* Slow path. */
const uint8_t *pbSrc = (const uint8_t *)pvVal;
int rc = 0;
while ( cbVal
&& !rc)
{
size_t cbThisWrite = MIN(cbVal, cbPduPayloadMax);
Req.SmnAddrStart = uSmnAddr;
Req.cbXfer = cbThisWrite;
rc = pspStubPduCtxReqRespWr(pThis, idCcd, PSPSERIALPDURRNID_REQUEST_PSP_SMN_WRITE,
PSPSERIALPDURRNID_RESPONSE_PSP_SMN_WRITE,
&Req, sizeof(Req), pbSrc, cbThisWrite, 10000);
if (!rc)
{
pbSrc += cbThisWrite;
uSmnAddr += cbThisWrite;
cbVal -= cbThisWrite;
}
}
return rc;
}
int pspStubPduCtxPspMemRead(PSPSTUBPDUCTX hPduCtx, uint32_t idCcd, PSPADDR uPspAddr, void *pvBuf, uint32_t cbRead)
{
PPSPSTUBPDUCTXINT pThis = hPduCtx;
PSPSERIALPSPMEMXFERREQ Req;
size_t cbPduPayloadMax = pThis->cbPduMax
- sizeof(Req)
- sizeof(PSPSERIALPDUHDR)
- sizeof(PSPSERIALPDUFOOTER);
if (cbRead <= cbPduPayloadMax)
{
Req.PspAddrStart = uPspAddr;
Req.cbXfer = cbRead;
return pspStubPduCtxReqResp(pThis, idCcd, PSPSERIALPDURRNID_REQUEST_PSP_MEM_READ,
PSPSERIALPDURRNID_RESPONSE_PSP_MEM_READ,
&Req, sizeof(Req), pvBuf, cbRead, 10000);
}
/* Slow path. */
uint8_t *pbBuf = (uint8_t *)pvBuf;
int rc = 0;
while ( cbRead
&& !rc)
{
size_t cbThisRead = MIN(cbRead, cbPduPayloadMax);
Req.PspAddrStart = uPspAddr;
Req.cbXfer = cbThisRead;
rc = pspStubPduCtxReqResp(pThis, idCcd, PSPSERIALPDURRNID_REQUEST_PSP_MEM_READ,
PSPSERIALPDURRNID_RESPONSE_PSP_MEM_READ,
&Req, sizeof(Req), pbBuf, cbThisRead, 10000);
if (!rc)
{
pbBuf += cbThisRead;
uPspAddr += cbThisRead;
cbRead -= cbThisRead;
}
}
return rc;
}
int pspStubPduCtxPspMemWrite(PSPSTUBPDUCTX hPduCtx, uint32_t idCcd, PSPADDR uPspAddr, const void *pvBuf, uint32_t cbWrite)
{
PPSPSTUBPDUCTXINT pThis = hPduCtx;
PSPSERIALPSPMEMXFERREQ Req;
size_t cbPduPayloadMax = pThis->cbPduMax
- sizeof(Req)
- sizeof(PSPSERIALPDUHDR)
- sizeof(PSPSERIALPDUFOOTER);
if (cbWrite <= cbPduPayloadMax)
{
Req.PspAddrStart = uPspAddr;
Req.cbXfer = cbWrite;
return pspStubPduCtxReqRespWr(pThis, idCcd, PSPSERIALPDURRNID_REQUEST_PSP_MEM_WRITE,
PSPSERIALPDURRNID_RESPONSE_PSP_MEM_WRITE,
&Req, sizeof(Req), pvBuf, cbWrite, 10000);
}
/* Slow path. */
const uint8_t *pbBuf = (const uint8_t *)pvBuf;
int rc = 0;
while ( cbWrite
&& !rc)
{
size_t cbThisWrite = MIN(cbWrite, cbPduPayloadMax);
Req.PspAddrStart = uPspAddr;
Req.cbXfer = cbThisWrite;
rc = pspStubPduCtxReqRespWr(pThis, idCcd, PSPSERIALPDURRNID_REQUEST_PSP_MEM_WRITE,
PSPSERIALPDURRNID_RESPONSE_PSP_MEM_WRITE,
&Req, sizeof(Req), pbBuf, cbThisWrite, 10000);
if (!rc)
{
pbBuf += cbThisWrite;
uPspAddr += cbThisWrite;
cbWrite -= cbThisWrite;
}
}
return rc;
}
int pspStubPduCtxPspMmioRead(PSPSTUBPDUCTX hPduCtx, uint32_t idCcd, PSPADDR uPspAddr, void *pvVal, uint32_t cbVal)
{
PPSPSTUBPDUCTXINT pThis = hPduCtx;
PSPSERIALPSPMEMXFERREQ Req;
Req.PspAddrStart = uPspAddr;
Req.cbXfer = cbVal;
return pspStubPduCtxReqResp(pThis, idCcd, PSPSERIALPDURRNID_REQUEST_PSP_MMIO_READ,
PSPSERIALPDURRNID_RESPONSE_PSP_MMIO_READ,
&Req, sizeof(Req), pvVal, cbVal, 10000);
}
int pspStubPduCtxPspMmioWrite(PSPSTUBPDUCTX hPduCtx, uint32_t idCcd, PSPADDR uPspAddr, const void *pvVal, uint32_t cbVal)
{
PPSPSTUBPDUCTXINT pThis = hPduCtx;
PSPSERIALPSPMEMXFERREQ Req;
Req.PspAddrStart = uPspAddr;
Req.cbXfer = cbVal;
return pspStubPduCtxReqRespWr(pThis, idCcd, PSPSERIALPDURRNID_REQUEST_PSP_MMIO_WRITE,
PSPSERIALPDURRNID_RESPONSE_PSP_MMIO_WRITE,
&Req, sizeof(Req), pvVal, cbVal, 10000);
}
int pspStubPduCtxPspX86MemRead(PSPSTUBPDUCTX hPduCtx, uint32_t idCcd, X86PADDR PhysX86Addr, void *pvBuf, uint32_t cbRead)
{
PPSPSTUBPDUCTXINT pThis = hPduCtx;
PSPSERIALX86MEMXFERREQ Req;
size_t cbPduPayloadMax = pThis->cbPduMax
- sizeof(Req)
- sizeof(PSPSERIALPDUHDR)
- sizeof(PSPSERIALPDUFOOTER);
if (cbRead <= cbPduPayloadMax)
{
Req.PhysX86Start = PhysX86Addr;
Req.cbXfer = cbRead;
Req.u32Pad0 = 0;
return pspStubPduCtxReqResp(pThis, idCcd, PSPSERIALPDURRNID_REQUEST_PSP_X86_MEM_READ,
PSPSERIALPDURRNID_RESPONSE_PSP_X86_MEM_READ,
&Req, sizeof(Req), pvBuf, cbRead, 10000);
}
/* Slow path. */
uint8_t *pbBuf = (uint8_t *)pvBuf;
int rc = 0;
while ( cbRead
&& !rc)
{
size_t cbThisRead = MIN(cbRead, cbPduPayloadMax);
Req.PhysX86Start = PhysX86Addr;
Req.cbXfer = cbThisRead;
rc = pspStubPduCtxReqResp(pThis, idCcd, PSPSERIALPDURRNID_REQUEST_PSP_X86_MEM_READ,
PSPSERIALPDURRNID_RESPONSE_PSP_X86_MEM_READ,
&Req, sizeof(Req), pbBuf, cbThisRead, 10000);
if (!rc)
{
pbBuf += cbThisRead;
PhysX86Addr += cbThisRead;
cbRead -= cbThisRead;
}
}
return rc;
}
int pspStubPduCtxPspX86MemWrite(PSPSTUBPDUCTX hPduCtx, uint32_t idCcd, X86PADDR PhysX86Addr, const void *pvBuf, uint32_t cbWrite)
{
PPSPSTUBPDUCTXINT pThis = hPduCtx;
PSPSERIALX86MEMXFERREQ Req;
size_t cbPduPayloadMax = pThis->cbPduMax
- sizeof(Req)
- sizeof(PSPSERIALPDUHDR)
- sizeof(PSPSERIALPDUFOOTER);
if (cbWrite <= cbPduPayloadMax)
{
Req.PhysX86Start = PhysX86Addr;
Req.cbXfer = cbWrite;