Skip to content

Commit ff10435

Browse files
dependabot[bot]mayank-microsoft
authored andcommitted
feat: add scsi mode sense operation to detect write protection
1 parent 0e56f3d commit ff10435

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

MdePkg/Include/IndustryStandard/Atapi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,8 @@ typedef union {
503503
#define ATA_CMD_PREVENT_ALLOW_MEDIA_REMOVAL 0x1E ///< defined in ATAPI Removable Rewritable Media Devices
504504
#define ATA_CMD_MODE_SELECT 0x55 ///< defined in ATAPI Removable Rewritable Media Devices
505505

506+
507+
#define ATA_CMD_MODE_SENSE6 0x1A ///< defined in ATAPI Removable Rewritable Media Devices
506508
#define ATA_CMD_MODE_SENSE 0x5A ///< defined in ATAPI Removable Rewritable Media Devices
507509
#define ATA_PAGE_CODE_READ_WRITE_ERROR 0x01 ///< defined in ATAPI Removable Rewritable Media Devices
508510
#define ATA_PAGE_CODE_CACHING_PAGE 0x08 ///< defined in ATAPI Removable Rewritable Media Devices

pip-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
##
1414

1515
edk2-pytool-library~=0.23.8 # MU_CHANGE
16-
edk2-pytool-extensions~=0.30.0 # MU_CHANGE
16+
edk2-pytool-extensions~=0.30.2 # MU_CHANGE
1717
antlr4-python3-runtime==4.13.2
1818
lcov-cobertura==2.1.1
1919
pygount==3.1.0 # MU_CHANGE

0 commit comments

Comments
 (0)