-
Notifications
You must be signed in to change notification settings - Fork 21
/
format.c
1756 lines (1707 loc) · 76.9 KB
/
format.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
//
// Do NOT modify or remove this copyright and license
//
// Copyright (c) 2012 - 2020 Seagate Technology LLC and/or its Affiliates, All Rights Reserved
//
// This software is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// ******************************************************************************************
//
// \file format.c
// \brief This file defines the functions for performing some format unit operations
#include "format.h"
#include "logs.h"
#include "nvme_helper_func.h"
#include "platform_helper.h"
bool is_Format_Unit_Supported(tDevice *device, bool *fastFormatSupported)
{
uint8_t formatSupportData[10] = { 0 };
if (fastFormatSupported)
{
*fastFormatSupported = false;//make sure this defaults to false
}
if (device->drive_info.scsiVersion >= SCSI_VERSION_SPC_3 && SUCCESS == scsi_Report_Supported_Operation_Codes(device, false, 1, SCSI_FORMAT_UNIT_CMD, 0, 10, formatSupportData))
{
//uint16_t cdbSize = M_BytesTo2ByteValue(formatSupportData[2], formatSupportData[3]);
uint8_t supportField = formatSupportData[1] & 0x07;//only need bits 2:0
switch (supportField)
{
case 0://information not available...try again?
case 1://not supported by device server
return false;
case 3://supported in conformance with a SCSI standard
case 5://supported in a vendor specific manor
break;
case 4://reserved
case 2://reserved
default:
return false;
}
//if we made it here, then it's at least supported...not check the bit field for fast format support
if (fastFormatSupported)//make sure the pointer is valid
{
if (!(formatSupportData[7] == 0xFF && formatSupportData[8] == 0xFF))//if both these bytes are FFh, then the drive conforms to SCSI2 where this was the "interleave" field
{
if (formatSupportData[8] & 0x03)//checks that fast format bits are available for use.
{
*fastFormatSupported = true;
}
}
}
return true;
}
else if (device->drive_info.drive_type == SCSI_DRIVE && device->drive_info.interface_type == SCSI_INTERFACE)
{
//hack for old scsi drives that don't support the report supported op codes command. - TJE
return true;
}
else
{
return false;
}
}
int get_Format_Progress(tDevice *device, double *percentComplete)
{
uint8_t senseData[SPC3_SENSE_LEN] = { 0 };
*percentComplete = 0.0;
if (SUCCESS == scsi_Request_Sense_Cmd(device, false, senseData, SPC3_SENSE_LEN))
{
uint8_t senseKey = 0, asc = 0, ascq = 0, fru = 0;
get_Sense_Key_ASC_ASCQ_FRU(senseData, SPC3_SENSE_LEN, &senseKey, &asc, &ascq, &fru);
if (senseKey == SENSE_KEY_NOT_READY)
{
if (asc == 0x04 && ascq == 0x04)
{
//get progress
*percentComplete = (M_BytesTo2ByteValue(senseData[16], senseData[17]) * 100.0) / 65536.0;
return IN_PROGRESS;
}
else
{
return UNKNOWN;
}
}
else if (senseKey == SENSE_KEY_NO_ERROR)
{
return SUCCESS;
}
else
{
return FAILURE;
}
}
else
{
return FAILURE;
}
}
int show_Format_Unit_Progress(tDevice *device)
{
int ret = UNKNOWN;
double percentComplete = 0;
ret = get_Format_Progress(device, &percentComplete);
if (ret == IN_PROGRESS)
{
printf("\tFormat Unit Progress = %3.2f%% \n", percentComplete);
//add 0.005 to round up since this is what is happening in the %f print above (more or less) and
//we really don't need a call to round() to accomplish this. This is also simple enough and close enough to warn the user that the drive is not yet done
//with the format
if (percentComplete + 0.005 >= 100.0)
{
printf("\tWARNING: Even though progress reports 100%%, the sense data indicates\n");
printf("\t that a format is still in progress! Please wait an additional\n");
printf("\t 30 seconds and check again to see when the sense data no longer\n");
printf("\t indicates that a format is in progress!\n");
}
}
else if (ret == SUCCESS)
{
printf("\tFormat Unit command is not currently in progress. It is either complete or has not been run.\n");
}
else
{
printf("\tError occurred while retrieving format unit progress!\n");
}
return ret;
}
int run_Format_Unit(tDevice *device, runFormatUnitParameters formatParameters, bool pollForProgress)
{
int ret = SUCCESS;
uint8_t *dataBuf = NULL;
uint32_t dataSize = 4;//assume short list for now
bool longList = false;
bool fastFormatSupported = false;//not used yet - TJE
bool formatSupported = is_Format_Unit_Supported(device, &fastFormatSupported);
uint8_t fmtpInfo = 0;//set protection information to zero
uint8_t defectListFormat = 0;
bool initializationPattern = false;
uint32_t offset = 2;//for filling in parameter data
uint8_t patternType = 0;
//validate the input parameters first
//start with flags in the cdb
if (!formatParameters.currentBlockSize && formatParameters.newBlockSize == 0)
{
return BAD_PARAMETER;
}
if (formatParameters.gList && formatParameters.glistSize == 0)
{
return BAD_PARAMETER;
}
if (formatParameters.gList)
{
defectListFormat = formatParameters.gList[1] & 0x03;
if (formatParameters.glistSize > 0xFFFF && !longList)
{
dataSize += 4;
longList = true;
offset = 4;
}
dataSize += formatParameters.glistSize;
}
if (!formatParameters.defaultFormat && (formatParameters.pattern || formatParameters.securityInitialize))
{
initializationPattern = true;
dataSize += 4;
if (formatParameters.pattern)
{
patternType = 1;
dataSize += formatParameters.patternLength;
}
else
{
formatParameters.patternLength = 0;
}
}
//check if format is supported
if (!formatSupported || formatParameters.protectionType > 3)
{
return NOT_SUPPORTED;
}
if ((formatParameters.protectionType == 2 || formatParameters.protectionType == 3) && formatParameters.protectionIntervalExponent != 0)
{
dataSize += 4;
longList = true;
offset = 4;
}
//dataSize += 1;//adding 1 to make sure we don't go over the end of out memory
//allocate memory
dataBuf = (uint8_t*)calloc_aligned(dataSize *sizeof(uint8_t), sizeof(uint8_t), device->os_info.minimumAlignment);
if (!dataBuf)
{
return MEMORY_FAILURE;
}
//now flags that go in parameter data
//bytes 0 & 1 are the same between short and long parameter headers
dataBuf[0] = 0;//no protection field stuff to set for now
if (!formatParameters.disableImmediate)
{
dataBuf[1] = BIT1;//immediate bit
}
if (!formatParameters.defaultFormat)
{
dataBuf[1] |= BIT7;//set FOV bit
if (formatParameters.disableCertification)
{
dataBuf[1] |= BIT5;//dcrt bit
}
if (formatParameters.disablePrimaryList)
{
dataBuf[1] |= BIT6;//dpry bit
}
if (formatParameters.stopOnListError)
{
dataBuf[1] |= BIT4;//stpf bit
}
if (initializationPattern)
{
dataBuf[1] |= BIT3;//ip bit
}
}
if (!formatParameters.changeProtectionType)
{
formatParameters.protectionType = device->drive_info.currentProtectionType;
formatParameters.protectionIntervalExponent = device->drive_info.piExponent;
}
if (!(formatParameters.defaultFormat && formatParameters.disableImmediate))//Only set the fmtpInfo bit if we are sending data to the drive via parameter list...otherwise this is an illegal combination
{
//set up protection fields
switch (formatParameters.protectionType)
{
case 1://type 1
fmtpInfo = 2;//01b
//protection field usage should be left zero
break;
case 2://type 2
fmtpInfo = 3;//11b
//protection field usage should be left zero
break;
case 3://type 3
fmtpInfo = 3;//11b
dataBuf[1] |= BIT0;//001b
break;
case 0://no protection
default://don't set protection. There is a condition before this that should catch this error.
fmtpInfo = 0;
break;
}
}
if (longList)
{
if ((formatParameters.protectionType == 2 || formatParameters.protectionType == 3) && formatParameters.protectionIntervalExponent != 0)
{
//P_I_Information should be left as zero always! the exponent is in the lower nibble of this same byte
dataBuf[3] = M_Nibble0(formatParameters.protectionIntervalExponent);
}
//set defect list length
dataBuf[offset] = M_Byte3(formatParameters.glistSize);
dataBuf[offset + 1] = M_Byte2(formatParameters.glistSize);
dataBuf[offset + 2] = M_Byte1(formatParameters.glistSize);
dataBuf[offset + 3] = M_Byte0(formatParameters.glistSize);
offset += 4;
}
else
{
dataBuf[offset] = M_Byte1(formatParameters.glistSize);
dataBuf[offset + 1] = M_Byte0(formatParameters.glistSize);
offset += 2;
}
if (initializationPattern)
{
if (formatParameters.securityInitialize)
{
dataBuf[offset] |= BIT5;
}
dataBuf[offset + 1] = patternType;
dataBuf[offset + 2] = M_Byte1(formatParameters.patternLength);
dataBuf[offset + 3] = M_Byte0(formatParameters.patternLength);
offset += 4;
if (formatParameters.pattern && formatParameters.patternLength > 0)
{
//copy pattern into buffer
memcpy(&dataBuf[offset], formatParameters.pattern, formatParameters.patternLength);
offset += formatParameters.patternLength;
}
}
if (formatParameters.gList)
{
memcpy(&dataBuf[offset], formatParameters.gList, formatParameters.glistSize);
offset += formatParameters.glistSize;
}
dataSize = offset;
//if they want to change the sector size, we need to do a mode select command
if (!formatParameters.currentBlockSize || formatParameters.newMaxLBA)
{
bool modeSelect10 = true;
uint8_t modeParameterData[24] = { 0 };
//try mode sense 10 with LongLBA bit set...if that fails, try mode sense 6
if (SUCCESS != scsi_Mode_Sense_10(device, 0, 24, 0, false, true, MPC_CURRENT_VALUES, modeParameterData))
{
//try mode sense 10 without the longLBA bit now
if (SUCCESS != scsi_Mode_Sense_10(device, 0, 24, 0, false, false, MPC_CURRENT_VALUES, modeParameterData))
{
modeSelect10 = false;
//all else fails, try mode sense 6
if (SUCCESS != scsi_Mode_Sense_6(device, 0, 12, 0, false, MPC_CURRENT_VALUES, modeParameterData))
{
safe_Free_aligned(dataBuf);
return NOT_SUPPORTED;
}
}
}
//modify the BD
uint16_t blockDescriptorLength = 0;
uint8_t blockDescriptorOffset = 0;
//set the block size to the new block size and the max lba to all F's...they can change the sector size later with another command if they want to
if (modeSelect10)
{
blockDescriptorOffset = MODE_PARAMETER_HEADER_10_LEN;
blockDescriptorLength = M_BytesTo2ByteValue(modeParameterData[6], modeParameterData[7]);
}
else //mode sense 6
{
blockDescriptorOffset = MODE_PARAMETER_HEADER_6_LEN;
blockDescriptorLength = modeParameterData[3];
}
if (blockDescriptorLength == 8)
{
//short block descriptor
//set the LBA to all Fs to reset to maximum LBA of the drive
if (formatParameters.newMaxLBA)
{
formatParameters.newMaxLBA += 1;//Need to add 1 for SCSI so that this will match the -i report. If this is not done, then we end up with 1 less than the value provided.
modeParameterData[blockDescriptorOffset + 0] = M_Byte3(formatParameters.newMaxLBA);
modeParameterData[blockDescriptorOffset + 1] = M_Byte2(formatParameters.newMaxLBA);
modeParameterData[blockDescriptorOffset + 2] = M_Byte1(formatParameters.newMaxLBA);
modeParameterData[blockDescriptorOffset + 3] = M_Byte0(formatParameters.newMaxLBA);
}
else
{
modeParameterData[blockDescriptorOffset + 0] = 0xFF;
modeParameterData[blockDescriptorOffset + 1] = 0xFF;
modeParameterData[blockDescriptorOffset + 2] = 0xFF;
modeParameterData[blockDescriptorOffset + 3] = 0xFF;
}
//1 reserved byte (don't touch it)
//set logical block length in bytes 5 to 7
if (!formatParameters.currentBlockSize)
{
modeParameterData[blockDescriptorOffset + 5] = M_Byte2(formatParameters.newBlockSize);
modeParameterData[blockDescriptorOffset + 6] = M_Byte1(formatParameters.newBlockSize);
modeParameterData[blockDescriptorOffset + 7] = M_Byte0(formatParameters.newBlockSize);
}
}
else if (blockDescriptorLength == 16)
{
//long block descriptor
//set the LBA to all Fs to reset to maximum LBA of the drive
if (formatParameters.newMaxLBA)
{
formatParameters.newMaxLBA += 1;//Need to add 1 for SCSI so that this will match the -i report. If this is not done, then we end up with 1 less than the value provided.
modeParameterData[blockDescriptorOffset + 0] = M_Byte7(formatParameters.newMaxLBA);
modeParameterData[blockDescriptorOffset + 1] = M_Byte6(formatParameters.newMaxLBA);
modeParameterData[blockDescriptorOffset + 2] = M_Byte5(formatParameters.newMaxLBA);
modeParameterData[blockDescriptorOffset + 3] = M_Byte4(formatParameters.newMaxLBA);
modeParameterData[blockDescriptorOffset + 4] = M_Byte3(formatParameters.newMaxLBA);
modeParameterData[blockDescriptorOffset + 5] = M_Byte2(formatParameters.newMaxLBA);
modeParameterData[blockDescriptorOffset + 6] = M_Byte1(formatParameters.newMaxLBA);
modeParameterData[blockDescriptorOffset + 7] = M_Byte0(formatParameters.newMaxLBA);
}
else
{
modeParameterData[blockDescriptorOffset + 0] = 0xFF;
modeParameterData[blockDescriptorOffset + 1] = 0xFF;
modeParameterData[blockDescriptorOffset + 2] = 0xFF;
modeParameterData[blockDescriptorOffset + 3] = 0xFF;
modeParameterData[blockDescriptorOffset + 4] = 0xFF;
modeParameterData[blockDescriptorOffset + 5] = 0xFF;
modeParameterData[blockDescriptorOffset + 6] = 0xFF;
modeParameterData[blockDescriptorOffset + 7] = 0xFF;
}
//8 reserved bytes (don't touch them)
//set logical block length in bytes 12 to 15
if (!formatParameters.currentBlockSize)
{
modeParameterData[blockDescriptorOffset + 12] = M_Byte3(formatParameters.newBlockSize);
modeParameterData[blockDescriptorOffset + 13] = M_Byte2(formatParameters.newBlockSize);
modeParameterData[blockDescriptorOffset + 14] = M_Byte1(formatParameters.newBlockSize);
modeParameterData[blockDescriptorOffset + 15] = M_Byte0(formatParameters.newBlockSize);
}
}
else
{
//invalid block descriptor length
safe_Free_aligned(dataBuf);
return NOT_SUPPORTED;
}
//now send a mode select command
if (modeSelect10)
{
ret = scsi_Mode_Select_10(device, 24, false, true, false, modeParameterData, 24); //turning off page format bit due to reading page 0 above
}
else
{
ret = scsi_Mode_Select_6(device, 12, false, true, false, modeParameterData, 12); //turning off page format bit due to reading page 0 above
}
}
if (ret == SUCCESS)
{
uint32_t formatCommandTimeout = 15;
if (formatParameters.disableImmediate)
{
if (os_Is_Infinite_Timeout_Supported())
{
formatCommandTimeout = INFINITE_TIMEOUT_VALUE;
}
else
{
formatCommandTimeout = MAX_CMD_TIMEOUT_SECONDS;
}
}
if (device->deviceVerbosity >= VERBOSITY_DEFAULT)
{
printf("Performing SCSI drive format.\n");
printf("Depending on the format request, this could take minutes to hours or days.\n");
printf("Do not remove power or attempt other access as interrupting it may make\n");
printf("the drive unusable or require performing this command again!!\n");
}
//send the format command
if (formatParameters.defaultFormat && formatParameters.disableImmediate)
{
ret = scsi_Format_Unit(device, fmtpInfo, longList, false, formatParameters.completeList, defectListFormat, 0, NULL, 0, formatParameters.formatType, formatCommandTimeout);
}
else
{
ret = scsi_Format_Unit(device, fmtpInfo, longList, true, formatParameters.completeList, defectListFormat, 0, dataBuf, dataSize, formatParameters.formatType, formatCommandTimeout);
}
//poll for progress
if (pollForProgress && ret == SUCCESS && !formatParameters.disableImmediate)
{
double progress = 0;
uint32_t delayTimeSeconds = 300;
if (is_SSD(device))
{
delayTimeSeconds = 5;
}
switch (formatParameters.formatType)
{
case FORMAT_FAST_WRITE_NOT_REQUIRED:
case FORMAT_FAST_WRITE_REQUIRED:
delayTimeSeconds = 5;
break;
default:
break;
}
delay_Seconds(2); //2 second delay to make sure it starts (and on SSD this may be enough for it to finish immediately)
if (VERBOSITY_QUIET < device->deviceVerbosity)
{
uint8_t seconds = 0, minutes = 0, hours = 0;
convert_Seconds_To_Displayable_Time(delayTimeSeconds, NULL, NULL, &hours, &minutes, &seconds);
printf("Progress will be updated every ");
print_Time_To_Screen(NULL, NULL, &hours, &minutes, &seconds);
printf("\n");
}
while (IN_PROGRESS == get_Format_Progress(device, &progress))
{
if (VERBOSITY_QUIET < device->deviceVerbosity)
{
printf("\r\tPercent Complete: %0.02f%%", progress);
fflush(stdout);
}
delay_Seconds(delayTimeSeconds); //time set above
}
ret = get_Format_Progress(device, &progress);
if (ret == SUCCESS && progress < 100.00)
{
if (VERBOSITY_QUIET < device->deviceVerbosity)
{
printf("\r\tPercent Complete: 100.00%%\n");
fflush(stdout);
}
}
else if (VERBOSITY_QUIET < device->deviceVerbosity)
{
printf("\n");
}
}
else
{
//check if there was an invalid parameter field specifying the security initialize bit...if so, print a message and return not supported - TJE
}
}
safe_Free_aligned(dataBuf);
return ret;
}
int get_Format_Status(tDevice *device, ptrFormatStatus formatStatus)
{
int ret = SUCCESS;
if (!device || !formatStatus)
{
return BAD_PARAMETER;
}
//Need to allocate enough memory to read all parameters (0 - 5)
//4 for header
//4 + 255 for param 0
//4 + 8 for param 1
//4 + 8 for param 2
//4 + 8 for param 3
//4 + 4 for param 4
uint8_t *formatStatusPage = (uint8_t*)calloc_aligned(307, sizeof(uint8_t), device->os_info.minimumAlignment);
if (!formatStatusPage)
{
return MEMORY_FAILURE;
}
if (SUCCESS == scsi_Log_Sense_Cmd(device, false, LPC_CUMULATIVE_VALUES, LP_FORMAT_STATUS_LOG_PAGE, 0, 0, formatStatusPage, 307))
{
//TODO: Parameters will be all F's when the data is not available or new or the last format failed
if (M_GETBITRANGE(formatStatusPage[0], 5, 0) == LP_FORMAT_STATUS_LOG_PAGE && !(formatStatusPage[0] & BIT6) && formatStatusPage[0] & BIT7 && formatStatusPage[1] == 0)//make sure we got the right page!
{
//got the data, so let's loop through it.
uint16_t pageLength = M_BytesTo2ByteValue(formatStatusPage[2], formatStatusPage[3]);
uint32_t offset = 4;//start here to begin looking at the parameters
uint8_t parameterLength = 0;
bool lastFormatUnitAllFs = false, grownDefectsDuringCertificationAllFs = false, totalBlockReassignsDuringFormatAllFs = false, totalNewBlocksReassignedAllFs = false, powerOnMinutesSinceLastFormatAllFs = false;
for (; offset < (pageLength + 4U) && offset < 307U; offset += parameterLength + 4)
{
uint16_t parameterCode = M_BytesTo2ByteValue(formatStatusPage[offset + 0], formatStatusPage[offset + 1]);
parameterLength = formatStatusPage[offset + 3];
if (parameterLength == 0)
{
//stop the infinite loop before it would start
break;
}
switch (parameterCode)
{
case 0://format data out
formatStatus->lastFormatParametersValid = true;
{
uint8_t *allFs = (uint8_t*)calloc(formatStatusPage[offset + 3], sizeof(uint8_t*));
if (allFs)
{
if (memcmp(allFs, &formatStatusPage[offset + 4], formatStatusPage[offset + 3]) == 0)
{
lastFormatUnitAllFs = true;
}
safe_Free(allFs);
}
else
{
if (formatStatusPage[offset + 3] >= 4)
{
formatStatus->lastFormatData.protectionFieldUsage = M_GETBITRANGE(formatStatusPage[offset + 4], 2, 0);
formatStatus->lastFormatData.formatOptionsValid = formatStatusPage[offset + 5] & BIT7;
formatStatus->lastFormatData.disablePrimaryList = formatStatusPage[offset + 5] & BIT6;
formatStatus->lastFormatData.disableCertify = formatStatusPage[offset + 5] & BIT5;
formatStatus->lastFormatData.stopFormat = formatStatusPage[offset + 5] & BIT4;
formatStatus->lastFormatData.initializationPattern = formatStatusPage[offset + 5] & BIT3;
formatStatus->lastFormatData.obsoleteDisableSaveParameters = formatStatusPage[offset + 5] & BIT2;
formatStatus->lastFormatData.immediateResponse = formatStatusPage[offset + 5] & BIT1;
formatStatus->lastFormatData.vendorSpecific = formatStatusPage[offset + 5] & BIT0;
}
switch (formatStatusPage[offset + 3])//based on the length of the data, it may be a short or long list...
{
case 4://short list
formatStatus->lastFormatData.defectListLength = M_BytesTo2ByteValue(formatStatusPage[offset + 6], formatStatusPage[offset + 7]);
break;
case 8://long list
formatStatus->lastFormatData.isLongList = true;
formatStatus->lastFormatData.defectListLength = M_BytesTo4ByteValue(formatStatusPage[offset + 8], formatStatusPage[offset + 9], formatStatusPage[offset + 10], formatStatusPage[offset + 11]);
formatStatus->lastFormatData.p_i_information = M_Nibble1(formatStatusPage[offset + 7]);
formatStatus->lastFormatData.protectionIntervalExponent = M_Nibble0(formatStatusPage[offset + 7]);
break;
default://unknown
break;
}
}
}
break;
case 1://grown defects during certification
formatStatus->grownDefectsDuringCertificationValid = true;
formatStatus->grownDefectsDuringCertification = M_BytesTo8ByteValue(formatStatusPage[offset + 4], formatStatusPage[offset + 5], formatStatusPage[offset + 6], formatStatusPage[offset + 7], formatStatusPage[offset + 8], formatStatusPage[offset + 9], formatStatusPage[offset + 10], formatStatusPage[offset + 11]);
if (formatStatus->grownDefectsDuringCertification == UINT64_MAX)
{
grownDefectsDuringCertificationAllFs = true;
formatStatus->grownDefectsDuringCertificationValid = false;
}
break;
case 2://total blocks reassigned during format
formatStatus->totalBlockReassignsDuringFormatValid = true;
formatStatus->totalBlockReassignsDuringFormat = M_BytesTo8ByteValue(formatStatusPage[offset + 4], formatStatusPage[offset + 5], formatStatusPage[offset + 6], formatStatusPage[offset + 7], formatStatusPage[offset + 8], formatStatusPage[offset + 9], formatStatusPage[offset + 10], formatStatusPage[offset + 11]);
if (formatStatus->totalBlockReassignsDuringFormat == UINT64_MAX)
{
totalBlockReassignsDuringFormatAllFs = true;
formatStatus->totalBlockReassignsDuringFormatValid = false;
}
break;
case 3://total new blocks reassigned
formatStatus->totalNewBlocksReassignedValid = true;
formatStatus->totalNewBlocksReassigned = M_BytesTo8ByteValue(formatStatusPage[offset + 4], formatStatusPage[offset + 5], formatStatusPage[offset + 6], formatStatusPage[offset + 7], formatStatusPage[offset + 8], formatStatusPage[offset + 9], formatStatusPage[offset + 10], formatStatusPage[offset + 11]);
if (formatStatus->totalNewBlocksReassigned == UINT64_MAX)
{
totalNewBlocksReassignedAllFs = true;
formatStatus->totalNewBlocksReassignedValid = false;
}
break;
case 4://power on minutes since format
formatStatus->powerOnMinutesSinceFormatValid = true;
formatStatus->powerOnMinutesSinceFormat = M_BytesTo4ByteValue(formatStatusPage[offset + 4], formatStatusPage[offset + 5], formatStatusPage[offset + 6], formatStatusPage[offset + 7]);
if (formatStatus->powerOnMinutesSinceFormat == UINT32_MAX)
{
powerOnMinutesSinceLastFormatAllFs = true;
formatStatus->powerOnMinutesSinceFormatValid = false;
}
break;
default:
break;
}
}
formatStatus->formatParametersAllFs = false;
//TODO: should we handle setting the flag below if we didn't get all the parameters? They are marked mandatory, but that doesn't mean much since there are many times that mandatory support is missing on various deviecs.
if (lastFormatUnitAllFs && grownDefectsDuringCertificationAllFs && totalBlockReassignsDuringFormatAllFs && totalNewBlocksReassignedAllFs && powerOnMinutesSinceLastFormatAllFs)
{
formatStatus->formatParametersAllFs = true;
}
}
else
{
formatStatus->grownDefectsDuringCertificationValid = false;
formatStatus->lastFormatParametersValid = false;
formatStatus->powerOnMinutesSinceFormatValid = false;
formatStatus->totalBlockReassignsDuringFormatValid = false;
formatStatus->totalNewBlocksReassignedValid = false;
ret = NOT_SUPPORTED;
}
safe_Free_aligned(formatStatusPage);
}
else
{
formatStatus->grownDefectsDuringCertificationValid = false;
formatStatus->lastFormatParametersValid = false;
formatStatus->powerOnMinutesSinceFormatValid = false;
formatStatus->totalBlockReassignsDuringFormatValid = false;
formatStatus->totalNewBlocksReassignedValid = false;
ret = NOT_SUPPORTED;
}
return ret;
}
void show_Format_Status_Log(ptrFormatStatus formatStatus)
{
if (formatStatus)
{
printf("Format Status:\n");
if (!formatStatus->formatParametersAllFs)
{
if (formatStatus->lastFormatParametersValid)
{
printf("The last format unit was performed with the following parameters:\n");
printf("\tProtection Field Usage: %" PRIX8"h\n", formatStatus->lastFormatData.protectionFieldUsage);
if (formatStatus->lastFormatData.formatOptionsValid)
{
printf("\tFormat Options Valid\n");
if (formatStatus->lastFormatData.disablePrimaryList)
{
printf("\tPrimary List Disabled\n");
}
if (formatStatus->lastFormatData.disableCertify)
{
printf("\tCertification Disabled\n");
}
if (formatStatus->lastFormatData.stopFormat)
{
printf("\tStop format on list error\n");
}
if (formatStatus->lastFormatData.initializationPattern)
{
printf("\tInitialization Pattern provided\n");
}
}
else
{
printf("\tDefault format\n");
}
if (formatStatus->lastFormatData.obsoleteDisableSaveParameters)
{
printf("\tObsolete disable save parameters bit set\n");
}
if (formatStatus->lastFormatData.immediateResponse)
{
printf("\tImmediate Response Bit set\n");
}
if (formatStatus->lastFormatData.vendorSpecific)
{
printf("\tVendor Specific Bit set\n");
}
if (formatStatus->lastFormatData.isLongList)
{
printf("\tP_I_Information: %" PRIX8"h", formatStatus->lastFormatData.p_i_information);
printf("\tProtection Interval Exponent: %" PRIu8"\n", formatStatus->lastFormatData.protectionIntervalExponent);
}
printf("\tDefect List Length: %" PRIu32"\n", formatStatus->lastFormatData.defectListLength);
}
if (formatStatus->grownDefectsDuringCertificationValid)
{
printf("Grown Defects During Certification: %" PRIu64"\n", formatStatus->grownDefectsDuringCertification);
}
if (formatStatus->totalBlockReassignsDuringFormatValid)
{
printf("Total Block Reassigns During Format: %" PRIu64"\n", formatStatus->totalBlockReassignsDuringFormat);
}
if (formatStatus->totalNewBlocksReassignedValid)
{
printf("Total New Blocks Reassigned: %" PRIu64"\n", formatStatus->totalNewBlocksReassigned);
}
if (formatStatus->powerOnMinutesSinceFormatValid)
{
printf("Power On Minutes Since Last Format: %" PRIu32"\n", formatStatus->powerOnMinutesSinceFormat);
//convert the time to seconds, then print it in a displayable format
printf("Power On Time Since Last Format: ");
uint8_t years, days = 0, hours = 0, minutes = 0, seconds = 0;
convert_Seconds_To_Displayable_Time(formatStatus->powerOnMinutesSinceFormat * UINT32_C(60), &years, &days, &hours, &minutes, &seconds);
print_Time_To_Screen(&years, &days, &hours, &minutes, &seconds);
printf("\n");
}
}
else
{
printf("Format unit currently in progress or the last format command failed!\n");
}
}
return;
}
bool is_Set_Sector_Configuration_Supported(tDevice *device)
{
if (device->drive_info.drive_type == ATA_DRIVE)
{
uint8_t idDataLogSupportedCapabilities[LEGACY_DRIVE_SEC_SIZE] = { 0 };
if (SUCCESS == send_ATA_Read_Log_Ext_Cmd(device, ATA_LOG_IDENTIFY_DEVICE_DATA, ATA_ID_DATA_LOG_SUPPORTED_CAPABILITIES, idDataLogSupportedCapabilities, LEGACY_DRIVE_SEC_SIZE, 0))
{
uint64_t qword0 = M_BytesTo8ByteValue(idDataLogSupportedCapabilities[7], idDataLogSupportedCapabilities[6], idDataLogSupportedCapabilities[5], idDataLogSupportedCapabilities[4], idDataLogSupportedCapabilities[3], idDataLogSupportedCapabilities[2], idDataLogSupportedCapabilities[1], idDataLogSupportedCapabilities[0]);
if (qword0 & BIT63 && M_Byte2(qword0) == ATA_ID_DATA_LOG_SUPPORTED_CAPABILITIES && M_Word0(qword0) >= 0x0001)
{
uint64_t supportedCapabilitiesQWord = M_BytesTo8ByteValue(idDataLogSupportedCapabilities[15], idDataLogSupportedCapabilities[14], idDataLogSupportedCapabilities[13], idDataLogSupportedCapabilities[12], idDataLogSupportedCapabilities[11], idDataLogSupportedCapabilities[10], idDataLogSupportedCapabilities[9], idDataLogSupportedCapabilities[8]);
if (supportedCapabilitiesQWord & BIT63 && supportedCapabilitiesQWord & BIT49)//check bit63 since it should always be 1, then bit 49 for set sector configuration
{
return true;
}
else
{
return false;
}
}
}
else
{
return false;
}
}
else if (device->drive_info.drive_type == SCSI_DRIVE)
{
bool fastFormatSupported = false;
if (is_Format_Unit_Supported(device, &fastFormatSupported))
{
return fastFormatSupported;
}
else
{
return false;
}
}
else
{
return false;
}
return false;
}
#define MAX_NUMBER_SUPPORTED_SECTOR_SIZES UINT32_C(32)
uint32_t get_Number_Of_Supported_Sector_Sizes(tDevice *device)
{
if (device->drive_info.drive_type == ATA_DRIVE)
{
return MAX_NUMBER_SUPPORTED_SECTOR_SIZES;//This should be ok on ATA...we would have to pull the log and count to know for sure, but this is the max available in the log
}
else if (device->drive_info.drive_type == SCSI_DRIVE)
{
//pull the VPD page and determine how many are supported based on descriptor length and the VPD page length
uint32_t scsiSectorSizesSupported = 0;
uint8_t supportedBlockLengthsData[4] = { 0 };
if (SUCCESS == get_SCSI_VPD(device, SUPPORTED_BLOCK_LENGTHS_AND_PROTECTION_TYPES, NULL, NULL, true, supportedBlockLengthsData, 4, NULL))
{
uint16_t pageLength = M_BytesTo2ByteValue(supportedBlockLengthsData[2], supportedBlockLengthsData[3]);
scsiSectorSizesSupported = pageLength / 8;//each descriptor is 8 bytes in size
}
else
{
bool fastFormatSup = false;
//This device either doesn't support any other sector sizes, or supports legacy sector sizes...
if (is_Format_Unit_Supported(device, &fastFormatSup))
{
if (fastFormatSup)
{
scsiSectorSizesSupported = 6;//guessing
}
else
{
scsiSectorSizesSupported = 3;//guessing
}
}
else
{
//leave at zero for now
scsiSectorSizesSupported = 0;
}
}
return scsiSectorSizesSupported;
}
#if !defined (DISABLE_NVME_PASSTHROUGH)
else if (device->drive_info.drive_type == NVME_DRIVE)
{
return device->drive_info.IdentifyData.nvme.ns.nlbaf + 1;//zeros based value so add 1
}
#endif
else
{
return 0;
}
}
int ata_Get_Supported_Formats(tDevice *device, ptrSupportedFormats formats)
{
int ret = NOT_SUPPORTED;
if (is_Set_Sector_Configuration_Supported(device))
{
uint8_t sectorConfigurationLog[LEGACY_DRIVE_SEC_SIZE] = { 0 };
if (SUCCESS == send_ATA_Read_Log_Ext_Cmd(device, ATA_LOG_SECTOR_CONFIGURATION_LOG, 0, sectorConfigurationLog, LEGACY_DRIVE_SEC_SIZE, 0))
{
formats->deviceSupportsOtherFormats = true;
formats->protectionInformationSupported.deviceSupportsProtection = false;
uint32_t numberOfSizes = formats->numberOfSectorSizes;
formats->numberOfSectorSizes = 0;
for (uint16_t iter = 0, sectorSizeCounter = 0; iter < LEGACY_DRIVE_SEC_SIZE && sectorSizeCounter < numberOfSizes; iter += 16, ++sectorSizeCounter)
{
formats->sectorSizes[sectorSizeCounter].logicalBlockLength = M_BytesTo4ByteValue(sectorConfigurationLog[7 + iter], sectorConfigurationLog[6 + iter], sectorConfigurationLog[5 + iter], sectorConfigurationLog[4 + iter]) * 2;
formats->sectorSizes[sectorSizeCounter].ataSetSectorFields.descriptorCheck = M_BytesTo2ByteValue(sectorConfigurationLog[3 + iter], sectorConfigurationLog[2 + iter]);
if (formats->sectorSizes[sectorSizeCounter].logicalBlockLength > 0 && formats->sectorSizes[sectorSizeCounter].ataSetSectorFields.descriptorCheck != 0)
{
formats->sectorSizes[sectorSizeCounter].valid = true;
formats->sectorSizes[sectorSizeCounter].additionalInformationType = SECTOR_SIZE_ADDITIONAL_INFO_ATA;
if (formats->sectorSizes[sectorSizeCounter].logicalBlockLength == device->drive_info.deviceBlockSize)
{
formats->sectorSizes[sectorSizeCounter].currentFormat = true;
}
++(formats->numberOfSectorSizes);
}
formats->sectorSizes[sectorSizeCounter].ataSetSectorFields.descriptorIndex = (uint8_t)(iter / 16);
}
ret = SUCCESS;
}
else
{
ret = FAILURE;
}
}
else
{
formats->deviceSupportsOtherFormats = false;
formats->numberOfSectorSizes = 1;
formats->protectionInformationSupported.deviceSupportsProtection = false;
formats->sectorSizes[0].valid = true;
formats->sectorSizes[0].currentFormat = true;
formats->sectorSizes[0].logicalBlockLength = device->drive_info.deviceBlockSize;
formats->sectorSizes[0].additionalInformationType = SECTOR_SIZE_ADDITIONAL_INFO_NONE;
ret = SUCCESS;
}
return ret;
}
int scsi_Get_Supported_Formats(tDevice *device, ptrSupportedFormats formats)
{
int ret = NOT_SUPPORTED;
uint8_t *inquiryData = (uint8_t*)calloc_aligned(INQ_RETURN_DATA_LENGTH, sizeof(uint8_t), device->os_info.minimumAlignment);
if (!inquiryData)
{
return MEMORY_FAILURE;
}
if (SUCCESS == scsi_Inquiry(device, inquiryData, INQ_RETURN_DATA_LENGTH, 0, false, false))
{
ret = SUCCESS;
if (inquiryData[5] & BIT0)
{
formats->protectionInformationSupported.deviceSupportsProtection = true;
//now read the extended inquiry data VPD page
if (SUCCESS == scsi_Inquiry(device, inquiryData, INQ_RETURN_DATA_LENGTH, EXTENDED_INQUIRY_DATA, true, false))
{
switch (inquiryData[0] & 0x1F)
{
case 0://direct access block device
switch ((inquiryData[4] >> 3) & 0x07)//spt field
{
case 0:
formats->protectionInformationSupported.protectionType1Supported = true;
formats->protectionInformationSupported.protectionType2Supported = false;
formats->protectionInformationSupported.protectionType3Supported = false;
break;
case 1:
formats->protectionInformationSupported.protectionType1Supported = true;
formats->protectionInformationSupported.protectionType2Supported = true;
formats->protectionInformationSupported.protectionType3Supported = false;
break;
case 2:
formats->protectionInformationSupported.protectionType1Supported = false;
formats->protectionInformationSupported.protectionType2Supported = true;
formats->protectionInformationSupported.protectionType3Supported = false;
break;
case 3:
formats->protectionInformationSupported.protectionType1Supported = true;
formats->protectionInformationSupported.protectionType2Supported = false;
formats->protectionInformationSupported.protectionType3Supported = true;
break;
case 4:
formats->protectionInformationSupported.protectionType1Supported = false;
formats->protectionInformationSupported.protectionType2Supported = false;
formats->protectionInformationSupported.protectionType3Supported = true;
break;
case 5:
formats->protectionInformationSupported.protectionType1Supported = false;
formats->protectionInformationSupported.protectionType2Supported = true;
formats->protectionInformationSupported.protectionType3Supported = true;
break;
case 6:
formats->protectionInformationSupported.protectionReportedPerSectorSize = true;
break;
case 7:
formats->protectionInformationSupported.protectionType1Supported = true;
formats->protectionInformationSupported.protectionType2Supported = true;
formats->protectionInformationSupported.protectionType3Supported = true;
break;
}
break;
case 1://sequential access block device (we don't care...it's a tape drive...)
default:
break;
}
}
}
}
safe_Free_aligned(inquiryData);
bool dummyUpCommonSizes = true;
uint32_t supportedSectorSizesDataLength = 0;
get_SCSI_VPD_Page_Size(device, SUPPORTED_BLOCK_LENGTHS_AND_PROTECTION_TYPES, &supportedSectorSizesDataLength);
if (formats->protectionInformationSupported.protectionReportedPerSectorSize || supportedSectorSizesDataLength)
{
uint8_t *supportedBlockLengthsData = (uint8_t*)calloc_aligned(supportedSectorSizesDataLength, sizeof(uint8_t), device->os_info.minimumAlignment);
if (!supportedBlockLengthsData)
{
return MEMORY_FAILURE;
}
if (SUCCESS == get_SCSI_VPD(device, SUPPORTED_BLOCK_LENGTHS_AND_PROTECTION_TYPES, NULL, NULL, true, supportedBlockLengthsData, supportedSectorSizesDataLength, NULL))
{
dummyUpCommonSizes = false;
uint32_t numberOfSizes = formats->numberOfSectorSizes;
formats->numberOfSectorSizes = 0;
for (uint32_t iter = 4, sectorSizeCounter = 0; (iter + 8) < supportedSectorSizesDataLength && sectorSizeCounter < numberOfSizes; iter += 8, ++sectorSizeCounter, ++formats->numberOfSectorSizes)
{
formats->sectorSizes[sectorSizeCounter].valid = true;
formats->sectorSizes[sectorSizeCounter].logicalBlockLength = M_BytesTo4ByteValue(supportedBlockLengthsData[iter + 0], supportedBlockLengthsData[iter + 1], supportedBlockLengthsData[iter + 2], supportedBlockLengthsData[iter + 3]);
formats->sectorSizes[sectorSizeCounter].additionalInformationType = SECTOR_SIZE_ADDITIONAL_INFO_SCSI;
formats->sectorSizes[sectorSizeCounter].scsiSectorBits.piSupportBitsValid = true;
formats->sectorSizes[sectorSizeCounter].scsiSectorBits.p_i_i_sup = (bool)(supportedBlockLengthsData[iter + 4] & BIT6);
formats->sectorSizes[sectorSizeCounter].scsiSectorBits.no_pi_chk = (bool)(supportedBlockLengthsData[iter + 4] & BIT3);
formats->sectorSizes[sectorSizeCounter].scsiSectorBits.grd_chk = (bool)(supportedBlockLengthsData[iter + 4] & BIT2);
formats->sectorSizes[sectorSizeCounter].scsiSectorBits.app_chk = (bool)(supportedBlockLengthsData[iter + 4] & BIT1);
formats->sectorSizes[sectorSizeCounter].scsiSectorBits.ref_chk = (bool)(supportedBlockLengthsData[iter + 5] & BIT0);
formats->sectorSizes[sectorSizeCounter].scsiSectorBits.t0ps = (bool)(supportedBlockLengthsData[iter + 5] & BIT0);
if (supportedBlockLengthsData[iter + 5] & BIT1)
{
formats->sectorSizes[sectorSizeCounter].scsiSectorBits.t1ps = true;
formats->protectionInformationSupported.protectionType1Supported = true;
}
if (supportedBlockLengthsData[iter + 5] & BIT2)
{
formats->sectorSizes[sectorSizeCounter].scsiSectorBits.t2ps = true;
formats->protectionInformationSupported.protectionType2Supported = true;
}
if (supportedBlockLengthsData[iter + 5] & BIT3)
{
formats->sectorSizes[sectorSizeCounter].scsiSectorBits.t3ps = true;
formats->protectionInformationSupported.protectionType3Supported = true;
}
if (device->drive_info.deviceBlockSize == formats->sectorSizes[sectorSizeCounter].logicalBlockLength)