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
;
789 // In the case where the drive wants to send more data than we need to read,
790 // the DRQ bit will be set and cause delays from DRQClear2().
791 // We need to read data from the drive until it clears DRQ so we can move on.
793 AtapiReadPendingData (IdeDev
);
797 // After data transfer is completed, normally, DRQ bit should clear.
799 Status
= DRQClear2 (IdeDev
, ATAPITIMEOUT
);
800 if (EFI_ERROR (Status
)) {
801 return EFI_DEVICE_ERROR
;
805 // read status register to check whether error happens.
807 return CheckErrorStatus (IdeDev
);
811 Sends out ATAPI Test Unit Ready Packet Command to the specified device
812 to find out whether device is accessible.
814 @param[in] *IdeDev Pointer pointing to IDE_BLK_IO_DEV data structure, used
815 to record all the information of the IDE device.
816 @param[in] *SenseCount Sense count for this packet command
818 @retval EFI_SUCCESS Device is accessible.
819 @retval EFI_DEVICE_ERROR Device is not accessible.
824 IN IDE_BLK_IO_DEV
*IdeDev
,
825 OUT UINTN
*SenseCount
828 ATAPI_PACKET_COMMAND Packet
;
834 // fill command packet
836 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
837 Packet
.TestUnitReady
.opcode
= TEST_UNIT_READY
;
840 // send command packet
842 Status
= AtapiPacketCommandIn (IdeDev
, &Packet
, NULL
, 0, ATAPITIMEOUT
);
843 if (EFI_ERROR (Status
)) {
847 Status
= AtapiRequestSense (IdeDev
, SenseCount
);
848 if (EFI_ERROR (Status
)) {
857 Sends out ATAPI Request Sense Packet Command to the specified device.
858 This command will return all the current Sense data in the device.
859 This function will pack all the Sense data in one single buffer.
862 pointer pointing to IDE_BLK_IO_DEV data structure, used
863 to record all the information of the IDE device.
865 @param[out] **SenseBuffers
866 allocated in this function, and freed by the calling function.
867 This buffer is used to accommodate all the sense data returned
871 record the unit size of the sense data block in the SenseBuffers,
873 @param[out] *BufNumbers
874 record the number of units in the SenseBuffers.
877 Request Sense command completes successfully.
879 @retval EFI_DEVICE_ERROR
880 Request Sense command failed.
885 IN IDE_BLK_IO_DEV
*IdeDev
,
886 OUT UINTN
*SenseCounts
890 REQUEST_SENSE_DATA
*Sense
;
892 BOOLEAN FetchSenseData
;
893 ATAPI_PACKET_COMMAND Packet
;
897 ZeroMem (IdeDev
->SenseData
, sizeof (REQUEST_SENSE_DATA
) * (IdeDev
->SenseDataNumber
));
899 // fill command packet for Request Sense Packet Command
901 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
902 Packet
.RequestSence
.opcode
= REQUEST_SENSE
;
903 Packet
.RequestSence
.allocation_length
= sizeof (REQUEST_SENSE_DATA
);
906 // initialize pointer
908 Ptr
= (UINT16
*) IdeDev
->SenseData
;
910 // request sense data from device continuously until no sense data
911 // exists in the device.
913 for (FetchSenseData
= TRUE
; FetchSenseData
;) {
915 Sense
= (REQUEST_SENSE_DATA
*) Ptr
;
918 // send out Request Sense Packet Command and get one Sense data form device
920 Status
= AtapiPacketCommandIn (
924 sizeof (REQUEST_SENSE_DATA
),
928 // failed to get Sense data
930 if (EFI_ERROR (Status
)) {
931 if (*SenseCounts
== 0) {
932 return EFI_DEVICE_ERROR
;
940 // We limit MAX sense data count to 20 in order to avoid dead loop. Some
941 // incompatible ATAPI devices don't retrive NO_SENSE when there is no media.
942 // In this case, dead loop occurs if we don't have a gatekeeper. 20 is
943 // supposed to be large enough for any ATAPI device.
945 if ((Sense
->sense_key
!= SK_NO_SENSE
) && ((*SenseCounts
) < 20)) {
947 // Ptr is word-based pointer
949 Ptr
+= (sizeof (REQUEST_SENSE_DATA
) + 1) >> 1;
953 // when no sense key, skip out the loop
955 FetchSenseData
= FALSE
;
963 Sends out ATAPI Read Capacity Packet Command to the specified device.
964 This command will return the information regarding the capacity of the
967 Current device status will impact device's response to the Read Capacity
968 Command. For example, if the device once reset, the Read Capacity
969 Command will fail. The Sense data record the current device status, so
970 if the Read Capacity Command failed, the Sense data must be requested
971 and be analyzed to determine if the Read Capacity Command should retry.
973 @param[in] *IdeDev Pointer pointing to IDE_BLK_IO_DEV data structure, used
974 to record all the information of the IDE device.
975 @param[in] SenseCount Sense count for this packet command
977 @retval EFI_SUCCESS Read Capacity Command finally completes successfully.
978 @retval EFI_DEVICE_ERROR Read Capacity Command failed because of device error.
980 @note Parameter "IdeDev" will be updated in this function.
982 TODO: EFI_NOT_READY - add return value to function comment
986 IN IDE_BLK_IO_DEV
*IdeDev
,
987 OUT UINTN
*SenseCount
991 // status returned by Read Capacity Packet Command
994 EFI_STATUS SenseStatus
;
995 ATAPI_PACKET_COMMAND Packet
;
998 // used for capacity data returned from ATAPI device
1000 READ_CAPACITY_DATA Data
;
1001 READ_FORMAT_CAPACITY_DATA FormatData
;
1005 ZeroMem (&Data
, sizeof (Data
));
1006 ZeroMem (&FormatData
, sizeof (FormatData
));
1008 if (IdeDev
->Type
== IdeCdRom
) {
1010 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1011 Packet
.Inquiry
.opcode
= READ_CAPACITY
;
1012 Status
= AtapiPacketCommandIn (
1016 sizeof (READ_CAPACITY_DATA
),
1022 // Type == IdeMagnetic
1024 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1025 Packet
.ReadFormatCapacity
.opcode
= READ_FORMAT_CAPACITY
;
1026 Packet
.ReadFormatCapacity
.allocation_length_lo
= 12;
1027 Status
= AtapiPacketCommandIn (
1030 (UINT16
*) &FormatData
,
1031 sizeof (READ_FORMAT_CAPACITY_DATA
),
1036 if (Status
== EFI_TIMEOUT
) {
1041 SenseStatus
= AtapiRequestSense (IdeDev
, SenseCount
);
1043 if (!EFI_ERROR (SenseStatus
)) {
1045 if (!EFI_ERROR (Status
)) {
1047 if (IdeDev
->Type
== IdeCdRom
) {
1049 IdeDev
->BlkIo
.Media
->LastBlock
= (Data
.LastLba3
<< 24) |
1050 (Data
.LastLba2
<< 16) |
1051 (Data
.LastLba1
<< 8) |
1054 if (IdeDev
->BlkIo
.Media
->LastBlock
!= 0) {
1056 IdeDev
->BlkIo
.Media
->BlockSize
= (Data
.BlockSize3
<< 24) |
1057 (Data
.BlockSize2
<< 16) |
1058 (Data
.BlockSize1
<< 8) |
1061 IdeDev
->BlkIo
.Media
->MediaPresent
= TRUE
;
1063 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1064 return EFI_DEVICE_ERROR
;
1067 IdeDev
->BlkIo
.Media
->ReadOnly
= TRUE
;
1070 // Because the user data portion in the sector of the Data CD supported
1073 IdeDev
->BlkIo
.Media
->BlockSize
= 0x800;
1076 if (IdeDev
->Type
== IdeMagnetic
) {
1078 if (FormatData
.DesCode
== 3) {
1079 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1080 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
1083 IdeDev
->BlkIo
.Media
->LastBlock
= (FormatData
.LastLba3
<< 24) |
1084 (FormatData
.LastLba2
<< 16) |
1085 (FormatData
.LastLba1
<< 8) |
1086 FormatData
.LastLba0
;
1087 if (IdeDev
->BlkIo
.Media
->LastBlock
!= 0) {
1088 IdeDev
->BlkIo
.Media
->LastBlock
--;
1090 IdeDev
->BlkIo
.Media
->BlockSize
= (FormatData
.BlockSize2
<< 16) |
1091 (FormatData
.BlockSize1
<< 8) |
1092 FormatData
.BlockSize0
;
1094 IdeDev
->BlkIo
.Media
->MediaPresent
= TRUE
;
1096 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1098 // Return EFI_NOT_READY operation succeeds but returned capacity is 0
1100 return EFI_NOT_READY
;
1103 IdeDev
->BlkIo
.Media
->BlockSize
= 0x200;
1113 return EFI_DEVICE_ERROR
;
1118 Used before read/write blocks from/to ATAPI device media.
1119 Since ATAPI device media is removable, it is necessary to detect
1120 whether media is present and get current present media's
1121 information, and if media has been changed, Block I/O Protocol
1122 need to be reinstalled.
1125 pointer pointing to IDE_BLK_IO_DEV data structure, used
1126 to record all the information of the IDE device.
1128 @param[out] *MediaChange
1129 return value that indicates if the media of the device has been
1133 media found successfully.
1135 @retval EFI_DEVICE_ERROR
1136 any error encounters during media detection.
1138 @retval EFI_NO_MEDIA
1142 parameter IdeDev may be updated in this function.
1147 IN IDE_BLK_IO_DEV
*IdeDev
,
1148 OUT BOOLEAN
*MediaChange
1152 EFI_STATUS CleanStateStatus
;
1153 EFI_BLOCK_IO_MEDIA OldMediaInfo
;
1155 UINTN RetryNotReady
;
1157 SENSE_RESULT SResult
;
1158 BOOLEAN WriteProtected
;
1160 CopyMem (&OldMediaInfo
, IdeDev
->BlkIo
.Media
, sizeof (EFI_BLOCK_IO_MEDIA
));
1161 *MediaChange
= FALSE
;
1163 // Retry for SenseDeviceNotReadyNeedRetry.
1164 // Each retry takes 1s and we limit the upper boundary to
1165 // 120 times about 2 min.
1167 RetryNotReady
= 120;
1170 // Do Test Unit Ready
1177 while (RetryTimes
!= 0) {
1179 Status
= AtapiTestUnitReady (IdeDev
, &SenseCount
);
1181 if (EFI_ERROR (Status
)) {
1183 // Test Unit Ready error without sense data.
1184 // For some devices, this means there's extra data
1185 // that has not been read, so we read these extra
1186 // data out before going on.
1188 CleanStateStatus
= AtapiReadPendingData (IdeDev
);
1189 if (EFI_ERROR (CleanStateStatus
)) {
1191 // Busy wait failed, try again
1196 // Try again without counting down RetryTimes
1201 ParseSenseData (IdeDev
, SenseCount
, &SResult
);
1204 case SenseNoSenseKey
:
1205 if (IdeDev
->BlkIo
.Media
->MediaPresent
) {
1209 // Media present but the internal structure need refreshed.
1210 // Try Read Capacity
1216 case SenseDeviceNotReadyNeedRetry
:
1217 if (--RetryNotReady
== 0) {
1218 return EFI_DEVICE_ERROR
;
1220 gBS
->Stall (1000 * STALL_1_MILLI_SECOND
);
1225 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1226 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
1230 case SenseDeviceNotReadyNoRetry
:
1231 case SenseMediaError
:
1232 return EFI_DEVICE_ERROR
;
1234 case SenseMediaChange
:
1235 IdeDev
->BlkIo
.Media
->MediaId
++;
1246 return EFI_DEVICE_ERROR
;
1254 while (RetryTimes
!= 0) {
1256 Status
= AtapiReadCapacity (IdeDev
, &SenseCount
);
1258 if (EFI_ERROR (Status
)) {
1263 ParseSenseData (IdeDev
, SenseCount
, &SResult
);
1266 case SenseNoSenseKey
:
1270 case SenseDeviceNotReadyNeedRetry
:
1272 // We use Test Unit Ready to retry which
1279 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1280 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
1284 case SenseDeviceNotReadyNoRetry
:
1285 case SenseMediaError
:
1286 return EFI_DEVICE_ERROR
;
1288 case SenseMediaChange
:
1289 IdeDev
->BlkIo
.Media
->MediaId
++;
1300 return EFI_DEVICE_ERROR
;
1304 // the following code is to check the write-protected for LS120 media
1306 if ((IdeDev
->BlkIo
.Media
->MediaPresent
) && (IdeDev
->Type
== IdeMagnetic
)) {
1308 Status
= IsLS120orZipWriteProtected (IdeDev
, &WriteProtected
);
1309 if (!EFI_ERROR (Status
)) {
1311 if (WriteProtected
) {
1313 IdeDev
->BlkIo
.Media
->ReadOnly
= TRUE
;
1316 IdeDev
->BlkIo
.Media
->ReadOnly
= FALSE
;
1322 if (IdeDev
->BlkIo
.Media
->MediaId
!= OldMediaInfo
.MediaId
) {
1324 // Media change information got from the device
1326 *MediaChange
= TRUE
;
1329 if (IdeDev
->BlkIo
.Media
->ReadOnly
!= OldMediaInfo
.ReadOnly
) {
1330 *MediaChange
= TRUE
;
1331 IdeDev
->BlkIo
.Media
->MediaId
+= 1;
1334 if (IdeDev
->BlkIo
.Media
->BlockSize
!= OldMediaInfo
.BlockSize
) {
1335 *MediaChange
= TRUE
;
1336 IdeDev
->BlkIo
.Media
->MediaId
+= 1;
1339 if (IdeDev
->BlkIo
.Media
->LastBlock
!= OldMediaInfo
.LastBlock
) {
1340 *MediaChange
= TRUE
;
1341 IdeDev
->BlkIo
.Media
->MediaId
+= 1;
1344 if (IdeDev
->BlkIo
.Media
->MediaPresent
!= OldMediaInfo
.MediaPresent
) {
1345 if (IdeDev
->BlkIo
.Media
->MediaPresent
) {
1347 // when change from no media to media present, reset the MediaId to 1.
1349 IdeDev
->BlkIo
.Media
->MediaId
= 1;
1352 // when no media, reset the MediaId to zero.
1354 IdeDev
->BlkIo
.Media
->MediaId
= 0;
1357 *MediaChange
= TRUE
;
1361 // if any change on current existing media,
1362 // the Block I/O protocol need to be reinstalled.
1365 gBS
->ReinstallProtocolInterface (
1367 &gEfiBlockIoProtocolGuid
,
1373 if (IdeDev
->BlkIo
.Media
->MediaPresent
) {
1376 return EFI_NO_MEDIA
;
1381 This function is called by the AtapiBlkIoReadBlocks() to perform
1382 read from media in block unit.
1384 The main command used to access media here is READ(10) Command.
1385 READ(10) Command requests that the ATAPI device media transfer
1386 specified data to the host. Data is transferred in block(sector)
1387 unit. The maximum number of blocks that can be transferred once is
1388 65536. This is the main difference between READ(10) and READ(12)
1389 Command. The maximum number of blocks in READ(12) is 2 power 32.
1392 pointer pointing to IDE_BLK_IO_DEV data structure, used
1393 to record all the information of the IDE device.
1396 A pointer to the destination buffer for the data.
1399 The starting logical block address to read from
1400 on the device media.
1402 @param[in] NumberOfBlocks
1403 The number of transfer data blocks.
1405 @return status is fully dependent on the return status
1406 of AtapiPacketCommandIn() function.
1411 IN IDE_BLK_IO_DEV
*IdeDev
,
1414 IN UINTN NumberOfBlocks
1418 ATAPI_PACKET_COMMAND Packet
;
1419 READ10_CMD
*Read10Packet
;
1421 UINTN BlocksRemaining
;
1431 // fill command packet for Read(10) command
1433 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1434 Read10Packet
= &Packet
.Read10
;
1435 Lba32
= (UINT32
) Lba
;
1438 BlockSize
= IdeDev
->BlkIo
.Media
->BlockSize
;
1441 // limit the data bytes that can be transferred by one Read(10) Command
1445 BlocksRemaining
= NumberOfBlocks
;
1447 Status
= EFI_SUCCESS
;
1448 while (BlocksRemaining
> 0) {
1450 if (BlocksRemaining
<= MaxBlock
) {
1452 SectorCount
= (UINT16
) BlocksRemaining
;
1455 SectorCount
= MaxBlock
;
1459 // fill the Packet data structure
1462 Read10Packet
->opcode
= READ_10
;
1465 // Lba0 ~ Lba3 specify the start logical block address of the data transfer.
1466 // Lba0 is MSB, Lba3 is LSB
1468 Read10Packet
->Lba3
= (UINT8
) (Lba32
& 0xff);
1469 Read10Packet
->Lba2
= (UINT8
) (Lba32
>> 8);
1470 Read10Packet
->Lba1
= (UINT8
) (Lba32
>> 16);
1471 Read10Packet
->Lba0
= (UINT8
) (Lba32
>> 24);
1474 // TranLen0 ~ TranLen1 specify the transfer length in block unit.
1475 // TranLen0 is MSB, TranLen is LSB
1477 Read10Packet
->TranLen1
= (UINT8
) (SectorCount
& 0xff);
1478 Read10Packet
->TranLen0
= (UINT8
) (SectorCount
>> 8);
1480 ByteCount
= SectorCount
* BlockSize
;
1482 if (IdeDev
->Type
== IdeCdRom
) {
1483 TimeOut
= CDROMLONGTIMEOUT
;
1485 TimeOut
= ATAPILONGTIMEOUT
;
1488 Status
= AtapiPacketCommandIn (
1491 (UINT16
*) PtrBuffer
,
1495 if (EFI_ERROR (Status
)) {
1499 Lba32
+= SectorCount
;
1500 PtrBuffer
= (UINT8
*) PtrBuffer
+ SectorCount
* BlockSize
;
1501 BlocksRemaining
-= SectorCount
;
1508 This function is called by the AtapiBlkIoWriteBlocks() to perform
1509 write onto media in block unit.
1510 The main command used to access media here is Write(10) Command.
1511 Write(10) Command requests that the ATAPI device media transfer
1512 specified data to the host. Data is transferred in block (sector)
1513 unit. The maximum number of blocks that can be transferred once is
1517 pointer pointing to IDE_BLK_IO_DEV data structure, used
1518 to record all the information of the IDE device.
1521 A pointer to the source buffer for the data.
1524 The starting logical block address to write onto
1527 @param[in] NumberOfBlocks
1528 The number of transfer data blocks.
1530 @return status is fully dependent on the return status
1531 of AtapiPacketCommandOut() function.
1536 IN IDE_BLK_IO_DEV
*IdeDev
,
1539 IN UINTN NumberOfBlocks
1543 ATAPI_PACKET_COMMAND Packet
;
1544 READ10_CMD
*Read10Packet
;
1547 UINTN BlocksRemaining
;
1556 // fill command packet for Write(10) command
1557 // Write(10) command packet has the same data structure as
1558 // Read(10) command packet,
1559 // so here use the Read10Packet data structure
1560 // for the Write(10) command packet.
1562 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1563 Read10Packet
= &Packet
.Read10
;
1565 Lba32
= (UINT32
) Lba
;
1568 BlockSize
= IdeDev
->BlkIo
.Media
->BlockSize
;
1571 // limit the data bytes that can be transferred by one Read(10) Command
1573 MaxBlock
= (UINT16
) (65536 / BlockSize
);
1575 BlocksRemaining
= NumberOfBlocks
;
1577 Status
= EFI_SUCCESS
;
1578 while (BlocksRemaining
> 0) {
1580 if (BlocksRemaining
>= MaxBlock
) {
1581 SectorCount
= MaxBlock
;
1583 SectorCount
= (UINT16
) BlocksRemaining
;
1587 // Command code is WRITE_10.
1589 Read10Packet
->opcode
= WRITE_10
;
1592 // Lba0 ~ Lba3 specify the start logical block address of the data transfer.
1593 // Lba0 is MSB, Lba3 is LSB
1595 Read10Packet
->Lba3
= (UINT8
) (Lba32
& 0xff);
1596 Read10Packet
->Lba2
= (UINT8
) (Lba32
>> 8);
1597 Read10Packet
->Lba1
= (UINT8
) (Lba32
>> 16);
1598 Read10Packet
->Lba0
= (UINT8
) (Lba32
>> 24);
1601 // TranLen0 ~ TranLen1 specify the transfer length in block unit.
1602 // TranLen0 is MSB, TranLen is LSB
1604 Read10Packet
->TranLen1
= (UINT8
) (SectorCount
& 0xff);
1605 Read10Packet
->TranLen0
= (UINT8
) (SectorCount
>> 8);
1607 ByteCount
= SectorCount
* BlockSize
;
1609 Status
= AtapiPacketCommandOut (
1612 (UINT16
*) PtrBuffer
,
1616 if (EFI_ERROR (Status
)) {
1620 Lba32
+= SectorCount
;
1621 PtrBuffer
= ((UINT8
*) PtrBuffer
+ SectorCount
* BlockSize
);
1622 BlocksRemaining
-= SectorCount
;
1629 This function is used to implement the Soft Reset on the specified
1630 ATAPI device. Different from the AtaSoftReset(), here reset is a ATA
1631 Soft Reset Command special for ATAPI device, and it only take effects
1632 on the specified ATAPI device, not on the whole IDE bus.
1633 Since the ATAPI soft reset is needed when device is in exceptional
1634 condition (such as BSY bit is always set ), I think the Soft Reset
1635 command should be sent without waiting for the BSY clear and DRDY
1637 This function is called by IdeBlkIoReset(),
1638 a interface function of Block I/O protocol.
1641 pointer pointing to IDE_BLK_IO_DEV data structure, used
1642 to record all the information of the IDE device.
1645 Soft reset completes successfully.
1647 @retval EFI_DEVICE_ERROR
1648 Any step during the reset process is failed.
1653 IN IDE_BLK_IO_DEV
*IdeDev
1661 // for ATAPI device, no need to wait DRDY ready after device selecting.
1662 // (bit7 and bit5 are both set to 1 for backward compatibility)
1664 DeviceSelect
= (UINT8
) (((bit7
| bit5
) | (IdeDev
->Device
<< 4)));
1665 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, DeviceSelect
);
1667 Command
= ATAPI_SOFT_RESET_CMD
;
1668 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, Command
);
1671 // BSY cleared is the only status return to the host by the device
1672 // when reset is completed.
1673 // slave device needs at most 31s to clear BSY
1675 Status
= WaitForBSYClear (IdeDev
, 31000);
1676 if (EFI_ERROR (Status
)) {
1677 return EFI_DEVICE_ERROR
;
1681 // stall 5 seconds to make the device status stable
1683 gBS
->Stall (5000000);
1689 This function is the ATAPI implementation for ReadBlocks in the
1690 Block I/O Protocol interface.
1692 @param[in] *IdeBlkIoDev
1693 Indicates the calling context.
1696 The media id that the read request is for.
1699 The starting logical block address to read from
1702 @param[in] BufferSize
1703 The size of the Buffer in bytes. This must be a
1704 multiple of the intrinsic block size of the device.
1707 A pointer to the destination buffer for the data.
1708 The caller is responsible for either having implicit
1709 or explicit ownership of the memory that data is read into.
1712 Read Blocks successfully.
1714 @retval EFI_DEVICE_ERROR
1717 @retval EFI_NO_MEDIA
1718 There is no media in the device.
1720 @retval EFI_MEDIA_CHANGED
1721 The MediaId is not for the current media.
1723 @retval EFI_BAD_BUFFER_SIZE
1724 The BufferSize parameter is not a multiple of the
1725 intrinsic block size of the device.
1727 @retval EFI_INVALID_PARAMETER
1728 The read request contains LBAs that are not valid,
1729 or the data buffer is not valid.
1733 AtapiBlkIoReadBlocks (
1734 IN IDE_BLK_IO_DEV
*IdeBlkIoDevice
,
1737 IN UINTN BufferSize
,
1741 EFI_BLOCK_IO_MEDIA
*Media
;
1743 UINTN NumberOfBlocks
;
1746 BOOLEAN MediaChange
;
1748 if (Buffer
== NULL
) {
1749 return EFI_INVALID_PARAMETER
;
1752 if (BufferSize
== 0) {
1757 // ATAPI device media is removable, so it is a must
1758 // to detect media first before read operation
1760 MediaChange
= FALSE
;
1761 Status
= AtapiDetectMedia (IdeBlkIoDevice
, &MediaChange
);
1762 if (EFI_ERROR (Status
)) {
1764 if (IdeBlkIoDevice
->Cache
!= NULL
) {
1765 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1766 IdeBlkIoDevice
->Cache
= NULL
;
1772 // Get the intrinsic block size
1774 Media
= IdeBlkIoDevice
->BlkIo
.Media
;
1775 BlockSize
= Media
->BlockSize
;
1777 NumberOfBlocks
= BufferSize
/ BlockSize
;
1779 if (!(Media
->MediaPresent
)) {
1781 if (IdeBlkIoDevice
->Cache
!= NULL
) {
1782 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1783 IdeBlkIoDevice
->Cache
= NULL
;
1785 return EFI_NO_MEDIA
;
1789 if ((MediaId
!= Media
->MediaId
) || MediaChange
) {
1791 if (IdeBlkIoDevice
->Cache
!= NULL
) {
1792 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1793 IdeBlkIoDevice
->Cache
= NULL
;
1795 return EFI_MEDIA_CHANGED
;
1798 if (BufferSize
% BlockSize
!= 0) {
1799 return EFI_BAD_BUFFER_SIZE
;
1802 if (LBA
> Media
->LastBlock
) {
1803 return EFI_INVALID_PARAMETER
;
1806 if ((LBA
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
1807 return EFI_INVALID_PARAMETER
;
1810 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
1811 return EFI_INVALID_PARAMETER
;
1815 // if all the parameters are valid, then perform read sectors command
1816 // to transfer data from device to host.
1818 Status
= AtapiReadSectors (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1819 if (EFI_ERROR (Status
)) {
1820 return EFI_DEVICE_ERROR
;
1824 // Read blocks succeeded
1828 // save the first block to the cache for performance
1830 if (LBA
== 0 && !IdeBlkIoDevice
->Cache
) {
1831 IdeBlkIoDevice
->Cache
= AllocatePool (BlockSize
);
1832 if (IdeBlkIoDevice
!= NULL
) {
1833 CopyMem ((UINT8
*) IdeBlkIoDevice
->Cache
, (UINT8
*) Buffer
, BlockSize
);
1842 This function is the ATAPI implementation for WriteBlocks in the
1843 Block I/O Protocol interface.
1846 Indicates the calling context.
1849 The media id that the write request is for.
1852 The starting logical block address to write onto
1855 @param[in] BufferSize
1856 The size of the Buffer in bytes. This must be a
1857 multiple of the intrinsic block size of the device.
1860 A pointer to the source buffer for the data.
1861 The caller is responsible for either having implicit
1862 or explicit ownership of the memory that data is
1866 Write Blocks successfully.
1868 @retval EFI_DEVICE_ERROR
1869 Write Blocks failed.
1871 @retval EFI_NO_MEDIA
1872 There is no media in the device.
1874 @retval EFI_MEDIA_CHANGE
1875 The MediaId is not for the current media.
1877 @retval EFI_BAD_BUFFER_SIZE
1878 The BufferSize parameter is not a multiple of the
1879 intrinsic block size of the device.
1881 @retval EFI_INVALID_PARAMETER
1882 The write request contains LBAs that are not valid,
1883 or the data buffer is not valid.
1885 TODO: EFI_MEDIA_CHANGED - add return value to function comment
1886 TODO: EFI_WRITE_PROTECTED - add return value to function comment
1889 AtapiBlkIoWriteBlocks (
1890 IN IDE_BLK_IO_DEV
*IdeBlkIoDevice
,
1893 IN UINTN BufferSize
,
1898 EFI_BLOCK_IO_MEDIA
*Media
;
1900 UINTN NumberOfBlocks
;
1902 BOOLEAN MediaChange
;
1904 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1905 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1906 IdeBlkIoDevice
->Cache
= NULL
;
1909 if (Buffer
== NULL
) {
1910 return EFI_INVALID_PARAMETER
;
1913 if (BufferSize
== 0) {
1918 // ATAPI device media is removable,
1919 // so it is a must to detect media first before write operation
1921 MediaChange
= FALSE
;
1922 Status
= AtapiDetectMedia (IdeBlkIoDevice
, &MediaChange
);
1923 if (EFI_ERROR (Status
)) {
1925 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1926 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1927 IdeBlkIoDevice
->Cache
= NULL
;
1933 // Get the intrinsic block size
1935 Media
= IdeBlkIoDevice
->BlkIo
.Media
;
1936 BlockSize
= Media
->BlockSize
;
1937 NumberOfBlocks
= BufferSize
/ BlockSize
;
1939 if (!(Media
->MediaPresent
)) {
1941 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1942 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1943 IdeBlkIoDevice
->Cache
= NULL
;
1945 return EFI_NO_MEDIA
;
1948 if ((MediaId
!= Media
->MediaId
) || MediaChange
) {
1950 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1951 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1952 IdeBlkIoDevice
->Cache
= NULL
;
1954 return EFI_MEDIA_CHANGED
;
1957 if (Media
->ReadOnly
) {
1958 return EFI_WRITE_PROTECTED
;
1961 if (BufferSize
% BlockSize
!= 0) {
1962 return EFI_BAD_BUFFER_SIZE
;
1965 if (LBA
> Media
->LastBlock
) {
1966 return EFI_INVALID_PARAMETER
;
1969 if ((LBA
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
1970 return EFI_INVALID_PARAMETER
;
1973 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
1974 return EFI_INVALID_PARAMETER
;
1978 // if all the parameters are valid,
1979 // then perform write sectors command to transfer data from host to device.
1981 Status
= AtapiWriteSectors (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1982 if (EFI_ERROR (Status
)) {
1983 return EFI_DEVICE_ERROR
;
1991 This function is used to parse sense data. Only the first
1992 sense data is honoured.
1994 @param[in] IdeDev Indicates the calling context.
1995 @param[in] SenseCount Count of sense data.
1996 @param[out] Result The parsed result.
1998 @retval EFI_SUCCESS Successfully parsed.
1999 @retval EFI_INVALID_PARAMETER Count of sense data is zero.
2004 IN IDE_BLK_IO_DEV
*IdeDev
,
2005 IN UINTN SenseCount
,
2006 OUT SENSE_RESULT
*Result
2009 REQUEST_SENSE_DATA
*SenseData
;
2011 if (SenseCount
== 0) {
2012 return EFI_INVALID_PARAMETER
;
2016 // Only use the first sense data
2018 SenseData
= IdeDev
->SenseData
;
2019 *Result
= SenseOtherSense
;
2021 switch (SenseData
->sense_key
) {
2023 *Result
= SenseNoSenseKey
;
2026 switch (SenseData
->addnl_sense_code
) {
2028 *Result
= SenseNoMedia
;
2030 case ASC_MEDIA_UPSIDE_DOWN
:
2031 *Result
= SenseMediaError
;
2034 if (SenseData
->addnl_sense_code_qualifier
== ASCQ_IN_PROGRESS
) {
2035 *Result
= SenseDeviceNotReadyNeedRetry
;
2037 *Result
= SenseDeviceNotReadyNoRetry
;
2042 case SK_UNIT_ATTENTION
:
2043 if (SenseData
->addnl_sense_code
== ASC_MEDIA_CHANGE
) {
2044 *Result
= SenseMediaChange
;
2047 case SK_MEDIUM_ERROR
:
2048 switch (SenseData
->addnl_sense_code
) {
2049 case ASC_MEDIA_ERR1
:
2050 case ASC_MEDIA_ERR2
:
2051 case ASC_MEDIA_ERR3
:
2052 case ASC_MEDIA_ERR4
:
2053 *Result
= SenseMediaError
;
2065 This function reads the pending data in the device.
2067 @param[in] IdeDev Indicates the calling context.
2069 @retval EFI_SUCCESS Successfully read.
2070 @retval EFI_NOT_READY The BSY is set avoiding reading.
2074 AtapiReadPendingData (
2075 IN IDE_BLK_IO_DEV
*IdeDev
2079 UINT16 TempWordBuffer
;
2081 AltRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.AltStatus
);
2082 if ((AltRegister
& BSY
) == BSY
) {
2083 return EFI_NOT_READY
;
2085 if ((AltRegister
& (BSY
| DRQ
)) == DRQ
) {
2086 TempWordBuffer
= IDEReadPortB (IdeDev
->PciIo
,IdeDev
->IoPort
->Alt
.AltStatus
);
2087 while ((TempWordBuffer
& (BSY
| DRQ
)) == DRQ
) {
2088 IDEReadPortWMultiple (
2090 IdeDev
->IoPort
->Data
,
2094 TempWordBuffer
= IDEReadPortB (IdeDev
->PciIo
,IdeDev
->IoPort
->Alt
.AltStatus
);
2101 TODO: Add function description
2103 @param IdeDev TODO: add argument description
2104 @param WriteProtected TODO: add argument description
2106 @retval EFI_DEVICE_ERROR TODO: Add description for return value
2107 @retval EFI_DEVICE_ERROR TODO: Add description for return value
2108 @retval EFI_SUCCESS TODO: Add description for return value
2112 IsLS120orZipWriteProtected (
2113 IN IDE_BLK_IO_DEV
*IdeDev
,
2114 OUT BOOLEAN
*WriteProtected
2119 *WriteProtected
= FALSE
;
2121 Status
= LS120EnableMediaStatus (IdeDev
, TRUE
);
2122 if (EFI_ERROR (Status
)) {
2123 return EFI_DEVICE_ERROR
;
2127 // the Get Media Status Command is only valid
2128 // if a Set Features/Enable Media Status Command has been priviously issued.
2130 if (LS120GetMediaStatus (IdeDev
) == EFI_WRITE_PROTECTED
) {
2132 *WriteProtected
= TRUE
;
2135 *WriteProtected
= FALSE
;
2139 // After Get Media Status Command completes,
2140 // Set Features/Disable Media Command should be sent.
2142 Status
= LS120EnableMediaStatus (IdeDev
, FALSE
);
2143 if (EFI_ERROR (Status
)) {
2144 return EFI_DEVICE_ERROR
;