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 {
22 echo '<0> Return to menu'
23 echo -e '\nPlease enter your choice:'
28 if [ "$vtOpt" = "1" ]; then
29 echo -e '\n\nSystem is rebooting ... \n'
32 elif [ "$vtOpt" = "2" ]; then
33 echo -e '\n\nSystem is halting ... \n'
39 function get_os_type {
41 for file in "efi/microsoft" "sources/boot.wim" "boot/bcd" "bootmgr.efi" "boot/etfsboot.com"; do
42 if [ -e $1/$file ]; then
48 if [ -n "${vtdebug_flag}" ]; then
53 function locate_initrd {
54 vt_linux_locate_initrd
56 if [ -n "${vtdebug_flag}" ]; then
62 function find_wim_file {
65 for file in "sources/boot.wim" "sources/BOOT.WIM" "Sources/Win10PEx64.WIM" "boot/BOOT.WIM" "winpe_x64.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
108 function distro_specify_initrd_file_phase2 {
109 if [ -f (loop)/boot/initrd.img ]; then
110 vt_linux_specify_initrd_file /boot/initrd.img
111 elif [ -f (loop)/Setup/initrd.gz ]; then
112 vt_linux_specify_initrd_file /Setup/initrd.gz
117 function uefi_windows_menu_func {
120 if [ "$ventoy_compatible" = "NO" ]; then
122 if [ -n "$ventoy_wim_file" ]; then
123 vt_windows_locate_wim $ventoy_wim_file
127 vt_windows_chain_data ${1}${chosen_path}
129 if [ -n "${vtdebug_flag}" ]; then
133 if [ -n "$vtoy_chain_mem_addr" ]; then
134 terminal_output console
135 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
138 echo "chain empty failed"
143 function uefi_linux_menu_func {
144 if [ "$ventoy_compatible" = "NO" ]; then
145 vt_load_cpio ${vtoy_path}/ventoy.cpio
147 vt_linux_clear_initrd
149 if [ -d (loop)/pmagic ]; then
150 vt_linux_specify_initrd_file /pmagic/initrd.img
152 for file in "boot/grub/grub.cfg" "EFI/BOOT/grub.cfg" "EFI/boot/grub.cfg" "efi/boot/grub.cfg" "EFI/BOOT/BOOTX64.conf"; do
153 if [ -e (loop)/$file ]; then
154 vt_linux_parse_initrd_grub file (loop)/$file
159 # special process for special distros
160 if [ -d (loop)/loader/entries ]; then
161 set LoadIsoEfiDriver=on
162 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
163 elif [ -d (loop)/boot/grub ]; then
164 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
167 if [ -e (loop)/syslinux/alt0/full.cz ]; then
168 set LoadIsoEfiDriver=on
169 set FirstTryBootFile='@EFI@BOOT@grubx64.efi'
172 distro_specify_initrd_file
174 vt_linux_initrd_count vtcount
175 if [ $vtcount -eq 0 ]; then
176 distro_specify_initrd_file_phase2
178 if [ "$vt_efi_dir" = "NO" ]; then
179 if [ -f (loop)/efi.img ]; then
180 vt_add_replace_file 0 "initrd"
188 vt_linux_chain_data ${1}${chosen_path}
190 if [ -n "$vtoy_chain_mem_addr" ]; then
191 terminal_output console
192 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}
195 echo "chain empty failed"
201 function uefi_iso_menu_func {
203 if [ -d (loop)/ ]; then
207 if [ -n "$vtisouefi" ]; then
208 set LoadIsoEfiDriver=on
210 elif vt_check_mode 2; then
211 set LoadIsoEfiDriver=on
213 unset LoadIsoEfiDriver
216 vt_chosen_img_path chosen_path
218 if vt_is_udf ${1}${chosen_path}; then
219 set ventoy_fs_probe=udf
221 set ventoy_fs_probe=iso9660
224 loopback loop ${1}${chosen_path}
227 if [ -d (loop)/EFI ]; then
229 elif [ -d (loop)/efi ]; then
235 if [ -n "$vtcompat" ]; then
236 set ventoy_compatible=YES
238 elif vt_check_mode 1; then
239 set ventoy_compatible=YES
241 vt_check_compatible (loop)
244 vt_img_sector ${1}${chosen_path}
246 if [ "$vtoy_os" = "Windows" ]; then
247 if [ "$ventoy_fs_probe" = "iso9660" ]; then
248 set ventoy_compatible=YES
249 elif [ -f (loop)/HBCD_PE.ini ]; then
250 set ventoy_compatible=YES
253 uefi_windows_menu_func $1
255 uefi_linux_menu_func $1
258 terminal_output gfxterm
261 function uefi_iso_memdisk {
262 vt_chosen_img_path chosen_path
264 echo 'Loading ISO file to memory ...'
265 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
267 terminal_output console
268 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}
273 function legacy_windows_menu_func {
276 if [ "$ventoy_compatible" = "NO" ]; then
278 if [ -n "$ventoy_wim_file" ]; then
279 vt_windows_locate_wim $ventoy_wim_file
280 elif [ -n "${vtdebug_flag}" ]; then
281 echo No wim file found
285 vt_windows_chain_data ${1}${chosen_path}
287 if [ -n "${vtdebug_flag}" ]; then
291 if [ -n "$vtoy_chain_mem_addr" ]; then
292 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft
293 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
296 echo "chain empty failed"
301 function legacy_linux_menu_func {
302 if [ "$ventoy_compatible" = "NO" ]; then
304 vt_load_cpio $vtoy_path/ventoy.cpio
306 vt_linux_clear_initrd
308 if [ -d (loop)/pmagic ]; then
309 vt_linux_specify_initrd_file /pmagic/initrd.img
311 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
312 if [ -d (loop)/$dir ]; then
313 vt_linux_parse_initrd_isolinux (loop)/$dir/
318 # special process for special distros
320 if [ -d (loop)/arch/boot/syslinux ]; then
321 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
322 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
325 elif [ -d (loop)/manjaro ]; then
326 if [ -e (loop)/boot/grub/kernels.cfg ]; then
327 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
329 elif [ -e (loop)/boot/grub/grub.cfg ]; then
330 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
333 distro_specify_initrd_file
335 vt_linux_initrd_count vtcount
336 if [ $vtcount -eq 0 ]; then
337 distro_specify_initrd_file_phase2
343 vt_linux_chain_data ${1}${chosen_path}
345 if [ -n "${vtdebug_flag}" ]; then
349 if [ -n "$vtoy_chain_mem_addr" ]; then
350 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag}
351 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
354 echo "chain empty failed"
359 function legacy_iso_menu_func {
361 if [ -d (loop)/ ]; then
365 vt_chosen_img_path chosen_path
367 if vt_is_udf ${1}${chosen_path}; then
368 set ventoy_fs_probe=udf
370 set ventoy_fs_probe=iso9660
373 loopback loop ${1}${chosen_path}
377 if [ -n "$vtcompat" ]; then
378 set ventoy_compatible=YES
380 elif vt_check_mode 1; then
381 set ventoy_compatible=YES
383 vt_check_compatible (loop)
386 vt_img_sector ${1}${chosen_path}
388 if [ "$vtoy_os" = "Windows" ]; then
389 if [ "$ventoy_fs_probe" = "iso9660" ]; then
390 set ventoy_compatible=YES
391 elif [ -f (loop)/HBCD_PE.ini ]; then
392 set ventoy_compatible=YES
395 legacy_windows_menu_func $1
397 legacy_linux_menu_func $1
401 function legacy_iso_memdisk {
402 vt_chosen_img_path chosen_path
404 linux16 $vtoy_path/memdisk iso raw
405 echo "Loading ISO file to memory ..."
406 initrd16 ${1}${chosen_path}
410 function common_menuentry {
411 if [ "$grub_platform" = "pc" ]; then
412 if vt_check_mode 0; then
413 legacy_iso_memdisk $iso_path
415 legacy_iso_menu_func $iso_path
418 if vt_check_mode 0; then
419 uefi_iso_memdisk $iso_path
421 uefi_iso_menu_func $iso_path
427 #############################################################
428 #############################################################
429 #############################################################
430 ####### Main Process ###########
431 #############################################################
432 #############################################################
433 #############################################################
435 set VENTOY_VERSION="1.0.08"
440 set VTOY_MEM_DISK_STR="MEMDISK"
441 set VTOY_ISO_RAW_STR="ISO RAW"
442 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
444 set VTOY_F2_CMD="ventoy_power"
445 set VTOY_F3_CMD="vt_dynamic_menu tree"
447 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:TreeView"
448 if [ "$grub_platform" = "pc" ]; then
449 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION BIOS www.ventoy.net"
451 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION UEFI www.ventoy.net"
454 vt_device $root vtoy_dev
456 if [ "$vtoy_dev" = "tftp" ]; then
457 set vtoy_path=($root)
458 for vtid in 0 1 2 3; do
459 if [ -d (hd$vtid,2)/ventoy ]; then
460 set iso_path=(hd$vtid,1)
465 set vtoy_path=($root)/ventoy
466 set iso_path=($vtoy_dev,1)
471 if [ -f $iso_path/ventoy/ventoy.json ]; then
472 vt_load_plugin $iso_path
475 if [ -n "$vtoy_gfxmode" ]; then
476 set gfxmode=$vtoy_gfxmode
478 set gfxmode=1920x1080,1366x768,1024x768
481 if [ -n "$vtoy_theme" ]; then
482 set theme=$vtoy_theme
484 set theme=$prefix/themes/ventoy/theme.txt
487 terminal_output gfxterm
489 #colect all image files (iso files)
490 set ventoy_img_count=0
491 vt_list_img $iso_path ventoy_img_count
494 if [ $ventoy_img_count -gt 0 ]; then
497 menuentry "No ISO files found (Press enter to reboot ...)" {
498 echo -e "\n Rebooting ... "