Announcement

Collapse
No announcement yet.

MBR - Partition Table Info

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • MBR - Partition Table Info

    For a project I need to read a SD card that is written by dedicated hardware. It has FAT on it.
    To be able to fully use the card, I need to know the total number of sectors available on the partitions.
    That info is found in the MBR -> Partition Table Entries. (The last 4 bytes of every 16 byte entry holds the number of sectors in a partition.)
    Is there a way to retrieve that info? (Preferrably without being logged in with administrator rights...)
    Also the partition type is stored there and is of interest (byte 5 of table entry)
    Last edited by Peter Lameijn; 29 Aug 2008, 10:23 AM.
    Regards,
    Peter

  • #2
    This will do the trick (Gives info on the first removable drive in your system):
    Code:
    '==================================================================================================
    #Compile Exe
    #Include "win32api.inc"
    #Include "commctrl.INC"
    #Include "WinIoCtl.inc"
    
    Type BootRecordType
      JumpCode          As String * 3
      OEMName           As String * 8
      BytesPerSector    As Word
      SectorsAllowed    As Byte
      ReservedSectors   As Word
      NumberOfFATs      As Byte
      RootEntries       As Word
      Sectors16         As Word
      MediaType         As Byte
      SectorsPerFAT     As Word
      SectorsPerTrack   As Word
      NumberOfHeads     As Word
      HiddenSectors     As Dword
      Sectors32         As Dword
      DriveID           As Byte
      ReservedForNT     As Byte
      ExtendedSignature As Byte
      SerialNumber      As Dword
      PartitionName     As String * 11
      FATType           As String * 8
      BootCode          As String * 450
    End Type
    
    Global BR           As BOOTRECORDTYPE
    Global fPtr         As Dword
    Global hDlg, hCD    As Dword
    Global Result       As Long
    Global dwNotUsed    As Dword
    Global fPtr         As Dword
    
    '==================================================================================================
    ' Get Disk drive letter
    '--------------------------------------------------------------------------------------------------
    Function GetDisk() As String
      Local Ret As Long, Cnt As Long, lzStr As Asciiz * 4',
      For Cnt = 3 To 26
        lzStr = Chr$(Cnt + &h40) + ":\"
        Ret = GetDriveType (lzStr)
        If Ret = %DRIVE_REMOVABLE Then
           Function = Left$(lzStr,2)
           Exit Function
        End If
      Next
      Function = ""
    End Function
    
    '==================================================================================================
    ' Callback
    '--------------------------------------------------------------------------------------------------
    CallBack Function CbMain()
      Local Result As Dword
      Select Case CbMsg
        Case %WM_DESTROY
          CloseHandle (hCD)
      End Select
    End Function
    
    '==================================================================================================
    ' Main
    '--------------------------------------------------------------------------------------------------
    Function WinMain (ByVal CurInst&, ByVal PrvInst&, ByVal CmdLine As Asciiz Ptr, ByVal CmdShow&) Export As Long
      If GetDisk <> "" Then
        Dialog New 0, "Removable Media - Info:",,, 200, 200, %WS_SYSMENU To hDlg
    '--------------------------------------------------------------------------------------------------
        hCD = CreateFile ("\\.\" & GetDisk, %GENERIC_READ Or %GENERIC_WRITE, _
                                   %FILE_SHARE_READ Or %FILE_SHARE_WRITE, _
                                   ByVal %NULL, _
                                   %OPEN_EXISTING, _
                                   %FILE_ATTRIBUTE_NORMAL, _
                                   ByVal %NULL)
    '--------------------------------------------------------------------------------------------------
        If (hCD <> %INVALID_HANDLE_VALUE) Then
          fPtr = 0
          SetFilePointer (hCD, fPtr, ByVal 0, %FILE_BEGIN)
          If (ReadFile (hCD, BR, SizeOf(BR), dwNotUsed, ByVal %NULL)) Then
            Control Add Label, hDlg,   -1, "Drive: "                                , 10, 10, 80, 12
            Control Add Label, hDlg,   -1, GetDisk                                  , 90, 10, 80, 12
            Control Add Label, hDlg,   -1, "Partition size (bytes): "               , 10, 22, 80, 12
            If BR.Sectors16 <> 0 Then
              Result = BR.Sectors16
                Else
              Result = BR.Sectors32
            End If
            Control Add Label, hDlg,   -1, Format$(Result*BR.BytesPerSector,"0,000"), 90, 22, 80, 12
            Control Add Label, hDlg,   -1, "Start sector: "                         , 10, 34, 80, 12
            Control Add Label, hDlg,   -1, Format$(1 + _
                                                   BR.NumberOfFats * _
                                                  BR.SectorsPerFat + _
                                                  BR.RootEntries\16)                , 90, 34, 80, 12
            Control Add Label, hDlg,   -1, "Ending sector: "                        , 10, 46, 80, 12
            Control Add Label, hDlg,   -1, Format$(Result)                          , 90, 46, 80, 12
            Control Add Label, hDlg,   -1, "Usable data area: "                     , 10, 58, 80, 12
            Control Add Label, hDlg,   -1, Format$((Result - 1 - _
                                                   BR.NumberOfFats * _
                                                   BR.SectorsPerFat - _
                                                   BR.RootEntries\16) * _
                                                   BR.BytesPerSector, "0,000")       , 90, 58, 80, 12
            Control Add Label, hDlg,   -1, "Partition name: "                        , 10, 70, 80, 12
            Control Add Label, hDlg,   -1, BR.PartitionName                          , 90, 70, 80, 12
            Control Add Label, hDlg,   -1, "Partition type: "                        , 10, 82, 80, 12
            Control Add Label, hDlg,   -1, BR.FATType                                , 90, 82, 80, 12
            Control Add Label, hDlg,   -1, "Serial number:  "                        , 10, 94, 80, 12
            Control Add Label, hDlg,   -1, Left$(Hex$(BR.SerialNumber,8),4) & _
                                     "-" & Right$(Hex$(BR.SerialNumber,8),4)         , 90, 94, 80, 12
            Control Add Label, hDlg,   -1, "OEM info: "                              , 10,106, 80, 12
            Control Add Label, hDlg,   -1, BR.OEMName                                , 90,106, 80, 12
            Control Add Label, hDlg,   -1, "Bytes per sector: "                      , 10,118, 80, 12
            Control Add Label, hDlg,   -1, Format$(BR.BytesPerSector)                , 90,118, 80, 12
            Control Add Label, hDlg,   -1, "Sectors per FAT: "                       , 10,130, 80, 12
            Control Add Label, hDlg,   -1, Format$(BR.SectorsPerFAT)                 , 90,130, 80, 12
            Control Add Label, hDlg,   -1, "Root dir. entries: "                     , 10,142, 80, 12
            Control Add Label, hDlg,   -1, Format$(BR.RootEntries)                   , 90,142, 80, 12
            Control Add Label, hDlg,   -1, "Number of FAT tables: "                  , 10,154, 80, 12
            Control Add Label, hDlg,   -1, Format$(BR.NumberOfFATs)                  , 90,154, 80, 12
            Dialog Show Modal hDlg, Call CbMain
    
          Else
            MsgBox "Drive " & GetDisk & " not ready...",, "Info:"
          End If
        End If
      Else
        MsgBox "No drive found",, "Info"
      End If
    End Function
    Last edited by Peter Lameijn; 31 Aug 2008, 06:50 PM.
    Regards,
    Peter

    Comment

    Working...
    X