problem: bcdboot vs. bcdedit with VHD

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

  1. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    895
    482
    30
    #1 KNARZ, Feb 9, 2018
    Last edited: Feb 9, 2018
  2. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    534
    926
    30
    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."
    }
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. sebus

    sebus MDL Guru

    Jul 23, 2008
    6,354
    2,026
    210
    Compare both with Visual BCD
     
  4. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    895
    482
    30
    neither Visual BCD (works NOT like charme) nor BOOTICE show any differences.
     
  5. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    895
    482
    30
    all done. - the doc uses bcdboot, which will include a bunch of unnecessary entries i want to avoid.
     
  6. LiteOS

    LiteOS Windowizer

    Mar 7, 2014
    2,198
    974
    90
    #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 ?
     
  7. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    895
    482
    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".
     
  8. LiteOS

    LiteOS Windowizer

    Mar 7, 2014
    2,198
    974
    90
    #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?
     
  9. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    895
    482
    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)
     
  10. LiteOS

    LiteOS Windowizer

    Mar 7, 2014
    2,198
    974
    90
  11. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    895
    482
    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.
     
  12. sebus

    sebus MDL Guru

    Jul 23, 2008
    6,354
    2,026
    210
    So you have it working?
     
  13. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    895
    482
    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]
     
  14. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    534
    926
    30
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  15. sebus

    sebus MDL Guru

    Jul 23, 2008
    6,354
    2,026
    210
    @GodHand

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

    GodHand MDL Addicted

    Jul 15, 2016
    534
    926
    30
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  17. sebus

    sebus MDL Guru

    Jul 23, 2008
    6,354
    2,026
    210
    Please do, thanks

    sebus