-
Notifications
You must be signed in to change notification settings - Fork 27
/
frame.c
2708 lines (2355 loc) · 79.1 KB
/
frame.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
/*
* Homeplug 1.0/AV Ethernet frame handling and operations
*
* Copyright (C) 2007-2008 Xavier Carcelle <[email protected]>
* Florian Fainelli <[email protected]>
* Nicolas Thill <[email protected]>
*
* The BSD License
* ===============
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of OpenLink Software Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OPENLINK OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
#include <arpa/inet.h>
#include <sys/types.h>
#ifndef __CYGWIN__
#include <net/ethernet.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <inttypes.h>
#include "faifa.h"
#include "faifa_compat.h"
#include "faifa_priv.h"
#include "frame.h"
#include "homeplug.h"
#include "homeplug_av.h"
#include "crypto.h"
#include "endian.h"
#include "crypto.h"
#include "crc32.h"
FILE *err_stream;
FILE *out_stream;
FILE *in_stream;
/* Constants */
static u_int8_t hpav_intellon_oui[3] = { 0x00, 0xB0, 0x52};
static u_int8_t hpav_intellon_macaddr[ETHER_ADDR_LEN] = { 0x00, 0xB0, 0x52, 0x00, 0x00, 0x01 };
static u_int8_t broadcast_macaddr[ETHER_ADDR_LEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
/**
* init_hex - initialize a buffer using hexadecimal parsing (%2hx)
* @buf: buffer to initialize
* @len: length of the buffer (sizeof(buf))
*/
static int init_hex(void *buf, int len)
{
int avail = len;
u_int8_t *p = buf;
while (avail > 0) {
if (fscanf(in_stream, "%2hx", (short unsigned int *)p) <= 0)
break;
p++;
avail--;
}
return (len - avail);
}
/**
* dump_hex - dump a buffer using the hexadecimal conversion (%02hX)
* @buf: buffer to dump the content
* @len: length of the buffer (sizeof(buf))
* @sep: optional separator, defaults to empty
*/
int dump_hex(void *buf, int len, char *sep)
{
int avail = len;
u_int8_t *p = buf;
while (avail > 0) {
faifa_printf(out_stream, "%02hX%s", *p, (avail > 1) ? sep : "");
p++;
avail--;
}
return len;
}
#define HEX_BLOB_BYTES_PER_ROW 16
static u_int32_t dump_hex_blob(faifa_t *UNUSED(faifa), u_int8_t *buf, u_int32_t len)
{
u_int32_t i, d, m = len % HEX_BLOB_BYTES_PER_ROW;
faifa_printf(out_stream, "Binary Data, %lu bytes", (unsigned long int)len);
for (i = 0; i < len; i += HEX_BLOB_BYTES_PER_ROW) {
d = (len - i) / HEX_BLOB_BYTES_PER_ROW;
faifa_printf(out_stream, "\n%08lu: ", (unsigned long int)i);
dump_hex((u_int8_t *)buf + i, (d > 0) ? HEX_BLOB_BYTES_PER_ROW : m, " ");
}
faifa_printf(out_stream, "\n");
return len;
}
/**
* init_empty_frame - do nothing to a frame
* @buf: unused
* @len: unused
*/
static int init_empty_frame(void *UNUSED(buf), int UNUSED(len), void *UNUSED(user))
{
return 0;
}
/*
* The following functions are not documented and are faifa internal
* operations. They should never be called from outside do_frame or
* do_receive_frame because the of generic buffer handling.
*
* These functions are called as generic callbacks to a corresponding
* request, confirmation, indication or response.
*/
static int hpav_init_write_mac_memory_request(void *buf, int len, void *UNUSED(buffer))
{
int avail = len;
struct write_mac_memory_request *mm = buf;
int ret;
faifa_printf(out_stream, "Address? ");
ret = fscanf(in_stream, "%8x", &(mm->address));
if (ret < 0)
return ret;
faifa_printf(out_stream, "Length? ");
ret = fscanf(in_stream, "%8x", &(mm->length));
if (ret < 0)
return ret;
avail -= sizeof(*mm);
faifa_printf(out_stream, "Data?\n");
avail -= init_hex(mm->data, mm->length);
return (len - avail);
}
static const char *int6x00_device_id_str(u_int8_t device_id)
{
switch (device_id) {
case INT6000_DEVICE_ID:
return "INT6000";
case INT6300_DEVICE_ID:
return "INT6300";
case INT6400_DEVICE_ID:
return "INT6400";
default:
return "Unknown";
}
}
static int hpav_dump_get_device_sw_version_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct get_device_sw_version_confirm *mm = buf;
faifa_printf(out_stream, "Status: %s\n", mm->mstatus ? "Failure" : "Success");
faifa_printf(out_stream, "Device ID: %s, Version: %s, upgradeable: %hhd\n",
int6x00_device_id_str(mm->device_id),
(char *)(mm->version), mm->upgradeable);
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_dump_write_mac_memory_request(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct write_mac_memory_request *mm = buf;
faifa_printf(out_stream, "Address: 0x%08x\n", mm->address);
faifa_printf(out_stream, "Length: 0x%08x\n", mm->length);
avail -= sizeof(*mm);
faifa_printf(out_stream, "Data: ");
avail -= dump_hex(mm->data, mm->length, " ");
faifa_printf(out_stream, "\n");
return (len - avail);
}
static int hpav_dump_write_mac_memory_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct write_mac_memory_confirm *mm = buf;
switch (mm->mstatus) {
case 0x00:
faifa_printf(out_stream, "Status: Succes\n");
break;
case 0x10:
faifa_printf(out_stream, "Status: Invalid address\n");
goto out;
break;
case 0x14:
faifa_printf(out_stream, "Status: Invalid length\n");
goto out;
break;
}
faifa_printf(out_stream, "Address: 0x%08x\n", mm->address);
faifa_printf(out_stream, "Length: 0x%08x\n", mm->length);
out:
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_init_read_mac_memory_request(void *buf, int len, void *UNUSED(buffer))
{
int avail = len;
struct read_mac_memory_request *mm = buf;
int ret;
faifa_printf(out_stream, "Address? ");
ret = fscanf(in_stream, "%8x", &(mm->address));
if (ret < 0)
return ret;
faifa_printf(out_stream, "Length? ");
ret = fscanf(in_stream, "%8x", &(mm->length));
if (ret < 0)
return ret;
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_dump_read_mac_memory_request(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct read_mac_memory_confirm *mm = buf;
faifa_printf(out_stream, "Address: 0x%08x\n", mm->address);
faifa_printf(out_stream, "Length: %u (0x%08x)\n", mm->length, mm->length);
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_dump_read_mac_memory_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct read_mac_memory_confirm *mm = buf;
switch (mm->mstatus) {
case 0x00:
faifa_printf(out_stream, "Status: Succes\n");
break;
case 0x10:
faifa_printf(out_stream, "Status: Invalid address\n");
goto out;
break;
case 0x14:
faifa_printf(out_stream, "Status: Invalid length\n");
goto out;
break;
}
faifa_printf(out_stream, "Address: 0x%08x\n", mm->address);
faifa_printf(out_stream, "Length: %u (0x%08x)\n", mm->length, mm->length);
faifa_printf(out_stream, "Data: ");
avail -= dump_hex(mm->data, mm->length, " ");
faifa_printf(out_stream, "\n");
out:
avail -= sizeof(*mm);
return (len - avail);
}
static const char *get_signal_level_str(u_int8_t sig_level)
{
switch (sig_level) {
case 0x00:
return "N/A";
case 0x01:
return "> - 10 dB, but <= 0 dB";
case 0x02:
return "> - 15 dB, but <= -10 dB";
case 0x03:
return "> - 20 dB, but <= -15 dB";
case 0x04:
return "> - 25 dB, but <= -20 dB";
case 0x05:
return "> - 30 dB, but <= -25 dB";
case 0x06:
return "> - 35 dB, but <= -30 dB";
case 0x07:
return "> - 40 dB, but <= -35 dB";
case 0x08:
return "> - 45 dB, but <= -40 dB";
case 0x09:
return "> - 50 dB, but <= -45 dB";
case 0x0A:
return "> - 55 dB, but <= -50 dB";
case 0x0B:
return "> - 60 dB, but <= -55 dB";
case 0x0C:
return "> - 65 dB, but <= -60 dB";
case 0x0D:
return "> - 70 dB, but <= -65 dB";
case 0x0E:
return "> - 75 dB, but <= -70 dB";
case 0x0F:
return "<= -75 dB";
default:
return "Unknown";
}
return NULL;
}
static void dump_cc_sta_info(struct cc_sta_info *sta_info)
{
faifa_printf(out_stream, "MAC address: ");
dump_hex(sta_info->macaddr, ETHER_ADDR_LEN, ":");
faifa_printf(out_stream, "\n");
faifa_printf(out_stream, "TEI: %d\n", sta_info->tei);
faifa_printf(out_stream, "Same network: %s\n", sta_info->same_network ? "Yes" : "No");
faifa_printf(out_stream, "SNID: %d\n", sta_info->snid);
faifa_printf(out_stream, "CCo caps: %02hhx\n", sta_info->cco_cap);
faifa_printf(out_stream, "Signal Level: %s\n", get_signal_level_str(sta_info->sig_level));
}
static const char *get_cco_status_str(u_int8_t status)
{
switch (status) {
case 0x00:
return "Unknown";
case 0x01:
return "Non-Coordinating network";
case 0x02:
return "Coordinating, group status unknown";
case 0x03:
return "Coordinating network, same group as CCo";
case 0x04:
return "Coordinating network, not same group as CCo";
default:
return "Unknown";
}
return NULL;
}
static void dump_cc_net_info(struct cc_net_info *net_info)
{
faifa_printf(out_stream, "Network ID: "); dump_hex(net_info->nid, sizeof(net_info->nid), " "); faifa_printf(out_stream, "\n");
faifa_printf(out_stream, "SNID: %d\n", net_info->snid);
faifa_printf(out_stream, "Hybrid mode: %d\n", net_info->hybrid_mode);
faifa_printf(out_stream, "Number of BCN slots: %d\n", net_info->num_bcn_slots);
faifa_printf(out_stream, "CCo status: %s\n", get_cco_status_str(net_info->cco_status));
faifa_printf(out_stream, "Beacon offset: %04hx\n", net_info->bcn_offset);
}
static int hpav_dump_cc_discover_list_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct cc_discover_list_confirm *mm = buf;
struct cc_sta_infos *sta = (struct cc_sta_infos *)&(mm->sta);
struct cc_net_infos *net = (struct cc_net_infos *)&(sta->infos[sta->count]);
int i;
faifa_printf(out_stream, "Number of Stations: %d\n", sta->count);
avail -= sizeof(*sta);
for (i = 0; i < sta->count; i++) {
dump_cc_sta_info(&(sta->infos[i]));
avail -= sizeof(sta->infos[i]);
}
faifa_printf(out_stream, "Number of Networks: %d\n", net->count);
avail -= sizeof(*net);
for (i = 0; i < net->count; i++) {
dump_cc_net_info(&(net->infos[i]));
avail -= sizeof(net->infos[i]);
}
return (len - avail);
}
static int hpav_init_start_mac_request(void *buf, int len, void *UNUSED(buffer))
{
int avail = len;
struct start_mac_request *mm = buf;
int ret;
faifa_printf(out_stream, "Module ID? ");
ret = fscanf(in_stream, "%2hhx", &(mm->module_id));
if (ret < 0)
return ret;
faifa_printf(out_stream, "Image load address? ");
ret = fscanf(in_stream, "%8x", &(mm->image_load));
if (ret < 0)
return ret;
faifa_printf(out_stream, "Image length? ");
ret = fscanf(in_stream, "%8x", &(mm->image_length));
if (ret < 0)
return ret;
faifa_printf(out_stream, "Image checksum? ");
ret = fscanf(in_stream, "%8x", &(mm->image_chksum));
if (ret < 0)
return ret;
faifa_printf(out_stream, "Image start address? ");
ret = fscanf(in_stream, "%8x", &(mm->image_saddr));
if (ret < 0)
return ret;
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_dump_start_mac_request(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct start_mac_request *mm = buf;
faifa_printf(out_stream, "Module ID: %02hhx\n", mm->module_id);
faifa_printf(out_stream, "Image load address: %08x\n", mm->image_load);
faifa_printf(out_stream, "Image length: %u (0x%08x)\n", mm->image_length, mm->image_length);
faifa_printf(out_stream, "Image checksum: %08x\n", mm->image_chksum);
faifa_printf(out_stream, "Image start address: %08x\n", mm->image_saddr);
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_dump_start_mac_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct start_mac_confirm *mm = buf;
switch (mm->mstatus) {
case 0x00:
faifa_printf(out_stream, "Status: Success\n");
break;
case 0x10:
faifa_printf(out_stream, "Status: Invalid module ID\n");
goto out;
break;
case 0x14:
faifa_printf(out_stream, "Status: NVM not present\n");
goto out;
break;
case 0x18:
faifa_printf(out_stream, "Status: NVM too small\n");
goto out;
break;
case 0x1C:
faifa_printf(out_stream, "Status: Invalid header checksum\n");
goto out;
break;
case 0x20:
faifa_printf(out_stream, "Status: Invalid section checksum\n");
goto out;
break;
}
faifa_printf(out_stream, "Module ID: %02hhx\n", mm->module_id);
out:
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_dump_nvram_params_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct get_nvm_parameters_confirm *mm = buf;
faifa_printf(out_stream, "Status: %s\n", mm->mstatus ? "NVRAM not present" : "Success");
faifa_printf(out_stream, "Manufacturer code: %08x\n", mm->manuf_code);
faifa_printf(out_stream, "Page size: %u (0x%08x)\n", mm->page_size, mm->page_size);
faifa_printf(out_stream, "Block size: %u (0x%08x)\n", mm->block_size, mm->block_size);
faifa_printf(out_stream, "Memory size: %u (0x%08x)\n", mm->mem_size, mm->mem_size);
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_dump_reset_device_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct reset_device_confirm *mm = buf;
faifa_printf(out_stream, "Status : %s\n", mm->mstatus ? "Failure" : "Success");
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_init_write_data_request(void *buf, int len, void *UNUSED(buffer))
{
int avail = len;
struct write_mod_data_request *mm = buf;
char filename[256];
char *buffer;
FILE *fp;
short unsigned int size;
uint32_t crc32;
int ret;
faifa_printf(out_stream, "Module ID? ");
ret = fscanf(in_stream, "%2hhx", &(mm->module_id));
if (ret < 0)
return ret;
faifa_printf(out_stream, "Offset? ");
ret = fscanf(in_stream, "%8x", &(mm->offset));
if (ret < 0)
return ret;
faifa_printf(out_stream, "Firmware file? ");
ret = fscanf(in_stream, "%s", (char *)filename);
if (ret < 0)
return ret;
fp = fopen(filename, "rb");
if (!fp) {
faifa_printf(err_stream, "Cannot open: %s\n", filename);
avail = -1;
goto out;
}
fseek(fp, 0, SEEK_END);
size = ftell(fp);
if (size > 1024) {
faifa_printf(out_stream, "Invalid file size > 1024\n");
avail = -1;
goto out;
}
fseek(fp, 0, SEEK_SET);
mm->length = size;
buffer = malloc(size);
if (!buffer) {
faifa_printf(err_stream, "Cannot allocate memory\n");
avail = -1;
goto out;
}
ret = fread(buffer, size, 1, fp);
if (ret < size)
return ret;
/* Compute crc on the file */
crc32 = crc32buf(buffer, size);
memcpy(&(mm->data), buffer, size);
mm->checksum = crc32;
out:
if (fp)
fclose(fp);
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_dump_write_mod_data_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct write_mod_data_confirm *mm =(struct write_mod_data_confirm *)buf;
switch((short unsigned int)(mm->module_id)) {
case SUCCESS:
faifa_printf(out_stream, "Status: Success\n");
break;
case INV_MOD_ID:
faifa_printf(out_stream, "Status: Invalid module ID\n");
return (len - avail);
break;
case BAD_HDR_CHKSUM:
faifa_printf(out_stream, "Status: Bad header checksum\n");
return (len - avail);
break;
case INV_LEN:
faifa_printf(out_stream, "Status: Invalid length\n");
return (len - avail);
break;
case UNEX_OFF:
faifa_printf(out_stream, "Status: Unexpected offset\n");
return (len - avail);
break;
case INV_CHKSUM:
faifa_printf(out_stream, "Status: Invalid checksum\n");
return (len - avail);
break;
default:
break;
}
faifa_printf(out_stream, "Length: %d\n", mm->length);
faifa_printf(out_stream, "Offset: %08x\n", mm->offset);
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_init_read_mod_data_request(void *buf, int len, void *UNUSED(buffer))
{
int avail = len;
struct read_mod_data_request *mm = buf;
int ret;
faifa_printf(out_stream, "Module ID? ");
ret = fscanf(in_stream, "%2hhx", &(mm->module_id));
if (ret < 0)
return ret;
faifa_printf(out_stream, "Length? ");
ret = fscanf(in_stream, "%hu", &(mm->length));
if (ret < 0)
return ret;
faifa_printf(out_stream, "Offset? ");
ret = fscanf(in_stream, "%u", &(mm->offset));
if (ret < 0)
return ret;
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_dump_read_mod_data_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct read_mod_data_confirm *mm = buf;
faifa_printf(out_stream, "Status: %s\n", mm->mstatus ? "Failure" : "Success");
faifa_printf(out_stream, "Module ID: 0x%02hhx\n", mm->module_id);
faifa_printf(out_stream, "Length: %d\n", mm->length);
faifa_printf(out_stream, "Offset: 0x%08x\n", mm->offset);
faifa_printf(out_stream, "Checksum: 0x%08x\n", mm->checksum);
faifa_printf(out_stream, "Data:\n");
dump_hex(mm->data + mm->offset, (unsigned int)(mm->length), " ");
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_dump_get_manuf_string_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct get_manuf_string_confirm *mm = buf;
faifa_printf(out_stream, "Status: %s\n", mm->status ? "Failure" : "Success");
faifa_printf(out_stream, "Length: %hhd (0x%02hhx)\n", mm->length, mm->length);
faifa_printf(out_stream, "Manufacturer string: %s\n", (char *)(mm->data));
avail -= sizeof(*mm);
return (len - avail);
}
static void dump_conf_block(struct block_header *hdr)
{
faifa_printf(out_stream, "Version: %08x\n", hdr->version);
faifa_printf(out_stream, "Image address in NVRAM: 0x%08x\n", hdr->img_rom_addr);
faifa_printf(out_stream, "Image address in SDRAM: 0x%08x\n", hdr->img_sdram_addr);
faifa_printf(out_stream, "Image length: %u (0x%08x)\n", hdr->img_length, hdr->img_length);
faifa_printf(out_stream, "Image checksum: %08x\n", hdr->img_checksum);
faifa_printf(out_stream, "Image SDRAM entry point: 0x%08x\n", hdr->entry_point);
faifa_printf(out_stream, "Address of next header: 0x%08x\n", hdr->next_header);
faifa_printf(out_stream, "Header checksum: 0x%08x\n", hdr->hdr_checksum);
}
static void dump_sdram_block(struct sdram_config *config)
{
faifa_printf(out_stream, "Size : %u (0x%08x)\n", config->size, config->size);
faifa_printf(out_stream, "Configuration reg: 0x%08x\n", config->conf_reg);
faifa_printf(out_stream, "Timing reg0: 0x%08x\n", config->timing0);
faifa_printf(out_stream, "Timing reg1: 0x%08x\n", config->timing1);
faifa_printf(out_stream, "Control reg: 0x%08x\n", config->ctl_reg);
faifa_printf(out_stream, "Refresh reg: 0x%08x\n", config->ref_reg);
faifa_printf(out_stream, "MAC clock reg : %u (0x%08x)\n", config->clk_reg_val, config->clk_reg_val);
}
static int hpav_dump_read_config_block_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct read_config_block_confirm *mm = buf;
switch (mm->mstatus) {
case 0x00:
faifa_printf(out_stream, "Status: Success\n");
break;
case 0x01:
faifa_printf(out_stream, "Status: Failure\n");
goto out;
break;
case 0x10:
faifa_printf(out_stream, "Status: No flash\n");
goto out;
break;
}
faifa_printf(out_stream, "Config length: %d\n", mm->config_length);
dump_conf_block(&(mm->hdr));
dump_sdram_block(&(mm->config));
out:
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_dump_set_sdram_config_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
short unsigned int *status = (short unsigned int *)buf;
switch (*status) {
case SUCCESS:
faifa_printf(out_stream, "Status: Success\n");
break;
case SDR_INV_CHKSUM:
faifa_printf(out_stream, "Status: Invalid checksum\n");
break;
case SDR_BIST_FAILED:
faifa_printf(out_stream, "Status: BIST failed\n");
break;
default:
break;
}
avail -= sizeof(*status);
return (len - avail);
}
static int hpav_init_get_devices_attrs_request(void *buf, int len, void *UNUSED(buffer))
{
int avail = len;
struct get_devices_attrs_request *mm = buf;
/* Generate a random number for the cookie */
srand(getpid());
mm->cookie = (long unsigned int)(random());
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_dump_get_devices_attrs_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct get_devices_attrs_confirm *mm = buf;
switch (mm->status) {
case 0x00:
faifa_printf(out_stream, "Status: Success\n");
break;
case 0x01:
faifa_printf(out_stream, "Status: Failure\n");
goto out;
break;
case 0x02:
faifa_printf(out_stream, "Status: Not supported\n");
goto out;
break;
}
faifa_printf(out_stream, "Cookie: %u\n", mm->cookie);
faifa_printf(out_stream, "Report type: %s\n", mm->rtype ? "XML" : "Binary");
faifa_printf(out_stream, "Size: %d\n", mm->size);
faifa_printf(out_stream, "Hardware: %s\n", mm->fmt.hardware);
faifa_printf(out_stream, "Software: %s\n", mm->fmt.software);
faifa_printf(out_stream, "Major: %u\n", mm->fmt.major);
faifa_printf(out_stream, "Minor: %u\n", mm->fmt.minor);
faifa_printf(out_stream, "Subversion: %u\n", mm->fmt.subversion);
faifa_printf(out_stream, "Build number: %u\n", mm->fmt.build_number);
faifa_printf(out_stream, "Build date: %s\n", mm->fmt.build_date);
faifa_printf(out_stream, "Release type: %s\n", mm->fmt.release_type);
out:
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_init_get_enet_phy_settings_request(void *buf, int len, void *UNUSED(user))
{
int avail = len;
struct get_enet_phy_settings_request *mm = buf;
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_dump_get_enet_phy_settings_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct get_enet_phy_settings_confirm *mm = buf;
faifa_printf(out_stream, "Status: %s\n", mm->status ? "Failure" : "Success");
switch(mm->speed) {
case ENET:
faifa_printf(out_stream, "Speed: Ethernet (10Mbits)\n");
break;
case FA_ENET:
faifa_printf(out_stream, "Speed: Fast Ethernet (100Mbits)\n");
break;
case GIG_ENET:
faifa_printf(out_stream, "Speed : Gigabit Ethernet (1Gbits)\n");
break;
}
faifa_printf(out_stream, "Duplex: %s\n", mm->duplex ? "Full duplex" : "Half duplex");
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_init_get_tone_map_charac_request(void *buf, int len, void *UNUSED(user))
{
int avail = len;
u_int8_t macaddr[ETHER_ADDR_LEN];
struct get_tone_map_charac_request *mm = buf;
int ret;
faifa_printf(out_stream, "Address of peer node?\n");
ret = fscanf(in_stream, "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx",
&macaddr[0], &macaddr[1], &macaddr[2],
&macaddr[3], &macaddr[4], &macaddr[5]);
if (ret < 0)
return ret;
memcpy(mm->macaddr, macaddr, ETHER_ADDR_LEN);
faifa_printf(out_stream, "Tone map slot?\n0 -> slot 0\n1 -> slot 1 ...\n");
ret = fscanf(in_stream, "%2hhx", &(mm->tmslot));
if (ret < 0)
return ret;
avail -= sizeof(*mm);
return (len - avail);
}
char *get_carrier_modulation_str(short unsigned int modulation, struct modulation_stats *stats)
{
switch(modulation) {
case NO:
stats->no++;
return "No";
case BPSK:
stats->bpsk++;
return "BPSK";
case QPSK:
stats->qpsk++;
return "QPSK";
case QAM_8:
stats->qam8++;
return "QAM-8";
case QAM_16:
stats->qam16++;
return "QAM-16";
case QAM_64:
stats->qam64++;
return "QAM-64";
case QAM_256:
stats->qam256++;
return "QAM-256";
case QAM_1024:
stats->qam1024++;
return "QAM-1024";
default:
stats->unknown++;
return "Unknown";
}
}
static void dump_modulation_stats(struct modulation_stats *stats)
{
unsigned sum = 0;
sum = stats->no + stats->bpsk + stats->qpsk + stats->qam8 + stats->qam16 + stats->qam64 +
stats->qam256 + stats->qam1024 + stats->unknown;
faifa_printf(out_stream, "Number of carriers with NO modulation: %d (%f %%)\n", stats->no, (double)((stats->no * 100) / sum));
faifa_printf(out_stream, "Number of carriers with BPSK modulation: %d (%f %%)\n", stats->bpsk, (double)((stats->bpsk * 100) / sum));
faifa_printf(out_stream, "Number of carriers with QPSK modulation: %d (%f %%)\n", stats->qpsk, (double)((stats->qpsk * 100) / sum));
faifa_printf(out_stream, "Number of carriers with QAM-8 modulation: %d (%f %%)\n", stats->qam8, (double)((stats->qam8 * 100) / sum));
faifa_printf(out_stream, "Number of carriers with QAM-16 modulation: %d (%f %%)\n", stats->qam16, (double)((stats->qam16 * 100) / sum));
faifa_printf(out_stream, "Number of carriers with QAM-64 modulation: %d (%f %%)\n", stats->qam64, (double)((stats->qam64 * 100) / sum));
faifa_printf(out_stream, "Number of carriers with QAM-256 modulation: %d (%f %%)\n", stats->qam256, (double)((stats->qam256 * 100) / sum));
faifa_printf(out_stream, "Number of carriers with QAM-1024 modulation: %d (%f %%)\n", stats->qam1024, (double)((stats->qam1024 * 100) / sum));
faifa_printf(out_stream, "Number of carriers with Unknown/unused modulation: %d (%f %%)\n", stats->unknown, (double)((stats->unknown * 100) / sum));
faifa_printf(out_stream, "Number of modulation: %d\n", sum);
}
static int hpav_dump_get_tone_map_charac_confirm(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
int i;
struct get_tone_map_charac_confirm *mm = buf;
struct modulation_stats stats;
uint16_t max_carriers;
switch (mm->mstatus) {
case 0x00:
faifa_printf(out_stream, "Status: Success\n");
break;
case 0x01:
faifa_printf(out_stream, "Status: Unknown MAC address\n");
goto out;
break;
case 0x02:
faifa_printf(out_stream, "Status: unknown ToneMap slot\n");
goto out;
break;
}
faifa_printf(out_stream, "Tone map slot: %02hhd\n", mm->tmslot);
faifa_printf(out_stream, "Number of tone map: %02hhd\n", mm->num_tms);
faifa_printf(out_stream, "Tone map number of active carriers: %d\n", mm->tm_num_act_carrier);
memset(&stats, 0, sizeof(stats));
max_carriers = mm->tm_num_act_carrier / 2;
if (mm->tm_num_act_carrier & 1)
max_carriers += 1;
for (i = 0; i < max_carriers; i++) {
faifa_printf(out_stream, "Modulation for carrier: %d : %s\n", i, get_carrier_modulation_str(mm->carriers[i].mod_carrier_lo, &stats));
faifa_printf(out_stream, "Modulation for carrier: %d : %s\n", i + 1, get_carrier_modulation_str(mm->carriers[i].mod_carrier_hi, &stats));
}
faifa_printf(out_stream, "Modulation statistics\n");
dump_modulation_stats(&stats);
out:
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_init_watchdog_report_request(void *buf, int len, void *UNUSED(buffer))
{
int avail = len;
struct get_watchdog_report_request *mm = buf;
srand(getpid());
mm->session_id = (unsigned int)(random());
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_dump_watchdog_report_indicate(void *buf, int len, struct ether_header *UNUSED(hdr))
{
int avail = len;
struct get_watchdog_report_indicate *mm = buf;
faifa_printf(out_stream, "Status: %s\n", mm->mstatus ? "Failure" : "Success");
faifa_printf(out_stream, "Session ID: %d\n", mm->session_id);
faifa_printf(out_stream, "Number of parts: %d\n", mm->num_parts);
faifa_printf(out_stream, "Current part: %d\n", mm->cur_part);
faifa_printf(out_stream, "Data length: %d\n", mm->data_length);
faifa_printf(out_stream, "Data offset: 0x%02hhx\n", mm->data_offset);
avail -= sizeof(*mm);
return (len - avail);
}
static int hpav_init_link_stats_request(void *buf, int len, void *UNUSED(user))
{
int avail = len;
struct link_statistics_request *mm = buf;
u_int8_t link_id;
u_int8_t macaddr[ETHER_ADDR_LEN];
int direction;
int ret;
faifa_printf(out_stream, "Direction ?\n0: TX\n1: RX\n2: TX and RX\n");
ret = fscanf(in_stream, "%2d", &direction);
if (ret < 0)