Jump to content











Photo
- - - - -

Scripts to switch UEFI/EFI Boot Loaders within grub4dos

tool uefi efi grub4dos windows linux grub2 refind

  • Please log in to reply
1 reply to this topic

#1 generalmx

generalmx

    Newbie

  • Members
  • 14 posts
  •  
    United States

Posted 28 March 2016 - 01:31 AM

I posted this a while ago in a different thread but as I still find it useful I thought I'd post both scripts together. These scripts make it easier to use something like Easy2Boot for all your BIOS booting needs while keeping loose files for your UEFI/EFI booting, as long as they can boot from the same filesystem.

 

uefi_switch.lst

WINX64.EFI - Windows UEFI Boot Loader that's not currently active

GRUBX64.EFI - Grub2 UEFI Boot Loader that's not currently active

REFX64.EFI - reFIND EFI Boot Loader that's not currently active

BOOTX64.EFI - Currently active 64-bit UEFI/EFI Boot Loader

Note: Requires FAT module. This script assumes one of these is currently renamed to BOOTX64.EFI, but can work without BOOTX64.EFI even existing.

iftitle [if exist /efi/boot/WINX64.EFI] Switch to Windows UEFI Boot Loader
# Load or reload the FAT module
insmod fat
find --set-root /uefi_switch.lst
if not exist /efi/boot/GRUBX64.EFI fat ren /efi/boot/BOOTX64.EFI /efi/boot/GRUBX64.EFI
if exist /efi/boot/BOOTX64.EFI if not exist /efi/boot/REFX64.EFI fat ren /efi/boot/BOOTX64.EFI /efi/boot/REFX64.EFI
fat ren /efi/boot/WINX64.EFI /efi/boot/BOOTX64.EFI
if not exist /efi/boot/WINX64.EFI echo OK, Windows UEFI Boot Loader set
pause
root ()

iftitle [if exist /efi/boot/GRUBX64.EFI] Switch to Grub2 UEFI Boot Loader (Memtest, Linux, etc.)
# Load or reload the FAT module
insmod fat
find --set-root /uefi_switch.lst
if not exist /efi/boot/WINX64.EFI fat ren /efi/boot/BOOTX64.EFI /efi/boot/WINX64.EFI
if exist /efi/boot/BOOTX64.EFI if not exist /efi/boot/REFX64.EFI fat ren /efi/boot/BOOTX64.EFI /efi/boot/REFX64.EFI
fat ren /efi/boot/GRUBX64.EFI /efi/boot/BOOTX64.EFI
if not exist /efi/boot/GRUBX64.EFI echo OK, Grub2 UEFI Boot Loader set
pause
root ()

iftitle [if exist /efi/boot/REFX64.EFI] Switch to reFIND UEFI Boot Loader (EFI Multiboot)
# Load or reload the FAT module
insmod fat
find --set-root /uefi_switch.lst
if not exist /efi/boot/WINX64.EFI fat ren /efi/boot/BOOTX64.EFI /efi/boot/WINX64.EFI
if exist /efi/boot/BOOTX64.EFI if not exist /efi/boot/GRUBX64.EFI fat ren /efi/boot/BOOTX64.EFI /efi/boot/GRUBX64.EFI
fat ren /efi/boot/REFX64.EFI /efi/boot/BOOTX64.EFI
if not exist /efi/boot/REFX64.EFI echo OK, reFIND UEFI Boot Loader set
pause
root ()

title \n
root

title ...back to main menu
find --set-root /menu.lst
configfile /menu.lst

Accompanying Windows batch file to switch without loading grub4dos, requires BOOTICE if you also want to support saving & restoring the MBR & PBR (optional):

@echo off
REM Switch between different boot loaders.

SET _PREVMBR=bootice.prev.mbr
SET _PREVPBR=bootice.prev.pbr
SET _DISKINFO=bootice.diskinfo.txt

REM EFI Bootloaders
SET _BOOTEFI=%~dp0efi\boot\BOOTX64.EFI
SET _WINEFI=%~dp0efi\boot\WINX64.EFI
SET _GRUBEFI=%~dp0efi\boot\GRUBX64.EFI
SET _REFEFI=%~dp0efi\boot\REFX64.EFI
SET _OTHEREFI=%~dp0efi\boot\OTHERX64.EFI

cd /d "%~dp0"

IF /I [%PROCESSOR_ARCHITECTURE%]==[x86] (
    set BOOTICE=BOOTICEx86.exe
) ELSE IF NOT EXIST "BOOTICEx64.exe" (
    set BOOTICE=BOOTICEx86.exe
) ELSE (
    set BOOTICE=BOOTICEx64.exe
)

IF NOT EXIST "%BOOTICE%" (
    set BOOTICE=BOOTICE.exe
)
IF NOT EXIST "%BOOTICE%" (
    echo Warning: BOOTICE not detected. MBR/PBR functions disabled.
) ELSE (
    IF NOT EXIST "%_DISKINFO%" (
        "%BOOTICE%" /device=%~d0 /diskinfo /file="%_DISKINFO%"
    )
    type "%_DISKINFO%"
)
echo.
echo *************************************
echo * This file switches boot loaders for both the BIOS/CSM and UEFI.
echo *************************************
echo Drive Letter: [%~d0]
echo.

IF EXIST "%BOOTICE%" (
    IF EXIST "%_PREVMBR%" (
        echo [BIOS] 1. Restore previous non-Windows boot loader [Press ENTER]
    ) ELSE (
        echo [BIOS] 1. Switch to Windows Boot Loader [Press ENTER]
    )
)
IF EXIST "%_WINEFI%" (
    echo [UEFI] 2. Switch to Windows UEFI Boot Loader
)
IF EXIST "%_GRUBEFI%" (
    echo [UEFI] 3. Switch to GRUB UEFI Boot Loader
)
IF EXIST "%_REFEFI%" (
    echo [UEFI] 4. Switch to REFIND EFI Boot Loader
)
IF EXIST "%_OTHEREFI%" (
    echo [UEFI] 5. Switch to other/unknown EFI Boot Loader
)
echo.
:enterselection
set /p "choice=Enter your selection: "
IF EXIST "%_WINEFI%" IF [%choice%]==[2] GOTO :winefi
IF EXIST "%_GRUBEFI%" IF [%choice%]==[3] GOTO :grubefi
IF EXIST "%_REFEFI%" IF [%choice%]==[4] GOTO :refindefi
IF EXIST "%_OTHEREFI%" IF [%choice%]==[5] GOTO :otherefi
IF EXIST "%BOOTICE%" (
    IF EXIST "%_PREVMBR%" GOTO :restorembr
    GOTO :windowsmbr
) ELSE (
    GOTO :enterselection
)

:windowsmbr
echo ** Installing Windows Boot Loader to MBR and PBR for [%~d0]...
"%BOOTICE%" /device=%~d0 /mbr /backup /file="%_PREVMBR%" /quiet && "%BOOTICE%" /device=%~d0 /pbr /backup /file="%_PREVPBR%" /quiet && "%BOOTICE%" /device=%~d0 /mbr /install /type=nt60 /quiet && "%BOOTICE%" /device=%~d0 /pbr /install /type=bootmgr /quiet
GOTO :done

:restorembr
echo ** Restoring previous boot loader to MBR and PBR for [%~d0]...
"%BOOTICE%" /device=%~d0 /mbr /restore /file="%_PREVMBR%" /quiet && "%BOOTICE%" /device=%~d0 /pbr /restore /file="%_PREVPBR%" /quiet && del /f "%_PREVMBR%" && del /f "%_PREVPBR%"
GOTO :done

:winefi
echo ** Switching to Windows UEFI Boot Loader...
REM Figure out which boot loader is boot.efi by missing files
IF NOT EXIST "%_GRUBEFI%" (
    echo ** ...from Windows UEFI Boot Loader...
    move /y "%_BOOTEFI%" "%_GRUBEFI%"
) ELSE IF NOT EXIST "%_REFEFI%" (
    echo ** ...from reFIND EFI Boot Loader...
    move /y "%_BOOTEFI%" "%_REFEFI%"
) ELSE (
    echo ** ...from unknown/other EFI Boot Loader...
    move /y "%_BOOTEFI%" "%_OTHEREFI%"
)
move /y "%_WINEFI%" "%_BOOTEFI%"
GOTO :done

:grubefi
echo ** Switching to GRUB2 UEFI Boot Loader for [%~d0]...
REM Figure out which boot loader is boot.efi by missing files
IF NOT EXIST "%_WINEFI%" (
    echo ** ...from Windows UEFI Boot Loader...
    move /y "%_BOOTEFI%" "%_WINEFI%"
) ELSE IF NOT EXIST "%_REFEFI%" (
    echo ** ...from reFIND EFI Boot Loader...
    move /y "%_BOOTEFI%" "%_REFEFI%"
) ELSE (
    echo ** ...from unknown/other EFI Boot Loader...
    move /y "%_BOOTEFI%" "%_OTHEREFI%"
)
move /y "%_GRUBEFI%" "%_BOOTEFI%"
GOTO :done

:refefi
echo ** Switching to REFIND EFI Boot Loader for [%~d0]...
REM Figure out which boot loader is boot.efi by missing files
IF NOT EXIST "%_WINEFI%" (
    echo ** ...from Windows UEFI Boot Loader...
    move /y "%_BOOTEFI%" "%_WINEFI%"
) ELSE IF NOT EXIST "%_GRUBEFI%" (
    echo ** ...from GRUB2 UEFI Boot Loader...
    move /y "%_BOOTEFI%" "%_GRUBEFI%"
) ELSE (
    echo ** ...from unknown/other EFI Boot Loader...
    move /y "%_BOOTEFI%" "%_OTHEREFI%"
)
move /y "%_REFEFI%" "%_BOOTEFI%"
GOTO :done

:otherefi
echo ** Switching to other/unknown EFI Boot Loader for [%~d0]...
REM Figure out which boot loader is boot.efi by missing files
IF NOT EXIST "%_WINEFI%" (
    echo ** ...from Windows UEFI Boot Loader...
    move /y "%_BOOTEFI%" "%_WINEFI%"
) ELSE IF NOT EXIST "%_GRUBEFI%" (
    echo ** ...from GRUB2 UEFI Boot Loader...
    move /y "%_BOOTEFI%" "%_GRUBEFI%"
) ELSE IF NOT EXIST "%_REFEFI%" (
    echo ** ...from reFIND EFI Boot Loader...
    move /y "%_BOOTEFI%" "%_REFEFI%"
)
move /y "%_OTHEREFI%" "%_BOOTEFI%"
GOTO :done

:done
echo ** Done.
pause

Edited by generalmx, 28 March 2016 - 01:40 AM.

  • wimb likes this

#2 steve6375

steve6375

    Platinum Member

  • Developer
  • 6351 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films,guitars
  •  
    United Kingdom

Posted 31 March 2016 - 09:41 AM

Useful!

 

The problem with having EFI boot files on an Easy2Boot USB drive is that:

1. You have to use FAT32 and so cannot have files larger than 4GB.

2. Some systems will not offer a boot menu that contains the Legacy\BIOS\CSM boot option for the E2B USB drive, so you cannot boot to E2B at all on these systems (you can only UEFI-boot). These systems have CSM enabled and will boot to E2B as long as there are no EFI boot files present.

 

It is best to not have an EFI folder on the E2B drive so that you can boot it on all systems.


  • wimb likes this





Also tagged with one or more of these keywords: tool, uefi, efi, grub4dos, windows, linux, grub2, refind

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users