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
159 function uefi_windows_menu_func {
162 if [ "$ventoy_compatible" = "NO" ]; then
164 if [ "$ventoy_fs_probe" = "iso9660" ]; then
166 vt_iso9660_nojoliet 1
170 for file in "efi/microsoft/boot/bcd"; do
171 vt_windows_collect_wim_patch bcd (loop)/$file
174 vt_windows_count_wim_patch vt_wim_cnt
175 if [ $vt_wim_cnt -eq 0 ]; then
176 distro_specify_wim_patch_phase2
183 vt_windows_chain_data ${1}${chosen_path}
186 if [ -n "$vtoy_chain_mem_addr" ]; then
187 terminal_output console
188 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
191 echo "chain empty failed"
196 function uefi_linux_menu_func {
197 if [ "$ventoy_compatible" = "NO" ]; then
199 if [ "$ventoy_fs_probe" = "udf" ]; then
201 set ventoy_fs_probe=iso9660
205 vt_load_cpio ${vtoy_path}/ventoy.cpio $2 $1
207 vt_linux_clear_initrd
209 if [ -d (loop)/pmagic ]; then
210 vt_linux_specify_initrd_file /pmagic/initrd.img
212 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
213 if [ -e (loop)/$file ]; then
214 vt_linux_parse_initrd_grub file (loop)/$file
219 # special process for special distros
220 if [ -d (loop)/loader/entries ]; then
221 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
222 elif [ -d (loop)/boot/grub ]; then
223 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
226 distro_specify_initrd_file
228 vt_linux_initrd_count vtcount
229 if [ $vtcount -eq 0 ]; then
230 distro_specify_initrd_file_phase2
232 if [ "$vt_efi_dir" = "NO" ]; then
233 if [ -f (loop)/efi.img ]; then
234 vt_add_replace_file 0 "initrd"
241 if [ -d (loop)/loader/entries ]; then
242 vt_linux_get_main_initrd_index vtindex
244 if [ -d (loop)/arch ]; then
245 if [ -f (loop)/arch/boot/x86_64/archiso.img ]; then
246 vt_add_replace_file $vtindex "EFI\\archiso\\archiso.img"
247 elif [ -f (loop)/boot/initramfs_x86_64.img ]; then
248 vt_add_replace_file $vtindex "boot\\initramfs_x86_64.img"
250 elif [ -f (loop)/EFI/BOOT/initrd.gz ]; then
251 vt_add_replace_file $vtindex "EFI\\BOOT\\initrd.gz"
253 elif [ -e (loop)/syslinux/alt0/full.cz ]; then
254 vt_add_replace_file 0 "EFI\\BOOT\\full.cz"
255 set FirstTryBootFile='@EFI@BOOT@grubx64.efi'
260 vt_linux_chain_data ${1}${chosen_path}
262 if [ -n "$vtoy_chain_mem_addr" ]; then
263 terminal_output console
264 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}
267 echo "chain empty failed"
273 function uefi_iso_menu_func {
275 if [ -d (loop)/ ]; then
279 if [ -n "$vtisouefi" ]; then
280 set LoadIsoEfiDriver=on
282 elif vt_check_mode 2; then
283 set LoadIsoEfiDriver=on
285 unset LoadIsoEfiDriver
288 vt_chosen_img_path chosen_path
289 vt_select_auto_install ${chosen_path}
290 vt_select_persistence ${chosen_path}
292 if vt_is_udf ${1}${chosen_path}; then
293 set ventoy_fs_probe=udf
295 set ventoy_fs_probe=iso9660
296 vt_iso9660_nojoliet 0
299 loopback loop ${1}${chosen_path}
302 if [ -d (loop)/EFI ]; then
304 elif [ -d (loop)/efi ]; then
310 if [ -n "$vtcompat" ]; then
311 set ventoy_compatible=YES
313 elif vt_check_mode 1; then
314 set ventoy_compatible=YES
316 vt_check_compatible (loop)
319 vt_img_sector ${1}${chosen_path}
321 if [ "$vtoy_os" = "Windows" ]; then
322 vt_check_compatible_pe (loop)
323 uefi_windows_menu_func $1 ${chosen_path}
325 uefi_linux_menu_func $1 ${chosen_path}
328 terminal_output gfxterm
331 function uefi_iso_memdisk {
332 vt_chosen_img_path chosen_path
334 echo 'Loading ISO file to memory ...'
335 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
337 terminal_output console
338 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 function legacy_windows_menu_func {
346 if [ "$ventoy_compatible" = "NO" ]; then
348 if [ "$ventoy_fs_probe" = "iso9660" ]; then
350 vt_iso9660_nojoliet 1
354 for file in "boot/bcd" "/efi/microsoft/boot/bcd" "SSTR/BCD"; do
355 vt_windows_collect_wim_patch bcd (loop)/$file
358 distro_specify_wim_patch
360 vt_windows_count_wim_patch vt_wim_cnt
361 if [ $vt_wim_cnt -eq 0 ]; then
362 distro_specify_wim_patch_phase2
369 vt_windows_chain_data ${1}${chosen_path}
372 if [ -n "$vtoy_chain_mem_addr" ]; then
373 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
376 echo "chain empty failed"
381 function legacy_linux_menu_func {
382 if [ "$ventoy_compatible" = "NO" ]; then
384 if [ "$ventoy_fs_probe" = "udf" ]; then
386 set ventoy_fs_probe=iso9660
390 vt_load_cpio $vtoy_path/ventoy.cpio $2 $1
392 vt_linux_clear_initrd
394 if [ -d (loop)/pmagic ]; then
395 vt_linux_specify_initrd_file /pmagic/initrd.img
397 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
398 if [ -d (loop)/$dir ]; then
399 vt_linux_parse_initrd_isolinux (loop)/$dir/
404 # special process for special distros
406 if [ -d (loop)/arch/boot/syslinux ]; then
407 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
408 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
411 elif [ -d (loop)/manjaro ]; then
412 if [ -e (loop)/boot/grub/kernels.cfg ]; then
413 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
415 elif [ -e (loop)/boot/grub/grub.cfg ]; then
416 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
419 distro_specify_initrd_file
421 vt_linux_initrd_count vtcount
422 if [ $vtcount -eq 0 ]; then
423 distro_specify_initrd_file_phase2
429 vt_linux_chain_data ${1}${chosen_path}
432 if [ -n "$vtoy_chain_mem_addr" ]; then
433 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
436 echo "chain empty failed"
441 function legacy_iso_menu_func {
443 if [ -d (loop)/ ]; then
447 vt_chosen_img_path chosen_path
448 vt_select_auto_install ${chosen_path}
449 vt_select_persistence ${chosen_path}
451 if vt_is_udf ${1}${chosen_path}; then
452 set ventoy_fs_probe=udf
454 set ventoy_fs_probe=iso9660
455 vt_iso9660_nojoliet 0
458 loopback loop ${1}${chosen_path}
461 if [ -n "$vtcompat" ]; then
462 set ventoy_compatible=YES
464 elif vt_check_mode 1; then
465 set ventoy_compatible=YES
467 vt_check_compatible (loop)
470 vt_img_sector ${1}${chosen_path}
472 if [ "$vtoy_os" = "Windows" ]; then
473 vt_check_compatible_pe (loop)
474 legacy_windows_menu_func $1 ${chosen_path}
476 legacy_linux_menu_func $1 ${chosen_path}
480 function legacy_iso_memdisk {
481 vt_chosen_img_path chosen_path
483 linux16 $vtoy_path/memdisk iso raw
484 echo "Loading ISO file to memory ..."
485 initrd16 ${1}${chosen_path}
489 function iso_common_menuentry {
490 if [ "$grub_platform" = "pc" ]; then
491 if vt_check_mode 0; then
492 legacy_iso_memdisk $iso_path
494 legacy_iso_menu_func $iso_path
497 if vt_check_mode 0; then
498 uefi_iso_memdisk $iso_path
500 uefi_iso_menu_func $iso_path
505 function iso_unsupport_menuentry {
506 echo -e "\n The name of the iso file could NOT contain space or non-ascii characters. \n"
507 echo -e "\n Will return to main menu after 10 seconds ...\n"
512 function wim_common_menuentry {
513 vt_chosen_img_path chosen_path
514 vt_wim_chain_data ${iso_path}${chosen_path}
518 if [ -n "$vtoy_chain_mem_addr" ]; then
519 if [ "$grub_platform" = "pc" ]; then
520 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
522 terminal_output console
523 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
527 echo "chain empty failed"
532 function wim_unsupport_menuentry {
533 echo -e "\n The name of the wim file could NOT contain space or non-ascii characters. \n"
534 echo -e "\n Will return to main menu after 10 seconds ...\n"
538 #############################################################
539 #############################################################
540 #############################################################
541 ####### Main Process ###########
542 #############################################################
543 #############################################################
544 #############################################################
546 set VENTOY_VERSION="1.0.13"
548 # Default menu display mode, you can change it as you want.
551 set VTOY_DEFAULT_MENU_MODE=0
556 set VTOY_MEM_DISK_STR="[Memdisk]"
557 set VTOY_ISO_RAW_STR="Compatible Mode"
558 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
560 set VTOY_F2_CMD="ventoy_power"
561 set VTOY_F4_CMD="ventoy_localboot"
562 set VTOY_F5_CMD="ventoy_diagnosis"
564 if [ "$grub_platform" = "pc" ]; then
565 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION BIOS www.ventoy.net"
567 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION UEFI www.ventoy.net"
570 vt_device $root vtoy_dev
572 if [ "$vtoy_dev" = "tftp" ]; then
573 set vtoy_path=($root)
574 for vtid in 0 1 2 3; do
575 if [ -d (hd$vtid,2)/ventoy ]; then
576 set iso_path=(hd$vtid,1)
577 set vtoy_efi_part=(hd$vtid,2)
583 if [ "$prefix" = "(ventoydisk)/grub" ]; then
584 set vtoy_path=(ventoydisk)/ventoy
586 set vtoy_path=($root)/ventoy
589 set iso_path=($vtoy_dev,1)
590 set vtoy_efi_part=($vtoy_dev,2)
596 if [ -f $iso_path/ventoy/ventoy.json ]; then
597 vt_load_plugin $iso_path
600 if [ -f $iso_path/ventoy/ventoy_wimboot.img ]; then
601 vt_load_wimboot $iso_path/ventoy/ventoy_wimboot.img
602 elif [ -f $vtoy_efi_part/ventoy/ventoy_wimboot.img ]; then
603 vt_load_wimboot $vtoy_efi_part/ventoy/ventoy_wimboot.img
607 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
608 set VTOY_F3_CMD="vt_dynamic_menu 1 1"
609 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:TreeView F4:Localboot F5:Debug"
611 set VTOY_F3_CMD="vt_dynamic_menu 1 0"
612 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:ListView F4:Localboot F5:Debug"
616 if [ -n "$vtoy_gfxmode" ]; then
617 set gfxmode=$vtoy_gfxmode
619 set gfxmode=1920x1080,1366x768,1024x768
622 if [ -n "$vtoy_theme" ]; then
623 set theme=$vtoy_theme
625 set theme=$prefix/themes/ventoy/theme.txt
628 if [ "$vtoy_display_mode" = "CLI" ]; then
629 terminal_output console
631 terminal_output gfxterm
636 #colect all image files (iso files)
637 set ventoy_img_count=0
638 vt_list_img $iso_path ventoy_img_count
641 if [ $ventoy_img_count -gt 0 ]; then
642 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
648 if [ -n "$VTOY_NO_ISO_TIP" ]; then
649 NO_ISO_MENU="No ISO files found, $VTOY_NO_ISO_TIP"
650 elif [ -n "$VTOY_DEFAULT_SEARCH_ROOT" ]; then
651 NO_ISO_MENU="No ISO files found, please check VTOY_DEFAULT_SEARCH_ROOT"
653 NO_ISO_MENU="No ISO files found"
655 menuentry "$NO_ISO_MENU (Press enter to reboot ...)" {
656 echo -e "\n Rebooting ... "