Results 1 to 4 of 4

Thread: reading/writing sectors on a USB memory stick including MBR

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Posts
    8

    reading/writing sectors on a USB memory stick including MBR

    Hi,
    With VB6 under WinXP i am able to read and write directly to sectors on a USB stick (using kernel32). However i am unable to access the MBR. Apparently with the kernel32 you can only get to the sectors in a volume (of a partitioned USB memory stick).
    Does anybody know about a way to read/write with VB6 to the MBR and maybe some more sectors that are outside the volume?
    Djuskali

  2. #2
    Addicted Member pcuser's Avatar
    Join Date
    Jun 2008
    Posts
    219

    Re: reading/writing sectors on a USB memory stick including MBR

    You have to use the device name returned from SetupDiGetDeviceInterfaceDetail. This is the order of steps:

    1) SetupDiGetClassDevs
    2) SetupDiEnumDeviceInterfaces
    3) SetupDiGetDeviceInterfaceDetail (first time is to get the size needed)
    4) SetupDiGetDeviceInterfaceDetail (second time is the actual call that returns the device name)

    Here's a CreateFile example using the device name of my usb drive:
    Code:
    DevicePathName = "\\?\usbstor#disk&ven_&prod_patriot_memory&rev_pmap#079113be1b5d&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}"
    MyDevice = CreateFile(DevicePathName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0&)
    Keep in mind that starting with Vista, directly writing to certain areas of a mounted drive or volume are blocked unless it's locked first.

    Blocking Direct Write Operations to Volumes and Disks

    Some of the most compelling new features in Windows Vista are its guarantees regarding code and data integrity. To better protect Windows Vista from potentially malicious techniques, changes to the storage driver stack have been introduced to block direct write operations to volume and disk handles. If your application or driver writes directly to a volume that has a file system mounted on it, without first obtaining exclusive access to it, you could cause corruption or system instability, or both. This is because those writes might collide with other changes coming from the file system and leave the contents of the volume or file system, or both in an inconsistent state. To prevent this, we have made the following changes.
    After you get a handle to the physical device using CreateFile you can use that handle with LockFile to lock the drive before writing to it and then UnlockFile and CloseHandle when you're finished.

    Just out of curiousity, what are you doing with the MBR?

  3. #3
    Addicted Member pcuser's Avatar
    Join Date
    Jun 2008
    Posts
    219

    Re: reading/writing sectors on a USB memory stick including MBR

    Attached is a small sample project.

    Call modDriveMap.CreateMappings to enumerate all of your usb drives and create an array with mapping details about each drive. Each element of the array "MappedResult()" consists of 3 comma seperated values:

    "drive letter", "physical drive number", "device path name"

    Here's how to get the device name for drive letter H:

    Code:
    Private Sub Command1_Click()
        Dim SplitMapping() As String
        Dim i as Integer
        CreateMappings
        For i = LBound(MappedResult) To UBound(MappedResult)
            SplitMapping = Split(MappedResult(i), ",")
            If SplitMapping(0) = "H:" Then
                MsgBox "Drive Letter - " & SplitMapping(0) & vbCrLf & _
                "Drive Number - " & SplitMapping(1) & vbCrLf & _
                "Device Path Name - " & SplitMapping(2)
            End If
        Next
    End Sub
    If you want to write to the MBR of the drive that H: is on then you'd use SplitMapping(2) in your CreateFile call instead of "\\.\H:"

    SplitMapping(0) = Drive Letter
    SplitMapping(1) = PhysicalDrive?
    SplitMapping(2) = DevicePathName

    modDirectDrive contains the functions to read/write directly to the drive.

    Here's how to read the first byte of the MBR and write hex "AA" to the last byte of the MBR:

    Code:
    Private Sub Command1_Click()
        Dim SplitMapping() As String
        CreateMappings
        For i = LBound(MappedResult) To UBound(MappedResult)
            SplitMapping = Split(MappedResult(i), ",")
            If SplitMapping(0) = "H:" Then
                MsgBox Hex(Direct_Read_Drive(SplitMapping(2), 0, 0, 1))
                DirectWriteDriveNT SplitMapping(2), 0, 511, hex2ascii("AA")
            End If
        Next
    End Sub
    Here's how to write hex "55" to the second to last byte of the first partitions boot sector:

    Code:
    Private Sub Command1_Click()
        Dim SplitMapping() As String
        CreateMappings
        For i = LBound(MappedResult) To UBound(MappedResult)
            SplitMapping = Split(MappedResult(i), ",")
            If SplitMapping(0) = "H:" Then
                DirectWriteDriveNT SplitMapping(2), 63, 510, hex2ascii("55")
            End If
        Next
    End Sub
    Attached Files Attached Files

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Posts
    8

    Re: reading/writing sectors on a USB memory stick including MBR

    Thanks a lot Pcuser,
    This should get me started.

    You asked me why i want to access the MBR (sector 0). Well i am adding a USB stick to an old Z-80 computer using one of those fancy interface chips that handles all the complex business. Although this chip can handle FAT i am using the chip in raw mode (sector read/write). In this mode that chip really starts from sector 0. To be able to read/write the stick in a PC i need in the PC also to start from sector 0 (which is the MBR).

    Once again thanks for the zip file.
    Djuskal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width