]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/grub/grub.cfg
1.0.08 release
[Ventoy.git] / INSTALL / grub / grub.cfg
1 #************************************************************************************
2 # Copyright (c) 2020, longpanda <admin@ventoy.net>
3 #
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.
8 #
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.
13 #
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/>.
16 #
17 #************************************************************************************
18
19 function ventoy_power {
20 echo '<1> Reboot'
21 echo '<2> Halt'
22 echo '<0> Return to menu'
23 echo -e '\nPlease enter your choice:'
24
25 unset vtOpt
26 read vtOpt
27
28 if [ "$vtOpt" = "1" ]; then
29 echo -e '\n\nSystem is rebooting ... \n'
30 sleep 1
31 reboot
32 elif [ "$vtOpt" = "2" ]; then
33 echo -e '\n\nSystem is halting ... \n'
34 sleep 1
35 halt
36 fi
37 }
38
39 function get_os_type {
40 set vtoy_os=Linux
41 for file in "efi/microsoft" "sources/boot.wim" "boot/bcd" "bootmgr.efi" "boot/etfsboot.com"; do
42 if [ -e $1/$file ]; then
43 set vtoy_os=Windows
44 break
45 fi
46 done
47
48 if [ -n "${vtdebug_flag}" ]; then
49 echo ISO is $vtoy_os
50 fi
51 }
52
53 function locate_initrd {
54 vt_linux_locate_initrd
55
56 if [ -n "${vtdebug_flag}" ]; then
57 vt_linux_dump_initrd
58 sleep 5
59 fi
60 }
61
62 function find_wim_file {
63 unset ventoy_wim_file
64
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
68 break
69 fi
70 done
71 }
72
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
79 fi
80 elif [ -d (loop)/casper ]; then
81 if [ -e (loop)/casper/initrd ]; then
82 vt_linux_specify_initrd_file /casper/initrd
83 fi
84 if [ -e (loop)/casper/initrd-oem ]; then
85 vt_linux_specify_initrd_file /casper/initrd-oem
86 fi
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
103
104
105 fi
106 }
107
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
113
114 fi
115 }
116
117 function uefi_windows_menu_func {
118 vt_windows_reset
119
120 if [ "$ventoy_compatible" = "NO" ]; then
121 find_wim_file (loop)
122 if [ -n "$ventoy_wim_file" ]; then
123 vt_windows_locate_wim $ventoy_wim_file
124 fi
125 fi
126
127 vt_windows_chain_data ${1}${chosen_path}
128
129 if [ -n "${vtdebug_flag}" ]; then
130 sleep 5
131 fi
132
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}
136 boot
137 else
138 echo "chain empty failed"
139 sleep 5
140 fi
141 }
142
143 function uefi_linux_menu_func {
144 if [ "$ventoy_compatible" = "NO" ]; then
145 vt_load_cpio ${vtoy_path}/ventoy.cpio
146
147 vt_linux_clear_initrd
148
149 if [ -d (loop)/pmagic ]; then
150 vt_linux_specify_initrd_file /pmagic/initrd.img
151 else
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
155 fi
156 done
157 fi
158
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/
165 fi
166
167 if [ -e (loop)/syslinux/alt0/full.cz ]; then
168 set LoadIsoEfiDriver=on
169 set FirstTryBootFile='@EFI@BOOT@grubx64.efi'
170 fi
171
172 distro_specify_initrd_file
173
174 vt_linux_initrd_count vtcount
175 if [ $vtcount -eq 0 ]; then
176 distro_specify_initrd_file_phase2
177
178 if [ "$vt_efi_dir" = "NO" ]; then
179 if [ -f (loop)/efi.img ]; then
180 vt_add_replace_file 0 "initrd"
181 fi
182 fi
183 fi
184
185 locate_initrd
186 fi
187
188 vt_linux_chain_data ${1}${chosen_path}
189
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}
193 boot
194 else
195 echo "chain empty failed"
196 sleep 5
197 fi
198 }
199
200
201 function uefi_iso_menu_func {
202
203 if [ -d (loop)/ ]; then
204 loopback -d loop
205 fi
206
207 if [ -n "$vtisouefi" ]; then
208 set LoadIsoEfiDriver=on
209 unset vtisouefi
210 elif vt_check_mode 2; then
211 set LoadIsoEfiDriver=on
212 else
213 unset LoadIsoEfiDriver
214 fi
215
216 vt_chosen_img_path chosen_path
217
218 if vt_is_udf ${1}${chosen_path}; then
219 set ventoy_fs_probe=udf
220 else
221 set ventoy_fs_probe=iso9660
222 fi
223
224 loopback loop ${1}${chosen_path}
225 get_os_type (loop)
226
227 if [ -d (loop)/EFI ]; then
228 set vt_efi_dir=YES
229 elif [ -d (loop)/efi ]; then
230 set vt_efi_dir=YES
231 else
232 set vt_efi_dir=NO
233 fi
234
235 if [ -n "$vtcompat" ]; then
236 set ventoy_compatible=YES
237 unset vtcompat
238 elif vt_check_mode 1; then
239 set ventoy_compatible=YES
240 else
241 vt_check_compatible (loop)
242 fi
243
244 vt_img_sector ${1}${chosen_path}
245
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
251 fi
252
253 uefi_windows_menu_func $1
254 else
255 uefi_linux_menu_func $1
256 fi
257
258 terminal_output gfxterm
259 }
260
261 function uefi_iso_memdisk {
262 vt_chosen_img_path chosen_path
263
264 echo 'Loading ISO file to memory ...'
265 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
266
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}
269 boot
270 }
271
272
273 function legacy_windows_menu_func {
274 vt_windows_reset
275
276 if [ "$ventoy_compatible" = "NO" ]; then
277 find_wim_file (loop)
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
282 fi
283 fi
284
285 vt_windows_chain_data ${1}${chosen_path}
286
287 if [ -n "${vtdebug_flag}" ]; then
288 sleep 5
289 fi
290
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}
294 boot
295 else
296 echo "chain empty failed"
297 sleep 5
298 fi
299 }
300
301 function legacy_linux_menu_func {
302 if [ "$ventoy_compatible" = "NO" ]; then
303
304 vt_load_cpio $vtoy_path/ventoy.cpio
305
306 vt_linux_clear_initrd
307
308 if [ -d (loop)/pmagic ]; then
309 vt_linux_specify_initrd_file /pmagic/initrd.img
310 else
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/
314 fi
315 done
316 fi
317
318 # special process for special distros
319 #archlinux
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/
323
324 #manjaro
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
328 fi
329 elif [ -e (loop)/boot/grub/grub.cfg ]; then
330 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
331 fi
332
333 distro_specify_initrd_file
334
335 vt_linux_initrd_count vtcount
336 if [ $vtcount -eq 0 ]; then
337 distro_specify_initrd_file_phase2
338 fi
339
340 locate_initrd
341 fi
342
343 vt_linux_chain_data ${1}${chosen_path}
344
345 if [ -n "${vtdebug_flag}" ]; then
346 sleep 5
347 fi
348
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}
352 boot
353 else
354 echo "chain empty failed"
355 sleep 5
356 fi
357 }
358
359 function legacy_iso_menu_func {
360
361 if [ -d (loop)/ ]; then
362 loopback -d loop
363 fi
364
365 vt_chosen_img_path chosen_path
366
367 if vt_is_udf ${1}${chosen_path}; then
368 set ventoy_fs_probe=udf
369 else
370 set ventoy_fs_probe=iso9660
371 fi
372
373 loopback loop ${1}${chosen_path}
374
375 get_os_type (loop)
376
377 if [ -n "$vtcompat" ]; then
378 set ventoy_compatible=YES
379 unset vtcompat
380 elif vt_check_mode 1; then
381 set ventoy_compatible=YES
382 else
383 vt_check_compatible (loop)
384 fi
385
386 vt_img_sector ${1}${chosen_path}
387
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
393 fi
394
395 legacy_windows_menu_func $1
396 else
397 legacy_linux_menu_func $1
398 fi
399 }
400
401 function legacy_iso_memdisk {
402 vt_chosen_img_path chosen_path
403
404 linux16 $vtoy_path/memdisk iso raw
405 echo "Loading ISO file to memory ..."
406 initrd16 ${1}${chosen_path}
407 boot
408 }
409
410 function common_menuentry {
411 if [ "$grub_platform" = "pc" ]; then
412 if vt_check_mode 0; then
413 legacy_iso_memdisk $iso_path
414 else
415 legacy_iso_menu_func $iso_path
416 fi
417 else
418 if vt_check_mode 0; then
419 uefi_iso_memdisk $iso_path
420 else
421 uefi_iso_menu_func $iso_path
422 fi
423 fi
424 }
425
426
427 #############################################################
428 #############################################################
429 #############################################################
430 ####### Main Process ###########
431 #############################################################
432 #############################################################
433 #############################################################
434
435 set VENTOY_VERSION="1.0.08"
436
437 #disable timeout
438 unset timeout
439
440 set VTOY_MEM_DISK_STR="MEMDISK"
441 set VTOY_ISO_RAW_STR="ISO RAW"
442 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
443
444 set VTOY_F2_CMD="ventoy_power"
445 set VTOY_F3_CMD="vt_dynamic_menu tree"
446
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"
450 else
451 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION UEFI www.ventoy.net"
452 fi
453
454 vt_device $root vtoy_dev
455
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)
461 break
462 fi
463 done
464 else
465 set vtoy_path=($root)/ventoy
466 set iso_path=($vtoy_dev,1)
467 fi
468
469 loadfont ascii
470
471 if [ -f $iso_path/ventoy/ventoy.json ]; then
472 vt_load_plugin $iso_path
473 fi
474
475 if [ -n "$vtoy_gfxmode" ]; then
476 set gfxmode=$vtoy_gfxmode
477 else
478 set gfxmode=1920x1080,1366x768,1024x768
479 fi
480
481 if [ -n "$vtoy_theme" ]; then
482 set theme=$vtoy_theme
483 else
484 set theme=$prefix/themes/ventoy/theme.txt
485 fi
486
487 terminal_output gfxterm
488
489 #colect all image files (iso files)
490 set ventoy_img_count=0
491 vt_list_img $iso_path ventoy_img_count
492
493 #Main menu
494 if [ $ventoy_img_count -gt 0 ]; then
495 vt_dynamic_menu
496 else
497 menuentry "No ISO files found (Press enter to reboot ...)" {
498 echo -e "\n Rebooting ... "
499 reboot
500 }
501 fi