1 #************************************************************************************
2 # Copyright (c) 2020, longpanda <admin@ventoy.net>
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, see <http://www.gnu.org/licenses/>.
17 #************************************************************************************
19 function ventoy_power {
22 echo '<0> Return to menu'
23 echo -e '\nPlease enter your choice:'
28 if [ "$vtOpt" = "1" ]; then
29 echo -e '\n\nSystem is rebooting ... \n'
32 elif [ "$vtOpt" = "2" ]; then
33 echo -e '\n\nSystem is halting ... \n'
39 function get_os_type {
41 for file in "efi/microsoft" "sources/boot.wim" "boot/bcd" "bootmgr.efi" "boot/etfsboot.com"; do
42 if [ -e $1/$file ]; then
48 if [ -n "${vtdebug_flag}" ]; then
53 function locate_initrd {
54 vt_linux_locate_initrd
56 if [ -n "${vtdebug_flag}" ]; then
62 function find_wim_file {
65 for file in "sources/boot.wim" "sources/BOOT.WIM" "Sources/Win10PEx64.WIM" "boot/BOOT.WIM" "winpe_x64.wim"; do
66 if [ -e $1/$file ]; then
67 set ventoy_wim_file=$1/$file
73 function distro_specify_initrd_file {
74 if [ -e (loop)/boot/all.rdz ]; then
75 vt_linux_specify_initrd_file /boot/all.rdz
76 elif [ -e (loop)/boot/xen.gz ]; then
77 if [ -e (loop)/install.img ]; then
78 vt_linux_specify_initrd_file /install.img
80 elif [ -d (loop)/casper ]; then
81 if [ -e (loop)/casper/initrd ]; then
82 vt_linux_specify_initrd_file /casper/initrd
84 if [ -e (loop)/casper/initrd-oem ]; then
85 vt_linux_specify_initrd_file /casper/initrd-oem
87 elif [ -e (loop)/boot/grub/initrd.xz ]; then
88 vt_linux_specify_initrd_file /boot/grub/initrd.xz
89 elif [ -e (loop)/initrd.gz ]; then
90 vt_linux_specify_initrd_file /initrd.gz
91 elif [ -e (loop)/slax/boot/initrfs.img ]; then
92 vt_linux_specify_initrd_file /slax/boot/initrfs.img
93 elif [ -e (loop)/pmagic/initrd.img ]; then
94 vt_linux_specify_initrd_file /pmagic/initrd.img
95 elif [ -e (loop)/boot/initrd.xz ]; then
96 vt_linux_specify_initrd_file /boot/initrd.xz
97 elif [ -f (loop)/boot/initrd ]; then
98 vt_linux_specify_initrd_file /boot/initrd
99 elif [ -f (loop)/boot/x86_64/loader/initrd ]; then
100 vt_linux_specify_initrd_file /boot/x86_64/loader/initrd
101 elif [ -f (loop)/boot/initramfs-x86_64.img ]; then
102 vt_linux_specify_initrd_file /boot/initramfs-x86_64.img
108 function distro_specify_initrd_file_phase2 {
109 if [ -f (loop)/boot/initrd.img ]; then
110 vt_linux_specify_initrd_file /boot/initrd.img
111 elif [ -f (loop)/Setup/initrd.gz ]; then
112 vt_linux_specify_initrd_file /Setup/initrd.gz
113 elif [ -f (loop)/isolinux/initramfs ]; then
114 vt_linux_specify_initrd_file /isolinux/initramfs
115 elif [ -f (loop)/boot/iniramfs.igz ]; then
116 vt_linux_specify_initrd_file /boot/iniramfs.igz
121 function uefi_windows_menu_func {
124 if [ "$ventoy_compatible" = "NO" ]; then
126 if [ -n "$ventoy_wim_file" ]; then
127 vt_windows_locate_wim $ventoy_wim_file
131 vt_windows_chain_data ${1}${chosen_path}
133 if [ -n "${vtdebug_flag}" ]; then
137 if [ -n "$vtoy_chain_mem_addr" ]; then
138 terminal_output console
139 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
142 echo "chain empty failed"
147 function uefi_linux_menu_func {
148 if [ "$ventoy_compatible" = "NO" ]; then
149 vt_load_cpio ${vtoy_path}/ventoy.cpio $2 $1
151 vt_linux_clear_initrd
153 if [ -d (loop)/pmagic ]; then
154 vt_linux_specify_initrd_file /pmagic/initrd.img
156 for file in "boot/grub/grub.cfg" "EFI/BOOT/grub.cfg" "EFI/boot/grub.cfg" "efi/boot/grub.cfg" "EFI/BOOT/BOOTX64.conf" "/grub/grub.cfg"; do
157 if [ -e (loop)/$file ]; then
158 vt_linux_parse_initrd_grub file (loop)/$file
163 # special process for special distros
164 if [ -d (loop)/loader/entries ]; then
165 set LoadIsoEfiDriver=on
166 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
167 elif [ -d (loop)/boot/grub ]; then
168 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
171 if [ -e (loop)/syslinux/alt0/full.cz ]; then
172 set LoadIsoEfiDriver=on
173 set FirstTryBootFile='@EFI@BOOT@grubx64.efi'
176 distro_specify_initrd_file
178 vt_linux_initrd_count vtcount
179 if [ $vtcount -eq 0 ]; then
180 distro_specify_initrd_file_phase2
182 if [ "$vt_efi_dir" = "NO" ]; then
183 if [ -f (loop)/efi.img ]; then
184 vt_add_replace_file 0 "initrd"
192 vt_linux_chain_data ${1}${chosen_path}
194 if [ -n "$vtoy_chain_mem_addr" ]; then
195 terminal_output console
196 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} FirstTry=${FirstTryBootFile} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
199 echo "chain empty failed"
205 function uefi_iso_menu_func {
207 if [ -d (loop)/ ]; then
211 if [ -n "$vtisouefi" ]; then
212 set LoadIsoEfiDriver=on
214 elif vt_check_mode 2; then
215 set LoadIsoEfiDriver=on
217 unset LoadIsoEfiDriver
220 vt_chosen_img_path chosen_path
222 if vt_is_udf ${1}${chosen_path}; then
223 set ventoy_fs_probe=udf
225 set ventoy_fs_probe=iso9660
228 loopback loop ${1}${chosen_path}
231 if [ -d (loop)/EFI ]; then
233 elif [ -d (loop)/efi ]; then
239 if [ -n "$vtcompat" ]; then
240 set ventoy_compatible=YES
242 elif vt_check_mode 1; then
243 set ventoy_compatible=YES
245 vt_check_compatible (loop)
248 vt_img_sector ${1}${chosen_path}
250 if [ "$vtoy_os" = "Windows" ]; then
251 if [ "$ventoy_fs_probe" = "iso9660" ]; then
252 set ventoy_compatible=YES
253 elif [ -f (loop)/HBCD_PE.ini ]; then
254 set ventoy_compatible=YES
257 uefi_windows_menu_func $1 ${chosen_path}
259 uefi_linux_menu_func $1 ${chosen_path}
262 terminal_output gfxterm
265 function uefi_iso_memdisk {
266 vt_chosen_img_path chosen_path
268 echo 'Loading ISO file to memory ...'
269 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
271 terminal_output console
272 chainloader ${vtoy_path}/ventoy_x64.efi memdisk env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_iso_buf_addr}:size:${vtoy_iso_buf_size}
277 function legacy_windows_menu_func {
280 if [ "$ventoy_compatible" = "NO" ]; then
282 if [ -n "$ventoy_wim_file" ]; then
283 vt_windows_locate_wim $ventoy_wim_file
284 elif [ -n "${vtdebug_flag}" ]; then
285 echo No wim file found
289 vt_windows_chain_data ${1}${chosen_path}
291 if [ -n "${vtdebug_flag}" ]; then
295 if [ -n "$vtoy_chain_mem_addr" ]; then
296 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft
297 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
300 echo "chain empty failed"
305 function legacy_linux_menu_func {
306 if [ "$ventoy_compatible" = "NO" ]; then
307 vt_load_cpio $vtoy_path/ventoy.cpio $2 $1
309 vt_linux_clear_initrd
311 if [ -d (loop)/pmagic ]; then
312 vt_linux_specify_initrd_file /pmagic/initrd.img
314 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
315 if [ -d (loop)/$dir ]; then
316 vt_linux_parse_initrd_isolinux (loop)/$dir/
321 # special process for special distros
323 if [ -d (loop)/arch/boot/syslinux ]; then
324 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
325 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
328 elif [ -d (loop)/manjaro ]; then
329 if [ -e (loop)/boot/grub/kernels.cfg ]; then
330 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
332 elif [ -e (loop)/boot/grub/grub.cfg ]; then
333 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
336 distro_specify_initrd_file
338 vt_linux_initrd_count vtcount
339 if [ $vtcount -eq 0 ]; then
340 distro_specify_initrd_file_phase2
346 vt_linux_chain_data ${1}${chosen_path}
348 if [ -n "${vtdebug_flag}" ]; then
352 if [ -n "$vtoy_chain_mem_addr" ]; then
353 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag}
354 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
357 echo "chain empty failed"
362 function legacy_iso_menu_func {
364 if [ -d (loop)/ ]; then
368 vt_chosen_img_path chosen_path
370 if vt_is_udf ${1}${chosen_path}; then
371 set ventoy_fs_probe=udf
373 set ventoy_fs_probe=iso9660
376 loopback loop ${1}${chosen_path}
380 if [ -n "$vtcompat" ]; then
381 set ventoy_compatible=YES
383 elif vt_check_mode 1; then
384 set ventoy_compatible=YES
386 vt_check_compatible (loop)
389 vt_img_sector ${1}${chosen_path}
391 if [ "$vtoy_os" = "Windows" ]; then
392 if [ "$ventoy_fs_probe" = "iso9660" ]; then
393 set ventoy_compatible=YES
394 elif [ -f (loop)/HBCD_PE.ini ]; then
395 set ventoy_compatible=YES
398 legacy_windows_menu_func $1 ${chosen_path}
400 legacy_linux_menu_func $1 ${chosen_path}
404 function legacy_iso_memdisk {
405 vt_chosen_img_path chosen_path
407 linux16 $vtoy_path/memdisk iso raw
408 echo "Loading ISO file to memory ..."
409 initrd16 ${1}${chosen_path}
413 function common_menuentry {
414 if [ "$grub_platform" = "pc" ]; then
415 if vt_check_mode 0; then
416 legacy_iso_memdisk $iso_path
418 legacy_iso_menu_func $iso_path
421 if vt_check_mode 0; then
422 uefi_iso_memdisk $iso_path
424 uefi_iso_menu_func $iso_path
430 #############################################################
431 #############################################################
432 #############################################################
433 ####### Main Process ###########
434 #############################################################
435 #############################################################
436 #############################################################
438 set VENTOY_VERSION="1.0.09"
441 # Default menu display mode, you can change it as you want.
444 set VTOY_DEFAULT_MENU_MODE=0
449 set VTOY_MEM_DISK_STR="MEMDISK"
450 set VTOY_ISO_RAW_STR="ISO RAW"
451 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
453 set VTOY_F2_CMD="ventoy_power"
455 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
456 set VTOY_F3_CMD="vt_dynamic_menu 1 1"
457 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:TreeView"
459 set VTOY_F3_CMD="vt_dynamic_menu 1 0"
460 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:ListView"
463 if [ "$grub_platform" = "pc" ]; then
464 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION BIOS www.ventoy.net"
466 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION UEFI www.ventoy.net"
469 vt_device $root vtoy_dev
471 if [ "$vtoy_dev" = "tftp" ]; then
472 set vtoy_path=($root)
473 for vtid in 0 1 2 3; do
474 if [ -d (hd$vtid,2)/ventoy ]; then
475 set iso_path=(hd$vtid,1)
480 set vtoy_path=($root)/ventoy
481 set iso_path=($vtoy_dev,1)
486 if [ -f $iso_path/ventoy/ventoy.json ]; then
487 vt_load_plugin $iso_path
490 if [ -n "$vtoy_gfxmode" ]; then
491 set gfxmode=$vtoy_gfxmode
493 set gfxmode=1920x1080,1366x768,1024x768
496 if [ -n "$vtoy_theme" ]; then
497 set theme=$vtoy_theme
499 set theme=$prefix/themes/ventoy/theme.txt
502 terminal_output gfxterm
504 #colect all image files (iso files)
505 set ventoy_img_count=0
506 vt_list_img $iso_path ventoy_img_count
509 if [ $ventoy_img_count -gt 0 ]; then
510 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
516 menuentry "No ISO files found (Press enter to reboot ...)" {
517 echo -e "\n Rebooting ... "