057 – AUTOMATIC GRUB4DOS MENUS, USING HOTKEYS, HIDING THE MENU AND SILENT STARTUP

If you have followed some of these tutorials, you may have several grub4dos USB drives with different ‘payloads’ (e.g. ISO files, IMG files, IMA files, etc.) on them. You may find that you often change the contents of your USB drive and when you do, you have to remember to delete some of the menu entries whenever you remove a large file such as an ISO file. If you later add back the ISO file, you will also then need to add back in the correct menu entry. Alternatively, you have to comment out any menu entry that is no longer valid and then uncomment it later if you add back the relevant ISO file.

Note: Since 2021. there is now legacy grub4dos and uefi ‘grub4dos for uefi’ (aka grub4efi). The utilities used by these two versions are different and not interchangeable.

However, there is a way to automatically show only valid menu entries in a grub4dos menu. We can make grub4dos look for the ‘payload’ file and only list the menu entry if the payload file exists. Once you have set up your grub4dos menu, you can simply add or delete any ISO/IMG/IMA/WIM file from the USB drive and the menu entry for that file will automatically appear or disappear the next time you boot from the USB drive. Any grub4dos bootable USB drive you make in future can simply have your full menu copied to it, but it will only list menu entries for the files that exist on the USB drive.

The way to do this is to use the iftitle command (2012 grub4dos versions or later, or use latest version of RMPrepUSB to install grub4dos) which can be used to check for the existence of the payload file, for example:

  • iftitle [if exist (bd)/test.img] find /test.img on any drive and boot to it
  • map –mem (bd)/test.img (fd0)
  • map –hook
  • chainloader (fd0)+1

Note that no spaces must be present after the [ or before the ] brackets or it may not work correctly. The statement inside the [ ] brackets must equate to TRUE for the menu entry to be listed by grub4dos.

iftitle with menu auto-numbering

The latest chenall build of grub4dos (in latest RMPrepUSB) has the new command iftitle. Now you can make your menu.lst easily without needing any extra lines of code. Use RMPrepUSB and the Install grub4dos button. Here are some examples for the new iftitle command and also a patch to make the grub4dos menu automatically number itself:

  • #autonumber boot entries with a space (20) after the number
  • write –bytes=2 0x8274 0x2001
  • #autonumber boot entries with a hyphen (2d) after the number
  • #write –bytes=2 0x8274 0x2d01
  • #autonumber all entries (ff) including non-boot entries with a space (20) after the number
  • #write –bytes=2 0x8274 0x20ff
  • iftitle [if exist (hd0,0)/test.img] boot to (hd0,0)/test.img
  • map –mem (hd0,0)/test.img (fd0)
  • map –hook
  • chainloader (fd0)+1
  • iftitle [find /test.img] find /test.img on any drive and boot to it
  • find –set-root /test.img
  • map –mem /test.img (fd0)
  • map –hook
  • chainloader (fd0)+1
  • iftitle [if exist /win764.iso && checkrange 2,3 is64bit] for 64-bit system
  • #(load a 64-bit only ISO)
  • iftitle [if exist /win732.iso && checkrange 0,1 is64bit] for 32-bit system
  • #(load a 32-bit only OS)
  • iftitle [checkrange 0x80 read 0x8280] We have booted from a hard disk type device
  • #(load from hd here)
  • iftitle [ls /boot] check if the folder /boot exists
  • #(do stuff here)
  • iftitle [find –set-root –devices=h /bootmgr && call set bootmgr=%@root^%] load BOOTMGR of Windows VISTA/WIN7/WIN2008 on %bootmgr%
  • # we must set the current root again, as it may be changed by any following iftitle commands as each iftitle menu command is loaded and run…
  • find –set-root –devices=h /bootmgr
  • echo Booting from %bootmgr%/bootmgr …
  • chainloader /bootmgr

Beware: the syntax if very fussy – you must not have any white space immediately after the [ or before the ] brackets!

If a command returns <false> you can check the results using checkrange

iftitle [checkrange 0x01:0xff hiddenflag (hd0,0)] ptn 0 is hidden
root ()
iftitle [checkrange 0x0 hiddenflag (hd0,0)] ptn 0 is unhidden
root ()
iftitle [checkrange 0x01:0xff hiddenflag (hd0,1)] ptn 1 is hidden
root ()
iftitle [checkrange 0x0 hiddenflag (hd0,1)] ptn 1 is unhidden root ()

The auto-number patch (write –bytes=2 0x8274 aabb), can be used to auto-number all ‘runable’ menu entries. aa=character after number, bb= 0 or 1 to enable numbering.

  • #autonumber entries (all bootable entries) and add a hyphen (2d) after the number
  • write –bytes=2 0x8274 0x2d01
  • title This is menu title
  • root
  • title
  • root
  • title This is next menu title
  • root

will appear when you boot to grub4dos as

0-This is menu title
2-This is next menu title

Note: It is better to use the setmenu –auto-num-on command than write bytes to grub4dos in case of future changes. Type help setmenu to see all options.

Add hotkey menu entries

The latest chenall build of grub4dos can be used with a ‘hotkey’ executable to enable the user to use hotkeys such as F1 or CTRL+F1 to pick a menu entry. Then download the file ‘hotkey.zip‘ from the grubutils download page here and extract the file hotkey and place it on the USB drive also.

Note: Later 2021 versions of grub4dos include these utilities in the \ext folder within the grub4dos .zip file download. Older versions may not work with newer versions of grub4dos and vice versa! There are also grub4efi version of these utilities which only work with the uefi version of grub4dos (aka ‘grub4efi).

Now you can add hotkeys to your menu as follows:

  • ## run the hotkey command
  • /hotkey
  • ## run hotkey once to enable hotkeys.
  • ## this will also auto enable hotkeys in any sub-menu
  • ## “hotkey” = display usage and current global hotkeys (and install if not previously installed)
  • ## “hotkey -u” = uninstall (disable) hotkey (can be used in sub-menus)
  • ## “hotkey -nc” = only “hotkeys” and “arrow keys” accepted, other keys such as number keys or ‘c’ ‘e’ ‘b’ ‘p’ will be ignored. Executes menu immediately hotkey is pressed
  • ## “hotkey -nb” = only selects the correct menu entry when a hotkey is pressed – it does not auto boot it, the user must press the Enter key.
  • ## “hotkey -nc -nb” = only select menu entry when hotkey pressed, number keys and other keys ignored, must use hotkey or cursor keys to select a menu
  • ## “hotkey -A” = AutoHotKey feature – Jump to first letter of menu entry – e.g. press W to jump to a menu entry beginning with W
  • title ^F1 Press F1 to Boot from (hd0,0)
  • chainloader (hd0,0)+1
  • title ^Ctrl+F1 Press Ctrl+F1 to boot From (hd0,0)/ntldr
  • chainloader (hd0,0)/ntldr

  • iftitle [find –set-root –devices=h /bootmgr] ^F2 Press F2 to Boot Windows from hard disk
  • find –set-root –devices=h /bootmgr
  • chainloader /bootmgr

  • title ^R Reboot
  • reboot

  • title [H] Halt
  • halt

The menu presented to the user will look like this (if bootmgr is present on a hard disk device):

Press F1 to Boot From (hd0,0)
Press Ctrl+F1 to boot From (hd0,0)/ntldr
Press F2 to Boot Windows from hard disk
Reboot
[H] Halt

Note that the ^F1 portion of the menu text is not displayed if ‘hotkey’ has been loaded (but it will be displayed if hotkey has not been loaded and hotkeys will not work).

^ after title means do not show the hotkey

Instead of using ^, you can use square brackets:
title [F1] Press F1 to Boot From (hd0,0)
which will be displayed as
[F1] Press F1 to Boot From (hd0,0)
i.e. [xx] after title does not hide the characters and also sets the hotkey

If the user presses F1 then the first menu entry will immediately run. If you use hotkey -nc then pressing F1 will only select the menu entry but will not run it until the user hits {Enter}.

You can use Ctrl+, Shift+ and Alt+ as modifiers.

Note that not all key combinations actually generate a scan code (see here for a list) – e.g. ctrl-2 and ctrl+6 work, but other numbers such as ctrl+1 do not work!

Avoid using number keys (e.g ^1) as the user usually presses a number to select a menu entry. You can use Shift+1 or Alt+1 or Ctrl+1 instead.

Avoid using ^P, ^E and ^C (with or without SHIFT) as these are normal grub4dos menu edit keys. Also ‘B’ can be used instead of the [Enter] key to boot a selected menu item, so also avoid using ^B as a hotkey.

Note: GFXMenu does not support hotkeys and so will display the complete title entry – e.g. ^R Reboot

Global hotkeys

Latest version supports ‘Global’ hotkeys: (Apr 8 2015)

You can use ;; to separate grub4dos commands.

Hotkey [HOTKEY] “COMMAND”

Example: After using the following command, if you press F9, it will execute reboot from any menu (unless disabled).

hotkey [F9] reboot

Support for multiple commands at the same time using “”

hotkey [A] “echo Is test ;; pause test”

You can use @ to hide the commands without any screen display.

hotkey [F2] “@kernel /xxxx ;; initrd /xxxx ;; boot”

Use the hotkey without command to remove or disable the hotkey. For example: Disable registered F9 hotkey

hotkey [F9]

Note: Both hotkey modes (global+menu entry) can be used.

Autoboot

If you want to automatically boot from a USB drive depending on what boot file you have on your USB key, you can do it like this (you must have the file default in the root of the drive – this special file can be downloaded from the Beta Downloads page)

  • default /default
  • timeout 20
  • if exist /test.img default 0 && timeout 1
  • if exist /win764.iso && checkrange 2,3 is64bit && default 1 && timeout 1
  • if exist /win732.iso && checkrange 0,1 is64bit && default 2 && timeout 1
  • title find /test.img on any drive and boot to it
  • find –set-root /test.img
  • map –mem /test.img (fd0)
  • map –hook
  • chainloader (fd0)+1
  • title 64-bit system
  • (load a 64-bit only ISO)

  • title 32-bit system
  • (load a 32-bit only OS)

Suppress grub4dos startup/bootup messages (silent)

grub4dos (grldr) echoes some debug messages to the screen on startup as it boots, which you may not want the user to see. They can be patched out by using a grub4dos batch file.

Run this patchg4d.g4b (patches 0.4.5c only) batch file at the start of your menu.lst – it will automatically make the code patches to blank out the messages the first time it is run.

It only needs to be run once (on a real system, not QEMU) for the patch to take affect.

e.g.

# patch out messages for silent startup

/patchg4d.g4b

To patch 0.4.6a versions of grub4dos, see the Easy2Boot project and the \_ISO\Sample_MyE2B.cfg file for instructions and the latest version of the patch code.

Contents of the patchg4d.g4b file (for 0.4.5c)

  • !BAT
  • #patch to get rid of grub4dos startup text!
  • #first check if already done and exit if it has!
  • cat –locate=”Running menu” –number=1 ()/grldr > nul || goto :EOF
  • #Max locate string length=16 bytes
  • #Do not exceed original string length
  • #End of string is a 0 byte (\0)
  • cat –locate=”Try (hd0,0 )” –number=1 –replace=\0\0 (hd0)+20 > nul
  • cat –locate=”EXT2: ” –number=1 –replace=\0\0 (hd0)+20 > nul
  • cat –locate=”NTFS5: ” –number=1 –replace=\0\0 (hd0)+20 > nul
  • cat –locate=”FAT32: ” –number=1 –replace=\0\0 (hd0)+20 > nul
  • cat –locate=”FAT16: ” –number=1 –replace=\0\0 (hd0)+20 > nul
  • cat –locate=”FAT12: ” –number=1 –replace=\0\0 (hd0)+20 > nul
  • cat –locate=”non-MS: ” –number=1 –replace=\0\0 (hd0)+20 > nul
  • cat –locate=”skip ” –number=1 –replace=\0\0 (hd0)+20 > nul
  • cat –locate=”Extended: ” –number=1 –replace=\0\0 (hd0)+20 > nul
  • cat –locate=”invalid or null” –number=1 –replace=\0\0 (hd0)+20 > nul
  • cat –locate=”Initialize var” –number=1 –replace=\0\0 ()/grldr > nul
  • cat –locate=”Starting cmain” –number=1 –replace=\0\0 ()/grldr > nul
  • cat –locate=”Running menu” –number=1 –replace=\0\0 ()/grldr > nul
  • #Default Help text:
  • #patch default: Use the %c and %c keys to highlight an entry.
  • #replace with: Use the four cursor keys to select an entry.\0
  • #other strings:
  • # Press ENTER or ‘b’ to boot. Press ‘e’ to edit the commands before booting, or ‘c’ for a command line.
  • # Press ‘p’ to gain privileged control etc….
  • #change message to use four cursor keys as < and > are valid keys
  • cat –locate=”Use the %c and %” –number=1 –replace=”Use the four cursor keys to select an entry.\0″ ()/grldr > nul
  • #Don’t tell user how to edit the menu!
  • cat –locate=”Press \’e\’ to edi” –number=1 –replace=”\0″ ()/grldr > nul
  • echo Patching finished – press a key to reboot…
  • pause
  • reboot

SHOW A BITMAP BEFORE THE MENU IS DISPLAYED

  • graphicsmode -1 800 > nul
  • splashimage /background.bmp > nul
  • # set cursor mode and show graphics
  • call Fn.70 3
  • clear
  • # hide cursor
  • call Fn.5 0 38
  • # delay 3 secs
  • call Fn.73 3
  • # restore cursor mode
  • call Fn.70 2
  • title xxxxxx
  • (etc)

SPLASH SCREEN ONLY (NO MENU ON BOOT)

The user will just see the wallpaper image with no text or highlight line at all – if he presses F4 or F5 then the menu entry chosen will be executed immediately. Cut and paste the lines so that you get the correct number of spaces!

  • clear
  • debug off
  • graphicsmode -1 800 100:1000 24:32 > nul
  • splashimage /aurora24.bmp.gz > nul
  • # load hotkey utility
  • /hotkey
  • timeout 10
  • default 0
  • #set black text on black background so we see no menu or help text!
  • color black/black
  • # display the grub4dos heading and cover up grub4dos version text
  • write (md)0x220+1 !BAT\necho -n -P:0000 $[0133] \0
  • initscript (md)0x220+1
  • # set 0 size border
  • /menusetting.gz 0 0 0 0 0 0 0 0
  • title ^F5 Reboot (F5) \nThis will reboot your PC
  • reboot
  • title ^F4 Halt (F4)\nThis will halt your PC
  • color standard=0x04
  • echo This text will be in red! Hit a key to HALT!
  • pause
  • halt

Only boot grub4dos if hotkey pressed

You can install grub4dos using grubinst.exe so that grldr and the grub4dos menu will only be loaded if a hotkey is pressed by the user.

A (modified) copy of grubinst.exe is used by RMPrepUSB and can be found in the RMPrepUSB installation folder (press F3 to view the installation folder in Explorer).

Here is what the user will see when grub4dos is installed in this way:

1. Reboot or Switch on the computer

2. Normal PC startup screen appears

3. grub4dos stage 1 bootloader will run and this message will be displayed on the screen:

Press F10 to start GRUB, any other key to boot to previous MBR …

Timeout: 3

4. The timeout will count down to 0 seconds (unless F10 is pressed) and then will boot from the boot disk as normal (using the bootloader that was on the drive before grub4dos was installed).

If F10 is pressed, then the grldr stage 2 file is loaded which will look for your menu.lst file and load it and display the grub4dos menu.

The command line to install grub4dos in this way to your drive (assuming your drive is hd2 and you want a 3 second timeout) is:

grubinst.exe –boot-prevmbr-first –time-out=3 –hot-key=0x4400 –key-name=F10 (hd2)

Some Scan codes you can use are:

F12=8600, F11=8500, F10=4400, F9=4300, F8=4200, F7=4100, F6=4000, F5=3F00, F4=3E00, F3=3D00, F2=3C00, F1=3B00, ESC=011B (use pause –test-key to print any key scan code)

Note: It is best to avoid using F12 or F11 as these often do not work on some models of Notebooks due to non-standard notebook keyboards.

All the command line switches for grubinst.exe can be found here.

To change the message, boot from the disk to grub4dos and use the following grub4dos patch command from the grub4dos command shell.

cat –locate=” to start GRUB,” –number=1 –replace=” to Restore the Hard Disk Image.\0″ (hd0)+20 > nul

Note that the –replace string must not be longer than 53 characters or you will corrupt the boot code!

If you want to entirely suppress the ‘Press F10 to start…’ message and the Timeout message and just have the 3..2..1..0 countdown displayed, the following grub4dos commands will patch the disk that grub4dos is installed on, to suppress these messages (omit !BAT if you are typing this into the console):

  • !BAT
  • cat –locate=”Press ” –number=2 –replace=\0\0 (hd0)+20 > nul
  • cat –locate=” to start GRUB,” –number=1 –replace=\0\0 (hd0)+20 > nul
  • cat –locate=”Timeout : ” –number=1 –replace=\0\0 (hd0)+20 > nul
  • cat –locate=”F10″ –number=1 –replace=\0\0 (hd0)+20 > nul
  • cat –locate=”hot-key” –number=1 –replace=\0\0 (hd0)+20 > nul

You can make this into a text batch file – e.g. nomsg.g4b, by keeping the first line of text “!BAT” , and then run the batch file from the command line by typing /nomsg.g4b.

To suppress all early startup messages from the MBR boot code (including error messages and timeout counter), use:

cat –locate=\x2e\xac\x3c\x00\x75\xf6\xc3 –number=1 –replace=\xc3 (hd0)0+1 > nul

DISGUISE (OBFUSCATE) THE MENU.LST FILE

To obscure the text in the menu.lst file so that it cannot be easily read as plain text by a curious end user, compress it using 7Zip to the Gzip format (but keep the filename as menu.lst).

Easy2Boot (E2B) is popular multiboot USB solution that also contains agFM and Ventoy. It supports both Legacy and UEFI.
Simply copy on your bootable ISO files to the E2B USB drive and boot! Boot to DOS, Linux, Windows Install ISOs (XP>Win11),
automate Windows installs, WIM files, VHD files, images of flash drives, Linux ISO+persistence, etc.
E2B is unique in that it uses partition images which allows you to directly boot from Secure Boot images (no need to disable Secure Boot or run MOK manager or modify your UEFI BIOS).

eBooks (multi-buy discount 10%)

The following eBooks (in PDF format) are available from the developer (rated 4.5/5 stars).

Also visit Easy2Boot.xyz and the my blog – please subscribe for the latest news, tips, USB boot articles and news of free eBook updates.