Retrieve the partition number of BootMgr on Windows Vista and later

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 05:32:07

问题


I need to map Boot Manager to a partition number:

 Manufacturer Recovery partition = Partition 0 
 Boot manager = Partition 1
 C:\ = Partition 2
 D:\ = Partition 3

For mounted partitions, like C:\ or D:\, I use IOCTL_STORAGE_GET_DEVICE_NUMBER to retrieve partition numbers.

Now I would like to do something similar to get the number of the Windows Boot Manager. I can't assume that the BootMgr is the 100 MB partition or the previous to the System partition.

I have had a look to IOCTLs related to disk geometry but I didn't find anything useful. I need to distinguish the Boot Manager partition from, let's say, a 100 MB manufacturer Recovery partition.

BCDEDIT.exe tool shows the info needed;

Identificador           {bootmgr}
device                  partition=\Device\HarddiskVolume1
description             Windows Boot Manager
locale                  es-ES
inherit                 {globalsettings}
extendedinput           Yes
default                 {current}
resumeobject            {5586dd33-361b-11e0-8df8-0018716eb820}
displayorder            {current}
toolsdisplayorder       {memdiag}
timeout                 30
customactions           0x1000085000001
                        0x5400000f
custom:5400000f         {1f473c8f-0c00-11e1-898d-78acc0c157a7}

I'm developing my app in C and therefore the BCDEDIT approach implies to include a COM/WMI dependency to my relatively simple application.

Please, note that I'm talking about opening a handle to BootMgr partition using "\Device\HarddiskVolume1" retrieved through WMI and then using IOCTL_STORAGE_GET_DEVICE_NUMBER:

hHandle = CreateFile ("\\\\?\\GLOBALROOT\\Device\\HarddiskVolume1", 
                        GENERIC_READ|GENERIC_WRITE,
                        FILE_SHARE_READ|FILE_SHARE_WRITE, 
                        NULL, 
                        OPEN_EXISTING, 
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);

if ( hHandle != INVALID_HANDLE_VALUE )
{
    VOLUME_DISK_EXTENTS diskExtents;
    DWORD dwSize;
    BOOL iRes;

    iRes = DeviceIoControl(hHandle,
                            IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
                            NULL,
                            0,
                            (LPVOID) &diskExtents,
                            (DWORD) sizeof(diskExtents),
                            (LPDWORD) &dwSize,
                            NULL);

    if (iRes)
    {
        STORAGE_DEVICE_NUMBER deviceNumber;
        DWORD bytesReturned =  0;

        iRes = DeviceIoControl(hHandle, 
                        IOCTL_STORAGE_GET_DEVICE_NUMBER, 
                        NULL, 
                        0, 
                        &deviceNumber, 
                        sizeof(deviceNumber), 
                        &bytesReturned, NULL);

Any ideas for a simpler workaround?


回答1:


You can find the system volume (where Windows booted and also where bootmgr reside) by looking in the registry: HKLM\SYSTEM\Setup\SystemPartition. This will contain a name like \Device\HardDiskVolume1.

On a disk formatted by Windows 7 setup, this corresponds to the 100MB partition that starts before the partition that contains drive C:. On Windows 7, the big partition that contains drive C: is \Device\HardDiskVolume2.




回答2:


Perhaps it is too late, but this info can help someone;

You may try to find BootIndicator partition by sending IOCTL code such as PARTITION_INFORMATION_EX for real solution.



来源:https://stackoverflow.com/questions/15361617/retrieve-the-partition-number-of-bootmgr-on-windows-vista-and-later

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!