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_power {
20 configfile ${vtoy_path}/grub/power.cfg
23 function get_os_type {
25 for file in "efi/microsoft" "sources/boot.wim" "boot/bcd" "bootmgr.efi" "boot/etfsboot.com" "BOOT/etfsboot.com"; do
26 if [ -e $1/$file ]; then
32 if [ -n "${vtdebug_flag}" ]; then
37 function vt_check_pe {
40 if [ -f $1/HBCD_PE.ini ]; then
41 set ventoy_compatible=YES
43 elif [ -f $1/easyu.flg ]; then
45 elif [ -f $1/USM.ICO ]; then
47 elif [ -d $1/USM_TOOL ]; then
52 function locate_initrd {
53 vt_linux_locate_initrd
55 if [ -n "${vtdebug_flag}" ]; then
61 function find_wim_file {
64 for file in "sources/boot.wim" "sources/BOOT.WIM" "Sources/Win10PEx64.WIM" "boot/BOOT.WIM" \
65 "winpe_x64.wim" "boot/10pex64.wim" "BOOT/USM1PE6L.WIM" "BOOT/USM1PE6F.WIM"; do
66 if [ -e $1/$file ]; then
67 set ventoy_wim_file=$1/$file
73 function distro_specify_initrd_file {
74 if [ -e (loop)/boot/all.rdz ]; then
75 vt_linux_specify_initrd_file /boot/all.rdz
76 elif [ -e (loop)/boot/xen.gz ]; then
77 if [ -e (loop)/install.img ]; then
78 vt_linux_specify_initrd_file /install.img
80 elif [ -d (loop)/casper ]; then
81 if [ -e (loop)/casper/initrd ]; then
82 vt_linux_specify_initrd_file /casper/initrd
84 if [ -e (loop)/casper/initrd-oem ]; then
85 vt_linux_specify_initrd_file /casper/initrd-oem
87 elif [ -e (loop)/boot/grub/initrd.xz ]; then
88 vt_linux_specify_initrd_file /boot/grub/initrd.xz
89 elif [ -e (loop)/initrd.gz ]; then
90 vt_linux_specify_initrd_file /initrd.gz
91 elif [ -e (loop)/slax/boot/initrfs.img ]; then
92 vt_linux_specify_initrd_file /slax/boot/initrfs.img
93 elif [ -e (loop)/pmagic/initrd.img ]; then
94 vt_linux_specify_initrd_file /pmagic/initrd.img
95 elif [ -e (loop)/boot/initrd.xz ]; then
96 vt_linux_specify_initrd_file /boot/initrd.xz
97 elif [ -f (loop)/boot/initrd ]; then
98 vt_linux_specify_initrd_file /boot/initrd
99 elif [ -f (loop)/boot/x86_64/loader/initrd ]; then
100 vt_linux_specify_initrd_file /boot/x86_64/loader/initrd
101 elif [ -f (loop)/boot/initramfs-x86_64.img ]; then
102 vt_linux_specify_initrd_file /boot/initramfs-x86_64.img
109 function distro_specify_initrd_file_phase2 {
110 if [ -f (loop)/boot/initrd.img ]; then
111 vt_linux_specify_initrd_file /boot/initrd.img
112 elif [ -f (loop)/Setup/initrd.gz ]; then
113 vt_linux_specify_initrd_file /Setup/initrd.gz
114 elif [ -f (loop)/isolinux/initramfs ]; then
115 vt_linux_specify_initrd_file /isolinux/initramfs
116 elif [ -f (loop)/boot/iniramfs.igz ]; then
117 vt_linux_specify_initrd_file /boot/iniramfs.igz
118 elif [ -f (loop)/initrd-x86_64 ]; then
119 vt_linux_specify_initrd_file /initrd-x86_64
120 elif [ -f (loop)/live/initrd.img ]; then
121 vt_linux_specify_initrd_file /live/initrd.img
126 function uefi_windows_menu_func {
129 if [ "$ventoy_compatible" = "NO" ]; then
131 if [ -n "$ventoy_wim_file" ]; then
132 vt_windows_locate_wim $ventoy_wim_file
136 vt_windows_chain_data ${1}${chosen_path}
138 if [ -n "${vtdebug_flag}" ]; then
142 if [ -n "$vtoy_chain_mem_addr" ]; then
143 terminal_output console
144 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
147 echo "chain empty failed"
152 function uefi_linux_menu_func {
153 if [ "$ventoy_compatible" = "NO" ]; then
154 vt_load_cpio ${vtoy_path}/ventoy.cpio $2 $1
156 vt_linux_clear_initrd
158 if [ -d (loop)/pmagic ]; then
159 vt_linux_specify_initrd_file /pmagic/initrd.img
161 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
162 if [ -e (loop)/$file ]; then
163 vt_linux_parse_initrd_grub file (loop)/$file
168 # special process for special distros
169 if [ -d (loop)/loader/entries ]; then
170 set LoadIsoEfiDriver=on
171 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
172 elif [ -d (loop)/boot/grub ]; then
173 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
176 if [ -e (loop)/syslinux/alt0/full.cz ]; then
177 set LoadIsoEfiDriver=on
178 set FirstTryBootFile='@EFI@BOOT@grubx64.efi'
181 distro_specify_initrd_file
183 vt_linux_initrd_count vtcount
184 if [ $vtcount -eq 0 ]; then
185 distro_specify_initrd_file_phase2
187 if [ "$vt_efi_dir" = "NO" ]; then
188 if [ -f (loop)/efi.img ]; then
189 vt_add_replace_file 0 "initrd"
197 vt_linux_chain_data ${1}${chosen_path}
199 if [ -n "$vtoy_chain_mem_addr" ]; then
200 terminal_output console
201 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}
204 echo "chain empty failed"
210 function uefi_iso_menu_func {
212 if [ -d (loop)/ ]; then
216 if [ -n "$vtisouefi" ]; then
217 set LoadIsoEfiDriver=on
219 elif vt_check_mode 2; then
220 set LoadIsoEfiDriver=on
222 unset LoadIsoEfiDriver
225 vt_chosen_img_path chosen_path
226 vt_select_auto_install ${chosen_path}
227 vt_select_persistence ${chosen_path}
229 if vt_is_udf ${1}${chosen_path}; then
230 set ventoy_fs_probe=udf
232 set ventoy_fs_probe=iso9660
235 loopback loop ${1}${chosen_path}
238 if [ -d (loop)/EFI ]; then
240 elif [ -d (loop)/efi ]; then
246 if [ -n "$vtcompat" ]; then
247 set ventoy_compatible=YES
249 elif vt_check_mode 1; then
250 set ventoy_compatible=YES
252 vt_check_compatible (loop)
255 vt_img_sector ${1}${chosen_path}
257 if [ "$vtoy_os" = "Windows" ]; then
259 if [ "$VT_PE_SUPPORT" != "YES" ]; then
260 if [ "$ventoy_fs_probe" = "iso9660" ]; then
261 set ventoy_compatible=YES
265 uefi_windows_menu_func $1 ${chosen_path}
267 uefi_linux_menu_func $1 ${chosen_path}
270 terminal_output gfxterm
273 function uefi_iso_memdisk {
274 vt_chosen_img_path chosen_path
276 echo 'Loading ISO file to memory ...'
277 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
279 terminal_output console
280 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}
285 function legacy_windows_menu_func {
288 if [ "$ventoy_compatible" = "NO" ]; then
290 if [ -n "$ventoy_wim_file" ]; then
291 vt_windows_locate_wim $ventoy_wim_file
292 elif [ -n "${vtdebug_flag}" ]; then
293 echo No wim file found
297 vt_windows_chain_data ${1}${chosen_path}
299 if [ -n "${vtdebug_flag}" ]; then
303 if [ -n "$vtoy_chain_mem_addr" ]; then
304 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
307 echo "chain empty failed"
312 function legacy_linux_menu_func {
313 if [ "$ventoy_compatible" = "NO" ]; then
314 vt_load_cpio $vtoy_path/ventoy.cpio $2 $1
316 vt_linux_clear_initrd
318 if [ -d (loop)/pmagic ]; then
319 vt_linux_specify_initrd_file /pmagic/initrd.img
321 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
322 if [ -d (loop)/$dir ]; then
323 vt_linux_parse_initrd_isolinux (loop)/$dir/
328 # special process for special distros
330 if [ -d (loop)/arch/boot/syslinux ]; then
331 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
332 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
335 elif [ -d (loop)/manjaro ]; then
336 if [ -e (loop)/boot/grub/kernels.cfg ]; then
337 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
339 elif [ -e (loop)/boot/grub/grub.cfg ]; then
340 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
343 distro_specify_initrd_file
345 vt_linux_initrd_count vtcount
346 if [ $vtcount -eq 0 ]; then
347 distro_specify_initrd_file_phase2
353 vt_linux_chain_data ${1}${chosen_path}
355 if [ -n "${vtdebug_flag}" ]; then
359 if [ -n "$vtoy_chain_mem_addr" ]; then
360 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
363 echo "chain empty failed"
368 function legacy_iso_menu_func {
370 if [ -d (loop)/ ]; then
374 vt_chosen_img_path chosen_path
375 vt_select_auto_install ${chosen_path}
376 vt_select_persistence ${chosen_path}
378 if vt_is_udf ${1}${chosen_path}; then
379 set ventoy_fs_probe=udf
381 set ventoy_fs_probe=iso9660
384 loopback loop ${1}${chosen_path}
388 if [ -n "$vtcompat" ]; then
389 set ventoy_compatible=YES
391 elif vt_check_mode 1; then
392 set ventoy_compatible=YES
394 vt_check_compatible (loop)
397 vt_img_sector ${1}${chosen_path}
399 if [ "$vtoy_os" = "Windows" ]; then
401 if [ "$VT_PE_SUPPORT" != "YES" ]; then
402 if [ "$ventoy_fs_probe" = "iso9660" ]; then
403 set ventoy_compatible=YES
407 legacy_windows_menu_func $1 ${chosen_path}
409 legacy_linux_menu_func $1 ${chosen_path}
413 function legacy_iso_memdisk {
414 vt_chosen_img_path chosen_path
416 linux16 $vtoy_path/memdisk iso raw
417 echo "Loading ISO file to memory ..."
418 initrd16 ${1}${chosen_path}
422 function iso_common_menuentry {
423 if [ "$grub_platform" = "pc" ]; then
424 if vt_check_mode 0; then
425 legacy_iso_memdisk $iso_path
427 legacy_iso_menu_func $iso_path
430 if vt_check_mode 0; then
431 uefi_iso_memdisk $iso_path
433 uefi_iso_menu_func $iso_path
438 function iso_unsupport_menuentry {
439 echo -e "\n The name of the iso file could NOT contain space or non-ascii characters. \n"
440 echo -e "\n Will return to main menu after 10 seconds ...\n"
445 function wim_common_menuentry {
446 vt_chosen_img_path chosen_path
447 vt_wim_chain_data ${iso_path}${chosen_path}
448 if [ -n "${vtdebug_flag}" ]; then
452 if [ -n "$vtoy_chain_mem_addr" ]; then
453 if [ "$grub_platform" = "pc" ]; then
454 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
456 terminal_output console
457 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
461 echo "chain empty failed"
466 function wim_unsupport_menuentry {
467 echo -e "\n The name of the wim file could NOT contain space or non-ascii characters. \n"
468 echo -e "\n Will return to main menu after 10 seconds ...\n"
472 #############################################################
473 #############################################################
474 #############################################################
475 ####### Main Process ###########
476 #############################################################
477 #############################################################
478 #############################################################
480 set VENTOY_VERSION="1.0.12"
482 # Default menu display mode, you can change it as you want.
485 set VTOY_DEFAULT_MENU_MODE=0
490 set VTOY_MEM_DISK_STR="[Memdisk]"
491 set VTOY_ISO_RAW_STR="ISO RAW"
492 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
494 set VTOY_F2_CMD="ventoy_power"
496 if [ "$grub_platform" = "pc" ]; then
497 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION BIOS www.ventoy.net"
499 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION UEFI www.ventoy.net"
502 vt_device $root vtoy_dev
504 if [ "$vtoy_dev" = "tftp" ]; then
505 set vtoy_path=($root)
506 for vtid in 0 1 2 3; do
507 if [ -d (hd$vtid,2)/ventoy ]; then
508 set iso_path=(hd$vtid,1)
509 set vtoy_efi_part=(hd$vtid,2)
514 set vtoy_path=($root)/ventoy
515 set iso_path=($vtoy_dev,1)
516 set vtoy_efi_part=($vtoy_dev,2)
522 if [ -f $iso_path/ventoy/ventoy.json ]; then
523 vt_load_plugin $iso_path
526 if [ -f $iso_path/ventoy/ventoy_wimboot.img ]; then
527 vt_load_wimboot $iso_path/ventoy/ventoy_wimboot.img
528 elif [ -f $vtoy_efi_part/ventoy/ventoy_wimboot.img ]; then
529 vt_load_wimboot $vtoy_efi_part/ventoy/ventoy_wimboot.img
533 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
534 set VTOY_F3_CMD="vt_dynamic_menu 1 1"
535 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:TreeView"
537 set VTOY_F3_CMD="vt_dynamic_menu 1 0"
538 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:ListView"
542 if [ -n "$vtoy_gfxmode" ]; then
543 set gfxmode=$vtoy_gfxmode
545 set gfxmode=1920x1080,1366x768,1024x768
548 if [ -n "$vtoy_theme" ]; then
549 set theme=$vtoy_theme
551 set theme=$prefix/themes/ventoy/theme.txt
554 terminal_output gfxterm
556 #colect all image files (iso files)
557 set ventoy_img_count=0
558 vt_list_img $iso_path ventoy_img_count
561 if [ $ventoy_img_count -gt 0 ]; then
562 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
568 if [ -n "$VTOY_NO_ISO_TIP" ]; then
569 NO_ISO_MENU="No ISO files found, $VTOY_NO_ISO_TIP"
570 elif [ -n "$VTOY_DEFAULT_SEARCH_ROOT" ]; then
571 NO_ISO_MENU="No ISO files found, please check VTOY_DEFAULT_SEARCH_ROOT"
573 NO_ISO_MENU="No ISO files found"
575 menuentry "$NO_ISO_MENU (Press enter to reboot ...)" {
576 echo -e "\n Rebooting ... "