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 get_os_type {
21 for file in "efi/microsoft" "sources/boot.wim" "boot/bcd" "bootmgr.efi" "boot/etfsboot.com"; do
22 if [ -e $1/$file ]; then
28 if [ -n "${vtdebug_flag}" ]; then
33 function locate_initrd {
34 vt_linux_locate_initrd
36 if [ -n "${vtdebug_flag}" ]; then
42 function find_wim_file {
45 for file in "sources/boot.wim" "sources/BOOT.WIM" "Sources/Win10PEx64.WIM" "boot/BOOT.WIM" "winpe_x64.wim"; do
46 if [ -e $1/$file ]; then
47 set ventoy_wim_file=$1/$file
53 function distro_specify_initrd_file {
54 if [ -e (loop)/boot/all.rdz ]; then
55 vt_linux_specify_initrd_file /boot/all.rdz
56 elif [ -e (loop)/boot/xen.gz ]; then
57 if [ -e (loop)/install.img ]; then
58 vt_linux_specify_initrd_file /install.img
60 elif [ -d (loop)/casper ]; then
61 if [ -e (loop)/casper/initrd ]; then
62 vt_linux_specify_initrd_file /casper/initrd
64 if [ -e (loop)/casper/initrd-oem ]; then
65 vt_linux_specify_initrd_file /casper/initrd-oem
67 elif [ -e (loop)/boot/grub/initrd.xz ]; then
68 vt_linux_specify_initrd_file /boot/grub/initrd.xz
69 elif [ -e (loop)/initrd.gz ]; then
70 vt_linux_specify_initrd_file /initrd.gz
71 elif [ -e (loop)/slax/boot/initrfs.img ]; then
72 vt_linux_specify_initrd_file /slax/boot/initrfs.img
73 elif [ -e (loop)/pmagic/initrd.img ]; then
74 vt_linux_specify_initrd_file /pmagic/initrd.img
75 elif [ -e (loop)/boot/initrd.xz ]; then
76 vt_linux_specify_initrd_file /boot/initrd.xz
77 elif [ -f (loop)/boot/initrd ]; then
78 vt_linux_specify_initrd_file /boot/initrd
79 elif [ -f (loop)/boot/x86_64/loader/initrd ]; then
80 vt_linux_specify_initrd_file /boot/x86_64/loader/initrd
81 elif [ -f (loop)/boot/initramfs-x86_64.img ]; then
82 vt_linux_specify_initrd_file /boot/initramfs-x86_64.img
88 function distro_specify_initrd_file_phase2 {
89 if [ -f (loop)/boot/initrd.img ]; then
90 vt_linux_specify_initrd_file /boot/initrd.img
94 function uefi_windows_menu_func {
97 if [ "$ventoy_compatible" = "NO" ]; then
99 if [ -n "$ventoy_wim_file" ]; then
100 vt_windows_locate_wim $ventoy_wim_file
104 vt_windows_chain_data ${1}${chosen_path}
106 if [ -n "${vtdebug_flag}" ]; then
110 if [ -n "$vtoy_chain_mem_addr" ]; then
111 terminal_output console
112 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
115 echo "chain empty failed"
120 function uefi_linux_menu_func {
121 if [ "$ventoy_compatible" = "NO" ]; then
122 vt_load_cpio ${vtoy_path}/ventoy.cpio
124 vt_linux_clear_initrd
126 if [ -d (loop)/pmagic ]; then
127 vt_linux_specify_initrd_file /pmagic/initrd.img
129 for file in "boot/grub/grub.cfg" "EFI/BOOT/grub.cfg" "EFI/boot/grub.cfg" "efi/boot/grub.cfg" "EFI/BOOT/BOOTX64.conf"; do
130 if [ -e (loop)/$file ]; then
131 vt_linux_parse_initrd_grub file (loop)/$file
136 # special process for special distros
137 if [ -d (loop)/loader/entries ]; then
138 set LoadIsoEfiDriver=on
139 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
140 elif [ -d (loop)/boot/grub ]; then
141 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
144 if [ -e (loop)/syslinux/alt0/full.cz ]; then
145 set LoadIsoEfiDriver=on
146 set FirstTryBootFile='@EFI@BOOT@grubx64.efi'
149 distro_specify_initrd_file
151 vt_linux_initrd_count vtcount
152 if [ $vtcount -eq 0 ]; then
153 distro_specify_initrd_file_phase2
155 if [ "$vt_efi_dir" = "NO" ]; then
156 if [ -f (loop)/efi.img ]; then
157 vt_add_replace_file 0 "initrd"
165 vt_linux_chain_data ${1}${chosen_path}
167 if [ -n "$vtoy_chain_mem_addr" ]; then
168 terminal_output console
169 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}
172 echo "chain empty failed"
178 function uefi_iso_menu_func {
180 if [ -d (loop)/ ]; then
184 if [ -n "$vtisouefi" ]; then
185 set LoadIsoEfiDriver=on
187 elif [ -n "$VTOY_ISO_UEFI_DRV" ]; then
188 set LoadIsoEfiDriver=on
190 unset LoadIsoEfiDriver
193 vt_chosen_img_path chosen_path
195 if vt_is_udf ${1}${chosen_path}; then
196 set ventoy_fs_probe=udf
198 set ventoy_fs_probe=iso9660
201 loopback loop ${1}${chosen_path}
204 if [ -d (loop)/EFI ]; then
206 elif [ -d (loop)/efi ]; then
212 if [ -n "$vtcompat" ]; then
213 set ventoy_compatible=YES
215 elif [ -n "$VTOY_ISO_RAW" ]; then
216 set ventoy_compatible=YES
218 vt_check_compatible (loop)
221 vt_img_sector ${1}${chosen_path}
223 if [ "$vtoy_os" = "Windows" ]; then
224 if [ "$ventoy_fs_probe" = "iso9660" ]; then
225 set ventoy_compatible=YES
226 elif [ -f (loop)/HBCD_PE.ini ]; then
227 set ventoy_compatible=YES
230 uefi_windows_menu_func $1
232 uefi_linux_menu_func $1
235 terminal_output gfxterm
238 function uefi_iso_memdisk {
239 vt_chosen_img_path chosen_path
241 echo 'Loading ISO file to memory ...'
242 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
244 terminal_output console
245 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}
252 function legacy_windows_menu_func {
255 if [ "$ventoy_compatible" = "NO" ]; then
257 if [ -n "$ventoy_wim_file" ]; then
258 vt_windows_locate_wim $ventoy_wim_file
259 elif [ -n "${vtdebug_flag}" ]; then
260 echo No wim file found
264 vt_windows_chain_data ${1}${chosen_path}
266 if [ -n "${vtdebug_flag}" ]; then
270 if [ -n "$vtoy_chain_mem_addr" ]; then
271 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft
272 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
275 echo "chain empty failed"
280 function legacy_linux_menu_func {
281 if [ "$ventoy_compatible" = "NO" ]; then
283 vt_load_cpio $vtoy_path/ventoy.cpio
285 vt_linux_clear_initrd
287 if [ -d (loop)/pmagic ]; then
288 vt_linux_specify_initrd_file /pmagic/initrd.img
290 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
291 if [ -d (loop)/$dir ]; then
292 vt_linux_parse_initrd_isolinux (loop)/$dir/
297 # special process for special distros
299 if [ -d (loop)/arch/boot/syslinux ]; then
300 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
301 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
304 elif [ -d (loop)/manjaro ]; then
305 if [ -e (loop)/boot/grub/kernels.cfg ]; then
306 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
308 elif [ -e (loop)/boot/grub/grub.cfg ]; then
309 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
312 distro_specify_initrd_file
314 vt_linux_initrd_count vtcount
315 if [ $vtcount -eq 0 ]; then
316 distro_specify_initrd_file_phase2
322 vt_linux_chain_data ${1}${chosen_path}
324 if [ -n "${vtdebug_flag}" ]; then
328 if [ -n "$vtoy_chain_mem_addr" ]; then
329 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag}
330 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
333 echo "chain empty failed"
338 function legacy_iso_menu_func {
340 if [ -d (loop)/ ]; then
344 vt_chosen_img_path chosen_path
346 if vt_is_udf ${1}${chosen_path}; then
347 set ventoy_fs_probe=udf
349 set ventoy_fs_probe=iso9660
352 loopback loop ${1}${chosen_path}
356 if [ -n "$vtcompat" ]; then
357 set ventoy_compatible=YES
359 elif [ -n "$VTOY_ISO_RAW" ]; then
360 set ventoy_compatible=YES
362 vt_check_compatible (loop)
365 vt_img_sector ${1}${chosen_path}
367 if [ "$vtoy_os" = "Windows" ]; then
368 if [ "$ventoy_fs_probe" = "iso9660" ]; then
369 set ventoy_compatible=YES
370 elif [ -f (loop)/HBCD_PE.ini ]; then
371 set ventoy_compatible=YES
374 legacy_windows_menu_func $1
376 legacy_linux_menu_func $1
380 function legacy_iso_memdisk {
381 vt_chosen_img_path chosen_path
383 linux16 $vtoy_path/memdisk iso raw
384 echo "Loading ISO file to memory ..."
385 initrd16 ${1}${chosen_path}
390 #############################################################
391 #############################################################
392 #############################################################
393 ####### Main Process ###########
394 #############################################################
395 #############################################################
396 #############################################################
398 set VENTOY_VERSION="1.0.07"
403 set VTOY_MEM_DISK_STR="MEMDISK"
404 set VTOY_ISO_RAW_STR="ISO RAW"
405 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
407 vt_device $root vtoy_dev
409 if [ "$vtoy_dev" = "tftp" ]; then
410 set vtoy_path=($root)
411 for vtid in 0 1 2 3; do
412 if [ -d (hd$vtid,2)/ventoy ]; then
413 set iso_path=(hd$vtid,1)
418 set vtoy_path=($root)/ventoy
419 set iso_path=($vtoy_dev,1)
424 if [ -f $iso_path/ventoy/ventoy.json ]; then
425 vt_load_plugin $iso_path
428 if [ -n "$vtoy_gfxmode" ]; then
429 set gfxmode=$vtoy_gfxmode
431 set gfxmode=1920x1080,1366x768,1024x768
434 if [ -n "$vtoy_theme" ]; then
435 set theme=$vtoy_theme
437 set theme=$prefix/themes/ventoy/theme.txt
440 terminal_output gfxterm
442 #colect all image files (iso files)
443 set ventoy_img_count=0
444 vt_list_img $iso_path ventoy_img_count
446 #Dynamic menu for every iso file
447 if vt_cmp $ventoy_img_count ne 0; then
449 while vt_cmp $imgid lt $ventoy_img_count; do
450 vt_img_name $imgid img_name
451 menuentry "$img_name" {
452 if [ "$grub_platform" = "pc" ]; then
453 if [ -n "$VTOY_MEM_DISK" ]; then
454 legacy_iso_memdisk $iso_path
456 legacy_iso_menu_func $iso_path
459 if [ -n "$VTOY_MEM_DISK" ]; then
460 uefi_iso_memdisk $iso_path
462 uefi_iso_menu_func $iso_path
470 menuentry "No ISO files found (Press enter to reboot ...)" {
471 echo -e "\n Rebooting ... "