Sign in to follow this  
Followers 0
adamxp

Write a MBR Partition Type

10 posts in this topic

#1 ·  Posted (edited)

Each time the below function is called the mbr is not changed... please help

an example call,

_SetMBRPartitionType("\\.\e:",$PARTITION_HIDDEN_FAT32)

Local Const $PARTITION_IFS                                  = 7      ; IFS Partition 
Local Const $PARTITION_FAT32                                = 11      ; FAT32 
Local Const $PARTITION_FAT32_XINT13                         = 12      ; FAT32 using extended int13 services 

Local Const $PARTITION_HIDDEN_FAT32                         = 27      ; Hidden WIN95 OSR2 32-bit FAT
Local Const $PARTITION_HIDDEN_FAT32_LBA                     = 28      ; Hidden WIN95 OSR2 32-bit FAT, LBA-mapped


Local Const $FILE_ANY_ACCESS                                = 0

Local Const $PARTITION_STYLE_MBR                            = 0

Local Const $FILE_SHARE_NEITHER                             = 0
Local Const $FILE_OPEN_EXISTING                             = 3
Local Const $FILE_ATTRIBUTE_NORMAL                          = 128

Local Const $IOCTL_DISK_SET_PARTITION_INFO                  = 0x7C008
Local Const $IOCTL_DISK_UPDATE_PROPERTIES                   = 0x70140

Func _SetMBRPartitionType($sDevice, $sType)
    Local Const $tagPARTITION_SET_MBR = "short;ubyte "& $sType;"
   
    Local Const $tagPARTITION_SET_EX_MBR = _
        "int " & $PARTITION_STYLE_MBR & ;" & _
        $tagPARTITION_SET_MBR;"

    Local $tPIX = DllStructCreate($tagPARTITION_SET_EX_MBR)

    Local $hDevice
    Local $a_hCall, $a_iCall

    $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFile", _
                "str", $sDevice, _ 
                "dword", $FILE_ANY_ACCESS, _ 
                "dword", 0, _
                "ptr", $FILE_SHARE_NEITHER, _
                "dword", $FILE_OPEN_EXISTING, _
                "dword", $FILE_ATTRIBUTE_NORMAL, _
                "ptr", 0)
                
    $hDevice = $a_hCall[0]

    $a_iCall = DllCall("kernel32.dll", "int", "DeviceIoControl", _
        "hwnd", $hDevice, _
        "dword", $IOCTL_DISK_SET_PARTITION_INFO_EX, _; IOCTL_DISK_SET_PARTITION_INFO_EX
        "ptr", DllStructGetPtr($tPIX), _
        "dword", DllStructGetSize($tPIX), _
        "ptr", 0, _
        "dword", 0, _
        "dword*", 0, _
        "ptr", 0)
        
    $a_iCall = DllCall("kernel32.dll", "int", "DeviceIoControl", _
        "hwnd", $hDevice, _
        "dword", $IOCTL_DISK_UPDATE_PROPERTIES, _; IOCTL_DISK_UPDATE_PROPERTIES
        "ptr", 0, _
        "dword", 0, _
        "ptr", 0, _
        "dword", 0, _
        "dword*", 0, _
        "ptr", 0)

    DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hDevice)
        
EndFunc
Edited by adamxp

Share this post


Link to post
Share on other sites



Are you running as admin?

Share this post


Link to post
Share on other sites

yes, the #RequireAdmin is in the script, but the function still does not change the MBR data

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

This stuff looks plain wrong:

Local Const $tagPARTITION_SET_MBR = "short;ubyte "& $sType;"
   
    Local Const $tagPARTITION_SET_EX_MBR = _
        "int " & $PARTITION_STYLE_MBR & ;" & _
        $tagPARTITION_SET_MBR;"

Given your call: _SetMBRPartitionType("\\.\e:",$PARTITION_HIDDEN_FAT32)

That parses to:

Local Const $tagPARTITION_SET_MBR = "short;ubyte " & "27";"
   
    Local Const $tagPARTITION_SET_EX_MBR = _
        "int " & 0 & ;" & _
        $tagPARTITION_SET_MBR;"

Besides the syntax error on the first line of that, are you trying to initialize the values of the struct elements inside the tag string?

:idea:

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

yes, sorry I am new to the dllstructure syntax and would appreciate really any help you could provide :idea:

Share this post


Link to post
Share on other sites

You've jumped in to the deep end, way over my head. :)

But, never one to avoid looking like a fool for trying, this is how I would start it:

Local Const $PARTITION_HIDDEN_FAT32                         = 27      ; Hidden WIN95 OSR2 32-bit FAT
Local Const $PARTITION_STYLE_MBR                            = 0

Func _SetMBRPartitionType($sDevice, $sType)
    Local Const $tagSET_PARTITION_INFORMATION_MBR = "char PartitionType"
    
    Local Const $tagGUID = "dword; word; word; byte[8]"
    
    Local Const $tagSET_PARTITION_INFORMATION_GPT = _
            $tagGUID & ";" & _
            $tagGUID & ";" & _
            "UINT64 Attributes;" & _
            "wchar Name[36]"
        
    Local Const $tagPARTITION_SET_EX_MBR = _
            "int PartitionStyle;" & _
            $tagSET_PARTITION_INFORMATION_MBR & ";" & _
            $tagSET_PARTITION_INFORMATION_GPT

    Local $tPARTITION_SET_EX_MBR = DllStructCreate($tagPARTITION_SET_EX_MBR)

    DllStructSetData($tPARTITION_SET_EX_MBR, "PartitionType", $sType)
    DllStructSetData($tPARTITION_SET_EX_MBR, "PartitionStyle", $PARTITION_STYLE_MBR)
    
    ; ...
    
EndFunc

:idea:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Oh good catch PsaltyDS, I didn't realize there was an issue in the structure since I don't use AutoIt for that really.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

You've jumped in to the deep end, way over my head. :)

But, never one to avoid looking like a fool for trying, this is how I would start it:

Local Const $PARTITION_HIDDEN_FAT32                         = 27      ; Hidden WIN95 OSR2 32-bit FAT
Local Const $PARTITION_STYLE_MBR                            = 0

Func _SetMBRPartitionType($sDevice, $sType)
    Local Const $tagSET_PARTITION_INFORMATION_MBR = "char PartitionType"
    
    Local Const $tagGUID = "dword; word; word; byte[8]"
    
    Local Const $tagSET_PARTITION_INFORMATION_GPT = _
            $tagGUID & ";" & _
            $tagGUID & ";" & _
            "UINT64 Attributes;" & _
            "wchar Name[36]"
        
    Local Const $tagPARTITION_SET_EX_MBR = _
            "int PartitionStyle;" & _
            $tagSET_PARTITION_INFORMATION_MBR & ";" & _
            $tagSET_PARTITION_INFORMATION_GPT

    Local $tPARTITION_SET_EX_MBR = DllStructCreate($tagPARTITION_SET_EX_MBR)

    DllStructSetData($tPARTITION_SET_EX_MBR, "PartitionType", $sType)
    DllStructSetData($tPARTITION_SET_EX_MBR, "PartitionStyle", $PARTITION_STYLE_MBR)
    
    ; ...
    
EndFunc

:idea:

Thanks for trying but the code didn't work when appended to the function I already created. There must be a logic error in the way I am using the device io function call PARTITION_SET_EX_MBR but this is way over my head.

I just want to thank for your help, and let you know I hav'nt given up I will continue on trying to get this function working. I am completely new to calling api's and must seem like a complete idiot but I am trying to learn there just does not seem to be much information on how to use the IOCTL_DISK_SET_PARTITION_INFO_EX. I am crossing my fingers that an expert may come on this forum topic and bail me out.

Me being in the deep end and just learning on how to swim is a bad combination (sorry for the bad analogy) so I better get out before I drown. I naively thought if I could find some examples code for this dll api call on the internet that I could adapt and change them to meet my needs. However there is very little information on these Win Api functions and no information on how to use or code them in autoit.

So I am stumped

Edited by adamxp

Share this post


Link to post
Share on other sites

Some advice: Don't experiment on the API with a function that might cause data loss, like setting partition info. Figure it out with equivalent "GET" partition info, then take the lessons learned and start messing around with "SET" functions when you're more sure of yourself.

:mellow:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

There is a UDF that has wrappers for DeviceIoControl: http://www.autoitscript.com/forum/index.php?showtopic=98712. I'd use those as a starting point.

Creating the partition is still a fair bit of work judging by the example code I found:

char str[128];
        wsprintf(str,"\\\\.\\PhysicalDrive%d", i);
    hDevice = CreateFile( ,"\\\\.\\PhysicalDrive1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );

    LARGE_INTEGER dimDiscoD;
    LARGE_INTEGER dimDiscoE;
    LARGE_INTEGER dimDiscoF;
    LARGE_INTEGER dimDiscoG;
    DISK_GEOMETRY_EX pDiskGeometryEx;

    DWORD dwBytesReturned;
    BOOL r = DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, &pDiskGeometryEx, sizeof(pDiskGeometryEx), &dwBytesReturned, NULL );

    dimDiscoG.QuadPart = DIM_DISCOG;
    dimDiscoD.QuadPart = (( pDiskGeometryEx.DiskSize.QuadPart - dimDiscoG.QuadPart )/6) * 4;
    dimDiscoE.QuadPart = dimDiscoD.QuadPart/4;
    dimDiscoF.QuadPart = dimDiscoE.QuadPart;

    CREATE_DISK pCreateDisk;
    ZeroMemory(&pCreateDisk,sizeof(CREATE_DISK));

    DWORD dwBytesReturned;
    pCreateDisk.PartitionStyle =  PARTITION_STYLE_MBR;
    pCreateDisk.Mbr.Signature = 0xA4B57310;

    DeviceIoControl(hDevice, IOCTL_DISK_CREATE_DISK, (LPVOID)&pCreateDisk, sizeof(pCreateDisk), NULL, 0, &dwBytesReturned, (LPOVERLAPPED) NULL)

    DWORD junk1;
      
    DeviceIoControl(hDevice,IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &junk1, NULL);

    int SectorSize=pDiskGeometryEx.Geometry.BytesPerSector;

    DWORD szNewLayout = sizeof(DRIVE_LAYOUT_INFORMATION_EX)+4*sizeof (PARTITION_INFORMATION_EX);
    DRIVE_LAYOUT_INFORMATION_EX *dl = (DRIVE_LAYOUT_INFORMATION_EX*) new BYTE[szNewLayout];
    ZeroMemory(dl,szNewLayout);

    dl->PartitionEntry[0].PartitionStyle = PARTITION_STYLE_MBR;
    dl->PartitionEntry[0].StartingOffset.QuadPart = 32256i64; // = 63 * 512
    dl->PartitionEntry[0].PartitionLength.QuadPart = dimDiscoD.QuadPart;
    dl->PartitionEntry[0].PartitionNumber = 1;
    dl->PartitionEntry[0].RewritePartition = TRUE;
    dl->PartitionEntry[0].Mbr.PartitionType = 0x07;// PARTITION_IFS (NTFS partition or logical drive)
    //dl->PartitionEntry[0].Mbr.BootIndicator = TRUE;
    dl->PartitionEntry[0].Mbr.RecognizedPartition = 1;
    dl->PartitionEntry[0].Mbr.HiddenSectors=32256/SectorSize;

    dl->PartitionEntry[1].PartitionStyle=PARTITION_STYLE_MBR;
    dl->PartitionEntry[1].StartingOffset.QuadPart= 32256i64 + dimDiscoD.QuadPart;
    dl->PartitionEntry[1].PartitionLength.QuadPart = dimDiscoE.QuadPart;
    dl->PartitionEntry[1].PartitionNumber=2;
    dl->PartitionEntry[1].RewritePartition = TRUE;
    dl->PartitionEntry[1].Mbr.PartitionType = 0x07;
    dl->PartitionEntry[1].Mbr.RecognizedPartition = 1;
    dl->PartitionEntry[1].Mbr.HiddenSectors = (32256i64 + dimDiscoD.QuadPart)/SectorSize;  //SectorSize;

    dl->PartitionEntry[2].PartitionStyle=PARTITION_STYLE_MBR;
    dl->PartitionEntry[2].StartingOffset.QuadPart= 32256i64 + dimDiscoD.QuadPart + dimDiscoE.QuadPart;
    dl->PartitionEntry[2].PartitionLength.QuadPart = dimDiscoF.QuadPart;
    dl->PartitionEntry[2].PartitionNumber=3;
    dl->PartitionEntry[2].RewritePartition = TRUE;
    dl->PartitionEntry[2].Mbr.PartitionType = 0x07;
    dl->PartitionEntry[2].Mbr.RecognizedPartition = 1;
    dl->PartitionEntry[2].Mbr.HiddenSectors = (32256i64 + dimDiscoD.QuadPart + dimDiscoE.QuadPart)/SectorSize;  //SectorSize;

    dl->PartitionEntry[3].PartitionStyle=PARTITION_STYLE_MBR;
    dl->PartitionEntry[3].StartingOffset.QuadPart= 32256i64 + dimDiscoD.QuadPart + dimDiscoE.QuadPart + dimDiscoF.QuadPart;
    dl->PartitionEntry[3].PartitionLength.QuadPart = pDiskGeometryEx.DiskSize.QuadPart - (dimDiscoD.QuadPart + dimDiscoE.QuadPart + dimDiscoF.QuadPart );
    dl->PartitionEntry[3].PartitionNumber=4;
    dl->PartitionEntry[3].RewritePartition = TRUE;
    dl->PartitionEntry[3].Mbr.PartitionType = 0x07;
    dl->PartitionEntry[3].Mbr.RecognizedPartition = 1;
    dl->PartitionEntry[3].Mbr.HiddenSectors = (32256i64 + dimDiscoD.QuadPart + dimDiscoE.QuadPart + dimDiscoF.QuadPart )/SectorSize; //SectorSize;

    dl->PartitionStyle = PARTITION_STYLE_MBR;
    dl->PartitionCount = 4;// specify AT LEAST 4 partitions!!!
    dl->Mbr.Signature = 0xA4B57310; //

    DeviceIoControl(hDevice, IOCTL_DISK_SET_DRIVE_LAYOUT_EX, dl, szNewLayout, NULL, 0, &junk, (LPOVERLAPPED) NULL) )

    DWORD junk;
    DeviceIoControl(hDevice, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &junk, NULL);

    CloseHandle(hDevice);
    delete dl;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0