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 ......"
33 function ventoy_cli_console {
34 if [ "$grub_platform" = "pc" ]; then
35 terminal_output vga_text
37 if [ "$vtoy_display_mode" != "CLI" ]; then
38 terminal_output console
43 function ventoy_gui_console {
44 if [ "$grub_platform" = "pc" ]; then
45 if [ "$vtoy_display_mode" = "CLI" ]; then
46 terminal_output console
48 terminal_output gfxterm
51 if [ "$vtoy_display_mode" != "CLI" ]; then
52 terminal_output gfxterm
57 function ventoy_power {
58 configfile $prefix/power.cfg
61 function ventoy_diagnosis {
62 configfile $prefix/debug.cfg
65 function ventoy_localboot {
66 configfile $prefix/localboot.cfg
69 function get_os_type {
72 for file in "efi/microsoft/boot/bcd" "sources/boot.wim" "boot/bcd" "bootmgr.efi" "boot/etfsboot.com" ; do
73 if vt_file_exist_nocase (loop)/$file; then
79 if [ -n "${vtdebug_flag}" ]; then
84 function vt_check_compatible_pe {
85 #Check for PE without external tools
86 if [ -f $1/HBCD_PE.ini ]; then
87 set ventoy_compatible=YES
91 function locate_initrd {
92 vt_linux_locate_initrd
94 if [ -n "${vtdebug_flag}" ]; then
100 function locate_wim {
101 vt_windows_locate_wim_patch (loop)
103 if [ -n "${vtdebug_flag}" ]; then
104 echo '###############################################'
106 echo '###############################################'
111 function distro_specify_wim_patch {
112 if [ -d (loop)/h3pe ]; then
113 vt_windows_collect_wim_patch wim /BOOT/H3_10PE.WIM
114 vt_windows_collect_wim_patch wim /BOOT/H3_7PE.WIM
115 vt_windows_collect_wim_patch wim /BOOT/H3_8PE.WIM
116 vt_windows_collect_wim_patch wim /BOOT/H3_81PE.WIM
120 function distro_specify_wim_patch_phase2 {
121 if [ -f (loop)/boot/boot.wim ]; then
122 vt_windows_collect_wim_patch wim /boot/boot.wim
127 function distro_specify_initrd_file {
128 if [ -e (loop)/boot/all.rdz ]; then
129 vt_linux_specify_initrd_file /boot/all.rdz
130 elif [ -e (loop)/boot/xen.gz ]; then
131 if [ -e (loop)/install.img ]; then
132 vt_linux_specify_initrd_file /install.img
134 elif [ -d (loop)/casper ]; then
135 if [ -e (loop)/casper/initrd ]; then
136 vt_linux_specify_initrd_file /casper/initrd
138 if [ -e (loop)/casper/initrd-oem ]; then
139 vt_linux_specify_initrd_file /casper/initrd-oem
141 elif [ -e (loop)/boot/grub/initrd.xz ]; then
142 vt_linux_specify_initrd_file /boot/grub/initrd.xz
143 elif [ -e (loop)/initrd.gz ]; then
144 vt_linux_specify_initrd_file /initrd.gz
145 elif [ -e (loop)/slax/boot/initrfs.img ]; then
146 vt_linux_specify_initrd_file /slax/boot/initrfs.img
147 elif [ -e (loop)/pmagic/initrd.img ]; then
148 vt_linux_specify_initrd_file /pmagic/initrd.img
149 elif [ -e (loop)/boot/initrd.xz ]; then
150 vt_linux_specify_initrd_file /boot/initrd.xz
151 elif [ -f (loop)/boot/initrd ]; then
152 vt_linux_specify_initrd_file /boot/initrd
153 elif [ -f (loop)/boot/x86_64/loader/initrd ]; then
154 vt_linux_specify_initrd_file /boot/x86_64/loader/initrd
155 elif [ -f (loop)/boot/initramfs-x86_64.img ]; then
156 vt_linux_specify_initrd_file /boot/initramfs-x86_64.img
163 function distro_specify_initrd_file_phase2 {
164 if [ -f (loop)/boot/initrd.img ]; then
165 vt_linux_specify_initrd_file /boot/initrd.img
166 elif [ -f (loop)/Setup/initrd.gz ]; then
167 vt_linux_specify_initrd_file /Setup/initrd.gz
168 elif [ -f (loop)/isolinux/initramfs ]; then
169 vt_linux_specify_initrd_file /isolinux/initramfs
170 elif [ -f (loop)/boot/iniramfs.igz ]; then
171 vt_linux_specify_initrd_file /boot/iniramfs.igz
172 elif [ -f (loop)/initrd-x86_64 ]; then
173 vt_linux_specify_initrd_file /initrd-x86_64
174 elif [ -f (loop)/live/initrd.img ]; then
175 vt_linux_specify_initrd_file /live/initrd.img
176 elif [ -f (loop)/initrd.img ]; then
177 vt_linux_specify_initrd_file /initrd.img
178 elif [ -f (loop)/sysresccd/boot/x86_64/sysresccd.img ]; then
179 vt_linux_specify_initrd_file /sysresccd/boot/x86_64/sysresccd.img
184 function uefi_windows_menu_func {
187 if [ "$ventoy_compatible" = "NO" ]; then
189 if [ "$ventoy_fs_probe" = "iso9660" ]; then
191 vt_iso9660_nojoliet 1
195 for file in "efi/microsoft/boot/bcd"; do
196 vt_windows_collect_wim_patch bcd (loop)/$file
199 vt_windows_count_wim_patch vt_wim_cnt
200 if [ $vt_wim_cnt -eq 0 ]; then
201 distro_specify_wim_patch_phase2
208 vt_windows_chain_data ${1}${chosen_path}
211 if [ -n "$vtoy_chain_mem_addr" ]; then
213 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
216 echo "chain empty failed"
221 function uefi_linux_menu_func {
222 if [ "$ventoy_compatible" = "NO" ]; then
224 if [ "$ventoy_fs_probe" = "udf" ]; then
226 set ventoy_fs_probe=iso9660
230 vt_load_cpio ${vtoy_path}/ventoy.cpio $2 $1
232 vt_linux_clear_initrd
234 if [ -d (loop)/pmagic ]; then
235 vt_linux_specify_initrd_file /pmagic/initrd.img
237 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
238 if [ -e (loop)/$file ]; then
239 vt_linux_parse_initrd_grub file (loop)/$file
244 # special process for special distros
245 if [ -d (loop)/loader/entries ]; then
246 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
247 elif [ -d (loop)/boot/grub ]; then
248 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
251 distro_specify_initrd_file
253 vt_linux_initrd_count vtcount
254 if [ $vtcount -eq 0 ]; then
255 distro_specify_initrd_file_phase2
257 if [ "$vt_efi_dir" = "NO" ]; then
258 if [ -f (loop)/efi.img ]; then
259 vt_add_replace_file 0 "initrd"
266 if [ -d (loop)/loader/entries ]; then
267 vt_linux_get_main_initrd_index vtindex
269 if [ -d (loop)/arch ]; then
270 if [ -f (loop)/arch/boot/x86_64/archiso.img ]; then
271 vt_add_replace_file $vtindex "EFI\\archiso\\archiso.img"
272 elif [ -f (loop)/boot/initramfs_x86_64.img ]; then
273 vt_add_replace_file $vtindex "boot\\initramfs_x86_64.img"
275 elif [ -f (loop)/EFI/BOOT/initrd.gz ]; then
276 vt_add_replace_file $vtindex "EFI\\BOOT\\initrd.gz"
278 elif [ -e (loop)/syslinux/alt0/full.cz ]; then
279 vt_add_replace_file 0 "EFI\\BOOT\\full.cz"
280 set FirstTryBootFile='@EFI@BOOT@grubx64.efi'
285 vt_linux_chain_data ${1}${chosen_path}
287 if [ -n "$vtoy_chain_mem_addr" ]; then
289 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}
292 echo "chain empty failed"
298 function uefi_iso_menu_func {
300 if [ -d (loop)/ ]; then
304 if [ -n "$vtisouefi" ]; then
305 set LoadIsoEfiDriver=on
307 elif vt_check_mode 2; then
308 set LoadIsoEfiDriver=on
310 unset LoadIsoEfiDriver
313 vt_chosen_img_path chosen_path
314 vt_select_auto_install ${chosen_path}
315 vt_select_persistence ${chosen_path}
317 if vt_is_udf ${1}${chosen_path}; then
318 set ventoy_fs_probe=udf
320 set ventoy_fs_probe=iso9660
321 vt_iso9660_nojoliet 0
324 loopback loop ${1}${chosen_path}
327 if [ -d (loop)/EFI ]; then
329 elif [ -d (loop)/efi ]; then
335 if [ -n "$vtcompat" ]; then
336 set ventoy_compatible=YES
338 elif vt_check_mode 1; then
339 set ventoy_compatible=YES
341 vt_check_compatible (loop)
344 vt_img_sector ${1}${chosen_path}
346 if [ "$vtoy_os" = "Windows" ]; then
347 vt_check_compatible_pe (loop)
348 uefi_windows_menu_func $1 ${chosen_path}
350 uefi_linux_menu_func $1 ${chosen_path}
356 function uefi_iso_memdisk {
357 vt_chosen_img_path chosen_path
359 echo 'Loading ISO file to memory ...'
360 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
363 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}
370 function legacy_windows_menu_func {
373 if [ "$ventoy_compatible" = "NO" ]; then
375 if [ "$ventoy_fs_probe" = "iso9660" ]; then
377 vt_iso9660_nojoliet 1
381 for file in "boot/bcd" "/efi/microsoft/boot/bcd" "SSTR/BCD"; do
382 vt_windows_collect_wim_patch bcd (loop)/$file
385 distro_specify_wim_patch
387 vt_windows_count_wim_patch vt_wim_cnt
388 if [ $vt_wim_cnt -eq 0 ]; then
389 distro_specify_wim_patch_phase2
396 vt_windows_chain_data ${1}${chosen_path}
399 if [ -n "$vtoy_chain_mem_addr" ]; then
400 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
403 echo "chain empty failed"
408 function legacy_linux_menu_func {
409 if [ "$ventoy_compatible" = "NO" ]; then
411 if [ "$ventoy_fs_probe" = "udf" ]; then
413 set ventoy_fs_probe=iso9660
417 vt_load_cpio $vtoy_path/ventoy.cpio $2 $1
419 vt_linux_clear_initrd
421 if [ -d (loop)/pmagic ]; then
422 vt_linux_specify_initrd_file /pmagic/initrd.img
424 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
425 if [ -d (loop)/$dir ]; then
426 vt_linux_parse_initrd_isolinux (loop)/$dir/
431 # special process for special distros
433 if [ -d (loop)/arch/boot/syslinux ]; then
434 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
435 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
438 elif [ -d (loop)/manjaro ]; then
439 if [ -e (loop)/boot/grub/kernels.cfg ]; then
440 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
442 elif [ -e (loop)/boot/grub/grub.cfg ]; then
443 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
446 distro_specify_initrd_file
448 vt_linux_initrd_count vtcount
449 if [ $vtcount -eq 0 ]; then
450 distro_specify_initrd_file_phase2
456 vt_linux_chain_data ${1}${chosen_path}
459 if [ -n "$vtoy_chain_mem_addr" ]; then
460 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
463 echo "chain empty failed"
468 function legacy_iso_menu_func {
470 if [ -d (loop)/ ]; then
474 vt_chosen_img_path chosen_path
475 vt_select_auto_install ${chosen_path}
476 vt_select_persistence ${chosen_path}
478 if vt_is_udf ${1}${chosen_path}; then
479 set ventoy_fs_probe=udf
481 set ventoy_fs_probe=iso9660
482 vt_iso9660_nojoliet 0
485 loopback loop ${1}${chosen_path}
488 if [ -n "$vtcompat" ]; then
489 set ventoy_compatible=YES
491 elif vt_check_mode 1; then
492 set ventoy_compatible=YES
494 vt_check_compatible (loop)
497 vt_img_sector ${1}${chosen_path}
499 if [ "$vtoy_os" = "Windows" ]; then
500 vt_check_compatible_pe (loop)
501 legacy_windows_menu_func $1 ${chosen_path}
503 legacy_linux_menu_func $1 ${chosen_path}
507 function legacy_iso_memdisk {
508 vt_chosen_img_path chosen_path
510 linux16 $vtoy_path/memdisk iso raw
511 echo "Loading ISO file to memory ..."
512 initrd16 ${1}${chosen_path}
516 function iso_common_menuentry {
517 if [ "$grub_platform" = "pc" ]; then
518 if vt_check_mode 0; then
519 legacy_iso_memdisk $iso_path
521 legacy_iso_menu_func $iso_path
524 if vt_check_mode 0; then
525 uefi_iso_memdisk $iso_path
527 uefi_iso_menu_func $iso_path
532 function common_unsupport_menuentry {
533 echo -e "\n The name of the iso file could NOT contain space or non-ascii characters. \n"
534 echo -e " 文件名中不能有中文或空格 \n"
535 echo -e "\n Will return to main menu after 10 seconds ...\n"
539 function iso_unsupport_menuentry {
540 common_unsupport_menuentry
543 function wim_common_menuentry {
544 vt_chosen_img_path chosen_path
545 vt_wim_chain_data ${iso_path}${chosen_path}
549 if [ -n "$vtoy_chain_mem_addr" ]; then
550 if [ "$grub_platform" = "pc" ]; then
551 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
554 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
559 echo "chain empty failed"
564 function wim_unsupport_menuentry {
565 common_unsupport_menuentry
568 function efi_common_menuentry {
569 vt_chosen_img_path chosen_path
572 chainloader ${iso_path}${chosen_path}
577 function efi_unsupport_menuentry {
578 common_unsupport_menuentry
581 #############################################################
582 #############################################################
583 #############################################################
584 ####### Main Process ###########
585 #############################################################
586 #############################################################
587 #############################################################
589 set VENTOY_VERSION="1.0.14"
591 # Default menu display mode, you can change it as you want.
594 set VTOY_DEFAULT_MENU_MODE=0
596 set VTOY_MEM_DISK_STR="[Memdisk]"
597 set VTOY_ISO_RAW_STR="Compatible Mode"
598 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
600 set VTOY_F2_CMD="ventoy_power"
601 set VTOY_F4_CMD="ventoy_localboot"
602 set VTOY_F5_CMD="ventoy_diagnosis"
604 if [ "$grub_platform" = "pc" ]; then
605 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION BIOS www.ventoy.net"
607 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION UEFI www.ventoy.net"
610 vt_device $root vtoy_dev
612 if [ "$vtoy_dev" = "tftp" ]; then
613 set vtoy_path=($root)
614 for vtid in 0 1 2 3; do
615 if [ -d (hd$vtid,2)/ventoy ]; then
616 set iso_path=(hd$vtid,1)
617 set vtoy_efi_part=(hd$vtid,2)
623 if [ -f $iso_path/ventoy/ventoy.json ]; then
624 set vt_plugin_path=$iso_path
626 set vt_plugin_path=$prefix
627 vt_load_plugin $vt_plugin_path
630 if [ "$prefix" = "(ventoydisk)/grub" ]; then
631 set vtoy_path=(ventoydisk)/ventoy
633 set vtoy_path=($root)/ventoy
636 set iso_path=($vtoy_dev,1)
637 set vtoy_efi_part=($vtoy_dev,2)
639 set vt_plugin_path=$iso_path
644 if [ -f $iso_path/ventoy/ventoy.json ]; then
645 vt_load_plugin $iso_path
648 if [ -n "$VTOY_MENU_TIMEOUT" ]; then
649 set timeout=$VTOY_MENU_TIMEOUT
654 if [ -f $iso_path/ventoy/ventoy_wimboot.img ]; then
655 vt_load_wimboot $iso_path/ventoy/ventoy_wimboot.img
656 elif [ -f $vtoy_efi_part/ventoy/ventoy_wimboot.img ]; then
657 vt_load_wimboot $vtoy_efi_part/ventoy/ventoy_wimboot.img
661 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
662 set VTOY_F3_CMD="vt_dynamic_menu 1 1"
663 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:TreeView F4:Localboot F5:Debug"
665 set VTOY_F3_CMD="vt_dynamic_menu 1 0"
666 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:ListView F4:Localboot F5:Debug"
670 if [ -n "$vtoy_gfxmode" ]; then
671 set gfxmode=$vtoy_gfxmode
673 set gfxmode=1920x1080,1366x768,1024x768
676 if [ "$vtoy_display_mode" = "CLI" ]; then
677 terminal_output console
679 if [ -n "$vtoy_theme" ]; then
680 set theme=$vtoy_theme
682 set theme=$prefix/themes/ventoy/theme.txt
684 terminal_output gfxterm
687 #colect all image files (iso files)
688 set ventoy_img_count=0
689 vt_list_img $iso_path ventoy_img_count
692 if [ $ventoy_img_count -gt 0 ]; then
693 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
699 if [ -n "$VTOY_NO_ISO_TIP" ]; then
700 NO_ISO_MENU="No ISO files found, $VTOY_NO_ISO_TIP"
701 elif [ -n "$VTOY_DEFAULT_SEARCH_ROOT" ]; then
702 NO_ISO_MENU="No ISO files found, please check VTOY_DEFAULT_SEARCH_ROOT"
704 NO_ISO_MENU="No ISO files found"
706 menuentry "$NO_ISO_MENU (Press enter to reboot ...)" {
707 echo -e "\n Rebooting ... "