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_pause {
20 if [ -n "${vtdebug_flag}" ]; then
21 echo "press Enter to continue ......"
26 function ventoy_debug_pause {
27 if [ -n "${vtdebug_flag}" ]; then
28 echo "press Enter to continue ......"
34 function ventoy_power {
35 configfile $prefix/power.cfg
38 function ventoy_diagnosis {
39 configfile $prefix/debug.cfg
42 function ventoy_localboot {
43 configfile $prefix/localboot.cfg
46 function get_os_type {
49 for file in "efi/microsoft/boot/bcd" "sources/boot.wim" "boot/bcd" "bootmgr.efi" "boot/etfsboot.com" ; do
50 if vt_file_exist_nocase (loop)/$file; then
56 if [ -n "${vtdebug_flag}" ]; then
61 function vt_check_compatible_pe {
62 #Check for PE without external tools
63 if [ -f $1/HBCD_PE.ini ]; then
64 set ventoy_compatible=YES
68 function locate_initrd {
69 vt_linux_locate_initrd
71 if [ -n "${vtdebug_flag}" ]; then
78 vt_windows_locate_wim_patch (loop)
80 if [ -n "${vtdebug_flag}" ]; then
81 echo '###############################################'
83 echo '###############################################'
88 function distro_specify_wim_patch {
89 if [ -d (loop)/h3pe ]; then
90 vt_windows_collect_wim_patch wim /BOOT/H3_10PE.WIM
91 vt_windows_collect_wim_patch wim /BOOT/H3_7PE.WIM
92 vt_windows_collect_wim_patch wim /BOOT/H3_8PE.WIM
93 vt_windows_collect_wim_patch wim /BOOT/H3_81PE.WIM
97 function distro_specify_wim_patch_phase2 {
98 if [ -f (loop)/boot/boot.wim ]; then
99 vt_windows_collect_wim_patch wim /boot/boot.wim
104 function distro_specify_initrd_file {
105 if [ -e (loop)/boot/all.rdz ]; then
106 vt_linux_specify_initrd_file /boot/all.rdz
107 elif [ -e (loop)/boot/xen.gz ]; then
108 if [ -e (loop)/install.img ]; then
109 vt_linux_specify_initrd_file /install.img
111 elif [ -d (loop)/casper ]; then
112 if [ -e (loop)/casper/initrd ]; then
113 vt_linux_specify_initrd_file /casper/initrd
115 if [ -e (loop)/casper/initrd-oem ]; then
116 vt_linux_specify_initrd_file /casper/initrd-oem
118 elif [ -e (loop)/boot/grub/initrd.xz ]; then
119 vt_linux_specify_initrd_file /boot/grub/initrd.xz
120 elif [ -e (loop)/initrd.gz ]; then
121 vt_linux_specify_initrd_file /initrd.gz
122 elif [ -e (loop)/slax/boot/initrfs.img ]; then
123 vt_linux_specify_initrd_file /slax/boot/initrfs.img
124 elif [ -e (loop)/pmagic/initrd.img ]; then
125 vt_linux_specify_initrd_file /pmagic/initrd.img
126 elif [ -e (loop)/boot/initrd.xz ]; then
127 vt_linux_specify_initrd_file /boot/initrd.xz
128 elif [ -f (loop)/boot/initrd ]; then
129 vt_linux_specify_initrd_file /boot/initrd
130 elif [ -f (loop)/boot/x86_64/loader/initrd ]; then
131 vt_linux_specify_initrd_file /boot/x86_64/loader/initrd
132 elif [ -f (loop)/boot/initramfs-x86_64.img ]; then
133 vt_linux_specify_initrd_file /boot/initramfs-x86_64.img
140 function distro_specify_initrd_file_phase2 {
141 if [ -f (loop)/boot/initrd.img ]; then
142 vt_linux_specify_initrd_file /boot/initrd.img
143 elif [ -f (loop)/Setup/initrd.gz ]; then
144 vt_linux_specify_initrd_file /Setup/initrd.gz
145 elif [ -f (loop)/isolinux/initramfs ]; then
146 vt_linux_specify_initrd_file /isolinux/initramfs
147 elif [ -f (loop)/boot/iniramfs.igz ]; then
148 vt_linux_specify_initrd_file /boot/iniramfs.igz
149 elif [ -f (loop)/initrd-x86_64 ]; then
150 vt_linux_specify_initrd_file /initrd-x86_64
151 elif [ -f (loop)/live/initrd.img ]; then
152 vt_linux_specify_initrd_file /live/initrd.img
153 elif [ -f (loop)/initrd.img ]; then
154 vt_linux_specify_initrd_file /initrd.img
155 elif [ -f (loop)/sysresccd/boot/x86_64/sysresccd.img ]; then
156 vt_linux_specify_initrd_file /sysresccd/boot/x86_64/sysresccd.img
161 function uefi_windows_menu_func {
164 if [ "$ventoy_compatible" = "NO" ]; then
166 if [ "$ventoy_fs_probe" = "iso9660" ]; then
168 vt_iso9660_nojoliet 1
172 for file in "efi/microsoft/boot/bcd"; do
173 vt_windows_collect_wim_patch bcd (loop)/$file
176 vt_windows_count_wim_patch vt_wim_cnt
177 if [ $vt_wim_cnt -eq 0 ]; then
178 distro_specify_wim_patch_phase2
185 vt_windows_chain_data ${1}${chosen_path}
188 if [ -n "$vtoy_chain_mem_addr" ]; then
189 terminal_output console
190 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
193 echo "chain empty failed"
198 function uefi_linux_menu_func {
199 if [ "$ventoy_compatible" = "NO" ]; then
201 if [ "$ventoy_fs_probe" = "udf" ]; then
203 set ventoy_fs_probe=iso9660
207 vt_load_cpio ${vtoy_path}/ventoy.cpio $2 $1
209 vt_linux_clear_initrd
211 if [ -d (loop)/pmagic ]; then
212 vt_linux_specify_initrd_file /pmagic/initrd.img
214 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
215 if [ -e (loop)/$file ]; then
216 vt_linux_parse_initrd_grub file (loop)/$file
221 # special process for special distros
222 if [ -d (loop)/loader/entries ]; then
223 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
224 elif [ -d (loop)/boot/grub ]; then
225 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
228 distro_specify_initrd_file
230 vt_linux_initrd_count vtcount
231 if [ $vtcount -eq 0 ]; then
232 distro_specify_initrd_file_phase2
234 if [ "$vt_efi_dir" = "NO" ]; then
235 if [ -f (loop)/efi.img ]; then
236 vt_add_replace_file 0 "initrd"
243 if [ -d (loop)/loader/entries ]; then
244 vt_linux_get_main_initrd_index vtindex
246 if [ -d (loop)/arch ]; then
247 if [ -f (loop)/arch/boot/x86_64/archiso.img ]; then
248 vt_add_replace_file $vtindex "EFI\\archiso\\archiso.img"
249 elif [ -f (loop)/boot/initramfs_x86_64.img ]; then
250 vt_add_replace_file $vtindex "boot\\initramfs_x86_64.img"
252 elif [ -f (loop)/EFI/BOOT/initrd.gz ]; then
253 vt_add_replace_file $vtindex "EFI\\BOOT\\initrd.gz"
255 elif [ -e (loop)/syslinux/alt0/full.cz ]; then
256 vt_add_replace_file 0 "EFI\\BOOT\\full.cz"
257 set FirstTryBootFile='@EFI@BOOT@grubx64.efi'
262 vt_linux_chain_data ${1}${chosen_path}
264 if [ -n "$vtoy_chain_mem_addr" ]; then
265 terminal_output console
266 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}
269 echo "chain empty failed"
275 function uefi_iso_menu_func {
277 if [ -d (loop)/ ]; then
281 if [ -n "$vtisouefi" ]; then
282 set LoadIsoEfiDriver=on
284 elif vt_check_mode 2; then
285 set LoadIsoEfiDriver=on
287 unset LoadIsoEfiDriver
290 vt_chosen_img_path chosen_path
291 vt_select_auto_install ${chosen_path}
292 vt_select_persistence ${chosen_path}
294 if vt_is_udf ${1}${chosen_path}; then
295 set ventoy_fs_probe=udf
297 set ventoy_fs_probe=iso9660
298 vt_iso9660_nojoliet 0
301 loopback loop ${1}${chosen_path}
304 if [ -d (loop)/EFI ]; then
306 elif [ -d (loop)/efi ]; then
312 if [ -n "$vtcompat" ]; then
313 set ventoy_compatible=YES
315 elif vt_check_mode 1; then
316 set ventoy_compatible=YES
318 vt_check_compatible (loop)
321 vt_img_sector ${1}${chosen_path}
323 if [ "$vtoy_os" = "Windows" ]; then
324 vt_check_compatible_pe (loop)
325 uefi_windows_menu_func $1 ${chosen_path}
327 uefi_linux_menu_func $1 ${chosen_path}
330 terminal_output gfxterm
333 function uefi_iso_memdisk {
334 vt_chosen_img_path chosen_path
336 echo 'Loading ISO file to memory ...'
337 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
339 terminal_output console
340 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}
343 terminal_output gfxterm
347 function legacy_windows_menu_func {
350 if [ "$ventoy_compatible" = "NO" ]; then
352 if [ "$ventoy_fs_probe" = "iso9660" ]; then
354 vt_iso9660_nojoliet 1
358 for file in "boot/bcd" "/efi/microsoft/boot/bcd" "SSTR/BCD"; do
359 vt_windows_collect_wim_patch bcd (loop)/$file
362 distro_specify_wim_patch
364 vt_windows_count_wim_patch vt_wim_cnt
365 if [ $vt_wim_cnt -eq 0 ]; then
366 distro_specify_wim_patch_phase2
373 vt_windows_chain_data ${1}${chosen_path}
376 if [ -n "$vtoy_chain_mem_addr" ]; then
377 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
380 echo "chain empty failed"
385 function legacy_linux_menu_func {
386 if [ "$ventoy_compatible" = "NO" ]; then
388 if [ "$ventoy_fs_probe" = "udf" ]; then
390 set ventoy_fs_probe=iso9660
394 vt_load_cpio $vtoy_path/ventoy.cpio $2 $1
396 vt_linux_clear_initrd
398 if [ -d (loop)/pmagic ]; then
399 vt_linux_specify_initrd_file /pmagic/initrd.img
401 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
402 if [ -d (loop)/$dir ]; then
403 vt_linux_parse_initrd_isolinux (loop)/$dir/
408 # special process for special distros
410 if [ -d (loop)/arch/boot/syslinux ]; then
411 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
412 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
415 elif [ -d (loop)/manjaro ]; then
416 if [ -e (loop)/boot/grub/kernels.cfg ]; then
417 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
419 elif [ -e (loop)/boot/grub/grub.cfg ]; then
420 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
423 distro_specify_initrd_file
425 vt_linux_initrd_count vtcount
426 if [ $vtcount -eq 0 ]; then
427 distro_specify_initrd_file_phase2
433 vt_linux_chain_data ${1}${chosen_path}
436 if [ -n "$vtoy_chain_mem_addr" ]; then
437 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
440 echo "chain empty failed"
445 function legacy_iso_menu_func {
447 if [ -d (loop)/ ]; then
451 vt_chosen_img_path chosen_path
452 vt_select_auto_install ${chosen_path}
453 vt_select_persistence ${chosen_path}
455 if vt_is_udf ${1}${chosen_path}; then
456 set ventoy_fs_probe=udf
458 set ventoy_fs_probe=iso9660
459 vt_iso9660_nojoliet 0
462 loopback loop ${1}${chosen_path}
465 if [ -n "$vtcompat" ]; then
466 set ventoy_compatible=YES
468 elif vt_check_mode 1; then
469 set ventoy_compatible=YES
471 vt_check_compatible (loop)
474 vt_img_sector ${1}${chosen_path}
476 if [ "$vtoy_os" = "Windows" ]; then
477 vt_check_compatible_pe (loop)
478 legacy_windows_menu_func $1 ${chosen_path}
480 legacy_linux_menu_func $1 ${chosen_path}
484 function legacy_iso_memdisk {
485 vt_chosen_img_path chosen_path
487 linux16 $vtoy_path/memdisk iso raw
488 echo "Loading ISO file to memory ..."
489 initrd16 ${1}${chosen_path}
493 function iso_common_menuentry {
494 if [ "$grub_platform" = "pc" ]; then
495 if vt_check_mode 0; then
496 legacy_iso_memdisk $iso_path
498 legacy_iso_menu_func $iso_path
501 if vt_check_mode 0; then
502 uefi_iso_memdisk $iso_path
504 uefi_iso_menu_func $iso_path
509 function common_unsupport_menuentry {
510 echo -e "\n The name of the iso file could NOT contain space or non-ascii characters. \n"
511 echo -e " 文件名中不能有中文或空格 \n"
512 echo -e "\n Will return to main menu after 10 seconds ...\n"
516 function iso_unsupport_menuentry {
517 common_unsupport_menuentry
520 function wim_common_menuentry {
521 vt_chosen_img_path chosen_path
522 vt_wim_chain_data ${iso_path}${chosen_path}
526 if [ -n "$vtoy_chain_mem_addr" ]; then
527 if [ "$grub_platform" = "pc" ]; then
528 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
530 terminal_output console
531 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
532 terminal_output gfxterm
536 echo "chain empty failed"
541 function wim_unsupport_menuentry {
542 common_unsupport_menuentry
545 function efi_common_menuentry {
546 vt_chosen_img_path chosen_path
548 terminal_output console
549 chainloader ${iso_path}${chosen_path}
551 terminal_output gfxterm
554 function efi_unsupport_menuentry {
555 common_unsupport_menuentry
558 #############################################################
559 #############################################################
560 #############################################################
561 ####### Main Process ###########
562 #############################################################
563 #############################################################
564 #############################################################
566 set VENTOY_VERSION="1.0.14"
568 # Default menu display mode, you can change it as you want.
571 set VTOY_DEFAULT_MENU_MODE=0
573 set VTOY_MEM_DISK_STR="[Memdisk]"
574 set VTOY_ISO_RAW_STR="Compatible Mode"
575 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
577 set VTOY_F2_CMD="ventoy_power"
578 set VTOY_F4_CMD="ventoy_localboot"
579 set VTOY_F5_CMD="ventoy_diagnosis"
581 if [ "$grub_platform" = "pc" ]; then
582 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION BIOS www.ventoy.net"
584 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION UEFI www.ventoy.net"
587 vt_device $root vtoy_dev
589 if [ "$vtoy_dev" = "tftp" ]; then
590 set vtoy_path=($root)
591 for vtid in 0 1 2 3; do
592 if [ -d (hd$vtid,2)/ventoy ]; then
593 set iso_path=(hd$vtid,1)
594 set vtoy_efi_part=(hd$vtid,2)
600 if [ -f $iso_path/ventoy/ventoy.json ]; then
601 set vt_plugin_path=$iso_path
603 set vt_plugin_path=$prefix
604 vt_load_plugin $vt_plugin_path
607 if [ "$prefix" = "(ventoydisk)/grub" ]; then
608 set vtoy_path=(ventoydisk)/ventoy
610 set vtoy_path=($root)/ventoy
613 set iso_path=($vtoy_dev,1)
614 set vtoy_efi_part=($vtoy_dev,2)
616 set vt_plugin_path=$iso_path
621 if [ -f $iso_path/ventoy/ventoy.json ]; then
622 vt_load_plugin $iso_path
625 if [ -n "$VTOY_MENU_TIMEOUT" ]; then
626 set timeout=$VTOY_MENU_TIMEOUT
631 if [ -f $iso_path/ventoy/ventoy_wimboot.img ]; then
632 vt_load_wimboot $iso_path/ventoy/ventoy_wimboot.img
633 elif [ -f $vtoy_efi_part/ventoy/ventoy_wimboot.img ]; then
634 vt_load_wimboot $vtoy_efi_part/ventoy/ventoy_wimboot.img
638 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
639 set VTOY_F3_CMD="vt_dynamic_menu 1 1"
640 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:TreeView F4:Localboot F5:Debug"
642 set VTOY_F3_CMD="vt_dynamic_menu 1 0"
643 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:ListView F4:Localboot F5:Debug"
647 if [ -n "$vtoy_gfxmode" ]; then
648 set gfxmode=$vtoy_gfxmode
650 set gfxmode=1920x1080,1366x768,1024x768
653 if [ -n "$vtoy_theme" ]; then
654 set theme=$vtoy_theme
656 set theme=$prefix/themes/ventoy/theme.txt
659 if [ "$vtoy_display_mode" = "CLI" ]; then
660 terminal_output console
662 terminal_output gfxterm
665 #colect all image files (iso files)
666 set ventoy_img_count=0
667 vt_list_img $iso_path ventoy_img_count
670 if [ $ventoy_img_count -gt 0 ]; then
671 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
677 if [ -n "$VTOY_NO_ISO_TIP" ]; then
678 NO_ISO_MENU="No ISO files found, $VTOY_NO_ISO_TIP"
679 elif [ -n "$VTOY_DEFAULT_SEARCH_ROOT" ]; then
680 NO_ISO_MENU="No ISO files found, please check VTOY_DEFAULT_SEARCH_ROOT"
682 NO_ISO_MENU="No ISO files found"
684 menuentry "$NO_ISO_MENU (Press enter to reboot ...)" {
685 echo -e "\n Rebooting ... "