problem: bcdboot vs. bcdedit with VHD

Discussion in 'Windows 10' started by KNARZ, Feb 9, 2018.

  1. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    865
    447
    30
    #1 KNARZ, Feb 9, 2018
    Last edited: Feb 9, 2018
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. GodHand

    GodHand MDL Senior Member

    Jul 15, 2016
    277
    221
    10
    I just set the constants and never have had issues making any partition-style bootable.

    Code:
     {
       [CmdletBinding()]
       param (
           [Parameter(Mandatory = $true)][string][ValidateNotNullOrEmpty()]$BcdStoreFile,
           [Parameter()][string][ValidateNotNullOrEmpty()][ValidateScript({ ($_ -eq $PARTITION_STYLE_MBR) -or ($_ -eq $PARTITION_STYLE_GPT) })]$PartitionStyle = $PARTITION_STYLE_MBR,
           [Parameter()][UInt64][ValidateScript({ $_ -ge 0 })]$DiskSignature,
           [Parameter()][UInt64][ValidateScript({ $_ -ge 0 })]$PartitionOffset
       )
       
       #########  Set Constants #########
       $BOOTMGR_ID = "{9DEA862C-5CDD-4E70-ACC1-F32B344D4795}"
       $DEFAULT_TYPE = 0x23000003
       $APPLICATION_DEVICE_TYPE = 0x11000001
       $OS_DEVICE_TYPE = 0x21000001
       ##################################
       
       Write-Verbose "Opening $($BcdStoreFile) for configuration..."
       Write-Output "Partition Style : $PartitionStyle"
       Write-Output "Disk Signature  : $DiskSignature"
       Write-Output "Partition Offset: $PartitionOffset"
       
       $conn = New-Object Management.ConnectionOptions
       $scope = New-Object Management.ManagementScope -ArgumentList "\\.\ROOT\WMI", $conn
       $scope.Connect()
       
       $path = New-Object Management.ManagementPath `
                          -ArgumentList "\\.\ROOT\WMI:BCDObject.Id=`"$($BOOTMGR_ID)`",StoreFilePath=`"$($BcdStoreFile.Replace('\', '\\'))`""
       $options = New-Object Management.ObjectGetOptions
       $bootMgr = New-Object Management.ManagementObject -ArgumentList $scope, $path, $options
       
       try
       {
           $bootMgr.Get()
       }
       catch
       {
           throw "Could not get the BootMgr object from the Virtual Disks BCDStore."
       }
       
       Write-Output "Setting Qualified Partition Device Element for Virtual Disk boot..."
       
       
       $ret = $bootMgr.SetQualifiedPartitionDeviceElement($APPLICATION_DEVICE_TYPE, $PartitionStyle, $DiskSignature, $PartitionOffset)
       if (!$ret.ReturnValue)
       {
           throw "Unable to set Qualified Partition Device Element in Virtual Disks BCDStore."
       }
       
       Write-Output "Getting the default boot entry..."
       $defaultBootEntryId = ($bootMgr.GetElement($DEFAULT_TYPE)).Element.Id
       
       Write-Output "Getting the OS Loader..."
       
       $Path = New-Object Management.ManagementPath `
                          -ArgumentList "\\.\ROOT\WMI:BCDObject.Id=`"$($defaultBootEntryId)`",StoreFilePath=`"$($BcdStoreFile.Replace('\', '\\'))`""
       
       $OsLoader = New-Object Management.ManagementObject -ArgumentList $scope, $path, $options
       
       try
       {
           $OsLoader.Get()
       }
       catch
       {
           throw "Could not get the OS Loader..."
       }
       
       Write-Output "Setting Qualified Partition Device Element in the OS Loader Application..."
       $ret = $osLoader.SetQualifiedPartitionDeviceElement($APPLICATION_DEVICE_TYPE, $PartitionStyle, $DiskSignature, $PartitionOffset)
       if (!$ret.ReturnValue)
       {
           throw "Could not set Qualified Partition Device Element in the OS Loader Application."
       }
       
       Write-Output "Setting Qualified Partition Device Element in the OS Loader Device..."
       $ret = $osLoader.SetQualifiedPartitionDeviceElement($OS_DEVICE_TYPE, $PartitionStyle, $DiskSignature, $PartitionOffset)
       if (!$ret.ReturnValue)
       {
           throw "Could not set Qualified Partition Device Element in the OS Loader Device."
       }
       
       Write-Verbose "BCD configuration complete."
    }
    
     
  3. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    865
    447
    30
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. sebus

    sebus MDL Guru

    Jul 23, 2008
    5,792
    1,737
    180
    Compare both with Visual BCD
     
  5. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    865
    447
    30
    neither Visual BCD (works NOT like charme) nor BOOTICE show any differences.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    865
    447
    30
    all done. - the doc uses bcdboot, which will include a bunch of unnecessary entries i want to avoid.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. LiteOS

    LiteOS MDL Expert

    Mar 7, 2014
    1,212
    453
    60
    #8 LiteOS, Feb 9, 2018
    Last edited: Feb 9, 2018
    With mount the vhd it should work and then adding
    u cant mount in this env ?
     
  8. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    865
    447
    30
    the vhd is not the problem. it works. - it's about bcdedit and bcdboot. I clearly pointed this out and confirmed it by editing the working parameters into the non working, as the only change, and it boots.
    this is a more deep topic than just "i want to boot a vhd".
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. LiteOS

    LiteOS MDL Expert

    Mar 7, 2014
    1,212
    453
    60
    #10 LiteOS, Feb 9, 2018
    Last edited: Feb 9, 2018
    Sometime ago i tried to make vhd of vm bootable using my host tool didnt succeed

    U looking for same thing making bcd store that will work on any other pc?
     
  10. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    865
    447
    30
    Applying my BCD entries to an internal Drive works (byte: 06).
    Applying it to extern UFD or fixed SSD won't work and result in a failure. (byte: 08)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. LiteOS

    LiteOS MDL Expert

    Mar 7, 2014
    1,212
    453
    60
  12. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    865
    447
    30
    found the bug.

    as with "path". "systemroot" also needs to specified in the osloader itself.
    It's not enough to set "systemroot" in {bootloadersettings}.

    inherit {bootloadersettings} get's kind of useless.... 5 hours down the hole.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  13. sebus

    sebus MDL Guru

    Jul 23, 2008
    5,792
    1,737
    180
    So you have it working?
     
  14. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    865
    447
    30
    the described problem is solved, yes. - But the entries are still different (06 vs 08 byte)
    addig systemroot and path to the bcd entry is mandatory and can not be defined in {bootloadersettings} alone. [I put them in there as they are always the same]
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  15. sebus

    sebus MDL Guru

    Jul 23, 2008
    5,792
    1,737
    180
    @GodHand

    You ought to remember that not everybody is a coder...
     
  16. GodHand

    GodHand MDL Senior Member

    Jul 15, 2016
    277
    221
    10
    Well to be fair, a lot of them require very little actual code to utilize, because you use them in a wrapper. Attaining data like BCD info is not an exhaustive process that requires *any* declarations. I'm just giving out the info on the way I manage these because I constantly apply images to VHDs or physical disks, as well as create them, etc. in a single go; and I've had issues in the past trying to manage this info either statically via variables in scripts, or would end up with absolutely ridiculous lines of code to trim, get substrings, convert, etc. just so I could *possibly* have it work. That said, I would never spend any substantial amount of time creating an advanced function for when my server will directly manipulate all of that automatically just by running MDT or SCOM on it to "test" the hardware.

    I have a few examples I can post that people can use at their desire.
     
  17. sebus

    sebus MDL Guru

    Jul 23, 2008
    5,792
    1,737
    180
    Please do, thanks

    sebus