57 - Automatic grub4dos menus, using hotkeys, hiding the menu and silent startup

Unique hits statistics in vBulletin

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.

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


title This is next menu title

will appear when you boot to grub4dos as

0-This is menu title

2-This is next menu title

Add hotkey menu entries

The latest chenall build of grub4dos can be used with a new '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. Now you can add hotkeys to your menu as follows:

## run the hotkey command
## run hotkey once to enable hotkeys.
## this will also auto enable hotkeys in any sub-menu
## "hotkey" = when user presses hotkey execute that menu entry immediately
## "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' or 'e' etc. 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" = select menu entry when hotkey pressed, number keys and other keys ignored, must use hotkey or cursor keys to select a menu
## NEW! 2015-04-08 - -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

title [H] 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
[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).
^ means do not show the hotkey
[xx] means display the hotkey
If the user presses F1 the first menu entry will immediately run. If you use hotkey -nc then pressing F1 will only select the first 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 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


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.

# patch out messages for silent startup

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

Contents of the patchg4d.g4b file

#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...


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
# hide cursor
call Fn.5 0 38
# delay 3 secs
call Fn.73 3
# restore cursor mode
call Fn.70 2

title xxxxxx

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!

debug off
graphicsmode -1 800 100:1000 24:32 > nul
splashimage /aurora24.bmp.gz > nul
# load hotkey utility
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

title ^F4 Halt (F4)\nThis will halt your PC
color standard=0x04
echo This text will be in red! Hit a key to 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 used.
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):

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.

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).


The Methods below are deprecated. The new grub4dos iftitle command makes these workarounds no longer necessary! They are included for the interested reader only!

NOTE: PLEASE USE THE LATEST CHENALL BUILD GRUB4DOS VERSION (also included with the latest RMPrepUSB download)

Note: Method 2 uses two menu files (menu.lst and main.lst) and allows you to expand the scheme so that you could modify more than one menu file (e.g. you can have main.lst which would load either main1.lst, main2.lst, main3.lst, etc.). This is useful if your menu system uses multiple menu files. If you only use one menu.lst file (probably 90% of users!) then you can use just a single self-modifying file (i.e. one menu.lst) as in Method 1.

(OLD) Method 1 - Method using a single MENU.LST file

1. Cut and paste the lines in the template example below (up to the # YOUR NORMAL GRUB4DOS MENU GOES HERE position) into your existing menu.lst file (insert at the very top of your menu.lst). The --replace=" " text should have EXACTLY 3 SPACES between the double-quotes.

2. Modify your existing menu entries by adding four-character identifiers (#XX<space>) to the beginning of each of your menu lines as detailed above and as shown in the example menu below.

3. Modify the 'if exist' lines (just below the #PLACE ALL YOUR TEST ENTRIES HERE text) so that they match the identifiers you used in step 2 and match the iso or 'payload' file used by each menu entry.

Alternatively, just use the utility to convert your existing menu.lst
Download the GrubAuto utility to convert your current menu.lst to an 'automated' one!

1. Click on the Load Menu button and select your current menu.lst file
2. Click on Copy To ClipBoard
3. Paste the contents into a new menu.lst file and save it.

Menu.lst template (green text is the original menu text):

cat /menu.lst > (md)800+100
cat --locate="cat /menu.lst" --replace="#" (md)800+100 >nul
cat --locate="configfile (md)" --replace="#" (md)800+100 >nul

if exist /trk3/menu.lst cat --locate=#TR --replace=" " (md)800+100 >nul
if exist /dban-2.2.6_i586.iso cat --locate=#DK --replace=" " (md)800+100 >nul

#load menu from ram
echo Loading modified menu.lst...
configfile (md)800+100

# Place all your normal grub4dos menu entries here
# No special changes are needed except for adding the #XX characters at the beginning of each command line

color black/cyan yellow/cyan
timeout 5
default 1

#TR title Trinity Rescue v3.4
# You must set your own drive's volume label on the next line or it will not load correctly!
#TR configfile /trk3/menu.lst

#DK title Dariks Boot and Nuke dban-2.2.6_i586.iso
#DK find --set-root --ignore-floppies /dban-2.2.6_i586.iso
#DK map --mem --heads=0 --sectors-per-track=0 /dban-2.2.6_i586.iso (0xff)
#DK map --hook
#DK root (0xff)
#DK chainloader (0xff)

How it works

This menu.lst is self-modifying. It loads itself into memory (1st line) and then comments out the lines that loaded it into memory in the next two lines (otherwise it would loop and continuously keep loading itself into memory!). The self-modification is done by looking for two command lines and then commenting those lines out.
The 'if exist' lines are not commented out, so they will actually run twice and the second time menu.lst is loaded from memory, it will change itself - but it does not matter (except it will take slightly longer to run) because the menu lines will have already been modified when the 'if exist' lines ran the first time!


Method 2 (using two menu files)

1. First rename your menu.lst file to main.lst

2. Next add 4 characters to the beginning of each non-commented menu entry in the main.lst file. The first character you add should be a #, the next can be any 2 alphanumeric characters and the last one should be a space - thus making four characters in total added to each non-commented line. The 2nd and 3rd character should be different for each menu entry.

For example, suppose you have menu entries for KonBoot and MemTest86+ as shown below:

title KONBOOT\nPress Enter once Kon-Boot starts to run then allow it to reboot your system to Windows
errorcheck off
map --mem /FD0-konboot-v1.1-2in1.img (fd0)
# If current grub4dos root drive (UDF)(stored at 0x8280 in memory) is 0x80 (=hd0), remap (hd0) to (hd1), else do nothing
checkrange 0x80 read 0x8280 && map (hd0) (hd1)
# If current grub4dos root drive (UDF) (stored at 0x8280 in memory) is 0x80 (=hd0), remap (hd1) (=internal hard drive) to (hd0), else do nothing
checkrange 0x80 read 0x8280 && map (hd1) (hd0)
map --hook
chainloader (fd0)+1
rootnoverify (fd0)

title MEMTEST86+
find --set-root /mt410.iso
map /mt410.iso (hd32)
map --hook
root (hd32)
chainloader (hd32)

This could be changed to:

#KB title KONBOOT\nPress Enter once Kon-Boot starts to run then allow it to reboot your system to Windows
#KB errorcheck off
#KB map --mem /FD0-konboot-v1.1-2in1.img (fd0)
# If current grub4dos root drive (UDF)(stored at 0x8280 in memory) is 0x80 (=hd0), remap (hd0) to (hd1), else do nothing
#KB checkrange 0x80 read 0x8280 && map (hd0) (hd1)
# If current grub4dos root drive (UDF) (stored at 0x8280 in memory) is 0x80 (=hd0), remap (hd1) (=internal hard drive) to (hd0), else do nothing
#KB checkrange 0x80 read 0x8280 && map (hd1) (hd0)
#KB map --hook
#KB chainloader (fd0)+1
#KB rootnoverify (fd0)

#MT title MEMTEST86+
#MT find --set-root /mt410.iso
#MT map /mt410.iso (hd32)
#MT map --hook
#MT root (hd32)
#MT chainloader (hd32)

Note that we have now commented out all the menu lines!
The lines that were already commented out with a single # do not need to be changed.

3. To the top of the main.lst file add these two lines:

map --unhook
map --unmap=hd60

This undoes the mapping that we will need to do in menu.lst (see below).

4. Now make a new file called menu.lst and add the contents as shown below

# prevent abort if error
errorcheck off
map --mem /main.lst (hd60)
map --hook
# enable menu entries if payload file exists - ensure there are THREE SPACES between the replace doublequotes
if exist /FD0-konboot-v1.1-2in1.img cat --locate=#KB --replace=" " (hd60)+50 > nul
if exist /mt410.iso cat --locate=#MT --replace=" " (hd60)+50 > nul
# put more entries here
echo Loading Main.lst
#load menu from ram
configfile (hd60)+50

Notice that you will need to add a line that tests for the presence of each 'payload' file (shown highlighted in grey for this example). Make sure that there are THREE SPACES between the double quotes in each line of the text
--replace=" "

5. Now test the USB drive (you can use RMPrepUSB and press F11 to run QEMU). If some menu entries appear to be missing, check that the menu.lst file is checking the correct location for the 'payload' file including any path (not case sensitive). e.g. if exist /iso/mt410.iso or if exist /images/linux/gl40.iso .

That's it! Now you can add or delete any payload (ISO/IMG/IMA etc) file from your USB drive or make a new grub4dos bootable USB drive and just add the payload files that you want. You can always use the same menu.lst and main.lst files. If you ever need to add another menu, simply update both menu.lst and main.lst. You will never need to delete any menu entries ever again and you can build up your main.lst menu with working entries over the next few years without needing to edit it whenever you make a new USB drive.

If your main.lst menu file grows to more than 25K in size, change the