-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
common.c
executable file
·1566 lines (1400 loc) · 42.1 KB
/
common.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
// Copyright (C) 2015-2022 Scott Dwyer.
// You may use/distribute/modify this freely, under the terms of
// the GNU General Public License version 2 or later version.
// This software is distributed WITHOUT ANY WARRANTY.
#include "common.h"
#include "clone_gui_language.h"
#include "io.h"
time_t start_time_ccc, end_time_ccc;
struct timeval tvBegin_ccc, tvEnd_ccc, tvDiff_ccc, tvCurrent_ccc;
bool check_only_ccc;
bool do_indentation_ccc;
int ccc_indent_ccc;
int spaces_ccc;
char *argument_ccc[MAX_ARGUMENTS];
unsigned int argument_count_ccc;
bool ncq_supported_ccc = false; // word 76 bit 8
bool rebuild_assist_supported_ccc = false; // word 78 bit 11
bool rebuild_assist_enabled_ccc;
int rebuild_assist_test_ccc = -1;
int process_id_ccc;
int driver_return_zeros_on_error_ccc;
int driver_io_scsi_only_ccc;
bool quiet_ccc;
bool stop_signal_ccc;
bool superclone_ccc;
bool critical_process_ccc;
bool pending_exit_ccc;
bool logfile_changed_ccc;
bool domainfile_changed_ccc;
bool force_danger_ccc;
bool force_stupid_ccc;
bool repair_log_ccc;
bool fill_mode_ccc;
bool driver_only_ccc;
bool cleanup_mode_ccc;
unsigned long long verbose_ccc;
unsigned long long debug_ccc;
bool debugversion_ccc;
bool verbosedebug_ccc;
int return_value_ccc;
bool print_hex_ccc;
int if_statement_level_ccc;
int while_statement_level_ccc;
long long error_level_ccc;
unsigned char exitcode_ccc;
FILE *debug_file_ccc;
char *disk_1_ccc;
char *disk_2_ccc=NULL;
char *script_file_ccc;
char current_script_ccc[MAX_LINE_LENGTH];
int print_error_message_ccc;
char exit_message_ccc[EXIT_MESSAGE_SIZE];
char error_message_ccc[ERROR_MESSAGE_SIZE];
char display_message_ccc[DISPLAY_MESSAGE_SIZE];
char tempmessage_ccc[TEMP_MESSAGE_SIZE];
char debug_message_ccc[DEBUG_MESSAGE_SIZE];
char log_display_message_ccc[DISPLAY_MESSAGE_SIZE];
void* ccc_buffer_ccc;
unsigned long long ccc_main_buffer_size_ccc;
void* ccc_scratchpad_ccc;
unsigned long long ccc_main_scratchpad_size_ccc;
void* sensebuffer_ccc;
unsigned long long sensebuffer_size_ccc;
void* ccc_usbbuffer_ccc;
char usbcbwbuffer_ccc[USBCBW_BUFFER_SIZE];
char usbcswbuffer_ccc[USBCSW_BUFFER_SIZE];
unsigned long long ccc_main_usbbuffer_size_ccc;
void* padding_buffer_ccc=NULL;
unsigned char identify_buffer_ccc[IDENTIFY_BUFFER_SIZE];
unsigned char ata_identify_buffer_ccc[IDENTIFY_BUFFER_SIZE];
unsigned char read_capacity_buffer_ccc[512];
int append_script_ccc;
long long *number_variable_buffer_ccc;
char **string_variable_pointer_ccc;
char current_date_ccc[40];
char current_time_ccc[40];
unsigned char sbyte_ccc[16];
unsigned char superbyte_ccc[1024];
int check_command_ccc;
int check_read_ccc;
int check_write_ccc;
int disk1_fd_ccc;
int disk2_fd_ccc;
int current_disk_ccc;
int main_driver_fd_ccc;
bool command_line_ccc;
int command_length_ccc;
unsigned char sense_buf_ccc[255];
unsigned char sense_key_ccc;
unsigned char asc_ccc;
unsigned char ascq_ccc;
unsigned char ata_error_ccc;
unsigned char ata_status_ccc;
unsigned char ata_device_ccc;
unsigned char ata_count1_ccc;
unsigned char ata_count2_ccc;
unsigned char ata_lba1_ccc;
unsigned char ata_lba2_ccc;
unsigned char ata_lba3_ccc;
unsigned char ata_lba4_ccc;
unsigned char ata_lba5_ccc;
unsigned char ata_lba6_ccc;
long long ata_lba_ccc;
int ata_count_ccc;
int io_status_ccc;
int io_masked_status_ccc;
int msg_status_ccc;
int io_sb_len_wr_ccc;
int io_host_status_ccc;
int io_driver_status_ccc;
int io_resid_ccc;
int io_duration_ccc;
int io_info_ccc;
int ata_return_valid_ccc;
int ata_check_ccc;
bool pio_mode_ccc;
int driver_installed_ccc;
long long hpa_lba_ccc;
long long dco_lba_ccc;
bool extended_support_ccc;
bool enable_process_chunk_ccc;
bool enable_read_twice_ccc;
bool enable_scsi_write_ccc=false;
bool charater_device_driver_mode_ccc;
unsigned long reg_base_address_ccc;
unsigned long control_base_address_ccc;
unsigned long bus_base_address_ccc;
bool device_select_base_ccc;
unsigned long long hba_base_address_ccc;
unsigned long long hba_reset_address_ccc;
unsigned long long port_base_address_ccc;
unsigned long long port_reset_address_ccc;
int port_number_base_ccc;
char device_driver_base_ccc[64];
char device_bus_base_ccc[64];
char current_source_model_ccc[64];
char current_source_serial_ccc[64];
char current_destination_model_ccc[64];
char current_destination_serial_ccc[64];
int current_device_type_ccc;
bool copy_image_mode_ccc;
bool generic_mode_ccc;
bool direct_mode_ccc;
bool ahci_mode_ccc;
bool ata_passthrough_ccc;
bool scsi_passthrough_ccc;
bool nvme_passthrough_ccc;
bool auto_passthrough_ccc;
bool usb_mode_ccc;
bool usb_allow_ata_ccc;
bool supertool_mode_chosen_ccc;
unsigned int pagesize_ccc;
unsigned long long table_virtual_address_ccc;
unsigned long long command_list_virtual_address_ccc;
unsigned long long fis_virtual_address_ccc;
unsigned long long buffer_virtual_address_ccc;
unsigned long long table_physical_address_ccc;
unsigned long long command_list_physical_address_ccc;
unsigned long long fis_physical_address_ccc;
unsigned long long buffer_physical_address_ccc[8192];
int command_list_size_ccc;
int fis_size_ccc;
void* table_buffer_ccc;
void* command_list_buffer_ccc;
void* fis_buffer_ccc;
int hba_mem_dev_ccc;
uint32_t hba_alloc_mem_size_ccc;
void *hba_mem_pointer_ccc;
int port_mem_dev_ccc;
uint32_t port_alloc_mem_size_ccc;
void *port_mem_pointer_ccc;
void *port_virt_addr_ccc;
unsigned long long max_dma_size_ccc;
unsigned long long real_buffer_size_ccc;
unsigned long long first_read_time_ccc;
unsigned long long soft_reset_time_ccc;
unsigned long long hard_reset_time_ccc;
unsigned long long busy_wait_time_ccc;
unsigned long long reset_wait_time_ccc;
unsigned long long initial_busy_wait_time_ccc;
unsigned long long general_timeout_ccc;
int usb_timeout_ccc;
int cbw_timeout_ccc;
int csw_timeout_ccc;
unsigned long long power_cycle_time_ccc;
bool phase_timers_ccc;
unsigned long long orig_soft_reset_time_ccc;
unsigned long long p12_soft_reset_time_ccc;
unsigned long long p3_soft_reset_time_ccc;
unsigned long long p4_soft_reset_time_ccc;
unsigned long long td_soft_reset_time_ccc;
unsigned long long d2_soft_reset_time_ccc;
unsigned long long sc_soft_reset_time_ccc;
unsigned long long rt_soft_reset_time_ccc;
char *drive_serial_ccc;
int new_source_ccc;
int new_destination_ccc;
long long source_total_size_ccc;
long long bytes_per_sector_ccc;
long long target_total_size_ccc;
long long generic_source_total_size_ccc;
unsigned char *driver_control_address_ccc;
unsigned char *driver_error_bitmap_address_ccc;
unsigned char *driver_transfer_buffer_address_ccc;
unsigned char *driver_main_data_buffer_address_ccc;
unsigned char *driver_table_buffer_ccc;
unsigned char *driver_command_list_buffer_ccc;
unsigned char *driver_fis_buffer_ccc;
int driver_memory_mapped_ccc;
bool always_wait_for_reset_timers_ccc;
bool dont_wait_for_drdy_ccc;
bool table_address_changed_ccc;
unsigned char table_address_backup_ccc[4];
bool ahci_address_changed_ccc;
uint64_t command_list_address_backup_ccc;
uint64_t fis_address_backup_ccc;
bool ahci_interrupt_changed_ccc;
uint32_t interrupt_backup_ccc;
bool memory_mapped_ccc;
bool memory_failed_ccc;
bool io_permissions_ccc;
int table_size_ccc;
int table_entry_count_ccc;
FILE *pagemap_ccc;
char device_driver_ccc[MAX_DEVICES][64];
char device_bus_ccc[MAX_DEVICES][64];
unsigned long long reg_address_ccc[MAX_DEVICES];
unsigned long long control_address_ccc[MAX_DEVICES];
unsigned long long bus_address_ccc[MAX_DEVICES];
unsigned long long hba_address_ccc[MAX_DEVICES];
int port_number_ccc[MAX_DEVICES];
unsigned int port_status_ccc[MAX_DEVICES];
unsigned long long port_address_ccc[MAX_DEVICES];
unsigned int port_signature_ccc[MAX_DEVICES];
bool device_select_ccc[MAX_DEVICES];
bool device_present_ccc[MAX_DEVICES];
bool device_visable_ccc[MAX_DEVICES];
char device_reference_ccc[MAX_DEVICES][32];
char device_name_ccc[MAX_DEVICES][32];
char model_ccc[MAX_DEVICES][41];
char serial_ccc[MAX_DEVICES][21];
char drive_list_ccc[MAX_DEVICES][64];
long long drive_size_ccc[MAX_DEVICES];
int drive_bytes_per_sector_ccc[MAX_DEVICES];
int device_type_ccc[MAX_DEVICES];
int device_count_ccc;
int choose_device_mode_ccc;
int did_reset_ccc;
bool enable_hba_reset_on_connect_source_ccc;
bool enable_hba_test_on_connect_source_ccc;
bool enable_dont_identify_on_choose_source_ccc;
bool enable_data_dump_ccc;
char data_dump_filename_ccc[1024];
bool enable_dump_identify_on_check_ccc;
int usbr1_vendor_id_ccc;
int usbr1_product_id_ccc;
char usbr1_vendor_string_ccc[256];
char usbr1_product_string_ccc[256];
char usbr1_extra_id_string_ccc[256];
int usbr1_bus_number_ccc;
int usbr1_bus_real_number_ccc;
int usbr1_device_number_ccc;
int usbd1_vendor_id_ccc;
int usbd1_product_id_ccc;
char usbd1_vendor_string_ccc[256];
char usbd1_product_string_ccc[256];
char usbd1_extra_id_string_ccc[256];
int usbd1_bus_number_ccc;
int usbd1_bus_real_number_ccc;
int usbd1_device_number_ccc;
int usbd1_bulk_in_endpoint_ccc;
int usbd1_bulk_out_endpoint_ccc;
int usb_device_count_ccc;
int usb_vendor_id_ccc[MAX_USB_DEVICES];
int usb_product_id_ccc[MAX_USB_DEVICES];
char usb_vendor_string_ccc[MAX_USB_DEVICES][256];
char usb_product_string_ccc[MAX_USB_DEVICES][256];
char usb_serial_string_ccc[MAX_USB_DEVICES][256];
char usb_extra_id_string_ccc[MAX_USB_DEVICES][256];
int usb_bus_number_ccc[MAX_USB_DEVICES];
int usb_bus_real_number_ccc[MAX_USB_DEVICES];
int usb_device_number_ccc[MAX_USB_DEVICES];
int usb_known_relay_ccc[MAX_USB_DEVICES];
int usb_mass_storage_ccc[MAX_USB_DEVICES];
int usb_bulk_in_endpoint_ccc[MAX_USB_DEVICES];
int usb_bulk_out_endpoint_ccc[MAX_USB_DEVICES];
struct usb_dev_handle *usbr1_dev_handle_ccc;
struct usb_dev_handle *usbd1_dev_handle_ccc;
int new_usb_ccc;
unsigned char usb_transfer_direction_ccc;
unsigned char usb_lun_ccc;
int usb_lun_set_ccc;
uint32_t usb_tag_ccc;
uint32_t usb_transfer_length_ccc;
unsigned char usb_csw_status_ccc;
uint32_t usb_csw_residue_ccc;
int usb_read_residue_ccc;
int usb_csw_valid_ccc;
int usb_bulk_reset_enabled_ccc;
int usb_soft_reset_enabled_ccc;
int usb_hard_reset_enabled_ccc;
int usb_port_reset_enabled_ccc;
bool use_rebuild_assist_ccc;
bool rebuild_assist_data_valid_ccc;
unsigned char rebuild_assist_element_length_ccc;
char rebuild_assist_element_mask_ccc[REBUILD_ASSIST_FIELD_LENGTH];
char rebuild_assist_working_mask_ccc[REBUILD_ASSIST_FIELD_LENGTH];
passthrough_ccc_type passthrough_ccc;
driver_control_data_ccc_type driver_control_data_ccc;
ncq_error_log_data_ccc_type ncq_error_log_data_ccc;
rebuild_assist_log_data_ccc_type rebuild_assist_log_data_ccc;
unsigned int total_script_lines_ccc;
char **script_line_pointer_ccc;
#include "strncpy_wrapper.h"
int message_exit_ccc(char *message)
{
if (debug_ccc > 0)
{
fprintf (debug_file_ccc, "%s", message);
}
strncat(exit_message_ccc, message, EXIT_MESSAGE_SIZE - strlen(exit_message_ccc) - 1);
return 0;
}
int message_error_ccc(char *message)
{
if (debug_ccc > 0)
{
fprintf (debug_file_ccc, "%s", message);
}
strncat(error_message_ccc, message, ERROR_MESSAGE_SIZE - strlen(error_message_ccc) - 1);
return 0;
}
int clear_error_message_ccc(void)
{
strcpy(error_message_ccc, "\0");
return 0;
}
int message_display_ccc(char *message)
{
if (debug_ccc & DEBUG18)
{
fprintf (debug_file_ccc, "%s", message);
}
strncat(display_message_ccc, message, DISPLAY_MESSAGE_SIZE - strlen(display_message_ccc) - 1);
if (strlen(message) + strlen(display_message_ccc) < DISPLAY_MESSAGE_SIZE -1)
{
//strcat(display_message_ccc, message);
}
return 0;
}
int message_now_ccc(char *message)
{
fprintf (stdout, "%s", message);
if (debug_ccc)
{
get_the_time_ccc();
fprintf (debug_file_ccc, "%s %s", current_time_ccc, message);
}
return 0;
}
int message_console_log_ccc(char *message, unsigned long long level)
{
get_the_time_ccc();
if (level == 0 || (level & verbose_ccc))
{
fprintf (stdout, "%s %s", current_time_ccc, message);
if (debug_ccc)
{
fprintf (debug_file_ccc, "%s %s", current_time_ccc, message);
}
if (enable_data_dump_ccc || enable_dump_identify_on_check_ccc)
{
char temp_string[1024];
snprintf(temp_string, sizeof(temp_string), "%s %s", current_time_ccc, message);
dump_info_to_filename_ccc (data_dump_filename_ccc, temp_string);
}
}
return 0;
}
int message_debug_ccc(char *message, unsigned long long level)
{
get_the_time_ccc();
if (debug_ccc & level)
{
fprintf (debug_file_ccc, "%s %s", current_time_ccc, message);
}
if (verbose_ccc & level)
{
fprintf (stdout, "%s %s", current_time_ccc, message);
}
return 0;
}
// function to set main buffer
int set_main_buffer_ccc(void)
{
if (usb_mode_ccc)
{
if (ccc_main_buffer_size_ccc > MAX_USB_BUFFER_SIZE || ccc_main_buffer_size_ccc > real_buffer_size_ccc)
{
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "ERROR: Maximum buffer size exceeded.\n");
message_now_ccc(tempmessage_ccc);
return (-1);
}
}
else if (ccc_main_buffer_size_ccc > MAX_BUFFER_SIZE || ccc_main_buffer_size_ccc > real_buffer_size_ccc)
{
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "ERROR: Maximum buffer size exceeded.\n");
message_now_ccc(tempmessage_ccc);
return (-1);
}
if (ahci_mode_ccc && ccc_main_buffer_size_ccc > max_dma_size_ccc)
{
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "ERROR: Maximum AHCI buffer size (%lld) exceeded.\n", max_dma_size_ccc);
message_now_ccc(tempmessage_ccc);
return (-1);
}
if (direct_mode_ccc)
{
create_dma_table_ccc();
}
return (0);
}
int create_dma_table_ccc(void)
{
// ahci mode
if (ahci_mode_ccc)
{
int max_entries = max_dma_size_ccc / pagesize_ccc;
memset (table_buffer_ccc, 0, table_size_ccc);
unsigned int write_size;
int i;
int page_count = (ccc_main_buffer_size_ccc / pagesize_ccc);
if (ccc_main_buffer_size_ccc % pagesize_ccc > 0)
{
page_count++;
}
//fprintf (stdout, "page_count2=%d\n", page_count);
table_entry_count_ccc = page_count;
for (i = 0; i < page_count; i++)
{
if (i >= max_entries)
{
table_entry_count_ccc = max_entries;
break;
}
int n = (i*16) + superbyte_ccc[15];
uint32_t dword = buffer_physical_address_ccc[i];
memcpy(table_buffer_ccc+n, &dword, 4);
memset(table_buffer_ccc+n+4, 0, 8);
// if last page get remainder of buffer size
if (i == page_count - 1)
{
if (ccc_main_buffer_size_ccc % pagesize_ccc == 0)
{
write_size = pagesize_ccc;
}
else
{
write_size = ccc_main_buffer_size_ccc % pagesize_ccc;
}
}
else
{
write_size = pagesize_ccc;
}
//fprintf (stdout, "write_size=%d\n", write_size);
dword = (write_size - 1);
memcpy(table_buffer_ccc+12+n, &dword, 4);
#ifdef DEBUG
if (debug_ccc & DEBUG33)
{
int i;
for (i = 0; i < 16; i++)
{
unsigned char *c;
c = (unsigned char *)table_buffer_ccc+i+n;
fprintf (stdout, "tb%02x ", *c);
}
fprintf (stdout, "\n");
}
#endif
}
}
//direct mode
else
{
int max_entries = max_dma_size_ccc / pagesize_ccc;
memset (table_buffer_ccc, 0, table_size_ccc);
unsigned int write_size;
unsigned char c;
int i;
int page_count = (ccc_main_buffer_size_ccc / pagesize_ccc);
if (ccc_main_buffer_size_ccc % pagesize_ccc > 0)
{
page_count++;
}
//fprintf (stdout, "page_count2=%d\n", page_count);
for (i = 0; i < page_count; i++)
{
if (i >= max_entries)
{
break;
}
c = buffer_physical_address_ccc[i];
memcpy(table_buffer_ccc+(i*8), &c, 1);
c = buffer_physical_address_ccc[i] >> 8;
memcpy(table_buffer_ccc+1+(i*8), &c, 1);
c = buffer_physical_address_ccc[i] >> 16;
memcpy(table_buffer_ccc+2+(i*8), &c, 1);
c = buffer_physical_address_ccc[i] >> 24;
memcpy(table_buffer_ccc+3+(i*8), &c, 1);
// if last page get remainder of buffer size
if (i == page_count - 1)
{
if (ccc_main_buffer_size_ccc % pagesize_ccc == 0)
{
write_size = pagesize_ccc;
}
else
{
write_size = ccc_main_buffer_size_ccc % pagesize_ccc;
}
}
else
{
write_size = pagesize_ccc;
}
//fprintf (stdout, "write_size=%d\n", write_size);
c = write_size;
memcpy(table_buffer_ccc+4+(i*8), &c, 1);
c = write_size >> 8;
memcpy(table_buffer_ccc+5+(i*8), &c, 1);
c = 0;
memcpy(table_buffer_ccc+6+(i*8), &c, 1);
// if last page then mark it
if (i == page_count - 1)
{
c = superbyte_ccc[15];
}
else
{
c = superbyte_ccc[11];
}
memcpy(table_buffer_ccc+7+(i*8), &c, 1);
#ifdef DEBUG
if (debug_ccc & DEBUG33)
{
int i;
for (i = 0; i < 8; i++)
{
unsigned char *c;
c = (unsigned char *)table_buffer_ccc+i;
fprintf (stdout, "tb%02x ", *c);
}
fprintf (stdout, "\n");
}
#endif
}
}
return (0);
}
// function to initialize table buffer
int set_table_buffer_ccc(void)
{
int multiplier = 1;
if (driver_installed_ccc && driver_memory_mapped_ccc)
{
multiplier = 4;
}
table_size_ccc = pagesize_ccc * multiplier;
long long tries = 0;
long long maxtries = 0x10000000 / table_size_ccc;
//fprintf (stdout, "maxtries=%lld\n", maxtries); //debug
while (tries < maxtries)
{
if (!driver_memory_mapped_ccc)
{
free (table_buffer_ccc);
table_buffer_ccc = NULL;
}
if (tries != 0)
{
padding_buffer_ccc = realloc(padding_buffer_ccc, table_size_ccc*tries);
}
if (driver_memory_mapped_ccc)
{
table_buffer_ccc = driver_table_buffer_ccc;
}
else
{
table_buffer_ccc = valloc(table_size_ccc);
if(!table_buffer_ccc) {
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "posix_memalign failed (%s)", strerror(errno));
message_now_ccc(tempmessage_ccc);
return (-1);
}
}
memset (table_buffer_ccc, 0, table_size_ccc);
#if UINTPTR_MAX == 0xffffffff
table_virtual_address_ccc = (unsigned int)table_buffer_ccc;
#elif UINTPTR_MAX == 0xffffffffffffffff
table_virtual_address_ccc = (unsigned long long)table_buffer_ccc;
#else
table_virtual_address_ccc = (unsigned int)table_buffer_ccc;
#endif
//fprintf (stdout, "table address=0x%llx\n", table_virtual_address_ccc); //debug
return_value_ccc = get_table_physical_memory_location_ccc();
if (return_value_ccc == 0)
{
break;
}
if (driver_memory_mapped_ccc)
{
return -1;
}
tries++;
}
//fprintf (stdout, "tries=%lld\n", tries); //debug
return (return_value_ccc);
}
int get_table_physical_memory_location_ccc(void)
{
int out_of_range = 0;
// get table buffer location
int multiplier = 1;
if (driver_installed_ccc && driver_memory_mapped_ccc)
{
multiplier = 4;
}
table_size_ccc = pagesize_ccc * multiplier;
unsigned long long file_offset = table_virtual_address_ccc / pagesize_ccc * 8;
int proc_id = getpid();
//char command[255];
//snprintf (command, sizeof(command), "cat /proc/%d/maps", proc_id);
//fprintf (stdout, "%s\n", command);
//system (command);
char path[255];
snprintf (path, sizeof(path), "/proc/%d/pagemap", proc_id);
pagemap_ccc = fopen(path, "rb");
if(!pagemap_ccc)
{
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "Error! Cannot open %s\n", path);
message_now_ccc(tempmessage_ccc);
return -1;
}
//printf("Vaddr: 0x%llx, pagesize_ccc: %d\n", table_virtual_address_ccc, pagesize_ccc);
//printf("Reading %s at 0x%llx\n", path, file_offset);
int status = fseek(pagemap_ccc, file_offset, SEEK_SET);
if(status){
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "Failed to do fseek! (%s)", strerror(errno));
message_now_ccc(tempmessage_ccc);
return -1;
}
int count = (table_size_ccc / pagesize_ccc);
if (table_size_ccc % pagesize_ccc > 0)
{
count++;
}
unsigned int read_size = 8 * count;
//fprintf (stdout, "count=%d\n", count);
//exit (0);
unsigned char file_data[read_size];
if (fread(file_data, 1, read_size, pagemap_ccc) == read_size )
{
while (read_size > 0)
{
int i;
for (i = read_size-1; i >= (int)read_size - 8; i--)
{
//fprintf (stdout, "%02x", file_data[i]);
table_physical_address_ccc = (table_physical_address_ccc << 8) + file_data[i];
}
//fprintf (stdout, "\n");
table_physical_address_ccc <<= 12;
#ifdef DEBUG
if (debug_ccc & DEBUG34)
{
fprintf (stdout, "table_physical_address=0x%llx\n", table_physical_address_ccc);
}
#endif
if (table_physical_address_ccc > 0xffffffff)
{
#ifdef DEBUG
if (debug_ccc & DEBUG34)
{
fprintf (stdout, "table_physical_address=0x%llx is out of range\n", table_physical_address_ccc);
}
#endif
out_of_range++;
}
read_size -= 8;
}
}
fclose(pagemap_ccc);
return (out_of_range);
}
// function to initialize command list buffer
int set_command_list_buffer_ccc(void)
{
command_list_size_ccc = pagesize_ccc;
long long tries = 0;
long long maxtries = 0x10000000 / command_list_size_ccc;
//fprintf (stdout, "maxtries=%lld\n", maxtries);
while (tries < maxtries)
{
if (!driver_memory_mapped_ccc)
{
free (command_list_buffer_ccc);
}
unsigned int align = pagesize_ccc;
if (tries != 0)
{
padding_buffer_ccc = realloc(padding_buffer_ccc, command_list_size_ccc*tries);
//memset (padding_buffer_ccc, 0, command_list_size_ccc*tries);
}
if (driver_memory_mapped_ccc)
{
command_list_buffer_ccc = driver_command_list_buffer_ccc;
}
else if (posix_memalign(&command_list_buffer_ccc, align, command_list_size_ccc))
{
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "posix_memalign failed (%s)", strerror(errno));
message_now_ccc(tempmessage_ccc);
return (-1);
}
memset (command_list_buffer_ccc, 0, command_list_size_ccc);
#if UINTPTR_MAX == 0xffffffff
command_list_virtual_address_ccc = (unsigned int)command_list_buffer_ccc;
#elif UINTPTR_MAX == 0xffffffffffffffff
command_list_virtual_address_ccc = (unsigned long long)command_list_buffer_ccc;
#else
command_list_virtual_address_ccc = (unsigned int)command_list_buffer_ccc;
#endif
//fprintf (stdout, "command list address=0x%llx\n", command_list_virtual_address_ccc);
return_value_ccc = get_command_list_physical_memory_location_ccc();
if (return_value_ccc == 0)
{
break;
}
if (driver_memory_mapped_ccc)
{
return (-1);
}
tries++;
}
//fprintf (stdout, "tries=%lld\n", tries);
return (return_value_ccc);
}
int get_command_list_physical_memory_location_ccc(void)
{
int out_of_range = 0;
// get command list buffer location
command_list_size_ccc = pagesize_ccc;
unsigned long long file_offset = command_list_virtual_address_ccc / pagesize_ccc * 8;
int proc_id = getpid();
//char command[255];
//snprintf (command, sizeof(command), "cat /proc/%d/maps", proc_id);
//fprintf (stdout, "%s\n", command);
//system (command);
char path[255];
snprintf (path, sizeof(path), "/proc/%d/pagemap", proc_id);
pagemap_ccc = fopen(path, "rb");
if(!pagemap_ccc)
{
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "Error! Cannot open %s\n", path);
message_now_ccc(tempmessage_ccc);
return -1;
}
//printf("Vaddr: 0x%llx, pagesize_ccc: %d\n", command_list_virtual_address_ccc, pagesize_ccc);
//printf("Reading %s at 0x%llx\n", path, file_offset);
int status = fseek(pagemap_ccc, file_offset, SEEK_SET);
if(status){
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "Failed to do fseek! (%s)", strerror(errno));
message_now_ccc(tempmessage_ccc);
return -1;
}
int count = (command_list_size_ccc / pagesize_ccc);
if (command_list_size_ccc % pagesize_ccc > 0)
{
count++;
}
unsigned int read_size = 8 * count;
//fprintf (stdout, "count=%d\n", count);
//exit (0);
unsigned char file_data[read_size];
if (fread(file_data, 1, read_size, pagemap_ccc) == read_size )
{
while (read_size > 0)
{
int i;
for (i = read_size-1; i >= (int)read_size - 8; i--)
{
//fprintf (stdout, "%02x", file_data[i]);
command_list_physical_address_ccc = (command_list_physical_address_ccc << 8) + file_data[i];
}
//fprintf (stdout, "\n");
command_list_physical_address_ccc <<= 12;
#ifdef DEBUG
if (debug_ccc & DEBUG34)
{
fprintf (stdout, "command_list_physical_address=0x%llx\n", command_list_physical_address_ccc);
}
#endif
if (command_list_physical_address_ccc > 0xffffffff)
{
#ifdef DEBUG
if (debug_ccc & DEBUG34)
{
fprintf (stdout, "command_list_physical_address=0x%llx is out of range\n", command_list_physical_address_ccc);
}
#endif
out_of_range++;
}
read_size -= 8;
}
}
fclose(pagemap_ccc);
return (out_of_range);
}
// function to initialize fis buffer
int set_fis_buffer_ccc(void)
{
int fis_size_ccc = pagesize_ccc;
long long tries = 0;
long long maxtries = 0x10000000 / fis_size_ccc;
//fprintf (stdout, "maxtries=%lld\n", maxtries);
while (tries < maxtries)
{
if (!driver_memory_mapped_ccc)
{
free (fis_buffer_ccc);
}
unsigned int align = pagesize_ccc;
if (tries != 0)
{
padding_buffer_ccc = realloc(padding_buffer_ccc, fis_size_ccc*tries);
//memset (padding_buffer_ccc, 0, command_list_size_ccc*tries);
}
if (driver_memory_mapped_ccc)
{
fis_buffer_ccc = driver_fis_buffer_ccc;
}
else if (posix_memalign(&fis_buffer_ccc, align, fis_size_ccc))
{
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "posix_memalign failed (%s)", strerror(errno));
message_now_ccc(tempmessage_ccc);
return (-1);
}
memset (fis_buffer_ccc, 0, fis_size_ccc);
#if UINTPTR_MAX == 0xffffffff
fis_virtual_address_ccc = (unsigned int)fis_buffer_ccc;
#elif UINTPTR_MAX == 0xffffffffffffffff
fis_virtual_address_ccc = (unsigned long long)fis_buffer_ccc;
#else
fis_virtual_address_ccc = (unsigned int)fis_buffer_ccc;
#endif
//fprintf (stdout, "fis address=0x%llx\n", fis_virtual_address_ccc);
return_value_ccc = get_fis_physical_memory_location_ccc();
if (return_value_ccc == 0)
{
break;
}
if (driver_memory_mapped_ccc)
{
return -1;
}
tries++;
}
//fprintf (stdout, "tries=%lld\n", tries);
return (return_value_ccc);
}
int get_fis_physical_memory_location_ccc(void)
{
int out_of_range = 0;
// get FIS buffer location
fis_size_ccc = pagesize_ccc;
unsigned long long file_offset = fis_virtual_address_ccc / pagesize_ccc * 8;
int proc_id = getpid();
//char command[255];
//snprintf (command, sizeof(command), "cat /proc/%d/maps", proc_id);
//fprintf (stdout, "%s\n", command);
//system (command);
char path[255];
snprintf (path, sizeof(path), "/proc/%d/pagemap", proc_id);
pagemap_ccc = fopen(path, "rb");
if(!pagemap_ccc)
{
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "Error! Cannot open %s\n", path);
message_now_ccc(tempmessage_ccc);
return -1;
}