2 Copyright (c) 2006, Intel Corporation
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 This function is used to get the current status of the media residing
17 in the LS-120 drive or ZIP drive. The media status is returned in the
21 pointer pointing to IDE_BLK_IO_DEV data structure, used
22 to record all the information of the IDE device.
25 The media status is achieved successfully and the media
28 @retval EFI_DEVICE_ERROR
29 Get Media Status Command is failed.
32 There is no media in the drive.
34 @retval EFI_WRITE_PROTECTED
35 The media is writing protected.
38 This function must be called after the LS120EnableMediaStatus()
39 with second parameter set to TRUE
40 (means enable media status notification) is called.
46 IN IDE_BLK_IO_DEV
*IdeDev
53 // Poll Alternate Register for BSY clear within timeout.
55 EfiStatus
= WaitForBSYClear2 (IdeDev
, ATATIMEOUT
);
56 if (EFI_ERROR (EfiStatus
)) {
57 return EFI_DEVICE_ERROR
;
61 // Select device via Device/Head Register.
63 DeviceSelect
= (UINT8
) ((IdeDev
->Device
) << 4 | 0xe0);
64 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, DeviceSelect
);
67 // Poll Alternate Register for DRDY set within timeout.
68 // After device is selected, DRDY set indicates the device is ready to
71 EfiStatus
= DRDYReady2 (IdeDev
, ATATIMEOUT
);
72 if (EFI_ERROR (EfiStatus
)) {
73 return EFI_DEVICE_ERROR
;
77 // Get Media Status Command is sent
79 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, 0xDA);
82 // BSY bit will clear after command is complete.
84 EfiStatus
= WaitForBSYClear2 (IdeDev
, ATATIMEOUT
);
85 if (EFI_ERROR (EfiStatus
)) {
86 return EFI_DEVICE_ERROR
;
90 // the media status is returned by the command in the ERROR register
92 StatusValue
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Error
);
94 if (StatusValue
& bit1
) {
98 if (StatusValue
& bit6
) {
99 return EFI_WRITE_PROTECTED
;
106 This function is used to send Enable Media Status Notification Command
107 or Disable Media Status Notification Command.
110 pointer pointing to IDE_BLK_IO_DEV data structure, used
111 to record all the information of the IDE device.
114 a flag that indicates whether enable or disable media
118 If command completes successfully.
120 @retval EFI_DEVICE_ERROR
126 LS120EnableMediaStatus (
127 IN IDE_BLK_IO_DEV
*IdeDev
,
135 // Poll Alternate Register for BSY clear within timeout.
137 Status
= WaitForBSYClear2 (IdeDev
, ATATIMEOUT
);
138 if (EFI_ERROR (Status
)) {
139 return EFI_DEVICE_ERROR
;
143 // Select device via Device/Head Register.
145 DeviceSelect
= (UINT8
) ((IdeDev
->Device
) << 4 | 0xe0);
146 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, DeviceSelect
);
149 // Poll Alternate Register for DRDY set within timeout.
150 // After device is selected, DRDY set indicates the device is ready to
153 Status
= DRDYReady2 (IdeDev
, ATATIMEOUT
);
154 if (EFI_ERROR (Status
)) {
155 return EFI_DEVICE_ERROR
;
160 // 0x95: Enable media status notification
162 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, 0x95);
165 // 0x31: Disable media status notification
167 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, 0x31);
170 // Set Feature Command is sent
172 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, 0xEF);
175 // BSY bit will clear after command is complete.
177 Status
= WaitForBSYClear (IdeDev
, ATATIMEOUT
);
178 if (EFI_ERROR (Status
)) {
179 return EFI_DEVICE_ERROR
;
186 This function is called by DiscoverIdeDevice() during its device
189 Its main purpose is to get enough information for the device media
190 to fill in the Media data structure of the Block I/O Protocol interface.
192 There are 5 steps to reach such objective:
194 1. Sends out the ATAPI Identify Command to the specified device.
195 Only ATAPI device responses to this command. If the command succeeds,
196 it returns the Identify data structure which filled with information
197 about the device. Since the ATAPI device contains removable media,
198 the only meaningful information is the device module name.
200 2. Sends out ATAPI Inquiry Packet Command to the specified device.
201 This command will return inquiry data of the device, which contains
202 the device type information.
204 3. Allocate sense data space for future use. We don't detect the media
205 presence here to improvement boot performance, especially when CD
206 media is present. The media detection will be performed just before
207 each BLK_IO read/write
210 pointer pointing to IDE_BLK_IO_DEV data structure, used
211 to record all the information of the IDE device.
214 Identify ATAPI device successfully.
216 @retval EFI_DEVICE_ERROR
217 ATAPI Identify Device Command failed or device type
218 is not supported by this IDE driver.
221 Parameter "IdeDev" will be updated in this function.
223 TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
224 TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
228 IN IDE_BLK_IO_DEV
*IdeDev
231 EFI_IDENTIFY_DATA
*AtapiIdentifyPointer
;
239 DeviceSelect
= (UINT8
) ((IdeDev
->Device
) << 4);
241 AtapiIdentifyPointer
= AllocatePool (sizeof (EFI_IDENTIFY_DATA
));
242 if (AtapiIdentifyPointer
== NULL
) {
243 return EFI_OUT_OF_RESOURCES
;
246 // Send ATAPI Identify Command to get IDENTIFY data.
248 Status
= AtaPioDataIn (
250 (VOID
*) AtapiIdentifyPointer
,
251 sizeof (EFI_IDENTIFY_DATA
),
252 ATAPI_IDENTIFY_DEVICE_CMD
,
260 if (EFI_ERROR (Status
)) {
261 gBS
->FreePool (AtapiIdentifyPointer
);
262 return EFI_DEVICE_ERROR
;
265 IdeDev
->pIdData
= AtapiIdentifyPointer
;
266 PrintAtaModuleName (IdeDev
);
269 // Send ATAPI Inquiry Packet Command to get INQUIRY data.
271 Status
= AtapiInquiry (IdeDev
);
272 if (EFI_ERROR (Status
)) {
273 gBS
->FreePool (IdeDev
->pIdData
);
275 // Make sure the pIdData will not be freed again.
277 IdeDev
->pIdData
= NULL
;
278 return EFI_DEVICE_ERROR
;
281 // Get media removable info from INQUIRY data.
283 IdeDev
->BlkIo
.Media
->RemovableMedia
= (UINT8
) ((IdeDev
->pInquiryData
->RMB
& 0x80) == 0x80);
286 // Identify device type via INQUIRY data.
288 switch (IdeDev
->pInquiryData
->peripheral_type
& 0x1f) {
296 // device is LS120 or ZIP drive.
298 IdeDev
->Type
= IdeMagnetic
;
300 IdeDev
->BlkIo
.Media
->MediaId
= 0;
302 // Give initial value
304 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
306 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
307 IdeDev
->BlkIo
.Media
->BlockSize
= 0x200;
315 IdeDev
->Type
= IdeCdRom
;
316 IdeDev
->BlkIo
.Media
->MediaId
= 0;
318 // Give initial value
320 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
322 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
323 IdeDev
->BlkIo
.Media
->BlockSize
= 0x800;
324 IdeDev
->BlkIo
.Media
->ReadOnly
= TRUE
;
343 IdeDev
->Type
= IdeUnknown
;
344 gBS
->FreePool (IdeDev
->pIdData
);
345 gBS
->FreePool (IdeDev
->pInquiryData
);
347 // Make sure the pIdData and pInquiryData will not be freed again.
349 IdeDev
->pIdData
= NULL
;
350 IdeDev
->pInquiryData
= NULL
;
351 return EFI_DEVICE_ERROR
;
355 // original sense data numbers
357 IdeDev
->SenseDataNumber
= 20;
359 IdeDev
->SenseData
= AllocatePool (IdeDev
->SenseDataNumber
* sizeof (REQUEST_SENSE_DATA
));
360 if (IdeDev
->SenseData
== NULL
) {
361 gBS
->FreePool (IdeDev
->pIdData
);
362 gBS
->FreePool (IdeDev
->pInquiryData
);
364 // Make sure the pIdData and pInquiryData will not be freed again.
366 IdeDev
->pIdData
= NULL
;
367 IdeDev
->pInquiryData
= NULL
;
368 return EFI_OUT_OF_RESOURCES
;
375 Sends out ATAPI Inquiry Packet Command to the specified device.
376 This command will return INQUIRY data of the device.
379 pointer pointing to IDE_BLK_IO_DEV data structure, used
380 to record all the information of the IDE device.
383 Inquiry command completes successfully.
385 @retval EFI_DEVICE_ERROR
386 Inquiry command failed.
389 Parameter "IdeDev" will be updated in this function.
394 IN IDE_BLK_IO_DEV
*IdeDev
397 ATAPI_PACKET_COMMAND Packet
;
399 INQUIRY_DATA
*InquiryData
;
402 // prepare command packet for the ATAPI Inquiry Packet Command.
404 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
405 Packet
.Inquiry
.opcode
= INQUIRY
;
406 Packet
.Inquiry
.page_code
= 0;
407 Packet
.Inquiry
.allocation_length
= sizeof (INQUIRY_DATA
);
409 InquiryData
= AllocatePool (sizeof (INQUIRY_DATA
));
410 if (InquiryData
== NULL
) {
411 return EFI_DEVICE_ERROR
;
415 // Send command packet and get requested Inquiry data.
417 Status
= AtapiPacketCommandIn (
420 (UINT16
*) InquiryData
,
421 sizeof (INQUIRY_DATA
),
424 if (EFI_ERROR (Status
)) {
425 gBS
->FreePool (InquiryData
);
426 return EFI_DEVICE_ERROR
;
429 IdeDev
->pInquiryData
= InquiryData
;
435 This function is used to send out ATAPI commands conforms to the
436 Packet Command with PIO Data In Protocol.
439 pointer pointing to IDE_BLK_IO_DEV data structure, used
440 to record all the information of the IDE device.
443 pointer pointing to ATAPI_PACKET_COMMAND data structure
444 which contains the contents of the command.
447 buffer contained data transferred from device to host.
450 data size in byte unit of the buffer.
453 this parameter is used to specify the timeout
454 value for the PioReadWriteData() function.
457 send out the ATAPI packet command successfully
458 and device sends data successfully.
460 @retval EFI_DEVICE_ERROR
461 the device failed to send data.
465 AtapiPacketCommandIn (
466 IN IDE_BLK_IO_DEV
*IdeDev
,
467 IN ATAPI_PACKET_COMMAND
*Packet
,
473 UINT16
*CommandIndex
;
478 // Set all the command parameters by fill related registers.
479 // Before write to all the following registers, BSY and DRQ must be 0.
481 Status
= DRQClear2 (IdeDev
, ATAPITIMEOUT
);
482 if (EFI_ERROR (Status
)) {
487 // Select device via Device/Head Register.
491 IdeDev
->IoPort
->Head
,
492 (UINT8
) ((IdeDev
->Device
<< 4) | DEFAULT_CMD
) // DEFAULT_CMD: 0xa0 (1010,0000)
498 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, 0x00);
501 // set the transfersize to MAX_ATAPI_BYTE_COUNT to let the device
502 // determine how many data should be transferred.
506 IdeDev
->IoPort
->CylinderLsb
,
507 (UINT8
) (MAX_ATAPI_BYTE_COUNT
& 0x00ff)
511 IdeDev
->IoPort
->CylinderMsb
,
512 (UINT8
) (MAX_ATAPI_BYTE_COUNT
>> 8)
516 // DEFAULT_CTL:0x0a (0000,1010)
519 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DEFAULT_CTL
);
522 // Send Packet command to inform device
523 // that the following data bytes are command packet.
525 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, PACKET_CMD
);
527 Status
= DRQReady (IdeDev
, ATAPITIMEOUT
);
528 if (EFI_ERROR (Status
)) {
533 // Send out command packet
535 CommandIndex
= Packet
->Data16
;
536 for (Count
= 0; Count
< 6; Count
++, CommandIndex
++) {
538 IDEWritePortW (IdeDev
->PciIo
, IdeDev
->IoPort
->Data
, *CommandIndex
);
543 // call PioReadWriteData() function to get
544 // requested transfer data form device.
546 return PioReadWriteData (IdeDev
, Buffer
, ByteCount
, 1, TimeOut
);
550 This function is used to send out ATAPI commands conforms to the
551 Packet Command with PIO Data Out Protocol.
554 pointer pointing to IDE_BLK_IO_DEV data structure, used
555 to record all the information of the IDE device.
558 pointer pointing to ATAPI_PACKET_COMMAND data structure
559 which contains the contents of the command.
562 buffer contained data transferred from host to device.
565 data size in byte unit of the buffer.
568 this parameter is used to specify the timeout
569 value for the PioReadWriteData() function.
572 send out the ATAPI packet command successfully
573 and device received data successfully.
575 @retval EFI_DEVICE_ERROR
576 the device failed to send data.
580 AtapiPacketCommandOut (
581 IN IDE_BLK_IO_DEV
*IdeDev
,
582 IN ATAPI_PACKET_COMMAND
*Packet
,
588 UINT16
*CommandIndex
;
593 // set all the command parameters
594 // Before write to all the following registers, BSY and DRQ must be 0.
596 Status
= DRQClear2 (IdeDev
, ATAPITIMEOUT
);
597 if (EFI_ERROR (Status
)) {
602 // Select device via Device/Head Register.
606 IdeDev
->IoPort
->Head
,
607 (UINT8
) ((IdeDev
->Device
<< 4) | DEFAULT_CMD
) // DEFAULT_CMD: 0xa0 (1010,0000)
613 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg1
.Feature
, 0x00);
616 // set the transfersize to MAX_ATAPI_BYTE_COUNT to
617 // let the device determine how many data should be transferred.
621 IdeDev
->IoPort
->CylinderLsb
,
622 (UINT8
) (MAX_ATAPI_BYTE_COUNT
& 0x00ff)
626 IdeDev
->IoPort
->CylinderMsb
,
627 (UINT8
) (MAX_ATAPI_BYTE_COUNT
>> 8)
631 // DEFAULT_CTL:0x0a (0000,1010)
634 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.DeviceControl
, DEFAULT_CTL
);
637 // Send Packet command to inform device
638 // that the following data bytes are command packet.
640 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, PACKET_CMD
);
642 Status
= DRQReady2 (IdeDev
, ATAPITIMEOUT
);
643 if (EFI_ERROR (Status
)) {
648 // Send out command packet
650 CommandIndex
= Packet
->Data16
;
651 for (Count
= 0; Count
< 6; Count
++, CommandIndex
++) {
652 IDEWritePortW (IdeDev
->PciIo
, IdeDev
->IoPort
->Data
, *CommandIndex
);
657 // call PioReadWriteData() function to send requested transfer data to device.
659 return PioReadWriteData (IdeDev
, Buffer
, ByteCount
, 0, TimeOut
);
663 This function is called by either AtapiPacketCommandIn() or
664 AtapiPacketCommandOut(). It is used to transfer data between
665 host and device. The data direction is specified by the fourth
669 pointer pointing to IDE_BLK_IO_DEV data structure, used
670 to record all the information of the IDE device.
673 buffer contained data transferred between host and device.
676 data size in byte unit of the buffer.
679 flag used to determine the data transfer direction.
680 Read equals 1, means data transferred from device to host;
681 Read equals 0, means data transferred from host to device.
684 timeout value for wait DRQ ready before each data
688 data is transferred successfully.
690 @retval EFI_DEVICE_ERROR
691 the device failed to transfer data.
696 IN IDE_BLK_IO_DEV
*IdeDev
,
704 // required transfer data in word unit.
706 UINT32 RequiredWordCount
;
709 // actual transfer data in word unit.
711 UINT32 ActualWordCount
;
717 // containing status byte read from Status Register.
719 UINT8 StatusRegister
;
722 // No data transfer is premitted.
724 if (ByteCount
== 0) {
728 // for performance, we assert the ByteCount is an even number
729 // which is actually a resonable assumption
730 ASSERT((ByteCount
%2) == 0);
733 RequiredWordCount
= ByteCount
/ 2;
735 // ActuralWordCount means the word count of data really transferred.
739 while (ActualWordCount
< RequiredWordCount
) {
742 // before each data transfer stream, the host should poll DRQ bit ready,
743 // to see whether indicates device is ready to transfer data.
745 Status
= DRQReady2 (IdeDev
, TimeOut
);
746 if (EFI_ERROR (Status
)) {
747 return CheckErrorStatus (IdeDev
);
751 // read Status Register will clear interrupt
753 StatusRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
756 // get current data transfer size from Cylinder Registers.
760 (IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
) << 8) |
761 IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
)
765 WordCount
= EFI_MIN (WordCount
, (RequiredWordCount
- ActualWordCount
));
768 IDEReadPortWMultiple (
770 IdeDev
->IoPort
->Data
,
775 IDEWritePortWMultiple (
777 IdeDev
->IoPort
->Data
,
783 PtrBuffer
+= WordCount
;
784 ActualWordCount
+= WordCount
;
788 // After data transfer is completed, normally, DRQ bit should clear.
790 Status
= DRQClear2 (IdeDev
, ATAPITIMEOUT
);
791 if (EFI_ERROR (Status
)) {
792 return EFI_DEVICE_ERROR
;
796 // read status register to check whether error happens.
798 return CheckErrorStatus (IdeDev
);
802 Sends out ATAPI Test Unit Ready Packet Command to the specified device
803 to find out whether device is accessible.
806 pointer pointing to IDE_BLK_IO_DEV data structure, used
807 to record all the information of the IDE device.
810 device is accessible.
812 @retval EFI_DEVICE_ERROR
813 device is not accessible.
818 IN IDE_BLK_IO_DEV
*IdeDev
821 ATAPI_PACKET_COMMAND Packet
;
825 // fill command packet
827 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
828 Packet
.TestUnitReady
.opcode
= TEST_UNIT_READY
;
831 // send command packet
833 Status
= AtapiPacketCommandIn (IdeDev
, &Packet
, NULL
, 0, ATAPITIMEOUT
);
838 Sends out ATAPI Request Sense Packet Command to the specified device.
839 This command will return all the current Sense data in the device.
840 This function will pack all the Sense data in one single buffer.
843 pointer pointing to IDE_BLK_IO_DEV data structure, used
844 to record all the information of the IDE device.
846 @param[out] **SenseBuffers
847 allocated in this function, and freed by the calling function.
848 This buffer is used to accommodate all the sense data returned
852 record the unit size of the sense data block in the SenseBuffers,
854 @param[out] *BufNumbers
855 record the number of units in the SenseBuffers.
858 Request Sense command completes successfully.
860 @retval EFI_DEVICE_ERROR
861 Request Sense command failed.
866 IN IDE_BLK_IO_DEV
*IdeDev
,
867 OUT UINTN
*SenseCounts
871 REQUEST_SENSE_DATA
*Sense
;
873 BOOLEAN FetchSenseData
;
874 ATAPI_PACKET_COMMAND Packet
;
878 ZeroMem (IdeDev
->SenseData
, sizeof (REQUEST_SENSE_DATA
) * (IdeDev
->SenseDataNumber
));
880 // fill command packet for Request Sense Packet Command
882 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
883 Packet
.RequestSence
.opcode
= REQUEST_SENSE
;
884 Packet
.RequestSence
.allocation_length
= sizeof (REQUEST_SENSE_DATA
);
887 // initialize pointer
889 Ptr
= (UINT16
*) IdeDev
->SenseData
;
891 // request sense data from device continuously until no sense data
892 // exists in the device.
894 for (FetchSenseData
= TRUE
; FetchSenseData
;) {
896 Sense
= (REQUEST_SENSE_DATA
*) Ptr
;
899 // send out Request Sense Packet Command and get one Sense data form device
901 Status
= AtapiPacketCommandIn (
905 sizeof (REQUEST_SENSE_DATA
),
909 // failed to get Sense data
911 if (EFI_ERROR (Status
)) {
912 if (*SenseCounts
== 0) {
913 return EFI_DEVICE_ERROR
;
921 // We limit MAX sense data count to 20 in order to avoid dead loop. Some
922 // incompatible ATAPI devices don't retrive NO_SENSE when there is no media.
923 // In this case, dead loop occurs if we don't have a gatekeeper. 20 is
924 // supposed to be large enough for any ATAPI device.
926 if ((Sense
->sense_key
!= SK_NO_SENSE
) && ((*SenseCounts
) < 20)) {
928 // Ptr is word-based pointer
930 Ptr
+= sizeof (REQUEST_SENSE_DATA
) / 2;
934 // when no sense key, skip out the loop
936 FetchSenseData
= FALSE
;
944 Sends out ATAPI Read Capacity Packet Command to the specified device.
945 This command will return the information regarding the capacity of the
948 Current device status will impact device's response to the Read Capacity
949 Command. For example, if the device once reset, the Read Capacity
950 Command will fail. The Sense data record the current device status, so
951 if the Read Capacity Command failed, the Sense data must be requested
952 and be analyzed to determine if the Read Capacity Command should retry.
955 pointer pointing to IDE_BLK_IO_DEV data structure, used
956 to record all the information of the IDE device.
959 Read Capacity Command finally completes successfully.
961 @retval EFI_DEVICE_ERROR
962 Read Capacity Command failed because of device error.
965 parameter "IdeDev" will be updated in this function.
967 TODO: EFI_NOT_READY - add return value to function comment
971 IN IDE_BLK_IO_DEV
*IdeDev
975 // status returned by Read Capacity Packet Command
978 ATAPI_PACKET_COMMAND Packet
;
981 // used for capacity data returned from ATAPI device
983 READ_CAPACITY_DATA Data
;
984 READ_FORMAT_CAPACITY_DATA FormatData
;
986 ZeroMem (&Data
, sizeof (Data
));
987 ZeroMem (&FormatData
, sizeof (FormatData
));
989 if (IdeDev
->Type
== IdeCdRom
) {
991 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
992 Packet
.Inquiry
.opcode
= READ_CAPACITY
;
993 Status
= AtapiPacketCommandIn (
997 sizeof (READ_CAPACITY_DATA
),
1003 // Type == IdeMagnetic
1005 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1006 Packet
.ReadFormatCapacity
.opcode
= READ_FORMAT_CAPACITY
;
1007 Packet
.ReadFormatCapacity
.allocation_length_lo
= 12;
1008 Status
= AtapiPacketCommandIn (
1011 (UINT16
*) &FormatData
,
1012 sizeof (READ_FORMAT_CAPACITY_DATA
),
1017 if (!EFI_ERROR (Status
)) {
1019 if (IdeDev
->Type
== IdeCdRom
) {
1021 IdeDev
->BlkIo
.Media
->LastBlock
= (Data
.LastLba3
<< 24) |
1022 (Data
.LastLba2
<< 16) |
1023 (Data
.LastLba1
<< 8) |
1026 if (IdeDev
->BlkIo
.Media
->LastBlock
!= 0) {
1028 IdeDev
->BlkIo
.Media
->BlockSize
= (Data
.BlockSize3
<< 24) |
1029 (Data
.BlockSize2
<< 16) |
1030 (Data
.BlockSize1
<< 8) |
1033 IdeDev
->BlkIo
.Media
->MediaPresent
= TRUE
;
1035 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1036 return EFI_DEVICE_ERROR
;
1039 IdeDev
->BlkIo
.Media
->ReadOnly
= TRUE
;
1042 // Because the user data portion in the sector of the Data CD supported
1045 IdeDev
->BlkIo
.Media
->BlockSize
= 0x800;
1048 if (IdeDev
->Type
== IdeMagnetic
) {
1050 if (FormatData
.DesCode
== 3) {
1051 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1052 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
1055 IdeDev
->BlkIo
.Media
->LastBlock
= (FormatData
.LastLba3
<< 24) |
1056 (FormatData
.LastLba2
<< 16) |
1057 (FormatData
.LastLba1
<< 8) |
1058 FormatData
.LastLba0
;
1059 if (IdeDev
->BlkIo
.Media
->LastBlock
!= 0) {
1060 IdeDev
->BlkIo
.Media
->LastBlock
--;
1062 IdeDev
->BlkIo
.Media
->BlockSize
= (FormatData
.BlockSize2
<< 16) |
1063 (FormatData
.BlockSize1
<< 8) |
1064 FormatData
.BlockSize0
;
1066 IdeDev
->BlkIo
.Media
->MediaPresent
= TRUE
;
1068 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1070 // Return EFI_NOT_READY operation succeeds but returned capacity is 0
1072 return EFI_NOT_READY
;
1075 IdeDev
->BlkIo
.Media
->BlockSize
= 0x200;
1084 return EFI_DEVICE_ERROR
;
1089 Used before read/write blocks from/to ATAPI device media.
1090 Since ATAPI device media is removable, it is necessary to detect
1091 whether media is present and get current present media's
1092 information, and if media has been changed, Block I/O Protocol
1093 need to be reinstalled.
1096 pointer pointing to IDE_BLK_IO_DEV data structure, used
1097 to record all the information of the IDE device.
1099 @param[out] *MediaChange
1100 return value that indicates if the media of the device has been
1104 media found successfully.
1106 @retval EFI_DEVICE_ERROR
1107 any error encounters during media detection.
1109 @retval EFI_NO_MEDIA
1113 parameter IdeDev may be updated in this function.
1118 IN IDE_BLK_IO_DEV
*IdeDev
,
1119 OUT BOOLEAN
*MediaChange
1123 EFI_STATUS ReadCapacityStatus
;
1124 EFI_BLOCK_IO_MEDIA OldMediaInfo
;
1128 UINTN MaximumRetryTimes
;
1129 UINTN ReadyWaitFactor
;
1132 // a flag used to determine whether need to perform Read Capacity command.
1134 BOOLEAN NeedReadCapacity
;
1135 BOOLEAN WriteProtected
;
1140 CopyMem (&OldMediaInfo
, IdeDev
->BlkIo
.Media
, sizeof (OldMediaInfo
));
1141 // OldMediaInfo = *(IdeDev->BlkIo.Media);
1142 *MediaChange
= FALSE
;
1143 ReadCapacityStatus
= EFI_DEVICE_ERROR
;
1146 // if there is no media, or media is not changed,
1147 // the request sense command will detect faster than read capacity command.
1148 // read capacity command can be bypassed, thus improve performance.
1152 // Test Unit Ready command is used to detect whether device is accessible,
1153 // the device will produce corresponding Sense data.
1155 for (RetryIndex
= 0; RetryIndex
< 2; RetryIndex
++) {
1157 Status
= AtapiTestUnitReady (IdeDev
);
1158 if (!EFI_ERROR (Status
)) {
1160 // skip the loop if test unit command succeeds.
1165 Status
= AtapiSoftReset (IdeDev
);
1167 if (EFI_ERROR (Status
)) {
1168 AtaSoftReset (IdeDev
);
1173 NeedReadCapacity
= TRUE
;
1176 // at most retry 5 times
1178 MaximumRetryTimes
= 5;
1181 for (RetryIndex
= 0;
1182 (RetryIndex
< RetryTimes
) && (RetryIndex
< MaximumRetryTimes
);
1185 Status
= AtapiRequestSense (IdeDev
, &SenseCounts
);
1187 if (!EFI_ERROR (Status
)) {
1189 // if first time there is no Sense Key, no need to read capacity any more
1191 if (!HaveSenseKey (IdeDev
->SenseData
, SenseCounts
) &&
1192 (IdeDev
->BlkIo
.Media
->MediaPresent
)) {
1194 if (RetryIndex
== 0) {
1195 NeedReadCapacity
= FALSE
;
1202 if (IsNoMedia (IdeDev
->SenseData
, SenseCounts
)) {
1203 NeedReadCapacity
= FALSE
;
1204 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1205 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
1210 if (IsMediaChange (IdeDev
->SenseData
, SenseCounts
)) {
1211 NeedReadCapacity
= TRUE
;
1212 IdeDev
->BlkIo
.Media
->MediaId
++;
1217 if (IsMediaError (IdeDev
->SenseData
, SenseCounts
)) {
1218 return EFI_DEVICE_ERROR
;
1224 // retry once more, if request sense command met errors.
1230 if (NeedReadCapacity
) {
1232 // at most retry 5 times
1234 MaximumRetryTimes
= 5;
1236 // initial retry twice
1239 ReadyWaitFactor
= 2;
1241 for (RetryIndex
= 0;
1242 (RetryIndex
< RetryTimes
) && (RetryIndex
< MaximumRetryTimes
);
1245 ReadCapacityStatus
= AtapiReadCapacity (IdeDev
);
1249 if (!EFI_ERROR (ReadCapacityStatus
)) {
1251 // Read Capacity succeeded
1257 if (ReadCapacityStatus
== EFI_NOT_READY
) {
1259 // If device not ready, wait here... waiting time increases by retry
1262 gBS
->Stall (ReadyWaitFactor
* 2000 * STALL_1_MILLI_SECOND
);
1272 // Other errors returned, requery sense data
1274 Status
= AtapiRequestSense (IdeDev
, &SenseCounts
);
1277 // If Request Sense data failed, reset the device and retry.
1279 if (EFI_ERROR (Status
)) {
1281 Status
= AtapiSoftReset (IdeDev
);
1284 // if ATAPI soft reset fail,
1285 // use stronger reset mechanism -- ATA soft reset.
1287 if (EFI_ERROR (Status
)) {
1288 AtaSoftReset (IdeDev
);
1300 if (IsNoMedia (IdeDev
->SenseData
, SenseCounts
)) {
1302 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1303 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
1304 return EFI_NO_MEDIA
;
1307 if (IsMediaError (IdeDev
->SenseData
, SenseCounts
)) {
1308 return EFI_DEVICE_ERROR
;
1314 if (IsMediaChange (IdeDev
->SenseData
, SenseCounts
)) {
1315 IdeDev
->BlkIo
.Media
->MediaId
++;
1318 if (!IsDriveReady (IdeDev
->SenseData
, SenseCounts
, &NeedRetry
)) {
1321 // Drive not ready: if NeedRetry, then retry once more;
1322 // else return error
1326 // Stall 1 second to wait for drive becoming ready
1328 gBS
->Stall (1000 * STALL_1_MILLI_SECOND
);
1330 // reset retry variable to zero,
1331 // to make it retry for "drive in progress of becoming ready".
1336 AtapiSoftReset (IdeDev
);
1337 return EFI_DEVICE_ERROR
;
1341 // if read capacity fail not for above reasons, retry once more
1349 // tell whether the readcapacity process is successful or not in the end
1351 if (EFI_ERROR (ReadCapacityStatus
)) {
1352 return EFI_DEVICE_ERROR
;
1357 // the following code is to check the write-protected for LS120 media
1359 if ((IdeDev
->BlkIo
.Media
->MediaPresent
) && (IdeDev
->Type
== IdeMagnetic
)) {
1361 Status
= IsLS120orZipWriteProtected (IdeDev
, &WriteProtected
);
1362 if (!EFI_ERROR (Status
)) {
1364 if (WriteProtected
) {
1366 IdeDev
->BlkIo
.Media
->ReadOnly
= TRUE
;
1369 IdeDev
->BlkIo
.Media
->ReadOnly
= FALSE
;
1375 if (IdeDev
->BlkIo
.Media
->MediaId
!= OldMediaInfo
.MediaId
) {
1377 // Media change information got from the device
1379 *MediaChange
= TRUE
;
1382 if (IdeDev
->BlkIo
.Media
->ReadOnly
!= OldMediaInfo
.ReadOnly
) {
1383 *MediaChange
= TRUE
;
1384 IdeDev
->BlkIo
.Media
->MediaId
+= 1;
1387 if (IdeDev
->BlkIo
.Media
->BlockSize
!= OldMediaInfo
.BlockSize
) {
1388 *MediaChange
= TRUE
;
1389 IdeDev
->BlkIo
.Media
->MediaId
+= 1;
1392 if (IdeDev
->BlkIo
.Media
->LastBlock
!= OldMediaInfo
.LastBlock
) {
1393 *MediaChange
= TRUE
;
1394 IdeDev
->BlkIo
.Media
->MediaId
+= 1;
1397 if (IdeDev
->BlkIo
.Media
->MediaPresent
!= OldMediaInfo
.MediaPresent
) {
1398 if (IdeDev
->BlkIo
.Media
->MediaPresent
) {
1400 // when change from no media to media present, reset the MediaId to 1.
1402 IdeDev
->BlkIo
.Media
->MediaId
= 1;
1405 // when no media, reset the MediaId to zero.
1407 IdeDev
->BlkIo
.Media
->MediaId
= 0;
1410 *MediaChange
= TRUE
;
1414 // if any change on current existing media,
1415 // the Block I/O protocol need to be reinstalled.
1418 gBS
->ReinstallProtocolInterface (
1420 &gEfiBlockIoProtocolGuid
,
1431 This function is called by the AtapiBlkIoReadBlocks() to perform
1432 read from media in block unit.
1434 The main command used to access media here is READ(10) Command.
1435 READ(10) Command requests that the ATAPI device media transfer
1436 specified data to the host. Data is transferred in block(sector)
1437 unit. The maximum number of blocks that can be transferred once is
1438 65536. This is the main difference between READ(10) and READ(12)
1439 Command. The maximum number of blocks in READ(12) is 2 power 32.
1442 pointer pointing to IDE_BLK_IO_DEV data structure, used
1443 to record all the information of the IDE device.
1446 A pointer to the destination buffer for the data.
1449 The starting logical block address to read from
1450 on the device media.
1452 @param[in] NumberOfBlocks
1453 The number of transfer data blocks.
1455 @return status is fully dependent on the return status
1456 of AtapiPacketCommandIn() function.
1461 IN IDE_BLK_IO_DEV
*IdeDev
,
1464 IN UINTN NumberOfBlocks
1468 ATAPI_PACKET_COMMAND Packet
;
1469 READ10_CMD
*Read10Packet
;
1471 UINTN BlocksRemaining
;
1481 // fill command packet for Read(10) command
1483 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1484 Read10Packet
= &Packet
.Read10
;
1485 Lba32
= (UINT32
) Lba
;
1488 BlockSize
= IdeDev
->BlkIo
.Media
->BlockSize
;
1491 // limit the data bytes that can be transferred by one Read(10) Command
1493 MaxBlock
= (UINT16
) (65536 / BlockSize
);
1495 BlocksRemaining
= NumberOfBlocks
;
1497 Status
= EFI_SUCCESS
;
1498 while (BlocksRemaining
> 0) {
1500 if (BlocksRemaining
<= MaxBlock
) {
1502 SectorCount
= (UINT16
) BlocksRemaining
;
1505 SectorCount
= MaxBlock
;
1509 // fill the Packet data structure
1512 Read10Packet
->opcode
= READ_10
;
1515 // Lba0 ~ Lba3 specify the start logical block address of the data transfer.
1516 // Lba0 is MSB, Lba3 is LSB
1518 Read10Packet
->Lba3
= (UINT8
) (Lba32
& 0xff);
1519 Read10Packet
->Lba2
= (UINT8
) (Lba32
>> 8);
1520 Read10Packet
->Lba1
= (UINT8
) (Lba32
>> 16);
1521 Read10Packet
->Lba0
= (UINT8
) (Lba32
>> 24);
1524 // TranLen0 ~ TranLen1 specify the transfer length in block unit.
1525 // TranLen0 is MSB, TranLen is LSB
1527 Read10Packet
->TranLen1
= (UINT8
) (SectorCount
& 0xff);
1528 Read10Packet
->TranLen0
= (UINT8
) (SectorCount
>> 8);
1530 ByteCount
= SectorCount
* BlockSize
;
1532 if (IdeDev
->Type
== IdeCdRom
) {
1533 TimeOut
= CDROMLONGTIMEOUT
;
1535 TimeOut
= ATAPILONGTIMEOUT
;
1538 Status
= AtapiPacketCommandIn (
1541 (UINT16
*) PtrBuffer
,
1545 if (EFI_ERROR (Status
)) {
1549 Lba32
+= SectorCount
;
1550 PtrBuffer
= (UINT8
*) PtrBuffer
+ SectorCount
* BlockSize
;
1551 BlocksRemaining
-= SectorCount
;
1558 This function is called by the AtapiBlkIoWriteBlocks() to perform
1559 write onto media in block unit.
1560 The main command used to access media here is Write(10) Command.
1561 Write(10) Command requests that the ATAPI device media transfer
1562 specified data to the host. Data is transferred in block (sector)
1563 unit. The maximum number of blocks that can be transferred once is
1567 pointer pointing to IDE_BLK_IO_DEV data structure, used
1568 to record all the information of the IDE device.
1571 A pointer to the source buffer for the data.
1574 The starting logical block address to write onto
1577 @param[in] NumberOfBlocks
1578 The number of transfer data blocks.
1580 @return status is fully dependent on the return status
1581 of AtapiPacketCommandOut() function.
1586 IN IDE_BLK_IO_DEV
*IdeDev
,
1589 IN UINTN NumberOfBlocks
1593 ATAPI_PACKET_COMMAND Packet
;
1594 READ10_CMD
*Read10Packet
;
1597 UINTN BlocksRemaining
;
1606 // fill command packet for Write(10) command
1607 // Write(10) command packet has the same data structure as
1608 // Read(10) command packet,
1609 // so here use the Read10Packet data structure
1610 // for the Write(10) command packet.
1612 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1613 Read10Packet
= &Packet
.Read10
;
1615 Lba32
= (UINT32
) Lba
;
1618 BlockSize
= IdeDev
->BlkIo
.Media
->BlockSize
;
1621 // limit the data bytes that can be transferred by one Read(10) Command
1623 MaxBlock
= (UINT16
) (65536 / BlockSize
);
1625 BlocksRemaining
= NumberOfBlocks
;
1627 Status
= EFI_SUCCESS
;
1628 while (BlocksRemaining
> 0) {
1630 if (BlocksRemaining
>= MaxBlock
) {
1631 SectorCount
= MaxBlock
;
1633 SectorCount
= (UINT16
) BlocksRemaining
;
1637 // Command code is WRITE_10.
1639 Read10Packet
->opcode
= WRITE_10
;
1642 // Lba0 ~ Lba3 specify the start logical block address of the data transfer.
1643 // Lba0 is MSB, Lba3 is LSB
1645 Read10Packet
->Lba3
= (UINT8
) (Lba32
& 0xff);
1646 Read10Packet
->Lba2
= (UINT8
) (Lba32
>> 8);
1647 Read10Packet
->Lba1
= (UINT8
) (Lba32
>> 16);
1648 Read10Packet
->Lba0
= (UINT8
) (Lba32
>> 24);
1651 // TranLen0 ~ TranLen1 specify the transfer length in block unit.
1652 // TranLen0 is MSB, TranLen is LSB
1654 Read10Packet
->TranLen1
= (UINT8
) (SectorCount
& 0xff);
1655 Read10Packet
->TranLen0
= (UINT8
) (SectorCount
>> 8);
1657 ByteCount
= SectorCount
* BlockSize
;
1659 Status
= AtapiPacketCommandOut (
1662 (UINT16
*) PtrBuffer
,
1666 if (EFI_ERROR (Status
)) {
1670 Lba32
+= SectorCount
;
1671 PtrBuffer
= ((UINT8
*) PtrBuffer
+ SectorCount
* BlockSize
);
1672 BlocksRemaining
-= SectorCount
;
1679 This function is used to implement the Soft Reset on the specified
1680 ATAPI device. Different from the AtaSoftReset(), here reset is a ATA
1681 Soft Reset Command special for ATAPI device, and it only take effects
1682 on the specified ATAPI device, not on the whole IDE bus.
1683 Since the ATAPI soft reset is needed when device is in exceptional
1684 condition (such as BSY bit is always set ), I think the Soft Reset
1685 command should be sent without waiting for the BSY clear and DRDY
1687 This function is called by IdeBlkIoReset(),
1688 a interface function of Block I/O protocol.
1691 pointer pointing to IDE_BLK_IO_DEV data structure, used
1692 to record all the information of the IDE device.
1695 Soft reset completes successfully.
1697 @retval EFI_DEVICE_ERROR
1698 Any step during the reset process is failed.
1703 IN IDE_BLK_IO_DEV
*IdeDev
1711 // for ATAPI device, no need to wait DRDY ready after device selecting.
1712 // (bit7 and bit5 are both set to 1 for backward compatibility)
1714 DeviceSelect
= (UINT8
) (((bit7
| bit5
) | (IdeDev
->Device
<< 4)));
1715 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, DeviceSelect
);
1717 Command
= ATAPI_SOFT_RESET_CMD
;
1718 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, Command
);
1721 // BSY cleared is the only status return to the host by the device
1722 // when reset is completed.
1723 // slave device needs at most 31s to clear BSY
1725 Status
= WaitForBSYClear (IdeDev
, 31000);
1726 if (EFI_ERROR (Status
)) {
1727 return EFI_DEVICE_ERROR
;
1731 // stall 5 seconds to make the device status stable
1733 gBS
->Stall (5000000);
1739 This function is the ATAPI implementation for ReadBlocks in the
1740 Block I/O Protocol interface.
1742 @param[in] *IdeBlkIoDev
1743 Indicates the calling context.
1746 The media id that the read request is for.
1749 The starting logical block address to read from
1752 @param[in] BufferSize
1753 The size of the Buffer in bytes. This must be a
1754 multiple of the intrinsic block size of the device.
1757 A pointer to the destination buffer for the data.
1758 The caller is responsible for either having implicit
1759 or explicit ownership of the memory that data is read into.
1762 Read Blocks successfully.
1764 @retval EFI_DEVICE_ERROR
1767 @retval EFI_NO_MEDIA
1768 There is no media in the device.
1770 @retval EFI_MEDIA_CHANGED
1771 The MediaId is not for the current media.
1773 @retval EFI_BAD_BUFFER_SIZE
1774 The BufferSize parameter is not a multiple of the
1775 intrinsic block size of the device.
1777 @retval EFI_INVALID_PARAMETER
1778 The read request contains LBAs that are not valid,
1779 or the data buffer is not valid.
1783 AtapiBlkIoReadBlocks (
1784 IN IDE_BLK_IO_DEV
*IdeBlkIoDevice
,
1787 IN UINTN BufferSize
,
1791 EFI_BLOCK_IO_MEDIA
*Media
;
1793 UINTN NumberOfBlocks
;
1796 BOOLEAN MediaChange
;
1798 if (Buffer
== NULL
) {
1799 return EFI_INVALID_PARAMETER
;
1802 if (BufferSize
== 0) {
1807 // ATAPI device media is removable, so it is a must
1808 // to detect media first before read operation
1810 MediaChange
= FALSE
;
1811 Status
= AtapiDetectMedia (IdeBlkIoDevice
, &MediaChange
);
1812 if (EFI_ERROR (Status
)) {
1814 if (IdeBlkIoDevice
->Cache
!= NULL
) {
1815 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1816 IdeBlkIoDevice
->Cache
= NULL
;
1822 // Get the intrinsic block size
1824 Media
= IdeBlkIoDevice
->BlkIo
.Media
;
1825 BlockSize
= Media
->BlockSize
;
1827 NumberOfBlocks
= BufferSize
/ BlockSize
;
1829 if (!(Media
->MediaPresent
)) {
1831 if (IdeBlkIoDevice
->Cache
!= NULL
) {
1832 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1833 IdeBlkIoDevice
->Cache
= NULL
;
1835 return EFI_NO_MEDIA
;
1839 if ((MediaId
!= Media
->MediaId
) || MediaChange
) {
1841 if (IdeBlkIoDevice
->Cache
!= NULL
) {
1842 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1843 IdeBlkIoDevice
->Cache
= NULL
;
1845 return EFI_MEDIA_CHANGED
;
1848 if (BufferSize
% BlockSize
!= 0) {
1849 return EFI_BAD_BUFFER_SIZE
;
1852 if (LBA
> Media
->LastBlock
) {
1853 return EFI_INVALID_PARAMETER
;
1856 if ((LBA
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
1857 return EFI_INVALID_PARAMETER
;
1860 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
1861 return EFI_INVALID_PARAMETER
;
1865 // if all the parameters are valid, then perform read sectors command
1866 // to transfer data from device to host.
1868 Status
= AtapiReadSectors (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1869 if (EFI_ERROR (Status
)) {
1870 return EFI_DEVICE_ERROR
;
1874 // Read blocks succeeded
1878 // save the first block to the cache for performance
1880 if (LBA
== 0 && !IdeBlkIoDevice
->Cache
) {
1881 IdeBlkIoDevice
->Cache
= AllocatePool (BlockSize
);
1882 if (IdeBlkIoDevice
!= NULL
) {
1883 CopyMem ((UINT8
*) IdeBlkIoDevice
->Cache
, (UINT8
*) Buffer
, BlockSize
);
1892 This function is the ATAPI implementation for WriteBlocks in the
1893 Block I/O Protocol interface.
1896 Indicates the calling context.
1899 The media id that the write request is for.
1902 The starting logical block address to write onto
1905 @param[in] BufferSize
1906 The size of the Buffer in bytes. This must be a
1907 multiple of the intrinsic block size of the device.
1910 A pointer to the source buffer for the data.
1911 The caller is responsible for either having implicit
1912 or explicit ownership of the memory that data is
1916 Write Blocks successfully.
1918 @retval EFI_DEVICE_ERROR
1919 Write Blocks failed.
1921 @retval EFI_NO_MEDIA
1922 There is no media in the device.
1924 @retval EFI_MEDIA_CHANGE
1925 The MediaId is not for the current media.
1927 @retval EFI_BAD_BUFFER_SIZE
1928 The BufferSize parameter is not a multiple of the
1929 intrinsic block size of the device.
1931 @retval EFI_INVALID_PARAMETER
1932 The write request contains LBAs that are not valid,
1933 or the data buffer is not valid.
1935 TODO: EFI_MEDIA_CHANGED - add return value to function comment
1936 TODO: EFI_WRITE_PROTECTED - add return value to function comment
1939 AtapiBlkIoWriteBlocks (
1940 IN IDE_BLK_IO_DEV
*IdeBlkIoDevice
,
1943 IN UINTN BufferSize
,
1948 EFI_BLOCK_IO_MEDIA
*Media
;
1950 UINTN NumberOfBlocks
;
1952 BOOLEAN MediaChange
;
1954 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1955 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1956 IdeBlkIoDevice
->Cache
= NULL
;
1959 if (Buffer
== NULL
) {
1960 return EFI_INVALID_PARAMETER
;
1963 if (BufferSize
== 0) {
1968 // ATAPI device media is removable,
1969 // so it is a must to detect media first before write operation
1971 MediaChange
= FALSE
;
1972 Status
= AtapiDetectMedia (IdeBlkIoDevice
, &MediaChange
);
1973 if (EFI_ERROR (Status
)) {
1975 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1976 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1977 IdeBlkIoDevice
->Cache
= NULL
;
1983 // Get the intrinsic block size
1985 Media
= IdeBlkIoDevice
->BlkIo
.Media
;
1986 BlockSize
= Media
->BlockSize
;
1987 NumberOfBlocks
= BufferSize
/ BlockSize
;
1989 if (!(Media
->MediaPresent
)) {
1991 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1992 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1993 IdeBlkIoDevice
->Cache
= NULL
;
1995 return EFI_NO_MEDIA
;
1998 if ((MediaId
!= Media
->MediaId
) || MediaChange
) {
2000 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
2001 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
2002 IdeBlkIoDevice
->Cache
= NULL
;
2004 return EFI_MEDIA_CHANGED
;
2007 if (Media
->ReadOnly
) {
2008 return EFI_WRITE_PROTECTED
;
2011 if (BufferSize
% BlockSize
!= 0) {
2012 return EFI_BAD_BUFFER_SIZE
;
2015 if (LBA
> Media
->LastBlock
) {
2016 return EFI_INVALID_PARAMETER
;
2019 if ((LBA
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
2020 return EFI_INVALID_PARAMETER
;
2023 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
2024 return EFI_INVALID_PARAMETER
;
2028 // if all the parameters are valid,
2029 // then perform write sectors command to transfer data from host to device.
2031 Status
= AtapiWriteSectors (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
2032 if (EFI_ERROR (Status
)) {
2033 return EFI_DEVICE_ERROR
;
2041 // The following functions are a set of helper functions,
2042 // which are used to parse sense key returned by the device.
2046 TODO: Add function description
2048 @param SenseData TODO: add argument description
2049 @param SenseCounts TODO: add argument description
2051 TODO: add return values
2056 IN REQUEST_SENSE_DATA
*SenseData
,
2057 IN UINTN SenseCounts
2060 REQUEST_SENSE_DATA
*SensePointer
;
2065 SensePointer
= SenseData
;
2067 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2069 // Sense Key is SK_NOT_READY (0x2),
2070 // Additional Sense Code is ASC_NO_MEDIA (0x3A)
2072 if ((SensePointer
->sense_key
== SK_NOT_READY
) &&
2073 (SensePointer
->addnl_sense_code
== ASC_NO_MEDIA
)) {
2085 Test if the device meets a media error after media changed
2087 @param[in] *SenseData
2088 pointer pointing to ATAPI device sense data list.
2089 @param[in] SenseCounts
2090 sense data number of the list
2092 @retval TRUE Device meets a media error
2093 @retval FALSE No media error
2098 IN REQUEST_SENSE_DATA
*SenseData
,
2099 IN UINTN SenseCounts
2102 REQUEST_SENSE_DATA
*SensePointer
;
2107 SensePointer
= SenseData
;
2109 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2111 switch (SensePointer
->sense_key
) {
2113 case SK_MEDIUM_ERROR
:
2115 // Sense Key is SK_MEDIUM_ERROR (0x3)
2117 switch (SensePointer
->addnl_sense_code
) {
2118 case ASC_MEDIA_ERR1
:
2119 case ASC_MEDIA_ERR2
:
2120 case ASC_MEDIA_ERR3
:
2121 case ASC_MEDIA_ERR4
:
2133 // Sense Key is SK_NOT_READY (0x2)
2135 switch (SensePointer
->addnl_sense_code
) {
2137 // Additional Sense Code is ASC_MEDIA_UPSIDE_DOWN (0x6)
2139 case ASC_MEDIA_UPSIDE_DOWN
:
2159 TODO: Add function description
2161 @param SenseData TODO: add argument description
2162 @param SenseCounts TODO: add argument description
2164 TODO: add return values
2169 IN REQUEST_SENSE_DATA
*SenseData
,
2170 IN UINTN SenseCounts
2173 REQUEST_SENSE_DATA
*SensePointer
;
2175 BOOLEAN IsMediaChange
;
2177 IsMediaChange
= FALSE
;
2178 SensePointer
= SenseData
;
2180 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2182 // Sense Key is SK_UNIT_ATTENTION (0x6)
2184 if ((SensePointer
->sense_key
== SK_UNIT_ATTENTION
) &&
2185 (SensePointer
->addnl_sense_code
== ASC_MEDIA_CHANGE
)) {
2187 IsMediaChange
= TRUE
;
2193 return IsMediaChange
;
2197 TODO: Add function description
2199 @param SenseData TODO: add argument description
2200 @param SenseCounts TODO: add argument description
2201 @param NeedRetry TODO: add argument description
2203 TODO: add return values
2208 IN REQUEST_SENSE_DATA
*SenseData
,
2209 IN UINTN SenseCounts
,
2210 OUT BOOLEAN
*NeedRetry
2213 REQUEST_SENSE_DATA
*SensePointer
;
2219 SensePointer
= SenseData
;
2221 for (Index
= 0; Index
< SenseCounts
; Index
++) {
2223 switch (SensePointer
->sense_key
) {
2227 // Sense Key is SK_NOT_READY (0x2)
2229 switch (SensePointer
->addnl_sense_code
) {
2232 // Additional Sense Code is ASC_NOT_READY (0x4)
2234 switch (SensePointer
->addnl_sense_code_qualifier
) {
2235 case ASCQ_IN_PROGRESS
:
2237 // Additional Sense Code Qualifier is ASCQ_IN_PROGRESS (0x1)
2266 TODO: Add function description
2268 @param SenseData TODO: add argument description
2269 @param SenseCounts TODO: add argument description
2271 TODO: add return values
2276 IN REQUEST_SENSE_DATA
*SenseData
,
2277 IN UINTN SenseCounts
2285 // if first sense key in the Sense Data Array is SK_NO_SENSE,
2286 // it indicates there is no more sense key in the Sense Data Array.
2288 if (SenseData
->sense_key
== SK_NO_SENSE
) {
2296 TODO: Add function description
2298 @param IdeDev TODO: add argument description
2299 @param WriteProtected TODO: add argument description
2301 @retval EFI_DEVICE_ERROR TODO: Add description for return value
2302 @retval EFI_DEVICE_ERROR TODO: Add description for return value
2303 @retval EFI_SUCCESS TODO: Add description for return value
2307 IsLS120orZipWriteProtected (
2308 IN IDE_BLK_IO_DEV
*IdeDev
,
2309 OUT BOOLEAN
*WriteProtected
2314 *WriteProtected
= FALSE
;
2316 Status
= LS120EnableMediaStatus (IdeDev
, TRUE
);
2317 if (EFI_ERROR (Status
)) {
2318 return EFI_DEVICE_ERROR
;
2322 // the Get Media Status Command is only valid
2323 // if a Set Features/Enable Media Status Command has been priviously issued.
2325 if (LS120GetMediaStatus (IdeDev
) == EFI_WRITE_PROTECTED
) {
2327 *WriteProtected
= TRUE
;
2330 *WriteProtected
= FALSE
;
2334 // After Get Media Status Command completes,
2335 // Set Features/Disable Media Command should be sent.
2337 Status
= LS120EnableMediaStatus (IdeDev
, FALSE
);
2338 if (EFI_ERROR (Status
)) {
2339 return EFI_DEVICE_ERROR
;