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 ventoy_ext_menu {
71 if [ -e $vt_plugin_path/ventoy/ventoy_grub.cfg ]; then
72 set ventoy_new_context=1
73 configfile $vt_plugin_path/ventoy/ventoy_grub.cfg
74 unset ventoy_new_context
76 echo "ventoy_grub.cfg NOT exist."
77 echo -e "\npress ENTER to exit ..."
82 function get_os_type {
85 for file in "efi/microsoft/boot/bcd" "sources/boot.wim" "boot/bcd" "bootmgr.efi" "boot/etfsboot.com" ; do
86 if vt_file_exist_nocase (loop)/$file; then
92 if [ -n "${vtdebug_flag}" ]; then
97 function vt_check_compatible_pe {
98 #Check for PE without external tools
99 if [ -f $1/HBCD_PE.ini ]; then
100 set ventoy_compatible=YES
104 function locate_initrd {
105 vt_linux_locate_initrd
107 if [ -n "${vtdebug_flag}" ]; then
113 function locate_wim {
114 vt_windows_locate_wim_patch (loop)
116 if [ -n "${vtdebug_flag}" ]; then
117 echo '###############################################'
119 echo '###############################################'
124 function distro_specify_wim_patch {
125 if [ -d (loop)/h3pe ]; then
126 vt_windows_collect_wim_patch wim /BOOT/H3_10PE.WIM
127 vt_windows_collect_wim_patch wim /BOOT/H3_7PE.WIM
128 vt_windows_collect_wim_patch wim /BOOT/H3_8PE.WIM
129 vt_windows_collect_wim_patch wim /BOOT/H3_81PE.WIM
133 function distro_specify_wim_patch_phase2 {
134 if [ -f (loop)/boot/boot.wim ]; then
135 vt_windows_collect_wim_patch wim /boot/boot.wim
140 function distro_specify_initrd_file {
141 if [ -e (loop)/boot/all.rdz ]; then
142 vt_linux_specify_initrd_file /boot/all.rdz
143 elif [ -e (loop)/boot/xen.gz ]; then
144 if [ -e (loop)/install.img ]; then
145 vt_linux_specify_initrd_file /install.img
147 elif [ -d (loop)/casper ]; then
148 if [ -e (loop)/casper/initrd ]; then
149 vt_linux_specify_initrd_file /casper/initrd
151 if [ -e (loop)/casper/initrd-oem ]; then
152 vt_linux_specify_initrd_file /casper/initrd-oem
154 elif [ -e (loop)/boot/grub/initrd.xz ]; then
155 vt_linux_specify_initrd_file /boot/grub/initrd.xz
156 elif [ -e (loop)/initrd.gz ]; then
157 vt_linux_specify_initrd_file /initrd.gz
158 elif [ -e (loop)/slax/boot/initrfs.img ]; then
159 vt_linux_specify_initrd_file /slax/boot/initrfs.img
160 elif [ -e (loop)/pmagic/initrd.img ]; then
161 vt_linux_specify_initrd_file /pmagic/initrd.img
162 elif [ -e (loop)/boot/initrd.xz ]; then
163 vt_linux_specify_initrd_file /boot/initrd.xz
164 elif [ -f (loop)/boot/initrd ]; then
165 vt_linux_specify_initrd_file /boot/initrd
166 elif [ -f (loop)/boot/x86_64/loader/initrd ]; then
167 vt_linux_specify_initrd_file /boot/x86_64/loader/initrd
168 elif [ -f (loop)/boot/initramfs-x86_64.img ]; then
169 vt_linux_specify_initrd_file /boot/initramfs-x86_64.img
176 function distro_specify_initrd_file_phase2 {
177 if [ -f (loop)/boot/initrd.img ]; then
178 vt_linux_specify_initrd_file /boot/initrd.img
179 elif [ -f (loop)/Setup/initrd.gz ]; then
180 vt_linux_specify_initrd_file /Setup/initrd.gz
181 elif [ -f (loop)/isolinux/initramfs ]; then
182 vt_linux_specify_initrd_file /isolinux/initramfs
183 elif [ -f (loop)/boot/iniramfs.igz ]; then
184 vt_linux_specify_initrd_file /boot/iniramfs.igz
185 elif [ -f (loop)/initrd-x86_64 ]; then
186 vt_linux_specify_initrd_file /initrd-x86_64
187 elif [ -f (loop)/live/initrd.img ]; then
188 vt_linux_specify_initrd_file /live/initrd.img
189 elif [ -f (loop)/initrd.img ]; then
190 vt_linux_specify_initrd_file /initrd.img
191 elif [ -f (loop)/sysresccd/boot/x86_64/sysresccd.img ]; then
192 vt_linux_specify_initrd_file /sysresccd/boot/x86_64/sysresccd.img
193 elif [ -f (loop)/CDlinux/initrd ]; then
194 vt_linux_specify_initrd_file /CDlinux/initrd
195 elif [ -f (loop)/parabola/boot/x86_64/parabolaiso.img ]; then
196 vt_linux_specify_initrd_file /parabola/boot/x86_64/parabolaiso.img
197 if [ -f (loop)/parabola/boot/i686/parabolaiso.img ]; then
198 vt_linux_specify_initrd_file /parabola/boot/i686/parabolaiso.img
200 elif [ -f (loop)/EFI/BOOT/initrd.img ]; then
202 vt_linux_specify_initrd_file /EFI/BOOT/initrd.img
203 if [ "$grub_platform" != "pc" ]; then
204 vt_add_replace_file 0 "initrd.img"
206 elif [ -f (loop)/initrd ]; then
207 vt_linux_specify_initrd_file /initrd
208 elif [ -f (loop)/live/initrd1 ]; then
209 vt_linux_specify_initrd_file /live/initrd1
214 function uefi_windows_menu_func {
217 if [ "$ventoy_compatible" = "NO" ]; then
219 if [ "$ventoy_fs_probe" = "iso9660" ]; then
221 vt_iso9660_nojoliet 1
225 for file in "efi/microsoft/boot/bcd"; do
226 vt_windows_collect_wim_patch bcd (loop)/$file
229 vt_windows_count_wim_patch vt_wim_cnt
230 if [ $vt_wim_cnt -eq 0 ]; then
231 distro_specify_wim_patch_phase2
238 vt_windows_chain_data ${1}${chosen_path}
241 if [ -n "$vtoy_chain_mem_addr" ]; then
243 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
246 echo "chain empty failed"
251 function uefi_linux_menu_func {
252 if [ "$ventoy_compatible" = "NO" ]; then
254 if [ "$ventoy_fs_probe" = "udf" ]; then
256 set ventoy_fs_probe=iso9660
260 vt_load_cpio ${vtoy_path}/ventoy.cpio $2 $1
262 vt_linux_clear_initrd
264 if [ -d (loop)/pmagic ]; then
265 vt_linux_specify_initrd_file /pmagic/initrd.img
267 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
268 if [ -e (loop)/$file ]; then
269 vt_linux_parse_initrd_grub file (loop)/$file
274 # special process for special distros
275 if [ -d (loop)/loader/entries ]; then
276 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
277 elif [ -d (loop)/boot/grub ]; then
278 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
281 distro_specify_initrd_file
283 vt_linux_initrd_count vtcount
284 if [ $vtcount -eq 0 ]; then
285 distro_specify_initrd_file_phase2
287 if [ "$vt_efi_dir" = "NO" ]; then
288 if [ -f (loop)/efi.img ]; then
289 vt_add_replace_file 0 "initrd"
296 if [ -d (loop)/loader/entries ]; then
297 vt_linux_get_main_initrd_index vtindex
299 if [ -d (loop)/arch ]; then
300 if [ -f (loop)/arch/boot/x86_64/archiso.img ]; then
301 vt_add_replace_file $vtindex "EFI\\archiso\\archiso.img"
302 elif [ -f (loop)/boot/initramfs_x86_64.img ]; then
303 vt_add_replace_file $vtindex "boot\\initramfs_x86_64.img"
305 elif [ -f (loop)/EFI/BOOT/initrd.gz ]; then
306 vt_add_replace_file $vtindex "EFI\\BOOT\\initrd.gz"
307 elif [ -f (loop)/loader/entries/thinstation.conf ]; then
308 vt_add_replace_file $vtindex "boot\\initrd"
310 elif [ -d (loop)/EFI/boot/entries ]; then
311 if [ -f (loop)/parabola/boot/x86_64/parabolaiso.img ]; then
312 vt_add_replace_file 0 "EFI\\parabolaiso\\parabolaiso.img"
314 elif [ -e (loop)/syslinux/alt0/full.cz ]; then
315 vt_add_replace_file 0 "EFI\\BOOT\\full.cz"
316 set FirstTryBootFile='@EFI@BOOT@grubx64.efi'
321 vt_linux_chain_data ${1}${chosen_path}
323 if [ -n "$vtoy_chain_mem_addr" ]; then
325 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}
328 echo "chain empty failed"
334 function uefi_iso_menu_func {
336 if [ -d (loop)/ ]; then
340 if [ -n "$vtisouefi" ]; then
341 set LoadIsoEfiDriver=on
343 elif vt_check_mode 2; then
344 set LoadIsoEfiDriver=on
346 unset LoadIsoEfiDriver
349 vt_chosen_img_path chosen_path
350 vt_select_auto_install ${chosen_path}
351 vt_select_persistence ${chosen_path}
353 if vt_is_udf ${1}${chosen_path}; then
354 set ventoy_fs_probe=udf
356 set ventoy_fs_probe=iso9660
357 vt_iso9660_nojoliet 0
360 loopback loop ${1}${chosen_path}
363 if [ -d (loop)/EFI ]; then
365 elif [ -d (loop)/efi ]; then
371 if [ -n "$vtcompat" ]; then
372 set ventoy_compatible=YES
374 elif vt_check_mode 1; then
375 set ventoy_compatible=YES
377 vt_check_compatible (loop)
380 vt_img_sector ${1}${chosen_path}
382 if [ "$vtoy_os" = "Windows" ]; then
383 vt_check_compatible_pe (loop)
384 uefi_windows_menu_func $1 ${chosen_path}
386 uefi_linux_menu_func $1 ${chosen_path}
392 function uefi_iso_memdisk {
393 vt_chosen_img_path chosen_path
395 echo 'Loading ISO file to memory ...'
396 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
399 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}
406 function legacy_windows_menu_func {
409 if [ "$ventoy_compatible" = "NO" ]; then
411 if [ "$ventoy_fs_probe" = "iso9660" ]; then
413 vt_iso9660_nojoliet 1
417 for file in "boot/bcd" "/efi/microsoft/boot/bcd" "SSTR/BCD"; do
418 vt_windows_collect_wim_patch bcd (loop)/$file
421 distro_specify_wim_patch
423 vt_windows_count_wim_patch vt_wim_cnt
424 if [ $vt_wim_cnt -eq 0 ]; then
425 distro_specify_wim_patch_phase2
432 vt_windows_chain_data ${1}${chosen_path}
435 if [ -n "$vtoy_chain_mem_addr" ]; then
436 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
439 echo "chain empty failed"
444 function legacy_linux_menu_func {
445 if [ "$ventoy_compatible" = "NO" ]; then
447 if [ "$ventoy_fs_probe" = "udf" ]; then
449 set ventoy_fs_probe=iso9660
453 vt_load_cpio $vtoy_path/ventoy.cpio $2 $1
455 vt_linux_clear_initrd
457 if [ -d (loop)/pmagic ]; then
458 vt_linux_specify_initrd_file /pmagic/initrd.img
460 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
461 if [ -d (loop)/$dir ]; then
462 vt_linux_parse_initrd_isolinux (loop)/$dir/
467 # special process for special distros
469 if [ -d (loop)/arch/boot/syslinux ]; then
470 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
471 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
474 elif [ -d (loop)/manjaro ]; then
475 if [ -e (loop)/boot/grub/kernels.cfg ]; then
476 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
478 elif [ -e (loop)/boot/grub/grub.cfg ]; then
479 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
482 distro_specify_initrd_file
484 vt_linux_initrd_count vtcount
485 if [ $vtcount -eq 0 ]; then
486 distro_specify_initrd_file_phase2
492 vt_linux_chain_data ${1}${chosen_path}
495 if [ -n "$vtoy_chain_mem_addr" ]; then
496 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
499 echo "chain empty failed"
504 function legacy_iso_menu_func {
506 if [ -d (loop)/ ]; then
510 vt_chosen_img_path chosen_path
511 vt_select_auto_install ${chosen_path}
512 vt_select_persistence ${chosen_path}
514 if vt_is_udf ${1}${chosen_path}; then
515 set ventoy_fs_probe=udf
517 set ventoy_fs_probe=iso9660
518 vt_iso9660_nojoliet 0
521 loopback loop ${1}${chosen_path}
524 if [ -n "$vtcompat" ]; then
525 set ventoy_compatible=YES
527 elif vt_check_mode 1; then
528 set ventoy_compatible=YES
530 vt_check_compatible (loop)
533 vt_img_sector ${1}${chosen_path}
535 if [ "$vtoy_os" = "Windows" ]; then
536 vt_check_compatible_pe (loop)
537 legacy_windows_menu_func $1 ${chosen_path}
539 legacy_linux_menu_func $1 ${chosen_path}
543 function legacy_iso_memdisk {
544 vt_chosen_img_path chosen_path
546 linux16 $vtoy_path/memdisk iso raw
547 echo "Loading ISO file to memory ..."
548 initrd16 ${1}${chosen_path}
552 function iso_common_menuentry {
553 if [ "$grub_platform" = "pc" ]; then
554 if vt_check_mode 0; then
555 legacy_iso_memdisk $vtoy_iso_part
557 legacy_iso_menu_func $vtoy_iso_part
560 if vt_check_mode 0; then
561 uefi_iso_memdisk $vtoy_iso_part
563 uefi_iso_menu_func $vtoy_iso_part
568 function common_unsupport_menuentry {
569 echo -e "\n The name of the iso file could NOT contain space or non-ascii characters. \n"
570 echo -e " 文件名中不能有中文或空格 \n"
571 echo -e "\npress ENTER to exit (请按 回车 键返回) ..."
575 function iso_unsupport_menuentry {
576 common_unsupport_menuentry
579 function wim_common_menuentry {
580 vt_chosen_img_path chosen_path
581 vt_wim_chain_data ${vtoy_iso_part}${chosen_path}
585 if [ -n "$vtoy_chain_mem_addr" ]; then
586 if [ "$grub_platform" = "pc" ]; then
587 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
590 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
595 echo "chain empty failed"
600 function wim_unsupport_menuentry {
601 common_unsupport_menuentry
604 function efi_common_menuentry {
605 vt_chosen_img_path chosen_path
608 chainloader ${vtoy_iso_part}${chosen_path}
613 function efi_unsupport_menuentry {
614 common_unsupport_menuentry
619 #############################################################
620 #############################################################
621 #############################################################
622 ####### Main Process ###########
623 #############################################################
624 #############################################################
625 #############################################################
627 set VENTOY_VERSION="1.0.17"
629 # Default menu display mode, you can change it as you want.
632 set VTOY_DEFAULT_MENU_MODE=0
634 set VTOY_MEM_DISK_STR="[Memdisk]"
635 set VTOY_ISO_RAW_STR="Compatible Mode"
636 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
638 set VTOY_F2_CMD="ventoy_power"
639 set VTOY_F4_CMD="ventoy_localboot"
640 set VTOY_F5_CMD="ventoy_diagnosis"
641 set VTOY_F6_CMD="ventoy_ext_menu"
643 if [ "$grub_platform" = "pc" ]; then
644 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION BIOS www.ventoy.net"
646 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION UEFI www.ventoy.net"
649 vt_device $root vtoy_dev
651 if [ "$vtoy_dev" = "tftp" ]; then
652 set vtoy_path=($root)
653 for vtid in 0 1 2 3; do
654 if [ -d (hd$vtid,2)/ventoy ]; then
655 set vtoy_iso_part=(hd$vtid,1)
656 set vtoy_efi_part=(hd$vtid,2)
662 if [ -f $vtoy_iso_part/ventoy/ventoy.json ]; then
663 set vt_plugin_path=$vtoy_iso_part
665 set vt_plugin_path=$prefix
666 vt_load_plugin $vt_plugin_path
669 if [ "$prefix" = "(ventoydisk)/grub" ]; then
670 set vtoy_path=(ventoydisk)/ventoy
672 set vtoy_path=($root)/ventoy
675 set vtoy_iso_part=($vtoy_dev,1)
676 set vtoy_efi_part=($vtoy_dev,2)
678 set vt_plugin_path=$vtoy_iso_part
683 if [ -f $vtoy_iso_part/ventoy/ventoy.json ]; then
684 vt_load_plugin $vtoy_iso_part
687 if [ -n "$VTOY_MENU_TIMEOUT" ]; then
688 set timeout=$VTOY_MENU_TIMEOUT
693 if [ -f $vtoy_iso_part/ventoy/ventoy_wimboot.img ]; then
694 vt_load_wimboot $vtoy_iso_part/ventoy/ventoy_wimboot.img
695 elif [ -f $vtoy_efi_part/ventoy/ventoy_wimboot.img ]; then
696 vt_load_wimboot $vtoy_efi_part/ventoy/ventoy_wimboot.img
700 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
701 set VTOY_F3_CMD="vt_dynamic_menu 1 1"
702 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:TreeView F4:Localboot F5:Debug F6:ExMenu"
704 set VTOY_F3_CMD="vt_dynamic_menu 1 0"
705 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:ListView F4:Localboot F5:Debug F6:ExMenu"
709 if [ -n "$vtoy_gfxmode" ]; then
710 set gfxmode=$vtoy_gfxmode
712 set gfxmode=1920x1080,1366x768,1024x768
715 if [ "$vtoy_display_mode" = "CLI" ]; then
716 terminal_output console
718 if [ -n "$vtoy_theme" ]; then
719 set theme=$vtoy_theme
721 set theme=$prefix/themes/ventoy/theme.txt
723 terminal_output gfxterm
726 #export necessary variable
732 export VENTOY_VERSION
737 #colect all image files (iso files)
738 set ventoy_img_count=0
739 vt_list_img $vtoy_iso_part ventoy_img_count
742 if [ $ventoy_img_count -gt 0 ]; then
743 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
749 if [ -n "$VTOY_NO_ISO_TIP" ]; then
750 NO_ISO_MENU="No ISO files found, $VTOY_NO_ISO_TIP"
751 elif [ -n "$VTOY_DEFAULT_SEARCH_ROOT" ]; then
752 NO_ISO_MENU="No ISO files found, please check VTOY_DEFAULT_SEARCH_ROOT"
754 NO_ISO_MENU="No ISO files found"
756 menuentry "$NO_ISO_MENU (Press enter to reboot ...)" {
757 echo -e "\n Rebooting ... "