@@ -196,6 +196,47 @@ ScsiDiskDriverBindingSupported (
196196 return Status ;
197197}
198198
199+ EFI_STATUS
200+ IsWriteProtected (
201+ IN OUT SCSI_DISK_DEV * ScsiDiskDevice ,
202+ OUT BOOLEAN * WriteProtectionEnabled
203+ )
204+ {
205+ EFI_STATUS Status ;
206+ EFI_SCSI_IO_SCSI_REQUEST_PACKET CommandPacket ;
207+ UINT8 Cdb [6 ];
208+ UINT8 DataBuffer [64 ];
209+
210+ //
211+ // Initialize SCSI REQUEST_PACKET and 6-byte Cdb
212+ //
213+ ZeroMem (& CommandPacket , sizeof (CommandPacket ));
214+ ZeroMem (Cdb , sizeof (Cdb ));
215+
216+ Cdb [0 ] = ATA_CMD_MODE_SENSE6 ;
217+ Cdb [1 ] = 0b1000 ; // Setting the bit for Disable Block Descriptor
218+ Cdb [2 ] = ATA_PAGE_CODE_RETURN_ALL_PAGES ;
219+ Cdb [4 ] = sizeof (DataBuffer );
220+
221+ CommandPacket .Timeout = SCSI_DISK_TIMEOUT ;
222+ CommandPacket .Cdb = Cdb ;
223+ CommandPacket .CdbLength = (UINT8 )sizeof (Cdb );
224+ CommandPacket .InDataBuffer = & DataBuffer ;
225+ CommandPacket .InTransferLength = sizeof (DataBuffer );
226+
227+ Status = ScsiDiskDevice -> ScsiIo -> ExecuteScsiCommand (ScsiDiskDevice -> ScsiIo , & CommandPacket , NULL );
228+
229+ if (EFI_ERROR (Status )) {
230+ return Status ;
231+ }
232+
233+ // Mode Sense 6 Byte Command returns the Write Protection status in the 3rd byte
234+ // Bit 7 of the 3rd byte indicates the Write Protection status
235+ * WriteProtectionEnabled = (DataBuffer [2 ] & 0x80 ) != 0 ;
236+ return EFI_SUCCESS ;
237+ }
238+
239+
199240/**
200241 Start this driver on ControllerHandle.
201242
@@ -234,6 +275,7 @@ ScsiDiskDriverBindingStart (
234275 CHAR8 VendorStr [VENDOR_IDENTIFICATION_LENGTH + 1 ];
235276 CHAR8 ProductStr [PRODUCT_IDENTIFICATION_LENGTH + 1 ];
236277 CHAR16 DeviceStr [VENDOR_IDENTIFICATION_LENGTH + PRODUCT_IDENTIFICATION_LENGTH + 2 ];
278+ BOOLEAN WriteProtectionEnabled = FALSE;
237279
238280 MustReadCapacity = TRUE;
239281
@@ -297,6 +339,17 @@ ScsiDiskDriverBindingStart (
297339 break ;
298340 }
299341
342+ if (ScsiDiskDevice -> DeviceType == EFI_SCSI_TYPE_DISK ) {
343+ Status = IsWriteProtected (ScsiDiskDevice , & WriteProtectionEnabled );
344+ if (EFI_ERROR (Status )) {
345+ DEBUG ((DEBUG_ERROR , "ScsiDisk: IsWriteProtected() fails. Status = %r\n" , Status ));
346+ }
347+ if (WriteProtectionEnabled ) {
348+ ScsiDiskDevice -> BlkIo .Media -> ReadOnly = TRUE;
349+ }
350+ }
351+
352+
300353 //
301354 // The Sense Data Array's initial size is 6
302355 //
@@ -6332,6 +6385,8 @@ AtapiIdentifyDevice (
63326385 return ScsiDiskDevice -> ScsiIo -> ExecuteScsiCommand (ScsiDiskDevice -> ScsiIo , & CommandPacket , NULL );
63336386}
63346387
6388+
6389+
63356390/**
63366391 Initialize the installation of DiskInfo protocol.
63376392
0 commit comments