-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnode.c
More file actions
961 lines (866 loc) · 35 KB
/
node.c
File metadata and controls
961 lines (866 loc) · 35 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
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
#include "node.h"
#include <assert.h>
enum rmap_status rmap_node_initialize(
struct rmap_node *node,
void *const custom_context,
const struct rmap_node_callbacks *const callbacks,
const struct rmap_node_initialize_flags flags)
{
if (!flags.is_target && !flags.is_initiator) {
return RMAP_NODE_NO_TARGET_OR_INITIATOR;
}
node->custom_context = custom_context;
node->callbacks = *callbacks;
node->is_target = flags.is_target;
node->is_initiator = flags.is_initiator;
node->is_reply_for_unused_packet_type_enabled =
flags.is_reply_for_unused_packet_type_enabled;
return RMAP_OK;
}
/** Create and send an error reply to a given command header.
*
* Allocate space for, create, and send an error reply based on a given
* command header and status code.
*
* Read and RMW replies (which normally contain data) will have the data length
* set to 0 and will have a data field with 0 bytes, regardless of the data
* length in the command header.
*
* @pre @p command must have been verified to be a valid RMAP command
* header.
* @pre @p error must be a valid non-success RMAP status field code.
*
* @param[in,out] node Node context object.
* @param[in,out] transaction_custom_context Transaction custom context object.
* @param[in] command Command header to create reply for.
* @param error RMAP status field code to use in reply.
*
* @retval RMAP_NODE_ALLOCATION_FAILURE Failed to allocate space for reply.
* @retval RMAP_NODE_SEND_REPLY_FAILURE Failed to send reply.
* @retval RMAP_OK Reply packet sent successfully.
*/
static enum rmap_status send_error_reply(
struct rmap_node *node,
void *const transaction_custom_context,
const void *const command,
const enum rmap_status_field_code error)
{
size_t header_offset;
size_t reply_size;
assert(error != RMAP_STATUS_FIELD_CODE_SUCCESS);
const size_t reply_size_max =
RMAP_REPLY_ADDRESS_LENGTH_MAX + RMAP_HEADER_SIZE_MAX + 1;
uint8_t *const reply_buf = node->callbacks.target.allocate(
node,
transaction_custom_context,
reply_size_max);
if (!reply_buf) {
return RMAP_NODE_ALLOCATION_FAILURE;
}
const enum rmap_status create_reply_status =
rmap_create_success_reply_from_command(
reply_buf,
&header_offset,
reply_size_max,
command);
assert(create_reply_status == RMAP_OK);
(void)create_reply_status;
uint8_t *const reply_header = reply_buf + header_offset;
reply_size = header_offset + rmap_calculate_header_size(reply_header);
rmap_set_status(reply_header, error);
if (!rmap_is_write(command)) {
rmap_set_data_length(reply_header, 0);
uint8_t *const data =
reply_header + rmap_calculate_header_size(reply_header);
data[0] = rmap_crc_calculate(data, 0);
reply_size += 1;
}
rmap_calculate_and_set_header_crc(reply_buf + header_offset);
const enum rmap_status send_status = node->callbacks.target.send_reply(
node,
transaction_custom_context,
reply_buf,
reply_size);
switch (send_status) {
case RMAP_NODE_SEND_REPLY_FAILURE:
return send_status;
default:
assert(send_status == RMAP_OK);
break;
}
return RMAP_OK;
}
/** Handle incoming write command packet to node.
*
* Verify the data field and forward the request for authorization and
* processing by the user, send reply if relevant.
*
* @pre @p packet must have been verified to contain a valid RMAP write command
* header.
* @pre @p size must indicate the exact number of bytes in the write command
* packet.
*
* @param[in,out] node Node context object.
* @param[in,out] transaction_custom_context Transaction custom context object.
* @param[in] packet Incoming packet.
* @param size Number of bytes in incoming packet in @p packet (excluding the
* EOP or EEP).
* @param has_eep_termination Flag indicating if the incoming packet was
* terminated with an EEP.
*
* @retval RMAP_NODE_ALLOCATION_FAILURE Incoming packet and intended reply
* discarded due to allocation failure.
* @retval RMAP_NODE_SEND_REPLY_FAILURE Incoming packet and intended reply
* discarded due to reply sending failure.
* @retval RMAP_INSUFFICIENT_DATA Incoming packet rejected due to being smaller
* than indicated by the data length. An error reply has been sent if
* applicable.
* @retval RMAP_NODE_INSUFFICIENT_DATA_WITH_EEP Incoming packet (terminated
* with an EEP) rejected due to being smaller than indicated by the
* data length. An error reply has been sent if applicable.
* @retval RMAP_TOO_MUCH_DATA Incoming packet rejected due to being larger than
* indicated by the data length. An error reply has been sent if
* applicable.
* @retval RMAP_INVALID_DATA_CRC Incoming packet rejected due to the data CRC
* indicating errors in the data. An error reply has been sent if
* applicable.
* @retval RMAP_NODE_INVALID_KEY Incoming packet rejected due to its key not
* being authorized by the request callback. An error reply has been
* sent if applicable.
* @retval RMAP_NODE_INVALID_TARGET_LOGICAL_ADDRESS Incoming packet rejected
* due to its target logical address not being authorized by
* the request callback. An error reply has been sent if applicable.
* @retval RMAP_NODE_COMMAND_NOT_IMPLEMENTED_OR_NOT_AUTHORIZED Incoming packet
* rejected due to not being authorized for "any other reason" by the
* request callback. An error reply has been sent if applicable.
* @retval RMAP_NODE_MEMORY_ACCESS_ERROR Incoming packet processing aborted due
* to write memory access error. An error reply has been sent if
* applicable.
* @retval RMAP_OK Incoming packet processed successfully. A reply has been
* sent if applicable.
*/
static enum rmap_status handle_write_command(
struct rmap_node *node,
void *const transaction_custom_context,
const uint8_t *const packet,
const size_t size,
const bool has_eep_termination)
{
enum rmap_status_field_code status_field_code;
enum rmap_status verify_status;
size_t reply_header_offset;
enum rmap_status write_status;
/* Since the whole packet is available, verification is always done before
* write regardless.
*
* TODO: Should the write still be done before verification in order to
* match the standard RMAP behaviour?
*/
status_field_code = RMAP_STATUS_FIELD_CODE_SUCCESS;
verify_status = rmap_verify_data(packet, size);
switch (verify_status) {
case RMAP_INSUFFICIENT_DATA:
status_field_code = RMAP_STATUS_FIELD_CODE_EARLY_EOP;
if (has_eep_termination) {
status_field_code = RMAP_STATUS_FIELD_CODE_EEP;
verify_status = RMAP_NODE_INSUFFICIENT_DATA_WITH_EEP;
}
break;
case RMAP_TOO_MUCH_DATA:
status_field_code = RMAP_STATUS_FIELD_CODE_TOO_MUCH_DATA;
break;
case RMAP_INVALID_DATA_CRC:
status_field_code = RMAP_STATUS_FIELD_CODE_INVALID_DATA_CRC;
break;
default:
assert(verify_status == RMAP_OK);
break;
}
if (verify_status != RMAP_OK) {
if (rmap_is_with_reply(packet)) {
const enum rmap_status send_status = send_error_reply(
node,
transaction_custom_context,
packet,
status_field_code);
switch (send_status) {
case RMAP_NODE_ALLOCATION_FAILURE:
case RMAP_NODE_SEND_REPLY_FAILURE:
return send_status;
default:
assert(send_status == RMAP_OK);
break;
}
}
return verify_status;
}
const struct rmap_node_target_request write_request = {
.target_logical_address = rmap_get_target_logical_address(packet),
.instruction = rmap_get_instruction(packet),
.key = rmap_get_key(packet),
.initiator_logical_address = rmap_get_initiator_logical_address(packet),
.transaction_identifier = rmap_get_transaction_identifier(packet),
.extended_address = rmap_get_extended_address(packet),
.address = rmap_get_address(packet),
.data_length = rmap_get_data_length(packet)};
status_field_code = node->callbacks.target.write_request(
node,
transaction_custom_context,
&write_request,
packet + rmap_calculate_header_size(packet));
write_status = RMAP_OK;
switch (status_field_code) {
case RMAP_STATUS_FIELD_CODE_INVALID_KEY:
write_status = RMAP_NODE_INVALID_KEY;
break;
case RMAP_STATUS_FIELD_CODE_INVALID_TARGET_LOGICAL_ADDRESS:
write_status = RMAP_NODE_INVALID_TARGET_LOGICAL_ADDRESS;
break;
case RMAP_STATUS_FIELD_CODE_COMMAND_NOT_IMPLEMENTED_OR_NOT_AUTHORIZED:
write_status = RMAP_NODE_COMMAND_NOT_IMPLEMENTED_OR_NOT_AUTHORIZED;
break;
case RMAP_STATUS_FIELD_CODE_GENERAL_ERROR_CODE:
write_status = RMAP_NODE_MEMORY_ACCESS_ERROR;
break;
default:
assert(status_field_code == RMAP_STATUS_FIELD_CODE_SUCCESS);
break;
}
if (status_field_code != RMAP_STATUS_FIELD_CODE_SUCCESS) {
if (rmap_is_with_reply(packet)) {
const enum rmap_status send_status = send_error_reply(
node,
transaction_custom_context,
packet,
status_field_code);
switch (send_status) {
case RMAP_NODE_ALLOCATION_FAILURE:
case RMAP_NODE_SEND_REPLY_FAILURE:
return send_status;
default:
assert(send_status == RMAP_OK);
break;
}
}
return write_status;
}
const size_t reply_maximum_size =
RMAP_REPLY_ADDRESS_LENGTH_MAX + RMAP_WRITE_REPLY_HEADER_STATIC_SIZE;
uint8_t *const reply_buf = node->callbacks.target.allocate(
node,
transaction_custom_context,
reply_maximum_size);
if (!reply_buf) {
return RMAP_NODE_ALLOCATION_FAILURE;
}
/* TODO: Might make sense to avoid calculating header CRC here and then
* recalculate it later?
*/
const enum rmap_status create_reply_status =
rmap_create_success_reply_from_command(
reply_buf,
&reply_header_offset,
reply_maximum_size,
packet);
assert(create_reply_status == RMAP_OK);
(void)create_reply_status;
const size_t reply_size = reply_header_offset +
rmap_calculate_header_size(reply_buf + reply_header_offset);
const enum rmap_status send_status = node->callbacks.target.send_reply(
node,
transaction_custom_context,
reply_buf,
reply_size);
switch (send_status) {
case RMAP_NODE_SEND_REPLY_FAILURE:
return send_status;
default:
assert(send_status == RMAP_OK);
break;
}
return RMAP_OK;
}
/** Handle incoming read command packet to node.
*
* Forward the request for authorization and processing by the user, send
* reply.
*
* @pre @p packet must have been verified to contain a valid RMAP read command
* header.
*
* @param[in,out] node Node context object.
* @param[in,out] transaction_custom_context Transaction custom context object.
* @param[in] packet Incoming packet.
*
* @retval RMAP_NODE_ALLOCATION_FAILURE Incoming packet and intended reply
* discarded due to allocation failure.
* @retval RMAP_NODE_SEND_REPLY_FAILURE Incoming packet and intended reply
* discarded due to reply sending failure.
* @retval RMAP_NODE_INVALID_KEY Incoming packet rejected due to its key not
* being authorized by the request callback. An error reply has been
* sent.
* @retval RMAP_NODE_INVALID_TARGET_LOGICAL_ADDRESS Incoming packet rejected
* due to its target logical address not being authorized by
* the request callback. An error reply has been sent.
* @retval RMAP_NODE_COMMAND_NOT_IMPLEMENTED_OR_NOT_AUTHORIZED Incoming packet
* rejected due to not being authorized for "any other reason" by the
* request callback. An error reply has been sent.
* @retval RMAP_NODE_MEMORY_ACCESS_ERROR Incoming packet processing aborted due
* to read memory access error. An error reply has been sent.
* @retval RMAP_OK Incoming packet processed successfully. A reply has been
* sent if applicable.
*/
static enum rmap_status handle_read_command(
struct rmap_node *node,
void *const transaction_custom_context,
const uint8_t *const packet)
{
uint8_t reply_address[RMAP_REPLY_ADDRESS_LENGTH_MAX];
size_t reply_address_size;
uint8_t *reply_data;
void *reply_data_void;
size_t reply_header_offset;
size_t reply_data_size;
enum rmap_status read_status;
const size_t reply_maximum_size = RMAP_REPLY_ADDRESS_LENGTH_MAX +
RMAP_COMMAND_HEADER_STATIC_SIZE + rmap_get_data_length(packet) + 1;
uint8_t *const reply_allocation = node->callbacks.target.allocate(
node,
transaction_custom_context,
reply_maximum_size);
if (!reply_allocation) {
return RMAP_NODE_ALLOCATION_FAILURE;
}
const enum rmap_status reply_address_status = rmap_get_reply_address(
reply_address,
&reply_address_size,
sizeof(reply_address),
packet);
assert(reply_address_status == RMAP_OK);
(void)reply_address_status;
const size_t reply_data_offset =
reply_address_size + RMAP_READ_REPLY_HEADER_STATIC_SIZE;
reply_data = reply_allocation + reply_data_offset;
reply_data_void = reply_data;
const struct rmap_node_target_request read_request = {
.target_logical_address = rmap_get_target_logical_address(packet),
.instruction = rmap_get_instruction(packet),
.key = rmap_get_key(packet),
.initiator_logical_address = rmap_get_initiator_logical_address(packet),
.transaction_identifier = rmap_get_transaction_identifier(packet),
.extended_address = rmap_get_extended_address(packet),
.address = rmap_get_address(packet),
.data_length = rmap_get_data_length(packet)};
const enum rmap_status_field_code status_field_code =
node->callbacks.target.read_request(
node,
transaction_custom_context,
&reply_data_void,
&reply_data_size,
&read_request);
read_status = RMAP_OK;
switch (status_field_code) {
case RMAP_STATUS_FIELD_CODE_INVALID_KEY:
read_status = RMAP_NODE_INVALID_KEY;
break;
case RMAP_STATUS_FIELD_CODE_INVALID_TARGET_LOGICAL_ADDRESS:
read_status = RMAP_NODE_INVALID_TARGET_LOGICAL_ADDRESS;
break;
case RMAP_STATUS_FIELD_CODE_COMMAND_NOT_IMPLEMENTED_OR_NOT_AUTHORIZED:
read_status = RMAP_NODE_COMMAND_NOT_IMPLEMENTED_OR_NOT_AUTHORIZED;
break;
default:
assert(status_field_code == RMAP_STATUS_FIELD_CODE_SUCCESS);
if (reply_data_size != read_request.data_length) {
read_status = RMAP_NODE_MEMORY_ACCESS_ERROR;
}
break;
}
/* Data offset may have been moved forwards by read request callback. */
reply_data = reply_data_void;
uint8_t *const reply_buf = reply_data - reply_data_offset;
/* TODO: Might make sense to avoid calculating header CRC here and then
* recalculate it later?
*/
const enum rmap_status create_reply_status =
rmap_create_success_reply_from_command(
reply_buf,
&reply_header_offset,
reply_data_offset,
packet);
assert(create_reply_status == RMAP_OK);
(void)create_reply_status;
assert(
reply_header_offset +
rmap_calculate_header_size(reply_buf + reply_header_offset) ==
reply_data_offset);
size_t reply_size;
if (status_field_code == RMAP_STATUS_FIELD_CODE_SUCCESS) {
reply_buf[reply_data_offset + reply_data_size] =
rmap_crc_calculate(reply_buf + reply_data_offset, reply_data_size);
reply_size = reply_data_offset + reply_data_size + 1;
} else {
rmap_set_status(reply_buf + reply_header_offset, status_field_code);
rmap_set_data_length(reply_buf + reply_header_offset, 0);
rmap_calculate_and_set_header_crc(reply_buf + reply_header_offset);
reply_buf[reply_data_offset] =
rmap_crc_calculate(reply_buf + reply_data_offset, 0);
reply_size = reply_data_offset + 1;
}
const enum rmap_status send_status = node->callbacks.target.send_reply(
node,
transaction_custom_context,
reply_buf,
reply_size);
switch (send_status) {
case RMAP_NODE_SEND_REPLY_FAILURE:
return send_status;
default:
assert(send_status == RMAP_OK);
break;
}
return read_status;
}
/** Handle incoming RMW command packet to node.
*
* Verify the data field and forward the request for authorization and
* processing by the user, send reply if relevant.
*
* @pre @p packet must have been verified to contain a valid RMAP RMW command
* header.
* @pre @p size must indicate the exact number of bytes in the RMW command
* packet.
*
* @param[in,out] node Node context object.
* @param[in,out] transaction_custom_context Transaction custom context object.
* @param[in] packet Incoming packet.
* @param size Number of bytes in incoming packet in @p packet (excluding the
* EOP or EEP).
* @param has_eep_termination Flag indicating if the incoming packet was
* terminated with an EEP.
*
* @retval RMAP_NODE_ALLOCATION_FAILURE Incoming packet and intended reply
* discarded due to allocation failure.
* @retval RMAP_NODE_SEND_REPLY_FAILURE Incoming packet and intended reply
* discarded due to reply sending failure.
* @retval RMAP_RMW_DATA_LENGTH_ERROR Incoming packet rejected due to data
* length field value being invalid for a RMW command. An error reply
* has been sent.
* @retval RMAP_INSUFFICIENT_DATA Incoming packet rejected due to being smaller
* than indicated by the data length. An error reply has been sent.
* @retval RMAP_NODE_INSUFFICIENT_DATA_WITH_EEP Incoming packet (terminated
* with an EEP) rejected due to being smaller than indicated by the
* data length. An error reply has been sent.
* @retval RMAP_TOO_MUCH_DATA Incoming packet rejected due to being larger than
* indicated by the data length. An error reply has been sent.
* @retval RMAP_INVALID_DATA_CRC Incoming packet rejected due to the data CRC
* indicating errors in the data. An error reply has been sent.
* @retval RMAP_NODE_INVALID_KEY Incoming packet rejected due to its key not
* being authorized by the request callback. An error reply has been
* sent.
* @retval RMAP_NODE_INVALID_TARGET_LOGICAL_ADDRESS Incoming packet rejected
* due to its target logical address not being authorized by
* the request callback. An error reply has been sent.
* @retval RMAP_NODE_COMMAND_NOT_IMPLEMENTED_OR_NOT_AUTHORIZED Incoming packet
* rejected due to not being authorized for "any other reason" by the
* request callback. An error reply has been sent.
* @retval RMAP_NODE_MEMORY_ACCESS_ERROR Incoming packet processing aborted due
* to read or write memory access error. An error reply has been sent.
* @retval RMAP_OK Incoming packet processed successfully. A reply has been
* sent if applicable.
*/
static enum rmap_status handle_rmw_command(
struct rmap_node *node,
void *const transaction_custom_context,
const uint8_t *const packet,
const size_t size,
const bool has_eep_termination)
{
enum rmap_status_field_code status_field_code;
enum rmap_status verify_status;
uint8_t reply_address[RMAP_REPLY_ADDRESS_LENGTH_MAX];
size_t reply_address_size;
uint8_t *reply_data;
void *reply_data_void;
size_t reply_header_offset;
size_t reply_data_size;
enum rmap_status rmw_status;
status_field_code = RMAP_STATUS_FIELD_CODE_SUCCESS;
verify_status = rmap_verify_data(packet, size);
switch (verify_status) {
case RMAP_RMW_DATA_LENGTH_ERROR:
status_field_code = RMAP_STATUS_FIELD_CODE_RMW_DATA_LENGTH_ERROR;
break;
case RMAP_INSUFFICIENT_DATA:
status_field_code = RMAP_STATUS_FIELD_CODE_EARLY_EOP;
if (has_eep_termination) {
status_field_code = RMAP_STATUS_FIELD_CODE_EEP;
verify_status = RMAP_NODE_INSUFFICIENT_DATA_WITH_EEP;
}
break;
case RMAP_TOO_MUCH_DATA:
status_field_code = RMAP_STATUS_FIELD_CODE_TOO_MUCH_DATA;
break;
case RMAP_INVALID_DATA_CRC:
status_field_code = RMAP_STATUS_FIELD_CODE_INVALID_DATA_CRC;
break;
default:
assert(verify_status == RMAP_OK);
break;
}
if (verify_status != RMAP_OK) {
const enum rmap_status send_status = send_error_reply(
node,
transaction_custom_context,
packet,
status_field_code);
switch (send_status) {
case RMAP_NODE_ALLOCATION_FAILURE:
case RMAP_NODE_SEND_REPLY_FAILURE:
return send_status;
default:
assert(send_status == RMAP_OK);
break;
}
return verify_status;
}
const size_t reply_maximum_size = RMAP_REPLY_ADDRESS_LENGTH_MAX +
RMAP_COMMAND_HEADER_STATIC_SIZE + rmap_get_data_length(packet) / 2 + 1;
uint8_t *const reply_allocation = node->callbacks.target.allocate(
node,
transaction_custom_context,
reply_maximum_size);
if (!reply_allocation) {
return RMAP_NODE_ALLOCATION_FAILURE;
}
const enum rmap_status reply_address_status = rmap_get_reply_address(
reply_address,
&reply_address_size,
sizeof(reply_address),
packet);
assert(reply_address_status == RMAP_OK);
(void)reply_address_status;
const size_t reply_data_offset =
reply_address_size + RMAP_READ_REPLY_HEADER_STATIC_SIZE;
reply_data = reply_allocation + reply_data_offset;
reply_data_void = reply_data;
const struct rmap_node_target_request rmw_request = {
.target_logical_address = rmap_get_target_logical_address(packet),
.instruction = rmap_get_instruction(packet),
.key = rmap_get_key(packet),
.initiator_logical_address = rmap_get_initiator_logical_address(packet),
.transaction_identifier = rmap_get_transaction_identifier(packet),
.extended_address = rmap_get_extended_address(packet),
.address = rmap_get_address(packet),
.data_length = rmap_get_data_length(packet)};
rmw_status = RMAP_OK;
status_field_code = node->callbacks.target.rmw_request(
node,
transaction_custom_context,
&reply_data_void,
&reply_data_size,
&rmw_request,
packet + rmap_calculate_header_size(packet));
/* Data offset may have been moved forwards by RMW request callback. */
reply_data = reply_data_void;
uint8_t *const reply_buf = reply_data - reply_data_offset;
/* TODO: Might make sense to avoid calculating header CRC here and then
* recalculate it later?
*/
const enum rmap_status create_reply_status =
rmap_create_success_reply_from_command(
reply_buf,
&reply_header_offset,
reply_data_offset,
packet);
assert(create_reply_status == RMAP_OK);
(void)create_reply_status;
assert(
reply_header_offset +
rmap_calculate_header_size(reply_buf + reply_header_offset) ==
reply_data_offset);
switch (status_field_code) {
case RMAP_STATUS_FIELD_CODE_INVALID_KEY:
rmw_status = RMAP_NODE_INVALID_KEY;
reply_data_size = 0;
rmap_set_data_length(reply_buf + reply_header_offset, 0);
break;
case RMAP_STATUS_FIELD_CODE_INVALID_TARGET_LOGICAL_ADDRESS:
rmw_status = RMAP_NODE_INVALID_TARGET_LOGICAL_ADDRESS;
reply_data_size = 0;
rmap_set_data_length(reply_buf + reply_header_offset, 0);
break;
case RMAP_STATUS_FIELD_CODE_COMMAND_NOT_IMPLEMENTED_OR_NOT_AUTHORIZED:
rmw_status = RMAP_NODE_COMMAND_NOT_IMPLEMENTED_OR_NOT_AUTHORIZED;
reply_data_size = 0;
rmap_set_data_length(reply_buf + reply_header_offset, 0);
break;
case RMAP_STATUS_FIELD_CODE_GENERAL_ERROR_CODE:
rmw_status = RMAP_NODE_MEMORY_ACCESS_ERROR;
break;
default:
assert(status_field_code == RMAP_STATUS_FIELD_CODE_SUCCESS);
if (reply_data_size != rmw_request.data_length / 2) {
rmw_status = RMAP_NODE_MEMORY_ACCESS_ERROR;
}
break;
}
rmap_set_status(reply_buf + reply_header_offset, status_field_code);
rmap_calculate_and_set_header_crc(reply_buf + reply_header_offset);
reply_buf[reply_data_offset + reply_data_size] =
rmap_crc_calculate(reply_buf + reply_data_offset, reply_data_size);
const size_t reply_size = reply_data_offset + reply_data_size + 1;
const enum rmap_status send_status = node->callbacks.target.send_reply(
node,
transaction_custom_context,
reply_buf,
reply_size);
switch (send_status) {
case RMAP_NODE_SEND_REPLY_FAILURE:
return send_status;
default:
assert(send_status == RMAP_OK);
break;
}
return rmw_status;
}
/** Handle incoming command packet to node.
*
* Verify header, check if the node accepts commands, verify data field if
* applicable, and forward the request for authorization and processing by the
* user, send reply if relevant.
*
* @pre @p packet must have been verified to contain a complete RMAP command
* header via rmap_verify_header_integrity() and rmap_is_command().
* @pre @p size must indicate the exact number of bytes in the command packet.
*
* @param[in,out] node Node context object.
* @param[in,out] transaction_custom_context Transaction custom context object.
* @param[in] packet Incoming packet.
* @param size Number of bytes in incoming packet in @p packet (excluding the
* EOP or EEP).
* @param has_eep_termination Flag indicating if the incoming packet was
* terminated with an EEP.
*
* @retval RMAP_NODE_COMMAND_HEADER_FOLLOWED_BY_EEP Incoming command packet
* discarded due to valid header being immediately followed by EEP.
* @retval RMAP_NODE_COMMAND_RECEIVED_BY_INITIATOR Incoming command packet
* discarded due to node being configured to reject incoming commands.
* @retval RMAP_NODE_ALLOCATION_FAILURE Incoming packet and intended reply
* discarded due to allocation failure.
* @retval RMAP_NODE_SEND_REPLY_FAILURE Incoming packet and intended reply
* discarded due to reply sending failure.
* @retval RMAP_UNUSED_PACKET_TYPE Incoming packet rejected due to the packet
* type field having the reserved bit set. An error reply may have been
* sent, if applicable, depending on the configuration of the node.
* @retval RMAP_UNUSED_COMMAND_CODE Incoming packet rejected due to the command
* field containing a reserved command code.
* @retval RMAP_INSUFFICIENT_DATA Incoming write or RMW command packet rejected
* due to being smaller than indicated by the data length. An error
* reply has been sent if applicable.
* @retval RMAP_NODE_INSUFFICIENT_DATA_WITH_EEP Incoming write or RMW command
* packet (terminated with an EEP) rejected due to being smaller than
* indicated by the data length. An error reply has been sent if
* applicable.
* @retval RMAP_TOO_MUCH_DATA Incoming write or RMW command packet rejected due
* to being larger than indicated by the data length. An error reply
* has been sent if applicable.
* @retval RMAP_INVALID_DATA_CRC Incoming write or RMW command packet rejected
* due to the data CRC indicating errors in the data. An error reply
* has been sent if applicable.
* @retval RMAP_RMW_DATA_LENGTH_ERROR Incoming RMW command packet rejected due
* to data length field value being invalid for a RMW command. An error
* reply has been sent.
* @retval RMAP_NODE_INVALID_KEY Incoming command packet rejected due to its
* key not being authorized by the request callback. An error reply has
* been sent if applicable.
* @retval RMAP_NODE_INVALID_TARGET_LOGICAL_ADDRESS Incoming command packet
* rejected due to its target logical address not being authorized by
* the request callback. An error reply has been sent if applicable.
* @retval RMAP_NODE_COMMAND_NOT_IMPLEMENTED_OR_NOT_AUTHORIZED Incoming command
* packet rejected due to not being authorized for "any other reason"
* by the request callback. An error reply has been sent if applicable.
* @retval RMAP_NODE_MEMORY_ACCESS_ERROR Incoming command packet processing
* aborted due to write or read memory access error. An error reply has
* been sent if applicable.
* @retval RMAP_OK Incoming packet processed successfully. A reply has been
* sent if applicable.
*/
static enum rmap_status handle_command(
struct rmap_node *node,
void *const transaction_custom_context,
const void *const packet,
const size_t size,
const bool has_eep_termination)
{
if (has_eep_termination && rmap_calculate_header_size(packet) == size) {
return RMAP_NODE_COMMAND_HEADER_FOLLOWED_BY_EEP;
}
if (!node->is_target) {
return RMAP_NODE_COMMAND_RECEIVED_BY_INITIATOR;
}
/* Node is target. */
const enum rmap_status verify_status =
rmap_verify_header_instruction(packet);
switch (verify_status) {
case RMAP_UNUSED_PACKET_TYPE:
if (!node->is_reply_for_unused_packet_type_enabled) {
return verify_status;
}
/* Fall through. */
case RMAP_UNUSED_COMMAND_CODE:
if (rmap_is_with_reply(packet)) {
const enum rmap_status send_status = send_error_reply(
node,
transaction_custom_context,
packet,
RMAP_STATUS_FIELD_CODE_UNUSED_PACKET_TYPE_OR_COMMAND_CODE);
switch (send_status) {
case RMAP_NODE_ALLOCATION_FAILURE:
case RMAP_NODE_SEND_REPLY_FAILURE:
return send_status;
default:
assert(send_status == RMAP_OK);
break;
}
}
return verify_status;
default:
assert(verify_status == RMAP_OK);
break;
}
if (rmap_is_write(packet)) {
return handle_write_command(
node,
transaction_custom_context,
packet,
size,
has_eep_termination);
}
if (rmap_is_rmw(packet)) {
return handle_rmw_command(
node,
transaction_custom_context,
packet,
size,
has_eep_termination);
}
return handle_read_command(node, transaction_custom_context, packet);
}
/** Handle incoming reply packet to node.
*
* Verify header, check if the node accepts replies, verify data field if
* applicable, and forward the reply information to the user.
*
* @pre @p packet must have been verified to contain a complete RMAP reply
* header via rmap_verify_header_integrity() and rmap_is_reply().
* @pre @p size must indicate the exact number of bytes in the reply packet.
*
* @param[in,out] node Node context object.
* @param[in,out] transaction_custom_context Transaction custom context object.
* @param[in] packet Incoming packet.
* @param size Number of bytes in incoming packet in @p packet.
*
* @retval RMAP_NODE_PACKET_ERROR Incoming packet rejected due to one of:
* * The packet type field having the reserved bit set.
* * The command field containing a reserved command code.
* * The command field not having the reply bit set.
* @retval RMAP_NODE_INVALID_REPLY Incoming packet rejected due to one of:
* * The packet being a RMW reply with a data length field value that
* is invalid for a RMW reply.
* * The packet being smaller than indicated by the data length field.
* * The packet being larger than indicated by the data length field.
* * The data CRC indicating errors in the data.
* @retval RMAP_OK Incoming packet processed successfully.
*/
static enum rmap_status handle_reply(
struct rmap_node *node,
void *const transaction_custom_context,
const uint8_t *const packet,
const size_t size)
{
enum rmap_status status;
if (!node->is_initiator) {
return RMAP_NODE_REPLY_RECEIVED_BY_TARGET;
}
/* Node is initiator. */
status = rmap_verify_header_instruction(packet);
switch (status) {
case RMAP_UNUSED_PACKET_TYPE:
case RMAP_NO_REPLY:
case RMAP_UNUSED_COMMAND_CODE:
/* Merged to match error defined in RMAP standard for replies. */
return RMAP_NODE_PACKET_ERROR;
default:
assert(status == RMAP_OK);
break;
}
if (rmap_is_write(packet)) {
node->callbacks.initiator.received_write_reply(
node,
transaction_custom_context,
rmap_get_transaction_identifier(packet),
rmap_get_status(packet));
return RMAP_OK;
}
/* Read or RMW. */
status = rmap_verify_data(packet, size);
switch (status) {
case RMAP_RMW_DATA_LENGTH_ERROR:
case RMAP_INSUFFICIENT_DATA:
case RMAP_TOO_MUCH_DATA:
case RMAP_INVALID_DATA_CRC:
/* Merged to match error defined in RMAP standard for replies. */
return RMAP_NODE_INVALID_REPLY;
default:
assert(status == RMAP_OK);
break;
}
if (rmap_is_rmw(packet)) {
node->callbacks.initiator.received_rmw_reply(
node,
transaction_custom_context,
rmap_get_transaction_identifier(packet),
rmap_get_status(packet),
packet + rmap_calculate_header_size(packet),
size - rmap_calculate_header_size(packet) - 1);
return RMAP_OK;
}
/* Read. */
node->callbacks.initiator.received_read_reply(
node,
transaction_custom_context,
rmap_get_transaction_identifier(packet),
rmap_get_status(packet),
packet + rmap_calculate_header_size(packet),
size - rmap_calculate_header_size(packet) - 1);
return RMAP_OK;
}
enum rmap_status rmap_node_handle_incoming(
struct rmap_node *node,
void *const transaction_custom_context,
const void *const packet,
const size_t size,
const bool has_eep_termination)
{
enum rmap_status status;
status = rmap_verify_header_integrity(packet, size);
switch (status) {
case RMAP_NO_RMAP_PROTOCOL:
case RMAP_INCOMPLETE_HEADER:
case RMAP_HEADER_CRC_ERROR:
return status;
default:
assert(status == RMAP_OK);
break;
}
if (rmap_is_command(packet)) {
return handle_command(
node,
transaction_custom_context,
packet,
size,
has_eep_termination);
}
return handle_reply(node, transaction_custom_context, packet, size);
}