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
36 terminal_output console
38 if [ "$vtoy_display_mode" != "CLI" ]; then
39 terminal_output console
44 function ventoy_gui_console {
45 if [ "$grub_platform" = "pc" ]; then
46 if [ "$vtoy_display_mode" = "CLI" ]; then
47 terminal_output console
49 terminal_output gfxterm
52 if [ "$vtoy_display_mode" != "CLI" ]; then
53 terminal_output gfxterm
58 function ventoy_power {
59 configfile $prefix/power.cfg
62 function ventoy_diagnosis {
63 configfile $prefix/debug.cfg
66 function ventoy_localboot {
67 configfile $prefix/localboot.cfg
70 function get_os_type {
73 for file in "efi/microsoft/boot/bcd" "sources/boot.wim" "boot/bcd" "bootmgr.efi" "boot/etfsboot.com" ; do
74 if vt_file_exist_nocase (loop)/$file; then
80 if [ -n "${vtdebug_flag}" ]; then
85 function vt_check_compatible_pe {
86 #Check for PE without external tools
87 if [ -f $1/HBCD_PE.ini ]; then
88 set ventoy_compatible=YES
92 function locate_initrd {
93 vt_linux_locate_initrd
95 if [ -n "${vtdebug_flag}" ]; then
101 function locate_wim {
102 vt_windows_locate_wim_patch (loop)
104 if [ -n "${vtdebug_flag}" ]; then
105 echo '###############################################'
107 echo '###############################################'
112 function distro_specify_wim_patch {
113 if [ -d (loop)/h3pe ]; then
114 vt_windows_collect_wim_patch wim /BOOT/H3_10PE.WIM
115 vt_windows_collect_wim_patch wim /BOOT/H3_7PE.WIM
116 vt_windows_collect_wim_patch wim /BOOT/H3_8PE.WIM
117 vt_windows_collect_wim_patch wim /BOOT/H3_81PE.WIM
121 function distro_specify_wim_patch_phase2 {
122 if [ -f (loop)/boot/boot.wim ]; then
123 vt_windows_collect_wim_patch wim /boot/boot.wim
128 function distro_specify_initrd_file {
129 if [ -e (loop)/boot/all.rdz ]; then
130 vt_linux_specify_initrd_file /boot/all.rdz
131 elif [ -e (loop)/boot/xen.gz ]; then
132 if [ -e (loop)/install.img ]; then
133 vt_linux_specify_initrd_file /install.img
135 elif [ -d (loop)/casper ]; then
136 if [ -e (loop)/casper/initrd ]; then
137 vt_linux_specify_initrd_file /casper/initrd
139 if [ -e (loop)/casper/initrd-oem ]; then
140 vt_linux_specify_initrd_file /casper/initrd-oem
142 elif [ -e (loop)/boot/grub/initrd.xz ]; then
143 vt_linux_specify_initrd_file /boot/grub/initrd.xz
144 elif [ -e (loop)/initrd.gz ]; then
145 vt_linux_specify_initrd_file /initrd.gz
146 elif [ -e (loop)/slax/boot/initrfs.img ]; then
147 vt_linux_specify_initrd_file /slax/boot/initrfs.img
148 elif [ -e (loop)/pmagic/initrd.img ]; then
149 vt_linux_specify_initrd_file /pmagic/initrd.img
150 elif [ -e (loop)/boot/initrd.xz ]; then
151 vt_linux_specify_initrd_file /boot/initrd.xz
152 elif [ -f (loop)/boot/initrd ]; then
153 vt_linux_specify_initrd_file /boot/initrd
154 elif [ -f (loop)/boot/x86_64/loader/initrd ]; then
155 vt_linux_specify_initrd_file /boot/x86_64/loader/initrd
156 elif [ -f (loop)/boot/initramfs-x86_64.img ]; then
157 vt_linux_specify_initrd_file /boot/initramfs-x86_64.img
164 function distro_specify_initrd_file_phase2 {
165 if [ -f (loop)/boot/initrd.img ]; then
166 vt_linux_specify_initrd_file /boot/initrd.img
167 elif [ -f (loop)/Setup/initrd.gz ]; then
168 vt_linux_specify_initrd_file /Setup/initrd.gz
169 elif [ -f (loop)/isolinux/initramfs ]; then
170 vt_linux_specify_initrd_file /isolinux/initramfs
171 elif [ -f (loop)/boot/iniramfs.igz ]; then
172 vt_linux_specify_initrd_file /boot/iniramfs.igz
173 elif [ -f (loop)/initrd-x86_64 ]; then
174 vt_linux_specify_initrd_file /initrd-x86_64
175 elif [ -f (loop)/live/initrd.img ]; then
176 vt_linux_specify_initrd_file /live/initrd.img
177 elif [ -f (loop)/initrd.img ]; then
178 vt_linux_specify_initrd_file /initrd.img
179 elif [ -f (loop)/sysresccd/boot/x86_64/sysresccd.img ]; then
180 vt_linux_specify_initrd_file /sysresccd/boot/x86_64/sysresccd.img
181 elif [ -f (loop)/CDlinux/initrd ]; then
182 vt_linux_specify_initrd_file /CDlinux/initrd
183 elif [ -f (loop)/parabola/boot/x86_64/parabolaiso.img ]; then
184 vt_linux_specify_initrd_file /parabola/boot/x86_64/parabolaiso.img
185 if [ -f (loop)/parabola/boot/i686/parabolaiso.img ]; then
186 vt_linux_specify_initrd_file /parabola/boot/i686/parabolaiso.img
188 elif [ -f (loop)/EFI/BOOT/initrd.img ]; then
190 vt_linux_specify_initrd_file /EFI/BOOT/initrd.img
191 if [ "$grub_platform" != "pc" ]; then
192 vt_add_replace_file 0 "initrd.img"
194 elif [ -f (loop)/initrd ]; then
195 vt_linux_specify_initrd_file /initrd
196 elif [ -f (loop)/live/initrd1 ]; then
197 vt_linux_specify_initrd_file /live/initrd1
202 function uefi_windows_menu_func {
205 if [ "$ventoy_compatible" = "NO" ]; then
207 if [ "$ventoy_fs_probe" = "iso9660" ]; then
209 vt_iso9660_nojoliet 1
213 for file in "efi/microsoft/boot/bcd"; do
214 vt_windows_collect_wim_patch bcd (loop)/$file
217 vt_windows_count_wim_patch vt_wim_cnt
218 if [ $vt_wim_cnt -eq 0 ]; then
219 distro_specify_wim_patch_phase2
226 vt_windows_chain_data ${1}${chosen_path}
229 if [ -n "$vtoy_chain_mem_addr" ]; then
231 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
234 echo "chain empty failed"
239 function uefi_linux_menu_func {
240 if [ "$ventoy_compatible" = "NO" ]; then
242 if [ "$ventoy_fs_probe" = "udf" ]; then
244 set ventoy_fs_probe=iso9660
248 vt_load_cpio ${vtoy_path}/ventoy.cpio $2 $1
250 vt_linux_clear_initrd
252 if [ -d (loop)/pmagic ]; then
253 vt_linux_specify_initrd_file /pmagic/initrd.img
255 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
256 if [ -e (loop)/$file ]; then
257 vt_linux_parse_initrd_grub file (loop)/$file
262 # special process for special distros
263 if [ -d (loop)/loader/entries ]; then
264 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
265 elif [ -d (loop)/boot/grub ]; then
266 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
269 distro_specify_initrd_file
271 vt_linux_initrd_count vtcount
272 if [ $vtcount -eq 0 ]; then
273 distro_specify_initrd_file_phase2
275 if [ "$vt_efi_dir" = "NO" ]; then
276 if [ -f (loop)/efi.img ]; then
277 vt_add_replace_file 0 "initrd"
284 if [ -d (loop)/loader/entries ]; then
285 vt_linux_get_main_initrd_index vtindex
287 if [ -d (loop)/arch ]; then
288 if [ -f (loop)/arch/boot/x86_64/archiso.img ]; then
289 vt_add_replace_file $vtindex "EFI\\archiso\\archiso.img"
290 elif [ -f (loop)/boot/initramfs_x86_64.img ]; then
291 vt_add_replace_file $vtindex "boot\\initramfs_x86_64.img"
293 elif [ -f (loop)/EFI/BOOT/initrd.gz ]; then
294 vt_add_replace_file $vtindex "EFI\\BOOT\\initrd.gz"
296 elif [ -d (loop)/EFI/boot/entries ]; then
297 if [ -f (loop)/parabola/boot/x86_64/parabolaiso.img ]; then
298 vt_add_replace_file 0 "EFI\\parabolaiso\\parabolaiso.img"
300 elif [ -e (loop)/syslinux/alt0/full.cz ]; then
301 vt_add_replace_file 0 "EFI\\BOOT\\full.cz"
302 set FirstTryBootFile='@EFI@BOOT@grubx64.efi'
307 vt_linux_chain_data ${1}${chosen_path}
309 if [ -n "$vtoy_chain_mem_addr" ]; then
311 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}
314 echo "chain empty failed"
320 function uefi_iso_menu_func {
322 if [ -d (loop)/ ]; then
326 if [ -n "$vtisouefi" ]; then
327 set LoadIsoEfiDriver=on
329 elif vt_check_mode 2; then
330 set LoadIsoEfiDriver=on
332 unset LoadIsoEfiDriver
335 vt_chosen_img_path chosen_path
336 vt_select_auto_install ${chosen_path}
337 vt_select_persistence ${chosen_path}
339 if vt_is_udf ${1}${chosen_path}; then
340 set ventoy_fs_probe=udf
342 set ventoy_fs_probe=iso9660
343 vt_iso9660_nojoliet 0
346 loopback loop ${1}${chosen_path}
349 if [ -d (loop)/EFI ]; then
351 elif [ -d (loop)/efi ]; then
357 if [ -n "$vtcompat" ]; then
358 set ventoy_compatible=YES
360 elif vt_check_mode 1; then
361 set ventoy_compatible=YES
363 vt_check_compatible (loop)
366 vt_img_sector ${1}${chosen_path}
368 if [ "$vtoy_os" = "Windows" ]; then
369 vt_check_compatible_pe (loop)
370 uefi_windows_menu_func $1 ${chosen_path}
372 uefi_linux_menu_func $1 ${chosen_path}
378 function uefi_iso_memdisk {
379 vt_chosen_img_path chosen_path
381 echo 'Loading ISO file to memory ...'
382 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
385 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}
392 function legacy_windows_menu_func {
395 if [ "$ventoy_compatible" = "NO" ]; then
397 if [ "$ventoy_fs_probe" = "iso9660" ]; then
399 vt_iso9660_nojoliet 1
403 for file in "boot/bcd" "/efi/microsoft/boot/bcd" "SSTR/BCD"; do
404 vt_windows_collect_wim_patch bcd (loop)/$file
407 distro_specify_wim_patch
409 vt_windows_count_wim_patch vt_wim_cnt
410 if [ $vt_wim_cnt -eq 0 ]; then
411 distro_specify_wim_patch_phase2
418 vt_windows_chain_data ${1}${chosen_path}
421 if [ -n "$vtoy_chain_mem_addr" ]; then
422 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
425 echo "chain empty failed"
430 function legacy_linux_menu_func {
431 if [ "$ventoy_compatible" = "NO" ]; then
433 if [ "$ventoy_fs_probe" = "udf" ]; then
435 set ventoy_fs_probe=iso9660
439 vt_load_cpio $vtoy_path/ventoy.cpio $2 $1
441 vt_linux_clear_initrd
443 if [ -d (loop)/pmagic ]; then
444 vt_linux_specify_initrd_file /pmagic/initrd.img
446 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
447 if [ -d (loop)/$dir ]; then
448 vt_linux_parse_initrd_isolinux (loop)/$dir/
453 # special process for special distros
455 if [ -d (loop)/arch/boot/syslinux ]; then
456 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
457 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
460 elif [ -d (loop)/manjaro ]; then
461 if [ -e (loop)/boot/grub/kernels.cfg ]; then
462 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
464 elif [ -e (loop)/boot/grub/grub.cfg ]; then
465 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
468 distro_specify_initrd_file
470 vt_linux_initrd_count vtcount
471 if [ $vtcount -eq 0 ]; then
472 distro_specify_initrd_file_phase2
478 vt_linux_chain_data ${1}${chosen_path}
481 if [ -n "$vtoy_chain_mem_addr" ]; then
482 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
485 echo "chain empty failed"
490 function legacy_iso_menu_func {
492 if [ -d (loop)/ ]; then
496 vt_chosen_img_path chosen_path
497 vt_select_auto_install ${chosen_path}
498 vt_select_persistence ${chosen_path}
500 if vt_is_udf ${1}${chosen_path}; then
501 set ventoy_fs_probe=udf
503 set ventoy_fs_probe=iso9660
504 vt_iso9660_nojoliet 0
507 loopback loop ${1}${chosen_path}
510 if [ -n "$vtcompat" ]; then
511 set ventoy_compatible=YES
513 elif vt_check_mode 1; then
514 set ventoy_compatible=YES
516 vt_check_compatible (loop)
519 vt_img_sector ${1}${chosen_path}
521 if [ "$vtoy_os" = "Windows" ]; then
522 vt_check_compatible_pe (loop)
523 legacy_windows_menu_func $1 ${chosen_path}
525 legacy_linux_menu_func $1 ${chosen_path}
529 function legacy_iso_memdisk {
530 vt_chosen_img_path chosen_path
532 linux16 $vtoy_path/memdisk iso raw
533 echo "Loading ISO file to memory ..."
534 initrd16 ${1}${chosen_path}
538 function iso_common_menuentry {
539 if [ "$grub_platform" = "pc" ]; then
540 if vt_check_mode 0; then
541 legacy_iso_memdisk $iso_path
543 legacy_iso_menu_func $iso_path
546 if vt_check_mode 0; then
547 uefi_iso_memdisk $iso_path
549 uefi_iso_menu_func $iso_path
554 function common_unsupport_menuentry {
555 echo -e "\n The name of the iso file could NOT contain space or non-ascii characters. \n"
556 echo -e " 文件名中不能有中文或空格 \n"
557 echo -e "\n Will return to main menu after 10 seconds ...\n"
561 function iso_unsupport_menuentry {
562 common_unsupport_menuentry
565 function wim_common_menuentry {
566 vt_chosen_img_path chosen_path
567 vt_wim_chain_data ${iso_path}${chosen_path}
571 if [ -n "$vtoy_chain_mem_addr" ]; then
572 if [ "$grub_platform" = "pc" ]; then
573 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
576 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
581 echo "chain empty failed"
586 function wim_unsupport_menuentry {
587 common_unsupport_menuentry
590 function efi_common_menuentry {
591 vt_chosen_img_path chosen_path
594 chainloader ${iso_path}${chosen_path}
599 function efi_unsupport_menuentry {
600 common_unsupport_menuentry
605 #############################################################
606 #############################################################
607 #############################################################
608 ####### Main Process ###########
609 #############################################################
610 #############################################################
611 #############################################################
613 set VENTOY_VERSION="1.0.15"
615 # Default menu display mode, you can change it as you want.
618 set VTOY_DEFAULT_MENU_MODE=0
620 set VTOY_MEM_DISK_STR="[Memdisk]"
621 set VTOY_ISO_RAW_STR="Compatible Mode"
622 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
624 set VTOY_F2_CMD="ventoy_power"
625 set VTOY_F4_CMD="ventoy_localboot"
626 set VTOY_F5_CMD="ventoy_diagnosis"
628 if [ "$grub_platform" = "pc" ]; then
629 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION BIOS www.ventoy.net"
631 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION UEFI www.ventoy.net"
634 vt_device $root vtoy_dev
636 if [ "$vtoy_dev" = "tftp" ]; then
637 set vtoy_path=($root)
638 for vtid in 0 1 2 3; do
639 if [ -d (hd$vtid,2)/ventoy ]; then
640 set iso_path=(hd$vtid,1)
641 set vtoy_efi_part=(hd$vtid,2)
647 if [ -f $iso_path/ventoy/ventoy.json ]; then
648 set vt_plugin_path=$iso_path
650 set vt_plugin_path=$prefix
651 vt_load_plugin $vt_plugin_path
654 if [ "$prefix" = "(ventoydisk)/grub" ]; then
655 set vtoy_path=(ventoydisk)/ventoy
657 set vtoy_path=($root)/ventoy
660 set iso_path=($vtoy_dev,1)
661 set vtoy_efi_part=($vtoy_dev,2)
663 set vt_plugin_path=$iso_path
668 if [ -f $iso_path/ventoy/ventoy.json ]; then
669 vt_load_plugin $iso_path
672 if [ -n "$VTOY_MENU_TIMEOUT" ]; then
673 set timeout=$VTOY_MENU_TIMEOUT
678 if [ -f $iso_path/ventoy/ventoy_wimboot.img ]; then
679 vt_load_wimboot $iso_path/ventoy/ventoy_wimboot.img
680 elif [ -f $vtoy_efi_part/ventoy/ventoy_wimboot.img ]; then
681 vt_load_wimboot $vtoy_efi_part/ventoy/ventoy_wimboot.img
685 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
686 set VTOY_F3_CMD="vt_dynamic_menu 1 1"
687 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:TreeView F4:Localboot F5:Debug"
689 set VTOY_F3_CMD="vt_dynamic_menu 1 0"
690 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:ListView F4:Localboot F5:Debug"
694 if [ -n "$vtoy_gfxmode" ]; then
695 set gfxmode=$vtoy_gfxmode
697 set gfxmode=1920x1080,1366x768,1024x768
700 if [ "$vtoy_display_mode" = "CLI" ]; then
701 terminal_output console
703 if [ -n "$vtoy_theme" ]; then
704 set theme=$vtoy_theme
706 set theme=$prefix/themes/ventoy/theme.txt
708 terminal_output gfxterm
711 #colect all image files (iso files)
712 set ventoy_img_count=0
713 vt_list_img $iso_path ventoy_img_count
716 if [ $ventoy_img_count -gt 0 ]; then
717 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
723 if [ -n "$VTOY_NO_ISO_TIP" ]; then
724 NO_ISO_MENU="No ISO files found, $VTOY_NO_ISO_TIP"
725 elif [ -n "$VTOY_DEFAULT_SEARCH_ROOT" ]; then
726 NO_ISO_MENU="No ISO files found, please check VTOY_DEFAULT_SEARCH_ROOT"
728 NO_ISO_MENU="No ISO files found"
730 menuentry "$NO_ISO_MENU (Press enter to reboot ...)" {
731 echo -e "\n Rebooting ... "