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 ($root)/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
122 elif [ -f (loop)/initrd.img ]; then
123 vt_linux_specify_initrd_file /initrd.img
128 function uefi_windows_menu_func {
131 if [ "$ventoy_compatible" = "NO" ]; then
133 if [ -n "$ventoy_wim_file" ]; then
134 vt_windows_locate_wim $ventoy_wim_file
138 vt_windows_chain_data ${1}${chosen_path}
140 if [ -n "${vtdebug_flag}" ]; then
144 if [ -n "$vtoy_chain_mem_addr" ]; then
145 terminal_output console
146 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
149 echo "chain empty failed"
154 function uefi_linux_menu_func {
155 if [ "$ventoy_compatible" = "NO" ]; then
156 vt_load_cpio ${vtoy_path}/ventoy.cpio $2 $1
158 vt_linux_clear_initrd
160 if [ -d (loop)/pmagic ]; then
161 vt_linux_specify_initrd_file /pmagic/initrd.img
163 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
164 if [ -e (loop)/$file ]; then
165 vt_linux_parse_initrd_grub file (loop)/$file
170 # special process for special distros
171 if [ -d (loop)/loader/entries ]; then
172 set LoadIsoEfiDriver=on
173 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
174 elif [ -d (loop)/boot/grub ]; then
175 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
178 if [ -e (loop)/syslinux/alt0/full.cz ]; then
179 set LoadIsoEfiDriver=on
180 set FirstTryBootFile='@EFI@BOOT@grubx64.efi'
183 distro_specify_initrd_file
185 vt_linux_initrd_count vtcount
186 if [ $vtcount -eq 0 ]; then
187 distro_specify_initrd_file_phase2
189 if [ "$vt_efi_dir" = "NO" ]; then
190 if [ -f (loop)/efi.img ]; then
191 vt_add_replace_file 0 "initrd"
199 vt_linux_chain_data ${1}${chosen_path}
201 if [ -n "$vtoy_chain_mem_addr" ]; then
202 terminal_output console
203 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}
206 echo "chain empty failed"
212 function uefi_iso_menu_func {
214 if [ -d (loop)/ ]; then
218 if [ -n "$vtisouefi" ]; then
219 set LoadIsoEfiDriver=on
221 elif vt_check_mode 2; then
222 set LoadIsoEfiDriver=on
224 unset LoadIsoEfiDriver
227 vt_chosen_img_path chosen_path
228 vt_select_auto_install ${chosen_path}
229 vt_select_persistence ${chosen_path}
231 if vt_is_udf ${1}${chosen_path}; then
232 set ventoy_fs_probe=udf
234 set ventoy_fs_probe=iso9660
237 loopback loop ${1}${chosen_path}
240 if [ -d (loop)/EFI ]; then
242 elif [ -d (loop)/efi ]; then
248 if [ -n "$vtcompat" ]; then
249 set ventoy_compatible=YES
251 elif vt_check_mode 1; then
252 set ventoy_compatible=YES
254 vt_check_compatible (loop)
257 vt_img_sector ${1}${chosen_path}
259 if [ "$vtoy_os" = "Windows" ]; then
261 if [ "$VT_PE_SUPPORT" != "YES" ]; then
262 if [ "$ventoy_fs_probe" = "iso9660" ]; then
263 set ventoy_compatible=YES
267 uefi_windows_menu_func $1 ${chosen_path}
269 uefi_linux_menu_func $1 ${chosen_path}
272 terminal_output gfxterm
275 function uefi_iso_memdisk {
276 vt_chosen_img_path chosen_path
278 echo 'Loading ISO file to memory ...'
279 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
281 terminal_output console
282 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}
287 function legacy_windows_menu_func {
290 if [ "$ventoy_compatible" = "NO" ]; then
292 if [ -n "$ventoy_wim_file" ]; then
293 vt_windows_locate_wim $ventoy_wim_file
294 elif [ -n "${vtdebug_flag}" ]; then
295 echo No wim file found
299 vt_windows_chain_data ${1}${chosen_path}
301 if [ -n "${vtdebug_flag}" ]; then
305 if [ -n "$vtoy_chain_mem_addr" ]; then
306 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
309 echo "chain empty failed"
314 function legacy_linux_menu_func {
315 if [ "$ventoy_compatible" = "NO" ]; then
316 vt_load_cpio $vtoy_path/ventoy.cpio $2 $1
318 vt_linux_clear_initrd
320 if [ -d (loop)/pmagic ]; then
321 vt_linux_specify_initrd_file /pmagic/initrd.img
323 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
324 if [ -d (loop)/$dir ]; then
325 vt_linux_parse_initrd_isolinux (loop)/$dir/
330 # special process for special distros
332 if [ -d (loop)/arch/boot/syslinux ]; then
333 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
334 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
337 elif [ -d (loop)/manjaro ]; then
338 if [ -e (loop)/boot/grub/kernels.cfg ]; then
339 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
341 elif [ -e (loop)/boot/grub/grub.cfg ]; then
342 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
345 distro_specify_initrd_file
347 vt_linux_initrd_count vtcount
348 if [ $vtcount -eq 0 ]; then
349 distro_specify_initrd_file_phase2
355 vt_linux_chain_data ${1}${chosen_path}
357 if [ -n "${vtdebug_flag}" ]; then
361 if [ -n "$vtoy_chain_mem_addr" ]; then
362 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
365 echo "chain empty failed"
370 function legacy_iso_menu_func {
372 if [ -d (loop)/ ]; then
376 vt_chosen_img_path chosen_path
377 vt_select_auto_install ${chosen_path}
378 vt_select_persistence ${chosen_path}
380 if vt_is_udf ${1}${chosen_path}; then
381 set ventoy_fs_probe=udf
383 set ventoy_fs_probe=iso9660
386 loopback loop ${1}${chosen_path}
390 if [ -n "$vtcompat" ]; then
391 set ventoy_compatible=YES
393 elif vt_check_mode 1; then
394 set ventoy_compatible=YES
396 vt_check_compatible (loop)
399 vt_img_sector ${1}${chosen_path}
401 if [ "$vtoy_os" = "Windows" ]; then
403 if [ "$VT_PE_SUPPORT" != "YES" ]; then
404 if [ "$ventoy_fs_probe" = "iso9660" ]; then
405 set ventoy_compatible=YES
409 legacy_windows_menu_func $1 ${chosen_path}
411 legacy_linux_menu_func $1 ${chosen_path}
415 function legacy_iso_memdisk {
416 vt_chosen_img_path chosen_path
418 linux16 $vtoy_path/memdisk iso raw
419 echo "Loading ISO file to memory ..."
420 initrd16 ${1}${chosen_path}
424 function iso_common_menuentry {
425 if [ "$grub_platform" = "pc" ]; then
426 if vt_check_mode 0; then
427 legacy_iso_memdisk $iso_path
429 legacy_iso_menu_func $iso_path
432 if vt_check_mode 0; then
433 uefi_iso_memdisk $iso_path
435 uefi_iso_menu_func $iso_path
440 function iso_unsupport_menuentry {
441 echo -e "\n The name of the iso file could NOT contain space or non-ascii characters. \n"
442 echo -e "\n Will return to main menu after 10 seconds ...\n"
447 function wim_common_menuentry {
448 vt_chosen_img_path chosen_path
449 vt_wim_chain_data ${iso_path}${chosen_path}
450 if [ -n "${vtdebug_flag}" ]; then
454 if [ -n "$vtoy_chain_mem_addr" ]; then
455 if [ "$grub_platform" = "pc" ]; then
456 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
458 terminal_output console
459 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
463 echo "chain empty failed"
468 function wim_unsupport_menuentry {
469 echo -e "\n The name of the wim file could NOT contain space or non-ascii characters. \n"
470 echo -e "\n Will return to main menu after 10 seconds ...\n"
474 #############################################################
475 #############################################################
476 #############################################################
477 ####### Main Process ###########
478 #############################################################
479 #############################################################
480 #############################################################
482 set VENTOY_VERSION="1.0.12"
484 # Default menu display mode, you can change it as you want.
487 set VTOY_DEFAULT_MENU_MODE=0
492 set VTOY_MEM_DISK_STR="[Memdisk]"
493 set VTOY_ISO_RAW_STR="ISO RAW"
494 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
496 set VTOY_F2_CMD="ventoy_power"
498 if [ "$grub_platform" = "pc" ]; then
499 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION BIOS www.ventoy.net"
501 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION UEFI www.ventoy.net"
504 vt_device $root vtoy_dev
506 if [ "$vtoy_dev" = "tftp" ]; then
507 set vtoy_path=($root)
508 for vtid in 0 1 2 3; do
509 if [ -d (hd$vtid,2)/ventoy ]; then
510 set iso_path=(hd$vtid,1)
511 set vtoy_efi_part=(hd$vtid,2)
516 set vtoy_path=($root)/ventoy
517 set iso_path=($vtoy_dev,1)
518 set vtoy_efi_part=($vtoy_dev,2)
524 if [ -f $iso_path/ventoy/ventoy.json ]; then
525 vt_load_plugin $iso_path
528 if [ -f $iso_path/ventoy/ventoy_wimboot.img ]; then
529 vt_load_wimboot $iso_path/ventoy/ventoy_wimboot.img
530 elif [ -f $vtoy_efi_part/ventoy/ventoy_wimboot.img ]; then
531 vt_load_wimboot $vtoy_efi_part/ventoy/ventoy_wimboot.img
535 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
536 set VTOY_F3_CMD="vt_dynamic_menu 1 1"
537 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:TreeView"
539 set VTOY_F3_CMD="vt_dynamic_menu 1 0"
540 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:ListView"
544 if [ -n "$vtoy_gfxmode" ]; then
545 set gfxmode=$vtoy_gfxmode
547 set gfxmode=1920x1080,1366x768,1024x768
550 if [ -n "$vtoy_theme" ]; then
551 set theme=$vtoy_theme
553 set theme=$prefix/themes/ventoy/theme.txt
556 terminal_output gfxterm
558 #colect all image files (iso files)
559 set ventoy_img_count=0
560 vt_list_img $iso_path ventoy_img_count
563 if [ $ventoy_img_count -gt 0 ]; then
564 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
570 if [ -n "$VTOY_NO_ISO_TIP" ]; then
571 NO_ISO_MENU="No ISO files found, $VTOY_NO_ISO_TIP"
572 elif [ -n "$VTOY_DEFAULT_SEARCH_ROOT" ]; then
573 NO_ISO_MENU="No ISO files found, please check VTOY_DEFAULT_SEARCH_ROOT"
575 NO_ISO_MENU="No ISO files found"
577 menuentry "$NO_ISO_MENU (Press enter to reboot ...)" {
578 echo -e "\n Rebooting ... "