Boot Windows from Grub2 command line

Grub2 is a powerful boot loader and supports booting most operating systems. This article will guide you to boot Windows from Grub2 command line.

Although you can scan and boot into most of the operating systems installed on computers such as Windows, Linux, FreeBSD and macOS from the AIO Boot’s Grub2 menu. But Windows is a common operating system, AIO Boot v0.9.7.6 has added a menu to support booting into Windows faster without having to scan other operating systems (press i).

Grub2 command line

Of course we will use the menu instead of booting Windows from the Grub2 command line. But there are several reasons why you can not access the Grub2 menu. The most common reason is that you have accidentally deleted the partition that contains the Grub2 configuration file. Using the Grub2 command line is the only way before you have to use other rescue devices.

When you delete the partition containing Grub2 configuration file (grub.cfg) and its modules, Grub2 will stop in rescue mode. If the Grub2 modules are preloaded, you still have the opportunity to boot into Windows. Specifically, we need to load the ntldr module in Legacy BIOS mode and the chainloader module in UEFI mode. In my experience, on Ubuntu, Linux Mint, Debian, and other Linux operating systems, these modules are preloaded only when you boot in UEFI mode. This means that these modules have been embedded in the grubx64.efi file. AIO Boot is the same. But we should try anyway.

This is the Grub2 screen that stops in rescue mode.

grub rescue

error: no such device:
error: unknown filesystem.
Entering rescue mode...
grub rescue>

First, we will use the search command to find the partition containing the BCD file. This partition also contains the bootmgr or bootmgfw.efi file. Then we will load this file to boot into Windows.

UEFI

For UEFI, the BCD file will be located at /EFI/Microsoft/Boot/BCD on the EFI partition. This partition also contains the file /EFI/Microsoft/Boot/bootmgfw.efi. Use the chainloader command to chainload this file.

search -s root -f /EFI/Microsoft/Boot/BCD
chainloader /EFI/Microsoft/Boot/bootmgfw.efi
boot

Legacy

For Windows Vista, Windows 7 to Windows 10, the BCD file will be located at /Boot/BCD. This partition also contains the file /bootmgr. Use the ntldr command to load this file.

search -s root -f /Boot/BCD
ntldr /bootmgr
boot

For Windows XP/2003, you will find the partition that contains /NTDETECT.COM and load the /ntldr file. You need to use the drivemap command to MAP the hard disk on which you installed Windows XP/2003 as the first hard disk if it is not.

search -s root -f /NTDETECT.COM
drivemap -s hd0 $root
ntldr /ntldr
boot

You will see the following errors if the corresponding modules are not preloaded as mentioned above.

Unknown command 'search'.
error: unknown filesystem.
Unknown command 'chainloader'.
Unknown command 'ntldr'.

If the files are not found on all partitions, you will see one of the following errors:

error: no such device: /EFI/Microsoft/Boot/BCD.
error: no such device: /Boot/BCD.
error: no such device: /NTDETECT.COM.

Add Grub2 menu

If you install Ubuntu or other Linux operating systems alongside Windows, then Windows will automatically be added to the Grub2 menu. If you install Grub2 manually, you can add Windows to the Grub2 menu manually. Here is the Grub2 script that I wrote for AIO Boot:

menuentry "Windows" {
	insmod regexp
	set saved_root=$root
	for dev in (*,*); do
	regexp -s device '\((.*)\)' $dev
	if searchindevice "$device" ; then
		probe -s fstype -f $device
		if [ -z "$fstype" ] -o [ "$fstype" == "procfs" ]; then continue; fi
		probe -s tscheme -p $device
		if [ -n "$pc" ] -a [ "$tscheme" == "msdos" ]; then
			if test \
				   '(' -f ($device)/bootmgr -a -f ($device)/boot/bcd ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/boot/Bcd ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/boot/BCD ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/Boot/bcd ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/Boot/Bcd ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/Boot/BCD ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/BOOT/bcd ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/BOOT/Bcd ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/BOOT/BCD ')' \
				; then
				set root="$device"
				ntldr /bootmgr
				boot
			elif test \
				   '(' -f ($device)/ntldr -a -e ($device)/ntdetect.com -a -f ($device)/boot.ini ')' \
				   -o \
				   '(' -f ($device)/ntldr -a -e ($device)/ntdetect.com -a -f ($device)/BOOT.INI ')' \
				   -o \
				   '(' -f ($device)/ntldr -a -e ($device)/NTDETECT.COM -a -f ($device)/boot.ini ')' \
				   -o \
				   '(' -f ($device)/ntldr -a -e ($device)/NTDETECT.COM -a -f ($device)/BOOT.INI ')' \
				   -o \
				   '(' -f ($device)/NTLDR -a -e ($device)/ntdetect.com -a -f ($device)/boot.ini ')' \
				   -o \
				   '(' -f ($device)/NTLDR -a -e ($device)/ntdetect.com -a -f ($device)/BOOT.INI ')' \
				   -o \
				   '(' -f ($device)/NTLDR -a -e ($device)/NTDETECT.COM -a -f ($device)/boot.ini ')' \
				   -o \
				   '(' -f ($device)/NTLDR -a -e ($device)/NTDETECT.COM -a -f ($device)/BOOT.INI ')' \
				; then
				set root="$device"
				regexp -s devnum 'hd([0-9]+)' $root
				if test "$devnum" != "0"; then drivemap -s hd0 $root; fi
				ntldr /ntldr
				boot
			fi
		elif [ -n "$efi" ] -a [ "$fstype" == "fat" ] -a [ "$tscheme" == "gpt" ] -a [ -f ($device)/EFI/Microsoft/Boot/bootmgfw.efi ] -a [ -f ($device)/EFI/Microsoft/Boot/BCD ]; then
			root="$device"
			chainloader /EFI/Microsoft/Boot/bootmgfw.efi
			boot
		fi
		unset tscheme
		unset fstype
	fi
    done
	set root=$saved_root
	if [ "${grub_platform}" == "efi" ]; then set mode="UEFI mode"; else set mode="Legacy BIOS mode"; fi
	echo "Did not find the Windows operating system can be booted in ${mode}."
	echo
	echo -n "Press ESC to return to the menu "
	sleep -i -v 60
}

The above menu will automatically scan and boot into the Windows operating system installed on the computer. Supports Windows Vista, Windows 7 to Windows 10 for both UEFI and Legacy BIOS modes. Supports Windows XP/2003/2000 in Legacy mode. I think Windows 98/ME does not need to be added.

Share this...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on RedditShare on TumblrShare on VKBuffer this pageDigg thisEmail this to someoneFlattr the authorPrint this pageShare on StumbleUponShare on Yummly