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 // No data transfer is premitted.
719 if (ByteCount
== 0) {
723 // for performance, we assert the ByteCount is an even number
724 // which is actually a resonable assumption
725 ASSERT((ByteCount
%2) == 0);
728 RequiredWordCount
= ByteCount
/ 2;
730 // ActuralWordCount means the word count of data really transferred.
734 while (ActualWordCount
< RequiredWordCount
) {
737 // before each data transfer stream, the host should poll DRQ bit ready,
738 // to see whether indicates device is ready to transfer data.
740 Status
= DRQReady2 (IdeDev
, TimeOut
);
741 if (EFI_ERROR (Status
)) {
742 return CheckErrorStatus (IdeDev
);
746 // read Status Register will clear interrupt
748 IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Status
);
751 // get current data transfer size from Cylinder Registers.
753 WordCount
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderMsb
) << 8;
754 WordCount
= WordCount
| IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->CylinderLsb
);
755 WordCount
= WordCount
& 0xffff;
758 WordCount
= EFI_MIN (WordCount
, (RequiredWordCount
- ActualWordCount
));
761 IDEReadPortWMultiple (
763 IdeDev
->IoPort
->Data
,
768 IDEWritePortWMultiple (
770 IdeDev
->IoPort
->Data
,
776 PtrBuffer
+= WordCount
;
777 ActualWordCount
+= WordCount
;
782 // In the case where the drive wants to send more data than we need to read,
783 // the DRQ bit will be set and cause delays from DRQClear2().
784 // We need to read data from the drive until it clears DRQ so we can move on.
786 AtapiReadPendingData (IdeDev
);
790 // After data transfer is completed, normally, DRQ bit should clear.
792 Status
= DRQClear2 (IdeDev
, ATAPITIMEOUT
);
793 if (EFI_ERROR (Status
)) {
794 return EFI_DEVICE_ERROR
;
798 // read status register to check whether error happens.
800 return CheckErrorStatus (IdeDev
);
804 Sends out ATAPI Test Unit Ready Packet Command to the specified device
805 to find out whether device is accessible.
807 @param[in] *IdeDev Pointer pointing to IDE_BLK_IO_DEV data structure, used
808 to record all the information of the IDE device.
809 @param[in] *SenseCount Sense count for this packet command
811 @retval EFI_SUCCESS Device is accessible.
812 @retval EFI_DEVICE_ERROR Device is not accessible.
817 IN IDE_BLK_IO_DEV
*IdeDev
,
818 OUT UINTN
*SenseCount
821 ATAPI_PACKET_COMMAND Packet
;
827 // fill command packet
829 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
830 Packet
.TestUnitReady
.opcode
= TEST_UNIT_READY
;
833 // send command packet
835 Status
= AtapiPacketCommandIn (IdeDev
, &Packet
, NULL
, 0, ATAPITIMEOUT
);
836 if (EFI_ERROR (Status
)) {
840 Status
= AtapiRequestSense (IdeDev
, SenseCount
);
841 if (EFI_ERROR (Status
)) {
850 Sends out ATAPI Request Sense Packet Command to the specified device.
851 This command will return all the current Sense data in the device.
852 This function will pack all the Sense data in one single buffer.
855 pointer pointing to IDE_BLK_IO_DEV data structure, used
856 to record all the information of the IDE device.
858 @param[out] **SenseBuffers
859 allocated in this function, and freed by the calling function.
860 This buffer is used to accommodate all the sense data returned
864 record the unit size of the sense data block in the SenseBuffers,
866 @param[out] *BufNumbers
867 record the number of units in the SenseBuffers.
870 Request Sense command completes successfully.
872 @retval EFI_DEVICE_ERROR
873 Request Sense command failed.
878 IN IDE_BLK_IO_DEV
*IdeDev
,
879 OUT UINTN
*SenseCounts
883 REQUEST_SENSE_DATA
*Sense
;
885 BOOLEAN FetchSenseData
;
886 ATAPI_PACKET_COMMAND Packet
;
890 ZeroMem (IdeDev
->SenseData
, sizeof (REQUEST_SENSE_DATA
) * (IdeDev
->SenseDataNumber
));
892 // fill command packet for Request Sense Packet Command
894 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
895 Packet
.RequestSence
.opcode
= REQUEST_SENSE
;
896 Packet
.RequestSence
.allocation_length
= sizeof (REQUEST_SENSE_DATA
);
899 // initialize pointer
901 Ptr
= (UINT16
*) IdeDev
->SenseData
;
903 // request sense data from device continuously until no sense data
904 // exists in the device.
906 for (FetchSenseData
= TRUE
; FetchSenseData
;) {
908 Sense
= (REQUEST_SENSE_DATA
*) Ptr
;
911 // send out Request Sense Packet Command and get one Sense data form device
913 Status
= AtapiPacketCommandIn (
917 sizeof (REQUEST_SENSE_DATA
),
921 // failed to get Sense data
923 if (EFI_ERROR (Status
)) {
924 if (*SenseCounts
== 0) {
925 return EFI_DEVICE_ERROR
;
933 // We limit MAX sense data count to 20 in order to avoid dead loop. Some
934 // incompatible ATAPI devices don't retrive NO_SENSE when there is no media.
935 // In this case, dead loop occurs if we don't have a gatekeeper. 20 is
936 // supposed to be large enough for any ATAPI device.
938 if ((Sense
->sense_key
!= SK_NO_SENSE
) && ((*SenseCounts
) < 20)) {
940 // Ptr is word-based pointer
942 Ptr
+= (sizeof (REQUEST_SENSE_DATA
) + 1) >> 1;
946 // when no sense key, skip out the loop
948 FetchSenseData
= FALSE
;
956 Sends out ATAPI Read Capacity Packet Command to the specified device.
957 This command will return the information regarding the capacity of the
960 Current device status will impact device's response to the Read Capacity
961 Command. For example, if the device once reset, the Read Capacity
962 Command will fail. The Sense data record the current device status, so
963 if the Read Capacity Command failed, the Sense data must be requested
964 and be analyzed to determine if the Read Capacity Command should retry.
966 @param[in] *IdeDev Pointer pointing to IDE_BLK_IO_DEV data structure, used
967 to record all the information of the IDE device.
968 @param[in] SenseCount Sense count for this packet command
970 @retval EFI_SUCCESS Read Capacity Command finally completes successfully.
971 @retval EFI_DEVICE_ERROR Read Capacity Command failed because of device error.
973 @note Parameter "IdeDev" will be updated in this function.
975 TODO: EFI_NOT_READY - add return value to function comment
979 IN IDE_BLK_IO_DEV
*IdeDev
,
980 OUT UINTN
*SenseCount
984 // status returned by Read Capacity Packet Command
987 EFI_STATUS SenseStatus
;
988 ATAPI_PACKET_COMMAND Packet
;
991 // used for capacity data returned from ATAPI device
993 READ_CAPACITY_DATA Data
;
994 READ_FORMAT_CAPACITY_DATA FormatData
;
998 ZeroMem (&Data
, sizeof (Data
));
999 ZeroMem (&FormatData
, sizeof (FormatData
));
1001 if (IdeDev
->Type
== IdeCdRom
) {
1003 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1004 Packet
.Inquiry
.opcode
= READ_CAPACITY
;
1005 Status
= AtapiPacketCommandIn (
1009 sizeof (READ_CAPACITY_DATA
),
1015 // Type == IdeMagnetic
1017 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1018 Packet
.ReadFormatCapacity
.opcode
= READ_FORMAT_CAPACITY
;
1019 Packet
.ReadFormatCapacity
.allocation_length_lo
= 12;
1020 Status
= AtapiPacketCommandIn (
1023 (UINT16
*) &FormatData
,
1024 sizeof (READ_FORMAT_CAPACITY_DATA
),
1029 if (Status
== EFI_TIMEOUT
) {
1034 SenseStatus
= AtapiRequestSense (IdeDev
, SenseCount
);
1036 if (!EFI_ERROR (SenseStatus
)) {
1038 if (!EFI_ERROR (Status
)) {
1040 if (IdeDev
->Type
== IdeCdRom
) {
1042 IdeDev
->BlkIo
.Media
->LastBlock
= (Data
.LastLba3
<< 24) |
1043 (Data
.LastLba2
<< 16) |
1044 (Data
.LastLba1
<< 8) |
1047 if (IdeDev
->BlkIo
.Media
->LastBlock
!= 0) {
1049 IdeDev
->BlkIo
.Media
->BlockSize
= (Data
.BlockSize3
<< 24) |
1050 (Data
.BlockSize2
<< 16) |
1051 (Data
.BlockSize1
<< 8) |
1054 IdeDev
->BlkIo
.Media
->MediaPresent
= TRUE
;
1056 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1057 return EFI_DEVICE_ERROR
;
1060 IdeDev
->BlkIo
.Media
->ReadOnly
= TRUE
;
1063 // Because the user data portion in the sector of the Data CD supported
1066 IdeDev
->BlkIo
.Media
->BlockSize
= 0x800;
1069 if (IdeDev
->Type
== IdeMagnetic
) {
1071 if (FormatData
.DesCode
== 3) {
1072 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1073 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
1076 IdeDev
->BlkIo
.Media
->LastBlock
= (FormatData
.LastLba3
<< 24) |
1077 (FormatData
.LastLba2
<< 16) |
1078 (FormatData
.LastLba1
<< 8) |
1079 FormatData
.LastLba0
;
1080 if (IdeDev
->BlkIo
.Media
->LastBlock
!= 0) {
1081 IdeDev
->BlkIo
.Media
->LastBlock
--;
1083 IdeDev
->BlkIo
.Media
->BlockSize
= (FormatData
.BlockSize2
<< 16) |
1084 (FormatData
.BlockSize1
<< 8) |
1085 FormatData
.BlockSize0
;
1087 IdeDev
->BlkIo
.Media
->MediaPresent
= TRUE
;
1089 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1091 // Return EFI_NOT_READY operation succeeds but returned capacity is 0
1093 return EFI_NOT_READY
;
1096 IdeDev
->BlkIo
.Media
->BlockSize
= 0x200;
1106 return EFI_DEVICE_ERROR
;
1111 Used before read/write blocks from/to ATAPI device media.
1112 Since ATAPI device media is removable, it is necessary to detect
1113 whether media is present and get current present media's
1114 information, and if media has been changed, Block I/O Protocol
1115 need to be reinstalled.
1118 pointer pointing to IDE_BLK_IO_DEV data structure, used
1119 to record all the information of the IDE device.
1121 @param[out] *MediaChange
1122 return value that indicates if the media of the device has been
1126 media found successfully.
1128 @retval EFI_DEVICE_ERROR
1129 any error encounters during media detection.
1131 @retval EFI_NO_MEDIA
1135 parameter IdeDev may be updated in this function.
1140 IN IDE_BLK_IO_DEV
*IdeDev
,
1141 OUT BOOLEAN
*MediaChange
1145 EFI_STATUS CleanStateStatus
;
1146 EFI_BLOCK_IO_MEDIA OldMediaInfo
;
1148 UINTN RetryNotReady
;
1150 SENSE_RESULT SResult
;
1151 BOOLEAN WriteProtected
;
1153 CopyMem (&OldMediaInfo
, IdeDev
->BlkIo
.Media
, sizeof (EFI_BLOCK_IO_MEDIA
));
1154 *MediaChange
= FALSE
;
1156 // Retry for SenseDeviceNotReadyNeedRetry.
1157 // Each retry takes 1s and we limit the upper boundary to
1158 // 120 times about 2 min.
1160 RetryNotReady
= 120;
1163 // Do Test Unit Ready
1170 while (RetryTimes
!= 0) {
1172 Status
= AtapiTestUnitReady (IdeDev
, &SenseCount
);
1174 if (EFI_ERROR (Status
)) {
1176 // Test Unit Ready error without sense data.
1177 // For some devices, this means there's extra data
1178 // that has not been read, so we read these extra
1179 // data out before going on.
1181 CleanStateStatus
= AtapiReadPendingData (IdeDev
);
1182 if (EFI_ERROR (CleanStateStatus
)) {
1184 // Busy wait failed, try again
1189 // Try again without counting down RetryTimes
1194 ParseSenseData (IdeDev
, SenseCount
, &SResult
);
1197 case SenseNoSenseKey
:
1198 if (IdeDev
->BlkIo
.Media
->MediaPresent
) {
1202 // Media present but the internal structure need refreshed.
1203 // Try Read Capacity
1209 case SenseDeviceNotReadyNeedRetry
:
1210 if (--RetryNotReady
== 0) {
1211 return EFI_DEVICE_ERROR
;
1213 gBS
->Stall (1000 * STALL_1_MILLI_SECOND
);
1218 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1219 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
1223 case SenseDeviceNotReadyNoRetry
:
1224 case SenseMediaError
:
1225 return EFI_DEVICE_ERROR
;
1227 case SenseMediaChange
:
1228 IdeDev
->BlkIo
.Media
->MediaId
++;
1239 return EFI_DEVICE_ERROR
;
1247 while (RetryTimes
!= 0) {
1249 Status
= AtapiReadCapacity (IdeDev
, &SenseCount
);
1251 if (EFI_ERROR (Status
)) {
1256 ParseSenseData (IdeDev
, SenseCount
, &SResult
);
1259 case SenseNoSenseKey
:
1263 case SenseDeviceNotReadyNeedRetry
:
1265 // We use Test Unit Ready to retry which
1272 IdeDev
->BlkIo
.Media
->MediaPresent
= FALSE
;
1273 IdeDev
->BlkIo
.Media
->LastBlock
= 0;
1277 case SenseDeviceNotReadyNoRetry
:
1278 case SenseMediaError
:
1279 return EFI_DEVICE_ERROR
;
1281 case SenseMediaChange
:
1282 IdeDev
->BlkIo
.Media
->MediaId
++;
1293 return EFI_DEVICE_ERROR
;
1297 // the following code is to check the write-protected for LS120 media
1299 if ((IdeDev
->BlkIo
.Media
->MediaPresent
) && (IdeDev
->Type
== IdeMagnetic
)) {
1301 Status
= IsLS120orZipWriteProtected (IdeDev
, &WriteProtected
);
1302 if (!EFI_ERROR (Status
)) {
1304 if (WriteProtected
) {
1306 IdeDev
->BlkIo
.Media
->ReadOnly
= TRUE
;
1309 IdeDev
->BlkIo
.Media
->ReadOnly
= FALSE
;
1315 if (IdeDev
->BlkIo
.Media
->MediaId
!= OldMediaInfo
.MediaId
) {
1317 // Media change information got from the device
1319 *MediaChange
= TRUE
;
1322 if (IdeDev
->BlkIo
.Media
->ReadOnly
!= OldMediaInfo
.ReadOnly
) {
1323 *MediaChange
= TRUE
;
1324 IdeDev
->BlkIo
.Media
->MediaId
+= 1;
1327 if (IdeDev
->BlkIo
.Media
->BlockSize
!= OldMediaInfo
.BlockSize
) {
1328 *MediaChange
= TRUE
;
1329 IdeDev
->BlkIo
.Media
->MediaId
+= 1;
1332 if (IdeDev
->BlkIo
.Media
->LastBlock
!= OldMediaInfo
.LastBlock
) {
1333 *MediaChange
= TRUE
;
1334 IdeDev
->BlkIo
.Media
->MediaId
+= 1;
1337 if (IdeDev
->BlkIo
.Media
->MediaPresent
!= OldMediaInfo
.MediaPresent
) {
1338 if (IdeDev
->BlkIo
.Media
->MediaPresent
) {
1340 // when change from no media to media present, reset the MediaId to 1.
1342 IdeDev
->BlkIo
.Media
->MediaId
= 1;
1345 // when no media, reset the MediaId to zero.
1347 IdeDev
->BlkIo
.Media
->MediaId
= 0;
1350 *MediaChange
= TRUE
;
1354 // if any change on current existing media,
1355 // the Block I/O protocol need to be reinstalled.
1358 gBS
->ReinstallProtocolInterface (
1360 &gEfiBlockIoProtocolGuid
,
1366 if (IdeDev
->BlkIo
.Media
->MediaPresent
) {
1369 return EFI_NO_MEDIA
;
1374 This function is called by the AtapiBlkIoReadBlocks() to perform
1375 read from media in block unit.
1377 The main command used to access media here is READ(10) Command.
1378 READ(10) Command requests that the ATAPI device media transfer
1379 specified data to the host. Data is transferred in block(sector)
1380 unit. The maximum number of blocks that can be transferred once is
1381 65536. This is the main difference between READ(10) and READ(12)
1382 Command. The maximum number of blocks in READ(12) is 2 power 32.
1385 pointer pointing to IDE_BLK_IO_DEV data structure, used
1386 to record all the information of the IDE device.
1389 A pointer to the destination buffer for the data.
1392 The starting logical block address to read from
1393 on the device media.
1395 @param[in] NumberOfBlocks
1396 The number of transfer data blocks.
1398 @return status is fully dependent on the return status
1399 of AtapiPacketCommandIn() function.
1404 IN IDE_BLK_IO_DEV
*IdeDev
,
1407 IN UINTN NumberOfBlocks
1411 ATAPI_PACKET_COMMAND Packet
;
1412 READ10_CMD
*Read10Packet
;
1414 UINTN BlocksRemaining
;
1424 // fill command packet for Read(10) command
1426 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1427 Read10Packet
= &Packet
.Read10
;
1428 Lba32
= (UINT32
) Lba
;
1431 BlockSize
= IdeDev
->BlkIo
.Media
->BlockSize
;
1434 // limit the data bytes that can be transferred by one Read(10) Command
1438 BlocksRemaining
= NumberOfBlocks
;
1440 Status
= EFI_SUCCESS
;
1441 while (BlocksRemaining
> 0) {
1443 if (BlocksRemaining
<= MaxBlock
) {
1445 SectorCount
= (UINT16
) BlocksRemaining
;
1448 SectorCount
= MaxBlock
;
1452 // fill the Packet data structure
1455 Read10Packet
->opcode
= READ_10
;
1458 // Lba0 ~ Lba3 specify the start logical block address of the data transfer.
1459 // Lba0 is MSB, Lba3 is LSB
1461 Read10Packet
->Lba3
= (UINT8
) (Lba32
& 0xff);
1462 Read10Packet
->Lba2
= (UINT8
) (Lba32
>> 8);
1463 Read10Packet
->Lba1
= (UINT8
) (Lba32
>> 16);
1464 Read10Packet
->Lba0
= (UINT8
) (Lba32
>> 24);
1467 // TranLen0 ~ TranLen1 specify the transfer length in block unit.
1468 // TranLen0 is MSB, TranLen is LSB
1470 Read10Packet
->TranLen1
= (UINT8
) (SectorCount
& 0xff);
1471 Read10Packet
->TranLen0
= (UINT8
) (SectorCount
>> 8);
1473 ByteCount
= SectorCount
* BlockSize
;
1475 if (IdeDev
->Type
== IdeCdRom
) {
1476 TimeOut
= CDROMLONGTIMEOUT
;
1478 TimeOut
= ATAPILONGTIMEOUT
;
1481 Status
= AtapiPacketCommandIn (
1484 (UINT16
*) PtrBuffer
,
1488 if (EFI_ERROR (Status
)) {
1492 Lba32
+= SectorCount
;
1493 PtrBuffer
= (UINT8
*) PtrBuffer
+ SectorCount
* BlockSize
;
1494 BlocksRemaining
-= SectorCount
;
1501 This function is called by the AtapiBlkIoWriteBlocks() to perform
1502 write onto media in block unit.
1503 The main command used to access media here is Write(10) Command.
1504 Write(10) Command requests that the ATAPI device media transfer
1505 specified data to the host. Data is transferred in block (sector)
1506 unit. The maximum number of blocks that can be transferred once is
1510 pointer pointing to IDE_BLK_IO_DEV data structure, used
1511 to record all the information of the IDE device.
1514 A pointer to the source buffer for the data.
1517 The starting logical block address to write onto
1520 @param[in] NumberOfBlocks
1521 The number of transfer data blocks.
1523 @return status is fully dependent on the return status
1524 of AtapiPacketCommandOut() function.
1529 IN IDE_BLK_IO_DEV
*IdeDev
,
1532 IN UINTN NumberOfBlocks
1536 ATAPI_PACKET_COMMAND Packet
;
1537 READ10_CMD
*Read10Packet
;
1540 UINTN BlocksRemaining
;
1549 // fill command packet for Write(10) command
1550 // Write(10) command packet has the same data structure as
1551 // Read(10) command packet,
1552 // so here use the Read10Packet data structure
1553 // for the Write(10) command packet.
1555 ZeroMem (&Packet
, sizeof (ATAPI_PACKET_COMMAND
));
1556 Read10Packet
= &Packet
.Read10
;
1558 Lba32
= (UINT32
) Lba
;
1561 BlockSize
= IdeDev
->BlkIo
.Media
->BlockSize
;
1564 // limit the data bytes that can be transferred by one Read(10) Command
1566 MaxBlock
= (UINT16
) (65536 / BlockSize
);
1568 BlocksRemaining
= NumberOfBlocks
;
1570 Status
= EFI_SUCCESS
;
1571 while (BlocksRemaining
> 0) {
1573 if (BlocksRemaining
>= MaxBlock
) {
1574 SectorCount
= MaxBlock
;
1576 SectorCount
= (UINT16
) BlocksRemaining
;
1580 // Command code is WRITE_10.
1582 Read10Packet
->opcode
= WRITE_10
;
1585 // Lba0 ~ Lba3 specify the start logical block address of the data transfer.
1586 // Lba0 is MSB, Lba3 is LSB
1588 Read10Packet
->Lba3
= (UINT8
) (Lba32
& 0xff);
1589 Read10Packet
->Lba2
= (UINT8
) (Lba32
>> 8);
1590 Read10Packet
->Lba1
= (UINT8
) (Lba32
>> 16);
1591 Read10Packet
->Lba0
= (UINT8
) (Lba32
>> 24);
1594 // TranLen0 ~ TranLen1 specify the transfer length in block unit.
1595 // TranLen0 is MSB, TranLen is LSB
1597 Read10Packet
->TranLen1
= (UINT8
) (SectorCount
& 0xff);
1598 Read10Packet
->TranLen0
= (UINT8
) (SectorCount
>> 8);
1600 ByteCount
= SectorCount
* BlockSize
;
1602 Status
= AtapiPacketCommandOut (
1605 (UINT16
*) PtrBuffer
,
1609 if (EFI_ERROR (Status
)) {
1613 Lba32
+= SectorCount
;
1614 PtrBuffer
= ((UINT8
*) PtrBuffer
+ SectorCount
* BlockSize
);
1615 BlocksRemaining
-= SectorCount
;
1622 This function is used to implement the Soft Reset on the specified
1623 ATAPI device. Different from the AtaSoftReset(), here reset is a ATA
1624 Soft Reset Command special for ATAPI device, and it only take effects
1625 on the specified ATAPI device, not on the whole IDE bus.
1626 Since the ATAPI soft reset is needed when device is in exceptional
1627 condition (such as BSY bit is always set ), I think the Soft Reset
1628 command should be sent without waiting for the BSY clear and DRDY
1630 This function is called by IdeBlkIoReset(),
1631 a interface function of Block I/O protocol.
1634 pointer pointing to IDE_BLK_IO_DEV data structure, used
1635 to record all the information of the IDE device.
1638 Soft reset completes successfully.
1640 @retval EFI_DEVICE_ERROR
1641 Any step during the reset process is failed.
1646 IN IDE_BLK_IO_DEV
*IdeDev
1654 // for ATAPI device, no need to wait DRDY ready after device selecting.
1655 // (bit7 and bit5 are both set to 1 for backward compatibility)
1657 DeviceSelect
= (UINT8
) (((bit7
| bit5
) | (IdeDev
->Device
<< 4)));
1658 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Head
, DeviceSelect
);
1660 Command
= ATAPI_SOFT_RESET_CMD
;
1661 IDEWritePortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Reg
.Command
, Command
);
1664 // BSY cleared is the only status return to the host by the device
1665 // when reset is completed.
1666 // slave device needs at most 31s to clear BSY
1668 Status
= WaitForBSYClear (IdeDev
, 31000);
1669 if (EFI_ERROR (Status
)) {
1670 return EFI_DEVICE_ERROR
;
1674 // stall 5 seconds to make the device status stable
1676 gBS
->Stall (5000000);
1682 This function is the ATAPI implementation for ReadBlocks in the
1683 Block I/O Protocol interface.
1685 @param[in] *IdeBlkIoDev
1686 Indicates the calling context.
1689 The media id that the read request is for.
1692 The starting logical block address to read from
1695 @param[in] BufferSize
1696 The size of the Buffer in bytes. This must be a
1697 multiple of the intrinsic block size of the device.
1700 A pointer to the destination buffer for the data.
1701 The caller is responsible for either having implicit
1702 or explicit ownership of the memory that data is read into.
1705 Read Blocks successfully.
1707 @retval EFI_DEVICE_ERROR
1710 @retval EFI_NO_MEDIA
1711 There is no media in the device.
1713 @retval EFI_MEDIA_CHANGED
1714 The MediaId is not for the current media.
1716 @retval EFI_BAD_BUFFER_SIZE
1717 The BufferSize parameter is not a multiple of the
1718 intrinsic block size of the device.
1720 @retval EFI_INVALID_PARAMETER
1721 The read request contains LBAs that are not valid,
1722 or the data buffer is not valid.
1726 AtapiBlkIoReadBlocks (
1727 IN IDE_BLK_IO_DEV
*IdeBlkIoDevice
,
1730 IN UINTN BufferSize
,
1734 EFI_BLOCK_IO_MEDIA
*Media
;
1736 UINTN NumberOfBlocks
;
1739 BOOLEAN MediaChange
;
1741 if (Buffer
== NULL
) {
1742 return EFI_INVALID_PARAMETER
;
1745 if (BufferSize
== 0) {
1750 // ATAPI device media is removable, so it is a must
1751 // to detect media first before read operation
1753 MediaChange
= FALSE
;
1754 Status
= AtapiDetectMedia (IdeBlkIoDevice
, &MediaChange
);
1755 if (EFI_ERROR (Status
)) {
1757 if (IdeBlkIoDevice
->Cache
!= NULL
) {
1758 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1759 IdeBlkIoDevice
->Cache
= NULL
;
1765 // Get the intrinsic block size
1767 Media
= IdeBlkIoDevice
->BlkIo
.Media
;
1768 BlockSize
= Media
->BlockSize
;
1770 NumberOfBlocks
= BufferSize
/ BlockSize
;
1772 if (!(Media
->MediaPresent
)) {
1774 if (IdeBlkIoDevice
->Cache
!= NULL
) {
1775 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1776 IdeBlkIoDevice
->Cache
= NULL
;
1778 return EFI_NO_MEDIA
;
1782 if ((MediaId
!= Media
->MediaId
) || MediaChange
) {
1784 if (IdeBlkIoDevice
->Cache
!= NULL
) {
1785 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1786 IdeBlkIoDevice
->Cache
= NULL
;
1788 return EFI_MEDIA_CHANGED
;
1791 if (BufferSize
% BlockSize
!= 0) {
1792 return EFI_BAD_BUFFER_SIZE
;
1795 if (LBA
> Media
->LastBlock
) {
1796 return EFI_INVALID_PARAMETER
;
1799 if ((LBA
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
1800 return EFI_INVALID_PARAMETER
;
1803 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
1804 return EFI_INVALID_PARAMETER
;
1808 // if all the parameters are valid, then perform read sectors command
1809 // to transfer data from device to host.
1811 Status
= AtapiReadSectors (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1812 if (EFI_ERROR (Status
)) {
1813 return EFI_DEVICE_ERROR
;
1817 // Read blocks succeeded
1821 // save the first block to the cache for performance
1823 if (LBA
== 0 && !IdeBlkIoDevice
->Cache
) {
1824 IdeBlkIoDevice
->Cache
= AllocatePool (BlockSize
);
1825 if (IdeBlkIoDevice
!= NULL
) {
1826 CopyMem ((UINT8
*) IdeBlkIoDevice
->Cache
, (UINT8
*) Buffer
, BlockSize
);
1835 This function is the ATAPI implementation for WriteBlocks in the
1836 Block I/O Protocol interface.
1839 Indicates the calling context.
1842 The media id that the write request is for.
1845 The starting logical block address to write onto
1848 @param[in] BufferSize
1849 The size of the Buffer in bytes. This must be a
1850 multiple of the intrinsic block size of the device.
1853 A pointer to the source buffer for the data.
1854 The caller is responsible for either having implicit
1855 or explicit ownership of the memory that data is
1859 Write Blocks successfully.
1861 @retval EFI_DEVICE_ERROR
1862 Write Blocks failed.
1864 @retval EFI_NO_MEDIA
1865 There is no media in the device.
1867 @retval EFI_MEDIA_CHANGE
1868 The MediaId is not for the current media.
1870 @retval EFI_BAD_BUFFER_SIZE
1871 The BufferSize parameter is not a multiple of the
1872 intrinsic block size of the device.
1874 @retval EFI_INVALID_PARAMETER
1875 The write request contains LBAs that are not valid,
1876 or the data buffer is not valid.
1878 TODO: EFI_MEDIA_CHANGED - add return value to function comment
1879 TODO: EFI_WRITE_PROTECTED - add return value to function comment
1882 AtapiBlkIoWriteBlocks (
1883 IN IDE_BLK_IO_DEV
*IdeBlkIoDevice
,
1886 IN UINTN BufferSize
,
1891 EFI_BLOCK_IO_MEDIA
*Media
;
1893 UINTN NumberOfBlocks
;
1895 BOOLEAN MediaChange
;
1897 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1898 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1899 IdeBlkIoDevice
->Cache
= NULL
;
1902 if (Buffer
== NULL
) {
1903 return EFI_INVALID_PARAMETER
;
1906 if (BufferSize
== 0) {
1911 // ATAPI device media is removable,
1912 // so it is a must to detect media first before write operation
1914 MediaChange
= FALSE
;
1915 Status
= AtapiDetectMedia (IdeBlkIoDevice
, &MediaChange
);
1916 if (EFI_ERROR (Status
)) {
1918 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1919 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1920 IdeBlkIoDevice
->Cache
= NULL
;
1926 // Get the intrinsic block size
1928 Media
= IdeBlkIoDevice
->BlkIo
.Media
;
1929 BlockSize
= Media
->BlockSize
;
1930 NumberOfBlocks
= BufferSize
/ BlockSize
;
1932 if (!(Media
->MediaPresent
)) {
1934 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1935 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1936 IdeBlkIoDevice
->Cache
= NULL
;
1938 return EFI_NO_MEDIA
;
1941 if ((MediaId
!= Media
->MediaId
) || MediaChange
) {
1943 if (LBA
== 0 && IdeBlkIoDevice
->Cache
) {
1944 gBS
->FreePool (IdeBlkIoDevice
->Cache
);
1945 IdeBlkIoDevice
->Cache
= NULL
;
1947 return EFI_MEDIA_CHANGED
;
1950 if (Media
->ReadOnly
) {
1951 return EFI_WRITE_PROTECTED
;
1954 if (BufferSize
% BlockSize
!= 0) {
1955 return EFI_BAD_BUFFER_SIZE
;
1958 if (LBA
> Media
->LastBlock
) {
1959 return EFI_INVALID_PARAMETER
;
1962 if ((LBA
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
1963 return EFI_INVALID_PARAMETER
;
1966 if ((Media
->IoAlign
> 1) && (((UINTN
) Buffer
& (Media
->IoAlign
- 1)) != 0)) {
1967 return EFI_INVALID_PARAMETER
;
1971 // if all the parameters are valid,
1972 // then perform write sectors command to transfer data from host to device.
1974 Status
= AtapiWriteSectors (IdeBlkIoDevice
, Buffer
, LBA
, NumberOfBlocks
);
1975 if (EFI_ERROR (Status
)) {
1976 return EFI_DEVICE_ERROR
;
1984 This function is used to parse sense data. Only the first
1985 sense data is honoured.
1987 @param[in] IdeDev Indicates the calling context.
1988 @param[in] SenseCount Count of sense data.
1989 @param[out] Result The parsed result.
1991 @retval EFI_SUCCESS Successfully parsed.
1992 @retval EFI_INVALID_PARAMETER Count of sense data is zero.
1997 IN IDE_BLK_IO_DEV
*IdeDev
,
1998 IN UINTN SenseCount
,
1999 OUT SENSE_RESULT
*Result
2002 REQUEST_SENSE_DATA
*SenseData
;
2004 if (SenseCount
== 0) {
2005 return EFI_INVALID_PARAMETER
;
2009 // Only use the first sense data
2011 SenseData
= IdeDev
->SenseData
;
2012 *Result
= SenseOtherSense
;
2014 switch (SenseData
->sense_key
) {
2016 *Result
= SenseNoSenseKey
;
2019 switch (SenseData
->addnl_sense_code
) {
2021 *Result
= SenseNoMedia
;
2023 case ASC_MEDIA_UPSIDE_DOWN
:
2024 *Result
= SenseMediaError
;
2027 if (SenseData
->addnl_sense_code_qualifier
== ASCQ_IN_PROGRESS
) {
2028 *Result
= SenseDeviceNotReadyNeedRetry
;
2030 *Result
= SenseDeviceNotReadyNoRetry
;
2035 case SK_UNIT_ATTENTION
:
2036 if (SenseData
->addnl_sense_code
== ASC_MEDIA_CHANGE
) {
2037 *Result
= SenseMediaChange
;
2040 case SK_MEDIUM_ERROR
:
2041 switch (SenseData
->addnl_sense_code
) {
2042 case ASC_MEDIA_ERR1
:
2043 case ASC_MEDIA_ERR2
:
2044 case ASC_MEDIA_ERR3
:
2045 case ASC_MEDIA_ERR4
:
2046 *Result
= SenseMediaError
;
2058 This function reads the pending data in the device.
2060 @param[in] IdeDev Indicates the calling context.
2062 @retval EFI_SUCCESS Successfully read.
2063 @retval EFI_NOT_READY The BSY is set avoiding reading.
2067 AtapiReadPendingData (
2068 IN IDE_BLK_IO_DEV
*IdeDev
2072 UINT16 TempWordBuffer
;
2074 AltRegister
= IDEReadPortB (IdeDev
->PciIo
, IdeDev
->IoPort
->Alt
.AltStatus
);
2075 if ((AltRegister
& BSY
) == BSY
) {
2076 return EFI_NOT_READY
;
2078 if ((AltRegister
& (BSY
| DRQ
)) == DRQ
) {
2079 TempWordBuffer
= IDEReadPortB (IdeDev
->PciIo
,IdeDev
->IoPort
->Alt
.AltStatus
);
2080 while ((TempWordBuffer
& (BSY
| DRQ
)) == DRQ
) {
2081 IDEReadPortWMultiple (
2083 IdeDev
->IoPort
->Data
,
2087 TempWordBuffer
= IDEReadPortB (IdeDev
->PciIo
,IdeDev
->IoPort
->Alt
.AltStatus
);
2094 TODO: Add function description
2096 @param IdeDev TODO: add argument description
2097 @param WriteProtected TODO: add argument description
2099 @retval EFI_DEVICE_ERROR TODO: Add description for return value
2100 @retval EFI_DEVICE_ERROR TODO: Add description for return value
2101 @retval EFI_SUCCESS TODO: Add description for return value
2105 IsLS120orZipWriteProtected (
2106 IN IDE_BLK_IO_DEV
*IdeDev
,
2107 OUT BOOLEAN
*WriteProtected
2112 *WriteProtected
= FALSE
;
2114 Status
= LS120EnableMediaStatus (IdeDev
, TRUE
);
2115 if (EFI_ERROR (Status
)) {
2116 return EFI_DEVICE_ERROR
;
2120 // the Get Media Status Command is only valid
2121 // if a Set Features/Enable Media Status Command has been priviously issued.
2123 if (LS120GetMediaStatus (IdeDev
) == EFI_WRITE_PROTECTED
) {
2125 *WriteProtected
= TRUE
;
2128 *WriteProtected
= FALSE
;
2132 // After Get Media Status Command completes,
2133 // Set Features/Disable Media Command should be sent.
2135 Status
= LS120EnableMediaStatus (IdeDev
, FALSE
);
2136 if (EFI_ERROR (Status
)) {
2137 return EFI_DEVICE_ERROR
;