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 0x8274 0x2001

#autonumber boot entries with a hyphen (2d) after the number
#write 0x8274 0x2d01

#autonumber all entries (ff) including non-boot entries with a space (20) after the number
#write 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 [email protected]^%] 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 0x8274 aabb line), can be used to auto-number all menu entries, however, all title commands will be numbered, even blank ones - e.g.

#autonumber all entries (including non-bootable entries) and add a hyphen (2d) after the number
write 0x8274 0x2dff

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

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
/hotkey
## 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
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).
^ 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

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

Contents of the patchg4d.g4b file

!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