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
187 function uefi_windows_menu_func {
190 if [ "$ventoy_compatible" = "NO" ]; then
192 if [ "$ventoy_fs_probe" = "iso9660" ]; then
194 vt_iso9660_nojoliet 1
198 for file in "efi/microsoft/boot/bcd"; do
199 vt_windows_collect_wim_patch bcd (loop)/$file
202 vt_windows_count_wim_patch vt_wim_cnt
203 if [ $vt_wim_cnt -eq 0 ]; then
204 distro_specify_wim_patch_phase2
211 vt_windows_chain_data ${1}${chosen_path}
214 if [ -n "$vtoy_chain_mem_addr" ]; then
216 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
219 echo "chain empty failed"
224 function uefi_linux_menu_func {
225 if [ "$ventoy_compatible" = "NO" ]; then
227 if [ "$ventoy_fs_probe" = "udf" ]; then
229 set ventoy_fs_probe=iso9660
233 vt_load_cpio ${vtoy_path}/ventoy.cpio $2 $1
235 vt_linux_clear_initrd
237 if [ -d (loop)/pmagic ]; then
238 vt_linux_specify_initrd_file /pmagic/initrd.img
240 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
241 if [ -e (loop)/$file ]; then
242 vt_linux_parse_initrd_grub file (loop)/$file
247 # special process for special distros
248 if [ -d (loop)/loader/entries ]; then
249 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
250 elif [ -d (loop)/boot/grub ]; then
251 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
254 distro_specify_initrd_file
256 vt_linux_initrd_count vtcount
257 if [ $vtcount -eq 0 ]; then
258 distro_specify_initrd_file_phase2
260 if [ "$vt_efi_dir" = "NO" ]; then
261 if [ -f (loop)/efi.img ]; then
262 vt_add_replace_file 0 "initrd"
269 if [ -d (loop)/loader/entries ]; then
270 vt_linux_get_main_initrd_index vtindex
272 if [ -d (loop)/arch ]; then
273 if [ -f (loop)/arch/boot/x86_64/archiso.img ]; then
274 vt_add_replace_file $vtindex "EFI\\archiso\\archiso.img"
275 elif [ -f (loop)/boot/initramfs_x86_64.img ]; then
276 vt_add_replace_file $vtindex "boot\\initramfs_x86_64.img"
278 elif [ -f (loop)/EFI/BOOT/initrd.gz ]; then
279 vt_add_replace_file $vtindex "EFI\\BOOT\\initrd.gz"
281 elif [ -e (loop)/syslinux/alt0/full.cz ]; then
282 vt_add_replace_file 0 "EFI\\BOOT\\full.cz"
283 set FirstTryBootFile='@EFI@BOOT@grubx64.efi'
288 vt_linux_chain_data ${1}${chosen_path}
290 if [ -n "$vtoy_chain_mem_addr" ]; then
292 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}
295 echo "chain empty failed"
301 function uefi_iso_menu_func {
303 if [ -d (loop)/ ]; then
307 if [ -n "$vtisouefi" ]; then
308 set LoadIsoEfiDriver=on
310 elif vt_check_mode 2; then
311 set LoadIsoEfiDriver=on
313 unset LoadIsoEfiDriver
316 vt_chosen_img_path chosen_path
317 vt_select_auto_install ${chosen_path}
318 vt_select_persistence ${chosen_path}
320 if vt_is_udf ${1}${chosen_path}; then
321 set ventoy_fs_probe=udf
323 set ventoy_fs_probe=iso9660
324 vt_iso9660_nojoliet 0
327 loopback loop ${1}${chosen_path}
330 if [ -d (loop)/EFI ]; then
332 elif [ -d (loop)/efi ]; then
338 if [ -n "$vtcompat" ]; then
339 set ventoy_compatible=YES
341 elif vt_check_mode 1; then
342 set ventoy_compatible=YES
344 vt_check_compatible (loop)
347 vt_img_sector ${1}${chosen_path}
349 if [ "$vtoy_os" = "Windows" ]; then
350 vt_check_compatible_pe (loop)
351 uefi_windows_menu_func $1 ${chosen_path}
353 uefi_linux_menu_func $1 ${chosen_path}
359 function uefi_iso_memdisk {
360 vt_chosen_img_path chosen_path
362 echo 'Loading ISO file to memory ...'
363 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
366 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}
373 function legacy_windows_menu_func {
376 if [ "$ventoy_compatible" = "NO" ]; then
378 if [ "$ventoy_fs_probe" = "iso9660" ]; then
380 vt_iso9660_nojoliet 1
384 for file in "boot/bcd" "/efi/microsoft/boot/bcd" "SSTR/BCD"; do
385 vt_windows_collect_wim_patch bcd (loop)/$file
388 distro_specify_wim_patch
390 vt_windows_count_wim_patch vt_wim_cnt
391 if [ $vt_wim_cnt -eq 0 ]; then
392 distro_specify_wim_patch_phase2
399 vt_windows_chain_data ${1}${chosen_path}
402 if [ -n "$vtoy_chain_mem_addr" ]; then
403 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
406 echo "chain empty failed"
411 function legacy_linux_menu_func {
412 if [ "$ventoy_compatible" = "NO" ]; then
414 if [ "$ventoy_fs_probe" = "udf" ]; then
416 set ventoy_fs_probe=iso9660
420 vt_load_cpio $vtoy_path/ventoy.cpio $2 $1
422 vt_linux_clear_initrd
424 if [ -d (loop)/pmagic ]; then
425 vt_linux_specify_initrd_file /pmagic/initrd.img
427 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
428 if [ -d (loop)/$dir ]; then
429 vt_linux_parse_initrd_isolinux (loop)/$dir/
434 # special process for special distros
436 if [ -d (loop)/arch/boot/syslinux ]; then
437 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
438 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
441 elif [ -d (loop)/manjaro ]; then
442 if [ -e (loop)/boot/grub/kernels.cfg ]; then
443 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
445 elif [ -e (loop)/boot/grub/grub.cfg ]; then
446 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
449 distro_specify_initrd_file
451 vt_linux_initrd_count vtcount
452 if [ $vtcount -eq 0 ]; then
453 distro_specify_initrd_file_phase2
459 vt_linux_chain_data ${1}${chosen_path}
462 if [ -n "$vtoy_chain_mem_addr" ]; then
463 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
466 echo "chain empty failed"
471 function legacy_iso_menu_func {
473 if [ -d (loop)/ ]; then
477 vt_chosen_img_path chosen_path
478 vt_select_auto_install ${chosen_path}
479 vt_select_persistence ${chosen_path}
481 if vt_is_udf ${1}${chosen_path}; then
482 set ventoy_fs_probe=udf
484 set ventoy_fs_probe=iso9660
485 vt_iso9660_nojoliet 0
488 loopback loop ${1}${chosen_path}
491 if [ -n "$vtcompat" ]; then
492 set ventoy_compatible=YES
494 elif vt_check_mode 1; then
495 set ventoy_compatible=YES
497 vt_check_compatible (loop)
500 vt_img_sector ${1}${chosen_path}
502 if [ "$vtoy_os" = "Windows" ]; then
503 vt_check_compatible_pe (loop)
504 legacy_windows_menu_func $1 ${chosen_path}
506 legacy_linux_menu_func $1 ${chosen_path}
510 function legacy_iso_memdisk {
511 vt_chosen_img_path chosen_path
513 linux16 $vtoy_path/memdisk iso raw
514 echo "Loading ISO file to memory ..."
515 initrd16 ${1}${chosen_path}
519 function iso_common_menuentry {
520 if [ "$grub_platform" = "pc" ]; then
521 if vt_check_mode 0; then
522 legacy_iso_memdisk $iso_path
524 legacy_iso_menu_func $iso_path
527 if vt_check_mode 0; then
528 uefi_iso_memdisk $iso_path
530 uefi_iso_menu_func $iso_path
535 function common_unsupport_menuentry {
536 echo -e "\n The name of the iso file could NOT contain space or non-ascii characters. \n"
537 echo -e " 文件名中不能有中文或空格 \n"
538 echo -e "\n Will return to main menu after 10 seconds ...\n"
542 function iso_unsupport_menuentry {
543 common_unsupport_menuentry
546 function wim_common_menuentry {
547 vt_chosen_img_path chosen_path
548 vt_wim_chain_data ${iso_path}${chosen_path}
552 if [ -n "$vtoy_chain_mem_addr" ]; then
553 if [ "$grub_platform" = "pc" ]; then
554 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
557 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
562 echo "chain empty failed"
567 function wim_unsupport_menuentry {
568 common_unsupport_menuentry
571 function efi_common_menuentry {
572 vt_chosen_img_path chosen_path
575 chainloader ${iso_path}${chosen_path}
580 function efi_unsupport_menuentry {
581 common_unsupport_menuentry
586 #############################################################
587 #############################################################
588 #############################################################
589 ####### Main Process ###########
590 #############################################################
591 #############################################################
592 #############################################################
594 set VENTOY_VERSION="1.0.15"
596 # Default menu display mode, you can change it as you want.
599 set VTOY_DEFAULT_MENU_MODE=0
601 set VTOY_MEM_DISK_STR="[Memdisk]"
602 set VTOY_ISO_RAW_STR="Compatible Mode"
603 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
605 set VTOY_F2_CMD="ventoy_power"
606 set VTOY_F4_CMD="ventoy_localboot"
607 set VTOY_F5_CMD="ventoy_diagnosis"
609 if [ "$grub_platform" = "pc" ]; then
610 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION BIOS www.ventoy.net"
612 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION UEFI www.ventoy.net"
615 vt_device $root vtoy_dev
617 if [ "$vtoy_dev" = "tftp" ]; then
618 set vtoy_path=($root)
619 for vtid in 0 1 2 3; do
620 if [ -d (hd$vtid,2)/ventoy ]; then
621 set iso_path=(hd$vtid,1)
622 set vtoy_efi_part=(hd$vtid,2)
628 if [ -f $iso_path/ventoy/ventoy.json ]; then
629 set vt_plugin_path=$iso_path
631 set vt_plugin_path=$prefix
632 vt_load_plugin $vt_plugin_path
635 if [ "$prefix" = "(ventoydisk)/grub" ]; then
636 set vtoy_path=(ventoydisk)/ventoy
638 set vtoy_path=($root)/ventoy
641 set iso_path=($vtoy_dev,1)
642 set vtoy_efi_part=($vtoy_dev,2)
644 set vt_plugin_path=$iso_path
649 if [ -f $iso_path/ventoy/ventoy.json ]; then
650 vt_load_plugin $iso_path
653 if [ -n "$VTOY_MENU_TIMEOUT" ]; then
654 set timeout=$VTOY_MENU_TIMEOUT
659 if [ -f $iso_path/ventoy/ventoy_wimboot.img ]; then
660 vt_load_wimboot $iso_path/ventoy/ventoy_wimboot.img
661 elif [ -f $vtoy_efi_part/ventoy/ventoy_wimboot.img ]; then
662 vt_load_wimboot $vtoy_efi_part/ventoy/ventoy_wimboot.img
666 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
667 set VTOY_F3_CMD="vt_dynamic_menu 1 1"
668 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:TreeView F4:Localboot F5:Debug"
670 set VTOY_F3_CMD="vt_dynamic_menu 1 0"
671 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:ListView F4:Localboot F5:Debug"
675 if [ -n "$vtoy_gfxmode" ]; then
676 set gfxmode=$vtoy_gfxmode
678 set gfxmode=1920x1080,1366x768,1024x768
681 if [ "$vtoy_display_mode" = "CLI" ]; then
682 terminal_output console
684 if [ -n "$vtoy_theme" ]; then
685 set theme=$vtoy_theme
687 set theme=$prefix/themes/ventoy/theme.txt
689 terminal_output gfxterm
692 #colect all image files (iso files)
693 set ventoy_img_count=0
694 vt_list_img $iso_path ventoy_img_count
697 if [ $ventoy_img_count -gt 0 ]; then
698 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
704 if [ -n "$VTOY_NO_ISO_TIP" ]; then
705 NO_ISO_MENU="No ISO files found, $VTOY_NO_ISO_TIP"
706 elif [ -n "$VTOY_DEFAULT_SEARCH_ROOT" ]; then
707 NO_ISO_MENU="No ISO files found, please check VTOY_DEFAULT_SEARCH_ROOT"
709 NO_ISO_MENU="No ISO files found"
711 menuentry "$NO_ISO_MENU (Press enter to reboot ...)" {
712 echo -e "\n Rebooting ... "