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
80 function uefi_windows_menu_func {
83 if [ "$ventoy_compatible" = "NO" ]; then
85 if [ -n "$ventoy_wim_file" ]; then
86 vt_windows_locate_wim $ventoy_wim_file
90 vt_windows_chain_data ${1}${chosen_path}
92 if [ -n "${vtdebug_flag}" ]; then
96 if [ -n "$vtoy_chain_mem_addr" ]; then
97 terminal_output console
98 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
101 echo "chain empty failed"
106 function uefi_linux_menu_func {
107 if [ "$ventoy_compatible" = "NO" ]; then
108 vt_load_cpio ${vtoy_path}/ventoy.cpio
110 vt_linux_clear_initrd
112 if [ -d (loop)/pmagic ]; then
113 vt_linux_specify_initrd_file /pmagic/initrd.img
115 for file in "boot/grub/grub.cfg" "EFI/BOOT/grub.cfg" "EFI/boot/grub.cfg" "efi/boot/grub.cfg" "EFI/BOOT/BOOTX64.conf"; do
116 if [ -e (loop)/$file ]; then
117 vt_linux_parse_initrd_grub file (loop)/$file
122 # special process for special distros
123 if [ -d (loop)/loader/entries ]; then
124 set LoadIsoEfiDriver=on
125 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
126 elif [ -d (loop)/boot/grub ]; then
127 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
130 distro_specify_initrd_file
135 vt_linux_chain_data ${1}${chosen_path}
137 if [ -n "$vtoy_chain_mem_addr" ]; then
138 terminal_output console
139 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
142 echo "chain empty failed"
148 function uefi_iso_menu_func {
150 if [ -d (loop)/ ]; then
154 if [ -n "$vtisouefi" ]; then
155 set LoadIsoEfiDriver=on
157 elif [ -n "$VTOY_ISO_UEFI_DRV" ]; then
158 set LoadIsoEfiDriver=on
160 unset LoadIsoEfiDriver
163 vt_chosen_img_path chosen_path
165 if vt_is_udf ${1}${chosen_path}; then
166 set ventoy_fs_probe=udf
168 set ventoy_fs_probe=iso9660
171 loopback loop ${1}${chosen_path}
174 if [ -n "$vtcompat" ]; then
175 set ventoy_compatible=YES
177 elif [ -n "$VTOY_ISO_RAW" ]; then
178 set ventoy_compatible=YES
180 vt_check_compatible (loop)
183 vt_img_sector ${1}${chosen_path}
185 if [ "$vtoy_os" = "Windows" ]; then
186 if [ "$ventoy_fs_probe" = "iso9660" ]; then
187 set ventoy_compatible=YES
190 uefi_windows_menu_func $1
192 uefi_linux_menu_func $1
195 terminal_output gfxterm
198 function uefi_iso_memdisk {
199 vt_chosen_img_path chosen_path
201 echo 'Loading ISO file to memory ...'
202 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
204 terminal_output console
205 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}
213 function legacy_windows_menu_func {
216 if [ "$ventoy_compatible" = "NO" ]; then
218 if [ -n "$ventoy_wim_file" ]; then
219 vt_windows_locate_wim $ventoy_wim_file
220 elif [ -n "${vtdebug_flag}" ]; then
221 echo No wim file found
225 vt_windows_chain_data ${1}${chosen_path}
227 if [ -n "${vtdebug_flag}" ]; then
231 if [ -n "$vtoy_chain_mem_addr" ]; then
232 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft
233 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
236 echo "chain empty failed"
241 function legacy_linux_menu_func {
242 if [ "$ventoy_compatible" = "NO" ]; then
244 vt_load_cpio $vtoy_path/ventoy.cpio
246 vt_linux_clear_initrd
248 if [ -d (loop)/pmagic ]; then
249 vt_linux_specify_initrd_file /pmagic/initrd.img
251 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
252 if [ -d (loop)/$dir ]; then
253 vt_linux_parse_initrd_isolinux (loop)/$dir/
258 # special process for special distros
260 if [ -d (loop)/arch/boot/syslinux ]; then
261 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
262 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
265 elif [ -d (loop)/manjaro ]; then
266 if [ -e (loop)/boot/grub/kernels.cfg ]; then
267 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
269 elif [ -e (loop)/boot/grub/grub.cfg ]; then
270 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
273 distro_specify_initrd_file
278 vt_linux_chain_data ${1}${chosen_path}
280 if [ -n "${vtdebug_flag}" ]; then
284 if [ -n "$vtoy_chain_mem_addr" ]; then
285 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag}
286 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
289 echo "chain empty failed"
294 function legacy_iso_menu_func {
296 if [ -d (loop)/ ]; then
300 vt_chosen_img_path chosen_path
302 if vt_is_udf ${1}${chosen_path}; then
303 set ventoy_fs_probe=udf
305 set ventoy_fs_probe=iso9660
308 loopback loop ${1}${chosen_path}
312 if [ -n "$vtcompat" ]; then
313 set ventoy_compatible=YES
315 elif [ -n "$VTOY_ISO_RAW" ]; then
316 set ventoy_compatible=YES
318 vt_check_compatible (loop)
321 vt_img_sector ${1}${chosen_path}
323 if [ "$vtoy_os" = "Windows" ]; then
324 if [ "$ventoy_fs_probe" = "iso9660" ]; then
325 set ventoy_compatible=YES
327 legacy_windows_menu_func $1
329 legacy_linux_menu_func $1
333 function legacy_iso_memdisk {
334 vt_chosen_img_path chosen_path
336 linux16 $vtoy_path/memdisk iso raw
337 echo "Loading ISO file to memory ..."
338 initrd16 ${1}${chosen_path}
343 #############################################################
344 #############################################################
345 #############################################################
346 ####### Main Process ###########
347 #############################################################
348 #############################################################
349 #############################################################
351 set VENTOY_VERSION="1.0.05"
356 set VTOY_MEM_DISK_STR="MEMDISK"
357 set VTOY_ISO_RAW_STR="ISO RAW"
358 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
360 vt_device $root vtoy_dev
362 if [ "$vtoy_dev" = "tftp" ]; then
363 set vtoy_path=($root)
364 for vtid in 0 1 2 3; do
365 if [ -d (hd$vtid,2)/ventoy ]; then
366 set iso_path=(hd$vtid,1)
371 set vtoy_path=($root)/ventoy
372 set iso_path=($vtoy_dev,1)
377 if [ -f $iso_path/ventoy/ventoy.json ]; then
378 vt_load_plugin $iso_path
381 terminal_output gfxterm
383 if [ -n "$vtoy_theme" ]; then
384 set theme=$vtoy_theme
386 set theme=$prefix/themes/ventoy/theme.txt
389 if [ -n "$vtoy_gfxmode" ]; then
390 set gfxmode=$vtoy_gfxmode
395 #colect all image files (iso files)
396 set ventoy_img_count=0
397 vt_list_img $iso_path ventoy_img_count
399 #Dynamic menu for every iso file
400 if vt_cmp $ventoy_img_count ne 0; then
402 while vt_cmp $imgid lt $ventoy_img_count; do
403 vt_img_name $imgid img_name
404 menuentry "$img_name" {
405 if [ "$grub_platform" = "pc" ]; then
406 if [ -n "$VTOY_MEM_DISK" ]; then
407 legacy_iso_memdisk $iso_path
409 legacy_iso_menu_func $iso_path
412 if [ -n "$VTOY_MEM_DISK" ]; then
413 uefi_iso_memdisk $iso_path
415 uefi_iso_menu_func $iso_path
423 menuentry "No ISO files found (Press enter to reboot ...)" {
424 echo -e "\n Rebooting ... "